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