1 // shared_ptr and weak_ptr implementation details -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25 // GCC Note: Based on files from version 1.32.0 of the Boost library.
28 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
31 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
32 // Copyright (C) 2001, 2002, 2003 Peter Dimov
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
37 // enable_shared_from_this.hpp
38 // Copyright (C) 2002 Peter Dimov
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)
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}
49 #ifndef _SHARED_PTR_BASE_H
50 #define _SHARED_PTR_BASE_H 1
52 namespace std _GLIBCXX_VISIBILITY(default)
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
57 * @brief Exception possibly thrown by @c shared_ptr.
60 class bad_weak_ptr : public std::exception
64 what() const noexcept;
66 virtual ~bad_weak_ptr() throw();
69 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
71 __throw_bad_weak_ptr()
80 using __gnu_cxx::_Lock_policy;
81 using __gnu_cxx::__default_lock_policy;
82 using __gnu_cxx::_S_single;
83 using __gnu_cxx::_S_mutex;
84 using __gnu_cxx::_S_atomic;
86 // Empty helper class except when the template argument is _S_mutex.
87 template<_Lock_policy _Lp>
91 // The atomic policy uses fully-fenced builtins, single doesn't care.
92 enum { _S_need_barriers = 0 };
96 class _Mutex_base<_S_mutex>
97 : public __gnu_cxx::__mutex
100 // This policy is used when atomic builtins are not available.
101 // The replacement atomic operations might not have the necessary
103 enum { _S_need_barriers = 1 };
106 template<_Lock_policy _Lp = __default_lock_policy>
107 class _Sp_counted_base
108 : public _Mutex_base<_Lp>
111 _Sp_counted_base() noexcept
112 : _M_use_count(1), _M_weak_count(1) { }
115 ~_Sp_counted_base() noexcept
118 // Called when _M_use_count drops to zero, to release the resources
121 _M_dispose() noexcept = 0;
123 // Called when _M_weak_count drops to zero.
125 _M_destroy() noexcept
129 _M_get_deleter(const std::type_info&) = 0;
133 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
139 _M_release() noexcept
141 // Be race-detector-friendly. For more info see bits/c++config.
142 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
143 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
145 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
147 // There must be a memory barrier between dispose() and destroy()
148 // to ensure that the effects of dispose() are observed in the
149 // thread that runs destroy().
150 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
151 if (_Mutex_base<_Lp>::_S_need_barriers)
153 _GLIBCXX_READ_MEM_BARRIER;
154 _GLIBCXX_WRITE_MEM_BARRIER;
157 // Be race-detector-friendly. For more info see bits/c++config.
158 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
159 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
162 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
169 _M_weak_add_ref() noexcept
170 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
173 _M_weak_release() noexcept
175 // Be race-detector-friendly. For more info see bits/c++config.
176 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
177 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
179 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
180 if (_Mutex_base<_Lp>::_S_need_barriers)
183 // destroy() must observe results of dispose()
184 _GLIBCXX_READ_MEM_BARRIER;
185 _GLIBCXX_WRITE_MEM_BARRIER;
192 _M_get_use_count() const noexcept
194 // No memory barrier is used here so there is no synchronization
195 // with other threads.
196 return const_cast<const volatile _Atomic_word&>(_M_use_count);
200 _Sp_counted_base(_Sp_counted_base const&) = delete;
201 _Sp_counted_base& operator=(_Sp_counted_base const&) = delete;
203 _Atomic_word _M_use_count; // #shared
204 _Atomic_word _M_weak_count; // #weak + (#shared != 0)
209 _Sp_counted_base<_S_single>::
212 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
215 __throw_bad_weak_ptr();
221 _Sp_counted_base<_S_mutex>::
224 __gnu_cxx::__scoped_lock sentry(*this);
225 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
228 __throw_bad_weak_ptr();
234 _Sp_counted_base<_S_atomic>::
237 // Perform lock-free add-if-not-zero operation.
238 _Atomic_word __count;
241 __count = _M_use_count;
243 __throw_bad_weak_ptr();
245 // Replace the current counter value with the old value + 1, as
246 // long as it's not changed meanwhile.
248 while (!__sync_bool_compare_and_swap(&_M_use_count, __count,
253 // Forward declarations.
254 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
257 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
260 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
261 class __enable_shared_from_this;
263 template<typename _Tp>
266 template<typename _Tp>
269 template<typename _Tp>
272 template<typename _Tp>
273 class enable_shared_from_this;
275 template<_Lock_policy _Lp = __default_lock_policy>
278 template<_Lock_policy _Lp = __default_lock_policy>
279 class __shared_count;
282 // Counted ptr with no deleter or allocator support
283 template<typename _Ptr, _Lock_policy _Lp>
284 class _Sp_counted_ptr : public _Sp_counted_base<_Lp>
288 _Sp_counted_ptr(_Ptr __p)
292 _M_dispose() noexcept
296 _M_destroy() noexcept
300 _M_get_deleter(const std::type_info&)
303 _Sp_counted_ptr(const _Sp_counted_ptr&) = delete;
304 _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete;
307 _Ptr _M_ptr; // copy constructor must not throw
312 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
316 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
320 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
322 // Support for custom deleter and/or allocator
323 template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
324 class _Sp_counted_deleter : public _Sp_counted_base<_Lp>
326 typedef typename _Alloc::template
327 rebind<_Sp_counted_deleter>::other _My_alloc_type;
329 // Helper class that stores the Deleter and also acts as an allocator.
330 // Used to dispose of the owned pointer and the internal refcount
331 // Requires that copies of _Alloc can free each other's memory.
333 : public _My_alloc_type // copy constructor must not throw
335 _Deleter _M_del; // copy constructor must not throw
336 _My_Deleter(_Deleter __d, const _Alloc& __a)
337 : _My_alloc_type(__a), _M_del(__d) { }
341 // __d(__p) must not throw.
342 _Sp_counted_deleter(_Ptr __p, _Deleter __d)
343 : _M_ptr(__p), _M_del(__d, _Alloc()) { }
345 // __d(__p) must not throw.
346 _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a)
347 : _M_ptr(__p), _M_del(__d, __a) { }
350 _M_dispose() noexcept
351 { _M_del._M_del(_M_ptr); }
354 _M_destroy() noexcept
356 _My_alloc_type __a(_M_del);
357 this->~_Sp_counted_deleter();
358 __a.deallocate(this, 1);
362 _M_get_deleter(const std::type_info& __ti)
365 return __ti == typeid(_Deleter) ? &_M_del._M_del : 0;
372 _Ptr _M_ptr; // copy constructor must not throw
373 _My_Deleter _M_del; // copy constructor must not throw
376 // helpers for make_shared / allocate_shared
378 template<typename _Tp>
379 struct _Sp_destroy_inplace
381 void operator()(_Tp* __p) const { if (__p) __p->~_Tp(); }
384 struct _Sp_make_shared_tag { };
386 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
387 class _Sp_counted_ptr_inplace
388 : public _Sp_counted_deleter<_Tp*, _Sp_destroy_inplace<_Tp>, _Alloc, _Lp>
390 typedef _Sp_counted_deleter<_Tp*, _Sp_destroy_inplace<_Tp>, _Alloc, _Lp>
395 _Sp_counted_ptr_inplace(_Alloc __a)
396 : _Base_type(static_cast<_Tp*>(0), _Sp_destroy_inplace<_Tp>(), __a)
399 void* __p = &_M_storage;
400 ::new (__p) _Tp(); // might throw
401 _Base_type::_M_ptr = static_cast<_Tp*>(__p);
404 template<typename... _Args>
405 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
406 : _Base_type(static_cast<_Tp*>(0), _Sp_destroy_inplace<_Tp>(), __a)
409 void* __p = &_M_storage;
410 ::new (__p) _Tp(std::forward<_Args>(__args)...); // might throw
411 _Base_type::_M_ptr = static_cast<_Tp*>(__p);
414 // Override because the allocator needs to know the dynamic type
416 _M_destroy() noexcept
418 typedef typename _Alloc::template
419 rebind<_Sp_counted_ptr_inplace>::other _My_alloc_type;
420 _My_alloc_type __a(_Base_type::_M_del);
421 this->~_Sp_counted_ptr_inplace();
422 __a.deallocate(this, 1);
425 // Sneaky trick so __shared_ptr can get the managed pointer
427 _M_get_deleter(const std::type_info& __ti) noexcept
430 return __ti == typeid(_Sp_make_shared_tag)
431 ? static_cast<void*>(&_M_storage)
432 : _Base_type::_M_get_deleter(__ti);
439 typename aligned_storage<sizeof(_Tp), alignment_of<_Tp>::value>::type
443 template<_Lock_policy _Lp>
447 constexpr __shared_count() noexcept : _M_pi(0)
450 template<typename _Ptr>
452 __shared_count(_Ptr __p) : _M_pi(0)
456 _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
461 __throw_exception_again;
465 template<typename _Ptr, typename _Deleter>
466 __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
468 // The allocator's value_type doesn't matter, will rebind it anyway.
469 typedef std::allocator<int> _Alloc;
470 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
471 typedef std::allocator<_Sp_cd_type> _Alloc2;
475 _M_pi = __a2.allocate(1);
476 ::new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d);
480 __d(__p); // Call _Deleter on __p.
482 __a2.deallocate(static_cast<_Sp_cd_type*>(_M_pi), 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 _Alloc::template rebind<_Sp_cd_type>::other _Alloc2;
495 _M_pi = __a2.allocate(1);
496 ::new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d, __a);
500 __d(__p); // Call _Deleter on __p.
502 __a2.deallocate(static_cast<_Sp_cd_type*>(_M_pi), 1);
503 __throw_exception_again;
507 template<typename _Tp, typename _Alloc, typename... _Args>
508 __shared_count(_Sp_make_shared_tag, _Tp*, const _Alloc& __a,
512 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
513 typedef typename _Alloc::template rebind<_Sp_cp_type>::other _Alloc2;
517 _M_pi = __a2.allocate(1);
518 ::new(static_cast<void*>(_M_pi)) _Sp_cp_type(__a,
519 std::forward<_Args>(__args)...);
524 __a2.deallocate(static_cast<_Sp_cp_type*>(_M_pi), 1);
525 __throw_exception_again;
529 #if _GLIBCXX_USE_DEPRECATED
530 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
531 template<typename _Tp>
533 __shared_count(std::auto_ptr<_Tp>&& __r)
534 : _M_pi(new _Sp_counted_ptr<_Tp*, _Lp>(__r.get()))
538 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
539 template<typename _Tp, typename _Del>
541 __shared_count(std::unique_ptr<_Tp, _Del>&& __r)
542 : _M_pi(_S_create_from_up(std::move(__r)))
545 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
546 explicit __shared_count(const __weak_count<_Lp>& __r);
548 ~__shared_count() noexcept
554 __shared_count(const __shared_count& __r) noexcept
558 _M_pi->_M_add_ref_copy();
562 operator=(const __shared_count& __r) noexcept
564 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
568 __tmp->_M_add_ref_copy();
577 _M_swap(__shared_count& __r) noexcept
579 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
585 _M_get_use_count() const noexcept
586 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
589 _M_unique() const noexcept
590 { return this->_M_get_use_count() == 1; }
593 _M_get_deleter(const std::type_info& __ti) const noexcept
594 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
597 _M_less(const __shared_count& __rhs) const noexcept
598 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
601 _M_less(const __weak_count<_Lp>& __rhs) const noexcept
602 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
604 // Friend function injected into enclosing namespace and found by ADL
606 operator==(const __shared_count& __a, const __shared_count& __b) noexcept
607 { return __a._M_pi == __b._M_pi; }
610 friend class __weak_count<_Lp>;
612 template<typename _Tp, typename _Del>
613 static _Sp_counted_base<_Lp>*
614 _S_create_from_up(std::unique_ptr<_Tp, _Del>&& __r,
615 typename std::enable_if<!std::is_reference<_Del>::value>::type* = 0)
617 return new _Sp_counted_deleter<_Tp*, _Del, std::allocator<_Tp>,
618 _Lp>(__r.get(), __r.get_deleter());
621 template<typename _Tp, typename _Del>
622 static _Sp_counted_base<_Lp>*
623 _S_create_from_up(std::unique_ptr<_Tp, _Del>&& __r,
624 typename std::enable_if<std::is_reference<_Del>::value>::type* = 0)
626 typedef typename std::remove_reference<_Del>::type _Del1;
627 typedef std::reference_wrapper<_Del1> _Del2;
628 return new _Sp_counted_deleter<_Tp*, _Del2, std::allocator<_Tp>,
629 _Lp>(__r.get(), std::ref(__r.get_deleter()));
632 _Sp_counted_base<_Lp>* _M_pi;
636 template<_Lock_policy _Lp>
640 constexpr __weak_count() noexcept : _M_pi(0)
643 __weak_count(const __shared_count<_Lp>& __r) noexcept
647 _M_pi->_M_weak_add_ref();
650 __weak_count(const __weak_count<_Lp>& __r) noexcept
654 _M_pi->_M_weak_add_ref();
657 ~__weak_count() noexcept
660 _M_pi->_M_weak_release();
664 operator=(const __shared_count<_Lp>& __r) noexcept
666 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
668 __tmp->_M_weak_add_ref();
670 _M_pi->_M_weak_release();
676 operator=(const __weak_count<_Lp>& __r) noexcept
678 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
680 __tmp->_M_weak_add_ref();
682 _M_pi->_M_weak_release();
688 _M_swap(__weak_count<_Lp>& __r) noexcept
690 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
696 _M_get_use_count() const noexcept
697 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
700 _M_less(const __weak_count& __rhs) const noexcept
701 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
704 _M_less(const __shared_count<_Lp>& __rhs) const noexcept
705 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
707 // Friend function injected into enclosing namespace and found by ADL
709 operator==(const __weak_count& __a, const __weak_count& __b) noexcept
710 { return __a._M_pi == __b._M_pi; }
713 friend class __shared_count<_Lp>;
715 _Sp_counted_base<_Lp>* _M_pi;
718 // Now that __weak_count is defined we can define this constructor:
719 template<_Lock_policy _Lp>
720 inline __shared_count<_Lp>:: __shared_count(const __weak_count<_Lp>& __r)
724 _M_pi->_M_add_ref_lock();
726 __throw_bad_weak_ptr();
730 // Support for enable_shared_from_this.
732 // Friend of __enable_shared_from_this.
733 template<_Lock_policy _Lp, typename _Tp1, typename _Tp2>
735 __enable_shared_from_this_helper(const __shared_count<_Lp>&,
736 const __enable_shared_from_this<_Tp1,
737 _Lp>*, const _Tp2*) noexcept;
739 // Friend of enable_shared_from_this.
740 template<typename _Tp1, typename _Tp2>
742 __enable_shared_from_this_helper(const __shared_count<>&,
743 const enable_shared_from_this<_Tp1>*,
744 const _Tp2*) noexcept;
746 template<_Lock_policy _Lp>
748 __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...) noexcept
752 template<typename _Tp, _Lock_policy _Lp>
756 typedef _Tp element_type;
758 constexpr __shared_ptr() noexcept
759 : _M_ptr(0), _M_refcount()
762 template<typename _Tp1>
763 explicit __shared_ptr(_Tp1* __p)
764 : _M_ptr(__p), _M_refcount(__p)
766 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
767 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
768 __enable_shared_from_this_helper(_M_refcount, __p, __p);
771 template<typename _Tp1, typename _Deleter>
772 __shared_ptr(_Tp1* __p, _Deleter __d)
773 : _M_ptr(__p), _M_refcount(__p, __d)
775 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
776 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
777 __enable_shared_from_this_helper(_M_refcount, __p, __p);
780 template<typename _Tp1, typename _Deleter, typename _Alloc>
781 __shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
782 : _M_ptr(__p), _M_refcount(__p, __d, std::move(__a))
784 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
785 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
786 __enable_shared_from_this_helper(_M_refcount, __p, __p);
789 template<typename _Deleter>
790 __shared_ptr(nullptr_t __p, _Deleter __d)
791 : _M_ptr(0), _M_refcount(__p, __d)
794 template<typename _Deleter, typename _Alloc>
795 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
796 : _M_ptr(0), _M_refcount(__p, __d, std::move(__a))
799 template<typename _Tp1>
800 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p) noexcept
801 : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
804 __shared_ptr(const __shared_ptr&) noexcept = default;
805 __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
806 ~__shared_ptr() = default;
808 template<typename _Tp1, typename = typename
809 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
810 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
811 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
814 __shared_ptr(__shared_ptr&& __r) noexcept
815 : _M_ptr(__r._M_ptr), _M_refcount()
817 _M_refcount._M_swap(__r._M_refcount);
821 template<typename _Tp1, typename = typename
822 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
823 __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
824 : _M_ptr(__r._M_ptr), _M_refcount()
826 _M_refcount._M_swap(__r._M_refcount);
830 template<typename _Tp1>
831 explicit __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
832 : _M_refcount(__r._M_refcount) // may throw
834 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
836 // It is now safe to copy __r._M_ptr, as
837 // _M_refcount(__r._M_refcount) did not throw.
841 // If an exception is thrown this constructor has no effect.
842 template<typename _Tp1, typename _Del>
843 __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
844 : _M_ptr(__r.get()), _M_refcount()
846 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
847 _Tp1* __tmp = __r.get();
848 _M_refcount = __shared_count<_Lp>(std::move(__r));
849 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
852 #if _GLIBCXX_USE_DEPRECATED
853 // Postcondition: use_count() == 1 and __r.get() == 0
854 template<typename _Tp1>
855 __shared_ptr(std::auto_ptr<_Tp1>&& __r)
856 : _M_ptr(__r.get()), _M_refcount()
858 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
859 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
860 _Tp1* __tmp = __r.get();
861 _M_refcount = __shared_count<_Lp>(std::move(__r));
862 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
866 /* TODO: use delegating constructor */
867 constexpr __shared_ptr(nullptr_t) noexcept
868 : _M_ptr(0), _M_refcount()
871 template<typename _Tp1>
873 operator=(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
876 _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
880 #if _GLIBCXX_USE_DEPRECATED
881 template<typename _Tp1>
883 operator=(std::auto_ptr<_Tp1>&& __r)
885 __shared_ptr(std::move(__r)).swap(*this);
891 operator=(__shared_ptr&& __r) noexcept
893 __shared_ptr(std::move(__r)).swap(*this);
899 operator=(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
901 __shared_ptr(std::move(__r)).swap(*this);
905 template<typename _Tp1, typename _Del>
907 operator=(std::unique_ptr<_Tp1, _Del>&& __r)
909 __shared_ptr(std::move(__r)).swap(*this);
915 { __shared_ptr().swap(*this); }
917 template<typename _Tp1>
919 reset(_Tp1* __p) // _Tp1 must be complete.
921 // Catch self-reset errors.
922 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
923 __shared_ptr(__p).swap(*this);
926 template<typename _Tp1, typename _Deleter>
928 reset(_Tp1* __p, _Deleter __d)
929 { __shared_ptr(__p, __d).swap(*this); }
931 template<typename _Tp1, typename _Deleter, typename _Alloc>
933 reset(_Tp1* __p, _Deleter __d, _Alloc __a)
934 { __shared_ptr(__p, __d, std::move(__a)).swap(*this); }
936 // Allow class instantiation when _Tp is [cv-qual] void.
937 typename std::add_lvalue_reference<_Tp>::type
938 operator*() const noexcept
940 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
945 operator->() const noexcept
947 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
955 explicit operator bool() const // never throws
956 { return _M_ptr == 0 ? false : true; }
959 unique() const noexcept
960 { return _M_refcount._M_unique(); }
963 use_count() const noexcept
964 { return _M_refcount._M_get_use_count(); }
967 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
969 std::swap(_M_ptr, __other._M_ptr);
970 _M_refcount._M_swap(__other._M_refcount);
973 template<typename _Tp1>
975 owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const
976 { return _M_refcount._M_less(__rhs._M_refcount); }
978 template<typename _Tp1>
980 owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const
981 { return _M_refcount._M_less(__rhs._M_refcount); }
985 // This constructor is non-standard, it is used by allocate_shared.
986 template<typename _Alloc, typename... _Args>
987 __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
989 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
990 std::forward<_Args>(__args)...)
992 // _M_ptr needs to point to the newly constructed object.
993 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
994 void* __p = _M_refcount._M_get_deleter(typeid(__tag));
995 _M_ptr = static_cast<_Tp*>(__p);
996 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
999 template<typename _Alloc>
1002 void operator()(_Tp* __ptr)
1004 _M_alloc.destroy(__ptr);
1005 _M_alloc.deallocate(__ptr, 1);
1010 template<typename _Alloc, typename... _Args>
1011 __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
1013 : _M_ptr(), _M_refcount()
1015 typedef typename _Alloc::template rebind<_Tp>::other _Alloc2;
1016 _Deleter<_Alloc2> __del = { _Alloc2(__a) };
1017 _M_ptr = __del._M_alloc.allocate(1);
1020 __del._M_alloc.construct(_M_ptr, std::forward<_Args>(__args)...);
1024 __del._M_alloc.deallocate(_M_ptr, 1);
1025 __throw_exception_again;
1027 __shared_count<_Lp> __count(_M_ptr, __del, __del._M_alloc);
1028 _M_refcount._M_swap(__count);
1029 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1033 template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
1035 friend __shared_ptr<_Tp1, _Lp1>
1036 __allocate_shared(const _Alloc& __a, _Args&&... __args);
1040 _M_get_deleter(const std::type_info& __ti) const noexcept
1041 { return _M_refcount._M_get_deleter(__ti); }
1043 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1044 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1046 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
1047 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1049 _Tp* _M_ptr; // Contained pointer.
1050 __shared_count<_Lp> _M_refcount; // Reference counter.
1054 // 20.8.13.2.7 shared_ptr comparisons
1055 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1057 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
1058 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1059 { return __a.get() == __b.get(); }
1061 template<typename _Tp, _Lock_policy _Lp>
1063 operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1066 template<typename _Tp, _Lock_policy _Lp>
1068 operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1071 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1073 operator!=(const __shared_ptr<_Tp1, _Lp>& __a,
1074 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1075 { return __a.get() != __b.get(); }
1077 template<typename _Tp, _Lock_policy _Lp>
1079 operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1080 { return (bool)__a; }
1082 template<typename _Tp, _Lock_policy _Lp>
1084 operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1085 { return (bool)__a; }
1087 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1089 operator<(const __shared_ptr<_Tp1, _Lp>& __a,
1090 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1092 typedef typename std::common_type<_Tp1*, _Tp2*>::type _CT;
1093 return std::less<_CT>()(__a.get(), __b.get());
1096 template<typename _Tp, _Lock_policy _Lp>
1098 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1099 { return std::less<_Tp*>()(__a.get(), nullptr); }
1101 template<typename _Tp, _Lock_policy _Lp>
1103 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1104 { return std::less<_Tp*>()(nullptr, __a.get()); }
1106 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1108 operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1109 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1110 { return !(__b < __a); }
1112 template<typename _Tp, _Lock_policy _Lp>
1114 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1115 { return !(nullptr < __a); }
1117 template<typename _Tp, _Lock_policy _Lp>
1119 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1120 { return !(__a < nullptr); }
1122 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1124 operator>(const __shared_ptr<_Tp1, _Lp>& __a,
1125 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1126 { return (__b < __a); }
1128 template<typename _Tp, _Lock_policy _Lp>
1130 operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1131 { return std::less<_Tp*>()(nullptr, __a.get()); }
1133 template<typename _Tp, _Lock_policy _Lp>
1135 operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1136 { return std::less<_Tp*>()(__a.get(), nullptr); }
1138 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1140 operator>=(const __shared_ptr<_Tp1, _Lp>& __a,
1141 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1142 { return !(__a < __b); }
1144 template<typename _Tp, _Lock_policy _Lp>
1146 operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1147 { return !(__a < nullptr); }
1149 template<typename _Tp, _Lock_policy _Lp>
1151 operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1152 { return !(nullptr < __a); }
1154 template<typename _Sp>
1155 struct _Sp_less : public binary_function<_Sp, _Sp, bool>
1158 operator()(const _Sp& __lhs, const _Sp& __rhs) const noexcept
1160 typedef typename _Sp::element_type element_type;
1161 return std::less<element_type*>()(__lhs.get(), __rhs.get());
1165 template<typename _Tp, _Lock_policy _Lp>
1166 struct less<__shared_ptr<_Tp, _Lp>>
1167 : public _Sp_less<__shared_ptr<_Tp, _Lp>>
1170 // 2.2.3.8 shared_ptr specialized algorithms.
1171 template<typename _Tp, _Lock_policy _Lp>
1173 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1176 // 2.2.3.9 shared_ptr casts
1178 // The seemingly equivalent code:
1179 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1180 // will eventually result in undefined behaviour, attempting to
1181 // delete the same object twice.
1182 /// static_pointer_cast
1183 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1184 inline __shared_ptr<_Tp, _Lp>
1185 static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1186 { return __shared_ptr<_Tp, _Lp>(__r, static_cast<_Tp*>(__r.get())); }
1188 // The seemingly equivalent code:
1189 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
1190 // will eventually result in undefined behaviour, attempting to
1191 // delete the same object twice.
1192 /// const_pointer_cast
1193 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1194 inline __shared_ptr<_Tp, _Lp>
1195 const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1196 { return __shared_ptr<_Tp, _Lp>(__r, const_cast<_Tp*>(__r.get())); }
1198 // The seemingly equivalent code:
1199 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
1200 // will eventually result in undefined behaviour, attempting to
1201 // delete the same object twice.
1202 /// dynamic_pointer_cast
1203 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1204 inline __shared_ptr<_Tp, _Lp>
1205 dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1207 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
1208 return __shared_ptr<_Tp, _Lp>(__r, __p);
1209 return __shared_ptr<_Tp, _Lp>();
1213 template<typename _Tp, _Lock_policy _Lp>
1217 typedef _Tp element_type;
1219 constexpr __weak_ptr() noexcept
1220 : _M_ptr(0), _M_refcount()
1223 __weak_ptr(const __weak_ptr&) noexcept = default;
1224 __weak_ptr& operator=(const __weak_ptr&) noexcept = default;
1225 ~__weak_ptr() = default;
1227 // The "obvious" converting constructor implementation:
1229 // template<typename _Tp1>
1230 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1231 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1234 // has a serious problem.
1236 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1237 // conversion may require access to *__r._M_ptr (virtual inheritance).
1239 // It is not possible to avoid spurious access violations since
1240 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1241 template<typename _Tp1, typename = typename
1242 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
1243 __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1244 : _M_refcount(__r._M_refcount)
1245 { _M_ptr = __r.lock().get(); }
1247 template<typename _Tp1, typename = typename
1248 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
1249 __weak_ptr(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1250 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1253 template<typename _Tp1>
1255 operator=(const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1257 _M_ptr = __r.lock().get();
1258 _M_refcount = __r._M_refcount;
1262 template<typename _Tp1>
1264 operator=(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1266 _M_ptr = __r._M_ptr;
1267 _M_refcount = __r._M_refcount;
1271 __shared_ptr<_Tp, _Lp>
1272 lock() const noexcept
1275 // Optimization: avoid throw overhead.
1277 return __shared_ptr<element_type, _Lp>();
1281 return __shared_ptr<element_type, _Lp>(*this);
1283 __catch(const bad_weak_ptr&)
1285 // Q: How can we get here?
1286 // A: Another thread may have invalidated r after the
1287 // use_count test above.
1288 return __shared_ptr<element_type, _Lp>();
1292 // Optimization: avoid try/catch overhead when single threaded.
1293 return expired() ? __shared_ptr<element_type, _Lp>()
1294 : __shared_ptr<element_type, _Lp>(*this);
1300 use_count() const noexcept
1301 { return _M_refcount._M_get_use_count(); }
1304 expired() const noexcept
1305 { return _M_refcount._M_get_use_count() == 0; }
1307 template<typename _Tp1>
1309 owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const
1310 { return _M_refcount._M_less(__rhs._M_refcount); }
1312 template<typename _Tp1>
1314 owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const
1315 { return _M_refcount._M_less(__rhs._M_refcount); }
1319 { __weak_ptr().swap(*this); }
1322 swap(__weak_ptr& __s) noexcept
1324 std::swap(_M_ptr, __s._M_ptr);
1325 _M_refcount._M_swap(__s._M_refcount);
1329 // Used by __enable_shared_from_this.
1331 _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept
1334 _M_refcount = __refcount;
1337 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1338 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1339 friend class __enable_shared_from_this<_Tp, _Lp>;
1340 friend class enable_shared_from_this<_Tp>;
1342 _Tp* _M_ptr; // Contained pointer.
1343 __weak_count<_Lp> _M_refcount; // Reference counter.
1346 // 20.8.13.3.7 weak_ptr specialized algorithms.
1347 template<typename _Tp, _Lock_policy _Lp>
1349 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1352 template<typename _Tp, typename _Tp1>
1353 struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
1356 operator()(const _Tp& __lhs, const _Tp& __rhs) const
1357 { return __lhs.owner_before(__rhs); }
1360 operator()(const _Tp& __lhs, const _Tp1& __rhs) const
1361 { return __lhs.owner_before(__rhs); }
1364 operator()(const _Tp1& __lhs, const _Tp& __rhs) const
1365 { return __lhs.owner_before(__rhs); }
1368 template<typename _Tp, _Lock_policy _Lp>
1369 struct owner_less<__shared_ptr<_Tp, _Lp>>
1370 : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1373 template<typename _Tp, _Lock_policy _Lp>
1374 struct owner_less<__weak_ptr<_Tp, _Lp>>
1375 : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1379 template<typename _Tp, _Lock_policy _Lp>
1380 class __enable_shared_from_this
1383 constexpr __enable_shared_from_this() noexcept { }
1385 __enable_shared_from_this(const __enable_shared_from_this&) noexcept { }
1387 __enable_shared_from_this&
1388 operator=(const __enable_shared_from_this&) noexcept
1391 ~__enable_shared_from_this() { }
1394 __shared_ptr<_Tp, _Lp>
1396 { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1398 __shared_ptr<const _Tp, _Lp>
1399 shared_from_this() const
1400 { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1403 template<typename _Tp1>
1405 _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept
1406 { _M_weak_this._M_assign(__p, __n); }
1408 template<typename _Tp1>
1410 __enable_shared_from_this_helper(const __shared_count<_Lp>& __pn,
1411 const __enable_shared_from_this* __pe,
1412 const _Tp1* __px) noexcept
1415 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1418 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1422 template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args>
1423 inline __shared_ptr<_Tp, _Lp>
1424 __allocate_shared(const _Alloc& __a, _Args&&... __args)
1426 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1427 std::forward<_Args>(__args)...);
1430 template<typename _Tp, _Lock_policy _Lp, typename... _Args>
1431 inline __shared_ptr<_Tp, _Lp>
1432 __make_shared(_Args&&... __args)
1434 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1435 return __allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
1436 std::forward<_Args>(__args)...);
1439 /// std::hash specialization for __shared_ptr.
1440 template<typename _Tp, _Lock_policy _Lp>
1441 struct hash<__shared_ptr<_Tp, _Lp>>
1442 : public std::unary_function<__shared_ptr<_Tp, _Lp>, size_t>
1445 operator()(const __shared_ptr<_Tp, _Lp>& __s) const
1446 { return std::hash<_Tp*>()(__s.get()); }
1449 _GLIBCXX_END_NAMESPACE_VERSION
1452 #endif // _SHARED_PTR_BASE_H