From 406de3337e443de30b2a024468a834b42cb0d055 Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Wed, 23 May 2018 11:07:59 -0400 Subject: [PATCH] Move zeromalloc trickery to internal malloc/free functions --- mojoshader.c | 6 ++---- mojoshader_assembler.c | 6 ++---- mojoshader_common.c | 12 ++++++++++-- mojoshader_compiler.c | 6 ++---- mojoshader_opengl.c | 5 ++--- mojoshader_preprocessor.c | 6 ++---- 6 files changed, 20 insertions(+), 21 deletions(-) diff --git a/mojoshader.c b/mojoshader.c index a5fba001..083d8a79 100644 --- a/mojoshader.c +++ b/mojoshader.c @@ -307,10 +307,9 @@ static inline void out_of_memory(Context *ctx) ctx->isfail = ctx->out_of_memory = 1; } // out_of_memory -static char zeromalloc = 0; static inline void *Malloc(Context *ctx, const size_t len) { - void *retval = (len == 0) ? &zeromalloc : ctx->malloc((int) len, ctx->malloc_data); + void *retval = ctx->malloc((int) len, ctx->malloc_data); if (retval == NULL) out_of_memory(ctx); return retval; @@ -326,8 +325,7 @@ static inline char *StrDup(Context *ctx, const char *str) static inline void Free(Context *ctx, void *ptr) { - if ((ptr != &zeromalloc) && (ptr != NULL)) - ctx->free(ptr, ctx->malloc_data); + ctx->free(ptr, ctx->malloc_data); } // Free static void * MOJOSHADERCALL MallocBridge(int bytes, void *data) diff --git a/mojoshader_assembler.c b/mojoshader_assembler.c index 0621af85..c9148410 100644 --- a/mojoshader_assembler.c +++ b/mojoshader_assembler.c @@ -72,10 +72,9 @@ static inline void out_of_memory(Context *ctx) ctx->isfail = ctx->out_of_memory = 1; } // out_of_memory -static char zeromalloc = 0; static inline void *Malloc(Context *ctx, const size_t len) { - void *retval = (len == 0) ? &zeromalloc : ctx->malloc((int) len, ctx->malloc_data); + void *retval = ctx->malloc((int) len, ctx->malloc_data); if (retval == NULL) out_of_memory(ctx); return retval; @@ -91,8 +90,7 @@ static inline char *StrDup(Context *ctx, const char *str) static inline void Free(Context *ctx, void *ptr) { - if ((ptr != &zeromalloc) && (ptr != NULL)) - ctx->free(ptr, ctx->malloc_data); + ctx->free(ptr, ctx->malloc_data); } // Free static void *MallocBridge(int bytes, void *data) diff --git a/mojoshader_common.c b/mojoshader_common.c index 32d2bc91..601cb331 100644 --- a/mojoshader_common.c +++ b/mojoshader_common.c @@ -4,8 +4,16 @@ // Convenience functions for allocators... #if !MOJOSHADER_FORCE_ALLOCATOR -void * MOJOSHADERCALL MOJOSHADER_internal_malloc(int bytes, void *d) { return malloc(bytes); } -void MOJOSHADERCALL MOJOSHADER_internal_free(void *ptr, void *d) { free(ptr); } +static char zeromalloc = 0; +void * MOJOSHADERCALL MOJOSHADER_internal_malloc(int bytes, void *d) +{ + return (bytes == 0) ? &zeromalloc : malloc(bytes); +} // MOJOSHADER_internal_malloc +void MOJOSHADERCALL MOJOSHADER_internal_free(void *ptr, void *d) +{ + if ((ptr != &zeromalloc) && (ptr != NULL)) + free(ptr); +} // MOJOSHADER_internal_free #endif MOJOSHADER_error MOJOSHADER_out_of_mem_error = { diff --git a/mojoshader_compiler.c b/mojoshader_compiler.c index 012997cb..3e49ff64 100644 --- a/mojoshader_compiler.c +++ b/mojoshader_compiler.c @@ -160,10 +160,9 @@ static inline void out_of_memory(Context *ctx) ctx->isfail = ctx->out_of_memory = 1; } // out_of_memory -static char zeromalloc = 0; static inline void *Malloc(Context *ctx, const size_t len) { - void *retval = (len == 0) ? &zeromalloc : ctx->malloc((int) len, ctx->malloc_data); + void *retval = ctx->malloc((int) len, ctx->malloc_data); if (retval == NULL) out_of_memory(ctx); return retval; @@ -179,8 +178,7 @@ static inline char *StrDup(Context *ctx, const char *str) static inline void Free(Context *ctx, void *ptr) { - if ((ptr != &zeromalloc) && (ptr != NULL)) - ctx->free(ptr, ctx->malloc_data); + ctx->free(ptr, ctx->malloc_data); } // Free static void *MallocBridge(int bytes, void *data) diff --git a/mojoshader_opengl.c b/mojoshader_opengl.c index 2e88e358..08b9773f 100644 --- a/mojoshader_opengl.c +++ b/mojoshader_opengl.c @@ -347,10 +347,9 @@ static inline void out_of_memory(void) set_error("out of memory"); } // out_of_memory -static char zeromalloc = 0; static inline void *Malloc(const size_t len) { - void *retval = (len == 0) ? &zeromalloc : ctx->malloc_fn((int) len, ctx->malloc_data); + void *retval = ctx->malloc_fn((int) len, ctx->malloc_data); if (retval == NULL) out_of_memory(); return retval; @@ -358,7 +357,7 @@ static inline void *Malloc(const size_t len) static inline void Free(void *ptr) { - if ((ptr != &zeromalloc) && (ptr != NULL)) + if (ptr != NULL) ctx->free_fn(ptr, ctx->malloc_data); } // Free diff --git a/mojoshader_preprocessor.c b/mojoshader_preprocessor.c index 664b530e..5b5090ba 100644 --- a/mojoshader_preprocessor.c +++ b/mojoshader_preprocessor.c @@ -68,10 +68,9 @@ static inline void out_of_memory(Context *ctx) ctx->out_of_memory = 1; } // out_of_memory -static char zeromalloc = 0; static inline void *Malloc(Context *ctx, const size_t len) { - void *retval = (len == 0) ? &zeromalloc : ctx->malloc((int) len, ctx->malloc_data); + void *retval = ctx->malloc((int) len, ctx->malloc_data); if (retval == NULL) out_of_memory(ctx); return retval; @@ -79,8 +78,7 @@ static inline void *Malloc(Context *ctx, const size_t len) static inline void Free(Context *ctx, void *ptr) { - if ((ptr != &zeromalloc) && (ptr != NULL)) - ctx->free(ptr, ctx->malloc_data); + ctx->free(ptr, ctx->malloc_data); } // Free static void *MallocBridge(int bytes, void *data)