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. */
23 /* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE, _GL_ATTRIBUTE_ALLOC_SIZE,
24 _GL_ATTRIBUTE_MALLOC, _GL_ATTRIBUTE_RETURNS_NONNULL, HAVE_POSIX_MEMALIGN. */
25 #if !_GL_CONFIG_H_INCLUDED
26 #error "Please include config.h first."
32 #if defined __CHERI_PURE_CAPABILITY__
36 _GL_INLINE_HEADER_BEGIN
37 #ifndef ALIGNALLOC_INLINE
38 # define ALIGNALLOC_INLINE _GL_INLINE
46 /* Whether aligned_alloc supports any power-of-two alignment,
47 returns a nonnull pointer for size-zero allocations,
48 and sets errno on failure. */
49 #if 2 < __GLIBC__ + (16 <= __GLIBC_MINOR__)
50 # define ALIGNALLOC_VIA_ALIGNED_ALLOC 1
52 # define ALIGNALLOC_VIA_ALIGNED_ALLOC 0
55 /* Work around AddressSanitizer bug.
56 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104262
57 https://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20220124/1001910.html
59 #ifdef __SANITIZE_ADDRESS__
60 # undef ALIGNALLOC_VIA_ALIGNED_ALLOC
61 # define ALIGNALLOC_VIA_ALIGNED_ALLOC 0
64 # if __has_feature (address_sanitizer)
65 # undef ALIGNALLOC_VIA_ALIGNED_ALLOC
66 # define ALIGNALLOC_VIA_ALIGNED_ALLOC 0
70 #if ALIGNALLOC_VIA_ALIGNED_ALLOC || HAVE_POSIX_MEMALIGN
72 /* Free storage allocated via alignalloc. Do nothing if PTR is null. */
74 ALIGNALLOC_INLINE
void
80 /* Return an ALIGNMENT-aligned pointer to new storage of size SIZE,
81 or a null pointer (setting errno) if memory is exhausted.
82 ALIGNMENT must be a power of two.
83 If SIZE is zero, on success return a unique pointer each time.
84 To free storage later, call alignfree. */
87 _GL_ATTRIBUTE_MALLOC
_GL_ATTRIBUTE_ALLOC_SIZE ((2))
88 /* _GL_ATTRIBUTE_DEALLOC (alignfree, 1) */
90 alignalloc (idx_t alignment
, idx_t size
)
92 if ((size_t) -1 < alignment
)
93 alignment
= (size_t) -1;
94 if ((size_t) -1 < size
)
97 # if ALIGNALLOC_VIA_ALIGNED_ALLOC
98 return aligned_alloc (alignment
, size
);
101 if (alignment
< sizeof (void *))
102 alignment
= sizeof (void *);
103 /* Work around posix_memalign glitch by treating a 0 size as if it were 1,
104 so that returning NULL is equivalent to failing. */
105 errno
= posix_memalign (&ptr
, alignment
, size
? size
: 1);
106 # if defined __CHERI_PURE_CAPABILITY__
108 ptr
= cheri_bounds_set (ptr
, size
);
114 #else /* ! (ALIGNALLOC_VIA_ALIGNED_ALLOC || HAVE_POSIX_MEMALIGN) */
116 void alignfree (void *);
117 void *alignalloc (idx_t
, idx_t
)
118 _GL_ATTRIBUTE_MALLOC
_GL_ATTRIBUTE_ALLOC_SIZE ((2))
119 _GL_ATTRIBUTE_DEALLOC (alignfree
, 1);
123 /* Like alignalloc, but die instead of returning a null pointer. */
124 void *xalignalloc (idx_t
, idx_t
)
125 _GL_ATTRIBUTE_MALLOC
_GL_ATTRIBUTE_ALLOC_SIZE ((2))
126 _GL_ATTRIBUTE_RETURNS_NONNULL
/* _GL_ATTRIBUTE_DEALLOC (alignfree, 1) */;
133 _GL_INLINE_HEADER_END
135 #endif /* !ALIGNALLOC_H_ */