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