Boost logo

Boost-Commit :

From: phil_at_[hidden]
Date: 2008-06-04 12:36:38


Author: pbouchard
Date: 2008-06-04 12:36:38 EDT (Wed, 04 Jun 2008)
New Revision: 46125
URL: http://svn.boost.org/trac/boost/changeset/46125

Log:
Finalized pointer by adding generalized allocator.
Text files modified:
   sandbox/shifted_ptr/boost/detail/sh_owned_base_nt.hpp | 101 +++++++++++++++++----------------------
   sandbox/shifted_ptr/boost/shifted_ptr.hpp | 31 +++++-------
   sandbox/shifted_ptr/libs/smart_ptr/example/shifted_ptr_test2.cpp | 39 +++++++++++++++
   3 files changed, 96 insertions(+), 75 deletions(-)

Modified: sandbox/shifted_ptr/boost/detail/sh_owned_base_nt.hpp
==============================================================================
--- sandbox/shifted_ptr/boost/detail/sh_owned_base_nt.hpp (original)
+++ sandbox/shifted_ptr/boost/detail/sh_owned_base_nt.hpp 2008-06-04 12:36:38 EDT (Wed, 04 Jun 2008)
@@ -32,6 +32,7 @@
 #include <boost/thread.hpp>
 #include <boost/thread/tss.hpp>
 #include <boost/pool/pool.hpp>
+#include <boost/numeric/interval.hpp>
 #include <boost/type_traits/is_array.hpp>
 #include <boost/type_traits/remove_extent.hpp>
 #include <boost/type_traits/has_trivial_destructor.hpp>
@@ -55,58 +56,59 @@
 class owned_base;
 
 
-#ifndef BOOST_SH_DISABLE_THREADS
-struct thread_specific_stack : thread_specific_ptr< std::stack<owned_base *> >
-{
- thread_specific_stack()
- {
- reset(new std::stack<owned_base *>());
- }
-};
-#endif
-
-
 /**
- Segment boundaries.
+ Allocator wrapper tracking allocations.
 */
 
-struct segment : std::pair<const void *, const void *>
+struct pool : boost::pool<>,
+#ifndef BOOST_SH_DISABLE_THREADS
+ thread_specific_ptr< std::list< numeric::interval<int> > >
+#else
+ std::auto_ptr< std::list< numeric::interval<int> > >
+#endif
 {
- typedef std::pair<const void *, const void *> base;
+ typedef std::list< numeric::interval<int> > li;
 
- segment(const void * p = (const void *)(std::numeric_limits<unsigned>::max)(), const void * q = (const void *)(std::numeric_limits<unsigned>::min)()) : base((const void *)(p), (const void *)(q))
+ pool() : boost::pool<>(1)
     {
+ reset(new std::list< numeric::interval<int> >());
     }
-
- void include(const void * p)
+
+ owned_base * top(void * p)
     {
- if (p < static_cast<const void *>(first)) first = p;
- if (p > static_cast<const void *>(second)) second = p;
+ li::reverse_iterator i;
+
+ for (i = get()->rbegin(); i != get()->rend(); i ++)
+ if (in((int)(p), * i))
+ {
+ get()->erase(i.base(), get()->end());
+ break;
+ }
+
+ return (owned_base *)(i->lower());
     }
-
- bool is_from(const void * p)
+
+ void * allocate(std::size_t s)
     {
- return ! (static_cast<char const *>(p) < first || static_cast<char const *>(p) > second);
+ void * p = ordered_malloc(s);
+
+ get()->push_back(numeric::interval<int>((int) p, int((char *)(p) + s)));
+
+ return p;
     }
-};
-
-
-/**
- Auto stack boundaries.
-*/
 
-struct stack_segment : segment
-{
- bool is_from(const void * p)
+ void deallocate(void * p)
     {
-#if defined(__GNUC__)
- include(__builtin_frame_address(0));
- include(__builtin_frame_address(5));
-#else
-#error Compiler not yet supported.
-#endif
+ li::reverse_iterator i;
         
- return segment::is_from(p);
+ for (i = get()->rbegin(); i != get()->rend(); i ++)
+ if (in((int)(p), * i))
+ {
+ get()->erase(i.base(), get()->end());
+ break;
+ }
+
+ free(p, i->upper() - i->lower());
     }
 };
 
@@ -134,25 +136,11 @@
     intrusive_list::node * set_tag() { return & set_tag_; }
     intrusive_list::node * init_tag() { return & init_tag_; }
 
- static pool<> pool_;
- static stack_segment stack_;
-
-#ifndef BOOST_SH_DISABLE_THREADS
- static thread_specific_stack last;
-#else
- static std::stack<owned_base *> * last;
-#endif
+ static pool pool_;
 };
 
 
