Path: blob/main/contrib/llvm-project/libcxx/include/__memory/shared_ptr.h
35233 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___MEMORY_SHARED_PTR_H10#define _LIBCPP___MEMORY_SHARED_PTR_H1112#include <__compare/compare_three_way.h>13#include <__compare/ordering.h>14#include <__config>15#include <__exception/exception.h>16#include <__functional/binary_function.h>17#include <__functional/operations.h>18#include <__functional/reference_wrapper.h>19#include <__fwd/ostream.h>20#include <__iterator/access.h>21#include <__memory/addressof.h>22#include <__memory/allocation_guard.h>23#include <__memory/allocator.h>24#include <__memory/allocator_destructor.h>25#include <__memory/allocator_traits.h>26#include <__memory/auto_ptr.h>27#include <__memory/compressed_pair.h>28#include <__memory/construct_at.h>29#include <__memory/pointer_traits.h>30#include <__memory/uninitialized_algorithms.h>31#include <__memory/unique_ptr.h>32#include <__type_traits/add_lvalue_reference.h>33#include <__type_traits/conditional.h>34#include <__type_traits/conjunction.h>35#include <__type_traits/disjunction.h>36#include <__type_traits/is_array.h>37#include <__type_traits/is_bounded_array.h>38#include <__type_traits/is_constructible.h>39#include <__type_traits/is_convertible.h>40#include <__type_traits/is_reference.h>41#include <__type_traits/is_unbounded_array.h>42#include <__type_traits/nat.h>43#include <__type_traits/negation.h>44#include <__type_traits/remove_extent.h>45#include <__type_traits/remove_reference.h>46#include <__utility/declval.h>47#include <__utility/forward.h>48#include <__utility/move.h>49#include <__utility/swap.h>50#include <__verbose_abort>51#include <cstddef>52#include <new>53#include <typeinfo>54#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)55# include <__atomic/memory_order.h>56#endif5758#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)59# pragma GCC system_header60#endif6162_LIBCPP_PUSH_MACROS63#include <__undef_macros>6465_LIBCPP_BEGIN_NAMESPACE_STD6667// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)68// should be sufficient for thread safety.69// See https://llvm.org/PR2280370#if defined(__clang__) && __has_builtin(__atomic_add_fetch) && defined(__ATOMIC_RELAXED) && defined(__ATOMIC_ACQ_REL)71# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT72#elif defined(_LIBCPP_COMPILER_GCC)73# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT74#endif7576template <class _ValueType>77inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_relaxed_load(_ValueType const* __value) {78#if !defined(_LIBCPP_HAS_NO_THREADS) && defined(__ATOMIC_RELAXED) && \79(__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))80return __atomic_load_n(__value, __ATOMIC_RELAXED);81#else82return *__value;83#endif84}8586template <class _ValueType>87inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_acquire_load(_ValueType const* __value) {88#if !defined(_LIBCPP_HAS_NO_THREADS) && defined(__ATOMIC_ACQUIRE) && \89(__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))90return __atomic_load_n(__value, __ATOMIC_ACQUIRE);91#else92return *__value;93#endif94}9596template <class _Tp>97inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT {98#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)99return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);100#else101return __t += 1;102#endif103}104105template <class _Tp>106inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT {107#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)108return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);109#else110return __t -= 1;111#endif112}113114class _LIBCPP_EXPORTED_FROM_ABI bad_weak_ptr : public std::exception {115public:116_LIBCPP_HIDE_FROM_ABI bad_weak_ptr() _NOEXCEPT = default;117_LIBCPP_HIDE_FROM_ABI bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;118_LIBCPP_HIDE_FROM_ABI bad_weak_ptr& operator=(const bad_weak_ptr&) _NOEXCEPT = default;119~bad_weak_ptr() _NOEXCEPT override;120const char* what() const _NOEXCEPT override;121};122123_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_weak_ptr() {124#ifndef _LIBCPP_HAS_NO_EXCEPTIONS125throw bad_weak_ptr();126#else127_LIBCPP_VERBOSE_ABORT("bad_weak_ptr was thrown in -fno-exceptions mode");128#endif129}130131template <class _Tp>132class _LIBCPP_TEMPLATE_VIS weak_ptr;133134class _LIBCPP_EXPORTED_FROM_ABI __shared_count {135__shared_count(const __shared_count&);136__shared_count& operator=(const __shared_count&);137138protected:139long __shared_owners_;140virtual ~__shared_count();141142private:143virtual void __on_zero_shared() _NOEXCEPT = 0;144145public:146_LIBCPP_HIDE_FROM_ABI explicit __shared_count(long __refs = 0) _NOEXCEPT : __shared_owners_(__refs) {}147148#if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)149void __add_shared() noexcept;150bool __release_shared() noexcept;151#else152_LIBCPP_HIDE_FROM_ABI void __add_shared() _NOEXCEPT { __libcpp_atomic_refcount_increment(__shared_owners_); }153_LIBCPP_HIDE_FROM_ABI bool __release_shared() _NOEXCEPT {154if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {155__on_zero_shared();156return true;157}158return false;159}160#endif161_LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __libcpp_relaxed_load(&__shared_owners_) + 1; }162};163164class _LIBCPP_EXPORTED_FROM_ABI __shared_weak_count : private __shared_count {165long __shared_weak_owners_;166167public:168_LIBCPP_HIDE_FROM_ABI explicit __shared_weak_count(long __refs = 0) _NOEXCEPT169: __shared_count(__refs),170__shared_weak_owners_(__refs) {}171172protected:173~__shared_weak_count() override;174175public:176#if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)177void __add_shared() noexcept;178void __add_weak() noexcept;179void __release_shared() noexcept;180#else181_LIBCPP_HIDE_FROM_ABI void __add_shared() _NOEXCEPT { __shared_count::__add_shared(); }182_LIBCPP_HIDE_FROM_ABI void __add_weak() _NOEXCEPT { __libcpp_atomic_refcount_increment(__shared_weak_owners_); }183_LIBCPP_HIDE_FROM_ABI void __release_shared() _NOEXCEPT {184if (__shared_count::__release_shared())185__release_weak();186}187#endif188void __release_weak() _NOEXCEPT;189_LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __shared_count::use_count(); }190__shared_weak_count* lock() _NOEXCEPT;191192virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;193194private:195virtual void __on_zero_shared_weak() _NOEXCEPT = 0;196};197198template <class _Tp, class _Dp, class _Alloc>199class __shared_ptr_pointer : public __shared_weak_count {200__compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;201202public:203_LIBCPP_HIDE_FROM_ABI __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)204: __data_(__compressed_pair<_Tp, _Dp>(__p, std::move(__d)), std::move(__a)) {}205206#ifndef _LIBCPP_HAS_NO_RTTI207_LIBCPP_HIDE_FROM_ABI_VIRTUAL const void* __get_deleter(const type_info&) const _NOEXCEPT override;208#endif209210private:211_LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;212_LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override;213};214215#ifndef _LIBCPP_HAS_NO_RTTI216217template <class _Tp, class _Dp, class _Alloc>218const void* __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT {219return __t == typeid(_Dp) ? std::addressof(__data_.first().second()) : nullptr;220}221222#endif // _LIBCPP_HAS_NO_RTTI223224template <class _Tp, class _Dp, class _Alloc>225void __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT {226__data_.first().second()(__data_.first().first());227__data_.first().second().~_Dp();228}229230template <class _Tp, class _Dp, class _Alloc>231void __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT {232typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;233typedef allocator_traits<_Al> _ATraits;234typedef pointer_traits<typename _ATraits::pointer> _PTraits;235236_Al __a(__data_.second());237__data_.second().~_Alloc();238__a.deallocate(_PTraits::pointer_to(*this), 1);239}240241// This tag is used to instantiate an allocator type. The various shared_ptr control blocks242// detect that the allocator has been instantiated for this type and perform alternative243// initialization/destruction based on that.244struct __for_overwrite_tag {};245246template <class _Tp, class _Alloc>247struct __shared_ptr_emplace : __shared_weak_count {248template <class... _Args,249class _Allocator = _Alloc,250__enable_if_t<is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0>251_LIBCPP_HIDE_FROM_ABI explicit __shared_ptr_emplace(_Alloc __a, _Args&&...) : __storage_(std::move(__a)) {252static_assert(253sizeof...(_Args) == 0, "No argument should be provided to the control block when using _for_overwrite");254::new ((void*)__get_elem()) _Tp;255}256257template <class... _Args,258class _Allocator = _Alloc,259__enable_if_t<!is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0>260_LIBCPP_HIDE_FROM_ABI explicit __shared_ptr_emplace(_Alloc __a, _Args&&... __args) : __storage_(std::move(__a)) {261using _TpAlloc = typename __allocator_traits_rebind<_Alloc, __remove_cv_t<_Tp> >::type;262_TpAlloc __tmp(*__get_alloc());263allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), std::forward<_Args>(__args)...);264}265266_LIBCPP_HIDE_FROM_ABI _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }267268_LIBCPP_HIDE_FROM_ABI _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }269270private:271template <class _Allocator = _Alloc,272__enable_if_t<is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0>273_LIBCPP_HIDE_FROM_ABI void __on_zero_shared_impl() _NOEXCEPT {274__get_elem()->~_Tp();275}276277template <class _Allocator = _Alloc,278__enable_if_t<!is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0>279_LIBCPP_HIDE_FROM_ABI void __on_zero_shared_impl() _NOEXCEPT {280using _TpAlloc = typename __allocator_traits_rebind<_Allocator, __remove_cv_t<_Tp> >::type;281_TpAlloc __tmp(*__get_alloc());282allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());283}284285_LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override { __on_zero_shared_impl(); }286287_LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override {288using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;289using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;290_ControlBlockAlloc __tmp(*__get_alloc());291__storage_.~_Storage();292allocator_traits<_ControlBlockAlloc>::deallocate(__tmp, pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);293}294295// This class implements the control block for non-array shared pointers created296// through `std::allocate_shared` and `std::make_shared`.297//298// In previous versions of the library, we used a compressed pair to store299// both the _Alloc and the _Tp. This implies using EBO, which is incompatible300// with Allocator construction for _Tp. To allow implementing P0674 in C++20,301// we now use a properly aligned char buffer while making sure that we maintain302// the same layout that we had when we used a compressed pair.303using _CompressedPair = __compressed_pair<_Alloc, _Tp>;304struct _ALIGNAS_TYPE(_CompressedPair) _Storage {305char __blob_[sizeof(_CompressedPair)];306307_LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) { ::new ((void*)__get_alloc()) _Alloc(std::move(__a)); }308_LIBCPP_HIDE_FROM_ABI ~_Storage() { __get_alloc()->~_Alloc(); }309_LIBCPP_HIDE_FROM_ABI _Alloc* __get_alloc() _NOEXCEPT {310_CompressedPair* __as_pair = reinterpret_cast<_CompressedPair*>(__blob_);311typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);312_Alloc* __alloc = reinterpret_cast<_Alloc*>(__first);313return __alloc;314}315_LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT {316_CompressedPair* __as_pair = reinterpret_cast<_CompressedPair*>(__blob_);317typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);318_Tp* __elem = reinterpret_cast<_Tp*>(__second);319return __elem;320}321};322323static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");324static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");325_Storage __storage_;326};327328struct __shared_ptr_dummy_rebind_allocator_type;329template <>330class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type> {331public:332template <class _Other>333struct rebind {334typedef allocator<_Other> other;335};336};337338template <class _Tp>339class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;340341// http://eel.is/c++draft/util.sharedptr#util.smartptr.shared.general-6342// A pointer type Y* is said to be compatible with a pointer type T*343// when either Y* is convertible to T* or Y is U[N] and T is cv U[].344#if _LIBCPP_STD_VER >= 17345template <class _Yp, class _Tp>346struct __bounded_convertible_to_unbounded : false_type {};347348template <class _Up, std::size_t _Np, class _Tp>349struct __bounded_convertible_to_unbounded<_Up[_Np], _Tp> : is_same<__remove_cv_t<_Tp>, _Up[]> {};350351template <class _Yp, class _Tp>352struct __compatible_with : _Or< is_convertible<_Yp*, _Tp*>, __bounded_convertible_to_unbounded<_Yp, _Tp> > {};353#else354template <class _Yp, class _Tp>355struct __compatible_with : is_convertible<_Yp*, _Tp*> {};356#endif // _LIBCPP_STD_VER >= 17357358// Constructors that take raw pointers have a different set of "compatible" constraints359// http://eel.is/c++draft/util.sharedptr#util.smartptr.shared.const-9.1360// - If T is an array type, then either T is U[N] and Y(*)[N] is convertible to T*,361// or T is U[] and Y(*)[] is convertible to T*.362// - If T is not an array type, then Y* is convertible to T*.363#if _LIBCPP_STD_VER >= 17364template <class _Yp, class _Tp, class = void>365struct __raw_pointer_compatible_with : _And< _Not<is_array<_Tp>>, is_convertible<_Yp*, _Tp*> > {};366367template <class _Yp, class _Up, std::size_t _Np>368struct __raw_pointer_compatible_with<_Yp, _Up[_Np], __enable_if_t< is_convertible<_Yp (*)[_Np], _Up (*)[_Np]>::value> >369: true_type {};370371template <class _Yp, class _Up>372struct __raw_pointer_compatible_with<_Yp, _Up[], __enable_if_t< is_convertible<_Yp (*)[], _Up (*)[]>::value> >373: true_type {};374375#else376template <class _Yp, class _Tp>377struct __raw_pointer_compatible_with : is_convertible<_Yp*, _Tp*> {};378#endif // _LIBCPP_STD_VER >= 17379380template <class _Ptr, class = void>381struct __is_deletable : false_type {};382template <class _Ptr>383struct __is_deletable<_Ptr, decltype(delete std::declval<_Ptr>())> : true_type {};384385template <class _Ptr, class = void>386struct __is_array_deletable : false_type {};387template <class _Ptr>388struct __is_array_deletable<_Ptr, decltype(delete[] std::declval<_Ptr>())> : true_type {};389390template <class _Dp, class _Pt, class = decltype(std::declval<_Dp>()(std::declval<_Pt>()))>391true_type __well_formed_deleter_test(int);392393template <class, class>394false_type __well_formed_deleter_test(...);395396template <class _Dp, class _Pt>397struct __well_formed_deleter : decltype(std::__well_formed_deleter_test<_Dp, _Pt>(0)) {};398399template <class _Dp, class _Yp, class _Tp>400struct __shared_ptr_deleter_ctor_reqs {401static const bool value = __raw_pointer_compatible_with<_Yp, _Tp>::value && is_move_constructible<_Dp>::value &&402__well_formed_deleter<_Dp, _Yp*>::value;403};404405template <class _Dp>406using __shared_ptr_nullptr_deleter_ctor_reqs = _And<is_move_constructible<_Dp>, __well_formed_deleter<_Dp, nullptr_t> >;407408#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)409# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((__trivial_abi__))410#else411# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI412#endif413414template <class _Tp>415class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr {416struct __nullptr_sfinae_tag {};417418public:419#if _LIBCPP_STD_VER >= 17420typedef weak_ptr<_Tp> weak_type;421typedef remove_extent_t<_Tp> element_type;422#else423typedef _Tp element_type;424#endif425426// A shared_ptr contains only two raw pointers which point to the heap and move constructing already doesn't require427// any bookkeeping, so it's always trivially relocatable.428using __trivially_relocatable = shared_ptr;429430private:431element_type* __ptr_;432__shared_weak_count* __cntrl_;433434public:435_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {}436437_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {}438439template <class _Yp,440__enable_if_t< _And< __raw_pointer_compatible_with<_Yp, _Tp>441// In C++03 we get errors when trying to do SFINAE with the442// delete operator, so we always pretend that it's deletable.443// The same happens on GCC.444#if !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_COMPILER_GCC)445,446_If<is_array<_Tp>::value, __is_array_deletable<_Yp*>, __is_deletable<_Yp*> >447#endif448>::value,449int> = 0>450_LIBCPP_HIDE_FROM_ABI explicit shared_ptr(_Yp* __p) : __ptr_(__p) {451unique_ptr<_Yp> __hold(__p);452typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;453typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT> _CntrlBlk;454__cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());455__hold.release();456__enable_weak_this(__p, __p);457}458459template <class _Yp, class _Dp, __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int> = 0>460_LIBCPP_HIDE_FROM_ABI shared_ptr(_Yp* __p, _Dp __d) : __ptr_(__p) {461#ifndef _LIBCPP_HAS_NO_EXCEPTIONS462try {463#endif // _LIBCPP_HAS_NO_EXCEPTIONS464typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;465typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT> _CntrlBlk;466#ifndef _LIBCPP_CXX03_LANG467__cntrl_ = new _CntrlBlk(__p, std::move(__d), _AllocT());468#else469__cntrl_ = new _CntrlBlk(__p, __d, _AllocT());470#endif // not _LIBCPP_CXX03_LANG471__enable_weak_this(__p, __p);472#ifndef _LIBCPP_HAS_NO_EXCEPTIONS473} catch (...) {474__d(__p);475throw;476}477#endif // _LIBCPP_HAS_NO_EXCEPTIONS478}479480template <class _Yp,481class _Dp,482class _Alloc,483__enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int> = 0>484_LIBCPP_HIDE_FROM_ABI shared_ptr(_Yp* __p, _Dp __d, _Alloc __a) : __ptr_(__p) {485#ifndef _LIBCPP_HAS_NO_EXCEPTIONS486try {487#endif // _LIBCPP_HAS_NO_EXCEPTIONS488typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;489typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;490typedef __allocator_destructor<_A2> _D2;491_A2 __a2(__a);492unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));493::new ((void*)std::addressof(*__hold2.get()))494#ifndef _LIBCPP_CXX03_LANG495_CntrlBlk(__p, std::move(__d), __a);496#else497_CntrlBlk(__p, __d, __a);498#endif // not _LIBCPP_CXX03_LANG499__cntrl_ = std::addressof(*__hold2.release());500__enable_weak_this(__p, __p);501#ifndef _LIBCPP_HAS_NO_EXCEPTIONS502} catch (...) {503__d(__p);504throw;505}506#endif // _LIBCPP_HAS_NO_EXCEPTIONS507}508509template <class _Dp>510_LIBCPP_HIDE_FROM_ABI shared_ptr(511nullptr_t __p,512_Dp __d,513__enable_if_t<__shared_ptr_nullptr_deleter_ctor_reqs<_Dp>::value, __nullptr_sfinae_tag> = __nullptr_sfinae_tag())514: __ptr_(nullptr) {515#ifndef _LIBCPP_HAS_NO_EXCEPTIONS516try {517#endif // _LIBCPP_HAS_NO_EXCEPTIONS518typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;519typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT> _CntrlBlk;520#ifndef _LIBCPP_CXX03_LANG521__cntrl_ = new _CntrlBlk(__p, std::move(__d), _AllocT());522#else523__cntrl_ = new _CntrlBlk(__p, __d, _AllocT());524#endif // not _LIBCPP_CXX03_LANG525#ifndef _LIBCPP_HAS_NO_EXCEPTIONS526} catch (...) {527__d(__p);528throw;529}530#endif // _LIBCPP_HAS_NO_EXCEPTIONS531}532533template <class _Dp, class _Alloc>534_LIBCPP_HIDE_FROM_ABI shared_ptr(535nullptr_t __p,536_Dp __d,537_Alloc __a,538__enable_if_t<__shared_ptr_nullptr_deleter_ctor_reqs<_Dp>::value, __nullptr_sfinae_tag> = __nullptr_sfinae_tag())539: __ptr_(nullptr) {540#ifndef _LIBCPP_HAS_NO_EXCEPTIONS541try {542#endif // _LIBCPP_HAS_NO_EXCEPTIONS543typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;544typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;545typedef __allocator_destructor<_A2> _D2;546_A2 __a2(__a);547unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));548::new ((void*)std::addressof(*__hold2.get()))549#ifndef _LIBCPP_CXX03_LANG550_CntrlBlk(__p, std::move(__d), __a);551#else552_CntrlBlk(__p, __d, __a);553#endif // not _LIBCPP_CXX03_LANG554__cntrl_ = std::addressof(*__hold2.release());555#ifndef _LIBCPP_HAS_NO_EXCEPTIONS556} catch (...) {557__d(__p);558throw;559}560#endif // _LIBCPP_HAS_NO_EXCEPTIONS561}562563template <class _Yp>564_LIBCPP_HIDE_FROM_ABI shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT565: __ptr_(__p),566__cntrl_(__r.__cntrl_) {567if (__cntrl_)568__cntrl_->__add_shared();569}570571// LWG-2996572// We don't backport because it is an evolutionary change.573#if _LIBCPP_STD_VER >= 20574template <class _Yp>575_LIBCPP_HIDE_FROM_ABI shared_ptr(shared_ptr<_Yp>&& __r, element_type* __p) noexcept576: __ptr_(__p), __cntrl_(__r.__cntrl_) {577__r.__ptr_ = nullptr;578__r.__cntrl_ = nullptr;579}580#endif581582_LIBCPP_HIDE_FROM_ABI shared_ptr(const shared_ptr& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {583if (__cntrl_)584__cntrl_->__add_shared();585}586587template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>588_LIBCPP_HIDE_FROM_ABI shared_ptr(const shared_ptr<_Yp>& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {589if (__cntrl_)590__cntrl_->__add_shared();591}592593_LIBCPP_HIDE_FROM_ABI shared_ptr(shared_ptr&& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {594__r.__ptr_ = nullptr;595__r.__cntrl_ = nullptr;596}597598template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>599_LIBCPP_HIDE_FROM_ABI shared_ptr(shared_ptr<_Yp>&& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {600__r.__ptr_ = nullptr;601__r.__cntrl_ = nullptr;602}603604template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>605_LIBCPP_HIDE_FROM_ABI explicit shared_ptr(const weak_ptr<_Yp>& __r)606: __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_) {607if (__cntrl_ == nullptr)608__throw_bad_weak_ptr();609}610611#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)612template <class _Yp, __enable_if_t<is_convertible<_Yp*, element_type*>::value, int> = 0>613_LIBCPP_HIDE_FROM_ABI shared_ptr(auto_ptr<_Yp>&& __r) : __ptr_(__r.get()) {614typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<__remove_cv_t<_Yp> > > _CntrlBlk;615__cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<__remove_cv_t<_Yp> >());616__enable_weak_this(__r.get(), __r.get());617__r.release();618}619#endif620621template <class _Yp,622class _Dp,623__enable_if_t<!is_lvalue_reference<_Dp>::value && __compatible_with<_Yp, _Tp>::value &&624is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,625int> = 0>626_LIBCPP_HIDE_FROM_ABI shared_ptr(unique_ptr<_Yp, _Dp>&& __r) : __ptr_(__r.get()) {627#if _LIBCPP_STD_VER >= 14628if (__ptr_ == nullptr)629__cntrl_ = nullptr;630else631#endif632{633typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;634typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT> _CntrlBlk;635__cntrl_ = new _CntrlBlk(__r.get(), std::move(__r.get_deleter()), _AllocT());636__enable_weak_this(__r.get(), __r.get());637}638__r.release();639}640641template <class _Yp,642class _Dp,643class = void,644__enable_if_t<is_lvalue_reference<_Dp>::value && __compatible_with<_Yp, _Tp>::value &&645is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,646int> = 0>647_LIBCPP_HIDE_FROM_ABI shared_ptr(unique_ptr<_Yp, _Dp>&& __r) : __ptr_(__r.get()) {648#if _LIBCPP_STD_VER >= 14649if (__ptr_ == nullptr)650__cntrl_ = nullptr;651else652#endif653{654typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;655typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer,656reference_wrapper<__libcpp_remove_reference_t<_Dp> >,657_AllocT>658_CntrlBlk;659__cntrl_ = new _CntrlBlk(__r.get(), std::ref(__r.get_deleter()), _AllocT());660__enable_weak_this(__r.get(), __r.get());661}662__r.release();663}664665_LIBCPP_HIDE_FROM_ABI ~shared_ptr() {666if (__cntrl_)667__cntrl_->__release_shared();668}669670_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(const shared_ptr& __r) _NOEXCEPT {671shared_ptr(__r).swap(*this);672return *this;673}674675template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>676_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT {677shared_ptr(__r).swap(*this);678return *this;679}680681_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(shared_ptr&& __r) _NOEXCEPT {682shared_ptr(std::move(__r)).swap(*this);683return *this;684}685686template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>687_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(shared_ptr<_Yp>&& __r) {688shared_ptr(std::move(__r)).swap(*this);689return *this;690}691692#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)693template <class _Yp,694__enable_if_t<!is_array<_Yp>::value && is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,695int> = 0>696_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(auto_ptr<_Yp>&& __r) {697shared_ptr(std::move(__r)).swap(*this);698return *this;699}700#endif701702template <class _Yp,703class _Dp,704__enable_if_t<_And< __compatible_with<_Yp, _Tp>,705is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*> >::value,706int> = 0>707_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(unique_ptr<_Yp, _Dp>&& __r) {708shared_ptr(std::move(__r)).swap(*this);709return *this;710}711712_LIBCPP_HIDE_FROM_ABI void swap(shared_ptr& __r) _NOEXCEPT {713std::swap(__ptr_, __r.__ptr_);714std::swap(__cntrl_, __r.__cntrl_);715}716717_LIBCPP_HIDE_FROM_ABI void reset() _NOEXCEPT { shared_ptr().swap(*this); }718719template <class _Yp, __enable_if_t<__raw_pointer_compatible_with<_Yp, _Tp>::value, int> = 0>720_LIBCPP_HIDE_FROM_ABI void reset(_Yp* __p) {721shared_ptr(__p).swap(*this);722}723724template <class _Yp, class _Dp, __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int> = 0>725_LIBCPP_HIDE_FROM_ABI void reset(_Yp* __p, _Dp __d) {726shared_ptr(__p, __d).swap(*this);727}728729template <class _Yp,730class _Dp,731class _Alloc,732__enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int> = 0>733_LIBCPP_HIDE_FROM_ABI void reset(_Yp* __p, _Dp __d, _Alloc __a) {734shared_ptr(__p, __d, __a).swap(*this);735}736737_LIBCPP_HIDE_FROM_ABI element_type* get() const _NOEXCEPT { return __ptr_; }738739_LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<element_type> operator*() const _NOEXCEPT { return *__ptr_; }740741_LIBCPP_HIDE_FROM_ABI element_type* operator->() const _NOEXCEPT {742static_assert(!is_array<_Tp>::value, "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");743return __ptr_;744}745746_LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __cntrl_ ? __cntrl_->use_count() : 0; }747748#if _LIBCPP_STD_VER < 20 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_SHARED_PTR_UNIQUE)749_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI bool unique() const _NOEXCEPT { return use_count() == 1; }750#endif751752_LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return get() != nullptr; }753754template <class _Up>755_LIBCPP_HIDE_FROM_ABI bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT {756return __cntrl_ < __p.__cntrl_;757}758759template <class _Up>760_LIBCPP_HIDE_FROM_ABI bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT {761return __cntrl_ < __p.__cntrl_;762}763764_LIBCPP_HIDE_FROM_ABI bool __owner_equivalent(const shared_ptr& __p) const { return __cntrl_ == __p.__cntrl_; }765766#if _LIBCPP_STD_VER >= 17767_LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<element_type> operator[](ptrdiff_t __i) const {768static_assert(is_array<_Tp>::value, "std::shared_ptr<T>::operator[] is only valid when T is an array type.");769return __ptr_[__i];770}771#endif772773#ifndef _LIBCPP_HAS_NO_RTTI774template <class _Dp>775_LIBCPP_HIDE_FROM_ABI _Dp* __get_deleter() const _NOEXCEPT {776return static_cast<_Dp*>(__cntrl_ ? const_cast<void*>(__cntrl_->__get_deleter(typeid(_Dp))) : nullptr);777}778#endif // _LIBCPP_HAS_NO_RTTI779780template <class _Yp, class _CntrlBlk>781_LIBCPP_HIDE_FROM_ABI static shared_ptr<_Tp> __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT {782shared_ptr<_Tp> __r;783__r.__ptr_ = __p;784__r.__cntrl_ = __cntrl;785__r.__enable_weak_this(__r.__ptr_, __r.__ptr_);786return __r;787}788789private:790template <class _Yp, bool = is_function<_Yp>::value>791struct __shared_ptr_default_allocator {792typedef allocator<__remove_cv_t<_Yp> > type;793};794795template <class _Yp>796struct __shared_ptr_default_allocator<_Yp, true> {797typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;798};799800template <class _Yp,801class _OrigPtr,802__enable_if_t<is_convertible<_OrigPtr*, const enable_shared_from_this<_Yp>*>::value, int> = 0>803_LIBCPP_HIDE_FROM_ABI void __enable_weak_this(const enable_shared_from_this<_Yp>* __e, _OrigPtr* __ptr) _NOEXCEPT {804typedef __remove_cv_t<_Yp> _RawYp;805if (__e && __e->__weak_this_.expired()) {806__e->__weak_this_ = shared_ptr<_RawYp>(*this, const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));807}808}809810_LIBCPP_HIDE_FROM_ABI void __enable_weak_this(...) _NOEXCEPT {}811812template <class, class _Yp>813struct __shared_ptr_default_delete : default_delete<_Yp> {};814815template <class _Yp, class _Un, size_t _Sz>816struct __shared_ptr_default_delete<_Yp[_Sz], _Un> : default_delete<_Yp[]> {};817818template <class _Yp, class _Un>819struct __shared_ptr_default_delete<_Yp[], _Un> : default_delete<_Yp[]> {};820821template <class _Up>822friend class _LIBCPP_TEMPLATE_VIS shared_ptr;823template <class _Up>824friend class _LIBCPP_TEMPLATE_VIS weak_ptr;825};826827#if _LIBCPP_STD_VER >= 17828template <class _Tp>829shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;830template <class _Tp, class _Dp>831shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;832#endif833834//835// std::allocate_shared and std::make_shared836//837template <class _Tp, class _Alloc, class... _Args, __enable_if_t<!is_array<_Tp>::value, int> = 0>838_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&&... __args) {839using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;840using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;841__allocation_guard<_ControlBlockAllocator> __guard(__a, 1);842::new ((void*)std::addressof(*__guard.__get())) _ControlBlock(__a, std::forward<_Args>(__args)...);843auto __control_block = __guard.__release_ptr();844return shared_ptr<_Tp>::__create_with_control_block(845(*__control_block).__get_elem(), std::addressof(*__control_block));846}847848template <class _Tp, class... _Args, __enable_if_t<!is_array<_Tp>::value, int> = 0>849_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(_Args&&... __args) {850return std::allocate_shared<_Tp>(allocator<__remove_cv_t<_Tp> >(), std::forward<_Args>(__args)...);851}852853#if _LIBCPP_STD_VER >= 20854855template <class _Tp, class _Alloc, __enable_if_t<!is_array<_Tp>::value, int> = 0>856_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a) {857using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>;858_ForOverwriteAllocator __alloc(__a);859return std::allocate_shared<_Tp>(__alloc);860}861862template <class _Tp, __enable_if_t<!is_array<_Tp>::value, int> = 0>863_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite() {864return std::allocate_shared_for_overwrite<_Tp>(allocator<__remove_cv_t<_Tp>>());865}866867#endif // _LIBCPP_STD_VER >= 20868869#if _LIBCPP_STD_VER >= 17870871template <size_t _Alignment>872struct __sp_aligned_storage {873alignas(_Alignment) char __storage[_Alignment];874};875876template <class _Tp, class _Alloc>877struct __unbounded_array_control_block;878879template <class _Tp, class _Alloc>880struct __unbounded_array_control_block<_Tp[], _Alloc> : __shared_weak_count {881_LIBCPP_HIDE_FROM_ABI constexpr _Tp* __get_data() noexcept { return __data_; }882883_LIBCPP_HIDE_FROM_ABI explicit __unbounded_array_control_block(884_Alloc const& __alloc, size_t __count, _Tp const& __arg)885: __alloc_(__alloc), __count_(__count) {886std::__uninitialized_allocator_fill_n_multidimensional(__alloc_, std::begin(__data_), __count_, __arg);887}888889_LIBCPP_HIDE_FROM_ABI explicit __unbounded_array_control_block(_Alloc const& __alloc, size_t __count)890: __alloc_(__alloc), __count_(__count) {891# if _LIBCPP_STD_VER >= 20892if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) {893// We are purposefully not using an allocator-aware default construction because the spec says so.894// There's currently no way of expressing default initialization in an allocator-aware manner anyway.895std::uninitialized_default_construct_n(std::begin(__data_), __count_);896} else {897std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::begin(__data_), __count_);898}899# else900std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::begin(__data_), __count_);901# endif902}903904// Returns the number of bytes required to store a control block followed by the given number905// of elements of _Tp, with the whole storage being aligned to a multiple of _Tp's alignment.906_LIBCPP_HIDE_FROM_ABI static constexpr size_t __bytes_for(size_t __elements) {907// When there's 0 elements, the control block alone is enough since it holds one element.908// Otherwise, we allocate one fewer element than requested because the control block already909// holds one. Also, we use the bitwise formula below to ensure that we allocate enough bytes910// for the whole allocation to be a multiple of _Tp's alignment. That formula is taken from [1].911//912// [1]: https://en.wikipedia.org/wiki/Data_structure_alignment#Computing_padding913size_t __bytes = __elements == 0 ? sizeof(__unbounded_array_control_block)914: (__elements - 1) * sizeof(_Tp) + sizeof(__unbounded_array_control_block);915constexpr size_t __align = alignof(_Tp);916return (__bytes + __align - 1) & ~(__align - 1);917}918919_LIBCPP_HIDE_FROM_ABI_VIRTUAL920~__unbounded_array_control_block() override {921} // can't be `= default` because of the sometimes-non-trivial union member __data_922923private:924_LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override {925# if _LIBCPP_STD_VER >= 20926if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) {927std::__reverse_destroy(__data_, __data_ + __count_);928} else {929__allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_);930std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + __count_);931}932# else933__allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_);934std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + __count_);935# endif936}937938_LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override {939using _AlignedStorage = __sp_aligned_storage<alignof(__unbounded_array_control_block)>;940using _StorageAlloc = __allocator_traits_rebind_t<_Alloc, _AlignedStorage>;941using _PointerTraits = pointer_traits<typename allocator_traits<_StorageAlloc>::pointer>;942943_StorageAlloc __tmp(__alloc_);944__alloc_.~_Alloc();945size_t __size = __unbounded_array_control_block::__bytes_for(__count_);946_AlignedStorage* __storage = reinterpret_cast<_AlignedStorage*>(this);947allocator_traits<_StorageAlloc>::deallocate(948__tmp, _PointerTraits::pointer_to(*__storage), __size / sizeof(_AlignedStorage));949}950951_LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_;952size_t __count_;953union {954_Tp __data_[1];955};956};957958template <class _Array, class _Alloc, class... _Arg>959_LIBCPP_HIDE_FROM_ABI shared_ptr<_Array>960__allocate_shared_unbounded_array(const _Alloc& __a, size_t __n, _Arg&&... __arg) {961static_assert(__libcpp_is_unbounded_array<_Array>::value);962// We compute the number of bytes necessary to hold the control block and the963// array elements. Then, we allocate an array of properly-aligned dummy structs964// large enough to hold the control block and array. This allows shifting the965// burden of aligning memory properly from us to the allocator.966using _ControlBlock = __unbounded_array_control_block<_Array, _Alloc>;967using _AlignedStorage = __sp_aligned_storage<alignof(_ControlBlock)>;968using _StorageAlloc = __allocator_traits_rebind_t<_Alloc, _AlignedStorage>;969__allocation_guard<_StorageAlloc> __guard(__a, _ControlBlock::__bytes_for(__n) / sizeof(_AlignedStorage));970_ControlBlock* __control_block = reinterpret_cast<_ControlBlock*>(std::addressof(*__guard.__get()));971std::__construct_at(__control_block, __a, __n, std::forward<_Arg>(__arg)...);972__guard.__release_ptr();973return shared_ptr<_Array>::__create_with_control_block(__control_block->__get_data(), __control_block);974}975976template <class _Tp, class _Alloc>977struct __bounded_array_control_block;978979template <class _Tp, size_t _Count, class _Alloc>980struct __bounded_array_control_block<_Tp[_Count], _Alloc> : __shared_weak_count {981_LIBCPP_HIDE_FROM_ABI constexpr _Tp* __get_data() noexcept { return __data_; }982983_LIBCPP_HIDE_FROM_ABI explicit __bounded_array_control_block(_Alloc const& __alloc, _Tp const& __arg)984: __alloc_(__alloc) {985std::__uninitialized_allocator_fill_n_multidimensional(__alloc_, std::addressof(__data_[0]), _Count, __arg);986}987988_LIBCPP_HIDE_FROM_ABI explicit __bounded_array_control_block(_Alloc const& __alloc) : __alloc_(__alloc) {989# if _LIBCPP_STD_VER >= 20990if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) {991// We are purposefully not using an allocator-aware default construction because the spec says so.992// There's currently no way of expressing default initialization in an allocator-aware manner anyway.993std::uninitialized_default_construct_n(std::addressof(__data_[0]), _Count);994} else {995std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::addressof(__data_[0]), _Count);996}997# else998std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::addressof(__data_[0]), _Count);999# endif1000}10011002_LIBCPP_HIDE_FROM_ABI_VIRTUAL1003~__bounded_array_control_block() override {1004} // can't be `= default` because of the sometimes-non-trivial union member __data_10051006private:1007_LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override {1008# if _LIBCPP_STD_VER >= 201009if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) {1010std::__reverse_destroy(__data_, __data_ + _Count);1011} else {1012__allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_);1013std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + _Count);1014}1015# else1016__allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_);1017std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + _Count);1018# endif1019}10201021_LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override {1022using _ControlBlockAlloc = __allocator_traits_rebind_t<_Alloc, __bounded_array_control_block>;1023using _PointerTraits = pointer_traits<typename allocator_traits<_ControlBlockAlloc>::pointer>;10241025_ControlBlockAlloc __tmp(__alloc_);1026__alloc_.~_Alloc();1027allocator_traits<_ControlBlockAlloc>::deallocate(__tmp, _PointerTraits::pointer_to(*this), 1);1028}10291030_LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_;1031union {1032_Tp __data_[_Count];1033};1034};10351036template <class _Array, class _Alloc, class... _Arg>1037_LIBCPP_HIDE_FROM_ABI shared_ptr<_Array> __allocate_shared_bounded_array(const _Alloc& __a, _Arg&&... __arg) {1038static_assert(__libcpp_is_bounded_array<_Array>::value);1039using _ControlBlock = __bounded_array_control_block<_Array, _Alloc>;1040using _ControlBlockAlloc = __allocator_traits_rebind_t<_Alloc, _ControlBlock>;10411042__allocation_guard<_ControlBlockAlloc> __guard(__a, 1);1043_ControlBlock* __control_block = reinterpret_cast<_ControlBlock*>(std::addressof(*__guard.__get()));1044std::__construct_at(__control_block, __a, std::forward<_Arg>(__arg)...);1045__guard.__release_ptr();1046return shared_ptr<_Array>::__create_with_control_block(__control_block->__get_data(), __control_block);1047}10481049#endif // _LIBCPP_STD_VER >= 1710501051#if _LIBCPP_STD_VER >= 2010521053// bounded array variants1054template <class _Tp, class _Alloc, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>1055_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a) {1056return std::__allocate_shared_bounded_array<_Tp>(__a);1057}10581059template <class _Tp, class _Alloc, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>1060_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, const remove_extent_t<_Tp>& __u) {1061return std::__allocate_shared_bounded_array<_Tp>(__a, __u);1062}10631064template <class _Tp, class _Alloc, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>1065_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a) {1066using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>;1067_ForOverwriteAllocator __alloc(__a);1068return std::__allocate_shared_bounded_array<_Tp>(__alloc);1069}10701071template <class _Tp, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>1072_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared() {1073return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>());1074}10751076template <class _Tp, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>1077_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(const remove_extent_t<_Tp>& __u) {1078return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>(), __u);1079}10801081template <class _Tp, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>1082_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite() {1083return std::__allocate_shared_bounded_array<_Tp>(allocator<__for_overwrite_tag>());1084}10851086// unbounded array variants1087template <class _Tp, class _Alloc, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>1088_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, size_t __n) {1089return std::__allocate_shared_unbounded_array<_Tp>(__a, __n);1090}10911092template <class _Tp, class _Alloc, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>1093_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, size_t __n, const remove_extent_t<_Tp>& __u) {1094return std::__allocate_shared_unbounded_array<_Tp>(__a, __n, __u);1095}10961097template <class _Tp, class _Alloc, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>1098_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a, size_t __n) {1099using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>;1100_ForOverwriteAllocator __alloc(__a);1101return std::__allocate_shared_unbounded_array<_Tp>(__alloc, __n);1102}11031104template <class _Tp, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>1105_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(size_t __n) {1106return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n);1107}11081109template <class _Tp, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>1110_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(size_t __n, const remove_extent_t<_Tp>& __u) {1111return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n, __u);1112}11131114template <class _Tp, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>1115_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite(size_t __n) {1116return std::__allocate_shared_unbounded_array<_Tp>(allocator<__for_overwrite_tag>(), __n);1117}11181119#endif // _LIBCPP_STD_VER >= 2011201121template <class _Tp, class _Up>1122inline _LIBCPP_HIDE_FROM_ABI bool operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {1123return __x.get() == __y.get();1124}11251126#if _LIBCPP_STD_VER <= 1711271128template <class _Tp, class _Up>1129inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {1130return !(__x == __y);1131}11321133template <class _Tp, class _Up>1134inline _LIBCPP_HIDE_FROM_ABI bool operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {1135# if _LIBCPP_STD_VER <= 111136typedef typename common_type<_Tp*, _Up*>::type _Vp;1137return less<_Vp>()(__x.get(), __y.get());1138# else1139return less<>()(__x.get(), __y.get());1140# endif1141}11421143template <class _Tp, class _Up>1144inline _LIBCPP_HIDE_FROM_ABI bool operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {1145return __y < __x;1146}11471148template <class _Tp, class _Up>1149inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {1150return !(__y < __x);1151}11521153template <class _Tp, class _Up>1154inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {1155return !(__x < __y);1156}11571158#endif // _LIBCPP_STD_VER <= 1711591160#if _LIBCPP_STD_VER >= 201161template <class _Tp, class _Up>1162_LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) noexcept {1163return compare_three_way()(__x.get(), __y.get());1164}1165#endif11661167template <class _Tp>1168inline _LIBCPP_HIDE_FROM_ABI bool operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT {1169return !__x;1170}11711172#if _LIBCPP_STD_VER <= 1711731174template <class _Tp>1175inline _LIBCPP_HIDE_FROM_ABI bool operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT {1176return !__x;1177}11781179template <class _Tp>1180inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT {1181return static_cast<bool>(__x);1182}11831184template <class _Tp>1185inline _LIBCPP_HIDE_FROM_ABI bool operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT {1186return static_cast<bool>(__x);1187}11881189template <class _Tp>1190inline _LIBCPP_HIDE_FROM_ABI bool operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT {1191return less<typename shared_ptr<_Tp>::element_type*>()(__x.get(), nullptr);1192}11931194template <class _Tp>1195inline _LIBCPP_HIDE_FROM_ABI bool operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT {1196return less<typename shared_ptr<_Tp>::element_type*>()(nullptr, __x.get());1197}11981199template <class _Tp>1200inline _LIBCPP_HIDE_FROM_ABI bool operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT {1201return nullptr < __x;1202}12031204template <class _Tp>1205inline _LIBCPP_HIDE_FROM_ABI bool operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT {1206return __x < nullptr;1207}12081209template <class _Tp>1210inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT {1211return !(nullptr < __x);1212}12131214template <class _Tp>1215inline _LIBCPP_HIDE_FROM_ABI bool operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT {1216return !(__x < nullptr);1217}12181219template <class _Tp>1220inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT {1221return !(__x < nullptr);1222}12231224template <class _Tp>1225inline _LIBCPP_HIDE_FROM_ABI bool operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT {1226return !(nullptr < __x);1227}12281229#endif // _LIBCPP_STD_VER <= 1712301231#if _LIBCPP_STD_VER >= 201232template <class _Tp>1233_LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(shared_ptr<_Tp> const& __x, nullptr_t) noexcept {1234return compare_three_way()(__x.get(), static_cast<typename shared_ptr<_Tp>::element_type*>(nullptr));1235}1236#endif12371238template <class _Tp>1239inline _LIBCPP_HIDE_FROM_ABI void swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT {1240__x.swap(__y);1241}12421243template <class _Tp, class _Up>1244inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {1245return shared_ptr<_Tp>(__r, static_cast< typename shared_ptr<_Tp>::element_type*>(__r.get()));1246}12471248// LWG-29961249// We don't backport because it is an evolutionary change.1250#if _LIBCPP_STD_VER >= 201251template <class _Tp, class _Up>1252_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> static_pointer_cast(shared_ptr<_Up>&& __r) noexcept {1253return shared_ptr<_Tp>(std::move(__r), static_cast<typename shared_ptr<_Tp>::element_type*>(__r.get()));1254}1255#endif12561257template <class _Tp, class _Up>1258inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {1259typedef typename shared_ptr<_Tp>::element_type _ET;1260_ET* __p = dynamic_cast<_ET*>(__r.get());1261return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();1262}12631264// LWG-29961265// We don't backport because it is an evolutionary change.1266#if _LIBCPP_STD_VER >= 201267template <class _Tp, class _Up>1268_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> dynamic_pointer_cast(shared_ptr<_Up>&& __r) noexcept {1269auto* __p = dynamic_cast<typename shared_ptr<_Tp>::element_type*>(__r.get());1270return __p ? shared_ptr<_Tp>(std::move(__r), __p) : shared_ptr<_Tp>();1271}1272#endif12731274template <class _Tp, class _Up>1275_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {1276typedef typename shared_ptr<_Tp>::element_type _RTp;1277return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));1278}12791280// LWG-29961281// We don't backport because it is an evolutionary change.1282#if _LIBCPP_STD_VER >= 201283template <class _Tp, class _Up>1284_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> const_pointer_cast(shared_ptr<_Up>&& __r) noexcept {1285return shared_ptr<_Tp>(std::move(__r), const_cast<typename shared_ptr<_Tp>::element_type*>(__r.get()));1286}1287#endif12881289template <class _Tp, class _Up>1290_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {1291return shared_ptr<_Tp>(__r, reinterpret_cast< typename shared_ptr<_Tp>::element_type*>(__r.get()));1292}12931294// LWG-29961295// We don't backport because it is an evolutionary change.1296#if _LIBCPP_STD_VER >= 201297template <class _Tp, class _Up>1298_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> reinterpret_pointer_cast(shared_ptr<_Up>&& __r) noexcept {1299return shared_ptr<_Tp>(std::move(__r), reinterpret_cast<typename shared_ptr<_Tp>::element_type*>(__r.get()));1300}1301#endif13021303#ifndef _LIBCPP_HAS_NO_RTTI13041305template <class _Dp, class _Tp>1306inline _LIBCPP_HIDE_FROM_ABI _Dp* get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT {1307return __p.template __get_deleter<_Dp>();1308}13091310#endif // _LIBCPP_HAS_NO_RTTI13111312template <class _Tp>1313class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr {1314public:1315#if _LIBCPP_STD_VER >= 171316typedef remove_extent_t<_Tp> element_type;1317#else1318typedef _Tp element_type;1319#endif13201321// A weak_ptr contains only two raw pointers which point to the heap and move constructing already doesn't require1322// any bookkeeping, so it's always trivially relocatable.1323using __trivially_relocatable = weak_ptr;13241325private:1326element_type* __ptr_;1327__shared_weak_count* __cntrl_;13281329public:1330_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;13311332template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>1333_LIBCPP_HIDE_FROM_ABI weak_ptr(shared_ptr<_Yp> const& __r) _NOEXCEPT;13341335_LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr const& __r) _NOEXCEPT;13361337template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>1338_LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr<_Yp> const& __r) _NOEXCEPT;13391340_LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr&& __r) _NOEXCEPT;13411342template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>1343_LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr<_Yp>&& __r) _NOEXCEPT;13441345_LIBCPP_HIDE_FROM_ABI ~weak_ptr();13461347_LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;1348template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>1349_LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;13501351_LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;1352template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>1353_LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;13541355template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>1356_LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;13571358_LIBCPP_HIDE_FROM_ABI void swap(weak_ptr& __r) _NOEXCEPT;1359_LIBCPP_HIDE_FROM_ABI void reset() _NOEXCEPT;13601361_LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __cntrl_ ? __cntrl_->use_count() : 0; }1362_LIBCPP_HIDE_FROM_ABI bool expired() const _NOEXCEPT { return __cntrl_ == nullptr || __cntrl_->use_count() == 0; }1363_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> lock() const _NOEXCEPT;1364template <class _Up>1365_LIBCPP_HIDE_FROM_ABI bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT {1366return __cntrl_ < __r.__cntrl_;1367}1368template <class _Up>1369_LIBCPP_HIDE_FROM_ABI bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT {1370return __cntrl_ < __r.__cntrl_;1371}13721373template <class _Up>1374friend class _LIBCPP_TEMPLATE_VIS weak_ptr;1375template <class _Up>1376friend class _LIBCPP_TEMPLATE_VIS shared_ptr;1377};13781379#if _LIBCPP_STD_VER >= 171380template <class _Tp>1381weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;1382#endif13831384template <class _Tp>1385inline _LIBCPP_CONSTEXPR weak_ptr<_Tp>::weak_ptr() _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {}13861387template <class _Tp>1388inline weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {1389if (__cntrl_)1390__cntrl_->__add_weak();1391}13921393template <class _Tp>1394template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >1395inline weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {1396if (__cntrl_)1397__cntrl_->__add_weak();1398}13991400template <class _Tp>1401template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >1402inline weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r) _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {1403shared_ptr<_Yp> __s = __r.lock();1404*this = weak_ptr<_Tp>(__s);1405}14061407template <class _Tp>1408inline weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {1409__r.__ptr_ = nullptr;1410__r.__cntrl_ = nullptr;1411}14121413template <class _Tp>1414template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >1415inline weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r) _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {1416shared_ptr<_Yp> __s = __r.lock();1417*this = weak_ptr<_Tp>(__s);1418__r.reset();1419}14201421template <class _Tp>1422weak_ptr<_Tp>::~weak_ptr() {1423if (__cntrl_)1424__cntrl_->__release_weak();1425}14261427template <class _Tp>1428inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT {1429weak_ptr(__r).swap(*this);1430return *this;1431}14321433template <class _Tp>1434template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >1435inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT {1436weak_ptr(__r).swap(*this);1437return *this;1438}14391440template <class _Tp>1441inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT {1442weak_ptr(std::move(__r)).swap(*this);1443return *this;1444}14451446template <class _Tp>1447template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >1448inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT {1449weak_ptr(std::move(__r)).swap(*this);1450return *this;1451}14521453template <class _Tp>1454template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >1455inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT {1456weak_ptr(__r).swap(*this);1457return *this;1458}14591460template <class _Tp>1461inline void weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT {1462std::swap(__ptr_, __r.__ptr_);1463std::swap(__cntrl_, __r.__cntrl_);1464}14651466template <class _Tp>1467inline _LIBCPP_HIDE_FROM_ABI void swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT {1468__x.swap(__y);1469}14701471template <class _Tp>1472inline void weak_ptr<_Tp>::reset() _NOEXCEPT {1473weak_ptr().swap(*this);1474}14751476template <class _Tp>1477shared_ptr<_Tp> weak_ptr<_Tp>::lock() const _NOEXCEPT {1478shared_ptr<_Tp> __r;1479__r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;1480if (__r.__cntrl_)1481__r.__ptr_ = __ptr_;1482return __r;1483}14841485#if _LIBCPP_STD_VER >= 171486template <class _Tp = void>1487struct owner_less;1488#else1489template <class _Tp>1490struct owner_less;1491#endif14921493template <class _Tp>1494struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> > : __binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool> {1495_LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT {1496return __x.owner_before(__y);1497}1498_LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT {1499return __x.owner_before(__y);1500}1501_LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT {1502return __x.owner_before(__y);1503}1504};15051506template <class _Tp>1507struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> > : __binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool> {1508_LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT {1509return __x.owner_before(__y);1510}1511_LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT {1512return __x.owner_before(__y);1513}1514_LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT {1515return __x.owner_before(__y);1516}1517};15181519#if _LIBCPP_STD_VER >= 171520template <>1521struct _LIBCPP_TEMPLATE_VIS owner_less<void> {1522template <class _Tp, class _Up>1523_LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT {1524return __x.owner_before(__y);1525}1526template <class _Tp, class _Up>1527_LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT {1528return __x.owner_before(__y);1529}1530template <class _Tp, class _Up>1531_LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT {1532return __x.owner_before(__y);1533}1534template <class _Tp, class _Up>1535_LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT {1536return __x.owner_before(__y);1537}1538typedef void is_transparent;1539};1540#endif15411542template <class _Tp>1543class _LIBCPP_TEMPLATE_VIS enable_shared_from_this {1544mutable weak_ptr<_Tp> __weak_this_;15451546protected:1547_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR enable_shared_from_this() _NOEXCEPT {}1548_LIBCPP_HIDE_FROM_ABI enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}1549_LIBCPP_HIDE_FROM_ABI enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT { return *this; }1550_LIBCPP_HIDE_FROM_ABI ~enable_shared_from_this() {}15511552public:1553_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> shared_from_this() { return shared_ptr<_Tp>(__weak_this_); }1554_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp const> shared_from_this() const { return shared_ptr<const _Tp>(__weak_this_); }15551556#if _LIBCPP_STD_VER >= 171557_LIBCPP_HIDE_FROM_ABI weak_ptr<_Tp> weak_from_this() _NOEXCEPT { return __weak_this_; }15581559_LIBCPP_HIDE_FROM_ABI weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT { return __weak_this_; }1560#endif // _LIBCPP_STD_VER >= 1715611562template <class _Up>1563friend class shared_ptr;1564};15651566template <class _Tp>1567struct _LIBCPP_TEMPLATE_VIS hash;15681569template <class _Tp>1570struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> > {1571#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)1572_LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> argument_type;1573_LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;1574#endif15751576_LIBCPP_HIDE_FROM_ABI size_t operator()(const shared_ptr<_Tp>& __ptr) const _NOEXCEPT {1577return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());1578}1579};15801581template <class _CharT, class _Traits, class _Yp>1582inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&1583operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);15841585#if !defined(_LIBCPP_HAS_NO_THREADS)15861587class _LIBCPP_EXPORTED_FROM_ABI __sp_mut {1588void* __lx_;15891590public:1591void lock() _NOEXCEPT;1592void unlock() _NOEXCEPT;15931594private:1595_LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;1596__sp_mut(const __sp_mut&);1597__sp_mut& operator=(const __sp_mut&);15981599friend _LIBCPP_EXPORTED_FROM_ABI __sp_mut& __get_sp_mut(const void*);1600};16011602_LIBCPP_EXPORTED_FROM_ABI __sp_mut& __get_sp_mut(const void*);16031604template <class _Tp>1605inline _LIBCPP_HIDE_FROM_ABI bool atomic_is_lock_free(const shared_ptr<_Tp>*) {1606return false;1607}16081609template <class _Tp>1610_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> atomic_load(const shared_ptr<_Tp>* __p) {1611__sp_mut& __m = std::__get_sp_mut(__p);1612__m.lock();1613shared_ptr<_Tp> __q = *__p;1614__m.unlock();1615return __q;1616}16171618template <class _Tp>1619inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order) {1620return std::atomic_load(__p);1621}16221623template <class _Tp>1624_LIBCPP_HIDE_FROM_ABI void atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) {1625__sp_mut& __m = std::__get_sp_mut(__p);1626__m.lock();1627__p->swap(__r);1628__m.unlock();1629}16301631template <class _Tp>1632inline _LIBCPP_HIDE_FROM_ABI void atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order) {1633std::atomic_store(__p, __r);1634}16351636template <class _Tp>1637_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) {1638__sp_mut& __m = std::__get_sp_mut(__p);1639__m.lock();1640__p->swap(__r);1641__m.unlock();1642return __r;1643}16441645template <class _Tp>1646inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>1647atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order) {1648return std::atomic_exchange(__p, __r);1649}16501651template <class _Tp>1652_LIBCPP_HIDE_FROM_ABI bool1653atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w) {1654shared_ptr<_Tp> __temp;1655__sp_mut& __m = std::__get_sp_mut(__p);1656__m.lock();1657if (__p->__owner_equivalent(*__v)) {1658std::swap(__temp, *__p);1659*__p = __w;1660__m.unlock();1661return true;1662}1663std::swap(__temp, *__v);1664*__v = *__p;1665__m.unlock();1666return false;1667}16681669template <class _Tp>1670inline _LIBCPP_HIDE_FROM_ABI bool1671atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w) {1672return std::atomic_compare_exchange_strong(__p, __v, __w);1673}16741675template <class _Tp>1676inline _LIBCPP_HIDE_FROM_ABI bool atomic_compare_exchange_strong_explicit(1677shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w, memory_order, memory_order) {1678return std::atomic_compare_exchange_strong(__p, __v, __w);1679}16801681template <class _Tp>1682inline _LIBCPP_HIDE_FROM_ABI bool atomic_compare_exchange_weak_explicit(1683shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w, memory_order, memory_order) {1684return std::atomic_compare_exchange_weak(__p, __v, __w);1685}16861687#endif // !defined(_LIBCPP_HAS_NO_THREADS)16881689_LIBCPP_END_NAMESPACE_STD16901691_LIBCPP_POP_MACROS16921693#endif // _LIBCPP___MEMORY_SHARED_PTR_H169416951696