3 /* _PDCLIB_allocpages( int const )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
9 /* This is an example implementation of _PDCLIB_allocpages() (declared in
10 _PDCLIB_config.h), fit for use with POSIX kernels.
16 void * sbrk( intptr_t );
18 #ifndef _PDCLIB_GLUE_H
19 #define _PDCLIB_GLUE_H _PDCLIB_GLUE_H
20 #include <_PDCLIB_glue.h>
23 static void * membreak = NULL;
25 void * _PDCLIB_allocpages( int const n )
27 if ( membreak == NULL )
29 /* first call, make sure end-of-heap is page-aligned */
30 intptr_t unaligned = 0;
32 unaligned = _PDCLIB_PAGESIZE - (intptr_t)membreak % _PDCLIB_PAGESIZE;
33 if ( unaligned < _PDCLIB_PAGESIZE )
35 /* end-of-heap not page-aligned - adjust */
36 if ( sbrk( unaligned ) != membreak )
41 membreak = (char *)membreak + unaligned;
44 /* increasing or decreasing heap - standard operation */
45 void * oldbreak = membreak;
46 membreak = (void *)( (char *)membreak + ( n * _PDCLIB_PAGESIZE ) );
47 if ( sbrk( (char*)membreak - (char*)oldbreak ) == membreak )
61 #include <_PDCLIB_test.h>
65 char * startbreak = sbrk( 0 );
66 TESTCASE( _PDCLIB_allocpages( 0 ) );
67 TESTCASE( ( (char *)sbrk( 0 ) - startbreak ) <= _PDCLIB_PAGESIZE );
68 startbreak = sbrk( 0 );
69 TESTCASE( _PDCLIB_allocpages( 1 ) );
70 TESTCASE( sbrk( 0 ) == startbreak + ( 1 * _PDCLIB_PAGESIZE ) );
71 TESTCASE( _PDCLIB_allocpages( 5 ) );
72 TESTCASE( sbrk( 0 ) == startbreak + ( 6 * _PDCLIB_PAGESIZE ) );
73 TESTCASE( _PDCLIB_allocpages( -3 ) );
74 TESTCASE( sbrk( 0 ) == startbreak + ( 3 * _PDCLIB_PAGESIZE ) );