libstdc++
shared_ptr_base.h
Go to the documentation of this file.
1// shared_ptr and weak_ptr implementation details -*- C++ -*-
2
3// Copyright (C) 2007-2026 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25// GCC Note: Based on files from version 1.32.0 of the Boost library.
26
27// shared_count.hpp
28// Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
29
30// shared_ptr.hpp
31// Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
32// Copyright (C) 2001, 2002, 2003 Peter Dimov
33
34// weak_ptr.hpp
35// Copyright (C) 2001, 2002, 2003 Peter Dimov
36
37// enable_shared_from_this.hpp
38// Copyright (C) 2002 Peter Dimov
39
40// Distributed under the Boost Software License, Version 1.0. (See
41// accompanying file LICENSE_1_0.txt or copy at
42// http://www.boost.org/LICENSE_1_0.txt)
43
44/** @file bits/shared_ptr_base.h
45 * This is an internal header file, included by other library headers.
46 * Do not attempt to use it directly. @headername{memory}
47 */
48
49#ifndef _SHARED_PTR_BASE_H
50#define _SHARED_PTR_BASE_H 1
51
52#include <typeinfo>
53#include <bits/allocated_ptr.h>
54#include <bits/allocator.h>
57#include <bits/refwrap.h>
58#include <bits/stl_function.h> // std::less
59#include <bits/unique_ptr.h>
60#include <ext/aligned_buffer.h>
61#include <ext/atomicity.h>
62#include <ext/concurrence.h>
63#if __cplusplus >= 202002L
64# include <compare>
65# include <bits/align.h> // std::align
67#endif
68
69namespace std _GLIBCXX_VISIBILITY(default)
70{
71_GLIBCXX_BEGIN_NAMESPACE_VERSION
72
73#if _GLIBCXX_USE_DEPRECATED
74#pragma GCC diagnostic push
75#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
76 template<typename> class auto_ptr;
77#pragma GCC diagnostic pop
78#endif
79
80 /**
81 * @brief Exception possibly thrown by @c shared_ptr.
82 * @ingroup exceptions
83 */
85 {
86 public:
87 virtual char const* what() const noexcept;
88
89 virtual ~bad_weak_ptr() noexcept;
90 };
91
92 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
93 inline void
94 __throw_bad_weak_ptr()
95 { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr()); }
96
97 using __gnu_cxx::_Lock_policy;
98 using __gnu_cxx::__default_lock_policy;
99 using __gnu_cxx::_S_single;
100 using __gnu_cxx::_S_mutex;
101 using __gnu_cxx::_S_atomic;
102
103 // Empty helper class except when the template argument is _S_mutex.
104 template<_Lock_policy _Lp>
105 class _Mutex_base
106 {
107 protected:
108 // The atomic policy uses fully-fenced builtins, single doesn't care.
109 enum { _S_need_barriers = 0 };
110 };
111
112 template<>
113 class _Mutex_base<_S_mutex>
114 : public __gnu_cxx::__mutex
115 {
116 protected:
117 // This policy is used when atomic builtins are not available.
118 // The replacement atomic operations might not have the necessary
119 // memory barriers.
120 enum { _S_need_barriers = 1 };
121 };
122
123 template<_Lock_policy _Lp = __default_lock_policy>
124 class _Sp_counted_base
125 : public _Mutex_base<_Lp>
126 {
127 public:
128 _Sp_counted_base() noexcept
129 : _M_use_count(1), _M_weak_count(1) { }
130
131 virtual
132 ~_Sp_counted_base() noexcept
133 { }
134
135 // Called when _M_use_count drops to zero, to release the resources
136 // managed by *this.
137 virtual void
138 _M_dispose() noexcept = 0;
139
140 // Called when _M_weak_count drops to zero.
141 virtual void
142 _M_destroy() noexcept
143 { delete this; }
144
145 virtual void*
146 _M_get_deleter(const std::type_info&) noexcept = 0;
147
148 // Increment the use count (used when the count is greater than zero).
149 void
150 _M_add_ref_copy()
151 { _S_chk(__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1)); }
152
153 // Increment the use count if it is non-zero, throw otherwise.
154 void
155 _M_add_ref_lock()
156 {
157 if (!_M_add_ref_lock_nothrow())
158 __throw_bad_weak_ptr();
159 }
160
161 // Increment the use count if it is non-zero.
162 bool
163 _M_add_ref_lock_nothrow() noexcept;
164
165 // Decrement the use count.
166 void
167 _M_release() noexcept;
168
169 // Called by _M_release() when the use count reaches zero.
170 void
171 _M_release_last_use() noexcept
172 {
173 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
174 _M_dispose();
175 // There must be a memory barrier between dispose() and destroy()
176 // to ensure that the effects of dispose() are observed in the
177 // thread that runs destroy().
178 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
179 if (_Mutex_base<_Lp>::_S_need_barriers)
180 {
181 __atomic_thread_fence (__ATOMIC_ACQ_REL);
182 }
183
184 // Be race-detector-friendly. For more info see bits/c++config.
185 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
186 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
187 -1) == 1)
188 {
189 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
190 _M_destroy();
191 }
192 }
193
194 // As above, but 'noinline' to reduce code size on the cold path.
195 __attribute__((__noinline__))
196 void
197 _M_release_last_use_cold() noexcept
198 { _M_release_last_use(); }
199
200 // Increment the weak count.
201 void
202 _M_weak_add_ref() noexcept
203 {
204 // _M_weak_count can always use negative values because it cannot be
205 // observed by users (unlike _M_use_count). See _S_chk for details.
206 constexpr _Atomic_word __max = -1;
207 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, 1) == __max)
208 [[__unlikely__]] __builtin_trap();
209 }
210
211 // Decrement the weak count.
212 void
213 _M_weak_release() noexcept
214 {
215 // Be race-detector-friendly. For more info see bits/c++config.
216 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
217 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
218 {
219 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
220 if (_Mutex_base<_Lp>::_S_need_barriers)
221 {
222 // See _M_release(),
223 // destroy() must observe results of dispose()
224 __atomic_thread_fence (__ATOMIC_ACQ_REL);
225 }
226 _M_destroy();
227 }
228 }
229
230 long
231 _M_get_use_count() const noexcept
232 {
233 // No memory barrier is used here so there is no synchronization
234 // with other threads.
235 auto __count = __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
236
237 // If long is wider than _Atomic_word then we can treat _Atomic_word
238 // as unsigned, and so double its usable range. If the widths are the
239 // same then casting to unsigned and then to long is a no-op.
240 return static_cast<_Unsigned_count_type>(__count);
241 }
242
243 private:
244 _Sp_counted_base(_Sp_counted_base const&) = delete;
245 _Sp_counted_base& operator=(_Sp_counted_base const&) = delete;
246
247#pragma GCC diagnostic push
248#pragma GCC diagnostic ignored "-Wignored-attributes"
249 // This is only to be used for arithmetic, not for atomic ops.
250 using _Unsigned_count_type = make_unsigned<_Atomic_word>::type;
251#pragma GCC diagnostic pop
252
253 // Called when incrementing _M_use_count to cause a trap on overflow.
254 // This should be passed the value of the counter before the increment.
255 static void
256 _S_chk(_Atomic_word __count)
257 {
258 constexpr _Atomic_word __max_atomic_word = _Unsigned_count_type(-1)/2;
259
260 // __max is the maximum allowed value for the shared reference count.
261 // All valid reference count values need to fit into [0,LONG_MAX)
262 // because users can observe the count via shared_ptr::use_count().
263 //
264 // When long is wider than _Atomic_word, _M_use_count can go negative
265 // and the cast in _Sp_counted_base::use_count() will turn it into a
266 // positive value suitable for returning to users. The implementation
267 // only cares whether _M_use_count reaches zero after a decrement,
268 // so negative values are not a problem internally.
269 // So when possible, use -1 for __max (incrementing past that would
270 // overflow _M_use_count to 0, which means an empty shared_ptr).
271 //
272 // When long is not wider than _Atomic_word, __max is just the type's
273 // maximum positive value. We cannot use negative counts because they
274 // would not fit in [0,LONG_MAX) after casting to an unsigned type,
275 // which would cause use_count() to return bogus values.
276 constexpr _Atomic_word __max
277 = sizeof(long) > sizeof(_Atomic_word) ? -1 : __max_atomic_word;
278
279 if (__count == __max) [[__unlikely__]]
280 __builtin_trap();
281 }
282
283 _Atomic_word _M_use_count; // #shared
284 _Atomic_word _M_weak_count; // #weak + (#shared != 0)
285 };
286
287 // We use __atomic_add_single and __exchange_and_add_single in the _S_single
288 // member specializations because they use unsigned arithmetic and so avoid
289 // undefined overflow.
290 template<>
291 inline void
292 _Sp_counted_base<_S_single>::_M_add_ref_copy()
293 {
294 _S_chk(_M_use_count);
295 __gnu_cxx::__atomic_add_single(&_M_use_count, 1);
296 }
297
298 template<>
299 inline void
300 _Sp_counted_base<_S_single>::_M_weak_release() noexcept
301 {
302 if (__gnu_cxx::__exchange_and_add_single(&_M_weak_count, -1) == 1)
303 _M_destroy();
304 }
305
306 template<>
307 inline long
308 _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept
309 {
310 return static_cast<_Unsigned_count_type>(_M_use_count);
311 }
312
313
314 template<>
315 inline bool
316 _Sp_counted_base<_S_single>::
317 _M_add_ref_lock_nothrow() noexcept
318 {
319 if (_M_use_count == 0)
320 return false;
321 _M_add_ref_copy();
322 return true;
323 }
324
325 template<>
326 inline bool
327 _Sp_counted_base<_S_mutex>::
328 _M_add_ref_lock_nothrow() noexcept
329 {
330 __gnu_cxx::__scoped_lock sentry(*this);
331 if (auto __c = __gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1))
332 _S_chk(__c);
333 else
334 {
335 // Count was zero, so we cannot lock it to get a shared_ptr.
336 // Reset to zero. This isn't racy, because there are no shared_ptr
337 // objects using this count and any other weak_ptr objects using it
338 // must call this function to modify _M_use_count, so would be
339 // synchronized by the mutex.
340 _M_use_count = 0;
341 return false;
342 }
343 return true;
344 }
345
346 template<>
347 inline bool
348 _Sp_counted_base<_S_atomic>::
349 _M_add_ref_lock_nothrow() noexcept
350 {
351 // Perform lock-free add-if-not-zero operation.
352 _Atomic_word __count = _M_get_use_count();
353 do
354 {
355 if (__count == 0)
356 return false;
357 // Replace the current counter value with the old value + 1, as
358 // long as it's not changed meanwhile.
359 }
360 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
361 true, __ATOMIC_ACQ_REL,
362 __ATOMIC_RELAXED));
363 _S_chk(__count);
364 return true;
365 }
366
367 template<>
368 inline void
369 _Sp_counted_base<_S_single>::_M_release() noexcept
370 {
371 if (__gnu_cxx::__exchange_and_add_single(&_M_use_count, -1) == 1)
372 {
373 _M_dispose();
374 _M_weak_release();
375 }
376 }
377
378 template<>
379 inline void
380 _Sp_counted_base<_S_mutex>::_M_release() noexcept
381 {
382 // Be race-detector-friendly. For more info see bits/c++config.
383 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
384 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
385 {
386 _M_release_last_use();
387 }
388 }
389
390 template<>
391 inline void
392 _Sp_counted_base<_S_atomic>::_M_release() noexcept
393 {
394#pragma GCC diagnostic push
395#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
396 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
397#if ! _GLIBCXX_TSAN
398 constexpr bool __lock_free
399 = __atomic_always_lock_free(sizeof(long long), 0)
400 && __atomic_always_lock_free(sizeof(_Atomic_word), 0);
401 constexpr bool __double_word
402 = sizeof(long long) == 2 * sizeof(_Atomic_word);
403 // The ref-count members follow the vptr, so are aligned to
404 // alignof(void*).
405 constexpr bool __aligned = __alignof(long long) <= alignof(void*);
406 if constexpr (__lock_free && __double_word && __aligned)
407 {
408 constexpr int __wordbits = __CHAR_BIT__ * sizeof(_Atomic_word);
409 constexpr int __shiftbits = __double_word ? __wordbits : 0;
410 constexpr long long __unique_ref = 1LL + (1LL << __shiftbits);
411 auto __both_counts = reinterpret_cast<long long*>(&_M_use_count);
412
413 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
414 if (__atomic_load_n(__both_counts, __ATOMIC_ACQUIRE) == __unique_ref)
415 {
416 // Both counts are 1, so there are no weak references and
417 // we are releasing the last strong reference. No other
418 // threads can observe the effects of this _M_release()
419 // call (e.g. calling use_count()) without a data race.
420 _M_weak_count = _M_use_count = 0;
421 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
422 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
423 _M_dispose();
424 _M_destroy();
425 return;
426 }
427 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
428 [[__unlikely__]]
429 {
430 _M_release_last_use_cold();
431 return;
432 }
433 }
434 else
435#endif
436 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
437 {
438 _M_release_last_use();
439 }
440#pragma GCC diagnostic pop
441 }
442
443 // Forward declarations.
444 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
445 class __shared_ptr;
446
447 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
448 class __weak_ptr;
449
450 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
451 class __enable_shared_from_this;
452
453 template<typename _Tp>
454 class shared_ptr;
455
456 template<typename _Tp>
457 class weak_ptr;
458
459 template<typename _Tp>
460 struct owner_less;
461
462 template<typename _Tp>
463 class enable_shared_from_this;
464
465 template<_Lock_policy _Lp = __default_lock_policy>
466 class __weak_count;
467
468 template<_Lock_policy _Lp = __default_lock_policy>
469 class __shared_count;
470
471#ifdef __glibcxx_atomic_shared_ptr
472 template<typename>
473 class _Sp_atomic;
474#endif
475
476 // Counted ptr with no deleter or allocator support
477 template<typename _Ptr, _Lock_policy _Lp>
478 class _Sp_counted_ptr final : public _Sp_counted_base<_Lp>
479 {
480 public:
481 explicit
482 _Sp_counted_ptr(_Ptr __p) noexcept
483 : _M_ptr(__p) { }
484
485 virtual void
486 _M_dispose() noexcept
487 { delete _M_ptr; }
488
489 virtual void
490 _M_destroy() noexcept
491 { delete this; }
492
493 virtual void*
494 _M_get_deleter(const std::type_info&) noexcept
495 { return nullptr; }
496
497 _Sp_counted_ptr(const _Sp_counted_ptr&) = delete;
498 _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete;
499
500 private:
501 _Ptr _M_ptr;
502 };
503
504 template<>
505 inline void
506 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
507
508 template<>
509 inline void
510 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
511
512 template<>
513 inline void
514 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
515
516 // FIXME: once __has_cpp_attribute(__no_unique_address__)) is true for
517 // all supported compilers we can greatly simplify _Sp_ebo_helper.
518 // N.B. unconditionally applying the attribute could change layout for
519 // final types, which currently cannot use EBO so have a unique address.
520
521 template<int _Nm, typename _Tp,
522 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
523 struct _Sp_ebo_helper;
524
525 /// Specialization using EBO.
526 template<int _Nm, typename _Tp>
527 struct _Sp_ebo_helper<_Nm, _Tp, true> : private _Tp
528 {
529 explicit _Sp_ebo_helper(const _Tp& __tp) : _Tp(__tp) { }
530 explicit _Sp_ebo_helper(_Tp&& __tp) : _Tp(std::move(__tp)) { }
531
532 static _Tp&
533 _S_get(_Sp_ebo_helper& __eboh) { return static_cast<_Tp&>(__eboh); }
534 };
535
536 /// Specialization not using EBO.
537 template<int _Nm, typename _Tp>
538 struct _Sp_ebo_helper<_Nm, _Tp, false>
539 {
540 explicit _Sp_ebo_helper(const _Tp& __tp) : _M_tp(__tp) { }
541 explicit _Sp_ebo_helper(_Tp&& __tp) : _M_tp(std::move(__tp)) { }
542
543 static _Tp&
544 _S_get(_Sp_ebo_helper& __eboh)
545 { return __eboh._M_tp; }
546
547 private:
548 _Tp _M_tp;
549 };
550
551 // Support for custom deleter and/or allocator
552 template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
553 class _Sp_counted_deleter final : public _Sp_counted_base<_Lp>
554 {
555 class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
556 {
557 typedef _Sp_ebo_helper<0, _Deleter> _Del_base;
558 typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base;
559
560 public:
561 _Impl(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept
562 : _Del_base(std::move(__d)), _Alloc_base(__a), _M_ptr(__p)
563 { }
564
565 _Deleter& _M_del() noexcept { return _Del_base::_S_get(*this); }
566 _Alloc& _M_alloc() noexcept { return _Alloc_base::_S_get(*this); }
567
568 _Ptr _M_ptr;
569 };
570
571 public:
572 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
573
574 // __d(__p) must not throw.
575 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
576 : _M_impl(__p, std::move(__d), _Alloc()) { }
577
578 // __d(__p) must not throw.
579 _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept
580 : _M_impl(__p, std::move(__d), __a) { }
581
582#pragma GCC diagnostic push // PR tree-optimization/122197
583#pragma GCC diagnostic ignored "-Wfree-nonheap-object"
584 template<typename> class auto_ptr;
585 ~_Sp_counted_deleter() noexcept { }
586#pragma GCC diagnostic pop
587
588 virtual void
589 _M_dispose() noexcept
590 { _M_impl._M_del()(_M_impl._M_ptr); }
591
592 virtual void
593 _M_destroy() noexcept
594 {
595 __allocator_type __a(_M_impl._M_alloc());
596 __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
597 this->~_Sp_counted_deleter();
598 }
599
600 virtual void*
601 _M_get_deleter(const type_info& __ti [[__gnu__::__unused__]]) noexcept
602 {
603#if __cpp_rtti
604 // _GLIBCXX_RESOLVE_LIB_DEFECTS
605 // 2400. shared_ptr's get_deleter() should use addressof()
606 return __ti == typeid(_Deleter)
607 ? std::__addressof(_M_impl._M_del())
608 : nullptr;
609#else
610 return nullptr;
611#endif
612 }
613
614 private:
615#ifdef __glibcxx_out_ptr
616 template<typename, typename, typename...> friend class out_ptr_t;
617#endif
618 _Impl _M_impl;
619 };
620
621 // helpers for make_shared / allocate_shared
622
623 struct _Sp_make_shared_tag
624 {
625 private:
626 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
627 friend class _Sp_counted_ptr_inplace;
628
629 static const type_info&
630 _S_ti() noexcept _GLIBCXX_VISIBILITY(default)
631 {
632 alignas(type_info) static constexpr char __tag[sizeof(type_info)] = { };
633 return reinterpret_cast<const type_info&>(__tag);
634 }
635
636 static bool _S_eq(const type_info&) noexcept;
637 };
638
639 template<typename _Alloc>
640 struct _Sp_alloc_shared_tag
641 {
642 const _Alloc& _M_a;
643 };
644
645 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
646 class _Sp_counted_ptr_inplace final : public _Sp_counted_base<_Lp>
647 {
648 class _Impl : _Sp_ebo_helper<0, _Alloc>
649 {
650 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
651
652 public:
653 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
654
655 _Alloc& _M_alloc() noexcept { return _A_base::_S_get(*this); }
656
657 __gnu_cxx::__aligned_buffer<__remove_cv_t<_Tp>> _M_storage;
658 };
659
660 public:
661 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
662
663 // Alloc parameter is not a reference so doesn't alias anything in __args
664 template<typename... _Args>
665 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
666 : _M_impl(__a)
667 {
668 // _GLIBCXX_RESOLVE_LIB_DEFECTS
669 // 2070. allocate_shared should use allocator_traits<A>::construct
671 std::forward<_Args>(__args)...); // might throw
672 }
673
674#pragma GCC diagnostic push // PR tree-optimization/122197
675#pragma GCC diagnostic ignored "-Warray-bounds"
676 ~_Sp_counted_ptr_inplace() noexcept { }
677#pragma GCC diagnostic pop
678
679 virtual void
680 _M_dispose() noexcept
681 {
682 allocator_traits<_Alloc>::destroy(_M_impl._M_alloc(), _M_ptr());
683 }
684
685 // Override because the allocator needs to know the dynamic type
686 virtual void
687 _M_destroy() noexcept
688 {
689 __allocator_type __a(_M_impl._M_alloc());
690 __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
691 this->~_Sp_counted_ptr_inplace();
692 }
693
694 private:
695 friend class __shared_count<_Lp>; // To be able to call _M_ptr().
696
697 // No longer used, but code compiled against old libstdc++ headers
698 // might still call it from __shared_ptr ctor to get the pointer out.
699 virtual void*
700 _M_get_deleter(const std::type_info& __ti) noexcept override
701 {
702 // Check for the fake type_info first, so we don't try to access it
703 // as a real type_info object. Otherwise, check if it's the real
704 // type_info for this class. With RTTI enabled we can check directly,
705 // or call a library function to do it.
706 if (&__ti == &_Sp_make_shared_tag::_S_ti()
707 ||
708#if __cpp_rtti
709 __ti == typeid(_Sp_make_shared_tag)
710#else
711 _Sp_make_shared_tag::_S_eq(__ti)
712#endif
713 )
714 return _M_ptr();
715 return nullptr;
716 }
717
718 __remove_cv_t<_Tp>*
719 _M_ptr() noexcept { return _M_impl._M_storage._M_ptr(); }
720
721 _Impl _M_impl;
722 };
723
724#ifdef __glibcxx_smart_ptr_for_overwrite // C++ >= 20 && HOSTED
725 struct _Sp_overwrite_tag { };
726
727 // Partial specialization used for make_shared_for_overwrite<non-array>().
728 // This partial specialization is used when the allocator's value type
729 // is the special _Sp_overwrite_tag type.
730 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
731 requires is_same_v<typename _Alloc::value_type, _Sp_overwrite_tag>
732 class _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> final
733 : public _Sp_counted_base<_Lp>
734 {
735 [[no_unique_address]] _Alloc _M_alloc;
736
737 union {
738 remove_cv_t<_Tp> _M_obj;
739 char _M_unused;
740 };
741
742 friend class __shared_count<_Lp>; // To be able to call _M_ptr().
743
744 auto _M_ptr() noexcept { return std::__addressof(_M_obj); }
745
746 public:
747 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
748
749 _Sp_counted_ptr_inplace(const _Alloc& __a)
750 : _M_alloc(__a)
751 {
752 ::new((void*)_M_ptr()) _Tp; // default-initialized, for overwrite.
753 }
754
755 ~_Sp_counted_ptr_inplace() noexcept { }
756
757 virtual void
758 _M_dispose() noexcept
759 {
760 _M_obj.~_Tp();
761 }
762
763 // Override because the allocator needs to know the dynamic type
764 virtual void
765 _M_destroy() noexcept
766 {
767 using pointer = typename allocator_traits<__allocator_type>::pointer;
768 __allocator_type __a(_M_alloc);
769 auto __p = pointer_traits<pointer>::pointer_to(*this);
770 __allocated_ptr<__allocator_type> __guard_ptr{ __a, __p };
771 this->~_Sp_counted_ptr_inplace();
772 }
773
774 void*
775 _M_get_deleter(const std::type_info&) noexcept override
776 { return nullptr; }
777 };
778#endif // __glibcxx_smart_ptr_for_overwrite
779
780#if __glibcxx_shared_ptr_arrays >= 201707L // C++ >= 20 && HOSTED
781 struct _Sp_overwrite_tag;
782
783 // For make_shared<T[]>, make_shared<T[N]>, allocate_shared<T[]> etc.
784 template<typename _Alloc>
785 struct _Sp_counted_array_base
786 {
787 [[no_unique_address]] _Alloc _M_alloc{};
788 size_t _M_n = 0;
789 bool _M_overwrite = false;
790
791 typename allocator_traits<_Alloc>::pointer
792 _M_alloc_array(size_t __tail)
793 {
794 return allocator_traits<_Alloc>::allocate(_M_alloc, _M_n + __tail);
795 }
796
797 void
798 _M_dealloc_array(typename allocator_traits<_Alloc>::pointer __p,
799 size_t __tail)
800 {
801 allocator_traits<_Alloc>::deallocate(_M_alloc, __p, _M_n + __tail);
802 }
803
804 // Init the array elements
805 template<typename _Init>
806 void
807 _M_init(typename allocator_traits<_Alloc>::value_type* __p,
808 _Init __init)
809 {
810 using _Tp = remove_pointer_t<_Init>;
811 using _Up = typename allocator_traits<_Alloc>::value_type;
812
813 if constexpr (is_same_v<_Init, _Sp_overwrite_tag>)
814 {
816 _M_overwrite = true;
817 }
818 else if (__init == nullptr)
819 std::__uninitialized_default_n_a(__p, _M_n, _M_alloc);
820 else if constexpr (!is_array_v<_Tp>)
821 std::__uninitialized_fill_n_a(__p, _M_n, *__init, _M_alloc);
822 else
823 {
824#pragma GCC diagnostic push
825#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
826 struct _Iter
827 {
828 using value_type = _Up;
829 using difference_type = ptrdiff_t;
830 using pointer = const _Up*;
831 using reference = const _Up&;
832 using iterator_category = forward_iterator_tag;
833
834 const _Up* _M_p;
835 size_t _M_len;
836 size_t _M_pos;
837
838 _Iter& operator++() { ++_M_pos; return *this; }
839 _Iter operator++(int) { auto __i(*this); ++_M_pos; return __i; }
840
841 reference operator*() const { return _M_p[_M_pos % _M_len]; }
842 pointer operator->() const { return _M_p + (_M_pos % _M_len); }
843
844 bool operator==(const _Iter& __i) const
845 { return _M_pos == __i._M_pos; }
846 };
847#pragma GCC diagnostic pop
848
849 _Iter __first{_S_first_elem(__init), sizeof(_Tp) / sizeof(_Up)};
850 _Iter __last = __first;
851 __last._M_pos = _M_n;
852 std::__uninitialized_copy_a(__first, __last, __p, _M_alloc);
853 }
854 }
855
856 protected:
857 // Destroy the array elements
858 void
859 _M_dispose_array(typename allocator_traits<_Alloc>::value_type* __p)
860 {
861 if (_M_overwrite)
862 std::destroy_n(__p, _M_n);
863 else
864 {
865 size_t __n = _M_n;
866 while (__n--)
867 allocator_traits<_Alloc>::destroy(_M_alloc, __p + __n);
868 }
869 }
870
871 private:
872 template<typename _Tp>
873 static _Tp*
874 _S_first_elem(_Tp* __p) { return __p; }
875
876 template<typename _Tp, size_t _Nm>
877 static auto
878 _S_first_elem(_Tp (*__p)[_Nm]) { return _S_first_elem(*__p); }
879 };
880
881 // Control block for make_shared<T[]>, make_shared<T[N]> etc. that will be
882 // placed into unused memory at the end of the array.
883 template<typename _Alloc, _Lock_policy _Lp>
884 class _Sp_counted_array final
885 : public _Sp_counted_base<_Lp>, _Sp_counted_array_base<_Alloc>
886 {
887 using pointer = typename allocator_traits<_Alloc>::pointer;
888
889 pointer _M_alloc_ptr;
890
891 auto _M_ptr() const noexcept { return std::to_address(_M_alloc_ptr); }
892
893 friend class __shared_count<_Lp>; // To be able to call _M_ptr().
894
895 public:
896 _Sp_counted_array(const _Sp_counted_array_base<_Alloc>& __a,
897 pointer __p) noexcept
898 : _Sp_counted_array_base<_Alloc>(__a), _M_alloc_ptr(__p)
899 { }
900
901 ~_Sp_counted_array() = default;
902
903 virtual void
904 _M_dispose() noexcept
905 {
906 if (this->_M_n)
907 this->_M_dispose_array(_M_ptr());
908 }
909
910 // Override because the allocator needs to know the dynamic type
911 virtual void
912 _M_destroy() noexcept
913 {
914 _Sp_counted_array_base<_Alloc> __a = *this;
915 pointer __p = _M_alloc_ptr;
916 this->~_Sp_counted_array();
917 __a._M_dealloc_array(__p, _S_tail());
918 }
919
920 // Returns the number of additional array elements that must be
921 // allocated in order to store a _Sp_counted_array at the end.
922 static constexpr size_t
923 _S_tail()
924 {
925 // The array elemenent type.
926 using _Tp = typename allocator_traits<_Alloc>::value_type;
927
928 // The space needed to store a _Sp_counted_array object.
929 size_t __bytes = sizeof(_Sp_counted_array);
930
931 // Add any padding needed for manual alignment within the buffer.
932 if constexpr (alignof(_Tp) < alignof(_Sp_counted_array))
933 __bytes += alignof(_Sp_counted_array) - alignof(_Tp);
934
935 return (__bytes + sizeof(_Tp) - 1) / sizeof(_Tp);
936 }
937
938 void*
939 _M_get_deleter(const std::type_info&) noexcept override
940 { return nullptr; }
941 };
942#endif // __glibcxx_shared_ptr_arrays >= 201707L
943
944 // The default deleter for shared_ptr<T[]> and shared_ptr<T[N]>.
945 struct __sp_array_delete
946 {
947 template<typename _Yp>
948 void operator()(_Yp* __p) const { delete[] __p; }
949 };
950
951 template<_Lock_policy _Lp>
952 class __shared_count
953 {
954 // Prevent _Sp_alloc_shared_tag from matching the shared_ptr(P, D) ctor.
955 template<typename _Tp>
956 struct __not_alloc_shared_tag { using type = void; };
957
958 template<typename _Tp>
959 struct __not_alloc_shared_tag<_Sp_alloc_shared_tag<_Tp>> { };
960
961#if __glibcxx_shared_ptr_arrays >= 201707L // C++ >= 20 && HOSTED
962 template<typename _Alloc>
963 struct __not_alloc_shared_tag<_Sp_counted_array_base<_Alloc>> { };
964#endif
965
966 public:
967 constexpr __shared_count() noexcept : _M_pi(0)
968 { }
969
970 template<typename _Ptr>
971 explicit
972 __shared_count(_Ptr __p) : _M_pi(0)
973 {
974 __try
975 {
976 _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
977 }
978 __catch(...)
979 {
980 delete __p;
981 __throw_exception_again;
982 }
983 }
984
985 template<typename _Ptr>
986 __shared_count(_Ptr __p, /* is_array = */ false_type)
987 : __shared_count(__p)
988 { }
989
990 template<typename _Ptr>
991 __shared_count(_Ptr __p, /* is_array = */ true_type)
992 : __shared_count(__p, __sp_array_delete{}, allocator<void>())
993 { }
994
995 template<typename _Ptr, typename _Deleter,
996 typename = typename __not_alloc_shared_tag<_Deleter>::type>
997 __shared_count(_Ptr __p, _Deleter __d)
998 : __shared_count(__p, std::move(__d), allocator<void>())
999 { }
1000
1001 template<typename _Ptr, typename _Deleter, typename _Alloc,
1002 typename = typename __not_alloc_shared_tag<_Deleter>::type>
1003 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
1004 {
1005 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
1006 __try
1007 {
1008 typename _Sp_cd_type::__allocator_type __a2(__a);
1009 auto __guard = std::__allocate_guarded(__a2);
1010 _Sp_cd_type* __mem = __guard.get();
1011 ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a));
1012 _M_pi = __mem;
1013 __guard = nullptr;
1014 }
1015 __catch(...)
1016 {
1017 __d(__p); // Call _Deleter on __p.
1018 __throw_exception_again;
1019 }
1020 }
1021
1022 template<typename _Tp, typename _Alloc, typename... _Args>
1023 __shared_count(_Tp*& __p, _Sp_alloc_shared_tag<_Alloc> __a,
1024 _Args&&... __args)
1025 {
1026 using _Tp2 = __remove_cv_t<_Tp>;
1027 using _Sp_cp_type = _Sp_counted_ptr_inplace<_Tp2, _Alloc, _Lp>;
1028 typename _Sp_cp_type::__allocator_type __a2(__a._M_a);
1029 auto __guard = std::__allocate_guarded(__a2);
1030 _Sp_cp_type* __mem = __guard.get();
1031 auto __pi = ::new (__mem)
1032 _Sp_cp_type(__a._M_a, std::forward<_Args>(__args)...);
1033 __guard = nullptr;
1034 _M_pi = __pi;
1035 __p = __pi->_M_ptr();
1036 }
1037
1038#if __glibcxx_shared_ptr_arrays >= 201707L // C++ >= 20 && HOSTED
1039 template<typename _Tp, typename _Alloc, typename _Init>
1040 __shared_count(_Tp*& __p, const _Sp_counted_array_base<_Alloc>& __a,
1041 _Init __init)
1042 {
1043 using _Up = remove_all_extents_t<_Tp>;
1044 static_assert(is_same_v<_Up, typename _Alloc::value_type>);
1045
1046 using _Sp_ca_type = _Sp_counted_array<_Alloc, _Lp>;
1047 const size_t __tail = _Sp_ca_type::_S_tail();
1048
1049 struct _Guarded_ptr : _Sp_counted_array_base<_Alloc>
1050 {
1051 typename allocator_traits<_Alloc>::pointer _M_ptr;
1052
1053 _Guarded_ptr(_Sp_counted_array_base<_Alloc> __a)
1054 : _Sp_counted_array_base<_Alloc>(__a),
1055 _M_ptr(this->_M_alloc_array(_Sp_ca_type::_S_tail()))
1056 { }
1057
1058 ~_Guarded_ptr()
1059 {
1060 if (_M_ptr)
1061 this->_M_dealloc_array(_M_ptr, _Sp_ca_type::_S_tail());
1062 }
1063 };
1064
1065 _Guarded_ptr __guard{__a};
1066 _Up* const __raw = std::to_address(__guard._M_ptr);
1067 __guard._M_init(__raw, __init); // might throw
1068
1069 void* __c = __raw + __a._M_n;
1070 if constexpr (alignof(_Up) < alignof(_Sp_ca_type))
1071 {
1072 size_t __space = sizeof(_Up) * __tail;
1073 __c = std::align(alignof(_Sp_ca_type), sizeof(_Sp_ca_type),
1074 __c, __space);
1075 }
1076 auto __pi = ::new(__c) _Sp_ca_type(__guard, __guard._M_ptr);
1077 __guard._M_ptr = nullptr;
1078 _M_pi = __pi;
1079 __p = reinterpret_cast<_Tp*>(__raw);
1080 }
1081#endif
1082
1083#if _GLIBCXX_USE_DEPRECATED
1084#pragma GCC diagnostic push
1085#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1086 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
1087 template<typename _Tp>
1088 explicit
1089 __shared_count(std::auto_ptr<_Tp>&& __r);
1090#pragma GCC diagnostic pop
1091#endif
1092
1093 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
1094 template<typename _Tp, typename _Del>
1095 explicit
1096 __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0)
1097 {
1098 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1099 // 2415. Inconsistency between unique_ptr and shared_ptr
1100 if (__r.get() == nullptr)
1101 return;
1102
1103 using _Ptr = typename unique_ptr<_Tp, _Del>::pointer;
1104 using _Del2 = __conditional_t<is_reference<_Del>::value,
1105 reference_wrapper<typename remove_reference<_Del>::type>,
1106 _Del>;
1107 using _Sp_cd_type
1108 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
1109 using _Alloc = allocator<_Sp_cd_type>;
1110 using _Alloc_traits = allocator_traits<_Alloc>;
1111 _Alloc __a;
1112 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
1113 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1114 // 3548. shared_ptr construction from unique_ptr should move
1115 // (not copy) the deleter
1116 _Alloc_traits::construct(__a, __mem, __r.release(),
1117 std::forward<_Del>(__r.get_deleter()));
1118 _M_pi = __mem;
1119 }
1120
1121 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
1122 explicit __shared_count(const __weak_count<_Lp>& __r);
1123
1124 // Does not throw if __r._M_get_use_count() == 0, caller must check.
1125 explicit
1126 __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t) noexcept;
1127
1128 ~__shared_count() noexcept
1129 {
1130 if (_M_pi != nullptr)
1131 _M_pi->_M_release();
1132 }
1133
1134 __shared_count(const __shared_count& __r) noexcept
1135 : _M_pi(__r._M_pi)
1136 {
1137 if (_M_pi != nullptr)
1138 _M_pi->_M_add_ref_copy();
1139 }
1140
1141 __shared_count&
1142 operator=(const __shared_count& __r) noexcept
1143 {
1144 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1145 if (__tmp != _M_pi)
1146 {
1147 if (__tmp != nullptr)
1148 __tmp->_M_add_ref_copy();
1149 if (_M_pi != nullptr)
1150 _M_pi->_M_release();
1151 _M_pi = __tmp;
1152 }
1153 return *this;
1154 }
1155
1156 void
1157 _M_swap(__shared_count& __r) noexcept
1158 {
1159 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1160 __r._M_pi = _M_pi;
1161 _M_pi = __tmp;
1162 }
1163
1164 long
1165 _M_get_use_count() const noexcept
1166 { return _M_pi ? _M_pi->_M_get_use_count() : 0; }
1167
1168 bool
1169 _M_unique() const noexcept
1170 { return this->_M_get_use_count() == 1; }
1171
1172 void*
1173 _M_get_deleter(const std::type_info& __ti) const noexcept
1174 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : nullptr; }
1175
1176 bool
1177 _M_less(const __shared_count& __rhs) const noexcept
1178 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1179
1180 bool
1181 _M_less(const __weak_count<_Lp>& __rhs) const noexcept
1182 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1183
1184#ifdef __glibcxx_smart_ptr_owner_equality // >= C++26
1185 size_t
1186 _M_owner_hash() const noexcept
1187 { return std::hash<_Sp_counted_base<_Lp>*>()(this->_M_pi); }
1188#endif
1189
1190 // Friend function injected into enclosing namespace and found by ADL
1191 friend inline bool
1192 operator==(const __shared_count& __a, const __shared_count& __b) noexcept
1193 { return __a._M_pi == __b._M_pi; }
1194
1195 private:
1196 friend class __weak_count<_Lp>;
1197#ifdef __glibcxx_atomic_shared_ptr
1198 template<typename> friend class _Sp_atomic;
1199#endif
1200#ifdef __glibcxx_out_ptr
1201 template<typename, typename, typename...> friend class out_ptr_t;
1202#endif
1203
1204 _Sp_counted_base<_Lp>* _M_pi;
1205 };
1206
1207
1208 template<_Lock_policy _Lp>
1209 class __weak_count
1210 {
1211 public:
1212 constexpr __weak_count() noexcept : _M_pi(nullptr)
1213 { }
1214
1215 __weak_count(const __shared_count<_Lp>& __r) noexcept
1216 : _M_pi(__r._M_pi)
1217 {
1218 if (_M_pi != nullptr)
1219 _M_pi->_M_weak_add_ref();
1220 }
1221
1222 __weak_count(const __weak_count& __r) noexcept
1223 : _M_pi(__r._M_pi)
1224 {
1225 if (_M_pi != nullptr)
1226 _M_pi->_M_weak_add_ref();
1227 }
1228
1229 __weak_count(__weak_count&& __r) noexcept
1230 : _M_pi(__r._M_pi)
1231 { __r._M_pi = nullptr; }
1232
1233 ~__weak_count() noexcept
1234 {
1235 if (_M_pi != nullptr)
1236 _M_pi->_M_weak_release();
1237 }
1238
1239 __weak_count&
1240 operator=(const __shared_count<_Lp>& __r) noexcept
1241 {
1242 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1243 if (__tmp != nullptr)
1244 __tmp->_M_weak_add_ref();
1245 if (_M_pi != nullptr)
1246 _M_pi->_M_weak_release();
1247 _M_pi = __tmp;
1248 return *this;
1249 }
1250
1251 __weak_count&
1252 operator=(const __weak_count& __r) noexcept
1253 {
1254 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1255 if (__tmp != nullptr)
1256 __tmp->_M_weak_add_ref();
1257 if (_M_pi != nullptr)
1258 _M_pi->_M_weak_release();
1259 _M_pi = __tmp;
1260 return *this;
1261 }
1262
1263 __weak_count&
1264 operator=(__weak_count&& __r) noexcept
1265 {
1266 if (_M_pi != nullptr)
1267 _M_pi->_M_weak_release();
1268 _M_pi = __r._M_pi;
1269 __r._M_pi = nullptr;
1270 return *this;
1271 }
1272
1273 void
1274 _M_swap(__weak_count& __r) noexcept
1275 {
1276 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1277 __r._M_pi = _M_pi;
1278 _M_pi = __tmp;
1279 }
1280
1281 long
1282 _M_get_use_count() const noexcept
1283 { return _M_pi != nullptr ? _M_pi->_M_get_use_count() : 0; }
1284
1285 bool
1286 _M_less(const __weak_count& __rhs) const noexcept
1287 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1288
1289 bool
1290 _M_less(const __shared_count<_Lp>& __rhs) const noexcept
1291 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1292
1293#ifdef __glibcxx_smart_ptr_owner_equality // >= C++26
1294 size_t
1295 _M_owner_hash() const noexcept
1296 { return std::hash<_Sp_counted_base<_Lp>*>()(this->_M_pi); }
1297#endif
1298
1299 // Friend function injected into enclosing namespace and found by ADL
1300 friend inline bool
1301 operator==(const __weak_count& __a, const __weak_count& __b) noexcept
1302 { return __a._M_pi == __b._M_pi; }
1303
1304 private:
1305 friend class __shared_count<_Lp>;
1306#ifdef __glibcxx_atomic_shared_ptr
1307 template<typename> friend class _Sp_atomic;
1308#endif
1309
1310 _Sp_counted_base<_Lp>* _M_pi;
1311 };
1312
1313 // Now that __weak_count is defined we can define this constructor:
1314 template<_Lock_policy _Lp>
1315 inline
1316 __shared_count<_Lp>::__shared_count(const __weak_count<_Lp>& __r)
1317 : _M_pi(__r._M_pi)
1318 {
1319 if (_M_pi == nullptr || !_M_pi->_M_add_ref_lock_nothrow())
1320 __throw_bad_weak_ptr();
1321 }
1322
1323 // Now that __weak_count is defined we can define this constructor:
1324 template<_Lock_policy _Lp>
1325 inline
1326 __shared_count<_Lp>::
1327 __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t) noexcept
1328 : _M_pi(__r._M_pi)
1329 {
1330 if (_M_pi && !_M_pi->_M_add_ref_lock_nothrow())
1331 _M_pi = nullptr;
1332 }
1333
1334 // Helper traits for shared_ptr of array:
1335
1336 // A pointer type Y* is said to be compatible with a pointer type T* when
1337 // either Y* is convertible to T* or Y is U[N] and T is U cv [].
1338 template<typename _Yp_ptr, typename _Tp_ptr>
1339 struct __sp_compatible_with
1340 : false_type
1341 { };
1342
1343 template<typename _Yp, typename _Tp>
1344 struct __sp_compatible_with<_Yp*, _Tp*>
1345 : is_convertible<_Yp*, _Tp*>::type
1346 { };
1347
1348 template<typename _Up, size_t _Nm>
1349 struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
1350 : true_type
1351 { };
1352
1353 template<typename _Up, size_t _Nm>
1354 struct __sp_compatible_with<_Up(*)[_Nm], const _Up(*)[]>
1355 : true_type
1356 { };
1357
1358 template<typename _Up, size_t _Nm>
1359 struct __sp_compatible_with<_Up(*)[_Nm], volatile _Up(*)[]>
1360 : true_type
1361 { };
1362
1363 template<typename _Up, size_t _Nm>
1364 struct __sp_compatible_with<_Up(*)[_Nm], const volatile _Up(*)[]>
1365 : true_type
1366 { };
1367
1368 // Test conversion from Y(*)[N] to U(*)[N] without forming invalid type Y[N].
1369 template<typename _Up, size_t _Nm, typename _Yp, typename = void>
1370 struct __sp_is_constructible_arrN
1371 : false_type
1372 { };
1373
1374 template<typename _Up, size_t _Nm, typename _Yp>
1375 struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
1376 : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
1377 { };
1378
1379 // Test conversion from Y(*)[] to U(*)[] without forming invalid type Y[].
1380 template<typename _Up, typename _Yp, typename = void>
1381 struct __sp_is_constructible_arr
1382 : false_type
1383 { };
1384
1385 template<typename _Up, typename _Yp>
1386 struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
1387 : is_convertible<_Yp(*)[], _Up(*)[]>::type
1388 { };
1389
1390 // Trait to check if shared_ptr<T> can be constructed from Y*.
1391 template<typename _Tp, typename _Yp>
1392 struct __sp_is_constructible;
1393
1394 // When T is U[N], Y(*)[N] shall be convertible to T*;
1395 template<typename _Up, size_t _Nm, typename _Yp>
1396 struct __sp_is_constructible<_Up[_Nm], _Yp>
1397 : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
1398 { };
1399
1400 // when T is U[], Y(*)[] shall be convertible to T*;
1401 template<typename _Up, typename _Yp>
1402 struct __sp_is_constructible<_Up[], _Yp>
1403 : __sp_is_constructible_arr<_Up, _Yp>::type
1404 { };
1405
1406 // otherwise, Y* shall be convertible to T*.
1407 template<typename _Tp, typename _Yp>
1408 struct __sp_is_constructible
1409 : is_convertible<_Yp*, _Tp*>::type
1410 { };
1411
1412
1413 template<typename _Tp>
1414 [[__gnu__::__always_inline__]]
1415 inline _Tp*
1416 __shared_ptr_deref(_Tp* __p)
1417 {
1418 __glibcxx_assert(__p != nullptr);
1419 return __p;
1420 }
1421
1422 // Define operator* and operator-> for shared_ptr<T>.
1423 template<typename _Tp, _Lock_policy _Lp,
1425 class __shared_ptr_access
1426 {
1427 public:
1428 using element_type = _Tp;
1429
1430 element_type&
1431 operator*() const noexcept
1432 { return *std::__shared_ptr_deref(_M_get()); }
1433
1434 element_type*
1435 operator->() const noexcept
1436 {
1437 _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
1438 return _M_get();
1439 }
1440
1441 private:
1442 element_type*
1443 _M_get() const noexcept
1444 { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
1445 };
1446
1447 // Define operator-> for shared_ptr<cv void>.
1448 template<typename _Tp, _Lock_policy _Lp>
1449 class __shared_ptr_access<_Tp, _Lp, false, true>
1450 {
1451 public:
1452 using element_type = _Tp;
1453
1454 element_type*
1455 operator->() const noexcept
1456 {
1457 auto __ptr = static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get();
1458 _GLIBCXX_DEBUG_PEDASSERT(__ptr != nullptr);
1459 return __ptr;
1460 }
1461 };
1462
1463 // Define operator[] for shared_ptr<T[]> and shared_ptr<T[N]>.
1464 template<typename _Tp, _Lock_policy _Lp>
1465 class __shared_ptr_access<_Tp, _Lp, true, false>
1466 {
1467 public:
1468 using element_type = typename remove_extent<_Tp>::type;
1469
1470#if __cplusplus <= 201402L
1471 [[__deprecated__("shared_ptr<T[]>::operator* is absent from C++17")]]
1472 element_type&
1473 operator*() const noexcept
1474 { return *std::__shared_ptr_deref(_M_get()); }
1475
1476 [[__deprecated__("shared_ptr<T[]>::operator-> is absent from C++17")]]
1477 element_type*
1478 operator->() const noexcept
1479 {
1480 _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
1481 return _M_get();
1482 }
1483#endif
1484
1485#pragma GCC diagnostic push
1486#pragma GCC diagnostic ignored "-Wc++17-extensions"
1487 element_type&
1488 operator[](ptrdiff_t __i) const noexcept
1489 {
1490 if constexpr (extent<_Tp>::value)
1491 __glibcxx_assert(__i < extent<_Tp>::value);
1492 return std::__shared_ptr_deref(_M_get())[__i];
1493 }
1494#pragma GCC diagnostic pop
1495
1496 private:
1497 element_type*
1498 _M_get() const noexcept
1499 { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
1500 };
1501
1502 template<typename _Tp, _Lock_policy _Lp>
1503 class __shared_ptr
1504 : public __shared_ptr_access<_Tp, _Lp>
1505 {
1506 public:
1507 using element_type = typename remove_extent<_Tp>::type;
1508
1509 private:
1510 // Constraint for taking ownership of a pointer of type _Yp*:
1511 template<typename _Yp>
1512 using _SafeConv
1513 = typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
1514
1515 // Constraint for construction from shared_ptr and weak_ptr:
1516 template<typename _Yp, typename _Res = void>
1517 using _Compatible = typename
1518 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1519
1520 // Constraint for assignment from shared_ptr and weak_ptr:
1521 template<typename _Yp>
1522 using _Assignable = _Compatible<_Yp, __shared_ptr&>;
1523
1524 // Constraint for construction from unique_ptr:
1525 template<typename _Yp, typename _Del, typename _Res = void,
1526 typename _Ptr = typename unique_ptr<_Yp, _Del>::pointer>
1527 using _UniqCompatible = __enable_if_t<__and_<
1528 __sp_compatible_with<_Yp*, _Tp*>,
1529 is_convertible<_Ptr, element_type*>,
1530 is_move_constructible<_Del>
1531 >::value, _Res>;
1532
1533 // Constraint for assignment from unique_ptr:
1534 template<typename _Yp, typename _Del>
1535 using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
1536
1537 public:
1538
1539#if __cplusplus > 201402L
1540 using weak_type = __weak_ptr<_Tp, _Lp>;
1541#endif
1542
1543 constexpr __shared_ptr() noexcept
1544 : _M_ptr(0), _M_refcount()
1545 { }
1546
1547 template<typename _Yp, typename = _SafeConv<_Yp>>
1548 explicit
1549 __shared_ptr(_Yp* __p)
1550 : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
1551 {
1552 static_assert( !is_void<_Yp>::value, "incomplete type" );
1553 static_assert( sizeof(_Yp) > 0, "incomplete type" );
1554 _M_enable_shared_from_this_with(__p);
1555 }
1556
1557 template<typename _Yp, typename _Deleter, typename = _SafeConv<_Yp>>
1558 __shared_ptr(_Yp* __p, _Deleter __d)
1559 : _M_ptr(__p), _M_refcount(__p, std::move(__d))
1560 {
1561 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1562 "deleter expression d(p) is well-formed");
1563 _M_enable_shared_from_this_with(__p);
1564 }
1565
1566 template<typename _Yp, typename _Deleter, typename _Alloc,
1567 typename = _SafeConv<_Yp>>
1568 __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
1569 : _M_ptr(__p), _M_refcount(__p, std::move(__d), std::move(__a))
1570 {
1571 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1572 "deleter expression d(p) is well-formed");
1573 _M_enable_shared_from_this_with(__p);
1574 }
1575
1576 template<typename _Deleter>
1577 __shared_ptr(nullptr_t __p, _Deleter __d)
1578 : _M_ptr(0), _M_refcount(__p, std::move(__d))
1579 { }
1580
1581 template<typename _Deleter, typename _Alloc>
1582 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
1583 : _M_ptr(0), _M_refcount(__p, std::move(__d), std::move(__a))
1584 { }
1585
1586 // Aliasing constructor
1587 template<typename _Yp>
1588 __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r,
1589 element_type* __p) noexcept
1590 : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
1591 { }
1592
1593 // Aliasing constructor
1594 template<typename _Yp>
1595 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r,
1596 element_type* __p) noexcept
1597 : _M_ptr(__p), _M_refcount()
1598 {
1599 _M_refcount._M_swap(__r._M_refcount);
1600 __r._M_ptr = nullptr;
1601 }
1602
1603 __shared_ptr(const __shared_ptr&) noexcept = default;
1604 __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
1605 ~__shared_ptr() = default;
1606
1607 template<typename _Yp, typename = _Compatible<_Yp>>
1608 __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1609 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1610 { }
1611
1612 __shared_ptr(__shared_ptr&& __r) noexcept
1613 : _M_ptr(__r._M_ptr), _M_refcount()
1614 {
1615 _M_refcount._M_swap(__r._M_refcount);
1616 __r._M_ptr = nullptr;
1617 }
1618
1619 template<typename _Yp, typename = _Compatible<_Yp>>
1620 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1621 : _M_ptr(__r._M_ptr), _M_refcount()
1622 {
1623 _M_refcount._M_swap(__r._M_refcount);
1624 __r._M_ptr = nullptr;
1625 }
1626
1627 template<typename _Yp, typename = _Compatible<_Yp>>
1628 explicit __shared_ptr(const __weak_ptr<_Yp, _Lp>& __r)
1629 : _M_refcount(__r._M_refcount) // may throw
1630 {
1631 // It is now safe to copy __r._M_ptr, as
1632 // _M_refcount(__r._M_refcount) did not throw.
1633 _M_ptr = __r._M_ptr;
1634 }
1635
1636 // If an exception is thrown this constructor has no effect.
1637 template<typename _Yp, typename _Del,
1638 typename = _UniqCompatible<_Yp, _Del>>
1639 __shared_ptr(unique_ptr<_Yp, _Del>&& __r)
1640 : _M_ptr(__r.get()), _M_refcount()
1641 {
1642 auto __raw = std::__to_address(__r.get());
1643 _M_refcount = __shared_count<_Lp>(std::move(__r));
1644 _M_enable_shared_from_this_with(__raw);
1645 }
1646
1647#if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
1648 protected:
1649 // If an exception is thrown this constructor has no effect.
1650 template<typename _Tp1, typename _Del,
1651 typename enable_if<__and_<
1652 __not_<is_array<_Tp>>, is_array<_Tp1>,
1653 is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
1654 >::value, bool>::type = true>
1655 __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
1656 : _M_ptr(__r.get()), _M_refcount()
1657 {
1658 auto __raw = std::__to_address(__r.get());
1659 _M_refcount = __shared_count<_Lp>(std::move(__r));
1660 _M_enable_shared_from_this_with(__raw);
1661 }
1662 public:
1663#endif
1664
1665#if _GLIBCXX_USE_DEPRECATED
1666#pragma GCC diagnostic push
1667#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1668 // Postcondition: use_count() == 1 and __r.get() == 0
1669 template<typename _Yp, typename = _Compatible<_Yp>>
1670 __shared_ptr(auto_ptr<_Yp>&& __r);
1671#pragma GCC diagnostic pop
1672#endif
1673
1674 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
1675
1676 template<typename _Yp>
1677 _Assignable<_Yp>
1678 operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1679 {
1680 _M_ptr = __r._M_ptr;
1681 _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
1682 return *this;
1683 }
1684
1685#if _GLIBCXX_USE_DEPRECATED
1686#pragma GCC diagnostic push
1687#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1688 template<typename _Yp>
1689 _Assignable<_Yp>
1690 operator=(auto_ptr<_Yp>&& __r)
1691 {
1692 __shared_ptr(std::move(__r)).swap(*this);
1693 return *this;
1694 }
1695#pragma GCC diagnostic pop
1696#endif
1697
1698 __shared_ptr&
1699 operator=(__shared_ptr&& __r) noexcept
1700 {
1701 __shared_ptr(std::move(__r)).swap(*this);
1702 return *this;
1703 }
1704
1705 template<class _Yp>
1706 _Assignable<_Yp>
1707 operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1708 {
1709 __shared_ptr(std::move(__r)).swap(*this);
1710 return *this;
1711 }
1712
1713 template<typename _Yp, typename _Del>
1714 _UniqAssignable<_Yp, _Del>
1715 operator=(unique_ptr<_Yp, _Del>&& __r)
1716 {
1717 __shared_ptr(std::move(__r)).swap(*this);
1718 return *this;
1719 }
1720
1721 void
1722 reset() noexcept
1723 { __shared_ptr().swap(*this); }
1724
1725 template<typename _Yp>
1726 _SafeConv<_Yp>
1727 reset(_Yp* __p) // _Yp must be complete.
1728 {
1729 // Catch self-reset errors.
1730 __glibcxx_assert(__p == nullptr || __p != _M_ptr);
1731 __shared_ptr(__p).swap(*this);
1732 }
1733
1734 template<typename _Yp, typename _Deleter>
1735 _SafeConv<_Yp>
1736 reset(_Yp* __p, _Deleter __d)
1737 { __shared_ptr(__p, std::move(__d)).swap(*this); }
1738
1739 template<typename _Yp, typename _Deleter, typename _Alloc>
1740 _SafeConv<_Yp>
1741 reset(_Yp* __p, _Deleter __d, _Alloc __a)
1742 { __shared_ptr(__p, std::move(__d), std::move(__a)).swap(*this); }
1743
1744 /// Return the stored pointer.
1745 element_type*
1746 get() const noexcept
1747 { return _M_ptr; }
1748
1749 /// Return true if the stored pointer is not null.
1750 explicit operator bool() const noexcept
1751 { return _M_ptr != nullptr; }
1752
1753 /// Return true if use_count() == 1.
1754 bool
1755 unique() const noexcept
1756 { return _M_refcount._M_unique(); }
1757
1758 /// If *this owns a pointer, return the number of owners, otherwise zero.
1759 long
1760 use_count() const noexcept
1761 { return _M_refcount._M_get_use_count(); }
1762
1763 /// Exchange both the owned pointer and the stored pointer.
1764 void
1765 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
1766 {
1767 std::swap(_M_ptr, __other._M_ptr);
1768 _M_refcount._M_swap(__other._M_refcount);
1769 }
1770
1771 /** @brief Define an ordering based on ownership.
1772 *
1773 * This function defines a strict weak ordering between two shared_ptr
1774 * or weak_ptr objects, such that one object is less than the other
1775 * unless they share ownership of the same pointer, or are both empty.
1776 * @{
1777 */
1778 template<typename _Tp1>
1779 bool
1780 owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const noexcept
1781 { return _M_refcount._M_less(__rhs._M_refcount); }
1782
1783 template<typename _Tp1>
1784 bool
1785 owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const noexcept
1786 { return _M_refcount._M_less(__rhs._M_refcount); }
1787 /// @}
1788
1789#ifdef __glibcxx_smart_ptr_owner_equality // >= C++26
1790 size_t owner_hash() const noexcept { return _M_refcount._M_owner_hash(); }
1791
1792 template<typename _Tp1>
1793 bool
1794 owner_equal(__shared_ptr<_Tp1, _Lp> const& __rhs) const noexcept
1795 { return _M_refcount == __rhs._M_refcount; }
1796
1797 template<typename _Tp1>
1798 bool
1799 owner_equal(__weak_ptr<_Tp1, _Lp> const& __rhs) const noexcept
1800 { return _M_refcount == __rhs._M_refcount; }
1801#endif
1802
1803 protected:
1804 // This constructor is non-standard, it is used by allocate_shared.
1805 template<typename _Alloc, typename... _Args>
1806 __shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
1807 : _M_ptr(), _M_refcount(_M_ptr, __tag, std::forward<_Args>(__args)...)
1808 { _M_enable_shared_from_this_with(_M_ptr); }
1809
1810 template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
1811 typename... _Args>
1812 friend __shared_ptr<_Tp1, _Lp1>
1813 __allocate_shared(const _Alloc& __a, _Args&&... __args);
1814
1815#if __glibcxx_shared_ptr_arrays >= 201707L // C++ >= 20 && HOSTED
1816 // This constructor is non-standard, it is used by allocate_shared<T[]>.
1817 template<typename _Alloc, typename _Init = const remove_extent_t<_Tp>*>
1818 __shared_ptr(const _Sp_counted_array_base<_Alloc>& __a,
1819 _Init __init = nullptr)
1820 : _M_ptr(), _M_refcount(_M_ptr, __a, __init)
1821 { }
1822#endif
1823
1824 // This constructor is used by __weak_ptr::lock() and
1825 // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t).
1826 __shared_ptr(const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t) noexcept
1827 : _M_refcount(__r._M_refcount, std::nothrow)
1828 {
1829 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr : nullptr;
1830 }
1831
1832 friend class __weak_ptr<_Tp, _Lp>;
1833
1834 private:
1835
1836 template<typename _Yp>
1837 using __esft_base_t = decltype(__enable_shared_from_this_base(
1838 std::declval<const __shared_count<_Lp>&>(),
1840
1841 // Detect an accessible and unambiguous enable_shared_from_this base.
1842 template<typename _Yp, typename = void>
1843 struct __has_esft_base
1844 : false_type { };
1845
1846 template<typename _Yp>
1847 struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
1848 : __not_<is_array<_Tp>> { }; // No enable shared_from_this for arrays
1849
1850 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1851 typename enable_if<__has_esft_base<_Yp2>::value>::type
1852 _M_enable_shared_from_this_with(_Yp* __p) noexcept
1853 {
1854 if (auto __base = __enable_shared_from_this_base(_M_refcount, __p))
1855 __base->_M_weak_assign(const_cast<_Yp2*>(__p), _M_refcount);
1856 }
1857
1858 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1859 typename enable_if<!__has_esft_base<_Yp2>::value>::type
1860 _M_enable_shared_from_this_with(_Yp*) noexcept
1861 { }
1862
1863 void*
1864 _M_get_deleter(const std::type_info& __ti) const noexcept
1865 { return _M_refcount._M_get_deleter(__ti); }
1866
1867 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1868 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1869
1870 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
1871 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1872
1873 template<typename _Del, typename _Tp1>
1874 friend _Del* get_deleter(const shared_ptr<_Tp1>&) noexcept;
1875
1876#ifdef __glibcxx_atomic_shared_ptr
1877 friend _Sp_atomic<shared_ptr<_Tp>>;
1878#endif
1879#ifdef __glibcxx_out_ptr
1880 template<typename, typename, typename...> friend class out_ptr_t;
1881#endif
1882
1883 element_type* _M_ptr; // Contained pointer.
1884 __shared_count<_Lp> _M_refcount; // Reference counter.
1885 };
1886
1887
1888 // 20.7.2.2.7 shared_ptr comparisons
1889 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1890 inline bool
1891 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
1892 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1893 { return __a.get() == __b.get(); }
1894
1895 template<typename _Tp, _Lock_policy _Lp>
1896 inline bool
1897 operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1898 { return !__a; }
1899
1900#ifdef __cpp_lib_three_way_comparison
1901 template<typename _Tp, typename _Up, _Lock_policy _Lp>
1902 inline strong_ordering
1903 operator<=>(const __shared_ptr<_Tp, _Lp>& __a,
1904 const __shared_ptr<_Up, _Lp>& __b) noexcept
1905 { return compare_three_way()(__a.get(), __b.get()); }
1906
1907 template<typename _Tp, _Lock_policy _Lp>
1908 inline strong_ordering
1909 operator<=>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1910 {
1911 using pointer = typename __shared_ptr<_Tp, _Lp>::element_type*;
1912 return compare_three_way()(__a.get(), static_cast<pointer>(nullptr));
1913 }
1914#else
1915 template<typename _Tp, _Lock_policy _Lp>
1916 inline bool
1917 operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1918 { return !__a; }
1919
1920 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1921 inline bool
1922 operator!=(const __shared_ptr<_Tp1, _Lp>& __a,
1923 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1924 { return __a.get() != __b.get(); }
1925
1926 template<typename _Tp, _Lock_policy _Lp>
1927 inline bool
1928 operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1929 { return (bool)__a; }
1930
1931 template<typename _Tp, _Lock_policy _Lp>
1932 inline bool
1933 operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1934 { return (bool)__a; }
1935
1936 template<typename _Tp, typename _Up, _Lock_policy _Lp>
1937 inline bool
1938 operator<(const __shared_ptr<_Tp, _Lp>& __a,
1939 const __shared_ptr<_Up, _Lp>& __b) noexcept
1940 {
1941 using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1942 using _Up_elt = typename __shared_ptr<_Up, _Lp>::element_type;
1943 using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type;
1944 return less<_Vp>()(__a.get(), __b.get());
1945 }
1946
1947 template<typename _Tp, _Lock_policy _Lp>
1948 inline bool
1949 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1950 {
1951 using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1952 return less<_Tp_elt*>()(__a.get(), nullptr);
1953 }
1954
1955 template<typename _Tp, _Lock_policy _Lp>
1956 inline bool
1957 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1958 {
1959 using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1960 return less<_Tp_elt*>()(nullptr, __a.get());
1961 }
1962
1963 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1964 inline bool
1965 operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1966 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1967 { return !(__b < __a); }
1968
1969 template<typename _Tp, _Lock_policy _Lp>
1970 inline bool
1971 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1972 { return !(nullptr < __a); }
1973
1974 template<typename _Tp, _Lock_policy _Lp>
1975 inline bool
1976 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1977 { return !(__a < nullptr); }
1978
1979 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1980 inline bool
1981 operator>(const __shared_ptr<_Tp1, _Lp>& __a,
1982 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1983 { return (__b < __a); }
1984
1985 template<typename _Tp, _Lock_policy _Lp>
1986 inline bool
1987 operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1988 { return nullptr < __a; }
1989
1990 template<typename _Tp, _Lock_policy _Lp>
1991 inline bool
1992 operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1993 { return __a < nullptr; }
1994
1995 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1996 inline bool
1997 operator>=(const __shared_ptr<_Tp1, _Lp>& __a,
1998 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1999 { return !(__a < __b); }
2000
2001 template<typename _Tp, _Lock_policy _Lp>
2002 inline bool
2003 operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
2004 { return !(__a < nullptr); }
2005
2006 template<typename _Tp, _Lock_policy _Lp>
2007 inline bool
2008 operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
2009 { return !(nullptr < __a); }
2010#endif // three-way comparison
2011
2012 // 20.7.2.2.8 shared_ptr specialized algorithms.
2013 template<typename _Tp, _Lock_policy _Lp>
2014 inline void
2015 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
2016 { __a.swap(__b); }
2017
2018 // 20.7.2.2.9 shared_ptr casts
2019
2020 // The seemingly equivalent code:
2021 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
2022 // will eventually result in undefined behaviour, attempting to
2023 // delete the same object twice.
2024 /// static_pointer_cast
2025 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
2026 inline __shared_ptr<_Tp, _Lp>
2027 static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
2028 {
2029 using _Sp = __shared_ptr<_Tp, _Lp>;
2030 return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
2031 }
2032
2033 // The seemingly equivalent code:
2034 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
2035 // will eventually result in undefined behaviour, attempting to
2036 // delete the same object twice.
2037 /// const_pointer_cast
2038 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
2039 inline __shared_ptr<_Tp, _Lp>
2040 const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
2041 {
2042 using _Sp = __shared_ptr<_Tp, _Lp>;
2043 return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
2044 }
2045
2046 // The seemingly equivalent code:
2047 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
2048 // will eventually result in undefined behaviour, attempting to
2049 // delete the same object twice.
2050 /// dynamic_pointer_cast
2051 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
2052 inline __shared_ptr<_Tp, _Lp>
2053 dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
2054 {
2055 using _Sp = __shared_ptr<_Tp, _Lp>;
2056 if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
2057 return _Sp(__r, __p);
2058 return _Sp();
2059 }
2060
2061#if __cplusplus > 201402L
2062 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
2063 inline __shared_ptr<_Tp, _Lp>
2064 reinterpret_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
2065 {
2066 using _Sp = __shared_ptr<_Tp, _Lp>;
2067 return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
2068 }
2069#endif
2070
2071 template<typename _Tp, _Lock_policy _Lp>
2072 class __weak_ptr
2073 {
2074 public:
2075 using element_type = typename remove_extent<_Tp>::type;
2076
2077 private:
2078 template<typename _Yp, typename _Res = void>
2079 using _Compatible = typename
2081
2082 // Constraint for assignment from shared_ptr and weak_ptr:
2083 template<typename _Yp>
2084 using _Assignable = _Compatible<_Yp, __weak_ptr&>;
2085
2086#pragma GCC diagnostic push
2087#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
2088 // Helper for construction/assignment:
2089 template<typename _Yp>
2090 static element_type*
2091 _S_safe_upcast(const __weak_ptr<_Yp, _Lp>& __r)
2092 {
2093 // We know that _Yp and _Tp are compatible, that is, either
2094 // _Yp* is convertible to _Tp* or _Yp is U[N] and _Tp is U cv [].
2095
2096 // If _Yp is the same as _Tp after removing extents and cv
2097 // qualifications, there's no pointer adjustments to do. This
2098 // also allows us to support incomplete types.
2099 using _At = typename remove_cv<typename remove_extent<_Tp>::type>::type;
2100 using _Bt = typename remove_cv<typename remove_extent<_Yp>::type>::type;
2101 if constexpr (is_same<_At, _Bt>::value)
2102 return __r._M_ptr;
2103 // If they're not the same type, but they're both scalars,
2104 // we again don't need any adjustment. This allows us to support e.g.
2105 // pointers to a differently cv qualified type X.
2106 else if constexpr (__and_<is_scalar<_At>, is_scalar<_Bt>>::value)
2107 return __r._M_ptr;
2108#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_is_virtual_base_of)
2109 // If _Tp is not a virtual base class of _Yp, the pointer
2110 // conversion does not require dereferencing __r._M_ptr; just
2111 // rely on the implicit conversion.
2112 else if constexpr (!__builtin_is_virtual_base_of(_Tp, _Yp))
2113 return __r._M_ptr;
2114#endif
2115 // Expensive path; must lock() and do the pointer conversion while
2116 // a shared_ptr keeps the pointee alive (because we may need
2117 // to dereference).
2118 else
2119 return __r.lock().get();
2120 }
2121#pragma GCC diagnostic pop
2122
2123 public:
2124 constexpr __weak_ptr() noexcept
2125 : _M_ptr(nullptr), _M_refcount()
2126 { }
2127
2128 __weak_ptr(const __weak_ptr&) noexcept = default;
2129
2130 ~__weak_ptr() = default;
2131
2132 // The "obvious" converting constructor implementation:
2133 //
2134 // template<typename _Tp1>
2135 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
2136 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
2137 // { }
2138 //
2139 // has a serious problem.
2140 //
2141 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
2142 // conversion may require access to *__r._M_ptr (virtual inheritance).
2143 //
2144 // It is not possible to avoid spurious access violations since
2145 // in multithreaded programs __r._M_ptr may be invalidated at any point.
2146 template<typename _Yp, typename = _Compatible<_Yp>>
2147 __weak_ptr(const __weak_ptr<_Yp, _Lp>& __r) noexcept
2148 : _M_ptr(_S_safe_upcast(__r)), _M_refcount(__r._M_refcount)
2149 { }
2150
2151 template<typename _Yp, typename = _Compatible<_Yp>>
2152 __weak_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
2153 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
2154 { }
2155
2156 __weak_ptr(__weak_ptr&& __r) noexcept
2157 : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount))
2158 { __r._M_ptr = nullptr; }
2159
2160 template<typename _Yp, typename = _Compatible<_Yp>>
2161 __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
2162 : _M_ptr(_S_safe_upcast(__r)), _M_refcount(std::move(__r._M_refcount))
2163 { __r._M_ptr = nullptr; }
2164
2165 __weak_ptr&
2166 operator=(const __weak_ptr& __r) noexcept = default;
2167
2168 template<typename _Yp>
2169 _Assignable<_Yp>
2170 operator=(const __weak_ptr<_Yp, _Lp>& __r) noexcept
2171 {
2172 _M_ptr = _S_safe_upcast(__r);
2173 _M_refcount = __r._M_refcount;
2174 return *this;
2175 }
2176
2177 template<typename _Yp>
2178 _Assignable<_Yp>
2179 operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
2180 {
2181 _M_ptr = __r._M_ptr;
2182 _M_refcount = __r._M_refcount;
2183 return *this;
2184 }
2185
2186 __weak_ptr&
2187 operator=(__weak_ptr&& __r) noexcept
2188 {
2189 __weak_ptr(std::move(__r)).swap(*this);
2190 return *this;
2191 }
2192
2193 template<typename _Yp>
2194 _Assignable<_Yp>
2195 operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept
2196 {
2197 _M_ptr = _S_safe_upcast(__r);
2198 _M_refcount = std::move(__r._M_refcount);
2199 __r._M_ptr = nullptr;
2200 return *this;
2201 }
2202
2203 __shared_ptr<_Tp, _Lp>
2204 lock() const noexcept
2205 { return __shared_ptr<_Tp, _Lp>(*this, std::nothrow); }
2206
2207 long
2208 use_count() const noexcept
2209 { return _M_refcount._M_get_use_count(); }
2210
2211 bool
2212 expired() const noexcept
2213 { return _M_refcount._M_get_use_count() == 0; }
2214
2215 template<typename _Tp1>
2216 bool
2217 owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const noexcept
2218 { return _M_refcount._M_less(__rhs._M_refcount); }
2219
2220 template<typename _Tp1>
2221 bool
2222 owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const noexcept
2223 { return _M_refcount._M_less(__rhs._M_refcount); }
2224
2225#ifdef __glibcxx_smart_ptr_owner_equality // >= C++26
2226 size_t owner_hash() const noexcept { return _M_refcount._M_owner_hash(); }
2227
2228 template<typename _Tp1>
2229 bool
2230 owner_equal(const __shared_ptr<_Tp1, _Lp> & __rhs) const noexcept
2231 { return _M_refcount == __rhs._M_refcount; }
2232
2233 template<typename _Tp1>
2234 bool
2235 owner_equal(const __weak_ptr<_Tp1, _Lp> & __rhs) const noexcept
2236 { return _M_refcount == __rhs._M_refcount; }
2237#endif
2238
2239 void
2240 reset() noexcept
2241 { __weak_ptr().swap(*this); }
2242
2243 void
2244 swap(__weak_ptr& __s) noexcept
2245 {
2246 std::swap(_M_ptr, __s._M_ptr);
2247 _M_refcount._M_swap(__s._M_refcount);
2248 }
2249
2250 private:
2251 // Used by __enable_shared_from_this.
2252 void
2253 _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept
2254 {
2255 if (use_count() == 0)
2256 {
2257 _M_ptr = __ptr;
2258 _M_refcount = __refcount;
2259 }
2260 }
2261
2262 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
2263 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
2264 friend class __enable_shared_from_this<_Tp, _Lp>;
2265 friend class enable_shared_from_this<_Tp>;
2266#ifdef __glibcxx_atomic_shared_ptr
2267 friend _Sp_atomic<weak_ptr<_Tp>>;
2268#endif
2269
2270 element_type* _M_ptr; // Contained pointer.
2271 __weak_count<_Lp> _M_refcount; // Reference counter.
2272 };
2273
2274 // 20.7.2.3.6 weak_ptr specialized algorithms.
2275 template<typename _Tp, _Lock_policy _Lp>
2276 inline void
2277 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
2278 { __a.swap(__b); }
2279
2280#pragma GCC diagnostic push
2281#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2282 template<typename _Tp, typename _Tp1>
2283 struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
2284 {
2285 bool
2286 operator()(const _Tp& __lhs, const _Tp& __rhs) const noexcept
2287 { return __lhs.owner_before(__rhs); }
2288
2289 bool
2290 operator()(const _Tp& __lhs, const _Tp1& __rhs) const noexcept
2291 { return __lhs.owner_before(__rhs); }
2292
2293 bool
2294 operator()(const _Tp1& __lhs, const _Tp& __rhs) const noexcept
2295 { return __lhs.owner_before(__rhs); }
2296 };
2297#pragma GCC diagnostic pop
2298
2299 template<>
2300 struct _Sp_owner_less<void, void>
2301 {
2302 template<typename _Tp, typename _Up>
2303 auto
2304 operator()(const _Tp& __lhs, const _Up& __rhs) const noexcept
2305 -> decltype(__lhs.owner_before(__rhs))
2306 { return __lhs.owner_before(__rhs); }
2307
2308 using is_transparent = void;
2309 };
2310
2311 template<typename _Tp, _Lock_policy _Lp>
2312 struct owner_less<__shared_ptr<_Tp, _Lp>>
2313 : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
2314 { };
2315
2316 template<typename _Tp, _Lock_policy _Lp>
2317 struct owner_less<__weak_ptr<_Tp, _Lp>>
2318 : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
2319 { };
2320
2321
2322 template<typename _Tp, _Lock_policy _Lp>
2323 class __enable_shared_from_this
2324 {
2325 protected:
2326 constexpr __enable_shared_from_this() noexcept { }
2327
2328 __enable_shared_from_this(const __enable_shared_from_this&) noexcept { }
2329
2330 __enable_shared_from_this&
2331 operator=(const __enable_shared_from_this&) noexcept
2332 { return *this; }
2333
2334 ~__enable_shared_from_this() { }
2335
2336 public:
2337 __shared_ptr<_Tp, _Lp>
2338 shared_from_this()
2339 { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
2340
2341 __shared_ptr<const _Tp, _Lp>
2342 shared_from_this() const
2343 { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
2344
2345#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
2346 __weak_ptr<_Tp, _Lp>
2347 weak_from_this() noexcept
2348 { return this->_M_weak_this; }
2349
2350 __weak_ptr<const _Tp, _Lp>
2351 weak_from_this() const noexcept
2352 { return this->_M_weak_this; }
2353#endif
2354
2355 private:
2356 template<typename _Tp1>
2357 void
2358 _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept
2359 { _M_weak_this._M_assign(__p, __n); }
2360
2361 friend const __enable_shared_from_this*
2362 __enable_shared_from_this_base(const __shared_count<_Lp>&,
2363 const __enable_shared_from_this* __p)
2364 { return __p; }
2365
2366 template<typename, _Lock_policy>
2367 friend class __shared_ptr;
2368
2369 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
2370 };
2371
2372 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy,
2373 typename _Alloc, typename... _Args>
2374 inline __shared_ptr<_Tp, _Lp>
2375 __allocate_shared(const _Alloc& __a, _Args&&... __args)
2376 {
2377 static_assert(!is_array<_Tp>::value, "make_shared<T[]> not supported");
2378
2379 return __shared_ptr<_Tp, _Lp>(_Sp_alloc_shared_tag<_Alloc>{__a},
2380 std::forward<_Args>(__args)...);
2381 }
2382
2383 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy,
2384 typename... _Args>
2385 inline __shared_ptr<_Tp, _Lp>
2386 __make_shared(_Args&&... __args)
2387 {
2388 typedef typename std::remove_const<_Tp>::type _Tp_nc;
2389 return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
2390 std::forward<_Args>(__args)...);
2391 }
2392
2393 /// std::hash specialization for __shared_ptr.
2394 template<typename _Tp, _Lock_policy _Lp>
2395 struct hash<__shared_ptr<_Tp, _Lp>>
2396 : public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
2397 {
2398 size_t
2399 operator()(const __shared_ptr<_Tp, _Lp>& __s) const noexcept
2400 {
2402 __s.get());
2403 }
2404 };
2405
2406_GLIBCXX_END_NAMESPACE_VERSION
2407} // namespace
2408
2409#endif // _SHARED_PTR_BASE_H
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
Definition complex:434
constexpr _ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __count)
Default-initializes objects in the range [first,first+count).
void * align(size_t __align, size_t __size, void *&__ptr, size_t &__space) noexcept
Fit aligned storage in buffer.
Definition align.h:60
constexpr _Tp * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.
Definition ptr_traits.h:234
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:119
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:122
auto declval() noexcept -> decltype(__declval< _Tp >(0))
Definition type_traits:2718
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
void lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
Generic lock.
Definition mutex:686
ISO C++ entities toplevel namespace is std.
__shared_ptr< _Tp, _Lp > dynamic_pointer_cast(const __shared_ptr< _Tp1, _Lp > &__r) noexcept
dynamic_pointer_cast
__shared_ptr< _Tp, _Lp > static_pointer_cast(const __shared_ptr< _Tp1, _Lp > &__r) noexcept
static_pointer_cast
__shared_ptr< _Tp, _Lp > const_pointer_cast(const __shared_ptr< _Tp1, _Lp > &__r) noexcept
const_pointer_cast
constexpr _Iterator __base(_Iterator __it)
Primary class template hash.
Define a member typedef type only if a boolean constant is true.
Definition type_traits:137
remove_cv
Definition type_traits:1796
is_void
Definition type_traits:331
is_array
Definition type_traits:608
is_scalar
Definition type_traits:878
common_type
Definition type_traits:2577
static constexpr void construct(_Alloc &__a, _Tp *__p, _Args &&... __args) noexcept(_S_nothrow_construct< _Tp, _Args... >())
Construct an object of type _Tp.
static constexpr void destroy(_Alloc &__a, _Tp *__p) noexcept(_S_nothrow_destroy< _Tp >())
Destroy an object of type _Tp.
Base class for all library exceptions.
Definition exception.h:62
Primary template owner_less.
Base class allowing use of the member function shared_from_this.
A simple smart pointer providing strict ownership semantics.
Definition auto_ptr.h:94
Exception possibly thrown by shared_ptr.
virtual char const * what() const noexcept
One of the comparison functors.