From 574ccee478b9da9457cdf0e476b8df6eb584b580 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Mon, 16 Jul 2012 06:48:11 +0000 Subject: Minor memory management, performance, and tracing improvements. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1361917 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/kernel/gc.hpp | 101 ++++++++++++++++++++++++++++++++------------ 1 file changed, 75 insertions(+), 26 deletions(-) (limited to 'sca-cpp/trunk/kernel/gc.hpp') diff --git a/sca-cpp/trunk/kernel/gc.hpp b/sca-cpp/trunk/kernel/gc.hpp index 260688f4ff..32ad8160cc 100644 --- a/sca-cpp/trunk/kernel/gc.hpp +++ b/sca-cpp/trunk/kernel/gc.hpp @@ -149,21 +149,13 @@ public: private: friend apr_pool_t* pool(const gc_pool& pool); friend class gc_global_pool_t; + friend class gc_child_pool; + friend class gc_local_pool; friend class gc_scoped_pool; apr_pool_t* apr_pool; }; -/** - * Make a new APR pool. - */ -apr_pool_t* mkpool() { - apr_pool_t* p = NULL; - apr_pool_create(&p, NULL); - assertOrFail(p != NULL); - return p; -} - /** * Return the APR pool used by a gc_pool. */ @@ -171,16 +163,6 @@ apr_pool_t* pool(const gc_pool& pool) { return pool.apr_pool; } -/** - * Destroy a memory pool. - */ -const bool destroy(const gc_pool& p) { - if (pool(p) == NULL) - return false; - apr_pool_destroy(pool(p)); - return true; -} - /** * Maintain a stack of memory pools. */ @@ -244,19 +226,77 @@ apr_pool_t* gc_current_pool() { } /** - * A memory pool scope, used to setup a scope in which a particular pool - * will be used for all allocations. + * A child memory pool, which will be destroyed when its parent pool is destroyed. + */ +class gc_child_pool : public gc_pool { +public: + + gc_child_pool() : gc_pool(NULL), owner(true) { + apr_pool_create(&apr_pool, gc_current_pool()); + assertOrFail(apr_pool != NULL); + } + + gc_child_pool(const gc_child_pool& p) : gc_pool(p.apr_pool), owner(false) { + } + + const gc_child_pool& operator=(const gc_child_pool& p) { + if(this == &p) + return *this; + apr_pool = p.apr_pool; + owner = false; + return *this; + } + + +private: + bool owner; +}; + +/** + * A local pool scope, which will be destroyed when exiting the current scope. + */ +class gc_local_pool : public gc_pool { +public: + + gc_local_pool() : gc_pool(NULL), owner(true) { + apr_pool_create(&apr_pool, gc_current_pool()); + assertOrFail(apr_pool != NULL); + } + + ~gc_local_pool() { + if (owner) + apr_pool_destroy(apr_pool); + } + + gc_local_pool(const gc_local_pool& p) : gc_pool(p.apr_pool), owner(false) { + } + + const gc_local_pool& operator=(const gc_local_pool& p) { + if(this == &p) + return *this; + apr_pool = p.apr_pool; + owner = false; + return *this; + } + +private: + bool owner; +}; + +/** + * A memory pool scope, used to setup a scope in which a particular pool will be + * used for all allocations. Will be destroyed when existing the current scope. */ class gc_scoped_pool : public gc_pool { public: gc_scoped_pool() : gc_pool(NULL), prev(gc_current_pool()), owner(true) { - apr_pool_create(&apr_pool, NULL); + apr_pool_create(&apr_pool, prev); assertOrFail(apr_pool != NULL); gc_push_pool(apr_pool); } - gc_scoped_pool(apr_pool_t* pool) : gc_pool(pool), prev(gc_current_pool()), owner(false) { + gc_scoped_pool(apr_pool_t* p) : gc_pool(p), prev(gc_current_pool()), owner(false) { gc_push_pool(apr_pool); } @@ -266,10 +306,19 @@ public: gc_pop_pool(prev); } -private: - gc_scoped_pool(const gc_scoped_pool& pool) : gc_pool(pool.apr_pool), prev(NULL), owner(false) { + gc_scoped_pool(const gc_scoped_pool& p) : gc_pool(p.apr_pool), prev(p.prev), owner(false) { } + const gc_scoped_pool& operator=(const gc_scoped_pool& p) { + if(this == &p) + return *this; + apr_pool = p.apr_pool; + prev = p.prev; + owner = false; + return *this; + } + +private: apr_pool_t* prev; bool owner; }; -- cgit v1.2.3