1 /* xalloc.h -- malloc with out-of-memory checking
3 Copyright (C) 1990-2000, 2003-2004, 2006-2021 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/>. */
27 # include "intprops.h"
30 #ifndef _GL_INLINE_HEADER_BEGIN
31 #error "Please include config.h first."
33 _GL_INLINE_HEADER_BEGIN
35 # define XALLOC_INLINE _GL_INLINE
46 /* This function is always triggered when memory is exhausted.
47 It must be defined by the application, either explicitly
48 or by using gnulib's xalloc-die module. This is the
49 function to call when one wants the program to die because of a
50 memory allocation failure. */
51 /*extern*/ _Noreturn void xalloc_die (void);
53 #endif /* GNULIB_XALLOC_DIE */
57 void *xmalloc (size_t s)
58 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
59 _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
60 void *ximalloc (idx_t s)
61 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
62 _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
63 void *xzalloc (size_t s)
64 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
65 _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
66 void *xizalloc (idx_t s)
67 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
68 _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
69 void *xcalloc (size_t n, size_t s)
70 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
71 _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
72 void *xicalloc (idx_t n, idx_t s)
73 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
74 _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
75 void *xrealloc (void *p, size_t s)
76 _GL_ATTRIBUTE_ALLOC_SIZE ((2));
77 void *xirealloc (void *p, idx_t s)
78 _GL_ATTRIBUTE_ALLOC_SIZE ((2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
79 void *xreallocarray (void *p, size_t n, size_t s)
80 _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
81 void *xireallocarray (void *p, idx_t n, idx_t s)
82 _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3)) _GL_ATTRIBUTE_RETURNS_NONNULL;
83 void *x2realloc (void *p, size_t *ps) /* superseded by xpalloc */
84 _GL_ATTRIBUTE_RETURNS_NONNULL;
85 void *x2nrealloc (void *p, size_t *pn, size_t s) /* superseded by xpalloc */
86 _GL_ATTRIBUTE_RETURNS_NONNULL;
87 void *xpalloc (void *pa, idx_t *pn, idx_t n_incr_min, ptrdiff_t n_max, idx_t s)
88 _GL_ATTRIBUTE_RETURNS_NONNULL;
89 void *xmemdup (void const *p, size_t s)
90 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
91 _GL_ATTRIBUTE_ALLOC_SIZE ((2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
92 void *ximemdup (void const *p, idx_t s)
93 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
94 _GL_ATTRIBUTE_ALLOC_SIZE ((2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
95 char *ximemdup0 (void const *p, idx_t s)
96 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
97 _GL_ATTRIBUTE_RETURNS_NONNULL;
98 char *xstrdup (char const *str)
99 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
100 _GL_ATTRIBUTE_RETURNS_NONNULL;
102 /* In the following macros, T must be an elementary or structure/union or
103 typedef'ed type, or a pointer to such a type. To apply one of the
104 following macros to a function pointer or array type, you need to typedef
105 it first and use the typedef name. */
107 /* Allocate an object of type T dynamically, with error checking. */
108 /* extern t *XMALLOC (typename t); */
109 # define XMALLOC(t) ((t *) xmalloc (sizeof (t)))
111 /* Allocate memory for N elements of type T, with error checking. */
112 /* extern t *XNMALLOC (size_t n, typename t); */
113 # define XNMALLOC(n, t) \
114 ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t))))
116 /* Allocate an object of type T dynamically, with error checking,
118 /* extern t *XZALLOC (typename t); */
119 # define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
121 /* Allocate memory for N elements of type T, with error checking,
123 /* extern t *XCALLOC (size_t n, typename t); */
124 # define XCALLOC(n, t) \
125 ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
128 /* Allocate an array of N objects, each with S bytes of memory,
129 dynamically, with error checking. S must be nonzero. */
131 XALLOC_INLINE void *xnmalloc (size_t n, size_t s)
132 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
133 _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
135 xnmalloc (size_t n, size_t s)
137 return xreallocarray (NULL, n, s);
140 /* FIXME: Deprecate this in favor of xreallocarray? */
141 /* Change the size of an allocated block of memory P to an array of N
142 objects each of S bytes, with error checking. S must be nonzero. */
144 XALLOC_INLINE void *xnrealloc (void *p, size_t n, size_t s)
145 _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
147 xnrealloc (void *p, size_t n, size_t s)
149 return xreallocarray (p, n, s);
152 /* Return a pointer to a new buffer of N bytes. This is like xmalloc,
153 except it returns char *. */
155 XALLOC_INLINE char *xcharalloc (size_t n)
156 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
157 _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
159 xcharalloc (size_t n)
161 return XNMALLOC (n, char);
164 #endif /* GNULIB_XALLOC */
172 #if GNULIB_XALLOC && defined __cplusplus
174 /* C++ does not allow conversions from void * to other pointer types
175 without a cast. Use templates to work around the problem when
178 template <typename T> inline T *
179 xrealloc (T *p, size_t s)
181 return (T *) xrealloc ((void *) p, s);
184 template <typename T> inline T *
185 xreallocarray (T *p, size_t n, size_t s)
187 return (T *) xreallocarray ((void *) p, n, s);
190 /* FIXME: Deprecate this in favor of xreallocarray? */
191 template <typename T> inline T *
192 xnrealloc (T *p, size_t n, size_t s)
194 return xreallocarray (p, n, s);
197 template <typename T> inline T *
198 x2realloc (T *p, size_t *pn)
200 return (T *) x2realloc ((void *) p, pn);
203 template <typename T> inline T *
204 x2nrealloc (T *p, size_t *pn, size_t s)
206 return (T *) x2nrealloc ((void *) p, pn, s);
209 template <typename T> inline T *
210 xmemdup (T const *p, size_t s)
212 return (T *) xmemdup ((void const *) p, s);
215 #endif /* GNULIB_XALLOC && C++ */
218 _GL_INLINE_HEADER_END
220 #endif /* !XALLOC_H_ */