[3611] | 1 | /* xalloc.h -- malloc with out-of-memory checking
|
---|
| 2 |
|
---|
| 3 | Copyright (C) 1990-2000, 2003-2004, 2006-2022 Free Software Foundation, Inc.
|
---|
| 4 |
|
---|
| 5 | This program is free software: you can redistribute it and/or modify
|
---|
| 6 | it under the terms of the GNU General Public License as published by
|
---|
| 7 | the Free Software Foundation, either version 3 of the License, or
|
---|
| 8 | (at your option) any later version.
|
---|
| 9 |
|
---|
| 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.
|
---|
| 14 |
|
---|
| 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/>. */
|
---|
| 17 |
|
---|
| 18 | #ifndef XALLOC_H_
|
---|
| 19 | #define XALLOC_H_
|
---|
| 20 |
|
---|
| 21 | #include <stddef.h>
|
---|
| 22 | #include <stdlib.h>
|
---|
| 23 |
|
---|
| 24 | #if GNULIB_XALLOC
|
---|
| 25 | # include "idx.h"
|
---|
| 26 | #endif
|
---|
| 27 |
|
---|
| 28 | #ifndef _GL_INLINE_HEADER_BEGIN
|
---|
| 29 | #error "Please include config.h first."
|
---|
| 30 | #endif
|
---|
| 31 | _GL_INLINE_HEADER_BEGIN
|
---|
| 32 | #ifndef XALLOC_INLINE
|
---|
| 33 | # define XALLOC_INLINE _GL_INLINE
|
---|
| 34 | #endif
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 | #ifdef __cplusplus
|
---|
| 38 | extern "C" {
|
---|
| 39 | #endif
|
---|
| 40 |
|
---|
| 41 |
|
---|
| 42 | #if GNULIB_XALLOC_DIE
|
---|
| 43 |
|
---|
| 44 | /* This function is always triggered when memory is exhausted.
|
---|
| 45 | It must be defined by the application, either explicitly
|
---|
| 46 | or by using gnulib's xalloc-die module. This is the
|
---|
| 47 | function to call when one wants the program to die because of a
|
---|
| 48 | memory allocation failure. */
|
---|
| 49 | /*extern*/ _Noreturn void xalloc_die (void);
|
---|
| 50 |
|
---|
| 51 | #endif /* GNULIB_XALLOC_DIE */
|
---|
| 52 |
|
---|
| 53 | #if GNULIB_XALLOC
|
---|
| 54 |
|
---|
| 55 | void *xmalloc (size_t s)
|
---|
| 56 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
|
---|
| 57 | _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
|
---|
| 58 | void *ximalloc (idx_t s)
|
---|
| 59 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
|
---|
| 60 | _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
|
---|
| 61 | void *xinmalloc (idx_t n, idx_t s)
|
---|
| 62 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
|
---|
| 63 | _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
|
---|
| 64 | void *xzalloc (size_t s)
|
---|
| 65 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
|
---|
| 66 | _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
|
---|
| 67 | void *xizalloc (idx_t s)
|
---|
| 68 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
|
---|
| 69 | _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
|
---|
| 70 | void *xcalloc (size_t n, size_t s)
|
---|
| 71 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
|
---|
| 72 | _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
|
---|
| 73 | void *xicalloc (idx_t n, idx_t s)
|
---|
| 74 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
|
---|
| 75 | _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
|
---|
| 76 | void *xrealloc (void *p, size_t s)
|
---|
| 77 | _GL_ATTRIBUTE_ALLOC_SIZE ((2));
|
---|
| 78 | void *xirealloc (void *p, idx_t s)
|
---|
| 79 | _GL_ATTRIBUTE_ALLOC_SIZE ((2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
|
---|
| 80 | void *xreallocarray (void *p, size_t n, size_t s)
|
---|
| 81 | _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
|
---|
| 82 | void *xireallocarray (void *p, idx_t n, idx_t s)
|
---|
| 83 | _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3)) _GL_ATTRIBUTE_RETURNS_NONNULL;
|
---|
| 84 | void *x2realloc (void *p, size_t *ps) /* superseded by xpalloc */
|
---|
| 85 | _GL_ATTRIBUTE_RETURNS_NONNULL;
|
---|
| 86 | void *x2nrealloc (void *p, size_t *pn, size_t s) /* superseded by xpalloc */
|
---|
| 87 | _GL_ATTRIBUTE_RETURNS_NONNULL;
|
---|
| 88 | void *xpalloc (void *pa, idx_t *pn, idx_t n_incr_min, ptrdiff_t n_max, idx_t s)
|
---|
| 89 | _GL_ATTRIBUTE_RETURNS_NONNULL;
|
---|
| 90 | void *xmemdup (void const *p, size_t s)
|
---|
| 91 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
|
---|
| 92 | _GL_ATTRIBUTE_ALLOC_SIZE ((2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
|
---|
| 93 | void *ximemdup (void const *p, idx_t s)
|
---|
| 94 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
|
---|
| 95 | _GL_ATTRIBUTE_ALLOC_SIZE ((2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
|
---|
| 96 | char *ximemdup0 (void const *p, idx_t s)
|
---|
| 97 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
|
---|
| 98 | _GL_ATTRIBUTE_RETURNS_NONNULL;
|
---|
| 99 | char *xstrdup (char const *str)
|
---|
| 100 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
|
---|
| 101 | _GL_ATTRIBUTE_RETURNS_NONNULL;
|
---|
| 102 |
|
---|
| 103 | /* In the following macros, T must be an elementary or structure/union or
|
---|
| 104 | typedef'ed type, or a pointer to such a type. To apply one of the
|
---|
| 105 | following macros to a function pointer or array type, you need to typedef
|
---|
| 106 | it first and use the typedef name. */
|
---|
| 107 |
|
---|
| 108 | /* Allocate an object of type T dynamically, with error checking. */
|
---|
| 109 | /* extern t *XMALLOC (typename t); */
|
---|
| 110 | # define XMALLOC(t) ((t *) xmalloc (sizeof (t)))
|
---|
| 111 |
|
---|
| 112 | /* Allocate memory for N elements of type T, with error checking. */
|
---|
| 113 | /* extern t *XNMALLOC (size_t n, typename t); */
|
---|
| 114 | # define XNMALLOC(n, t) \
|
---|
| 115 | ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t))))
|
---|
| 116 |
|
---|
| 117 | /* Allocate an object of type T dynamically, with error checking,
|
---|
| 118 | and zero it. */
|
---|
| 119 | /* extern t *XZALLOC (typename t); */
|
---|
| 120 | # define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
|
---|
| 121 |
|
---|
| 122 | /* Allocate memory for N elements of type T, with error checking,
|
---|
| 123 | and zero it. */
|
---|
| 124 | /* extern t *XCALLOC (size_t n, typename t); */
|
---|
| 125 | # define XCALLOC(n, t) \
|
---|
| 126 | ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
|
---|
| 127 |
|
---|
| 128 |
|
---|
| 129 | /* Allocate an array of N objects, each with S bytes of memory,
|
---|
| 130 | dynamically, with error checking. S must be nonzero. */
|
---|
| 131 |
|
---|
| 132 | void *xnmalloc (size_t n, size_t s)
|
---|
| 133 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
|
---|
| 134 | _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
|
---|
| 135 |
|
---|
| 136 | /* FIXME: Deprecate this in favor of xreallocarray? */
|
---|
| 137 | /* Change the size of an allocated block of memory P to an array of N
|
---|
| 138 | objects each of S bytes, with error checking. S must be nonzero. */
|
---|
| 139 |
|
---|
| 140 | XALLOC_INLINE void *xnrealloc (void *p, size_t n, size_t s)
|
---|
| 141 | _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
|
---|
| 142 | XALLOC_INLINE void *
|
---|
| 143 | xnrealloc (void *p, size_t n, size_t s)
|
---|
| 144 | {
|
---|
| 145 | return xreallocarray (p, n, s);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /* Return a pointer to a new buffer of N bytes. This is like xmalloc,
|
---|
| 149 | except it returns char *. */
|
---|
| 150 |
|
---|
| 151 | char *xcharalloc (size_t n)
|
---|
| 152 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
|
---|
| 153 | _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
|
---|
| 154 |
|
---|
| 155 | #endif /* GNULIB_XALLOC */
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 | #ifdef __cplusplus
|
---|
| 159 | }
|
---|
| 160 | #endif
|
---|
| 161 |
|
---|
| 162 |
|
---|
| 163 | #if GNULIB_XALLOC && defined __cplusplus
|
---|
| 164 |
|
---|
| 165 | /* C++ does not allow conversions from void * to other pointer types
|
---|
| 166 | without a cast. Use templates to work around the problem when
|
---|
| 167 | possible. */
|
---|
| 168 |
|
---|
| 169 | template <typename T> inline T *
|
---|
| 170 | xrealloc (T *p, size_t s)
|
---|
| 171 | {
|
---|
| 172 | return (T *) xrealloc ((void *) p, s);
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | template <typename T> inline T *
|
---|
| 176 | xreallocarray (T *p, size_t n, size_t s)
|
---|
| 177 | {
|
---|
| 178 | return (T *) xreallocarray ((void *) p, n, s);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | /* FIXME: Deprecate this in favor of xreallocarray? */
|
---|
| 182 | template <typename T> inline T *
|
---|
| 183 | xnrealloc (T *p, size_t n, size_t s)
|
---|
| 184 | {
|
---|
| 185 | return xreallocarray (p, n, s);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | template <typename T> inline T *
|
---|
| 189 | x2realloc (T *p, size_t *pn)
|
---|
| 190 | {
|
---|
| 191 | return (T *) x2realloc ((void *) p, pn);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | template <typename T> inline T *
|
---|
| 195 | x2nrealloc (T *p, size_t *pn, size_t s)
|
---|
| 196 | {
|
---|
| 197 | return (T *) x2nrealloc ((void *) p, pn, s);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | template <typename T> inline T *
|
---|
| 201 | xmemdup (T const *p, size_t s)
|
---|
| 202 | {
|
---|
| 203 | return (T *) xmemdup ((void const *) p, s);
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | #endif /* GNULIB_XALLOC && C++ */
|
---|
| 207 |
|
---|
| 208 |
|
---|
| 209 | _GL_INLINE_HEADER_END
|
---|
| 210 |
|
---|
| 211 | #endif /* !XALLOC_H_ */
|
---|