annotate lib/xalloc.h @ 276:23034db7dd8a 2.5

fixed expression 'slots' to not store null on a pass 1 store
author lost
date Mon, 31 Aug 2009 08:39:36 +0000
parents d5392bb5da3c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
272
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1 /* xalloc.h -- malloc with out-of-memory checking
d5392bb5da3c Added generated files
lost
parents:
diff changeset
2
d5392bb5da3c Added generated files
lost
parents:
diff changeset
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
4 1999, 2000, 2003, 2004, 2006, 2007, 2008 Free Software Foundation, Inc.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
5
d5392bb5da3c Added generated files
lost
parents:
diff changeset
6 This program is free software: you can redistribute it and/or modify
d5392bb5da3c Added generated files
lost
parents:
diff changeset
7 it under the terms of the GNU General Public License as published by
d5392bb5da3c Added generated files
lost
parents:
diff changeset
8 the Free Software Foundation; either version 3 of the License, or
d5392bb5da3c Added generated files
lost
parents:
diff changeset
9 (at your option) any later version.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
10
d5392bb5da3c Added generated files
lost
parents:
diff changeset
11 This program is distributed in the hope that it will be useful,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
d5392bb5da3c Added generated files
lost
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d5392bb5da3c Added generated files
lost
parents:
diff changeset
14 GNU General Public License for more details.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
15
d5392bb5da3c Added generated files
lost
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
d5392bb5da3c Added generated files
lost
parents:
diff changeset
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
18
d5392bb5da3c Added generated files
lost
parents:
diff changeset
19 #ifndef XALLOC_H_
d5392bb5da3c Added generated files
lost
parents:
diff changeset
20 # define XALLOC_H_
d5392bb5da3c Added generated files
lost
parents:
diff changeset
21
d5392bb5da3c Added generated files
lost
parents:
diff changeset
22 # include <stddef.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
23
d5392bb5da3c Added generated files
lost
parents:
diff changeset
24
d5392bb5da3c Added generated files
lost
parents:
diff changeset
25 # ifdef __cplusplus
d5392bb5da3c Added generated files
lost
parents:
diff changeset
26 extern "C" {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
27 # endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
28
d5392bb5da3c Added generated files
lost
parents:
diff changeset
29
d5392bb5da3c Added generated files
lost
parents:
diff changeset
30 # ifndef __attribute__
d5392bb5da3c Added generated files
lost
parents:
diff changeset
31 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
32 # define __attribute__(x)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
33 # endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
34 # endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
35
d5392bb5da3c Added generated files
lost
parents:
diff changeset
36 # ifndef ATTRIBUTE_NORETURN
d5392bb5da3c Added generated files
lost
parents:
diff changeset
37 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
38 # endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
39
d5392bb5da3c Added generated files
lost
parents:
diff changeset
40 # ifndef ATTRIBUTE_MALLOC
d5392bb5da3c Added generated files
lost
parents:
diff changeset
41 # if __GNUC__ >= 3
d5392bb5da3c Added generated files
lost
parents:
diff changeset
42 # define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
43 # else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
44 # define ATTRIBUTE_MALLOC
d5392bb5da3c Added generated files
lost
parents:
diff changeset
45 # endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
46 # endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
47
d5392bb5da3c Added generated files
lost
parents:
diff changeset
48 /* This function is always triggered when memory is exhausted.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
49 It must be defined by the application, either explicitly
d5392bb5da3c Added generated files
lost
parents:
diff changeset
50 or by using gnulib's xalloc-die module. This is the
d5392bb5da3c Added generated files
lost
parents:
diff changeset
51 function to call when one wants the program to die because of a
d5392bb5da3c Added generated files
lost
parents:
diff changeset
52 memory allocation failure. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
53 extern void xalloc_die (void) ATTRIBUTE_NORETURN;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
54
d5392bb5da3c Added generated files
lost
parents:
diff changeset
55 void *xmalloc (size_t s) ATTRIBUTE_MALLOC;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
56 void *xzalloc (size_t s) ATTRIBUTE_MALLOC;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
57 void *xcalloc (size_t n, size_t s) ATTRIBUTE_MALLOC;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
58 void *xrealloc (void *p, size_t s);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
59 void *x2realloc (void *p, size_t *pn);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
60 void *xmemdup (void const *p, size_t s) ATTRIBUTE_MALLOC;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
61 char *xstrdup (char const *str) ATTRIBUTE_MALLOC;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
62
d5392bb5da3c Added generated files
lost
parents:
diff changeset
63 /* Return 1 if an array of N objects, each of size S, cannot exist due
d5392bb5da3c Added generated files
lost
parents:
diff changeset
64 to size arithmetic overflow. S must be positive and N must be
d5392bb5da3c Added generated files
lost
parents:
diff changeset
65 nonnegative. This is a macro, not an inline function, so that it
d5392bb5da3c Added generated files
lost
parents:
diff changeset
66 works correctly even when SIZE_MAX < N.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
67
d5392bb5da3c Added generated files
lost
parents:
diff changeset
68 By gnulib convention, SIZE_MAX represents overflow in size
d5392bb5da3c Added generated files
lost
parents:
diff changeset
69 calculations, so the conservative dividend to use here is
d5392bb5da3c Added generated files
lost
parents:
diff changeset
70 SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
71 However, malloc (SIZE_MAX) fails on all known hosts where
d5392bb5da3c Added generated files
lost
parents:
diff changeset
72 sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
d5392bb5da3c Added generated files
lost
parents:
diff changeset
73 exactly-SIZE_MAX allocations on such hosts; this avoids a test and
d5392bb5da3c Added generated files
lost
parents:
diff changeset
74 branch when S is known to be 1. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
75 # define xalloc_oversized(n, s) \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
76 ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
77
d5392bb5da3c Added generated files
lost
parents:
diff changeset
78
d5392bb5da3c Added generated files
lost
parents:
diff changeset
79 /* In the following macros, T must be an elementary or structure/union or
d5392bb5da3c Added generated files
lost
parents:
diff changeset
80 typedef'ed type, or a pointer to such a type. To apply one of the
d5392bb5da3c Added generated files
lost
parents:
diff changeset
81 following macros to a function pointer or array type, you need to typedef
d5392bb5da3c Added generated files
lost
parents:
diff changeset
82 it first and use the typedef name. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
83
d5392bb5da3c Added generated files
lost
parents:
diff changeset
84 /* Allocate an object of type T dynamically, with error checking. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
85 /* extern t *XMALLOC (typename t); */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
86 # define XMALLOC(t) ((t *) xmalloc (sizeof (t)))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
87
d5392bb5da3c Added generated files
lost
parents:
diff changeset
88 /* Allocate memory for N elements of type T, with error checking. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
89 /* extern t *XNMALLOC (size_t n, typename t); */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
90 # define XNMALLOC(n, t) \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
91 ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t))))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
92
d5392bb5da3c Added generated files
lost
parents:
diff changeset
93 /* Allocate an object of type T dynamically, with error checking,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
94 and zero it. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
95 /* extern t *XZALLOC (typename t); */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
96 # define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
97
d5392bb5da3c Added generated files
lost
parents:
diff changeset
98 /* Allocate memory for N elements of type T, with error checking,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
99 and zero it. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
100 /* extern t *XCALLOC (size_t n, typename t); */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
101 # define XCALLOC(n, t) \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
102 ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
103
d5392bb5da3c Added generated files
lost
parents:
diff changeset
104
d5392bb5da3c Added generated files
lost
parents:
diff changeset
105 # if HAVE_INLINE
d5392bb5da3c Added generated files
lost
parents:
diff changeset
106 # define static_inline static inline
d5392bb5da3c Added generated files
lost
parents:
diff changeset
107 # else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
108 void *xnmalloc (size_t n, size_t s) ATTRIBUTE_MALLOC;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
109 void *xnrealloc (void *p, size_t n, size_t s);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
110 void *x2nrealloc (void *p, size_t *pn, size_t s);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
111 char *xcharalloc (size_t n) ATTRIBUTE_MALLOC;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
112 # endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
113
d5392bb5da3c Added generated files
lost
parents:
diff changeset
114 # ifdef static_inline
d5392bb5da3c Added generated files
lost
parents:
diff changeset
115
d5392bb5da3c Added generated files
lost
parents:
diff changeset
116 /* Allocate an array of N objects, each with S bytes of memory,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
117 dynamically, with error checking. S must be nonzero. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
118
d5392bb5da3c Added generated files
lost
parents:
diff changeset
119 static_inline void *xnmalloc (size_t n, size_t s) ATTRIBUTE_MALLOC;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
120 static_inline void *
d5392bb5da3c Added generated files
lost
parents:
diff changeset
121 xnmalloc (size_t n, size_t s)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
122 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
123 if (xalloc_oversized (n, s))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
124 xalloc_die ();
d5392bb5da3c Added generated files
lost
parents:
diff changeset
125 return xmalloc (n * s);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
126 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
127
d5392bb5da3c Added generated files
lost
parents:
diff changeset
128 /* Change the size of an allocated block of memory P to an array of N
d5392bb5da3c Added generated files
lost
parents:
diff changeset
129 objects each of S bytes, with error checking. S must be nonzero. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
130
d5392bb5da3c Added generated files
lost
parents:
diff changeset
131 static_inline void *
d5392bb5da3c Added generated files
lost
parents:
diff changeset
132 xnrealloc (void *p, size_t n, size_t s)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
133 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
134 if (xalloc_oversized (n, s))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
135 xalloc_die ();
d5392bb5da3c Added generated files
lost
parents:
diff changeset
136 return xrealloc (p, n * s);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
137 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
138
d5392bb5da3c Added generated files
lost
parents:
diff changeset
139 /* If P is null, allocate a block of at least *PN such objects;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
140 otherwise, reallocate P so that it contains more than *PN objects
d5392bb5da3c Added generated files
lost
parents:
diff changeset
141 each of S bytes. *PN must be nonzero unless P is null, and S must
d5392bb5da3c Added generated files
lost
parents:
diff changeset
142 be nonzero. Set *PN to the new number of objects, and return the
d5392bb5da3c Added generated files
lost
parents:
diff changeset
143 pointer to the new block. *PN is never set to zero, and the
d5392bb5da3c Added generated files
lost
parents:
diff changeset
144 returned pointer is never null.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
145
d5392bb5da3c Added generated files
lost
parents:
diff changeset
146 Repeated reallocations are guaranteed to make progress, either by
d5392bb5da3c Added generated files
lost
parents:
diff changeset
147 allocating an initial block with a nonzero size, or by allocating a
d5392bb5da3c Added generated files
lost
parents:
diff changeset
148 larger block.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
149
d5392bb5da3c Added generated files
lost
parents:
diff changeset
150 In the following implementation, nonzero sizes are increased by a
d5392bb5da3c Added generated files
lost
parents:
diff changeset
151 factor of approximately 1.5 so that repeated reallocations have
d5392bb5da3c Added generated files
lost
parents:
diff changeset
152 O(N) overall cost rather than O(N**2) cost, but the
d5392bb5da3c Added generated files
lost
parents:
diff changeset
153 specification for this function does not guarantee that rate.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
154
d5392bb5da3c Added generated files
lost
parents:
diff changeset
155 Here is an example of use:
d5392bb5da3c Added generated files
lost
parents:
diff changeset
156
d5392bb5da3c Added generated files
lost
parents:
diff changeset
157 int *p = NULL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
158 size_t used = 0;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
159 size_t allocated = 0;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
160
d5392bb5da3c Added generated files
lost
parents:
diff changeset
161 void
d5392bb5da3c Added generated files
lost
parents:
diff changeset
162 append_int (int value)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
163 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
164 if (used == allocated)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
165 p = x2nrealloc (p, &allocated, sizeof *p);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
166 p[used++] = value;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
167 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
168
d5392bb5da3c Added generated files
lost
parents:
diff changeset
169 This causes x2nrealloc to allocate a block of some nonzero size the
d5392bb5da3c Added generated files
lost
parents:
diff changeset
170 first time it is called.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
171
d5392bb5da3c Added generated files
lost
parents:
diff changeset
172 To have finer-grained control over the initial size, set *PN to a
d5392bb5da3c Added generated files
lost
parents:
diff changeset
173 nonzero value before calling this function with P == NULL. For
d5392bb5da3c Added generated files
lost
parents:
diff changeset
174 example:
d5392bb5da3c Added generated files
lost
parents:
diff changeset
175
d5392bb5da3c Added generated files
lost
parents:
diff changeset
176 int *p = NULL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
177 size_t used = 0;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
178 size_t allocated = 0;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
179 size_t allocated1 = 1000;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
180
d5392bb5da3c Added generated files
lost
parents:
diff changeset
181 void
d5392bb5da3c Added generated files
lost
parents:
diff changeset
182 append_int (int value)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
183 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
184 if (used == allocated)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
185 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
186 p = x2nrealloc (p, &allocated1, sizeof *p);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
187 allocated = allocated1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
188 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
189 p[used++] = value;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
190 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
191
d5392bb5da3c Added generated files
lost
parents:
diff changeset
192 */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
193
d5392bb5da3c Added generated files
lost
parents:
diff changeset
194 static_inline void *
d5392bb5da3c Added generated files
lost
parents:
diff changeset
195 x2nrealloc (void *p, size_t *pn, size_t s)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
196 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
197 size_t n = *pn;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
198
d5392bb5da3c Added generated files
lost
parents:
diff changeset
199 if (! p)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
200 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
201 if (! n)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
202 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
203 /* The approximate size to use for initial small allocation
d5392bb5da3c Added generated files
lost
parents:
diff changeset
204 requests, when the invoking code specifies an old size of
d5392bb5da3c Added generated files
lost
parents:
diff changeset
205 zero. 64 bytes is the largest "small" request for the
d5392bb5da3c Added generated files
lost
parents:
diff changeset
206 GNU C library malloc. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
207 enum { DEFAULT_MXFAST = 64 };
d5392bb5da3c Added generated files
lost
parents:
diff changeset
208
d5392bb5da3c Added generated files
lost
parents:
diff changeset
209 n = DEFAULT_MXFAST / s;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
210 n += !n;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
211 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
212 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
213 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
214 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
215 /* Set N = ceil (1.5 * N) so that progress is made if N == 1.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
216 Check for overflow, so that N * S stays in size_t range.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
217 The check is slightly conservative, but an exact check isn't
d5392bb5da3c Added generated files
lost
parents:
diff changeset
218 worth the trouble. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
219 if ((size_t) -1 / 3 * 2 / s <= n)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
220 xalloc_die ();
d5392bb5da3c Added generated files
lost
parents:
diff changeset
221 n += (n + 1) / 2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
222 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
223
d5392bb5da3c Added generated files
lost
parents:
diff changeset
224 *pn = n;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
225 return xrealloc (p, n * s);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
226 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
227
d5392bb5da3c Added generated files
lost
parents:
diff changeset
228 /* Return a pointer to a new buffer of N bytes. This is like xmalloc,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
229 except it returns char *. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
230
d5392bb5da3c Added generated files
lost
parents:
diff changeset
231 static_inline char *xcharalloc (size_t n) ATTRIBUTE_MALLOC;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
232 static_inline char *
d5392bb5da3c Added generated files
lost
parents:
diff changeset
233 xcharalloc (size_t n)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
234 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
235 return XNMALLOC (n, char);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
236 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
237
d5392bb5da3c Added generated files
lost
parents:
diff changeset
238 # endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
239
d5392bb5da3c Added generated files
lost
parents:
diff changeset
240 # ifdef __cplusplus
d5392bb5da3c Added generated files
lost
parents:
diff changeset
241 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
242
d5392bb5da3c Added generated files
lost
parents:
diff changeset
243 /* C++ does not allow conversions from void * to other pointer types
d5392bb5da3c Added generated files
lost
parents:
diff changeset
244 without a cast. Use templates to work around the problem when
d5392bb5da3c Added generated files
lost
parents:
diff changeset
245 possible. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
246
d5392bb5da3c Added generated files
lost
parents:
diff changeset
247 template <typename T> inline T *
d5392bb5da3c Added generated files
lost
parents:
diff changeset
248 xrealloc (T *p, size_t s)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
249 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
250 return (T *) xrealloc ((void *) p, s);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
251 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
252
d5392bb5da3c Added generated files
lost
parents:
diff changeset
253 template <typename T> inline T *
d5392bb5da3c Added generated files
lost
parents:
diff changeset
254 xnrealloc (T *p, size_t n, size_t s)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
255 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
256 return (T *) xnrealloc ((void *) p, n, s);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
257 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
258
d5392bb5da3c Added generated files
lost
parents:
diff changeset
259 template <typename T> inline T *
d5392bb5da3c Added generated files
lost
parents:
diff changeset
260 x2realloc (T *p, size_t *pn)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
261 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
262 return (T *) x2realloc ((void *) p, pn);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
263 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
264
d5392bb5da3c Added generated files
lost
parents:
diff changeset
265 template <typename T> inline T *
d5392bb5da3c Added generated files
lost
parents:
diff changeset
266 x2nrealloc (T *p, size_t *pn, size_t s)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
267 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
268 return (T *) x2nrealloc ((void *) p, pn, s);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
269 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
270
d5392bb5da3c Added generated files
lost
parents:
diff changeset
271 template <typename T> inline T *
d5392bb5da3c Added generated files
lost
parents:
diff changeset
272 xmemdup (T const *p, size_t s)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
273 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
274 return (T *) xmemdup ((void const *) p, s);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
275 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
276
d5392bb5da3c Added generated files
lost
parents:
diff changeset
277 # endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
278
d5392bb5da3c Added generated files
lost
parents:
diff changeset
279
d5392bb5da3c Added generated files
lost
parents:
diff changeset
280 #endif /* !XALLOC_H_ */