exit.h is replaced with stdlib.h.
[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 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 #include <config.h>
23
24 #include "pagealign_alloc.h"
25
26 #include <errno.h>
27 #include <stdlib.h>
28
29 #include <fcntl.h>
30 #include <unistd.h>
31
32 #if HAVE_MMAP
33 # include <sys/mman.h>
34 #endif
35
36 #include "error.h"
37 #include "getpagesize.h"
38 #include "xalloc.h"
39 #include "gettext.h"
40
41 #define _(str) gettext (str)
42
43 #if HAVE_MMAP
44 /* Define MAP_FILE when it isn't otherwise.  */
45 # ifndef MAP_FILE
46 #  define MAP_FILE 0
47 # endif
48 /* Define MAP_FAILED for old systems which neglect to.  */
49 # ifndef MAP_FAILED
50 #  define MAP_FAILED ((void *)-1)
51 # endif
52 #endif
53
54 /* The results of open() in this file are not used with fchdir,
55    therefore save some unnecessary work in fchdir.c.  */
56 #undef open
57 #undef close
58
59
60 #if HAVE_MMAP || ! HAVE_POSIX_MEMALIGN
61
62 # if HAVE_MMAP
63 /* For each memory region, we store its size.  */
64 typedef size_t info_t;
65 # else
66 /* For each memory region, we store the original pointer returned by
67    malloc().  */
68 typedef void * info_t;
69 # endif
70
71 /* A simple linked list of allocated memory regions.  It is probably not the
72    most efficient way to store these, but anyway...  */
73 typedef struct memnode_s memnode_t;
74 struct memnode_s
75 {
76   void *aligned_ptr;
77   info_t info;
78   memnode_t *next;
79 };
80
81 /* The list of currently allocated memory regions.  */
82 static memnode_t *memnode_table = NULL;
83
84
85 static void
86 new_memnode (void *aligned_ptr, info_t info)
87 {
88   memnode_t *new_node = XMALLOC (memnode_t);
89   new_node->aligned_ptr = aligned_ptr;
90   new_node->info = info;
91   new_node->next = memnode_table;
92   memnode_table = new_node;
93 }
94
95
96 /* Dispose of the memnode containing a map for the ALIGNED_PTR in question
97    and return the content of the node's INFO field.  */
98 static info_t
99 get_memnode (void *aligned_ptr)
100 {
101   info_t ret;
102   memnode_t *c;
103   memnode_t **p_next = &memnode_table;
104
105   for (c = *p_next; c != NULL; p_next = &c->next, c = c->next)
106     if (c->aligned_ptr == aligned_ptr)
107       break;
108
109   if (c == NULL)
110     /* An attempt to free untracked memory.  A wrong pointer was passed
111        to pagealign_free().  */
112     abort ();
113
114   /* Remove this entry from the list, save the return value, and free it.  */
115   *p_next = c->next;
116   ret = c->info;
117   free (c);
118
119   return ret;
120 }
121
122 #endif /* HAVE_MMAP || !HAVE_POSIX_MEMALIGN */
123
124
125 void *
126 pagealign_alloc (size_t size)
127 {
128   void *ret;
129 #if HAVE_MMAP
130 # ifdef HAVE_MAP_ANONYMOUS
131   const int fd = -1;
132   const int flags = MAP_ANONYMOUS | MAP_PRIVATE;
133 # else /* !HAVE_MAP_ANONYMOUS */
134   static int fd = -1;  /* Only open /dev/zero once in order to avoid limiting
135                           the amount of memory we may allocate based on the
136                           number of open file descriptors.  */
137   const int flags = MAP_FILE | MAP_PRIVATE;
138   if (fd == -1)
139     {
140       fd = open ("/dev/zero", O_RDONLY, 0666);
141       if (fd < 0)
142         error (EXIT_FAILURE, errno, _("Failed to open /dev/zero for read"));
143     }
144 # endif /* HAVE_MAP_ANONYMOUS */
145   ret = mmap (NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
146   if (ret == MAP_FAILED)
147     return NULL;
148   new_memnode (ret, size);
149 #elif HAVE_POSIX_MEMALIGN
150   int status = posix_memalign (&ret, getpagesize (), size);
151   if (status)
152     {
153       errno = status;
154       return NULL;
155     }
156 #else /* !HAVE_MMAP && !HAVE_POSIX_MEMALIGN */
157   size_t pagesize = getpagesize ();
158   void *unaligned_ptr = malloc (size + pagesize - 1);
159   if (unaligned_ptr == NULL)
160     {
161       /* Set errno.  We don't know whether malloc already set errno: some
162          implementations of malloc do, some don't.  */
163       errno = ENOMEM;
164       return NULL;
165     }
166   ret = (char *) unaligned_ptr
167         + ((- (unsigned long) unaligned_ptr) & (pagesize - 1));
168   new_memnode (ret, unaligned_ptr);
169 #endif /* HAVE_MMAP && HAVE_POSIX_MEMALIGN */
170   return ret;
171 }
172
173
174 void *
175 pagealign_xalloc (size_t size)
176 {
177   void *ret;
178
179   ret = pagealign_alloc (size);
180   if (ret == NULL)
181     xalloc_die ();
182   return ret;
183 }
184
185
186 void
187 pagealign_free (void *aligned_ptr)
188 {
189 #if HAVE_MMAP
190   if (munmap (aligned_ptr, get_memnode (aligned_ptr)) < 0)
191     error (EXIT_FAILURE, errno, "Failed to unmap memory");
192 #elif HAVE_POSIX_MEMALIGN
193   free (aligned_ptr);
194 #else
195   free (get_memnode (aligned_ptr));
196 #endif
197 }