1 /* xalloc.h -- malloc with out-of-memory checking
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2003, 2004, 2006 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
31 # ifndef __attribute__
32 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
33 # define __attribute__(x)
37 # ifndef ATTRIBUTE_NORETURN
38 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
41 /* This function is always triggered when memory is exhausted.
42 It must be defined by the application, either explicitly
43 or by using gnulib's xalloc-die module. This is the
44 function to call when one wants the program to die because of a
45 memory allocation failure. */
46 extern void xalloc_die (void) ATTRIBUTE_NORETURN;
48 void *xmalloc (size_t s);
49 void *xzalloc (size_t s);
50 void *xcalloc (size_t n, size_t s);
51 void *xrealloc (void *p, size_t s);
52 void *x2realloc (void *p, size_t *pn);
53 void *xmemdup (void const *p, size_t s);
54 char *xstrdup (char const *str);
56 /* Return 1 if an array of N objects, each of size S, cannot exist due
57 to size arithmetic overflow. S must be positive and N must be
58 nonnegative. This is a macro, not an inline function, so that it
59 works correctly even when SIZE_MAX < N.
61 By gnulib convention, SIZE_MAX represents overflow in size
62 calculations, so the conservative dividend to use here is
63 SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
64 However, malloc (SIZE_MAX) fails on all known hosts where
65 sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
66 exactly-SIZE_MAX allocations on such hosts; this avoids a test and
67 branch when S is known to be 1. */
68 # define xalloc_oversized(n, s) \
69 ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
71 /* Allocate an array of N objects, each with S bytes of memory,
72 dynamically, with error checking. S must be nonzero. */
75 xnmalloc (size_t n, size_t s)
77 if (xalloc_oversized (n, s))
79 return xmalloc (n * s);
82 /* Change the size of an allocated block of memory P to an array of N
83 objects each of S bytes, with error checking. S must be nonzero. */
86 xnrealloc (void *p, size_t n, size_t s)
88 if (xalloc_oversized (n, s))
90 return xrealloc (p, n * s);
93 /* If P is null, allocate a block of at least *PN such objects;
94 otherwise, reallocate P so that it contains more than *PN objects
95 each of S bytes. *PN must be nonzero unless P is null, and S must
96 be nonzero. Set *PN to the new number of objects, and return the
97 pointer to the new block. *PN is never set to zero, and the
98 returned pointer is never null.
100 Repeated reallocations are guaranteed to make progress, either by
101 allocating an initial block with a nonzero size, or by allocating a
104 In the following implementation, nonzero sizes are doubled so that
105 repeated reallocations have O(N log N) overall cost rather than
106 O(N**2) cost, but the specification for this function does not
107 guarantee that sizes are doubled.
109 Here is an example of use:
113 size_t allocated = 0;
116 append_int (int value)
118 if (used == allocated)
119 p = x2nrealloc (p, &allocated, sizeof *p);
123 This causes x2nrealloc to allocate a block of some nonzero size the
124 first time it is called.
126 To have finer-grained control over the initial size, set *PN to a
127 nonzero value before calling this function with P == NULL. For
132 size_t allocated = 0;
133 size_t allocated1 = 1000;
136 append_int (int value)
138 if (used == allocated)
140 p = x2nrealloc (p, &allocated1, sizeof *p);
141 allocated = allocated1;
149 x2nrealloc (void *p, size_t *pn, size_t s)
157 /* The approximate size to use for initial small allocation
158 requests, when the invoking code specifies an old size of
159 zero. 64 bytes is the largest "small" request for the
160 GNU C library malloc. */
161 enum { DEFAULT_MXFAST = 64 };
163 n = DEFAULT_MXFAST / s;
169 if (((size_t) -1) / 2 / s < n)
175 return xrealloc (p, n * s);
178 /* In the following macros, T must be an elementary or structure/union or
179 typedef'ed type, or a pointer to such a type. To apply one of the
180 following macros to a function pointer or array type, you need to typedef
181 it first and use the typedef name. */
183 /* Allocate an object of type T dynamically, with error checking. */
184 /* extern t *XMALLOC (typename t); */
185 #define XMALLOC(t) ((t *) xmalloc (sizeof (t)))
187 /* Allocate memory for N elements of type T, with error checking. */
188 /* extern t *XNMALLOC (size_t n, typename t); */
189 #define XNMALLOC(n, t) ((t *) xnmalloc (n, sizeof (t)))
191 /* Allocate an object of type T dynamically, with error checking,
193 /* extern t *XZALLOC (typename t); */
194 #define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
196 /* Allocate memory for N elements of type T, with error checking,
198 /* extern t *XCALLOC (size_t n, typename t); */
199 #define XCALLOC(n, t) ((t *) xcalloc (n, sizeof (t)))
201 /* Return a pointer to a new buffer of N bytes. This is like xmalloc,
202 except it returns char *. */
204 xcharalloc (size_t n)
206 return XNMALLOC (n, char);
212 /* C++ does not allow conversions from void * to other pointer types
213 without a cast. Use templates to work around the problem when
216 template <typename T> inline T *
217 xrealloc (T *p, size_t s)
219 return (T *) xrealloc ((void *) p, s);
222 template <typename T> inline T *
223 xnrealloc (T *p, size_t n, size_t s)
225 return (T *) xnrealloc ((void *) p, n, s);
228 template <typename T> inline T *
229 x2realloc (T *p, size_t *pn)
231 return (T *) x2realloc ((void *) p, pn);
234 template <typename T> inline T *
235 x2nrealloc (T *p, size_t *pn, size_t s)
237 return (T *) x2nrealloc ((void *) p, pn, s);
240 template <typename T> inline T *
241 xmemdup (T const *p, size_t s)
243 return (T *) xmemdup ((void const *) p, s);
249 #endif /* !XALLOC_H_ */