From 500913db6def096ab6c5b101f3925a54396c8974 Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Sun, 2 Dec 2012 22:05:31 +0000 Subject: [PATCH] Make specializations of detail array_deleter consistent. [SVN r81681] --- .../boost/smart_ptr/allocate_shared_array.hpp | 16 +++--- .../boost/smart_ptr/detail/array_deleter.hpp | 51 +++++++++++-------- include/boost/smart_ptr/make_shared_array.hpp | 20 ++++---- 3 files changed, 48 insertions(+), 39 deletions(-) diff --git a/include/boost/smart_ptr/allocate_shared_array.hpp b/include/boost/smart_ptr/allocate_shared_array.hpp index 94c417e..0c17edd 100644 --- a/include/boost/smart_ptr/allocate_shared_array.hpp +++ b/include/boost/smart_ptr/allocate_shared_array.hpp @@ -29,12 +29,12 @@ namespace boost { T2* p2 = 0; std::size_t n1 = size * boost::detail::array_total::size; boost::detail::allocate_array_helper a1(allocator, n1, &p2); - boost::detail::array_deleter d1; + boost::detail::array_deleter d1(n1); boost::shared_ptr s1(p1, d1, a1); boost::detail::array_deleter* d2; p1 = reinterpret_cast(p2); d2 = get_deleter >(s1); - d2->construct(p2, n1); + d2->construct(p2); return boost::shared_ptr(s1, p1); } #if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS) @@ -47,12 +47,12 @@ namespace boost { T2* p2 = 0; std::size_t n1 = size * boost::detail::array_total::size; boost::detail::allocate_array_helper a1(allocator, n1, &p2); - boost::detail::array_deleter d1; + boost::detail::array_deleter d1(n1); boost::shared_ptr s1(p1, d1, a1); boost::detail::array_deleter* d2; p1 = reinterpret_cast(p2); d2 = get_deleter >(s1); - d2->construct(p2, n1, boost::detail::sp_forward(args)...); + d2->construct(p2, boost::detail::sp_forward(args)...); return boost::shared_ptr(s1, p1); } template @@ -88,13 +88,13 @@ namespace boost { T3* p3 = 0; std::size_t n1 = list.size() * boost::detail::array_total::size; boost::detail::allocate_array_helper a1(allocator, n1, &p2); - boost::detail::array_deleter d1; + boost::detail::array_deleter d1(n1); boost::shared_ptr s1(p1, d1, a1); boost::detail::array_deleter* d2; p3 = reinterpret_cast(list.begin()); p1 = reinterpret_cast(p2); d2 = get_deleter >(s1); - d2->construct_list(p2, n1, p3); + d2->construct_list(p2, p3); return boost::shared_ptr(s1, p1); } #endif @@ -136,13 +136,13 @@ namespace boost { T3* p3 = 0; std::size_t n1 = M * size; boost::detail::allocate_array_helper a1(allocator, n1, &p2); - boost::detail::array_deleter d1; + boost::detail::array_deleter d1(n1); boost::shared_ptr s1(p1, d1, a1); boost::detail::array_deleter* d2; p3 = reinterpret_cast(list); p1 = reinterpret_cast(p2); d2 = get_deleter >(s1); - d2->construct_list(p2, n1, p3, M); + d2->construct_list(p2, p3, M); return boost::shared_ptr(s1, p1); } template diff --git a/include/boost/smart_ptr/detail/array_deleter.hpp b/include/boost/smart_ptr/detail/array_deleter.hpp index 4a62f97..5d89dd8 100644 --- a/include/boost/smart_ptr/detail/array_deleter.hpp +++ b/include/boost/smart_ptr/detail/array_deleter.hpp @@ -19,42 +19,48 @@ namespace boost { template class array_deleter { public: - array_deleter() - : size(0) { + array_deleter(std::size_t size) + : size(size), + object(0) { } ~array_deleter() { destroy(); } - void construct(T* memory, std::size_t count) { - for (object = memory; size < count; size++) { - void* p1 = object + size; + void construct(T* memory) { + object = memory; + for (std::size_t i = 0; i < size; i++) { + void* p1 = object + i; ::new(p1) T(); } } #if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS) template - void construct(T* memory, std::size_t count, Args&&... args) { - for (object = memory; size < count; size++) { - void* p1 = object + size; + void construct(T* memory, Args&&... args) { + object = memory; + for (std::size_t i = 0; i < size; i++) { + void* p1 = object + i; ::new(p1) T(args...); } } #endif - void construct_list(T* memory, std::size_t count, const T* list) { - for (object = memory; size < count; size++) { - void* p1 = object + size; - ::new(p1) T(list[size]); + void construct_list(T* memory, const T* list) { + object = memory; + for (std::size_t i = 0; i < size; i++) { + void* p1 = object + i; + ::new(p1) T(list[i]); } } - void construct_list(T* memory, std::size_t count, const T* list, std::size_t n) { - for (object = memory; size < count; size++) { - void* p1 = object + size; - ::new(p1) T(list[size % n]); + void construct_list(T* memory, const T* list, std::size_t n) { + object = memory; + for (std::size_t i = 0; i < size; i++) { + void* p1 = object + i; + ::new(p1) T(list[i % n]); } } - void construct_noinit(T* memory, std::size_t count) { - for (object = memory; size < count; size++) { - void* p1 = object + size; + void construct_noinit(T* memory) { + object = memory; + for (std::size_t i = 0; i < size; i++) { + void* p1 = object + i; ::new(p1) T; } } @@ -63,8 +69,11 @@ namespace boost { } private: void destroy() { - while (size > 0) { - object[--size].~T(); + if (object) { + for (std::size_t i = size; i > 0; ) { + object[--i].~T(); + } + object = 0; } } std::size_t size; diff --git a/include/boost/smart_ptr/make_shared_array.hpp b/include/boost/smart_ptr/make_shared_array.hpp index d268b71..89ff8f1 100644 --- a/include/boost/smart_ptr/make_shared_array.hpp +++ b/include/boost/smart_ptr/make_shared_array.hpp @@ -29,12 +29,12 @@ namespace boost { T2* p2 = 0; std::size_t n1 = size * boost::detail::array_total::size; boost::detail::make_array_helper a1(n1, &p2); - boost::detail::array_deleter d1; + boost::detail::array_deleter d1(n1); boost::shared_ptr s1(p1, d1, a1); boost::detail::array_deleter* d2; p1 = reinterpret_cast(p2); d2 = get_deleter >(s1); - d2->construct(p2, n1); + d2->construct(p2); return boost::shared_ptr(s1, p1); } #if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS) @@ -47,12 +47,12 @@ namespace boost { T2* p2 = 0; std::size_t n1 = size * boost::detail::array_total::size; boost::detail::make_array_helper a1(n1, &p2); - boost::detail::array_deleter d1; + boost::detail::array_deleter d1(n1); boost::shared_ptr s1(p1, d1, a1); boost::detail::array_deleter* d2; p1 = reinterpret_cast(p2); d2 = get_deleter >(s1); - d2->construct(p2, n1, boost::detail::sp_forward(args)...); + d2->construct(p2, boost::detail::sp_forward(args)...); return boost::shared_ptr(s1, p1); } template @@ -87,13 +87,13 @@ namespace boost { T3* p3 = 0; std::size_t n1 = list.size() * boost::detail::array_total::size; boost::detail::make_array_helper a1(n1, &p2); - boost::detail::array_deleter d1; + boost::detail::array_deleter d1(n1); boost::shared_ptr s1(p1, d1, a1); boost::detail::array_deleter* d2; p3 = reinterpret_cast(list.begin()); p1 = reinterpret_cast(p2); d2 = get_deleter >(s1); - d2->construct_list(p2, n1, p3); + d2->construct_list(p2, p3); return boost::shared_ptr(s1, p1); } #endif @@ -135,13 +135,13 @@ namespace boost { T3* p3 = 0; std::size_t n1 = M * size; boost::detail::make_array_helper a1(n1, &p2); - boost::detail::array_deleter d1; + boost::detail::array_deleter d1(n1); boost::shared_ptr s1(p1, d1, a1); boost::detail::array_deleter* d2; p3 = reinterpret_cast(list); p1 = reinterpret_cast(p2); d2 = get_deleter >(s1); - d2->construct_list(p2, n1, p3, M); + d2->construct_list(p2, p3, M); return boost::shared_ptr(s1, p1); } template @@ -177,12 +177,12 @@ namespace boost { T2* p2 = 0; std::size_t n1 = size * boost::detail::array_total::size; boost::detail::make_array_helper a1(n1, &p2); - boost::detail::array_deleter d1; + boost::detail::array_deleter d1(n1); boost::shared_ptr s1(p1, d1, a1); boost::detail::array_deleter* d2; p1 = reinterpret_cast(p2); d2 = get_deleter >(s1); - d2->construct_noinit(p2, n1); + d2->construct_noinit(p2); return boost::shared_ptr(s1, p1); } template