X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fxmalloc.c;h=204469f240ab7a67a48f92bdbe3e2ed92aafec61;hb=refs%2Ftags%2FFILEUTILS-3_16x;hp=22b09102184a4efb20004c668c8c1fcf23293c89;hpb=2753afe57a2f829b9bc54d4e0271a3838342aaf1;p=gnulib.git diff --git a/lib/xmalloc.c b/lib/xmalloc.c index 22b091021..204469f24 100644 --- a/lib/xmalloc.c +++ b/lib/xmalloc.c @@ -48,12 +48,18 @@ void free (); /* Prototypes for functions defined here. */ #if defined (__STDC__) && __STDC__ -static void *fixup_null_alloc (size_t n); void *xmalloc (size_t n); void *xcalloc (size_t n, size_t s); void *xrealloc (void *p, size_t n); #endif +#ifndef HAVE_DONE_WORKING_MALLOC_CHECK +you must run the autoconf test for a properly working malloc -- see malloc.m4 +#endif + +#ifndef HAVE_DONE_WORKING_REALLOC_CHECK +you must run the autoconf test for a properly working realloc -- see realloc.m4 +#endif /* Exit value when the requested amount of memory is not available. The caller may set it to some other value. */ @@ -63,7 +69,7 @@ int xalloc_exit_failure = EXIT_FAILURE; char *const xalloc_msg_memory_exhausted = N_("Memory exhausted"); /* FIXME: describe */ -void (*xalloc_fail_func) () = NULL; +void (*xalloc_fail_func) () = 0; #if __STDC__ && (HAVE_VPRINTF || HAVE_DOPRNT) void error (int, int, const char *, ...); @@ -71,22 +77,12 @@ void error (int, int, const char *, ...); void error (); #endif -static void * -fixup_null_alloc (n) - size_t n; +static void +xalloc_fail () { - void *p; - - p = 0; - if (n == 0) - p = malloc ((size_t) 1); - if (p == 0) - { - if (xalloc_fail_func) - (*xalloc_fail_func) (); - error (xalloc_exit_failure, 0, xalloc_msg_memory_exhausted); - } - return p; + if (xalloc_fail_func) + (*xalloc_fail_func) (); + error (xalloc_exit_failure, 0, xalloc_msg_memory_exhausted); } /* Allocate N bytes of memory dynamically, with error checking. */ @@ -99,21 +95,7 @@ xmalloc (n) p = malloc (n); if (p == 0) - p = fixup_null_alloc (n); - return p; -} - -/* Allocate memory for N elements of S bytes, with error checking. */ - -void * -xcalloc (n, s) - size_t n, s; -{ - void *p; - - p = calloc (n, s); - if (p == 0) - p = fixup_null_alloc (n); + xalloc_fail (); return p; } @@ -126,10 +108,26 @@ xrealloc (p, n) void *p; size_t n; { - if (p == 0) - return xmalloc (n); p = realloc (p, n); if (p == 0) - p = fixup_null_alloc (n); + xalloc_fail (); + return p; +} + +#ifdef NOT_USED + +/* Allocate memory for N elements of S bytes, with error checking. */ + +void * +xcalloc (n, s) + size_t n, s; +{ + void *p; + + p = calloc (n, s); + if (p == 0) + xalloc_fail (); return p; } + +#endif /* NOT_USED */