1 /* safe-alloc.h: safer memory allocation
2
3 Copyright (C) 2009-2021 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>. */
17
18 /* Written by Daniel Berrange and Paul Eggert. */
19
20 #ifndef SAFE_ALLOC_H_
21 #define SAFE_ALLOC_H_
22
23 #include <stdlib.h>
24
25 #ifndef _GL_INLINE_HEADER_BEGIN
26 #error "Please include config.h first."
27 #endif
28 _GL_INLINE_HEADER_BEGIN
29 #ifndef SAFE_ALLOC_INLINE
30 # define SAFE_ALLOC_INLINE _GL_INLINE
31 #endif
32
33 /* Don't call these directly - use the macros below. */
34 SAFE_ALLOC_INLINE void *
35 safe_alloc_realloc_n (void *ptr, size_t count, size_t size)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
36 {
37 if (count == 0 || size == 0)
38 count = size = 1;
39 return reallocarray (ptr, count, size);
40 }
41 _GL_ATTRIBUTE_NODISCARD SAFE_ALLOC_INLINE int
42 safe_alloc_check (void *ptr)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
43 {
44 /* Return 0 if the allocation was successful, -1 otherwise. */
45 return -!ptr;
46 }
47
48 /**
49 * ALLOC:
50 * @ptr: pointer to allocated memory
51 *
52 * Allocate sizeof *ptr bytes of memory and store
53 * the address of allocated memory in 'ptr'. Fill the
54 * newly allocated memory with zeros.
55 *
56 * Return -1 on failure to allocate, zero on success.
57 */
58 #define ALLOC(ptr) ALLOC_N (ptr, 1)
59
60 /**
61 * ALLOC_N:
62 * @ptr: pointer to allocated memory
63 * @count: number of elements to allocate
64 *
65 * Allocate an array of 'count' elements, each sizeof *ptr
66 * bytes long and store the address of allocated memory in
67 * 'ptr'. Fill the newly allocated memory with zeros.
68 *
69 * Return -1 on failure, 0 on success.
70 */
71 #define ALLOC_N(ptr, count) \
72 safe_alloc_check ((ptr) = calloc (count, sizeof *(ptr)))
73
74 /**
75 * ALLOC_N_UNINITIALIZED:
76 * @ptr: pointer to allocated memory
77 * @count: number of elements to allocate
78 *
79 * Allocate an array of 'count' elements, each sizeof *ptr
80 * bytes long and store the address of allocated memory in
81 * 'ptr'. Do not initialize the new memory at all.
82 *
83 * Return -1 on failure to allocate, zero on success.
84 */
85 #define ALLOC_N_UNINITIALIZED(ptr, count) \
86 safe_alloc_check ((ptr) = safe_alloc_realloc_n (NULL, count, sizeof *(ptr)))
87
88 /**
89 * REALLOC_N:
90 * @ptr: pointer to allocated memory
91 * @count: number of elements to allocate
92 *
93 * Re-allocate an array of 'count' elements, each sizeof *ptr
94 * bytes long and store the address of allocated memory in
95 * 'ptr'. Fill the newly allocated memory with zeros.
96 *
97 * Return -1 on failure to reallocate, zero on success.
98 */
99 #define REALLOC_N(ptr, count) \
100 safe_alloc_check ((ptr) = safe_alloc_realloc_n (ptr, count, sizeof *(ptr)))
101
102 /**
103 * FREE:
104 * @ptr: pointer holding address to be freed
105 *
106 * Free the memory stored in 'ptr' and update to point
107 * to NULL.
108 */
109 #define FREE(ptr) ((void) (free (ptr), (ptr) = NULL))
110
111 _GL_INLINE_HEADER_END
112
113 #endif /* SAFE_ALLOC_H_ */