From 3623645815bbdd66bfa9ba46e52afd6c08a7f057 Mon Sep 17 00:00:00 2001 From: terminx Date: Tue, 30 Sep 2014 04:12:27 +0000 Subject: [PATCH] Clean up the "X" variants of malloc and friends to avoid unncessary function calls and parameter passing git-svn-id: https://svn.eduke32.com/eduke32@4619 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/build/include/compat.h | 43 ++++++++++++++++++--- polymer/eduke32/build/src/compat.c | 52 ++++---------------------- 2 files changed, 45 insertions(+), 50 deletions(-) diff --git a/polymer/eduke32/build/include/compat.h b/polymer/eduke32/build/include/compat.h index f2c3ba02b..4e882667c 100644 --- a/polymer/eduke32/build/include/compat.h +++ b/polymer/eduke32/build/include/compat.h @@ -43,7 +43,7 @@ #ifndef __STDC_LIMIT_MACROS #define __STDC_LIMIT_MACROS #endif -#ifdef HAVE_INTTYPES +#if defined(HAVE_INTTYPES) || defined(__cplusplus) # include # include @@ -796,10 +796,43 @@ static inline void append_ext_UNSAFE(char *outbuf, const char *ext) extern void xalloc_set_location(int32_t line, const char *file, const char *func); #endif void set_memerr_handler(void (*handlerfunc)(int32_t, const char *, const char *)); -char *xstrdup(const char *s); -void *xmalloc(bsize_t size); -void *xcalloc(bsize_t nmemb, bsize_t size); -void *xrealloc(void *ptr, bsize_t size); +void handle_memerr(void); + +static inline char *xstrdup(const char *s) +{ + char *ptr = Bstrdup(s); + if (ptr == NULL) handle_memerr(); + return ptr; +} + +static inline void *xmalloc(const bsize_t size) +{ + void *ptr = Bmalloc(size); + if (ptr == NULL) handle_memerr(); + return ptr; +} + +static inline void *xcalloc(const bsize_t nmemb, const bsize_t size) +{ + void *ptr = Bcalloc(nmemb, size); + if (ptr == NULL) handle_memerr(); + return ptr; +} + +static inline void *xrealloc(void * const ptr, const bsize_t size) +{ + void *newptr = Brealloc(ptr, size); + + // According to the C Standard, + // - ptr == NULL makes realloc() behave like malloc() + // - size == 0 make it behave like free() if ptr != NULL + // Since we want to catch an out-of-mem in the first case, this leaves: + if (newptr == NULL && size != 0) + handle_memerr(); + + return newptr; +} + #ifdef EXTERNC } diff --git a/polymer/eduke32/build/src/compat.c b/polymer/eduke32/build/src/compat.c index 4fb8ea754..310ffdc2f 100644 --- a/polymer/eduke32/build/src/compat.c +++ b/polymer/eduke32/build/src/compat.c @@ -64,63 +64,25 @@ void xalloc_set_location(int32_t line, const char *file, const char *func) } #endif -static void handle_potential_memerr(void *ptr) +void handle_memerr(void) { - if (ptr == NULL) + if (g_MemErrHandler) { - if (g_MemErrHandler) - { #ifdef DEBUGGINGAIDS - g_MemErrHandler(g_MemErrLine, g_MemErrFile, g_MemErrFunc); + g_MemErrHandler(g_MemErrLine, g_MemErrFile, g_MemErrFunc); #else - g_MemErrHandler(0, "???", "???"); + g_MemErrHandler(0, "???", "???"); #endif - } - - Bexit(EXIT_FAILURE); } + + Bexit(EXIT_FAILURE); } -void set_memerr_handler(void (*handlerfunc)(int32_t, const char *, const char *)) +void set_memerr_handler(void(*handlerfunc)(int32_t, const char *, const char *)) { g_MemErrHandler = handlerfunc; } -char *xstrdup(const char *s) -{ - char *ptr = Bstrdup(s); - handle_potential_memerr(ptr); - return ptr; -} - -void *xmalloc(bsize_t size) -{ - void *ptr = Bmalloc(size); - handle_potential_memerr(ptr); - return ptr; -} - -void *xcalloc(bsize_t nmemb, bsize_t size) -{ - void *ptr = Bcalloc(nmemb, size); - handle_potential_memerr(ptr); - return ptr; -} - -void *xrealloc(void *ptr, bsize_t size) -{ - void *newptr = Brealloc(ptr, size); - - // According to the C Standard, - // - ptr == NULL makes realloc() behave like malloc() - // - size == 0 make it behave like free() if ptr != NULL - // Since we want to catch an out-of-mem in the first case, this leaves: - if (size != 0) - handle_potential_memerr(newptr); - - return newptr; -} - ////////// #ifndef __compat_h_macrodef__