1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "tests/sys_mman.h"
4
main(int argc,char ** argv)5 int main(int argc, char **argv)
6 {
7 void *p1;
8 void *p2;
9
10 if ( ( p1 = mmap( 0, 4096, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0 ) ) == MAP_FAILED )
11 {
12 perror( "aligned mmap failed" );
13 exit( 1 );
14 }
15
16 if ( munmap( p1, 4096 ) != 0 )
17 {
18 perror( "aligned munmap failed" );
19 exit( 1 );
20 }
21
22 // This fails because MAP_FIXED is specified and p1+1 isn't page-aligned.
23 // (On Linux, just non-page-alignment is enough for failure, but on Darwin
24 // MAP_FIXED has to also be specified.)
25 if ( ( p2 = mmap( p1 + 1, 4096, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0 ) ) == MAP_FAILED )
26 {
27 perror( "unaligned mmap failed" );
28 exit( 1 );
29 }
30
31 if ( munmap( p2, 4096 ) != 0 )
32 {
33 perror( "unaligned munmap failed" );
34 exit( 1 );
35 }
36
37 exit( 0 );
38 }
39