[Mesa-dev] [PATCH 1/2] ralloc: Make rewrite_tail increase "start" by the new text's length.

Kenneth Graunke kenneth at whitecape.org
Tue Feb 21 13:38:49 PST 2012


Both callers of rewrite_tail immediately compute the new total string
length by adding the (known) length of the existing string plus the
length of the newly appended text.  Unfortunately, callers generally
won't know the length of the new text, as it's printf-formatted.

Since ralloc already computes this length, it makes sense to add it in
and save the caller the effort.  This simplifies both existing callers,
but more importantly, will allow for cheap-appending in the next commit.

Signed-off-by: Kenneth Graunke <kenneth at whitecape.org>
---
 src/glsl/link_uniforms.cpp |   14 +++++---------
 src/glsl/linker.h          |    2 +-
 src/glsl/ralloc.c          |   11 ++++++-----
 src/glsl/ralloc.h          |    6 ++++--
 4 files changed, 16 insertions(+), 17 deletions(-)

diff --git a/src/glsl/link_uniforms.cpp b/src/glsl/link_uniforms.cpp
index d51850c..aca6dc0 100644
--- a/src/glsl/link_uniforms.cpp
+++ b/src/glsl/link_uniforms.cpp
@@ -67,7 +67,7 @@ uniform_field_visitor::process(ir_variable *var)
 
 void
 uniform_field_visitor::recursion(const glsl_type *t, char **name,
-				 unsigned name_length)
+				 size_t name_length)
 {
    /* Records need to have each field processed individually.
     *
@@ -80,20 +80,16 @@ uniform_field_visitor::recursion(const glsl_type *t, char **name,
 	 const char *field = t->fields.structure[i].name;
 
 	 /* Append '.field' to the current uniform name. */
-	 ralloc_asprintf_rewrite_tail(name, name_length, ".%s", field);
+	 ralloc_asprintf_rewrite_tail(name, &name_length, ".%s", field);
 
-	 recursion(t->fields.structure[i].type, name,
-		   name_length + 1 + strlen(field));
+	 recursion(t->fields.structure[i].type, name, name_length);
       }
    } else if (t->is_array() && t->fields.array->is_record()) {
       for (unsigned i = 0; i < t->length; i++) {
-	 char subscript[13];
-
 	 /* Append the subscript to the current uniform name */
-	 const unsigned subscript_length = snprintf(subscript, 13, "[%u]", i);
-	 ralloc_asprintf_rewrite_tail(name, name_length, "%s", subscript);
+	 ralloc_asprintf_rewrite_tail(name, &name_length, "[%u]", i);
 
-	 recursion(t->fields.array, name, name_length + subscript_length);
+	 recursion(t->fields.array, name, name_length);
       }
    } else {
       this->visit_field(t, *name);
diff --git a/src/glsl/linker.h b/src/glsl/linker.h
index 433c63b..0b4c001 100644
--- a/src/glsl/linker.h
+++ b/src/glsl/linker.h
@@ -76,7 +76,7 @@ private:
     * \param name_length  Length of the current name \b not including the
     *                     terminating \c NUL character.
     */
-   void recursion(const glsl_type *t, char **name, unsigned name_length);
+   void recursion(const glsl_type *t, char **name, size_t name_length);
 };
 
 #endif /* GLSL_LINKER_H */
diff --git a/src/glsl/ralloc.c b/src/glsl/ralloc.c
index 91e4bab..2f93dcd 100644
--- a/src/glsl/ralloc.c
+++ b/src/glsl/ralloc.c
@@ -448,11 +448,11 @@ ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
    size_t existing_length;
    assert(str != NULL);
    existing_length = *str ? strlen(*str) : 0;
-   return ralloc_vasprintf_rewrite_tail(str, existing_length, fmt, args);
+   return ralloc_vasprintf_rewrite_tail(str, &existing_length, fmt, args);
 }
 
 bool
-ralloc_asprintf_rewrite_tail(char **str, size_t start, const char *fmt, ...)
+ralloc_asprintf_rewrite_tail(char **str, size_t *start, const char *fmt, ...)
 {
    bool success;
    va_list args;
@@ -463,7 +463,7 @@ ralloc_asprintf_rewrite_tail(char **str, size_t start, const char *fmt, ...)
 }
 
 bool
-ralloc_vasprintf_rewrite_tail(char **str, size_t start, const char *fmt,
+ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
 			      va_list args)
 {
    size_t new_length;
@@ -479,11 +479,12 @@ ralloc_vasprintf_rewrite_tail(char **str, size_t start, const char *fmt,
 
    new_length = printf_length(fmt, args);
 
-   ptr = resize(*str, start + new_length + 1);
+   ptr = resize(*str, *start + new_length + 1);
    if (unlikely(ptr == NULL))
       return false;
 
-   vsnprintf(ptr + start, new_length + 1, fmt, args);
+   vsnprintf(ptr + *start, new_length + 1, fmt, args);
    *str = ptr;
+   *start += new_length;
    return true;
 }
diff --git a/src/glsl/ralloc.h b/src/glsl/ralloc.h
index 1324f34..86306b1 100644
--- a/src/glsl/ralloc.h
+++ b/src/glsl/ralloc.h
@@ -329,10 +329,11 @@ char *ralloc_vasprintf(const void *ctx, const char *fmt, va_list args);
  * \param fmt   A printf-style formatting string
  *
  * \p str will be updated to the new pointer unless allocation fails.
+ * \p start will be increased by the length of the newly formatted text.
  *
  * \return True unless allocation failed.
  */
-bool ralloc_asprintf_rewrite_tail(char **str, size_t start,
+bool ralloc_asprintf_rewrite_tail(char **str, size_t *start,
 				  const char *fmt, ...);
 
 /**
@@ -352,10 +353,11 @@ bool ralloc_asprintf_rewrite_tail(char **str, size_t start,
  * \param args  A va_list containing the data to be formatted
  *
  * \p str will be updated to the new pointer unless allocation fails.
+ * \p start will be increased by the length of the newly formatted text.
  *
  * \return True unless allocation failed.
  */
-bool ralloc_vasprintf_rewrite_tail(char **str, size_t start, const char *fmt,
+bool ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
 				   va_list args);
 
 /**
-- 
1.7.7.6



More information about the mesa-dev mailing list