From 95dbcc38cba62b96d04dabba37bec4466554fdaa Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Wed, 25 Aug 2010 22:11:10 +0200 Subject: [PATCH] Add x_calloc() --- ccache.h | 1 + util.c | 12 ++++++++++++ 2 files changed, 13 insertions(+) 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 */ -- 2.47.3