Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r71756 - in sandbox/block_ptr/boost: . detail
From: phil_at_[hidden]
Date: 2011-05-06 02:46:41


Author: pbouchard
Date: 2011-05-06 02:46:40 EDT (Fri, 06 May 2011)
New Revision: 71756
URL: http://svn.boost.org/trac/boost/changeset/71756

Log:
* Made pool a singleton
Text files modified:
   sandbox/block_ptr/boost/block_ptr.hpp | 34 +++++++++++-----
   sandbox/block_ptr/boost/detail/block_base.hpp | 78 ++++++++++++++++++++--------------------
   2 files changed, 62 insertions(+), 50 deletions(-)

Modified: sandbox/block_ptr/boost/block_ptr.hpp
==============================================================================
--- sandbox/block_ptr/boost/block_ptr.hpp (original)
+++ sandbox/block_ptr/boost/block_ptr.hpp 2011-05-06 02:46:40 EDT (Fri, 06 May 2011)
@@ -77,6 +77,10 @@
     intrusive_list includes_; /**< List of all sets of an union. */
     intrusive_list elements_; /**< List of all pointee objects belonging to a @c block_header . */
 
+#ifndef BOOST_DISABLE_THREADS
+ static mutex pool_mutex_;
+#endif
+
     static fast_pool_allocator<block_header> pool_;/**< Pool where all sets are allocated. */
 
         /**
@@ -172,6 +176,9 @@
         
     void * operator new (size_t s)
     {
+#ifndef BOOST_DISABLE_THREADS
+ mutex::scoped_lock scoped_lock(pool_mutex_);
+#endif
         return pool_.allocate(s);
     }
     
@@ -198,11 +205,16 @@
         
     void operator delete (void * p)
     {
+#ifndef BOOST_DISABLE_THREADS
+ mutex::scoped_lock scoped_lock(pool_mutex_);
+#endif
         pool_.deallocate(static_cast<block_header *>(p), sizeof(block_header));
     }
 };
 
-
+#ifndef BOOST_DISABLE_THREADS
+mutex block_header::pool_mutex_;
+#endif
 fast_pool_allocator<block_header> block_header::pool_;
 
 
@@ -253,7 +265,7 @@
         template <typename V>
             block_ptr(block<V> * p) : base(p)
             {
- if (! block_base::pool_.is_from(this))
+ if (! pool::is_from(this))
                 {
                     ps_ = new block_header();
 
@@ -261,8 +273,8 @@
                 }
                 else
                 {
- block_base::pool_.top(this)->ptrs_.push(& pn_);
- block_base::pool_.top(this)->inits_.merge(p->inits_);
+ pool::top(this)->ptrs_.push(& pn_);
+ pool::top(this)->inits_.merge(p->inits_);
                 }
             }
 
@@ -301,10 +313,10 @@
                 
         block_ptr() : ps_(0)
         {
- if (! block_base::pool_.is_from(this))
+ if (! pool::is_from(this))
                 ps_ = new block_header();
             else
- block_base::pool_.top(this)->ptrs_.push(& pn_);
+ pool::top(this)->ptrs_.push(& pn_);
         }
 
                 
@@ -317,10 +329,10 @@
         template <typename V>
             block_ptr(block_ptr<V> const & p) : base(p)
             {
- if (! block_base::pool_.is_from(this))
+ if (! pool::is_from(this))
                     ps_ = new block_header();
                 else
- block_base::pool_.top(this)->ptrs_.push(& pn_);
+ pool::top(this)->ptrs_.push(& pn_);
 
                 ps_->redir(p.ps_);
             }
@@ -334,10 +346,10 @@
 
                         block_ptr(block_ptr<T> const & p) : base(p)
             {
- if (! block_base::pool_.is_from(this))
+ if (! pool::is_from(this))
                     ps_ = new block_header();
                 else
- block_base::pool_.top(this)->ptrs_.push(& pn_);
+ pool::top(this)->ptrs_.push(& pn_);
                                 
                 ps_->redir(p.ps_);
             }
@@ -432,7 +444,7 @@
         {
             base::reset();
             
- if (! block_base::pool_.is_from(this))
+ if (! pool::is_from(this))
                 if (ps_->release())
                     if (! d)
                         new (ps_) block_header();

Modified: sandbox/block_ptr/boost/detail/block_base.hpp
==============================================================================
--- sandbox/block_ptr/boost/detail/block_base.hpp (original)
+++ sandbox/block_ptr/boost/detail/block_base.hpp 2011-05-06 02:46:40 EDT (Fri, 06 May 2011)
@@ -65,26 +65,22 @@
         Pool where all pointee objects are allocated and tracks memory blocks for later enlisting & marking the @c block_header the pointee object belongs to.
 */
 
