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