annotate lib/xalloc.h @ 16201:8250f2777afc

maint: update all copyright year number ranges Run "make update-copyright".
author Jim Meyering <meyering@redhat.com>
date Sun, 01 Jan 2012 10:04:58 +0100
parents 3f9f907b5afe
children 902795f52835
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_
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
19 # define XALLOC_H_
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
20
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
21 # 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
22
14643
b3b2f9e830e9 xalloc-oversized: new module
Eric Blake <eblake@redhat.com>
parents: 14351
diff changeset
23 # include "xalloc-oversized.h"
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
14350
4cf3b58aaf12 Don't interfere with a program's definition of __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
30 # if __GNUC__ >= 3
14351
724011432f7b Consistent macro naming for macros that use GCC __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14350
diff changeset
31 # define _GL_ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
14350
4cf3b58aaf12 Don't interfere with a program's definition of __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
32 # else
14351
724011432f7b Consistent macro naming for macros that use GCC __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14350
diff changeset
33 # define _GL_ATTRIBUTE_MALLOC
10074
cf0a8e173c39 Help GCC to do better code generation.
Bruno Haible <bruno@clisp.org>
parents: 10068
diff changeset
34 # endif
cf0a8e173c39 Help GCC to do better code generation.
Bruno Haible <bruno@clisp.org>
parents: 10068
diff changeset
35
14350
4cf3b58aaf12 Don't interfere with a program's definition of __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
36 # if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
14351
724011432f7b Consistent macro naming for macros that use GCC __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14350
diff changeset
37 # define _GL_ATTRIBUTE_ALLOC_SIZE(args) __attribute__ ((__alloc_size__ args))
14350
4cf3b58aaf12 Don't interfere with a program's definition of __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
38 # else
14351
724011432f7b Consistent macro naming for macros that use GCC __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14350
diff changeset
39 # define _GL_ATTRIBUTE_ALLOC_SIZE(args)
13815
86abc8c3b7d9 Make use of GCC's attribute __alloc_size__.
Bruno Haible <bruno@clisp.org>
parents: 12575
diff changeset
40 # endif
86abc8c3b7d9 Make use of GCC's attribute __alloc_size__.
Bruno Haible <bruno@clisp.org>
parents: 12575
diff changeset
41
5166
95d4226b62d3 Remove dependency of xalloc in exitfail, error, gettext.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5105
diff changeset
42 /* 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
43 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
44 or by using gnulib's xalloc-die module. This is the
1953
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
45 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
46 memory allocation failure. */
15412
7f0f3e1ac6fd stdnoreturn, stdnoreturn-tests: remove modules
Paul Eggert <eggert@cs.ucla.edu>
parents: 15408
diff changeset
47 extern _Noreturn void xalloc_die (void);
1953
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
48
13815
86abc8c3b7d9 Make use of GCC's attribute __alloc_size__.
Bruno Haible <bruno@clisp.org>
parents: 12575
diff changeset
49 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
50 _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
51 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
52 _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
53 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
54 _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
55 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
56 _GL_ATTRIBUTE_ALLOC_SIZE ((2));
4817
7c67f04e1c19 Add x2realloc, x2nrealloc. Port to C99 inline.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4783
diff changeset
57 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
58 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
59 _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
60 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
61 _GL_ATTRIBUTE_MALLOC;
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
62
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
63 /* 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
64 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
65 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
66 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
67
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
68 /* 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
69 /* extern t *XMALLOC (typename t); */
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
70 # 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
71
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
72 /* 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
73 /* 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
74 # define XNMALLOC(n, t) \
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
75 ((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
76
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
77 /* 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
78 and zero it. */
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
79 /* extern t *XZALLOC (typename t); */
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
80 # 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
81
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
82 /* 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
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 *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
85 # define XCALLOC(n, t) \
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
86 ((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
87
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
88
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
89 # if HAVE_INLINE
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
90 # define static_inline static inline
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
91 # else
13815
86abc8c3b7d9 Make use of GCC's attribute __alloc_size__.
Bruno Haible <bruno@clisp.org>
parents: 12575
diff changeset
92 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
93 _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
94 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
95 _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
12575
7b84f51e4373 xalloc.h: use consistent formatting
Jim Meyering <meyering@redhat.com>
parents: 12559
diff changeset
96 void *x2nrealloc (void *p, size_t *pn, size_t s);
13815
86abc8c3b7d9 Make use of GCC's attribute __alloc_size__.
Bruno Haible <bruno@clisp.org>
parents: 12575
diff changeset
97 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
98 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
99 # endif
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
100
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
101 # ifdef static_inline
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
102
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
103 /* 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
104 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
105
13815
86abc8c3b7d9 Make use of GCC's attribute __alloc_size__.
Bruno Haible <bruno@clisp.org>
parents: 12575
diff changeset
106 static_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
107 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
108 static_inline void *
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
109 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
110 {
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
111 if (xalloc_oversized (n, s))
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
112 xalloc_die ();
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
113 return xmalloc (n * 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
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
116 /* 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
117 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
118
13815
86abc8c3b7d9 Make use of GCC's attribute __alloc_size__.
Bruno Haible <bruno@clisp.org>
parents: 12575
diff changeset
119 static_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
120 _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
121 static_inline void *
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
122 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
123 {
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
124 if (xalloc_oversized (n, s))
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
125 xalloc_die ();
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
126 return xrealloc (p, n * s);
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
127 }
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 /* 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
130 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
131 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
132 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
133 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
134 returned pointer is never null.
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 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
137 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
138 larger block.
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
139
8065
25c6999713ef Give tools a better chance to allocate space for very large buffers.
Jim Meyering <jim@meyering.net>
parents: 7619
diff changeset
140 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
141 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
142 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
143 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
144
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
145 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
146
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
147 int *p = NULL;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
148 size_t used = 0;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
149 size_t allocated = 0;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
150
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
151 void
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
152 append_int (int value)
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
153 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
154 if (used == allocated)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
155 p = x2nrealloc (p, &allocated, sizeof *p);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
156 p[used++] = value;
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
157 }
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 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
160 first time it is called.
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 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
163 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
164 example:
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
165
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
166 int *p = NULL;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
167 size_t used = 0;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
168 size_t allocated = 0;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
169 size_t allocated1 = 1000;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
170
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
171 void
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
172 append_int (int value)
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
173 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
174 if (used == allocated)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
175 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
176 p = x2nrealloc (p, &allocated1, sizeof *p);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
177 allocated = allocated1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
178 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
179 p[used++] = value;
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
180 }
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
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
184 static_inline void *
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
185 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
186 {
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
187 size_t n = *pn;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
188
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
189 if (! p)
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
190 {
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
191 if (! n)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
192 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
193 /* 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
194 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
195 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
196 library malloc. */
3f9f907b5afe * lib/xalloc.h (DEFAULT_MXFAST): Track 64-bit glibc.
Paul Eggert <eggert@cs.ucla.edu>
parents: 15412
diff changeset
197 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
198
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
199 n = DEFAULT_MXFAST / s;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
200 n += !n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
201 }
7608
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 else
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
204 {
8077
9cd2d21f40cc * lib/xalloc.h (x2nrealloc): Fix an unlikely bug in the overflow
Paul Eggert <eggert@cs.ucla.edu>
parents: 8065
diff changeset
205 /* 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
206 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
207 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
208 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
209 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
210 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
211 n += (n + 1) / 2;
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
212 }
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 *pn = n;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
215 return xrealloc (p, n * s);
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
216 }
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
217
7603
23f14c284219 Simplify xmalloc expressions. Add overflow check in xmalloc arguments.
Bruno Haible <bruno@clisp.org>
parents: 7587
diff changeset
218 /* 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
219 except it returns char *. */
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
220
13815
86abc8c3b7d9 Make use of GCC's attribute __alloc_size__.
Bruno Haible <bruno@clisp.org>
parents: 12575
diff changeset
221 static_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
222 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
223 static_inline char *
7603
23f14c284219 Simplify xmalloc expressions. Add overflow check in xmalloc arguments.
Bruno Haible <bruno@clisp.org>
parents: 7587
diff changeset
224 xcharalloc (size_t n)
7587
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
225 {
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
226 return XNMALLOC (n, char);
7587
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
227 }
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
228
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
229 # endif
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
230
5105
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
231 # ifdef __cplusplus
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
232 }
7587
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
233
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
234 /* 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
235 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
236 possible. */
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 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
239 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
240 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
241 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
242 }
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 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
245 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
246 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
247 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
248 }
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 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
251 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
252 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
253 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
254 }
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 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
257 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
258 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
259 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
260 }
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
261
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
262 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
263 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
264 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
265 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
266 }
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
267
5105
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
268 # endif
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
269
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
270
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
271 #endif /* !XALLOC_H_ */