Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r54460 - in sandbox/monotonic/boost: heterogenous monotonic
From: christian.schladetsch_at_[hidden]
Date: 2009-06-28 05:18:39


Author: cschladetsch
Date: 2009-06-28 05:18:38 EDT (Sun, 28 Jun 2009)
New Revision: 54460
URL: http://svn.boost.org/trac/boost/changeset/54460

Log:
added alignment parameter to deallocate, so it can adjust back any padding added in allocate

Text files modified:
   sandbox/monotonic/boost/heterogenous/abstract_allocator.hpp | 11 ++++++++++-
   sandbox/monotonic/boost/heterogenous/base.hpp | 2 +-
   sandbox/monotonic/boost/heterogenous/make_clone_allocator.hpp | 20 +++++++++++++++-----
   sandbox/monotonic/boost/monotonic/allocator_base.hpp | 4 ++--
   4 files changed, 28 insertions(+), 9 deletions(-)

Modified: sandbox/monotonic/boost/heterogenous/abstract_allocator.hpp
==============================================================================
--- sandbox/monotonic/boost/heterogenous/abstract_allocator.hpp (original)
+++ sandbox/monotonic/boost/heterogenous/abstract_allocator.hpp 2009-06-28 05:18:38 EDT (Sun, 28 Jun 2009)
@@ -19,7 +19,16 @@
 
                         virtual pointer allocate_bytes(size_t num_bytes, size_t alignment) = 0;
 
- virtual void deallocate_bytes(pointer) = 0;
+ virtual void deallocate_bytes(pointer, size_t alignment) = 0;
+
+ static size_t calc_padding(pointer ptr, size_t alignment)
+ {
+ ptrdiff_t index = ptr - pointer(0);
+ size_t extra = index & (alignment - 1); // assumes 2^n alignment!
+ if (extra > 0)
+ extra = alignment - extra;
+ return extra;
+ }
                 };
 
         } // namespace heterogenous

Modified: sandbox/monotonic/boost/heterogenous/base.hpp
==============================================================================
--- sandbox/monotonic/boost/heterogenous/base.hpp (original)
+++ sandbox/monotonic/boost/heterogenous/base.hpp 2009-06-28 05:18:38 EDT (Sun, 28 Jun 2009)
@@ -39,7 +39,7 @@
                         void deallocate(common_base *object, abstract_allocator &alloc) const
                         {
                                 Derived *ptr = static_cast<Derived *>(object);
- alloc.deallocate_bytes(reinterpret_cast<abstract_allocator::pointer>(ptr));
+ alloc.deallocate_bytes(reinterpret_cast<abstract_allocator::pointer>(ptr), alignment);
                         }
 
                         virtual base<Derived> *create(abstract_allocator &alloc) const

Modified: sandbox/monotonic/boost/heterogenous/make_clone_allocator.hpp
==============================================================================
--- sandbox/monotonic/boost/heterogenous/make_clone_allocator.hpp (original)
+++ sandbox/monotonic/boost/heterogenous/make_clone_allocator.hpp 2009-06-28 05:18:38 EDT (Sun, 28 Jun 2009)
@@ -21,18 +21,29 @@
                         {
                                 typedef typename Alloc::template rebind<char>::other CharAlloc;
 
+ clone_allocator() { }
+ clone_allocator(Alloc &a) : Alloc(a) { }
+
                                 abstract_allocator::pointer allocate_bytes(size_t num_bytes, size_t alignment)
                                 {
                                         CharAlloc alloc(*this);
- // TODO: alignment; this is done already for monotonic, copy that here
- return alloc.allocate(num_bytes);
+ size_t required = num_bytes;
+ if (num_bytes < alignment)
+ required = alignment;
+ else
+ required = num_bytes + alignment; // TODO: don't really need this much
+ abstract_allocator::pointer ptr = alloc.allocate(required);
+ size_t extra = calc_padding(ptr, alignment);
+ return ptr + extra;
                                 }
 
- void deallocate_bytes(abstract_allocator::pointer ptr)
+ void deallocate_bytes(abstract_allocator::pointer ptr, size_t alignment)
                                 {
                                         CharAlloc alloc(*this);
- alloc.deallocate(ptr, 1);
+ size_t extra = calc_padding(ptr, alignment);
+ alloc.deallocate(ptr - extra, 1);
                                 }
+
                         };
 
                         template <class Alloc, bool>
@@ -44,7 +55,6 @@
                         template <class Alloc>
                         struct make_clone_allocator<Alloc, true>
                         {
- BOOST_STATIC_ASSERT(0);
                                 typedef Alloc type;
                         };
                 }

Modified: sandbox/monotonic/boost/monotonic/allocator_base.hpp
==============================================================================
--- sandbox/monotonic/boost/monotonic/allocator_base.hpp (original)
+++ sandbox/monotonic/boost/monotonic/allocator_base.hpp 2009-06-28 05:18:38 EDT (Sun, 28 Jun 2009)
@@ -28,7 +28,7 @@
         {
                 /// common to other monotonic allocators for type T of type Derived
                 template <class T, class Derived>
- struct allocator_base// : heterogenous::abstract_allocator
+ struct allocator_base : heterogenous::abstract_allocator
                 {
                         typedef size_t size_type;
                         typedef ptrdiff_t difference_type;
@@ -54,7 +54,7 @@
                                 return reinterpret_cast<heterogenous::abstract_allocator::pointer>(ptr);
                         }
 
- virtual void deallocate_bytes(char * /*bytes*/)
+ virtual void deallocate_bytes(char * /*bytes*/, size_t /*alignment*/ )
                         {
                                 // do nothing
                         }


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