Imported Upstream version 3.8
[platform/upstream/diffutils.git] / lib / xalloc.h
1 /* xalloc.h -- malloc with out-of-memory checking
2
3    Copyright (C) 1990-2000, 2003-2004, 2006-2021 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>.  */
17
18 #ifndef XALLOC_H_
19 #define XALLOC_H_
20
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24
25 #if GNULIB_XALLOC
26 # include "idx.h"
27 # include "intprops.h"
28 #endif
29
30 #ifndef _GL_INLINE_HEADER_BEGIN
31  #error "Please include config.h first."
32 #endif
33 _GL_INLINE_HEADER_BEGIN
34 #ifndef XALLOC_INLINE
35 # define XALLOC_INLINE _GL_INLINE
36 #endif
37
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43
44 #if GNULIB_XALLOC_DIE
45
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);
52
53 #endif /* GNULIB_XALLOC_DIE */
54
55 #if GNULIB_XALLOC
56
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;
101
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.  */
106
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)))
110
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))))
115
116 /* Allocate an object of type T dynamically, with error checking,
117    and zero it.  */
118 /* extern t *XZALLOC (typename t); */
119 # define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
120
121 /* Allocate memory for N elements of type T, with error checking,
122    and zero it.  */
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))))
126
127
128 /* Allocate an array of N objects, each with S bytes of memory,
129    dynamically, with error checking.  S must be nonzero.  */
130
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;
134 XALLOC_INLINE void *
135 xnmalloc (size_t n, size_t s)
136 {
137   return xreallocarray (NULL, n, s);
138 }
139
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.  */
143
144 XALLOC_INLINE void *xnrealloc (void *p, size_t n, size_t s)
145   _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
146 XALLOC_INLINE void *
147 xnrealloc (void *p, size_t n, size_t s)
148 {
149   return xreallocarray (p, n, s);
150 }
151
152 /* Return a pointer to a new buffer of N bytes.  This is like xmalloc,
153    except it returns char *.  */
154
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;
158 XALLOC_INLINE char *
159 xcharalloc (size_t n)
160 {
161   return XNMALLOC (n, char);
162 }
163
164 #endif /* GNULIB_XALLOC */
165
166
167 #ifdef __cplusplus
168 }
169 #endif
170
171
172 #if GNULIB_XALLOC && defined __cplusplus
173
174 /* C++ does not allow conversions from void * to other pointer types
175    without a cast.  Use templates to work around the problem when
176    possible.  */
177
178 template <typename T> inline T *
179 xrealloc (T *p, size_t s)
180 {
181   return (T *) xrealloc ((void *) p, s);
182 }
183
184 template <typename T> inline T *
185 xreallocarray (T *p, size_t n, size_t s)
186 {
187   return (T *) xreallocarray ((void *) p, n, s);
188 }
189
190 /* FIXME: Deprecate this in favor of xreallocarray?  */
191 template <typename T> inline T *
192 xnrealloc (T *p, size_t n, size_t s)
193 {
194   return xreallocarray (p, n, s);
195 }
196
197 template <typename T> inline T *
198 x2realloc (T *p, size_t *pn)
199 {
200   return (T *) x2realloc ((void *) p, pn);
201 }
202
203 template <typename T> inline T *
204 x2nrealloc (T *p, size_t *pn, size_t s)
205 {
206   return (T *) x2nrealloc ((void *) p, pn, s);
207 }
208
209 template <typename T> inline T *
210 xmemdup (T const *p, size_t s)
211 {
212   return (T *) xmemdup ((void const *) p, s);
213 }
214
215 #endif /* GNULIB_XALLOC && C++ */
216
217
218 _GL_INLINE_HEADER_END
219
220 #endif /* !XALLOC_H_ */