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