openat: don’t close (-1)
[gnulib.git] / lib / alignalloc.h
blob229ccf5dd95b29e098f219cea9964a5a2745cb9c
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 #ifdef __cplusplus
42 extern "C" {
43 #endif
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
51 #else
52 # define ALIGNALLOC_VIA_ALIGNED_ALLOC 0
53 #endif
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
62 #endif
63 #ifdef __has_feature
64 # if __has_feature (address_sanitizer)
65 # undef ALIGNALLOC_VIA_ALIGNED_ALLOC
66 # define ALIGNALLOC_VIA_ALIGNED_ALLOC 0
67 # endif
68 #endif
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
75 alignfree (void *ptr)
77 free (ptr);
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. */
86 ALIGNALLOC_INLINE
87 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((2))
88 /* _GL_ATTRIBUTE_DEALLOC (alignfree, 1) */
89 void *
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)
95 size = (size_t) -1;
97 # if ALIGNALLOC_VIA_ALIGNED_ALLOC
98 return aligned_alloc (alignment, size);
99 # else
100 void *ptr = NULL;
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__
107 if (ptr != NULL)
108 ptr = cheri_bounds_set (ptr, size);
109 # endif
110 return ptr;
111 # endif
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);
121 #endif
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) */;
129 #ifdef __cplusplus
131 #endif
133 _GL_INLINE_HEADER_END
135 #endif /* !ALIGNALLOC_H_ */