]> git.cworth.org Git - tar/blob - gnu/xalloc.h
Imported Upstream version 1.24
[tar] / gnu / xalloc.h
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* xalloc.h -- malloc with out-of-memory checking
4
5    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
6    2000, 2003, 2004, 2006, 2007, 2008, 2009, 2010 Free Software Foundation,
7    Inc.
8
9    This program is free software: you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #ifndef XALLOC_H_
23 # define XALLOC_H_
24
25 # include <stddef.h>
26
27
28 # ifdef __cplusplus
29 extern "C" {
30 # endif
31
32
33 # ifndef __attribute__
34 #  if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
35 #   define __attribute__(x)
36 #  endif
37 # endif
38
39 # ifndef ATTRIBUTE_NORETURN
40 #  define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
41 # endif
42
43 # ifndef ATTRIBUTE_MALLOC
44 #  if __GNUC__ >= 3
45 #   define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
46 #  else
47 #   define ATTRIBUTE_MALLOC
48 #  endif
49 # endif
50
51 # ifndef ATTRIBUTE_ALLOC_SIZE
52 #  if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
53 #   define ATTRIBUTE_ALLOC_SIZE(args) __attribute__ ((__alloc_size__ args))
54 #  else
55 #   define ATTRIBUTE_ALLOC_SIZE(args)
56 #  endif
57 # endif
58
59 /* This function is always triggered when memory is exhausted.
60    It must be defined by the application, either explicitly
61    or by using gnulib's xalloc-die module.  This is the
62    function to call when one wants the program to die because of a
63    memory allocation failure.  */
64 extern void xalloc_die (void) ATTRIBUTE_NORETURN;
65
66 void *xmalloc (size_t s)
67       ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE ((1));
68 void *xzalloc (size_t s)
69       ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE ((1));
70 void *xcalloc (size_t n, size_t s)
71       ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE ((1, 2));
72 void *xrealloc (void *p, size_t s)
73       ATTRIBUTE_ALLOC_SIZE ((2));
74 void *x2realloc (void *p, size_t *pn);
75 void *xmemdup (void const *p, size_t s)
76       ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE ((2));
77 char *xstrdup (char const *str)
78       ATTRIBUTE_MALLOC;
79
80 /* Return 1 if an array of N objects, each of size S, cannot exist due
81    to size arithmetic overflow.  S must be positive and N must be
82    nonnegative.  This is a macro, not an inline function, so that it
83    works correctly even when SIZE_MAX < N.
84
85    By gnulib convention, SIZE_MAX represents overflow in size
86    calculations, so the conservative dividend to use here is
87    SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
88    However, malloc (SIZE_MAX) fails on all known hosts where
89    sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
90    exactly-SIZE_MAX allocations on such hosts; this avoids a test and
91    branch when S is known to be 1.  */
92 # define xalloc_oversized(n, s) \
93     ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
94
95
96 /* In the following macros, T must be an elementary or structure/union or
97    typedef'ed type, or a pointer to such a type.  To apply one of the
98    following macros to a function pointer or array type, you need to typedef
99    it first and use the typedef name.  */
100
101 /* Allocate an object of type T dynamically, with error checking.  */
102 /* extern t *XMALLOC (typename t); */
103 # define XMALLOC(t) ((t *) xmalloc (sizeof (t)))
104
105 /* Allocate memory for N elements of type T, with error checking.  */
106 /* extern t *XNMALLOC (size_t n, typename t); */
107 # define XNMALLOC(n, t) \
108     ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t))))
109
110 /* Allocate an object of type T dynamically, with error checking,
111    and zero it.  */
112 /* extern t *XZALLOC (typename t); */
113 # define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
114
115 /* Allocate memory for N elements of type T, with error checking,
116    and zero it.  */
117 /* extern t *XCALLOC (size_t n, typename t); */
118 # define XCALLOC(n, t) \
119     ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
120
121
122 # if HAVE_INLINE
123 #  define static_inline static inline
124 # else
125 void *xnmalloc (size_t n, size_t s)
126       ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE ((1, 2));
127 void *xnrealloc (void *p, size_t n, size_t s)
128       ATTRIBUTE_ALLOC_SIZE ((2, 3));
129 void *x2nrealloc (void *p, size_t *pn, size_t s);
130 char *xcharalloc (size_t n)
131       ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE ((1));
132 # endif
133
134 # ifdef static_inline
135
136 /* Allocate an array of N objects, each with S bytes of memory,
137    dynamically, with error checking.  S must be nonzero.  */
138
139 static_inline void *xnmalloc (size_t n, size_t s)
140                     ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE ((1, 2));
141 static_inline void *
142 xnmalloc (size_t n, size_t s)
143 {
144   if (xalloc_oversized (n, s))
145     xalloc_die ();
146   return xmalloc (n * s);
147 }
148
149 /* Change the size of an allocated block of memory P to an array of N
150    objects each of S bytes, with error checking.  S must be nonzero.  */
151
152 static_inline void *xnrealloc (void *p, size_t n, size_t s)
153                     ATTRIBUTE_ALLOC_SIZE ((2, 3));
154 static_inline void *
155 xnrealloc (void *p, size_t n, size_t s)
156 {
157   if (xalloc_oversized (n, s))
158     xalloc_die ();
159   return xrealloc (p, n * s);
160 }
161
162 /* If P is null, allocate a block of at least *PN such objects;
163    otherwise, reallocate P so that it contains more than *PN objects
164    each of S bytes.  *PN must be nonzero unless P is null, and S must
165    be nonzero.  Set *PN to the new number of objects, and return the
166    pointer to the new block.  *PN is never set to zero, and the
167    returned pointer is never null.
168
169    Repeated reallocations are guaranteed to make progress, either by
170    allocating an initial block with a nonzero size, or by allocating a
171    larger block.
172
173    In the following implementation, nonzero sizes are increased by a
174    factor of approximately 1.5 so that repeated reallocations have
175    O(N) overall cost rather than O(N**2) cost, but the
176    specification for this function does not guarantee that rate.
177
178    Here is an example of use:
179
180      int *p = NULL;
181      size_t used = 0;
182      size_t allocated = 0;
183
184      void
185      append_int (int value)
186        {
187          if (used == allocated)
188            p = x2nrealloc (p, &allocated, sizeof *p);
189          p[used++] = value;
190        }
191
192    This causes x2nrealloc to allocate a block of some nonzero size the
193    first time it is called.
194
195    To have finer-grained control over the initial size, set *PN to a
196    nonzero value before calling this function with P == NULL.  For
197    example:
198
199      int *p = NULL;
200      size_t used = 0;
201      size_t allocated = 0;
202      size_t allocated1 = 1000;
203
204      void
205      append_int (int value)
206        {
207          if (used == allocated)
208            {
209              p = x2nrealloc (p, &allocated1, sizeof *p);
210              allocated = allocated1;
211            }
212          p[used++] = value;
213        }
214
215    */
216
217 static_inline void *
218 x2nrealloc (void *p, size_t *pn, size_t s)
219 {
220   size_t n = *pn;
221
222   if (! p)
223     {
224       if (! n)
225         {
226           /* The approximate size to use for initial small allocation
227              requests, when the invoking code specifies an old size of
228              zero.  64 bytes is the largest "small" request for the
229              GNU C library malloc.  */
230           enum { DEFAULT_MXFAST = 64 };
231
232           n = DEFAULT_MXFAST / s;
233           n += !n;
234         }
235     }
236   else
237     {
238       /* Set N = ceil (1.5 * N) so that progress is made if N == 1.
239          Check for overflow, so that N * S stays in size_t range.
240          The check is slightly conservative, but an exact check isn't
241          worth the trouble.  */
242       if ((size_t) -1 / 3 * 2 / s <= n)
243         xalloc_die ();
244       n += (n + 1) / 2;
245     }
246
247   *pn = n;
248   return xrealloc (p, n * s);
249 }
250
251 /* Return a pointer to a new buffer of N bytes.  This is like xmalloc,
252    except it returns char *.  */
253
254 static_inline char *xcharalloc (size_t n)
255                     ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE ((1));
256 static_inline char *
257 xcharalloc (size_t n)
258 {
259   return XNMALLOC (n, char);
260 }
261
262 # endif
263
264 # ifdef __cplusplus
265 }
266
267 /* C++ does not allow conversions from void * to other pointer types
268    without a cast.  Use templates to work around the problem when
269    possible.  */
270
271 template <typename T> inline T *
272 xrealloc (T *p, size_t s)
273 {
274   return (T *) xrealloc ((void *) p, s);
275 }
276
277 template <typename T> inline T *
278 xnrealloc (T *p, size_t n, size_t s)
279 {
280   return (T *) xnrealloc ((void *) p, n, s);
281 }
282
283 template <typename T> inline T *
284 x2realloc (T *p, size_t *pn)
285 {
286   return (T *) x2realloc ((void *) p, pn);
287 }
288
289 template <typename T> inline T *
290 x2nrealloc (T *p, size_t *pn, size_t s)
291 {
292   return (T *) x2nrealloc ((void *) p, pn, s);
293 }
294
295 template <typename T> inline T *
296 xmemdup (T const *p, size_t s)
297 {
298   return (T *) xmemdup ((void const *) p, s);
299 }
300
301 # endif
302
303
304 #endif /* !XALLOC_H_ */