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