add_define() can take a string we allocated elsewhere, to avoid extra StrDup().
authorRyan C. Gordon <icculus@icculus.org>
Mon, 16 Feb 2009 22:21:35 -0500
changeset 637 2e6202cde135
parent 636 6923c9cfb4b9
child 638 e910619c61f4
add_define() can take a string we allocated elsewhere, to avoid extra StrDup().
mojoshader_preprocessor.c
--- a/mojoshader_preprocessor.c	Mon Feb 16 21:43:14 2009 -0500
+++ b/mojoshader_preprocessor.c	Mon Feb 16 22:21:35 2009 -0500
@@ -385,7 +385,7 @@
 } // hash_define
 
 
-static int add_define(Context *ctx, const char *sym, const char *val)
+static int add_define(Context *ctx, const char *sym, const char *val, int copy)
 {
     char *identifier = NULL;
     char *definition = NULL;
@@ -406,19 +406,29 @@
         return 0;
 
     identifier = (char *) Malloc(ctx, strlen(sym) + 1);
-    definition = (char *) Malloc(ctx, strlen(val) + 1);
-    if ((identifier == NULL) || (definition == NULL))
+    if (identifier == NULL)
     {
-        Free(ctx, identifier);
-        Free(ctx, definition);
         Free(ctx, bucket);
         return 0;
     } // if
+    strcpy(identifier, sym);
 
-    strcpy(identifier, sym);
-    strcpy(definition, val);
+    if (!copy)
+        bucket->define.definition = val;
+    else
+    {
+        definition = (char *) Malloc(ctx, strlen(val) + 1);
+        if (definition == NULL)
+        {
+            Free(ctx, identifier);
+            Free(ctx, bucket);
+            return 0;
+        } // if
+        strcpy(definition, val);
+        bucket->define.definition = definition;
+    } // if
+
     bucket->define.identifier = identifier;
-    bucket->define.definition = definition;
     bucket->next = ctx->define_hashtable[hash];
     ctx->define_hashtable[hash] = bucket;
     return 1;
@@ -615,7 +625,7 @@
 
     for (i = 0; i < define_count; i++)
     {
-        if (!add_define(ctx, defines[i]->identifier, defines[i]->definition))
+        if (!add_define(ctx, defines[i]->identifier, defines[i]->definition, 1))
         {
             okay = 0;
             break;