-pool<> owned_base::pool_(1);
-stack_segment owned_base::stack_;
-
-#ifndef BOOST_SH_DISABLE_THREADS
-thread_specific_stack owned_base::last;
-#else
-std::stack<owned_base *> * owned_base::last = new std::stack<owned_base *>;
-#endif
+pool owned_base::pool_;
 
 
 /**
@@ -194,13 +182,12 @@
         
         void * operator new (size_t s)
         {
- return pool_.ordered_malloc(s);
+ return pool_.allocate(s);
         }
         
         void operator delete (void * p)
         {
- //!FIXME
- pool_.free(p, sizeof(owned));
+ pool_.deallocate(p);
         }
 
     private:

Modified: sandbox/shifted_ptr/boost/shifted_ptr.hpp
==============================================================================
--- sandbox/shifted_ptr/boost/shifted_ptr.hpp (original)
+++ sandbox/shifted_ptr/boost/shifted_ptr.hpp 2008-06-04 12:36:38 EDT (Wed, 04 Jun 2008)
@@ -74,11 +74,11 @@
         includes_.push_back(& tag_);
     }
 
- bool release(bool b)
+ bool release()
     {
         set * p = redir();
 
- if (! b && -- p->count_ == 0)
+ if (-- p->count_ == 0)
         {
             for (intrusive_list::iterator<owned_base, & owned_base::set_tag_> i; i = p->elements_.begin(), i != p->elements_.end(); )
             {
@@ -105,17 +105,14 @@
         else return redir_ = redir_->redir();
     }
 
- void redir(set * p, bool b)
+ void redir(set * p)
     {
         if (redir_ != p->redir())
         {
             redir_ = p->redir();
             redir_->includes_.merge(includes_);
             redir_->elements_.merge(elements_);
-
- // tie only if both from stack
- if (! b)
- redir_->count_ += count_;
+ redir_->count_ += count_;
         }
     }
 
@@ -155,7 +152,7 @@
             if (! owned_base::pool_.is_from(this))
                 ps_ = new set();
             else
- owned_base::last->top()->ptrs()->push(& pn_);
+ owned_base::pool_.top(this)->ptrs()->push(& pn_);
         }
 
         template <typename V>
@@ -169,8 +166,8 @@
                 }
                 else
                 {
- owned_base::last->top()->ptrs()->push(& pn_);
- owned_base::last->top()->inits()->merge(* p->inits());
+ owned_base::pool_.top(this)->ptrs()->push(& pn_);
+ owned_base::pool_.top(this)->inits()->merge(* p->inits());
                 }
             }
 
@@ -180,9 +177,9 @@
                 if (! owned_base::pool_.is_from(this))
                     ps_ = new set();
                 else
- owned_base::last->top()->ptrs()->push(& pn_);
+ owned_base::pool_.top(this)->ptrs()->push(& pn_);
 
- ps_->redir(p.ps_, ! owned_base::stack_.is_from(this));
+ ps_->redir(p.ps_);
             }
 
             shifted_ptr(shifted_ptr<T> const & p) : U<T>(p)
@@ -190,9 +187,9 @@
                 if (! owned_base::pool_.is_from(this))
                     ps_ = new set();
                 else
- owned_base::last->top()->ptrs()->push(& pn_);
+ owned_base::pool_.top(this)->ptrs()->push(& pn_);
 
- ps_->redir(p.ps_, ! owned_base::stack_.is_from(this));
+ ps_->redir(p.ps_);
             }
 
         template <typename V>
@@ -213,7 +210,7 @@
                     if (ps_->redir() != p.ps_->redir())
                     {
                         release();
- ps_->redir(p.ps_, ! owned_base::stack_.is_from(this));
+ ps_->redir(p.ps_);
                     }
                     U<T>::operator = (p);
                 }
@@ -240,7 +237,7 @@
         {
             if (! owned_base::pool_.is_from(this))
             {
- if (ps_->release(! owned_base::stack_.is_from(this)))
+ if (ps_->release())
                 {
                     U<T>::po_ = 0;
 
@@ -286,9 +283,7 @@
                         typedef typename remove_const<typename remove_volatile<T>::type>::type unqualified_type; \
                                                                                                                                                                                                                                 \
                         owned<T> * p = new owned<T>(); \
- owned_base::last->push(p); \
                         pointer_type q = reinterpret_cast<pointer_type>(new (const_cast<unqualified_type *>(p->element())) T(BOOST_PP_REPEAT(n, PARAMETER_DECL, 0))); \
- owned_base::last->pop(); \
                                                                                                                                                                                                                                 \
                         return (owned<T> *) (typename owned<T>::roofof) q; \
                 }

Modified: sandbox/shifted_ptr/libs/smart_ptr/example/shifted_ptr_test2.cpp
==============================================================================
--- sandbox/shifted_ptr/libs/smart_ptr/example/shifted_ptr_test2.cpp (original)
+++ sandbox/shifted_ptr/libs/smart_ptr/example/shifted_ptr_test2.cpp 2008-06-04 12:36:38 EDT (Wed, 04 Jun 2008)
@@ -11,6 +11,10 @@
 #include <vector>
 #include <iostream>
 
+#include <boost/mpl/range_c.hpp>
+#include <boost/mpl/for_each.hpp>
+#include <boost/array.hpp>
+
 
 static int count;
 
@@ -59,6 +63,13 @@
     std::vector<shifted_ptr<vector> > elements;
 };
 
+struct create_type {
+ template<class T>
+ void operator()(T) const {
+ new_sh<boost::array<char, T::value> >();
+ }
+};
+
 int main() {
     count = 0;
         {
@@ -78,4 +89,32 @@
         v->elements.push_back(v);
     }
     std::cout << count << std::endl;
+
+ count = 0;
+ {
+ shifted_ptr<vector> v = new_sh<vector>();
+ v->elements.push_back(v);
+ }
+ std::cout << count << std::endl;
+
+ {
+ vector v;
+ v.elements.push_back(new_sh<vector>());
+ }
+ std::cout << count << std::endl;
+
+ count = 0;
+ {
+ shifted_ptr<int> test = new_sh<int>(5);
+ test = test;
+
+ std::cout << "test = " << * test << std::endl;
+ }
+ std::cout << count << std::endl;
+
+ count = 0;
+ for(int i = 0; i < 500; ++i) {
+ boost::mpl::for_each<boost::mpl::range_c<int, 1, 100> >(create_type());
+ }
+ std::cout << count << std::endl;
 }


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