Boost logo

Boost-Commit :

From: phil_at_[hidden]
Date: 2008-06-05 11:14:29


Author: pbouchard
Date: 2008-06-05 11:14:28 EDT (Thu, 05 Jun 2008)
New Revision: 46169
URL: http://svn.boost.org/trac/boost/changeset/46169

Log:
Now uses fast_alloc and "new shifted<>" + cleanups
Text files modified:
   sandbox/shifted_ptr/boost/detail/sh_owned_base_nt.hpp | 127 +++++++++++++++++----------------------
   sandbox/shifted_ptr/boost/detail/shifted_ptr_base.hpp | 18 ++--
   sandbox/shifted_ptr/boost/shifted_ptr.hpp | 48 ++++++--------
   sandbox/shifted_ptr/libs/smart_ptr/example/shifted_ptr_test1.cpp | 16 ++--
   sandbox/shifted_ptr/libs/smart_ptr/example/shifted_ptr_test2.cpp | 16 ++--
   sandbox/shifted_ptr/libs/smart_ptr/example/shifted_ptr_test3.cpp | 16 ++--
   6 files changed, 112 insertions(+), 129 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-05 11:14:28 EDT (Thu, 05 Jun 2008)
@@ -32,10 +32,17 @@
 #include <boost/thread.hpp>
 #include <boost/thread/tss.hpp>
 #include <boost/pool/pool.hpp>
+#include <boost/pool/pool_alloc.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>
+#include <boost/preprocessor/control/expr_if.hpp>
+#include <boost/preprocessor/arithmetic/inc.hpp>
+#include <boost/preprocessor/punctuation/comma_if.hpp>
+#include <boost/preprocessor/repetition/repeat.hpp>
+#include <boost/preprocessor/repetition/repeat_from_to.hpp>
+
 #include <boost/detail/intrusive_list.hpp>
 #include <boost/detail/intrusive_stack.hpp>
 #include <boost/detail/sh_utility.h>
@@ -57,34 +64,38 @@
 
 
 /**
+ Syntax helper.
+*/
+
+typedef std::list< numeric::interval<int>, fast_pool_allocator< numeric::interval<int> > > pool_lii;
+
+
+/**
     Allocator wrapper tracking allocations.
 */
 
 struct pool : boost::pool<>,
 #ifndef BOOST_SH_DISABLE_THREADS
- thread_specific_ptr< std::list< numeric::interval<int> > >
+ thread_specific_ptr<pool_lii>
 #else
- std::auto_ptr< std::list< numeric::interval<int> > >
+ std::auto_ptr<pool_lii>
 #endif
 {
- typedef std::list< numeric::interval<int> > li;
-
     pool() : boost::pool<>(1)
     {
- reset(new std::list< numeric::interval<int> >());
+ reset(new pool_lii());
     }
     
     owned_base * top(void * p)
     {
- li::reverse_iterator i;
+ pool_lii::reverse_iterator i;
         
         for (i = get()->rbegin(); i != get()->rend(); i ++)
             if (in((int)(p), * i))
- {
- get()->erase(i.base(), get()->end());
                 break;
- }
-
+
+ get()->erase(i.base(), get()->end());
+
         return (owned_base *)(i->lower());
     }
     
@@ -97,18 +108,16 @@
         return p;
     }
 
- void deallocate(void * p)
+ void deallocate(void * p, std::size_t s)
     {
- li::reverse_iterator i;
+ pool_lii::reverse_iterator i;
         
         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());
+
+ get()->erase(i.base(), get()->end());
+ free(p, s);
     }
 };
 
@@ -143,41 +152,50 @@
 pool owned_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
+
+#define CONSTRUCT_OWNED(z, n, text) \
+ template <BOOST_PP_REPEAT(n, TEMPLATE_DECL, 0)> \
+ text(BOOST_PP_REPEAT(n, ARGUMENT_DECL, 0)) : elem_(BOOST_PP_REPEAT(n, PARAMETER_DECL, 0)) {}
+
 /**
         Object wrapper.
 */
 
 template <typename T>
