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