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