1 /*
2  * libc/stdlib/malloc/memalign.c -- memalign (`aligned malloc') function
3  *
4  *  Copyright (C) 2002  NEC Corporation
5  *  Copyright (C) 2002  Miles Bader <miles@gnu.org>
6  *
7  * This file is subject to the terms and conditions of the GNU Lesser
8  * General Public License.  See the file COPYING.LIB in the main
9  * directory of this archive for more details.
10  *
11  * Written by Miles Bader <miles@gnu.org>
12  */
13 
14 #include <errno.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <sys/mman.h>
18 
19 #include "malloc.h"
20 #include "heap.h"
21 
22 
23 /*
24       ______________________ TOTAL _________________________
25      /                                                      \
26     +---------------+-------------------------+--------------+
27     |               |                         |              |
28     +---------------+-------------------------+--------------+
29     \____ INIT ____/ \______ RETURNED _______/ \____ END ___/
30 */
31 
32 void *memalign (size_t alignment, size_t size);
33 /* XXX shadow outer malloc.h */
libc_hidden_proto(memalign)34 libc_hidden_proto(memalign)
35 void *
36 memalign (size_t alignment, size_t size)
37 {
38   void *mem, *base;
39   unsigned long tot_addr, tot_end_addr, addr, end_addr;
40   struct heap_free_area **heap = &__malloc_heap;
41 
42   if (unlikely(size > PTRDIFF_MAX)) {
43     __set_errno(ENOMEM);
44     return NULL;
45   }
46 
47   /* Make SIZE something we like.  */
48   size = HEAP_ADJUST_SIZE (size);
49 
50   /* Use malloc to do the initial allocation, since it deals with getting
51      system memory.  We over-allocate enough to be sure that we'll get
52      enough memory to hold a properly aligned block of size SIZE,
53      _somewhere_ in the result.  */
54   mem = malloc (size + 2 * alignment);
55   if (! mem)
56     /* Allocation failed, we can't do anything.  */
57     return 0;
58   if (alignment < MALLOC_ALIGNMENT)
59     return mem;
60 
61   /* Remember the base-address, of the allocation, although we normally
62      use the user-address for calculations, since that's where the
63      alignment matters.  */
64   base = MALLOC_BASE (mem);
65 
66   /* The bounds of the initial allocation.  */
67   tot_addr = (unsigned long)mem;
68   tot_end_addr = (unsigned long)base + MALLOC_SIZE (mem);
69 
70   /* Find a likely place inside MEM with the right alignment.  */
71   addr = MALLOC_ROUND_UP (tot_addr, alignment);
72 
73   /* Unless TOT_ADDR was already aligned correctly, we need to return the
74      initial part of MEM to the heap.  */
75   if (addr != tot_addr)
76     {
77       size_t init_size = addr - tot_addr;
78 
79       /* Ensure that memory returned to the heap is large enough.  */
80       if (init_size < HEAP_MIN_SIZE)
81 	{
82 	  addr = MALLOC_ROUND_UP (tot_addr + HEAP_MIN_SIZE, alignment);
83 	  init_size = addr - tot_addr;
84 	}
85 
86       __heap_lock (&__malloc_heap_lock);
87       __heap_free (heap, base, init_size);
88       __heap_unlock (&__malloc_heap_lock);
89 
90       /* Remember that we've freed the initial part of MEM.  */
91       base += init_size;
92     }
93 
94   /* Return the end part of MEM to the heap, unless it's too small.  */
95   end_addr = addr + size;
96   if (end_addr + MALLOC_REALLOC_MIN_FREE_SIZE < tot_end_addr) {
97     __heap_lock (&__malloc_heap_lock);
98     __heap_free (heap, (void *)end_addr, tot_end_addr - end_addr);
99     __heap_unlock (&__malloc_heap_lock);
100   } else
101     /* We didn't free the end, so include it in the size.  */
102     end_addr = tot_end_addr;
103 
104   return MALLOC_SETUP (base, end_addr - (unsigned long)base);
105 }
106 weak_alias(memalign, aligned_alloc)
107 libc_hidden_def(memalign)
108