shared_ptr_base.h 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  1. // shared_ptr and weak_ptr implementation details -*- C++ -*-
  2. // Copyright (C) 2007-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. // GCC Note: Based on files from version 1.32.0 of the Boost library.
  21. // shared_count.hpp
  22. // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
  23. // shared_ptr.hpp
  24. // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
  25. // Copyright (C) 2001, 2002, 2003 Peter Dimov
  26. // weak_ptr.hpp
  27. // Copyright (C) 2001, 2002, 2003 Peter Dimov
  28. // enable_shared_from_this.hpp
  29. // Copyright (C) 2002 Peter Dimov
  30. // Distributed under the Boost Software License, Version 1.0. (See
  31. // accompanying file LICENSE_1_0.txt or copy at
  32. // http://www.boost.org/LICENSE_1_0.txt)
  33. /** @file bits/shared_ptr_base.h
  34. * This is an internal header file, included by other library headers.
  35. * Do not attempt to use it directly. @headername{memory}
  36. */
  37. #ifndef _SHARED_PTR_BASE_H
  38. #define _SHARED_PTR_BASE_H 1
  39. #include <typeinfo>
  40. #include <bits/allocated_ptr.h>
  41. #include <bits/refwrap.h>
  42. #include <bits/stl_function.h>
  43. #include <ext/aligned_buffer.h>
  44. #if __cplusplus > 201703L
  45. # include <compare>
  46. #endif
  47. namespace std _GLIBCXX_VISIBILITY(default)
  48. {
  49. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  50. #if _GLIBCXX_USE_DEPRECATED
  51. #pragma GCC diagnostic push
  52. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  53. template<typename> class auto_ptr;
  54. #pragma GCC diagnostic pop
  55. #endif
  56. /**
  57. * @brief Exception possibly thrown by @c shared_ptr.
  58. * @ingroup exceptions
  59. */
  60. class bad_weak_ptr : public std::exception
  61. {
  62. public:
  63. virtual char const* what() const noexcept;
  64. virtual ~bad_weak_ptr() noexcept;
  65. };
  66. // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
  67. inline void
  68. __throw_bad_weak_ptr()
  69. { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr()); }
  70. using __gnu_cxx::_Lock_policy;
  71. using __gnu_cxx::__default_lock_policy;
  72. using __gnu_cxx::_S_single;
  73. using __gnu_cxx::_S_mutex;
  74. using __gnu_cxx::_S_atomic;
  75. // Empty helper class except when the template argument is _S_mutex.
  76. template<_Lock_policy _Lp>
  77. class _Mutex_base
  78. {
  79. protected:
  80. // The atomic policy uses fully-fenced builtins, single doesn't care.
  81. enum { _S_need_barriers = 0 };
  82. };
  83. template<>
  84. class _Mutex_base<_S_mutex>
  85. : public __gnu_cxx::__mutex
  86. {
  87. protected:
  88. // This policy is used when atomic builtins are not available.
  89. // The replacement atomic operations might not have the necessary
  90. // memory barriers.
  91. enum { _S_need_barriers = 1 };
  92. };
  93. template<_Lock_policy _Lp = __default_lock_policy>
  94. class _Sp_counted_base
  95. : public _Mutex_base<_Lp>
  96. {
  97. public:
  98. _Sp_counted_base() noexcept
  99. : _M_use_count(1), _M_weak_count(1) { }
  100. virtual
  101. ~_Sp_counted_base() noexcept
  102. { }
  103. // Called when _M_use_count drops to zero, to release the resources
  104. // managed by *this.
  105. virtual void
  106. _M_dispose() noexcept = 0;
  107. // Called when _M_weak_count drops to zero.
  108. virtual void
  109. _M_destroy() noexcept
  110. { delete this; }
  111. virtual void*
  112. _M_get_deleter(const std::type_info&) noexcept = 0;
  113. void
  114. _M_add_ref_copy()
  115. { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
  116. void
  117. _M_add_ref_lock();
  118. bool
  119. _M_add_ref_lock_nothrow();
  120. void
  121. _M_release() noexcept
  122. {
  123. // Be race-detector-friendly. For more info see bits/c++config.
  124. _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
  125. if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
  126. {
  127. _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
  128. _M_dispose();
  129. // There must be a memory barrier between dispose() and destroy()
  130. // to ensure that the effects of dispose() are observed in the
  131. // thread that runs destroy().
  132. // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
  133. if (_Mutex_base<_Lp>::_S_need_barriers)
  134. {
  135. __atomic_thread_fence (__ATOMIC_ACQ_REL);
  136. }
  137. // Be race-detector-friendly. For more info see bits/c++config.
  138. _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
  139. if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
  140. -1) == 1)
  141. {
  142. _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
  143. _M_destroy();
  144. }
  145. }
  146. }
  147. void
  148. _M_weak_add_ref() noexcept
  149. { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
  150. void
  151. _M_weak_release() noexcept
  152. {
  153. // Be race-detector-friendly. For more info see bits/c++config.
  154. _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
  155. if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
  156. {
  157. _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
  158. if (_Mutex_base<_Lp>::_S_need_barriers)
  159. {
  160. // See _M_release(),
  161. // destroy() must observe results of dispose()
  162. __atomic_thread_fence (__ATOMIC_ACQ_REL);
  163. }
  164. _M_destroy();
  165. }
  166. }
  167. long
  168. _M_get_use_count() const noexcept
  169. {
  170. // No memory barrier is used here so there is no synchronization
  171. // with other threads.
  172. return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
  173. }
  174. private:
  175. _Sp_counted_base(_Sp_counted_base const&) = delete;
  176. _Sp_counted_base& operator=(_Sp_counted_base const&) = delete;
  177. _Atomic_word _M_use_count; // #shared
  178. _Atomic_word _M_weak_count; // #weak + (#shared != 0)
  179. };
  180. template<>
  181. inline void
  182. _Sp_counted_base<_S_single>::
  183. _M_add_ref_lock()
  184. {
  185. if (_M_use_count == 0)
  186. __throw_bad_weak_ptr();
  187. ++_M_use_count;
  188. }
  189. template<>
  190. inline void
  191. _Sp_counted_base<_S_mutex>::
  192. _M_add_ref_lock()
  193. {
  194. __gnu_cxx::__scoped_lock sentry(*this);
  195. if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
  196. {
  197. _M_use_count = 0;
  198. __throw_bad_weak_ptr();
  199. }
  200. }
  201. template<>
  202. inline void
  203. _Sp_counted_base<_S_atomic>::
  204. _M_add_ref_lock()
  205. {
  206. // Perform lock-free add-if-not-zero operation.
  207. _Atomic_word __count = _M_get_use_count();
  208. do
  209. {
  210. if (__count == 0)
  211. __throw_bad_weak_ptr();
  212. // Replace the current counter value with the old value + 1, as
  213. // long as it's not changed meanwhile.
  214. }
  215. while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
  216. true, __ATOMIC_ACQ_REL,
  217. __ATOMIC_RELAXED));
  218. }
  219. template<>
  220. inline bool
  221. _Sp_counted_base<_S_single>::
  222. _M_add_ref_lock_nothrow()
  223. {
  224. if (_M_use_count == 0)
  225. return false;
  226. ++_M_use_count;
  227. return true;
  228. }
  229. template<>
  230. inline bool
  231. _Sp_counted_base<_S_mutex>::
  232. _M_add_ref_lock_nothrow()
  233. {
  234. __gnu_cxx::__scoped_lock sentry(*this);
  235. if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
  236. {
  237. _M_use_count = 0;
  238. return false;
  239. }
  240. return true;
  241. }
  242. template<>
  243. inline bool
  244. _Sp_counted_base<_S_atomic>::
  245. _M_add_ref_lock_nothrow()
  246. {
  247. // Perform lock-free add-if-not-zero operation.
  248. _Atomic_word __count = _M_get_use_count();
  249. do
  250. {
  251. if (__count == 0)
  252. return false;
  253. // Replace the current counter value with the old value + 1, as
  254. // long as it's not changed meanwhile.
  255. }
  256. while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
  257. true, __ATOMIC_ACQ_REL,
  258. __ATOMIC_RELAXED));
  259. return true;
  260. }
  261. template<>
  262. inline void
  263. _Sp_counted_base<_S_single>::_M_add_ref_copy()
  264. { ++_M_use_count; }
  265. template<>
  266. inline void
  267. _Sp_counted_base<_S_single>::_M_release() noexcept
  268. {
  269. if (--_M_use_count == 0)
  270. {
  271. _M_dispose();
  272. if (--_M_weak_count == 0)
  273. _M_destroy();
  274. }
  275. }
  276. template<>
  277. inline void
  278. _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept
  279. { ++_M_weak_count; }
  280. template<>
  281. inline void
  282. _Sp_counted_base<_S_single>::_M_weak_release() noexcept
  283. {
  284. if (--_M_weak_count == 0)
  285. _M_destroy();
  286. }
  287. template<>
  288. inline long
  289. _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept
  290. { return _M_use_count; }
  291. // Forward declarations.
  292. template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
  293. class __shared_ptr;
  294. template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
  295. class __weak_ptr;
  296. template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
  297. class __enable_shared_from_this;
  298. template<typename _Tp>
  299. class shared_ptr;
  300. template<typename _Tp>
  301. class weak_ptr;
  302. template<typename _Tp>
  303. struct owner_less;
  304. template<typename _Tp>
  305. class enable_shared_from_this;
  306. template<_Lock_policy _Lp = __default_lock_policy>
  307. class __weak_count;
  308. template<_Lock_policy _Lp = __default_lock_policy>
  309. class __shared_count;
  310. // Counted ptr with no deleter or allocator support
  311. template<typename _Ptr, _Lock_policy _Lp>
  312. class _Sp_counted_ptr final : public _Sp_counted_base<_Lp>
  313. {
  314. public:
  315. explicit
  316. _Sp_counted_ptr(_Ptr __p) noexcept
  317. : _M_ptr(__p) { }
  318. virtual void
  319. _M_dispose() noexcept
  320. { delete _M_ptr; }
  321. virtual void
  322. _M_destroy() noexcept
  323. { delete this; }
  324. virtual void*
  325. _M_get_deleter(const std::type_info&) noexcept
  326. { return nullptr; }
  327. _Sp_counted_ptr(const _Sp_counted_ptr&) = delete;
  328. _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete;
  329. private:
  330. _Ptr _M_ptr;
  331. };
  332. template<>
  333. inline void
  334. _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
  335. template<>
  336. inline void
  337. _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
  338. template<>
  339. inline void
  340. _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
  341. template<int _Nm, typename _Tp,
  342. bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
  343. struct _Sp_ebo_helper;
  344. /// Specialization using EBO.
  345. template<int _Nm, typename _Tp>
  346. struct _Sp_ebo_helper<_Nm, _Tp, true> : private _Tp
  347. {
  348. explicit _Sp_ebo_helper(const _Tp& __tp) : _Tp(__tp) { }
  349. explicit _Sp_ebo_helper(_Tp&& __tp) : _Tp(std::move(__tp)) { }
  350. static _Tp&
  351. _S_get(_Sp_ebo_helper& __eboh) { return static_cast<_Tp&>(__eboh); }
  352. };
  353. /// Specialization not using EBO.
  354. template<int _Nm, typename _Tp>
  355. struct _Sp_ebo_helper<_Nm, _Tp, false>
  356. {
  357. explicit _Sp_ebo_helper(const _Tp& __tp) : _M_tp(__tp) { }
  358. explicit _Sp_ebo_helper(_Tp&& __tp) : _M_tp(std::move(__tp)) { }
  359. static _Tp&
  360. _S_get(_Sp_ebo_helper& __eboh)
  361. { return __eboh._M_tp; }
  362. private:
  363. _Tp _M_tp;
  364. };
  365. // Support for custom deleter and/or allocator
  366. template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
  367. class _Sp_counted_deleter final : public _Sp_counted_base<_Lp>
  368. {
  369. class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
  370. {
  371. typedef _Sp_ebo_helper<0, _Deleter> _Del_base;
  372. typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base;
  373. public:
  374. _Impl(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept
  375. : _M_ptr(__p), _Del_base(std::move(__d)), _Alloc_base(__a)
  376. { }
  377. _Deleter& _M_del() noexcept { return _Del_base::_S_get(*this); }
  378. _Alloc& _M_alloc() noexcept { return _Alloc_base::_S_get(*this); }
  379. _Ptr _M_ptr;
  380. };
  381. public:
  382. using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
  383. // __d(__p) must not throw.
  384. _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
  385. : _M_impl(__p, std::move(__d), _Alloc()) { }
  386. // __d(__p) must not throw.
  387. _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept
  388. : _M_impl(__p, std::move(__d), __a) { }
  389. ~_Sp_counted_deleter() noexcept { }
  390. virtual void
  391. _M_dispose() noexcept
  392. { _M_impl._M_del()(_M_impl._M_ptr); }
  393. virtual void
  394. _M_destroy() noexcept
  395. {
  396. __allocator_type __a(_M_impl._M_alloc());
  397. __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
  398. this->~_Sp_counted_deleter();
  399. }
  400. virtual void*
  401. _M_get_deleter(const std::type_info& __ti) noexcept
  402. {
  403. #if __cpp_rtti
  404. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  405. // 2400. shared_ptr's get_deleter() should use addressof()
  406. return __ti == typeid(_Deleter)
  407. ? std::__addressof(_M_impl._M_del())
  408. : nullptr;
  409. #else
  410. return nullptr;
  411. #endif
  412. }
  413. private:
  414. _Impl _M_impl;
  415. };
  416. // helpers for make_shared / allocate_shared
  417. struct _Sp_make_shared_tag
  418. {
  419. private:
  420. template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
  421. friend class _Sp_counted_ptr_inplace;
  422. static const type_info&
  423. _S_ti() noexcept _GLIBCXX_VISIBILITY(default)
  424. {
  425. alignas(type_info) static constexpr char __tag[sizeof(type_info)] = { };
  426. return reinterpret_cast<const type_info&>(__tag);
  427. }
  428. static bool _S_eq(const type_info&) noexcept;
  429. };
  430. template<typename _Alloc>
  431. struct _Sp_alloc_shared_tag
  432. {
  433. const _Alloc& _M_a;
  434. };
  435. template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
  436. class _Sp_counted_ptr_inplace final : public _Sp_counted_base<_Lp>
  437. {
  438. class _Impl : _Sp_ebo_helper<0, _Alloc>
  439. {
  440. typedef _Sp_ebo_helper<0, _Alloc> _A_base;
  441. public:
  442. explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
  443. _Alloc& _M_alloc() noexcept { return _A_base::_S_get(*this); }
  444. __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
  445. };
  446. public:
  447. using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
  448. // Alloc parameter is not a reference so doesn't alias anything in __args
  449. template<typename... _Args>
  450. _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
  451. : _M_impl(__a)
  452. {
  453. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  454. // 2070. allocate_shared should use allocator_traits<A>::construct
  455. allocator_traits<_Alloc>::construct(__a, _M_ptr(),
  456. std::forward<_Args>(__args)...); // might throw
  457. }
  458. ~_Sp_counted_ptr_inplace() noexcept { }
  459. virtual void
  460. _M_dispose() noexcept
  461. {
  462. allocator_traits<_Alloc>::destroy(_M_impl._M_alloc(), _M_ptr());
  463. }
  464. // Override because the allocator needs to know the dynamic type
  465. virtual void
  466. _M_destroy() noexcept
  467. {
  468. __allocator_type __a(_M_impl._M_alloc());
  469. __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
  470. this->~_Sp_counted_ptr_inplace();
  471. }
  472. private:
  473. friend class __shared_count<_Lp>; // To be able to call _M_ptr().
  474. // No longer used, but code compiled against old libstdc++ headers
  475. // might still call it from __shared_ptr ctor to get the pointer out.
  476. virtual void*
  477. _M_get_deleter(const std::type_info& __ti) noexcept override
  478. {
  479. auto __ptr = const_cast<typename remove_cv<_Tp>::type*>(_M_ptr());
  480. // Check for the fake type_info first, so we don't try to access it
  481. // as a real type_info object. Otherwise, check if it's the real
  482. // type_info for this class. With RTTI enabled we can check directly,
  483. // or call a library function to do it.
  484. if (&__ti == &_Sp_make_shared_tag::_S_ti()
  485. ||
  486. #if __cpp_rtti
  487. __ti == typeid(_Sp_make_shared_tag)
  488. #else
  489. _Sp_make_shared_tag::_S_eq(__ti)
  490. #endif
  491. )
  492. return __ptr;
  493. return nullptr;
  494. }
  495. _Tp* _M_ptr() noexcept { return _M_impl._M_storage._M_ptr(); }
  496. _Impl _M_impl;
  497. };
  498. // The default deleter for shared_ptr<T[]> and shared_ptr<T[N]>.
  499. struct __sp_array_delete
  500. {
  501. template<typename _Yp>
  502. void operator()(_Yp* __p) const { delete[] __p; }
  503. };
  504. template<_Lock_policy _Lp>
  505. class __shared_count
  506. {
  507. template<typename _Tp>
  508. struct __not_alloc_shared_tag { using type = void; };
  509. template<typename _Tp>
  510. struct __not_alloc_shared_tag<_Sp_alloc_shared_tag<_Tp>> { };
  511. public:
  512. constexpr __shared_count() noexcept : _M_pi(0)
  513. { }
  514. template<typename _Ptr>
  515. explicit
  516. __shared_count(_Ptr __p) : _M_pi(0)
  517. {
  518. __try
  519. {
  520. _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
  521. }
  522. __catch(...)
  523. {
  524. delete __p;
  525. __throw_exception_again;
  526. }
  527. }
  528. template<typename _Ptr>
  529. __shared_count(_Ptr __p, /* is_array = */ false_type)
  530. : __shared_count(__p)
  531. { }
  532. template<typename _Ptr>
  533. __shared_count(_Ptr __p, /* is_array = */ true_type)
  534. : __shared_count(__p, __sp_array_delete{}, allocator<void>())
  535. { }
  536. template<typename _Ptr, typename _Deleter,
  537. typename = typename __not_alloc_shared_tag<_Deleter>::type>
  538. __shared_count(_Ptr __p, _Deleter __d)
  539. : __shared_count(__p, std::move(__d), allocator<void>())
  540. { }
  541. template<typename _Ptr, typename _Deleter, typename _Alloc,
  542. typename = typename __not_alloc_shared_tag<_Deleter>::type>
  543. __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
  544. {
  545. typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
  546. __try
  547. {
  548. typename _Sp_cd_type::__allocator_type __a2(__a);
  549. auto __guard = std::__allocate_guarded(__a2);
  550. _Sp_cd_type* __mem = __guard.get();
  551. ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a));
  552. _M_pi = __mem;
  553. __guard = nullptr;
  554. }
  555. __catch(...)
  556. {
  557. __d(__p); // Call _Deleter on __p.
  558. __throw_exception_again;
  559. }
  560. }
  561. template<typename _Tp, typename _Alloc, typename... _Args>
  562. __shared_count(_Tp*& __p, _Sp_alloc_shared_tag<_Alloc> __a,
  563. _Args&&... __args)
  564. {
  565. typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
  566. typename _Sp_cp_type::__allocator_type __a2(__a._M_a);
  567. auto __guard = std::__allocate_guarded(__a2);
  568. _Sp_cp_type* __mem = __guard.get();
  569. auto __pi = ::new (__mem)
  570. _Sp_cp_type(__a._M_a, std::forward<_Args>(__args)...);
  571. __guard = nullptr;
  572. _M_pi = __pi;
  573. __p = __pi->_M_ptr();
  574. }
  575. #if _GLIBCXX_USE_DEPRECATED
  576. #pragma GCC diagnostic push
  577. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  578. // Special case for auto_ptr<_Tp> to provide the strong guarantee.
  579. template<typename _Tp>
  580. explicit
  581. __shared_count(std::auto_ptr<_Tp>&& __r);
  582. #pragma GCC diagnostic pop
  583. #endif
  584. // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
  585. template<typename _Tp, typename _Del>
  586. explicit
  587. __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0)
  588. {
  589. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  590. // 2415. Inconsistency between unique_ptr and shared_ptr
  591. if (__r.get() == nullptr)
  592. return;
  593. using _Ptr = typename unique_ptr<_Tp, _Del>::pointer;
  594. using _Del2 = typename conditional<is_reference<_Del>::value,
  595. reference_wrapper<typename remove_reference<_Del>::type>,
  596. _Del>::type;
  597. using _Sp_cd_type
  598. = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
  599. using _Alloc = allocator<_Sp_cd_type>;
  600. using _Alloc_traits = allocator_traits<_Alloc>;
  601. _Alloc __a;
  602. _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
  603. _Alloc_traits::construct(__a, __mem, __r.release(),
  604. __r.get_deleter()); // non-throwing
  605. _M_pi = __mem;
  606. }
  607. // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
  608. explicit __shared_count(const __weak_count<_Lp>& __r);
  609. // Does not throw if __r._M_get_use_count() == 0, caller must check.
  610. explicit __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t);
  611. ~__shared_count() noexcept
  612. {
  613. if (_M_pi != nullptr)
  614. _M_pi->_M_release();
  615. }
  616. __shared_count(const __shared_count& __r) noexcept
  617. : _M_pi(__r._M_pi)
  618. {
  619. if (_M_pi != 0)
  620. _M_pi->_M_add_ref_copy();
  621. }
  622. __shared_count&
  623. operator=(const __shared_count& __r) noexcept
  624. {
  625. _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
  626. if (__tmp != _M_pi)
  627. {
  628. if (__tmp != 0)
  629. __tmp->_M_add_ref_copy();
  630. if (_M_pi != 0)
  631. _M_pi->_M_release();
  632. _M_pi = __tmp;
  633. }
  634. return *this;
  635. }
  636. void
  637. _M_swap(__shared_count& __r) noexcept
  638. {
  639. _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
  640. __r._M_pi = _M_pi;
  641. _M_pi = __tmp;
  642. }
  643. long
  644. _M_get_use_count() const noexcept
  645. { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
  646. bool
  647. _M_unique() const noexcept
  648. { return this->_M_get_use_count() == 1; }
  649. void*
  650. _M_get_deleter(const std::type_info& __ti) const noexcept
  651. { return _M_pi ? _M_pi->_M_get_deleter(__ti) : nullptr; }
  652. bool
  653. _M_less(const __shared_count& __rhs) const noexcept
  654. { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
  655. bool
  656. _M_less(const __weak_count<_Lp>& __rhs) const noexcept
  657. { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
  658. // Friend function injected into enclosing namespace and found by ADL
  659. friend inline bool
  660. operator==(const __shared_count& __a, const __shared_count& __b) noexcept
  661. { return __a._M_pi == __b._M_pi; }
  662. private:
  663. friend class __weak_count<_Lp>;
  664. _Sp_counted_base<_Lp>* _M_pi;
  665. };
  666. template<_Lock_policy _Lp>
  667. class __weak_count
  668. {
  669. public:
  670. constexpr __weak_count() noexcept : _M_pi(nullptr)
  671. { }
  672. __weak_count(const __shared_count<_Lp>& __r) noexcept
  673. : _M_pi(__r._M_pi)
  674. {
  675. if (_M_pi != nullptr)
  676. _M_pi->_M_weak_add_ref();
  677. }
  678. __weak_count(const __weak_count& __r) noexcept
  679. : _M_pi(__r._M_pi)
  680. {
  681. if (_M_pi != nullptr)
  682. _M_pi->_M_weak_add_ref();
  683. }
  684. __weak_count(__weak_count&& __r) noexcept
  685. : _M_pi(__r._M_pi)
  686. { __r._M_pi = nullptr; }
  687. ~__weak_count() noexcept
  688. {
  689. if (_M_pi != nullptr)
  690. _M_pi->_M_weak_release();
  691. }
  692. __weak_count&
  693. operator=(const __shared_count<_Lp>& __r) noexcept
  694. {
  695. _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
  696. if (__tmp != nullptr)
  697. __tmp->_M_weak_add_ref();
  698. if (_M_pi != nullptr)
  699. _M_pi->_M_weak_release();
  700. _M_pi = __tmp;
  701. return *this;
  702. }
  703. __weak_count&
  704. operator=(const __weak_count& __r) noexcept
  705. {
  706. _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
  707. if (__tmp != nullptr)
  708. __tmp->_M_weak_add_ref();
  709. if (_M_pi != nullptr)
  710. _M_pi->_M_weak_release();
  711. _M_pi = __tmp;
  712. return *this;
  713. }
  714. __weak_count&
  715. operator=(__weak_count&& __r) noexcept
  716. {
  717. if (_M_pi != nullptr)
  718. _M_pi->_M_weak_release();
  719. _M_pi = __r._M_pi;
  720. __r._M_pi = nullptr;
  721. return *this;
  722. }
  723. void
  724. _M_swap(__weak_count& __r) noexcept
  725. {
  726. _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
  727. __r._M_pi = _M_pi;
  728. _M_pi = __tmp;
  729. }
  730. long
  731. _M_get_use_count() const noexcept
  732. { return _M_pi != nullptr ? _M_pi->_M_get_use_count() : 0; }
  733. bool
  734. _M_less(const __weak_count& __rhs) const noexcept
  735. { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
  736. bool
  737. _M_less(const __shared_count<_Lp>& __rhs) const noexcept
  738. { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
  739. // Friend function injected into enclosing namespace and found by ADL
  740. friend inline bool
  741. operator==(const __weak_count& __a, const __weak_count& __b) noexcept
  742. { return __a._M_pi == __b._M_pi; }
  743. private:
  744. friend class __shared_count<_Lp>;
  745. _Sp_counted_base<_Lp>* _M_pi;
  746. };
  747. // Now that __weak_count is defined we can define this constructor:
  748. template<_Lock_policy _Lp>
  749. inline
  750. __shared_count<_Lp>::__shared_count(const __weak_count<_Lp>& __r)
  751. : _M_pi(__r._M_pi)
  752. {
  753. if (_M_pi != nullptr)
  754. _M_pi->_M_add_ref_lock();
  755. else
  756. __throw_bad_weak_ptr();
  757. }
  758. // Now that __weak_count is defined we can define this constructor:
  759. template<_Lock_policy _Lp>
  760. inline
  761. __shared_count<_Lp>::
  762. __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t)
  763. : _M_pi(__r._M_pi)
  764. {
  765. if (_M_pi != nullptr)
  766. if (!_M_pi->_M_add_ref_lock_nothrow())
  767. _M_pi = nullptr;
  768. }
  769. #define __cpp_lib_shared_ptr_arrays 201611L
  770. // Helper traits for shared_ptr of array:
  771. // A pointer type Y* is said to be compatible with a pointer type T* when
  772. // either Y* is convertible to T* or Y is U[N] and T is U cv [].
  773. template<typename _Yp_ptr, typename _Tp_ptr>
  774. struct __sp_compatible_with
  775. : false_type
  776. { };
  777. template<typename _Yp, typename _Tp>
  778. struct __sp_compatible_with<_Yp*, _Tp*>
  779. : is_convertible<_Yp*, _Tp*>::type
  780. { };
  781. template<typename _Up, size_t _Nm>
  782. struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
  783. : true_type
  784. { };
  785. template<typename _Up, size_t _Nm>
  786. struct __sp_compatible_with<_Up(*)[_Nm], const _Up(*)[]>
  787. : true_type
  788. { };
  789. template<typename _Up, size_t _Nm>
  790. struct __sp_compatible_with<_Up(*)[_Nm], volatile _Up(*)[]>
  791. : true_type
  792. { };
  793. template<typename _Up, size_t _Nm>
  794. struct __sp_compatible_with<_Up(*)[_Nm], const volatile _Up(*)[]>
  795. : true_type
  796. { };
  797. // Test conversion from Y(*)[N] to U(*)[N] without forming invalid type Y[N].
  798. template<typename _Up, size_t _Nm, typename _Yp, typename = void>
  799. struct __sp_is_constructible_arrN
  800. : false_type
  801. { };
  802. template<typename _Up, size_t _Nm, typename _Yp>
  803. struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
  804. : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
  805. { };
  806. // Test conversion from Y(*)[] to U(*)[] without forming invalid type Y[].
  807. template<typename _Up, typename _Yp, typename = void>
  808. struct __sp_is_constructible_arr
  809. : false_type
  810. { };
  811. template<typename _Up, typename _Yp>
  812. struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
  813. : is_convertible<_Yp(*)[], _Up(*)[]>::type
  814. { };
  815. // Trait to check if shared_ptr<T> can be constructed from Y*.
  816. template<typename _Tp, typename _Yp>
  817. struct __sp_is_constructible;
  818. // When T is U[N], Y(*)[N] shall be convertible to T*;
  819. template<typename _Up, size_t _Nm, typename _Yp>
  820. struct __sp_is_constructible<_Up[_Nm], _Yp>
  821. : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
  822. { };
  823. // when T is U[], Y(*)[] shall be convertible to T*;
  824. template<typename _Up, typename _Yp>
  825. struct __sp_is_constructible<_Up[], _Yp>
  826. : __sp_is_constructible_arr<_Up, _Yp>::type
  827. { };
  828. // otherwise, Y* shall be convertible to T*.
  829. template<typename _Tp, typename _Yp>
  830. struct __sp_is_constructible
  831. : is_convertible<_Yp*, _Tp*>::type
  832. { };
  833. // Define operator* and operator-> for shared_ptr<T>.
  834. template<typename _Tp, _Lock_policy _Lp,
  835. bool = is_array<_Tp>::value, bool = is_void<_Tp>::value>
  836. class __shared_ptr_access
  837. {
  838. public:
  839. using element_type = _Tp;
  840. element_type&
  841. operator*() const noexcept
  842. {
  843. __glibcxx_assert(_M_get() != nullptr);
  844. return *_M_get();
  845. }
  846. element_type*
  847. operator->() const noexcept
  848. {
  849. _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
  850. return _M_get();
  851. }
  852. private:
  853. element_type*
  854. _M_get() const noexcept
  855. { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
  856. };
  857. // Define operator-> for shared_ptr<cv void>.
  858. template<typename _Tp, _Lock_policy _Lp>
  859. class __shared_ptr_access<_Tp, _Lp, false, true>
  860. {
  861. public:
  862. using element_type = _Tp;
  863. element_type*
  864. operator->() const noexcept
  865. {
  866. auto __ptr = static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get();
  867. _GLIBCXX_DEBUG_PEDASSERT(__ptr != nullptr);
  868. return __ptr;
  869. }
  870. };
  871. // Define operator[] for shared_ptr<T[]> and shared_ptr<T[N]>.
  872. template<typename _Tp, _Lock_policy _Lp>
  873. class __shared_ptr_access<_Tp, _Lp, true, false>
  874. {
  875. public:
  876. using element_type = typename remove_extent<_Tp>::type;
  877. #if __cplusplus <= 201402L
  878. [[__deprecated__("shared_ptr<T[]>::operator* is absent from C++17")]]
  879. element_type&
  880. operator*() const noexcept
  881. {
  882. __glibcxx_assert(_M_get() != nullptr);
  883. return *_M_get();
  884. }
  885. [[__deprecated__("shared_ptr<T[]>::operator-> is absent from C++17")]]
  886. element_type*
  887. operator->() const noexcept
  888. {
  889. _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
  890. return _M_get();
  891. }
  892. #endif
  893. element_type&
  894. operator[](ptrdiff_t __i) const
  895. {
  896. __glibcxx_assert(_M_get() != nullptr);
  897. __glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value);
  898. return _M_get()[__i];
  899. }
  900. private:
  901. element_type*
  902. _M_get() const noexcept
  903. { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
  904. };
  905. template<typename _Tp, _Lock_policy _Lp>
  906. class __shared_ptr
  907. : public __shared_ptr_access<_Tp, _Lp>
  908. {
  909. public:
  910. using element_type = typename remove_extent<_Tp>::type;
  911. private:
  912. // Constraint for taking ownership of a pointer of type _Yp*:
  913. template<typename _Yp>
  914. using _SafeConv
  915. = typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
  916. // Constraint for construction from shared_ptr and weak_ptr:
  917. template<typename _Yp, typename _Res = void>
  918. using _Compatible = typename
  919. enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
  920. // Constraint for assignment from shared_ptr and weak_ptr:
  921. template<typename _Yp>
  922. using _Assignable = _Compatible<_Yp, __shared_ptr&>;
  923. // Constraint for construction from unique_ptr:
  924. template<typename _Yp, typename _Del, typename _Res = void,
  925. typename _Ptr = typename unique_ptr<_Yp, _Del>::pointer>
  926. using _UniqCompatible = typename enable_if<__and_<
  927. __sp_compatible_with<_Yp*, _Tp*>, is_convertible<_Ptr, element_type*>
  928. >::value, _Res>::type;
  929. // Constraint for assignment from unique_ptr:
  930. template<typename _Yp, typename _Del>
  931. using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
  932. public:
  933. #if __cplusplus > 201402L
  934. using weak_type = __weak_ptr<_Tp, _Lp>;
  935. #endif
  936. constexpr __shared_ptr() noexcept
  937. : _M_ptr(0), _M_refcount()
  938. { }
  939. template<typename _Yp, typename = _SafeConv<_Yp>>
  940. explicit
  941. __shared_ptr(_Yp* __p)
  942. : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
  943. {
  944. static_assert( !is_void<_Yp>::value, "incomplete type" );
  945. static_assert( sizeof(_Yp) > 0, "incomplete type" );
  946. _M_enable_shared_from_this_with(__p);
  947. }
  948. template<typename _Yp, typename _Deleter, typename = _SafeConv<_Yp>>
  949. __shared_ptr(_Yp* __p, _Deleter __d)
  950. : _M_ptr(__p), _M_refcount(__p, std::move(__d))
  951. {
  952. static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
  953. "deleter expression d(p) is well-formed");
  954. _M_enable_shared_from_this_with(__p);
  955. }
  956. template<typename _Yp, typename _Deleter, typename _Alloc,
  957. typename = _SafeConv<_Yp>>
  958. __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
  959. : _M_ptr(__p), _M_refcount(__p, std::move(__d), std::move(__a))
  960. {
  961. static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
  962. "deleter expression d(p) is well-formed");
  963. _M_enable_shared_from_this_with(__p);
  964. }
  965. template<typename _Deleter>
  966. __shared_ptr(nullptr_t __p, _Deleter __d)
  967. : _M_ptr(0), _M_refcount(__p, std::move(__d))
  968. { }
  969. template<typename _Deleter, typename _Alloc>
  970. __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
  971. : _M_ptr(0), _M_refcount(__p, std::move(__d), std::move(__a))
  972. { }
  973. // Aliasing constructor
  974. template<typename _Yp>
  975. __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r,
  976. element_type* __p) noexcept
  977. : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
  978. { }
  979. // Aliasing constructor
  980. template<typename _Yp>
  981. __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r,
  982. element_type* __p) noexcept
  983. : _M_ptr(__p), _M_refcount()
  984. {
  985. _M_refcount._M_swap(__r._M_refcount);
  986. __r._M_ptr = 0;
  987. }
  988. __shared_ptr(const __shared_ptr&) noexcept = default;
  989. __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
  990. ~__shared_ptr() = default;
  991. template<typename _Yp, typename = _Compatible<_Yp>>
  992. __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
  993. : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
  994. { }
  995. __shared_ptr(__shared_ptr&& __r) noexcept
  996. : _M_ptr(__r._M_ptr), _M_refcount()
  997. {
  998. _M_refcount._M_swap(__r._M_refcount);
  999. __r._M_ptr = 0;
  1000. }
  1001. template<typename _Yp, typename = _Compatible<_Yp>>
  1002. __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
  1003. : _M_ptr(__r._M_ptr), _M_refcount()
  1004. {
  1005. _M_refcount._M_swap(__r._M_refcount);
  1006. __r._M_ptr = 0;
  1007. }
  1008. template<typename _Yp, typename = _Compatible<_Yp>>
  1009. explicit __shared_ptr(const __weak_ptr<_Yp, _Lp>& __r)
  1010. : _M_refcount(__r._M_refcount) // may throw
  1011. {
  1012. // It is now safe to copy __r._M_ptr, as
  1013. // _M_refcount(__r._M_refcount) did not throw.
  1014. _M_ptr = __r._M_ptr;
  1015. }
  1016. // If an exception is thrown this constructor has no effect.
  1017. template<typename _Yp, typename _Del,
  1018. typename = _UniqCompatible<_Yp, _Del>>
  1019. __shared_ptr(unique_ptr<_Yp, _Del>&& __r)
  1020. : _M_ptr(__r.get()), _M_refcount()
  1021. {
  1022. auto __raw = __to_address(__r.get());
  1023. _M_refcount = __shared_count<_Lp>(std::move(__r));
  1024. _M_enable_shared_from_this_with(__raw);
  1025. }
  1026. #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
  1027. protected:
  1028. // If an exception is thrown this constructor has no effect.
  1029. template<typename _Tp1, typename _Del,
  1030. typename enable_if<__and_<
  1031. __not_<is_array<_Tp>>, is_array<_Tp1>,
  1032. is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
  1033. >::value, bool>::type = true>
  1034. __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
  1035. : _M_ptr(__r.get()), _M_refcount()
  1036. {
  1037. auto __raw = __to_address(__r.get());
  1038. _M_refcount = __shared_count<_Lp>(std::move(__r));
  1039. _M_enable_shared_from_this_with(__raw);
  1040. }
  1041. public:
  1042. #endif
  1043. #if _GLIBCXX_USE_DEPRECATED
  1044. #pragma GCC diagnostic push
  1045. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  1046. // Postcondition: use_count() == 1 and __r.get() == 0
  1047. template<typename _Yp, typename = _Compatible<_Yp>>
  1048. __shared_ptr(auto_ptr<_Yp>&& __r);
  1049. #pragma GCC diagnostic pop
  1050. #endif
  1051. constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
  1052. template<typename _Yp>
  1053. _Assignable<_Yp>
  1054. operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
  1055. {
  1056. _M_ptr = __r._M_ptr;
  1057. _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
  1058. return *this;
  1059. }
  1060. #if _GLIBCXX_USE_DEPRECATED
  1061. #pragma GCC diagnostic push
  1062. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  1063. template<typename _Yp>
  1064. _Assignable<_Yp>
  1065. operator=(auto_ptr<_Yp>&& __r)
  1066. {
  1067. __shared_ptr(std::move(__r)).swap(*this);
  1068. return *this;
  1069. }
  1070. #pragma GCC diagnostic pop
  1071. #endif
  1072. __shared_ptr&
  1073. operator=(__shared_ptr&& __r) noexcept
  1074. {
  1075. __shared_ptr(std::move(__r)).swap(*this);
  1076. return *this;
  1077. }
  1078. template<class _Yp>
  1079. _Assignable<_Yp>
  1080. operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept
  1081. {
  1082. __shared_ptr(std::move(__r)).swap(*this);
  1083. return *this;
  1084. }
  1085. template<typename _Yp, typename _Del>
  1086. _UniqAssignable<_Yp, _Del>
  1087. operator=(unique_ptr<_Yp, _Del>&& __r)
  1088. {
  1089. __shared_ptr(std::move(__r)).swap(*this);
  1090. return *this;
  1091. }
  1092. void
  1093. reset() noexcept
  1094. { __shared_ptr().swap(*this); }
  1095. template<typename _Yp>
  1096. _SafeConv<_Yp>
  1097. reset(_Yp* __p) // _Yp must be complete.
  1098. {
  1099. // Catch self-reset errors.
  1100. __glibcxx_assert(__p == 0 || __p != _M_ptr);
  1101. __shared_ptr(__p).swap(*this);
  1102. }
  1103. template<typename _Yp, typename _Deleter>
  1104. _SafeConv<_Yp>
  1105. reset(_Yp* __p, _Deleter __d)
  1106. { __shared_ptr(__p, std::move(__d)).swap(*this); }
  1107. template<typename _Yp, typename _Deleter, typename _Alloc>
  1108. _SafeConv<_Yp>
  1109. reset(_Yp* __p, _Deleter __d, _Alloc __a)
  1110. { __shared_ptr(__p, std::move(__d), std::move(__a)).swap(*this); }
  1111. /// Return the stored pointer.
  1112. element_type*
  1113. get() const noexcept
  1114. { return _M_ptr; }
  1115. /// Return true if the stored pointer is not null.
  1116. explicit operator bool() const // never throws
  1117. { return _M_ptr == 0 ? false : true; }
  1118. /// Return true if use_count() == 1.
  1119. bool
  1120. unique() const noexcept
  1121. { return _M_refcount._M_unique(); }
  1122. /// If *this owns a pointer, return the number of owners, otherwise zero.
  1123. long
  1124. use_count() const noexcept
  1125. { return _M_refcount._M_get_use_count(); }
  1126. /// Exchange both the owned pointer and the stored pointer.
  1127. void
  1128. swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
  1129. {
  1130. std::swap(_M_ptr, __other._M_ptr);
  1131. _M_refcount._M_swap(__other._M_refcount);
  1132. }
  1133. /** @brief Define an ordering based on ownership.
  1134. *
  1135. * This function defines a strict weak ordering between two shared_ptr
  1136. * or weak_ptr objects, such that one object is less than the other
  1137. * unless they share ownership of the same pointer, or are both empty.
  1138. * @{
  1139. */
  1140. template<typename _Tp1>
  1141. bool
  1142. owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const noexcept
  1143. { return _M_refcount._M_less(__rhs._M_refcount); }
  1144. template<typename _Tp1>
  1145. bool
  1146. owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const noexcept
  1147. { return _M_refcount._M_less(__rhs._M_refcount); }
  1148. // @}
  1149. protected:
  1150. // This constructor is non-standard, it is used by allocate_shared.
  1151. template<typename _Alloc, typename... _Args>
  1152. __shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
  1153. : _M_ptr(), _M_refcount(_M_ptr, __tag, std::forward<_Args>(__args)...)
  1154. { _M_enable_shared_from_this_with(_M_ptr); }
  1155. template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
  1156. typename... _Args>
  1157. friend __shared_ptr<_Tp1, _Lp1>
  1158. __allocate_shared(const _Alloc& __a, _Args&&... __args);
  1159. // This constructor is used by __weak_ptr::lock() and
  1160. // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t).
  1161. __shared_ptr(const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t)
  1162. : _M_refcount(__r._M_refcount, std::nothrow)
  1163. {
  1164. _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr : nullptr;
  1165. }
  1166. friend class __weak_ptr<_Tp, _Lp>;
  1167. private:
  1168. template<typename _Yp>
  1169. using __esft_base_t = decltype(__enable_shared_from_this_base(
  1170. std::declval<const __shared_count<_Lp>&>(),
  1171. std::declval<_Yp*>()));
  1172. // Detect an accessible and unambiguous enable_shared_from_this base.
  1173. template<typename _Yp, typename = void>
  1174. struct __has_esft_base
  1175. : false_type { };
  1176. template<typename _Yp>
  1177. struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
  1178. : __not_<is_array<_Tp>> { }; // No enable shared_from_this for arrays
  1179. template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
  1180. typename enable_if<__has_esft_base<_Yp2>::value>::type
  1181. _M_enable_shared_from_this_with(_Yp* __p) noexcept
  1182. {
  1183. if (auto __base = __enable_shared_from_this_base(_M_refcount, __p))
  1184. __base->_M_weak_assign(const_cast<_Yp2*>(__p), _M_refcount);
  1185. }
  1186. template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
  1187. typename enable_if<!__has_esft_base<_Yp2>::value>::type
  1188. _M_enable_shared_from_this_with(_Yp*) noexcept
  1189. { }
  1190. void*
  1191. _M_get_deleter(const std::type_info& __ti) const noexcept
  1192. { return _M_refcount._M_get_deleter(__ti); }
  1193. template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
  1194. template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
  1195. template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
  1196. friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept;
  1197. template<typename _Del, typename _Tp1>
  1198. friend _Del* get_deleter(const shared_ptr<_Tp1>&) noexcept;
  1199. element_type* _M_ptr; // Contained pointer.
  1200. __shared_count<_Lp> _M_refcount; // Reference counter.
  1201. };
  1202. // 20.7.2.2.7 shared_ptr comparisons
  1203. template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
  1204. inline bool
  1205. operator==(const __shared_ptr<_Tp1, _Lp>& __a,
  1206. const __shared_ptr<_Tp2, _Lp>& __b) noexcept
  1207. { return __a.get() == __b.get(); }
  1208. template<typename _Tp, _Lock_policy _Lp>
  1209. inline bool
  1210. operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
  1211. { return !__a; }
  1212. #ifdef __cpp_lib_three_way_comparison
  1213. template<typename _Tp, typename _Up, _Lock_policy _Lp>
  1214. inline strong_ordering
  1215. operator<=>(const __shared_ptr<_Tp, _Lp>& __a,
  1216. const __shared_ptr<_Up, _Lp>& __b) noexcept
  1217. { return compare_three_way()(__a.get(), __b.get()); }
  1218. template<typename _Tp, _Lock_policy _Lp>
  1219. inline strong_ordering
  1220. operator<=>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
  1221. {
  1222. using pointer = typename __shared_ptr<_Tp, _Lp>::element_type*;
  1223. return compare_three_way()(__a.get(), static_cast<pointer>(nullptr));
  1224. }
  1225. #else
  1226. template<typename _Tp, _Lock_policy _Lp>
  1227. inline bool
  1228. operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
  1229. { return !__a; }
  1230. template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
  1231. inline bool
  1232. operator!=(const __shared_ptr<_Tp1, _Lp>& __a,
  1233. const __shared_ptr<_Tp2, _Lp>& __b) noexcept
  1234. { return __a.get() != __b.get(); }
  1235. template<typename _Tp, _Lock_policy _Lp>
  1236. inline bool
  1237. operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
  1238. { return (bool)__a; }
  1239. template<typename _Tp, _Lock_policy _Lp>
  1240. inline bool
  1241. operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
  1242. { return (bool)__a; }
  1243. template<typename _Tp, typename _Up, _Lock_policy _Lp>
  1244. inline bool
  1245. operator<(const __shared_ptr<_Tp, _Lp>& __a,
  1246. const __shared_ptr<_Up, _Lp>& __b) noexcept
  1247. {
  1248. using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
  1249. using _Up_elt = typename __shared_ptr<_Up, _Lp>::element_type;
  1250. using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type;
  1251. return less<_Vp>()(__a.get(), __b.get());
  1252. }
  1253. template<typename _Tp, _Lock_policy _Lp>
  1254. inline bool
  1255. operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
  1256. {
  1257. using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
  1258. return less<_Tp_elt*>()(__a.get(), nullptr);
  1259. }
  1260. template<typename _Tp, _Lock_policy _Lp>
  1261. inline bool
  1262. operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
  1263. {
  1264. using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
  1265. return less<_Tp_elt*>()(nullptr, __a.get());
  1266. }
  1267. template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
  1268. inline bool
  1269. operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
  1270. const __shared_ptr<_Tp2, _Lp>& __b) noexcept
  1271. { return !(__b < __a); }
  1272. template<typename _Tp, _Lock_policy _Lp>
  1273. inline bool
  1274. operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
  1275. { return !(nullptr < __a); }
  1276. template<typename _Tp, _Lock_policy _Lp>
  1277. inline bool
  1278. operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
  1279. { return !(__a < nullptr); }
  1280. template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
  1281. inline bool
  1282. operator>(const __shared_ptr<_Tp1, _Lp>& __a,
  1283. const __shared_ptr<_Tp2, _Lp>& __b) noexcept
  1284. { return (__b < __a); }
  1285. template<typename _Tp, _Lock_policy _Lp>
  1286. inline bool
  1287. operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
  1288. { return nullptr < __a; }
  1289. template<typename _Tp, _Lock_policy _Lp>
  1290. inline bool
  1291. operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
  1292. { return __a < nullptr; }
  1293. template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
  1294. inline bool
  1295. operator>=(const __shared_ptr<_Tp1, _Lp>& __a,
  1296. const __shared_ptr<_Tp2, _Lp>& __b) noexcept
  1297. { return !(__a < __b); }
  1298. template<typename _Tp, _Lock_policy _Lp>
  1299. inline bool
  1300. operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
  1301. { return !(__a < nullptr); }
  1302. template<typename _Tp, _Lock_policy _Lp>
  1303. inline bool
  1304. operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
  1305. { return !(nullptr < __a); }
  1306. #endif // three-way comparison
  1307. // 20.7.2.2.8 shared_ptr specialized algorithms.
  1308. template<typename _Tp, _Lock_policy _Lp>
  1309. inline void
  1310. swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
  1311. { __a.swap(__b); }
  1312. // 20.7.2.2.9 shared_ptr casts
  1313. // The seemingly equivalent code:
  1314. // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
  1315. // will eventually result in undefined behaviour, attempting to
  1316. // delete the same object twice.
  1317. /// static_pointer_cast
  1318. template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
  1319. inline __shared_ptr<_Tp, _Lp>
  1320. static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
  1321. {
  1322. using _Sp = __shared_ptr<_Tp, _Lp>;
  1323. return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
  1324. }
  1325. // The seemingly equivalent code:
  1326. // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
  1327. // will eventually result in undefined behaviour, attempting to
  1328. // delete the same object twice.
  1329. /// const_pointer_cast
  1330. template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
  1331. inline __shared_ptr<_Tp, _Lp>
  1332. const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
  1333. {
  1334. using _Sp = __shared_ptr<_Tp, _Lp>;
  1335. return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
  1336. }
  1337. // The seemingly equivalent code:
  1338. // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
  1339. // will eventually result in undefined behaviour, attempting to
  1340. // delete the same object twice.
  1341. /// dynamic_pointer_cast
  1342. template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
  1343. inline __shared_ptr<_Tp, _Lp>
  1344. dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
  1345. {
  1346. using _Sp = __shared_ptr<_Tp, _Lp>;
  1347. if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
  1348. return _Sp(__r, __p);
  1349. return _Sp();
  1350. }
  1351. #if __cplusplus > 201402L
  1352. template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
  1353. inline __shared_ptr<_Tp, _Lp>
  1354. reinterpret_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
  1355. {
  1356. using _Sp = __shared_ptr<_Tp, _Lp>;
  1357. return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
  1358. }
  1359. #endif
  1360. template<typename _Tp, _Lock_policy _Lp>
  1361. class __weak_ptr
  1362. {
  1363. template<typename _Yp, typename _Res = void>
  1364. using _Compatible = typename
  1365. enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
  1366. // Constraint for assignment from shared_ptr and weak_ptr:
  1367. template<typename _Yp>
  1368. using _Assignable = _Compatible<_Yp, __weak_ptr&>;
  1369. public:
  1370. using element_type = typename remove_extent<_Tp>::type;
  1371. constexpr __weak_ptr() noexcept
  1372. : _M_ptr(nullptr), _M_refcount()
  1373. { }
  1374. __weak_ptr(const __weak_ptr&) noexcept = default;
  1375. ~__weak_ptr() = default;
  1376. // The "obvious" converting constructor implementation:
  1377. //
  1378. // template<typename _Tp1>
  1379. // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
  1380. // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
  1381. // { }
  1382. //
  1383. // has a serious problem.
  1384. //
  1385. // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
  1386. // conversion may require access to *__r._M_ptr (virtual inheritance).
  1387. //
  1388. // It is not possible to avoid spurious access violations since
  1389. // in multithreaded programs __r._M_ptr may be invalidated at any point.
  1390. template<typename _Yp, typename = _Compatible<_Yp>>
  1391. __weak_ptr(const __weak_ptr<_Yp, _Lp>& __r) noexcept
  1392. : _M_refcount(__r._M_refcount)
  1393. { _M_ptr = __r.lock().get(); }
  1394. template<typename _Yp, typename = _Compatible<_Yp>>
  1395. __weak_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
  1396. : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
  1397. { }
  1398. __weak_ptr(__weak_ptr&& __r) noexcept
  1399. : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount))
  1400. { __r._M_ptr = nullptr; }
  1401. template<typename _Yp, typename = _Compatible<_Yp>>
  1402. __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
  1403. : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount))
  1404. { __r._M_ptr = nullptr; }
  1405. __weak_ptr&
  1406. operator=(const __weak_ptr& __r) noexcept = default;
  1407. template<typename _Yp>
  1408. _Assignable<_Yp>
  1409. operator=(const __weak_ptr<_Yp, _Lp>& __r) noexcept
  1410. {
  1411. _M_ptr = __r.lock().get();
  1412. _M_refcount = __r._M_refcount;
  1413. return *this;
  1414. }
  1415. template<typename _Yp>
  1416. _Assignable<_Yp>
  1417. operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
  1418. {
  1419. _M_ptr = __r._M_ptr;
  1420. _M_refcount = __r._M_refcount;
  1421. return *this;
  1422. }
  1423. __weak_ptr&
  1424. operator=(__weak_ptr&& __r) noexcept
  1425. {
  1426. _M_ptr = __r._M_ptr;
  1427. _M_refcount = std::move(__r._M_refcount);
  1428. __r._M_ptr = nullptr;
  1429. return *this;
  1430. }
  1431. template<typename _Yp>
  1432. _Assignable<_Yp>
  1433. operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept
  1434. {
  1435. _M_ptr = __r.lock().get();
  1436. _M_refcount = std::move(__r._M_refcount);
  1437. __r._M_ptr = nullptr;
  1438. return *this;
  1439. }
  1440. __shared_ptr<_Tp, _Lp>
  1441. lock() const noexcept
  1442. { return __shared_ptr<element_type, _Lp>(*this, std::nothrow); }
  1443. long
  1444. use_count() const noexcept
  1445. { return _M_refcount._M_get_use_count(); }
  1446. bool
  1447. expired() const noexcept
  1448. { return _M_refcount._M_get_use_count() == 0; }
  1449. template<typename _Tp1>
  1450. bool
  1451. owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const noexcept
  1452. { return _M_refcount._M_less(__rhs._M_refcount); }
  1453. template<typename _Tp1>
  1454. bool
  1455. owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const noexcept
  1456. { return _M_refcount._M_less(__rhs._M_refcount); }
  1457. void
  1458. reset() noexcept
  1459. { __weak_ptr().swap(*this); }
  1460. void
  1461. swap(__weak_ptr& __s) noexcept
  1462. {
  1463. std::swap(_M_ptr, __s._M_ptr);
  1464. _M_refcount._M_swap(__s._M_refcount);
  1465. }
  1466. private:
  1467. // Used by __enable_shared_from_this.
  1468. void
  1469. _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept
  1470. {
  1471. if (use_count() == 0)
  1472. {
  1473. _M_ptr = __ptr;
  1474. _M_refcount = __refcount;
  1475. }
  1476. }
  1477. template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
  1478. template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
  1479. friend class __enable_shared_from_this<_Tp, _Lp>;
  1480. friend class enable_shared_from_this<_Tp>;
  1481. element_type* _M_ptr; // Contained pointer.
  1482. __weak_count<_Lp> _M_refcount; // Reference counter.
  1483. };
  1484. // 20.7.2.3.6 weak_ptr specialized algorithms.
  1485. template<typename _Tp, _Lock_policy _Lp>
  1486. inline void
  1487. swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
  1488. { __a.swap(__b); }
  1489. template<typename _Tp, typename _Tp1>
  1490. struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
  1491. {
  1492. bool
  1493. operator()(const _Tp& __lhs, const _Tp& __rhs) const noexcept
  1494. { return __lhs.owner_before(__rhs); }
  1495. bool
  1496. operator()(const _Tp& __lhs, const _Tp1& __rhs) const noexcept
  1497. { return __lhs.owner_before(__rhs); }
  1498. bool
  1499. operator()(const _Tp1& __lhs, const _Tp& __rhs) const noexcept
  1500. { return __lhs.owner_before(__rhs); }
  1501. };
  1502. template<>
  1503. struct _Sp_owner_less<void, void>
  1504. {
  1505. template<typename _Tp, typename _Up>
  1506. auto
  1507. operator()(const _Tp& __lhs, const _Up& __rhs) const noexcept
  1508. -> decltype(__lhs.owner_before(__rhs))
  1509. { return __lhs.owner_before(__rhs); }
  1510. using is_transparent = void;
  1511. };
  1512. template<typename _Tp, _Lock_policy _Lp>
  1513. struct owner_less<__shared_ptr<_Tp, _Lp>>
  1514. : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
  1515. { };
  1516. template<typename _Tp, _Lock_policy _Lp>
  1517. struct owner_less<__weak_ptr<_Tp, _Lp>>
  1518. : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
  1519. { };
  1520. template<typename _Tp, _Lock_policy _Lp>
  1521. class __enable_shared_from_this
  1522. {
  1523. protected:
  1524. constexpr __enable_shared_from_this() noexcept { }
  1525. __enable_shared_from_this(const __enable_shared_from_this&) noexcept { }
  1526. __enable_shared_from_this&
  1527. operator=(const __enable_shared_from_this&) noexcept
  1528. { return *this; }
  1529. ~__enable_shared_from_this() { }
  1530. public:
  1531. __shared_ptr<_Tp, _Lp>
  1532. shared_from_this()
  1533. { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
  1534. __shared_ptr<const _Tp, _Lp>
  1535. shared_from_this() const
  1536. { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
  1537. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  1538. __weak_ptr<_Tp, _Lp>
  1539. weak_from_this() noexcept
  1540. { return this->_M_weak_this; }
  1541. __weak_ptr<const _Tp, _Lp>
  1542. weak_from_this() const noexcept
  1543. { return this->_M_weak_this; }
  1544. #endif
  1545. private:
  1546. template<typename _Tp1>
  1547. void
  1548. _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept
  1549. { _M_weak_this._M_assign(__p, __n); }
  1550. friend const __enable_shared_from_this*
  1551. __enable_shared_from_this_base(const __shared_count<_Lp>&,
  1552. const __enable_shared_from_this* __p)
  1553. { return __p; }
  1554. template<typename, _Lock_policy>
  1555. friend class __shared_ptr;
  1556. mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
  1557. };
  1558. template<typename _Tp, _Lock_policy _Lp = __default_lock_policy,
  1559. typename _Alloc, typename... _Args>
  1560. inline __shared_ptr<_Tp, _Lp>
  1561. __allocate_shared(const _Alloc& __a, _Args&&... __args)
  1562. {
  1563. return __shared_ptr<_Tp, _Lp>(_Sp_alloc_shared_tag<_Alloc>{__a},
  1564. std::forward<_Args>(__args)...);
  1565. }
  1566. template<typename _Tp, _Lock_policy _Lp = __default_lock_policy,
  1567. typename... _Args>
  1568. inline __shared_ptr<_Tp, _Lp>
  1569. __make_shared(_Args&&... __args)
  1570. {
  1571. typedef typename std::remove_const<_Tp>::type _Tp_nc;
  1572. return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
  1573. std::forward<_Args>(__args)...);
  1574. }
  1575. /// std::hash specialization for __shared_ptr.
  1576. template<typename _Tp, _Lock_policy _Lp>
  1577. struct hash<__shared_ptr<_Tp, _Lp>>
  1578. : public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
  1579. {
  1580. size_t
  1581. operator()(const __shared_ptr<_Tp, _Lp>& __s) const noexcept
  1582. {
  1583. return hash<typename __shared_ptr<_Tp, _Lp>::element_type*>()(
  1584. __s.get());
  1585. }
  1586. };
  1587. _GLIBCXX_END_NAMESPACE_VERSION
  1588. } // namespace
  1589. #endif // _SHARED_PTR_BASE_H