diff options
author | Himbeer <himbeer@disroot.org> | 2024-09-17 17:59:48 +0200 |
---|---|---|
committer | Himbeer <himbeer@disroot.org> | 2024-09-17 18:01:10 +0200 |
commit | 1ec909c55c48c21279a080050ec24ec5f91cd3f9 (patch) | |
tree | 3a026a5149471ad59a26b5e29f850541993248ce | |
parent | fa3aa50a43476a7277931adbc3d2fd3c4e7cf671 (diff) |
Add OOM-safe must_calloc and must_realloc helper functions
-rw-r--r-- | include/util.h | 2 | ||||
-rw-r--r-- | src/util.c | 22 |
2 files changed, 24 insertions, 0 deletions
diff --git a/include/util.h b/include/util.h index bdf260e..5a5a819 100644 --- a/include/util.h +++ b/include/util.h @@ -10,5 +10,7 @@ enum exit_status { }; void *must_malloc(size_t size); +void *must_calloc(size_t nmemb, size_t size); +void *must_realloc(void *ptr, size_t size); #endif @@ -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; +} |