Boost logo

Boost-Commit :

From: phil_at_[hidden]
Date: 2008-06-19 12:18:40


Author: pbouchard
Date: 2008-06-19 12:18:39 EDT (Thu, 19 Jun 2008)
New Revision: 46526
URL: http://svn.boost.org/trac/boost/changeset/46526

Log:
Restructured shifted_allocator & cleaned up shifted_ptr.
Added:
   sandbox/shifted_ptr/boost/shifted_allocator.hpp (contents, props changed)
Text files modified:
   sandbox/shifted_ptr/boost/detail/sh_owned_base_nt.hpp | 168 ++++++---------------------------------
   sandbox/shifted_ptr/boost/shifted_ptr.hpp | 23 ++++
   sandbox/shifted_ptr/libs/smart_ptr/example/shifted_ptr_test2.cpp | 3
   3 files changed, 48 insertions(+), 146 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-19 12:18:39 EDT (Thu, 19 Jun 2008)
@@ -67,17 +67,11 @@
     Allocator wrapper tracking allocations.
 */
 
-class pool : public boost::pool<>
+struct pool : boost::pool<>
 {
     typedef std::list< std::pair<char *, char *>, fast_pool_allocator< std::pair<char *, char *> > > lpp;
 
-#ifndef BOOST_SH_DISABLE_THREADS
- thread_specific_ptr<lpp> alloc_, constr_;
-#else
- std::auto_ptr<lpp> alloc_, constr_;
-#endif
 
-public:
     pool() : boost::pool<>(1)
     {
         alloc_.reset(new lpp());
@@ -99,11 +93,15 @@
                 break;
 
         alloc_.get()->erase(i.base(), alloc_.get()->end());
- //constr_.get()->splice(constr_.get()->end(), * alloc_.get(), i.base());
         
         return (owned_base *)(i->first);
     }
     
+ lpp & construct()
+ {
+ return * constr_.get();
+ }
+
     void * allocate(std::size_t s)
     {
         char * p = static_cast<char *>(ordered_malloc(s));
@@ -123,9 +121,15 @@
                 break;
         
         alloc_.get()->erase(i.base(), alloc_.get()->end());
- //constr_.get()->splice(constr_.get()->end(), * alloc_.get(), i.base());
         free(p, s);
     }
+
+private:
+#ifndef BOOST_SH_DISABLE_THREADS
+ thread_specific_ptr<lpp> alloc_, constr_;
+#else
+ std::auto_ptr<lpp> alloc_, constr_;
+#endif
 };
 
 
@@ -142,16 +146,17 @@
 
         intrusive_stack ptrs_;
         intrusive_list inits_;
-
-public:
- intrusive_list::node set_tag_;
- intrusive_list::node init_tag_;
-
+
+protected:
     owned_base()
     {
         inits_.push_back(& init_tag_);
     }
 
+public:
+ intrusive_list::node set_tag_;
+ intrusive_list::node init_tag_;
+
     intrusive_stack * ptrs() { return & ptrs_; }
     intrusive_list * inits() { return & inits_; }
     intrusive_list::node * set_tag() { return & set_tag_; }
@@ -173,11 +178,8 @@
 #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)) \
- { \
- new (element()) data_type(BOOST_PP_REPEAT(n, PARAMETER_DECL, 0)); \
- }
+ template <BOOST_PP_REPEAT(n, TEMPLATE_DECL, 0)> \
+ text(BOOST_PP_REPEAT(n, ARGUMENT_DECL, 0)) : e_(BOOST_PP_REPEAT(n, PARAMETER_DECL, 0)) {}
 
 /**
         Object wrapper.
@@ -188,28 +190,20 @@
     {
         typedef T data_type;
 
- union
- {
- long a_;
- char p_[sizeof(data_type)];
- };
+ data_type e_; // need alignas<long>
         
     public:
         class roofof;
         friend class roofof;
 
- shifted()
- {
- new (element()) data_type();
- }
-
- BOOST_PP_REPEAT_FROM_TO(1, 10, CONSTRUCT_OWNED, shifted)
+ shifted() : e_() {}
 
+ BOOST_PP_REPEAT_FROM_TO(1, 10, CONSTRUCT_OWNED, shifted)
 
- data_type * element() { return static_cast<data_type *>(static_cast<void *>(& p_[0])); }
+ data_type * element() { return & e_; }
 
         virtual ~shifted() { dispose(); }
- virtual void dispose() { dispose(element(), is_array<data_type>()); }
+ virtual void dispose() { /*dispose(element(), is_array<data_type>());*/ }
 
         virtual void * get_deleter( std::type_info const & ti ) { return 0; } // dummy
 
