This source file includes following definitions.
- new_memnode
- get_memnode
- pagealign_alloc
- pagealign_xalloc
- pagealign_free
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <config.h>
21
22 #include "pagealign_alloc.h"
23
24 #include <errno.h>
25 #include <stdlib.h>
26
27 #include <fcntl.h>
28 #include <unistd.h>
29
30 #if HAVE_MMAP
31 # include <sys/mman.h>
32 #endif
33
34 #include "error.h"
35 #include "xalloc.h"
36 #include "gettext.h"
37
38 #define _(str) gettext (str)
39
40 #if HAVE_MMAP
41
42 # ifndef MAP_FILE
43 # define MAP_FILE 0
44 # endif
45
46 # ifndef MAP_FAILED
47 # define MAP_FAILED ((void *)-1)
48 # endif
49 #endif
50
51
52 #if HAVE_MMAP || ! HAVE_POSIX_MEMALIGN
53
54 # if HAVE_MMAP
55
56 typedef size_t info_t;
57 # else
58
59
60 typedef void * info_t;
61 # endif
62
63
64
65 typedef struct memnode_s memnode_t;
66 struct memnode_s
67 {
68 void *aligned_ptr;
69 info_t info;
70 memnode_t *next;
71 };
72
73
74 static memnode_t *memnode_table = NULL;
75
76
77 static void
78 new_memnode (void *aligned_ptr, info_t info)
79 {
80 memnode_t *new_node = XMALLOC (memnode_t);
81 new_node->aligned_ptr = aligned_ptr;
82 new_node->info = info;
83 new_node->next = memnode_table;
84 memnode_table = new_node;
85 }
86
87
88
89
90 static info_t
91 get_memnode (void *aligned_ptr)
92 {
93 info_t ret;
94 memnode_t *c;
95 memnode_t **p_next = &memnode_table;
96
97 for (c = *p_next; c != NULL; p_next = &c->next, c = c->next)
98 if (c->aligned_ptr == aligned_ptr)
99 break;
100
101 if (c == NULL)
102
103
104 abort ();
105
106
107 *p_next = c->next;
108 ret = c->info;
109 free (c);
110
111 return ret;
112 }
113
114 #endif
115
116
117 void *
118 pagealign_alloc (size_t size)
119 {
120 void *ret;
121
122
123
124 #if HAVE_MMAP
125 # ifdef HAVE_MAP_ANONYMOUS
126 const int fd = -1;
127 const int flags = MAP_ANONYMOUS | MAP_PRIVATE;
128 # else
129 static int fd = -1;
130
131
132 const int flags = MAP_FILE | MAP_PRIVATE;
133 if (fd == -1)
134 {
135 fd = open ("/dev/zero", O_RDONLY | O_CLOEXEC, 0666);
136 if (fd < 0)
137 error (EXIT_FAILURE, errno, _("Failed to open /dev/zero for read"));
138 }
139 # endif
140 ret = mmap (NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
141 if (ret == MAP_FAILED)
142 return NULL;
143 new_memnode (ret, size);
144 #elif HAVE_POSIX_MEMALIGN
145 int status = posix_memalign (&ret, getpagesize (), size);
146 if (status)
147 {
148 errno = status;
149 return NULL;
150 }
151 #else
152 size_t pagesize = getpagesize ();
153 void *unaligned_ptr = malloc (size + pagesize - 1);
154 if (unaligned_ptr == NULL)
155 {
156
157
158 errno = ENOMEM;
159 return NULL;
160 }
161 ret = (char *) unaligned_ptr
162 + ((- (unsigned long) unaligned_ptr) & (pagesize - 1));
163 new_memnode (ret, unaligned_ptr);
164 #endif
165 return ret;
166 }
167
168
169 void *
170 pagealign_xalloc (size_t size)
171 {
172 void *ret;
173
174 ret = pagealign_alloc (size);
175 if (ret == NULL)
176 xalloc_die ();
177 return ret;
178 }
179
180
181 void
182 pagealign_free (void *aligned_ptr)
183 {
184 #if HAVE_MMAP
185 if (munmap (aligned_ptr, get_memnode (aligned_ptr)) < 0)
186 error (EXIT_FAILURE, errno, "Failed to unmap memory");
187 #elif HAVE_POSIX_MEMALIGN
188 free (aligned_ptr);
189 #else
190 free (get_memnode (aligned_ptr));
191 #endif
192 }