subversion-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Julian Foad <julianf...@btopenworld.com>
Subject The recent svn_string_create_empty() function
Date Tue, 03 Apr 2012 17:09:19 GMT
For comments, please:
[[[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.
--This line, and those below, will be ignored--


Index: subversion/include/svn_string.h
===================================================================
--- subversion/include/svn_string.h     (revision 1309011)
+++ subversion/include/svn_string.h     (working copy)
@@ -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 *
Index: subversion/libsvn_subr/svn_string.c
===================================================================
--- subversion/libsvn_subr/svn_string.c (revision 1309011)
+++ subversion/libsvn_subr/svn_string.c (working copy)
@@ -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
 ^L
 /* 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 *
]]]

- Julian


Mime
View raw message