1 /* safe-alloc.h: safer memory allocation
3 Copyright (C) 2009-2020 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3 of the License, or any
10 This program 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Daniel Berrange <berrange@redhat.com>, 2008 */
21 # define SAFE_ALLOC_H_
25 /* Don't call these directly - use the macros below */
27 safe_alloc_alloc_n (void *ptrptr
, size_t size
, size_t count
, int zeroed
)
28 _GL_ATTRIBUTE_NODISCARD
;
31 safe_alloc_realloc_n (void *ptrptr
, size_t size
, size_t count
)
32 _GL_ATTRIBUTE_NODISCARD
;
36 * @ptr: pointer to hold address of allocated memory
38 * Allocate sizeof(*ptr) bytes of memory and store
39 * the address of allocated memory in 'ptr'. Fill the
40 * newly allocated memory with zeros.
42 * Return -1 on failure to allocate, zero on success
45 safe_alloc_alloc_n (&(ptr), sizeof (*(ptr)), 1, 1)
49 * @ptr: pointer to hold address of allocated memory
50 * @count: number of elements to allocate
52 * Allocate an array of 'count' elements, each sizeof(*ptr)
53 * bytes long and store the address of allocated memory in
54 * 'ptr'. Fill the newly allocated memory with zeros.
56 * Return -1 on failure, 0 on success
58 # define ALLOC_N(ptr, count) \
59 safe_alloc_alloc_n (&(ptr), sizeof (*(ptr)), (count), 1)
62 * ALLOC_N_UNINITIALIZED:
63 * @ptr: pointer to hold address of allocated memory
64 * @count: number of elements to allocate
66 * Allocate an array of 'count' elements, each sizeof(*ptr)
67 * bytes long and store the address of allocated memory in
68 * 'ptr'. Do not initialize the new memory at all.
70 * Return -1 on failure to allocate, zero on success
72 # define ALLOC_N_UNINITIALIZED(ptr, count) \
73 safe_alloc_alloc_n (&(ptr), sizeof (*(ptr)), (count), 0)
77 * @ptr: pointer to hold address of allocated memory
78 * @count: number of elements to allocate
80 * Re-allocate an array of 'count' elements, each sizeof(*ptr)
81 * bytes long and store the address of allocated memory in
82 * 'ptr'. Fill the newly allocated memory with zeros
84 * Return -1 on failure to reallocate, zero on success
86 # define REALLOC_N(ptr, count) \
87 safe_alloc_realloc_n (&(ptr), sizeof (*(ptr)), (count))
91 * @ptr: pointer holding address to be freed
93 * Free the memory stored in 'ptr' and update to point
104 #endif /* SAFE_ALLOC_H_ */