From 15ed558a29d8f6e2e2ccaf76eeffb9d148161c6b Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Sun, 5 Mar 2017 21:39:22 -0500 Subject: [PATCH] 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); }