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