xref: /netbsd-src/external/gpl2/xcvs/dist/lib/pagealign_alloc.c (revision 5a6c14c844c4c665da5632061aebde7bb2cb5766)
1a7c91847Schristos /* Memory allocation aligned to system page boundaries.
2a7c91847Schristos 
3a7c91847Schristos    Copyright (C) 2005 Free Software Foundation, Inc.
4a7c91847Schristos 
5a7c91847Schristos    This program is free software; you can redistribute it and/or modify it
6a7c91847Schristos    under the terms of the GNU General Public License as published
7a7c91847Schristos    by the Free Software Foundation; either version 2, or (at your option)
8a7c91847Schristos    any later version.
9a7c91847Schristos 
10a7c91847Schristos    This program is distributed in the hope that it will be useful,
11a7c91847Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
12a7c91847Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13a7c91847Schristos    General Public License for more details.
14a7c91847Schristos 
15a7c91847Schristos    You should have received a copy of the GNU General Public
16a7c91847Schristos    License along with this program; if not, write to the Free Software
17a7c91847Schristos    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18a7c91847Schristos    USA.  */
19*5a6c14c8Schristos #include <sys/cdefs.h>
20*5a6c14c8Schristos __RCSID("$NetBSD: pagealign_alloc.c,v 1.2 2016/05/17 14:00:09 christos Exp $");
21*5a6c14c8Schristos 
22a7c91847Schristos 
23a7c91847Schristos /* Written by Derek R. Price <derek@ximbiot.com>.  */
24a7c91847Schristos 
25a7c91847Schristos #ifdef HAVE_CONFIG_H
26a7c91847Schristos # include <config.h>
27a7c91847Schristos #endif
28a7c91847Schristos 
29a7c91847Schristos #include "pagealign_alloc.h"
30a7c91847Schristos 
31a7c91847Schristos #include <errno.h>
32a7c91847Schristos #include <stdlib.h>
33a7c91847Schristos 
34a7c91847Schristos #include <fcntl.h>
35a7c91847Schristos 
36a7c91847Schristos #if HAVE_UNISTD_H
37a7c91847Schristos # include <unistd.h>
38a7c91847Schristos #endif
39a7c91847Schristos 
40a7c91847Schristos #if HAVE_MMAP
41a7c91847Schristos # include <sys/mman.h>
42a7c91847Schristos #endif
43a7c91847Schristos 
44a7c91847Schristos #include "error.h"
45a7c91847Schristos #include "exit.h"
46a7c91847Schristos #include "getpagesize.h"
47a7c91847Schristos #include "xalloc.h"
48a7c91847Schristos #include "gettext.h"
49a7c91847Schristos 
50a7c91847Schristos #define _(str) gettext (str)
51a7c91847Schristos 
52a7c91847Schristos #if HAVE_MMAP
53a7c91847Schristos /* Define MAP_FILE when it isn't otherwise.  */
54a7c91847Schristos # ifndef MAP_FILE
55a7c91847Schristos #  define MAP_FILE 0
56a7c91847Schristos # endif
57a7c91847Schristos /* Define MAP_FAILED for old systems which neglect to.  */
58a7c91847Schristos # ifndef MAP_FAILED
59a7c91847Schristos #  define MAP_FAILED ((void *)-1)
60a7c91847Schristos # endif
61a7c91847Schristos #endif
62a7c91847Schristos 
63a7c91847Schristos 
64a7c91847Schristos #if HAVE_MMAP || ! HAVE_POSIX_MEMALIGN
65a7c91847Schristos 
66a7c91847Schristos # if HAVE_MMAP
67a7c91847Schristos /* For each memory region, we store its size.  */
68a7c91847Schristos typedef size_t info_t;
69a7c91847Schristos # else
70a7c91847Schristos /* For each memory region, we store the original pointer returned by
71a7c91847Schristos    malloc().  */
72a7c91847Schristos typedef void * info_t;
73a7c91847Schristos # endif
74a7c91847Schristos 
75a7c91847Schristos /* A simple linked list of allocated memory regions.  It is probably not the
76a7c91847Schristos    most efficient way to store these, but anyway...  */
77a7c91847Schristos typedef struct memnode_s memnode_t;
78a7c91847Schristos struct memnode_s
79a7c91847Schristos {
80a7c91847Schristos   void *aligned_ptr;
81a7c91847Schristos   info_t info;
82a7c91847Schristos   memnode_t *next;
83a7c91847Schristos };
84a7c91847Schristos 
85a7c91847Schristos /* The list of currently allocated memory regions.  */
86a7c91847Schristos static memnode_t *memnode_table = NULL;
87a7c91847Schristos 
88a7c91847Schristos 
89a7c91847Schristos static void
new_memnode(void * aligned_ptr,info_t info)90a7c91847Schristos new_memnode (void *aligned_ptr, info_t info)
91a7c91847Schristos {
92a7c91847Schristos   memnode_t *new_node = (memnode_t *) xmalloc (sizeof (memnode_t));
93a7c91847Schristos   new_node->aligned_ptr = aligned_ptr;
94a7c91847Schristos   new_node->info = info;
95a7c91847Schristos   new_node->next = memnode_table;
96a7c91847Schristos   memnode_table = new_node;
97a7c91847Schristos }
98a7c91847Schristos 
99a7c91847Schristos 
100a7c91847Schristos /* Dispose of the memnode containing a map for the ALIGNED_PTR in question
101a7c91847Schristos    and return the content of the node's INFO field.  */
102a7c91847Schristos static info_t
get_memnode(void * aligned_ptr)103a7c91847Schristos get_memnode (void *aligned_ptr)
104a7c91847Schristos {
105a7c91847Schristos   info_t ret;
106a7c91847Schristos   memnode_t *c;
107a7c91847Schristos   memnode_t **p_next = &memnode_table;
108a7c91847Schristos 
109a7c91847Schristos   for (c = *p_next; c != NULL; p_next = &c->next, c = c->next)
110a7c91847Schristos     if (c->aligned_ptr == aligned_ptr)
111a7c91847Schristos       break;
112a7c91847Schristos 
113a7c91847Schristos   if (c == NULL)
114a7c91847Schristos     /* An attempt to free untracked memory.  A wrong pointer was passed
115a7c91847Schristos        to pagealign_free().  */
116a7c91847Schristos     abort ();
117a7c91847Schristos 
118a7c91847Schristos   /* Remove this entry from the list, save the return value, and free it.  */
119a7c91847Schristos   *p_next = c->next;
120a7c91847Schristos   ret = c->info;
121a7c91847Schristos   free (c);
122a7c91847Schristos 
123a7c91847Schristos   return ret;
124a7c91847Schristos }
125a7c91847Schristos 
126a7c91847Schristos #endif /* HAVE_MMAP || !HAVE_POSIX_MEMALIGN */
127a7c91847Schristos 
128a7c91847Schristos 
129a7c91847Schristos void *
pagealign_alloc(size_t size)130a7c91847Schristos pagealign_alloc (size_t size)
131a7c91847Schristos {
132a7c91847Schristos   void *ret;
133a7c91847Schristos #if HAVE_MMAP
134a7c91847Schristos # ifdef HAVE_MAP_ANONYMOUS
135a7c91847Schristos   const int fd = -1;
136a7c91847Schristos   const int flags = MAP_ANONYMOUS | MAP_PRIVATE;
137a7c91847Schristos # else /* !HAVE_MAP_ANONYMOUS */
138a7c91847Schristos   static int fd = -1;  /* Only open /dev/zero once in order to avoid limiting
139a7c91847Schristos 			  the amount of memory we may allocate based on the
140a7c91847Schristos 			  number of open file descriptors.  */
141a7c91847Schristos   const int flags = MAP_FILE | MAP_PRIVATE;
142a7c91847Schristos   if (fd == -1)
143a7c91847Schristos     {
144a7c91847Schristos       fd = open ("/dev/zero", O_RDONLY, 0666);
145a7c91847Schristos       if (fd < 0)
146a7c91847Schristos 	error (EXIT_FAILURE, errno, _("Failed to open /dev/zero for read"));
147a7c91847Schristos     }
148a7c91847Schristos # endif /* HAVE_MAP_ANONYMOUS */
149a7c91847Schristos   ret = mmap (NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
150a7c91847Schristos   if (ret == MAP_FAILED)
151a7c91847Schristos     return NULL;
152a7c91847Schristos   new_memnode (ret, size);
153a7c91847Schristos #elif HAVE_POSIX_MEMALIGN
154a7c91847Schristos   int status = posix_memalign (&ret, getpagesize (), size);
155a7c91847Schristos   if (status)
156a7c91847Schristos     {
157a7c91847Schristos       errno = status;
158a7c91847Schristos       return NULL;
159a7c91847Schristos     }
160a7c91847Schristos #else /* !HAVE_MMAP && !HAVE_POSIX_MEMALIGN */
161a7c91847Schristos   size_t pagesize = getpagesize ();
162a7c91847Schristos   void *unaligned_ptr = malloc (size + pagesize - 1);
163a7c91847Schristos   if (unaligned_ptr == NULL)
164a7c91847Schristos     {
165a7c91847Schristos       /* Set errno.  We don't know whether malloc already set errno: some
166a7c91847Schristos 	 implementations of malloc do, some don't.  */
167a7c91847Schristos       errno = ENOMEM;
168a7c91847Schristos       return NULL;
169a7c91847Schristos     }
170a7c91847Schristos   ret = (char *) unaligned_ptr
171a7c91847Schristos         + ((- (unsigned long) unaligned_ptr) & (pagesize - 1));
172a7c91847Schristos   new_memnode (ret, unaligned_ptr);
173a7c91847Schristos #endif /* HAVE_MMAP && HAVE_POSIX_MEMALIGN */
174a7c91847Schristos   return ret;
175a7c91847Schristos }
176a7c91847Schristos 
177a7c91847Schristos 
178a7c91847Schristos void *
pagealign_xalloc(size_t size)179a7c91847Schristos pagealign_xalloc (size_t size)
180a7c91847Schristos {
181a7c91847Schristos   void *ret;
182a7c91847Schristos 
183a7c91847Schristos   ret = pagealign_alloc (size);
184a7c91847Schristos   if (ret == NULL)
185a7c91847Schristos     xalloc_die ();
186a7c91847Schristos   return ret;
187a7c91847Schristos }
188a7c91847Schristos 
189a7c91847Schristos 
190a7c91847Schristos void
pagealign_free(void * aligned_ptr)191a7c91847Schristos pagealign_free (void *aligned_ptr)
192a7c91847Schristos {
193a7c91847Schristos #if HAVE_MMAP
194a7c91847Schristos   if (munmap (aligned_ptr, get_memnode (aligned_ptr)) < 0)
195a7c91847Schristos     error (EXIT_FAILURE, errno, "Failed to unmap memory");
196a7c91847Schristos #elif HAVE_POSIX_MEMALIGN
197a7c91847Schristos   free (aligned_ptr);
198a7c91847Schristos #else
199a7c91847Schristos   free (get_memnode (aligned_ptr));
200a7c91847Schristos #endif
201a7c91847Schristos }
202