shared_ptr_base.h 65 KB

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