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-2017 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 #if __cpp_rtti
53 # include <typeinfo>
54 #endif
55 #include <bits/allocated_ptr.h>
56 #include <bits/refwrap.h>
57 #include <bits/stl_function.h>
58 #include <ext/aligned_buffer.h>
59 
60 namespace std _GLIBCXX_VISIBILITY(default)
61 {
62 _GLIBCXX_BEGIN_NAMESPACE_VERSION
63 
64 #if _GLIBCXX_USE_DEPRECATED
65  template<typename> class auto_ptr;
66 #endif
67 
68  /**
69  * @brief Exception possibly thrown by @c shared_ptr.
70  * @ingroup exceptions
71  */
73  {
74  public:
75  virtual char const* what() const noexcept;
76 
77  virtual ~bad_weak_ptr() noexcept;
78  };
79 
80  // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
81  inline void
82  __throw_bad_weak_ptr()
83  { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr()); }
84 
85  using __gnu_cxx::_Lock_policy;
86  using __gnu_cxx::__default_lock_policy;
87  using __gnu_cxx::_S_single;
88  using __gnu_cxx::_S_mutex;
89  using __gnu_cxx::_S_atomic;
90 
91  // Empty helper class except when the template argument is _S_mutex.
92  template<_Lock_policy _Lp>
93  class _Mutex_base
94  {
95  protected:
96  // The atomic policy uses fully-fenced builtins, single doesn't care.
97  enum { _S_need_barriers = 0 };
98  };
99 
100  template<>
101  class _Mutex_base<_S_mutex>
102  : public __gnu_cxx::__mutex
103  {
104  protected:
105  // This policy is used when atomic builtins are not available.
106  // The replacement atomic operations might not have the necessary
107  // memory barriers.
108  enum { _S_need_barriers = 1 };
109  };
110 
111  template<_Lock_policy _Lp = __default_lock_policy>
112  class _Sp_counted_base
113  : public _Mutex_base<_Lp>
114  {
115  public:
116  _Sp_counted_base() noexcept
117  : _M_use_count(1), _M_weak_count(1) { }
118 
119  virtual
120  ~_Sp_counted_base() noexcept
121  { }
122 
123  // Called when _M_use_count drops to zero, to release the resources
124  // managed by *this.
125  virtual void
126  _M_dispose() noexcept = 0;
127 
128  // Called when _M_weak_count drops to zero.
129  virtual void
130  _M_destroy() noexcept
131  { delete this; }
132 
133  virtual void*
134  _M_get_deleter(const std::type_info&) noexcept = 0;
135 
136  void
137  _M_add_ref_copy()
138  { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
139 
140  void
141  _M_add_ref_lock();
142 
143  bool
144  _M_add_ref_lock_nothrow();
145 
146  void
147  _M_release() noexcept
148  {
149  // Be race-detector-friendly. For more info see bits/c++config.
150  _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
151  if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
152  {
153  _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
154  _M_dispose();
155  // There must be a memory barrier between dispose() and destroy()
156  // to ensure that the effects of dispose() are observed in the
157  // thread that runs destroy().
158  // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
159  if (_Mutex_base<_Lp>::_S_need_barriers)
160  {
161  __atomic_thread_fence (__ATOMIC_ACQ_REL);
162  }
163 
164  // Be race-detector-friendly. For more info see bits/c++config.
165  _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
166  if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
167  -1) == 1)
168  {
169  _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
170  _M_destroy();
171  }
172  }
173  }
174 
175  void
176  _M_weak_add_ref() noexcept
177  { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
178 
179  void
180  _M_weak_release() noexcept
181  {
182  // Be race-detector-friendly. For more info see bits/c++config.
183  _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
184  if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
185  {
186  _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
187  if (_Mutex_base<_Lp>::_S_need_barriers)
188  {
189  // See _M_release(),
190  // destroy() must observe results of dispose()
191  __atomic_thread_fence (__ATOMIC_ACQ_REL);
192  }
193  _M_destroy();
194  }
195  }
196 
197  long
198  _M_get_use_count() const noexcept
199  {
200  // No memory barrier is used here so there is no synchronization
201  // with other threads.
202  return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
203  }
204 
205  private:
206  _Sp_counted_base(_Sp_counted_base const&) = delete;
207  _Sp_counted_base& operator=(_Sp_counted_base const&) = delete;
208 
209  _Atomic_word _M_use_count; // #shared
210  _Atomic_word _M_weak_count; // #weak + (#shared != 0)
211  };
212 
213  template<>
214  inline void
215  _Sp_counted_base<_S_single>::
216  _M_add_ref_lock()
217  {
218  if (_M_use_count == 0)
219  __throw_bad_weak_ptr();
220  ++_M_use_count;
221  }
222 
223  template<>
224  inline void
225  _Sp_counted_base<_S_mutex>::
226  _M_add_ref_lock()
227  {
228  __gnu_cxx::__scoped_lock sentry(*this);
229  if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
230  {
231  _M_use_count = 0;
232  __throw_bad_weak_ptr();
233  }
234  }
235 
236  template<>
237  inline void
238  _Sp_counted_base<_S_atomic>::
239  _M_add_ref_lock()
240  {
241  // Perform lock-free add-if-not-zero operation.
242  _Atomic_word __count = _M_get_use_count();
243  do
244  {
245  if (__count == 0)
246  __throw_bad_weak_ptr();
247  // Replace the current counter value with the old value + 1, as
248  // long as it's not changed meanwhile.
249  }
250  while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
251  true, __ATOMIC_ACQ_REL,
252  __ATOMIC_RELAXED));
253  }
254 
255  template<>
256  inline bool
257  _Sp_counted_base<_S_single>::
258  _M_add_ref_lock_nothrow()
259  {
260  if (_M_use_count == 0)
261  return false;
262  ++_M_use_count;
263  return true;
264  }
265 
266  template<>
267  inline bool
268  _Sp_counted_base<_S_mutex>::
269  _M_add_ref_lock_nothrow()
270  {
271  __gnu_cxx::__scoped_lock sentry(*this);
272  if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
273  {
274  _M_use_count = 0;
275  return false;
276  }
277  return true;
278  }
279 
280  template<>
281  inline bool
282  _Sp_counted_base<_S_atomic>::
283  _M_add_ref_lock_nothrow()
284  {
285  // Perform lock-free add-if-not-zero operation.
286  _Atomic_word __count = _M_get_use_count();
287  do
288  {
289  if (__count == 0)
290  return false;
291  // Replace the current counter value with the old value + 1, as
292  // long as it's not changed meanwhile.
293  }
294  while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
295  true, __ATOMIC_ACQ_REL,
296  __ATOMIC_RELAXED));
297  return true;
298  }
299 
300  template<>
301  inline void
302  _Sp_counted_base<_S_single>::_M_add_ref_copy()
303  { ++_M_use_count; }
304 
305  template<>
306  inline void
307  _Sp_counted_base<_S_single>::_M_release() noexcept
308  {
309  if (--_M_use_count == 0)
310  {
311  _M_dispose();
312  if (--_M_weak_count == 0)
313  _M_destroy();
314  }
315  }
316 
317  template<>
318  inline void
319  _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept
320  { ++_M_weak_count; }
321 
322  template<>
323  inline void
324  _Sp_counted_base<_S_single>::_M_weak_release() noexcept
325  {
326  if (--_M_weak_count == 0)
327  _M_destroy();
328  }
329 
330  template<>
331  inline long
332  _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept
333  { return _M_use_count; }
334 
335 
336  // Forward declarations.
337  template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
338  class __shared_ptr;
339 
340  template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
341  class __weak_ptr;
342 
343  template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
344  class __enable_shared_from_this;
345 
346  template<typename _Tp>
347  class shared_ptr;
348 
349  template<typename _Tp>
350  class weak_ptr;
351 
352  template<typename _Tp>
353  struct owner_less;
354 
355  template<typename _Tp>
357 
358  template<_Lock_policy _Lp = __default_lock_policy>
359  class __weak_count;
360 
361  template<_Lock_policy _Lp = __default_lock_policy>
362  class __shared_count;
363 
364 
365  // Counted ptr with no deleter or allocator support
366  template<typename _Ptr, _Lock_policy _Lp>
367  class _Sp_counted_ptr final : public _Sp_counted_base<_Lp>
368  {
369  public:
370  explicit
371  _Sp_counted_ptr(_Ptr __p) noexcept
372  : _M_ptr(__p) { }
373 
374  virtual void
375  _M_dispose() noexcept
376  { delete _M_ptr; }
377 
378  virtual void
379  _M_destroy() noexcept
380  { delete this; }
381 
382  virtual void*
383  _M_get_deleter(const std::type_info&) noexcept
384  { return nullptr; }
385 
386  _Sp_counted_ptr(const _Sp_counted_ptr&) = delete;
387  _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete;
388 
389  private:
390  _Ptr _M_ptr;
391  };
392 
393  template<>
394  inline void
395  _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
396 
397  template<>
398  inline void
399  _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
400 
401  template<>
402  inline void
403  _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
404 
405  template<int _Nm, typename _Tp,
406  bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
407  struct _Sp_ebo_helper;
408 
409  /// Specialization using EBO.
410  template<int _Nm, typename _Tp>
411  struct _Sp_ebo_helper<_Nm, _Tp, true> : private _Tp
412  {
413  explicit _Sp_ebo_helper(const _Tp& __tp) : _Tp(__tp) { }
414 
415  static _Tp&
416  _S_get(_Sp_ebo_helper& __eboh) { return static_cast<_Tp&>(__eboh); }
417  };
418 
419  /// Specialization not using EBO.
420  template<int _Nm, typename _Tp>
421  struct _Sp_ebo_helper<_Nm, _Tp, false>
422  {
423  explicit _Sp_ebo_helper(const _Tp& __tp) : _M_tp(__tp) { }
424 
425  static _Tp&
426  _S_get(_Sp_ebo_helper& __eboh)
427  { return __eboh._M_tp; }
428 
429  private:
430  _Tp _M_tp;
431  };
432 
433  // Support for custom deleter and/or allocator
434  template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
435  class _Sp_counted_deleter final : public _Sp_counted_base<_Lp>
436  {
437  class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
438  {
439  typedef _Sp_ebo_helper<0, _Deleter> _Del_base;
440  typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base;
441 
442  public:
443  _Impl(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept
444  : _M_ptr(__p), _Del_base(__d), _Alloc_base(__a)
445  { }
446 
447  _Deleter& _M_del() noexcept { return _Del_base::_S_get(*this); }
448  _Alloc& _M_alloc() noexcept { return _Alloc_base::_S_get(*this); }
449 
450  _Ptr _M_ptr;
451  };
452 
453  public:
454  using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
455 
456  // __d(__p) must not throw.
457  _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
458  : _M_impl(__p, __d, _Alloc()) { }
459 
460  // __d(__p) must not throw.
461  _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept
462  : _M_impl(__p, __d, __a) { }
463 
464  ~_Sp_counted_deleter() noexcept { }
465 
466  virtual void
467  _M_dispose() noexcept
468  { _M_impl._M_del()(_M_impl._M_ptr); }
469 
470  virtual void
471  _M_destroy() noexcept
472  {
473  __allocator_type __a(_M_impl._M_alloc());
474  __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
475  this->~_Sp_counted_deleter();
476  }
477 
478  virtual void*
479  _M_get_deleter(const std::type_info& __ti) noexcept
480  {
481 #if __cpp_rtti
482  // _GLIBCXX_RESOLVE_LIB_DEFECTS
483  // 2400. shared_ptr's get_deleter() should use addressof()
484  return __ti == typeid(_Deleter)
485  ? std::__addressof(_M_impl._M_del())
486  : nullptr;
487 #else
488  return nullptr;
489 #endif
490  }
491 
492  private:
493  _Impl _M_impl;
494  };
495 
496  // helpers for make_shared / allocate_shared
497 
498  struct _Sp_make_shared_tag { };
499 
500  template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
501  class _Sp_counted_ptr_inplace final : public _Sp_counted_base<_Lp>
502  {
503  class _Impl : _Sp_ebo_helper<0, _Alloc>
504  {
505  typedef _Sp_ebo_helper<0, _Alloc> _A_base;
506 
507  public:
508  explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
509 
510  _Alloc& _M_alloc() noexcept { return _A_base::_S_get(*this); }
511 
512  __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
513  };
514 
515  public:
516  using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
517 
518  template<typename... _Args>
519  _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
520  : _M_impl(__a)
521  {
522  // _GLIBCXX_RESOLVE_LIB_DEFECTS
523  // 2070. allocate_shared should use allocator_traits<A>::construct
525  std::forward<_Args>(__args)...); // might throw
526  }
527 
528  ~_Sp_counted_ptr_inplace() noexcept { }
529 
530  virtual void
531  _M_dispose() noexcept
532  {
533  allocator_traits<_Alloc>::destroy(_M_impl._M_alloc(), _M_ptr());
534  }
535 
536  // Override because the allocator needs to know the dynamic type
537  virtual void
538  _M_destroy() noexcept
539  {
540  __allocator_type __a(_M_impl._M_alloc());
541  __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
542  this->~_Sp_counted_ptr_inplace();
543  }
544 
545  // Sneaky trick so __shared_ptr can get the managed pointer
546  virtual void*
547  _M_get_deleter(const std::type_info& __ti) noexcept
548  {
549 #if __cpp_rtti
550  if (__ti == typeid(_Sp_make_shared_tag))
551  return const_cast<typename remove_cv<_Tp>::type*>(_M_ptr());
552 #endif
553  return nullptr;
554  }
555 
556  private:
557  _Tp* _M_ptr() noexcept { return _M_impl._M_storage._M_ptr(); }
558 
559  _Impl _M_impl;
560  };
561 
562  // The default deleter for shared_ptr<T[]> and shared_ptr<T[N]>.
563  struct __sp_array_delete
564  {
565  template<typename _Yp>
566  void operator()(_Yp* __p) const { delete[] __p; }
567  };
568 
569  template<_Lock_policy _Lp>
570  class __shared_count
571  {
572  public:
573  constexpr __shared_count() noexcept : _M_pi(0)
574  { }
575 
576  template<typename _Ptr>
577  explicit
578  __shared_count(_Ptr __p) : _M_pi(0)
579  {
580  __try
581  {
582  _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
583  }
584  __catch(...)
585  {
586  delete __p;
587  __throw_exception_again;
588  }
589  }
590 
591  template<typename _Ptr>
592  __shared_count(_Ptr __p, /* is_array = */ false_type)
593  : __shared_count(__p)
594  { }
595 
596  template<typename _Ptr>
597  __shared_count(_Ptr __p, /* is_array = */ true_type)
598  : __shared_count(__p, __sp_array_delete{}, allocator<void>())
599  { }
600 
601  template<typename _Ptr, typename _Deleter>
602  __shared_count(_Ptr __p, _Deleter __d)
603  : __shared_count(__p, std::move(__d), allocator<void>())
604  { }
605 
606  template<typename _Ptr, typename _Deleter, typename _Alloc>
607  __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
608  {
609  typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
610  __try
611  {
612  typename _Sp_cd_type::__allocator_type __a2(__a);
613  auto __guard = std::__allocate_guarded(__a2);
614  _Sp_cd_type* __mem = __guard.get();
615  ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a));
616  _M_pi = __mem;
617  __guard = nullptr;
618  }
619  __catch(...)
620  {
621  __d(__p); // Call _Deleter on __p.
622  __throw_exception_again;
623  }
624  }
625 
626  template<typename _Tp, typename _Alloc, typename... _Args>
627  __shared_count(_Sp_make_shared_tag, _Tp*, const _Alloc& __a,
628  _Args&&... __args)
629  : _M_pi(0)
630  {
631  typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
632  typename _Sp_cp_type::__allocator_type __a2(__a);
633  auto __guard = std::__allocate_guarded(__a2);
634  _Sp_cp_type* __mem = __guard.get();
635  ::new (__mem) _Sp_cp_type(std::move(__a),
636  std::forward<_Args>(__args)...);
637  _M_pi = __mem;
638  __guard = nullptr;
639  }
640 
641 #if _GLIBCXX_USE_DEPRECATED
642  // Special case for auto_ptr<_Tp> to provide the strong guarantee.
643  template<typename _Tp>
644  explicit
645  __shared_count(std::auto_ptr<_Tp>&& __r);
646 #endif
647 
648  // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
649  template<typename _Tp, typename _Del>
650  explicit
651  __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0)
652  {
653  // _GLIBCXX_RESOLVE_LIB_DEFECTS
654  // 2415. Inconsistency between unique_ptr and shared_ptr
655  if (__r.get() == nullptr)
656  return;
657 
658  using _Ptr = typename unique_ptr<_Tp, _Del>::pointer;
659  using _Del2 = typename conditional<is_reference<_Del>::value,
661  _Del>::type;
662  using _Sp_cd_type
663  = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
664  using _Alloc = allocator<_Sp_cd_type>;
665  using _Alloc_traits = allocator_traits<_Alloc>;
666  _Alloc __a;
667  _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
668  _Alloc_traits::construct(__a, __mem, __r.release(),
669  __r.get_deleter()); // non-throwing
670  _M_pi = __mem;
671  }
672 
673  // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
674  explicit __shared_count(const __weak_count<_Lp>& __r);
675 
676  // Does not throw if __r._M_get_use_count() == 0, caller must check.
677  explicit __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t);
678 
679  ~__shared_count() noexcept
680  {
681  if (_M_pi != nullptr)
682  _M_pi->_M_release();
683  }
684 
685  __shared_count(const __shared_count& __r) noexcept
686  : _M_pi(__r._M_pi)
687  {
688  if (_M_pi != 0)
689  _M_pi->_M_add_ref_copy();
690  }
691 
692  __shared_count&
693  operator=(const __shared_count& __r) noexcept
694  {
695  _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
696  if (__tmp != _M_pi)
697  {
698  if (__tmp != 0)
699  __tmp->_M_add_ref_copy();
700  if (_M_pi != 0)
701  _M_pi->_M_release();
702  _M_pi = __tmp;
703  }
704  return *this;
705  }
706 
707  void
708  _M_swap(__shared_count& __r) noexcept
709  {
710  _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
711  __r._M_pi = _M_pi;
712  _M_pi = __tmp;
713  }
714 
715  long
716  _M_get_use_count() const noexcept
717  { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
718 
719  bool
720  _M_unique() const noexcept
721  { return this->_M_get_use_count() == 1; }
722 
723  void*
724  _M_get_deleter(const std::type_info& __ti) const noexcept
725  { return _M_pi ? _M_pi->_M_get_deleter(__ti) : nullptr; }
726 
727  bool
728  _M_less(const __shared_count& __rhs) const noexcept
729  { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
730 
731  bool
732  _M_less(const __weak_count<_Lp>& __rhs) const noexcept
733  { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
734 
735  // Friend function injected into enclosing namespace and found by ADL
736  friend inline bool
737  operator==(const __shared_count& __a, const __shared_count& __b) noexcept
738  { return __a._M_pi == __b._M_pi; }
739 
740  private:
741  friend class __weak_count<_Lp>;
742 
743  _Sp_counted_base<_Lp>* _M_pi;
744  };
745 
746 
747  template<_Lock_policy _Lp>
748  class __weak_count
749  {
750  public:
751  constexpr __weak_count() noexcept : _M_pi(nullptr)
752  { }
753 
754  __weak_count(const __shared_count<_Lp>& __r) noexcept
755  : _M_pi(__r._M_pi)
756  {
757  if (_M_pi != nullptr)
758  _M_pi->_M_weak_add_ref();
759  }
760 
761  __weak_count(const __weak_count& __r) noexcept
762  : _M_pi(__r._M_pi)
763  {
764  if (_M_pi != nullptr)
765  _M_pi->_M_weak_add_ref();
766  }
767 
768  __weak_count(__weak_count&& __r) noexcept
769  : _M_pi(__r._M_pi)
770  { __r._M_pi = nullptr; }
771 
772  ~__weak_count() noexcept
773  {
774  if (_M_pi != nullptr)
775  _M_pi->_M_weak_release();
776  }
777 
778  __weak_count&
779  operator=(const __shared_count<_Lp>& __r) noexcept
780  {
781  _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
782  if (__tmp != nullptr)
783  __tmp->_M_weak_add_ref();
784  if (_M_pi != nullptr)
785  _M_pi->_M_weak_release();
786  _M_pi = __tmp;
787  return *this;
788  }
789 
790  __weak_count&
791  operator=(const __weak_count& __r) noexcept
792  {
793  _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
794  if (__tmp != nullptr)
795  __tmp->_M_weak_add_ref();
796  if (_M_pi != nullptr)
797  _M_pi->_M_weak_release();
798  _M_pi = __tmp;
799  return *this;
800  }
801 
802  __weak_count&
803  operator=(__weak_count&& __r) noexcept
804  {
805  if (_M_pi != nullptr)
806  _M_pi->_M_weak_release();
807  _M_pi = __r._M_pi;
808  __r._M_pi = nullptr;
809  return *this;
810  }
811 
812  void
813  _M_swap(__weak_count& __r) noexcept
814  {
815  _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
816  __r._M_pi = _M_pi;
817  _M_pi = __tmp;
818  }
819 
820  long
821  _M_get_use_count() const noexcept
822  { return _M_pi != nullptr ? _M_pi->_M_get_use_count() : 0; }
823 
824  bool
825  _M_less(const __weak_count& __rhs) const noexcept
826  { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
827 
828  bool
829  _M_less(const __shared_count<_Lp>& __rhs) const noexcept
830  { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
831 
832  // Friend function injected into enclosing namespace and found by ADL
833  friend inline bool
834  operator==(const __weak_count& __a, const __weak_count& __b) noexcept
835  { return __a._M_pi == __b._M_pi; }
836 
837  private:
838  friend class __shared_count<_Lp>;
839 
840  _Sp_counted_base<_Lp>* _M_pi;
841  };
842 
843  // Now that __weak_count is defined we can define this constructor:
844  template<_Lock_policy _Lp>
845  inline
846  __shared_count<_Lp>::__shared_count(const __weak_count<_Lp>& __r)
847  : _M_pi(__r._M_pi)
848  {
849  if (_M_pi != nullptr)
850  _M_pi->_M_add_ref_lock();
851  else
852  __throw_bad_weak_ptr();
853  }
854 
855  // Now that __weak_count is defined we can define this constructor:
856  template<_Lock_policy _Lp>
857  inline
858  __shared_count<_Lp>::
859  __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t)
860  : _M_pi(__r._M_pi)
861  {
862  if (_M_pi != nullptr)
863  if (!_M_pi->_M_add_ref_lock_nothrow())
864  _M_pi = nullptr;
865  }
866 
867 #define __cpp_lib_shared_ptr_arrays 201603
868 
869  // Helper traits for shared_ptr of array:
870 
871  // A pointer type Y* is said to be compatible with a pointer type T* when
872  // either Y* is convertible to T* or Y is U[N] and T is U cv [].
873  template<typename _Yp_ptr, typename _Tp_ptr>
874  struct __sp_compatible_with
875  : false_type
876  { };
877 
878  template<typename _Yp, typename _Tp>
879  struct __sp_compatible_with<_Yp*, _Tp*>
880  : is_convertible<_Yp*, _Tp*>::type
881  { };
882 
883  template<typename _Up, size_t _Nm>
884  struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
885  : true_type
886  { };
887 
888  template<typename _Up, size_t _Nm>
889  struct __sp_compatible_with<_Up(*)[_Nm], const _Up(*)[]>
890  : true_type
891  { };
892 
893  template<typename _Up, size_t _Nm>
894  struct __sp_compatible_with<_Up(*)[_Nm], volatile _Up(*)[]>
895  : true_type
896  { };
897 
898  template<typename _Up, size_t _Nm>
899  struct __sp_compatible_with<_Up(*)[_Nm], const volatile _Up(*)[]>
900  : true_type
901  { };
902 
903  // Test conversion from Y(*)[N] to U(*)[N] without forming invalid type Y[N].
904  template<typename _Up, size_t _Nm, typename _Yp, typename = void>
905  struct __sp_is_constructible_arrN
906  : false_type
907  { };
908 
909  template<typename _Up, size_t _Nm, typename _Yp>
910  struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
911  : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
912  { };
913 
914  // Test conversion from Y(*)[] to U(*)[] without forming invalid type Y[].
915  template<typename _Up, typename _Yp, typename = void>
916  struct __sp_is_constructible_arr
917  : false_type
918  { };
919 
920  template<typename _Up, typename _Yp>
921  struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
922  : is_convertible<_Yp(*)[], _Up(*)[]>::type
923  { };
924 
925  // Trait to check if shared_ptr<T> can be constructed from Y*.
926  template<typename _Tp, typename _Yp>
927  struct __sp_is_constructible;
928 
929  // When T is U[N], Y(*)[N] shall be convertible to T*;
930  template<typename _Up, size_t _Nm, typename _Yp>
931  struct __sp_is_constructible<_Up[_Nm], _Yp>
932  : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
933  { };
934 
935  // when T is U[], Y(*)[] shall be convertible to T*;
936  template<typename _Up, typename _Yp>
937  struct __sp_is_constructible<_Up[], _Yp>
938  : __sp_is_constructible_arr<_Up, _Yp>::type
939  { };
940 
941  // otherwise, Y* shall be convertible to T*.
942  template<typename _Tp, typename _Yp>
943  struct __sp_is_constructible
944  : is_convertible<_Yp*, _Tp*>::type
945  { };
946 
947 
948  // Define operator* and operator-> for shared_ptr<T>.
949  template<typename _Tp, _Lock_policy _Lp,
951  class __shared_ptr_access
952  {
953  public:
954  using element_type = _Tp;
955 
956  element_type&
957  operator*() const noexcept
958  {
959  __glibcxx_assert(_M_get() != nullptr);
960  return *_M_get();
961  }
962 
963  element_type*
964  operator->() const noexcept
965  {
966  _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
967  return _M_get();
968  }
969 
970  private:
971  element_type*
972  _M_get() const noexcept
973  { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
974  };
975 
976  // Define operator-> for shared_ptr<cv void>.
977  template<typename _Tp, _Lock_policy _Lp>
978  class __shared_ptr_access<_Tp, _Lp, false, true>
979  {
980  public:
981  using element_type = _Tp;
982 
983  element_type*
984  operator->() const noexcept
985  {
986  auto __ptr = static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get();
987  _GLIBCXX_DEBUG_PEDASSERT(__ptr != nullptr);
988  return __ptr;
989  }
990  };
991 
992  // Define operator[] for shared_ptr<T[]> and shared_ptr<T[N]>.
993  template<typename _Tp, _Lock_policy _Lp>
994  class __shared_ptr_access<_Tp, _Lp, true, false>
995  {
996  public:
997  using element_type = typename remove_extent<_Tp>::type;
998 
999 #if __cplusplus <= 201402L
1000  [[__deprecated__("shared_ptr<T[]>::operator* is absent from C++17")]]
1001  element_type&
1002  operator*() const noexcept
1003  {
1004  __glibcxx_assert(_M_get() != nullptr);
1005  return *_M_get();
1006  }
1007 
1008  [[__deprecated__("shared_ptr<T[]>::operator-> is absent from C++17")]]
1009  element_type*
1010  operator->() const noexcept
1011  {
1012  _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
1013  return _M_get();
1014  }
1015 #endif
1016 
1017  element_type&
1018  operator[](ptrdiff_t __i) const
1019  {
1020  __glibcxx_assert(_M_get() != nullptr);
1021  __glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value);
1022  return _M_get()[__i];
1023  }
1024 
1025  private:
1026  element_type*
1027  _M_get() const noexcept
1028  { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
1029  };
1030 
1031  template<typename _Tp, _Lock_policy _Lp>
1032  class __shared_ptr
1033  : public __shared_ptr_access<_Tp, _Lp>
1034  {
1035  public:
1036  using element_type = typename remove_extent<_Tp>::type;
1037 
1038  private:
1039  // Constraint for taking ownership of a pointer of type _Yp*:
1040  template<typename _Yp>
1041  using _SafeConv
1042  = typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
1043 
1044  // Constraint for construction from shared_ptr and weak_ptr:
1045  template<typename _Yp, typename _Res = void>
1046  using _Compatible = typename
1047  enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1048 
1049  // Constraint for assignment from shared_ptr and weak_ptr:
1050  template<typename _Yp>
1051  using _Assignable = _Compatible<_Yp, __shared_ptr&>;
1052 
1053  // Constraint for construction from unique_ptr:
1054  template<typename _Yp, typename _Del, typename _Res = void,
1055  typename _Ptr = typename unique_ptr<_Yp, _Del>::pointer>
1056  using _UniqCompatible = typename enable_if<__and_<
1057  __sp_compatible_with<_Yp*, _Tp*>, is_convertible<_Ptr, element_type*>
1058  >::value, _Res>::type;
1059 
1060  // Constraint for assignment from unique_ptr:
1061  template<typename _Yp, typename _Del>
1062  using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
1063 
1064  public:
1065 
1066 #if __cplusplus > 201402L
1067  using weak_type = __weak_ptr<_Tp, _Lp>;
1068 #endif
1069 
1070  constexpr __shared_ptr() noexcept
1071  : _M_ptr(0), _M_refcount()
1072  { }
1073 
1074  template<typename _Yp, typename = _SafeConv<_Yp>>
1075  explicit
1076  __shared_ptr(_Yp* __p)
1077  : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
1078  {
1079  static_assert( !is_void<_Yp>::value, "incomplete type" );
1080  static_assert( sizeof(_Yp) > 0, "incomplete type" );
1081  _M_enable_shared_from_this_with(__p);
1082  }
1083 
1084  template<typename _Yp, typename _Deleter, typename = _SafeConv<_Yp>>
1085  __shared_ptr(_Yp* __p, _Deleter __d)
1086  : _M_ptr(__p), _M_refcount(__p, __d)
1087  {
1088  static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1089  "deleter expression d(p) is well-formed");
1090  _M_enable_shared_from_this_with(__p);
1091  }
1092 
1093  template<typename _Yp, typename _Deleter, typename _Alloc,
1094  typename = _SafeConv<_Yp>>
1095  __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
1096  : _M_ptr(__p), _M_refcount(__p, __d, std::move(__a))
1097  {
1098  static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1099  "deleter expression d(p) is well-formed");
1100  _M_enable_shared_from_this_with(__p);
1101  }
1102 
1103  template<typename _Deleter>
1104  __shared_ptr(nullptr_t __p, _Deleter __d)
1105  : _M_ptr(0), _M_refcount(__p, __d)
1106  { }
1107 
1108  template<typename _Deleter, typename _Alloc>
1109  __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
1110  : _M_ptr(0), _M_refcount(__p, __d, std::move(__a))
1111  { }
1112 
1113  template<typename _Yp>
1114  __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r,
1115  element_type* __p) noexcept
1116  : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
1117  { }
1118 
1119  __shared_ptr(const __shared_ptr&) noexcept = default;
1120  __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
1121  ~__shared_ptr() = default;
1122 
1123  template<typename _Yp, typename = _Compatible<_Yp>>
1124  __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1125  : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1126  { }
1127 
1128  __shared_ptr(__shared_ptr&& __r) noexcept
1129  : _M_ptr(__r._M_ptr), _M_refcount()
1130  {
1131  _M_refcount._M_swap(__r._M_refcount);
1132  __r._M_ptr = 0;
1133  }
1134 
1135  template<typename _Yp, typename = _Compatible<_Yp>>
1136  __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1137  : _M_ptr(__r._M_ptr), _M_refcount()
1138  {
1139  _M_refcount._M_swap(__r._M_refcount);
1140  __r._M_ptr = 0;
1141  }
1142 
1143  template<typename _Yp, typename = _Compatible<_Yp>>
1144  explicit __shared_ptr(const __weak_ptr<_Yp, _Lp>& __r)
1145  : _M_refcount(__r._M_refcount) // may throw
1146  {
1147  // It is now safe to copy __r._M_ptr, as
1148  // _M_refcount(__r._M_refcount) did not throw.
1149  _M_ptr = __r._M_ptr;
1150  }
1151 
1152  // If an exception is thrown this constructor has no effect.
1153  template<typename _Yp, typename _Del,
1154  typename = _UniqCompatible<_Yp, _Del>>
1155  __shared_ptr(unique_ptr<_Yp, _Del>&& __r)
1156  : _M_ptr(__r.get()), _M_refcount()
1157  {
1158  auto __raw = _S_raw_ptr(__r.get());
1159  _M_refcount = __shared_count<_Lp>(std::move(__r));
1160  _M_enable_shared_from_this_with(__raw);
1161  }
1162 
1163 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
1164  protected:
1165  // If an exception is thrown this constructor has no effect.
1166  template<typename _Tp1, typename _Del,
1167  typename enable_if<__and_<
1168  __not_<is_array<_Tp>>, is_array<_Tp1>,
1169  is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
1170  >::value, bool>::type = true>
1171  __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
1172  : _M_ptr(__r.get()), _M_refcount()
1173  {
1174  auto __raw = _S_raw_ptr(__r.get());
1175  _M_refcount = __shared_count<_Lp>(std::move(__r));
1176  _M_enable_shared_from_this_with(__raw);
1177  }
1178  public:
1179 #endif
1180 
1181 #if _GLIBCXX_USE_DEPRECATED
1182  // Postcondition: use_count() == 1 and __r.get() == 0
1183  template<typename _Yp, typename = _Compatible<_Yp>>
1184  __shared_ptr(auto_ptr<_Yp>&& __r);
1185 #endif
1186 
1187  constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
1188 
1189  template<typename _Yp>
1190  _Assignable<_Yp>
1191  operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1192  {
1193  _M_ptr = __r._M_ptr;
1194  _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
1195  return *this;
1196  }
1197 
1198 #if _GLIBCXX_USE_DEPRECATED
1199  template<typename _Yp>
1200  _Assignable<_Yp>
1201  operator=(auto_ptr<_Yp>&& __r)
1202  {
1203  __shared_ptr(std::move(__r)).swap(*this);
1204  return *this;
1205  }
1206 #endif
1207 
1208  __shared_ptr&
1209  operator=(__shared_ptr&& __r) noexcept
1210  {
1211  __shared_ptr(std::move(__r)).swap(*this);
1212  return *this;
1213  }
1214 
1215  template<class _Yp>
1216  _Assignable<_Yp>
1217  operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1218  {
1219  __shared_ptr(std::move(__r)).swap(*this);
1220  return *this;
1221  }
1222 
1223  template<typename _Yp, typename _Del>
1224  _UniqAssignable<_Yp, _Del>
1225  operator=(unique_ptr<_Yp, _Del>&& __r)
1226  {
1227  __shared_ptr(std::move(__r)).swap(*this);
1228  return *this;
1229  }
1230 
1231  void
1232  reset() noexcept
1233  { __shared_ptr().swap(*this); }
1234 
1235  template<typename _Yp>
1236  _SafeConv<_Yp>
1237  reset(_Yp* __p) // _Yp must be complete.
1238  {
1239  // Catch self-reset errors.
1240  __glibcxx_assert(__p == 0 || __p != _M_ptr);
1241  __shared_ptr(__p).swap(*this);
1242  }
1243 
1244  template<typename _Yp, typename _Deleter>
1245  _SafeConv<_Yp>
1246  reset(_Yp* __p, _Deleter __d)
1247  { __shared_ptr(__p, __d).swap(*this); }
1248 
1249  template<typename _Yp, typename _Deleter, typename _Alloc>
1250  _SafeConv<_Yp>
1251  reset(_Yp* __p, _Deleter __d, _Alloc __a)
1252  { __shared_ptr(__p, __d, std::move(__a)).swap(*this); }
1253 
1254  element_type*
1255  get() const noexcept
1256  { return _M_ptr; }
1257 
1258  explicit operator bool() const // never throws
1259  { return _M_ptr == 0 ? false : true; }
1260 
1261  bool
1262  unique() const noexcept
1263  { return _M_refcount._M_unique(); }
1264 
1265  long
1266  use_count() const noexcept
1267  { return _M_refcount._M_get_use_count(); }
1268 
1269  void
1270  swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
1271  {
1272  std::swap(_M_ptr, __other._M_ptr);
1273  _M_refcount._M_swap(__other._M_refcount);
1274  }
1275 
1276  template<typename _Tp1>
1277  bool
1278  owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const
1279  { return _M_refcount._M_less(__rhs._M_refcount); }
1280 
1281  template<typename _Tp1>
1282  bool
1283  owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const
1284  { return _M_refcount._M_less(__rhs._M_refcount); }
1285 
1286 #if __cpp_rtti
1287  protected:
1288  // This constructor is non-standard, it is used by allocate_shared.
1289  template<typename _Alloc, typename... _Args>
1290  __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
1291  _Args&&... __args)
1292  : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
1293  std::forward<_Args>(__args)...)
1294  {
1295  // _M_ptr needs to point to the newly constructed object.
1296  // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
1297  void* __p = _M_refcount._M_get_deleter(typeid(__tag));
1298  _M_ptr = static_cast<_Tp*>(__p);
1299  _M_enable_shared_from_this_with(_M_ptr);
1300  }
1301 #else
1302  template<typename _Alloc>
1303  struct _Deleter
1304  {
1305  void operator()(typename _Alloc::value_type* __ptr)
1306  {
1307  __allocated_ptr<_Alloc> __guard{ _M_alloc, __ptr };
1308  allocator_traits<_Alloc>::destroy(_M_alloc, __guard.get());
1309  }
1310  _Alloc _M_alloc;
1311  };
1312 
1313  template<typename _Alloc, typename... _Args>
1314  __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
1315  _Args&&... __args)
1316  : _M_ptr(), _M_refcount()
1317  {
1318  typedef typename allocator_traits<_Alloc>::template
1319  rebind_traits<typename std::remove_cv<_Tp>::type> __traits;
1320  _Deleter<typename __traits::allocator_type> __del = { __a };
1321  auto __guard = std::__allocate_guarded(__del._M_alloc);
1322  auto __ptr = __guard.get();
1323  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1324  // 2070. allocate_shared should use allocator_traits<A>::construct
1325  __traits::construct(__del._M_alloc, __ptr,
1326  std::forward<_Args>(__args)...);
1327  __guard = nullptr;
1328  __shared_count<_Lp> __count(__ptr, __del, __del._M_alloc);
1329  _M_refcount._M_swap(__count);
1330  _M_ptr = __ptr;
1331  _M_enable_shared_from_this_with(_M_ptr);
1332  }
1333 #endif
1334 
1335  template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
1336  typename... _Args>
1337  friend __shared_ptr<_Tp1, _Lp1>
1338  __allocate_shared(const _Alloc& __a, _Args&&... __args);
1339 
1340  // This constructor is used by __weak_ptr::lock() and
1341  // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t).
1342  __shared_ptr(const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t)
1343  : _M_refcount(__r._M_refcount, std::nothrow)
1344  {
1345  _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr : nullptr;
1346  }
1347 
1348  friend class __weak_ptr<_Tp, _Lp>;
1349 
1350  private:
1351 
1352  template<typename _Yp>
1353  using __esft_base_t = decltype(__enable_shared_from_this_base(
1354  std::declval<const __shared_count<_Lp>&>(),
1355  std::declval<_Yp*>()));
1356 
1357  // Detect an accessible and unambiguous enable_shared_from_this base.
1358  template<typename _Yp, typename = void>
1359  struct __has_esft_base
1360  : false_type { };
1361 
1362  template<typename _Yp>
1363  struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
1364  : __not_<is_array<_Tp>> { }; // No enable shared_from_this for arrays
1365 
1366  template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1367  typename enable_if<__has_esft_base<_Yp2>::value>::type
1368  _M_enable_shared_from_this_with(_Yp* __p) noexcept
1369  {
1370  if (auto __base = __enable_shared_from_this_base(_M_refcount, __p))
1371  __base->_M_weak_assign(const_cast<_Yp2*>(__p), _M_refcount);
1372  }
1373 
1374  template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1375  typename enable_if<!__has_esft_base<_Yp2>::value>::type
1376  _M_enable_shared_from_this_with(_Yp*) noexcept
1377  { }
1378 
1379  void*
1380  _M_get_deleter(const std::type_info& __ti) const noexcept
1381  { return _M_refcount._M_get_deleter(__ti); }
1382 
1383  template<typename _Tp1>
1384  static _Tp1*
1385  _S_raw_ptr(_Tp1* __ptr)
1386  { return __ptr; }
1387 
1388  template<typename _Tp1>
1389  static auto
1390  _S_raw_ptr(_Tp1 __ptr) -> decltype(std::__addressof(*__ptr))
1391  { return std::__addressof(*__ptr); }
1392 
1393  template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1394  template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1395 
1396  template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
1397  friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1398 
1399  element_type* _M_ptr; // Contained pointer.
1400  __shared_count<_Lp> _M_refcount; // Reference counter.
1401  };
1402 
1403 
1404  // 20.7.2.2.7 shared_ptr comparisons
1405  template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1406  inline bool
1407  operator==(const __shared_ptr<_Tp1, _Lp>& __a,
1408  const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1409  { return __a.get() == __b.get(); }
1410 
1411  template<typename _Tp, _Lock_policy _Lp>
1412  inline bool
1413  operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1414  { return !__a; }
1415 
1416  template<typename _Tp, _Lock_policy _Lp>
1417  inline bool
1418  operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1419  { return !__a; }
1420 
1421  template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1422  inline bool
1423  operator!=(const __shared_ptr<_Tp1, _Lp>& __a,
1424  const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1425  { return __a.get() != __b.get(); }
1426 
1427  template<typename _Tp, _Lock_policy _Lp>
1428  inline bool
1429  operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1430  { return (bool)__a; }
1431 
1432  template<typename _Tp, _Lock_policy _Lp>
1433  inline bool
1434  operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1435  { return (bool)__a; }
1436 
1437  template<typename _Tp, typename _Up, _Lock_policy _Lp>
1438  inline bool
1439  operator<(const __shared_ptr<_Tp, _Lp>& __a,
1440  const __shared_ptr<_Up, _Lp>& __b) noexcept
1441  {
1442  using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1443  using _Up_elt = typename __shared_ptr<_Up, _Lp>::element_type;
1444  using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type;
1445  return less<_Vp>()(__a.get(), __b.get());
1446  }
1447 
1448  template<typename _Tp, _Lock_policy _Lp>
1449  inline bool
1450  operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1451  {
1452  using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1453  return less<_Tp_elt*>()(__a.get(), nullptr);
1454  }
1455 
1456  template<typename _Tp, _Lock_policy _Lp>
1457  inline bool
1458  operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1459  {
1460  using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1461  return less<_Tp_elt*>()(nullptr, __a.get());
1462  }
1463 
1464  template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1465  inline bool
1466  operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1467  const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1468  { return !(__b < __a); }
1469 
1470  template<typename _Tp, _Lock_policy _Lp>
1471  inline bool
1472  operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1473  { return !(nullptr < __a); }
1474 
1475  template<typename _Tp, _Lock_policy _Lp>
1476  inline bool
1477  operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1478  { return !(__a < nullptr); }
1479 
1480  template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1481  inline bool
1482  operator>(const __shared_ptr<_Tp1, _Lp>& __a,
1483  const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1484  { return (__b < __a); }
1485 
1486  template<typename _Tp, _Lock_policy _Lp>
1487  inline bool
1488  operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1489  { return nullptr < __a; }
1490 
1491  template<typename _Tp, _Lock_policy _Lp>
1492  inline bool
1493  operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1494  { return __a < nullptr; }
1495 
1496  template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1497  inline bool
1498  operator>=(const __shared_ptr<_Tp1, _Lp>& __a,
1499  const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1500  { return !(__a < __b); }
1501 
1502  template<typename _Tp, _Lock_policy _Lp>
1503  inline bool
1504  operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1505  { return !(__a < nullptr); }
1506 
1507  template<typename _Tp, _Lock_policy _Lp>
1508  inline bool
1509  operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1510  { return !(nullptr < __a); }
1511 
1512  template<typename _Sp>
1513  struct _Sp_less : public binary_function<_Sp, _Sp, bool>
1514  {
1515  bool
1516  operator()(const _Sp& __lhs, const _Sp& __rhs) const noexcept
1517  {
1518  typedef typename _Sp::element_type element_type;
1519  return std::less<element_type*>()(__lhs.get(), __rhs.get());
1520  }
1521  };
1522 
1523  template<typename _Tp, _Lock_policy _Lp>
1524  struct less<__shared_ptr<_Tp, _Lp>>
1525  : public _Sp_less<__shared_ptr<_Tp, _Lp>>
1526  { };
1527 
1528  // 20.7.2.2.8 shared_ptr specialized algorithms.
1529  template<typename _Tp, _Lock_policy _Lp>
1530  inline void
1531  swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1532  { __a.swap(__b); }
1533 
1534  // 20.7.2.2.9 shared_ptr casts
1535 
1536  // The seemingly equivalent code:
1537  // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1538  // will eventually result in undefined behaviour, attempting to
1539  // delete the same object twice.
1540  /// static_pointer_cast
1541  template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1542  inline __shared_ptr<_Tp, _Lp>
1543  static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1544  {
1545  using _Sp = __shared_ptr<_Tp, _Lp>;
1546  return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
1547  }
1548 
1549  // The seemingly equivalent code:
1550  // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
1551  // will eventually result in undefined behaviour, attempting to
1552  // delete the same object twice.
1553  /// const_pointer_cast
1554  template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1555  inline __shared_ptr<_Tp, _Lp>
1556  const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1557  {
1558  using _Sp = __shared_ptr<_Tp, _Lp>;
1559  return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
1560  }
1561 
1562  // The seemingly equivalent code:
1563  // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
1564  // will eventually result in undefined behaviour, attempting to
1565  // delete the same object twice.
1566  /// dynamic_pointer_cast
1567  template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1568  inline __shared_ptr<_Tp, _Lp>
1569  dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1570  {
1571  using _Sp = __shared_ptr<_Tp, _Lp>;
1572  if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
1573  return _Sp(__r, __p);
1574  return _Sp();
1575  }
1576 
1577 #if __cplusplus > 201402L
1578  template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1579  inline __shared_ptr<_Tp, _Lp>
1580  reinterpret_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1581  {
1582  using _Sp = __shared_ptr<_Tp, _Lp>;
1583  return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
1584  }
1585 #endif
1586 
1587  template<typename _Tp, _Lock_policy _Lp>
1588  class __weak_ptr
1589  {
1590  template<typename _Yp, typename _Res = void>
1591  using _Compatible = typename
1592  enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1593 
1594  // Constraint for assignment from shared_ptr and weak_ptr:
1595  template<typename _Yp>
1596  using _Assignable = _Compatible<_Yp, __weak_ptr&>;
1597 
1598  public:
1599  using element_type = typename remove_extent<_Tp>::type;
1600 
1601  constexpr __weak_ptr() noexcept
1602  : _M_ptr(nullptr), _M_refcount()
1603  { }
1604 
1605  __weak_ptr(const __weak_ptr&) noexcept = default;
1606 
1607  ~__weak_ptr() = default;
1608 
1609  // The "obvious" converting constructor implementation:
1610  //
1611  // template<typename _Tp1>
1612  // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1613  // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1614  // { }
1615  //
1616  // has a serious problem.
1617  //
1618  // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1619  // conversion may require access to *__r._M_ptr (virtual inheritance).
1620  //
1621  // It is not possible to avoid spurious access violations since
1622  // in multithreaded programs __r._M_ptr may be invalidated at any point.
1623  template<typename _Yp, typename = _Compatible<_Yp>>
1624  __weak_ptr(const __weak_ptr<_Yp, _Lp>& __r) noexcept
1625  : _M_refcount(__r._M_refcount)
1626  { _M_ptr = __r.lock().get(); }
1627 
1628  template<typename _Yp, typename = _Compatible<_Yp>>
1629  __weak_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1630  : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1631  { }
1632 
1633  __weak_ptr(__weak_ptr&& __r) noexcept
1634  : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount))
1635  { __r._M_ptr = nullptr; }
1636 
1637  template<typename _Yp, typename = _Compatible<_Yp>>
1638  __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1639  : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount))
1640  { __r._M_ptr = nullptr; }
1641 
1642  __weak_ptr&
1643  operator=(const __weak_ptr& __r) noexcept = default;
1644 
1645  template<typename _Yp>
1646  _Assignable<_Yp>
1647  operator=(const __weak_ptr<_Yp, _Lp>& __r) noexcept
1648  {
1649  _M_ptr = __r.lock().get();
1650  _M_refcount = __r._M_refcount;
1651  return *this;
1652  }
1653 
1654  template<typename _Yp>
1655  _Assignable<_Yp>
1656  operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1657  {
1658  _M_ptr = __r._M_ptr;
1659  _M_refcount = __r._M_refcount;
1660  return *this;
1661  }
1662 
1663  __weak_ptr&
1664  operator=(__weak_ptr&& __r) noexcept
1665  {
1666  _M_ptr = __r._M_ptr;
1667  _M_refcount = std::move(__r._M_refcount);
1668  __r._M_ptr = nullptr;
1669  return *this;
1670  }
1671 
1672  template<typename _Yp>
1673  _Assignable<_Yp>
1674  operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1675  {
1676  _M_ptr = __r.lock().get();
1677  _M_refcount = std::move(__r._M_refcount);
1678  __r._M_ptr = nullptr;
1679  return *this;
1680  }
1681 
1682  __shared_ptr<_Tp, _Lp>
1683  lock() const noexcept
1684  { return __shared_ptr<element_type, _Lp>(*this, std::nothrow); }
1685 
1686  long
1687  use_count() const noexcept
1688  { return _M_refcount._M_get_use_count(); }
1689 
1690  bool
1691  expired() const noexcept
1692  { return _M_refcount._M_get_use_count() == 0; }
1693 
1694  template<typename _Tp1>
1695  bool
1696  owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const
1697  { return _M_refcount._M_less(__rhs._M_refcount); }
1698 
1699  template<typename _Tp1>
1700  bool
1701  owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const
1702  { return _M_refcount._M_less(__rhs._M_refcount); }
1703 
1704  void
1705  reset() noexcept
1706  { __weak_ptr().swap(*this); }
1707 
1708  void
1709  swap(__weak_ptr& __s) noexcept
1710  {
1711  std::swap(_M_ptr, __s._M_ptr);
1712  _M_refcount._M_swap(__s._M_refcount);
1713  }
1714 
1715  private:
1716  // Used by __enable_shared_from_this.
1717  void
1718  _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept
1719  {
1720  if (use_count() == 0)
1721  {
1722  _M_ptr = __ptr;
1723  _M_refcount = __refcount;
1724  }
1725  }
1726 
1727  template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1728  template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1729  friend class __enable_shared_from_this<_Tp, _Lp>;
1730  friend class enable_shared_from_this<_Tp>;
1731 
1732  element_type* _M_ptr; // Contained pointer.
1733  __weak_count<_Lp> _M_refcount; // Reference counter.
1734  };
1735 
1736  // 20.7.2.3.6 weak_ptr specialized algorithms.
1737  template<typename _Tp, _Lock_policy _Lp>
1738  inline void
1739  swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1740  { __a.swap(__b); }
1741 
1742  template<typename _Tp, typename _Tp1>
1743  struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
1744  {
1745  bool
1746  operator()(const _Tp& __lhs, const _Tp& __rhs) const
1747  { return __lhs.owner_before(__rhs); }
1748 
1749  bool
1750  operator()(const _Tp& __lhs, const _Tp1& __rhs) const
1751  { return __lhs.owner_before(__rhs); }
1752 
1753  bool
1754  operator()(const _Tp1& __lhs, const _Tp& __rhs) const
1755  { return __lhs.owner_before(__rhs); }
1756  };
1757 
1758  template<>
1759  struct _Sp_owner_less<void, void>
1760  {
1761  template<typename _Tp, typename _Up>
1762  auto
1763  operator()(const _Tp& __lhs, const _Up& __rhs) const
1764  -> decltype(__lhs.owner_before(__rhs))
1765  { return __lhs.owner_before(__rhs); }
1766 
1767  using is_transparent = void;
1768  };
1769 
1770  template<typename _Tp, _Lock_policy _Lp>
1771  struct owner_less<__shared_ptr<_Tp, _Lp>>
1772  : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1773  { };
1774 
1775  template<typename _Tp, _Lock_policy _Lp>
1776  struct owner_less<__weak_ptr<_Tp, _Lp>>
1777  : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1778  { };
1779 
1780 
1781  template<typename _Tp, _Lock_policy _Lp>
1782  class __enable_shared_from_this
1783  {
1784  protected:
1785  constexpr __enable_shared_from_this() noexcept { }
1786 
1787  __enable_shared_from_this(const __enable_shared_from_this&) noexcept { }
1788 
1789  __enable_shared_from_this&
1790  operator=(const __enable_shared_from_this&) noexcept
1791  { return *this; }
1792 
1793  ~__enable_shared_from_this() { }
1794 
1795  public:
1796  __shared_ptr<_Tp, _Lp>
1797  shared_from_this()
1798  { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1799 
1800  __shared_ptr<const _Tp, _Lp>
1801  shared_from_this() const
1802  { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1803 
1804 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
1805  __weak_ptr<_Tp, _Lp>
1806  weak_from_this() noexcept
1807  { return this->_M_weak_this; }
1808 
1809  __weak_ptr<const _Tp, _Lp>
1810  weak_from_this() const noexcept
1811  { return this->_M_weak_this; }
1812 #endif
1813 
1814  private:
1815  template<typename _Tp1>
1816  void
1817  _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept
1818  { _M_weak_this._M_assign(__p, __n); }
1819 
1820  friend const __enable_shared_from_this*
1821  __enable_shared_from_this_base(const __shared_count<_Lp>&,
1822  const __enable_shared_from_this* __p)
1823  { return __p; }
1824 
1825  template<typename, _Lock_policy>
1826  friend class __shared_ptr;
1827 
1828  mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1829  };
1830 
1831  template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args>
1832  inline __shared_ptr<_Tp, _Lp>
1833  __allocate_shared(const _Alloc& __a, _Args&&... __args)
1834  {
1835  return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1836  std::forward<_Args>(__args)...);
1837  }
1838 
1839  template<typename _Tp, _Lock_policy _Lp, typename... _Args>
1840  inline __shared_ptr<_Tp, _Lp>
1841  __make_shared(_Args&&... __args)
1842  {
1843  typedef typename std::remove_const<_Tp>::type _Tp_nc;
1844  return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
1845  std::forward<_Args>(__args)...);
1846  }
1847 
1848  /// std::hash specialization for __shared_ptr.
1849  template<typename _Tp, _Lock_policy _Lp>
1850  struct hash<__shared_ptr<_Tp, _Lp>>
1851  : public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1852  {
1853  size_t
1854  operator()(const __shared_ptr<_Tp, _Lp>& __s) const noexcept
1855  {
1857  __s.get());
1858  }
1859  };
1860 
1861 _GLIBCXX_END_NAMESPACE_VERSION
1862 } // namespace
1863 
1864 #endif // _SHARED_PTR_BASE_H
The standard allocator, as per [20.4].
Definition: allocator.h:108
A simple smart pointer providing strict ownership semantics.
Definition: auto_ptr.h:87
virtual char const * what() const noexcept
complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
Definition: complex:386
Exception possibly thrown by shared_ptr.
Scoped lock idiom.
Definition: concurrence.h:231
Primary class template for reference_wrapper.
Definition: refwrap.h:311
integral_constant
Definition: type_traits:69
Base class allowing use of member function shared_from_this.
__allocated_ptr< _Alloc > __allocate_guarded(_Alloc &__a)
Allocate space for a single object using __a.
ISO C++ entities toplevel namespace is std.
A smart pointer with reference-counted copy semantics.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:47
A smart pointer with weak semantics.
20.7.1.2 unique_ptr for single objects.
Definition: unique_ptr.h:157
is_array
Definition: type_traits:362
allocator<void> specialization.
Definition: allocator.h:68
Part of RTTI.
Definition: typeinfo:88
One of the comparison functors.
Definition: stl_function.h:340
Base class for all library exceptions.
Definition: exception.h:60
Uniform interface to all allocator types.
static auto construct(_Alloc &__a, _Tp *__p, _Args &&...__args) -> decltype(_S_construct(__a, __p, std::forward< _Args >(__args)...))
Construct an object of type _Tp.
is_void
Definition: type_traits:217
static void destroy(_Alloc &__a, _Tp *__p)
Destroy an object of type _Tp.
_Del * get_deleter(const __shared_ptr< _Tp, _Lp > &__p) noexcept
20.7.2.2.10 shared_ptr get_deleter
Primary template owner_less.
void lock(_L1 &__l1, _L2 &__l2, _L3 &...__l3)
Generic lock.
Definition: mutex:542
Primary class template hash.
Definition: system_error:142
Non-standard RAII type for managing pointers obtained from allocators.
Definition: allocated_ptr.h:46