X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fxmalloc.c;h=687633c250cfeee9eaa1e9227feec92d1281612f;hb=5fa5d3d767b29cf1d757609afbb56459b09743fa;hp=9b7a948c2f18bc342294088ecbe2d2ab94f84ca6;hpb=b0cc20c2269022fd9907014686ac682dff0e5978;p=gnulib.git diff --git a/lib/xmalloc.c b/lib/xmalloc.c index 9b7a948c2..687633c25 100644 --- a/lib/xmalloc.c +++ b/lib/xmalloc.c @@ -1,7 +1,7 @@ /* xmalloc.c -- malloc with out of memory checking Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, - 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc. + 1999, 2000, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -15,9 +15,9 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, - Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#if HAVE_CONFIG_H +#ifdef HAVE_CONFIG_H # include #endif @@ -30,6 +30,15 @@ # define SIZE_MAX ((size_t) -1) #endif +/* 1 if calloc is known to be compatible with GNU calloc. This + matters if we are not also using the calloc module, which defines + HAVE_CALLOC and supports the GNU API even on non-GNU platforms. */ +#if defined HAVE_CALLOC || defined __GLIBC__ +enum { HAVE_GNU_CALLOC = 1 }; +#else +enum { HAVE_GNU_CALLOC = 0 }; +#endif + /* Allocate an array of N objects, each with S bytes of memory, dynamically, with error checking. S must be nonzero. */ @@ -204,18 +213,29 @@ xcalloc (size_t n, size_t s) { void *p; /* Test for overflow, since some calloc implementations don't have - proper overflow checks. */ - if (xalloc_oversized (n, s) || (! (p = calloc (n, s)) && n != 0)) + proper overflow checks. But omit overflow and size-zero tests if + HAVE_GNU_CALLOC, since GNU calloc catches overflow and never + returns NULL if successful. */ + if ((! HAVE_GNU_CALLOC && xalloc_oversized (n, s)) + || (! (p = calloc (n, s)) && (HAVE_GNU_CALLOC || n != 0))) xalloc_die (); return p; } /* Clone an object P of size S, with error checking. There's no need - for xnclone (P, N, S), since xclone (P, N * S) works without any + for xnmemdup (P, N, S), since xmemdup (P, N * S) works without any need for an arithmetic overflow check. */ void * -xclone (void const *p, size_t s) +xmemdup (void const *p, size_t s) { return memcpy (xmalloc (s), p, s); } + +/* Clone STRING. */ + +char * +xstrdup (char const *string) +{ + return xmemdup (string, strlen (string) + 1); +}