Change copyright notice from GPLv2+ to GPLv3+.
[gnulib.git] / lib / pagealign_alloc.c
1 /* Memory allocation aligned to system page boundaries.
2
3    Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
4
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.
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
13    GNU General Public License for more details.
14
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/>.  */
17
18 /* Written by Derek R. Price <derek@ximbiot.com>.  */
19
20 #include <config.h>
21
22 #include "pagealign_alloc.h"
23
24 #include <errno.h>
25 #include <stdlib.h>
26
27 #include <fcntl.h>
28 #include <unistd.h>
29
30 #if HAVE_MMAP
31 # include <sys/mman.h>
32 #endif
33
34 #include "error.h"
35 #include "getpagesize.h"
36 #include "xalloc.h"
37 #include "gettext.h"
38
39 #define _(str) gettext (str)
40
41 #if HAVE_MMAP
42 /* Define MAP_FILE when it isn't otherwise.  */
43 # ifndef MAP_FILE
44 #  define MAP_FILE 0
45 # endif
46 /* Define MAP_FAILED for old systems which neglect to.  */
47 # ifndef MAP_FAILED
48 #  define MAP_FAILED ((void *)-1)
49 # endif
50 #endif
51
52 /* The results of open() in this file are not used with fchdir,
53    therefore save some unnecessary work in fchdir.c.  */
54 #undef open
55 #undef close
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 = XMALLOC (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 }