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