exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / safe-alloc.h
blobf7b0e5fb5153c38a71d9c2c1dce5e4bbc48d5ed3
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. */
20 #ifndef SAFE_ALLOC_H_
21 #define SAFE_ALLOC_H_
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."
27 #endif
29 #include <stdlib.h>
30 #if defined __CHERI_PURE_CAPABILITY__
31 # include <cheri.h>
32 #endif
34 _GL_INLINE_HEADER_BEGIN
35 #ifndef SAFE_ALLOC_INLINE
36 # define SAFE_ALLOC_INLINE _GL_INLINE
37 #endif
39 /* Don't call these directly - use the macros below. */
40 SAFE_ALLOC_INLINE void *
41 safe_alloc_realloc_n (void *ptr, size_t count, size_t size)
43 size_t countx = count;
44 size_t sizex = size;
45 if (count == 0 || size == 0)
46 countx = sizex = 1;
47 ptr = reallocarray (ptr, countx, sizex);
48 #if defined __CHERI_PURE_CAPABILITY__
49 if (ptr != NULL && (count == 0 || size == 0))
50 ptr = cheri_bounds_set (ptr, 0);
51 #endif
52 return ptr;
54 _GL_ATTRIBUTE_NODISCARD SAFE_ALLOC_INLINE int
55 safe_alloc_check (void *ptr)
57 /* Return 0 if the allocation was successful, -1 otherwise. */
58 return -!ptr;
61 /**
62 * ALLOC:
63 * @ptr: pointer to allocated memory
65 * Allocate sizeof *ptr bytes of memory and store
66 * the address of allocated memory in 'ptr'. Fill the
67 * newly allocated memory with zeros.
69 * Return -1 on failure to allocate, zero on success.
71 #define ALLOC(ptr) ALLOC_N (ptr, 1)
73 /**
74 * ALLOC_N:
75 * @ptr: pointer to allocated memory
76 * @count: number of elements to allocate
78 * Allocate an array of 'count' elements, each sizeof *ptr
79 * bytes long and store the address of allocated memory in
80 * 'ptr'. Fill the newly allocated memory with zeros.
82 * Return -1 on failure, 0 on success.
84 #define ALLOC_N(ptr, count) \
85 safe_alloc_check ((ptr) = calloc (count, sizeof *(ptr)))
87 /**
88 * ALLOC_N_UNINITIALIZED:
89 * @ptr: pointer to allocated memory
90 * @count: number of elements to allocate
92 * Allocate an array of 'count' elements, each sizeof *ptr
93 * bytes long and store the address of allocated memory in
94 * 'ptr'. Do not initialize the new memory at all.
96 * Return -1 on failure to allocate, zero on success.
98 #define ALLOC_N_UNINITIALIZED(ptr, count) \
99 safe_alloc_check ((ptr) = safe_alloc_realloc_n (NULL, count, sizeof *(ptr)))
102 * REALLOC_N:
103 * @ptr: pointer to allocated memory
104 * @count: number of elements to allocate
106 * Re-allocate an array of 'count' elements, each sizeof *ptr
107 * bytes long and store the address of allocated memory in
108 * 'ptr'. Fill the newly allocated memory with zeros.
110 * Return -1 on failure to reallocate, zero on success.
112 #define REALLOC_N(ptr, count) \
113 safe_alloc_check ((ptr) = safe_alloc_realloc_n (ptr, count, sizeof *(ptr)))
116 * FREE:
117 * @ptr: pointer holding address to be freed
119 * Free the memory stored in 'ptr' and update to point
120 * to NULL.
122 #define FREE(ptr) ((void) (free (ptr), (ptr) = NULL))
124 _GL_INLINE_HEADER_END
126 #endif /* SAFE_ALLOC_H_ */