Fix last ChangeLog entry.
[gnulib.git] / lib / xalloc.h
blob24273ffbe333b4fbbdfba2c11d247dc02ed33612
1 /* xalloc.h -- malloc with out-of-memory checking
3 Copyright (C) 1990-2000, 2003-2004, 2006-2020 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/>. */
18 #ifndef XALLOC_H_
19 #define XALLOC_H_
21 #include <stddef.h>
22 #include <stdint.h>
24 #include "xalloc-oversized.h"
26 #ifndef _GL_INLINE_HEADER_BEGIN
27 #error "Please include config.h first."
28 #endif
29 _GL_INLINE_HEADER_BEGIN
30 #ifndef XALLOC_INLINE
31 # define XALLOC_INLINE _GL_INLINE
32 #endif
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
39 /* This function is always triggered when memory is exhausted.
40 It must be defined by the application, either explicitly
41 or by using gnulib's xalloc-die module. This is the
42 function to call when one wants the program to die because of a
43 memory allocation failure. */
44 /*extern*/ _Noreturn void xalloc_die (void);
46 void *xmalloc (size_t s)
47 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
48 void *xzalloc (size_t s)
49 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
50 void *xcalloc (size_t n, size_t s)
51 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
52 void *xrealloc (void *p, size_t s)
53 _GL_ATTRIBUTE_ALLOC_SIZE ((2));
54 void *x2realloc (void *p, size_t *pn);
55 void *xmemdup (void const *p, size_t s)
56 _GL_ATTRIBUTE_ALLOC_SIZE ((2));
57 char *xstrdup (char const *str)
58 _GL_ATTRIBUTE_MALLOC;
60 /* In the following macros, T must be an elementary or structure/union or
61 typedef'ed type, or a pointer to such a type. To apply one of the
62 following macros to a function pointer or array type, you need to typedef
63 it first and use the typedef name. */
65 /* Allocate an object of type T dynamically, with error checking. */
66 /* extern t *XMALLOC (typename t); */
67 #define XMALLOC(t) ((t *) xmalloc (sizeof (t)))
69 /* Allocate memory for N elements of type T, with error checking. */
70 /* extern t *XNMALLOC (size_t n, typename t); */
71 #define XNMALLOC(n, t) \
72 ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t))))
74 /* Allocate an object of type T dynamically, with error checking,
75 and zero it. */
76 /* extern t *XZALLOC (typename t); */
77 #define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
79 /* Allocate memory for N elements of type T, with error checking,
80 and zero it. */
81 /* extern t *XCALLOC (size_t n, typename t); */
82 #define XCALLOC(n, t) \
83 ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
86 /* Allocate an array of N objects, each with S bytes of memory,
87 dynamically, with error checking. S must be nonzero. */
89 XALLOC_INLINE void *xnmalloc (size_t n, size_t s)
90 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
91 XALLOC_INLINE void *
92 xnmalloc (size_t n, size_t s)
94 if (xalloc_oversized (n, s))
95 xalloc_die ();
96 return xmalloc (n * s);
99 /* Change the size of an allocated block of memory P to an array of N
100 objects each of S bytes, with error checking. S must be nonzero. */
102 XALLOC_INLINE void *xnrealloc (void *p, size_t n, size_t s)
103 _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
104 XALLOC_INLINE void *
105 xnrealloc (void *p, size_t n, size_t s)
107 if (xalloc_oversized (n, s))
108 xalloc_die ();
109 return xrealloc (p, n * s);
112 /* If P is null, allocate a block of at least *PN such objects;
113 otherwise, reallocate P so that it contains more than *PN objects
114 each of S bytes. S must be nonzero. Set *PN to the new number of
115 objects, and return the pointer to the new block. *PN is never set
116 to zero, and the returned pointer is never null.
118 Repeated reallocations are guaranteed to make progress, either by
119 allocating an initial block with a nonzero size, or by allocating a
120 larger block.
122 In the following implementation, nonzero sizes are increased by a
123 factor of approximately 1.5 so that repeated reallocations have
124 O(N) overall cost rather than O(N**2) cost, but the
125 specification for this function does not guarantee that rate.
127 Here is an example of use:
129 int *p = NULL;
130 size_t used = 0;
131 size_t allocated = 0;
133 void
134 append_int (int value)
136 if (used == allocated)
137 p = x2nrealloc (p, &allocated, sizeof *p);
138 p[used++] = value;
141 This causes x2nrealloc to allocate a block of some nonzero size the
142 first time it is called.
144 To have finer-grained control over the initial size, set *PN to a
145 nonzero value before calling this function with P == NULL. For
146 example:
148 int *p = NULL;
149 size_t used = 0;
150 size_t allocated = 0;
151 size_t allocated1 = 1000;
153 void
154 append_int (int value)
156 if (used == allocated)
158 p = x2nrealloc (p, &allocated1, sizeof *p);
159 allocated = allocated1;
161 p[used++] = value;
166 XALLOC_INLINE void *
167 x2nrealloc (void *p, size_t *pn, size_t s)
169 size_t n = *pn;
171 if (! p)
173 if (! n)
175 /* The approximate size to use for initial small allocation
176 requests, when the invoking code specifies an old size of
177 zero. This is the largest "small" request for the GNU C
178 library malloc. */
179 enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
181 n = DEFAULT_MXFAST / s;
182 n += !n;
184 if (xalloc_oversized (n, s))
185 xalloc_die ();
187 else
189 /* Set N = floor (1.5 * N) + 1 so that progress is made even if N == 0.
190 Check for overflow, so that N * S stays in both ptrdiff_t and
191 size_t range. The check may be slightly conservative, but an
192 exact check isn't worth the trouble. */
193 if ((PTRDIFF_MAX < SIZE_MAX ? PTRDIFF_MAX : SIZE_MAX) / 3 * 2 / s
194 <= n)
195 xalloc_die ();
196 n += n / 2 + 1;
199 *pn = n;
200 return xrealloc (p, n * s);
203 /* Return a pointer to a new buffer of N bytes. This is like xmalloc,
204 except it returns char *. */
206 XALLOC_INLINE char *xcharalloc (size_t n)
207 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
208 XALLOC_INLINE char *
209 xcharalloc (size_t n)
211 return XNMALLOC (n, char);
214 #ifdef __cplusplus
217 /* C++ does not allow conversions from void * to other pointer types
218 without a cast. Use templates to work around the problem when
219 possible. */
221 template <typename T> inline T *
222 xrealloc (T *p, size_t s)
224 return (T *) xrealloc ((void *) p, s);
227 template <typename T> inline T *
228 xnrealloc (T *p, size_t n, size_t s)
230 return (T *) xnrealloc ((void *) p, n, s);
233 template <typename T> inline T *
234 x2realloc (T *p, size_t *pn)
236 return (T *) x2realloc ((void *) p, pn);
239 template <typename T> inline T *
240 x2nrealloc (T *p, size_t *pn, size_t s)
242 return (T *) x2nrealloc ((void *) p, pn, s);
245 template <typename T> inline T *
246 xmemdup (T const *p, size_t s)
248 return (T *) xmemdup ((void const *) p, s);
251 #endif
253 _GL_INLINE_HEADER_END
255 #endif /* !XALLOC_H_ */