subversion-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject svn commit: r1412418 - in /subversion/trunk/subversion: include/private/svn_string_private.h libsvn_subr/string.c
Date Thu, 22 Nov 2012 05:20:52 GMT
Author: brane
Date: Thu Nov 22 05:20:51 2012
New Revision: 1412418

URL: http://svn.apache.org/viewvc?rev=1412418&view=rev
Log:
Groundwork for issue #4261. Invent memory buffers.

* subversion/include/private/svn_string_private.h
  (svn_membuf_t, svn_membuf__create,
   svn_membuf__ensure, svn_membuf__resize,
   svn_membuf__zero, svn_membuf__nzero): New prototypes.
   SVN_MEMBUF__ZERO, SVN_MEMBUF__NZERO): New macros.

* subversion/libsvn_subr/string.c
  (svn_membuf_t, svn_membuf__create,
   svn_membuf__ensure, svn_membuf__resize,
   svn_membuf__zero, svn_membuf__nzero): Implement.

Modified:
    subversion/trunk/subversion/include/private/svn_string_private.h
    subversion/trunk/subversion/libsvn_subr/string.c

Modified: subversion/trunk/subversion/include/private/svn_string_private.h
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/include/private/svn_string_private.h?rev=1412418&r1=1412417&r2=1412418&view=diff
==============================================================================
--- subversion/trunk/subversion/include/private/svn_string_private.h (original)
+++ subversion/trunk/subversion/include/private/svn_string_private.h Thu Nov 22 05:20:51 2012
@@ -46,6 +46,83 @@ extern "C" {
  * @{
  */
 
+
+/** A self-contained memory buffer of known size.
+ *
+ * Intended to be used where a single variable-sized buffer is needed
+ * within an iteration, a scratch pool is available and we want to
+ * avoid the cost of creating another pool just for the iteration.
+ */
+typedef struct svn_membuf_t
+{
+  /** The a pool from which this buffer was originally allocated, and is not
+   * necessarily specific to this buffer.  This is used only for allocating
+   * more memory from when the buffer needs to grow.
+   */
+  apr_pool_t *pool;
+
+  /** pointer to the memory */
+  void *data;
+
+  /** total size of buffer allocated */
+  apr_size_t size;
+} svn_membuf_t;
+
+
+/* Initialize a memory buffer of the given size */
+void
+svn_membuf__create(svn_membuf_t *membuf, apr_size_t size, apr_pool_t *pool);
+
+/* Ensure that the given memory buffer has at least the given size */
+void
+svn_membuf__ensure(svn_membuf_t *membuf, apr_size_t size);
+
+/* Resize the given memory buffer, preserving its contents. */
+void
+svn_membuf__resize(svn_membuf_t *membuf, apr_size_t size);
+
+/* Zero-fill the given memory */
+void
+svn_membuf__zero(svn_membuf_t *membuf);
+
+/* Zero-fill the given memory buffer up to the smaller of SIZE and the
+   current buffer size. */
+void
+svn_membuf__nzero(svn_membuf_t *membuf, apr_size_t size);
+
+/* Inline implementation of svn_membuf__zero.
+ * Note that PMEMBUF is evaluated only once.
+ */
+#define SVN_MEMBUF__ZERO(pmembuf)                \
+  do                                             \
+    {                                            \
+      svn_membuf_t *const _m_b_f_ = (pmembuf);   \
+      memset(_m_b_f_->data, 0, _m_b_f_->size);   \
+    }                                            \
+  while(0)
+
+/* Inline implementation of svn_membuf__nzero
+ * Note that PMEMBUF and PSIZE are evaluated only once.
+ */
+#define SVN_MEMBUF__NZERO(pmembuf, psize)        \
+  do                                             \
+    {                                            \
+      svn_membuf_t *const _m_b_f_ = (pmembuf);   \
+      const apr_size_t _s_z_ = (psize);          \
+      if (_s_z_ > _m_b_f_->size)                 \
+        memset(_m_b_f_->data, 0, _m_b_f_->size); \
+      else                                       \
+        memset(_m_b_f_->data, 0, _s_z_);         \
+    }                                            \
+  while(0)
+
+#ifndef SVN_DEBUG
+/* In non-debug mode, just use these inlie replacements */
+#define svn_membuf__zero(B) SVN_MEMBUF__ZERO((B))
+#define svn_membuf__nzero(B, S) SVN_MEMBUF__NZERO((B), (S))
+#endif
+
+
 /** Returns the #svn_string_t information contained in the data and
  * len members of @a strbuf. This is effectively a typecast, converting
  * @a strbuf into an #svn_string_t. This first will become invalid and must

Modified: subversion/trunk/subversion/libsvn_subr/string.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_subr/string.c?rev=1412418&r1=1412417&r2=1412418&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_subr/string.c (original)
+++ subversion/trunk/subversion/libsvn_subr/string.c Thu Nov 22 05:20:51 2012
@@ -37,6 +37,53 @@
 #include "svn_private_config.h"
 
 
+
+void
+svn_membuf__create(svn_membuf_t *membuf, apr_size_t size, apr_pool_t *pool)
+{
+  membuf->pool = pool;
+  membuf->size = (size ? APR_ALIGN_DEFAULT(size) : 0);
+  membuf->data = (!membuf->size ? NULL : apr_palloc(pool, membuf->size));
+}
+
+void
+svn_membuf__ensure(svn_membuf_t *membuf, apr_size_t size)
+{
+  if (size > membuf->size)
+    {
+      membuf->size = APR_ALIGN_DEFAULT(size);
+      membuf->data = (!membuf->size ? NULL
+                      : apr_palloc(membuf->pool, membuf->size));
+    }
+}
+
+void
+svn_membuf__resize(svn_membuf_t *membuf, apr_size_t size)
+{
+  const void *const old_data = membuf->data;
+  const apr_size_t old_size = membuf->size;
+
+  svn_membuf__ensure(membuf, size);
+  if (old_data && old_data != membuf->data)
+    memcpy(membuf->data, old_data, old_size);
+}
+
+/* Always provide an out-of-line implementation of svn_membuf__zero */
+#undef svn_membuf__zero
+void
+svn_membuf__zero(svn_membuf_t *membuf)
+{
+  SVN_MEMBUF__ZERO(membuf);
+}
+
+/* Always provide an out-of-line implementation of svn_membuf__nzero */
+#undef svn_membuf__nzero
+void
+svn_membuf__nzero(svn_membuf_t *membuf, apr_size_t size)
+{
+  SVN_MEMBUF__NZERO(membuf, size);
+}
+
 /* Our own realloc, since APR doesn't have one.  Note: this is a
    generic realloc for memory pools, *not* for strings. */
 static void *



Mime
View raw message