@@ -219,7 +213,7 @@
             shifted<data_type> * p_;
 
         public:
- roofof(data_type * p) : p_(sh::roofof((data_type shifted<data_type>::*)(& shifted<data_type>::p_), p)) {}
+ roofof(data_type * p) : p_(sh::roofof((data_type shifted<data_type>::*)(& shifted<data_type>::e_), p)) {}
             
             operator shifted<data_type> * () const { return p_; }
         };
@@ -233,33 +227,6 @@
         {
             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>());
- }
     };
 
 
@@ -295,87 +262,6 @@
         };
     };
 
-
-/**
- STL compliant allocator.
-*/
-
-//! FIXME
-template <typename T>
- class shifted_allocator
- {
- public:
- typedef size_t size_type;
- typedef ptrdiff_t difference_type;
- typedef T * pointer;
- typedef const T * const_pointer;
- typedef T & reference;
- typedef const T & const_reference;
- typedef T value_type;
-
- template <typename U>
- struct rebind
- {
- typedef shifted_allocator<U> other;
- };
-
- shifted_allocator() throw() {}
- shifted_allocator(const shifted_allocator &) throw() {}
- template <typename U>
- shifted_allocator(const shifted_allocator<U> &) throw() {}
-
- ~shifted_allocator() throw() {}
- pointer address(reference x) const { return & x; }
- const_pointer address(const_reference x) const { return & x; }
-
- pointer allocate(size_type s, const void * = 0)
- {
- void * q = shifted<T>::operator new(s * sizeof(T));
-
- // only T's constructor will be called so take care of the rest
- return static_cast<shifted<T> *>(new (q) owned_base)->element();
- }
-
- void deallocate(pointer p, size_type)
- {
- owned_base * q = shifted<T>::roofof(p);
-
- // T's destructor already called so let's handle ~owned_base()
- q->owned_base::~owned_base();
-
- shifted<T>::operator delete(q);
- }
-
- //! FIXME
- size_type max_size() const throw()
- {
- return size_t(-1) / sizeof(T);
- }
-
- void construct(pointer p, const T & x)
- {
- ::new (p) T(x);
- }
-
- void destroy(pointer p)
- {
- p->~T();
- }
- };
-
-template <typename T>
- inline bool operator == (const shifted_allocator<T> &, const shifted_allocator<T> &)
- {
- return true;
- }
-
-template <typename T>
- inline bool operator != (const shifted_allocator<T> &, const shifted_allocator<T> &)
- {
- return false;
- }
-
-
 } // namespace sh
 
 } // namespace detail

