1 /* aligned memory allocation
2
3 Copyright 2022-2023 Free Software Foundation, Inc.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program 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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 /* Written by Paul Eggert. */
19
20 #ifndef ALIGNALLOC_H_
21 #define ALIGNALLOC_H_
22
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
28
29 #include <errno.h>
30 #include <stdlib.h>
31 #include "idx.h"
32
33 _GL_INLINE_HEADER_BEGIN
34 #ifndef ALIGNALLOC_INLINE
35 # define ALIGNALLOC_INLINE _GL_INLINE
36 #endif
37
38 /* Whether aligned_alloc supports any power-of-two alignment,
39 returns a nonnull pointer for size-zero allocations,
40 and sets errno on failure. */
41 #if 2 < __GLIBC__ + (16 <= __GLIBC_MINOR__)
42 # define ALIGNALLOC_VIA_ALIGNED_ALLOC 1
43 #else
44 # define ALIGNALLOC_VIA_ALIGNED_ALLOC 0
45 #endif
46
47 /* Work around AddressSanitizer bug.
48 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104262
49 https://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20220124/1001910.html
50 */
51 #ifdef __SANITIZE_ADDRESS__
52 # undef ALIGNALLOC_VIA_ALIGNED_ALLOC
53 # define ALIGNALLOC_VIA_ALIGNED_ALLOC 0
54 #endif
55 #ifdef __has_feature
56 # if __has_feature (address_sanitizer)
57 # undef ALIGNALLOC_VIA_ALIGNED_ALLOC
58 # define ALIGNALLOC_VIA_ALIGNED_ALLOC 0
59 # endif
60 #endif
61
62 #if ALIGNALLOC_VIA_ALIGNED_ALLOC || HAVE_POSIX_MEMALIGN
63
64 /* Free storage allocated via alignalloc. Do nothing if PTR is null. */
65
66 ALIGNALLOC_INLINE void
67 alignfree (void *ptr)
68 {
69 free (ptr);
70 }
71
72 /* Return an ALIGNMENT-aligned pointer to new storage of size SIZE,
73 or a null pointer (setting errno) if memory is exhausted.
74 ALIGNMENT must be a power of two.
75 If SIZE is zero, on success return a unique pointer each time.
76 To free storage later, call alignfree. */
77
78 ALIGNALLOC_INLINE
79 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((2))
80 /* _GL_ATTRIBUTE_DEALLOC (alignfree, 1) */
81 void *
82 alignalloc (idx_t alignment, idx_t size)
83 {
84 if ((size_t) -1 < alignment)
85 alignment = (size_t) -1;
86 if ((size_t) -1 < size)
87 size = (size_t) -1;
88
89 # if ALIGNALLOC_VIA_ALIGNED_ALLOC
90 return aligned_alloc (alignment, size);
91 # else
92 void *ptr = NULL;
93 if (alignment < sizeof (void *))
94 alignment = sizeof (void *);
95 errno = posix_memalign (&ptr, alignment, size | !size);
96 return ptr;
97 # endif
98 }
99
100 #else /* ! (ALIGNALLOC_VIA_ALIGNED_ALLOC || HAVE_POSIX_MEMALIGN) */
101
102 void alignfree (void *);
103 void *alignalloc (idx_t, idx_t)
104 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((2))
105 _GL_ATTRIBUTE_DEALLOC (alignfree, 1);
106
107 #endif
108
109 /* Like alignalloc, but die instead of returning a null pointer. */
110 void *xalignalloc (idx_t, idx_t)
111 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((2))
112 _GL_ATTRIBUTE_RETURNS_NONNULL /* _GL_ATTRIBUTE_DEALLOC (alignfree, 1) */;
113
114 _GL_INLINE_HEADER_END
115
116 #endif /* !ALIGNALLOC_H_ */