shared_ptr_base.h 53 KB

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