shared_ptr_base.h 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  1. // shared_ptr and weak_ptr implementation details -*- C++ -*-
  2. // Copyright (C) 2007-2019 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. static bool _S_eq(const type_info&) noexcept;
  426. };
  427. template<typename _Alloc>
  428. struct _Sp_alloc_shared_tag
  429. {
  430. const _Alloc& _M_a;
  431. };
  432. template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
  433. class _Sp_counted_ptr_inplace final : public _Sp_counted_base<_Lp>
  434. {
  435. class _Impl : _Sp_ebo_helper<0, _Alloc>
  436. {
  437. typedef _Sp_ebo_helper<0, _Alloc> _A_base;
  438. public:
  439. explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
  440. _Alloc& _M_alloc() noexcept { return _A_base::_S_get(*this); }
  441. __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
  442. };
  443. public:
  444. using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
  445. // Alloc parameter is not a reference so doesn't alias anything in __args
  446. template<typename... _Args>
  447. _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
  448. : _M_impl(__a)
  449. {
  450. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  451. // 2070. allocate_shared should use allocator_traits<A>::construct
  452. allocator_traits<_Alloc>::construct(__a, _M_ptr(),
  453. std::forward<_Args>(__args)...); // might throw
  454. }
  455. ~_Sp_counted_ptr_inplace() noexcept { }
  456. virtual void
  457. _M_dispose() noexcept
  458. {
  459. allocator_traits<_Alloc>::destroy(_M_impl._M_alloc(), _M_ptr());
  460. }
  461. // Override because the allocator needs to know the dynamic type
  462. virtual void
  463. _M_destroy() noexcept
  464. {
  465. __allocator_type __a(_M_impl._M_alloc());
  466. __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
  467. this->~_Sp_counted_ptr_inplace();
  468. }
  469. private:
  470. friend class __shared_count<_Lp>; // To be able to call _M_ptr().
  471. // No longer used, but code compiled against old libstdc++ headers
  472. // might still call it from __shared_ptr ctor to get the pointer out.
  473. virtual void*
  474. _M_get_deleter(const std::type_info& __ti) noexcept override
  475. {
  476. auto __ptr = const_cast<typename remove_cv<_Tp>::type*>(_M_ptr());
  477. // Check for the fake type_info first, so we don't try to access it
  478. // as a real type_info object. Otherwise, check if it's the real
  479. // type_info for this class. With RTTI enabled we can check directly,
  480. // or call a library function to do it.
  481. if (&__ti == &_Sp_make_shared_tag::_S_ti()
  482. ||
  483. #if __cpp_rtti
  484. __ti == typeid(_Sp_make_shared_tag)
  485. #else
  486. _Sp_make_shared_tag::_S_eq(__ti)
  487. #endif
  488. )
  489. return __ptr;
  490. return nullptr;
  491. }
  492. _Tp* _M_ptr() noexcept { return _M_impl._M_storage._M_ptr(); }
  493. _Impl _M_impl;
  494. };
  495. // The default deleter for shared_ptr<T[]> and shared_ptr<T[N]>.
  496. struct __sp_array_delete
  497. {
  498. template<typename _Yp>
  499. void operator()(_Yp* __p) const { delete[] __p; }
  500. };
  501. template<_Lock_policy _Lp>
  502. class __shared_count
  503. {
  504. template<typename _Tp>
  505. struct __not_alloc_shared_tag { using type = void; };
  506. template<typename _Tp>
  507. struct __not_alloc_shared_tag<_Sp_alloc_shared_tag<_Tp>> { };
  508. public:
  509. constexpr __shared_count() noexcept : _M_pi(0)
  510. { }
  511. template<typename _Ptr>
  512. explicit
  513. __shared_count(_Ptr __p) : _M_pi(0)
  514. {
  515. __try
  516. {
  517. _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
  518. }
  519. __catch(...)
  520. {
  521. delete __p;
  522. __throw_exception_again;
  523. }
  524. }
  525. template<typename _Ptr>
  526. __shared_count(_Ptr __p, /* is_array = */ false_type)
  527. : __shared_count(__p)
  528. { }
  529. template<typename _Ptr>
  530. __shared_count(_Ptr __p, /* is_array = */ true_type)
  531. : __shared_count(__p, __sp_array_delete{}, allocator<void>())
  532. { }
  533. template<typename _Ptr, typename _Deleter,
  534. typename = typename __not_alloc_shared_tag<_Deleter>::type>
  535. __shared_count(_Ptr __p, _Deleter __d)
  536. : __shared_count(__p, std::move(__d), allocator<void>())
  537. { }
  538. template<typename _Ptr, typename _Deleter, typename _Alloc,
  539. typename = typename __not_alloc_shared_tag<_Deleter>::type>
  540. __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
  541. {
  542. typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
  543. __try
  544. {
  545. typename _Sp_cd_type::__allocator_type __a2(__a);
  546. auto __guard = std::__allocate_guarded(__a2);
  547. _Sp_cd_type* __mem = __guard.get();
  548. ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a));
  549. _M_pi = __mem;
  550. __guard = nullptr;
  551. }
  552. __catch(...)
  553. {
  554. __d(__p); // Call _Deleter on __p.
  555. __throw_exception_again;
  556. }
  557. }
  558. template<typename _Tp, typename _Alloc, typename... _Args>
  559. __shared_count(_Tp*& __p, _Sp_alloc_shared_tag<_Alloc> __a,
  560. _Args&&... __args)
  561. {
  562. typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
  563. typename _Sp_cp_type::__allocator_type __a2(__a._M_a);
  564. auto __guard = std::__allocate_guarded(__a2);
  565. _Sp_cp_type* __mem = __guard.get();
  566. auto __pi = ::new (__mem)
  567. _Sp_cp_type(__a._M_a, std::forward<_Args>(__args)...);
  568. __guard = nullptr;
  569. _M_pi = __pi;
  570. __p = __pi->_M_ptr();
  571. }
  572. #if _GLIBCXX_USE_DEPRECATED
  573. #pragma GCC diagnostic push
  574. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  575. // Special case for auto_ptr<_Tp> to provide the strong guarantee.
  576. template<typename _Tp>
  577. explicit
  578. __shared_count(std::auto_ptr<_Tp>&& __r);
  579. #pragma GCC diagnostic pop
  580. #endif
  581. // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
  582. template<typename _Tp, typename _Del>
  583. explicit
  584. __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0)
  585. {
  586. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  587. // 2415. Inconsistency between unique_ptr and shared_ptr
  588. if (__r.get() == nullptr)
  589. return;
  590. using _Ptr = typename unique_ptr<_Tp, _Del>::pointer;
  591. using _Del2 = typename conditional<is_reference<_Del>::value,
  592. reference_wrapper<typename remove_reference<_Del>::type>,
  593. _Del>::type;
  594. using _Sp_cd_type
  595. = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
  596. using _Alloc = allocator<_Sp_cd_type>;
  597. using _Alloc_traits = allocator_traits<_Alloc>;
  598. _Alloc __a;
  599. _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
  600. _Alloc_traits::construct(__a, __mem, __r.release(),
  601. __r.get_deleter()); // non-throwing
  602. _M_pi = __mem;
  603. }
  604. // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
  605. explicit __shared_count(const __weak_count<_Lp>& __r);
  606. // Does not throw if __r._M_get_use_count() == 0, caller must check.
  607. explicit __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t);
  608. ~__shared_count() noexcept
  609. {
  610. if (_M_pi != nullptr)
  611. _M_pi->_M_release();
  612. }
  613. __shared_count(const __shared_count& __r) noexcept
  614. : _M_pi(__r._M_pi)
  615. {
  616. if (_M_pi != 0)
  617. _M_pi->_M_add_ref_copy();
  618. }
  619. __shared_count&
  620. operator=(const __shared_count& __r) noexcept
  621. {
  622. _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
  623. if (__tmp != _M_pi)
  624. {
  625. if (__tmp != 0)
  626. __tmp->_M_add_ref_copy();
  627. if (_M_pi != 0)
  628. _M_pi->_M_release();
  629. _M_pi = __tmp;
  630. }
  631. return *this;
  632. }
  633. void
  634. _M_swap(__shared_count& __r) noexcept
  635. {
  636. _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
  637. __r._M_pi = _M_pi;
  638. _M_pi = __tmp;
  639. }
  640. long
  641. _M_get_use_count() const noexcept
  642. { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
  643. bool
  644. _M_unique() const noexcept
  645. { return this->_M_get_use_count() == 1; }
  646. void*
  647. _M_get_deleter(const std::type_info& __ti) const noexcept
  648. { return _M_pi ? _M_pi->_M_get_deleter(__ti) : nullptr; }
  649. bool
  650. _M_less(const __shared_count& __rhs) const noexcept
  651. { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
  652. bool
  653. _M_less(const __weak_count<_Lp>& __rhs) const noexcept
  654. { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
  655. // Friend function injected into enclosing namespace and found by ADL
  656. friend inline bool
  657. operator==(const __shared_count& __a, const __shared_count& __b) noexcept
  658. { return __a._M_pi == __b._M_pi; }
  659. private:
  660. friend class __weak_count<_Lp>;
  661. _Sp_counted_base<_Lp>* _M_pi;
  662. };
  663. template<_Lock_policy _Lp>
  664. class __weak_count
  665. {
  666. public:
  667. constexpr __weak_count() noexcept : _M_pi(nullptr)
  668. { }
  669. __weak_count(const __shared_count<_Lp>& __r) noexcept
  670. : _M_pi(__r._M_pi)
  671. {
  672. if (_M_pi != nullptr)
  673. _M_pi->_M_weak_add_ref();
  674. }
  675. __weak_count(const __weak_count& __r) noexcept
  676. : _M_pi(__r._M_pi)
  677. {
  678. if (_M_pi != nullptr)
  679. _M_pi->_M_weak_add_ref();
  680. }
  681. __weak_count(__weak_count&& __r) noexcept
  682. : _M_pi(__r._M_pi)
  683. { __r._M_pi = nullptr; }
  684. ~__weak_count() noexcept
  685. {
  686. if (_M_pi != nullptr)
  687. _M_pi->_M_weak_release();
  688. }
  689. __weak_count&
  690. operator=(const __shared_count<_Lp>& __r) noexcept
  691. {
  692. _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
  693. if (__tmp != nullptr)
  694. __tmp->_M_weak_add_ref();
  695. if (_M_pi != nullptr)
  696. _M_pi->_M_weak_release();
  697. _M_pi = __tmp;
  698. return *this;
  699. }
  700. __weak_count&
  701. operator=(const __weak_count& __r) noexcept
  702. {
  703. _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
  704. if (__tmp != nullptr)
  705. __tmp->_M_weak_add_ref();
  706. if (_M_pi != nullptr)
  707. _M_pi->_M_weak_release();
  708. _M_pi = __tmp;
  709. return *this;
  710. }
  711. __weak_count&
  712. operator=(__weak_count&& __r) noexcept
  713. {
  714. if (_M_pi != nullptr)
  715. _M_pi->_M_weak_release();
  716. _M_pi = __r._M_pi;
  717. __r._M_pi = nullptr;
  718. return *this;
  719. }
  720. void
  721. _M_swap(__weak_count& __r) noexcept
  722. {
  723. _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
  724. __r._M_pi = _M_pi;
  725. _M_pi = __tmp;
  726. }
  727. long
  728. _M_get_use_count() const noexcept
  729. { return _M_pi != nullptr ? _M_pi->_M_get_use_count() : 0; }
  730. bool
  731. _M_less(const __weak_count& __rhs) const noexcept
  732. { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
  733. bool
  734. _M_less(const __shared_count<_Lp>& __rhs) const noexcept
  735. { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
  736. // Friend function injected into enclosing namespace and found by ADL
  737. friend inline bool
  738. operator==(const __weak_count& __a, const __weak_count& __b) noexcept
  739. { return __a._M_pi == __b._M_pi; }
  740. private:
  741. friend class __shared_count<_Lp>;
  742. _Sp_counted_base<_Lp>* _M_pi;
  743. };
  744. // Now that __weak_count is defined we can define this constructor:
  745. template<_Lock_policy _Lp>
  746. inline
  747. __shared_count<_Lp>::__shared_count(const __weak_count<_Lp>& __r)
  748. : _M_pi(__r._M_pi)
  749. {
  750. if (_M_pi != nullptr)
  751. _M_pi->_M_add_ref_lock();
  752. else
  753. __throw_bad_weak_ptr();
  754. }
  755. // Now that __weak_count is defined we can define this constructor:
  756. template<_Lock_policy _Lp>
  757. inline
  758. __shared_count<_Lp>::
  759. __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t)
  760. : _M_pi(__r._M_pi)
  761. {
  762. if (_M_pi != nullptr)
  763. if (!_M_pi->_M_add_ref_lock_nothrow())
  764. _M_pi = nullptr;
  765. }
  766. #define __cpp_lib_shared_ptr_arrays 201611L
  767. // Helper traits for shared_ptr of array:
  768. // A pointer type Y* is said to be compatible with a pointer type T* when
  769. // either Y* is convertible to T* or Y is U[N] and T is U cv [].
  770. template<typename _Yp_ptr, typename _Tp_ptr>
  771. struct __sp_compatible_with
  772. : false_type
  773. { };
  774. template<typename _Yp, typename _Tp>
  775. struct __sp_compatible_with<_Yp*, _Tp*>
  776. : is_convertible<_Yp*, _Tp*>::type
  777. { };
  778. template<typename _Up, size_t _Nm>
  779. struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
  780. : true_type
  781. { };
  782. template<typename _Up, size_t _Nm>
  783. struct __sp_compatible_with<_Up(*)[_Nm], const _Up(*)[]>
  784. : true_type
  785. { };
  786. template<typename _Up, size_t _Nm>
  787. struct __sp_compatible_with<_Up(*)[_Nm], volatile _Up(*)[]>
  788. : true_type
  789. { };
  790. template<typename _Up, size_t _Nm>
  791. struct __sp_compatible_with<_Up(*)[_Nm], const volatile _Up(*)[]>
  792. : true_type
  793. { };
  794. // Test conversion from Y(*)[N] to U(*)[N] without forming invalid type Y[N].
  795. template<typename _Up, size_t _Nm, typename _Yp, typename = void>
  796. struct __sp_is_constructible_arrN
  797. : false_type
  798. { };
  799. template<typename _Up, size_t _Nm, typename _Yp>
  800. struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
  801. : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
  802. { };
  803. // Test conversion from Y(*)[] to U(*)[] without forming invalid type Y[].
  804. template<typename _Up, typename _Yp, typename = void>
  805. struct __sp_is_constructible_arr
  806. : false_type
  807. { };
  808. template<typename _Up, typename _Yp>
  809. struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
  810. : is_convertible<_Yp(*)[], _Up(*)[]>::type
  811. { };
  812. // Trait to check if shared_ptr<T> can be constructed from Y*.
  813. template<typename _Tp, typename _Yp>
  814. struct __sp_is_constructible;
  815. // When T is U[N], Y(*)[N] shall be convertible to T*;
  816. template<typename _Up, size_t _Nm, typename _Yp>
  817. struct __sp_is_constructible<_Up[_Nm], _Yp>
  818. : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
  819. { };
  820. // when T is U[], Y(*)[] shall be convertible to T*;
  821. template<typename _Up, typename _Yp>
  822. struct __sp_is_constructible<_Up[], _Yp>
  823. : __sp_is_constructible_arr<_Up, _Yp>::type
  824. { };
  825. // otherwise, Y* shall be convertible to T*.
  826. template<typename _Tp, typename _Yp>
  827. struct __sp_is_constructible
  828. : is_convertible<_Yp*, _Tp*>::type
  829. { };
  830. // Define operator* and operator-> for shared_ptr<T>.
  831. template<typename _Tp, _Lock_policy _Lp,
  832. bool = is_array<_Tp>::value, bool = is_void<_Tp>::value>
  833. class __shared_ptr_access
  834. {
  835. public:
  836. using element_type = _Tp;
  837. element_type&
  838. operator*() const noexcept
  839. {
  840. __glibcxx_assert(_M_get() != nullptr);
  841. return *_M_get();
  842. }
  843. element_type*
  844. operator->() const noexcept
  845. {
  846. _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
  847. return _M_get();
  848. }
  849. private:
  850. element_type*
  851. _M_get() const noexcept
  852. { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
  853. };
  854. // Define operator-> for shared_ptr<cv void>.
  855. template<typename _Tp, _Lock_policy _Lp>
  856. class __shared_ptr_access<_Tp, _Lp, false, true>
  857. {
  858. public:
  859. using element_type = _Tp;
  860. element_type*
  861. operator->() const noexcept
  862. {
  863. auto __ptr = static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get();
  864. _GLIBCXX_DEBUG_PEDASSERT(__ptr != nullptr);
  865. return __ptr;
  866. }
  867. };
  868. // Define operator[] for shared_ptr<T[]> and shared_ptr<T[N]>.
  869. template<typename _Tp, _Lock_policy _Lp>
  870. class __shared_ptr_access<_Tp, _Lp, true, false>
  871. {
  872. public:
  873. using element_type = typename remove_extent<_Tp>::type;
  874. #if __cplusplus <= 201402L
  875. [[__deprecated__("shared_ptr<T[]>::operator* is absent from C++17")]]
  876. element_type&
  877. operator*() const noexcept
  878. {
  879. __glibcxx_assert(_M_get() != nullptr);
  880. return *_M_get();
  881. }
  882. [[__deprecated__("shared_ptr<T[]>::operator-> is absent from C++17")]]
  883. element_type*
  884. operator->() const noexcept
  885. {
  886. _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
  887. return _M_get();
  888. }
  889. #endif
  890. element_type&
  891. operator[](ptrdiff_t __i) const
  892. {
  893. __glibcxx_assert(_M_get() != nullptr);
  894. __glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value);
  895. return _M_get()[__i];
  896. }
  897. private:
  898. element_type*
  899. _M_get() const noexcept
  900. { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
  901. };
  902. template<typename _Tp, _Lock_policy _Lp>
  903. class __shared_ptr
  904. : public __shared_ptr_access<_Tp, _Lp>
  905. {
  906. public:
  907. using element_type = typename remove_extent<_Tp>::type;
  908. private:
  909. // Constraint for taking ownership of a pointer of type _Yp*:
  910. template<typename _Yp>
  911. using _SafeConv
  912. = typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
  913. // Constraint for construction from shared_ptr and weak_ptr:
  914. template<typename _Yp, typename _Res = void>
  915. using _Compatible = typename
  916. enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
  917. // Constraint for assignment from shared_ptr and weak_ptr:
  918. template<typename _Yp>
  919. using _Assignable = _Compatible<_Yp, __shared_ptr&>;
  920. // Constraint for construction from unique_ptr:
  921. template<typename _Yp, typename _Del, typename _Res = void,
  922. typename _Ptr = typename unique_ptr<_Yp, _Del>::pointer>
  923. using _UniqCompatible = typename enable_if<__and_<
  924. __sp_compatible_with<_Yp*, _Tp*>, is_convertible<_Ptr, element_type*>
  925. >::value, _Res>::type;
  926. // Constraint for assignment from unique_ptr:
  927. template<typename _Yp, typename _Del>
  928. using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
  929. public:
  930. #if __cplusplus > 201402L
  931. using weak_type = __weak_ptr<_Tp, _Lp>;
  932. #endif
  933. constexpr __shared_ptr() noexcept
  934. : _M_ptr(0), _M_refcount()
  935. { }
  936. template<typename _Yp, typename = _SafeConv<_Yp>>
  937. explicit
  938. __shared_ptr(_Yp* __p)
  939. : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
  940. {
  941. static_assert( !is_void<_Yp>::value, "incomplete type" );
  942. static_assert( sizeof(_Yp) > 0, "incomplete type" );
  943. _M_enable_shared_from_this_with(__p);
  944. }
  945. template<typename _Yp, typename _Deleter, typename = _SafeConv<_Yp>>
  946. __shared_ptr(_Yp* __p, _Deleter __d)
  947. : _M_ptr(__p), _M_refcount(__p, std::move(__d))
  948. {
  949. static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
  950. "deleter expression d(p) is well-formed");
  951. _M_enable_shared_from_this_with(__p);
  952. }
  953. template<typename _Yp, typename _Deleter, typename _Alloc,
  954. typename = _SafeConv<_Yp>>
  955. __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
  956. : _M_ptr(__p), _M_refcount(__p, std::move(__d), std::move(__a))
  957. {
  958. static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
  959. "deleter expression d(p) is well-formed");
  960. _M_enable_shared_from_this_with(__p);
  961. }
  962. template<typename _Deleter>
  963. __shared_ptr(nullptr_t __p, _Deleter __d)
  964. : _M_ptr(0), _M_refcount(__p, std::move(__d))
  965. { }
  966. template<typename _Deleter, typename _Alloc>
  967. __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
  968. : _M_ptr(0), _M_refcount(__p, std::move(__d), std::move(__a))
  969. { }
  970. template<typename _Yp>
  971. __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r,
  972. element_type* __p) noexcept
  973. : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
  974. { }
  975. __shared_ptr(const __shared_ptr&) noexcept = default;
  976. __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
  977. ~__shared_ptr() = default;
  978. template<typename _Yp, typename = _Compatible<_Yp>>
  979. __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
  980. : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
  981. { }
  982. __shared_ptr(__shared_ptr&& __r) noexcept
  983. : _M_ptr(__r._M_ptr), _M_refcount()
  984. {
  985. _M_refcount._M_swap(__r._M_refcount);
  986. __r._M_ptr = 0;
  987. }
  988. template<typename _Yp, typename = _Compatible<_Yp>>
  989. __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
  990. : _M_ptr(__r._M_ptr), _M_refcount()
  991. {
  992. _M_refcount._M_swap(__r._M_refcount);
  993. __r._M_ptr = 0;
  994. }
  995. template<typename _Yp, typename = _Compatible<_Yp>>
  996. explicit __shared_ptr(const __weak_ptr<_Yp, _Lp>& __r)
  997. : _M_refcount(__r._M_refcount) // may throw
  998. {
  999. // It is now safe to copy __r._M_ptr, as
  1000. // _M_refcount(__r._M_refcount) did not throw.
  1001. _M_ptr = __r._M_ptr;
  1002. }
  1003. // If an exception is thrown this constructor has no effect.
  1004. template<typename _Yp, typename _Del,
  1005. typename = _UniqCompatible<_Yp, _Del>>
  1006. __shared_ptr(unique_ptr<_Yp, _Del>&& __r)
  1007. : _M_ptr(__r.get()), _M_refcount()
  1008. {
  1009. auto __raw = __to_address(__r.get());
  1010. _M_refcount = __shared_count<_Lp>(std::move(__r));
  1011. _M_enable_shared_from_this_with(__raw);
  1012. }
  1013. #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
  1014. protected:
  1015. // If an exception is thrown this constructor has no effect.
  1016. template<typename _Tp1, typename _Del,
  1017. typename enable_if<__and_<
  1018. __not_<is_array<_Tp>>, is_array<_Tp1>,
  1019. is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
  1020. >::value, bool>::type = true>
  1021. __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
  1022. : _M_ptr(__r.get()), _M_refcount()
  1023. {
  1024. auto __raw = __to_address(__r.get());
  1025. _M_refcount = __shared_count<_Lp>(std::move(__r));
  1026. _M_enable_shared_from_this_with(__raw);
  1027. }
  1028. public:
  1029. #endif
  1030. #if _GLIBCXX_USE_DEPRECATED
  1031. #pragma GCC diagnostic push
  1032. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  1033. // Postcondition: use_count() == 1 and __r.get() == 0
  1034. template<typename _Yp, typename = _Compatible<_Yp>>
  1035. __shared_ptr(auto_ptr<_Yp>&& __r);
  1036. #pragma GCC diagnostic pop
  1037. #endif
  1038. constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
  1039. template<typename _Yp>
  1040. _Assignable<_Yp>
  1041. operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
  1042. {
  1043. _M_ptr = __r._M_ptr;
  1044. _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
  1045. return *this;
  1046. }
  1047. #if _GLIBCXX_USE_DEPRECATED
  1048. #pragma GCC diagnostic push
  1049. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  1050. template<typename _Yp>
  1051. _Assignable<_Yp>
  1052. operator=(auto_ptr<_Yp>&& __r)
  1053. {
  1054. __shared_ptr(std::move(__r)).swap(*this);
  1055. return *this;
  1056. }
  1057. #pragma GCC diagnostic pop
  1058. #endif
  1059. __shared_ptr&
  1060. operator=(__shared_ptr&& __r) noexcept
  1061. {
  1062. __shared_ptr(std::move(__r)).swap(*this);
  1063. return *this;
  1064. }
  1065. template<class _Yp>
  1066. _Assignable<_Yp>
  1067. operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept
  1068. {
  1069. __shared_ptr(std::move(__r)).swap(*this);
  1070. return *this;
  1071. }
  1072. template<typename _Yp, typename _Del>
  1073. _UniqAssignable<_Yp, _Del>
  1074. operator=(unique_ptr<_Yp, _Del>&& __r)
  1075. {
  1076. __shared_ptr(std::move(__r)).swap(*this);
  1077. return *this;
  1078. }
  1079. void
  1080. reset() noexcept
  1081. { __shared_ptr().swap(*this); }
  1082. template<typename _Yp>
  1083. _SafeConv<_Yp>
  1084. reset(_Yp* __p) // _Yp must be complete.
  1085. {
  1086. // Catch self-reset errors.
  1087. __glibcxx_assert(__p == 0 || __p != _M_ptr);
  1088. __shared_ptr(__p).swap(*this);
  1089. }
  1090. template<typename _Yp, typename _Deleter>
  1091. _SafeConv<_Yp>
  1092. reset(_Yp* __p, _Deleter __d)
  1093. { __shared_ptr(__p, std::move(__d)).swap(*this); }
  1094. template<typename _Yp, typename _Deleter, typename _Alloc>
  1095. _SafeConv<_Yp>
  1096. reset(_Yp* __p, _Deleter __d, _Alloc __a)
  1097. { __shared_ptr(__p, std::move(__d), std::move(__a)).swap(*this); }
  1098. element_type*
  1099. get() const noexcept
  1100. { return _M_ptr; }
  1101. explicit operator bool() const // never throws
  1102. { return _M_ptr == 0 ? false : true; }
  1103. bool
  1104. unique() const noexcept
  1105. { return _M_refcount._M_unique(); }
  1106. long
  1107. use_count() const noexcept
  1108. { return _M_refcount._M_get_use_count(); }
  1109. void
  1110. swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
  1111. {
  1112. std::swap(_M_ptr, __other._M_ptr);
  1113. _M_refcount._M_swap(__other._M_refcount);
  1114. }
  1115. template<typename _Tp1>
  1116. bool
  1117. owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const noexcept
  1118. { return _M_refcount._M_less(__rhs._M_refcount); }
  1119. template<typename _Tp1>
  1120. bool
  1121. owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const noexcept
  1122. { return _M_refcount._M_less(__rhs._M_refcount); }
  1123. protected:
  1124. // This constructor is non-standard, it is used by allocate_shared.
  1125. template<typename _Alloc, typename... _Args>
  1126. __shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
  1127. : _M_ptr(), _M_refcount(_M_ptr, __tag, std::forward<_Args>(__args)...)
  1128. { _M_enable_shared_from_this_with(_M_ptr); }
  1129. template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
  1130. typename... _Args>
  1131. friend __shared_ptr<_Tp1, _Lp1>
  1132. __allocate_shared(const _Alloc& __a, _Args&&... __args);
  1133. // This constructor is used by __weak_ptr::lock() and
  1134. // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t).
  1135. __shared_ptr(const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t)
  1136. : _M_refcount(__r._M_refcount, std::nothrow)
  1137. {
  1138. _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr : nullptr;
  1139. }
  1140. friend class __weak_ptr<_Tp, _Lp>;
  1141. private:
  1142. template<typename _Yp>
  1143. using __esft_base_t = decltype(__enable_shared_from_this_base(
  1144. std::declval<const __shared_count<_Lp>&>(),
  1145. std::declval<_Yp*>()));
  1146. // Detect an accessible and unambiguous enable_shared_from_this base.
  1147. template<typename _Yp, typename = void>
  1148. struct __has_esft_base
  1149. : false_type { };
  1150. template<typename _Yp>
  1151. struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
  1152. : __not_<is_array<_Tp>> { }; // No enable shared_from_this for arrays
  1153. template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
  1154. typename enable_if<__has_esft_base<_Yp2>::value>::type
  1155. _M_enable_shared_from_this_with(_Yp* __p) noexcept
  1156. {
  1157. if (auto __base = __enable_shared_from_this_base(_M_refcount, __p))
  1158. __base->_M_weak_assign(const_cast<_Yp2*>(__p), _M_refcount);
  1159. }
  1160. template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
  1161. typename enable_if<!__has_esft_base<_Yp2>::value>::type
  1162. _M_enable_shared_from_this_with(_Yp*) noexcept
  1163. { }
  1164. void*
  1165. _M_get_deleter(const std::type_info& __ti) const noexcept
  1166. { return _M_refcount._M_get_deleter(__ti); }
  1167. template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
  1168. template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
  1169. template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
  1170. friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept;
  1171. template<typename _Del, typename _Tp1>
  1172. friend _Del* get_deleter(const shared_ptr<_Tp1>&) noexcept;
  1173. element_type* _M_ptr; // Contained pointer.
  1174. __shared_count<_Lp> _M_refcount; // Reference counter.
  1175. };
  1176. // 20.7.2.2.7 shared_ptr comparisons
  1177. template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
  1178. inline bool
  1179. operator==(const __shared_ptr<_Tp1, _Lp>& __a,
  1180. const __shared_ptr<_Tp2, _Lp>& __b) noexcept
  1181. { return __a.get() == __b.get(); }
  1182. template<typename _Tp, _Lock_policy _Lp>
  1183. inline bool
  1184. operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
  1185. { return !__a; }
  1186. template<typename _Tp, _Lock_policy _Lp>
  1187. inline bool
  1188. operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
  1189. { return !__a; }
  1190. template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
  1191. inline bool
  1192. operator!=(const __shared_ptr<_Tp1, _Lp>& __a,
  1193. const __shared_ptr<_Tp2, _Lp>& __b) noexcept
  1194. { return __a.get() != __b.get(); }
  1195. template<typename _Tp, _Lock_policy _Lp>
  1196. inline bool
  1197. operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
  1198. { return (bool)__a; }
  1199. template<typename _Tp, _Lock_policy _Lp>
  1200. inline bool
  1201. operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
  1202. { return (bool)__a; }
  1203. template<typename _Tp, typename _Up, _Lock_policy _Lp>
  1204. inline bool
  1205. operator<(const __shared_ptr<_Tp, _Lp>& __a,
  1206. const __shared_ptr<_Up, _Lp>& __b) noexcept
  1207. {
  1208. using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
  1209. using _Up_elt = typename __shared_ptr<_Up, _Lp>::element_type;
  1210. using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type;
  1211. return less<_Vp>()(__a.get(), __b.get());
  1212. }
  1213. template<typename _Tp, _Lock_policy _Lp>
  1214. inline bool
  1215. operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
  1216. {
  1217. using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
  1218. return less<_Tp_elt*>()(__a.get(), nullptr);
  1219. }
  1220. template<typename _Tp, _Lock_policy _Lp>
  1221. inline bool
  1222. operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
  1223. {
  1224. using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
  1225. return less<_Tp_elt*>()(nullptr, __a.get());
  1226. }
  1227. template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
  1228. inline bool
  1229. operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
  1230. const __shared_ptr<_Tp2, _Lp>& __b) noexcept
  1231. { return !(__b < __a); }
  1232. template<typename _Tp, _Lock_policy _Lp>
  1233. inline bool
  1234. operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
  1235. { return !(nullptr < __a); }
  1236. template<typename _Tp, _Lock_policy _Lp>
  1237. inline bool
  1238. operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
  1239. { return !(__a < nullptr); }
  1240. template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
  1241. inline bool
  1242. operator>(const __shared_ptr<_Tp1, _Lp>& __a,
  1243. const __shared_ptr<_Tp2, _Lp>& __b) noexcept
  1244. { return (__b < __a); }
  1245. template<typename _Tp, _Lock_policy _Lp>
  1246. inline bool
  1247. operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
  1248. { return nullptr < __a; }
  1249. template<typename _Tp, _Lock_policy _Lp>
  1250. inline bool
  1251. operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
  1252. { return __a < nullptr; }
  1253. template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
  1254. inline bool
  1255. operator>=(const __shared_ptr<_Tp1, _Lp>& __a,
  1256. const __shared_ptr<_Tp2, _Lp>& __b) noexcept
  1257. { return !(__a < __b); }
  1258. template<typename _Tp, _Lock_policy _Lp>
  1259. inline bool
  1260. operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
  1261. { return !(__a < nullptr); }
  1262. template<typename _Tp, _Lock_policy _Lp>
  1263. inline bool
  1264. operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
  1265. { return !(nullptr < __a); }
  1266. // 20.7.2.2.8 shared_ptr specialized algorithms.
  1267. template<typename _Tp, _Lock_policy _Lp>
  1268. inline void
  1269. swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
  1270. { __a.swap(__b); }
  1271. // 20.7.2.2.9 shared_ptr casts
  1272. // The seemingly equivalent code:
  1273. // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
  1274. // will eventually result in undefined behaviour, attempting to
  1275. // delete the same object twice.
  1276. /// static_pointer_cast
  1277. template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
  1278. inline __shared_ptr<_Tp, _Lp>
  1279. static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
  1280. {
  1281. using _Sp = __shared_ptr<_Tp, _Lp>;
  1282. return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
  1283. }
  1284. // The seemingly equivalent code:
  1285. // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
  1286. // will eventually result in undefined behaviour, attempting to
  1287. // delete the same object twice.
  1288. /// const_pointer_cast
  1289. template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
  1290. inline __shared_ptr<_Tp, _Lp>
  1291. const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
  1292. {
  1293. using _Sp = __shared_ptr<_Tp, _Lp>;
  1294. return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
  1295. }
  1296. // The seemingly equivalent code:
  1297. // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
  1298. // will eventually result in undefined behaviour, attempting to
  1299. // delete the same object twice.
  1300. /// dynamic_pointer_cast
  1301. template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
  1302. inline __shared_ptr<_Tp, _Lp>
  1303. dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
  1304. {
  1305. using _Sp = __shared_ptr<_Tp, _Lp>;
  1306. if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
  1307. return _Sp(__r, __p);
  1308. return _Sp();
  1309. }
  1310. #if __cplusplus > 201402L
  1311. template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
  1312. inline __shared_ptr<_Tp, _Lp>
  1313. reinterpret_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
  1314. {
  1315. using _Sp = __shared_ptr<_Tp, _Lp>;
  1316. return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
  1317. }
  1318. #endif
  1319. template<typename _Tp, _Lock_policy _Lp>
  1320. class __weak_ptr
  1321. {
  1322. template<typename _Yp, typename _Res = void>
  1323. using _Compatible = typename
  1324. enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
  1325. // Constraint for assignment from shared_ptr and weak_ptr:
  1326. template<typename _Yp>
  1327. using _Assignable = _Compatible<_Yp, __weak_ptr&>;
  1328. public:
  1329. using element_type = typename remove_extent<_Tp>::type;
  1330. constexpr __weak_ptr() noexcept
  1331. : _M_ptr(nullptr), _M_refcount()
  1332. { }
  1333. __weak_ptr(const __weak_ptr&) noexcept = default;
  1334. ~__weak_ptr() = default;
  1335. // The "obvious" converting constructor implementation:
  1336. //
  1337. // template<typename _Tp1>
  1338. // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
  1339. // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
  1340. // { }
  1341. //
  1342. // has a serious problem.
  1343. //
  1344. // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
  1345. // conversion may require access to *__r._M_ptr (virtual inheritance).
  1346. //
  1347. // It is not possible to avoid spurious access violations since
  1348. // in multithreaded programs __r._M_ptr may be invalidated at any point.
  1349. template<typename _Yp, typename = _Compatible<_Yp>>
  1350. __weak_ptr(const __weak_ptr<_Yp, _Lp>& __r) noexcept
  1351. : _M_refcount(__r._M_refcount)
  1352. { _M_ptr = __r.lock().get(); }
  1353. template<typename _Yp, typename = _Compatible<_Yp>>
  1354. __weak_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
  1355. : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
  1356. { }
  1357. __weak_ptr(__weak_ptr&& __r) noexcept
  1358. : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount))
  1359. { __r._M_ptr = nullptr; }
  1360. template<typename _Yp, typename = _Compatible<_Yp>>
  1361. __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
  1362. : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount))
  1363. { __r._M_ptr = nullptr; }
  1364. __weak_ptr&
  1365. operator=(const __weak_ptr& __r) noexcept = default;
  1366. template<typename _Yp>
  1367. _Assignable<_Yp>
  1368. operator=(const __weak_ptr<_Yp, _Lp>& __r) noexcept
  1369. {
  1370. _M_ptr = __r.lock().get();
  1371. _M_refcount = __r._M_refcount;
  1372. return *this;
  1373. }
  1374. template<typename _Yp>
  1375. _Assignable<_Yp>
  1376. operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
  1377. {
  1378. _M_ptr = __r._M_ptr;
  1379. _M_refcount = __r._M_refcount;
  1380. return *this;
  1381. }
  1382. __weak_ptr&
  1383. operator=(__weak_ptr&& __r) noexcept
  1384. {
  1385. _M_ptr = __r._M_ptr;
  1386. _M_refcount = std::move(__r._M_refcount);
  1387. __r._M_ptr = nullptr;
  1388. return *this;
  1389. }
  1390. template<typename _Yp>
  1391. _Assignable<_Yp>
  1392. operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept
  1393. {
  1394. _M_ptr = __r.lock().get();
  1395. _M_refcount = std::move(__r._M_refcount);
  1396. __r._M_ptr = nullptr;
  1397. return *this;
  1398. }
  1399. __shared_ptr<_Tp, _Lp>
  1400. lock() const noexcept
  1401. { return __shared_ptr<element_type, _Lp>(*this, std::nothrow); }
  1402. long
  1403. use_count() const noexcept
  1404. { return _M_refcount._M_get_use_count(); }
  1405. bool
  1406. expired() const noexcept
  1407. { return _M_refcount._M_get_use_count() == 0; }
  1408. template<typename _Tp1>
  1409. bool
  1410. owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const noexcept
  1411. { return _M_refcount._M_less(__rhs._M_refcount); }
  1412. template<typename _Tp1>
  1413. bool
  1414. owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const noexcept
  1415. { return _M_refcount._M_less(__rhs._M_refcount); }
  1416. void
  1417. reset() noexcept
  1418. { __weak_ptr().swap(*this); }
  1419. void
  1420. swap(__weak_ptr& __s) noexcept
  1421. {
  1422. std::swap(_M_ptr, __s._M_ptr);
  1423. _M_refcount._M_swap(__s._M_refcount);
  1424. }
  1425. private:
  1426. // Used by __enable_shared_from_this.
  1427. void
  1428. _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept
  1429. {
  1430. if (use_count() == 0)
  1431. {
  1432. _M_ptr = __ptr;
  1433. _M_refcount = __refcount;
  1434. }
  1435. }
  1436. template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
  1437. template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
  1438. friend class __enable_shared_from_this<_Tp, _Lp>;
  1439. friend class enable_shared_from_this<_Tp>;
  1440. element_type* _M_ptr; // Contained pointer.
  1441. __weak_count<_Lp> _M_refcount; // Reference counter.
  1442. };
  1443. // 20.7.2.3.6 weak_ptr specialized algorithms.
  1444. template<typename _Tp, _Lock_policy _Lp>
  1445. inline void
  1446. swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
  1447. { __a.swap(__b); }
  1448. template<typename _Tp, typename _Tp1>
  1449. struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
  1450. {
  1451. bool
  1452. operator()(const _Tp& __lhs, const _Tp& __rhs) const noexcept
  1453. { return __lhs.owner_before(__rhs); }
  1454. bool
  1455. operator()(const _Tp& __lhs, const _Tp1& __rhs) const noexcept
  1456. { return __lhs.owner_before(__rhs); }
  1457. bool
  1458. operator()(const _Tp1& __lhs, const _Tp& __rhs) const noexcept
  1459. { return __lhs.owner_before(__rhs); }
  1460. };
  1461. template<>
  1462. struct _Sp_owner_less<void, void>
  1463. {
  1464. template<typename _Tp, typename _Up>
  1465. auto
  1466. operator()(const _Tp& __lhs, const _Up& __rhs) const noexcept
  1467. -> decltype(__lhs.owner_before(__rhs))
  1468. { return __lhs.owner_before(__rhs); }
  1469. using is_transparent = void;
  1470. };
  1471. template<typename _Tp, _Lock_policy _Lp>
  1472. struct owner_less<__shared_ptr<_Tp, _Lp>>
  1473. : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
  1474. { };
  1475. template<typename _Tp, _Lock_policy _Lp>
  1476. struct owner_less<__weak_ptr<_Tp, _Lp>>
  1477. : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
  1478. { };
  1479. template<typename _Tp, _Lock_policy _Lp>
  1480. class __enable_shared_from_this
  1481. {
  1482. protected:
  1483. constexpr __enable_shared_from_this() noexcept { }
  1484. __enable_shared_from_this(const __enable_shared_from_this&) noexcept { }
  1485. __enable_shared_from_this&
  1486. operator=(const __enable_shared_from_this&) noexcept
  1487. { return *this; }
  1488. ~__enable_shared_from_this() { }
  1489. public:
  1490. __shared_ptr<_Tp, _Lp>
  1491. shared_from_this()
  1492. { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
  1493. __shared_ptr<const _Tp, _Lp>
  1494. shared_from_this() const
  1495. { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
  1496. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  1497. __weak_ptr<_Tp, _Lp>
  1498. weak_from_this() noexcept
  1499. { return this->_M_weak_this; }
  1500. __weak_ptr<const _Tp, _Lp>
  1501. weak_from_this() const noexcept
  1502. { return this->_M_weak_this; }
  1503. #endif
  1504. private:
  1505. template<typename _Tp1>
  1506. void
  1507. _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept
  1508. { _M_weak_this._M_assign(__p, __n); }
  1509. friend const __enable_shared_from_this*
  1510. __enable_shared_from_this_base(const __shared_count<_Lp>&,
  1511. const __enable_shared_from_this* __p)
  1512. { return __p; }
  1513. template<typename, _Lock_policy>
  1514. friend class __shared_ptr;
  1515. mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
  1516. };
  1517. template<typename _Tp, _Lock_policy _Lp = __default_lock_policy,
  1518. typename _Alloc, typename... _Args>
  1519. inline __shared_ptr<_Tp, _Lp>
  1520. __allocate_shared(const _Alloc& __a, _Args&&... __args)
  1521. {
  1522. return __shared_ptr<_Tp, _Lp>(_Sp_alloc_shared_tag<_Alloc>{__a},
  1523. std::forward<_Args>(__args)...);
  1524. }
  1525. template<typename _Tp, _Lock_policy _Lp = __default_lock_policy,
  1526. typename... _Args>
  1527. inline __shared_ptr<_Tp, _Lp>
  1528. __make_shared(_Args&&... __args)
  1529. {
  1530. typedef typename std::remove_const<_Tp>::type _Tp_nc;
  1531. return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
  1532. std::forward<_Args>(__args)...);
  1533. }
  1534. /// std::hash specialization for __shared_ptr.
  1535. template<typename _Tp, _Lock_policy _Lp>
  1536. struct hash<__shared_ptr<_Tp, _Lp>>
  1537. : public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
  1538. {
  1539. size_t
  1540. operator()(const __shared_ptr<_Tp, _Lp>& __s) const noexcept
  1541. {
  1542. return hash<typename __shared_ptr<_Tp, _Lp>::element_type*>()(
  1543. __s.get());
  1544. }
  1545. };
  1546. _GLIBCXX_END_NAMESPACE_VERSION
  1547. } // namespace
  1548. #endif // _SHARED_PTR_BASE_H