fchmod-tests, fchmodat tests, lchmod tests: Add more tests.
[gnulib.git] / lib / pagealign_alloc.c
blob6bfa852d5195f97ad4f1b9c5a7bafaa5ec674cc6
1 /* Memory allocation aligned to system page boundaries.
3 Copyright (C) 2005-2007, 2009-2021 Free Software Foundation, Inc.
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Derek R. Price <derek@ximbiot.com>. */
20 #include <config.h>
22 #include "pagealign_alloc.h"
24 #include <errno.h>
25 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <unistd.h>
30 #if HAVE_MMAP
31 # include <sys/mman.h>
32 #endif
34 #include "error.h"
35 #include "xalloc.h"
36 #include "gettext.h"
38 #define _(str) gettext (str)
40 #if HAVE_MMAP
41 /* Define MAP_FILE when it isn't otherwise. */
42 # ifndef MAP_FILE
43 # define MAP_FILE 0
44 # endif
45 /* Define MAP_FAILED for old systems which neglect to. */
46 # ifndef MAP_FAILED
47 # define MAP_FAILED ((void *)-1)
48 # endif
49 #endif
52 #if HAVE_MMAP || ! HAVE_POSIX_MEMALIGN
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
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
68 void *aligned_ptr;
69 info_t info;
70 memnode_t *next;
73 /* The list of currently allocated memory regions. */
74 static memnode_t *memnode_table = NULL;
77 static void
78 new_memnode (void *aligned_ptr, info_t info)
80 memnode_t *new_node = XMALLOC (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;
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)
93 info_t ret;
94 memnode_t *c;
95 memnode_t **p_next = &memnode_table;
97 for (c = *p_next; c != NULL; p_next = &c->next, c = c->next)
98 if (c->aligned_ptr == aligned_ptr)
99 break;
101 if (c == NULL)
102 /* An attempt to free untracked memory. A wrong pointer was passed
103 to pagealign_free(). */
104 abort ();
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);
111 return ret;
114 #endif /* HAVE_MMAP || !HAVE_POSIX_MEMALIGN */
117 void *
118 pagealign_alloc (size_t size)
120 void *ret;
121 /* We prefer the mmap() approach over the posix_memalign() or malloc()
122 based approaches, since the latter often waste an entire memory page
123 per call. */
124 #if HAVE_MMAP
125 # ifdef HAVE_MAP_ANONYMOUS
126 const int fd = -1;
127 const int flags = MAP_ANONYMOUS | MAP_PRIVATE;
128 # else /* !HAVE_MAP_ANONYMOUS */
129 static int fd = -1; /* Only open /dev/zero once in order to avoid limiting
130 the amount of memory we may allocate based on the
131 number of open file descriptors. */
132 const int flags = MAP_FILE | MAP_PRIVATE;
133 if (fd == -1)
135 fd = open ("/dev/zero", O_RDONLY | O_CLOEXEC, 0666);
136 if (fd < 0)
137 error (EXIT_FAILURE, errno, _("Failed to open /dev/zero for read"));
139 # endif /* HAVE_MAP_ANONYMOUS */
140 ret = mmap (NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
141 if (ret == MAP_FAILED)
142 return NULL;
143 new_memnode (ret, size);
144 #elif HAVE_POSIX_MEMALIGN
145 int status = posix_memalign (&ret, getpagesize (), size);
146 if (status)
148 errno = status;
149 return NULL;
151 #else /* !HAVE_MMAP && !HAVE_POSIX_MEMALIGN */
152 size_t pagesize = getpagesize ();
153 void *unaligned_ptr = malloc (size + pagesize - 1);
154 if (unaligned_ptr == NULL)
156 /* Set errno. We don't know whether malloc already set errno: some
157 implementations of malloc do, some don't. */
158 errno = ENOMEM;
159 return NULL;
161 ret = (char *) unaligned_ptr
162 + ((- (unsigned long) unaligned_ptr) & (pagesize - 1));
163 new_memnode (ret, unaligned_ptr);
164 #endif /* HAVE_MMAP && HAVE_POSIX_MEMALIGN */
165 return ret;
169 void *
170 pagealign_xalloc (size_t size)
172 void *ret;
174 ret = pagealign_alloc (size);
175 if (ret == NULL)
176 xalloc_die ();
177 return ret;
181 void
182 pagealign_free (void *aligned_ptr)
184 #if HAVE_MMAP
185 if (munmap (aligned_ptr, get_memnode (aligned_ptr)) < 0)
186 error (EXIT_FAILURE, errno, "Failed to unmap memory");
187 #elif HAVE_POSIX_MEMALIGN
188 free (aligned_ptr);
189 #else
190 free (get_memnode (aligned_ptr));
191 #endif