1*86d7f5d3SJohn Marino /* Memory allocation aligned to system page boundaries.
2*86d7f5d3SJohn Marino
3*86d7f5d3SJohn Marino Copyright (C) 2005 Free Software Foundation, Inc.
4*86d7f5d3SJohn Marino
5*86d7f5d3SJohn Marino This program is free software; you can redistribute it and/or modify it
6*86d7f5d3SJohn Marino under the terms of the GNU General Public License as published
7*86d7f5d3SJohn Marino by the Free Software Foundation; either version 2, or (at your option)
8*86d7f5d3SJohn Marino any later version.
9*86d7f5d3SJohn Marino
10*86d7f5d3SJohn Marino This program is distributed in the hope that it will be useful,
11*86d7f5d3SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
12*86d7f5d3SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13*86d7f5d3SJohn Marino General Public License for more details.
14*86d7f5d3SJohn Marino
15*86d7f5d3SJohn Marino You should have received a copy of the GNU General Public
16*86d7f5d3SJohn Marino License along with this program; if not, write to the Free Software
17*86d7f5d3SJohn Marino Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18*86d7f5d3SJohn Marino USA. */
19*86d7f5d3SJohn Marino
20*86d7f5d3SJohn Marino /* Written by Derek R. Price <derek@ximbiot.com>. */
21*86d7f5d3SJohn Marino
22*86d7f5d3SJohn Marino #ifdef HAVE_CONFIG_H
23*86d7f5d3SJohn Marino # include <config.h>
24*86d7f5d3SJohn Marino #endif
25*86d7f5d3SJohn Marino
26*86d7f5d3SJohn Marino #include "pagealign_alloc.h"
27*86d7f5d3SJohn Marino
28*86d7f5d3SJohn Marino #include <errno.h>
29*86d7f5d3SJohn Marino #include <stdlib.h>
30*86d7f5d3SJohn Marino
31*86d7f5d3SJohn Marino #include <fcntl.h>
32*86d7f5d3SJohn Marino
33*86d7f5d3SJohn Marino #if HAVE_UNISTD_H
34*86d7f5d3SJohn Marino # include <unistd.h>
35*86d7f5d3SJohn Marino #endif
36*86d7f5d3SJohn Marino
37*86d7f5d3SJohn Marino #if HAVE_MMAP
38*86d7f5d3SJohn Marino # include <sys/mman.h>
39*86d7f5d3SJohn Marino #endif
40*86d7f5d3SJohn Marino
41*86d7f5d3SJohn Marino #include "error.h"
42*86d7f5d3SJohn Marino #include "exit.h"
43*86d7f5d3SJohn Marino #include "getpagesize.h"
44*86d7f5d3SJohn Marino #include "xalloc.h"
45*86d7f5d3SJohn Marino #include "gettext.h"
46*86d7f5d3SJohn Marino
47*86d7f5d3SJohn Marino #define _(str) gettext (str)
48*86d7f5d3SJohn Marino
49*86d7f5d3SJohn Marino #if HAVE_MMAP
50*86d7f5d3SJohn Marino /* Define MAP_FILE when it isn't otherwise. */
51*86d7f5d3SJohn Marino # ifndef MAP_FILE
52*86d7f5d3SJohn Marino # define MAP_FILE 0
53*86d7f5d3SJohn Marino # endif
54*86d7f5d3SJohn Marino /* Define MAP_FAILED for old systems which neglect to. */
55*86d7f5d3SJohn Marino # ifndef MAP_FAILED
56*86d7f5d3SJohn Marino # define MAP_FAILED ((void *)-1)
57*86d7f5d3SJohn Marino # endif
58*86d7f5d3SJohn Marino #endif
59*86d7f5d3SJohn Marino
60*86d7f5d3SJohn Marino
61*86d7f5d3SJohn Marino #if HAVE_MMAP || ! HAVE_POSIX_MEMALIGN
62*86d7f5d3SJohn Marino
63*86d7f5d3SJohn Marino # if HAVE_MMAP
64*86d7f5d3SJohn Marino /* For each memory region, we store its size. */
65*86d7f5d3SJohn Marino typedef size_t info_t;
66*86d7f5d3SJohn Marino # else
67*86d7f5d3SJohn Marino /* For each memory region, we store the original pointer returned by
68*86d7f5d3SJohn Marino malloc(). */
69*86d7f5d3SJohn Marino typedef void * info_t;
70*86d7f5d3SJohn Marino # endif
71*86d7f5d3SJohn Marino
72*86d7f5d3SJohn Marino /* A simple linked list of allocated memory regions. It is probably not the
73*86d7f5d3SJohn Marino most efficient way to store these, but anyway... */
74*86d7f5d3SJohn Marino typedef struct memnode_s memnode_t;
75*86d7f5d3SJohn Marino struct memnode_s
76*86d7f5d3SJohn Marino {
77*86d7f5d3SJohn Marino void *aligned_ptr;
78*86d7f5d3SJohn Marino info_t info;
79*86d7f5d3SJohn Marino memnode_t *next;
80*86d7f5d3SJohn Marino };
81*86d7f5d3SJohn Marino
82*86d7f5d3SJohn Marino /* The list of currently allocated memory regions. */
83*86d7f5d3SJohn Marino static memnode_t *memnode_table = NULL;
84*86d7f5d3SJohn Marino
85*86d7f5d3SJohn Marino
86*86d7f5d3SJohn Marino static void
new_memnode(void * aligned_ptr,info_t info)87*86d7f5d3SJohn Marino new_memnode (void *aligned_ptr, info_t info)
88*86d7f5d3SJohn Marino {
89*86d7f5d3SJohn Marino memnode_t *new_node = (memnode_t *) xmalloc (sizeof (memnode_t));
90*86d7f5d3SJohn Marino new_node->aligned_ptr = aligned_ptr;
91*86d7f5d3SJohn Marino new_node->info = info;
92*86d7f5d3SJohn Marino new_node->next = memnode_table;
93*86d7f5d3SJohn Marino memnode_table = new_node;
94*86d7f5d3SJohn Marino }
95*86d7f5d3SJohn Marino
96*86d7f5d3SJohn Marino
97*86d7f5d3SJohn Marino /* Dispose of the memnode containing a map for the ALIGNED_PTR in question
98*86d7f5d3SJohn Marino and return the content of the node's INFO field. */
99*86d7f5d3SJohn Marino static info_t
get_memnode(void * aligned_ptr)100*86d7f5d3SJohn Marino get_memnode (void *aligned_ptr)
101*86d7f5d3SJohn Marino {
102*86d7f5d3SJohn Marino info_t ret;
103*86d7f5d3SJohn Marino memnode_t *c;
104*86d7f5d3SJohn Marino memnode_t **p_next = &memnode_table;
105*86d7f5d3SJohn Marino
106*86d7f5d3SJohn Marino for (c = *p_next; c != NULL; p_next = &c->next, c = c->next)
107*86d7f5d3SJohn Marino if (c->aligned_ptr == aligned_ptr)
108*86d7f5d3SJohn Marino break;
109*86d7f5d3SJohn Marino
110*86d7f5d3SJohn Marino if (c == NULL)
111*86d7f5d3SJohn Marino /* An attempt to free untracked memory. A wrong pointer was passed
112*86d7f5d3SJohn Marino to pagealign_free(). */
113*86d7f5d3SJohn Marino abort ();
114*86d7f5d3SJohn Marino
115*86d7f5d3SJohn Marino /* Remove this entry from the list, save the return value, and free it. */
116*86d7f5d3SJohn Marino *p_next = c->next;
117*86d7f5d3SJohn Marino ret = c->info;
118*86d7f5d3SJohn Marino free (c);
119*86d7f5d3SJohn Marino
120*86d7f5d3SJohn Marino return ret;
121*86d7f5d3SJohn Marino }
122*86d7f5d3SJohn Marino
123*86d7f5d3SJohn Marino #endif /* HAVE_MMAP || !HAVE_POSIX_MEMALIGN */
124*86d7f5d3SJohn Marino
125*86d7f5d3SJohn Marino
126*86d7f5d3SJohn Marino void *
pagealign_alloc(size_t size)127*86d7f5d3SJohn Marino pagealign_alloc (size_t size)
128*86d7f5d3SJohn Marino {
129*86d7f5d3SJohn Marino void *ret;
130*86d7f5d3SJohn Marino #if HAVE_MMAP
131*86d7f5d3SJohn Marino # ifdef HAVE_MAP_ANONYMOUS
132*86d7f5d3SJohn Marino const int fd = -1;
133*86d7f5d3SJohn Marino const int flags = MAP_ANONYMOUS | MAP_PRIVATE;
134*86d7f5d3SJohn Marino # else /* !HAVE_MAP_ANONYMOUS */
135*86d7f5d3SJohn Marino static int fd = -1; /* Only open /dev/zero once in order to avoid limiting
136*86d7f5d3SJohn Marino the amount of memory we may allocate based on the
137*86d7f5d3SJohn Marino number of open file descriptors. */
138*86d7f5d3SJohn Marino const int flags = MAP_FILE | MAP_PRIVATE;
139*86d7f5d3SJohn Marino if (fd == -1)
140*86d7f5d3SJohn Marino {
141*86d7f5d3SJohn Marino fd = open ("/dev/zero", O_RDONLY, 0666);
142*86d7f5d3SJohn Marino if (fd < 0)
143*86d7f5d3SJohn Marino error (EXIT_FAILURE, errno, _("Failed to open /dev/zero for read"));
144*86d7f5d3SJohn Marino }
145*86d7f5d3SJohn Marino # endif /* HAVE_MAP_ANONYMOUS */
146*86d7f5d3SJohn Marino ret = mmap (NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
147*86d7f5d3SJohn Marino if (ret == MAP_FAILED)
148*86d7f5d3SJohn Marino return NULL;
149*86d7f5d3SJohn Marino new_memnode (ret, size);
150*86d7f5d3SJohn Marino #elif HAVE_POSIX_MEMALIGN
151*86d7f5d3SJohn Marino int status = posix_memalign (&ret, getpagesize (), size);
152*86d7f5d3SJohn Marino if (status)
153*86d7f5d3SJohn Marino {
154*86d7f5d3SJohn Marino errno = status;
155*86d7f5d3SJohn Marino return NULL;
156*86d7f5d3SJohn Marino }
157*86d7f5d3SJohn Marino #else /* !HAVE_MMAP && !HAVE_POSIX_MEMALIGN */
158*86d7f5d3SJohn Marino size_t pagesize = getpagesize ();
159*86d7f5d3SJohn Marino void *unaligned_ptr = malloc (size + pagesize - 1);
160*86d7f5d3SJohn Marino if (unaligned_ptr == NULL)
161*86d7f5d3SJohn Marino {
162*86d7f5d3SJohn Marino /* Set errno. We don't know whether malloc already set errno: some
163*86d7f5d3SJohn Marino implementations of malloc do, some don't. */
164*86d7f5d3SJohn Marino errno = ENOMEM;
165*86d7f5d3SJohn Marino return NULL;
166*86d7f5d3SJohn Marino }
167*86d7f5d3SJohn Marino ret = (char *) unaligned_ptr
168*86d7f5d3SJohn Marino + ((- (unsigned long) unaligned_ptr) & (pagesize - 1));
169*86d7f5d3SJohn Marino new_memnode (ret, unaligned_ptr);
170*86d7f5d3SJohn Marino #endif /* HAVE_MMAP && HAVE_POSIX_MEMALIGN */
171*86d7f5d3SJohn Marino return ret;
172*86d7f5d3SJohn Marino }
173*86d7f5d3SJohn Marino
174*86d7f5d3SJohn Marino
175*86d7f5d3SJohn Marino void *
pagealign_xalloc(size_t size)176*86d7f5d3SJohn Marino pagealign_xalloc (size_t size)
177*86d7f5d3SJohn Marino {
178*86d7f5d3SJohn Marino void *ret;
179*86d7f5d3SJohn Marino
180*86d7f5d3SJohn Marino ret = pagealign_alloc (size);
181*86d7f5d3SJohn Marino if (ret == NULL)
182*86d7f5d3SJohn Marino xalloc_die ();
183*86d7f5d3SJohn Marino return ret;
184*86d7f5d3SJohn Marino }
185*86d7f5d3SJohn Marino
186*86d7f5d3SJohn Marino
187*86d7f5d3SJohn Marino void
pagealign_free(void * aligned_ptr)188*86d7f5d3SJohn Marino pagealign_free (void *aligned_ptr)
189*86d7f5d3SJohn Marino {
190*86d7f5d3SJohn Marino #if HAVE_MMAP
191*86d7f5d3SJohn Marino if (munmap (aligned_ptr, get_memnode (aligned_ptr)) < 0)
192*86d7f5d3SJohn Marino error (EXIT_FAILURE, errno, "Failed to unmap memory");
193*86d7f5d3SJohn Marino #elif HAVE_POSIX_MEMALIGN
194*86d7f5d3SJohn Marino free (aligned_ptr);
195*86d7f5d3SJohn Marino #else
196*86d7f5d3SJohn Marino free (get_memnode (aligned_ptr));
197*86d7f5d3SJohn Marino #endif
198*86d7f5d3SJohn Marino }
199