Author: julianfoad
Date: Thu Apr  5 09:53:30 2012
New Revision: 1309730

URL: http://svn.apache.org/viewvc?rev=1309730&view=rev
Log:
Tweak the recently introduced svn_stringbuf_create_empty() function.

Unlike with svn_string_create_empty() which produces an immutable string,
the resulting string buffer is often going to have data appended to it.  The
initial implementation created a non-standard buffer that claimed that no
space was allocated, not even for the null terminator, which was strictly
incorrect and meant it would always need reallocation before appending any
data.  By calling the standard create function, we ensure the class
invariants are maintained without any tricks, and also ensure there is
enough space for a little expansion before reallocation is required, without
sacrificing any significant space or speed.

* subversion/include/svn_string.h
  (svn_stringbuf_create_empty): Don't claim that the allocated size will be
    zero.

* subversion/libsvn_subr/svn_string.c
  (empty_buffer): Make this 'const' now that we can because it's only used
    for svn_string_t and not svn_stringbuf_t. Add a doc string.
  (create_stringbuf): Add a doc string and assertions.
  (svn_stringbuf_create_empty): Call svn_stringbuf_create_ensure() instead
    of constructing a non-standard, "very empty" stringbuf.

Modified:
    subversion/trunk/subversion/include/svn_string.h
    subversion/trunk/subversion/libsvn_subr/svn_string.c

Modified: subversion/trunk/subversion/include/svn_string.h
URL: 
http://svn.apache.org/viewvc/subversion/trunk/subversion/include/svn_string.h?rev=1309730&r1=1309729&r2=1309730&view=diff
==============================================================================
--- subversion/trunk/subversion/include/svn_string.h (original)
+++ subversion/trunk/subversion/include/svn_string.h Thu Apr  5 09:53:30 2012
@@ -200,7 +200,7 @@ svn_stringbuf_create(const char *cstring
 svn_stringbuf_t *
 svn_stringbuf_ncreate(const char *bytes, apr_size_t size, apr_pool_t *pool);
 
-/** Create a truely empty string object (length and blocksize are 0)
+/** Create a new, empty bytestring.
  * @since New in 1.8.
  */
 svn_stringbuf_t *

Modified: subversion/trunk/subversion/libsvn_subr/svn_string.c
URL: 
http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_subr/svn_string.c?rev=1309730&r1=1309729&r2=1309730&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_subr/svn_string.c (original)
+++ subversion/trunk/subversion/libsvn_subr/svn_string.c Thu Apr  5 09:53:30 
2012
@@ -132,7 +132,9 @@ create_string(const char *data, apr_size
   return new_string;
 }
 
-static char empty_buffer[1] = {0};
+/* A data buffer for a zero-length string (just a null terminator).  Many
+ * svn_string_t instances may share this same buffer. */
+static const char empty_buffer[1] = {0};
 
 svn_string_t *
 svn_string_create_empty(apr_pool_t *pool)
@@ -281,6 +283,9 @@ svn_stringbuf__morph_into_string(svn_str
 
 /* svn_stringbuf functions */
 
+/* Create a stringbuf referring to (not copying) an existing block of memory
+ * at DATA, of which SIZE bytes are the user data and BLOCKSIZE bytes are
+ * allocated in total.  DATA[SIZE] must be a zero byte. */
 static svn_stringbuf_t *
 create_stringbuf(char *data, apr_size_t size, apr_size_t blocksize,
                  apr_pool_t *pool)
@@ -289,6 +294,9 @@ create_stringbuf(char *data, apr_size_t 
 
   new_string = apr_palloc(pool, sizeof(*new_string));
 
+  SVN_ERR_ASSERT_NO_RETURN(size < blocksize);
+  SVN_ERR_ASSERT_NO_RETURN(data[size] == '\0');
+
   new_string->data = data;
   new_string->len = size;
   new_string->blocksize = blocksize;
@@ -300,12 +308,7 @@ create_stringbuf(char *data, apr_size_t 
 svn_stringbuf_t *
 svn_stringbuf_create_empty(apr_pool_t *pool)
 {
-  /* All instances share the same zero-length buffer.
-   * Some algorithms, however, assume that they may write
-   * the terminating zero. So, empty_buffer must be writable
-   * (a simple (char *)"" will cause SEGFAULTs). */
-
-  return create_stringbuf(empty_buffer, 0, 0, pool);
+  return svn_stringbuf_create_ensure(0, pool);
 }
 
 svn_stringbuf_t *


Reply via email to