Check or don't check for allocation failure? Provide both alternatives.
[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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 #if HAVE_FCNTL_H
32 # include <fcntl.h>
33 #endif
34
35 #if HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38
39 #if HAVE_MMAP
40 # include <sys/mman.h>
41 #endif
42
43 #include "error.h"
44 #include "exit.h"
45 #include "getpagesize.h"
46 #include "xalloc.h"
47
48
49 #if HAVE_MMAP || ! HAVE_POSIX_MEMALIGN
50
51 # if HAVE_MMAP
52 /* For each memory region, we store its size.  */
53 typedef size_t info_t;
54 # else
55 /* For each memory region, we store the original pointer returned by
56    malloc().  */
57 typedef void * info_t;
58 # endif
59
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;
63 struct memnode_s
64 {
65   void *aligned_ptr;
66   info_t info;
67   memnode_t *next;
68 };
69
70 /* The list of currently allocated memory regions.  */
71 static memnode_t *memnode_table = NULL;
72
73
74 static void
75 new_memnode (void *aligned_ptr, info_t info)
76 {
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;
82 }
83
84
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.  */
87 static info_t
88 get_memnode (void *aligned_ptr)
89 {
90   info_t ret;
91   memnode_t *c;
92   memnode_t **p_next = &memnode_table;
93
94   for (c = *p_next; c != NULL; p_next = &c->next, c = c->next)
95     if (c->aligned_ptr == aligned_ptr)
96       break;
97
98   if (c == NULL)
99     /* An attempt to free untracked memory.  A wrong pointer was passed
100        to pagealign_free().  */
101     abort ();
102
103   /* Remove this entry from the list, save the return value, and free it.  */
104   *p_next = c->next;
105   ret = c->info;
106   free (c);
107
108   return ret;
109 }
110
111 #endif /* HAVE_MMAP || !HAVE_POSIX_MEMALIGN */
112
113
114 void *
115 pagealign_alloc (size_t size)
116 {
117   void *ret;
118 #if HAVE_MMAP
119   int flags;
120 # ifdef HAVE_MAP_ANONYMOUS
121   const int fd = -1;
122   flags = MAP_ANONYMOUS | MAP_PRIVATE;
123 # else /* !HAVE_MAP_ANONYMOUS */
124   static int fd = -1;  /* Only open /dev/zero once in order to avoid limiting
125                           the amount of memory we may allocate based on the
126                           number of open file descriptors.  */
127   flags = MAP_FILE | MAP_PRIVATE;
128   if (fd == -1)
129     {
130       fd = open ("/dev/zero", O_RDONLY, 0666);
131       if (fd < 0)
132         error (EXIT_FAILURE, errno, "Failed to open /dev/zero for read");
133     }
134 # endif /* HAVE_MAP_ANONYMOUS */
135   ret = mmap (NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
136   if (!ret)
137     return NULL;
138   new_memnode (ret, size);
139 #elif HAVE_POSIX_MEMALIGN
140   int status = posix_memalign (&ret, getpagesize (), size);
141   if (status)
142     return NULL;
143 #else /* !HAVE_MMAP && !HAVE_POSIX_MEMALIGN */
144   size_t pagesize = getpagesize ();
145   void *unaligned_ptr = malloc (size + pagesize - 1);
146   if (unaligned_ptr == NULL)
147     return NULL;
148   ret = (char *) unaligned_ptr
149         + ((- (unsigned long) unaligned_ptr) & (pagesize - 1));
150   new_memnode (ret, unaligned_ptr);
151 #endif /* HAVE_MMAP && HAVE_POSIX_MEMALIGN */
152   return ret;
153 }
154
155
156 void *
157 pagealign_xalloc (size_t size)
158 {
159   void *ret;
160
161   ret = pagealign_alloc (size);
162   if (ret == NULL)
163     xalloc_die ();
164   return ret;
165 }
166
167
168 void
169 pagealign_free (void *aligned_ptr)
170 {
171 #if HAVE_MMAP
172   munmap (aligned_ptr, get_memnode (aligned_ptr));
173 #elif HAVE_POSIX_MEMALIGN
174   free (aligned_ptr);
175 #else
176   free (get_memnode (aligned_ptr));
177 #endif
178 }