From 8292b7419d0405e94a5ea270ba710d20f0eb071f Mon Sep 17 00:00:00 2001 From: Kenneth Graunke Date: Thu, 9 Feb 2012 20:03:36 -0800 Subject: [PATCH] ralloc: Make rewrite_tail increase "start" by the new text's length. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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. v2: The link_uniforms code needs both the old and new length. Apply the obvious fix (which sadly makes it less of a cleanup). Signed-off-by: Kenneth Graunke Reviewed-by: Ian Romanick [v1] Acked-by: José Fonseca [v1] --- src/glsl/link_uniforms.cpp | 15 +++++++-------- src/glsl/linker.h | 2 +- src/glsl/ralloc.c | 11 ++++++----- src/glsl/ralloc.h | 6 ++++-- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/glsl/link_uniforms.cpp b/src/glsl/link_uniforms.cpp index d51850c216a..613c9b7ae22 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. * @@ -78,22 +78,21 @@ uniform_field_visitor::recursion(const glsl_type *t, char **name, if (t->is_record()) { for (unsigned i = 0; i < t->length; i++) { const char *field = t->fields.structure[i].name; + size_t new_length = name_length; /* Append '.field' to the current uniform name. */ - ralloc_asprintf_rewrite_tail(name, name_length, ".%s", field); + ralloc_asprintf_rewrite_tail(name, &new_length, ".%s", field); - recursion(t->fields.structure[i].type, name, - name_length + 1 + strlen(field)); + recursion(t->fields.structure[i].type, name, new_length); } } else if (t->is_array() && t->fields.array->is_record()) { for (unsigned i = 0; i < t->length; i++) { - char subscript[13]; + size_t new_length = name_length; /* 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, &new_length, "[%u]", i); - recursion(t->fields.array, name, name_length + subscript_length); + recursion(t->fields.array, name, new_length); } } else { this->visit_field(t, *name); diff --git a/src/glsl/linker.h b/src/glsl/linker.h index 433c63be246..0b4c001f7e3 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 91e4bab2ebd..2f93dcdeaf7 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 1324f3466bb..86306b1f558 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); /** -- 2.30.2