a9595b8efc910bb0d8f0bf4965f326165ccc93ca
[debian/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-2000, 2003-2004, 2006-2013 Free Software Foundation, Inc.
6
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #ifndef XALLOC_H_
21 #define XALLOC_H_
22
23 #include <stddef.h>
24
25 #include "xalloc-oversized.h"
26
27 _GL_INLINE_HEADER_BEGIN
28 #ifndef XALLOC_INLINE
29 # define XALLOC_INLINE _GL_INLINE
30 #endif
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36
37 #if __GNUC__ >= 3
38 # define _GL_ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
39 #else
40 # define _GL_ATTRIBUTE_MALLOC
41 #endif
42
43 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
44 # define _GL_ATTRIBUTE_ALLOC_SIZE(args) __attribute__ ((__alloc_size__ args))
45 #else
46 # define _GL_ATTRIBUTE_ALLOC_SIZE(args)
47 #endif
48
49 /* This function is always triggered when memory is exhausted.
50    It must be defined by the application, either explicitly
51    or by using gnulib's xalloc-die module.  This is the
52    function to call when one wants the program to die because of a
53    memory allocation failure.  */
54 extern _Noreturn void xalloc_die (void);
55
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 *x2realloc (void *p, size_t *pn);
65 void *xmemdup (void const *p, size_t s)
66       _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((2));
67 char *xstrdup (char const *str)
68       _GL_ATTRIBUTE_MALLOC;
69
70 /* In the following macros, T must be an elementary or structure/union or
71    typedef'ed type, or a pointer to such a type.  To apply one of the
72    following macros to a function pointer or array type, you need to typedef
73    it first and use the typedef name.  */
74
75 /* Allocate an object of type T dynamically, with error checking.  */
76 /* extern t *XMALLOC (typename t); */
77 #define XMALLOC(t) ((t *) xmalloc (sizeof (t)))
78
79 /* Allocate memory for N elements of type T, with error checking.  */
80 /* extern t *XNMALLOC (size_t n, typename t); */
81 #define XNMALLOC(n, t) \
82    ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t))))
83
84 /* Allocate an object of type T dynamically, with error checking,
85    and zero it.  */
86 /* extern t *XZALLOC (typename t); */
87 #define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
88
89 /* Allocate memory for N elements of type T, with error checking,
90    and zero it.  */
91 /* extern t *XCALLOC (size_t n, typename t); */
92 #define XCALLOC(n, t) \
93    ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
94
95
96 /* Allocate an array of N objects, each with S bytes of memory,
97    dynamically, with error checking.  S must be nonzero.  */
98
99 XALLOC_INLINE void *xnmalloc (size_t n, size_t s)
100                     _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
101 XALLOC_INLINE void *
102 xnmalloc (size_t n, size_t s)
103 {
104   if (xalloc_oversized (n, s))
105     xalloc_die ();
106   return xmalloc (n * s);
107 }
108
109 /* Change the size of an allocated block of memory P to an array of N
110    objects each of S bytes, with error checking.  S must be nonzero.  */
111
112 XALLOC_INLINE void *xnrealloc (void *p, size_t n, size_t s)
113                     _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
114 XALLOC_INLINE void *
115 xnrealloc (void *p, size_t n, size_t s)
116 {
117   if (xalloc_oversized (n, s))
118     xalloc_die ();
119   return xrealloc (p, n * s);
120 }
121
122 /* If P is null, allocate a block of at least *PN such objects;
123    otherwise, reallocate P so that it contains more than *PN objects
124    each of S bytes.  *PN must be nonzero unless P is null, and S must
125    be nonzero.  Set *PN to the new number of objects, and return the
126    pointer to the new block.  *PN is never set to zero, and the
127    returned pointer is never null.
128
129    Repeated reallocations are guaranteed to make progress, either by
130    allocating an initial block with a nonzero size, or by allocating a
131    larger block.
132
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.
137
138    Here is an example of use:
139
140      int *p = NULL;
141      size_t used = 0;
142      size_t allocated = 0;
143
144      void
145      append_int (int value)
146        {
147          if (used == allocated)
148            p = x2nrealloc (p, &allocated, sizeof *p);
149          p[used++] = value;
150        }
151
152    This causes x2nrealloc to allocate a block of some nonzero size the
153    first time it is called.
154
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
157    example:
158
159      int *p = NULL;
160      size_t used = 0;
161      size_t allocated = 0;
162      size_t allocated1 = 1000;
163
164      void
165      append_int (int value)
166        {
167          if (used == allocated)
168            {
169              p = x2nrealloc (p, &allocated1, sizeof *p);
170              allocated = allocated1;
171            }
172          p[used++] = value;
173        }
174
175    */
176
177 XALLOC_INLINE void *
178 x2nrealloc (void *p, size_t *pn, size_t s)
179 {
180   size_t n = *pn;
181
182   if (! p)
183     {
184       if (! n)
185         {
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
189              library malloc.  */
190           enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
191
192           n = DEFAULT_MXFAST / s;
193           n += !n;
194         }
195     }
196   else
197     {
198       /* Set N = ceil (1.5 * N) so that progress is made if N == 1.
199          Check for overflow, so that N * S stays in size_t range.
200          The check is slightly conservative, but an exact check isn't
201          worth the trouble.  */
202       if ((size_t) -1 / 3 * 2 / s <= n)
203         xalloc_die ();
204       n += (n + 1) / 2;
205     }
206
207   *pn = n;
208   return xrealloc (p, n * s);
209 }
210
211 /* Return a pointer to a new buffer of N bytes.  This is like xmalloc,
212    except it returns char *.  */
213
214 XALLOC_INLINE char *xcharalloc (size_t n)
215                     _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
216 XALLOC_INLINE char *
217 xcharalloc (size_t n)
218 {
219   return XNMALLOC (n, char);
220 }
221
222 #ifdef __cplusplus
223 }
224
225 /* C++ does not allow conversions from void * to other pointer types
226    without a cast.  Use templates to work around the problem when
227    possible.  */
228
229 template <typename T> inline T *
230 xrealloc (T *p, size_t s)
231 {
232   return (T *) xrealloc ((void *) p, s);
233 }
234
235 template <typename T> inline T *
236 xnrealloc (T *p, size_t n, size_t s)
237 {
238   return (T *) xnrealloc ((void *) p, n, s);
239 }
240
241 template <typename T> inline T *
242 x2realloc (T *p, size_t *pn)
243 {
244   return (T *) x2realloc ((void *) p, pn);
245 }
246
247 template <typename T> inline T *
248 x2nrealloc (T *p, size_t *pn, size_t s)
249 {
250   return (T *) x2nrealloc ((void *) p, pn, s);
251 }
252
253 template <typename T> inline T *
254 xmemdup (T const *p, size_t s)
255 {
256   return (T *) xmemdup ((void const *) p, s);
257 }
258
259 #endif
260
261
262 #endif /* !XALLOC_H_ */