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