1 /* xalloc.h -- malloc with out-of-memory checking
3 Copyright (C) 1990-2000, 2003-2004, 2006-2021 Free Software Foundation, Inc.
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.
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/>. */
26 # include "intprops.h"
29 #ifndef _GL_INLINE_HEADER_BEGIN
30 #error "Please include config.h first."
32 _GL_INLINE_HEADER_BEGIN
34 # define XALLOC_INLINE _GL_INLINE
45 /* This function is always triggered when memory is exhausted.
46 It must be defined by the application, either explicitly
47 or by using gnulib's xalloc-die module. This is the
48 function to call when one wants the program to die because of a
49 memory allocation failure. */
50 /*extern*/ _Noreturn void xalloc_die (void);
52 #endif /* GNULIB_XALLOC_DIE */
56 void *xmalloc (size_t s)
57 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
58 void *xzalloc (size_t s)
59 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
60 void *xcalloc (size_t n, size_t s)
61 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
62 void *xrealloc (void *p, size_t s)
63 _GL_ATTRIBUTE_ALLOC_SIZE ((2));
64 void *xreallocarray (void *p, size_t n, size_t s)
65 _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
66 void *x2realloc (void *p, size_t *pn);
67 void *xpalloc (void *pa, idx_t *nitems, idx_t nitems_incr_min,
68 ptrdiff_t nitems_max, idx_t item_size);
69 void *xmemdup (void const *p, size_t s)
70 _GL_ATTRIBUTE_ALLOC_SIZE ((2));
71 char *xstrdup (char const *str)
74 /* In the following macros, T must be an elementary or structure/union or
75 typedef'ed type, or a pointer to such a type. To apply one of the
76 following macros to a function pointer or array type, you need to typedef
77 it first and use the typedef name. */
79 /* Allocate an object of type T dynamically, with error checking. */
80 /* extern t *XMALLOC (typename t); */
81 # define XMALLOC(t) ((t *) xmalloc (sizeof (t)))
83 /* Allocate memory for N elements of type T, with error checking. */
84 /* extern t *XNMALLOC (size_t n, typename t); */
85 # define XNMALLOC(n, t) \
86 ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t))))
88 /* Allocate an object of type T dynamically, with error checking,
90 /* extern t *XZALLOC (typename t); */
91 # define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
93 /* Allocate memory for N elements of type T, with error checking,
95 /* extern t *XCALLOC (size_t n, typename t); */
96 # define XCALLOC(n, t) \
97 ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
100 /* Allocate an array of N objects, each with S bytes of memory,
101 dynamically, with error checking. S must be nonzero. */
103 XALLOC_INLINE void *xnmalloc (size_t n, size_t s)
104 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
106 xnmalloc (size_t n, size_t s)
108 return xreallocarray (NULL, n, s);
111 /* FIXME: Deprecate this in favor of xreallocarray? */
112 /* Change the size of an allocated block of memory P to an array of N
113 objects each of S bytes, with error checking. S must be nonzero. */
115 XALLOC_INLINE void *xnrealloc (void *p, size_t n, size_t s)
116 _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
118 xnrealloc (void *p, size_t n, size_t s)
120 return xreallocarray (p, n, s);
123 /* If P is null, allocate a block of at least *PN such objects;
124 otherwise, reallocate P so that it contains more than *PN objects
125 each of S bytes. S must be nonzero. Set *PN to the new number of
126 objects, and return the pointer to the new block. *PN is never set
127 to zero, and the returned pointer is never null.
129 Repeated reallocations are guaranteed to make progress, either by
130 allocating an initial block with a nonzero size, or by allocating a
133 In the following implementation, nonzero sizes are increased by a
134 factor of approximately 1.5 so that repeated reallocations have
135 O(N) overall cost rather than O(N**2) cost, but the
136 specification for this function does not guarantee that rate.
138 Here is an example of use:
142 size_t allocated = 0;
145 append_int (int value)
147 if (used == allocated)
148 p = x2nrealloc (p, &allocated, sizeof *p);
152 This causes x2nrealloc to allocate a block of some nonzero size the
153 first time it is called.
155 To have finer-grained control over the initial size, set *PN to a
156 nonzero value before calling this function with P == NULL. For
161 size_t allocated = 0;
162 size_t allocated1 = 1000;
165 append_int (int value)
167 if (used == allocated)
169 p = x2nrealloc (p, &allocated1, sizeof *p);
170 allocated = allocated1;
178 x2nrealloc (void *p, size_t *pn, size_t s)
186 /* The approximate size to use for initial small allocation
187 requests, when the invoking code specifies an old size of
188 zero. This is the largest "small" request for the GNU C
190 enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
192 n = DEFAULT_MXFAST / s;
198 /* Set N = floor (1.5 * N) + 1 to make progress even if N == 0. */
199 if (INT_ADD_WRAPV (n, (n >> 1) + 1, &n))
203 p = xreallocarray (p, n, s);
208 /* Return a pointer to a new buffer of N bytes. This is like xmalloc,
209 except it returns char *. */
211 XALLOC_INLINE char *xcharalloc (size_t n)
212 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
214 xcharalloc (size_t n)
216 return XNMALLOC (n, char);
219 #endif /* GNULIB_XALLOC */
227 #if GNULIB_XALLOC && defined __cplusplus
229 /* C++ does not allow conversions from void * to other pointer types
230 without a cast. Use templates to work around the problem when
233 template <typename T> inline T *
234 xrealloc (T *p, size_t s)
236 return (T *) xrealloc ((void *) p, s);
239 template <typename T> inline T *
240 xreallocarray (T *p, size_t n, size_t s)
242 return (T *) xreallocarray ((void *) p, n, s);
245 /* FIXME: Deprecate this in favor of xreallocarray? */
246 template <typename T> inline T *
247 xnrealloc (T *p, size_t n, size_t s)
249 return xreallocarray (p, n, s);
252 template <typename T> inline T *
253 x2realloc (T *p, size_t *pn)
255 return (T *) x2realloc ((void *) p, pn);
258 template <typename T> inline T *
259 x2nrealloc (T *p, size_t *pn, size_t s)
261 return (T *) x2nrealloc ((void *) p, pn, s);
264 template <typename T> inline T *
265 xmemdup (T const *p, size_t s)
267 return (T *) xmemdup ((void const *) p, s);
270 #endif /* GNULIB_XALLOC && C++ */
273 _GL_INLINE_HEADER_END
275 #endif /* !XALLOC_H_ */