1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef _LIBCPP___MEMORY_SHARED_PTR_H 11 #define _LIBCPP___MEMORY_SHARED_PTR_H 12 13 #include <__availability> 14 #include <__compare/compare_three_way.h> 15 #include <__compare/ordering.h> 16 #include <__config> 17 #include <__exception/exception.h> 18 #include <__functional/binary_function.h> 19 #include <__functional/operations.h> 20 #include <__functional/reference_wrapper.h> 21 #include <__fwd/ostream.h> 22 #include <__iterator/access.h> 23 #include <__memory/addressof.h> 24 #include <__memory/allocation_guard.h> 25 #include <__memory/allocator.h> 26 #include <__memory/allocator_destructor.h> 27 #include <__memory/allocator_traits.h> 28 #include <__memory/auto_ptr.h> 29 #include <__memory/compressed_pair.h> 30 #include <__memory/construct_at.h> 31 #include <__memory/pointer_traits.h> 32 #include <__memory/uninitialized_algorithms.h> 33 #include <__memory/unique_ptr.h> 34 #include <__type_traits/add_lvalue_reference.h> 35 #include <__type_traits/conditional.h> 36 #include <__type_traits/conjunction.h> 37 #include <__type_traits/disjunction.h> 38 #include <__type_traits/is_array.h> 39 #include <__type_traits/is_bounded_array.h> 40 #include <__type_traits/is_convertible.h> 41 #include <__type_traits/is_move_constructible.h> 42 #include <__type_traits/is_reference.h> 43 #include <__type_traits/is_unbounded_array.h> 44 #include <__type_traits/nat.h> 45 #include <__type_traits/negation.h> 46 #include <__type_traits/remove_extent.h> 47 #include <__type_traits/remove_reference.h> 48 #include <__utility/declval.h> 49 #include <__utility/forward.h> 50 #include <__utility/move.h> 51 #include <__utility/swap.h> 52 #include <__verbose_abort> 53 #include <cstddef> 54 #include <new> 55 #include <typeinfo> 56 #if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER) 57 # include <__atomic/memory_order.h> 58 #endif 59 60 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 61 # pragma GCC system_header 62 #endif 63 64 _LIBCPP_BEGIN_NAMESPACE_STD 65 66 // NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively) 67 // should be sufficient for thread safety. 68 // See https://llvm.org/PR22803 69 #if defined(__clang__) && __has_builtin(__atomic_add_fetch) && defined(__ATOMIC_RELAXED) && defined(__ATOMIC_ACQ_REL) 70 # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT 71 #elif defined(_LIBCPP_COMPILER_GCC) 72 # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT 73 #endif 74 75 template <class _ValueType> 76 inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_relaxed_load(_ValueType const* __value) { 77 #if !defined(_LIBCPP_HAS_NO_THREADS) && defined(__ATOMIC_RELAXED) && \ 78 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC)) 79 return __atomic_load_n(__value, __ATOMIC_RELAXED); 80 #else 81 return *__value; 82 #endif 83 } 84 85 template <class _ValueType> 86 inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_acquire_load(_ValueType const* __value) { 87 #if !defined(_LIBCPP_HAS_NO_THREADS) && defined(__ATOMIC_ACQUIRE) && \ 88 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC)) 89 return __atomic_load_n(__value, __ATOMIC_ACQUIRE); 90 #else 91 return *__value; 92 #endif 93 } 94 95 template <class _Tp> 96 inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT { 97 #if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS) 98 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED); 99 #else 100 return __t += 1; 101 #endif 102 } 103 104 template <class _Tp> 105 inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT { 106 #if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS) 107 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL); 108 #else 109 return __t -= 1; 110 #endif 111 } 112 113 class _LIBCPP_EXPORTED_FROM_ABI bad_weak_ptr : public std::exception { 114 public: 115 _LIBCPP_HIDE_FROM_ABI bad_weak_ptr() _NOEXCEPT = default; 116 _LIBCPP_HIDE_FROM_ABI bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default; 117 _LIBCPP_HIDE_FROM_ABI bad_weak_ptr& operator=(const bad_weak_ptr&) _NOEXCEPT = default; 118 ~bad_weak_ptr() _NOEXCEPT override; 119 const char* what() const _NOEXCEPT override; 120 }; 121 122 _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_weak_ptr() { 123 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 124 throw bad_weak_ptr(); 125 #else 126 _LIBCPP_VERBOSE_ABORT("bad_weak_ptr was thrown in -fno-exceptions mode"); 127 #endif 128 } 129 130 template <class _Tp> 131 class _LIBCPP_TEMPLATE_VIS weak_ptr; 132 133 class _LIBCPP_EXPORTED_FROM_ABI __shared_count { 134 __shared_count(const __shared_count&); 135 __shared_count& operator=(const __shared_count&); 136 137 protected: 138 long __shared_owners_; 139 virtual ~__shared_count(); 140 141 private: 142 virtual void __on_zero_shared() _NOEXCEPT = 0; 143 144 public: 145 _LIBCPP_HIDE_FROM_ABI explicit __shared_count(long __refs = 0) _NOEXCEPT : __shared_owners_(__refs) {} 146 147 #if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS) 148 void __add_shared() noexcept; 149 bool __release_shared() noexcept; 150 #else 151 _LIBCPP_HIDE_FROM_ABI void __add_shared() _NOEXCEPT { __libcpp_atomic_refcount_increment(__shared_owners_); } 152 _LIBCPP_HIDE_FROM_ABI bool __release_shared() _NOEXCEPT { 153 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) { 154 __on_zero_shared(); 155 return true; 156 } 157 return false; 158 } 159 #endif 160 _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __libcpp_relaxed_load(&__shared_owners_) + 1; } 161 }; 162 163 class _LIBCPP_EXPORTED_FROM_ABI __shared_weak_count : private __shared_count { 164 long __shared_weak_owners_; 165 166 public: 167 _LIBCPP_HIDE_FROM_ABI explicit __shared_weak_count(long __refs = 0) _NOEXCEPT 168 : __shared_count(__refs), 169 __shared_weak_owners_(__refs) {} 170 171 protected: 172 ~__shared_weak_count() override; 173 174 public: 175 #if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS) 176 void __add_shared() noexcept; 177 void __add_weak() noexcept; 178 void __release_shared() noexcept; 179 #else 180 _LIBCPP_HIDE_FROM_ABI void __add_shared() _NOEXCEPT { __shared_count::__add_shared(); } 181 _LIBCPP_HIDE_FROM_ABI void __add_weak() _NOEXCEPT { __libcpp_atomic_refcount_increment(__shared_weak_owners_); } 182 _LIBCPP_HIDE_FROM_ABI void __release_shared() _NOEXCEPT { 183 if (__shared_count::__release_shared()) 184 __release_weak(); 185 } 186 #endif 187 void __release_weak() _NOEXCEPT; 188 _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __shared_count::use_count(); } 189 __shared_weak_count* lock() _NOEXCEPT; 190 191 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT; 192 193 private: 194 virtual void __on_zero_shared_weak() _NOEXCEPT = 0; 195 }; 196 197 template <class _Tp, class _Dp, class _Alloc> 198 class __shared_ptr_pointer : public __shared_weak_count { 199 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_; 200 201 public: 202 _LIBCPP_HIDE_FROM_ABI __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a) 203 : __data_(__compressed_pair<_Tp, _Dp>(__p, std::move(__d)), std::move(__a)) {} 204 205 #ifndef _LIBCPP_HAS_NO_RTTI 206 _LIBCPP_HIDE_FROM_ABI_VIRTUAL const void* __get_deleter(const type_info&) const _NOEXCEPT override; 207 #endif 208 209 private: 210 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override; 211 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override; 212 }; 213 214 #ifndef _LIBCPP_HAS_NO_RTTI 215 216 template <class _Tp, class _Dp, class _Alloc> 217 const void* __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT { 218 return __t == typeid(_Dp) ? std::addressof(__data_.first().second()) : nullptr; 219 } 220 221 #endif // _LIBCPP_HAS_NO_RTTI 222 223 template <class _Tp, class _Dp, class _Alloc> 224 void __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT { 225 __data_.first().second()(__data_.first().first()); 226 __data_.first().second().~_Dp(); 227 } 228 229 template <class _Tp, class _Dp, class _Alloc> 230 void __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT { 231 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al; 232 typedef allocator_traits<_Al> _ATraits; 233 typedef pointer_traits<typename _ATraits::pointer> _PTraits; 234 235 _Al __a(__data_.second()); 236 __data_.second().~_Alloc(); 237 __a.deallocate(_PTraits::pointer_to(*this), 1); 238 } 239 240 // This tag is used to instantiate an allocator type. The various shared_ptr control blocks 241 // detect that the allocator has been instantiated for this type and perform alternative 242 // initialization/destruction based on that. 243 struct __for_overwrite_tag {}; 244 245 template <class _Tp, class _Alloc> 246 struct __shared_ptr_emplace : __shared_weak_count { 247 template <class... _Args, 248 class _Allocator = _Alloc, 249 __enable_if_t<is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0> 250 _LIBCPP_HIDE_FROM_ABI explicit __shared_ptr_emplace(_Alloc __a, _Args&&...) : __storage_(std::move(__a)) { 251 static_assert( 252 sizeof...(_Args) == 0, "No argument should be provided to the control block when using _for_overwrite"); 253 ::new ((void*)__get_elem()) _Tp; 254 } 255 256 template <class... _Args, 257 class _Allocator = _Alloc, 258 __enable_if_t<!is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0> 259 _LIBCPP_HIDE_FROM_ABI explicit __shared_ptr_emplace(_Alloc __a, _Args&&... __args) : __storage_(std::move(__a)) { 260 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type; 261 _TpAlloc __tmp(*__get_alloc()); 262 allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), std::forward<_Args>(__args)...); 263 } 264 265 _LIBCPP_HIDE_FROM_ABI _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); } 266 267 _LIBCPP_HIDE_FROM_ABI _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); } 268 269 private: 270 template <class _Allocator = _Alloc, 271 __enable_if_t<is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0> 272 _LIBCPP_HIDE_FROM_ABI void __on_zero_shared_impl() _NOEXCEPT { 273 __get_elem()->~_Tp(); 274 } 275 276 template <class _Allocator = _Alloc, 277 __enable_if_t<!is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0> 278 _LIBCPP_HIDE_FROM_ABI void __on_zero_shared_impl() _NOEXCEPT { 279 using _TpAlloc = typename __allocator_traits_rebind<_Allocator, _Tp>::type; 280 _TpAlloc __tmp(*__get_alloc()); 281 allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem()); 282 } 283 284 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override { __on_zero_shared_impl(); } 285 286 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override { 287 using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type; 288 using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer; 289 _ControlBlockAlloc __tmp(*__get_alloc()); 290 __storage_.~_Storage(); 291 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp, pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1); 292 } 293 294 // This class implements the control block for non-array shared pointers created 295 // through `std::allocate_shared` and `std::make_shared`. 296 // 297 // In previous versions of the library, we used a compressed pair to store 298 // both the _Alloc and the _Tp. This implies using EBO, which is incompatible 299 // with Allocator construction for _Tp. To allow implementing P0674 in C++20, 300 // we now use a properly aligned char buffer while making sure that we maintain 301 // the same layout that we had when we used a compressed pair. 302 using _CompressedPair = __compressed_pair<_Alloc, _Tp>; 303 struct _ALIGNAS_TYPE(_CompressedPair) _Storage { 304 char __blob_[sizeof(_CompressedPair)]; 305 306 _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) { ::new ((void*)__get_alloc()) _Alloc(std::move(__a)); } 307 _LIBCPP_HIDE_FROM_ABI ~_Storage() { __get_alloc()->~_Alloc(); } 308 _LIBCPP_HIDE_FROM_ABI _Alloc* __get_alloc() _NOEXCEPT { 309 _CompressedPair* __as_pair = reinterpret_cast<_CompressedPair*>(__blob_); 310 typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair); 311 _Alloc* __alloc = reinterpret_cast<_Alloc*>(__first); 312 return __alloc; 313 } 314 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT { 315 _CompressedPair* __as_pair = reinterpret_cast<_CompressedPair*>(__blob_); 316 typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair); 317 _Tp* __elem = reinterpret_cast<_Tp*>(__second); 318 return __elem; 319 } 320 }; 321 322 static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), ""); 323 static_assert(sizeof(_Storage) == sizeof(_CompressedPair), ""); 324 _Storage __storage_; 325 }; 326 327 struct __shared_ptr_dummy_rebind_allocator_type; 328 template <> 329 class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type> { 330 public: 331 template <class _Other> 332 struct rebind { 333 typedef allocator<_Other> other; 334 }; 335 }; 336 337 template <class _Tp> 338 class _LIBCPP_TEMPLATE_VIS enable_shared_from_this; 339 340 // http://eel.is/c++draft/util.sharedptr#util.smartptr.shared.general-6 341 // A pointer type Y* is said to be compatible with a pointer type T* 342 // when either Y* is convertible to T* or Y is U[N] and T is cv U[]. 343 #if _LIBCPP_STD_VER >= 17 344 template <class _Yp, class _Tp> 345 struct __bounded_convertible_to_unbounded : false_type {}; 346 347 template <class _Up, std::size_t _Np, class _Tp> 348 struct __bounded_convertible_to_unbounded<_Up[_Np], _Tp> : is_same<__remove_cv_t<_Tp>, _Up[]> {}; 349 350 template <class _Yp, class _Tp> 351 struct __compatible_with : _Or< is_convertible<_Yp*, _Tp*>, __bounded_convertible_to_unbounded<_Yp, _Tp> > {}; 352 #else 353 template <class _Yp, class _Tp> 354 struct __compatible_with : is_convertible<_Yp*, _Tp*> {}; 355 #endif // _LIBCPP_STD_VER >= 17 356 357 // Constructors that take raw pointers have a different set of "compatible" constraints 358 // http://eel.is/c++draft/util.sharedptr#util.smartptr.shared.const-9.1 359 // - If T is an array type, then either T is U[N] and Y(*)[N] is convertible to T*, 360 // or T is U[] and Y(*)[] is convertible to T*. 361 // - If T is not an array type, then Y* is convertible to T*. 362 #if _LIBCPP_STD_VER >= 17 363 template <class _Yp, class _Tp, class = void> 364 struct __raw_pointer_compatible_with : _And< _Not<is_array<_Tp>>, is_convertible<_Yp*, _Tp*> > {}; 365 366 template <class _Yp, class _Up, std::size_t _Np> 367 struct __raw_pointer_compatible_with<_Yp, _Up[_Np], __enable_if_t< is_convertible<_Yp (*)[_Np], _Up (*)[_Np]>::value> > 368 : true_type {}; 369 370 template <class _Yp, class _Up> 371 struct __raw_pointer_compatible_with<_Yp, _Up[], __enable_if_t< is_convertible<_Yp (*)[], _Up (*)[]>::value> > 372 : true_type {}; 373 374 #else 375 template <class _Yp, class _Tp> 376 struct __raw_pointer_compatible_with : is_convertible<_Yp*, _Tp*> {}; 377 #endif // _LIBCPP_STD_VER >= 17 378 379 template <class _Ptr, class = void> 380 struct __is_deletable : false_type {}; 381 template <class _Ptr> 382 struct __is_deletable<_Ptr, decltype(delete std::declval<_Ptr>())> : true_type {}; 383 384 template <class _Ptr, class = void> 385 struct __is_array_deletable : false_type {}; 386 template <class _Ptr> 387 struct __is_array_deletable<_Ptr, decltype(delete[] std::declval<_Ptr>())> : true_type {}; 388 389 template <class _Dp, class _Pt, class = decltype(std::declval<_Dp>()(std::declval<_Pt>()))> 390 true_type __well_formed_deleter_test(int); 391 392 template <class, class> 393 false_type __well_formed_deleter_test(...); 394 395 template <class _Dp, class _Pt> 396 struct __well_formed_deleter : decltype(std::__well_formed_deleter_test<_Dp, _Pt>(0)) {}; 397 398 template <class _Dp, class _Yp, class _Tp> 399 struct __shared_ptr_deleter_ctor_reqs { 400 static const bool value = __raw_pointer_compatible_with<_Yp, _Tp>::value && is_move_constructible<_Dp>::value && 401 __well_formed_deleter<_Dp, _Yp*>::value; 402 }; 403 404 #if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI) 405 # define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((__trivial_abi__)) 406 #else 407 # define _LIBCPP_SHARED_PTR_TRIVIAL_ABI 408 #endif 409 410 template <class _Tp> 411 class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr { 412 public: 413 #if _LIBCPP_STD_VER >= 17 414 typedef weak_ptr<_Tp> weak_type; 415 typedef remove_extent_t<_Tp> element_type; 416 #else 417 typedef _Tp element_type; 418 #endif 419 420 private: 421 element_type* __ptr_; 422 __shared_weak_count* __cntrl_; 423 424 public: 425 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {} 426 427 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {} 428 429 template <class _Yp, 430 class = __enable_if_t< _And< __raw_pointer_compatible_with<_Yp, _Tp> 431 // In C++03 we get errors when trying to do SFINAE with the 432 // delete operator, so we always pretend that it's deletable. 433 // The same happens on GCC. 434 #if !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_COMPILER_GCC) 435 , 436 _If<is_array<_Tp>::value, __is_array_deletable<_Yp*>, __is_deletable<_Yp*> > 437 #endif 438 >::value > > 439 _LIBCPP_HIDE_FROM_ABI explicit shared_ptr(_Yp* __p) : __ptr_(__p) { 440 unique_ptr<_Yp> __hold(__p); 441 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; 442 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT> _CntrlBlk; 443 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT()); 444 __hold.release(); 445 __enable_weak_this(__p, __p); 446 } 447 448 template <class _Yp, class _Dp, class = __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value> > 449 _LIBCPP_HIDE_FROM_ABI shared_ptr(_Yp* __p, _Dp __d) : __ptr_(__p) { 450 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 451 try { 452 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 453 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; 454 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT> _CntrlBlk; 455 #ifndef _LIBCPP_CXX03_LANG 456 __cntrl_ = new _CntrlBlk(__p, std::move(__d), _AllocT()); 457 #else 458 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT()); 459 #endif // not _LIBCPP_CXX03_LANG 460 __enable_weak_this(__p, __p); 461 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 462 } catch (...) { 463 __d(__p); 464 throw; 465 } 466 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 467 } 468 469 template <class _Yp, 470 class _Dp, 471 class _Alloc, 472 class = __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value> > 473 _LIBCPP_HIDE_FROM_ABI shared_ptr(_Yp* __p, _Dp __d, _Alloc __a) : __ptr_(__p) { 474 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 475 try { 476 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 477 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk; 478 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2; 479 typedef __allocator_destructor<_A2> _D2; 480 _A2 __a2(__a); 481 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1)); 482 ::new ((void*)std::addressof(*__hold2.get())) 483 #ifndef _LIBCPP_CXX03_LANG 484 _CntrlBlk(__p, std::move(__d), __a); 485 #else 486 _CntrlBlk(__p, __d, __a); 487 #endif // not _LIBCPP_CXX03_LANG 488 __cntrl_ = std::addressof(*__hold2.release()); 489 __enable_weak_this(__p, __p); 490 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 491 } catch (...) { 492 __d(__p); 493 throw; 494 } 495 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 496 } 497 498 template <class _Dp> 499 _LIBCPP_HIDE_FROM_ABI shared_ptr(nullptr_t __p, _Dp __d) : __ptr_(nullptr) { 500 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 501 try { 502 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 503 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT; 504 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT> _CntrlBlk; 505 #ifndef _LIBCPP_CXX03_LANG 506 __cntrl_ = new _CntrlBlk(__p, std::move(__d), _AllocT()); 507 #else 508 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT()); 509 #endif // not _LIBCPP_CXX03_LANG 510 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 511 } catch (...) { 512 __d(__p); 513 throw; 514 } 515 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 516 } 517 518 template <class _Dp, class _Alloc> 519 _LIBCPP_HIDE_FROM_ABI shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a) : __ptr_(nullptr) { 520 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 521 try { 522 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 523 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk; 524 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2; 525 typedef __allocator_destructor<_A2> _D2; 526 _A2 __a2(__a); 527 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1)); 528 ::new ((void*)std::addressof(*__hold2.get())) 529 #ifndef _LIBCPP_CXX03_LANG 530 _CntrlBlk(__p, std::move(__d), __a); 531 #else 532 _CntrlBlk(__p, __d, __a); 533 #endif // not _LIBCPP_CXX03_LANG 534 __cntrl_ = std::addressof(*__hold2.release()); 535 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 536 } catch (...) { 537 __d(__p); 538 throw; 539 } 540 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 541 } 542 543 template <class _Yp> 544 _LIBCPP_HIDE_FROM_ABI shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT 545 : __ptr_(__p), 546 __cntrl_(__r.__cntrl_) { 547 if (__cntrl_) 548 __cntrl_->__add_shared(); 549 } 550 551 // LWG-2996 552 // We don't backport because it is an evolutionary change. 553 #if _LIBCPP_STD_VER >= 20 554 template <class _Yp> 555 _LIBCPP_HIDE_FROM_ABI shared_ptr(shared_ptr<_Yp>&& __r, element_type* __p) noexcept 556 : __ptr_(__p), __cntrl_(__r.__cntrl_) { 557 __r.__ptr_ = nullptr; 558 __r.__cntrl_ = nullptr; 559 } 560 #endif 561 562 _LIBCPP_HIDE_FROM_ABI shared_ptr(const shared_ptr& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) { 563 if (__cntrl_) 564 __cntrl_->__add_shared(); 565 } 566 567 template <class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> > 568 _LIBCPP_HIDE_FROM_ABI shared_ptr(const shared_ptr<_Yp>& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) { 569 if (__cntrl_) 570 __cntrl_->__add_shared(); 571 } 572 573 _LIBCPP_HIDE_FROM_ABI shared_ptr(shared_ptr&& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) { 574 __r.__ptr_ = nullptr; 575 __r.__cntrl_ = nullptr; 576 } 577 578 template <class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> > 579 _LIBCPP_HIDE_FROM_ABI shared_ptr(shared_ptr<_Yp>&& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) { 580 __r.__ptr_ = nullptr; 581 __r.__cntrl_ = nullptr; 582 } 583 584 template <class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> > 585 _LIBCPP_HIDE_FROM_ABI explicit shared_ptr(const weak_ptr<_Yp>& __r) 586 : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_) { 587 if (__cntrl_ == nullptr) 588 __throw_bad_weak_ptr(); 589 } 590 591 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) 592 template <class _Yp, class = __enable_if_t<is_convertible<_Yp*, element_type*>::value> > 593 _LIBCPP_HIDE_FROM_ABI shared_ptr(auto_ptr<_Yp>&& __r) : __ptr_(__r.get()) { 594 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk; 595 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>()); 596 __enable_weak_this(__r.get(), __r.get()); 597 __r.release(); 598 } 599 #endif 600 601 template <class _Yp, 602 class _Dp, 603 class = __enable_if_t< !is_lvalue_reference<_Dp>::value && __compatible_with<_Yp, _Tp>::value && 604 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value > > 605 _LIBCPP_HIDE_FROM_ABI shared_ptr(unique_ptr<_Yp, _Dp>&& __r) : __ptr_(__r.get()) { 606 #if _LIBCPP_STD_VER >= 14 607 if (__ptr_ == nullptr) 608 __cntrl_ = nullptr; 609 else 610 #endif 611 { 612 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; 613 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT> _CntrlBlk; 614 __cntrl_ = new _CntrlBlk(__r.get(), std::move(__r.get_deleter()), _AllocT()); 615 __enable_weak_this(__r.get(), __r.get()); 616 } 617 __r.release(); 618 } 619 620 template <class _Yp, 621 class _Dp, 622 class = void, 623 class = __enable_if_t< is_lvalue_reference<_Dp>::value && __compatible_with<_Yp, _Tp>::value && 624 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value > > 625 _LIBCPP_HIDE_FROM_ABI shared_ptr(unique_ptr<_Yp, _Dp>&& __r) : __ptr_(__r.get()) { 626 #if _LIBCPP_STD_VER >= 14 627 if (__ptr_ == nullptr) 628 __cntrl_ = nullptr; 629 else 630 #endif 631 { 632 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; 633 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, 634 reference_wrapper<__libcpp_remove_reference_t<_Dp> >, 635 _AllocT> 636 _CntrlBlk; 637 __cntrl_ = new _CntrlBlk(__r.get(), std::ref(__r.get_deleter()), _AllocT()); 638 __enable_weak_this(__r.get(), __r.get()); 639 } 640 __r.release(); 641 } 642 643 _LIBCPP_HIDE_FROM_ABI ~shared_ptr() { 644 if (__cntrl_) 645 __cntrl_->__release_shared(); 646 } 647 648 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(const shared_ptr& __r) _NOEXCEPT { 649 shared_ptr(__r).swap(*this); 650 return *this; 651 } 652 653 template <class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> > 654 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT { 655 shared_ptr(__r).swap(*this); 656 return *this; 657 } 658 659 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(shared_ptr&& __r) _NOEXCEPT { 660 shared_ptr(std::move(__r)).swap(*this); 661 return *this; 662 } 663 664 template <class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> > 665 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(shared_ptr<_Yp>&& __r) { 666 shared_ptr(std::move(__r)).swap(*this); 667 return *this; 668 } 669 670 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) 671 template <class _Yp, 672 class = __enable_if_t< !is_array<_Yp>::value && 673 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value > > 674 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(auto_ptr<_Yp>&& __r) { 675 shared_ptr(std::move(__r)).swap(*this); 676 return *this; 677 } 678 #endif 679 680 template < 681 class _Yp, 682 class _Dp, 683 class = __enable_if_t<_And< __compatible_with<_Yp, _Tp>, 684 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*> >::value> > 685 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(unique_ptr<_Yp, _Dp>&& __r) { 686 shared_ptr(std::move(__r)).swap(*this); 687 return *this; 688 } 689 690 _LIBCPP_HIDE_FROM_ABI void swap(shared_ptr& __r) _NOEXCEPT { 691 std::swap(__ptr_, __r.__ptr_); 692 std::swap(__cntrl_, __r.__cntrl_); 693 } 694 695 _LIBCPP_HIDE_FROM_ABI void reset() _NOEXCEPT { shared_ptr().swap(*this); } 696 697 template <class _Yp, class = __enable_if_t< __raw_pointer_compatible_with<_Yp, _Tp>::value > > 698 _LIBCPP_HIDE_FROM_ABI void reset(_Yp* __p) { 699 shared_ptr(__p).swap(*this); 700 } 701 702 template <class _Yp, class _Dp, class = __enable_if_t< __shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value> > 703 _LIBCPP_HIDE_FROM_ABI void reset(_Yp* __p, _Dp __d) { 704 shared_ptr(__p, __d).swap(*this); 705 } 706 707 template <class _Yp, 708 class _Dp, 709 class _Alloc, 710 class = __enable_if_t< __shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value> > 711 _LIBCPP_HIDE_FROM_ABI void reset(_Yp* __p, _Dp __d, _Alloc __a) { 712 shared_ptr(__p, __d, __a).swap(*this); 713 } 714 715 _LIBCPP_HIDE_FROM_ABI element_type* get() const _NOEXCEPT { return __ptr_; } 716 717 _LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<element_type> operator*() const _NOEXCEPT { return *__ptr_; } 718 719 _LIBCPP_HIDE_FROM_ABI element_type* operator->() const _NOEXCEPT { 720 static_assert(!is_array<_Tp>::value, "std::shared_ptr<T>::operator-> is only valid when T is not an array type."); 721 return __ptr_; 722 } 723 724 _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __cntrl_ ? __cntrl_->use_count() : 0; } 725 726 #if _LIBCPP_STD_VER < 20 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_SHARED_PTR_UNIQUE) 727 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI bool unique() const _NOEXCEPT { return use_count() == 1; } 728 #endif 729 730 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return get() != nullptr; } 731 732 template <class _Up> 733 _LIBCPP_HIDE_FROM_ABI bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT { 734 return __cntrl_ < __p.__cntrl_; 735 } 736 737 template <class _Up> 738 _LIBCPP_HIDE_FROM_ABI bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT { 739 return __cntrl_ < __p.__cntrl_; 740 } 741 742 _LIBCPP_HIDE_FROM_ABI bool __owner_equivalent(const shared_ptr& __p) const { return __cntrl_ == __p.__cntrl_; } 743 744 #if _LIBCPP_STD_VER >= 17 745 _LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<element_type> operator[](ptrdiff_t __i) const { 746 static_assert(is_array<_Tp>::value, "std::shared_ptr<T>::operator[] is only valid when T is an array type."); 747 return __ptr_[__i]; 748 } 749 #endif 750 751 #ifndef _LIBCPP_HAS_NO_RTTI 752 template <class _Dp> 753 _LIBCPP_HIDE_FROM_ABI _Dp* __get_deleter() const _NOEXCEPT { 754 return static_cast<_Dp*>(__cntrl_ ? const_cast<void*>(__cntrl_->__get_deleter(typeid(_Dp))) : nullptr); 755 } 756 #endif // _LIBCPP_HAS_NO_RTTI 757 758 template <class _Yp, class _CntrlBlk> 759 _LIBCPP_HIDE_FROM_ABI static shared_ptr<_Tp> __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT { 760 shared_ptr<_Tp> __r; 761 __r.__ptr_ = __p; 762 __r.__cntrl_ = __cntrl; 763 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_); 764 return __r; 765 } 766 767 private: 768 template <class _Yp, bool = is_function<_Yp>::value> 769 struct __shared_ptr_default_allocator { 770 typedef allocator<_Yp> type; 771 }; 772 773 template <class _Yp> 774 struct __shared_ptr_default_allocator<_Yp, true> { 775 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type; 776 }; 777 778 template <class _Yp, 779 class _OrigPtr, 780 class = __enable_if_t< is_convertible<_OrigPtr*, const enable_shared_from_this<_Yp>*>::value > > 781 _LIBCPP_HIDE_FROM_ABI void __enable_weak_this(const enable_shared_from_this<_Yp>* __e, _OrigPtr* __ptr) _NOEXCEPT { 782 typedef __remove_cv_t<_Yp> _RawYp; 783 if (__e && __e->__weak_this_.expired()) { 784 __e->__weak_this_ = shared_ptr<_RawYp>(*this, const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr))); 785 } 786 } 787 788 _LIBCPP_HIDE_FROM_ABI void __enable_weak_this(...) _NOEXCEPT {} 789 790 template <class, class _Yp> 791 struct __shared_ptr_default_delete : default_delete<_Yp> {}; 792 793 template <class _Yp, class _Un, size_t _Sz> 794 struct __shared_ptr_default_delete<_Yp[_Sz], _Un> : default_delete<_Yp[]> {}; 795 796 template <class _Yp, class _Un> 797 struct __shared_ptr_default_delete<_Yp[], _Un> : default_delete<_Yp[]> {}; 798 799 template <class _Up> 800 friend class _LIBCPP_TEMPLATE_VIS shared_ptr; 801 template <class _Up> 802 friend class _LIBCPP_TEMPLATE_VIS weak_ptr; 803 }; 804 805 #if _LIBCPP_STD_VER >= 17 806 template <class _Tp> 807 shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>; 808 template <class _Tp, class _Dp> 809 shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>; 810 #endif 811 812 // 813 // std::allocate_shared and std::make_shared 814 // 815 template <class _Tp, class _Alloc, class... _Args, class = __enable_if_t<!is_array<_Tp>::value> > 816 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&&... __args) { 817 using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>; 818 using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type; 819 __allocation_guard<_ControlBlockAllocator> __guard(__a, 1); 820 ::new ((void*)std::addressof(*__guard.__get())) _ControlBlock(__a, std::forward<_Args>(__args)...); 821 auto __control_block = __guard.__release_ptr(); 822 return shared_ptr<_Tp>::__create_with_control_block( 823 (*__control_block).__get_elem(), std::addressof(*__control_block)); 824 } 825 826 template <class _Tp, class... _Args, class = __enable_if_t<!is_array<_Tp>::value> > 827 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(_Args&&... __args) { 828 return std::allocate_shared<_Tp>(allocator<_Tp>(), std::forward<_Args>(__args)...); 829 } 830 831 #if _LIBCPP_STD_VER >= 20 832 833 template <class _Tp, class _Alloc, __enable_if_t<!is_array<_Tp>::value, int> = 0> 834 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a) { 835 using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>; 836 _ForOverwriteAllocator __alloc(__a); 837 return std::allocate_shared<_Tp>(__alloc); 838 } 839 840 template <class _Tp, __enable_if_t<!is_array<_Tp>::value, int> = 0> 841 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite() { 842 return std::allocate_shared_for_overwrite<_Tp>(allocator<_Tp>()); 843 } 844 845 #endif // _LIBCPP_STD_VER >= 20 846 847 #if _LIBCPP_STD_VER >= 17 848 849 template <size_t _Alignment> 850 struct __sp_aligned_storage { 851 alignas(_Alignment) char __storage[_Alignment]; 852 }; 853 854 template <class _Tp, class _Alloc> 855 struct __unbounded_array_control_block; 856 857 template <class _Tp, class _Alloc> 858 struct __unbounded_array_control_block<_Tp[], _Alloc> : __shared_weak_count { 859 _LIBCPP_HIDE_FROM_ABI constexpr _Tp* __get_data() noexcept { return __data_; } 860 861 _LIBCPP_HIDE_FROM_ABI explicit __unbounded_array_control_block( 862 _Alloc const& __alloc, size_t __count, _Tp const& __arg) 863 : __alloc_(__alloc), __count_(__count) { 864 std::__uninitialized_allocator_fill_n_multidimensional(__alloc_, std::begin(__data_), __count_, __arg); 865 } 866 867 _LIBCPP_HIDE_FROM_ABI explicit __unbounded_array_control_block(_Alloc const& __alloc, size_t __count) 868 : __alloc_(__alloc), __count_(__count) { 869 # if _LIBCPP_STD_VER >= 20 870 if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) { 871 // We are purposefully not using an allocator-aware default construction because the spec says so. 872 // There's currently no way of expressing default initialization in an allocator-aware manner anyway. 873 std::uninitialized_default_construct_n(std::begin(__data_), __count_); 874 } else { 875 std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::begin(__data_), __count_); 876 } 877 # else 878 std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::begin(__data_), __count_); 879 # endif 880 } 881 882 // Returns the number of bytes required to store a control block followed by the given number 883 // of elements of _Tp, with the whole storage being aligned to a multiple of _Tp's alignment. 884 _LIBCPP_HIDE_FROM_ABI static constexpr size_t __bytes_for(size_t __elements) { 885 // When there's 0 elements, the control block alone is enough since it holds one element. 886 // Otherwise, we allocate one fewer element than requested because the control block already 887 // holds one. Also, we use the bitwise formula below to ensure that we allocate enough bytes 888 // for the whole allocation to be a multiple of _Tp's alignment. That formula is taken from [1]. 889 // 890 // [1]: https://en.wikipedia.org/wiki/Data_structure_alignment#Computing_padding 891 size_t __bytes = __elements == 0 ? sizeof(__unbounded_array_control_block) 892 : (__elements - 1) * sizeof(_Tp) + sizeof(__unbounded_array_control_block); 893 constexpr size_t __align = alignof(_Tp); 894 return (__bytes + __align - 1) & ~(__align - 1); 895 } 896 897 _LIBCPP_HIDE_FROM_ABI_VIRTUAL 898 ~__unbounded_array_control_block() override { 899 } // can't be `= default` because of the sometimes-non-trivial union member __data_ 900 901 private: 902 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override { 903 # if _LIBCPP_STD_VER >= 20 904 if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) { 905 std::__reverse_destroy(__data_, __data_ + __count_); 906 } else { 907 __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_); 908 std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + __count_); 909 } 910 # else 911 __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_); 912 std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + __count_); 913 # endif 914 } 915 916 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override { 917 using _AlignedStorage = __sp_aligned_storage<alignof(__unbounded_array_control_block)>; 918 using _StorageAlloc = __allocator_traits_rebind_t<_Alloc, _AlignedStorage>; 919 using _PointerTraits = pointer_traits<typename allocator_traits<_StorageAlloc>::pointer>; 920 921 _StorageAlloc __tmp(__alloc_); 922 __alloc_.~_Alloc(); 923 size_t __size = __unbounded_array_control_block::__bytes_for(__count_); 924 _AlignedStorage* __storage = reinterpret_cast<_AlignedStorage*>(this); 925 allocator_traits<_StorageAlloc>::deallocate( 926 __tmp, _PointerTraits::pointer_to(*__storage), __size / sizeof(_AlignedStorage)); 927 } 928 929 _LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_; 930 size_t __count_; 931 union { 932 _Tp __data_[1]; 933 }; 934 }; 935 936 template <class _Array, class _Alloc, class... _Arg> 937 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Array> 938 __allocate_shared_unbounded_array(const _Alloc& __a, size_t __n, _Arg&&... __arg) { 939 static_assert(__libcpp_is_unbounded_array<_Array>::value); 940 // We compute the number of bytes necessary to hold the control block and the 941 // array elements. Then, we allocate an array of properly-aligned dummy structs 942 // large enough to hold the control block and array. This allows shifting the 943 // burden of aligning memory properly from us to the allocator. 944 using _ControlBlock = __unbounded_array_control_block<_Array, _Alloc>; 945 using _AlignedStorage = __sp_aligned_storage<alignof(_ControlBlock)>; 946 using _StorageAlloc = __allocator_traits_rebind_t<_Alloc, _AlignedStorage>; 947 __allocation_guard<_StorageAlloc> __guard(__a, _ControlBlock::__bytes_for(__n) / sizeof(_AlignedStorage)); 948 _ControlBlock* __control_block = reinterpret_cast<_ControlBlock*>(std::addressof(*__guard.__get())); 949 std::__construct_at(__control_block, __a, __n, std::forward<_Arg>(__arg)...); 950 __guard.__release_ptr(); 951 return shared_ptr<_Array>::__create_with_control_block(__control_block->__get_data(), __control_block); 952 } 953 954 template <class _Tp, class _Alloc> 955 struct __bounded_array_control_block; 956 957 template <class _Tp, size_t _Count, class _Alloc> 958 struct __bounded_array_control_block<_Tp[_Count], _Alloc> : __shared_weak_count { 959 _LIBCPP_HIDE_FROM_ABI constexpr _Tp* __get_data() noexcept { return __data_; } 960 961 _LIBCPP_HIDE_FROM_ABI explicit __bounded_array_control_block(_Alloc const& __alloc, _Tp const& __arg) 962 : __alloc_(__alloc) { 963 std::__uninitialized_allocator_fill_n_multidimensional(__alloc_, std::addressof(__data_[0]), _Count, __arg); 964 } 965 966 _LIBCPP_HIDE_FROM_ABI explicit __bounded_array_control_block(_Alloc const& __alloc) : __alloc_(__alloc) { 967 # if _LIBCPP_STD_VER >= 20 968 if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) { 969 // We are purposefully not using an allocator-aware default construction because the spec says so. 970 // There's currently no way of expressing default initialization in an allocator-aware manner anyway. 971 std::uninitialized_default_construct_n(std::addressof(__data_[0]), _Count); 972 } else { 973 std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::addressof(__data_[0]), _Count); 974 } 975 # else 976 std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::addressof(__data_[0]), _Count); 977 # endif 978 } 979 980 _LIBCPP_HIDE_FROM_ABI_VIRTUAL 981 ~__bounded_array_control_block() override { 982 } // can't be `= default` because of the sometimes-non-trivial union member __data_ 983 984 private: 985 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override { 986 # if _LIBCPP_STD_VER >= 20 987 if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) { 988 std::__reverse_destroy(__data_, __data_ + _Count); 989 } else { 990 __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_); 991 std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + _Count); 992 } 993 # else 994 __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_); 995 std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + _Count); 996 # endif 997 } 998 999 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override { 1000 using _ControlBlockAlloc = __allocator_traits_rebind_t<_Alloc, __bounded_array_control_block>; 1001 using _PointerTraits = pointer_traits<typename allocator_traits<_ControlBlockAlloc>::pointer>; 1002 1003 _ControlBlockAlloc __tmp(__alloc_); 1004 __alloc_.~_Alloc(); 1005 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp, _PointerTraits::pointer_to(*this), 1); 1006 } 1007 1008 _LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_; 1009 union { 1010 _Tp __data_[_Count]; 1011 }; 1012 }; 1013 1014 template <class _Array, class _Alloc, class... _Arg> 1015 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Array> __allocate_shared_bounded_array(const _Alloc& __a, _Arg&&... __arg) { 1016 static_assert(__libcpp_is_bounded_array<_Array>::value); 1017 using _ControlBlock = __bounded_array_control_block<_Array, _Alloc>; 1018 using _ControlBlockAlloc = __allocator_traits_rebind_t<_Alloc, _ControlBlock>; 1019 1020 __allocation_guard<_ControlBlockAlloc> __guard(__a, 1); 1021 _ControlBlock* __control_block = reinterpret_cast<_ControlBlock*>(std::addressof(*__guard.__get())); 1022 std::__construct_at(__control_block, __a, std::forward<_Arg>(__arg)...); 1023 __guard.__release_ptr(); 1024 return shared_ptr<_Array>::__create_with_control_block(__control_block->__get_data(), __control_block); 1025 } 1026 1027 #endif // _LIBCPP_STD_VER >= 17 1028 1029 #if _LIBCPP_STD_VER >= 20 1030 1031 // bounded array variants 1032 template <class _Tp, class _Alloc, class = __enable_if_t<is_bounded_array<_Tp>::value>> 1033 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a) { 1034 return std::__allocate_shared_bounded_array<_Tp>(__a); 1035 } 1036 1037 template <class _Tp, class _Alloc, class = __enable_if_t<is_bounded_array<_Tp>::value>> 1038 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, const remove_extent_t<_Tp>& __u) { 1039 return std::__allocate_shared_bounded_array<_Tp>(__a, __u); 1040 } 1041 1042 template <class _Tp, class _Alloc, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0> 1043 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a) { 1044 using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>; 1045 _ForOverwriteAllocator __alloc(__a); 1046 return std::__allocate_shared_bounded_array<_Tp>(__alloc); 1047 } 1048 1049 template <class _Tp, class = __enable_if_t<is_bounded_array<_Tp>::value>> 1050 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared() { 1051 return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>()); 1052 } 1053 1054 template <class _Tp, class = __enable_if_t<is_bounded_array<_Tp>::value>> 1055 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(const remove_extent_t<_Tp>& __u) { 1056 return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>(), __u); 1057 } 1058 1059 template <class _Tp, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0> 1060 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite() { 1061 return std::__allocate_shared_bounded_array<_Tp>(allocator<__for_overwrite_tag>()); 1062 } 1063 1064 // unbounded array variants 1065 template <class _Tp, class _Alloc, class = __enable_if_t<is_unbounded_array<_Tp>::value>> 1066 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, size_t __n) { 1067 return std::__allocate_shared_unbounded_array<_Tp>(__a, __n); 1068 } 1069 1070 template <class _Tp, class _Alloc, class = __enable_if_t<is_unbounded_array<_Tp>::value>> 1071 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, size_t __n, const remove_extent_t<_Tp>& __u) { 1072 return std::__allocate_shared_unbounded_array<_Tp>(__a, __n, __u); 1073 } 1074 1075 template <class _Tp, class _Alloc, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0> 1076 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a, size_t __n) { 1077 using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>; 1078 _ForOverwriteAllocator __alloc(__a); 1079 return std::__allocate_shared_unbounded_array<_Tp>(__alloc, __n); 1080 } 1081 1082 template <class _Tp, class = __enable_if_t<is_unbounded_array<_Tp>::value>> 1083 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(size_t __n) { 1084 return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n); 1085 } 1086 1087 template <class _Tp, class = __enable_if_t<is_unbounded_array<_Tp>::value>> 1088 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(size_t __n, const remove_extent_t<_Tp>& __u) { 1089 return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n, __u); 1090 } 1091 1092 template <class _Tp, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0> 1093 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite(size_t __n) { 1094 return std::__allocate_shared_unbounded_array<_Tp>(allocator<__for_overwrite_tag>(), __n); 1095 } 1096 1097 #endif // _LIBCPP_STD_VER >= 20 1098 1099 template <class _Tp, class _Up> 1100 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT { 1101 return __x.get() == __y.get(); 1102 } 1103 1104 #if _LIBCPP_STD_VER <= 17 1105 1106 template <class _Tp, class _Up> 1107 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT { 1108 return !(__x == __y); 1109 } 1110 1111 template <class _Tp, class _Up> 1112 inline _LIBCPP_HIDE_FROM_ABI bool operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT { 1113 # if _LIBCPP_STD_VER <= 11 1114 typedef typename common_type<_Tp*, _Up*>::type _Vp; 1115 return less<_Vp>()(__x.get(), __y.get()); 1116 # else 1117 return less<>()(__x.get(), __y.get()); 1118 # endif 1119 } 1120 1121 template <class _Tp, class _Up> 1122 inline _LIBCPP_HIDE_FROM_ABI bool operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT { 1123 return __y < __x; 1124 } 1125 1126 template <class _Tp, class _Up> 1127 inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT { 1128 return !(__y < __x); 1129 } 1130 1131 template <class _Tp, class _Up> 1132 inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT { 1133 return !(__x < __y); 1134 } 1135 1136 #endif // _LIBCPP_STD_VER <= 17 1137 1138 #if _LIBCPP_STD_VER >= 20 1139 template <class _Tp, class _Up> 1140 _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) noexcept { 1141 return compare_three_way()(__x.get(), __y.get()); 1142 } 1143 #endif 1144 1145 template <class _Tp> 1146 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT { 1147 return !__x; 1148 } 1149 1150 #if _LIBCPP_STD_VER <= 17 1151 1152 template <class _Tp> 1153 inline _LIBCPP_HIDE_FROM_ABI bool operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT { 1154 return !__x; 1155 } 1156 1157 template <class _Tp> 1158 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT { 1159 return static_cast<bool>(__x); 1160 } 1161 1162 template <class _Tp> 1163 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT { 1164 return static_cast<bool>(__x); 1165 } 1166 1167 template <class _Tp> 1168 inline _LIBCPP_HIDE_FROM_ABI bool operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT { 1169 return less<_Tp*>()(__x.get(), nullptr); 1170 } 1171 1172 template <class _Tp> 1173 inline _LIBCPP_HIDE_FROM_ABI bool operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT { 1174 return less<_Tp*>()(nullptr, __x.get()); 1175 } 1176 1177 template <class _Tp> 1178 inline _LIBCPP_HIDE_FROM_ABI bool operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT { 1179 return nullptr < __x; 1180 } 1181 1182 template <class _Tp> 1183 inline _LIBCPP_HIDE_FROM_ABI bool operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT { 1184 return __x < nullptr; 1185 } 1186 1187 template <class _Tp> 1188 inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT { 1189 return !(nullptr < __x); 1190 } 1191 1192 template <class _Tp> 1193 inline _LIBCPP_HIDE_FROM_ABI bool operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT { 1194 return !(__x < nullptr); 1195 } 1196 1197 template <class _Tp> 1198 inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT { 1199 return !(__x < nullptr); 1200 } 1201 1202 template <class _Tp> 1203 inline _LIBCPP_HIDE_FROM_ABI bool operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT { 1204 return !(nullptr < __x); 1205 } 1206 1207 #endif // _LIBCPP_STD_VER <= 17 1208 1209 #if _LIBCPP_STD_VER >= 20 1210 template <class _Tp> 1211 _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(shared_ptr<_Tp> const& __x, nullptr_t) noexcept { 1212 return compare_three_way()(__x.get(), static_cast<typename shared_ptr<_Tp>::element_type*>(nullptr)); 1213 } 1214 #endif 1215 1216 template <class _Tp> 1217 inline _LIBCPP_HIDE_FROM_ABI void swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT { 1218 __x.swap(__y); 1219 } 1220 1221 template <class _Tp, class _Up> 1222 inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT { 1223 return shared_ptr<_Tp>(__r, static_cast< typename shared_ptr<_Tp>::element_type*>(__r.get())); 1224 } 1225 1226 // LWG-2996 1227 // We don't backport because it is an evolutionary change. 1228 #if _LIBCPP_STD_VER >= 20 1229 template <class _Tp, class _Up> 1230 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> static_pointer_cast(shared_ptr<_Up>&& __r) noexcept { 1231 return shared_ptr<_Tp>(std::move(__r), static_cast<typename shared_ptr<_Tp>::element_type*>(__r.get())); 1232 } 1233 #endif 1234 1235 template <class _Tp, class _Up> 1236 inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT { 1237 typedef typename shared_ptr<_Tp>::element_type _ET; 1238 _ET* __p = dynamic_cast<_ET*>(__r.get()); 1239 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>(); 1240 } 1241 1242 // LWG-2996 1243 // We don't backport because it is an evolutionary change. 1244 #if _LIBCPP_STD_VER >= 20 1245 template <class _Tp, class _Up> 1246 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> dynamic_pointer_cast(shared_ptr<_Up>&& __r) noexcept { 1247 auto* __p = dynamic_cast<typename shared_ptr<_Tp>::element_type*>(__r.get()); 1248 return __p ? shared_ptr<_Tp>(std::move(__r), __p) : shared_ptr<_Tp>(); 1249 } 1250 #endif 1251 1252 template <class _Tp, class _Up> 1253 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT { 1254 typedef typename shared_ptr<_Tp>::element_type _RTp; 1255 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get())); 1256 } 1257 1258 // LWG-2996 1259 // We don't backport because it is an evolutionary change. 1260 #if _LIBCPP_STD_VER >= 20 1261 template <class _Tp, class _Up> 1262 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> const_pointer_cast(shared_ptr<_Up>&& __r) noexcept { 1263 return shared_ptr<_Tp>(std::move(__r), const_cast<typename shared_ptr<_Tp>::element_type*>(__r.get())); 1264 } 1265 #endif 1266 1267 template <class _Tp, class _Up> 1268 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT { 1269 return shared_ptr<_Tp>(__r, reinterpret_cast< typename shared_ptr<_Tp>::element_type*>(__r.get())); 1270 } 1271 1272 // LWG-2996 1273 // We don't backport because it is an evolutionary change. 1274 #if _LIBCPP_STD_VER >= 20 1275 template <class _Tp, class _Up> 1276 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> reinterpret_pointer_cast(shared_ptr<_Up>&& __r) noexcept { 1277 return shared_ptr<_Tp>(std::move(__r), reinterpret_cast<typename shared_ptr<_Tp>::element_type*>(__r.get())); 1278 } 1279 #endif 1280 1281 #ifndef _LIBCPP_HAS_NO_RTTI 1282 1283 template <class _Dp, class _Tp> 1284 inline _LIBCPP_HIDE_FROM_ABI _Dp* get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT { 1285 return __p.template __get_deleter<_Dp>(); 1286 } 1287 1288 #endif // _LIBCPP_HAS_NO_RTTI 1289 1290 template <class _Tp> 1291 class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr { 1292 public: 1293 #if _LIBCPP_STD_VER >= 17 1294 typedef remove_extent_t<_Tp> element_type; 1295 #else 1296 typedef _Tp element_type; 1297 #endif 1298 1299 private: 1300 element_type* __ptr_; 1301 __shared_weak_count* __cntrl_; 1302 1303 public: 1304 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT; 1305 1306 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0> 1307 _LIBCPP_HIDE_FROM_ABI weak_ptr(shared_ptr<_Yp> const& __r) _NOEXCEPT; 1308 1309 _LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr const& __r) _NOEXCEPT; 1310 1311 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0> 1312 _LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr<_Yp> const& __r) _NOEXCEPT; 1313 1314 _LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr&& __r) _NOEXCEPT; 1315 1316 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0> 1317 _LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr<_Yp>&& __r) _NOEXCEPT; 1318 1319 _LIBCPP_HIDE_FROM_ABI ~weak_ptr(); 1320 1321 _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT; 1322 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0> 1323 _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT; 1324 1325 _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT; 1326 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0> 1327 _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT; 1328 1329 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0> 1330 _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT; 1331 1332 _LIBCPP_HIDE_FROM_ABI void swap(weak_ptr& __r) _NOEXCEPT; 1333 _LIBCPP_HIDE_FROM_ABI void reset() _NOEXCEPT; 1334 1335 _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __cntrl_ ? __cntrl_->use_count() : 0; } 1336 _LIBCPP_HIDE_FROM_ABI bool expired() const _NOEXCEPT { return __cntrl_ == nullptr || __cntrl_->use_count() == 0; } 1337 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> lock() const _NOEXCEPT; 1338 template <class _Up> 1339 _LIBCPP_HIDE_FROM_ABI bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT { 1340 return __cntrl_ < __r.__cntrl_; 1341 } 1342 template <class _Up> 1343 _LIBCPP_HIDE_FROM_ABI bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT { 1344 return __cntrl_ < __r.__cntrl_; 1345 } 1346 1347 template <class _Up> 1348 friend class _LIBCPP_TEMPLATE_VIS weak_ptr; 1349 template <class _Up> 1350 friend class _LIBCPP_TEMPLATE_VIS shared_ptr; 1351 }; 1352 1353 #if _LIBCPP_STD_VER >= 17 1354 template <class _Tp> 1355 weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>; 1356 #endif 1357 1358 template <class _Tp> 1359 inline _LIBCPP_CONSTEXPR weak_ptr<_Tp>::weak_ptr() _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {} 1360 1361 template <class _Tp> 1362 inline weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) { 1363 if (__cntrl_) 1364 __cntrl_->__add_weak(); 1365 } 1366 1367 template <class _Tp> 1368 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> > 1369 inline weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) { 1370 if (__cntrl_) 1371 __cntrl_->__add_weak(); 1372 } 1373 1374 template <class _Tp> 1375 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> > 1376 inline weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r) _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) { 1377 shared_ptr<_Yp> __s = __r.lock(); 1378 *this = weak_ptr<_Tp>(__s); 1379 } 1380 1381 template <class _Tp> 1382 inline weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) { 1383 __r.__ptr_ = nullptr; 1384 __r.__cntrl_ = nullptr; 1385 } 1386 1387 template <class _Tp> 1388 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> > 1389 inline weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r) _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) { 1390 shared_ptr<_Yp> __s = __r.lock(); 1391 *this = weak_ptr<_Tp>(__s); 1392 __r.reset(); 1393 } 1394 1395 template <class _Tp> 1396 weak_ptr<_Tp>::~weak_ptr() { 1397 if (__cntrl_) 1398 __cntrl_->__release_weak(); 1399 } 1400 1401 template <class _Tp> 1402 inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT { 1403 weak_ptr(__r).swap(*this); 1404 return *this; 1405 } 1406 1407 template <class _Tp> 1408 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> > 1409 inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT { 1410 weak_ptr(__r).swap(*this); 1411 return *this; 1412 } 1413 1414 template <class _Tp> 1415 inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT { 1416 weak_ptr(std::move(__r)).swap(*this); 1417 return *this; 1418 } 1419 1420 template <class _Tp> 1421 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> > 1422 inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT { 1423 weak_ptr(std::move(__r)).swap(*this); 1424 return *this; 1425 } 1426 1427 template <class _Tp> 1428 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> > 1429 inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT { 1430 weak_ptr(__r).swap(*this); 1431 return *this; 1432 } 1433 1434 template <class _Tp> 1435 inline void weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT { 1436 std::swap(__ptr_, __r.__ptr_); 1437 std::swap(__cntrl_, __r.__cntrl_); 1438 } 1439 1440 template <class _Tp> 1441 inline _LIBCPP_HIDE_FROM_ABI void swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT { 1442 __x.swap(__y); 1443 } 1444 1445 template <class _Tp> 1446 inline void weak_ptr<_Tp>::reset() _NOEXCEPT { 1447 weak_ptr().swap(*this); 1448 } 1449 1450 template <class _Tp> 1451 shared_ptr<_Tp> weak_ptr<_Tp>::lock() const _NOEXCEPT { 1452 shared_ptr<_Tp> __r; 1453 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_; 1454 if (__r.__cntrl_) 1455 __r.__ptr_ = __ptr_; 1456 return __r; 1457 } 1458 1459 #if _LIBCPP_STD_VER >= 17 1460 template <class _Tp = void> 1461 struct owner_less; 1462 #else 1463 template <class _Tp> 1464 struct owner_less; 1465 #endif 1466 1467 template <class _Tp> 1468 struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> > : __binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool> { 1469 _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT { 1470 return __x.owner_before(__y); 1471 } 1472 _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT { 1473 return __x.owner_before(__y); 1474 } 1475 _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT { 1476 return __x.owner_before(__y); 1477 } 1478 }; 1479 1480 template <class _Tp> 1481 struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> > : __binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool> { 1482 _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT { 1483 return __x.owner_before(__y); 1484 } 1485 _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT { 1486 return __x.owner_before(__y); 1487 } 1488 _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT { 1489 return __x.owner_before(__y); 1490 } 1491 }; 1492 1493 #if _LIBCPP_STD_VER >= 17 1494 template <> 1495 struct _LIBCPP_TEMPLATE_VIS owner_less<void> { 1496 template <class _Tp, class _Up> 1497 _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT { 1498 return __x.owner_before(__y); 1499 } 1500 template <class _Tp, class _Up> 1501 _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT { 1502 return __x.owner_before(__y); 1503 } 1504 template <class _Tp, class _Up> 1505 _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT { 1506 return __x.owner_before(__y); 1507 } 1508 template <class _Tp, class _Up> 1509 _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT { 1510 return __x.owner_before(__y); 1511 } 1512 typedef void is_transparent; 1513 }; 1514 #endif 1515 1516 template <class _Tp> 1517 class _LIBCPP_TEMPLATE_VIS enable_shared_from_this { 1518 mutable weak_ptr<_Tp> __weak_this_; 1519 1520 protected: 1521 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR enable_shared_from_this() _NOEXCEPT {} 1522 _LIBCPP_HIDE_FROM_ABI enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {} 1523 _LIBCPP_HIDE_FROM_ABI enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT { return *this; } 1524 _LIBCPP_HIDE_FROM_ABI ~enable_shared_from_this() {} 1525 1526 public: 1527 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> shared_from_this() { return shared_ptr<_Tp>(__weak_this_); } 1528 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp const> shared_from_this() const { return shared_ptr<const _Tp>(__weak_this_); } 1529 1530 #if _LIBCPP_STD_VER >= 17 1531 _LIBCPP_HIDE_FROM_ABI weak_ptr<_Tp> weak_from_this() _NOEXCEPT { return __weak_this_; } 1532 1533 _LIBCPP_HIDE_FROM_ABI weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT { return __weak_this_; } 1534 #endif // _LIBCPP_STD_VER >= 17 1535 1536 template <class _Up> 1537 friend class shared_ptr; 1538 }; 1539 1540 template <class _Tp> 1541 struct _LIBCPP_TEMPLATE_VIS hash; 1542 1543 template <class _Tp> 1544 struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> > { 1545 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS) 1546 _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> argument_type; 1547 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type; 1548 #endif 1549 1550 _LIBCPP_HIDE_FROM_ABI size_t operator()(const shared_ptr<_Tp>& __ptr) const _NOEXCEPT { 1551 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get()); 1552 } 1553 }; 1554 1555 template <class _CharT, class _Traits, class _Yp> 1556 inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 1557 operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p); 1558 1559 #if !defined(_LIBCPP_HAS_NO_THREADS) 1560 1561 class _LIBCPP_EXPORTED_FROM_ABI __sp_mut { 1562 void* __lx_; 1563 1564 public: 1565 void lock() _NOEXCEPT; 1566 void unlock() _NOEXCEPT; 1567 1568 private: 1569 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT; 1570 __sp_mut(const __sp_mut&); 1571 __sp_mut& operator=(const __sp_mut&); 1572 1573 friend _LIBCPP_EXPORTED_FROM_ABI __sp_mut& __get_sp_mut(const void*); 1574 }; 1575 1576 _LIBCPP_EXPORTED_FROM_ABI __sp_mut& __get_sp_mut(const void*); 1577 1578 template <class _Tp> 1579 inline _LIBCPP_HIDE_FROM_ABI bool atomic_is_lock_free(const shared_ptr<_Tp>*) { 1580 return false; 1581 } 1582 1583 template <class _Tp> 1584 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> atomic_load(const shared_ptr<_Tp>* __p) { 1585 __sp_mut& __m = std::__get_sp_mut(__p); 1586 __m.lock(); 1587 shared_ptr<_Tp> __q = *__p; 1588 __m.unlock(); 1589 return __q; 1590 } 1591 1592 template <class _Tp> 1593 inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order) { 1594 return std::atomic_load(__p); 1595 } 1596 1597 template <class _Tp> 1598 _LIBCPP_HIDE_FROM_ABI void atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) { 1599 __sp_mut& __m = std::__get_sp_mut(__p); 1600 __m.lock(); 1601 __p->swap(__r); 1602 __m.unlock(); 1603 } 1604 1605 template <class _Tp> 1606 inline _LIBCPP_HIDE_FROM_ABI void atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order) { 1607 std::atomic_store(__p, __r); 1608 } 1609 1610 template <class _Tp> 1611 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) { 1612 __sp_mut& __m = std::__get_sp_mut(__p); 1613 __m.lock(); 1614 __p->swap(__r); 1615 __m.unlock(); 1616 return __r; 1617 } 1618 1619 template <class _Tp> 1620 inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> 1621 atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order) { 1622 return std::atomic_exchange(__p, __r); 1623 } 1624 1625 template <class _Tp> 1626 _LIBCPP_HIDE_FROM_ABI bool 1627 atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w) { 1628 shared_ptr<_Tp> __temp; 1629 __sp_mut& __m = std::__get_sp_mut(__p); 1630 __m.lock(); 1631 if (__p->__owner_equivalent(*__v)) { 1632 std::swap(__temp, *__p); 1633 *__p = __w; 1634 __m.unlock(); 1635 return true; 1636 } 1637 std::swap(__temp, *__v); 1638 *__v = *__p; 1639 __m.unlock(); 1640 return false; 1641 } 1642 1643 template <class _Tp> 1644 inline _LIBCPP_HIDE_FROM_ABI bool 1645 atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w) { 1646 return std::atomic_compare_exchange_strong(__p, __v, __w); 1647 } 1648 1649 template <class _Tp> 1650 inline _LIBCPP_HIDE_FROM_ABI bool atomic_compare_exchange_strong_explicit( 1651 shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w, memory_order, memory_order) { 1652 return std::atomic_compare_exchange_strong(__p, __v, __w); 1653 } 1654 1655 template <class _Tp> 1656 inline _LIBCPP_HIDE_FROM_ABI bool atomic_compare_exchange_weak_explicit( 1657 shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w, memory_order, memory_order) { 1658 return std::atomic_compare_exchange_weak(__p, __v, __w); 1659 } 1660 1661 #endif // !defined(_LIBCPP_HAS_NO_THREADS) 1662 1663 _LIBCPP_END_NAMESPACE_STD 1664 1665 #endif // _LIBCPP___MEMORY_SHARED_PTR_H 1666