exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / alignalloc.h
blobd33f62e29cefe32ae01f42d94c358ae7cce6342d
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. */
20 #ifndef ALIGNALLOC_H_
21 #define ALIGNALLOC_H_
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."
27 #endif
29 #include <errno.h>
30 #include <stdlib.h>
31 #include "idx.h"
32 #if defined __CHERI_PURE_CAPABILITY__
33 # include <cheri.h>
34 #endif
36 _GL_INLINE_HEADER_BEGIN
37 #ifndef ALIGNALLOC_INLINE
38 # define ALIGNALLOC_INLINE _GL_INLINE
39 #endif
41 /* Whether aligned_alloc supports any power-of-two alignment,
42 returns a nonnull pointer for size-zero allocations,
43 and sets errno on failure. */
44 #if 2 < __GLIBC__ + (16 <= __GLIBC_MINOR__)
45 # define ALIGNALLOC_VIA_ALIGNED_ALLOC 1
46 #else
47 # define ALIGNALLOC_VIA_ALIGNED_ALLOC 0
48 #endif
50 /* Work around AddressSanitizer bug.
51 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104262
52 https://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20220124/1001910.html
54 #ifdef __SANITIZE_ADDRESS__
55 # undef ALIGNALLOC_VIA_ALIGNED_ALLOC
56 # define ALIGNALLOC_VIA_ALIGNED_ALLOC 0
57 #endif
58 #ifdef __has_feature
59 # if __has_feature (address_sanitizer)
60 # undef ALIGNALLOC_VIA_ALIGNED_ALLOC
61 # define ALIGNALLOC_VIA_ALIGNED_ALLOC 0
62 # endif
63 #endif
65 #if ALIGNALLOC_VIA_ALIGNED_ALLOC || HAVE_POSIX_MEMALIGN
67 /* Free storage allocated via alignalloc. Do nothing if PTR is null. */
69 ALIGNALLOC_INLINE void
70 alignfree (void *ptr)
72 free (ptr);
75 /* Return an ALIGNMENT-aligned pointer to new storage of size SIZE,
76 or a null pointer (setting errno) if memory is exhausted.
77 ALIGNMENT must be a power of two.
78 If SIZE is zero, on success return a unique pointer each time.
79 To free storage later, call alignfree. */
81 ALIGNALLOC_INLINE
82 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((2))
83 /* _GL_ATTRIBUTE_DEALLOC (alignfree, 1) */
84 void *
85 alignalloc (idx_t alignment, idx_t size)
87 if ((size_t) -1 < alignment)
88 alignment = (size_t) -1;
89 if ((size_t) -1 < size)
90 size = (size_t) -1;
92 # if ALIGNALLOC_VIA_ALIGNED_ALLOC
93 return aligned_alloc (alignment, size);
94 # else
95 void *ptr = NULL;
96 if (alignment < sizeof (void *))
97 alignment = sizeof (void *);
98 errno = posix_memalign (&ptr, alignment, size | !size);
99 # if defined __CHERI_PURE_CAPABILITY__
100 if (ptr != NULL)
101 ptr = cheri_bounds_set (ptr, size);
102 # endif
103 return ptr;
104 # endif
107 #else /* ! (ALIGNALLOC_VIA_ALIGNED_ALLOC || HAVE_POSIX_MEMALIGN) */
109 void alignfree (void *);
110 void *alignalloc (idx_t, idx_t)
111 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((2))
112 _GL_ATTRIBUTE_DEALLOC (alignfree, 1);
114 #endif
116 /* Like alignalloc, but die instead of returning a null pointer. */
117 void *xalignalloc (idx_t, idx_t)
118 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((2))
119 _GL_ATTRIBUTE_RETURNS_NONNULL /* _GL_ATTRIBUTE_DEALLOC (alignfree, 1) */;
121 _GL_INLINE_HEADER_END
123 #endif /* !ALIGNALLOC_H_ */