1 // shared_ptr and weak_ptr implementation details -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009, 2010 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 _GLIBCXX_BEGIN_NAMESPACE(std
)
55 * @brief Exception possibly thrown by @c shared_ptr.
58 class bad_weak_ptr
: public std::exception
63 { return "std::bad_weak_ptr"; }
66 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
68 __throw_bad_weak_ptr()
77 using __gnu_cxx::_Lock_policy
;
78 using __gnu_cxx::__default_lock_policy
;
79 using __gnu_cxx::_S_single
;
80 using __gnu_cxx::_S_mutex
;
81 using __gnu_cxx::_S_atomic
;
83 // Empty helper class except when the template argument is _S_mutex.
84 template<_Lock_policy _Lp
>
88 // The atomic policy uses fully-fenced builtins, single doesn't care.
89 enum { _S_need_barriers
= 0 };
93 class _Mutex_base
<_S_mutex
>
94 : public __gnu_cxx::__mutex
97 // This policy is used when atomic builtins are not available.
98 // The replacement atomic operations might not have the necessary
100 enum { _S_need_barriers
= 1 };
103 template<_Lock_policy _Lp
= __default_lock_policy
>
104 class _Sp_counted_base
105 : public _Mutex_base
<_Lp
>
109 : _M_use_count(1), _M_weak_count(1) { }
112 ~_Sp_counted_base() // nothrow
115 // Called when _M_use_count drops to zero, to release the resources
118 _M_dispose() = 0; // nothrow
120 // Called when _M_weak_count drops to zero.
122 _M_destroy() // nothrow
126 _M_get_deleter(const std::type_info
&) = 0;
130 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count
, 1); }
136 _M_release() // nothrow
138 // Be race-detector-friendly. For more info see bits/c++config.
139 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count
);
140 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, -1) == 1)
142 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count
);
144 // There must be a memory barrier between dispose() and destroy()
145 // to ensure that the effects of dispose() are observed in the
146 // thread that runs destroy().
147 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
148 if (_Mutex_base
<_Lp
>::_S_need_barriers
)
150 _GLIBCXX_READ_MEM_BARRIER
;
151 _GLIBCXX_WRITE_MEM_BARRIER
;
154 // Be race-detector-friendly. For more info see bits/c++config.
155 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count
);
156 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count
,
159 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count
);
166 _M_weak_add_ref() // nothrow
167 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count
, 1); }
170 _M_weak_release() // nothrow
172 // Be race-detector-friendly. For more info see bits/c++config.
173 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count
);
174 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count
, -1) == 1)
176 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count
);
177 if (_Mutex_base
<_Lp
>::_S_need_barriers
)
180 // destroy() must observe results of dispose()
181 _GLIBCXX_READ_MEM_BARRIER
;
182 _GLIBCXX_WRITE_MEM_BARRIER
;
189 _M_get_use_count() const // nothrow
191 // No memory barrier is used here so there is no synchronization
192 // with other threads.
193 return const_cast<const volatile _Atomic_word
&>(_M_use_count
);
197 _Sp_counted_base(_Sp_counted_base
const&);
198 _Sp_counted_base
& operator=(_Sp_counted_base
const&);
200 _Atomic_word _M_use_count
; // #shared
201 _Atomic_word _M_weak_count
; // #weak + (#shared != 0)
206 _Sp_counted_base
<_S_single
>::
209 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
212 __throw_bad_weak_ptr();
218 _Sp_counted_base
<_S_mutex
>::
221 __gnu_cxx::__scoped_lock
sentry(*this);
222 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
225 __throw_bad_weak_ptr();
231 _Sp_counted_base
<_S_atomic
>::
234 // Perform lock-free add-if-not-zero operation.
235 _Atomic_word __count
;
238 __count
= _M_use_count
;
240 __throw_bad_weak_ptr();
242 // Replace the current counter value with the old value + 1, as
243 // long as it's not changed meanwhile.
245 while (!__sync_bool_compare_and_swap(&_M_use_count
, __count
,
250 // Forward declarations.
251 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
254 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
257 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
258 class __enable_shared_from_this
;
260 template<typename _Tp
>
263 template<typename _Tp
>
266 template<typename _Tp
>
269 template<typename _Tp
>
270 class enable_shared_from_this
;
272 template<_Lock_policy _Lp
= __default_lock_policy
>
275 template<_Lock_policy _Lp
= __default_lock_policy
>
276 class __shared_count
;
279 // Counted ptr with no deleter or allocator support
280 template<typename _Ptr
, _Lock_policy _Lp
>
281 class _Sp_counted_ptr
: public _Sp_counted_base
<_Lp
>
285 _Sp_counted_ptr(_Ptr __p
)
289 _M_dispose() // nothrow
293 _M_destroy() // nothrow
297 _M_get_deleter(const std::type_info
&)
300 _Sp_counted_ptr(const _Sp_counted_ptr
&) = delete;
301 _Sp_counted_ptr
& operator=(const _Sp_counted_ptr
&) = delete;
304 _Ptr _M_ptr
; // copy constructor must not throw
309 _Sp_counted_ptr
<nullptr_t
, _S_single
>::_M_dispose() { }
313 _Sp_counted_ptr
<nullptr_t
, _S_mutex
>::_M_dispose() { }
317 _Sp_counted_ptr
<nullptr_t
, _S_atomic
>::_M_dispose() { }
319 // Support for custom deleter and/or allocator
320 template<typename _Ptr
, typename _Deleter
, typename _Alloc
, _Lock_policy _Lp
>
321 class _Sp_counted_deleter
: public _Sp_counted_base
<_Lp
>
323 typedef typename
_Alloc::template
324 rebind
<_Sp_counted_deleter
>::other _My_alloc_type
;
326 // Helper class that stores the Deleter and also acts as an allocator.
327 // Used to dispose of the owned pointer and the internal refcount
328 // Requires that copies of _Alloc can free each other's memory.
330 : public _My_alloc_type
// copy constructor must not throw
332 _Deleter _M_del
; // copy constructor must not throw
333 _My_Deleter(_Deleter __d
, const _Alloc
& __a
)
334 : _My_alloc_type(__a
), _M_del(__d
) { }
338 // __d(__p) must not throw.
339 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
)
340 : _M_ptr(__p
), _M_del(__d
, _Alloc()) { }
342 // __d(__p) must not throw.
343 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
, const _Alloc
& __a
)
344 : _M_ptr(__p
), _M_del(__d
, __a
) { }
347 _M_dispose() // nothrow
348 { _M_del
._M_del(_M_ptr
); }
351 _M_destroy() // nothrow
353 _My_alloc_type
__a(_M_del
);
354 this->~_Sp_counted_deleter();
355 __a
.deallocate(this, 1);
359 _M_get_deleter(const std::type_info
& __ti
)
362 return __ti
== typeid(_Deleter
) ? &_M_del
._M_del
: 0;
369 _Ptr _M_ptr
; // copy constructor must not throw
370 _My_Deleter _M_del
; // copy constructor must not throw
373 // helpers for make_shared / allocate_shared
375 template<typename _Tp
>
376 struct _Sp_destroy_inplace
378 void operator()(_Tp
* __p
) const { if (__p
) __p
->~_Tp(); }
381 struct _Sp_make_shared_tag
{ };
383 template<typename _Tp
, typename _Alloc
, _Lock_policy _Lp
>
384 class _Sp_counted_ptr_inplace
385 : public _Sp_counted_deleter
<_Tp
*, _Sp_destroy_inplace
<_Tp
>, _Alloc
, _Lp
>
387 typedef _Sp_counted_deleter
<_Tp
*, _Sp_destroy_inplace
<_Tp
>, _Alloc
, _Lp
>
392 _Sp_counted_ptr_inplace(_Alloc __a
)
393 : _Base_type(static_cast<_Tp
*>(0), _Sp_destroy_inplace
<_Tp
>(), __a
)
396 void* __p
= &_M_storage
;
397 ::new (__p
) _Tp(); // might throw
398 _Base_type::_M_ptr
= static_cast<_Tp
*>(__p
);
401 template<typename
... _Args
>
402 _Sp_counted_ptr_inplace(_Alloc __a
, _Args
&&... __args
)
403 : _Base_type(static_cast<_Tp
*>(0), _Sp_destroy_inplace
<_Tp
>(), __a
)
406 void* __p
= &_M_storage
;
407 ::new (__p
) _Tp(std::forward
<_Args
>(__args
)...); // might throw
408 _Base_type::_M_ptr
= static_cast<_Tp
*>(__p
);
411 // Override because the allocator needs to know the dynamic type
413 _M_destroy() // nothrow
415 typedef typename
_Alloc::template
416 rebind
<_Sp_counted_ptr_inplace
>::other _My_alloc_type
;
417 _My_alloc_type
__a(_Base_type::_M_del
);
418 this->~_Sp_counted_ptr_inplace();
419 __a
.deallocate(this, 1);
422 // Sneaky trick so __shared_ptr can get the managed pointer
424 _M_get_deleter(const std::type_info
& __ti
)
427 return __ti
== typeid(_Sp_make_shared_tag
)
428 ? static_cast<void*>(&_M_storage
)
429 : _Base_type::_M_get_deleter(__ti
);
436 typename aligned_storage
<sizeof(_Tp
), alignment_of
<_Tp
>::value
>::type
440 template<_Lock_policy _Lp
>
444 constexpr __shared_count() : _M_pi(0) // nothrow
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 std::allocator
<_Sp_cd_type
> _Alloc2
;
472 _M_pi
= __a2
.allocate(1);
473 ::new(static_cast<void*>(_M_pi
)) _Sp_cd_type(__p
, __d
);
477 __d(__p
); // Call _Deleter on __p.
479 __a2
.deallocate(static_cast<_Sp_cd_type
*>(_M_pi
), 1);
480 __throw_exception_again
;
484 template<typename _Ptr
, typename _Deleter
, typename _Alloc
>
485 __shared_count(_Ptr __p
, _Deleter __d
, _Alloc __a
) : _M_pi(0)
487 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
488 typedef typename
_Alloc::template rebind
<_Sp_cd_type
>::other _Alloc2
;
492 _M_pi
= __a2
.allocate(1);
493 ::new(static_cast<void*>(_M_pi
)) _Sp_cd_type(__p
, __d
, __a
);
497 __d(__p
); // Call _Deleter on __p.
499 __a2
.deallocate(static_cast<_Sp_cd_type
*>(_M_pi
), 1);
500 __throw_exception_again
;
504 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
505 __shared_count(_Sp_make_shared_tag
, _Tp
*, const _Alloc
& __a
,
509 typedef _Sp_counted_ptr_inplace
<_Tp
, _Alloc
, _Lp
> _Sp_cp_type
;
510 typedef typename
_Alloc::template rebind
<_Sp_cp_type
>::other _Alloc2
;
514 _M_pi
= __a2
.allocate(1);
515 ::new(static_cast<void*>(_M_pi
)) _Sp_cp_type(__a
,
516 std::forward
<_Args
>(__args
)...);
521 __a2
.deallocate(static_cast<_Sp_cp_type
*>(_M_pi
), 1);
522 __throw_exception_again
;
526 #if _GLIBCXX_DEPRECATED
527 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
528 template<typename _Tp
>
530 __shared_count(std::auto_ptr
<_Tp
>&& __r
)
531 : _M_pi(new _Sp_counted_ptr
<_Tp
*, _Lp
>(__r
.get()))
535 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
536 template<typename _Tp
, typename _Del
>
538 __shared_count(std::unique_ptr
<_Tp
, _Del
>&& __r
)
539 : _M_pi(_S_create_from_up(std::move(__r
)))
542 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
543 explicit __shared_count(const __weak_count
<_Lp
>& __r
);
545 ~__shared_count() // nothrow
551 __shared_count(const __shared_count
& __r
)
552 : _M_pi(__r
._M_pi
) // nothrow
555 _M_pi
->_M_add_ref_copy();
559 operator=(const __shared_count
& __r
) // nothrow
561 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
565 __tmp
->_M_add_ref_copy();
574 _M_swap(__shared_count
& __r
) // nothrow
576 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
582 _M_get_use_count() const // nothrow
583 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
586 _M_unique() const // nothrow
587 { return this->_M_get_use_count() == 1; }
590 _M_get_deleter(const std::type_info
& __ti
) const
591 { return _M_pi
? _M_pi
->_M_get_deleter(__ti
) : 0; }
594 _M_less(const __shared_count
& __rhs
) const
595 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
598 _M_less(const __weak_count
<_Lp
>& __rhs
) const
599 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
601 // Friend function injected into enclosing namespace and found by ADL
603 operator==(const __shared_count
& __a
, const __shared_count
& __b
)
604 { return __a
._M_pi
== __b
._M_pi
; }
607 friend class __weak_count
<_Lp
>;
609 template<typename _Tp
, typename _Del
>
610 static _Sp_counted_base
<_Lp
>*
611 _S_create_from_up(std::unique_ptr
<_Tp
, _Del
>&& __r
,
612 typename
std::enable_if
<!std::is_reference
<_Del
>::value
>::type
* = 0)
614 return new _Sp_counted_deleter
<_Tp
*, _Del
, std::allocator
<_Tp
>,
615 _Lp
>(__r
.get(), __r
.get_deleter());
618 template<typename _Tp
, typename _Del
>
619 static _Sp_counted_base
<_Lp
>*
620 _S_create_from_up(std::unique_ptr
<_Tp
, _Del
>&& __r
,
621 typename
std::enable_if
<std::is_reference
<_Del
>::value
>::type
* = 0)
623 typedef typename
std::remove_reference
<_Del
>::type _Del1
;
624 typedef std::reference_wrapper
<_Del1
> _Del2
;
625 return new _Sp_counted_deleter
<_Tp
*, _Del2
, std::allocator
<_Tp
>,
626 _Lp
>(__r
.get(), std::ref(__r
.get_deleter()));
629 _Sp_counted_base
<_Lp
>* _M_pi
;
633 template<_Lock_policy _Lp
>
637 constexpr __weak_count() : _M_pi(0) // nothrow
640 __weak_count(const __shared_count
<_Lp
>& __r
) : _M_pi(__r
._M_pi
) // nothrow
643 _M_pi
->_M_weak_add_ref();
646 __weak_count(const __weak_count
<_Lp
>& __r
) : _M_pi(__r
._M_pi
) // nothrow
649 _M_pi
->_M_weak_add_ref();
652 ~__weak_count() // nothrow
655 _M_pi
->_M_weak_release();
659 operator=(const __shared_count
<_Lp
>& __r
) // nothrow
661 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
663 __tmp
->_M_weak_add_ref();
665 _M_pi
->_M_weak_release();
671 operator=(const __weak_count
<_Lp
>& __r
) // nothrow
673 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
675 __tmp
->_M_weak_add_ref();
677 _M_pi
->_M_weak_release();
683 _M_swap(__weak_count
<_Lp
>& __r
) // nothrow
685 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
691 _M_get_use_count() const // nothrow
692 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
695 _M_less(const __weak_count
& __rhs
) const
696 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
699 _M_less(const __shared_count
<_Lp
>& __rhs
) const
700 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
702 // Friend function injected into enclosing namespace and found by ADL
704 operator==(const __weak_count
& __a
, const __weak_count
& __b
)
705 { return __a
._M_pi
== __b
._M_pi
; }
708 friend class __shared_count
<_Lp
>;
710 _Sp_counted_base
<_Lp
>* _M_pi
;
713 // Now that __weak_count is defined we can define this constructor:
714 template<_Lock_policy _Lp
>
715 inline __shared_count
<_Lp
>:: __shared_count(const __weak_count
<_Lp
>& __r
)
719 _M_pi
->_M_add_ref_lock();
721 __throw_bad_weak_ptr();
725 // Support for enable_shared_from_this.
727 // Friend of __enable_shared_from_this.
728 template<_Lock_policy _Lp
, typename _Tp1
, typename _Tp2
>
730 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&,
731 const __enable_shared_from_this
<_Tp1
,
734 // Friend of enable_shared_from_this.
735 template<typename _Tp1
, typename _Tp2
>
737 __enable_shared_from_this_helper(const __shared_count
<>&,
738 const enable_shared_from_this
<_Tp1
>*,
741 template<_Lock_policy _Lp
>
743 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&, ...)
747 template<typename _Tp
, _Lock_policy _Lp
>
751 typedef _Tp element_type
;
753 constexpr __shared_ptr()
754 : _M_ptr(0), _M_refcount() // never throws
757 template<typename _Tp1
>
758 explicit __shared_ptr(_Tp1
* __p
)
759 : _M_ptr(__p
), _M_refcount(__p
)
761 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
762 static_assert( sizeof(_Tp1
) > 0, "incomplete type" );
763 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
766 template<typename _Tp1
, typename _Deleter
>
767 __shared_ptr(_Tp1
* __p
, _Deleter __d
)
768 : _M_ptr(__p
), _M_refcount(__p
, __d
)
770 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
771 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
772 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
775 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
776 __shared_ptr(_Tp1
* __p
, _Deleter __d
, _Alloc __a
)
777 : _M_ptr(__p
), _M_refcount(__p
, __d
, std::move(__a
))
779 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
780 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
781 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
784 template<typename _Deleter
>
785 __shared_ptr(nullptr_t __p
, _Deleter __d
)
786 : _M_ptr(0), _M_refcount(__p
, __d
)
789 template<typename _Deleter
, typename _Alloc
>
790 __shared_ptr(nullptr_t __p
, _Deleter __d
, _Alloc __a
)
791 : _M_ptr(0), _M_refcount(__p
, __d
, std::move(__a
))
794 template<typename _Tp1
>
795 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
, _Tp
* __p
)
796 : _M_ptr(__p
), _M_refcount(__r
._M_refcount
) // never throws
799 // generated copy constructor, assignment, destructor are fine.
801 template<typename _Tp1
, typename
= typename
802 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
803 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
804 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
807 __shared_ptr(__shared_ptr
&& __r
)
808 : _M_ptr(__r
._M_ptr
), _M_refcount() // never throws
810 _M_refcount
._M_swap(__r
._M_refcount
);
814 template<typename _Tp1
, typename
= typename
815 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
816 __shared_ptr(__shared_ptr
<_Tp1
, _Lp
>&& __r
)
817 : _M_ptr(__r
._M_ptr
), _M_refcount() // never throws
819 _M_refcount
._M_swap(__r
._M_refcount
);
823 template<typename _Tp1
>
824 explicit __shared_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
825 : _M_refcount(__r
._M_refcount
) // may throw
827 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
829 // It is now safe to copy __r._M_ptr, as
830 // _M_refcount(__r._M_refcount) did not throw.
834 // If an exception is thrown this constructor has no effect.
835 template<typename _Tp1
, typename _Del
>
836 __shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
837 : _M_ptr(__r
.get()), _M_refcount()
839 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
840 _Tp1
* __tmp
= __r
.get();
841 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
842 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
845 #if _GLIBCXX_DEPRECATED
846 // Postcondition: use_count() == 1 and __r.get() == 0
847 template<typename _Tp1
>
848 __shared_ptr(std::auto_ptr
<_Tp1
>&& __r
)
849 : _M_ptr(__r
.get()), _M_refcount()
851 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
852 static_assert( sizeof(_Tp1
) > 0, "incomplete type" );
853 _Tp1
* __tmp
= __r
.get();
854 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
855 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
859 /* TODO: use delegating constructor */
860 constexpr __shared_ptr(nullptr_t
)
861 : _M_ptr(0), _M_refcount() // never throws
864 template<typename _Tp1
>
866 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
869 _M_refcount
= __r
._M_refcount
; // __shared_count::op= doesn't throw
873 #if _GLIBCXX_DEPRECATED
874 template<typename _Tp1
>
876 operator=(std::auto_ptr
<_Tp1
>&& __r
)
878 __shared_ptr(std::move(__r
)).swap(*this);
884 operator=(__shared_ptr
&& __r
)
886 __shared_ptr(std::move(__r
)).swap(*this);
892 operator=(__shared_ptr
<_Tp1
, _Lp
>&& __r
)
894 __shared_ptr(std::move(__r
)).swap(*this);
898 template<typename _Tp1
, typename _Del
>
900 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
902 __shared_ptr(std::move(__r
)).swap(*this);
907 reset() // never throws
908 { __shared_ptr().swap(*this); }
910 template<typename _Tp1
>
912 reset(_Tp1
* __p
) // _Tp1 must be complete.
914 // Catch self-reset errors.
915 _GLIBCXX_DEBUG_ASSERT(__p
== 0 || __p
!= _M_ptr
);
916 __shared_ptr(__p
).swap(*this);
919 template<typename _Tp1
, typename _Deleter
>
921 reset(_Tp1
* __p
, _Deleter __d
)
922 { __shared_ptr(__p
, __d
).swap(*this); }
924 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
926 reset(_Tp1
* __p
, _Deleter __d
, _Alloc __a
)
927 { __shared_ptr(__p
, __d
, std::move(__a
)).swap(*this); }
929 // Allow class instantiation when _Tp is [cv-qual] void.
930 typename
std::add_lvalue_reference
<_Tp
>::type
931 operator*() const // never throws
933 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
938 operator->() const // never throws
940 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
945 get() const // never throws
948 explicit operator bool() const // never throws
949 { return _M_ptr
== 0 ? false : true; }
952 unique() const // never throws
953 { return _M_refcount
._M_unique(); }
956 use_count() const // never throws
957 { return _M_refcount
._M_get_use_count(); }
960 swap(__shared_ptr
<_Tp
, _Lp
>& __other
) // never throws
962 std::swap(_M_ptr
, __other
._M_ptr
);
963 _M_refcount
._M_swap(__other
._M_refcount
);
966 template<typename _Tp1
>
968 owner_before(__shared_ptr
<_Tp1
, _Lp
> const& __rhs
) const
969 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
971 template<typename _Tp1
>
973 owner_before(__weak_ptr
<_Tp1
, _Lp
> const& __rhs
) const
974 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
978 // This constructor is non-standard, it is used by allocate_shared.
979 template<typename _Alloc
, typename
... _Args
>
980 __shared_ptr(_Sp_make_shared_tag __tag
, const _Alloc
& __a
,
982 : _M_ptr(), _M_refcount(__tag
, (_Tp
*)0, __a
,
983 std::forward
<_Args
>(__args
)...)
985 // _M_ptr needs to point to the newly constructed object.
986 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
987 void* __p
= _M_refcount
._M_get_deleter(typeid(__tag
));
988 _M_ptr
= static_cast<_Tp
*>(__p
);
989 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
992 template<typename _Alloc
>
995 void operator()(_Tp
* __ptr
)
997 _M_alloc
.destroy(__ptr
);
998 _M_alloc
.deallocate(__ptr
, 1);
1003 template<typename _Alloc
, typename
... _Args
>
1004 __shared_ptr(_Sp_make_shared_tag __tag
, const _Alloc
& __a
,
1006 : _M_ptr(), _M_refcount()
1008 typedef typename
_Alloc::template rebind
<_Tp
>::other _Alloc2
;
1009 _Deleter
<_Alloc2
> __del
= { _Alloc2(__a
) };
1010 _M_ptr
= __del
._M_alloc
.allocate(1);
1013 __del
._M_alloc
.construct(_M_ptr
, std::forward
<_Args
>(__args
)...);
1017 __del
._M_alloc
.deallocate(_M_ptr
, 1);
1018 __throw_exception_again
;
1020 __shared_count
<_Lp
> __count(_M_ptr
, __del
, __del
._M_alloc
);
1021 _M_refcount
._M_swap(__count
);
1022 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
1026 template<typename _Tp1
, _Lock_policy _Lp1
, typename _Alloc
,
1028 friend __shared_ptr
<_Tp1
, _Lp1
>
1029 __allocate_shared(const _Alloc
& __a
, _Args
&&... __args
);
1033 _M_get_deleter(const std::type_info
& __ti
) const
1034 { return _M_refcount
._M_get_deleter(__ti
); }
1036 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1037 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1039 template<typename _Del
, typename _Tp1
, _Lock_policy _Lp1
>
1040 friend _Del
* get_deleter(const __shared_ptr
<_Tp1
, _Lp1
>&);
1042 _Tp
* _M_ptr
; // Contained pointer.
1043 __shared_count
<_Lp
> _M_refcount
; // Reference counter.
1047 // 20.8.13.2.7 shared_ptr comparisons
1048 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1050 operator==(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1051 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
1052 { return __a
.get() == __b
.get(); }
1054 template<typename _Tp
, _Lock_policy _Lp
>
1056 operator==(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
)
1057 { return __a
.get() == nullptr; }
1059 template<typename _Tp
, _Lock_policy _Lp
>
1061 operator==(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __b
)
1062 { return nullptr == __b
.get(); }
1064 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1066 operator!=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1067 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
1068 { return __a
.get() != __b
.get(); }
1070 template<typename _Tp
, _Lock_policy _Lp
>
1072 operator!=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
)
1073 { return __a
.get() != nullptr; }
1075 template<typename _Tp
, _Lock_policy _Lp
>
1077 operator!=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __b
)
1078 { return nullptr != __b
.get(); }
1080 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1082 operator<(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1083 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
1084 { return __a
.get() < __b
.get(); }
1086 template<typename _Sp
>
1087 struct _Sp_less
: public binary_function
<_Sp
, _Sp
, bool>
1090 operator()(const _Sp
& __lhs
, const _Sp
& __rhs
) const
1092 typedef typename
_Sp::element_type element_type
;
1093 return std::less
<element_type
*>()(__lhs
.get(), __rhs
.get());
1097 template<typename _Tp
, _Lock_policy _Lp
>
1098 struct less
<__shared_ptr
<_Tp
, _Lp
>>
1099 : public _Sp_less
<__shared_ptr
<_Tp
, _Lp
>>
1102 // 2.2.3.8 shared_ptr specialized algorithms.
1103 template<typename _Tp
, _Lock_policy _Lp
>
1105 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
)
1108 // 2.2.3.9 shared_ptr casts
1110 // The seemingly equivalent code:
1111 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1112 // will eventually result in undefined behaviour, attempting to
1113 // delete the same object twice.
1114 /// static_pointer_cast
1115 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1116 inline __shared_ptr
<_Tp
, _Lp
>
1117 static_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
1118 { return __shared_ptr
<_Tp
, _Lp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
1120 // The seemingly equivalent code:
1121 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
1122 // will eventually result in undefined behaviour, attempting to
1123 // delete the same object twice.
1124 /// const_pointer_cast
1125 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1126 inline __shared_ptr
<_Tp
, _Lp
>
1127 const_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
1128 { return __shared_ptr
<_Tp
, _Lp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
1130 // The seemingly equivalent code:
1131 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
1132 // will eventually result in undefined behaviour, attempting to
1133 // delete the same object twice.
1134 /// dynamic_pointer_cast
1135 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1136 inline __shared_ptr
<_Tp
, _Lp
>
1137 dynamic_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
1139 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
1140 return __shared_ptr
<_Tp
, _Lp
>(__r
, __p
);
1141 return __shared_ptr
<_Tp
, _Lp
>();
1145 template<typename _Tp
, _Lock_policy _Lp
>
1149 typedef _Tp element_type
;
1151 constexpr __weak_ptr()
1152 : _M_ptr(0), _M_refcount() // never throws
1155 // Generated copy constructor, assignment, destructor are fine.
1157 // The "obvious" converting constructor implementation:
1159 // template<typename _Tp1>
1160 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1161 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1164 // has a serious problem.
1166 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1167 // conversion may require access to *__r._M_ptr (virtual inheritance).
1169 // It is not possible to avoid spurious access violations since
1170 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1171 template<typename _Tp1
, typename
= typename
1172 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
1173 __weak_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
1174 : _M_refcount(__r
._M_refcount
) // never throws
1175 { _M_ptr
= __r
.lock().get(); }
1177 template<typename _Tp1
, typename
= typename
1178 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
1179 __weak_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
1180 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
1183 template<typename _Tp1
>
1185 operator=(const __weak_ptr
<_Tp1
, _Lp
>& __r
) // never throws
1187 _M_ptr
= __r
.lock().get();
1188 _M_refcount
= __r
._M_refcount
;
1192 template<typename _Tp1
>
1194 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
1196 _M_ptr
= __r
._M_ptr
;
1197 _M_refcount
= __r
._M_refcount
;
1201 __shared_ptr
<_Tp
, _Lp
>
1202 lock() const // never throws
1205 // Optimization: avoid throw overhead.
1207 return __shared_ptr
<element_type
, _Lp
>();
1211 return __shared_ptr
<element_type
, _Lp
>(*this);
1213 __catch(const bad_weak_ptr
&)
1215 // Q: How can we get here?
1216 // A: Another thread may have invalidated r after the
1217 // use_count test above.
1218 return __shared_ptr
<element_type
, _Lp
>();
1222 // Optimization: avoid try/catch overhead when single threaded.
1223 return expired() ? __shared_ptr
<element_type
, _Lp
>()
1224 : __shared_ptr
<element_type
, _Lp
>(*this);
1230 use_count() const // never throws
1231 { return _M_refcount
._M_get_use_count(); }
1234 expired() const // never throws
1235 { return _M_refcount
._M_get_use_count() == 0; }
1237 template<typename _Tp1
>
1239 owner_before(const __shared_ptr
<_Tp1
, _Lp
>& __rhs
) const
1240 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1242 template<typename _Tp1
>
1244 owner_before(const __weak_ptr
<_Tp1
, _Lp
>& __rhs
) const
1245 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1248 reset() // never throws
1249 { __weak_ptr().swap(*this); }
1252 swap(__weak_ptr
& __s
) // never throws
1254 std::swap(_M_ptr
, __s
._M_ptr
);
1255 _M_refcount
._M_swap(__s
._M_refcount
);
1259 // Used by __enable_shared_from_this.
1261 _M_assign(_Tp
* __ptr
, const __shared_count
<_Lp
>& __refcount
)
1264 _M_refcount
= __refcount
;
1267 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1268 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1269 friend class __enable_shared_from_this
<_Tp
, _Lp
>;
1270 friend class enable_shared_from_this
<_Tp
>;
1272 _Tp
* _M_ptr
; // Contained pointer.
1273 __weak_count
<_Lp
> _M_refcount
; // Reference counter.
1276 // 20.8.13.3.7 weak_ptr specialized algorithms.
1277 template<typename _Tp
, _Lock_policy _Lp
>
1279 swap(__weak_ptr
<_Tp
, _Lp
>& __a
, __weak_ptr
<_Tp
, _Lp
>& __b
)
1282 template<typename _Tp
, typename _Tp1
>
1283 struct _Sp_owner_less
: public binary_function
<_Tp
, _Tp
, bool>
1286 operator()(const _Tp
& __lhs
, const _Tp
& __rhs
) const
1287 { return __lhs
.owner_before(__rhs
); }
1290 operator()(const _Tp
& __lhs
, const _Tp1
& __rhs
) const
1291 { return __lhs
.owner_before(__rhs
); }
1294 operator()(const _Tp1
& __lhs
, const _Tp
& __rhs
) const
1295 { return __lhs
.owner_before(__rhs
); }
1298 template<typename _Tp
, _Lock_policy _Lp
>
1299 struct owner_less
<__shared_ptr
<_Tp
, _Lp
>>
1300 : public _Sp_owner_less
<__shared_ptr
<_Tp
, _Lp
>, __weak_ptr
<_Tp
, _Lp
>>
1303 template<typename _Tp
, _Lock_policy _Lp
>
1304 struct owner_less
<__weak_ptr
<_Tp
, _Lp
>>
1305 : public _Sp_owner_less
<__weak_ptr
<_Tp
, _Lp
>, __shared_ptr
<_Tp
, _Lp
>>
1309 template<typename _Tp
, _Lock_policy _Lp
>
1310 class __enable_shared_from_this
1313 constexpr __enable_shared_from_this() { }
1315 __enable_shared_from_this(const __enable_shared_from_this
&) { }
1317 __enable_shared_from_this
&
1318 operator=(const __enable_shared_from_this
&)
1321 ~__enable_shared_from_this() { }
1324 __shared_ptr
<_Tp
, _Lp
>
1326 { return __shared_ptr
<_Tp
, _Lp
>(this->_M_weak_this
); }
1328 __shared_ptr
<const _Tp
, _Lp
>
1329 shared_from_this() const
1330 { return __shared_ptr
<const _Tp
, _Lp
>(this->_M_weak_this
); }
1333 template<typename _Tp1
>
1335 _M_weak_assign(_Tp1
* __p
, const __shared_count
<_Lp
>& __n
) const
1336 { _M_weak_this
._M_assign(__p
, __n
); }
1338 template<typename _Tp1
>
1340 __enable_shared_from_this_helper(const __shared_count
<_Lp
>& __pn
,
1341 const __enable_shared_from_this
* __pe
,
1345 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1348 mutable __weak_ptr
<_Tp
, _Lp
> _M_weak_this
;
1352 template<typename _Tp
, _Lock_policy _Lp
, typename _Alloc
, typename
... _Args
>
1353 inline __shared_ptr
<_Tp
, _Lp
>
1354 __allocate_shared(const _Alloc
& __a
, _Args
&&... __args
)
1356 return __shared_ptr
<_Tp
, _Lp
>(_Sp_make_shared_tag(), __a
,
1357 std::forward
<_Args
>(__args
)...);
1360 template<typename _Tp
, _Lock_policy _Lp
, typename
... _Args
>
1361 inline __shared_ptr
<_Tp
, _Lp
>
1362 __make_shared(_Args
&&... __args
)
1364 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1365 return __allocate_shared
<_Tp
, _Lp
>(std::allocator
<_Tp_nc
>(),
1366 std::forward
<_Args
>(__args
)...);
1369 /// std::hash specialization for __shared_ptr.
1370 template<typename _Tp
, _Lock_policy _Lp
>
1371 struct hash
<__shared_ptr
<_Tp
, _Lp
>>
1372 : public std::unary_function
<__shared_ptr
<_Tp
, _Lp
>, size_t>
1375 operator()(const __shared_ptr
<_Tp
, _Lp
>& __s
) const
1376 { return std::hash
<_Tp
*>()(__s
.get()); }
1379 _GLIBCXX_END_NAMESPACE
1381 #endif // _SHARED_PTR_BASE_H