From 1ec909c55c48c21279a080050ec24ec5f91cd3f9 Mon Sep 17 00:00:00 2001 From: Himbeer Date: Tue, 17 Sep 2024 17:59:48 +0200 Subject: Add OOM-safe must_calloc and must_realloc helper functions --- src/util.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src') diff --git a/src/util.c b/src/util.c index 9b79ac7..5918968 100644 --- a/src/util.c +++ b/src/util.c @@ -11,3 +11,25 @@ must_malloc(size_t size) exit(EXIT_ABNORMAL); } } + +void * +must_calloc(size_t nmemb, size_t size) +{ + void *ret = calloc(nmemb, size); + if (ret == NULL) { + fprintf(stderr, "out of memory\n"); + exit(EXIT_ABNORMAL); + } + return ret; +} + +void * +must_realloc(void *ptr, size_t size) +{ + void *ret = realloc(ptr, size); + if (ret == NULL) { + fprintf(stderr, "out of memory\n"); + exit(EXIT_ABNORMAL); + } + return ret; +} -- cgit v1.2.3