annotate lib/xalloc.h @ 14351:724011432f7b

Consistent macro naming for macros that use GCC __attribute__. * lib/di-set.h (_GL_ATTRIBUTE_NONNULL): Renamed from _ATTRIBUTE_NONNULL_. * lib/ino-map.h (_GL_ATTRIBUTE_NONNULL): Likewise. * lib/hash.h (_GL_ATTRIBUTE_WUR): Renamed from ATTRIBUTE_WUR. * lib/ignore-value.h (_GL_ATTRIBUTE_DEPRECATED): Renamed from ATTRIBUTE_DEPRECATED. * lib/openat.h (_GL_ATTRIBUTE_NORETURN): Renamed from ATTRIBUTE_NORETURN. * lib/sigpipe-die.h (_GL_ATTRIBUTE_NORETURN): Likewise. * lib/xmemdup0.h (_GL_ATTRIBUTE_NORETURN): Likewise. * lib/xstrtol.h (_GL_ATTRIBUTE_NORETURN): Likewise. * lib/xalloc.h (_GL_ATTRIBUTE_NORETURN): Likewise. (_GL_ATTRIBUTE_MALLOC): Renamed from ATTRIBUTE_MALLOC. (_GL_ATTRIBUTE_ALLOC_SIZE): Renamed from ATTRIBUTE_ALLOC_SIZE. * lib/version-etc.h (_GL_ATTRIBUTE_SENTINEL): Renamed from ATTRIBUTE_SENTINEL. * lib/safe-alloc.h (_GL_ATTRIBUTE_RETURN_CHECK): Renamed from ATTRIBUTE_RETURN_CHECK. * tests/test-ignore-value.c (_GL_ATTRIBUTE_RETURN_CHECK): Likewise. * tests/test-argmatch.c (_GL_ATTRIBUTE_NORETURN): Renamed from ATTRIBUTE_NORETURN. * tests/test-exclude.c (_GL_ATTRIBUTE_NORETURN): Likewise. Reported by Paul Eggert.
author Bruno Haible <bruno@clisp.org>
date Sun, 13 Feb 2011 23:44:28 +0100
parents 4cf3b58aaf12
children b3b2f9e830e9
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
14079
97fc9a21a8fb maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents: 13815
diff changeset
3 Copyright (C) 1990-2000, 2003-2004, 2006-2011 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
5105
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
23
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
24 # ifdef __cplusplus
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
25 extern "C" {
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
26 # endif
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
27
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
28
14350
4cf3b58aaf12 Don't interfere with a program's definition of __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
29 # if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
14351
724011432f7b Consistent macro naming for macros that use GCC __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14350
diff changeset
30 # define _GL_ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
14350
4cf3b58aaf12 Don't interfere with a program's definition of __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
31 # else
14351
724011432f7b Consistent macro naming for macros that use GCC __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14350
diff changeset
32 # define _GL_ATTRIBUTE_NORETURN /* empty */
1953
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
33 # endif
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
34
14350
4cf3b58aaf12 Don't interfere with a program's definition of __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
35 # if __GNUC__ >= 3
14351
724011432f7b Consistent macro naming for macros that use GCC __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14350
diff changeset
36 # 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
37 # else
14351
724011432f7b Consistent macro naming for macros that use GCC __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14350
diff changeset
38 # define _GL_ATTRIBUTE_MALLOC
10074
cf0a8e173c39 Help GCC to do better code generation.
Bruno Haible <bruno@clisp.org>
parents: 10068
diff changeset
39 # endif
cf0a8e173c39 Help GCC to do better code generation.
Bruno Haible <bruno@clisp.org>
parents: 10068
diff changeset
40
14350
4cf3b58aaf12 Don't interfere with a program's definition of __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
41 # 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
42 # 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
43 # else
14351
724011432f7b Consistent macro naming for macros that use GCC __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14350
diff changeset
44 # 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
45 # endif
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. */
14351
724011432f7b Consistent macro naming for macros that use GCC __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14350
diff changeset
52 extern void xalloc_die (void) _GL_ATTRIBUTE_NORETURN;
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
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
68 /* 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
69 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
70 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
71 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
72
2759ea168f25 Reject allocations of exactly SIZE_MAX bytes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4830
diff changeset
73 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
74 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
75 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
76 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
77 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
78 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
79 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
80 # 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
81 ((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
82
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
83
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
84 /* 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
85 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
86 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
87 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
88
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
89 /* 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
90 /* extern t *XMALLOC (typename t); */
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
91 # 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
92
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
93 /* 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
94 /* 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
95 # define XNMALLOC(n, t) \
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
96 ((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
97
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
98 /* 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
99 and zero it. */
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
100 /* extern t *XZALLOC (typename t); */
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
101 # 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
102
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
103 /* 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
104 and zero it. */
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
105 /* 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
106 # define XCALLOC(n, t) \
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
107 ((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
108
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
109
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
110 # if HAVE_INLINE
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
111 # define static_inline static inline
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
112 # else
13815
86abc8c3b7d9 Make use of GCC's attribute __alloc_size__.
Bruno Haible <bruno@clisp.org>
parents: 12575
diff changeset
113 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
114 _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
115 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
116 _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
12575
7b84f51e4373 xalloc.h: use consistent formatting
Jim Meyering <meyering@redhat.com>
parents: 12559
diff changeset
117 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
118 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
119 _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
120 # endif
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
121
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
122 # ifdef static_inline
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
123
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
124 /* 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
125 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
126
13815
86abc8c3b7d9 Make use of GCC's attribute __alloc_size__.
Bruno Haible <bruno@clisp.org>
parents: 12575
diff changeset
127 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
128 _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
129 static_inline void *
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
130 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
131 {
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
132 if (xalloc_oversized (n, s))
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
133 xalloc_die ();
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
134 return xmalloc (n * s);
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
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
137 /* 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
138 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
139
13815
86abc8c3b7d9 Make use of GCC's attribute __alloc_size__.
Bruno Haible <bruno@clisp.org>
parents: 12575
diff changeset
140 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
141 _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
142 static_inline void *
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
143 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
144 {
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
145 if (xalloc_oversized (n, s))
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
146 xalloc_die ();
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
147 return xrealloc (p, n * s);
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 /* 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
151 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
152 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
153 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
154 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
155 returned pointer is never null.
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 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
158 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
159 larger block.
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
160
8065
25c6999713ef Give tools a better chance to allocate space for very large buffers.
Jim Meyering <jim@meyering.net>
parents: 7619
diff changeset
161 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
162 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
163 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
164 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
165
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
166 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
167
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
168 int *p = NULL;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
169 size_t used = 0;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
170 size_t allocated = 0;
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 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
175 if (used == allocated)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
176 p = x2nrealloc (p, &allocated, sizeof *p);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
177 p[used++] = value;
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
178 }
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 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
181 first time it is called.
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 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
184 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
185 example:
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 int *p = NULL;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
188 size_t used = 0;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
189 size_t allocated = 0;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
190 size_t allocated1 = 1000;
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 void
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
193 append_int (int value)
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
194 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
195 if (used == allocated)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
196 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
197 p = x2nrealloc (p, &allocated1, sizeof *p);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
198 allocated = allocated1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
199 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
200 p[used++] = value;
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
201 }
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
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
205 static_inline void *
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
206 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
207 {
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
208 size_t n = *pn;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
209
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
210 if (! p)
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
211 {
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
212 if (! n)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
213 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
214 /* 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
215 requests, when the invoking code specifies an old size of
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
216 zero. 64 bytes is the largest "small" request for the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
217 GNU C library malloc. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
218 enum { DEFAULT_MXFAST = 64 };
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
219
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
220 n = DEFAULT_MXFAST / s;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
221 n += !n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10103
diff changeset
222 }
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
223 }
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
224 else
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
225 {
8077
9cd2d21f40cc * lib/xalloc.h (x2nrealloc): Fix an unlikely bug in the overflow
Paul Eggert <eggert@cs.ucla.edu>
parents: 8065
diff changeset
226 /* 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
227 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
228 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
229 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
230 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
231 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
232 n += (n + 1) / 2;
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
233 }
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
234
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
235 *pn = n;
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
236 return xrealloc (p, n * s);
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
237 }
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
238
7603
23f14c284219 Simplify xmalloc expressions. Add overflow check in xmalloc arguments.
Bruno Haible <bruno@clisp.org>
parents: 7587
diff changeset
239 /* 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
240 except it returns char *. */
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
241
13815
86abc8c3b7d9 Make use of GCC's attribute __alloc_size__.
Bruno Haible <bruno@clisp.org>
parents: 12575
diff changeset
242 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
243 _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
244 static_inline char *
7603
23f14c284219 Simplify xmalloc expressions. Add overflow check in xmalloc arguments.
Bruno Haible <bruno@clisp.org>
parents: 7587
diff changeset
245 xcharalloc (size_t n)
7587
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
246 {
7608
060ba558f95d * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents: 7603
diff changeset
247 return XNMALLOC (n, char);
7587
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
7619
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
250 # endif
e0804993a043 * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
Paul Eggert <eggert@cs.ucla.edu>
parents: 7608
diff changeset
251
5105
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
252 # ifdef __cplusplus
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
253 }
7587
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 /* 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
256 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
257 possible. */
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 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
260 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
261 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
262 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
263 }
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 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
266 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
267 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
268 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
269 }
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
270
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
271 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
272 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
273 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
274 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
275 }
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
276
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
277 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
278 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
279 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
280 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
281 }
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
282
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
283 template <typename T> inline T *
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
284 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
285 {
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
286 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
287 }
1ce8d03ae034 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
288
5105
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
289 # endif
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
290
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
291
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
292 #endif /* !XALLOC_H_ */