[Mesa-dev] [PATCH] ralloc: Add new [v]asprintf_rewrite_tail functions.

Kenneth Graunke kenneth at whitecape.org
Mon Oct 24 19:33:16 PDT 2011


This can be useful if you want to create a bunch of temporary strings
with a common prefix.  For example, when iterating over uniform
structure fields, one might want to create temporary strings like
"pallete.primary", "palette.outline", and "pallette.shadow".

This could be done by overwriting the '.' with a null-byte and calling
ralloc_asprintf_append, but that incurs the cost of strlen("pallete")
every time...when this is already known.

These new functions allow you rewrite the tail of the string, given a
starting index.  If the starting index is the length of the string, this
is equivalent to appending.

Signed-off-by: Kenneth Graunke <kenneth at whitecape.org>
---
 src/glsl/ralloc.c |   27 +++++++++++++++++++++---
 src/glsl/ralloc.h |   57 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+), 4 deletions(-)

diff --git a/src/glsl/ralloc.c b/src/glsl/ralloc.c
index fb48a91..2967598 100644
--- a/src/glsl/ralloc.c
+++ b/src/glsl/ralloc.c
@@ -439,7 +439,27 @@ ralloc_asprintf_append(char **str, const char *fmt, ...)
 bool
 ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
 {
-   size_t existing_length, new_length;
+   assert(str != NULL);
+   size_t existing_length = *str ? strlen(*str) : 0;
+   return ralloc_vasprintf_rewrite_tail(str, existing_length, fmt, args);
+}
+
+bool
+ralloc_asprintf_rewrite_tail(char **str, size_t start, const char *fmt, ...)
+{
+   bool success;
+   va_list args;
+   va_start(args, fmt);
+   success = ralloc_vasprintf_rewrite_tail(str, start, fmt, args);
+   va_end(args);
+   return success;
+}
+
+bool
+ralloc_vasprintf_rewrite_tail(char **str, size_t start, const char *fmt,
+			      va_list args)
+{
+   size_t new_length;
    char *ptr;
 
    assert(str != NULL);
@@ -450,14 +470,13 @@ ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
       return true;
    }
 
-   existing_length = strlen(*str);
    new_length = printf_length(fmt, args);
 
-   ptr = resize(*str, existing_length + new_length + 1);
+   ptr = resize(*str, start + new_length + 1);
    if (unlikely(ptr == NULL))
       return false;
 
-   vsnprintf(ptr + existing_length, new_length + 1, fmt, args);
+   vsnprintf(ptr + start, new_length + 1, fmt, args);
    *str = ptr;
    return true;
 }
diff --git a/src/glsl/ralloc.h b/src/glsl/ralloc.h
index d533815..1324f34 100644
--- a/src/glsl/ralloc.h
+++ b/src/glsl/ralloc.h
@@ -314,9 +314,60 @@ char *ralloc_asprintf (const void *ctx, const char *fmt, ...);
 char *ralloc_vasprintf(const void *ctx, const char *fmt, va_list args);
 
 /**
+ * Rewrite the tail of an existing string, starting at a given index.
+ *
+ * Overwrites the contents of *str starting at \p start with newly formatted
+ * text, including a new null-terminator.  Allocates more memory as necessary.
+ *
+ * This can be used to append formatted text when the length of the existing
+ * string is already known, saving a strlen() call.
+ *
+ * \sa ralloc_asprintf_append
+ *
+ * \param str   The string to be updated.
+ * \param start The index to start appending new data at.
+ * \param fmt   A printf-style formatting string
+ *
+ * \p str will be updated to the new pointer unless allocation fails.
+ *
+ * \return True unless allocation failed.
+ */
+bool ralloc_asprintf_rewrite_tail(char **str, size_t start,
+				  const char *fmt, ...);
+
+/**
+ * Rewrite the tail of an existing string, starting at a given index.
+ *
+ * Overwrites the contents of *str starting at \p start with newly formatted
+ * text, including a new null-terminator.  Allocates more memory as necessary.
+ *
+ * This can be used to append formatted text when the length of the existing
+ * string is already known, saving a strlen() call.
+ *
+ * \sa ralloc_vasprintf_append
+ *
+ * \param str   The string to be updated.
+ * \param start The index to start appending new data at.
+ * \param fmt   A printf-style formatting string
+ * \param args  A va_list containing the data to be formatted
+ *
+ * \p str will be updated to the new pointer unless allocation fails.
+ *
+ * \return True unless allocation failed.
+ */
+bool ralloc_vasprintf_rewrite_tail(char **str, size_t start, const char *fmt,
+				   va_list args);
+
+/**
  * Append formatted text to the supplied string.
  *
+ * This is equivalent to
+ * \code
+ * ralloc_asprintf_rewrite_tail(str, strlen(*str), fmt, ...)
+ * \endcode
+ *
  * \sa ralloc_asprintf
+ * \sa ralloc_asprintf_rewrite_tail
  * \sa ralloc_strcat
  *
  * \p str will be updated to the new pointer unless allocation fails.
@@ -328,7 +379,13 @@ bool ralloc_asprintf_append (char **str, const char *fmt, ...);
 /**
  * Append formatted text to the supplied string, given a va_list.
  *
+ * This is equivalent to
+ * \code
+ * ralloc_vasprintf_rewrite_tail(str, strlen(*str), fmt, args)
+ * \endcode
+ *
  * \sa ralloc_vasprintf
+ * \sa ralloc_vasprintf_rewrite_tail
  * \sa ralloc_strcat
  *
  * \p str will be updated to the new pointer unless allocation fails.
-- 
1.7.7



More information about the mesa-dev mailing list