• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define _GNU_SOURCE 1
2 #include <sys/mman.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 
main(void)8 int main(void)
9 {
10    /* first  find free segment of 40K, then unmap it */
11    void *initial_area = mmap((void *)0x10000000, 40960, PROT_READ|PROT_WRITE,
12                              MAP_ANONYMOUS|MAP_PRIVATE,0,0);
13 
14    if (initial_area == MAP_FAILED)
15       perror ("initial area");
16    printf("initial_area= %p\n", initial_area);
17    if (munmap(initial_area, 40960) != 0)
18       perror ("munmap initial_area");
19 
20    /* remap the same segment, but with 4K size */
21    void *area = mmap(initial_area, 4096, PROT_READ|PROT_WRITE,
22                      MAP_ANONYMOUS|MAP_PRIVATE,0,0);
23    if (area == MAP_FAILED)
24       perror ("area");
25    if (area != initial_area)
26       printf("FAILED : was expecting to get back the initial_area\n");
27    printf("area= %p\n", area);
28    strcpy(area, "Hello World");
29 
30    /* extend it to 40K */
31    void *a2 = mremap(area, 4096, 40960, 0);
32    if (a2 == MAP_FAILED) {
33       perror("mremap");
34    }
35    if (a2 != initial_area)
36       printf("FAILED : was expecting to get back the same area increased\n");
37    printf("increased area= %p\n", a2);
38    printf("%s\n", (char *)a2);
39    return 0;
40 }
41