mesa: allocate the attribute stack on demand
authorMarek Olšák <marek.olsak@amd.com>
Thu, 12 Nov 2020 00:11:09 +0000 (19:11 -0500)
committerMarge Bot <eric+marge@anholt.net>
Tue, 1 Dec 2020 11:52:11 +0000 (11:52 +0000)
Reviewed-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6946>

src/mesa/main/attrib.c
src/mesa/main/mtypes.h

index b03457b56c0c1d98a75410f401020e76632e6114..0e385111dd9fde7aafefe3301660dc20b9338793 100644 (file)
@@ -78,7 +78,15 @@ _mesa_PushAttrib(GLbitfield mask)
       return;
    }
 
-   head = &ctx->AttribStack[ctx->AttribStackDepth];
+   head = ctx->AttribStack[ctx->AttribStackDepth];
+   if (unlikely(!head)) {
+      head = CALLOC_STRUCT(gl_attrib_node);
+      if (unlikely(!head)) {
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
+         return;
+      }
+      ctx->AttribStack[ctx->AttribStackDepth] = head;
+   }
    head->Mask = mask;
 
    if (mask & GL_ACCUM_BUFFER_BIT)
@@ -650,7 +658,7 @@ _mesa_PopAttrib(void)
    }
 
    ctx->AttribStackDepth--;
-   attr = &ctx->AttribStack[ctx->AttribStackDepth];
+   attr = ctx->AttribStack[ctx->AttribStackDepth];
 
    unsigned mask = attr->Mask;
 
@@ -1471,11 +1479,14 @@ _mesa_free_attrib_data(struct gl_context *ctx)
       struct gl_attrib_node *attr;
 
       ctx->AttribStackDepth--;
-      attr = &ctx->AttribStack[ctx->AttribStackDepth];
+      attr = ctx->AttribStack[ctx->AttribStackDepth];
 
       if (attr->Mask & GL_TEXTURE_BIT)
          _mesa_reference_shared_state(ctx, &attr->Texture.SharedRef, NULL);
    }
+
+   for (unsigned i = 0; i < ARRAY_SIZE(ctx->AttribStack); i++)
+      free(ctx->AttribStack[i]);
 }
 
 
index bc42670855fcc787eea79210f73e11a4b38febd3..a15bdf88dafc322ddde896c704a7b3ad57402e39 100644 (file)
@@ -5195,7 +5195,7 @@ struct gl_context
    /** \name State attribute stack (for glPush/PopAttrib) */
    /*@{*/
    GLuint AttribStackDepth;
-   struct gl_attrib_node AttribStack[MAX_ATTRIB_STACK_DEPTH];
+   struct gl_attrib_node *AttribStack[MAX_ATTRIB_STACK_DEPTH];
    /*@}*/
 
    /** \name Renderer attribute groups