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 > 14 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(__libcpp_is_unbounded_array<_Array>::value); 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(__libcpp_is_bounded_array<_Array>::value); 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 #endif // _LIBCPP_STD_VER > 14 1120 1121 #if _LIBCPP_STD_VER > 17 1122 1123 template<class _Tp, class _Alloc, class = __enable_if_t<is_bounded_array<_Tp>::value>> 1124 _LIBCPP_HIDE_FROM_ABI 1125 shared_ptr<_Tp> allocate_shared(const _Alloc& __a) 1126 { 1127 return std::__allocate_shared_bounded_array<_Tp>(__a); 1128 } 1129 1130 template<class _Tp, class _Alloc, class = __enable_if_t<is_bounded_array<_Tp>::value>> 1131 _LIBCPP_HIDE_FROM_ABI 1132 shared_ptr<_Tp> allocate_shared(const _Alloc& __a, const remove_extent_t<_Tp>& __u) 1133 { 1134 return std::__allocate_shared_bounded_array<_Tp>(__a, __u); 1135 } 1136 1137 template<class _Tp, class _Alloc, class = __enable_if_t<is_unbounded_array<_Tp>::value>> 1138 _LIBCPP_HIDE_FROM_ABI 1139 shared_ptr<_Tp> allocate_shared(const _Alloc& __a, size_t __n) 1140 { 1141 return std::__allocate_shared_unbounded_array<_Tp>(__a, __n); 1142 } 1143 1144 template<class _Tp, class _Alloc, class = __enable_if_t<is_unbounded_array<_Tp>::value>> 1145 _LIBCPP_HIDE_FROM_ABI 1146 shared_ptr<_Tp> allocate_shared(const _Alloc& __a, size_t __n, const remove_extent_t<_Tp>& __u) 1147 { 1148 return std::__allocate_shared_unbounded_array<_Tp>(__a, __n, __u); 1149 } 1150 1151 template<class _Tp, class = __enable_if_t<is_bounded_array<_Tp>::value>> 1152 _LIBCPP_HIDE_FROM_ABI 1153 shared_ptr<_Tp> make_shared() 1154 { 1155 return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>()); 1156 } 1157 1158 template<class _Tp, class = __enable_if_t<is_bounded_array<_Tp>::value>> 1159 _LIBCPP_HIDE_FROM_ABI 1160 shared_ptr<_Tp> make_shared(const remove_extent_t<_Tp>& __u) 1161 { 1162 return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>(), __u); 1163 } 1164 1165 template<class _Tp, class = __enable_if_t<is_unbounded_array<_Tp>::value>> 1166 _LIBCPP_HIDE_FROM_ABI 1167 shared_ptr<_Tp> make_shared(size_t __n) 1168 { 1169 return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n); 1170 } 1171 1172 template<class _Tp, class = __enable_if_t<is_unbounded_array<_Tp>::value>> 1173 _LIBCPP_HIDE_FROM_ABI 1174 shared_ptr<_Tp> make_shared(size_t __n, const remove_extent_t<_Tp>& __u) 1175 { 1176 return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n, __u); 1177 } 1178 1179 #endif // _LIBCPP_STD_VER > 17 1180 1181 template<class _Tp, class _Up> 1182 inline _LIBCPP_INLINE_VISIBILITY 1183 bool 1184 operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT 1185 { 1186 return __x.get() == __y.get(); 1187 } 1188 1189 template<class _Tp, class _Up> 1190 inline _LIBCPP_INLINE_VISIBILITY 1191 bool 1192 operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT 1193 { 1194 return !(__x == __y); 1195 } 1196 1197 template<class _Tp, class _Up> 1198 inline _LIBCPP_INLINE_VISIBILITY 1199 bool 1200 operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT 1201 { 1202 #if _LIBCPP_STD_VER <= 11 1203 typedef typename common_type<_Tp*, _Up*>::type _Vp; 1204 return less<_Vp>()(__x.get(), __y.get()); 1205 #else 1206 return less<>()(__x.get(), __y.get()); 1207 #endif 1208 1209 } 1210 1211 template<class _Tp, class _Up> 1212 inline _LIBCPP_INLINE_VISIBILITY 1213 bool 1214 operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT 1215 { 1216 return __y < __x; 1217 } 1218 1219 template<class _Tp, class _Up> 1220 inline _LIBCPP_INLINE_VISIBILITY 1221 bool 1222 operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT 1223 { 1224 return !(__y < __x); 1225 } 1226 1227 template<class _Tp, class _Up> 1228 inline _LIBCPP_INLINE_VISIBILITY 1229 bool 1230 operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT 1231 { 1232 return !(__x < __y); 1233 } 1234 1235 template<class _Tp> 1236 inline _LIBCPP_INLINE_VISIBILITY 1237 bool 1238 operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT 1239 { 1240 return !__x; 1241 } 1242 1243 template<class _Tp> 1244 inline _LIBCPP_INLINE_VISIBILITY 1245 bool 1246 operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT 1247 { 1248 return !__x; 1249 } 1250 1251 template<class _Tp> 1252 inline _LIBCPP_INLINE_VISIBILITY 1253 bool 1254 operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT 1255 { 1256 return static_cast<bool>(__x); 1257 } 1258 1259 template<class _Tp> 1260 inline _LIBCPP_INLINE_VISIBILITY 1261 bool 1262 operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT 1263 { 1264 return static_cast<bool>(__x); 1265 } 1266 1267 template<class _Tp> 1268 inline _LIBCPP_INLINE_VISIBILITY 1269 bool 1270 operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT 1271 { 1272 return less<_Tp*>()(__x.get(), nullptr); 1273 } 1274 1275 template<class _Tp> 1276 inline _LIBCPP_INLINE_VISIBILITY 1277 bool 1278 operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT 1279 { 1280 return less<_Tp*>()(nullptr, __x.get()); 1281 } 1282 1283 template<class _Tp> 1284 inline _LIBCPP_INLINE_VISIBILITY 1285 bool 1286 operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT 1287 { 1288 return nullptr < __x; 1289 } 1290 1291 template<class _Tp> 1292 inline _LIBCPP_INLINE_VISIBILITY 1293 bool 1294 operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT 1295 { 1296 return __x < nullptr; 1297 } 1298 1299 template<class _Tp> 1300 inline _LIBCPP_INLINE_VISIBILITY 1301 bool 1302 operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT 1303 { 1304 return !(nullptr < __x); 1305 } 1306 1307 template<class _Tp> 1308 inline _LIBCPP_INLINE_VISIBILITY 1309 bool 1310 operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT 1311 { 1312 return !(__x < nullptr); 1313 } 1314 1315 template<class _Tp> 1316 inline _LIBCPP_INLINE_VISIBILITY 1317 bool 1318 operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT 1319 { 1320 return !(__x < nullptr); 1321 } 1322 1323 template<class _Tp> 1324 inline _LIBCPP_INLINE_VISIBILITY 1325 bool 1326 operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT 1327 { 1328 return !(nullptr < __x); 1329 } 1330 1331 template<class _Tp> 1332 inline _LIBCPP_INLINE_VISIBILITY 1333 void 1334 swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT 1335 { 1336 __x.swap(__y); 1337 } 1338 1339 template<class _Tp, class _Up> 1340 inline _LIBCPP_INLINE_VISIBILITY 1341 shared_ptr<_Tp> 1342 static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT 1343 { 1344 return shared_ptr<_Tp>(__r, 1345 static_cast< 1346 typename shared_ptr<_Tp>::element_type*>(__r.get())); 1347 } 1348 1349 template<class _Tp, class _Up> 1350 inline _LIBCPP_INLINE_VISIBILITY 1351 shared_ptr<_Tp> 1352 dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT 1353 { 1354 typedef typename shared_ptr<_Tp>::element_type _ET; 1355 _ET* __p = dynamic_cast<_ET*>(__r.get()); 1356 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>(); 1357 } 1358 1359 template<class _Tp, class _Up> 1360 shared_ptr<_Tp> 1361 const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT 1362 { 1363 typedef typename shared_ptr<_Tp>::element_type _RTp; 1364 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get())); 1365 } 1366 1367 template<class _Tp, class _Up> 1368 shared_ptr<_Tp> 1369 reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT 1370 { 1371 return shared_ptr<_Tp>(__r, 1372 reinterpret_cast< 1373 typename shared_ptr<_Tp>::element_type*>(__r.get())); 1374 } 1375 1376 #ifndef _LIBCPP_NO_RTTI 1377 1378 template<class _Dp, class _Tp> 1379 inline _LIBCPP_INLINE_VISIBILITY 1380 _Dp* 1381 get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT 1382 { 1383 return __p.template __get_deleter<_Dp>(); 1384 } 1385 1386 #endif // _LIBCPP_NO_RTTI 1387 1388 template<class _Tp> 1389 class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr 1390 { 1391 public: 1392 #if _LIBCPP_STD_VER > 14 1393 typedef remove_extent_t<_Tp> element_type; 1394 #else 1395 typedef _Tp element_type; 1396 #endif 1397 1398 private: 1399 element_type* __ptr_; 1400 __shared_weak_count* __cntrl_; 1401 1402 public: 1403 _LIBCPP_INLINE_VISIBILITY 1404 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT; 1405 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r, 1406 typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type = 0) 1407 _NOEXCEPT; 1408 _LIBCPP_INLINE_VISIBILITY 1409 weak_ptr(weak_ptr const& __r) _NOEXCEPT; 1410 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r, 1411 typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type = 0) 1412 _NOEXCEPT; 1413 1414 _LIBCPP_INLINE_VISIBILITY 1415 weak_ptr(weak_ptr&& __r) _NOEXCEPT; 1416 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r, 1417 typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type = 0) 1418 _NOEXCEPT; 1419 ~weak_ptr(); 1420 1421 _LIBCPP_INLINE_VISIBILITY 1422 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT; 1423 template<class _Yp> 1424 typename enable_if 1425 < 1426 __compatible_with<_Yp, _Tp>::value, 1427 weak_ptr& 1428 >::type 1429 _LIBCPP_INLINE_VISIBILITY 1430 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT; 1431 1432 _LIBCPP_INLINE_VISIBILITY 1433 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT; 1434 template<class _Yp> 1435 typename enable_if 1436 < 1437 __compatible_with<_Yp, _Tp>::value, 1438 weak_ptr& 1439 >::type 1440 _LIBCPP_INLINE_VISIBILITY 1441 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT; 1442 1443 template<class _Yp> 1444 typename enable_if 1445 < 1446 __compatible_with<_Yp, _Tp>::value, 1447 weak_ptr& 1448 >::type 1449 _LIBCPP_INLINE_VISIBILITY 1450 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT; 1451 1452 _LIBCPP_INLINE_VISIBILITY 1453 void swap(weak_ptr& __r) _NOEXCEPT; 1454 _LIBCPP_INLINE_VISIBILITY 1455 void reset() _NOEXCEPT; 1456 1457 _LIBCPP_INLINE_VISIBILITY 1458 long use_count() const _NOEXCEPT 1459 {return __cntrl_ ? __cntrl_->use_count() : 0;} 1460 _LIBCPP_INLINE_VISIBILITY 1461 bool expired() const _NOEXCEPT 1462 {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;} 1463 shared_ptr<_Tp> lock() const _NOEXCEPT; 1464 template<class _Up> 1465 _LIBCPP_INLINE_VISIBILITY 1466 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT 1467 {return __cntrl_ < __r.__cntrl_;} 1468 template<class _Up> 1469 _LIBCPP_INLINE_VISIBILITY 1470 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT 1471 {return __cntrl_ < __r.__cntrl_;} 1472 1473 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr; 1474 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr; 1475 }; 1476 1477 #if _LIBCPP_STD_VER > 14 1478 template<class _Tp> 1479 weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>; 1480 #endif 1481 1482 template<class _Tp> 1483 inline 1484 _LIBCPP_CONSTEXPR 1485 weak_ptr<_Tp>::weak_ptr() _NOEXCEPT 1486 : __ptr_(nullptr), 1487 __cntrl_(nullptr) 1488 { 1489 } 1490 1491 template<class _Tp> 1492 inline 1493 weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT 1494 : __ptr_(__r.__ptr_), 1495 __cntrl_(__r.__cntrl_) 1496 { 1497 if (__cntrl_) 1498 __cntrl_->__add_weak(); 1499 } 1500 1501 template<class _Tp> 1502 template<class _Yp> 1503 inline 1504 weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r, 1505 typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type) 1506 _NOEXCEPT 1507 : __ptr_(__r.__ptr_), 1508 __cntrl_(__r.__cntrl_) 1509 { 1510 if (__cntrl_) 1511 __cntrl_->__add_weak(); 1512 } 1513 1514 template<class _Tp> 1515 template<class _Yp> 1516 inline 1517 weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r, 1518 typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type) 1519 _NOEXCEPT 1520 : __ptr_(__r.__ptr_), 1521 __cntrl_(__r.__cntrl_) 1522 { 1523 if (__cntrl_) 1524 __cntrl_->__add_weak(); 1525 } 1526 1527 template<class _Tp> 1528 inline 1529 weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT 1530 : __ptr_(__r.__ptr_), 1531 __cntrl_(__r.__cntrl_) 1532 { 1533 __r.__ptr_ = nullptr; 1534 __r.__cntrl_ = nullptr; 1535 } 1536 1537 template<class _Tp> 1538 template<class _Yp> 1539 inline 1540 weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r, 1541 typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type) 1542 _NOEXCEPT 1543 : __ptr_(__r.__ptr_), 1544 __cntrl_(__r.__cntrl_) 1545 { 1546 __r.__ptr_ = nullptr; 1547 __r.__cntrl_ = nullptr; 1548 } 1549 1550 template<class _Tp> 1551 weak_ptr<_Tp>::~weak_ptr() 1552 { 1553 if (__cntrl_) 1554 __cntrl_->__release_weak(); 1555 } 1556 1557 template<class _Tp> 1558 inline 1559 weak_ptr<_Tp>& 1560 weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT 1561 { 1562 weak_ptr(__r).swap(*this); 1563 return *this; 1564 } 1565 1566 template<class _Tp> 1567 template<class _Yp> 1568 inline 1569 typename enable_if 1570 < 1571 __compatible_with<_Yp, _Tp>::value, 1572 weak_ptr<_Tp>& 1573 >::type 1574 weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT 1575 { 1576 weak_ptr(__r).swap(*this); 1577 return *this; 1578 } 1579 1580 template<class _Tp> 1581 inline 1582 weak_ptr<_Tp>& 1583 weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT 1584 { 1585 weak_ptr(_VSTD::move(__r)).swap(*this); 1586 return *this; 1587 } 1588 1589 template<class _Tp> 1590 template<class _Yp> 1591 inline 1592 typename enable_if 1593 < 1594 __compatible_with<_Yp, _Tp>::value, 1595 weak_ptr<_Tp>& 1596 >::type 1597 weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT 1598 { 1599 weak_ptr(_VSTD::move(__r)).swap(*this); 1600 return *this; 1601 } 1602 1603 template<class _Tp> 1604 template<class _Yp> 1605 inline 1606 typename enable_if 1607 < 1608 __compatible_with<_Yp, _Tp>::value, 1609 weak_ptr<_Tp>& 1610 >::type 1611 weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT 1612 { 1613 weak_ptr(__r).swap(*this); 1614 return *this; 1615 } 1616 1617 template<class _Tp> 1618 inline 1619 void 1620 weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT 1621 { 1622 _VSTD::swap(__ptr_, __r.__ptr_); 1623 _VSTD::swap(__cntrl_, __r.__cntrl_); 1624 } 1625 1626 template<class _Tp> 1627 inline _LIBCPP_INLINE_VISIBILITY 1628 void 1629 swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT 1630 { 1631 __x.swap(__y); 1632 } 1633 1634 template<class _Tp> 1635 inline 1636 void 1637 weak_ptr<_Tp>::reset() _NOEXCEPT 1638 { 1639 weak_ptr().swap(*this); 1640 } 1641 1642 template<class _Tp> 1643 shared_ptr<_Tp> 1644 weak_ptr<_Tp>::lock() const _NOEXCEPT 1645 { 1646 shared_ptr<_Tp> __r; 1647 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_; 1648 if (__r.__cntrl_) 1649 __r.__ptr_ = __ptr_; 1650 return __r; 1651 } 1652 1653 #if _LIBCPP_STD_VER > 14 1654 template <class _Tp = void> struct owner_less; 1655 #else 1656 template <class _Tp> struct owner_less; 1657 #endif 1658 1659 1660 _LIBCPP_SUPPRESS_DEPRECATED_PUSH 1661 template <class _Tp> 1662 struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> > 1663 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES) 1664 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool> 1665 #endif 1666 { 1667 _LIBCPP_SUPPRESS_DEPRECATED_POP 1668 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS) 1669 _LIBCPP_DEPRECATED_IN_CXX17 typedef bool result_type; 1670 _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> first_argument_type; 1671 _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> second_argument_type; 1672 #endif 1673 _LIBCPP_INLINE_VISIBILITY 1674 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT 1675 {return __x.owner_before(__y);} 1676 _LIBCPP_INLINE_VISIBILITY 1677 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT 1678 {return __x.owner_before(__y);} 1679 _LIBCPP_INLINE_VISIBILITY 1680 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT 1681 {return __x.owner_before(__y);} 1682 }; 1683 1684 _LIBCPP_SUPPRESS_DEPRECATED_PUSH 1685 template <class _Tp> 1686 struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> > 1687 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES) 1688 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool> 1689 #endif 1690 { 1691 _LIBCPP_SUPPRESS_DEPRECATED_POP 1692 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS) 1693 _LIBCPP_DEPRECATED_IN_CXX17 typedef bool result_type; 1694 _LIBCPP_DEPRECATED_IN_CXX17 typedef weak_ptr<_Tp> first_argument_type; 1695 _LIBCPP_DEPRECATED_IN_CXX17 typedef weak_ptr<_Tp> second_argument_type; 1696 #endif 1697 _LIBCPP_INLINE_VISIBILITY 1698 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT 1699 {return __x.owner_before(__y);} 1700 _LIBCPP_INLINE_VISIBILITY 1701 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT 1702 {return __x.owner_before(__y);} 1703 _LIBCPP_INLINE_VISIBILITY 1704 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT 1705 {return __x.owner_before(__y);} 1706 }; 1707 1708 #if _LIBCPP_STD_VER > 14 1709 template <> 1710 struct _LIBCPP_TEMPLATE_VIS owner_less<void> 1711 { 1712 template <class _Tp, class _Up> 1713 _LIBCPP_INLINE_VISIBILITY 1714 bool operator()( shared_ptr<_Tp> const& __x, shared_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()( shared_ptr<_Tp> const& __x, weak_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, shared_ptr<_Up> const& __y) const _NOEXCEPT 1723 {return __x.owner_before(__y);} 1724 template <class _Tp, class _Up> 1725 _LIBCPP_INLINE_VISIBILITY 1726 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT 1727 {return __x.owner_before(__y);} 1728 typedef void is_transparent; 1729 }; 1730 #endif 1731 1732 template<class _Tp> 1733 class _LIBCPP_TEMPLATE_VIS enable_shared_from_this 1734 { 1735 mutable weak_ptr<_Tp> __weak_this_; 1736 protected: 1737 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 1738 enable_shared_from_this() _NOEXCEPT {} 1739 _LIBCPP_INLINE_VISIBILITY 1740 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {} 1741 _LIBCPP_INLINE_VISIBILITY 1742 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT 1743 {return *this;} 1744 _LIBCPP_INLINE_VISIBILITY 1745 ~enable_shared_from_this() {} 1746 public: 1747 _LIBCPP_INLINE_VISIBILITY 1748 shared_ptr<_Tp> shared_from_this() 1749 {return shared_ptr<_Tp>(__weak_this_);} 1750 _LIBCPP_INLINE_VISIBILITY 1751 shared_ptr<_Tp const> shared_from_this() const 1752 {return shared_ptr<const _Tp>(__weak_this_);} 1753 1754 #if _LIBCPP_STD_VER > 14 1755 _LIBCPP_INLINE_VISIBILITY 1756 weak_ptr<_Tp> weak_from_this() _NOEXCEPT 1757 { return __weak_this_; } 1758 1759 _LIBCPP_INLINE_VISIBILITY 1760 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT 1761 { return __weak_this_; } 1762 #endif // _LIBCPP_STD_VER > 14 1763 1764 template <class _Up> friend class shared_ptr; 1765 }; 1766 1767 template <class _Tp> struct _LIBCPP_TEMPLATE_VIS hash; 1768 1769 template <class _Tp> 1770 struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> > 1771 { 1772 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS) 1773 _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> argument_type; 1774 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type; 1775 #endif 1776 1777 _LIBCPP_INLINE_VISIBILITY 1778 size_t operator()(const shared_ptr<_Tp>& __ptr) const _NOEXCEPT 1779 { 1780 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get()); 1781 } 1782 }; 1783 1784 template<class _CharT, class _Traits, class _Yp> 1785 inline _LIBCPP_INLINE_VISIBILITY 1786 basic_ostream<_CharT, _Traits>& 1787 operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p); 1788 1789 1790 #if !defined(_LIBCPP_HAS_NO_THREADS) 1791 1792 class _LIBCPP_TYPE_VIS __sp_mut 1793 { 1794 void* __lx; 1795 public: 1796 void lock() _NOEXCEPT; 1797 void unlock() _NOEXCEPT; 1798 1799 private: 1800 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT; 1801 __sp_mut(const __sp_mut&); 1802 __sp_mut& operator=(const __sp_mut&); 1803 1804 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*); 1805 }; 1806 1807 _LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1808 __sp_mut& __get_sp_mut(const void*); 1809 1810 template <class _Tp> 1811 inline _LIBCPP_INLINE_VISIBILITY 1812 bool 1813 atomic_is_lock_free(const shared_ptr<_Tp>*) 1814 { 1815 return false; 1816 } 1817 1818 template <class _Tp> 1819 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1820 shared_ptr<_Tp> 1821 atomic_load(const shared_ptr<_Tp>* __p) 1822 { 1823 __sp_mut& __m = __get_sp_mut(__p); 1824 __m.lock(); 1825 shared_ptr<_Tp> __q = *__p; 1826 __m.unlock(); 1827 return __q; 1828 } 1829 1830 template <class _Tp> 1831 inline _LIBCPP_INLINE_VISIBILITY 1832 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1833 shared_ptr<_Tp> 1834 atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order) 1835 { 1836 return atomic_load(__p); 1837 } 1838 1839 template <class _Tp> 1840 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1841 void 1842 atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) 1843 { 1844 __sp_mut& __m = __get_sp_mut(__p); 1845 __m.lock(); 1846 __p->swap(__r); 1847 __m.unlock(); 1848 } 1849 1850 template <class _Tp> 1851 inline _LIBCPP_INLINE_VISIBILITY 1852 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1853 void 1854 atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order) 1855 { 1856 atomic_store(__p, __r); 1857 } 1858 1859 template <class _Tp> 1860 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1861 shared_ptr<_Tp> 1862 atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) 1863 { 1864 __sp_mut& __m = __get_sp_mut(__p); 1865 __m.lock(); 1866 __p->swap(__r); 1867 __m.unlock(); 1868 return __r; 1869 } 1870 1871 template <class _Tp> 1872 inline _LIBCPP_INLINE_VISIBILITY 1873 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1874 shared_ptr<_Tp> 1875 atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order) 1876 { 1877 return atomic_exchange(__p, __r); 1878 } 1879 1880 template <class _Tp> 1881 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1882 bool 1883 atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w) 1884 { 1885 shared_ptr<_Tp> __temp; 1886 __sp_mut& __m = __get_sp_mut(__p); 1887 __m.lock(); 1888 if (__p->__owner_equivalent(*__v)) 1889 { 1890 _VSTD::swap(__temp, *__p); 1891 *__p = __w; 1892 __m.unlock(); 1893 return true; 1894 } 1895 _VSTD::swap(__temp, *__v); 1896 *__v = *__p; 1897 __m.unlock(); 1898 return false; 1899 } 1900 1901 template <class _Tp> 1902 inline _LIBCPP_INLINE_VISIBILITY 1903 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1904 bool 1905 atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w) 1906 { 1907 return atomic_compare_exchange_strong(__p, __v, __w); 1908 } 1909 1910 template <class _Tp> 1911 inline _LIBCPP_INLINE_VISIBILITY 1912 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1913 bool 1914 atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, 1915 shared_ptr<_Tp> __w, memory_order, memory_order) 1916 { 1917 return atomic_compare_exchange_strong(__p, __v, __w); 1918 } 1919 1920 template <class _Tp> 1921 inline _LIBCPP_INLINE_VISIBILITY 1922 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1923 bool 1924 atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, 1925 shared_ptr<_Tp> __w, memory_order, memory_order) 1926 { 1927 return atomic_compare_exchange_weak(__p, __v, __w); 1928 } 1929 1930 #endif // !defined(_LIBCPP_HAS_NO_THREADS) 1931 1932 _LIBCPP_END_NAMESPACE_STD 1933 1934 #endif // _LIBCPP___MEMORY_SHARED_PTR_H 1935