Fix last ChangeLog entry.
[gnulib.git] / lib / safe-alloc.h
blob58970bd00ad92e8374da3e2fb9c88cdb91710f8a
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 #ifndef __GNUC_PREREQ
26 # if defined __GNUC__ && defined __GNUC_MINOR__
27 # define __GNUC_PREREQ(maj, min) \
28 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
29 # else
30 # define __GNUC_PREREQ(maj, min) 0
31 # endif
32 #endif
34 /* Don't call these directly - use the macros below */
35 int
36 safe_alloc_alloc_n (void *ptrptr, size_t size, size_t count, int zeroed)
37 _GL_ATTRIBUTE_NODISCARD;
39 int
40 safe_alloc_realloc_n (void *ptrptr, size_t size, size_t count)
41 _GL_ATTRIBUTE_NODISCARD;
43 /**
44 * ALLOC:
45 * @ptr: pointer to hold address of allocated memory
47 * Allocate sizeof(*ptr) bytes of memory and store
48 * the address of allocated memory in 'ptr'. Fill the
49 * newly allocated memory with zeros.
51 * Return -1 on failure to allocate, zero on success
53 # define ALLOC(ptr) \
54 safe_alloc_alloc_n (&(ptr), sizeof (*(ptr)), 1, 1)
56 /**
57 * ALLOC_N:
58 * @ptr: pointer to hold address of allocated memory
59 * @count: number of elements to allocate
61 * Allocate an array of 'count' elements, each sizeof(*ptr)
62 * bytes long and store the address of allocated memory in
63 * 'ptr'. Fill the newly allocated memory with zeros.
65 * Return -1 on failure, 0 on success
67 # define ALLOC_N(ptr, count) \
68 safe_alloc_alloc_n (&(ptr), sizeof (*(ptr)), (count), 1)
70 /**
71 * ALLOC_N_UNINITIALIZED:
72 * @ptr: pointer to hold address of allocated memory
73 * @count: number of elements to allocate
75 * Allocate an array of 'count' elements, each sizeof(*ptr)
76 * bytes long and store the address of allocated memory in
77 * 'ptr'. Do not initialize the new memory at all.
79 * Return -1 on failure to allocate, zero on success
81 # define ALLOC_N_UNINITIALIZED(ptr, count) \
82 safe_alloc_alloc_n (&(ptr), sizeof (*(ptr)), (count), 0)
84 /**
85 * REALLOC_N:
86 * @ptr: pointer to hold address of allocated memory
87 * @count: number of elements to allocate
89 * Re-allocate an array of 'count' elements, each sizeof(*ptr)
90 * bytes long and store the address of allocated memory in
91 * 'ptr'. Fill the newly allocated memory with zeros
93 * Return -1 on failure to reallocate, zero on success
95 # define REALLOC_N(ptr, count) \
96 safe_alloc_realloc_n (&(ptr), sizeof (*(ptr)), (count))
98 /**
99 * FREE:
100 * @ptr: pointer holding address to be freed
102 * Free the memory stored in 'ptr' and update to point
103 * to NULL.
105 # define FREE(ptr) \
106 do \
108 free (ptr); \
109 (ptr) = NULL; \
111 while (0)
113 #endif /* SAFE_ALLOC_H_ */