1 /* _PDCLIB_allocpages( int const )
3 This file is part of the Public Domain C Library (PDCLib).
4 Permission is granted to use, modify, and / or redistribute at will.
7 /* This is an example implementation of _PDCLIB_allocpages() (declared in
8 _PDCLIB_config.h), fit for use with POSIX kernels.
15 void * sbrk( intptr_t );
17 #include <_PDCLIB_glue.h>
19 static void * membreak = NULL;
21 void * _PDCLIB_allocpages( int const n )
23 if ( membreak == NULL )
25 /* first call, make sure end-of-heap is page-aligned */
26 intptr_t unaligned = 0;
28 unaligned = _PDCLIB_PAGESIZE - (intptr_t)membreak % _PDCLIB_PAGESIZE;
29 if ( unaligned < _PDCLIB_PAGESIZE )
31 /* end-of-heap not page-aligned - adjust */
32 if ( sbrk( unaligned ) != membreak )
37 membreak = (char *)membreak + unaligned;
40 /* increasing or decreasing heap - standard operation */
41 void * oldbreak = membreak;
42 membreak = (void *)( (char *)membreak + ( n * _PDCLIB_PAGESIZE ) );
43 if ( brk( membreak ) == 0 )
57 #include <_PDCLIB_test.h>
63 char * startbreak = sbrk( 0 );
64 TESTCASE( _PDCLIB_allocpages( 0 ) );
65 TESTCASE( ( (char *)sbrk( 0 ) - startbreak ) <= _PDCLIB_PAGESIZE );
66 startbreak = sbrk( 0 );
67 TESTCASE( _PDCLIB_allocpages( 1 ) );
68 TESTCASE( sbrk( 0 ) == startbreak + ( 1 * _PDCLIB_PAGESIZE ) );
69 TESTCASE( _PDCLIB_allocpages( 5 ) );
70 TESTCASE( sbrk( 0 ) == startbreak + ( 6 * _PDCLIB_PAGESIZE ) );
71 TESTCASE( _PDCLIB_allocpages( -3 ) );
72 TESTCASE( sbrk( 0 ) == startbreak + ( 3 * _PDCLIB_PAGESIZE ) );