1 /* safe-alloc.h: safer memory allocation
3 Copyright (C) 2009-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 Daniel Berrange and Paul Eggert. */
23 /* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE,
24 _GL_ATTRIBUTE_NODISCARD. */
25 #if !_GL_CONFIG_H_INCLUDED
26 #error "Please include config.h first."
30 #if defined __CHERI_PURE_CAPABILITY__
34 _GL_INLINE_HEADER_BEGIN
35 #ifndef SAFE_ALLOC_INLINE
36 # define SAFE_ALLOC_INLINE _GL_INLINE
44 /* Don't call these directly - use the macros below. */
45 SAFE_ALLOC_INLINE
void *
46 safe_alloc_realloc_n (void *ptr
, size_t count
, size_t size
)
48 size_t countx
= count
;
50 if (count
== 0 || size
== 0)
52 ptr
= reallocarray (ptr
, countx
, sizex
);
53 #if defined __CHERI_PURE_CAPABILITY__
54 if (ptr
!= NULL
&& (count
== 0 || size
== 0))
55 ptr
= cheri_bounds_set (ptr
, 0);
59 _GL_ATTRIBUTE_NODISCARD SAFE_ALLOC_INLINE
int
60 safe_alloc_check (void *ptr
)
62 /* Return 0 if the allocation was successful, -1 otherwise. */
68 * @ptr: pointer to allocated memory
70 * Allocate sizeof *ptr bytes of memory and store
71 * the address of allocated memory in 'ptr'. Fill the
72 * newly allocated memory with zeros.
74 * Return -1 on failure to allocate, zero on success.
76 #define ALLOC(ptr) ALLOC_N (ptr, 1)
80 * @ptr: pointer to allocated memory
81 * @count: number of elements to allocate
83 * Allocate an array of 'count' elements, each sizeof *ptr
84 * bytes long and store the address of allocated memory in
85 * 'ptr'. Fill the newly allocated memory with zeros.
87 * Return -1 on failure, 0 on success.
89 #define ALLOC_N(ptr, count) \
90 safe_alloc_check ((ptr) = calloc (count, sizeof *(ptr)))
93 * ALLOC_N_UNINITIALIZED:
94 * @ptr: pointer to allocated memory
95 * @count: number of elements to allocate
97 * Allocate an array of 'count' elements, each sizeof *ptr
98 * bytes long and store the address of allocated memory in
99 * 'ptr'. Do not initialize the new memory at all.
101 * Return -1 on failure to allocate, zero on success.
103 #define ALLOC_N_UNINITIALIZED(ptr, count) \
104 safe_alloc_check ((ptr) = safe_alloc_realloc_n (NULL, count, sizeof *(ptr)))
108 * @ptr: pointer to allocated memory
109 * @count: number of elements to allocate
111 * Re-allocate an array of 'count' elements, each sizeof *ptr
112 * bytes long and store the address of allocated memory in
113 * 'ptr'. Fill the newly allocated memory with zeros.
115 * Return -1 on failure to reallocate, zero on success.
117 #define REALLOC_N(ptr, count) \
118 safe_alloc_check ((ptr) = safe_alloc_realloc_n (ptr, count, sizeof *(ptr)))
122 * @ptr: pointer holding address to be freed
124 * Free the memory stored in 'ptr' and update to point
127 #define FREE(ptr) ((void) (free (ptr), (ptr) = NULL))
134 _GL_INLINE_HEADER_END
136 #endif /* SAFE_ALLOC_H_ */