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