annotate lib/xalloc.h @ 17177:902795f52835

xalloc: better 'inline' * lib/xmalloc.c, lib/xalloc.h (XALLOC_INLINE): New macro. Replace all uses of 'static inline' with it. (static_inline): Remove. * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc, xcharalloc): Let 'extern inline' do the work automatically, instead of doing it by hand. * m4/xalloc.m4 (gl_PREREQ_XALLOC, gl_PREREQ_XMALLOC): Remove. All uses removed. * modules/xalloc (Depends-on): Remove 'inline'. Add 'extern-inline'.
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 20 Nov 2012 22:25:07 -0800
parents 8250f2777afc
children e542fd46ad6f
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
16201
8250f2777afc maint: update all copyright year number ranges
Jim Meyering <meyering@redhat.com>
parents: 15486
diff changeset
3 Copyright (C) 1990-2000, 2003-2004, 2006-2012 Free Software Foundation, Inc.
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
4
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8077
diff changeset
5 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
6 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
7 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
8 (at your option) any later version.
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
9
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
10 This program is distributed in the hope that it will be useful,
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
13 GNU General Public License for more details.
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
14
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
15 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
16 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
17
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
18 #ifndef XALLOC_H_
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
19 #define XALLOC_H_
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
20
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
21 #include <stddef.h>
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
22
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
23 #include "xalloc-oversized.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
24
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
25 _GL_INLINE_HEADER_BEGIN
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
26 #ifndef XALLOC_INLINE
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
27 # define XALLOC_INLINE _GL_INLINE
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
28 #endif
5105
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
29
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
30 #ifdef __cplusplus
5105
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
31 extern "C" {
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
32 #endif
5105
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
33
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
34
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
35 #if __GNUC__ >= 3
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
36 # define _GL_ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
37 #else
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
38 # define _GL_ATTRIBUTE_MALLOC
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
39 #endif
10074
cf0a8e173c39 Help GCC to do better code generation.
Bruno Haible <bruno@clisp.org>
parents: 10068
diff changeset
40
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
41 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
42 # define _GL_ATTRIBUTE_ALLOC_SIZE(args) __attribute__ ((__alloc_size__ args))
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
43 #else
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
44 # define _GL_ATTRIBUTE_ALLOC_SIZE(args)
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
45 #endif
13815
86abc8c3b7d9 Make use of GCC's attribute __alloc_size__.
Bruno Haible <bruno@clisp.org>
parents: 12575
diff changeset
46
5166
95d4226b62d3 Remove dependency of xalloc in exitfail, error, gettext.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5105
diff changeset
47 /* 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
48 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
49 or by using gnulib's xalloc-die module. This is the
1953
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
50 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
51 memory allocation failure. */
15412
7f0f3e1ac6fd stdnoreturn, stdnoreturn-tests: remove modules
Paul Eggert <eggert@cs.ucla.edu>
parents: 15408
diff changeset
52 extern _Noreturn void xalloc_die (void);
1953
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
53
13815
86abc8c3b7d9 Make use of GCC's attribute __alloc_size__.
Bruno Haible <bruno@clisp.org>
parents: 12575
diff changeset
54 void *xmalloc (size_t s)
14351
724011432f7b Consistent macro naming for macros that use GCC __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14350
diff changeset
55 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
13815
86abc8c3b7d9 Make use of GCC's attribute __alloc_size__.
Bruno Haible <bruno@clisp.org>
parents: 12575
diff changeset
56 void *xzalloc (size_t s)
14351
724011432f7b Consistent macro naming for macros that use GCC __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14350
diff changeset
57 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
13815
86abc8c3b7d9 Make use of GCC's attribute __alloc_size__.
Bruno Haible <bruno@clisp.org>
parents: 12575
diff changeset
58 void *xcalloc (size_t n, size_t s)
14351
724011432f7b Consistent macro naming for macros that use GCC __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14350
diff changeset
59 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
13815
86abc8c3b7d9 Make use of GCC's attribute __alloc_size__.
Bruno Haible <bruno@clisp.org>
parents: 12575
diff changeset
60 void *xrealloc (void *p, size_t s)
14351
724011432f7b Consistent macro naming for macros that use GCC __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14350
diff changeset
61 _GL_ATTRIBUTE_ALLOC_SIZE ((2));
4817
7c67f04e1c19 Add x2realloc, x2nrealloc. Port to C99 inline.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4783
diff changeset
62 void *x2realloc (void *p, size_t *pn);
13815
86abc8c3b7d9 Make use of GCC's attribute __alloc_size__.
Bruno Haible <bruno@clisp.org>
parents: 12575
diff changeset
63 void *xmemdup (void const *p, size_t s)
14351
724011432f7b Consistent macro naming for macros that use GCC __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14350
diff changeset
64 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((2));
13815
86abc8c3b7d9 Make use of GCC's attribute __alloc_size__.
Bruno Haible <bruno@clisp.org>
parents: 12575
diff changeset
65 char *xstrdup (char const *str)
14351
724011432f7b Consistent macro naming for macros that use GCC __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14350
diff changeset
66 _GL_ATTRIBUTE_MALLOC;
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
67
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
68 /* 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
69 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
70 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
71 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
72
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
73 /* 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
74 /* extern t *XMALLOC (typename t); */
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
75 #define XMALLOC(t) ((t *) xmalloc (sizeof (t)))
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
76
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
77 /* 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
78 /* extern t *XNMALLOC (size_t n, typename t); */
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
79 #define XNMALLOC(n, t) \
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
80 ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t))))
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
81
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
82 /* 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
83 and zero it. */
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
84 /* extern t *XZALLOC (typename t); */
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
85 #define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
86
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
87 /* 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
88 and zero it. */
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
89 /* extern t *XCALLOC (size_t n, typename t); */
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
90 #define XCALLOC(n, t) \
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
91 ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
92
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
93
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
94 /* 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
95 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
96
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
97 XALLOC_INLINE void *xnmalloc (size_t n, size_t s)
14351
724011432f7b Consistent macro naming for macros that use GCC __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14350
diff changeset
98 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
99 XALLOC_INLINE void *
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
100 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
101 {
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
102 if (xalloc_oversized (n, s))
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
103 xalloc_die ();
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
104 return xmalloc (n * s);
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
105 }
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
106
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
107 /* 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
108 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
109
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
110 XALLOC_INLINE void *xnrealloc (void *p, size_t n, size_t s)
14351
724011432f7b Consistent macro naming for macros that use GCC __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14350
diff changeset
111 _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
112 XALLOC_INLINE void *
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
113 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
114 {
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
115 if (xalloc_oversized (n, s))
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
116 xalloc_die ();
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
117 return xrealloc (p, n * s);
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
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
120 /* 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
121 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
122 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
123 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
124 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
125 returned pointer is never null.
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
126
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
127 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
128 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
129 larger block.
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
130
8065
25c6999713ef Give tools a better chance to allocate space for very large buffers.
Jim Meyering <jim@meyering.net>
parents: 7619
diff changeset
131 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
132 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
133 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
134 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
135
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
136 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
137
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
138 int *p = NULL;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
139 size_t used = 0;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
140 size_t allocated = 0;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
141
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
142 void
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
143 append_int (int value)
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
144 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
145 if (used == allocated)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
146 p = x2nrealloc (p, &allocated, sizeof *p);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
147 p[used++] = value;
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
148 }
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
149
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
150 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
151 first time it is called.
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
152
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
153 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
154 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
155 example:
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
156
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
157 int *p = NULL;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
158 size_t used = 0;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
159 size_t allocated = 0;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
160 size_t allocated1 = 1000;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
161
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
162 void
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
163 append_int (int value)
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
164 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
165 if (used == allocated)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
166 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
167 p = x2nrealloc (p, &allocated1, sizeof *p);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
168 allocated = allocated1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
169 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
170 p[used++] = value;
7608
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
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
173 */
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
174
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
175 XALLOC_INLINE void *
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
176 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
177 {
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
178 size_t n = *pn;
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 if (! p)
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 if (! n)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
183 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
184 /* The approximate size to use for initial small allocation
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
185 requests, when the invoking code specifies an old size of
15486
3f9f907b5afe * lib/xalloc.h (DEFAULT_MXFAST): Track 64-bit glibc.
Paul Eggert <eggert@cs.ucla.edu>
parents: 15412
diff changeset
186 zero. This is the largest "small" request for the GNU C
3f9f907b5afe * lib/xalloc.h (DEFAULT_MXFAST): Track 64-bit glibc.
Paul Eggert <eggert@cs.ucla.edu>
parents: 15412
diff changeset
187 library malloc. */
3f9f907b5afe * lib/xalloc.h (DEFAULT_MXFAST): Track 64-bit glibc.
Paul Eggert <eggert@cs.ucla.edu>
parents: 15412
diff changeset
188 enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
189
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
190 n = DEFAULT_MXFAST / s;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
191 n += !n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
192 }
7608
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 else
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
195 {
8077
9cd2d21f40cc * lib/xalloc.h (x2nrealloc): Fix an unlikely bug in the overflow
Paul Eggert <eggert@cs.ucla.edu>
parents: 8065
diff changeset
196 /* Set N = ceil (1.5 * N) so that progress is made if N == 1.
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
197 Check for overflow, so that N * S stays in size_t range.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
198 The check is slightly conservative, but an exact check isn't
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
199 worth the trouble. */
8077
9cd2d21f40cc * lib/xalloc.h (x2nrealloc): Fix an unlikely bug in the overflow
Paul Eggert <eggert@cs.ucla.edu>
parents: 8065
diff changeset
200 if ((size_t) -1 / 3 * 2 / s <= n)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
201 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
202 n += (n + 1) / 2;
7608
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
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
205 *pn = n;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
206 return xrealloc (p, n * s);
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
207 }
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
208
7603
23f14c284219 Simplify xmalloc expressions. Add overflow check in xmalloc arguments.
Bruno Haible <bruno@clisp.org>
parents: 7587
diff changeset
209 /* 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
210 except it returns char *. */
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
211
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
212 XALLOC_INLINE char *xcharalloc (size_t n)
14351
724011432f7b Consistent macro naming for macros that use GCC __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14350
diff changeset
213 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
214 XALLOC_INLINE char *
7603
23f14c284219 Simplify xmalloc expressions. Add overflow check in xmalloc arguments.
Bruno Haible <bruno@clisp.org>
parents: 7587
diff changeset
215 xcharalloc (size_t n)
7587
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
216 {
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
217 return XNMALLOC (n, char);
7587
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
218 }
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
219
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
220 #ifdef __cplusplus
5105
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
221 }
7587
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
222
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
223 /* 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
224 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
225 possible. */
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 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
228 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
229 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
230 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
231 }
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 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
234 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
235 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
236 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
237 }
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
238
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
239 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
240 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
241 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
242 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
243 }
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
244
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
245 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
246 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
247 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
248 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
249 }
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
250
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
251 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
252 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
253 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
254 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
255 }
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
256
17177
902795f52835 xalloc: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
257 #endif
5105
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
258
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
259
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
260 #endif /* !XALLOC_H_ */