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-2016 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  _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
987  return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get();
988  }
989  };
990 
991  // Define operator[] for shared_ptr<T[]> and shared_ptr<T[N]>.
992  template<typename _Tp, _Lock_policy _Lp>
993  class __shared_ptr_access<_Tp, _Lp, true, false>
994  {
995  public:
996  using element_type = typename remove_extent<_Tp>::type;
997 
998 #if __cplusplus <= 201402L
999  [[__deprecated__("shared_ptr<T[]>::operator* is absent from C++17")]]
1000  element_type&
1001  operator*() const noexcept
1002  {
1003  __glibcxx_assert(_M_get() != nullptr);
1004  return *_M_get();
1005  }
1006 
1007  [[__deprecated__("shared_ptr<T[]>::operator-> is absent from C++17")]]
1008  element_type*
1009  operator->() const noexcept
1010  {
1011  _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
1012  return _M_get();
1013  }
1014 #endif
1015 
1016  element_type&
1017  operator[](ptrdiff_t __i) const
1018  {
1019  __glibcxx_assert(_M_get() != nullptr);
1020  __glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value);
1021  return _M_get()[__i];
1022  }
1023 
1024  private:
1025  element_type*
1026  _M_get() const noexcept
1027  { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
1028  };
1029 
1030  template<typename _Tp, _Lock_policy _Lp>
1031  class __shared_ptr
1032  : public __shared_ptr_access<_Tp, _Lp>
1033  {
1034  public:
1035  using element_type = typename remove_extent<_Tp>::type;
1036 
1037  private:
1038  // Constraint for taking ownership of a pointer of type _Yp*:
1039  template<typename _Yp>
1040  using _SafeConv
1041  = typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
1042 
1043  // Constraint for construction from shared_ptr and weak_ptr:
1044  template<typename _Yp, typename _Res = void>
1045  using _Compatible = typename
1046  enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1047 
1048  // Constraint for assignment from shared_ptr and weak_ptr:
1049  template<typename _Yp>
1050  using _Assignable = _Compatible<_Yp, __shared_ptr&>;
1051 
1052  // Constraint for construction from unique_ptr:
1053  template<typename _Yp, typename _Del, typename _Res = void,
1054  typename _Ptr = typename unique_ptr<_Yp, _Del>::pointer>
1055  using _UniqCompatible = typename enable_if<__and_<
1056  __sp_compatible_with<_Yp*, _Tp*>, is_convertible<_Ptr, element_type*>
1057  >::value, _Res>::type;
1058 
1059  // Constraint for assignment from unique_ptr:
1060  template<typename _Yp, typename _Del>
1061  using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
1062 
1063  public:
1064 
1065 #if __cplusplus > 201402L
1066  using weak_type = __weak_ptr<_Tp, _Lp>;
1067 #endif
1068 
1069  constexpr __shared_ptr() noexcept
1070  : _M_ptr(0), _M_refcount()
1071  { }
1072 
1073  template<typename _Yp, typename = _SafeConv<_Yp>>
1074  explicit
1075  __shared_ptr(_Yp* __p)
1076  : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
1077  {
1078  static_assert( !is_void<_Yp>::value, "incomplete type" );
1079  static_assert( sizeof(_Yp) > 0, "incomplete type" );
1080  _M_enable_shared_from_this_with(__p);
1081  }
1082 
1083  template<typename _Yp, typename _Deleter, typename = _SafeConv<_Yp>>
1084  __shared_ptr(_Yp* __p, _Deleter __d)
1085  : _M_ptr(__p), _M_refcount(__p, __d)
1086  {
1087  static_assert(__is_callable<_Deleter(_Yp*)>::value,
1088  "deleter expression d(p) is well-formed");
1089  _M_enable_shared_from_this_with(__p);
1090  }
1091 
1092  template<typename _Yp, typename _Deleter, typename _Alloc,
1093  typename = _SafeConv<_Yp>>
1094  __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
1095  : _M_ptr(__p), _M_refcount(__p, __d, std::move(__a))
1096  {
1097  static_assert(__is_callable<_Deleter(_Yp*)>::value,
1098  "deleter expression d(p) is well-formed");
1099  _M_enable_shared_from_this_with(__p);
1100  }
1101 
1102  template<typename _Deleter>
1103  __shared_ptr(nullptr_t __p, _Deleter __d)
1104  : _M_ptr(0), _M_refcount(__p, __d)
1105  { }
1106 
1107  template<typename _Deleter, typename _Alloc>
1108  __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
1109  : _M_ptr(0), _M_refcount(__p, __d, std::move(__a))
1110  { }
1111 
1112  template<typename _Yp>
1113  __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r,
1114  element_type* __p) noexcept
1115  : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
1116  { }
1117 
1118  __shared_ptr(const __shared_ptr&) noexcept = default;
1119  __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
1120  ~__shared_ptr() = default;
1121 
1122  template<typename _Yp, typename = _Compatible<_Yp>>
1123  __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1124  : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1125  { }
1126 
1127  __shared_ptr(__shared_ptr&& __r) noexcept
1128  : _M_ptr(__r._M_ptr), _M_refcount()
1129  {
1130  _M_refcount._M_swap(__r._M_refcount);
1131  __r._M_ptr = 0;
1132  }
1133 
1134  template<typename _Yp, typename = _Compatible<_Yp>>
1135  __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1136  : _M_ptr(__r._M_ptr), _M_refcount()
1137  {
1138  _M_refcount._M_swap(__r._M_refcount);
1139  __r._M_ptr = 0;
1140  }
1141 
1142  template<typename _Yp, typename = _Compatible<_Yp>>
1143  explicit __shared_ptr(const __weak_ptr<_Yp, _Lp>& __r)
1144  : _M_refcount(__r._M_refcount) // may throw
1145  {
1146  // It is now safe to copy __r._M_ptr, as
1147  // _M_refcount(__r._M_refcount) did not throw.
1148  _M_ptr = __r._M_ptr;
1149  }
1150 
1151  // If an exception is thrown this constructor has no effect.
1152  template<typename _Yp, typename _Del,
1153  typename = _UniqCompatible<_Yp, _Del>>
1154  __shared_ptr(unique_ptr<_Yp, _Del>&& __r)
1155  : _M_ptr(__r.get()), _M_refcount()
1156  {
1157  auto __raw = _S_raw_ptr(__r.get());
1158  _M_refcount = __shared_count<_Lp>(std::move(__r));
1159  _M_enable_shared_from_this_with(__raw);
1160  }
1161 
1162 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
1163  protected:
1164  // If an exception is thrown this constructor has no effect.
1165  template<typename _Tp1, typename _Del,
1166  typename enable_if<__and_<
1167  __not_<is_array<_Tp>>, is_array<_Tp1>,
1168  is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
1169  >::value, bool>::type = true>
1170  __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
1171  : _M_ptr(__r.get()), _M_refcount()
1172  {
1173  auto __raw = _S_raw_ptr(__r.get());
1174  _M_refcount = __shared_count<_Lp>(std::move(__r));
1175  _M_enable_shared_from_this_with(__raw);
1176  }
1177  public:
1178 #endif
1179 
1180 #if _GLIBCXX_USE_DEPRECATED
1181  // Postcondition: use_count() == 1 and __r.get() == 0
1182  template<typename _Yp, typename = _Compatible<_Yp>>
1183  __shared_ptr(auto_ptr<_Yp>&& __r);
1184 #endif
1185 
1186  constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
1187 
1188  template<typename _Yp>
1189  _Assignable<_Yp>
1190  operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1191  {
1192  _M_ptr = __r._M_ptr;
1193  _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
1194  return *this;
1195  }
1196 
1197 #if _GLIBCXX_USE_DEPRECATED
1198  template<typename _Yp>
1199  _Assignable<_Yp>
1200  operator=(auto_ptr<_Yp>&& __r)
1201  {
1202  __shared_ptr(std::move(__r)).swap(*this);
1203  return *this;
1204  }
1205 #endif
1206 
1207  __shared_ptr&
1208  operator=(__shared_ptr&& __r) noexcept
1209  {
1210  __shared_ptr(std::move(__r)).swap(*this);
1211  return *this;
1212  }
1213 
1214  template<class _Yp>
1215  _Assignable<_Yp>
1216  operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1217  {
1218  __shared_ptr(std::move(__r)).swap(*this);
1219  return *this;
1220  }
1221 
1222  template<typename _Yp, typename _Del>
1223  _UniqAssignable<_Yp, _Del>
1224  operator=(unique_ptr<_Yp, _Del>&& __r)
1225  {
1226  __shared_ptr(std::move(__r)).swap(*this);
1227  return *this;
1228  }
1229 
1230  void
1231  reset() noexcept
1232  { __shared_ptr().swap(*this); }
1233 
1234  template<typename _Yp>
1235  _SafeConv<_Yp>
1236  reset(_Yp* __p) // _Yp must be complete.
1237  {
1238  // Catch self-reset errors.
1239  __glibcxx_assert(__p == 0 || __p != _M_ptr);
1240  __shared_ptr(__p).swap(*this);
1241  }
1242 
1243  template<typename _Yp, typename _Deleter>
1244  _SafeConv<_Yp>
1245  reset(_Yp* __p, _Deleter __d)
1246  { __shared_ptr(__p, __d).swap(*this); }
1247 
1248  template<typename _Yp, typename _Deleter, typename _Alloc>
1249  _SafeConv<_Yp>
1250  reset(_Yp* __p, _Deleter __d, _Alloc __a)
1251  { __shared_ptr(__p, __d, std::move(__a)).swap(*this); }
1252 
1253  element_type*
1254  get() const noexcept
1255  { return _M_ptr; }
1256 
1257  explicit operator bool() const // never throws
1258  { return _M_ptr == 0 ? false : true; }
1259 
1260  bool
1261  unique() const noexcept
1262  { return _M_refcount._M_unique(); }
1263 
1264  long
1265  use_count() const noexcept
1266  { return _M_refcount._M_get_use_count(); }
1267 
1268  void
1269  swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
1270  {
1271  std::swap(_M_ptr, __other._M_ptr);
1272  _M_refcount._M_swap(__other._M_refcount);
1273  }
1274 
1275  template<typename _Tp1>
1276  bool
1277  owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const
1278  { return _M_refcount._M_less(__rhs._M_refcount); }
1279 
1280  template<typename _Tp1>
1281  bool
1282  owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const
1283  { return _M_refcount._M_less(__rhs._M_refcount); }
1284 
1285 #if __cpp_rtti
1286  protected:
1287  // This constructor is non-standard, it is used by allocate_shared.
1288  template<typename _Alloc, typename... _Args>
1289  __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
1290  _Args&&... __args)
1291  : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
1292  std::forward<_Args>(__args)...)
1293  {
1294  // _M_ptr needs to point to the newly constructed object.
1295  // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
1296  void* __p = _M_refcount._M_get_deleter(typeid(__tag));
1297  _M_ptr = static_cast<_Tp*>(__p);
1298  _M_enable_shared_from_this_with(_M_ptr);
1299  }
1300 #else
1301  template<typename _Alloc>
1302  struct _Deleter
1303  {
1304  void operator()(typename _Alloc::value_type* __ptr)
1305  {
1306  __allocated_ptr<_Alloc> __guard{ _M_alloc, __ptr };
1307  allocator_traits<_Alloc>::destroy(_M_alloc, __guard.get());
1308  }
1309  _Alloc _M_alloc;
1310  };
1311 
1312  template<typename _Alloc, typename... _Args>
1313  __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
1314  _Args&&... __args)
1315  : _M_ptr(), _M_refcount()
1316  {
1317  typedef typename allocator_traits<_Alloc>::template
1318  rebind_traits<typename std::remove_cv<_Tp>::type> __traits;
1319  _Deleter<typename __traits::allocator_type> __del = { __a };
1320  auto __guard = std::__allocate_guarded(__del._M_alloc);
1321  auto __ptr = __guard.get();
1322  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1323  // 2070. allocate_shared should use allocator_traits<A>::construct
1324  __traits::construct(__del._M_alloc, __ptr,
1325  std::forward<_Args>(__args)...);
1326  __guard = nullptr;
1327  __shared_count<_Lp> __count(__ptr, __del, __del._M_alloc);
1328  _M_refcount._M_swap(__count);
1329  _M_ptr = __ptr;
1330  _M_enable_shared_from_this_with(_M_ptr);
1331  }
1332 #endif
1333 
1334  template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
1335  typename... _Args>
1336  friend __shared_ptr<_Tp1, _Lp1>
1337  __allocate_shared(const _Alloc& __a, _Args&&... __args);
1338 
1339  // This constructor is used by __weak_ptr::lock() and
1340  // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t).
1341  __shared_ptr(const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t)
1342  : _M_refcount(__r._M_refcount, std::nothrow)
1343  {
1344  _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr : nullptr;
1345  }
1346 
1347  friend class __weak_ptr<_Tp, _Lp>;
1348 
1349  private:
1350 
1351  template<typename _Yp>
1352  using __esft_base_t = decltype(__enable_shared_from_this_base(
1353  std::declval<const __shared_count<_Lp>&>(),
1354  std::declval<_Yp*>()));
1355 
1356  // Detect an accessible and unambiguous enable_shared_from_this base.
1357  template<typename _Yp, typename = void>
1358  struct __has_esft_base
1359  : false_type { };
1360 
1361  template<typename _Yp>
1362  struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
1363  : __not_<is_array<_Tp>> { }; // No enable shared_from_this for arrays
1364 
1365  template<typename _Yp>
1366  typename enable_if<__has_esft_base<_Yp>::value>::type
1367  _M_enable_shared_from_this_with(const _Yp* __p) noexcept
1368  {
1369  if (auto __base = __enable_shared_from_this_base(_M_refcount, __p))
1370  __base->_M_weak_assign(const_cast<_Yp*>(__p), _M_refcount);
1371  }
1372 
1373  template<typename _Yp>
1374  typename enable_if<!__has_esft_base<_Yp>::value>::type
1375  _M_enable_shared_from_this_with(const _Yp*) noexcept
1376  { }
1377 
1378  void*
1379  _M_get_deleter(const std::type_info& __ti) const noexcept
1380  { return _M_refcount._M_get_deleter(__ti); }
1381 
1382  template<typename _Tp1>
1383  static _Tp1*
1384  _S_raw_ptr(_Tp1* __ptr)
1385  { return __ptr; }
1386 
1387  template<typename _Tp1>
1388  static auto
1389  _S_raw_ptr(_Tp1 __ptr) -> decltype(std::__addressof(*__ptr))
1390  { return std::__addressof(*__ptr); }
1391 
1392  template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1393  template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1394 
1395  template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
1396  friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1397 
1398  element_type* _M_ptr; // Contained pointer.
1399  __shared_count<_Lp> _M_refcount; // Reference counter.
1400  };
1401 
1402 
1403  // 20.7.2.2.7 shared_ptr comparisons
1404  template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1405  inline bool
1406  operator==(const __shared_ptr<_Tp1, _Lp>& __a,
1407  const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1408  { return __a.get() == __b.get(); }
1409 
1410  template<typename _Tp, _Lock_policy _Lp>
1411  inline bool
1412  operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1413  { return !__a; }
1414 
1415  template<typename _Tp, _Lock_policy _Lp>
1416  inline bool
1417  operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1418  { return !__a; }
1419 
1420  template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1421  inline bool
1422  operator!=(const __shared_ptr<_Tp1, _Lp>& __a,
1423  const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1424  { return __a.get() != __b.get(); }
1425 
1426  template<typename _Tp, _Lock_policy _Lp>
1427  inline bool
1428  operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1429  { return (bool)__a; }
1430 
1431  template<typename _Tp, _Lock_policy _Lp>
1432  inline bool
1433  operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1434  { return (bool)__a; }
1435 
1436  template<typename _Tp, typename _Up, _Lock_policy _Lp>
1437  inline bool
1438  operator<(const __shared_ptr<_Tp, _Lp>& __a,
1439  const __shared_ptr<_Up, _Lp>& __b) noexcept
1440  {
1441  using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1442  using _Up_elt = typename __shared_ptr<_Up, _Lp>::element_type;
1443  using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type;
1444  return less<_Vp>()(__a.get(), __b.get());
1445  }
1446 
1447  template<typename _Tp, _Lock_policy _Lp>
1448  inline bool
1449  operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1450  {
1451  using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1452  return less<_Tp_elt*>()(__a.get(), nullptr);
1453  }
1454 
1455  template<typename _Tp, _Lock_policy _Lp>
1456  inline bool
1457  operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1458  {
1459  using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1460  return less<_Tp_elt*>()(nullptr, __a.get());
1461  }
1462 
1463  template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1464  inline bool
1465  operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1466  const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1467  { return !(__b < __a); }
1468 
1469  template<typename _Tp, _Lock_policy _Lp>
1470  inline bool
1471  operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1472  { return !(nullptr < __a); }
1473 
1474  template<typename _Tp, _Lock_policy _Lp>
1475  inline bool
1476  operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1477  { return !(__a < nullptr); }
1478 
1479  template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1480  inline bool
1481  operator>(const __shared_ptr<_Tp1, _Lp>& __a,
1482  const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1483  { return (__b < __a); }
1484 
1485  template<typename _Tp, _Lock_policy _Lp>
1486  inline bool
1487  operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1488  { return nullptr < __a; }
1489 
1490  template<typename _Tp, _Lock_policy _Lp>
1491  inline bool
1492  operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1493  { return __a < nullptr; }
1494 
1495  template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1496  inline bool
1497  operator>=(const __shared_ptr<_Tp1, _Lp>& __a,
1498  const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1499  { return !(__a < __b); }
1500 
1501  template<typename _Tp, _Lock_policy _Lp>
1502  inline bool
1503  operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1504  { return !(__a < nullptr); }
1505 
1506  template<typename _Tp, _Lock_policy _Lp>
1507  inline bool
1508  operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1509  { return !(nullptr < __a); }
1510 
1511  template<typename _Sp>
1512  struct _Sp_less : public binary_function<_Sp, _Sp, bool>
1513  {
1514  bool
1515  operator()(const _Sp& __lhs, const _Sp& __rhs) const noexcept
1516  {
1517  typedef typename _Sp::element_type element_type;
1518  return std::less<element_type*>()(__lhs.get(), __rhs.get());
1519  }
1520  };
1521 
1522  template<typename _Tp, _Lock_policy _Lp>
1523  struct less<__shared_ptr<_Tp, _Lp>>
1524  : public _Sp_less<__shared_ptr<_Tp, _Lp>>
1525  { };
1526 
1527  // 20.7.2.2.8 shared_ptr specialized algorithms.
1528  template<typename _Tp, _Lock_policy _Lp>
1529  inline void
1530  swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1531  { __a.swap(__b); }
1532 
1533  // 20.7.2.2.9 shared_ptr casts
1534 
1535  // The seemingly equivalent code:
1536  // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1537  // will eventually result in undefined behaviour, attempting to
1538  // delete the same object twice.
1539  /// static_pointer_cast
1540  template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1541  inline __shared_ptr<_Tp, _Lp>
1542  static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1543  {
1544  using _Sp = __shared_ptr<_Tp, _Lp>;
1545  return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
1546  }
1547 
1548  // The seemingly equivalent code:
1549  // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
1550  // will eventually result in undefined behaviour, attempting to
1551  // delete the same object twice.
1552  /// const_pointer_cast
1553  template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1554  inline __shared_ptr<_Tp, _Lp>
1555  const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1556  {
1557  using _Sp = __shared_ptr<_Tp, _Lp>;
1558  return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
1559  }
1560 
1561  // The seemingly equivalent code:
1562  // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
1563  // will eventually result in undefined behaviour, attempting to
1564  // delete the same object twice.
1565  /// dynamic_pointer_cast
1566  template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1567  inline __shared_ptr<_Tp, _Lp>
1568  dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1569  {
1570  using _Sp = __shared_ptr<_Tp, _Lp>;
1571  if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
1572  return _Sp(__r, __p);
1573  return _Sp();
1574  }
1575 
1576 #if __cplusplus > 201402L
1577  template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1578  inline __shared_ptr<_Tp, _Lp>
1579  reinterpret_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1580  {
1581  using _Sp = __shared_ptr<_Tp, _Lp>;
1582  return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
1583  }
1584 #endif
1585 
1586  template<typename _Tp, _Lock_policy _Lp>
1587  class __weak_ptr
1588  {
1589  template<typename _Yp, typename _Res = void>
1590  using _Compatible = typename
1591  enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1592 
1593  // Constraint for assignment from shared_ptr and weak_ptr:
1594  template<typename _Yp>
1595  using _Assignable = _Compatible<_Yp, __weak_ptr&>;
1596 
1597  public:
1598  using element_type = typename remove_extent<_Tp>::type;
1599 
1600  constexpr __weak_ptr() noexcept
1601  : _M_ptr(nullptr), _M_refcount()
1602  { }
1603 
1604  __weak_ptr(const __weak_ptr&) noexcept = default;
1605 
1606  ~__weak_ptr() = default;
1607 
1608  // The "obvious" converting constructor implementation:
1609  //
1610  // template<typename _Tp1>
1611  // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1612  // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1613  // { }
1614  //
1615  // has a serious problem.
1616  //
1617  // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1618  // conversion may require access to *__r._M_ptr (virtual inheritance).
1619  //
1620  // It is not possible to avoid spurious access violations since
1621  // in multithreaded programs __r._M_ptr may be invalidated at any point.
1622  template<typename _Yp, typename = _Compatible<_Yp>>
1623  __weak_ptr(const __weak_ptr<_Yp, _Lp>& __r) noexcept
1624  : _M_refcount(__r._M_refcount)
1625  { _M_ptr = __r.lock().get(); }
1626 
1627  template<typename _Yp, typename = _Compatible<_Yp>>
1628  __weak_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1629  : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1630  { }
1631 
1632  __weak_ptr(__weak_ptr&& __r) noexcept
1633  : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount))
1634  { __r._M_ptr = nullptr; }
1635 
1636  template<typename _Yp, typename = _Compatible<_Yp>>
1637  __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1638  : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount))
1639  { __r._M_ptr = nullptr; }
1640 
1641  __weak_ptr&
1642  operator=(const __weak_ptr& __r) noexcept = default;
1643 
1644  template<typename _Yp>
1645  _Assignable<_Yp>
1646  operator=(const __weak_ptr<_Yp, _Lp>& __r) noexcept
1647  {
1648  _M_ptr = __r.lock().get();
1649  _M_refcount = __r._M_refcount;
1650  return *this;
1651  }
1652 
1653  template<typename _Yp>
1654  _Assignable<_Yp>
1655  operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1656  {
1657  _M_ptr = __r._M_ptr;
1658  _M_refcount = __r._M_refcount;
1659  return *this;
1660  }
1661 
1662  __weak_ptr&
1663  operator=(__weak_ptr&& __r) noexcept
1664  {
1665  _M_ptr = __r._M_ptr;
1666  _M_refcount = std::move(__r._M_refcount);
1667  __r._M_ptr = nullptr;
1668  return *this;
1669  }
1670 
1671  template<typename _Yp>
1672  _Assignable<_Yp>
1673  operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1674  {
1675  _M_ptr = __r.lock().get();
1676  _M_refcount = std::move(__r._M_refcount);
1677  __r._M_ptr = nullptr;
1678  return *this;
1679  }
1680 
1681  __shared_ptr<_Tp, _Lp>
1682  lock() const noexcept
1683  { return __shared_ptr<element_type, _Lp>(*this, std::nothrow); }
1684 
1685  long
1686  use_count() const noexcept
1687  { return _M_refcount._M_get_use_count(); }
1688 
1689  bool
1690  expired() const noexcept
1691  { return _M_refcount._M_get_use_count() == 0; }
1692 
1693  template<typename _Tp1>
1694  bool
1695  owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const
1696  { return _M_refcount._M_less(__rhs._M_refcount); }
1697 
1698  template<typename _Tp1>
1699  bool
1700  owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const
1701  { return _M_refcount._M_less(__rhs._M_refcount); }
1702 
1703  void
1704  reset() noexcept
1705  { __weak_ptr().swap(*this); }
1706 
1707  void
1708  swap(__weak_ptr& __s) noexcept
1709  {
1710  std::swap(_M_ptr, __s._M_ptr);
1711  _M_refcount._M_swap(__s._M_refcount);
1712  }
1713 
1714  private:
1715  // Used by __enable_shared_from_this.
1716  void
1717  _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept
1718  {
1719  if (use_count() == 0)
1720  {
1721  _M_ptr = __ptr;
1722  _M_refcount = __refcount;
1723  }
1724  }
1725 
1726  template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1727  template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1728  friend class __enable_shared_from_this<_Tp, _Lp>;
1729  friend class enable_shared_from_this<_Tp>;
1730 
1731  element_type* _M_ptr; // Contained pointer.
1732  __weak_count<_Lp> _M_refcount; // Reference counter.
1733  };
1734 
1735  // 20.7.2.3.6 weak_ptr specialized algorithms.
1736  template<typename _Tp, _Lock_policy _Lp>
1737  inline void
1738  swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1739  { __a.swap(__b); }
1740 
1741  template<typename _Tp, typename _Tp1>
1742  struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
1743  {
1744  bool
1745  operator()(const _Tp& __lhs, const _Tp& __rhs) const
1746  { return __lhs.owner_before(__rhs); }
1747 
1748  bool
1749  operator()(const _Tp& __lhs, const _Tp1& __rhs) const
1750  { return __lhs.owner_before(__rhs); }
1751 
1752  bool
1753  operator()(const _Tp1& __lhs, const _Tp& __rhs) const
1754  { return __lhs.owner_before(__rhs); }
1755  };
1756 
1757  template<>
1758  struct _Sp_owner_less<void, void>
1759  {
1760  template<typename _Tp, typename _Up>
1761  auto
1762  operator()(const _Tp& __lhs, const _Up& __rhs) const
1763  -> decltype(__lhs.owner_before(__rhs))
1764  { return __lhs.owner_before(__rhs); }
1765 
1766  using is_transparent = void;
1767  };
1768 
1769  template<typename _Tp, _Lock_policy _Lp>
1770  struct owner_less<__shared_ptr<_Tp, _Lp>>
1771  : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1772  { };
1773 
1774  template<typename _Tp, _Lock_policy _Lp>
1775  struct owner_less<__weak_ptr<_Tp, _Lp>>
1776  : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1777  { };
1778 
1779 
1780  template<typename _Tp, _Lock_policy _Lp>
1781  class __enable_shared_from_this
1782  {
1783  protected:
1784  constexpr __enable_shared_from_this() noexcept { }
1785 
1786  __enable_shared_from_this(const __enable_shared_from_this&) noexcept { }
1787 
1788  __enable_shared_from_this&
1789  operator=(const __enable_shared_from_this&) noexcept
1790  { return *this; }
1791 
1792  ~__enable_shared_from_this() { }
1793 
1794  public:
1795  __shared_ptr<_Tp, _Lp>
1796  shared_from_this()
1797  { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1798 
1799  __shared_ptr<const _Tp, _Lp>
1800  shared_from_this() const
1801  { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1802 
1803 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
1804  __weak_ptr<_Tp, _Lp>
1805  weak_from_this() noexcept
1806  { return this->_M_weak_this; }
1807 
1808  __weak_ptr<const _Tp, _Lp>
1809  weak_from_this() const noexcept
1810  { return this->_M_weak_this; }
1811 #endif
1812 
1813  private:
1814  template<typename _Tp1>
1815  void
1816  _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept
1817  { _M_weak_this._M_assign(__p, __n); }
1818 
1819  friend void
1820  __enable_shared_from_this_base(const __shared_count<_Lp>&,
1821  const __enable_shared_from_this* __p)
1822  { return __p; }
1823 
1824  mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1825  };
1826 
1827  template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args>
1828  inline __shared_ptr<_Tp, _Lp>
1829  __allocate_shared(const _Alloc& __a, _Args&&... __args)
1830  {
1831  return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1832  std::forward<_Args>(__args)...);
1833  }
1834 
1835  template<typename _Tp, _Lock_policy _Lp, typename... _Args>
1836  inline __shared_ptr<_Tp, _Lp>
1837  __make_shared(_Args&&... __args)
1838  {
1839  typedef typename std::remove_const<_Tp>::type _Tp_nc;
1840  return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
1841  std::forward<_Args>(__args)...);
1842  }
1843 
1844  /// std::hash specialization for __shared_ptr.
1845  template<typename _Tp, _Lock_policy _Lp>
1846  struct hash<__shared_ptr<_Tp, _Lp>>
1847  : public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1848  {
1849  size_t
1850  operator()(const __shared_ptr<_Tp, _Lp>& __s) const noexcept
1851  {
1853  __s.get());
1854  }
1855  };
1856 
1857 _GLIBCXX_END_NAMESPACE_VERSION
1858 } // namespace
1859 
1860 #endif // _SHARED_PTR_BASE_H
Primary class template hash.
Definition: system_error:141
Uniform interface to all allocator types.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:47
ISO C++ entities toplevel namespace is std.
static void destroy(_Alloc &__a, _Tp *__p)
Destroy an object of type _Tp.
allocator<void> specialization.
Definition: allocator.h:68
20.7.1.2 unique_ptr for single objects.
Definition: unique_ptr.h:153
complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
Definition: complex:386
Primary class template for reference_wrapper.
Definition: refwrap.h:311
Exception possibly thrown by shared_ptr.
virtual char const * what() const noexcept
void lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
Generic lock.
Definition: mutex:542
Scoped lock idiom.
Definition: concurrence.h:231
is_void
Definition: type_traits:217
Base class for all library exceptions.
Definition: exception.h:60
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.
static auto construct(_Alloc &__a, _Tp *__p, _Args &&... __args) -> decltype(_S_construct(__a, __p, std::forward< _Args >(__args)...))
Construct an object of type _Tp.
Primary template owner_less.
A smart pointer with reference-counted copy semantics.
One of the comparison functors.
Definition: stl_function.h:340
integral_constant
Definition: type_traits:69
Part of RTTI.
Definition: typeinfo:88
A simple smart pointer providing strict ownership semantics.
Definition: auto_ptr.h:87
Non-standard RAII type for managing pointers obtained from allocators.
Definition: allocated_ptr.h:46
_Del * get_deleter(const __shared_ptr< _Tp, _Lp > &__p) noexcept
20.7.2.2.10 shared_ptr get_deleter
A smart pointer with weak semantics.
is_array
Definition: type_traits:362
The standard allocator, as per [20.4].
Definition: allocator.h:108