- class owned : public owned_base
+ class shifted : public owned_base
     {
         typedef T data_type;
 
- union
- {
- void * vp_;
- char p_[sizeof(data_type)];
- };
-
+ T elem_; // need alignas<long>
+
     public:
         class roofof;
         friend class roofof;
 
- data_type * element() { return static_cast<data_type *>(static_cast<void *>(& p_[0])); }
+ shifted() : elem_() {}
+
+ BOOST_PP_REPEAT_FROM_TO(1, 10, CONSTRUCT_OWNED, shifted)
+
+
+ data_type * element() { return & elem_; }
 
- virtual ~owned() { dispose(); }
- virtual void dispose() { dispose(element(), is_array<data_type>()); }
+ virtual ~shifted() { dispose(); }
+ virtual void dispose() {}
 
         virtual void * get_deleter( std::type_info const & ti ) { return 0; } // dummy
 
     public:
         class roofof
         {
- owned<data_type> * p_;
+ shifted<data_type> * p_;
 
         public:
- roofof(data_type * p) : p_(sh::roofof((data_type owned<data_type>::*)(& owned<data_type>::p_), p)) {}
+ roofof(data_type * p) : p_(sh::roofof((data_type shifted<data_type>::*)(& shifted<data_type>::elem_), p)) {}
             
- operator owned<data_type> * () const { return p_; }
+ operator shifted<data_type> * () const { return p_; }
         };
         
         void * operator new (size_t s)
@@ -187,58 +205,27 @@
         
         void operator delete (void * p)
         {
- pool_.deallocate(p);
+ pool_.deallocate(p, sizeof(shifted));
         }
-
- private:
- template <typename U>
- static void dispose_array(U * p, const false_type &)
- {
- typedef typename remove_extent<U>::type element_type;
-
- for (element_type * i = * p; i != * p + sizeof(data_type) / sizeof(element_type); i ++)
- dispose(i, is_array<element_type>());
- }
-
- template <typename U>
- static void dispose_array(U * p, const true_type &)
- {
- }
-
- template <typename U>
- static void dispose(U * p, const false_type &)
- {
- p->~U();
- }
-
- template <typename U>
- static void dispose(U * p, const true_type &)
- {
- dispose_array(p, has_trivial_destructor<data_type>());
- }
     };
 
 
 template <>
- class owned<void> : public owned_base
+ class shifted<void> : public owned_base
     {
         typedef void data_type;
 
- union
- {
- void * vp_;
- char p_;
- };
+ long elem_;
 
- owned();
+ shifted();
 
     public:
         class roofof;
         friend class roofof;
 
- data_type * element() { return & p_; }
+ data_type * element() { return & elem_; }
 
- virtual ~owned() {}
+ virtual ~shifted() {}
         virtual void dispose() {}
 
         virtual void * get_deleter( std::type_info const & ti ) {}
@@ -246,12 +233,12 @@
     public:
         class roofof
         {
- owned<data_type> * p_;
+ shifted<data_type> * p_;
 
         public:
- roofof(data_type * p) : p_(sh::roofof((char owned<data_type>::*)(& owned<data_type>::p_), static_cast<char *>(p))) {}
+ roofof(data_type * p) : p_(sh::roofof((long shifted<data_type>::*)(& shifted<data_type>::elem_), static_cast<long *>(p))) {}
             
- operator owned<data_type> * () const { return p_; }
+ operator shifted<data_type> * () const { return p_; }
         };
     };
 

Modified: sandbox/shifted_ptr/boost/detail/shifted_ptr_base.hpp
==============================================================================
--- sandbox/shifted_ptr/boost/detail/shifted_ptr_base.hpp (original)
+++ sandbox/shifted_ptr/boost/detail/shifted_ptr_base.hpp 2008-06-05 11:14:28 EDT (Thu, 05 Jun 2008)
@@ -53,7 +53,7 @@
                 }
 
                 template <typename V>
