From e46d7f6e127b69f926c3a25952d156eb01cc39d2 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 6 Nov 2006 12:47:56 +0000 Subject: New function xnmalloc. --- gnulib-local/lib/xalloc.h | 4 ++++ gnulib-local/lib/xmalloc.c | 20 +++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) (limited to 'gnulib-local/lib') diff --git a/gnulib-local/lib/xalloc.h b/gnulib-local/lib/xalloc.h index a12c984..a044610 100644 --- a/gnulib-local/lib/xalloc.h +++ b/gnulib-local/lib/xalloc.h @@ -32,6 +32,10 @@ extern "C" { /* Allocate SIZE bytes of memory dynamically, with error checking. */ extern void *xmalloc (size_t size); +/* Allocate memory for NMEMB elements of SIZE bytes, with error checking. + SIZE must be > 0. */ +extern void *xnmalloc (size_t nmemb, size_t size); + /* Allocate SIZE bytes of memory dynamically, with error checking, and zero it. */ extern void *xzalloc (size_t size); diff --git a/gnulib-local/lib/xmalloc.c b/gnulib-local/lib/xmalloc.c index 8f06d89..ed9758e 100644 --- a/gnulib-local/lib/xmalloc.c +++ b/gnulib-local/lib/xmalloc.c @@ -48,7 +48,7 @@ fixup_null_alloc (size_t n) { void *p; - p = 0; + p = NULL; if (n == 0) p = malloc ((size_t) 1); if (p == NULL) @@ -69,6 +69,24 @@ xmalloc (size_t n) return p; } +/* Allocate memory for NMEMB elements of SIZE bytes, with error checking. + SIZE must be > 0. */ + +void * +xnmalloc (size_t nmemb, size_t size) +{ + size_t n; + void *p; + + if (xalloc_oversized (nmemb, size)) + xalloc_die (); + n = nmemb * size; + p = malloc (n); + if (p == NULL) + p = fixup_null_alloc (n); + return p; +} + /* Allocate SIZE bytes of memory dynamically, with error checking, and zero it. */ -- cgit v1.1