From 8c9e8b55566cb1ebd1d77d906176aa3b5d6eda35 Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Wed, 12 Feb 2014 22:12:04 -0800 Subject: [PATCH] Further simplification of ms_allocator --- .../boost/smart_ptr/allocate_shared_array.hpp | 36 +++---- .../smart_ptr/detail/array_allocator.hpp | 94 +++++++++---------- .../smart_ptr/detail/array_count_impl.hpp | 2 +- include/boost/smart_ptr/make_shared_array.hpp | 36 +++---- 4 files changed, 81 insertions(+), 87 deletions(-) diff --git a/include/boost/smart_ptr/allocate_shared_array.hpp b/include/boost/smart_ptr/allocate_shared_array.hpp index c91d4da..bdd8b1f 100644 --- a/include/boost/smart_ptr/allocate_shared_array.hpp +++ b/include/boost/smart_ptr/allocate_shared_array.hpp @@ -25,17 +25,17 @@ namespace boost { T1* p1 = 0; T2* p2 = 0; D1 d1; - A1 a1(allocator, size); + A1 a1(allocator, size, &p2); shared_ptr s1(p1, d1, a1); A1* a2 = static_cast(s1._internal_get_untyped_deleter()); - a2->swap(p2); + a2->set(0); #if !defined(BOOST_NO_CXX11_ALLOCATOR) boost::detail::as_init(allocator, p2, n1); #else boost::detail::ms_init(p2, n1); #endif + a2->set(p2); p1 = reinterpret_cast(p2); - a2->swap(p2); return shared_ptr(s1, p1); } @@ -53,17 +53,17 @@ namespace boost { T1* p1 = 0; T2* p2 = 0; D1 d1; - A1 a1(allocator); + A1 a1(allocator, &p2); shared_ptr s1(p1, d1, a1); A1* a2 = static_cast(s1._internal_get_untyped_deleter()); - a2->swap(p2); + a2->set(0); #if !defined(BOOST_NO_CXX11_ALLOCATOR) boost::detail::as_init(allocator, p2, N); #else boost::detail::ms_init(p2, N); #endif + a2->set(p2); p1 = reinterpret_cast(p2); - a2->swap(p2); return shared_ptr(s1, p1); } @@ -85,17 +85,17 @@ namespace boost { T2* p2 = 0; T3* p3 = reinterpret_cast(&value); D1 d1; - A1 a1(allocator, size); + A1 a1(allocator, size, &p2); shared_ptr s1(p1, d1, a1); A1* a2 = static_cast(s1._internal_get_untyped_deleter()); - a2->swap(p2); + a2->set(0); #if !defined(BOOST_NO_CXX11_ALLOCATOR) boost::detail::as_init(allocator, p2, n1, p3); #else boost::detail::ms_init(p2, n1, p3); #endif + a2->set(p2); p1 = reinterpret_cast(p2); - a2->swap(p2); return shared_ptr(s1, p1); } @@ -117,17 +117,17 @@ namespace boost { T2* p2 = 0; T3* p3 = reinterpret_cast(&value); D1 d1; - A1 a1(allocator); + A1 a1(allocator, &p2); shared_ptr s1(p1, d1, a1); A1* a2 = static_cast(s1._internal_get_untyped_deleter()); - a2->swap(p2); + a2->set(0); #if !defined(BOOST_NO_CXX11_ALLOCATOR) boost::detail::as_init(allocator, p2, N, p3); #else boost::detail::ms_init(p2, N, p3); #endif + a2->set(p2); p1 = reinterpret_cast(p2); - a2->swap(p2); return shared_ptr(s1, p1); } @@ -143,13 +143,13 @@ namespace boost { T1* p1 = 0; T2* p2 = 0; D1 d1; - A1 a1(allocator, size); + A1 a1(allocator, size, &p2); shared_ptr s1(p1, d1, a1); A1* a2 = static_cast(s1._internal_get_untyped_deleter()); - a2->swap(p2); + a2->set(0); boost::detail::ms_noinit(p2, n1); + a2->set(p2); p1 = reinterpret_cast(p2); - a2->swap(p2); return shared_ptr(s1, p1); } @@ -167,13 +167,13 @@ namespace boost { T1* p1 = 0; T2* p2 = 0; D1 d1; - A1 a1(allocator); + A1 a1(allocator, &p2); shared_ptr s1(p1, d1, a1); A1* a2 = static_cast(s1._internal_get_untyped_deleter()); - a2->swap(p2); + a2->set(0); boost::detail::ms_noinit(p2, N); + a2->set(p2); p1 = reinterpret_cast(p2); - a2->swap(p2); return shared_ptr(s1, p1); } } diff --git a/include/boost/smart_ptr/detail/array_allocator.hpp b/include/boost/smart_ptr/detail/array_allocator.hpp index 8a408e2..a5a2d61 100644 --- a/include/boost/smart_ptr/detail/array_allocator.hpp +++ b/include/boost/smart_ptr/detail/array_allocator.hpp @@ -45,6 +45,16 @@ namespace boost { struct ms_init_tag { }; struct ms_noinit_tag { }; + template + union ms_allocator_state { + ms_allocator_state(T** result_) + : result(result_) { + } + + T** result; + T* object; + }; + template class as_allocator : as_size_base { @@ -93,34 +103,21 @@ namespace boost { typedef as_allocator other; }; - as_allocator(const A& allocator) + as_allocator(const A& allocator, type** result) : as_size_base(allocator), - object(0) { + data(result) { } - as_allocator(const A& allocator, std::size_t size_) + as_allocator(const A& allocator, std::size_t size_, + type** result) : as_size_base(allocator, size_), - object(0) { - } - - 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 = offset + M - 1; - char* p1 = static_cast(memory) + n1; - while (std::size_t(p1) % M != 0) { - p1--; - } - object = reinterpret_cast(p1); + data(result) { } template as_allocator(const as_allocator& other) : as_size_base(other), - object(other.object) { + data(other.data) { } pointer address(reference value) const { @@ -150,6 +147,11 @@ namespace boost { #else void* p1 = ca.allocate(n1 + n2, value); #endif + char* p2 = static_cast(p1) + n1; + while (std::size_t(p2) % M != 0) { + p2--; + } + *data.result = reinterpret_cast(p2); return static_cast(p1); } @@ -207,12 +209,12 @@ namespace boost { return !(*this == other); } - void swap(type*& other) { - std::swap(object, other); + void set(type* memory) { + data.object = memory; } void operator()() { - if (object) { + if (data.object) { R tag; free(tag); } @@ -222,17 +224,17 @@ namespace boost { void free(ms_init_tag) { #if !defined(BOOST_NO_CXX11_ALLOCATOR) const A& a1(*this); - as_destroy(a1, object, size); + as_destroy(a1, data.object, size); #else - ms_destroy(object, size); + ms_destroy(data.object, size); #endif } void free(ms_noinit_tag) { - ms_destroy(object, size); + ms_destroy(data.object, size); } - type* object; + ms_allocator_state data; }; template @@ -264,6 +266,7 @@ namespace boost { public: typedef typename array_base::type type; + typedef Y value_type; typedef Y* pointer; typedef const Y* const_pointer; @@ -277,33 +280,19 @@ namespace boost { typedef ms_allocator other; }; - ms_allocator() - : object(0) { + ms_allocator(type** result) + : data(result) { } - ms_allocator(std::size_t size_) + ms_allocator(std::size_t size_, type** result) : ms_size_base(size_), - object(0) { - } - - 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 = offset + M - 1; - char* p1 = static_cast(memory) + n1; - while (std::size_t(p1) % M != 0) { - p1--; - } - object = reinterpret_cast(p1); + data(result) { } template ms_allocator(const ms_allocator& other) : ms_size_base(other), - object(other.object) { + data(other.data) { } pointer address(reference value) const { @@ -328,6 +317,11 @@ namespace boost { std::size_t n1 = count * sizeof(value_type) + M - 1; std::size_t n2 = size * sizeof(type); void* p1 = ::operator new(n1 + n2); + char* p2 = static_cast(p1) + n1; + while (std::size_t(p2) % M != 0) { + p2--; + } + *data.result = reinterpret_cast(p2); return static_cast(p1); } @@ -367,18 +361,18 @@ namespace boost { return !(*this == other); } - void swap(type*& other) { - std::swap(object, other); + void set(type* memory) { + data.object = memory; } void operator()() { - if (object) { - ms_destroy(object, size); + if (data.object) { + ms_destroy(data.object, size); } } private: - type* object; + ms_allocator_state data; }; class ms_in_allocator_tag { diff --git a/include/boost/smart_ptr/detail/array_count_impl.hpp b/include/boost/smart_ptr/detail/array_count_impl.hpp index 6a6a2a9..f81954d 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, sizeof(Y)) { + : allocator(allocator_) { } virtual void dispose() { diff --git a/include/boost/smart_ptr/make_shared_array.hpp b/include/boost/smart_ptr/make_shared_array.hpp index 96a45dc..8c2c5d0 100644 --- a/include/boost/smart_ptr/make_shared_array.hpp +++ b/include/boost/smart_ptr/make_shared_array.hpp @@ -25,13 +25,13 @@ namespace boost { T1* p1 = 0; T2* p2 = 0; D1 d1; - A1 a1(size); + A1 a1(size, &p2); shared_ptr s1(p1, d1, a1); A1* a2 = static_cast(s1._internal_get_untyped_deleter()); - a2->swap(p2); + a2->set(0); boost::detail::ms_init(p2, n1); + a2->set(p2); p1 = reinterpret_cast(p2); - a2->swap(p2); return shared_ptr(s1, p1); } @@ -49,13 +49,13 @@ namespace boost { T1* p1 = 0; T2* p2 = 0; D1 d1; - A1 a1; + A1 a1(&p2); shared_ptr s1(p1, d1, a1); A1* a2 = static_cast(s1._internal_get_untyped_deleter()); - a2->swap(p2); + a2->set(0); boost::detail::ms_init(p2, N); + a2->set(p2); p1 = reinterpret_cast(p2); - a2->swap(p2); return shared_ptr(s1, p1); } @@ -77,13 +77,13 @@ namespace boost { T2* p2 = 0; T3* p3 = reinterpret_cast(&value); D1 d1; - A1 a1(size); + A1 a1(size, &p2); shared_ptr s1(p1, d1, a1); A1* a2 = static_cast(s1._internal_get_untyped_deleter()); - a2->swap(p2); + a2->set(0); boost::detail::ms_init(p2, n1, p3); + a2->set(p2); p1 = reinterpret_cast(p2); - a2->swap(p2); return shared_ptr(s1, p1); } @@ -104,13 +104,13 @@ namespace boost { T2* p2 = 0; T3* p3 = reinterpret_cast(&value); D1 d1; - A1 a1; + A1 a1(&p2); shared_ptr s1(p1, d1, a1); A1* a2 = static_cast(s1._internal_get_untyped_deleter()); - a2->swap(p2); + a2->set(0); boost::detail::ms_init(p2, N, p3); + a2->set(p2); p1 = reinterpret_cast(p2); - a2->swap(p2); return shared_ptr(s1, p1); } @@ -126,13 +126,13 @@ namespace boost { T1* p1 = 0; T2* p2 = 0; D1 d1; - A1 a1(size); + A1 a1(size, &p2); shared_ptr s1(p1, d1, a1); A1* a2 = static_cast(s1._internal_get_untyped_deleter()); - a2->swap(p2); + a2->set(0); boost::detail::ms_noinit(p2, n1); + a2->set(p2); p1 = reinterpret_cast(p2); - a2->swap(p2); return shared_ptr(s1, p1); } @@ -150,13 +150,13 @@ namespace boost { T1* p1 = 0; T2* p2 = 0; D1 d1; - A1 a1; + A1 a1(&p2); shared_ptr s1(p1, d1, a1); A1* a2 = static_cast(s1._internal_get_untyped_deleter()); - a2->swap(p2); + a2->set(0); boost::detail::ms_noinit(p2, N); + a2->set(p2); p1 = reinterpret_cast(p2); - a2->swap(p2); return shared_ptr(s1, p1); } }