Avoid "warning: The macro `AC_DECL_SYS_SIGLIST' is obsolete".
[gnulib.git] / lib / safe-alloc.h
blob9bf1eca4313ee1b2f857c7f9780ee83e6a64ac67
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
8 later version.
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 */
20 #ifndef SAFE_ALLOC_H_
21 # define SAFE_ALLOC_H_
23 # include <stdlib.h>
25 /* Don't call these directly - use the macros below */
26 int
27 safe_alloc_alloc_n (void *ptrptr, size_t size, size_t count, int zeroed)
28 _GL_ATTRIBUTE_NODISCARD;
30 int
31 safe_alloc_realloc_n (void *ptrptr, size_t size, size_t count)
32 _GL_ATTRIBUTE_NODISCARD;
34 /**
35 * ALLOC:
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
44 # define ALLOC(ptr) \
45 safe_alloc_alloc_n (&(ptr), sizeof (*(ptr)), 1, 1)
47 /**
48 * ALLOC_N:
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)
61 /**
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)
75 /**
76 * REALLOC_N:
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))
89 /**
90 * FREE:
91 * @ptr: pointer holding address to be freed
93 * Free the memory stored in 'ptr' and update to point
94 * to NULL.
96 # define FREE(ptr) \
97 do \
98 { \
99 free (ptr); \
100 (ptr) = NULL; \
102 while (0)
104 #endif /* SAFE_ALLOC_H_ */