mesa: Allocate tokens from the heap.
authorJosé Fonseca <jfonseca@vmware.com>
Mon, 8 Jun 2009 15:27:59 +0000 (16:27 +0100)
committerJosé Fonseca <jfonseca@vmware.com>
Mon, 8 Jun 2009 15:56:41 +0000 (16:56 +0100)
The recent increase ST_MAX_SHADER_TOKENS to 8K causes stack overflows on
windows.

Failure to allocate is not being propagated to the caller. This is not
a regression since the previous _mesa_malloc result wasn't being
checked as well. Unfortunately it is not easy to fix, as the callers of
these functions do not have failure propagation mechanism either, and
so on. So leaving a just fixme note for now.

src/mesa/state_tracker/st_program.c

index 3492610..06030f6 100644 (file)
 #define TGSI_DEBUG 0
 
 
-/** XXX we should use the version of this from u_memory.h but including
- * that header causes symbol collisions.
- */
-static INLINE void *
-mem_dup(const void *src, uint size)
-{
-   void *dup = _mesa_malloc(size);
-   if (dup)
-      _mesa_memcpy(dup, src, size);
-   return dup;
-}
-
-
-
 /**
  * Translate a Mesa vertex shader into a TGSI shader.
  * \param outputMapping  to map vertex program output registers (VERT_RESULT_x)
@@ -84,7 +70,7 @@ st_translate_vertex_program(struct st_context *st,
                             const ubyte *outputSemanticIndex)
 {
    struct pipe_context *pipe = st->pipe;
-   struct tgsi_token tokens[ST_MAX_SHADER_TOKENS];
+   struct tgsi_token *tokens;
    GLuint defaultOutputMapping[VERT_RESULT_MAX];
    struct pipe_shader_state vs;
    GLuint attr, i;
@@ -102,6 +88,13 @@ st_translate_vertex_program(struct st_context *st,
    GLbitfield input_flags[MAX_PROGRAM_INPUTS];
    GLbitfield output_flags[MAX_PROGRAM_OUTPUTS];
 
+   tokens =  (struct tgsi_token *)MALLOC(ST_MAX_SHADER_TOKENS * sizeof *tokens);
+   if(!tokens) {
+      /* FIXME: propagate error to the caller */
+      assert(0);
+      return;
+   }
+
    memset(&vs, 0, sizeof(vs));
    memset(input_flags, 0, sizeof(input_flags));
    memset(output_flags, 0, sizeof(output_flags));
@@ -347,7 +340,9 @@ st_translate_vertex_program(struct st_context *st,
    assert(num_tokens < ST_MAX_SHADER_TOKENS);
 
    vs.tokens = (struct tgsi_token *)
-      mem_dup(tokens, num_tokens * sizeof(tokens[0]));
+      _mesa_realloc(tokens,
+                    ST_MAX_SHADER_TOKENS * sizeof *tokens,
+                    num_tokens * sizeof *tokens);
 
    stvp->num_inputs = vs_num_inputs;
    stvp->state = vs; /* struct copy */
@@ -375,7 +370,7 @@ st_translate_fragment_program(struct st_context *st,
                               const GLuint inputMapping[])
 {
    struct pipe_context *pipe = st->pipe;
-   struct tgsi_token tokens[ST_MAX_SHADER_TOKENS];
+   struct tgsi_token *tokens;
    GLuint outputMapping[FRAG_RESULT_MAX];
    GLuint defaultInputMapping[FRAG_ATTRIB_MAX];
    struct pipe_shader_state fs;
@@ -395,6 +390,13 @@ st_translate_fragment_program(struct st_context *st,
    GLbitfield input_flags[MAX_PROGRAM_INPUTS];
    GLbitfield output_flags[MAX_PROGRAM_OUTPUTS];
 
+   tokens =  (struct tgsi_token *)MALLOC(ST_MAX_SHADER_TOKENS * sizeof *tokens);
+   if(!tokens) {
+      /* FIXME: propagate error to the caller */
+      assert(0);
+      return;
+   }
+
    memset(&fs, 0, sizeof(fs));
    memset(input_flags, 0, sizeof(input_flags));
    memset(output_flags, 0, sizeof(output_flags));
@@ -536,7 +538,9 @@ st_translate_fragment_program(struct st_context *st,
    assert(num_tokens < ST_MAX_SHADER_TOKENS);
 
    fs.tokens = (struct tgsi_token *)
-      mem_dup(tokens, num_tokens * sizeof(tokens[0]));
+      _mesa_realloc(tokens,
+                    ST_MAX_SHADER_TOKENS * sizeof *tokens,
+                    num_tokens * sizeof *tokens);
 
    stfp->state = fs; /* struct copy */
    stfp->driver_shader = pipe->create_fs_state(pipe, &fs);