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
This commit is contained in:
terminx 2014-09-30 04:12:27 +00:00
parent 53ed4c619c
commit 3623645815
2 changed files with 45 additions and 50 deletions

View file

@ -43,7 +43,7 @@
#ifndef __STDC_LIMIT_MACROS #ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS #define __STDC_LIMIT_MACROS
#endif #endif
#ifdef HAVE_INTTYPES #if defined(HAVE_INTTYPES) || defined(__cplusplus)
# include <stdint.h> # include <stdint.h>
# include <inttypes.h> # include <inttypes.h>
@ -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); extern void xalloc_set_location(int32_t line, const char *file, const char *func);
#endif #endif
void set_memerr_handler(void (*handlerfunc)(int32_t, const char *, const char *)); void set_memerr_handler(void (*handlerfunc)(int32_t, const char *, const char *));
char *xstrdup(const char *s); void handle_memerr(void);
void *xmalloc(bsize_t size);
void *xcalloc(bsize_t nmemb, bsize_t size); static inline char *xstrdup(const char *s)
void *xrealloc(void *ptr, bsize_t size); {
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 #ifdef EXTERNC
} }

View file

@ -64,63 +64,25 @@ void xalloc_set_location(int32_t line, const char *file, const char *func)
} }
#endif #endif
static void handle_potential_memerr(void *ptr) void handle_memerr(void)
{ {
if (ptr == NULL) if (g_MemErrHandler)
{ {
if (g_MemErrHandler)
{
#ifdef DEBUGGINGAIDS #ifdef DEBUGGINGAIDS
g_MemErrHandler(g_MemErrLine, g_MemErrFile, g_MemErrFunc); g_MemErrHandler(g_MemErrLine, g_MemErrFile, g_MemErrFunc);
#else #else
g_MemErrHandler(0, "???", "???"); g_MemErrHandler(0, "???", "???");
#endif #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; 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__ #ifndef __compat_h_macrodef__