Move zeromalloc trickery to internal malloc/free functions default tip
authorEthan Lee <flibitijibibo@flibitijibibo.com>
Wed, 23 May 2018 11:07:59 -0400
changeset 1188 25000edc0176
parent 1187 0687a8d78a54
Move zeromalloc trickery to internal malloc/free functions
mojoshader.c
mojoshader_assembler.c
mojoshader_common.c
mojoshader_compiler.c
mojoshader_opengl.c
mojoshader_preprocessor.c
--- a/mojoshader.c	Thu May 17 13:05:09 2018 -0400
+++ b/mojoshader.c	Wed May 23 11:07:59 2018 -0400
@@ -307,10 +307,9 @@
     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 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)
--- a/mojoshader_assembler.c	Thu May 17 13:05:09 2018 -0400
+++ b/mojoshader_assembler.c	Wed May 23 11:07:59 2018 -0400
@@ -72,10 +72,9 @@
     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 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)
--- a/mojoshader_common.c	Thu May 17 13:05:09 2018 -0400
+++ b/mojoshader_common.c	Wed May 23 11:07:59 2018 -0400
@@ -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 = {
--- a/mojoshader_compiler.c	Thu May 17 13:05:09 2018 -0400
+++ b/mojoshader_compiler.c	Wed May 23 11:07:59 2018 -0400
@@ -160,10 +160,9 @@
     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 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)
--- a/mojoshader_opengl.c	Thu May 17 13:05:09 2018 -0400
+++ b/mojoshader_opengl.c	Wed May 23 11:07:59 2018 -0400
@@ -347,10 +347,9 @@
     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 Free(void *ptr)
 {
-    if ((ptr != &zeromalloc) && (ptr != NULL))
+    if (ptr != NULL)
         ctx->free_fn(ptr, ctx->malloc_data);
 } // Free
 
--- a/mojoshader_preprocessor.c	Thu May 17 13:05:09 2018 -0400
+++ b/mojoshader_preprocessor.c	Wed May 23 11:07:59 2018 -0400
@@ -68,10 +68,9 @@
     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 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)