]> pd.if.org Git - pdclib/blob - platform/posix/functions/_PDCLIB/_PDCLIB_allocpages.c
PDCLib includes with quotes, not <>.
[pdclib] / platform / posix / functions / _PDCLIB / _PDCLIB_allocpages.c
1 /* _PDCLIB_allocpages( int const )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 /* This is an example implementation of _PDCLIB_allocpages() (declared in
8    _PDCLIB_config.h), fit for use with POSIX kernels.
9 */
10
11 #ifndef REGTEST
12 #ifdef __linux__
13 #define _GNU_SOURCE
14 #endif
15 #include <stdint.h>
16 #include <stddef.h>
17 #include <sys/mman.h>
18 #include "_PDCLIB_glue.h"
19
20 #ifndef MAP_ANON
21 #define MAP_ANON MAP_ANOYNMOUS
22 #endif
23
24 void * _PDCLIB_allocpages( size_t n )
25 {
26     void *addr = mmap(
27         NULL, n * _PDCLIB_MALLOC_PAGESIZE, PROT_READ | PROT_WRITE,
28         MAP_ANON | MAP_PRIVATE, -1, 0);
29
30     if(addr != MAP_FAILED) {
31         return addr;
32     } else {
33         return NULL;
34     }
35 }
36
37 #endif
38
39 #ifdef TEST
40 #include "_PDCLIB_test.h"
41
42 int main( void )
43 {
44     return TEST_RESULTS;
45 }
46
47 #endif