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 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
65 { return "std::bad_weak_ptr"; }
68 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
70 __throw_bad_weak_ptr()
79 using __gnu_cxx::_Lock_policy
;
80 using __gnu_cxx::__default_lock_policy
;
81 using __gnu_cxx::_S_single
;
82 using __gnu_cxx::_S_mutex
;
83 using __gnu_cxx::_S_atomic
;
85 // Empty helper class except when the template argument is _S_mutex.
86 template<_Lock_policy _Lp
>
90 // The atomic policy uses fully-fenced builtins, single doesn't care.
91 enum { _S_need_barriers
= 0 };
95 class _Mutex_base
<_S_mutex
>
96 : public __gnu_cxx::__mutex
99 // This policy is used when atomic builtins are not available.
100 // The replacement atomic operations might not have the necessary
102 enum { _S_need_barriers
= 1 };
105 template<_Lock_policy _Lp
= __default_lock_policy
>
106 class _Sp_counted_base
107 : public _Mutex_base
<_Lp
>
111 : _M_use_count(1), _M_weak_count(1) { }
114 ~_Sp_counted_base() // nothrow
117 // Called when _M_use_count drops to zero, to release the resources
120 _M_dispose() = 0; // nothrow
122 // Called when _M_weak_count drops to zero.
124 _M_destroy() // nothrow
128 _M_get_deleter(const std::type_info
&) = 0;
132 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count
, 1); }
138 _M_release() // nothrow
140 // Be race-detector-friendly. For more info see bits/c++config.
141 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count
);
142 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, -1) == 1)
144 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count
);
146 // There must be a memory barrier between dispose() and destroy()
147 // to ensure that the effects of dispose() are observed in the
148 // thread that runs destroy().
149 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
150 if (_Mutex_base
<_Lp
>::_S_need_barriers
)
152 _GLIBCXX_READ_MEM_BARRIER
;
153 _GLIBCXX_WRITE_MEM_BARRIER
;
156 // Be race-detector-friendly. For more info see bits/c++config.
157 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count
);
158 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count
,
161 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count
);
168 _M_weak_add_ref() // nothrow
169 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count
, 1); }
172 _M_weak_release() // nothrow
174 // Be race-detector-friendly. For more info see bits/c++config.
175 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count
);
176 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count
, -1) == 1)
178 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count
);
179 if (_Mutex_base
<_Lp
>::_S_need_barriers
)
182 // destroy() must observe results of dispose()
183 _GLIBCXX_READ_MEM_BARRIER
;
184 _GLIBCXX_WRITE_MEM_BARRIER
;
191 _M_get_use_count() const // nothrow
193 // No memory barrier is used here so there is no synchronization
194 // with other threads.
195 return const_cast<const volatile _Atomic_word
&>(_M_use_count
);
199 _Sp_counted_base(_Sp_counted_base
const&);
200 _Sp_counted_base
& operator=(_Sp_counted_base
const&);
202 _Atomic_word _M_use_count
; // #shared
203 _Atomic_word _M_weak_count
; // #weak + (#shared != 0)
208 _Sp_counted_base
<_S_single
>::
211 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
214 __throw_bad_weak_ptr();
220 _Sp_counted_base
<_S_mutex
>::
223 __gnu_cxx::__scoped_lock
sentry(*this);
224 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
227 __throw_bad_weak_ptr();
233 _Sp_counted_base
<_S_atomic
>::
236 // Perform lock-free add-if-not-zero operation.
237 _Atomic_word __count
;
240 __count
= _M_use_count
;
242 __throw_bad_weak_ptr();
244 // Replace the current counter value with the old value + 1, as
245 // long as it's not changed meanwhile.
247 while (!__sync_bool_compare_and_swap(&_M_use_count
, __count
,
252 // Forward declarations.
253 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
256 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
259 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
260 class __enable_shared_from_this
;
262 template<typename _Tp
>
265 template<typename _Tp
>
268 template<typename _Tp
>
271 template<typename _Tp
>
272 class enable_shared_from_this
;
274 template<_Lock_policy _Lp
= __default_lock_policy
>
277 template<_Lock_policy _Lp
= __default_lock_policy
>
278 class __shared_count
;
281 // Counted ptr with no deleter or allocator support
282 template<typename _Ptr
, _Lock_policy _Lp
>
283 class _Sp_counted_ptr
: public _Sp_counted_base
<_Lp
>
287 _Sp_counted_ptr(_Ptr __p
)
291 _M_dispose() // nothrow
295 _M_destroy() // nothrow
299 _M_get_deleter(const std::type_info
&)
302 _Sp_counted_ptr(const _Sp_counted_ptr
&) = delete;
303 _Sp_counted_ptr
& operator=(const _Sp_counted_ptr
&) = delete;
306 _Ptr _M_ptr
; // copy constructor must not throw
311 _Sp_counted_ptr
<nullptr_t
, _S_single
>::_M_dispose() { }
315 _Sp_counted_ptr
<nullptr_t
, _S_mutex
>::_M_dispose() { }
319 _Sp_counted_ptr
<nullptr_t
, _S_atomic
>::_M_dispose() { }
321 // Support for custom deleter and/or allocator
322 template<typename _Ptr
, typename _Deleter
, typename _Alloc
, _Lock_policy _Lp
>
323 class _Sp_counted_deleter
: public _Sp_counted_base
<_Lp
>
325 typedef typename
_Alloc::template
326 rebind
<_Sp_counted_deleter
>::other _My_alloc_type
;
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 _My_alloc_type
// copy constructor must not throw
334 _Deleter _M_del
; // copy constructor must not throw
335 _My_Deleter(_Deleter __d
, const _Alloc
& __a
)
336 : _My_alloc_type(__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() // nothrow
350 { _M_del
._M_del(_M_ptr
); }
353 _M_destroy() // nothrow
355 _My_alloc_type
__a(_M_del
);
356 this->~_Sp_counted_deleter();
357 __a
.deallocate(this, 1);
361 _M_get_deleter(const std::type_info
& __ti
)
364 return __ti
== typeid(_Deleter
) ? &_M_del
._M_del
: 0;
371 _Ptr _M_ptr
; // copy constructor must not throw
372 _My_Deleter _M_del
; // copy constructor must not throw
375 // helpers for make_shared / allocate_shared
377 template<typename _Tp
>
378 struct _Sp_destroy_inplace
380 void operator()(_Tp
* __p
) const { if (__p
) __p
->~_Tp(); }
383 struct _Sp_make_shared_tag
{ };
385 template<typename _Tp
, typename _Alloc
, _Lock_policy _Lp
>
386 class _Sp_counted_ptr_inplace
387 : public _Sp_counted_deleter
<_Tp
*, _Sp_destroy_inplace
<_Tp
>, _Alloc
, _Lp
>
389 typedef _Sp_counted_deleter
<_Tp
*, _Sp_destroy_inplace
<_Tp
>, _Alloc
, _Lp
>
394 _Sp_counted_ptr_inplace(_Alloc __a
)
395 : _Base_type(static_cast<_Tp
*>(0), _Sp_destroy_inplace
<_Tp
>(), __a
)
398 void* __p
= &_M_storage
;
399 ::new (__p
) _Tp(); // might throw
400 _Base_type::_M_ptr
= static_cast<_Tp
*>(__p
);
403 template<typename
... _Args
>
404 _Sp_counted_ptr_inplace(_Alloc __a
, _Args
&&... __args
)
405 : _Base_type(static_cast<_Tp
*>(0), _Sp_destroy_inplace
<_Tp
>(), __a
)
408 void* __p
= &_M_storage
;
409 ::new (__p
) _Tp(std::forward
<_Args
>(__args
)...); // might throw
410 _Base_type::_M_ptr
= static_cast<_Tp
*>(__p
);
413 // Override because the allocator needs to know the dynamic type
415 _M_destroy() // nothrow
417 typedef typename
_Alloc::template
418 rebind
<_Sp_counted_ptr_inplace
>::other _My_alloc_type
;
419 _My_alloc_type
__a(_Base_type::_M_del
);
420 this->~_Sp_counted_ptr_inplace();
421 __a
.deallocate(this, 1);
424 // Sneaky trick so __shared_ptr can get the managed pointer
426 _M_get_deleter(const std::type_info
& __ti
)
429 return __ti
== typeid(_Sp_make_shared_tag
)
430 ? static_cast<void*>(&_M_storage
)
431 : _Base_type::_M_get_deleter(__ti
);
438 typename aligned_storage
<sizeof(_Tp
), alignment_of
<_Tp
>::value
>::type
442 template<_Lock_policy _Lp
>
446 constexpr __shared_count() : _M_pi(0) // nothrow
449 template<typename _Ptr
>
451 __shared_count(_Ptr __p
) : _M_pi(0)
455 _M_pi
= new _Sp_counted_ptr
<_Ptr
, _Lp
>(__p
);
460 __throw_exception_again
;
464 template<typename _Ptr
, typename _Deleter
>
465 __shared_count(_Ptr __p
, _Deleter __d
) : _M_pi(0)
467 // The allocator's value_type doesn't matter, will rebind it anyway.
468 typedef std::allocator
<int> _Alloc
;
469 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
470 typedef std::allocator
<_Sp_cd_type
> _Alloc2
;
474 _M_pi
= __a2
.allocate(1);
475 ::new(static_cast<void*>(_M_pi
)) _Sp_cd_type(__p
, __d
);
479 __d(__p
); // Call _Deleter on __p.
481 __a2
.deallocate(static_cast<_Sp_cd_type
*>(_M_pi
), 1);
482 __throw_exception_again
;
486 template<typename _Ptr
, typename _Deleter
, typename _Alloc
>
487 __shared_count(_Ptr __p
, _Deleter __d
, _Alloc __a
) : _M_pi(0)
489 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
490 typedef typename
_Alloc::template rebind
<_Sp_cd_type
>::other _Alloc2
;
494 _M_pi
= __a2
.allocate(1);
495 ::new(static_cast<void*>(_M_pi
)) _Sp_cd_type(__p
, __d
, __a
);
499 __d(__p
); // Call _Deleter on __p.
501 __a2
.deallocate(static_cast<_Sp_cd_type
*>(_M_pi
), 1);
502 __throw_exception_again
;
506 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
507 __shared_count(_Sp_make_shared_tag
, _Tp
*, const _Alloc
& __a
,
511 typedef _Sp_counted_ptr_inplace
<_Tp
, _Alloc
, _Lp
> _Sp_cp_type
;
512 typedef typename
_Alloc::template rebind
<_Sp_cp_type
>::other _Alloc2
;
516 _M_pi
= __a2
.allocate(1);
517 ::new(static_cast<void*>(_M_pi
)) _Sp_cp_type(__a
,
518 std::forward
<_Args
>(__args
)...);
523 __a2
.deallocate(static_cast<_Sp_cp_type
*>(_M_pi
), 1);
524 __throw_exception_again
;
528 #if _GLIBCXX_DEPRECATED
529 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
530 template<typename _Tp
>
532 __shared_count(std::auto_ptr
<_Tp
>&& __r
)
533 : _M_pi(new _Sp_counted_ptr
<_Tp
*, _Lp
>(__r
.get()))
537 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
538 template<typename _Tp
, typename _Del
>
540 __shared_count(std::unique_ptr
<_Tp
, _Del
>&& __r
)
541 : _M_pi(_S_create_from_up(std::move(__r
)))
544 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
545 explicit __shared_count(const __weak_count
<_Lp
>& __r
);
547 ~__shared_count() // nothrow
553 __shared_count(const __shared_count
& __r
)
554 : _M_pi(__r
._M_pi
) // nothrow
557 _M_pi
->_M_add_ref_copy();
561 operator=(const __shared_count
& __r
) // nothrow
563 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
567 __tmp
->_M_add_ref_copy();
576 _M_swap(__shared_count
& __r
) // nothrow
578 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
584 _M_get_use_count() const // nothrow
585 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
588 _M_unique() const // nothrow
589 { return this->_M_get_use_count() == 1; }
592 _M_get_deleter(const std::type_info
& __ti
) const
593 { return _M_pi
? _M_pi
->_M_get_deleter(__ti
) : 0; }
596 _M_less(const __shared_count
& __rhs
) const
597 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
600 _M_less(const __weak_count
<_Lp
>& __rhs
) const
601 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
603 // Friend function injected into enclosing namespace and found by ADL
605 operator==(const __shared_count
& __a
, const __shared_count
& __b
)
606 { return __a
._M_pi
== __b
._M_pi
; }
609 friend class __weak_count
<_Lp
>;
611 template<typename _Tp
, typename _Del
>
612 static _Sp_counted_base
<_Lp
>*
613 _S_create_from_up(std::unique_ptr
<_Tp
, _Del
>&& __r
,
614 typename
std::enable_if
<!std::is_reference
<_Del
>::value
>::type
* = 0)
616 return new _Sp_counted_deleter
<_Tp
*, _Del
, std::allocator
<_Tp
>,
617 _Lp
>(__r
.get(), __r
.get_deleter());
620 template<typename _Tp
, typename _Del
>
621 static _Sp_counted_base
<_Lp
>*
622 _S_create_from_up(std::unique_ptr
<_Tp
, _Del
>&& __r
,
623 typename
std::enable_if
<std::is_reference
<_Del
>::value
>::type
* = 0)
625 typedef typename
std::remove_reference
<_Del
>::type _Del1
;
626 typedef std::reference_wrapper
<_Del1
> _Del2
;
627 return new _Sp_counted_deleter
<_Tp
*, _Del2
, std::allocator
<_Tp
>,
628 _Lp
>(__r
.get(), std::ref(__r
.get_deleter()));
631 _Sp_counted_base
<_Lp
>* _M_pi
;
635 template<_Lock_policy _Lp
>
639 constexpr __weak_count() : _M_pi(0) // nothrow
642 __weak_count(const __shared_count
<_Lp
>& __r
) : _M_pi(__r
._M_pi
) // nothrow
645 _M_pi
->_M_weak_add_ref();
648 __weak_count(const __weak_count
<_Lp
>& __r
) : _M_pi(__r
._M_pi
) // nothrow
651 _M_pi
->_M_weak_add_ref();
654 ~__weak_count() // nothrow
657 _M_pi
->_M_weak_release();
661 operator=(const __shared_count
<_Lp
>& __r
) // nothrow
663 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
665 __tmp
->_M_weak_add_ref();
667 _M_pi
->_M_weak_release();
673 operator=(const __weak_count
<_Lp
>& __r
) // nothrow
675 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
677 __tmp
->_M_weak_add_ref();
679 _M_pi
->_M_weak_release();
685 _M_swap(__weak_count
<_Lp
>& __r
) // nothrow
687 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
693 _M_get_use_count() const // nothrow
694 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
697 _M_less(const __weak_count
& __rhs
) const
698 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
701 _M_less(const __shared_count
<_Lp
>& __rhs
) const
702 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
704 // Friend function injected into enclosing namespace and found by ADL
706 operator==(const __weak_count
& __a
, const __weak_count
& __b
)
707 { return __a
._M_pi
== __b
._M_pi
; }
710 friend class __shared_count
<_Lp
>;
712 _Sp_counted_base
<_Lp
>* _M_pi
;
715 // Now that __weak_count is defined we can define this constructor:
716 template<_Lock_policy _Lp
>
717 inline __shared_count
<_Lp
>:: __shared_count(const __weak_count
<_Lp
>& __r
)
721 _M_pi
->_M_add_ref_lock();
723 __throw_bad_weak_ptr();
727 // Support for enable_shared_from_this.
729 // Friend of __enable_shared_from_this.
730 template<_Lock_policy _Lp
, typename _Tp1
, typename _Tp2
>
732 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&,
733 const __enable_shared_from_this
<_Tp1
,
736 // Friend of enable_shared_from_this.
737 template<typename _Tp1
, typename _Tp2
>
739 __enable_shared_from_this_helper(const __shared_count
<>&,
740 const enable_shared_from_this
<_Tp1
>*,
743 template<_Lock_policy _Lp
>
745 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&, ...)
749 template<typename _Tp
, _Lock_policy _Lp
>
753 typedef _Tp element_type
;
755 constexpr __shared_ptr()
756 : _M_ptr(0), _M_refcount() // never throws
759 template<typename _Tp1
>
760 explicit __shared_ptr(_Tp1
* __p
)
761 : _M_ptr(__p
), _M_refcount(__p
)
763 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
764 static_assert( sizeof(_Tp1
) > 0, "incomplete type" );
765 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
768 template<typename _Tp1
, typename _Deleter
>
769 __shared_ptr(_Tp1
* __p
, _Deleter __d
)
770 : _M_ptr(__p
), _M_refcount(__p
, __d
)
772 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
773 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
774 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
777 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
778 __shared_ptr(_Tp1
* __p
, _Deleter __d
, _Alloc __a
)
779 : _M_ptr(__p
), _M_refcount(__p
, __d
, std::move(__a
))
781 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
782 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
783 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
786 template<typename _Deleter
>
787 __shared_ptr(nullptr_t __p
, _Deleter __d
)
788 : _M_ptr(0), _M_refcount(__p
, __d
)
791 template<typename _Deleter
, typename _Alloc
>
792 __shared_ptr(nullptr_t __p
, _Deleter __d
, _Alloc __a
)
793 : _M_ptr(0), _M_refcount(__p
, __d
, std::move(__a
))
796 template<typename _Tp1
>
797 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
, _Tp
* __p
)
798 : _M_ptr(__p
), _M_refcount(__r
._M_refcount
) // never throws
801 // generated copy constructor, assignment, destructor are fine.
803 template<typename _Tp1
, typename
= typename
804 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
805 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
806 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
809 __shared_ptr(__shared_ptr
&& __r
)
810 : _M_ptr(__r
._M_ptr
), _M_refcount() // never throws
812 _M_refcount
._M_swap(__r
._M_refcount
);
816 template<typename _Tp1
, typename
= typename
817 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
818 __shared_ptr(__shared_ptr
<_Tp1
, _Lp
>&& __r
)
819 : _M_ptr(__r
._M_ptr
), _M_refcount() // never throws
821 _M_refcount
._M_swap(__r
._M_refcount
);
825 template<typename _Tp1
>
826 explicit __shared_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
827 : _M_refcount(__r
._M_refcount
) // may throw
829 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
831 // It is now safe to copy __r._M_ptr, as
832 // _M_refcount(__r._M_refcount) did not throw.
836 // If an exception is thrown this constructor has no effect.
837 template<typename _Tp1
, typename _Del
>
838 __shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
839 : _M_ptr(__r
.get()), _M_refcount()
841 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
842 _Tp1
* __tmp
= __r
.get();
843 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
844 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
847 #if _GLIBCXX_DEPRECATED
848 // Postcondition: use_count() == 1 and __r.get() == 0
849 template<typename _Tp1
>
850 __shared_ptr(std::auto_ptr
<_Tp1
>&& __r
)
851 : _M_ptr(__r
.get()), _M_refcount()
853 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
854 static_assert( sizeof(_Tp1
) > 0, "incomplete type" );
855 _Tp1
* __tmp
= __r
.get();
856 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
857 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
861 /* TODO: use delegating constructor */
862 constexpr __shared_ptr(nullptr_t
)
863 : _M_ptr(0), _M_refcount() // never throws
866 template<typename _Tp1
>
868 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
871 _M_refcount
= __r
._M_refcount
; // __shared_count::op= doesn't throw
875 #if _GLIBCXX_DEPRECATED
876 template<typename _Tp1
>
878 operator=(std::auto_ptr
<_Tp1
>&& __r
)
880 __shared_ptr(std::move(__r
)).swap(*this);
886 operator=(__shared_ptr
&& __r
)
888 __shared_ptr(std::move(__r
)).swap(*this);
894 operator=(__shared_ptr
<_Tp1
, _Lp
>&& __r
)
896 __shared_ptr(std::move(__r
)).swap(*this);
900 template<typename _Tp1
, typename _Del
>
902 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
904 __shared_ptr(std::move(__r
)).swap(*this);
909 reset() // never throws
910 { __shared_ptr().swap(*this); }
912 template<typename _Tp1
>
914 reset(_Tp1
* __p
) // _Tp1 must be complete.
916 // Catch self-reset errors.
917 _GLIBCXX_DEBUG_ASSERT(__p
== 0 || __p
!= _M_ptr
);
918 __shared_ptr(__p
).swap(*this);
921 template<typename _Tp1
, typename _Deleter
>
923 reset(_Tp1
* __p
, _Deleter __d
)
924 { __shared_ptr(__p
, __d
).swap(*this); }
926 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
928 reset(_Tp1
* __p
, _Deleter __d
, _Alloc __a
)
929 { __shared_ptr(__p
, __d
, std::move(__a
)).swap(*this); }
931 // Allow class instantiation when _Tp is [cv-qual] void.
932 typename
std::add_lvalue_reference
<_Tp
>::type
933 operator*() const // never throws
935 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
940 operator->() const // never throws
942 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
947 get() const // never throws
950 explicit operator bool() const // never throws
951 { return _M_ptr
== 0 ? false : true; }
954 unique() const // never throws
955 { return _M_refcount
._M_unique(); }
958 use_count() const // never throws
959 { return _M_refcount
._M_get_use_count(); }
962 swap(__shared_ptr
<_Tp
, _Lp
>& __other
) // never throws
964 std::swap(_M_ptr
, __other
._M_ptr
);
965 _M_refcount
._M_swap(__other
._M_refcount
);
968 template<typename _Tp1
>
970 owner_before(__shared_ptr
<_Tp1
, _Lp
> const& __rhs
) const
971 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
973 template<typename _Tp1
>
975 owner_before(__weak_ptr
<_Tp1
, _Lp
> const& __rhs
) const
976 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
980 // This constructor is non-standard, it is used by allocate_shared.
981 template<typename _Alloc
, typename
... _Args
>
982 __shared_ptr(_Sp_make_shared_tag __tag
, const _Alloc
& __a
,
984 : _M_ptr(), _M_refcount(__tag
, (_Tp
*)0, __a
,
985 std::forward
<_Args
>(__args
)...)
987 // _M_ptr needs to point to the newly constructed object.
988 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
989 void* __p
= _M_refcount
._M_get_deleter(typeid(__tag
));
990 _M_ptr
= static_cast<_Tp
*>(__p
);
991 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
994 template<typename _Alloc
>
997 void operator()(_Tp
* __ptr
)
999 _M_alloc
.destroy(__ptr
);
1000 _M_alloc
.deallocate(__ptr
, 1);
1005 template<typename _Alloc
, typename
... _Args
>
1006 __shared_ptr(_Sp_make_shared_tag __tag
, const _Alloc
& __a
,
1008 : _M_ptr(), _M_refcount()
1010 typedef typename
_Alloc::template rebind
<_Tp
>::other _Alloc2
;
1011 _Deleter
<_Alloc2
> __del
= { _Alloc2(__a
) };
1012 _M_ptr
= __del
._M_alloc
.allocate(1);
1015 __del
._M_alloc
.construct(_M_ptr
, std::forward
<_Args
>(__args
)...);
1019 __del
._M_alloc
.deallocate(_M_ptr
, 1);
1020 __throw_exception_again
;
1022 __shared_count
<_Lp
> __count(_M_ptr
, __del
, __del
._M_alloc
);
1023 _M_refcount
._M_swap(__count
);
1024 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
1028 template<typename _Tp1
, _Lock_policy _Lp1
, typename _Alloc
,
1030 friend __shared_ptr
<_Tp1
, _Lp1
>
1031 __allocate_shared(const _Alloc
& __a
, _Args
&&... __args
);
1035 _M_get_deleter(const std::type_info
& __ti
) const
1036 { return _M_refcount
._M_get_deleter(__ti
); }
1038 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1039 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1041 template<typename _Del
, typename _Tp1
, _Lock_policy _Lp1
>
1042 friend _Del
* get_deleter(const __shared_ptr
<_Tp1
, _Lp1
>&);
1044 _Tp
* _M_ptr
; // Contained pointer.
1045 __shared_count
<_Lp
> _M_refcount
; // Reference counter.
1049 // 20.8.13.2.7 shared_ptr comparisons
1050 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1052 operator==(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1053 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
1054 { return __a
.get() == __b
.get(); }
1056 template<typename _Tp
, _Lock_policy _Lp
>
1058 operator==(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
)
1059 { return __a
.get() == nullptr; }
1061 template<typename _Tp
, _Lock_policy _Lp
>
1063 operator==(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __b
)
1064 { return nullptr == __b
.get(); }
1066 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1068 operator!=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1069 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
1070 { return __a
.get() != __b
.get(); }
1072 template<typename _Tp
, _Lock_policy _Lp
>
1074 operator!=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
)
1075 { return __a
.get() != nullptr; }
1077 template<typename _Tp
, _Lock_policy _Lp
>
1079 operator!=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __b
)
1080 { return nullptr != __b
.get(); }
1082 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1084 operator<(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1085 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
1086 { return __a
.get() < __b
.get(); }
1088 template<typename _Sp
>
1089 struct _Sp_less
: public binary_function
<_Sp
, _Sp
, bool>
1092 operator()(const _Sp
& __lhs
, const _Sp
& __rhs
) const
1094 typedef typename
_Sp::element_type element_type
;
1095 return std::less
<element_type
*>()(__lhs
.get(), __rhs
.get());
1099 template<typename _Tp
, _Lock_policy _Lp
>
1100 struct less
<__shared_ptr
<_Tp
, _Lp
>>
1101 : public _Sp_less
<__shared_ptr
<_Tp
, _Lp
>>
1104 // 2.2.3.8 shared_ptr specialized algorithms.
1105 template<typename _Tp
, _Lock_policy _Lp
>
1107 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
)
1110 // 2.2.3.9 shared_ptr casts
1112 // The seemingly equivalent code:
1113 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1114 // will eventually result in undefined behaviour, attempting to
1115 // delete the same object twice.
1116 /// static_pointer_cast
1117 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1118 inline __shared_ptr
<_Tp
, _Lp
>
1119 static_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
1120 { return __shared_ptr
<_Tp
, _Lp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
1122 // The seemingly equivalent code:
1123 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
1124 // will eventually result in undefined behaviour, attempting to
1125 // delete the same object twice.
1126 /// const_pointer_cast
1127 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1128 inline __shared_ptr
<_Tp
, _Lp
>
1129 const_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
1130 { return __shared_ptr
<_Tp
, _Lp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
1132 // The seemingly equivalent code:
1133 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
1134 // will eventually result in undefined behaviour, attempting to
1135 // delete the same object twice.
1136 /// dynamic_pointer_cast
1137 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1138 inline __shared_ptr
<_Tp
, _Lp
>
1139 dynamic_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
1141 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
1142 return __shared_ptr
<_Tp
, _Lp
>(__r
, __p
);
1143 return __shared_ptr
<_Tp
, _Lp
>();
1147 template<typename _Tp
, _Lock_policy _Lp
>
1151 typedef _Tp element_type
;
1153 constexpr __weak_ptr()
1154 : _M_ptr(0), _M_refcount() // never throws
1157 // Generated copy constructor, assignment, destructor are fine.
1159 // The "obvious" converting constructor implementation:
1161 // template<typename _Tp1>
1162 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1163 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1166 // has a serious problem.
1168 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1169 // conversion may require access to *__r._M_ptr (virtual inheritance).
1171 // It is not possible to avoid spurious access violations since
1172 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1173 template<typename _Tp1
, typename
= typename
1174 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
1175 __weak_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
1176 : _M_refcount(__r
._M_refcount
) // never throws
1177 { _M_ptr
= __r
.lock().get(); }
1179 template<typename _Tp1
, typename
= typename
1180 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
1181 __weak_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
1182 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
1185 template<typename _Tp1
>
1187 operator=(const __weak_ptr
<_Tp1
, _Lp
>& __r
) // never throws
1189 _M_ptr
= __r
.lock().get();
1190 _M_refcount
= __r
._M_refcount
;
1194 template<typename _Tp1
>
1196 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
1198 _M_ptr
= __r
._M_ptr
;
1199 _M_refcount
= __r
._M_refcount
;
1203 __shared_ptr
<_Tp
, _Lp
>
1204 lock() const // never throws
1207 // Optimization: avoid throw overhead.
1209 return __shared_ptr
<element_type
, _Lp
>();
1213 return __shared_ptr
<element_type
, _Lp
>(*this);
1215 __catch(const bad_weak_ptr
&)
1217 // Q: How can we get here?
1218 // A: Another thread may have invalidated r after the
1219 // use_count test above.
1220 return __shared_ptr
<element_type
, _Lp
>();
1224 // Optimization: avoid try/catch overhead when single threaded.
1225 return expired() ? __shared_ptr
<element_type
, _Lp
>()
1226 : __shared_ptr
<element_type
, _Lp
>(*this);
1232 use_count() const // never throws
1233 { return _M_refcount
._M_get_use_count(); }
1236 expired() const // never throws
1237 { return _M_refcount
._M_get_use_count() == 0; }
1239 template<typename _Tp1
>
1241 owner_before(const __shared_ptr
<_Tp1
, _Lp
>& __rhs
) const
1242 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1244 template<typename _Tp1
>
1246 owner_before(const __weak_ptr
<_Tp1
, _Lp
>& __rhs
) const
1247 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1250 reset() // never throws
1251 { __weak_ptr().swap(*this); }
1254 swap(__weak_ptr
& __s
) // never throws
1256 std::swap(_M_ptr
, __s
._M_ptr
);
1257 _M_refcount
._M_swap(__s
._M_refcount
);
1261 // Used by __enable_shared_from_this.
1263 _M_assign(_Tp
* __ptr
, const __shared_count
<_Lp
>& __refcount
)
1266 _M_refcount
= __refcount
;
1269 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1270 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1271 friend class __enable_shared_from_this
<_Tp
, _Lp
>;
1272 friend class enable_shared_from_this
<_Tp
>;
1274 _Tp
* _M_ptr
; // Contained pointer.
1275 __weak_count
<_Lp
> _M_refcount
; // Reference counter.
1278 // 20.8.13.3.7 weak_ptr specialized algorithms.
1279 template<typename _Tp
, _Lock_policy _Lp
>
1281 swap(__weak_ptr
<_Tp
, _Lp
>& __a
, __weak_ptr
<_Tp
, _Lp
>& __b
)
1284 template<typename _Tp
, typename _Tp1
>
1285 struct _Sp_owner_less
: public binary_function
<_Tp
, _Tp
, bool>
1288 operator()(const _Tp
& __lhs
, const _Tp
& __rhs
) const
1289 { return __lhs
.owner_before(__rhs
); }
1292 operator()(const _Tp
& __lhs
, const _Tp1
& __rhs
) const
1293 { return __lhs
.owner_before(__rhs
); }
1296 operator()(const _Tp1
& __lhs
, const _Tp
& __rhs
) const
1297 { return __lhs
.owner_before(__rhs
); }
1300 template<typename _Tp
, _Lock_policy _Lp
>
1301 struct owner_less
<__shared_ptr
<_Tp
, _Lp
>>
1302 : public _Sp_owner_less
<__shared_ptr
<_Tp
, _Lp
>, __weak_ptr
<_Tp
, _Lp
>>
1305 template<typename _Tp
, _Lock_policy _Lp
>
1306 struct owner_less
<__weak_ptr
<_Tp
, _Lp
>>
1307 : public _Sp_owner_less
<__weak_ptr
<_Tp
, _Lp
>, __shared_ptr
<_Tp
, _Lp
>>
1311 template<typename _Tp
, _Lock_policy _Lp
>
1312 class __enable_shared_from_this
1315 constexpr __enable_shared_from_this() { }
1317 __enable_shared_from_this(const __enable_shared_from_this
&) { }
1319 __enable_shared_from_this
&
1320 operator=(const __enable_shared_from_this
&)
1323 ~__enable_shared_from_this() { }
1326 __shared_ptr
<_Tp
, _Lp
>
1328 { return __shared_ptr
<_Tp
, _Lp
>(this->_M_weak_this
); }
1330 __shared_ptr
<const _Tp
, _Lp
>
1331 shared_from_this() const
1332 { return __shared_ptr
<const _Tp
, _Lp
>(this->_M_weak_this
); }
1335 template<typename _Tp1
>
1337 _M_weak_assign(_Tp1
* __p
, const __shared_count
<_Lp
>& __n
) const
1338 { _M_weak_this
._M_assign(__p
, __n
); }
1340 template<typename _Tp1
>
1342 __enable_shared_from_this_helper(const __shared_count
<_Lp
>& __pn
,
1343 const __enable_shared_from_this
* __pe
,
1347 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1350 mutable __weak_ptr
<_Tp
, _Lp
> _M_weak_this
;
1354 template<typename _Tp
, _Lock_policy _Lp
, typename _Alloc
, typename
... _Args
>
1355 inline __shared_ptr
<_Tp
, _Lp
>
1356 __allocate_shared(const _Alloc
& __a
, _Args
&&... __args
)
1358 return __shared_ptr
<_Tp
, _Lp
>(_Sp_make_shared_tag(), __a
,
1359 std::forward
<_Args
>(__args
)...);
1362 template<typename _Tp
, _Lock_policy _Lp
, typename
... _Args
>
1363 inline __shared_ptr
<_Tp
, _Lp
>
1364 __make_shared(_Args
&&... __args
)
1366 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1367 return __allocate_shared
<_Tp
, _Lp
>(std::allocator
<_Tp_nc
>(),
1368 std::forward
<_Args
>(__args
)...);
1371 /// std::hash specialization for __shared_ptr.
1372 template<typename _Tp
, _Lock_policy _Lp
>
1373 struct hash
<__shared_ptr
<_Tp
, _Lp
>>
1374 : public std::unary_function
<__shared_ptr
<_Tp
, _Lp
>, size_t>
1377 operator()(const __shared_ptr
<_Tp
, _Lp
>& __s
) const
1378 { return std::hash
<_Tp
*>()(__s
.get()); }
1381 _GLIBCXX_END_NAMESPACE_VERSION
1384 #endif // _SHARED_PTR_BASE_H