Author: stefan2
Date: Thu Apr 12 20:02:36 2012
New Revision: 1325478

URL: http://svn.apache.org/viewvc?rev=1325478&view=rev
Log:
Introduce a new API function to create and initialize a new
APR pool allocator. Make its thread-safety properties
explicit in the docstring.

Will be used by the next commit to replace boilerplate code.

* subversion/include/svn_pools.h
  (svn_pool_create_allocator): declare new API function
* subversion/libsvn_subr/pool.c
  (svn_pool_create_allocator): declare new API function

Modified:
    subversion/trunk/subversion/include/svn_pools.h
    subversion/trunk/subversion/libsvn_subr/pool.c

Modified: subversion/trunk/subversion/include/svn_pools.h
URL: 
http://svn.apache.org/viewvc/subversion/trunk/subversion/include/svn_pools.h?rev=1325478&r1=1325477&r2=1325478&view=diff
==============================================================================
--- subversion/trunk/subversion/include/svn_pools.h (original)
+++ subversion/trunk/subversion/include/svn_pools.h Thu Apr 12 20:02:36 2012
@@ -30,7 +30,7 @@
 #ifndef SVN_POOLS_H
 #define SVN_POOLS_H
 
-#include <apr_pools.h>
+#include "svn_types.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -86,6 +86,23 @@ svn_pool_create_ex_debug(pool, allocator
  */
 #define svn_pool_destroy apr_pool_destroy
 
+/** Return a new allocator.  This function limits the unused memory in the
+ * new allocator to @ref SVN_ALLOCATOR_RECOMMENDED_MAX_FREE and ensures
+ * proper synchronization if the allocator is used by multiple threads.
+ *
+ * If your application uses multiple threads, creating a separate allocator
+ * for each of these threads may not be feasible. Set the @a thread_safe
+ * parameter to @c TRUE in that case.  Pools will still not thread-safe, i.e.
+ * access to them may require explicit serialization.  Set the parameter to
+ * @c FALSE, otherwise, to maximize performance. 
+ * 
+ * To access the owner pool, which can also serve as the root pool for your
+ * sub-pools, call @c apr_allocator_get_owner().
+ *
+ * @since: New in 1.8
+ */
+apr_allocator_t *
+svn_pool_create_allocator(svn_boolean_t thread_safe);
 
 #ifdef __cplusplus
 }

Modified: subversion/trunk/subversion/libsvn_subr/pool.c
URL: 
http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_subr/pool.c?rev=1325478&r1=1325477&r2=1325478&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_subr/pool.c (original)
+++ subversion/trunk/subversion/libsvn_subr/pool.c Thu Apr 12 20:02:36 2012
@@ -28,6 +28,7 @@
 
 #include <apr_general.h>
 #include <apr_pools.h>
+#include <apr_thread_mutex.h>
 
 #include "svn_pools.h"
 
@@ -97,3 +98,44 @@ svn_pool_create_ex(apr_pool_t *pool, apr
 }
 
 #endif /* APR_POOL_DEBUG */
+
+apr_allocator_t *
+svn_pool_create_allocator(svn_boolean_t thread_safe)
+{
+  apr_allocator_t *allocator;
+  apr_pool_t *pool;
+
+  /* create the allocator and limit it's internal free list to keep
+   * memory usage in check */
+  
+  if (apr_allocator_create(&allocator))
+    abort_on_pool_failure(EXIT_FAILURE);
+
+  apr_allocator_max_free_set(allocator, SVN_ALLOCATOR_RECOMMENDED_MAX_FREE);
+
+  /* create the root pool */
+
+  pool = svn_pool_create_ex(NULL, allocator);
+  apr_allocator_owner_set(allocator, pool);
+
+#if APR_POOL_DEBUG
+  apr_pool_tag (pool, "svn root pool");
+#endif
+
+  /* By default, allocators are *not* thread-safe. We must provide a mutex
+   * if we want thread-safety for that mutex. */
+ 
+#if APR_HAS_THREADS
+  if (thread_safe)
+    {
+      apr_thread_mutex_t *mutex;
+      apr_thread_mutex_create (&mutex, APR_THREAD_MUTEX_DEFAULT, pool);
+      apr_allocator_mutex_set (allocator, mutex);
+    }
+#endif
+
+  /* better safe than sorry */
+  SVN_ERR_ASSERT_NO_RETURN(allocator != NULL);
+  
+  return allocator;
+}


Reply via email to