- shifted_ptr_base(detail::sh::owned<V> * p) : po_(p->element())
+ shifted_ptr_base(detail::sh::shifted<V> * p) : po_(p->element())
                         {
                         }
 
@@ -67,7 +67,7 @@
                         }
 
                 template <typename V>
- shifted_ptr_base & operator = (detail::sh::owned<V> * p)
+ shifted_ptr_base & operator = (detail::sh::shifted<V> * p)
                         {
                                 reset(p->element());
 
@@ -134,7 +134,7 @@
         protected:
                 detail::sh::owned_base * header() const
                 {
- detail::sh::owned_base * p = (owned<element_type> *) (typename owned<element_type>::roofof) static_cast<element_type *>(rootof<is_polymorphic<element_type>::value>::get(po_));
+ detail::sh::owned_base * p = (shifted<element_type> *) (typename shifted<element_type>::roofof) static_cast<element_type *>(rootof<is_polymorphic<element_type>::value>::get(po_));
                         return p;
                 }
         };
@@ -156,7 +156,7 @@
                 }
 
                 template <typename V>
- shifted_ptr_base(detail::sh::owned<V> * p) : po_(p->element())
+ shifted_ptr_base(detail::sh::shifted<V> * p) : po_(p->element())
                         {
                         }
 
@@ -170,7 +170,7 @@
                         }
 
                 template <typename V>
- shifted_ptr_base & operator = (detail::sh::owned<V> * p)
+ shifted_ptr_base & operator = (detail::sh::shifted<V> * p)
                         {
                                 reset(p->element());
 
@@ -237,7 +237,7 @@
         protected:
                 detail::sh::owned_base * header() const
                 {
- return (owned<element_type> *) (typename owned<element_type>::roofof) static_cast<element_type *>(rootof<is_polymorphic<element_type>::value>::get(po_));
+ return (shifted<element_type> *) (typename shifted<element_type>::roofof) static_cast<element_type *>(rootof<is_polymorphic<element_type>::value>::get(po_));
                 }
         };
 #endif
@@ -258,7 +258,7 @@
                 }
 
                 template <typename V>
- shifted_ptr_base(detail::sh::owned<V> * p) : po_(p->element())
+ shifted_ptr_base(detail::sh::shifted<V> * p) : po_(p->element())
                         {
                         }
 
@@ -272,7 +272,7 @@
                         }
 
                 template <typename V>
- shifted_ptr_base & operator = (detail::sh::owned<V> * p)
+ shifted_ptr_base & operator = (detail::sh::shifted<V> * p)
                         {
                                 reset(p->element());
 
@@ -329,7 +329,7 @@
         protected:
                 detail::sh::owned_base * header() const
                 {
- return (owned<element_type> *) (owned<element_type>::roofof) static_cast<element_type *>(rootof<is_polymorphic<element_type>::value>::get(po_));
+ return (shifted<element_type> *) (shifted<element_type>::roofof) static_cast<element_type *>(rootof<is_polymorphic<element_type>::value>::get(po_));
                 }
         };
 

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-05 11:14:28 EDT (Thu, 05 Jun 2008)
@@ -29,10 +29,8 @@
 #include <new.h>
 #endif
 
+#include <boost/pool/pool_alloc.hpp>
 #include <boost/type_traits/add_pointer.hpp>
-#include <boost/preprocessor/arithmetic/inc.hpp>
-#include <boost/preprocessor/punctuation/comma_if.hpp>
-#include <boost/preprocessor/repetition/repeat.hpp>
 
 #include <boost/detail/intrusive_list.hpp>
 #include <boost/detail/intrusive_stack.hpp>
@@ -65,6 +63,8 @@
     intrusive_list includes_;
     intrusive_list elements_;
 
+ static fast_pool_allocator<set> pool_;
+
 public:
     intrusive_list::node tag_;
 
@@ -120,9 +120,25 @@
     {
         return & redir()->elements_;
     }
