shared_ptr.h 57 KB

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