LCOV - code coverage report
Current view: top level - opt/homebrew/include/boost/smart_ptr - make_shared_object.hpp (source / functions) Hit Total Coverage
Test: test_dash_coverage.info Lines: 32 34 94.1 %
Date: 2026-06-25 07:23:51 Functions: 143 304 47.0 %

          Line data    Source code
       1             : #ifndef BOOST_SMART_PTR_MAKE_SHARED_OBJECT_HPP_INCLUDED
       2             : #define BOOST_SMART_PTR_MAKE_SHARED_OBJECT_HPP_INCLUDED
       3             : 
       4             : //  make_shared_object.hpp
       5             : //
       6             : //  Copyright (c) 2007, 2008, 2012 Peter Dimov
       7             : //
       8             : //  Distributed under the Boost Software License, Version 1.0.
       9             : //  See accompanying file LICENSE_1_0.txt or copy at
      10             : //  http://www.boost.org/LICENSE_1_0.txt
      11             : //
      12             : //  See http://www.boost.org/libs/smart_ptr/ for documentation.
      13             : 
      14             : #include <boost/smart_ptr/shared_ptr.hpp>
      15             : #include <boost/smart_ptr/detail/sp_type_traits.hpp>
      16             : #include <boost/config.hpp>
      17             : #include <utility>
      18             : #include <cstddef>
      19             : #include <new>
      20             : #include <type_traits>
      21             : 
      22             : namespace boost
      23             : {
      24             : namespace detail
      25             : {
      26             : 
      27             : template< std::size_t N, std::size_t A > struct sp_aligned_storage
      28             : {
      29             :     union type
      30             :     {
      31             :         char data_[ N ];
      32             :         typename sp_type_with_alignment< A >::type align_;
      33             :     };
      34             : };
      35             : 
      36             : template< class T > class sp_ms_deleter
      37             : {
      38             : private:
      39             : 
      40             :     typedef typename sp_aligned_storage< sizeof( T ), std::alignment_of< T >::value >::type storage_type;
      41             : 
      42             :     bool initialized_;
      43             :     storage_type storage_;
      44             : 
      45             : private:
      46             : 
      47        2388 :     void destroy() noexcept
      48             :     {
      49        2388 :         if( initialized_ )
      50             :         {
      51             : #if defined( __GNUC__ )
      52             : 
      53             :             // fixes incorrect aliasing warning
      54        1194 :             T * p = reinterpret_cast< T* >( storage_.data_ );
      55        1194 :             p->~T();
      56             : 
      57             : #else
      58             : 
      59             :             reinterpret_cast< T* >( storage_.data_ )->~T();
      60             : 
      61             : #endif
      62             : 
      63        1194 :             initialized_ = false;
      64        1194 :         }
      65        2388 :     }
      66             : 
      67             : public:
      68             : 
      69        8340 :     sp_ms_deleter() noexcept : initialized_( false )
      70        4170 :     {
      71        8340 :     }
      72             : 
      73             :     template<class A> explicit sp_ms_deleter( A const & ) noexcept : initialized_( false )
      74             :     {
      75             :     }
      76             : 
      77             :     // optimization: do not copy storage_
      78             :     sp_ms_deleter( sp_ms_deleter const & ) noexcept : initialized_( false )
      79             :     {
      80             :     }
      81             : 
      82        2388 :     ~sp_ms_deleter() noexcept
      83        1194 :     {
      84        1194 :         destroy();
      85        2388 :     }
      86             : 
      87        1194 :     void operator()( T * ) noexcept
      88             :     {
      89        1194 :         destroy();
      90        1194 :     }
      91             : 
      92           0 :     static void operator_fn( T* ) noexcept // operator() can't be static
      93             :     {
      94           0 :     }
      95             : 
      96        4170 :     void * address() noexcept
      97             :     {
      98        4170 :         return storage_.data_;
      99             :     }
     100             : 
     101        4170 :     void set_initialized() noexcept
     102             :     {
     103        4170 :         initialized_ = true;
     104        4170 :     }
     105             : };
     106             : 
     107             : template< class T, class A > class sp_as_deleter
     108             : {
     109             : private:
     110             : 
     111             :     typedef typename sp_aligned_storage< sizeof( T ), std::alignment_of< T >::value >::type storage_type;
     112             : 
     113             :     storage_type storage_;
     114             :     A a_;
     115             :     bool initialized_;
     116             : 
     117             : private:
     118             : 
     119             :     void destroy() noexcept
     120             :     {
     121             :         if( initialized_ )
     122             :         {
     123             :             T * p = reinterpret_cast< T* >( storage_.data_ );
     124             : 
     125             :             std::allocator_traits<A>::destroy( a_, p );
     126             : 
     127             :             initialized_ = false;
     128             :         }
     129             :     }
     130             : 
     131             : public:
     132             : 
     133             :     sp_as_deleter( A const & a ) noexcept : a_( a ), initialized_( false )
     134             :     {
     135             :     }
     136             : 
     137             :     // optimization: do not copy storage_
     138             :     sp_as_deleter( sp_as_deleter const & r ) noexcept : a_( r.a_), initialized_( false )
     139             :     {
     140             :     }
     141             : 
     142             :     ~sp_as_deleter() noexcept
     143             :     {
     144             :         destroy();
     145             :     }
     146             : 
     147             :     void operator()( T * ) noexcept
     148             :     {
     149             :         destroy();
     150             :     }
     151             : 
     152             :     static void operator_fn( T* ) noexcept // operator() can't be static
     153             :     {
     154             :     }
     155             : 
     156             :     void * address() noexcept
     157             :     {
     158             :         return storage_.data_;
     159             :     }
     160             : 
     161             :     void set_initialized() noexcept
     162             :     {
     163             :         initialized_ = true;
     164             :     }
     165             : };
     166             : 
     167             : template< class T > struct sp_if_not_array
     168             : {
     169             :     typedef boost::shared_ptr< T > type;
     170             : };
     171             : 
     172             : template< class T > struct sp_if_not_array< T[] >
     173             : {
     174             : };
     175             : 
     176             : template< class T, std::size_t N > struct sp_if_not_array< T[N] >
     177             : {
     178             : };
     179             : 
     180             : } // namespace detail
     181             : 
     182             : #define BOOST_SP_MSD( T ) boost::detail::sp_inplace_tag< boost::detail::sp_ms_deleter< T > >()
     183             : 
     184             : // _noinit versions
     185             : 
     186             : template< class T > typename boost::detail::sp_if_not_array< T >::type make_shared_noinit()
     187             : {
     188             :     boost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );
     189             : 
     190             :     boost::detail::sp_ms_deleter< T > * pd = static_cast<boost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );
     191             : 
     192             :     void * pv = pd->address();
     193             : 
     194             :     ::new( pv ) T;
     195             :     pd->set_initialized();
     196             : 
     197             :     T * pt2 = static_cast< T* >( pv );
     198             : 
     199             :     boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
     200             :     return boost::shared_ptr< T >( pt, pt2 );
     201             : }
     202             : 
     203             : template< class T, class A > typename boost::detail::sp_if_not_array< T >::type allocate_shared_noinit( A const & a )
     204             : {
     205             :     boost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );
     206             : 
     207             :     boost::detail::sp_ms_deleter< T > * pd = static_cast<boost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );
     208             : 
     209             :     void * pv = pd->address();
     210             : 
     211             :     ::new( pv ) T;
     212             :     pd->set_initialized();
     213             : 
     214             :     T * pt2 = static_cast< T* >( pv );
     215             : 
     216             :     boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
     217             :     return boost::shared_ptr< T >( pt, pt2 );
     218             : }
     219             : 
     220             : //
     221             : 
     222        4170 : template< class T, class... Args > typename boost::detail::sp_if_not_array< T >::type make_shared( Args && ... args )
     223             : {
     224        4170 :     boost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );
     225             : 
     226        4170 :     boost::detail::sp_ms_deleter< T > * pd = static_cast<boost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );
     227             : 
     228        4170 :     void * pv = pd->address();
     229             : 
     230        4170 :     ::new( pv ) T( std::forward<Args>( args )... );
     231        4170 :     pd->set_initialized();
     232             : 
     233        4170 :     T * pt2 = static_cast< T* >( pv );
     234             : 
     235        4170 :     boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
     236        4170 :     return boost::shared_ptr< T >( pt, pt2 );
     237        4170 : }
     238             : 
     239             : template< class T, class A, class... Args > typename boost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, Args && ... args )
     240             : {
     241             :     typedef typename std::allocator_traits<A>::template rebind_alloc<T> A2;
     242             :     A2 a2( a );
     243             : 
     244             :     typedef boost::detail::sp_as_deleter< T, A2 > D;
     245             : 
     246             :     boost::shared_ptr< T > pt( static_cast< T* >( 0 ), boost::detail::sp_inplace_tag<D>(), a2 );
     247             : 
     248             :     D * pd = static_cast< D* >( pt._internal_get_untyped_deleter() );
     249             :     void * pv = pd->address();
     250             : 
     251             :     std::allocator_traits<A2>::construct( a2, static_cast< T* >( pv ), std::forward<Args>( args )... );
     252             : 
     253             :     pd->set_initialized();
     254             : 
     255             :     T * pt2 = static_cast< T* >( pv );
     256             : 
     257             :     boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
     258             :     return boost::shared_ptr< T >( pt, pt2 );
     259             : }
     260             : 
     261             : #undef BOOST_SP_MSD
     262             : 
     263             : } // namespace boost
     264             : 
     265             : #endif // #ifndef BOOST_SMART_PTR_MAKE_SHARED_OBJECT_HPP_INCLUDED

Generated by: LCOV version 1.16