This source file includes following definitions.
- mmap_zeromap
- munmap
- mprotect
- mmap_zeromap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #if defined _WIN32 && !defined __CYGWIN__
20
21
22
23 # define WIN32_LEAN_AND_MEAN
24 # include <windows.h>
25 # include <winerror.h>
26 # define PROT_NONE PAGE_NOACCESS
27 # define PROT_READ PAGE_READONLY
28 # define PROT_READ_WRITE PAGE_READWRITE
29
30 static void *
31 mmap_zeromap (void *map_addr_hint, size_t map_len)
32 {
33 if (VirtualAlloc ((void *)((uintptr_t) map_addr_hint & -0x10000),
34 (((uintptr_t) map_addr_hint + map_len - 1) | 0xffff) + 1
35 - ((uintptr_t) map_addr_hint & -0x10000),
36 MEM_RESERVE, PAGE_NOACCESS)
37 && VirtualAlloc (map_addr_hint, map_len, MEM_COMMIT, PAGE_READWRITE))
38 return map_addr_hint;
39 else
40 return (void *)(-1);
41 }
42
43 int
44 munmap (void *addr, size_t len)
45 {
46 if (VirtualFree (addr, len, MEM_DECOMMIT))
47 return 0;
48 else
49 return -1;
50 }
51
52 int
53 mprotect (void *addr, size_t len, int prot)
54 {
55 DWORD oldprot;
56
57 if (VirtualProtect (addr, len, prot, &oldprot))
58 return 0;
59 else
60 return -1;
61 }
62
63 #else
64
65
66
67 # include <sys/types.h>
68 # include <sys/mman.h>
69 # include <fcntl.h>
70
71 # ifndef PROT_NONE
72 # define PROT_NONE 0
73 # endif
74 # define PROT_READ_WRITE (PROT_READ|PROT_WRITE)
75
76 # if HAVE_MAP_ANONYMOUS
77 # define zero_fd -1
78 # define map_flags MAP_ANONYMOUS | MAP_PRIVATE
79 # else
80 # ifndef MAP_FILE
81 # define MAP_FILE 0
82 # endif
83 static int zero_fd;
84 # define map_flags MAP_FILE | MAP_PRIVATE
85 # endif
86
87 static void *
88 mmap_zeromap (void *map_addr_hint, size_t map_len)
89 {
90 # ifdef __hpux
91
92
93 return (void *) mmap ((void *) 0, map_len, PROT_READ_WRITE, map_flags, zero_fd, 0);
94 # else
95 return (void *) mmap (map_addr_hint, map_len, PROT_READ_WRITE, map_flags, zero_fd, 0);
96 # endif
97 }
98
99 #endif