+
+ void * operator new (size_t s)
+ {
+ return pool_.allocate(s);
+ }
+
+ void * operator new (size_t s, set * p)
+ {
+ return p;
+ }
+
+ void operator delete (void * p)
+ {
+ pool_.deallocate(static_cast<set *>(p), sizeof(set));
+ }
 };
 
 
+fast_pool_allocator<set> set::pool_;
 
 
 /**
@@ -156,7 +172,7 @@
         }
 
         template <typename V>
- shifted_ptr(owned<V> * p) : U<T>(p)
+ shifted_ptr(shifted<V> * p) : U<T>(p)
             {
                 if (! owned_base::pool_.is_from(this))
                 {
@@ -193,7 +209,7 @@
             }
 
         template <typename V>
- shifted_ptr & operator = (owned<V> * p)
+ shifted_ptr & operator = (shifted<V> * p)
             {
                 release();
                 init(p);
@@ -271,32 +287,12 @@
     };
 
 
-#define TEMPLATE_DECL(z, n, text) , 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
-
-#define MAKE_SHIFTED_PTR(z, n, text) \
- template <typename T BOOST_PP_REPEAT(n, TEMPLATE_DECL, 0)> \
- inline owned<T> * new_sh(BOOST_PP_REPEAT(n, ARGUMENT_DECL, 0)) \
- { \
- typedef typename add_pointer<T>::type pointer_type; \
- typedef typename remove_const<typename remove_volatile<T>::type>::type unqualified_type; \
- \
- owned<T> * p = new owned<T>(); \
- pointer_type q = reinterpret_cast<pointer_type>(new (const_cast<unqualified_type *>(p->element())) T(BOOST_PP_REPEAT(n, PARAMETER_DECL, 0))); \
- \
- return (owned<T> *) (typename owned<T>::roofof) q; \
- }
-
-BOOST_PP_REPEAT(10, MAKE_SHIFTED_PTR, 0)
-
-
 } // namespace sh
 
 } // namespace detail
 
 using detail::sh::shifted_ptr;
-using detail::sh::new_sh;
+using detail::sh::shifted;
 
 } // namespace boost
 

Modified: sandbox/shifted_ptr/libs/smart_ptr/example/shifted_ptr_test1.cpp
==============================================================================
--- sandbox/shifted_ptr/libs/smart_ptr/example/shifted_ptr_test1.cpp (original)
+++ sandbox/shifted_ptr/libs/smart_ptr/example/shifted_ptr_test1.cpp 2008-06-05 11:14:28 EDT (Thu, 05 Jun 2008)
@@ -34,16 +34,16 @@
 
 int main()
 {
- shifted_ptr<A> p = new_sh<A>(7);
- shifted_ptr<A> q = new_sh<A>(8);
- shifted_ptr<A> r = new_sh<A>(9);
+ shifted_ptr<A> p = new shifted<A>(7);
+ shifted_ptr<A> q = new shifted<A>(8);
+ shifted_ptr<A> r = new shifted<A>(9);
 
- shifted_ptr<void> t = new_sh<A>(10);
- shifted_ptr<int const volatile> v = new_sh<int const volatile>(11);
+ shifted_ptr<void> t = new shifted<A>(10);
+ shifted_ptr<int const volatile> v = new shifted<int const volatile>(11);
 
         p->p = p;
         q = r;
- v = new_sh<int const volatile>(12);
+ v = new shifted<int const volatile>(12);
 
         cout << "p->i = " << p->i << endl;
         cout << "q->i = " << q->i << endl;
@@ -52,8 +52,8 @@
 
         // The following don't work with MSVC:
 #if ! defined(_MSC_VER)
- shifted_ptr<A[5]> s = new_sh<A[5]>();
- shifted_ptr<char[9]> u = new_sh<char[9]>();
+ shifted_ptr<A[5]> s = new shifted<A[5]>();
+ shifted_ptr<char[9]> u = new shifted<char[9]>();
 
         u[4] = 'Z';
 

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-05 11:14:28 EDT (Thu, 05 Jun 2008)
@@ -19,7 +19,7 @@
 static int count;
 
 using boost::shifted_ptr;
-using boost::new_sh;
+using boost::shifted;
 
 struct node {
     node() {
@@ -41,9 +41,9 @@
     }
     void insert() {
         if(front.get() == 0) {
- back = new_sh<node>();
+ back = new shifted<node>();
         } else {
- back->next = new_sh<node>();
+ back->next = new shifted<node>();
             back->next->prior = back;
             back = back->next;
         }
@@ -66,7 +66,7 @@
 struct create_type {
     template<class T>
     void operator()(T) const {
- new_sh<boost::array<char, T::value> >();
+ new shifted<boost::array<char, T::value> >();
     }
 };
 
@@ -85,27 +85,27 @@
     
     count = 0;
     {
- shifted_ptr<vector> v = new_sh<vector>();
+ shifted_ptr<vector> v = new shifted<vector>();
         v->elements.push_back(v);
     }
     std::cout << count << std::endl;
 
     count = 0;
     {
- shifted_ptr<vector> v = new_sh<vector>();
+ shifted_ptr<vector> v = new shifted<vector>();
         v->elements.push_back(v);
     }
     std::cout << count << std::endl;
 
     {
         vector v;
- v.elements.push_back(new_sh<vector>());
+ v.elements.push_back(new shifted<vector>());
     }
     std::cout << count << std::endl;
 
     count = 0;
     {
- shifted_ptr<int> test = new_sh<int>(5);
+ shifted_ptr<int> test = new shifted<int>(5);
         test = test;
         
         std::cout << "test = " << * test << std::endl;

Modified: sandbox/shifted_ptr/libs/smart_ptr/example/shifted_ptr_test3.cpp
==============================================================================
--- sandbox/shifted_ptr/libs/smart_ptr/example/shifted_ptr_test3.cpp (original)
+++ sandbox/shifted_ptr/libs/smart_ptr/example/shifted_ptr_test3.cpp 2008-06-05 11:14:28 EDT (Thu, 05 Jun 2008)
@@ -28,7 +28,7 @@
 static int count;
 
 using boost::shifted_ptr;
-using boost::new_sh;
+using boost::shifted;
 
 struct node {
     node() {
@@ -53,9 +53,9 @@
     }
     void insert() {
         if(front.get() == 0) {
- front = back = new_sh<node>();
+ front = back = new shifted<node>();
         } else {
- back->next = new_sh<node>();
+ back->next = new shifted<node>();
             back->next->prior = back;
             back = back->next;
         }
@@ -75,7 +75,7 @@
 struct create_type {
     template<class T>
     void operator()(T) const {
- new_sh<boost::array<char, T::value> >();
+ new shifted<boost::array<char, T::value> >();
     }
 };
 
@@ -83,7 +83,7 @@
 /*
     count = 0;
     {
- shifted_ptr<vector> v = new_sh<vector>();
+ shifted_ptr<vector> v = new shifted<vector>();
         v->elements.push_back(v);
     }
     BOOST_CHECK_EQUAL(count, 0);
@@ -102,7 +102,7 @@
 /*
     count = 0;
     {
- shifted_ptr<int> test = new_sh<int>(5);
+ shifted_ptr<int> test = new shifted<int>(5);
         test = test;
         
         BOOST_CHECK_NE(test.get(), static_cast<int*>(0));
@@ -116,14 +116,14 @@
 /*
     count = 0;
     {
- shifted_ptr<vector> v = new_sh<vector>();
+ shifted_ptr<vector> v = new shifted<vector>();
         v->elements.push_back(v);
     }
     BOOST_CHECK_EQUAL(count, 0);
 */
     {
         vector v;
- v.elements.push_back(new_sh<vector>());
+ v.elements.push_back(new shifted<vector>());
     }
     BOOST_CHECK_EQUAL(count, 0);
 }


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