I am trying to understand how boost::make_shared
does the memory allocation for the object managed by a boost::shared_ptr
and the reference-counting object (the shared_ptr
uses) together.
The make_shared
function begins execution here:
template< class T, class A1, class A2, class A3 >
typename boost::detail::sp_if_not_array< T >::type make_shared( A1 && a1, A2 && a2, A3 && a3 )
{
//Seems to create the smart_ptr for the object
boost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );
//Not sure?
boost::detail::sp_ms_deleter< T > * pd = static_cast<boost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );
//Calculates the address at which the bulk-allocation begins
void * pv = pd->address();
//Allocates the memory at address pv?
::new( pv ) T(
boost::detail::sp_forward<A1>( a1 ),
boost::detail::sp_forward<A2>( a2 ),
boost::detail::sp_forward<A3>( a3 )
);
//Not sure
pd->set_initialized();
//Not sure
T * pt2 = static_cast< T* >( pv );
//Not sure
boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
return boost::shared_ptr< T >( pt, pt2 );
}
Would somebody be able to help explain the remaining lines?
I am trying to identify where the size of the bulk memory allocation (the object being pointed to and the shared_ptr reference counting object) is determined?
What's throwing me is that the call to address()
doesn't seem to consider the size of the T
object when allocating the address to allocate at.
(I really don't get what the three AX
parameters are which enter the method and are passed to the placement new()
call)