shared_ptr.h 63 KB

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