/* Copyright 2012-2017 Glen Joseph Fernandes (glenjofe@gmail.com) Distributed under the Boost Software License, Version 1.0. (http://www.boost.org/LICENSE_1_0.txt) */ #ifndef BOOST_SMART_PTR_ALLOCATE_SHARED_ARRAY_HPP #define BOOST_SMART_PTR_ALLOCATE_SHARED_ARRAY_HPP #include #include #include #include #include #include namespace boost { namespace detail { template struct sp_if_array { }; template struct sp_if_array { typedef boost::shared_ptr type; }; template struct sp_if_size_array { }; template struct sp_if_size_array { typedef boost::shared_ptr type; }; template struct sp_array_element { }; template struct sp_array_element { typedef T type; }; template struct sp_array_element { typedef T type; }; template struct sp_array_scalar { typedef T type; }; template struct sp_array_scalar { typedef typename sp_array_scalar::type type; }; template struct sp_array_scalar { typedef typename sp_array_scalar::type type; }; template struct sp_array_scalar { typedef typename sp_array_scalar::type type; }; template struct sp_array_scalar { typedef typename sp_array_scalar::type type; }; template struct sp_array_scalar { typedef typename sp_array_scalar::type type; }; template struct sp_array_scalar { typedef typename sp_array_scalar::type type; }; template struct sp_array_scalar { typedef typename sp_array_scalar::type type; }; template struct sp_array_scalar { typedef typename sp_array_scalar::type type; }; template struct sp_array_count { enum { value = 1 }; }; template struct sp_array_count { enum { value = N * sp_array_count::value }; }; template struct sp_array_count { }; template inline D* sp_get_deleter(const boost::shared_ptr& value) BOOST_NOEXCEPT_OR_NOTHROW { return static_cast(value._internal_get_untyped_deleter()); } template struct sp_array_storage { union type { char value[N]; typename boost::type_with_alignment::type other; }; }; template struct sp_max_size { enum { value = N < M ? M : N }; }; #if !defined(BOOST_NO_CXX11_ALLOCATOR) template struct sp_bind_allocator { typedef typename std::allocator_traits::template rebind_alloc type; }; #else template struct sp_bind_allocator { typedef typename A::template rebind::other type; }; #endif template struct sp_enable { }; template struct sp_enable { typedef T type; }; template inline typename sp_enable::value>::type sp_array_destroy(T*, std::size_t) BOOST_NOEXCEPT { } template inline typename sp_enable::value>::type sp_array_destroy(T* storage, std::size_t size) { while (size > 0) { storage[--size].~T(); } } #if !defined(BOOST_NO_CXX11_ALLOCATOR) template inline void sp_array_destroy(A& allocator, T* storage, std::size_t size) { while (size > 0) { std::allocator_traits::destroy(allocator, storage + --size); } } #endif #if !defined(BOOST_NO_EXCEPTIONS) template inline typename sp_enable::value || boost::has_trivial_destructor::value>::type sp_array_construct(T* storage, std::size_t size) { for (std::size_t i = 0; i < size; ++i) { ::new(static_cast(storage + i)) T(); } } template inline typename sp_enable::value && !boost::has_trivial_destructor::value>::type sp_array_construct(T* storage, std::size_t size) { std::size_t i = 0; try { for (; i < size; ++i) { ::new(static_cast(storage + i)) T(); } } catch (...) { sp_array_destroy(storage, i); throw; } } template inline typename sp_enable::value || boost::has_trivial_destructor::value>::type sp_array_construct(T* storage, std::size_t size, const T* list, std::size_t count) { for (std::size_t i = 0; i < size; ++i) { ::new(static_cast(storage + i)) T(list[i % count]); } } template inline typename sp_enable::value && !boost::has_trivial_destructor::value>::type sp_array_construct(T* storage, std::size_t size, const T* list, std::size_t count) { std::size_t i = 0; try { for (; i < size; ++i) { ::new(static_cast(storage + i)) T(list[i % count]); } } catch (...) { sp_array_destroy(storage, i); throw; } } #else template inline void sp_array_construct(T* storage, std::size_t size) { for (std::size_t i = 0; i < size; ++i) { ::new(static_cast(storage + i)) T(); } } template inline void sp_array_construct(T* storage, std::size_t size, const T* list, std::size_t count) { for (std::size_t i = 0; i < size; ++i) { ::new(static_cast(storage + i)) T(list[i % count]); } } #endif #if !defined(BOOST_NO_CXX11_ALLOCATOR) #if !defined(BOOST_NO_EXCEPTIONS) template inline void sp_array_construct(A& allocator, T* storage, std::size_t size) { std::size_t i = 0; try { for (i = 0; i < size; ++i) { std::allocator_traits::construct(allocator, storage + i); } } catch (...) { sp_array_destroy(allocator, storage, i); throw; } } template inline void sp_array_construct(A& allocator, T* storage, std::size_t size, const T* list, std::size_t count) { std::size_t i = 0; try { for (i = 0; i < size; ++i) { std::allocator_traits::construct(allocator, storage + i, list[i % count]); } } catch (...) { sp_array_destroy(allocator, storage, i); throw; } } #else template inline void sp_array_construct(A& allocator, T* storage, std::size_t size) { for (std::size_t i = 0; i < size; ++i) { std::allocator_traits::construct(allocator, storage + i); } } template inline void sp_array_construct(A& allocator, T* storage, std::size_t size, const T* list, std::size_t count) { for (std::size_t i = 0; i < size; ++i) { std::allocator_traits::construct(allocator, storage + i, list[i % count]); } } #endif #endif template inline typename sp_enable::value>::type sp_array_default(T*, std::size_t) BOOST_NOEXCEPT { } #if !defined(BOOST_NO_EXCEPTIONS) template inline typename sp_enable::value && boost::has_trivial_destructor::value>::type sp_array_default(T* storage, std::size_t size) { for (std::size_t i = 0; i < size; ++i) { ::new(static_cast(storage + i)) T; } } template inline typename sp_enable::value && !boost::has_trivial_destructor::value>::type sp_array_default(T* storage, std::size_t size) { std::size_t i = 0; try { for (; i < size; ++i) { ::new(static_cast(storage + i)) T; } } catch (...) { sp_array_destroy(storage, i); throw; } } #else template inline typename sp_enable::value>::type sp_array_default(T* storage, std::size_t size) { for (std::size_t i = 0; i < size; ++i) { ::new(static_cast(storage + i)) T; } } #endif template struct sp_less_align { enum { value = (boost::alignment_of::value) < N }; }; template BOOST_CONSTEXPR inline typename sp_enable::value, std::size_t>::type sp_align(std::size_t size) BOOST_NOEXCEPT { return (sizeof(T) * size + N - 1) & ~(N - 1); } template BOOST_CONSTEXPR inline 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: template static void operator_fn(U) BOOST_NOEXCEPT { } sp_size_array_deleter() BOOST_NOEXCEPT : enabled_(false) { } template sp_size_array_deleter(const A&) BOOST_NOEXCEPT : enabled_(false) { } sp_size_array_deleter(const sp_size_array_deleter&) BOOST_NOEXCEPT : enabled_(false) { } ~sp_size_array_deleter() { if (enabled_) { sp_array_destroy(reinterpret_cast(&storage_), N); } } template void operator()(U) { if (enabled_) { sp_array_destroy(reinterpret_cast(&storage_), N); enabled_ = false; } } void* construct() { sp_array_construct(reinterpret_cast(&storage_), N); enabled_ = true; return &storage_; } void* construct(const T* list, std::size_t count) { sp_array_construct(reinterpret_cast(&storage_), N, list, count); enabled_ = true; return &storage_; } void* construct_default() { sp_array_default(reinterpret_cast(&storage_), N); enabled_ = true; return &storage_; } private: bool enabled_; typename sp_array_storage::value>::type storage_; }; #if !defined(BOOST_NO_CXX11_ALLOCATOR) template class sp_size_array_destroyer { public: template static void operator_fn(U) BOOST_NOEXCEPT { } template sp_size_array_destroyer(const U& allocator) BOOST_NOEXCEPT : allocator_(allocator), enabled_(false) { } sp_size_array_destroyer(const sp_size_array_destroyer& other) BOOST_NOEXCEPT : allocator_(other.allocator_), enabled_(false) { } ~sp_size_array_destroyer() { if (enabled_) { sp_array_destroy(allocator_, reinterpret_cast(&storage_), N); } } template void operator()(U) { if (enabled_) { sp_array_destroy(allocator_, reinterpret_cast(&storage_), N); enabled_ = false; } } void* construct() { sp_array_construct(allocator_, reinterpret_cast(&storage_), N); enabled_ = true; return &storage_; } void* construct(const T* list, std::size_t count) { sp_array_construct(allocator_, reinterpret_cast(&storage_), N, list, count); enabled_ = true; return &storage_; } const A& allocator() const BOOST_NOEXCEPT { return allocator_; } private: typename sp_array_storage::value>::type storage_; A allocator_; bool enabled_; }; #endif template class sp_array_deleter { public: template static void operator_fn(U) BOOST_NOEXCEPT { } sp_array_deleter(std::size_t size) BOOST_NOEXCEPT : address_(0), size_(size) { } template sp_array_deleter(const A& allocator) BOOST_NOEXCEPT : address_(0), size_(allocator.size()) { } template sp_array_deleter(const A&, std::size_t size) BOOST_NOEXCEPT : address_(0), size_(size) { } sp_array_deleter(const sp_array_deleter& other) BOOST_NOEXCEPT : address_(0), size_(other.size_) { } ~sp_array_deleter() { if (address_) { sp_array_destroy(static_cast(address_), size_); } } template void operator()(U) { if (address_) { sp_array_destroy(static_cast(address_), size_); address_ = 0; } } void construct(T* address) { sp_array_construct(address, size_); address_ = address; } void construct(T* address, const T* list, std::size_t count) { sp_array_construct(address, size_, list, count); address_ = address; } void construct_default(T* address) { sp_array_default(address, size_); address_ = address; } std::size_t size() const BOOST_NOEXCEPT { return size_; } private: void* address_; std::size_t size_; }; #if !defined(BOOST_NO_CXX11_ALLOCATOR) template class sp_array_destroyer { public: template static void operator_fn(U) BOOST_NOEXCEPT { } template sp_array_destroyer(const U& allocator, std::size_t size) BOOST_NOEXCEPT : allocator_(allocator), size_(size), address_(0) { } template sp_array_destroyer(const U& allocator) BOOST_NOEXCEPT : allocator_(allocator.allocator()), size_(allocator.size()), address_(0) { } sp_array_destroyer(const sp_array_destroyer& other) BOOST_NOEXCEPT : allocator_(other.allocator_), size_(other.size_), address_(0) { } ~sp_array_destroyer() { if (address_) { sp_array_destroy(allocator_, static_cast(address_), size_); } } template void operator()(U) { if (address_) { sp_array_destroy(allocator_, static_cast(address_), size_); address_ = 0; } } void construct(T* address) { sp_array_construct(allocator_, address, size_); address_ = address; } void construct(T* address, const T* list, std::size_t count) { sp_array_construct(allocator_, address, size_, list, count); address_ = address; } const A& allocator() const BOOST_NOEXCEPT { return allocator_; } std::size_t size() const BOOST_NOEXCEPT { return size_; } private: A allocator_; std::size_t size_; void* address_; }; #endif template class sp_array_allocator { template friend class sp_array_allocator; public: typedef typename A::value_type value_type; private: enum { alignment = sp_max_size::value, boost::alignment_of::value>::value }; typedef typename boost::type_with_alignment::type type; typedef typename sp_bind_allocator::type type_allocator; public: template struct rebind { typedef sp_array_allocator::type> other; }; sp_array_allocator(const A& allocator, std::size_t size, void** result) BOOST_NOEXCEPT : allocator_(allocator), size_(size), result_(result) { } sp_array_allocator(const A& allocator, std::size_t size) BOOST_NOEXCEPT : allocator_(allocator), size_(size) { } template sp_array_allocator(const sp_array_allocator& other) BOOST_NOEXCEPT : allocator_(other.allocator_), size_(other.size_), result_(other.result_) { } value_type* allocate(std::size_t count) { type_allocator allocator(allocator_); 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) + node; return reinterpret_cast(address); } void deallocate(value_type* value, std::size_t count) { type_allocator allocator(allocator_); std::size_t node = sp_align(count); std::size_t size = sp_types(node + sizeof(T) * size_); allocator.deallocate(reinterpret_cast(value), size); } const A& allocator() const BOOST_NOEXCEPT { return allocator_; } std::size_t size() const BOOST_NOEXCEPT { return size_; } private: A allocator_; std::size_t size_; void** result_; }; template inline bool operator==(const sp_array_allocator& first, const sp_array_allocator& second) BOOST_NOEXCEPT { return first.allocator() == second.allocator() && first.size() == second.size(); } template inline bool operator!=(const sp_array_allocator& first, const sp_array_allocator& second) BOOST_NOEXCEPT { return !(first == second); } #if !defined(BOOST_NO_CXX11_ALLOCATOR) template struct sp_select_size_deleter { typedef sp_size_array_destroyer::type> type; }; template struct sp_select_size_deleter, T, N> { typedef sp_size_array_deleter type; }; template struct sp_select_deleter { typedef sp_array_destroyer::type> type; }; template struct sp_select_deleter, T> { typedef sp_array_deleter type; }; #else template struct sp_select_size_deleter { typedef sp_size_array_deleter type; }; template struct sp_select_deleter { typedef sp_array_deleter type; }; #endif template class sp_counted_impl_pda, A> : public sp_counted_base { public: typedef sp_size_array_deleter deleter_type; private: typedef sp_counted_impl_pda type; typedef typename sp_bind_allocator::type deallocator; public: sp_counted_impl_pda(P, const deleter_type&, const A& allocator) : deleter_(allocator), allocator_(allocator) { } sp_counted_impl_pda(P, const A& allocator) : deleter_(allocator) { } void dispose() { deleter_(0); } void destroy() { deallocator allocator(allocator_); this->~type(); allocator.deallocate(this, 1); } void* get_deleter(const sp_typeinfo&) { return &reinterpret_cast(deleter_); } void* get_untyped_deleter() { return &reinterpret_cast(deleter_); } private: deleter_type deleter_; A allocator_; }; #if !defined(BOOST_NO_CXX11_ALLOCATOR) template class sp_counted_impl_pda, A> : public sp_counted_base { public: typedef sp_size_array_destroyer deleter_type; private: typedef sp_counted_impl_pda type; typedef typename sp_bind_allocator::type deallocator; public: sp_counted_impl_pda(P, const deleter_type&, const A& allocator) : deleter_(allocator) { } sp_counted_impl_pda(P, const A& allocator) : deleter_(allocator) { } void dispose() { deleter_(0); } void destroy() { deallocator allocator(deleter_.allocator()); this->~type(); allocator.deallocate(this, 1); } void* get_deleter(const sp_typeinfo&) { return &reinterpret_cast(deleter_); } void* get_untyped_deleter() { return &reinterpret_cast(deleter_); } private: deleter_type deleter_; }; #endif template class sp_counted_impl_pda, sp_array_allocator > : public sp_counted_base { public: typedef sp_array_deleter deleter_type; typedef sp_array_allocator allocator_type; private: typedef sp_counted_impl_pda type; typedef sp_array_allocator::type> deallocator; public: sp_counted_impl_pda(P, const deleter_type&, const allocator_type& allocator) : deleter_(allocator), allocator_(allocator.allocator()) { } sp_counted_impl_pda(P, const allocator_type& allocator) : deleter_(allocator), allocator_(allocator.allocator()) { } void dispose() { deleter_(0); } void destroy() { deallocator allocator(allocator_, deleter_.size()); this->~type(); allocator.deallocate(this, 1); } void* get_deleter(const sp_typeinfo&) { return &reinterpret_cast(deleter_); } void* get_untyped_deleter() { return &reinterpret_cast(deleter_); } private: deleter_type deleter_; A allocator_; }; #if !defined(BOOST_NO_CXX11_ALLOCATOR) template class sp_counted_impl_pda, sp_array_allocator > : public sp_counted_base { public: typedef sp_array_destroyer deleter_type; typedef sp_array_allocator allocator_type; private: typedef sp_counted_impl_pda type; typedef sp_array_allocator::type> deallocator; public: sp_counted_impl_pda(P, const deleter_type&, const allocator_type& allocator) : deleter_(allocator) { } sp_counted_impl_pda(P, const allocator_type& allocator) : deleter_(allocator) { } void dispose() { deleter_(0); } void destroy() { deallocator allocator(deleter_.allocator(), deleter_.size()); this->~type(); allocator.deallocate(this, 1); } void* get_deleter(const sp_typeinfo&) { return &reinterpret_cast(deleter_); } void* get_untyped_deleter() { return &reinterpret_cast(deleter_); } private: deleter_type deleter_; }; #endif } /* detail */ template inline typename detail::sp_if_size_array::type allocate_shared(const A& allocator) { typedef typename detail::sp_array_element::type type; typedef typename detail::sp_array_scalar::type scalar; typedef typename detail::sp_select_size_deleter::value>::type deleter; shared_ptr result(static_cast(0), detail::sp_inplace_tag(), allocator); deleter* state = detail::sp_get_deleter(result); void* start = state->construct(); return shared_ptr(result, static_cast(start)); } template inline typename detail::sp_if_size_array::type allocate_shared(const A& allocator, const typename detail::sp_array_element::type& value) { typedef typename detail::sp_array_element::type type; typedef typename detail::sp_array_scalar::type scalar; typedef typename detail::sp_select_size_deleter::value>::type deleter; shared_ptr result(static_cast(0), detail::sp_inplace_tag(), allocator); deleter* state = detail::sp_get_deleter(result); void* start = state->construct(reinterpret_cast(&value), detail::sp_array_count::value); return shared_ptr(result, static_cast(start)); } template inline typename detail::sp_if_size_array::type allocate_shared_noinit(const A& allocator) { typedef typename detail::sp_array_element::type type; typedef typename detail::sp_array_scalar::type scalar; typedef detail::sp_size_array_deleter::value> deleter; shared_ptr result(static_cast(0), detail::sp_inplace_tag(), allocator); deleter* state = detail::sp_get_deleter(result); void* start = state->construct_default(); return shared_ptr(result, static_cast(start)); } template inline typename detail::sp_if_array::type allocate_shared(const A& allocator, std::size_t count) { typedef typename detail::sp_array_element::type type; typedef typename detail::sp_array_scalar::type scalar; typedef typename detail::sp_select_deleter::type deleter; std::size_t size = count * detail::sp_array_count::value; void* start; shared_ptr result(static_cast(0), detail::sp_inplace_tag(), detail::sp_array_allocator(allocator, size, &start)); deleter* state = detail::sp_get_deleter(result); state->construct(static_cast(start)); return shared_ptr(result, static_cast(start)); } template inline typename detail::sp_if_array::type allocate_shared(const A& allocator, std::size_t count, const typename detail::sp_array_element::type& value) { typedef typename detail::sp_array_element::type type; typedef typename detail::sp_array_scalar::type scalar; typedef typename detail::sp_select_deleter::type deleter; std::size_t size = count * detail::sp_array_count::value; void* start; shared_ptr result(static_cast(0), detail::sp_inplace_tag(), detail::sp_array_allocator(allocator, size, &start)); deleter* state = detail::sp_get_deleter(result); state->construct(static_cast(start), reinterpret_cast(&value), detail::sp_array_count::value); return shared_ptr(result, static_cast(start)); } template inline typename detail::sp_if_array::type allocate_shared_noinit(const A& allocator, std::size_t count) { typedef typename detail::sp_array_element::type type; typedef typename detail::sp_array_scalar::type scalar; typedef detail::sp_array_deleter deleter; std::size_t size = count * detail::sp_array_count::value; void* start; shared_ptr result(static_cast(0), detail::sp_inplace_tag(), detail::sp_array_allocator(allocator, size, &start)); deleter* state = detail::sp_get_deleter(result); state->construct_default(static_cast(start)); return shared_ptr(result, static_cast(start)); } } /* boost */ #endif