Spatial optimization for make_shared for arrays

Saves up to sizeof(void*) + sizeof(size_t) bytes for make_shared and saves
sizeof(void*) + sizeof(size_t) + sizeof(A) bytes for allocate_shared where A is the
supplied allocator type.
This commit is contained in:
Glen Fernandes
2014-02-10 20:54:48 -08:00
parent 260af64027
commit 5f485c2952
9 changed files with 460 additions and 465 deletions
@@ -9,152 +9,166 @@
#ifndef BOOST_SMART_PTR_ALLOCATE_SHARED_ARRAY_HPP
#define BOOST_SMART_PTR_ALLOCATE_SHARED_ARRAY_HPP
#include <boost/smart_ptr/detail/array_allocator.hpp>
#include <boost/smart_ptr/detail/array_deleter.hpp>
#include <boost/smart_ptr/detail/array_count_impl.hpp>
#include <boost/smart_ptr/detail/sp_if_array.hpp>
#include <boost/type_traits/remove_cv.hpp>
namespace boost {
template<typename T, typename A>
template<class T, class A>
inline typename boost::detail::sp_if_array<T>::type
allocate_shared(const A& allocator, std::size_t size) {
typedef typename boost::detail::array_inner<T>::type T1;
typedef typename boost::detail::array_base<T1>::type T2;
typedef typename boost::remove_cv<T2>::type T3;
typedef boost::detail::as_allocator<T3[], A> A1;
typedef boost::detail::as_deleter<T3[], A> D1;
T1* p1 = 0;
T3* p2 = 0;
typedef boost::detail::ms_init_tag R1;
typedef boost::detail::ms_allocator<T, R1, A> A1;
typedef boost::detail::ms_noop D1;
std::size_t n1 = size * boost::detail::array_total<T1>::size;
D1 d1(allocator, n1);
A1 a1(allocator, n1, &p2);
boost::shared_ptr<T> s1(p1, d1, a1);
p1 = reinterpret_cast<T1*>(p2);
T1* p1 = 0;
T2* p2 = 0;
D1 d1;
A1 a1(allocator, size, &p2);
shared_ptr<T> s1(p1, d1, a1);
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
boost::detail::as_init(allocator, p2, n1);
D1* d2 = static_cast<D1*>(s1._internal_get_untyped_deleter());
d2->set(p2);
return boost::shared_ptr<T>(s1, p1);
#else
boost::detail::ms_init(p2, n1);
#endif
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
a2->set(p2);
p1 = reinterpret_cast<T1*>(p2);
return shared_ptr<T>(s1, p1);
}
template<typename T, typename A>
template<class T, class A>
inline typename boost::detail::sp_if_size_array<T>::type
allocate_shared(const A& allocator) {
typedef typename boost::detail::array_inner<T>::type T1;
typedef typename boost::detail::array_base<T1>::type T2;
typedef typename boost::remove_cv<T2>::type T3;
typedef boost::detail::ms_init_tag R1;
typedef boost::detail::ms_allocator<T, R1, A> A1;
typedef boost::detail::ms_noop D1;
enum {
N = boost::detail::array_total<T>::size
};
typedef boost::detail::as_allocator<T3[N], A> A1;
typedef boost::detail::as_deleter<T3[N], A> D1;
T1* p1 = 0;
T3* p2 = 0;
D1 d1(allocator);
T2* p2 = 0;
D1 d1;
A1 a1(allocator, &p2);
boost::shared_ptr<T> s1(p1, d1, a1);
p1 = reinterpret_cast<T1*>(p2);
shared_ptr<T> s1(p1, d1, a1);
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
boost::detail::as_init(allocator, p2, N);
D1* d2 = static_cast<D1*>(s1._internal_get_untyped_deleter());
d2->set(p2);
return boost::shared_ptr<T>(s1, p1);
#else
boost::detail::ms_init(p2, N);
#endif
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
a2->set(p2);
p1 = reinterpret_cast<T1*>(p2);
return shared_ptr<T>(s1, p1);
}
template<typename T, typename A>
template<class T, class A>
inline typename boost::detail::sp_if_array<T>::type
allocate_shared(const A& allocator, std::size_t size,
const typename boost::detail::array_inner<T>::type& value) {
typedef typename boost::detail::array_inner<T>::type T1;
typedef typename boost::detail::array_base<T1>::type T2;
typedef typename boost::remove_cv<T2>::type T3;
typedef const T2 T4;
typedef boost::detail::as_allocator<T3[], A> A1;
typedef boost::detail::as_deleter<T3[], A> D1;
typedef const T2 T3;
typedef boost::detail::ms_init_tag R1;
typedef boost::detail::ms_allocator<T, R1, A> A1;
typedef boost::detail::ms_noop D1;
enum {
M = boost::detail::array_total<T1>::size
};
T1* p1 = 0;
T3* p2 = 0;
T4* p3 = reinterpret_cast<T4*>(&value);
std::size_t n1 = M * size;
D1 d1(allocator, n1);
A1 a1(allocator, n1, &p2);
boost::shared_ptr<T> s1(p1, d1, a1);
T1* p1 = 0;
T2* p2 = 0;
T3* p3 = reinterpret_cast<T3*>(&value);
D1 d1;
A1 a1(allocator, size, &p2);
shared_ptr<T> s1(p1, d1, a1);
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
boost::detail::as_init<T2, A, M>(allocator, p2, n1, p3);
#else
boost::detail::ms_init<T2, M>(p2, n1, p3);
#endif
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
a2->set(p2);
p1 = reinterpret_cast<T1*>(p2);
boost::detail::as_init<T3, A, M>(allocator, p2, n1, p3);
D1* d2 = static_cast<D1*>(s1._internal_get_untyped_deleter());
d2->set(p2);
return boost::shared_ptr<T>(s1, p1);
return shared_ptr<T>(s1, p1);
}
template<typename T, typename A>
template<class T, class A>
inline typename boost::detail::sp_if_size_array<T>::type
allocate_shared(const A& allocator,
const typename boost::detail::array_inner<T>::type& value) {
typedef typename boost::detail::array_inner<T>::type T1;
typedef typename boost::detail::array_base<T1>::type T2;
typedef typename boost::remove_cv<T2>::type T3;
typedef const T2 T4;
typedef const T2 T3;
typedef boost::detail::ms_init_tag R1;
typedef boost::detail::ms_allocator<T, R1, A> A1;
typedef boost::detail::ms_noop D1;
enum {
M = boost::detail::array_total<T1>::size,
N = boost::detail::array_total<T>::size
N = boost::detail::array_total<T>::size,
M = boost::detail::array_total<T1>::size
};
typedef boost::detail::as_allocator<T3[N], A> A1;
typedef boost::detail::as_deleter<T3[N], A> D1;
T1* p1 = 0;
T3* p2 = 0;
T4* p3 = reinterpret_cast<T4*>(&value);
D1 d1(allocator);
T2* p2 = 0;
T3* p3 = reinterpret_cast<T3*>(&value);
D1 d1;
A1 a1(allocator, &p2);
boost::shared_ptr<T> s1(p1, d1, a1);
shared_ptr<T> s1(p1, d1, a1);
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
boost::detail::as_init<T2, A, M>(allocator, p2, N, p3);
#else
boost::detail::as_init<T2, M>(p2, N, p3);
#endif
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
a2->set(p2);
p1 = reinterpret_cast<T1*>(p2);
boost::detail::as_init<T3, A, M>(allocator, p2, N, p3);
D1* d2 = static_cast<D1*>(s1._internal_get_untyped_deleter());
d2->set(p2);
return boost::shared_ptr<T>(s1, p1);
return shared_ptr<T>(s1, p1);
}
template<typename T, typename A>
template<class T, class A>
inline typename boost::detail::sp_if_array<T>::type
allocate_shared_noinit(const A& allocator, std::size_t size) {
typedef typename boost::detail::array_inner<T>::type T1;
typedef typename boost::detail::array_base<T1>::type T2;
typedef typename boost::remove_cv<T2>::type T3;
typedef boost::detail::as_allocator<T3[], A> A1;
typedef boost::detail::ms_deleter<T3[]> D1;
T1* p1 = 0;
T3* p2 = 0;
typedef boost::detail::ms_noinit_tag R1;
typedef boost::detail::ms_allocator<T, R1, A> A1;
typedef boost::detail::ms_noop D1;
std::size_t n1 = size * boost::detail::array_total<T1>::size;
D1 d1(n1);
A1 a1(allocator, n1, &p2);
boost::shared_ptr<T> s1(p1, d1, a1);
p1 = reinterpret_cast<T1*>(p2);
T1* p1 = 0;
T2* p2 = 0;
D1 d1;
A1 a1(allocator, size, &p2);
shared_ptr<T> s1(p1, d1, a1);
boost::detail::ms_noinit(p2, n1);
D1* d2 = static_cast<D1*>(s1._internal_get_untyped_deleter());
d2->set(p2);
return boost::shared_ptr<T>(s1, p1);
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
a2->set(p2);
p1 = reinterpret_cast<T1*>(p2);
return shared_ptr<T>(s1, p1);
}
template<typename T, typename A>
template<class T, class A>
inline typename boost::detail::sp_if_size_array<T>::type
allocate_shared_noinit(const A& allocator) {
typedef typename boost::detail::array_inner<T>::type T1;
typedef typename boost::detail::array_base<T1>::type T2;
typedef typename boost::remove_cv<T2>::type T3;
typedef boost::detail::ms_noinit_tag R1;
typedef boost::detail::ms_allocator<T, R1, A> A1;
typedef boost::detail::ms_noop D1;
enum {
N = boost::detail::array_total<T>::size
};
typedef boost::detail::as_allocator<T3[N], A> A1;
typedef boost::detail::ms_deleter<T3[N]> D1;
T1* p1 = 0;
T3* p2 = 0;
T2* p2 = 0;
D1 d1;
A1 a1(allocator, &p2);
boost::shared_ptr<T> s1(p1, d1, a1);
p1 = reinterpret_cast<T1*>(p2);
shared_ptr<T> s1(p1, d1, a1);
boost::detail::ms_noinit(p2, N);
D1* d2 = static_cast<D1*>(s1._internal_get_untyped_deleter());
d2->set(p2);
return boost::shared_ptr<T>(s1, p1);
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
a2->set(p2);
p1 = reinterpret_cast<T1*>(p2);
return shared_ptr<T>(s1, p1);
}
}