2 * libc/stdlib/malloc/memalign.c -- memalign (`aligned malloc') function
4 * Copyright (C) 2002 NEC Corporation
5 * Copyright (C) 2002 Miles Bader <miles@gnu.org>
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.
11 * Written by Miles Bader <miles@gnu.org>
23 ______________________ TOTAL _________________________
25 +---------------+-------------------------+--------------+
27 +---------------+-------------------------+--------------+
28 \____ INIT ____/ \______ RETURNED _______/ \____ END ___/
31 void *memalign (size_t alignment, size_t size);
32 /* XXX shadow outer malloc.h */
33 libc_hidden_proto(memalign)
35 memalign (size_t alignment, size_t size)
38 unsigned long tot_addr, tot_end_addr, addr, end_addr;
39 struct heap_free_area **heap = &__malloc_heap;
41 /* Make SIZE something we like. */
42 size = HEAP_ADJUST_SIZE (size);
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);
50 /* Allocation failed, we can't do anything. */
52 if (alignment < MALLOC_ALIGNMENT)
55 /* Remember the base-address, of the allocation, although we normally
56 use the user-address for calculations, since that's where the
58 base = MALLOC_BASE (mem);
60 /* The bounds of the initial allocation. */
61 tot_addr = (unsigned long)mem;
62 tot_end_addr = (unsigned long)base + MALLOC_SIZE (mem);
64 /* Find a likely place inside MEM with the right alignment. */
65 addr = MALLOC_ROUND_UP (tot_addr, alignment);
67 /* Unless TOT_ADDR was already aligned correctly, we need to return the
68 initial part of MEM to the heap. */
71 size_t init_size = addr - tot_addr;
73 /* Ensure that memory returned to the heap is large enough. */
74 if (init_size < HEAP_MIN_SIZE)
76 addr = MALLOC_ROUND_UP (tot_addr + HEAP_MIN_SIZE, alignment);
77 init_size = addr - tot_addr;
80 __heap_free (heap, base, init_size);
82 /* Remember that we've freed the initial part of MEM. */
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);
91 /* We didn't free the end, so include it in the size. */
92 end_addr = tot_end_addr;
94 return MALLOC_SETUP (base, end_addr - (unsigned long)base);
96 libc_hidden_def(memalign)