From 9f70f6619f22e730a5fed08cd56e46e7f328e770 Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Sun, 5 Mar 2017 19:10:56 -0500 Subject: [PATCH 1/4] Do not rely on size of type_with_alignment --- include/boost/smart_ptr/allocate_shared_array.hpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/include/boost/smart_ptr/allocate_shared_array.hpp b/include/boost/smart_ptr/allocate_shared_array.hpp index 6257c55..5fe6cbd 100644 --- a/include/boost/smart_ptr/allocate_shared_array.hpp +++ b/include/boost/smart_ptr/allocate_shared_array.hpp @@ -367,12 +367,22 @@ sp_objects(std::size_t size) BOOST_NOEXCEPT template BOOST_CONSTEXPR inline -typename sp_enable::value, std::size_t>::type -sp_align(std::size_t size) BOOST_NOEXCEPT +typename sp_enable::value && + (sizeof(U) % 2 == 0), std::size_t>::type +sp_align(std::size_t size) BOOST_NOEXCEPT_OR_NOTHROW { return (sizeof(T) * size + sizeof(U) - 1) & ~(sizeof(U) - 1); } +template +BOOST_CONSTEXPR inline +typename sp_enable::value && + (sizeof(U) % 2 != 0), std::size_t>::type +sp_align(std::size_t size) BOOST_NOEXCEPT_OR_NOTHROW +{ + return (sizeof(T) * size + sizeof(U) - 1) / sizeof(U) * sizeof(U); +} + template BOOST_CONSTEXPR inline typename sp_enable::value, std::size_t>::type From 106ada77709df23d4a73e87db621b53f0f39d9ad Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Sun, 5 Mar 2017 19:13:26 -0500 Subject: [PATCH 2/4] Remove unnecessary helper function --- include/boost/smart_ptr/allocate_shared_array.hpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/include/boost/smart_ptr/allocate_shared_array.hpp b/include/boost/smart_ptr/allocate_shared_array.hpp index 5fe6cbd..1366aee 100644 --- a/include/boost/smart_ptr/allocate_shared_array.hpp +++ b/include/boost/smart_ptr/allocate_shared_array.hpp @@ -358,13 +358,6 @@ struct sp_less_align { }; }; -template -BOOST_CONSTEXPR inline std::size_t -sp_objects(std::size_t size) BOOST_NOEXCEPT -{ - return size / sizeof(T); -} - template BOOST_CONSTEXPR inline typename sp_enable::value && @@ -679,9 +672,9 @@ public: type_allocator allocator(allocator_); std::size_t head = sp_align(count); std::size_t tail = sp_align(size_); - std::size_t size = sp_objects(head + tail); + std::size_t size = (head + tail) / sizeof(type); type* address = allocator.allocate(size); - *result_ = address + sp_objects(head); + *result_ = address + head / sizeof(type); return reinterpret_cast(address); } @@ -689,7 +682,7 @@ public: type_allocator allocator(allocator_); std::size_t head = sp_align(count); std::size_t tail = sp_align(size_); - std::size_t size = sp_objects(head + tail); + std::size_t size = (head + tail) / sizeof(type); allocator.deallocate(reinterpret_cast(value), size); } From 15ed558a29d8f6e2e2ccaf76eeffb9d148161c6b Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Sun, 5 Mar 2017 21:39:22 -0500 Subject: [PATCH 3/4] Further simplify alignment logic in allocate --- .../boost/smart_ptr/allocate_shared_array.hpp | 48 +++++++++---------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/include/boost/smart_ptr/allocate_shared_array.hpp b/include/boost/smart_ptr/allocate_shared_array.hpp index 1366aee..2028795 100644 --- a/include/boost/smart_ptr/allocate_shared_array.hpp +++ b/include/boost/smart_ptr/allocate_shared_array.hpp @@ -350,40 +350,36 @@ sp_array_default(T* storage, std::size_t size) } #endif -template +template struct sp_less_align { enum { - value = (boost::alignment_of::value) < - (boost::alignment_of::value) + value = (boost::alignment_of::value) < N }; }; -template +template BOOST_CONSTEXPR inline -typename sp_enable::value && - (sizeof(U) % 2 == 0), std::size_t>::type -sp_align(std::size_t size) BOOST_NOEXCEPT_OR_NOTHROW +typename sp_enable::value, std::size_t>::type +sp_align(std::size_t size) BOOST_NOEXCEPT { - return (sizeof(T) * size + sizeof(U) - 1) & ~(sizeof(U) - 1); + return (sizeof(T) * size + N - 1) & ~(N - 1); } -template +template BOOST_CONSTEXPR inline -typename sp_enable::value && - (sizeof(U) % 2 != 0), std::size_t>::type -sp_align(std::size_t size) BOOST_NOEXCEPT_OR_NOTHROW -{ - return (sizeof(T) * size + sizeof(U) - 1) / sizeof(U) * sizeof(U); -} - -template -BOOST_CONSTEXPR inline -typename sp_enable::value, std::size_t>::type +typename sp_enable::value, std::size_t>::type sp_align(std::size_t size) BOOST_NOEXCEPT { return sizeof(T) * size; } +template +BOOST_CONSTEXPR inline std::size_t +sp_types(std::size_t size) BOOST_NOEXCEPT +{ + return (size + sizeof(T) - 1) / sizeof(T); +} + template class sp_size_array_deleter { public: @@ -670,19 +666,19 @@ public: value_type* allocate(std::size_t count) { type_allocator allocator(allocator_); - std::size_t head = sp_align(count); - std::size_t tail = sp_align(size_); - std::size_t size = (head + tail) / sizeof(type); + std::size_t head = sp_align(count); + std::size_t tail = sizeof(T) * size_; + std::size_t size = sp_types(head + tail); type* address = allocator.allocate(size); - *result_ = address + head / sizeof(type); + *result_ = reinterpret_cast(address) + head; return reinterpret_cast(address); } void deallocate(value_type* value, std::size_t count) { type_allocator allocator(allocator_); - std::size_t head = sp_align(count); - std::size_t tail = sp_align(size_); - std::size_t size = (head + tail) / sizeof(type); + std::size_t head = sp_align(count); + std::size_t tail = sizeof(T) * size_; + std::size_t size = sp_types(head + tail); allocator.deallocate(reinterpret_cast(value), size); } From 6ef791c7150acfc466eb575e45f5107c80fc3ca0 Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Sun, 5 Mar 2017 22:07:58 -0500 Subject: [PATCH 4/4] Rename identifiers in allocate and deallocate --- include/boost/smart_ptr/allocate_shared_array.hpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/include/boost/smart_ptr/allocate_shared_array.hpp b/include/boost/smart_ptr/allocate_shared_array.hpp index 2028795..b43b43d 100644 --- a/include/boost/smart_ptr/allocate_shared_array.hpp +++ b/include/boost/smart_ptr/allocate_shared_array.hpp @@ -666,19 +666,17 @@ public: value_type* allocate(std::size_t count) { type_allocator allocator(allocator_); - std::size_t head = sp_align(count); - std::size_t tail = sizeof(T) * size_; - std::size_t size = sp_types(head + tail); + std::size_t node = sp_align(count); + std::size_t size = sp_types(node + sizeof(T) * size_); type* address = allocator.allocate(size); - *result_ = reinterpret_cast(address) + head; + *result_ = reinterpret_cast(address) + node; return reinterpret_cast(address); } void deallocate(value_type* value, std::size_t count) { type_allocator allocator(allocator_); - std::size_t head = sp_align(count); - std::size_t tail = sizeof(T) * size_; - std::size_t size = sp_types(head + tail); + std::size_t node = sp_align(count); + std::size_t size = sp_types(node + sizeof(T) * size_); allocator.deallocate(reinterpret_cast(value), size); }