From 9c0d86401233d463835aa4a1e33d3a4a0f084933 Mon Sep 17 00:00:00 2001 From: Steam Deck User Date: Thu, 2 Feb 2023 12:21:24 -0500 Subject: [PATCH] Restore Q_* alloc features --- source/zone.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/source/zone.c b/source/zone.c index c64e1ca..4e3713b 100644 --- a/source/zone.c +++ b/source/zone.c @@ -382,6 +382,68 @@ bytecopy: return (dst); } +/* +=================== +Q_malloc +Use it instead of malloc so that if memory allocation fails, +the program exits with a message saying there's not enough memory +instead of crashing after trying to use a NULL pointer +=================== +*/ +void *Q_malloc (size_t size) +{ + void *p; + + if (!(p = malloc(size))) + Sys_Error ("Not enough memory free; check disk space"); + + return p; +} + +/* +=================== +Q_calloc +=================== +*/ +void *Q_calloc (size_t n, size_t size) +{ + void *p; + + if (!(p = calloc(n, size))) + Sys_Error ("Not enough memory free; check disk space"); + + return p; +} + +/* +=================== +Q_realloc +=================== +*/ +void *Q_realloc (void *ptr, size_t size) +{ + void *p; + + if (!(p = realloc(ptr, size))) + Sys_Error ("Not enough memory free; check disk space"); + + return p; +} + +/* +=================== +Q_strdup +=================== +*/ +void *Q_strdup (const char *str) +{ + char *p; + + if (!(p = strdup(str))) + Sys_Error ("Not enough memory free; check disk space"); + + return p; +} /* ==============================================================================