Skip to content

Commit

Permalink
Renamed internal_malloc() and internal_free().
Browse files Browse the repository at this point in the history
This is to prevent namespace clash if we statically link MojoShader to an app.
  • Loading branch information
icculus committed Feb 7, 2009
1 parent 0f57738 commit 85e84ce
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
14 changes: 8 additions & 6 deletions mojoshader.c
Expand Up @@ -237,6 +237,8 @@ struct Context


// Convenience functions for allocators...
void *MOJOSHADER_internal_malloc(int bytes, void *d) { return malloc(bytes); }
void MOJOSHADER_internal_free(void *ptr, void *d) { free(ptr); }

MOJOSHADER_error MOJOSHADER_out_of_mem_error = { "Out of memory", NULL, -1 };
MOJOSHADER_parseData MOJOSHADER_out_of_mem_data = {
Expand Down Expand Up @@ -6745,8 +6747,8 @@ static Context *build_context(const char *profile,
const unsigned int swizcount,
MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
{
if (m == NULL) m = internal_malloc;
if (f == NULL) f = internal_free;
if (m == NULL) m = MOJOSHADER_internal_malloc;
if (f == NULL) f = MOJOSHADER_internal_free;

Context *ctx = (Context *) m(sizeof (Context), d);
if (ctx == NULL)
Expand Down Expand Up @@ -6836,7 +6838,7 @@ static void destroy_context(Context *ctx)
{
if (ctx != NULL)
{
MOJOSHADER_free f = ((ctx->free != NULL) ? ctx->free : internal_free);
MOJOSHADER_free f = ((ctx->free != NULL) ? ctx->free : MOJOSHADER_internal_free);
void *d = ctx->malloc_data;
if (ctx->output_bytes != NULL)
f(d, ctx->output_bytes);
Expand Down Expand Up @@ -7289,8 +7291,8 @@ static MOJOSHADER_parseData *build_parsedata(Context *ctx)

retval->error_count = ctx->error_count;
retval->errors = errors;
retval->malloc = (ctx->malloc == internal_malloc) ? NULL : ctx->malloc;
retval->free = (ctx->free == internal_free) ? NULL : ctx->free;
retval->malloc = (ctx->malloc == MOJOSHADER_internal_malloc) ? NULL : ctx->malloc;
retval->free = (ctx->free == MOJOSHADER_internal_free) ? NULL : ctx->free;
retval->malloc_data = ctx->malloc_data;

return retval;
Expand Down Expand Up @@ -7539,7 +7541,7 @@ void MOJOSHADER_freeParseData(const MOJOSHADER_parseData *_data)
if ((data == NULL) || (data == &MOJOSHADER_out_of_mem_data))
return; // no-op.

MOJOSHADER_free f = (data->free == NULL) ? internal_free : data->free;
MOJOSHADER_free f = (data->free == NULL) ? MOJOSHADER_internal_free : data->free;
void *d = data->malloc_data;
int i;

Expand Down
10 changes: 5 additions & 5 deletions mojoshader_assembler.c
Expand Up @@ -1704,8 +1704,8 @@ static void parse_token(Context *ctx)
static Context *build_context(const char *source, MOJOSHADER_malloc m,
MOJOSHADER_free f, void *d)
{
if (m == NULL) m = internal_malloc;
if (f == NULL) f = internal_free;
if (m == NULL) m = MOJOSHADER_internal_malloc;
if (f == NULL) f = MOJOSHADER_internal_free;

Context *ctx = (Context *) m(sizeof (Context), d);
if (ctx == NULL)
Expand Down Expand Up @@ -1740,7 +1740,7 @@ static void destroy_context(Context *ctx)
{
if (ctx != NULL)
{
MOJOSHADER_free f = ((ctx->free != NULL) ? ctx->free : internal_free);
MOJOSHADER_free f = ((ctx->free != NULL) ? ctx->free : MOJOSHADER_internal_free);
void *d = ctx->malloc_data;
free_error_list(f, d, ctx->errors);
if (ctx->output != NULL)
Expand Down Expand Up @@ -1792,8 +1792,8 @@ static const MOJOSHADER_parseData *build_failed_assembly(Context *ctx)
return &MOJOSHADER_out_of_mem_data;

memset(retval, '\0', sizeof (MOJOSHADER_parseData));
retval->malloc = (ctx->malloc == internal_malloc) ? NULL : ctx->malloc;
retval->free = (ctx->free == internal_free) ? NULL : ctx->free;
retval->malloc = (ctx->malloc == MOJOSHADER_internal_malloc) ? NULL : ctx->malloc;
retval->free = (ctx->free == MOJOSHADER_internal_free) ? NULL : ctx->free;
retval->malloc_data = ctx->malloc_data;

retval->error_count = ctx->error_count;
Expand Down
8 changes: 4 additions & 4 deletions mojoshader_internal.h
Expand Up @@ -132,11 +132,11 @@ typedef int32_t int32;
// #define this to force app to supply an allocator, so there's no reference
// to the C runtime's malloc() and free()...
#if MOJOSHADER_FORCE_ALLOCATOR
#define internal_malloc NULL
#define internal_free NULL
#define MOJOSHADER_internal_malloc NULL
#define MOJOSHADER_internal_free NULL
#else
static void *internal_malloc(int bytes, void *d) { return malloc(bytes); }
static void internal_free(void *ptr, void *d) { free(ptr); }
void *MOJOSHADER_internal_malloc(int bytes, void *d);
void MOJOSHADER_internal_free(void *ptr, void *d);
#endif

// result modifiers.
Expand Down
4 changes: 2 additions & 2 deletions mojoshader_opengl.c
Expand Up @@ -939,8 +939,8 @@ MOJOSHADER_glContext *MOJOSHADER_glCreateContext(const char *profile,
MOJOSHADER_glContext *current_ctx = ctx;
ctx = NULL;

if (m == NULL) m = internal_malloc;
if (f == NULL) f = internal_free;
if (m == NULL) m = MOJOSHADER_internal_malloc;
if (f == NULL) f = MOJOSHADER_internal_free;

ctx = (MOJOSHADER_glContext *) m(sizeof (MOJOSHADER_glContext), d);
if (ctx == NULL)
Expand Down

0 comments on commit 85e84ce

Please sign in to comment.