Cleaned up memory management in MOJOSHADER_glLinkProgram(). trunk
authorRyan C. Gordon <icculus@icculus.org>
Tue, 03 Jun 2008 13:23:16 -0400
branchtrunk
changeset 355 f42ca594e366
parent 354 ec1d705d167a
child 356 8e23d7eb90d9
Cleaned up memory management in MOJOSHADER_glLinkProgram(). Explicitly initializes all malloc()'d buffers, doesn't allocate 0-byte arrays.
mojoshader_opengl.c
--- a/mojoshader_opengl.c	Mon Jun 02 17:54:41 2008 -0400
+++ b/mojoshader_opengl.c	Tue Jun 03 13:23:16 2008 -0400
@@ -948,18 +948,26 @@
     numregs = 0;
     if (vshader != NULL) numregs += vshader->parseData->uniform_count;
     if (pshader != NULL) numregs += pshader->parseData->uniform_count;
-    retval->uniforms = (UniformMap *) Malloc(sizeof (UniformMap) * numregs);
-    if (retval->uniforms == NULL)
-        goto link_program_fail;
-    memset(retval->uniforms, '\0', sizeof (UniformMap) * numregs);
+    if (numregs > 0)
+    {
+        const size_t len = sizeof (UniformMap) * numregs;
+        retval->uniforms = (UniformMap *) Malloc(len);
+        if (retval->uniforms == NULL)
+            goto link_program_fail;
+        memset(retval->uniforms, '\0', len);
+    } // if
 
     numregs = 0;
     if (vshader != NULL) numregs += vshader->parseData->sampler_count;
     if (pshader != NULL) numregs += pshader->parseData->sampler_count;
-    retval->samplers = (SamplerMap *) Malloc(sizeof (SamplerMap) * numregs);
-    if (retval->samplers == NULL)
-        goto link_program_fail;
-    memset(retval->samplers, '\0', sizeof (SamplerMap) * numregs);
+    if (numregs > 0)
+    {
+        const size_t len = sizeof (SamplerMap) * numregs;
+        retval->samplers = (SamplerMap *) Malloc(len);
+        if (retval->samplers == NULL)
+            goto link_program_fail;
+        memset(retval->samplers, '\0', len);
+    } // if
 
     retval->handle = program;
     retval->vertex = vshader;
@@ -970,12 +978,19 @@
     {
         if (consts < vshader->parseData->constant_count)
             consts = vshader->parseData->constant_count;
-        retval->attributes = (AttributeMap *) Malloc(sizeof (AttributeMap) *
-                                        vshader->parseData->attribute_count);
-        if (retval->attributes == NULL)
-            goto link_program_fail;
 
-        lookup_attributes(retval);
+        if (vshader->parseData->attribute_count > 0)
+        {
+            const int count = vshader->parseData->attribute_count;
+            const size_t len = sizeof (AttributeMap) * count;
+            retval->attributes = (AttributeMap *) Malloc(len);
+            if (retval->attributes == NULL)
+                goto link_program_fail;
+
+            memset(retval->attributes, '\0', len);
+            lookup_attributes(retval);
+        } // if
+
         lookup_uniforms(retval, vshader);
         lookup_samplers(retval, vshader);
         vshader->refcount++;
@@ -993,10 +1008,12 @@
 
     if (consts > 0)    
     {
-        retval->constants = (GLfloat *) Malloc(sizeof (GLfloat) * consts * 4);
+        const size_t len = sizeof (GLfloat) * consts * 4;
+        retval->constants = (GLfloat *) Malloc(len);
         if (retval->constants == NULL)
             goto link_program_fail;
         retval->constant_count = (uint32) consts;
+        memset(retval->constants, '\0', len);
     } // if
 
     return retval;