shared_ptr_base.h 53 KB

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