make_shared_object.hpp 6.0 KB

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