1 /* Memory allocation aligned to system page boundaries.
3 Copyright (C) 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Derek R. Price <derek@ximbiot.com>. */
22 #include "pagealign_alloc.h"
31 # include <sys/mman.h>
38 #define _(str) gettext (str)
41 /* Define MAP_FILE when it isn't otherwise. */
45 /* Define MAP_FAILED for old systems which neglect to. */
47 # define MAP_FAILED ((void *)-1)
51 /* The results of open() in this file are not used with fchdir,
52 therefore save some unnecessary work in fchdir.c. */
57 #if HAVE_MMAP || ! HAVE_POSIX_MEMALIGN
60 /* For each memory region, we store its size. */
61 typedef size_t info_t;
63 /* For each memory region, we store the original pointer returned by
65 typedef void * info_t;
68 /* A simple linked list of allocated memory regions. It is probably not the
69 most efficient way to store these, but anyway... */
70 typedef struct memnode_s memnode_t;
78 /* The list of currently allocated memory regions. */
79 static memnode_t *memnode_table = NULL;
83 new_memnode (void *aligned_ptr, info_t info)
85 memnode_t *new_node = XMALLOC (memnode_t);
86 new_node->aligned_ptr = aligned_ptr;
87 new_node->info = info;
88 new_node->next = memnode_table;
89 memnode_table = new_node;
93 /* Dispose of the memnode containing a map for the ALIGNED_PTR in question
94 and return the content of the node's INFO field. */
96 get_memnode (void *aligned_ptr)
100 memnode_t **p_next = &memnode_table;
102 for (c = *p_next; c != NULL; p_next = &c->next, c = c->next)
103 if (c->aligned_ptr == aligned_ptr)
107 /* An attempt to free untracked memory. A wrong pointer was passed
108 to pagealign_free(). */
111 /* Remove this entry from the list, save the return value, and free it. */
119 #endif /* HAVE_MMAP || !HAVE_POSIX_MEMALIGN */
123 pagealign_alloc (size_t size)
127 # ifdef HAVE_MAP_ANONYMOUS
129 const int flags = MAP_ANONYMOUS | MAP_PRIVATE;
130 # else /* !HAVE_MAP_ANONYMOUS */
131 static int fd = -1; /* Only open /dev/zero once in order to avoid limiting
132 the amount of memory we may allocate based on the
133 number of open file descriptors. */
134 const int flags = MAP_FILE | MAP_PRIVATE;
137 fd = open ("/dev/zero", O_RDONLY, 0666);
139 error (EXIT_FAILURE, errno, _("Failed to open /dev/zero for read"));
141 # endif /* HAVE_MAP_ANONYMOUS */
142 ret = mmap (NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
143 if (ret == MAP_FAILED)
145 new_memnode (ret, size);
146 #elif HAVE_POSIX_MEMALIGN
147 int status = posix_memalign (&ret, getpagesize (), size);
153 #else /* !HAVE_MMAP && !HAVE_POSIX_MEMALIGN */
154 size_t pagesize = getpagesize ();
155 void *unaligned_ptr = malloc (size + pagesize - 1);
156 if (unaligned_ptr == NULL)
158 /* Set errno. We don't know whether malloc already set errno: some
159 implementations of malloc do, some don't. */
163 ret = (char *) unaligned_ptr
164 + ((- (unsigned long) unaligned_ptr) & (pagesize - 1));
165 new_memnode (ret, unaligned_ptr);
166 #endif /* HAVE_MMAP && HAVE_POSIX_MEMALIGN */
172 pagealign_xalloc (size_t size)
176 ret = pagealign_alloc (size);
184 pagealign_free (void *aligned_ptr)
187 if (munmap (aligned_ptr, get_memnode (aligned_ptr)) < 0)
188 error (EXIT_FAILURE, errno, "Failed to unmap memory");
189 #elif HAVE_POSIX_MEMALIGN
192 free (get_memnode (aligned_ptr));