shared_ptr.h 66 KB

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