Added: sandbox/shifted_ptr/boost/shifted_allocator.hpp
==============================================================================
--- (empty file)
+++ sandbox/shifted_ptr/boost/shifted_allocator.hpp 2008-06-19 12:18:39 EDT (Thu, 19 Jun 2008)
@@ -0,0 +1,107 @@
+/**
+ @file
+ Boost shifted_allocator.hpp header file.
+
+ @note
+ Copyright (c) 2008 Phil Bouchard <phil_at_[hidden]>.
+
+ Distributed under the Boost Software License, Version 1.0.
+
+ See accompanying file LICENSE_1_0.txt or copy at
+ http://www.boost.org/LICENSE_1_0.txt
+
+ See http://www.boost.org/libs/smart_ptr/doc/index.html for documentation.
+*/
+
+
+#ifndef BOOST_SHIFTED_ALLOCATOR_HPP_INCLUDED
+#define BOOST_SHIFTED_ALLOCATOR_HPP_INCLUDED
+
+// MS compatible compilers support #pragma once
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+#include <boost/shifted_ptr.hpp>
+
+
+namespace boost
+{
+
+/**
+ STL compliant allocator.
+*/
+
+//! FIXME
+template <typename T>
+ class shifted_allocator
+ {
+ public:
+ typedef size_t size_type;
+ typedef ptrdiff_t difference_type;
+ typedef shifted_ptr<T> pointer;
+ typedef const shifted_ptr<T> const_pointer;
+ typedef shifted<T> & reference;
+ typedef const shifted<T> & const_reference;
+ typedef shifted<T> value_type;
+
+ template <typename U>
+ struct rebind
+ {
+ typedef shifted_allocator<U> other;
+ };
+
+ shifted_allocator() throw() {}
+ shifted_allocator(const shifted_allocator &) throw() {}
+ template <typename U>
+ shifted_allocator(const shifted_allocator<U> &) throw() {}
+
+ ~shifted_allocator() throw() {}
+ pointer address(reference x) const { return & x; }
+ const_pointer address(const_reference x) const { return & x; }
+
+ pointer allocate(size_type s, const void * = 0)
+ {
+ return shifted<T>::operator new(s);
+ }
+
+ void deallocate(pointer p, size_type)
+ {
+ shifted<T>::operator delete(p);
+ }
+
+ //! FIXME
+ size_type max_size() const throw()
+ {
+ return size_t(-1) / sizeof(T);
+ }
+
+ void construct(pointer p, const T & x)
+ {
+ ::new (p) T(x);
+ }
+
+ void destroy(pointer p)
+ {
+ p->~T();
+ }
+ };
+
+template <typename T>
+ inline bool operator == (const shifted_allocator<T> &, const shifted_allocator<T> &)
+ {
+ return true;
+ }
+
+template <typename T>
+ inline bool operator != (const shifted_allocator<T> &, const shifted_allocator<T> &)
+ {
+ return false;
+ }
+
+
+} // namespace boost
+
+
+#endif // #ifndef BOOST_SHIFTED_ALLOCATOR_HPP_INCLUDED

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-19 12:18:39 EDT (Thu, 19 Jun 2008)
@@ -163,6 +163,19 @@
     public:
         typedef T element_type;
 
+/*
+ //! FIXME
+ typedef iterator_category;
+ typedef value_type;
+ typedef difference_type;
+ typedef reference;
+ typedef pointer;
+ typedef reference;
+ typedef pointer;
+ typedef reference;
+*/
+ typedef size_t size_type;
+ typedef ptrdiff_t difference_type;
 
         shifted_ptr() : ps_(0)
         {
@@ -278,12 +291,17 @@
         void init(owned_base * p)
         {
             for (intrusive_list::iterator<owned_base, & owned_base::init_tag_> i = p->inits()->begin(), j; j = i, ++ j, i != p->inits()->end(); i = j)
+ //for (pool::lpp::iterator i = owned_base::pool_.construct().begin(); i != owned_base::pool_.construct().end(); i ++)
             {
+ //owned_base * q = reinterpret_cast<owned_base *>(i->first);
+
                 ps_->elements()->push_back(i->set_tag());
                 
- for (intrusive_stack::iterator<shifted_ptr, & shifted_ptr::pn_> m = i->ptrs()->begin(), n; n = m, ++ n, m != i->ptrs()->end(); m = n)
+ for (intrusive_stack::iterator<shifted_ptr, & shifted_ptr::pn_> m = p->ptrs()->begin(), n; n = m, ++ n, m != p->ptrs()->end(); m = n)
                     m->ps_ = ps_;
             }
+
+ owned_base::pool_.construct().clear();
         }
     };
 
@@ -294,9 +312,6 @@
 
 using detail::sh::shifted_ptr;
 using detail::sh::shifted;
-using detail::sh::shifted_allocator;
-using detail::sh::operator ==;
-using detail::sh::operator !=;
 
 } // namespace boost
 

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-19 12:18:39 EDT (Thu, 19 Jun 2008)
@@ -7,6 +7,7 @@
 */
 
 #include <boost/shifted_ptr.hpp>
+#include <boost/shifted_allocator.hpp>
 
 #include <vector>
 #include <iostream>
@@ -64,7 +65,7 @@
     ~vector() { --count; }
     vector(const vector& other) : elements(other.elements) { ++count; }
     std::vector<shifted_ptr<vector> > elements;
- //std::vector<shifted_ptr<vector>, shifted_allocator<vector> > elements; //! FIXME
+ //std::vector<shifted_ptr<vector>, shifted_allocator< shifted_ptr<vector> > > elements; //! FIXME
 };
 
 struct create_type {


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