Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move zeromalloc trickery to internal malloc/free functions
  • Loading branch information
flibitijibibo committed May 23, 2018
1 parent 2be6d3a commit 406de33
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 21 deletions.
6 changes: 2 additions & 4 deletions mojoshader.c
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions mojoshader_assembler.c
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions mojoshader_common.c
Expand Up @@ -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 = {
Expand Down
6 changes: 2 additions & 4 deletions mojoshader_compiler.c
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions mojoshader_opengl.c
Expand Up @@ -347,18 +347,17 @@ 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;
} // Malloc

static inline void Free(void *ptr)
{
if ((ptr != &zeromalloc) && (ptr != NULL))
if (ptr != NULL)
ctx->free_fn(ptr, ctx->malloc_data);
} // Free

Expand Down
6 changes: 2 additions & 4 deletions mojoshader_preprocessor.c
Expand Up @@ -68,19 +68,17 @@ 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;
} // Malloc

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)
Expand Down

0 comments on commit 406de33

Please sign in to comment.