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