1 /* xalloc.h -- malloc with out-of-memory checking
3 Copyright (C) 1990-2000, 2003-2004, 2006-2014 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 <http://www.gnu.org/licenses/>. */
23 #include "xalloc-oversized.h"
25 #ifndef _GL_INLINE_HEADER_BEGIN
26 #error "Please include config.h first."
28 _GL_INLINE_HEADER_BEGIN
30 # define XALLOC_INLINE _GL_INLINE
39 # define _GL_ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
41 # define _GL_ATTRIBUTE_MALLOC
44 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
45 # define _GL_ATTRIBUTE_ALLOC_SIZE(args) __attribute__ ((__alloc_size__ args))
47 # define _GL_ATTRIBUTE_ALLOC_SIZE(args)
50 /* This function is always triggered when memory is exhausted.
51 It must be defined by the application, either explicitly
52 or by using gnulib's xalloc-die module. This is the
53 function to call when one wants the program to die because of a
54 memory allocation failure. */
55 extern _Noreturn void xalloc_die (void);
57 void *xmalloc (size_t s)
58 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
59 void *xzalloc (size_t s)
60 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
61 void *xcalloc (size_t n, size_t s)
62 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
63 void *xrealloc (void *p, size_t s)
64 _GL_ATTRIBUTE_ALLOC_SIZE ((2));
65 void *x2realloc (void *p, size_t *pn);
66 void *xmemdup (void const *p, size_t s)
67 _GL_ATTRIBUTE_ALLOC_SIZE ((2));
68 char *xstrdup (char const *str)
71 /* In the following macros, T must be an elementary or structure/union or
72 typedef'ed type, or a pointer to such a type. To apply one of the
73 following macros to a function pointer or array type, you need to typedef
74 it first and use the typedef name. */
76 /* Allocate an object of type T dynamically, with error checking. */
77 /* extern t *XMALLOC (typename t); */
78 #define XMALLOC(t) ((t *) xmalloc (sizeof (t)))
80 /* Allocate memory for N elements of type T, with error checking. */
81 /* extern t *XNMALLOC (size_t n, typename t); */
82 #define XNMALLOC(n, t) \
83 ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t))))
85 /* Allocate an object of type T dynamically, with error checking,
87 /* extern t *XZALLOC (typename t); */
88 #define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
90 /* Allocate memory for N elements of type T, with error checking,
92 /* extern t *XCALLOC (size_t n, typename t); */
93 #define XCALLOC(n, t) \
94 ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
97 /* Allocate an array of N objects, each with S bytes of memory,
98 dynamically, with error checking. S must be nonzero. */
100 XALLOC_INLINE void *xnmalloc (size_t n, size_t s)
101 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
103 xnmalloc (size_t n, size_t s)
105 if (xalloc_oversized (n, s))
107 return xmalloc (n * s);
110 /* Change the size of an allocated block of memory P to an array of N
111 objects each of S bytes, with error checking. S must be nonzero. */
113 XALLOC_INLINE void *xnrealloc (void *p, size_t n, size_t s)
114 _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
116 xnrealloc (void *p, size_t n, size_t s)
118 if (xalloc_oversized (n, s))
120 return xrealloc (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 so that progress is made even if N == 0.
199 Check for overflow, so that N * S stays in size_t range.
200 The check may be slightly conservative, but an exact check isn't
201 worth the trouble. */
202 if ((size_t) -1 / 3 * 2 / s <= n)
208 return xrealloc (p, n * s);
211 /* Return a pointer to a new buffer of N bytes. This is like xmalloc,
212 except it returns char *. */
214 XALLOC_INLINE char *xcharalloc (size_t n)
215 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
217 xcharalloc (size_t n)
219 return XNMALLOC (n, char);
225 /* C++ does not allow conversions from void * to other pointer types
226 without a cast. Use templates to work around the problem when
229 template <typename T> inline T *
230 xrealloc (T *p, size_t s)
232 return (T *) xrealloc ((void *) p, s);
235 template <typename T> inline T *
236 xnrealloc (T *p, size_t n, size_t s)
238 return (T *) xnrealloc ((void *) p, n, s);
241 template <typename T> inline T *
242 x2realloc (T *p, size_t *pn)
244 return (T *) x2realloc ((void *) p, pn);
247 template <typename T> inline T *
248 x2nrealloc (T *p, size_t *pn, size_t s)
250 return (T *) x2nrealloc ((void *) p, pn, s);
253 template <typename T> inline T *
254 xmemdup (T const *p, size_t s)
256 return (T *) xmemdup ((void const *) p, s);
262 #endif /* !XALLOC_H_ */