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