COPYING and COPYING.LESSER are not on the web any more
[gnulib.git] / lib / pagealign_alloc.c
1 /* Memory allocation aligned to system page boundaries.
2
3    Copyright (C) 2005 Free Software Foundation, Inc.
4
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)
8    any later version.
9
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.
14
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,
18    USA.  */
19
20 /* Written by Derek R. Price <derek@ximbiot.com>.  */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include "pagealign_alloc.h"
27
28 #include <errno.h>
29 #include <stdlib.h>
30
31 #include <fcntl.h>
32 #include <unistd.h>
33
34 #if HAVE_MMAP
35 # include <sys/mman.h>
36 #endif
37
38 #include "error.h"
39 #include "exit.h"
40 #include "getpagesize.h"
41 #include "xalloc.h"
42 #include "gettext.h"
43
44 #define _(str) gettext (str)
45
46 #if HAVE_MMAP
47 /* Define MAP_FILE when it isn't otherwise.  */
48 # ifndef MAP_FILE
49 #  define MAP_FILE 0
50 # endif
51 /* Define MAP_FAILED for old systems which neglect to.  */
52 # ifndef MAP_FAILED
53 #  define MAP_FAILED ((void *)-1)
54 # endif
55 #endif
56
57
58 #if HAVE_MMAP || ! HAVE_POSIX_MEMALIGN
59
60 # if HAVE_MMAP
61 /* For each memory region, we store its size.  */
62 typedef size_t info_t;
63 # else
64 /* For each memory region, we store the original pointer returned by
65    malloc().  */
66 typedef void * info_t;
67 # endif
68
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;
72 struct memnode_s
73 {
74   void *aligned_ptr;
75   info_t info;
76   memnode_t *next;
77 };
78
79 /* The list of currently allocated memory regions.  */
80 static memnode_t *memnode_table = NULL;
81
82
83 static void
84 new_memnode (void *aligned_ptr, info_t info)
85 {
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;
91 }
92
93
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.  */
96 static info_t
97 get_memnode (void *aligned_ptr)
98 {
99   info_t ret;
100   memnode_t *c;
101   memnode_t **p_next = &memnode_table;
102
103   for (c = *p_next; c != NULL; p_next = &c->next, c = c->next)
104     if (c->aligned_ptr == aligned_ptr)
105       break;
106
107   if (c == NULL)
108     /* An attempt to free untracked memory.  A wrong pointer was passed
109        to pagealign_free().  */
110     abort ();
111
112   /* Remove this entry from the list, save the return value, and free it.  */
113   *p_next = c->next;
114   ret = c->info;
115   free (c);
116
117   return ret;
118 }
119
120 #endif /* HAVE_MMAP || !HAVE_POSIX_MEMALIGN */
121
122
123 void *
124 pagealign_alloc (size_t size)
125 {
126   void *ret;
127 #if HAVE_MMAP
128 # ifdef HAVE_MAP_ANONYMOUS
129   const int fd = -1;
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;
136   if (fd == -1)
137     {
138       fd = open ("/dev/zero", O_RDONLY, 0666);
139       if (fd < 0)
140         error (EXIT_FAILURE, errno, _("Failed to open /dev/zero for read"));
141     }
142 # endif /* HAVE_MAP_ANONYMOUS */
143   ret = mmap (NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
144   if (ret == MAP_FAILED)
145     return NULL;
146   new_memnode (ret, size);
147 #elif HAVE_POSIX_MEMALIGN
148   int status = posix_memalign (&ret, getpagesize (), size);
149   if (status)
150     {
151       errno = status;
152       return NULL;
153     }
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)
158     {
159       /* Set errno.  We don't know whether malloc already set errno: some
160          implementations of malloc do, some don't.  */
161       errno = ENOMEM;
162       return NULL;
163     }
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 */
168   return ret;
169 }
170
171
172 void *
173 pagealign_xalloc (size_t size)
174 {
175   void *ret;
176
177   ret = pagealign_alloc (size);
178   if (ret == NULL)
179     xalloc_die ();
180   return ret;
181 }
182
183
184 void
185 pagealign_free (void *aligned_ptr)
186 {
187 #if HAVE_MMAP
188   if (munmap (aligned_ptr, get_memnode (aligned_ptr)) < 0)
189     error (EXIT_FAILURE, errno, "Failed to unmap memory");
190 #elif HAVE_POSIX_MEMALIGN
191   free (aligned_ptr);
192 #else
193   free (get_memnode (aligned_ptr));
194 #endif
195 }