forked from boostorg/smart_ptr
Optimize make_local_shared to use a single allocation
This commit is contained in:
@@ -55,9 +55,9 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
virtual void destroy() BOOST_SP_NOEXCEPT = 0;
|
||||
virtual void local_cb_destroy() BOOST_SP_NOEXCEPT = 0;
|
||||
|
||||
virtual boost::shared_ptr<void> get_shared_ptr() const BOOST_SP_NOEXCEPT = 0;
|
||||
virtual boost::shared_ptr<void> local_cb_get_shared_ptr() const BOOST_SP_NOEXCEPT = 0;
|
||||
|
||||
void add_ref() BOOST_SP_NOEXCEPT
|
||||
{
|
||||
@@ -78,7 +78,7 @@ public:
|
||||
|
||||
if( local_use_count_ == 0 )
|
||||
{
|
||||
destroy();
|
||||
local_cb_destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,12 +112,12 @@ public:
|
||||
|
||||
#endif
|
||||
|
||||
virtual void destroy() BOOST_SP_NOEXCEPT
|
||||
virtual void local_cb_destroy() BOOST_SP_NOEXCEPT
|
||||
{
|
||||
delete this;
|
||||
}
|
||||
|
||||
virtual boost::shared_ptr<void> get_shared_ptr() const BOOST_SP_NOEXCEPT
|
||||
virtual boost::shared_ptr<void> local_cb_get_shared_ptr() const BOOST_SP_NOEXCEPT
|
||||
{
|
||||
return const_pointer_cast<void>( pn_ );
|
||||
}
|
||||
@@ -129,12 +129,12 @@ public:
|
||||
|
||||
boost::shared_ptr<void const volatile> pn_;
|
||||
|
||||
virtual void destroy() BOOST_SP_NOEXCEPT
|
||||
virtual void local_cb_destroy() BOOST_SP_NOEXCEPT
|
||||
{
|
||||
pn_.reset();
|
||||
}
|
||||
|
||||
virtual boost::shared_ptr<void> get_shared_ptr() const BOOST_SP_NOEXCEPT
|
||||
virtual boost::shared_ptr<void> local_cb_get_shared_ptr() const BOOST_SP_NOEXCEPT
|
||||
{
|
||||
return const_pointer_cast<void>( pn_ );
|
||||
}
|
||||
|
||||
@@ -132,6 +132,10 @@ template< class E, class P, class D, class A > inline void lsp_allocator_constru
|
||||
pn = pd;
|
||||
}
|
||||
|
||||
struct lsp_internal_constructor_tag
|
||||
{
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
//
|
||||
@@ -184,6 +188,11 @@ public:
|
||||
|
||||
#endif
|
||||
|
||||
// internal constructor, used by make_shared
|
||||
BOOST_CONSTEXPR local_shared_ptr( boost::detail::lsp_internal_constructor_tag, T * px_, boost::detail::local_counted_base * pn_ ) BOOST_SP_NOEXCEPT : px( px_ ), pn( pn_ )
|
||||
{
|
||||
}
|
||||
|
||||
template<class Y>
|
||||
explicit local_shared_ptr( Y * p ): px( p ), pn( 0 )
|
||||
{
|
||||
@@ -484,7 +493,7 @@ public:
|
||||
|
||||
if( pn )
|
||||
{
|
||||
return static_pointer_cast<Y>( pn->get_shared_ptr() );
|
||||
return static_pointer_cast<Y>( pn->local_cb_get_shared_ptr() );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -502,7 +511,7 @@ public:
|
||||
|
||||
if( pn )
|
||||
{
|
||||
return static_pointer_cast<Y>( pn->get_shared_ptr() );
|
||||
return static_pointer_cast<Y>( pn->local_cb_get_shared_ptr() );
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
// See http://www.boost.org/libs/smart_ptr/ for documentation.
|
||||
|
||||
#include <boost/smart_ptr/local_shared_ptr.hpp>
|
||||
#include <boost/smart_ptr/detail/local_counted_base.hpp>
|
||||
#include <boost/smart_ptr/make_shared.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <utility>
|
||||
@@ -23,6 +24,8 @@ namespace boost
|
||||
namespace detail
|
||||
{
|
||||
|
||||
// lsp_if_not_array
|
||||
|
||||
template<class T> struct lsp_if_not_array
|
||||
{
|
||||
typedef boost::local_shared_ptr<T> type;
|
||||
@@ -36,26 +39,142 @@ template<class T, std::size_t N> struct lsp_if_not_array<T[N]>
|
||||
{
|
||||
};
|
||||
|
||||
// lsp_ms_deleter
|
||||
|
||||
template<class T, class A> class lsp_ms_deleter: public local_counted_impl_em
|
||||
{
|
||||
private:
|
||||
|
||||
typedef typename sp_aligned_storage<sizeof(T), ::boost::alignment_of<T>::value>::type storage_type;
|
||||
|
||||
storage_type storage_;
|
||||
A a_;
|
||||
bool initialized_;
|
||||
|
||||
private:
|
||||
|
||||
void destroy() BOOST_SP_NOEXCEPT
|
||||
{
|
||||
if( initialized_ )
|
||||
{
|
||||
T * p = reinterpret_cast< T* >( storage_.data_ );
|
||||
|
||||
#if !defined( BOOST_NO_CXX11_ALLOCATOR )
|
||||
|
||||
std::allocator_traits<A>::destroy( a_, p );
|
||||
|
||||
#else
|
||||
|
||||
p->~T();
|
||||
|
||||
#endif
|
||||
|
||||
initialized_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
explicit lsp_ms_deleter( A const & a ) BOOST_SP_NOEXCEPT : a_( a ), initialized_( false )
|
||||
{
|
||||
}
|
||||
|
||||
// optimization: do not copy storage_
|
||||
lsp_ms_deleter( lsp_ms_deleter const & r ) BOOST_SP_NOEXCEPT : a_( r.a_), initialized_( false )
|
||||
{
|
||||
}
|
||||
|
||||
~lsp_ms_deleter() BOOST_SP_NOEXCEPT
|
||||
{
|
||||
destroy();
|
||||
}
|
||||
|
||||
void operator()( T * ) BOOST_SP_NOEXCEPT
|
||||
{
|
||||
destroy();
|
||||
}
|
||||
|
||||
static void operator_fn( T* ) BOOST_SP_NOEXCEPT // operator() can't be static
|
||||
{
|
||||
}
|
||||
|
||||
void * address() BOOST_SP_NOEXCEPT
|
||||
{
|
||||
return storage_.data_;
|
||||
}
|
||||
|
||||
void set_initialized() BOOST_SP_NOEXCEPT
|
||||
{
|
||||
initialized_ = true;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<class T, class... Args> typename boost::detail::lsp_if_not_array<T>::type make_local_shared( Args&&... args )
|
||||
{
|
||||
return boost::make_shared<T>( std::forward<Args>(args)... );
|
||||
}
|
||||
|
||||
template<class T> typename boost::detail::lsp_if_not_array<T>::type make_local_shared_noinit()
|
||||
{
|
||||
return boost::make_shared_noinit<T>();
|
||||
}
|
||||
|
||||
template<class T, class A, class... Args> typename boost::detail::lsp_if_not_array<T>::type allocate_local_shared( A const & a, Args&&... args )
|
||||
{
|
||||
return boost::allocate_shared<T>( a, std::forward<Args>(args)... );
|
||||
typedef typename std::allocator_traits<A>::template rebind_alloc<T> A2;
|
||||
A2 a2( a );
|
||||
|
||||
typedef boost::detail::lsp_ms_deleter<T, A2> D;
|
||||
|
||||
boost::shared_ptr<T> pt( static_cast< T* >( 0 ), boost::detail::sp_inplace_tag<D>(), a2 );
|
||||
|
||||
D * pd = static_cast< D* >( pt._internal_get_untyped_deleter() );
|
||||
void * pv = pd->address();
|
||||
|
||||
#if !defined( BOOST_NO_CXX11_ALLOCATOR )
|
||||
|
||||
std::allocator_traits<A2>::construct( a2, static_cast< T* >( pv ), std::forward<Args>( args )... );
|
||||
|
||||
#else
|
||||
|
||||
::new( pv ) T( std::forward<Args>( args )... );
|
||||
|
||||
#endif
|
||||
|
||||
pd->set_initialized();
|
||||
|
||||
T * pt2 = static_cast< T* >( pv );
|
||||
boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
|
||||
|
||||
pd->pn_ = boost::shared_ptr<T>( pt, pt2 );
|
||||
|
||||
return boost::local_shared_ptr<T>( boost::detail::lsp_internal_constructor_tag(), pt2, pd );
|
||||
}
|
||||
|
||||
template<class T, class A> typename boost::detail::lsp_if_not_array<T>::type allocate_local_shared_noinit( A const & a )
|
||||
{
|
||||
return boost::allocate_shared_noinit<T>( a );
|
||||
typedef typename std::allocator_traits<A>::template rebind_alloc<T> A2;
|
||||
A2 a2( a );
|
||||
|
||||
typedef boost::detail::lsp_ms_deleter<T, A2> D;
|
||||
|
||||
boost::shared_ptr<T> pt( static_cast< T* >( 0 ), boost::detail::sp_inplace_tag<D>(), a2 );
|
||||
|
||||
D * pd = static_cast< D* >( pt._internal_get_untyped_deleter() );
|
||||
void * pv = pd->address();
|
||||
|
||||
::new( pv ) T;
|
||||
|
||||
pd->set_initialized();
|
||||
|
||||
T * pt2 = static_cast< T* >( pv );
|
||||
boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
|
||||
|
||||
pd->pn_ = boost::shared_ptr<T>( pt, pt2 );
|
||||
|
||||
return boost::local_shared_ptr<T>( boost::detail::lsp_internal_constructor_tag(), pt2, pd );
|
||||
}
|
||||
|
||||
template<class T, class... Args> typename boost::detail::lsp_if_not_array<T>::type make_local_shared( Args&&... args )
|
||||
{
|
||||
return boost::allocate_local_shared<T>( std::allocator<T>(), std::forward<Args>(args)... );
|
||||
}
|
||||
|
||||
template<class T> typename boost::detail::lsp_if_not_array<T>::type make_local_shared_noinit()
|
||||
{
|
||||
return boost::allocate_shared_noinit<T>( std::allocator<T>() );
|
||||
}
|
||||
|
||||
} // namespace boost
|
||||
|
||||
Reference in New Issue
Block a user