1 |
/* xmalloc.c -- malloc with out of memory checking |
/* xmalloc.c -- malloc with out of memory checking |
2 |
|
|
3 |
Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
4 |
1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc. |
1999, 2000, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. |
5 |
|
|
6 |
This program is free software; you can redistribute it and/or modify |
This program is free software; you can redistribute it and/or modify |
7 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
30 |
# define SIZE_MAX ((size_t) -1) |
# define SIZE_MAX ((size_t) -1) |
31 |
#endif |
#endif |
32 |
|
|
33 |
|
/* 1 if calloc is known to be compatible with GNU calloc. This |
34 |
|
matters if we are not also using the calloc module, which defines |
35 |
|
HAVE_CALLOC and supports the GNU API even on non-GNU platforms. */ |
36 |
|
#if defined HAVE_CALLOC || defined __GLIBC__ |
37 |
|
enum { HAVE_GNU_CALLOC = 1 }; |
38 |
|
#else |
39 |
|
enum { HAVE_GNU_CALLOC = 0 }; |
40 |
|
#endif |
41 |
|
|
42 |
/* Allocate an array of N objects, each with S bytes of memory, |
/* Allocate an array of N objects, each with S bytes of memory, |
43 |
dynamically, with error checking. S must be nonzero. */ |
dynamically, with error checking. S must be nonzero. */ |
44 |
|
|
213 |
{ |
{ |
214 |
void *p; |
void *p; |
215 |
/* Test for overflow, since some calloc implementations don't have |
/* Test for overflow, since some calloc implementations don't have |
216 |
proper overflow checks. */ |
proper overflow checks. But omit overflow and size-zero tests if |
217 |
if (xalloc_oversized (n, s) || (! (p = calloc (n, s)) && n != 0)) |
HAVE_GNU_CALLOC, since GNU calloc catches overflow and never |
218 |
|
returns NULL if successful. */ |
219 |
|
if ((! HAVE_GNU_CALLOC && xalloc_oversized (n, s)) |
220 |
|
|| (! (p = calloc (n, s)) && (HAVE_GNU_CALLOC || n != 0))) |
221 |
xalloc_die (); |
xalloc_die (); |
222 |
return p; |
return p; |
223 |
} |
} |