annotate lib/xalloc.h @ 5105:d088e155d8f6

Add extern "C" for C++.
author Bruno Haible <bruno@clisp.org>
date Fri, 16 Jul 2004 16:28:48 +0000 (2004-07-16)
parents 8690f8e0e0bf
children 95d4226b62d3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
1 /* xalloc.h -- malloc with out-of-memory checking
4397
c6450308f123 Assume C89, so PARAMS isn't needed.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4347
diff changeset
2
c6450308f123 Assume C89, so PARAMS isn't needed.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4347
diff changeset
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
5105
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
4 1999, 2000, 2003, 2004 Free Software Foundation, Inc.
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
5
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
6 This program is free software; you can redistribute it and/or modify
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
7 it under the terms of the GNU General Public License as published by
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
8 the Free Software Foundation; either version 2, or (at your option)
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
9 any later version.
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
10
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
11 This program is distributed in the hope that it will be useful,
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
14 GNU General Public License for more details.
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
15
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
16 You should have received a copy of the GNU General Public License
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
17 along with this program; if not, write to the Free Software Foundation,
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
19
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
20 #ifndef XALLOC_H_
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
21 # define XALLOC_H_
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
22
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
23 # 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
24
5105
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
25
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
26 # ifdef __cplusplus
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
27 extern "C" {
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
28 # endif
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
29
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
30
1958
3bf5a026bc95 (__attribute__): Protect against redefinition.
Jim Meyering <jim@meyering.net>
parents: 1955
diff changeset
31 # ifndef __attribute__
1962
1f3b3ce10ab6 (__attribute__): Define to empty if GCC claims to
Jim Meyering <jim@meyering.net>
parents: 1958
diff changeset
32 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
1958
3bf5a026bc95 (__attribute__): Protect against redefinition.
Jim Meyering <jim@meyering.net>
parents: 1955
diff changeset
33 # define __attribute__(x)
3bf5a026bc95 (__attribute__): Protect against redefinition.
Jim Meyering <jim@meyering.net>
parents: 1955
diff changeset
34 # endif
1953
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
35 # endif
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
36
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
37 # ifndef ATTRIBUTE_NORETURN
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
38 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
39 # endif
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
40
5105
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
41
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
42 /* If this pointer is non-zero, run the specified function upon each
1953
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
43 allocation failure. It is initialized to zero. */
4397
c6450308f123 Assume C89, so PARAMS isn't needed.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4347
diff changeset
44 extern void (*xalloc_fail_func) (void);
1070
Jim Meyering <jim@meyering.net>
parents:
diff changeset
45
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
46 /* If XALLOC_FAIL_FUNC is undefined or a function that returns, this
2811
a3217eb32838 (xalloc_msg_memory_exhausted): Now char const[],
Jim Meyering <jim@meyering.net>
parents: 2807
diff changeset
47 message is output. It is translated via gettext.
a3217eb32838 (xalloc_msg_memory_exhausted): Now char const[],
Jim Meyering <jim@meyering.net>
parents: 2807
diff changeset
48 Its value is "memory exhausted". */
a3217eb32838 (xalloc_msg_memory_exhausted): Now char const[],
Jim Meyering <jim@meyering.net>
parents: 2807
diff changeset
49 extern char const xalloc_msg_memory_exhausted[];
1070
Jim Meyering <jim@meyering.net>
parents:
diff changeset
50
1953
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
51 /* This function is always triggered when memory is exhausted. It is
4473
fd6696a97000 Sync with coreutils xalloc.h, xmalloc.c, xmemcoll.h, xmemcoll.c.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4397
diff changeset
52 in charge of honoring the two previous items. It exits with status
fd6696a97000 Sync with coreutils xalloc.h, xmalloc.c, xmemcoll.h, xmemcoll.c.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4397
diff changeset
53 exit_failure (defined in exitfail.h). This is the
1953
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
54 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
55 memory allocation failure. */
4397
c6450308f123 Assume C89, so PARAMS isn't needed.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4347
diff changeset
56 extern void xalloc_die (void) ATTRIBUTE_NORETURN;
1953
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
57
4783
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
58 void *xmalloc (size_t s);
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
59 void *xnmalloc (size_t n, size_t s);
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
60 void *xzalloc (size_t s);
4397
c6450308f123 Assume C89, so PARAMS isn't needed.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4347
diff changeset
61 void *xcalloc (size_t n, size_t s);
4783
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
62 void *xrealloc (void *p, size_t s);
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
63 void *xnrealloc (void *p, size_t n, size_t s);
4817
7c67f04e1c19 Add x2realloc, x2nrealloc. Port to C99 inline.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4783
diff changeset
64 void *x2realloc (void *p, size_t *pn);
7c67f04e1c19 Add x2realloc, x2nrealloc. Port to C99 inline.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4783
diff changeset
65 void *x2nrealloc (void *p, size_t *pn, size_t s);
4783
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
66 void *xclone (void const *p, size_t s);
4397
c6450308f123 Assume C89, so PARAMS isn't needed.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4347
diff changeset
67 char *xstrdup (const char *str);
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
68
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
69 /* 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
70 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
71 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
72 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
73
2759ea168f25 Reject allocations of exactly SIZE_MAX bytes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4830
diff changeset
74 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
75 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
76 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
77 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
78 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
79 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
80 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
81 # 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
82 ((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
83
4783
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
84 /* These macros are deprecated; they will go away soon, and are retained
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
85 temporarily only to ease conversion to the functions described above. */
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
86 # define CCLONE(p, n) xclone (p, (n) * sizeof *(p))
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
87 # define CLONE(p) xclone (p, sizeof *(p))
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
88 # define NEW(type, var) type *var = xmalloc (sizeof (type))
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
89 # define XCALLOC(type, n) xcalloc (n, sizeof (type))
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
90 # define XMALLOC(type, n) xnmalloc (n, sizeof (type))
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
91 # define XREALLOC(p, type, n) xnrealloc (p, n, sizeof (type))
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
92 # define XFREE(p) free (p)
1953
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
93
5105
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
94
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
95 # ifdef __cplusplus
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
96 }
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
97 # endif
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
98
d088e155d8f6 Add extern "C" for C++.
Bruno Haible <bruno@clisp.org>
parents: 4865
diff changeset
99
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
100 #endif /* !XALLOC_H_ */