Further simplify alignment logic in allocate

This commit is contained in:
Glen Fernandes
2017-03-05 21:39:22 -05:00
parent 106ada7770
commit 15ed558a29

View File

@ -350,40 +350,36 @@ sp_array_default(T* storage, std::size_t size)
} }
#endif #endif
template<class T, class U> template<class T, std::size_t N>
struct sp_less_align { struct sp_less_align {
enum { enum {
value = (boost::alignment_of<T>::value) < value = (boost::alignment_of<T>::value) < N
(boost::alignment_of<U>::value)
}; };
}; };
template<class T, class U> template<class T, std::size_t N>
BOOST_CONSTEXPR inline BOOST_CONSTEXPR inline
typename sp_enable<sp_less_align<T, U>::value && typename sp_enable<sp_less_align<T, N>::value, std::size_t>::type
(sizeof(U) % 2 == 0), std::size_t>::type sp_align(std::size_t size) BOOST_NOEXCEPT
sp_align(std::size_t size) BOOST_NOEXCEPT_OR_NOTHROW
{ {
return (sizeof(T) * size + sizeof(U) - 1) & ~(sizeof(U) - 1); return (sizeof(T) * size + N - 1) & ~(N - 1);
} }
template<class T, class U> template<class T, std::size_t N>
BOOST_CONSTEXPR inline BOOST_CONSTEXPR inline
typename sp_enable<sp_less_align<T, U>::value && typename sp_enable<!sp_less_align<T, N>::value, std::size_t>::type
(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<class T, class U>
BOOST_CONSTEXPR inline
typename sp_enable<!sp_less_align<T, U>::value, std::size_t>::type
sp_align(std::size_t size) BOOST_NOEXCEPT sp_align(std::size_t size) BOOST_NOEXCEPT
{ {
return sizeof(T) * size; return sizeof(T) * size;
} }
template<class T>
BOOST_CONSTEXPR inline std::size_t
sp_types(std::size_t size) BOOST_NOEXCEPT
{
return (size + sizeof(T) - 1) / sizeof(T);
}
template<class T, std::size_t N> template<class T, std::size_t N>
class sp_size_array_deleter { class sp_size_array_deleter {
public: public:
@ -670,19 +666,19 @@ public:
value_type* allocate(std::size_t count) { value_type* allocate(std::size_t count) {
type_allocator allocator(allocator_); type_allocator allocator(allocator_);
std::size_t head = sp_align<value_type, type>(count); std::size_t head = sp_align<value_type, alignment>(count);
std::size_t tail = sp_align<T, type>(size_); std::size_t tail = sizeof(T) * size_;
std::size_t size = (head + tail) / sizeof(type); std::size_t size = sp_types<type>(head + tail);
type* address = allocator.allocate(size); type* address = allocator.allocate(size);
*result_ = address + head / sizeof(type); *result_ = reinterpret_cast<char*>(address) + head;
return reinterpret_cast<value_type*>(address); return reinterpret_cast<value_type*>(address);
} }
void deallocate(value_type* value, std::size_t count) { void deallocate(value_type* value, std::size_t count) {
type_allocator allocator(allocator_); type_allocator allocator(allocator_);
std::size_t head = sp_align<value_type, type>(count); std::size_t head = sp_align<value_type, alignment>(count);
std::size_t tail = sp_align<T, type>(size_); std::size_t tail = sizeof(T) * size_;
std::size_t size = (head + tail) / sizeof(type); std::size_t size = sp_types<type>(head + tail);
allocator.deallocate(reinterpret_cast<type*>(value), size); allocator.deallocate(reinterpret_cast<type*>(value), size);
} }