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#if ! __has_cpp_attribute(__no_unique_address__)
517#error "support for [[__no_unique_address__]] attribute is required"
518#endif
519
520#if ! _GLIBCXX_INLINE_VERSION
521 // N.B. unconditionally applying the attribute could change layout for
522 // final types, which currently cannot use EBO so have a unique address.
523 template<typename _Tp, bool = !__is_final(_Tp) && __is_empty(_Tp)>
524 struct _Sp_ebo_helper;
525#else
526 template<typename _Tp, bool = true>
527 struct _Sp_ebo_helper;
528#endif
529
530 /// Specialization using [[no_unique_address]].
531 template<typename _Tp>
532 struct _Sp_ebo_helper<_Tp, true>
533 {
534 [[__no_unique_address__]] _Tp _M_obj;
535 };
536
537#if ! _GLIBCXX_INLINE_VERSION
538 /// Specialization not using [[no_unique_address]].
539 template<typename _Tp>
540 struct _Sp_ebo_helper<_Tp, false>
541 {
542 _Tp _M_obj;
543 };
544#endif
545
546 // Support for custom deleter and/or allocator
547 template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
548 class _Sp_counted_deleter final : public _Sp_counted_base<_Lp>
549 {
550 public:
551 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
552
553 // __d(__p) must not throw.
554 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
555 : _M_del{std::move(__d)}, _M_alloc{}, _M_ptr(__p) { }
556
557 // __d(__p) must not throw.
558 _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept
559 : _M_del{std::move(__d)}, _M_alloc{__a}, _M_ptr(__p) { }
560
561#pragma GCC diagnostic push // PR tree-optimization/122197
562#pragma GCC diagnostic ignored "-Wfree-nonheap-object"
563 template<typename> class auto_ptr;
564 ~_Sp_counted_deleter() noexcept { }
565#pragma GCC diagnostic pop
566
567 virtual void
568 _M_dispose() noexcept
569 { _M_del._M_obj(_M_ptr); }
570
571 virtual void
572 _M_destroy() noexcept
573 {
574 __allocator_type __a(_M_alloc._M_obj);
575 __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
576 this->~_Sp_counted_deleter();
577 }
578
579 virtual void*
580 _M_get_deleter(const type_info& __ti [[__gnu__::__unused__]]) noexcept
581 {
582#if __cpp_rtti
583 // _GLIBCXX_RESOLVE_LIB_DEFECTS
584 // 2400. shared_ptr's get_deleter() should use addressof()
585 if (__ti == typeid(_Deleter))
586 return std::__addressof(_M_del._M_obj);
587#endif
588 return nullptr;
589 }
590
591 private:
592#ifdef __glibcxx_out_ptr
593 template<typename, typename, typename...> friend class out_ptr_t;
594#endif
595
596 [[__no_unique_address__]] _Sp_ebo_helper<_Deleter> _M_del;
597 [[__no_unique_address__]] _Sp_ebo_helper<_Alloc> _M_alloc;
598 _Ptr _M_ptr;
599 };
600
601 // helpers for make_shared / allocate_shared
602
603 struct _Sp_make_shared_tag
604 {
605 private:
606 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
607 friend class _Sp_counted_ptr_inplace;
608
609 static const type_info&
610 _S_ti() noexcept _GLIBCXX_VISIBILITY(default)
611 {
612 alignas(type_info) static constexpr char __tag[sizeof(type_info)] = { };
613 return reinterpret_cast<const type_info&>(__tag);
614 }
615
616 static bool _S_eq(const type_info&) noexcept;
617 };
618
619 template<typename _Alloc>
620 struct _Sp_alloc_shared_tag
621 {
622 const _Alloc& _M_a;
623 };
624
625 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
626 class _Sp_counted_ptr_inplace final : public _Sp_counted_base<_Lp>
627 {
628 public:
629 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
630
631 // Alloc parameter is not a reference so doesn't alias anything in __args
632 template<typename... _Args>
633 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
634 : _M_alloc{__a}
635 {
636 // _GLIBCXX_RESOLVE_LIB_DEFECTS
637 // 2070. allocate_shared should use allocator_traits<A>::construct
639 std::forward<_Args>(__args)...); // might throw
640 }
641
642#pragma GCC diagnostic push // PR tree-optimization/122197
643#pragma GCC diagnostic ignored "-Warray-bounds"
644 ~_Sp_counted_ptr_inplace() noexcept { }
645#pragma GCC diagnostic pop
646
647 virtual void
648 _M_dispose() noexcept
649 {
650 allocator_traits<_Alloc>::destroy(_M_alloc._M_obj, _M_ptr());
651 }
652
653 // Override because the allocator needs to know the dynamic type
654 virtual void
655 _M_destroy() noexcept
656 {
657 __allocator_type __a(_M_alloc._M_obj);
658 __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
659 this->~_Sp_counted_ptr_inplace();
660 }
661
662 private:
663 friend class __shared_count<_Lp>; // To be able to call _M_ptr().
664
665 // No longer used, but code compiled against old libstdc++ headers
666 // might still call it from __shared_ptr ctor to get the pointer out.
667 virtual void*
668 _M_get_deleter(const std::type_info& __ti) noexcept override
669 {
670 // Check for the fake type_info first, so we don't try to access it
671 // as a real type_info object. Otherwise, check if it's the real
672 // type_info for this class. With RTTI enabled we can check directly,
673 // or call a library function to do it.
674 if (&__ti == &_Sp_make_shared_tag::_S_ti()
675 ||
676#if __cpp_rtti
677 __ti == typeid(_Sp_make_shared_tag)
678#else
679 _Sp_make_shared_tag::_S_eq(__ti)
680#endif
681 )
682 return _M_ptr();
683 return nullptr;
684 }
685
686 __remove_cv_t<_Tp>*
687 _M_ptr() noexcept { return _M_storage._M_ptr(); }
688
689 [[__no_unique_address__]] _Sp_ebo_helper<_Alloc> _M_alloc;
690 __gnu_cxx::__aligned_buffer<__remove_cv_t<_Tp>> _M_storage;
691 };
692
693#ifdef __glibcxx_smart_ptr_for_overwrite // C++ >= 20 && HOSTED
694 struct _Sp_overwrite_tag { };
695
696 // Partial specialization used for make_shared_for_overwrite<non-array>().
697 // This partial specialization is used when the allocator's value type
698 // is the special _Sp_overwrite_tag type.
699 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
700 requires is_same_v<typename _Alloc::value_type, _Sp_overwrite_tag>
701 class _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> final
702 : public _Sp_counted_base<_Lp>
703 {
704 [[no_unique_address]] _Alloc _M_alloc;
705
706 union {
707 remove_cv_t<_Tp> _M_obj;
708 char _M_unused;
709 };
710
711 friend class __shared_count<_Lp>; // To be able to call _M_ptr().
712
713 auto _M_ptr() noexcept { return std::__addressof(_M_obj); }
714
715 public:
716 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
717
718 _Sp_counted_ptr_inplace(const _Alloc& __a)
719 : _M_alloc(__a)
720 {
721 ::new((void*)_M_ptr()) _Tp; // default-initialized, for overwrite.
722 }
723
724 ~_Sp_counted_ptr_inplace() noexcept { }
725
726 virtual void
727 _M_dispose() noexcept
728 {
729 _M_obj.~_Tp();
730 }
731
732 // Override because the allocator needs to know the dynamic type
733 virtual void
734 _M_destroy() noexcept
735 {
736 using pointer = typename allocator_traits<__allocator_type>::pointer;
737 __allocator_type __a(_M_alloc);
738 auto __p = pointer_traits<pointer>::pointer_to(*this);
739 __allocated_ptr<__allocator_type> __guard_ptr{ __a, __p };
740 this->~_Sp_counted_ptr_inplace();
741 }
742
743 void*
744 _M_get_deleter(const std::type_info&) noexcept override
745 { return nullptr; }
746 };
747#endif // __glibcxx_smart_ptr_for_overwrite
748
749#if __glibcxx_shared_ptr_arrays >= 201707L // C++ >= 20 && HOSTED
750 struct _Sp_overwrite_tag;
751
752 // For make_shared<T[]>, make_shared<T[N]>, allocate_shared<T[]> etc.
753 template<typename _Alloc>
754 struct _Sp_counted_array_base
755 {
756 [[no_unique_address]] _Alloc _M_alloc{};
757 size_t _M_n = 0;
758 bool _M_overwrite = false;
759
760 typename allocator_traits<_Alloc>::pointer
761 _M_alloc_array(size_t __tail)
762 {
763 return allocator_traits<_Alloc>::allocate(_M_alloc, _M_n + __tail);
764 }
765
766 void
767 _M_dealloc_array(typename allocator_traits<_Alloc>::pointer __p,
768 size_t __tail)
769 {
770 allocator_traits<_Alloc>::deallocate(_M_alloc, __p, _M_n + __tail);
771 }
772
773 // Init the array elements
774 template<typename _Init>
775 void
776 _M_init(typename allocator_traits<_Alloc>::value_type* __p,
777 _Init __init)
778 {
779 using _Tp = remove_pointer_t<_Init>;
780 using _Up = typename allocator_traits<_Alloc>::value_type;
781
782 if constexpr (is_same_v<_Init, _Sp_overwrite_tag>)
783 {
785 _M_overwrite = true;
786 }
787 else if (__init == nullptr)
788 std::__uninitialized_default_n_a(__p, _M_n, _M_alloc);
789 else if constexpr (!is_array_v<_Tp>)
790 std::__uninitialized_fill_n_a(__p, _M_n, *__init, _M_alloc);
791 else
792 {
793#pragma GCC diagnostic push
794#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
795 struct _Iter
796 {
797 using value_type = _Up;
798 using difference_type = ptrdiff_t;
799 using pointer = const _Up*;
800 using reference = const _Up&;
801 using iterator_category = forward_iterator_tag;
802
803 const _Up* _M_p;
804 size_t _M_len;
805 size_t _M_pos;
806
807 _Iter& operator++() { ++_M_pos; return *this; }
808 _Iter operator++(int) { auto __i(*this); ++_M_pos; return __i; }
809
810 reference operator*() const { return _M_p[_M_pos % _M_len]; }
811 pointer operator->() const { return _M_p + (_M_pos % _M_len); }
812
813 bool operator==(const _Iter& __i) const
814 { return _M_pos == __i._M_pos; }
815 };
816#pragma GCC diagnostic pop
817
818 _Iter __first{_S_first_elem(__init), sizeof(_Tp) / sizeof(_Up)};
819 _Iter __last = __first;
820 __last._M_pos = _M_n;
821 std::__uninitialized_copy_a(__first, __last, __p, _M_alloc);
822 }
823 }
824
825 protected:
826 // Destroy the array elements
827 void
828 _M_dispose_array(typename allocator_traits<_Alloc>::value_type* __p)
829 {
830 if (_M_overwrite)
831 std::destroy_n(__p, _M_n);
832 else
833 {
834 size_t __n = _M_n;
835 while (__n--)
836 allocator_traits<_Alloc>::destroy(_M_alloc, __p + __n);
837 }
838 }
839
840 private:
841 template<typename _Tp>
842 static _Tp*
843 _S_first_elem(_Tp* __p) { return __p; }
844
845 template<typename _Tp, size_t _Nm>
846 static auto
847 _S_first_elem(_Tp (*__p)[_Nm]) { return _S_first_elem(*__p); }
848 };
849
850 // Control block for make_shared<T[]>, make_shared<T[N]> etc. that will be
851 // placed into unused memory at the end of the array.
852 template<typename _Alloc, _Lock_policy _Lp>
853 class _Sp_counted_array final
854 : public _Sp_counted_base<_Lp>, _Sp_counted_array_base<_Alloc>
855 {
856 using pointer = typename allocator_traits<_Alloc>::pointer;
857
858 pointer _M_alloc_ptr;
859
860 auto _M_ptr() const noexcept { return std::to_address(_M_alloc_ptr); }
861
862 friend class __shared_count<_Lp>; // To be able to call _M_ptr().
863
864 public:
865 _Sp_counted_array(const _Sp_counted_array_base<_Alloc>& __a,
866 pointer __p) noexcept
867 : _Sp_counted_array_base<_Alloc>(__a), _M_alloc_ptr(__p)
868 { }
869
870 ~_Sp_counted_array() = default;
871
872 virtual void
873 _M_dispose() noexcept
874 {
875 if (this->_M_n)
876 this->_M_dispose_array(_M_ptr());
877 }
878
879 // Override because the allocator needs to know the dynamic type
880 virtual void
881 _M_destroy() noexcept
882 {
883 _Sp_counted_array_base<_Alloc> __a = *this;
884 pointer __p = _M_alloc_ptr;
885 this->~_Sp_counted_array();
886 __a._M_dealloc_array(__p, _S_tail());
887 }
888
889 // Returns the number of additional array elements that must be
890 // allocated in order to store a _Sp_counted_array at the end.
891 static constexpr size_t
892 _S_tail()
893 {
894 // The array elemenent type.
895 using _Tp = typename allocator_traits<_Alloc>::value_type;
896
897 // The space needed to store a _Sp_counted_array object.
898 size_t __bytes = sizeof(_Sp_counted_array);
899
900 // Add any padding needed for manual alignment within the buffer.
901 if constexpr (alignof(_Tp) < alignof(_Sp_counted_array))
902 __bytes += alignof(_Sp_counted_array) - alignof(_Tp);
903
904 return (__bytes + sizeof(_Tp) - 1) / sizeof(_Tp);
905 }
906
907 void*
908 _M_get_deleter(const std::type_info&) noexcept override
909 { return nullptr; }
910 };
911#endif // __glibcxx_shared_ptr_arrays >= 201707L
912
913 // The default deleter for shared_ptr<T[]> and shared_ptr<T[N]>.
914 struct __sp_array_delete
915 {
916 template<typename _Yp>
917 void operator()(_Yp* __p) const { delete[] __p; }
918 };
919
920 template<_Lock_policy _Lp>
921 class __shared_count
922 {
923 // Prevent _Sp_alloc_shared_tag from matching the shared_ptr(P, D) ctor.
924 template<typename _Tp>
925 struct __not_alloc_shared_tag { using type = void; };
926
927 template<typename _Tp>
928 struct __not_alloc_shared_tag<_Sp_alloc_shared_tag<_Tp>> { };
929
930#if __glibcxx_shared_ptr_arrays >= 201707L // C++ >= 20 && HOSTED
931 template<typename _Alloc>
932 struct __not_alloc_shared_tag<_Sp_counted_array_base<_Alloc>> { };
933#endif
934
935 public:
936 constexpr __shared_count() noexcept : _M_pi(0)
937 { }
938
939 template<typename _Ptr>
940 explicit
941 __shared_count(_Ptr __p) : _M_pi(0)
942 {
943 __try
944 {
945 _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
946 }
947 __catch(...)
948 {
949 delete __p;
950 __throw_exception_again;
951 }
952 }
953
954 template<typename _Ptr>
955 __shared_count(_Ptr __p, /* is_array = */ false_type)
956 : __shared_count(__p)
957 { }
958
959 template<typename _Ptr>
960 __shared_count(_Ptr __p, /* is_array = */ true_type)
961 : __shared_count(__p, __sp_array_delete{}, allocator<void>())
962 { }
963
964 template<typename _Ptr, typename _Deleter,
965 typename = typename __not_alloc_shared_tag<_Deleter>::type>
966 __shared_count(_Ptr __p, _Deleter __d)
967 : __shared_count(__p, std::move(__d), allocator<void>())
968 { }
969
970 template<typename _Ptr, typename _Deleter, typename _Alloc,
971 typename = typename __not_alloc_shared_tag<_Deleter>::type>
972 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
973 {
974 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
975 __try
976 {
977 typename _Sp_cd_type::__allocator_type __a2(__a);
978 auto __guard = std::__allocate_guarded(__a2);
979 _Sp_cd_type* __mem = __guard.get();
980 ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a));
981 _M_pi = __mem;
982 __guard = nullptr;
983 }
984 __catch(...)
985 {
986 __d(__p); // Call _Deleter on __p.
987 __throw_exception_again;
988 }
989 }
990
991 template<typename _Tp, typename _Alloc, typename... _Args>
992 __shared_count(_Tp*& __p, _Sp_alloc_shared_tag<_Alloc> __a,
993 _Args&&... __args)
994 {
995 using _Tp2 = __remove_cv_t<_Tp>;
996 using _Sp_cp_type = _Sp_counted_ptr_inplace<_Tp2, _Alloc, _Lp>;
997 typename _Sp_cp_type::__allocator_type __a2(__a._M_a);
998 auto __guard = std::__allocate_guarded(__a2);
999 _Sp_cp_type* __mem = __guard.get();
1000 auto __pi = ::new (__mem)
1001 _Sp_cp_type(__a._M_a, std::forward<_Args>(__args)...);
1002 __guard = nullptr;
1003 _M_pi = __pi;
1004 __p = __pi->_M_ptr();
1005 }
1006
1007#if __glibcxx_shared_ptr_arrays >= 201707L // C++ >= 20 && HOSTED
1008 template<typename _Tp, typename _Alloc, typename _Init>
1009 __shared_count(_Tp*& __p, const _Sp_counted_array_base<_Alloc>& __a,
1010 _Init __init)
1011 {
1012 using _Up = remove_all_extents_t<_Tp>;
1013 static_assert(is_same_v<_Up, typename _Alloc::value_type>);
1014
1015 using _Sp_ca_type = _Sp_counted_array<_Alloc, _Lp>;
1016 const size_t __tail = _Sp_ca_type::_S_tail();
1017
1018 struct _Guarded_ptr : _Sp_counted_array_base<_Alloc>
1019 {
1020 typename allocator_traits<_Alloc>::pointer _M_ptr;
1021
1022 _Guarded_ptr(_Sp_counted_array_base<_Alloc> __a)
1023 : _Sp_counted_array_base<_Alloc>(__a),
1024 _M_ptr(this->_M_alloc_array(_Sp_ca_type::_S_tail()))
1025 { }
1026
1027 ~_Guarded_ptr()
1028 {
1029 if (_M_ptr)
1030 this->_M_dealloc_array(_M_ptr, _Sp_ca_type::_S_tail());
1031 }
1032 };
1033
1034 _Guarded_ptr __guard{__a};
1035 _Up* const __raw = std::to_address(__guard._M_ptr);
1036 __guard._M_init(__raw, __init); // might throw
1037
1038 void* __c = __raw + __a._M_n;
1039 if constexpr (alignof(_Up) < alignof(_Sp_ca_type))
1040 {
1041 size_t __space = sizeof(_Up) * __tail;
1042 __c = std::align(alignof(_Sp_ca_type), sizeof(_Sp_ca_type),
1043 __c, __space);
1044 }
1045 auto __pi = ::new(__c) _Sp_ca_type(__guard, __guard._M_ptr);
1046 __guard._M_ptr = nullptr;
1047 _M_pi = __pi;
1048 __p = reinterpret_cast<_Tp*>(__raw);
1049 }
1050#endif
1051
1052#if _GLIBCXX_USE_DEPRECATED
1053#pragma GCC diagnostic push
1054#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1055 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
1056 template<typename _Tp>
1057 explicit
1058 __shared_count(std::auto_ptr<_Tp>&& __r);
1059#pragma GCC diagnostic pop
1060#endif
1061
1062 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
1063 template<typename _Tp, typename _Del>
1064 explicit
1065 __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0)
1066 {
1067 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1068 // 2415. Inconsistency between unique_ptr and shared_ptr
1069 if (__r.get() == nullptr)
1070 return;
1071
1072 using _Ptr = typename unique_ptr<_Tp, _Del>::pointer;
1073 using _Del2 = __conditional_t<is_reference<_Del>::value,
1074 reference_wrapper<typename remove_reference<_Del>::type>,
1075 _Del>;
1076 using _Sp_cd_type
1077 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
1078 using _Alloc = allocator<_Sp_cd_type>;
1079 using _Alloc_traits = allocator_traits<_Alloc>;
1080 _Alloc __a;
1081 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
1082 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1083 // 3548. shared_ptr construction from unique_ptr should move
1084 // (not copy) the deleter
1085 _Alloc_traits::construct(__a, __mem, __r.release(),
1086 std::forward<_Del>(__r.get_deleter()));
1087 _M_pi = __mem;
1088 }
1089
1090 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
1091 explicit __shared_count(const __weak_count<_Lp>& __r);
1092
1093 // Does not throw if __r._M_get_use_count() == 0, caller must check.
1094 explicit
1095 __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t) noexcept;
1096
1097 ~__shared_count() noexcept
1098 {
1099 if (_M_pi != nullptr)
1100 _M_pi->_M_release();
1101 }
1102
1103 __shared_count(const __shared_count& __r) noexcept
1104 : _M_pi(__r._M_pi)
1105 {
1106 if (_M_pi != nullptr)
1107 _M_pi->_M_add_ref_copy();
1108 }
1109
1110 __shared_count&
1111 operator=(const __shared_count& __r) noexcept
1112 {
1113 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1114 if (__tmp != _M_pi)
1115 {
1116 if (__tmp != nullptr)
1117 __tmp->_M_add_ref_copy();
1118 if (_M_pi != nullptr)
1119 _M_pi->_M_release();
1120 _M_pi = __tmp;
1121 }
1122 return *this;
1123 }
1124
1125 void
1126 _M_swap(__shared_count& __r) noexcept
1127 {
1128 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1129 __r._M_pi = _M_pi;
1130 _M_pi = __tmp;
1131 }
1132
1133 long
1134 _M_get_use_count() const noexcept
1135 { return _M_pi ? _M_pi->_M_get_use_count() : 0; }
1136
1137 bool
1138 _M_unique() const noexcept
1139 { return this->_M_get_use_count() == 1; }
1140
1141 void*
1142 _M_get_deleter(const std::type_info& __ti) const noexcept
1143 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : nullptr; }
1144
1145 bool
1146 _M_less(const __shared_count& __rhs) const noexcept
1147 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1148
1149 bool
1150 _M_less(const __weak_count<_Lp>& __rhs) const noexcept
1151 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1152
1153#ifdef __glibcxx_smart_ptr_owner_equality // >= C++26
1154 size_t
1155 _M_owner_hash() const noexcept
1156 { return std::hash<_Sp_counted_base<_Lp>*>()(this->_M_pi); }
1157#endif
1158
1159 // Friend function injected into enclosing namespace and found by ADL
1160 friend inline bool
1161 operator==(const __shared_count& __a, const __shared_count& __b) noexcept
1162 { return __a._M_pi == __b._M_pi; }
1163
1164 private:
1165 friend class __weak_count<_Lp>;
1166#ifdef __glibcxx_atomic_shared_ptr
1167 template<typename> friend class _Sp_atomic;
1168#endif
1169#ifdef __glibcxx_out_ptr
1170 template<typename, typename, typename...> friend class out_ptr_t;
1171#endif
1172
1173 _Sp_counted_base<_Lp>* _M_pi;
1174 };
1175
1176
1177 template<_Lock_policy _Lp>
1178 class __weak_count
1179 {
1180 public:
1181 constexpr __weak_count() noexcept : _M_pi(nullptr)
1182 { }
1183
1184 __weak_count(const __shared_count<_Lp>& __r) noexcept
1185 : _M_pi(__r._M_pi)
1186 {
1187 if (_M_pi != nullptr)
1188 _M_pi->_M_weak_add_ref();
1189 }
1190
1191 __weak_count(const __weak_count& __r) noexcept
1192 : _M_pi(__r._M_pi)
1193 {
1194 if (_M_pi != nullptr)
1195 _M_pi->_M_weak_add_ref();
1196 }
1197
1198 __weak_count(__weak_count&& __r) noexcept
1199 : _M_pi(__r._M_pi)
1200 { __r._M_pi = nullptr; }
1201
1202 ~__weak_count() noexcept
1203 {
1204 if (_M_pi != nullptr)
1205 _M_pi->_M_weak_release();
1206 }
1207
1208 __weak_count&
1209 operator=(const __shared_count<_Lp>& __r) noexcept
1210 {
1211 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1212 if (__tmp != nullptr)
1213 __tmp->_M_weak_add_ref();
1214 if (_M_pi != nullptr)
1215 _M_pi->_M_weak_release();
1216 _M_pi = __tmp;
1217 return *this;
1218 }
1219
1220 __weak_count&
1221 operator=(const __weak_count& __r) noexcept
1222 {
1223 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1224 if (__tmp != nullptr)
1225 __tmp->_M_weak_add_ref();
1226 if (_M_pi != nullptr)
1227 _M_pi->_M_weak_release();
1228 _M_pi = __tmp;
1229 return *this;
1230 }
1231
1232 __weak_count&
1233 operator=(__weak_count&& __r) noexcept
1234 {
1235 if (_M_pi != nullptr)
1236 _M_pi->_M_weak_release();
1237 _M_pi = __r._M_pi;
1238 __r._M_pi = nullptr;
1239 return *this;
1240 }
1241
1242 void
1243 _M_swap(__weak_count& __r) noexcept
1244 {
1245 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1246 __r._M_pi = _M_pi;
1247 _M_pi = __tmp;
1248 }
1249
1250 long
1251 _M_get_use_count() const noexcept
1252 { return _M_pi != nullptr ? _M_pi->_M_get_use_count() : 0; }
1253
1254 bool
1255 _M_less(const __weak_count& __rhs) const noexcept
1256 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1257
1258 bool
1259 _M_less(const __shared_count<_Lp>& __rhs) const noexcept
1260 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1261
1262#ifdef __glibcxx_smart_ptr_owner_equality // >= C++26
1263 size_t
1264 _M_owner_hash() const noexcept
1265 { return std::hash<_Sp_counted_base<_Lp>*>()(this->_M_pi); }
1266#endif
1267
1268 // Friend function injected into enclosing namespace and found by ADL
1269 friend inline bool
1270 operator==(const __weak_count& __a, const __weak_count& __b) noexcept
1271 { return __a._M_pi == __b._M_pi; }
1272
1273 private:
1274 friend class __shared_count<_Lp>;
1275#ifdef __glibcxx_atomic_shared_ptr
1276 template<typename> friend class _Sp_atomic;
1277#endif
1278
1279 _Sp_counted_base<_Lp>* _M_pi;
1280 };
1281
1282 // Now that __weak_count is defined we can define this constructor:
1283 template<_Lock_policy _Lp>
1284 inline
1285 __shared_count<_Lp>::__shared_count(const __weak_count<_Lp>& __r)
1286 : _M_pi(__r._M_pi)
1287 {
1288 if (_M_pi == nullptr || !_M_pi->_M_add_ref_lock_nothrow())
1289 __throw_bad_weak_ptr();
1290 }
1291
1292 // Now that __weak_count is defined we can define this constructor:
1293 template<_Lock_policy _Lp>
1294 inline
1295 __shared_count<_Lp>::
1296 __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t) noexcept
1297 : _M_pi(__r._M_pi)
1298 {
1299 if (_M_pi && !_M_pi->_M_add_ref_lock_nothrow())
1300 _M_pi = nullptr;
1301 }
1302
1303 // Helper traits for shared_ptr of array:
1304
1305 // A pointer type Y* is said to be compatible with a pointer type T* when
1306 // either Y* is convertible to T* or Y is U[N] and T is U cv [].
1307 template<typename _Yp_ptr, typename _Tp_ptr>
1308 struct __sp_compatible_with
1309 : false_type
1310 { };
1311
1312 template<typename _Yp, typename _Tp>
1313 struct __sp_compatible_with<_Yp*, _Tp*>
1314 : is_convertible<_Yp*, _Tp*>::type
1315 { };
1316
1317 template<typename _Up, size_t _Nm>
1318 struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
1319 : true_type
1320 { };
1321
1322 template<typename _Up, size_t _Nm>
1323 struct __sp_compatible_with<_Up(*)[_Nm], const _Up(*)[]>
1324 : true_type
1325 { };
1326
1327 template<typename _Up, size_t _Nm>
1328 struct __sp_compatible_with<_Up(*)[_Nm], volatile _Up(*)[]>
1329 : true_type
1330 { };
1331
1332 template<typename _Up, size_t _Nm>
1333 struct __sp_compatible_with<_Up(*)[_Nm], const volatile _Up(*)[]>
1334 : true_type
1335 { };
1336
1337 // Test conversion from Y(*)[N] to U(*)[N] without forming invalid type Y[N].
1338 template<typename _Up, size_t _Nm, typename _Yp, typename = void>
1339 struct __sp_is_constructible_arrN
1340 : false_type
1341 { };
1342
1343 template<typename _Up, size_t _Nm, typename _Yp>
1344 struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
1345 : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
1346 { };
1347
1348 // Test conversion from Y(*)[] to U(*)[] without forming invalid type Y[].
1349 template<typename _Up, typename _Yp, typename = void>
1350 struct __sp_is_constructible_arr
1351 : false_type
1352 { };
1353
1354 template<typename _Up, typename _Yp>
1355 struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
1356 : is_convertible<_Yp(*)[], _Up(*)[]>::type
1357 { };
1358
1359 // Trait to check if shared_ptr<T> can be constructed from Y*.
1360 template<typename _Tp, typename _Yp>
1361 struct __sp_is_constructible;
1362
1363 // When T is U[N], Y(*)[N] shall be convertible to T*;
1364 template<typename _Up, size_t _Nm, typename _Yp>
1365 struct __sp_is_constructible<_Up[_Nm], _Yp>
1366 : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
1367 { };
1368
1369 // when T is U[], Y(*)[] shall be convertible to T*;
1370 template<typename _Up, typename _Yp>
1371 struct __sp_is_constructible<_Up[], _Yp>
1372 : __sp_is_constructible_arr<_Up, _Yp>::type
1373 { };
1374
1375 // otherwise, Y* shall be convertible to T*.
1376 template<typename _Tp, typename _Yp>
1377 struct __sp_is_constructible
1378 : is_convertible<_Yp*, _Tp*>::type
1379 { };
1380
1381
1382 template<typename _Tp>
1383 [[__gnu__::__always_inline__]]
1384 inline _Tp*
1385 __shared_ptr_deref(_Tp* __p)
1386 {
1387 __glibcxx_assert(__p != nullptr);
1388 return __p;
1389 }
1390
1391 // Define operator* and operator-> for shared_ptr<T>.
1392 template<typename _Tp, _Lock_policy _Lp,
1394 class __shared_ptr_access
1395 {
1396 public:
1397 using element_type = _Tp;
1398
1399 element_type&
1400 operator*() const noexcept
1401 { return *std::__shared_ptr_deref(_M_get()); }
1402
1403 element_type*
1404 operator->() const noexcept
1405 {
1406 _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
1407 return _M_get();
1408 }
1409
1410 private:
1411 element_type*
1412 _M_get() const noexcept
1413 { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
1414 };
1415
1416 // Define operator-> for shared_ptr<cv void>.
1417 template<typename _Tp, _Lock_policy _Lp>
1418 class __shared_ptr_access<_Tp, _Lp, false, true>
1419 {
1420 public:
1421 using element_type = _Tp;
1422
1423 element_type*
1424 operator->() const noexcept
1425 {
1426 auto __ptr = static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get();
1427 _GLIBCXX_DEBUG_PEDASSERT(__ptr != nullptr);
1428 return __ptr;
1429 }
1430 };
1431
1432 // Define operator[] for shared_ptr<T[]> and shared_ptr<T[N]>.
1433 template<typename _Tp, _Lock_policy _Lp>
1434 class __shared_ptr_access<_Tp, _Lp, true, false>
1435 {
1436 public:
1437 using element_type = typename remove_extent<_Tp>::type;
1438
1439#if __cplusplus <= 201402L
1440 [[__deprecated__("shared_ptr<T[]>::operator* is absent from C++17")]]
1441 element_type&
1442 operator*() const noexcept
1443 { return *std::__shared_ptr_deref(_M_get()); }
1444
1445 [[__deprecated__("shared_ptr<T[]>::operator-> is absent from C++17")]]
1446 element_type*
1447 operator->() const noexcept
1448 {
1449 _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
1450 return _M_get();
1451 }
1452#endif
1453
1454#pragma GCC diagnostic push
1455#pragma GCC diagnostic ignored "-Wc++17-extensions"
1456 element_type&
1457 operator[](ptrdiff_t __i) const noexcept
1458 {
1459 if constexpr (extent<_Tp>::value)
1460 __glibcxx_assert(__i < extent<_Tp>::value);
1461 return std::__shared_ptr_deref(_M_get())[__i];
1462 }
1463#pragma GCC diagnostic pop
1464
1465 private:
1466 element_type*
1467 _M_get() const noexcept
1468 { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
1469 };
1470
1471 template<typename _Tp, _Lock_policy _Lp>
1472 class __shared_ptr
1473 : public __shared_ptr_access<_Tp, _Lp>
1474 {
1475 public:
1476 using element_type = typename remove_extent<_Tp>::type;
1477
1478 private:
1479 // Constraint for taking ownership of a pointer of type _Yp*:
1480 template<typename _Yp>
1481 using _SafeConv
1482 = typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
1483
1484 // Constraint for construction from shared_ptr and weak_ptr:
1485 template<typename _Yp, typename _Res = void>
1486 using _Compatible = typename
1487 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1488
1489 // Constraint for assignment from shared_ptr and weak_ptr:
1490 template<typename _Yp>
1491 using _Assignable = _Compatible<_Yp, __shared_ptr&>;
1492
1493 // Constraint for construction from unique_ptr:
1494 template<typename _Yp, typename _Del, typename _Res = void,
1495 typename _Ptr = typename unique_ptr<_Yp, _Del>::pointer>
1496 using _UniqCompatible = __enable_if_t<__and_<
1497 __sp_compatible_with<_Yp*, _Tp*>,
1498 is_convertible<_Ptr, element_type*>,
1499 is_move_constructible<_Del>
1500 >::value, _Res>;
1501
1502 // Constraint for assignment from unique_ptr:
1503 template<typename _Yp, typename _Del>
1504 using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
1505
1506 public:
1507
1508#if __cplusplus > 201402L
1509 using weak_type = __weak_ptr<_Tp, _Lp>;
1510#endif
1511
1512 constexpr __shared_ptr() noexcept
1513 : _M_ptr(0), _M_refcount()
1514 { }
1515
1516 template<typename _Yp, typename = _SafeConv<_Yp>>
1517 explicit
1518 __shared_ptr(_Yp* __p)
1519 : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
1520 {
1521 static_assert( !is_void<_Yp>::value, "incomplete type" );
1522 static_assert( sizeof(_Yp) > 0, "incomplete type" );
1523 _M_enable_shared_from_this_with(__p);
1524 }
1525
1526 template<typename _Yp, typename _Deleter, typename = _SafeConv<_Yp>>
1527 __shared_ptr(_Yp* __p, _Deleter __d)
1528 : _M_ptr(__p), _M_refcount(__p, std::move(__d))
1529 {
1530 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1531 "deleter expression d(p) is well-formed");
1532 _M_enable_shared_from_this_with(__p);
1533 }
1534
1535 template<typename _Yp, typename _Deleter, typename _Alloc,
1536 typename = _SafeConv<_Yp>>
1537 __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
1538 : _M_ptr(__p), _M_refcount(__p, std::move(__d), std::move(__a))
1539 {
1540 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1541 "deleter expression d(p) is well-formed");
1542 _M_enable_shared_from_this_with(__p);
1543 }
1544
1545 template<typename _Deleter>
1546 __shared_ptr(nullptr_t __p, _Deleter __d)
1547 : _M_ptr(0), _M_refcount(__p, std::move(__d))
1548 { }
1549
1550 template<typename _Deleter, typename _Alloc>
1551 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
1552 : _M_ptr(0), _M_refcount(__p, std::move(__d), std::move(__a))
1553 { }
1554
1555 // Aliasing constructor
1556 template<typename _Yp>
1557 __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r,
1558 element_type* __p) noexcept
1559 : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
1560 { }
1561
1562 // Aliasing constructor
1563 template<typename _Yp>
1564 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r,
1565 element_type* __p) noexcept
1566 : _M_ptr(__p), _M_refcount()
1567 {
1568 _M_refcount._M_swap(__r._M_refcount);
1569 __r._M_ptr = nullptr;
1570 }
1571
1572 __shared_ptr(const __shared_ptr&) noexcept = default;
1573 __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
1574 ~__shared_ptr() = default;
1575
1576 template<typename _Yp, typename = _Compatible<_Yp>>
1577 __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1578 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1579 { }
1580
1581 __shared_ptr(__shared_ptr&& __r) noexcept
1582 : _M_ptr(__r._M_ptr), _M_refcount()
1583 {
1584 _M_refcount._M_swap(__r._M_refcount);
1585 __r._M_ptr = nullptr;
1586 }
1587
1588 template<typename _Yp, typename = _Compatible<_Yp>>
1589 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1590 : _M_ptr(__r._M_ptr), _M_refcount()
1591 {
1592 _M_refcount._M_swap(__r._M_refcount);
1593 __r._M_ptr = nullptr;
1594 }
1595
1596 template<typename _Yp, typename = _Compatible<_Yp>>
1597 explicit __shared_ptr(const __weak_ptr<_Yp, _Lp>& __r)
1598 : _M_refcount(__r._M_refcount) // may throw
1599 {
1600 // It is now safe to copy __r._M_ptr, as
1601 // _M_refcount(__r._M_refcount) did not throw.
1602 _M_ptr = __r._M_ptr;
1603 }
1604
1605 // If an exception is thrown this constructor has no effect.
1606 template<typename _Yp, typename _Del,
1607 typename = _UniqCompatible<_Yp, _Del>>
1608 __shared_ptr(unique_ptr<_Yp, _Del>&& __r)
1609 : _M_ptr(__r.get()), _M_refcount()
1610 {
1611 auto __raw = std::__to_address(__r.get());
1612 _M_refcount = __shared_count<_Lp>(std::move(__r));
1613 _M_enable_shared_from_this_with(__raw);
1614 }
1615
1616#if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
1617 protected:
1618 // If an exception is thrown this constructor has no effect.
1619 template<typename _Tp1, typename _Del,
1620 typename enable_if<__and_<
1621 __not_<is_array<_Tp>>, is_array<_Tp1>,
1622 is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
1623 >::value, bool>::type = true>
1624 __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
1625 : _M_ptr(__r.get()), _M_refcount()
1626 {
1627 auto __raw = std::__to_address(__r.get());
1628 _M_refcount = __shared_count<_Lp>(std::move(__r));
1629 _M_enable_shared_from_this_with(__raw);
1630 }
1631 public:
1632#endif
1633
1634#if _GLIBCXX_USE_DEPRECATED
1635#pragma GCC diagnostic push
1636#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1637 // Postcondition: use_count() == 1 and __r.get() == 0
1638 template<typename _Yp, typename = _Compatible<_Yp>>
1639 __shared_ptr(auto_ptr<_Yp>&& __r);
1640#pragma GCC diagnostic pop
1641#endif
1642
1643 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
1644
1645 template<typename _Yp>
1646 _Assignable<_Yp>
1647 operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1648 {
1649 _M_ptr = __r._M_ptr;
1650 _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
1651 return *this;
1652 }
1653
1654#if _GLIBCXX_USE_DEPRECATED
1655#pragma GCC diagnostic push
1656#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1657 template<typename _Yp>
1658 _Assignable<_Yp>
1659 operator=(auto_ptr<_Yp>&& __r)
1660 {
1661 __shared_ptr(std::move(__r)).swap(*this);
1662 return *this;
1663 }
1664#pragma GCC diagnostic pop
1665#endif
1666
1667 __shared_ptr&
1668 operator=(__shared_ptr&& __r) noexcept
1669 {
1670 __shared_ptr(std::move(__r)).swap(*this);
1671 return *this;
1672 }
1673
1674 template<class _Yp>
1675 _Assignable<_Yp>
1676 operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1677 {
1678 __shared_ptr(std::move(__r)).swap(*this);
1679 return *this;
1680 }
1681
1682 template<typename _Yp, typename _Del>
1683 _UniqAssignable<_Yp, _Del>
1684 operator=(unique_ptr<_Yp, _Del>&& __r)
1685 {
1686 __shared_ptr(std::move(__r)).swap(*this);
1687 return *this;
1688 }
1689
1690 void
1691 reset() noexcept
1692 { __shared_ptr().swap(*this); }
1693
1694 template<typename _Yp>
1695 _SafeConv<_Yp>
1696 reset(_Yp* __p) // _Yp must be complete.
1697 {
1698 // Catch self-reset errors.
1699 __glibcxx_assert(__p == nullptr || __p != _M_ptr);
1700 __shared_ptr(__p).swap(*this);
1701 }
1702
1703 template<typename _Yp, typename _Deleter>
1704 _SafeConv<_Yp>
1705 reset(_Yp* __p, _Deleter __d)
1706 { __shared_ptr(__p, std::move(__d)).swap(*this); }
1707
1708 template<typename _Yp, typename _Deleter, typename _Alloc>
1709 _SafeConv<_Yp>
1710 reset(_Yp* __p, _Deleter __d, _Alloc __a)
1711 { __shared_ptr(__p, std::move(__d), std::move(__a)).swap(*this); }
1712
1713 /// Return the stored pointer.
1714 element_type*
1715 get() const noexcept
1716 { return _M_ptr; }
1717
1718 /// Return true if the stored pointer is not null.
1719 explicit operator bool() const noexcept
1720 { return _M_ptr != nullptr; }
1721
1722 /// Return true if use_count() == 1.
1723 bool
1724 unique() const noexcept
1725 { return _M_refcount._M_unique(); }
1726
1727 /// If *this owns a pointer, return the number of owners, otherwise zero.
1728 long
1729 use_count() const noexcept
1730 { return _M_refcount._M_get_use_count(); }
1731
1732 /// Exchange both the owned pointer and the stored pointer.
1733 void
1734 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
1735 {
1736 std::swap(_M_ptr, __other._M_ptr);
1737 _M_refcount._M_swap(__other._M_refcount);
1738 }
1739
1740 /** @brief Define an ordering based on ownership.
1741 *
1742 * This function defines a strict weak ordering between two shared_ptr
1743 * or weak_ptr objects, such that one object is less than the other
1744 * unless they share ownership of the same pointer, or are both empty.
1745 * @{
1746 */
1747 template<typename _Tp1>
1748 bool
1749 owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const noexcept
1750 { return _M_refcount._M_less(__rhs._M_refcount); }
1751
1752 template<typename _Tp1>
1753 bool
1754 owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const noexcept
1755 { return _M_refcount._M_less(__rhs._M_refcount); }
1756 /// @}
1757
1758#ifdef __glibcxx_smart_ptr_owner_equality // >= C++26
1759 size_t owner_hash() const noexcept { return _M_refcount._M_owner_hash(); }
1760
1761 template<typename _Tp1>
1762 bool
1763 owner_equal(__shared_ptr<_Tp1, _Lp> const& __rhs) const noexcept
1764 { return _M_refcount == __rhs._M_refcount; }
1765
1766 template<typename _Tp1>
1767 bool
1768 owner_equal(__weak_ptr<_Tp1, _Lp> const& __rhs) const noexcept
1769 { return _M_refcount == __rhs._M_refcount; }
1770#endif
1771
1772 protected:
1773 // This constructor is non-standard, it is used by allocate_shared.
1774 template<typename _Alloc, typename... _Args>
1775 __shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
1776 : _M_ptr(), _M_refcount(_M_ptr, __tag, std::forward<_Args>(__args)...)
1777 { _M_enable_shared_from_this_with(_M_ptr); }
1778
1779 template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
1780 typename... _Args>
1781 friend __shared_ptr<_Tp1, _Lp1>
1782 __allocate_shared(const _Alloc& __a, _Args&&... __args);
1783
1784#if __glibcxx_shared_ptr_arrays >= 201707L // C++ >= 20 && HOSTED
1785 // This constructor is non-standard, it is used by allocate_shared<T[]>.
1786 template<typename _Alloc, typename _Init = const remove_extent_t<_Tp>*>
1787 __shared_ptr(const _Sp_counted_array_base<_Alloc>& __a,
1788 _Init __init = nullptr)
1789 : _M_ptr(), _M_refcount(_M_ptr, __a, __init)
1790 { }
1791#endif
1792
1793 // This constructor is used by __weak_ptr::lock() and
1794 // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t).
1795 __shared_ptr(const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t) noexcept
1796 : _M_refcount(__r._M_refcount, std::nothrow)
1797 {
1798 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr : nullptr;
1799 }
1800
1801 friend class __weak_ptr<_Tp, _Lp>;
1802
1803 private:
1804
1805 template<typename _Yp>
1806 using __esft_base_t = decltype(__enable_shared_from_this_base(
1807 std::declval<const __shared_count<_Lp>&>(),
1809
1810 // Detect an accessible and unambiguous enable_shared_from_this base.
1811 template<typename _Yp, typename = void>
1812 struct __has_esft_base
1813 : false_type { };
1814
1815 template<typename _Yp>
1816 struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
1817 : __not_<is_array<_Tp>> { }; // No enable shared_from_this for arrays
1818
1819 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1820 typename enable_if<__has_esft_base<_Yp2>::value>::type
1821 _M_enable_shared_from_this_with(_Yp* __p) noexcept
1822 {
1823 if (auto __base = __enable_shared_from_this_base(_M_refcount, __p))
1824 __base->_M_weak_assign(const_cast<_Yp2*>(__p), _M_refcount);
1825 }
1826
1827 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1828 typename enable_if<!__has_esft_base<_Yp2>::value>::type
1829 _M_enable_shared_from_this_with(_Yp*) noexcept
1830 { }
1831
1832 void*
1833 _M_get_deleter(const std::type_info& __ti) const noexcept
1834 { return _M_refcount._M_get_deleter(__ti); }
1835
1836 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1837 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1838
1839 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
1840 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1841
1842 template<typename _Del, typename _Tp1>
1843 friend _Del* get_deleter(const shared_ptr<_Tp1>&) noexcept;
1844
1845#ifdef __glibcxx_atomic_shared_ptr
1846 friend _Sp_atomic<shared_ptr<_Tp>>;
1847#endif
1848#ifdef __glibcxx_out_ptr
1849 template<typename, typename, typename...> friend class out_ptr_t;
1850#endif
1851
1852 element_type* _M_ptr; // Contained pointer.
1853 __shared_count<_Lp> _M_refcount; // Reference counter.
1854 };
1855
1856
1857 // 20.7.2.2.7 shared_ptr comparisons
1858 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1859 inline bool
1860 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
1861 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1862 { return __a.get() == __b.get(); }
1863
1864 template<typename _Tp, _Lock_policy _Lp>
1865 inline bool
1866 operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1867 { return !__a; }
1868
1869#ifdef __cpp_lib_three_way_comparison
1870 template<typename _Tp, typename _Up, _Lock_policy _Lp>
1871 inline strong_ordering
1872 operator<=>(const __shared_ptr<_Tp, _Lp>& __a,
1873 const __shared_ptr<_Up, _Lp>& __b) noexcept
1874 { return compare_three_way()(__a.get(), __b.get()); }
1875
1876 template<typename _Tp, _Lock_policy _Lp>
1877 inline strong_ordering
1878 operator<=>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1879 {
1880 using pointer = typename __shared_ptr<_Tp, _Lp>::element_type*;
1881 return compare_three_way()(__a.get(), static_cast<pointer>(nullptr));
1882 }
1883#else
1884 template<typename _Tp, _Lock_policy _Lp>
1885 inline bool
1886 operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1887 { return !__a; }
1888
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 (bool)__a; }
1899
1900 template<typename _Tp, _Lock_policy _Lp>
1901 inline bool
1902 operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1903 { return (bool)__a; }
1904
1905 template<typename _Tp, typename _Up, _Lock_policy _Lp>
1906 inline bool
1907 operator<(const __shared_ptr<_Tp, _Lp>& __a,
1908 const __shared_ptr<_Up, _Lp>& __b) noexcept
1909 {
1910 using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1911 using _Up_elt = typename __shared_ptr<_Up, _Lp>::element_type;
1912 using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type;
1913 return less<_Vp>()(__a.get(), __b.get());
1914 }
1915
1916 template<typename _Tp, _Lock_policy _Lp>
1917 inline bool
1918 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1919 {
1920 using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1921 return less<_Tp_elt*>()(__a.get(), nullptr);
1922 }
1923
1924 template<typename _Tp, _Lock_policy _Lp>
1925 inline bool
1926 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1927 {
1928 using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1929 return less<_Tp_elt*>()(nullptr, __a.get());
1930 }
1931
1932 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1933 inline bool
1934 operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1935 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1936 { return !(__b < __a); }
1937
1938 template<typename _Tp, _Lock_policy _Lp>
1939 inline bool
1940 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1941 { return !(nullptr < __a); }
1942
1943 template<typename _Tp, _Lock_policy _Lp>
1944 inline bool
1945 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1946 { return !(__a < nullptr); }
1947
1948 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1949 inline bool
1950 operator>(const __shared_ptr<_Tp1, _Lp>& __a,
1951 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1952 { return (__b < __a); }
1953
1954 template<typename _Tp, _Lock_policy _Lp>
1955 inline bool
1956 operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1957 { return nullptr < __a; }
1958
1959 template<typename _Tp, _Lock_policy _Lp>
1960 inline bool
1961 operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1962 { return __a < nullptr; }
1963
1964 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1965 inline bool
1966 operator>=(const __shared_ptr<_Tp1, _Lp>& __a,
1967 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1968 { return !(__a < __b); }
1969
1970 template<typename _Tp, _Lock_policy _Lp>
1971 inline bool
1972 operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1973 { return !(__a < nullptr); }
1974
1975 template<typename _Tp, _Lock_policy _Lp>
1976 inline bool
1977 operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1978 { return !(nullptr < __a); }
1979#endif // three-way comparison
1980
1981 // 20.7.2.2.8 shared_ptr specialized algorithms.
1982 template<typename _Tp, _Lock_policy _Lp>
1983 inline void
1984 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1985 { __a.swap(__b); }
1986
1987 // 20.7.2.2.9 shared_ptr casts
1988
1989 // The seemingly equivalent code:
1990 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1991 // will eventually result in undefined behaviour, attempting to
1992 // delete the same object twice.
1993 /// static_pointer_cast
1994 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1995 inline __shared_ptr<_Tp, _Lp>
1996 static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1997 {
1998 using _Sp = __shared_ptr<_Tp, _Lp>;
1999 return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
2000 }
2001
2002 // The seemingly equivalent code:
2003 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
2004 // will eventually result in undefined behaviour, attempting to
2005 // delete the same object twice.
2006 /// const_pointer_cast
2007 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
2008 inline __shared_ptr<_Tp, _Lp>
2009 const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
2010 {
2011 using _Sp = __shared_ptr<_Tp, _Lp>;
2012 return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
2013 }
2014
2015 // The seemingly equivalent code:
2016 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
2017 // will eventually result in undefined behaviour, attempting to
2018 // delete the same object twice.
2019 /// dynamic_pointer_cast
2020 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
2021 inline __shared_ptr<_Tp, _Lp>
2022 dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
2023 {
2024 using _Sp = __shared_ptr<_Tp, _Lp>;
2025 if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
2026 return _Sp(__r, __p);
2027 return _Sp();
2028 }
2029
2030#if __cplusplus > 201402L
2031 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
2032 inline __shared_ptr<_Tp, _Lp>
2033 reinterpret_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
2034 {
2035 using _Sp = __shared_ptr<_Tp, _Lp>;
2036 return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
2037 }
2038#endif
2039
2040 template<typename _Tp, _Lock_policy _Lp>
2041 class __weak_ptr
2042 {
2043 public:
2044 using element_type = typename remove_extent<_Tp>::type;
2045
2046 private:
2047 template<typename _Yp, typename _Res = void>
2048 using _Compatible = typename
2050
2051 // Constraint for assignment from shared_ptr and weak_ptr:
2052 template<typename _Yp>
2053 using _Assignable = _Compatible<_Yp, __weak_ptr&>;
2054
2055#pragma GCC diagnostic push
2056#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
2057 // Helper for construction/assignment:
2058 template<typename _Yp>
2059 static element_type*
2060 _S_safe_upcast(const __weak_ptr<_Yp, _Lp>& __r)
2061 {
2062 // We know that _Yp and _Tp are compatible, that is, either
2063 // _Yp* is convertible to _Tp* or _Yp is U[N] and _Tp is U cv [].
2064
2065 // If _Yp is the same as _Tp after removing extents and cv
2066 // qualifications, there's no pointer adjustments to do. This
2067 // also allows us to support incomplete types.
2068 using _At = typename remove_cv<typename remove_extent<_Tp>::type>::type;
2069 using _Bt = typename remove_cv<typename remove_extent<_Yp>::type>::type;
2070 if constexpr (is_same<_At, _Bt>::value)
2071 return __r._M_ptr;
2072 // If they're not the same type, but they're both scalars,
2073 // we again don't need any adjustment. This allows us to support e.g.
2074 // pointers to a differently cv qualified type X.
2075 else if constexpr (__and_<is_scalar<_At>, is_scalar<_Bt>>::value)
2076 return __r._M_ptr;
2077#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_is_virtual_base_of)
2078 // If _Tp is not a virtual base class of _Yp, the pointer
2079 // conversion does not require dereferencing __r._M_ptr; just
2080 // rely on the implicit conversion.
2081 else if constexpr (!__builtin_is_virtual_base_of(_Tp, _Yp))
2082 return __r._M_ptr;
2083#endif
2084 // Expensive path; must lock() and do the pointer conversion while
2085 // a shared_ptr keeps the pointee alive (because we may need
2086 // to dereference).
2087 else
2088 return __r.lock().get();
2089 }
2090#pragma GCC diagnostic pop
2091
2092 public:
2093 constexpr __weak_ptr() noexcept
2094 : _M_ptr(nullptr), _M_refcount()
2095 { }
2096
2097 __weak_ptr(const __weak_ptr&) noexcept = default;
2098
2099 ~__weak_ptr() = default;
2100
2101 // The "obvious" converting constructor implementation:
2102 //
2103 // template<typename _Tp1>
2104 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
2105 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
2106 // { }
2107 //
2108 // has a serious problem.
2109 //
2110 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
2111 // conversion may require access to *__r._M_ptr (virtual inheritance).
2112 //
2113 // It is not possible to avoid spurious access violations since
2114 // in multithreaded programs __r._M_ptr may be invalidated at any point.
2115 template<typename _Yp, typename = _Compatible<_Yp>>
2116 __weak_ptr(const __weak_ptr<_Yp, _Lp>& __r) noexcept
2117 : _M_ptr(_S_safe_upcast(__r)), _M_refcount(__r._M_refcount)
2118 { }
2119
2120 template<typename _Yp, typename = _Compatible<_Yp>>
2121 __weak_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
2122 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
2123 { }
2124
2125 __weak_ptr(__weak_ptr&& __r) noexcept
2126 : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount))
2127 { __r._M_ptr = nullptr; }
2128
2129 template<typename _Yp, typename = _Compatible<_Yp>>
2130 __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
2131 : _M_ptr(_S_safe_upcast(__r)), _M_refcount(std::move(__r._M_refcount))
2132 { __r._M_ptr = nullptr; }
2133
2134 __weak_ptr&
2135 operator=(const __weak_ptr& __r) noexcept = default;
2136
2137 template<typename _Yp>
2138 _Assignable<_Yp>
2139 operator=(const __weak_ptr<_Yp, _Lp>& __r) noexcept
2140 {
2141 _M_ptr = _S_safe_upcast(__r);
2142 _M_refcount = __r._M_refcount;
2143 return *this;
2144 }
2145
2146 template<typename _Yp>
2147 _Assignable<_Yp>
2148 operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
2149 {
2150 _M_ptr = __r._M_ptr;
2151 _M_refcount = __r._M_refcount;
2152 return *this;
2153 }
2154
2155 __weak_ptr&
2156 operator=(__weak_ptr&& __r) noexcept
2157 {
2158 __weak_ptr(std::move(__r)).swap(*this);
2159 return *this;
2160 }
2161
2162 template<typename _Yp>
2163 _Assignable<_Yp>
2164 operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept
2165 {
2166 _M_ptr = _S_safe_upcast(__r);
2167 _M_refcount = std::move(__r._M_refcount);
2168 __r._M_ptr = nullptr;
2169 return *this;
2170 }
2171
2172 __shared_ptr<_Tp, _Lp>
2173 lock() const noexcept
2174 { return __shared_ptr<_Tp, _Lp>(*this, std::nothrow); }
2175
2176 long
2177 use_count() const noexcept
2178 { return _M_refcount._M_get_use_count(); }
2179
2180 bool
2181 expired() const noexcept
2182 { return _M_refcount._M_get_use_count() == 0; }
2183
2184 template<typename _Tp1>
2185 bool
2186 owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const noexcept
2187 { return _M_refcount._M_less(__rhs._M_refcount); }
2188
2189 template<typename _Tp1>
2190 bool
2191 owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const noexcept
2192 { return _M_refcount._M_less(__rhs._M_refcount); }
2193
2194#ifdef __glibcxx_smart_ptr_owner_equality // >= C++26
2195 size_t owner_hash() const noexcept { return _M_refcount._M_owner_hash(); }
2196
2197 template<typename _Tp1>
2198 bool
2199 owner_equal(const __shared_ptr<_Tp1, _Lp> & __rhs) const noexcept
2200 { return _M_refcount == __rhs._M_refcount; }
2201
2202 template<typename _Tp1>
2203 bool
2204 owner_equal(const __weak_ptr<_Tp1, _Lp> & __rhs) const noexcept
2205 { return _M_refcount == __rhs._M_refcount; }
2206#endif
2207
2208 void
2209 reset() noexcept
2210 { __weak_ptr().swap(*this); }
2211
2212 void
2213 swap(__weak_ptr& __s) noexcept
2214 {
2215 std::swap(_M_ptr, __s._M_ptr);
2216 _M_refcount._M_swap(__s._M_refcount);
2217 }
2218
2219 private:
2220 // Used by __enable_shared_from_this.
2221 void
2222 _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept
2223 {
2224 if (use_count() == 0)
2225 {
2226 _M_ptr = __ptr;
2227 _M_refcount = __refcount;
2228 }
2229 }
2230
2231 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
2232 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
2233 friend class __enable_shared_from_this<_Tp, _Lp>;
2234 friend class enable_shared_from_this<_Tp>;
2235#ifdef __glibcxx_atomic_shared_ptr
2236 friend _Sp_atomic<weak_ptr<_Tp>>;
2237#endif
2238
2239 element_type* _M_ptr; // Contained pointer.
2240 __weak_count<_Lp> _M_refcount; // Reference counter.
2241 };
2242
2243 // 20.7.2.3.6 weak_ptr specialized algorithms.
2244 template<typename _Tp, _Lock_policy _Lp>
2245 inline void
2246 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
2247 { __a.swap(__b); }
2248
2249#pragma GCC diagnostic push
2250#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2251 template<typename _Tp, typename _Tp1>
2252 struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
2253 {
2254 bool
2255 operator()(const _Tp& __lhs, const _Tp& __rhs) const noexcept
2256 { return __lhs.owner_before(__rhs); }
2257
2258 bool
2259 operator()(const _Tp& __lhs, const _Tp1& __rhs) const noexcept
2260 { return __lhs.owner_before(__rhs); }
2261
2262 bool
2263 operator()(const _Tp1& __lhs, const _Tp& __rhs) const noexcept
2264 { return __lhs.owner_before(__rhs); }
2265 };
2266#pragma GCC diagnostic pop
2267
2268 template<>
2269 struct _Sp_owner_less<void, void>
2270 {
2271 template<typename _Tp, typename _Up>
2272 auto
2273 operator()(const _Tp& __lhs, const _Up& __rhs) const noexcept
2274 -> decltype(__lhs.owner_before(__rhs))
2275 { return __lhs.owner_before(__rhs); }
2276
2277 using is_transparent = void;
2278 };
2279
2280 template<typename _Tp, _Lock_policy _Lp>
2281 struct owner_less<__shared_ptr<_Tp, _Lp>>
2282 : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
2283 { };
2284
2285 template<typename _Tp, _Lock_policy _Lp>
2286 struct owner_less<__weak_ptr<_Tp, _Lp>>
2287 : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
2288 { };
2289
2290
2291 template<typename _Tp, _Lock_policy _Lp>
2292 class __enable_shared_from_this
2293 {
2294 protected:
2295 constexpr __enable_shared_from_this() noexcept { }
2296
2297 __enable_shared_from_this(const __enable_shared_from_this&) noexcept { }
2298
2299 __enable_shared_from_this&
2300 operator=(const __enable_shared_from_this&) noexcept
2301 { return *this; }
2302
2303 ~__enable_shared_from_this() { }
2304
2305 public:
2306 __shared_ptr<_Tp, _Lp>
2307 shared_from_this()
2308 { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
2309
2310 __shared_ptr<const _Tp, _Lp>
2311 shared_from_this() const
2312 { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
2313
2314#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
2315 __weak_ptr<_Tp, _Lp>
2316 weak_from_this() noexcept
2317 { return this->_M_weak_this; }
2318
2319 __weak_ptr<const _Tp, _Lp>
2320 weak_from_this() const noexcept
2321 { return this->_M_weak_this; }
2322#endif
2323
2324 private:
2325 template<typename _Tp1>
2326 void
2327 _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept
2328 { _M_weak_this._M_assign(__p, __n); }
2329
2330 friend const __enable_shared_from_this*
2331 __enable_shared_from_this_base(const __shared_count<_Lp>&,
2332 const __enable_shared_from_this* __p)
2333 { return __p; }
2334
2335 template<typename, _Lock_policy>
2336 friend class __shared_ptr;
2337
2338 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
2339 };
2340
2341 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy,
2342 typename _Alloc, typename... _Args>
2343 inline __shared_ptr<_Tp, _Lp>
2344 __allocate_shared(const _Alloc& __a, _Args&&... __args)
2345 {
2346 static_assert(!is_array<_Tp>::value, "make_shared<T[]> not supported");
2347
2348 return __shared_ptr<_Tp, _Lp>(_Sp_alloc_shared_tag<_Alloc>{__a},
2349 std::forward<_Args>(__args)...);
2350 }
2351
2352 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy,
2353 typename... _Args>
2354 inline __shared_ptr<_Tp, _Lp>
2355 __make_shared(_Args&&... __args)
2356 {
2357 typedef typename std::remove_const<_Tp>::type _Tp_nc;
2358 return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
2359 std::forward<_Args>(__args)...);
2360 }
2361
2362 /// std::hash specialization for __shared_ptr.
2363 template<typename _Tp, _Lock_policy _Lp>
2364 struct hash<__shared_ptr<_Tp, _Lp>>
2365 : public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
2366 {
2367 size_t
2368 operator()(const __shared_ptr<_Tp, _Lp>& __s) const noexcept
2369 {
2371 __s.get());
2372 }
2373 };
2374
2375_GLIBCXX_END_NAMESPACE_VERSION
2376} // namespace
2377
2378#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.