annotate lib/xalloc.h @ 10068:c7189e3f4fa9

Split xmemdup0 into its own module. * modules/xmemdup0: New file. * lib/xmemdup0.h: Likewise. * lib/xmemdup0.c: Likewise. * MODULES.html.sh (Memory management functions): Add xmemdup0. * lib/xalloc.h (xmemdup0): Remove. * lib/xmalloc.c (xmemdup0): Likewise. Signed-off-by: Eric Blake <ebb9@byu.net>
author Eric Blake <ebb9@byu.net>
date Tue, 13 May 2008 21:28:43 -0600
parents 00a47a592b0a
children cf0a8e173c39
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
1 /* xalloc.h -- malloc with out-of-memory checking
4397
c6450308f123 Assume C89, so PARAMS isn't needed.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4347
diff changeset
2
c6450308f123 Assume C89, so PARAMS isn't needed.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4347
diff changeset
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
9613
c4fa39bf5223 Don't redefine __attribute__ without a need.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
4 1999, 2000, 2003, 2004, 2006, 2007, 2008 Free Software Foundation, Inc.
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
5
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8077
diff changeset
6 This program is free software: you can redistribute it and/or modify
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
7 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8077
diff changeset
8 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8077
diff changeset
9 (at your option) any later version.
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
10
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
11 This program is distributed in the hope that it will be useful,
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
14 GNU General Public License for more details.
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
15
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
16 You should have received a copy of the GNU General Public License
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8077
diff changeset
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
18
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
19 #ifndef XALLOC_H_
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
20 # define XALLOC_H_
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
21
4347
df44e79ce676 .h files should stand alone, but we shouldn't include <sys/types.h>
Paul Eggert <eggert@cs.ucla.edu>
parents: 2951
diff changeset
22 # include <stddef.h>
4862
1f6c3678919c Rely on SIZE_MAX and PTRDIFF_MAX as defined by ISO C 99.
Bruno Haible <bruno@clisp.org>
parents: 4859
diff changeset
23
5105
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
24
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
25 # ifdef __cplusplus
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
26 extern "C" {
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
27 # endif
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
28
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
29
1958
3bf5a026bc95 (__attribute__): Protect against redefinition.
Jim Meyering <jim@meyering.net>
parents: 1955
diff changeset
30 # ifndef __attribute__
9613
c4fa39bf5223 Don't redefine __attribute__ without a need.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
31 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
1958
3bf5a026bc95 (__attribute__): Protect against redefinition.
Jim Meyering <jim@meyering.net>
parents: 1955
diff changeset
32 # define __attribute__(x)
3bf5a026bc95 (__attribute__): Protect against redefinition.
Jim Meyering <jim@meyering.net>
parents: 1955
diff changeset
33 # endif
1953
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
34 # endif
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
35
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
36 # ifndef ATTRIBUTE_NORETURN
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
37 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
38 # endif
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
39
5166
95d4226b62d3 Remove dependency of xalloc in exitfail, error, gettext.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5105
diff changeset
40 /* This function is always triggered when memory is exhausted.
95d4226b62d3 Remove dependency of xalloc in exitfail, error, gettext.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5105
diff changeset
41 It must be defined by the application, either explicitly
95d4226b62d3 Remove dependency of xalloc in exitfail, error, gettext.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5105
diff changeset
42 or by using gnulib's xalloc-die module. This is the
1953
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
43 function to call when one wants the program to die because of a
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
44 memory allocation failure. */
4397
c6450308f123 Assume C89, so PARAMS isn't needed.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4347
diff changeset
45 extern void xalloc_die (void) ATTRIBUTE_NORETURN;
1953
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
46
4783
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
47 void *xmalloc (size_t s);
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
48 void *xzalloc (size_t s);
4397
c6450308f123 Assume C89, so PARAMS isn't needed.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4347
diff changeset
49 void *xcalloc (size_t n, size_t s);
4783
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
50 void *xrealloc (void *p, size_t s);
4817
7c67f04e1c19 Add x2realloc, x2nrealloc. Port to C99 inline.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4783
diff changeset
51 void *x2realloc (void *p, size_t *pn);
5321
fe390d57473a Rename xclone to xmemdup. Remove obsolete xalloc macros.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5166
diff changeset
52 void *xmemdup (void const *p, size_t s);
fe390d57473a Rename xclone to xmemdup. Remove obsolete xalloc macros.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5166
diff changeset
53 char *xstrdup (char const *str);
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
54
4830
410ea3e253b9 Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4818
diff changeset
55 /* Return 1 if an array of N objects, each of size S, cannot exist due
410ea3e253b9 Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4818
diff changeset
56 to size arithmetic overflow. S must be positive and N must be
410ea3e253b9 Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4818
diff changeset
57 nonnegative. This is a macro, not an inline function, so that it
4856
2759ea168f25 Reject allocations of exactly SIZE_MAX bytes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4830
diff changeset
58 works correctly even when SIZE_MAX < N.
2759ea168f25 Reject allocations of exactly SIZE_MAX bytes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4830
diff changeset
59
2759ea168f25 Reject allocations of exactly SIZE_MAX bytes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4830
diff changeset
60 By gnulib convention, SIZE_MAX represents overflow in size
4859
a7a0af3a9e13 Fix off-by-one error in xalloc_oversized.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4856
diff changeset
61 calculations, so the conservative dividend to use here is
a7a0af3a9e13 Fix off-by-one error in xalloc_oversized.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4856
diff changeset
62 SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
a7a0af3a9e13 Fix off-by-one error in xalloc_oversized.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4856
diff changeset
63 However, malloc (SIZE_MAX) fails on all known hosts where
4865
8690f8e0e0bf Remove dependency of xalloc.h on SIZE_MAX and PTRDIFF_MAX.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4862
diff changeset
64 sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
4856
2759ea168f25 Reject allocations of exactly SIZE_MAX bytes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4830
diff changeset
65 exactly-SIZE_MAX allocations on such hosts; this avoids a test and
2759ea168f25 Reject allocations of exactly SIZE_MAX bytes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4830
diff changeset
66 branch when S is known to be 1. */
4865
8690f8e0e0bf Remove dependency of xalloc.h on SIZE_MAX and PTRDIFF_MAX.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4862
diff changeset
67 # define xalloc_oversized(n, s) \
8690f8e0e0bf Remove dependency of xalloc.h on SIZE_MAX and PTRDIFF_MAX.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4862
diff changeset
68 ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
4818
167a92276385 New function xalloc_oversized.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4817
diff changeset
69
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
70
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
71 /* In the following macros, T must be an elementary or structure/union or
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
72 typedef'ed type, or a pointer to such a type. To apply one of the
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
73 following macros to a function pointer or array type, you need to typedef
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
74 it first and use the typedef name. */
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
75
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
76 /* Allocate an object of type T dynamically, with error checking. */
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
77 /* extern t *XMALLOC (typename t); */
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
78 # define XMALLOC(t) ((t *) xmalloc (sizeof (t)))
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
79
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
80 /* Allocate memory for N elements of type T, with error checking. */
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
81 /* extern t *XNMALLOC (size_t n, typename t); */
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
82 # define XNMALLOC(n, t) \
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
83 ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t))))
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
84
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
85 /* Allocate an object of type T dynamically, with error checking,
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
86 and zero it. */
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
87 /* extern t *XZALLOC (typename t); */
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
88 # define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
89
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
90 /* Allocate memory for N elements of type T, with error checking,
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
91 and zero it. */
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
92 /* extern t *XCALLOC (size_t n, typename t); */
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
93 # define XCALLOC(n, t) \
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
94 ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
95
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
96
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
97 # if HAVE_INLINE
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
98 # define static_inline static inline
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
99 # else
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
100 void *xnmalloc (size_t n, size_t s);
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
101 void *xnrealloc (void *p, size_t n, size_t s);
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
102 void *x2nrealloc (void *p, size_t *pn, size_t s);
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
103 char *xcharalloc (size_t n);
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
104 # endif
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
105
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
106 # ifdef static_inline
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
107
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
108 /* Allocate an array of N objects, each with S bytes of memory,
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
109 dynamically, with error checking. S must be nonzero. */
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
110
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
111 static_inline void *
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
112 xnmalloc (size_t n, size_t s)
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
113 {
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
114 if (xalloc_oversized (n, s))
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
115 xalloc_die ();
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
116 return xmalloc (n * s);
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
117 }
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
118
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
119 /* Change the size of an allocated block of memory P to an array of N
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
120 objects each of S bytes, with error checking. S must be nonzero. */
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
121
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
122 static_inline void *
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
123 xnrealloc (void *p, size_t n, size_t s)
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
124 {
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
125 if (xalloc_oversized (n, s))
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
126 xalloc_die ();
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
127 return xrealloc (p, n * s);
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
128 }
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
129
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
130 /* If P is null, allocate a block of at least *PN such objects;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
131 otherwise, reallocate P so that it contains more than *PN objects
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
132 each of S bytes. *PN must be nonzero unless P is null, and S must
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
133 be nonzero. Set *PN to the new number of objects, and return the
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
134 pointer to the new block. *PN is never set to zero, and the
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
135 returned pointer is never null.
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
136
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
137 Repeated reallocations are guaranteed to make progress, either by
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
138 allocating an initial block with a nonzero size, or by allocating a
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
139 larger block.
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
140
8065
25c6999713ef Give tools a better chance to allocate space for very large buffers.
Jim Meyering <jim@meyering.net>
parents: 7619
diff changeset
141 In the following implementation, nonzero sizes are increased by a
25c6999713ef Give tools a better chance to allocate space for very large buffers.
Jim Meyering <jim@meyering.net>
parents: 7619
diff changeset
142 factor of approximately 1.5 so that repeated reallocations have
8077
9cd2d21f40cc * lib/xalloc.h (x2nrealloc): Fix an unlikely bug in the overflow
Paul Eggert <eggert@cs.ucla.edu>
parents: 8065
diff changeset
143 O(N) overall cost rather than O(N**2) cost, but the
8065
25c6999713ef Give tools a better chance to allocate space for very large buffers.
Jim Meyering <jim@meyering.net>
parents: 7619
diff changeset
144 specification for this function does not guarantee that rate.
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
145
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
146 Here is an example of use:
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
147
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
148 int *p = NULL;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
149 size_t used = 0;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
150 size_t allocated = 0;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
151
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
152 void
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
153 append_int (int value)
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
154 {
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
155 if (used == allocated)
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
156 p = x2nrealloc (p, &allocated, sizeof *p);
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
157 p[used++] = value;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
158 }
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
159
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
160 This causes x2nrealloc to allocate a block of some nonzero size the
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
161 first time it is called.
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
162
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
163 To have finer-grained control over the initial size, set *PN to a
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
164 nonzero value before calling this function with P == NULL. For
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
165 example:
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
166
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
167 int *p = NULL;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
168 size_t used = 0;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
169 size_t allocated = 0;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
170 size_t allocated1 = 1000;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
171
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
172 void
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
173 append_int (int value)
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
174 {
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
175 if (used == allocated)
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
176 {
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
177 p = x2nrealloc (p, &allocated1, sizeof *p);
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
178 allocated = allocated1;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
179 }
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
180 p[used++] = value;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
181 }
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
182
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
183 */
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
184
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
185 static_inline void *
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
186 x2nrealloc (void *p, size_t *pn, size_t s)
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
187 {
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
188 size_t n = *pn;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
189
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
190 if (! p)
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
191 {
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
192 if (! n)
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
193 {
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
194 /* The approximate size to use for initial small allocation
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
195 requests, when the invoking code specifies an old size of
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
196 zero. 64 bytes is the largest "small" request for the
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
197 GNU C library malloc. */
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
198 enum { DEFAULT_MXFAST = 64 };
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
199
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
200 n = DEFAULT_MXFAST / s;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
201 n += !n;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
202 }
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
203 }
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
204 else
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
205 {
8077
9cd2d21f40cc * lib/xalloc.h (x2nrealloc): Fix an unlikely bug in the overflow
Paul Eggert <eggert@cs.ucla.edu>
parents: 8065
diff changeset
206 /* Set N = ceil (1.5 * N) so that progress is made if N == 1.
9cd2d21f40cc * lib/xalloc.h (x2nrealloc): Fix an unlikely bug in the overflow
Paul Eggert <eggert@cs.ucla.edu>
parents: 8065
diff changeset
207 Check for overflow, so that N * S stays in size_t range.
9cd2d21f40cc * lib/xalloc.h (x2nrealloc): Fix an unlikely bug in the overflow
Paul Eggert <eggert@cs.ucla.edu>
parents: 8065
diff changeset
208 The check is slightly conservative, but an exact check isn't
9cd2d21f40cc * lib/xalloc.h (x2nrealloc): Fix an unlikely bug in the overflow
Paul Eggert <eggert@cs.ucla.edu>
parents: 8065
diff changeset
209 worth the trouble. */
9cd2d21f40cc * lib/xalloc.h (x2nrealloc): Fix an unlikely bug in the overflow
Paul Eggert <eggert@cs.ucla.edu>
parents: 8065
diff changeset
210 if ((size_t) -1 / 3 * 2 / s <= n)
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
211 xalloc_die ();
8077
9cd2d21f40cc * lib/xalloc.h (x2nrealloc): Fix an unlikely bug in the overflow
Paul Eggert <eggert@cs.ucla.edu>
parents: 8065
diff changeset
212 n += (n + 1) / 2;
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
213 }
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
214
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
215 *pn = n;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
216 return xrealloc (p, n * s);
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
217 }
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
218
7603
23f14c284219 Simplify xmalloc expressions. Add overflow check in xmalloc arguments.
Bruno Haible <bruno@clisp.org>
parents: 7587
diff changeset
219 /* Return a pointer to a new buffer of N bytes. This is like xmalloc,
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
220 except it returns char *. */
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
221
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
222 static_inline char *
7603
23f14c284219 Simplify xmalloc expressions. Add overflow check in xmalloc arguments.
Bruno Haible <bruno@clisp.org>
parents: 7587
diff changeset
223 xcharalloc (size_t n)
7587
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
224 {
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
225 return XNMALLOC (n, char);
7587
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
226 }
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
227
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
228 # endif
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
229
5105
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
230 # ifdef __cplusplus
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
231 }
7587
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
232
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
233 /* C++ does not allow conversions from void * to other pointer types
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
234 without a cast. Use templates to work around the problem when
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
235 possible. */
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
236
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
237 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
238 xrealloc (T *p, size_t s)
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
239 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
240 return (T *) xrealloc ((void *) p, s);
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
241 }
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
242
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
243 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
244 xnrealloc (T *p, size_t n, size_t s)
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
245 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
246 return (T *) xnrealloc ((void *) p, n, s);
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
247 }
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
248
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
249 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
250 x2realloc (T *p, size_t *pn)
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
251 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
252 return (T *) x2realloc ((void *) p, pn);
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
253 }
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
254
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
255 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
256 x2nrealloc (T *p, size_t *pn, size_t s)
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
257 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
258 return (T *) x2nrealloc ((void *) p, pn, s);
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
259 }
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
260
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
261 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
262 xmemdup (T const *p, size_t s)
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
263 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
264 return (T *) xmemdup ((void const *) p, s);
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
265 }
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
266
5105
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
267 # endif
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
268
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
269
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
270 #endif /* !XALLOC_H_ */