From: Joel Rosdahl Date: Wed, 25 Aug 2010 20:11:10 +0000 (+0200) Subject: Add x_calloc() X-Git-Tag: v3.1~54 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=95dbcc38cba62b96d04dabba37bec4466554fdaa;p=thirdparty%2Fccache.git Add x_calloc() --- diff --git a/ccache.h b/ccache.h index 58f2cd474..a822d1a7c 100644 --- a/ccache.h +++ b/ccache.h @@ -125,6 +125,7 @@ char *x_strdup(const char *s); char *x_strndup(const char *s, size_t n); void *x_realloc(void *ptr, size_t size); void *x_malloc(size_t size); +void *x_calloc(size_t nmemb, size_t size); void traverse(const char *dir, void (*fn)(const char *, struct stat *)); char *basename(const char *s); char *dirname(char *s); diff --git a/util.c b/util.c index 2779971d0..d11331bc2 100644 --- a/util.c +++ b/util.c @@ -546,6 +546,18 @@ x_malloc(size_t size) return ret; } +/* This is like calloc() but dies if the allocation fails. */ +void * +x_calloc(size_t nmemb, size_t size) +{ + void *ret; + ret = calloc(nmemb, size); + if (!ret) { + fatal("x_calloc: Could not allocate %lu bytes", (unsigned long)size); + } + return ret; +} + /* this is like realloc() but dies if the malloc fails */