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