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