1 // shared_ptr and weak_ptr implementation details -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 // GCC Note: Based on files from version 1.32.0 of the Boost library.
29 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
32 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
33 // Copyright (C) 2001, 2002, 2003 Peter Dimov
36 // Copyright (C) 2001, 2002, 2003 Peter Dimov
38 // enable_shared_from_this.hpp
39 // Copyright (C) 2002 Peter Dimov
41 // Distributed under the Boost Software License, Version 1.0. (See
42 // accompanying file LICENSE_1_0.txt or copy at
43 // http://www.boost.org/LICENSE_1_0.txt)
45 /** @file bits/shared_ptr_base.h
46 * This is an internal header file, included by other library headers.
47 * Do not attempt to use it directly. @headername{memory}
50 #ifndef _SHARED_PTR_BASE_H
51 #define _SHARED_PTR_BASE_H 1
53 namespace std _GLIBCXX_VISIBILITY(default)
55 _GLIBCXX_BEGIN_NAMESPACE_VERSION
58 * @brief Exception possibly thrown by @c shared_ptr.
61 class bad_weak_ptr : public std::exception
65 what() const noexcept;
67 virtual ~bad_weak_ptr() noexcept;
70 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
72 __throw_bad_weak_ptr()
81 using __gnu_cxx::_Lock_policy;
82 using __gnu_cxx::__default_lock_policy;
83 using __gnu_cxx::_S_single;
84 using __gnu_cxx::_S_mutex;
85 using __gnu_cxx::_S_atomic;
87 // Empty helper class except when the template argument is _S_mutex.
88 template<_Lock_policy _Lp>
92 // The atomic policy uses fully-fenced builtins, single doesn't care.
93 enum { _S_need_barriers = 0 };
97 class _Mutex_base<_S_mutex>
98 : public __gnu_cxx::__mutex
101 // This policy is used when atomic builtins are not available.
102 // The replacement atomic operations might not have the necessary
104 enum { _S_need_barriers = 1 };
107 template<_Lock_policy _Lp = __default_lock_policy>
108 class _Sp_counted_base
109 : public _Mutex_base<_Lp>
112 _Sp_counted_base() noexcept
113 : _M_use_count(1), _M_weak_count(1) { }
116 ~_Sp_counted_base() noexcept
119 // Called when _M_use_count drops to zero, to release the resources
122 _M_dispose() noexcept = 0;
124 // Called when _M_weak_count drops to zero.
126 _M_destroy() noexcept
130 _M_get_deleter(const std::type_info&) = 0;
134 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
140 _M_release() noexcept
142 // Be race-detector-friendly. For more info see bits/c++config.
143 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
144 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
146 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
148 // There must be a memory barrier between dispose() and destroy()
149 // to ensure that the effects of dispose() are observed in the
150 // thread that runs destroy().
151 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
152 if (_Mutex_base<_Lp>::_S_need_barriers)
154 _GLIBCXX_READ_MEM_BARRIER;
155 _GLIBCXX_WRITE_MEM_BARRIER;
158 // Be race-detector-friendly. For more info see bits/c++config.
159 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
160 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
163 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
170 _M_weak_add_ref() noexcept
171 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
174 _M_weak_release() noexcept
176 // Be race-detector-friendly. For more info see bits/c++config.
177 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
178 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
180 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
181 if (_Mutex_base<_Lp>::_S_need_barriers)
184 // destroy() must observe results of dispose()
185 _GLIBCXX_READ_MEM_BARRIER;
186 _GLIBCXX_WRITE_MEM_BARRIER;
193 _M_get_use_count() const noexcept
195 // No memory barrier is used here so there is no synchronization
196 // with other threads.
197 return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
201 _Sp_counted_base(_Sp_counted_base const&) = delete;
202 _Sp_counted_base& operator=(_Sp_counted_base const&) = delete;
204 _Atomic_word _M_use_count; // #shared
205 _Atomic_word _M_weak_count; // #weak + (#shared != 0)
210 _Sp_counted_base<_S_single>::
213 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
216 __throw_bad_weak_ptr();
222 _Sp_counted_base<_S_mutex>::
225 __gnu_cxx::__scoped_lock sentry(*this);
226 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
229 __throw_bad_weak_ptr();
235 _Sp_counted_base<_S_atomic>::
238 // Perform lock-free add-if-not-zero operation.
239 _Atomic_word __count;
242 __count = _M_use_count;
244 __throw_bad_weak_ptr();
246 // Replace the current counter value with the old value + 1, as
247 // long as it's not changed meanwhile.
249 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
250 true, __ATOMIC_ACQ_REL,
255 // Forward declarations.
256 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
259 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
262 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
263 class __enable_shared_from_this;
265 template<typename _Tp>
268 template<typename _Tp>
271 template<typename _Tp>
274 template<typename _Tp>
275 class enable_shared_from_this;
277 template<_Lock_policy _Lp = __default_lock_policy>
280 template<_Lock_policy _Lp = __default_lock_policy>
281 class __shared_count;
284 // Counted ptr with no deleter or allocator support
285 template<typename _Ptr, _Lock_policy _Lp>
286 class _Sp_counted_ptr final : public _Sp_counted_base<_Lp>
290 _Sp_counted_ptr(_Ptr __p)
294 _M_dispose() noexcept
298 _M_destroy() noexcept
302 _M_get_deleter(const std::type_info&)
305 _Sp_counted_ptr(const _Sp_counted_ptr&) = delete;
306 _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete;
309 _Ptr _M_ptr; // copy constructor must not throw
314 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
318 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
322 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
324 // Support for custom deleter and/or allocator
325 template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
326 class _Sp_counted_deleter final : public _Sp_counted_base<_Lp>
328 // Helper class that stores the Deleter and also acts as an allocator.
329 // Used to dispose of the owned pointer and the internal refcount
330 // Requires that copies of _Alloc can free each other's memory.
332 : public _Alloc // copy constructor must not throw
334 _Deleter _M_del; // copy constructor must not throw
335 _My_Deleter(_Deleter __d, const _Alloc& __a)
336 : _Alloc(__a), _M_del(__d) { }
340 // __d(__p) must not throw.
341 _Sp_counted_deleter(_Ptr __p, _Deleter __d)
342 : _M_ptr(__p), _M_del(__d, _Alloc()) { }
344 // __d(__p) must not throw.
345 _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a)
346 : _M_ptr(__p), _M_del(__d, __a) { }
349 _M_dispose() noexcept
350 { _M_del._M_del(_M_ptr); }
353 _M_destroy() noexcept
355 typedef typename allocator_traits<_Alloc>::template
356 rebind_traits<_Sp_counted_deleter> _Alloc_traits;
357 typename _Alloc_traits::allocator_type __a(_M_del);
358 _Alloc_traits::destroy(__a, this);
359 _Alloc_traits::deallocate(__a, this, 1);
363 _M_get_deleter(const std::type_info& __ti)
366 return __ti == typeid(_Deleter) ? &_M_del._M_del : 0;
373 _Ptr _M_ptr; // copy constructor must not throw
374 _My_Deleter _M_del; // copy constructor must not throw
377 // helpers for make_shared / allocate_shared
379 struct _Sp_make_shared_tag { };
381 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
382 class _Sp_counted_ptr_inplace final : public _Sp_counted_base<_Lp>
384 // Helper class that stores the pointer and also acts as an allocator.
385 // Used to dispose of the owned pointer and the internal refcount
386 // Requires that copies of _Alloc can free each other's memory.
388 : public _Alloc // copy constructor must not throw
390 _Impl(_Alloc __a) : _Alloc(__a), _M_ptr() { }
395 template<typename... _Args>
396 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
397 : _M_impl(__a), _M_storage()
399 _M_impl._M_ptr = static_cast<_Tp*>(static_cast<void*>(&_M_storage));
400 // _GLIBCXX_RESOLVE_LIB_DEFECTS
401 // 2070. allocate_shared should use allocator_traits<A>::construct
402 allocator_traits<_Alloc>::construct(__a, _M_impl._M_ptr,
403 std::forward<_Args>(__args)...); // might throw
407 _M_dispose() noexcept
408 { allocator_traits<_Alloc>::destroy(_M_impl, _M_impl._M_ptr); }
410 // Override because the allocator needs to know the dynamic type
412 _M_destroy() noexcept
414 typedef typename allocator_traits<_Alloc>::template
415 rebind_traits<_Sp_counted_ptr_inplace> _Alloc_traits;
416 typename _Alloc_traits::allocator_type __a(_M_impl);
417 _Alloc_traits::destroy(__a, this);
418 _Alloc_traits::deallocate(__a, this, 1);
421 // Sneaky trick so __shared_ptr can get the managed pointer
423 _M_get_deleter(const std::type_info& __ti) noexcept
426 return __ti == typeid(_Sp_make_shared_tag)
427 ? static_cast<void*>(&_M_storage)
436 typename aligned_storage<sizeof(_Tp), alignment_of<_Tp>::value>::type
440 template<_Lock_policy _Lp>
444 constexpr __shared_count() noexcept : _M_pi(0)
447 template<typename _Ptr>
449 __shared_count(_Ptr __p) : _M_pi(0)
453 _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
458 __throw_exception_again;
462 template<typename _Ptr, typename _Deleter>
463 __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
465 // The allocator's value_type doesn't matter, will rebind it anyway.
466 typedef std::allocator<int> _Alloc;
467 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
468 typedef typename allocator_traits<_Alloc>::template
469 rebind_traits<_Sp_cd_type> _Alloc_traits;
470 typename _Alloc_traits::allocator_type __a;
471 _Sp_cd_type* __mem = 0;
474 __mem = _Alloc_traits::allocate(__a, 1);
475 _Alloc_traits::construct(__a, __mem, __p, std::move(__d));
480 __d(__p); // Call _Deleter on __p.
482 _Alloc_traits::deallocate(__a, __mem, 1);
483 __throw_exception_again;
487 template<typename _Ptr, typename _Deleter, typename _Alloc>
488 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
490 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
491 typedef typename allocator_traits<_Alloc>::template
492 rebind_traits<_Sp_cd_type> _Alloc_traits;
493 typename _Alloc_traits::allocator_type __a2(__a);
494 _Sp_cd_type* __mem = 0;
497 __mem = _Alloc_traits::allocate(__a2, 1);
498 _Alloc_traits::construct(__a2, __mem,
499 __p, std::move(__d), std::move(__a));
504 __d(__p); // Call _Deleter on __p.
506 _Alloc_traits::deallocate(__a2, __mem, 1);
507 __throw_exception_again;
511 template<typename _Tp, typename _Alloc, typename... _Args>
512 __shared_count(_Sp_make_shared_tag, _Tp*, const _Alloc& __a,
516 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
517 typedef typename allocator_traits<_Alloc>::template
518 rebind_traits<_Sp_cp_type> _Alloc_traits;
519 typename _Alloc_traits::allocator_type __a2(__a);
520 _Sp_cp_type* __mem = _Alloc_traits::allocate(__a2, 1);
523 _Alloc_traits::construct(__a2, __mem, std::move(__a),
524 std::forward<_Args>(__args)...);
529 _Alloc_traits::deallocate(__a2, __mem, 1);
530 __throw_exception_again;
534 #if _GLIBCXX_USE_DEPRECATED
535 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
536 template<typename _Tp>
538 __shared_count(std::auto_ptr<_Tp>&& __r)
539 : _M_pi(new _Sp_counted_ptr<_Tp*, _Lp>(__r.get()))
543 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
544 template<typename _Tp, typename _Del>
546 __shared_count(std::unique_ptr<_Tp, _Del>&& __r)
547 : _M_pi(_S_create_from_up(std::move(__r)))
550 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
551 explicit __shared_count(const __weak_count<_Lp>& __r);
553 ~__shared_count() noexcept
559 __shared_count(const __shared_count& __r) noexcept
563 _M_pi->_M_add_ref_copy();
567 operator=(const __shared_count& __r) noexcept
569 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
573 __tmp->_M_add_ref_copy();
582 _M_swap(__shared_count& __r) noexcept
584 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
590 _M_get_use_count() const noexcept
591 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
594 _M_unique() const noexcept
595 { return this->_M_get_use_count() == 1; }
598 _M_get_deleter(const std::type_info& __ti) const noexcept
599 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
602 _M_less(const __shared_count& __rhs) const noexcept
603 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
606 _M_less(const __weak_count<_Lp>& __rhs) const noexcept
607 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
609 // Friend function injected into enclosing namespace and found by ADL
611 operator==(const __shared_count& __a, const __shared_count& __b) noexcept
612 { return __a._M_pi == __b._M_pi; }
615 friend class __weak_count<_Lp>;
617 template<typename _Tp, typename _Del>
618 static _Sp_counted_base<_Lp>*
619 _S_create_from_up(std::unique_ptr<_Tp, _Del>&& __r,
620 typename std::enable_if<!std::is_reference<_Del>::value>::type* = 0)
622 return new _Sp_counted_deleter<_Tp*, _Del, std::allocator<_Tp>,
623 _Lp>(__r.get(), __r.get_deleter());
626 template<typename _Tp, typename _Del>
627 static _Sp_counted_base<_Lp>*
628 _S_create_from_up(std::unique_ptr<_Tp, _Del>&& __r,
629 typename std::enable_if<std::is_reference<_Del>::value>::type* = 0)
631 typedef typename std::remove_reference<_Del>::type _Del1;
632 typedef std::reference_wrapper<_Del1> _Del2;
633 return new _Sp_counted_deleter<_Tp*, _Del2, std::allocator<_Tp>,
634 _Lp>(__r.get(), std::ref(__r.get_deleter()));
637 _Sp_counted_base<_Lp>* _M_pi;
641 template<_Lock_policy _Lp>
645 constexpr __weak_count() noexcept : _M_pi(0)
648 __weak_count(const __shared_count<_Lp>& __r) noexcept
652 _M_pi->_M_weak_add_ref();
655 __weak_count(const __weak_count<_Lp>& __r) noexcept
659 _M_pi->_M_weak_add_ref();
662 ~__weak_count() noexcept
665 _M_pi->_M_weak_release();
669 operator=(const __shared_count<_Lp>& __r) noexcept
671 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
673 __tmp->_M_weak_add_ref();
675 _M_pi->_M_weak_release();
681 operator=(const __weak_count<_Lp>& __r) noexcept
683 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
685 __tmp->_M_weak_add_ref();
687 _M_pi->_M_weak_release();
693 _M_swap(__weak_count<_Lp>& __r) noexcept
695 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
701 _M_get_use_count() const noexcept
702 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
705 _M_less(const __weak_count& __rhs) const noexcept
706 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
709 _M_less(const __shared_count<_Lp>& __rhs) const noexcept
710 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
712 // Friend function injected into enclosing namespace and found by ADL
714 operator==(const __weak_count& __a, const __weak_count& __b) noexcept
715 { return __a._M_pi == __b._M_pi; }
718 friend class __shared_count<_Lp>;
720 _Sp_counted_base<_Lp>* _M_pi;
723 // Now that __weak_count is defined we can define this constructor:
724 template<_Lock_policy _Lp>
725 inline __shared_count<_Lp>:: __shared_count(const __weak_count<_Lp>& __r)
729 _M_pi->_M_add_ref_lock();
731 __throw_bad_weak_ptr();
735 // Support for enable_shared_from_this.
737 // Friend of __enable_shared_from_this.
738 template<_Lock_policy _Lp, typename _Tp1, typename _Tp2>
740 __enable_shared_from_this_helper(const __shared_count<_Lp>&,
741 const __enable_shared_from_this<_Tp1,
742 _Lp>*, const _Tp2*) noexcept;
744 // Friend of enable_shared_from_this.
745 template<typename _Tp1, typename _Tp2>
747 __enable_shared_from_this_helper(const __shared_count<>&,
748 const enable_shared_from_this<_Tp1>*,
749 const _Tp2*) noexcept;
751 template<_Lock_policy _Lp>
753 __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...) noexcept
757 template<typename _Tp, _Lock_policy _Lp>
761 typedef _Tp element_type;
763 constexpr __shared_ptr() noexcept
764 : _M_ptr(0), _M_refcount()
767 template<typename _Tp1>
768 explicit __shared_ptr(_Tp1* __p)
769 : _M_ptr(__p), _M_refcount(__p)
771 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
772 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
773 __enable_shared_from_this_helper(_M_refcount, __p, __p);
776 template<typename _Tp1, typename _Deleter>
777 __shared_ptr(_Tp1* __p, _Deleter __d)
778 : _M_ptr(__p), _M_refcount(__p, __d)
780 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
781 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
782 __enable_shared_from_this_helper(_M_refcount, __p, __p);
785 template<typename _Tp1, typename _Deleter, typename _Alloc>
786 __shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
787 : _M_ptr(__p), _M_refcount(__p, __d, std::move(__a))
789 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
790 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
791 __enable_shared_from_this_helper(_M_refcount, __p, __p);
794 template<typename _Deleter>
795 __shared_ptr(nullptr_t __p, _Deleter __d)
796 : _M_ptr(0), _M_refcount(__p, __d)
799 template<typename _Deleter, typename _Alloc>
800 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
801 : _M_ptr(0), _M_refcount(__p, __d, std::move(__a))
804 template<typename _Tp1>
805 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p) noexcept
806 : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
809 __shared_ptr(const __shared_ptr&) noexcept = default;
810 __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
811 ~__shared_ptr() = default;
813 template<typename _Tp1, typename = typename
814 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
815 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
816 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
819 __shared_ptr(__shared_ptr&& __r) noexcept
820 : _M_ptr(__r._M_ptr), _M_refcount()
822 _M_refcount._M_swap(__r._M_refcount);
826 template<typename _Tp1, typename = typename
827 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
828 __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
829 : _M_ptr(__r._M_ptr), _M_refcount()
831 _M_refcount._M_swap(__r._M_refcount);
835 template<typename _Tp1>
836 explicit __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
837 : _M_refcount(__r._M_refcount) // may throw
839 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
841 // It is now safe to copy __r._M_ptr, as
842 // _M_refcount(__r._M_refcount) did not throw.
846 // If an exception is thrown this constructor has no effect.
847 template<typename _Tp1, typename _Del>
848 __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
849 : _M_ptr(__r.get()), _M_refcount()
851 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
852 _Tp1* __tmp = __r.get();
853 _M_refcount = __shared_count<_Lp>(std::move(__r));
854 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
857 #if _GLIBCXX_USE_DEPRECATED
858 // Postcondition: use_count() == 1 and __r.get() == 0
859 template<typename _Tp1>
860 __shared_ptr(std::auto_ptr<_Tp1>&& __r)
861 : _M_ptr(__r.get()), _M_refcount()
863 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
864 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
865 _Tp1* __tmp = __r.get();
866 _M_refcount = __shared_count<_Lp>(std::move(__r));
867 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
871 /* TODO: use delegating constructor */
872 constexpr __shared_ptr(nullptr_t) noexcept
873 : _M_ptr(0), _M_refcount()
876 template<typename _Tp1>
878 operator=(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
881 _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
885 #if _GLIBCXX_USE_DEPRECATED
886 template<typename _Tp1>
888 operator=(std::auto_ptr<_Tp1>&& __r)
890 __shared_ptr(std::move(__r)).swap(*this);
896 operator=(__shared_ptr&& __r) noexcept
898 __shared_ptr(std::move(__r)).swap(*this);
904 operator=(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
906 __shared_ptr(std::move(__r)).swap(*this);
910 template<typename _Tp1, typename _Del>
912 operator=(std::unique_ptr<_Tp1, _Del>&& __r)
914 __shared_ptr(std::move(__r)).swap(*this);
920 { __shared_ptr().swap(*this); }
922 template<typename _Tp1>
924 reset(_Tp1* __p) // _Tp1 must be complete.
926 // Catch self-reset errors.
927 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
928 __shared_ptr(__p).swap(*this);
931 template<typename _Tp1, typename _Deleter>
933 reset(_Tp1* __p, _Deleter __d)
934 { __shared_ptr(__p, __d).swap(*this); }
936 template<typename _Tp1, typename _Deleter, typename _Alloc>
938 reset(_Tp1* __p, _Deleter __d, _Alloc __a)
939 { __shared_ptr(__p, __d, std::move(__a)).swap(*this); }
941 // Allow class instantiation when _Tp is [cv-qual] void.
942 typename std::add_lvalue_reference<_Tp>::type
943 operator*() const noexcept
945 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
950 operator->() const noexcept
952 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
960 explicit operator bool() const // never throws
961 { return _M_ptr == 0 ? false : true; }
964 unique() const noexcept
965 { return _M_refcount._M_unique(); }
968 use_count() const noexcept
969 { return _M_refcount._M_get_use_count(); }
972 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
974 std::swap(_M_ptr, __other._M_ptr);
975 _M_refcount._M_swap(__other._M_refcount);
978 template<typename _Tp1>
980 owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const
981 { return _M_refcount._M_less(__rhs._M_refcount); }
983 template<typename _Tp1>
985 owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const
986 { return _M_refcount._M_less(__rhs._M_refcount); }
990 // This constructor is non-standard, it is used by allocate_shared.
991 template<typename _Alloc, typename... _Args>
992 __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
994 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
995 std::forward<_Args>(__args)...)
997 // _M_ptr needs to point to the newly constructed object.
998 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
999 void* __p = _M_refcount._M_get_deleter(typeid(__tag));
1000 _M_ptr = static_cast<_Tp*>(__p);
1001 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1004 template<typename _Alloc>
1007 void operator()(_Tp* __ptr)
1009 typedef allocator_traits<_Alloc> _Alloc_traits;
1010 _Alloc_traits::destroy(_M_alloc, __ptr);
1011 _Alloc_traits::deallocate(_M_alloc, __ptr, 1);
1016 template<typename _Alloc, typename... _Args>
1017 __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
1019 : _M_ptr(), _M_refcount()
1021 typedef typename _Alloc::template rebind<_Tp>::other _Alloc2;
1022 _Deleter<_Alloc2> __del = { _Alloc2(__a) };
1023 typedef allocator_traits<_Alloc2> __traits;
1024 _M_ptr = __traits::allocate(__del._M_alloc, 1);
1027 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1028 // 2070. allocate_shared should use allocator_traits<A>::construct
1029 __traits::construct(__del._M_alloc, _M_ptr,
1030 std::forward<_Args>(__args)...);
1034 __traits::deallocate(__del._M_alloc, _M_ptr, 1);
1035 __throw_exception_again;
1037 __shared_count<_Lp> __count(_M_ptr, __del, __del._M_alloc);
1038 _M_refcount._M_swap(__count);
1039 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1043 template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
1045 friend __shared_ptr<_Tp1, _Lp1>
1046 __allocate_shared(const _Alloc& __a, _Args&&... __args);
1050 _M_get_deleter(const std::type_info& __ti) const noexcept
1051 { return _M_refcount._M_get_deleter(__ti); }
1053 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1054 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1056 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
1057 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1059 _Tp* _M_ptr; // Contained pointer.
1060 __shared_count<_Lp> _M_refcount; // Reference counter.
1064 // 20.8.13.2.7 shared_ptr comparisons
1065 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1067 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
1068 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1069 { return __a.get() == __b.get(); }
1071 template<typename _Tp, _Lock_policy _Lp>
1073 operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1076 template<typename _Tp, _Lock_policy _Lp>
1078 operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1081 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1083 operator!=(const __shared_ptr<_Tp1, _Lp>& __a,
1084 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1085 { return __a.get() != __b.get(); }
1087 template<typename _Tp, _Lock_policy _Lp>
1089 operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1090 { return (bool)__a; }
1092 template<typename _Tp, _Lock_policy _Lp>
1094 operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1095 { return (bool)__a; }
1097 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1099 operator<(const __shared_ptr<_Tp1, _Lp>& __a,
1100 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1102 typedef typename std::common_type<_Tp1*, _Tp2*>::type _CT;
1103 return std::less<_CT>()(__a.get(), __b.get());
1106 template<typename _Tp, _Lock_policy _Lp>
1108 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1109 { return std::less<_Tp*>()(__a.get(), nullptr); }
1111 template<typename _Tp, _Lock_policy _Lp>
1113 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1114 { return std::less<_Tp*>()(nullptr, __a.get()); }
1116 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1118 operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1119 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1120 { return !(__b < __a); }
1122 template<typename _Tp, _Lock_policy _Lp>
1124 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1125 { return !(nullptr < __a); }
1127 template<typename _Tp, _Lock_policy _Lp>
1129 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1130 { return !(__a < nullptr); }
1132 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1134 operator>(const __shared_ptr<_Tp1, _Lp>& __a,
1135 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1136 { return (__b < __a); }
1138 template<typename _Tp, _Lock_policy _Lp>
1140 operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1141 { return std::less<_Tp*>()(nullptr, __a.get()); }
1143 template<typename _Tp, _Lock_policy _Lp>
1145 operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1146 { return std::less<_Tp*>()(__a.get(), nullptr); }
1148 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1150 operator>=(const __shared_ptr<_Tp1, _Lp>& __a,
1151 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1152 { return !(__a < __b); }
1154 template<typename _Tp, _Lock_policy _Lp>
1156 operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1157 { return !(__a < nullptr); }
1159 template<typename _Tp, _Lock_policy _Lp>
1161 operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1162 { return !(nullptr < __a); }
1164 template<typename _Sp>
1165 struct _Sp_less : public binary_function<_Sp, _Sp, bool>
1168 operator()(const _Sp& __lhs, const _Sp& __rhs) const noexcept
1170 typedef typename _Sp::element_type element_type;
1171 return std::less<element_type*>()(__lhs.get(), __rhs.get());
1175 template<typename _Tp, _Lock_policy _Lp>
1176 struct less<__shared_ptr<_Tp, _Lp>>
1177 : public _Sp_less<__shared_ptr<_Tp, _Lp>>
1180 // 2.2.3.8 shared_ptr specialized algorithms.
1181 template<typename _Tp, _Lock_policy _Lp>
1183 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1186 // 2.2.3.9 shared_ptr casts
1188 // The seemingly equivalent code:
1189 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1190 // will eventually result in undefined behaviour, attempting to
1191 // delete the same object twice.
1192 /// static_pointer_cast
1193 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1194 inline __shared_ptr<_Tp, _Lp>
1195 static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1196 { return __shared_ptr<_Tp, _Lp>(__r, static_cast<_Tp*>(__r.get())); }
1198 // The seemingly equivalent code:
1199 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
1200 // will eventually result in undefined behaviour, attempting to
1201 // delete the same object twice.
1202 /// const_pointer_cast
1203 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1204 inline __shared_ptr<_Tp, _Lp>
1205 const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1206 { return __shared_ptr<_Tp, _Lp>(__r, const_cast<_Tp*>(__r.get())); }
1208 // The seemingly equivalent code:
1209 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
1210 // will eventually result in undefined behaviour, attempting to
1211 // delete the same object twice.
1212 /// dynamic_pointer_cast
1213 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1214 inline __shared_ptr<_Tp, _Lp>
1215 dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1217 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
1218 return __shared_ptr<_Tp, _Lp>(__r, __p);
1219 return __shared_ptr<_Tp, _Lp>();
1223 template<typename _Tp, _Lock_policy _Lp>
1227 typedef _Tp element_type;
1229 constexpr __weak_ptr() noexcept
1230 : _M_ptr(0), _M_refcount()
1233 __weak_ptr(const __weak_ptr&) noexcept = default;
1234 __weak_ptr& operator=(const __weak_ptr&) noexcept = default;
1235 ~__weak_ptr() = default;
1237 // The "obvious" converting constructor implementation:
1239 // template<typename _Tp1>
1240 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1241 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1244 // has a serious problem.
1246 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1247 // conversion may require access to *__r._M_ptr (virtual inheritance).
1249 // It is not possible to avoid spurious access violations since
1250 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1251 template<typename _Tp1, typename = typename
1252 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
1253 __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1254 : _M_refcount(__r._M_refcount)
1255 { _M_ptr = __r.lock().get(); }
1257 template<typename _Tp1, typename = typename
1258 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
1259 __weak_ptr(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1260 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1263 template<typename _Tp1>
1265 operator=(const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1267 _M_ptr = __r.lock().get();
1268 _M_refcount = __r._M_refcount;
1272 template<typename _Tp1>
1274 operator=(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1276 _M_ptr = __r._M_ptr;
1277 _M_refcount = __r._M_refcount;
1281 __shared_ptr<_Tp, _Lp>
1282 lock() const noexcept
1285 // Optimization: avoid throw overhead.
1287 return __shared_ptr<element_type, _Lp>();
1291 return __shared_ptr<element_type, _Lp>(*this);
1293 __catch(const bad_weak_ptr&)
1295 // Q: How can we get here?
1296 // A: Another thread may have invalidated r after the
1297 // use_count test above.
1298 return __shared_ptr<element_type, _Lp>();
1302 // Optimization: avoid try/catch overhead when single threaded.
1303 return expired() ? __shared_ptr<element_type, _Lp>()
1304 : __shared_ptr<element_type, _Lp>(*this);
1310 use_count() const noexcept
1311 { return _M_refcount._M_get_use_count(); }
1314 expired() const noexcept
1315 { return _M_refcount._M_get_use_count() == 0; }
1317 template<typename _Tp1>
1319 owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const
1320 { return _M_refcount._M_less(__rhs._M_refcount); }
1322 template<typename _Tp1>
1324 owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const
1325 { return _M_refcount._M_less(__rhs._M_refcount); }
1329 { __weak_ptr().swap(*this); }
1332 swap(__weak_ptr& __s) noexcept
1334 std::swap(_M_ptr, __s._M_ptr);
1335 _M_refcount._M_swap(__s._M_refcount);
1339 // Used by __enable_shared_from_this.
1341 _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept
1344 _M_refcount = __refcount;
1347 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1348 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1349 friend class __enable_shared_from_this<_Tp, _Lp>;
1350 friend class enable_shared_from_this<_Tp>;
1352 _Tp* _M_ptr; // Contained pointer.
1353 __weak_count<_Lp> _M_refcount; // Reference counter.
1356 // 20.8.13.3.7 weak_ptr specialized algorithms.
1357 template<typename _Tp, _Lock_policy _Lp>
1359 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1362 template<typename _Tp, typename _Tp1>
1363 struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
1366 operator()(const _Tp& __lhs, const _Tp& __rhs) const
1367 { return __lhs.owner_before(__rhs); }
1370 operator()(const _Tp& __lhs, const _Tp1& __rhs) const
1371 { return __lhs.owner_before(__rhs); }
1374 operator()(const _Tp1& __lhs, const _Tp& __rhs) const
1375 { return __lhs.owner_before(__rhs); }
1378 template<typename _Tp, _Lock_policy _Lp>
1379 struct owner_less<__shared_ptr<_Tp, _Lp>>
1380 : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1383 template<typename _Tp, _Lock_policy _Lp>
1384 struct owner_less<__weak_ptr<_Tp, _Lp>>
1385 : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1389 template<typename _Tp, _Lock_policy _Lp>
1390 class __enable_shared_from_this
1393 constexpr __enable_shared_from_this() noexcept { }
1395 __enable_shared_from_this(const __enable_shared_from_this&) noexcept { }
1397 __enable_shared_from_this&
1398 operator=(const __enable_shared_from_this&) noexcept
1401 ~__enable_shared_from_this() { }
1404 __shared_ptr<_Tp, _Lp>
1406 { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1408 __shared_ptr<const _Tp, _Lp>
1409 shared_from_this() const
1410 { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1413 template<typename _Tp1>
1415 _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept
1416 { _M_weak_this._M_assign(__p, __n); }
1418 template<typename _Tp1>
1420 __enable_shared_from_this_helper(const __shared_count<_Lp>& __pn,
1421 const __enable_shared_from_this* __pe,
1422 const _Tp1* __px) noexcept
1425 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1428 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1432 template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args>
1433 inline __shared_ptr<_Tp, _Lp>
1434 __allocate_shared(const _Alloc& __a, _Args&&... __args)
1436 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1437 std::forward<_Args>(__args)...);
1440 template<typename _Tp, _Lock_policy _Lp, typename... _Args>
1441 inline __shared_ptr<_Tp, _Lp>
1442 __make_shared(_Args&&... __args)
1444 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1445 return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
1446 std::forward<_Args>(__args)...);
1449 /// std::hash specialization for __shared_ptr.
1450 template<typename _Tp, _Lock_policy _Lp>
1451 struct hash<__shared_ptr<_Tp, _Lp>>
1452 : public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1455 operator()(const __shared_ptr<_Tp, _Lp>& __s) const noexcept
1456 { return std::hash<_Tp*>()(__s.get()); }
1459 _GLIBCXX_END_NAMESPACE_VERSION
1462 #endif // _SHARED_PTR_BASE_H