-struct pool : boost::pool<>
+struct pool
 {
- typedef std::list< numeric::interval<long>, fast_pool_allocator< numeric::interval<long> > > pool_lii; /**< Syntax helper. */
-
 #ifndef BOOST_DISABLE_THREADS
- thread_specific_ptr<pool_lii> plii_; /**< Thread specific list of memory boundaries. */
-#else
- std::auto_ptr<pool_lii> plii_; /**< List of memory boundaries. */
+ static mutex pool_mutex_;
 #endif
 
+ typedef std::list< numeric::interval<long>, fast_pool_allocator< numeric::interval<long> > > pool_lii; /**< Syntax helper. */
 
- /**
- Initialization of a pool instance.
- */
-
- pool() : boost::pool<>(1)
- {
- plii_.reset(new pool_lii());
- }
-
+ static pool_lii plii_; /**< List of memory boundaries. */
+
+ typedef boost::singleton_pool<pool, sizeof(char)> pool_t;
+
+ static bool is_from(void * p)
+ {
+ return pool_t::is_from(p);
+ }
         
         /**
                 Tracks the memory boundaries where a pointer belongs to. Also gets rid of the boundaries that were allocated before the pointer was allocated.
@@ -93,20 +89,21 @@
                 @return Pointer to the pointee object where @c p belongs to.
         */
         
- block_base * top(void * p)
+ static block_base * top(void * p)
     {
- if (plii_.get() == 0)
- plii_.reset(new pool_lii());
+#ifndef BOOST_DISABLE_THREADS
+ mutex::scoped_lock scoped_lock(pool_mutex_);
+#endif
 
         pool_lii::reverse_iterator i;
         
- for (i = plii_->rbegin(); i != plii_->rend(); i ++)
+ for (i = plii_.rbegin(); i != plii_.rend(); i ++)
             if (in((long)(p), * i))
                 break;
 
- plii_->erase(i.base(), plii_->end());
+ plii_.erase(i.base(), plii_.end());
         
- return (block_base *)(plii_->rbegin()->lower());
+ return (block_base *)(plii_.rbegin()->lower());
     }
     
         
@@ -117,14 +114,15 @@
                 @return Address of the newly allocated block.
         */
         
- void * allocate(std::size_t s)
+ static void * allocate(std::size_t s)
     {
- if (plii_.get() == 0)
- plii_.reset(new pool_lii());
+#ifndef BOOST_DISABLE_THREADS
+ mutex::scoped_lock scoped_lock(pool_mutex_);
+#endif
 
- void * p = ordered_malloc(s);
+ void * p = pool_t::ordered_malloc(s);
         
- plii_->push_back(numeric::interval<long>((long) p, long((char *)(p) + s)));
+ plii_.push_back(numeric::interval<long>((long) p, long((char *)(p) + s)));
         
         return p;
     }
@@ -137,22 +135,29 @@
                 @param s Size of the memory block.
         */
         
- void deallocate(void * p, std::size_t s)
+ static void deallocate(void * p, std::size_t s)
     {
- if (plii_.get() == 0)
- plii_.reset(new pool_lii());
+#ifndef BOOST_DISABLE_THREADS
+ mutex::scoped_lock scoped_lock(pool_mutex_);
+#endif
 
         pool_lii::reverse_iterator i;
         
- for (i = plii_->rbegin(); i != plii_->rend(); i ++)
+ for (i = plii_.rbegin(); i != plii_.rend(); i ++)
             if (in((long)(p), * i))
                 break;
 
- plii_->erase(i.base(), plii_->end());
- ordered_free(p, s);
+ plii_.erase(i.base(), plii_.end());
+ pool_t::ordered_free(p, s);
     }
 };
 
+pool::pool_lii pool::plii_;
+
+#ifndef BOOST_DISABLE_THREADS
+mutex pool::pool_mutex_;
+#endif
+
 
 /**
         Root class of all pointee objects.
@@ -175,17 +180,12 @@
         inits_.push_back(& init_tag_);
     }
 
- static pool pool_; /**< Pool where all pointee objects are allocated from. */
-
 protected:
     virtual void dispose() {} /**< dublocky */
     virtual void * get_deleter( std::type_info const & ti ) { return 0; } /**< dublocky */
 };
 
 
-pool block_base::pool_;
-
-
 #define TEMPLATE_DECL(z, n, text) BOOST_PP_COMMA_IF(n) typename T ## n
 #define ARGUMENT_DECL(z, n, text) BOOST_PP_COMMA_IF(n) T ## n const & t ## n
 #define PARAMETER_DECL(z, n, text) BOOST_PP_COMMA_IF(n) t ## n
@@ -263,7 +263,7 @@
                 
         void * operator new (size_t s)
         {
- return pool_.allocate(s);
+ return pool::allocate(s);
         }
         
 
@@ -275,7 +275,7 @@
                 
         void operator delete (void * p)
         {
- pool_.deallocate(p, sizeof(block));
+ pool::deallocate(p, sizeof(block));
         }
     };
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk