1 /* aligned memory allocation
3 Copyright 2022-2024 Free Software Foundation, Inc.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file 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
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert. */
22 #define ALIGNALLOC_INLINE _GL_EXTERN_INLINE
23 #include "alignalloc.h"
26 #include <stdckdint.h>
29 #if !ALIGNALLOC_VIA_ALIGNED_ALLOC
30 # if HAVE_POSIX_MEMALIGN
32 /* posix_memalign requires the alignment to be a power-of-two multiple of
33 sizeof (void *), whereas alignalloc requires it to be a power of two.
34 To make it OK for the latter to call the former, check that
35 sizeof (void *) is a power of two, which is true on all known platforms.
36 This check is here rather than in alignalloc.h to save the compiler
37 the trouble of checking it each time alignalloc.h is included. */
38 static_assert (! (sizeof (void *) & (sizeof (void *) - 1)));
40 # else /* !HAVE_POSIX_MEMALIGN */
42 /* Return P aligned down to ALIGNMENT, which should be a power of two. */
45 align_down (void *p
, idx_t alignment
)
48 return c
- ((uintptr_t) p
& (alignment
- 1));
51 /* If alignalloc returned R and the base of the originally-allocated
52 storage is less than R - UCHAR_MAX, return the address of a pointer
53 holding the base of the originally-allocated storage. */
56 address_of_pointer_to_malloced (unsigned char *r
)
58 /* The pointer P is located at the highest address A such that A is
59 aligned for pointers, and A + sizeof P < R so that there is room
60 for a 0 byte at R - 1. This approach assumes UCHAR_MAX is large
61 enough so that there is room for P; although true on all
62 plausible platforms, check the assumption to be safe. */
63 static_assert (sizeof (void *) + alignof (void *) - 1 <= UCHAR_MAX
);
65 return align_down (r
- 1 - sizeof (void *), alignof (void *));
68 /* Return an ALIGNMENT-aligned pointer to new storage of size SIZE,
69 or a null pointer (setting errno) if memory is exhausted.
70 ALIGNMENT must be a power of two.
71 If SIZE is zero, on success return a unique pointer each time.
72 To free storage later, call alignfree. */
75 alignalloc (idx_t alignment
, idx_t size
)
77 /* malloc (ALIGNMENT + SIZE); if it succeeds, there must be at least
78 one byte available before the returned pointer. It's OK if
79 ALIGNMENT + SIZE fits in size_t but not idx_t. */
83 if (ckd_add (&malloc_size
, size
, alignment
)
84 || ! (q
= malloc (malloc_size
)))
90 unsigned char *r
= align_down (q
+ alignment
, alignment
);
93 if (offset
<= UCHAR_MAX
)
98 *address_of_pointer_to_malloced (r
) = q
;
104 /* Free storage allocated via alignalloc. Do nothing if PTR is null. */
107 alignfree (void *ptr
)
111 unsigned char *r
= ptr
;
112 unsigned char offset
= r
[-1];
113 void *q
= offset
? r
- offset
: *address_of_pointer_to_malloced (r
);
118 # endif /* ! HAVE_POSIX_MEMALIGN */
119 #endif /* ! ALIGNALLOC_VIA_ALIGNED_ALLOC */