VirtualBox

source: kBuild/trunk/src/grep/lib/xalloc.h

Last change on this file was 3529, checked in by bird, 3 years ago

Imported grep 3.7 from grep-3.7.tar.gz (sha256: c22b0cf2d4f6bbe599c902387e8058990e1eee99aef333a203829e5fd3dbb342), applying minimal auto-props.

  • Property svn:eol-style set to native
File size: 6.6 KB
Line 
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
40extern "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
57void *xmalloc (size_t s)
58 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
59 _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
60void *ximalloc (idx_t s)
61 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
62 _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
63void *xzalloc (size_t s)
64 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
65 _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
66void *xizalloc (idx_t s)
67 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
68 _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
69void *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;
72void *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;
75void *xrealloc (void *p, size_t s)
76 _GL_ATTRIBUTE_ALLOC_SIZE ((2));
77void *xirealloc (void *p, idx_t s)
78 _GL_ATTRIBUTE_ALLOC_SIZE ((2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
79void *xreallocarray (void *p, size_t n, size_t s)
80 _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
81void *xireallocarray (void *p, idx_t n, idx_t s)
82 _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3)) _GL_ATTRIBUTE_RETURNS_NONNULL;
83void *x2realloc (void *p, size_t *ps) /* superseded by xpalloc */
84 _GL_ATTRIBUTE_RETURNS_NONNULL;
85void *x2nrealloc (void *p, size_t *pn, size_t s) /* superseded by xpalloc */
86 _GL_ATTRIBUTE_RETURNS_NONNULL;
87void *xpalloc (void *pa, idx_t *pn, idx_t n_incr_min, ptrdiff_t n_max, idx_t s)
88 _GL_ATTRIBUTE_RETURNS_NONNULL;
89void *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;
92void *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;
95char *ximemdup0 (void const *p, idx_t s)
96 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
97 _GL_ATTRIBUTE_RETURNS_NONNULL;
98char *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
131void *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
135/* FIXME: Deprecate this in favor of xreallocarray? */
136/* Change the size of an allocated block of memory P to an array of N
137 objects each of S bytes, with error checking. S must be nonzero. */
138
139XALLOC_INLINE void *xnrealloc (void *p, size_t n, size_t s)
140 _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
141XALLOC_INLINE void *
142xnrealloc (void *p, size_t n, size_t s)
143{
144 return xreallocarray (p, n, s);
145}
146
147/* Return a pointer to a new buffer of N bytes. This is like xmalloc,
148 except it returns char *. */
149
150char *xcharalloc (size_t n)
151 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
152 _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
153
154#endif /* GNULIB_XALLOC */
155
156
157#ifdef __cplusplus
158}
159#endif
160
161
162#if GNULIB_XALLOC && defined __cplusplus
163
164/* C++ does not allow conversions from void * to other pointer types
165 without a cast. Use templates to work around the problem when
166 possible. */
167
168template <typename T> inline T *
169xrealloc (T *p, size_t s)
170{
171 return (T *) xrealloc ((void *) p, s);
172}
173
174template <typename T> inline T *
175xreallocarray (T *p, size_t n, size_t s)
176{
177 return (T *) xreallocarray ((void *) p, n, s);
178}
179
180/* FIXME: Deprecate this in favor of xreallocarray? */
181template <typename T> inline T *
182xnrealloc (T *p, size_t n, size_t s)
183{
184 return xreallocarray (p, n, s);
185}
186
187template <typename T> inline T *
188x2realloc (T *p, size_t *pn)
189{
190 return (T *) x2realloc ((void *) p, pn);
191}
192
193template <typename T> inline T *
194x2nrealloc (T *p, size_t *pn, size_t s)
195{
196 return (T *) x2nrealloc ((void *) p, pn, s);
197}
198
199template <typename T> inline T *
200xmemdup (T const *p, size_t s)
201{
202 return (T *) xmemdup ((void const *) p, s);
203}
204
205#endif /* GNULIB_XALLOC && C++ */
206
207
208_GL_INLINE_HEADER_END
209
210#endif /* !XALLOC_H_ */
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette