5 /* _PDCLIB_allocpages( int const )
7 This file is part of the Public Domain C Library (PDCLib).
8 Permission is granted to use, modify, and / or redistribute at will.
11 /* This is an example implementation of _PDCLIB_allocpages() (declared in
12 _PDCLIB_config.h), fit for use with POSIX kernels.
19 #ifndef _PDCLIB_CONFIG_H
20 #define _PDCLIB_CONFIG_H _PDCLIB_CONFIG_H
21 #include <_PDCLIB_config.h>
24 static void * membreak = NULL;
26 void * _PDCLIB_allocpages( int const n )
28 if ( membreak == NULL )
30 /* first call, make sure end-of-heap is page-aligned */
31 intptr_t unaligned = 0;
33 unaligned = _PDCLIB_PAGESIZE - (intptr_t)membreak % _PDCLIB_PAGESIZE;
34 if ( unaligned < _PDCLIB_PAGESIZE )
36 /* end-of-heap not page-aligned - adjust */
37 if ( sbrk( unaligned ) != membreak )
42 membreak += unaligned;
45 /* increasing or decreasing heap - standard operation */
46 void * oldbreak = membreak;
47 membreak = (void *)( (char *)membreak + ( n * _PDCLIB_PAGESIZE ) );
48 if ( brk( membreak ) == 0 )
62 #include <_PDCLIB_test.h>
64 int puts( const char * );
71 void * startbreak = sbrk( 0 );
72 TESTCASE( _PDCLIB_allocpages( 0 ) );
73 TESTCASE( ( sbrk( 0 ) - startbreak ) <= _PDCLIB_PAGESIZE );
74 startbreak = sbrk( 0 );
75 TESTCASE( _PDCLIB_allocpages( 1 ) );
76 TESTCASE( sbrk( 0 ) == startbreak + ( 1 * _PDCLIB_PAGESIZE ) );
77 TESTCASE( _PDCLIB_allocpages( 5 ) );
78 TESTCASE( sbrk( 0 ) == startbreak + ( 6 * _PDCLIB_PAGESIZE ) );
79 TESTCASE( _PDCLIB_allocpages( -3 ) );
80 TESTCASE( sbrk( 0 ) == startbreak + ( 3 * _PDCLIB_PAGESIZE ) );