Module: Mesa
Branch: master
Commit: 26f4657c3f075045527c4568e8cbbaaa5d0b08e4
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=26f4657c3f075045527c4568e8cbbaaa5d0b08e4

Author: Timothy Arceri <tarc...@itsqueeze.com>
Date:   Wed Aug  9 13:34:08 2017 +1000

util/ralloc: add ralloc_str_append() helper

This function differs from ralloc_strcat() and ralloc_strncat()
in that it  does not do any strlen() calls which can become
costly on large strings.

Reviewed-by: Thomas Helland <thomashellan...@gmail.com>

---

 src/util/ralloc.c | 19 +++++++++++++++++++
 src/util/ralloc.h | 18 ++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/src/util/ralloc.c b/src/util/ralloc.c
index 821ee72fe8..bf46439df4 100644
--- a/src/util/ralloc.c
+++ b/src/util/ralloc.c
@@ -403,6 +403,25 @@ ralloc_strncat(char **dest, const char *str, size_t n)
    return cat(dest, str, strnlen(str, n));
 }
 
+bool
+ralloc_str_append(char **dest, const char *str,
+                  size_t existing_length, size_t str_size)
+{
+   char *both;
+   assert(dest != NULL && *dest != NULL);
+
+   both = resize(*dest, existing_length + str_size + 1);
+   if (unlikely(both == NULL))
+      return false;
+
+   memcpy(both + existing_length, str, str_size);
+   both[existing_length + str_size] = '\0';
+
+   *dest = both;
+
+   return true;
+}
+
 char *
 ralloc_asprintf(const void *ctx, const char *fmt, ...)
 {
diff --git a/src/util/ralloc.h b/src/util/ralloc.h
index 7d90651966..05ae8f8407 100644
--- a/src/util/ralloc.h
+++ b/src/util/ralloc.h
@@ -293,6 +293,24 @@ bool ralloc_strcat(char **dest, const char *str);
 bool ralloc_strncat(char **dest, const char *str, size_t n);
 
 /**
+ * Concatenate two strings, allocating the necessary space.
+ *
+ * This appends \p n bytes of \p str to \p *dest, using ralloc_resize
+ * to expand \p *dest to the appropriate size.  \p dest will be updated to the
+ * new pointer unless allocation fails.
+ *
+ * The result will always be null-terminated.
+ *
+ * This function differs from ralloc_strcat() and ralloc_strncat() in that it
+ * does not do any strlen() calls which can become costly on large strings.
+ *
+ * \return True unless allocation failed.
+ */
+bool
+ralloc_str_append(char **dest, const char *str,
+                  size_t existing_length, size_t str_size);
+
+/**
  * Print to a string.
  *
  * This is analogous to \c sprintf, but allocates enough space (using \p ctx

_______________________________________________
mesa-commit mailing list
mesa-commit@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-commit

Reply via email to