1 /* Memory allocation aligned to system page boundaries.
3 Copyright (C) 2005 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public
16 License along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 /* Written by Derek R. Price <derek@ximbiot.com>. */
26 #include "pagealign_alloc.h"
35 # include <sys/mman.h>
40 #include "getpagesize.h"
44 #define _(str) gettext (str)
47 /* Define MAP_FILE when it isn't otherwise. */
51 /* Define MAP_FAILED for old systems which neglect to. */
53 # define MAP_FAILED ((void *)-1)
58 #if HAVE_MMAP || ! HAVE_POSIX_MEMALIGN
61 /* For each memory region, we store its size. */
62 typedef size_t info_t;
64 /* For each memory region, we store the original pointer returned by
66 typedef void * info_t;
69 /* A simple linked list of allocated memory regions. It is probably not the
70 most efficient way to store these, but anyway... */
71 typedef struct memnode_s memnode_t;
79 /* The list of currently allocated memory regions. */
80 static memnode_t *memnode_table = NULL;
84 new_memnode (void *aligned_ptr, info_t info)
86 memnode_t *new_node = (memnode_t *) xmalloc (sizeof (memnode_t));
87 new_node->aligned_ptr = aligned_ptr;
88 new_node->info = info;
89 new_node->next = memnode_table;
90 memnode_table = new_node;
94 /* Dispose of the memnode containing a map for the ALIGNED_PTR in question
95 and return the content of the node's INFO field. */
97 get_memnode (void *aligned_ptr)
101 memnode_t **p_next = &memnode_table;
103 for (c = *p_next; c != NULL; p_next = &c->next, c = c->next)
104 if (c->aligned_ptr == aligned_ptr)
108 /* An attempt to free untracked memory. A wrong pointer was passed
109 to pagealign_free(). */
112 /* Remove this entry from the list, save the return value, and free it. */
120 #endif /* HAVE_MMAP || !HAVE_POSIX_MEMALIGN */
124 pagealign_alloc (size_t size)
128 # ifdef HAVE_MAP_ANONYMOUS
130 const int flags = MAP_ANONYMOUS | MAP_PRIVATE;
131 # else /* !HAVE_MAP_ANONYMOUS */
132 static int fd = -1; /* Only open /dev/zero once in order to avoid limiting
133 the amount of memory we may allocate based on the
134 number of open file descriptors. */
135 const int flags = MAP_FILE | MAP_PRIVATE;
138 fd = open ("/dev/zero", O_RDONLY, 0666);
140 error (EXIT_FAILURE, errno, _("Failed to open /dev/zero for read"));
142 # endif /* HAVE_MAP_ANONYMOUS */
143 ret = mmap (NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
144 if (ret == MAP_FAILED)
146 new_memnode (ret, size);
147 #elif HAVE_POSIX_MEMALIGN
148 int status = posix_memalign (&ret, getpagesize (), size);
154 #else /* !HAVE_MMAP && !HAVE_POSIX_MEMALIGN */
155 size_t pagesize = getpagesize ();
156 void *unaligned_ptr = malloc (size + pagesize - 1);
157 if (unaligned_ptr == NULL)
159 /* Set errno. We don't know whether malloc already set errno: some
160 implementations of malloc do, some don't. */
164 ret = (char *) unaligned_ptr
165 + ((- (unsigned long) unaligned_ptr) & (pagesize - 1));
166 new_memnode (ret, unaligned_ptr);
167 #endif /* HAVE_MMAP && HAVE_POSIX_MEMALIGN */
173 pagealign_xalloc (size_t size)
177 ret = pagealign_alloc (size);
185 pagealign_free (void *aligned_ptr)
188 if (munmap (aligned_ptr, get_memnode (aligned_ptr)) < 0)
189 error (EXIT_FAILURE, errno, "Failed to unmap memory");
190 #elif HAVE_POSIX_MEMALIGN
193 free (get_memnode (aligned_ptr));