From d9333e537579bed09ea1cfd0be08f7ed6c27f6a4 Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Wed, 12 Feb 2014 19:19:02 -0800 Subject: [PATCH] Simplify array_allocator; update documentation --- .../smart_ptr/detail/array_allocator.hpp | 29 +++++++++---------- .../smart_ptr/detail/array_count_impl.hpp | 2 +- make_shared_array.html | 5 ++-- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/include/boost/smart_ptr/detail/array_allocator.hpp b/include/boost/smart_ptr/detail/array_allocator.hpp index 2289084..8a408e2 100644 --- a/include/boost/smart_ptr/detail/array_allocator.hpp +++ b/include/boost/smart_ptr/detail/array_allocator.hpp @@ -103,14 +103,14 @@ namespace boost { object(0) { } - template - as_allocator(const as_allocator& other, U* memory) + as_allocator(const as_allocator& other, void* memory, + std::size_t offset) : as_size_base(other) { enum { M = boost::alignment_of::value }; - std::size_t n1 = sizeof(U) + M - 1; - char* p1 = reinterpret_cast(memory) + n1; + std::size_t n1 = offset + M - 1; + char* p1 = static_cast(memory) + n1; while (std::size_t(p1) % M != 0) { p1--; } @@ -138,11 +138,11 @@ namespace boost { return ya.max_size(); } - pointer allocate(size_type, const void* value = 0) { + pointer allocate(size_type count, const void* value = 0) { enum { M = boost::alignment_of::value }; - std::size_t n1 = sizeof(value_type) + M - 1; + std::size_t n1 = count * sizeof(value_type) + M - 1; std::size_t n2 = size * sizeof(type); CA ca(*this); #if !defined(BOOST_NO_CXX11_ALLOCATOR) @@ -153,11 +153,11 @@ namespace boost { return static_cast(p1); } - void deallocate(pointer memory, size_type) { + void deallocate(pointer memory, size_type count) { enum { M = boost::alignment_of::value }; - std::size_t n1 = sizeof(value_type) + M - 1; + std::size_t n1 = count * sizeof(value_type) + M - 1; char* p1 = reinterpret_cast(memory); CA ca(*this); #if !defined(BOOST_NO_CXX11_ALLOCATOR) @@ -172,7 +172,6 @@ namespace boost { YA ya(*this); #if !defined(BOOST_NO_CXX11_ALLOCATOR) YT::construct(ya, memory, value); - #else ya.construct(memory, value); #endif @@ -287,14 +286,14 @@ namespace boost { object(0) { } - template - ms_allocator(const ms_allocator& other, U* memory) + ms_allocator(const ms_allocator& other, void* memory, + std::size_t offset) : ms_size_base(other) { enum { M = boost::alignment_of::value }; - std::size_t n1 = sizeof(U) + M - 1; - char* p1 = reinterpret_cast(memory) + n1; + std::size_t n1 = offset + M - 1; + char* p1 = static_cast(memory) + n1; while (std::size_t(p1) % M != 0) { p1--; } @@ -322,11 +321,11 @@ namespace boost { return N; } - pointer allocate(size_type, const void* = 0) { + pointer allocate(size_type count, const void* = 0) { enum { M = boost::alignment_of::value }; - std::size_t n1 = sizeof(value_type) + M - 1; + std::size_t n1 = count * sizeof(value_type) + M - 1; std::size_t n2 = size * sizeof(type); void* p1 = ::operator new(n1 + n2); return static_cast(p1); diff --git a/include/boost/smart_ptr/detail/array_count_impl.hpp b/include/boost/smart_ptr/detail/array_count_impl.hpp index da2d9dd..6a6a2a9 100644 --- a/include/boost/smart_ptr/detail/array_count_impl.hpp +++ b/include/boost/smart_ptr/detail/array_count_impl.hpp @@ -21,7 +21,7 @@ namespace boost { typedef sp_counted_impl_pda Y; public: sp_counted_impl_pda(P, const D&, const A& allocator_) - : allocator(allocator_, this) { + : allocator(allocator_, this, sizeof(Y)) { } virtual void dispose() { diff --git a/make_shared_array.html b/make_shared_array.html index 6c323b8..56341cd 100644 --- a/make_shared_array.html +++ b/make_shared_array.html @@ -252,8 +252,9 @@ boost::shared_ptr<int[4][2]> a2 = boost::make_shared_noinit<int[4][2]&g

History

February 2014. Glen Fernandes updated overloads of make_shared and allocate_shared to conform to the specification in C++ standard paper - N3870 including resolving C++ standard library - defect report 2070.

+ N3870, including resolving C++ standard library + defect report 2070, and reduced the spatial overhead of the internal + bookkeeping structures.

November 2012. Glen Fernandes contributed implementations of make_shared and allocate_shared for arrays.

References