]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4re-core/uclibc/lib/contrib/uclibc/libc/stdlib/malloc/memalign.c
Update
[l4.git] / l4 / pkg / l4re-core / uclibc / lib / contrib / uclibc / libc / stdlib / malloc / memalign.c
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 <stdlib.h>
15 #include <unistd.h>
16 #include <sys/mman.h>
17
18 #include "malloc.h"
19 #include "heap.h"
20
21
22 /*
23       ______________________ TOTAL _________________________
24      /                                                      \
25     +---------------+-------------------------+--------------+
26     |               |                         |              |
27     +---------------+-------------------------+--------------+
28     \____ INIT ____/ \______ RETURNED _______/ \____ END ___/
29 */
30
31 void *memalign (size_t alignment, size_t size);
32 /* XXX shadow outer malloc.h */
33 libc_hidden_proto(memalign)
34 void *
35 memalign (size_t alignment, size_t size)
36 {
37   void *mem, *base;
38   unsigned long tot_addr, tot_end_addr, addr, end_addr;
39   struct heap_free_area **heap = &__malloc_heap;
40
41   /* Make SIZE something we like.  */
42   size = HEAP_ADJUST_SIZE (size);
43
44   /* Use malloc to do the initial allocation, since it deals with getting
45      system memory.  We over-allocate enough to be sure that we'll get
46      enough memory to hold a properly aligned block of size SIZE,
47      _somewhere_ in the result.  */
48   mem = malloc (size + 2 * alignment);
49   if (! mem)
50     /* Allocation failed, we can't do anything.  */
51     return 0;
52   if (alignment < MALLOC_ALIGNMENT)
53     return mem;
54
55   /* Remember the base-address, of the allocation, although we normally
56      use the user-address for calculations, since that's where the
57      alignment matters.  */
58   base = MALLOC_BASE (mem);
59
60   /* The bounds of the initial allocation.  */
61   tot_addr = (unsigned long)mem;
62   tot_end_addr = (unsigned long)base + MALLOC_SIZE (mem);
63
64   /* Find a likely place inside MEM with the right alignment.  */
65   addr = MALLOC_ROUND_UP (tot_addr, alignment);
66
67   /* Unless TOT_ADDR was already aligned correctly, we need to return the
68      initial part of MEM to the heap.  */
69   if (addr != tot_addr)
70     {
71       size_t init_size = addr - tot_addr;
72
73       /* Ensure that memory returned to the heap is large enough.  */
74       if (init_size < HEAP_MIN_SIZE)
75         {
76           addr = MALLOC_ROUND_UP (tot_addr + HEAP_MIN_SIZE, alignment);
77           init_size = addr - tot_addr;
78         }
79
80       __heap_free (heap, base, init_size);
81
82       /* Remember that we've freed the initial part of MEM.  */
83       base += init_size;
84     }
85
86   /* Return the end part of MEM to the heap, unless it's too small.  */
87   end_addr = addr + size;
88   if (end_addr + MALLOC_REALLOC_MIN_FREE_SIZE < tot_end_addr)
89     __heap_free (heap, (void *)end_addr, tot_end_addr - end_addr);
90   else
91     /* We didn't free the end, so include it in the size.  */
92     end_addr = tot_end_addr;
93
94   return MALLOC_SETUP (base, end_addr - (unsigned long)base);
95 }
96 libc_hidden_def(memalign)