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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 /* Written by Derek R. Price <derek@ximbiot.com>. */
26 #include "pagealign_alloc.h"
40 # include <sys/mman.h>
45 #include "getpagesize.h"
49 #if HAVE_MMAP || ! HAVE_POSIX_MEMALIGN
52 /* For each memory region, we store its size. */
53 typedef size_t info_t;
55 /* For each memory region, we store the original pointer returned by
57 typedef void * info_t;
60 /* A simple linked list of allocated memory regions. It is probably not the
61 most efficient way to store these, but anyway... */
62 typedef struct memnode_s memnode_t;
70 /* The list of currently allocated memory regions. */
71 static memnode_t *memnode_table = NULL;
75 new_memnode (void *aligned_ptr, info_t info)
77 memnode_t *new_node = (memnode_t *) xmalloc (sizeof (memnode_t));
78 new_node->aligned_ptr = aligned_ptr;
79 new_node->info = info;
80 new_node->next = memnode_table;
81 memnode_table = new_node;
85 /* Dispose of the memnode containing a map for the ALIGNED_PTR in question
86 and return the content of the node's INFO field. */
88 get_memnode (void *aligned_ptr)
92 memnode_t **p_next = &memnode_table;
94 for (c = *p_next; c != NULL; p_next = &c->next, c = c->next)
95 if (c->aligned_ptr == aligned_ptr)
99 /* An attempt to free untracked memory. A wrong pointer was passed
100 to pagealign_free(). */
103 /* Remove this entry from the list, save the return value, and free it. */
111 #endif /* HAVE_MMAP || !HAVE_POSIX_MEMALIGN */
115 pagealign_alloc (size_t size)
120 static int fd = -1; /* Only open /dev/zero once in order to avoid limiting
121 the amount of memory we may allocate based on the
122 number of open file descriptors. */
123 # ifdef HAVE_MAP_ANONYMOUS
124 flags = MAP_ANONYMOUS | MAP_PRIVATE;
126 # else /* !HAVE_MAP_ANONYMOUS */
127 flags = MAP_FILE | MAP_PRIVATE;
130 fd = open ("/dev/zero", O_RDONLY, 0666);
132 error (EXIT_FAILURE, errno, "Failed to open /dev/zero for read");
134 # endif /* HAVE_MAP_ANONYMOUS */
135 ret = mmap (NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
137 error (EXIT_FAILURE, errno, "mmap to /dev/zero failed");
138 new_memnode (ret, size);
139 #elif HAVE_POSIX_MEMALIGN
140 int status = posix_memalign (&ret, getpagesize (), size);
142 error (EXIT_FAILURE, status, "posix_memalign failed");
143 #else /* !HAVE_MMAP && !HAVE_POSIX_MEMALIGN */
144 size_t pagesize = getpagesize ();
145 void *unaligned_ptr = xmalloc (size + pagesize - 1);
146 ret = (char *) unaligned_ptr
147 + ((- (unsigned long) unaligned_ptr) & (pagesize - 1));
148 new_memnode (ret, unaligned_ptr);
149 #endif /* HAVE_MMAP && HAVE_POSIX_MEMALIGN */
155 pagealign_free (void *aligned_ptr)
158 munmap (aligned_ptr, get_memnode (aligned_ptr));
159 #elif HAVE_POSIX_MEMALIGN
162 free (get_memnode (aligned_ptr));