reallocarray: Don't assume unportable behaviour of realloc.
[gnulib.git] / lib / safe-alloc.h
blobbee973ed4d0e452f67ccbc44d5f38500bdbb75c6
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 #ifdef __cplusplus
40 extern "C" {
41 #endif
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;
49 size_t sizex = size;
50 if (count == 0 || size == 0)
51 countx = sizex = 1;
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);
56 #endif
57 return ptr;
59 _GL_ATTRIBUTE_NODISCARD SAFE_ALLOC_INLINE int
60 safe_alloc_check (void *ptr)
62 /* Return 0 if the allocation was successful, -1 otherwise. */
63 return -!ptr;
66 /**
67 * ALLOC:
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)
78 /**
79 * ALLOC_N:
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)))
92 /**
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)))
107 * REALLOC_N:
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)))
121 * FREE:
122 * @ptr: pointer holding address to be freed
124 * Free the memory stored in 'ptr' and update to point
125 * to NULL.
127 #define FREE(ptr) ((void) (free (ptr), (ptr) = NULL))
130 #ifdef __cplusplus
132 #endif
134 _GL_INLINE_HEADER_END
136 #endif /* SAFE_ALLOC_H_ */