1 /* xalloc.h -- malloc with out-of-memory checking
3 Copyright (C) 1990-2000, 2003-2004, 2006-2023 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/>. */
28 #ifndef _GL_INLINE_HEADER_BEGIN
29 #error "Please include config.h first."
31 _GL_INLINE_HEADER_BEGIN
33 # define XALLOC_INLINE _GL_INLINE
44 /* This function is always triggered when memory is exhausted.
45 It must be defined by the application, either explicitly
46 or by using gnulib's xalloc-die module. This is the
47 function to call when one wants the program to die because of a
48 memory allocation failure. */
49 /*extern*/ _Noreturn
void xalloc_die (void);
51 #endif /* GNULIB_XALLOC_DIE */
55 void *xmalloc (size_t s
)
56 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
57 _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL
;
58 void *ximalloc (idx_t s
)
59 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
60 _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL
;
61 void *xinmalloc (idx_t n
, idx_t s
)
62 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
63 _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL
;
64 void *xzalloc (size_t s
)
65 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
66 _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL
;
67 void *xizalloc (idx_t s
)
68 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
69 _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL
;
70 void *xcalloc (size_t n
, size_t s
)
71 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
72 _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL
;
73 void *xicalloc (idx_t n
, idx_t s
)
74 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
75 _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL
;
76 void *xrealloc (void *p
, size_t s
)
77 _GL_ATTRIBUTE_ALLOC_SIZE ((2));
78 void *xirealloc (void *p
, idx_t s
)
79 _GL_ATTRIBUTE_ALLOC_SIZE ((2)) _GL_ATTRIBUTE_RETURNS_NONNULL
;
80 void *xreallocarray (void *p
, size_t n
, size_t s
)
81 _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
82 void *xireallocarray (void *p
, idx_t n
, idx_t s
)
83 _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3)) _GL_ATTRIBUTE_RETURNS_NONNULL
;
84 void *x2realloc (void *p
, size_t *ps
) /* superseded by xpalloc */
85 _GL_ATTRIBUTE_RETURNS_NONNULL
;
86 void *x2nrealloc (void *p
, size_t *pn
, size_t s
) /* superseded by xpalloc */
87 _GL_ATTRIBUTE_RETURNS_NONNULL
;
88 void *xpalloc (void *pa
, idx_t
*pn
, idx_t n_incr_min
, ptrdiff_t n_max
, idx_t s
)
89 _GL_ATTRIBUTE_RETURNS_NONNULL
;
90 void *xmemdup (void const *p
, size_t s
)
91 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
92 _GL_ATTRIBUTE_ALLOC_SIZE ((2)) _GL_ATTRIBUTE_RETURNS_NONNULL
;
93 void *ximemdup (void const *p
, idx_t s
)
94 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
95 _GL_ATTRIBUTE_ALLOC_SIZE ((2)) _GL_ATTRIBUTE_RETURNS_NONNULL
;
96 char *ximemdup0 (void const *p
, idx_t s
)
97 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
98 _GL_ATTRIBUTE_RETURNS_NONNULL
;
99 char *xstrdup (char const *str
)
100 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
101 _GL_ATTRIBUTE_RETURNS_NONNULL
;
103 /* In the following macros, T must be an elementary or structure/union or
104 typedef'ed type, or a pointer to such a type. To apply one of the
105 following macros to a function pointer or array type, you need to typedef
106 it first and use the typedef name. */
108 /* Allocate an object of type T dynamically, with error checking. */
109 /* extern t *XMALLOC (typename t); */
110 # define XMALLOC(t) ((t *) xmalloc (sizeof (t)))
112 /* Allocate memory for N elements of type T, with error checking. */
113 /* extern t *XNMALLOC (size_t n, typename t); */
114 # define XNMALLOC(n, t) \
115 ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t))))
117 /* Allocate an object of type T dynamically, with error checking,
119 /* extern t *XZALLOC (typename t); */
120 # define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
122 /* Allocate memory for N elements of type T, with error checking,
124 /* extern t *XCALLOC (size_t n, typename t); */
125 # define XCALLOC(n, t) \
126 ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
129 /* Allocate an array of N objects, each with S bytes of memory,
130 dynamically, with error checking. S must be nonzero. */
132 void *xnmalloc (size_t n
, size_t s
)
133 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
134 _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL
;
136 /* FIXME: Deprecate this in favor of xreallocarray? */
137 /* Change the size of an allocated block of memory P to an array of N
138 objects each of S bytes, with error checking. S must be nonzero. */
140 XALLOC_INLINE
void *xnrealloc (void *p
, size_t n
, size_t s
)
141 _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
143 xnrealloc (void *p
, size_t n
, size_t s
)
145 return xreallocarray (p
, n
, s
);
148 /* Return a pointer to a new buffer of N bytes. This is like xmalloc,
149 except it returns char *. */
151 char *xcharalloc (size_t n
)
152 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
153 _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL
;
155 #endif /* GNULIB_XALLOC */
163 #if GNULIB_XALLOC && defined __cplusplus
165 /* C++ does not allow conversions from void * to other pointer types
166 without a cast. Use templates to work around the problem when
169 template <typename T
> inline T
*
170 xrealloc (T
*p
, size_t s
)
172 return (T
*) xrealloc ((void *) p
, s
);
175 template <typename T
> inline T
*
176 xreallocarray (T
*p
, size_t n
, size_t s
)
178 return (T
*) xreallocarray ((void *) p
, n
, s
);
181 /* FIXME: Deprecate this in favor of xreallocarray? */
182 template <typename T
> inline T
*
183 xnrealloc (T
*p
, size_t n
, size_t s
)
185 return xreallocarray (p
, n
, s
);
188 template <typename T
> inline T
*
189 x2realloc (T
*p
, size_t *pn
)
191 return (T
*) x2realloc ((void *) p
, pn
);
194 template <typename T
> inline T
*
195 x2nrealloc (T
*p
, size_t *pn
, size_t s
)
197 return (T
*) x2nrealloc ((void *) p
, pn
, s
);
200 template <typename T
> inline T
*
201 xmemdup (T
const *p
, size_t s
)
203 return (T
*) xmemdup ((void const *) p
, s
);
206 #endif /* GNULIB_XALLOC && C++ */
209 _GL_INLINE_HEADER_END
211 #endif /* !XALLOC_H_ */