annotate lib/xalloc.h @ 4830:410ea3e253b9

Revamp xalloc_oversized so that its count arg need not fit into size_t. Fix up group-member, quotearg accordingly.
author Paul Eggert <eggert@cs.ucla.edu>
date Thu, 30 Oct 2003 06:33:39 +0000
parents 167a92276385
children 2759ea168f25
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,
c6450308f123 Assume C89, so PARAMS isn't needed.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4347
diff changeset
4 1999, 2000, 2003 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>
df44e79ce676 .h files should stand alone, but we shouldn't include <sys/types.h>
Paul Eggert <eggert@cs.ucla.edu>
parents: 2951
diff changeset
24
1958
3bf5a026bc95 (__attribute__): Protect against redefinition.
Jim Meyering <jim@meyering.net>
parents: 1955
diff changeset
25 # ifndef __attribute__
1962
1f3b3ce10ab6 (__attribute__): Define to empty if GCC claims to
Jim Meyering <jim@meyering.net>
parents: 1958
diff changeset
26 # 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
27 # define __attribute__(x)
3bf5a026bc95 (__attribute__): Protect against redefinition.
Jim Meyering <jim@meyering.net>
parents: 1955
diff changeset
28 # endif
1953
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
29 # endif
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
30
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
31 # ifndef ATTRIBUTE_NORETURN
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
32 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
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
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
35 /* 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
36 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
37 extern void (*xalloc_fail_func) (void);
1070
Jim Meyering <jim@meyering.net>
parents:
diff changeset
38
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
39 /* 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
40 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
41 Its value is "memory exhausted". */
a3217eb32838 (xalloc_msg_memory_exhausted): Now char const[],
Jim Meyering <jim@meyering.net>
parents: 2807
diff changeset
42 extern char const xalloc_msg_memory_exhausted[];
1070
Jim Meyering <jim@meyering.net>
parents:
diff changeset
43
1953
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
44 /* 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
45 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
46 exit_failure (defined in exitfail.h). This is the
1953
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
47 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
48 memory allocation failure. */
4397
c6450308f123 Assume C89, so PARAMS isn't needed.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4347
diff changeset
49 extern void xalloc_die (void) ATTRIBUTE_NORETURN;
1953
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
50
4783
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
51 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
52 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
53 void *xzalloc (size_t s);
4397
c6450308f123 Assume C89, so PARAMS isn't needed.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4347
diff changeset
54 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
55 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
56 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
57 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
58 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
59 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
60 char *xstrdup (const char *str);
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
61
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
62 /* 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
63 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
64 nonnegative. This is a macro, not an inline function, so that it
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
65 works correctly even when SIZE_MAX < N. */
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
66 #define xalloc_oversized(n, s) ((size_t) -1 / (s) < (n))
4818
167a92276385 New function xalloc_oversized.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4817
diff changeset
67
4783
d1dc5d9bf1ba Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4473
diff changeset
68 /* 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
69 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
70 # 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
71 # 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
72 # 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
73 # 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
74 # 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
75 # 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
76 # define XFREE(p) free (p)
1953
134bb4a01712 (__attribute__): Define.
Jim Meyering <jim@meyering.net>
parents: 1943
diff changeset
77
1603
394e749157f3 Add comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
78 #endif /* !XALLOC_H_ */