dev  

Re: Changing the order of cleanup for some core objects

Bojan Smojver
Mon, 21 Jul 2008 05:07:03 -0700

On Mon, 2008-07-21 at 21:16 +1000, Bojan Smojver wrote:

> How about we have apr_pool_destroy_safe(). We give it two arguments:

For example... Don't mind the details, especially locks - they are most
likely all screwed up.

-- 
Bojan
Index: memory/unix/apr_pools.c
===================================================================
--- memory/unix/apr_pools.c	(revision 678146)
+++ memory/unix/apr_pools.c	(working copy)
@@ -825,6 +825,62 @@
     }
 }
 
+static apr_status_t find_pool(apr_pool_t *root, apr_pool_t *pool)
+{
+    apr_status_t rv;
+    apr_pool_t *p;
+
+    for (p = root; p; p = p->sibling) {
+        if (p == pool) {
+            return APR_SUCCESS;
+        }
+        else {
+#if APR_HAS_THREADS
+            apr_thread_mutex_t *mutex;
+
+            if ((mutex = apr_allocator_mutex_get(p->allocator)) != NULL)
+                apr_thread_mutex_lock(mutex);
+#endif /* APR_HAS_THREADS */
+
+            rv = find_pool (p->child, pool);
+
+#if APR_HAS_THREADS
+            if (mutex)
+                apr_thread_mutex_unlock(mutex);
+#endif /* APR_HAS_THREADS */
+
+            if (rv == APR_SUCCESS) {
+                return rv;
+            }
+        }
+    }
+
+    return APR_EGENERAL;
+}
+
+APR_DECLARE(void) apr_pool_destroy_safe(apr_pool_t *pool,apr_pool_t *root)
+{
+    apr_status_t rv;
+#if APR_HAS_THREADS
+    apr_thread_mutex_t *mutex;
+
+    if (root == NULL)
+        root = global_pool;
+
+    if ((mutex = apr_allocator_mutex_get(root->allocator)) != NULL)
+        apr_thread_mutex_lock(mutex);
+#endif /* APR_HAS_THREADS */
+
+    rv = find_pool(root->child, pool);
+    if (rv == APR_SUCCESS)
+        apr_pool_destroy(pool);
+
+#if APR_HAS_THREADS
+    if (mutex)
+        apr_thread_mutex_unlock(mutex);
+#endif /* APR_HAS_THREADS */
+}
+
 APR_DECLARE(apr_status_t) apr_pool_create_ex(apr_pool_t **newpool,
                                              apr_pool_t *parent,
                                              apr_abortfunc_t abort_fn,