#ifndef _SHARED_PTR_BASE_H
#define _SHARED_PTR_BASE_H 1
+#include <bits/allocated_ptr.h>
#include <ext/aligned_buffer.h>
namespace std _GLIBCXX_VISIBILITY(default)
void
_M_add_ref_lock();
-
+
+ bool
+ _M_add_ref_lock_nothrow();
+
void
_M_release() noexcept
{
__ATOMIC_RELAXED));
}
+ template<>
+ inline bool
+ _Sp_counted_base<_S_single>::
+ _M_add_ref_lock_nothrow()
+ {
+ if (_M_use_count == 0)
+ return false;
+ ++_M_use_count;
+ return true;
+ }
+
+ template<>
+ inline bool
+ _Sp_counted_base<_S_mutex>::
+ _M_add_ref_lock_nothrow()
+ {
+ __gnu_cxx::__scoped_lock sentry(*this);
+ if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
+ {
+ _M_use_count = 0;
+ return false;
+ }
+ return true;
+ }
+
+ template<>
+ inline bool
+ _Sp_counted_base<_S_atomic>::
+ _M_add_ref_lock_nothrow()
+ {
+ // Perform lock-free add-if-not-zero operation.
+ _Atomic_word __count = _M_get_use_count();
+ do
+ {
+ if (__count == 0)
+ return false;
+ // Replace the current counter value with the old value + 1, as
+ // long as it's not changed meanwhile.
+ }
+ while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
+ true, __ATOMIC_ACQ_REL,
+ __ATOMIC_RELAXED));
+ return true;
+ }
+
template<>
inline void
_Sp_counted_base<_S_single>::_M_add_ref_copy()
};
public:
+ using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
+
// __d(__p) must not throw.
_Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
: _M_impl(__p, __d, _Alloc()) { }
virtual void
_M_destroy() noexcept
{
- typedef typename allocator_traits<_Alloc>::template
- rebind_traits<_Sp_counted_deleter> _Alloc_traits;
- typename _Alloc_traits::allocator_type __a(_M_impl._M_alloc());
- _Alloc_traits::destroy(__a, this);
- _Alloc_traits::deallocate(__a, this, 1);
+ __allocator_type __a(_M_impl._M_alloc());
+ __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
+ this->~_Sp_counted_deleter();
}
virtual void*
};
public:
+ using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
+
template<typename... _Args>
_Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
: _M_impl(__a)
virtual void
_M_destroy() noexcept
{
- typedef typename allocator_traits<_Alloc>::template
- rebind_traits<_Sp_counted_ptr_inplace> _Alloc_traits;
- typename _Alloc_traits::allocator_type __a(_M_impl._M_alloc());
- _Alloc_traits::destroy(__a, this);
- _Alloc_traits::deallocate(__a, this, 1);
+ __allocator_type __a(_M_impl._M_alloc());
+ __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
+ this->~_Sp_counted_ptr_inplace();
}
// Sneaky trick so __shared_ptr can get the managed pointer
__shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
{
typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
- typedef typename allocator_traits<_Alloc>::template
- rebind_traits<_Sp_cd_type> _Alloc_traits;
- typename _Alloc_traits::allocator_type __a2(__a);
- _Sp_cd_type* __mem = 0;
__try
{
- __mem = _Alloc_traits::allocate(__a2, 1);
- _Alloc_traits::construct(__a2, __mem,
- __p, std::move(__d), std::move(__a));
+ typename _Sp_cd_type::__allocator_type __a2(__a);
+ auto __guard = std::__allocate_guarded(__a2);
+ _Sp_cd_type* __mem = __guard.get();
+ ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a));
_M_pi = __mem;
+ __guard = nullptr;
}
__catch(...)
{
__d(__p); // Call _Deleter on __p.
- if (__mem)
- _Alloc_traits::deallocate(__a2, __mem, 1);
__throw_exception_again;
}
}
: _M_pi(0)
{
typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
- typedef typename allocator_traits<_Alloc>::template
- rebind_traits<_Sp_cp_type> _Alloc_traits;
- typename _Alloc_traits::allocator_type __a2(__a);
- _Sp_cp_type* __mem = _Alloc_traits::allocate(__a2, 1);
- __try
- {
- _Alloc_traits::construct(__a2, __mem, std::move(__a),
- std::forward<_Args>(__args)...);
- _M_pi = __mem;
- }
- __catch(...)
- {
- _Alloc_traits::deallocate(__a2, __mem, 1);
- __throw_exception_again;
- }
+ typename _Sp_cp_type::__allocator_type __a2(__a);
+ auto __guard = std::__allocate_guarded(__a2);
+ _Sp_cp_type* __mem = __guard.get();
+ ::new (__mem) _Sp_cp_type(std::move(__a),
+ std::forward<_Args>(__args)...);
+ _M_pi = __mem;
+ __guard = nullptr;
}
#if _GLIBCXX_USE_DEPRECATED
// Throw bad_weak_ptr when __r._M_get_use_count() == 0.
explicit __shared_count(const __weak_count<_Lp>& __r);
+ // Does not throw if __r._M_get_use_count() == 0, caller must check.
+ explicit __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t);
+
~__shared_count() noexcept
{
if (_M_pi != nullptr)
// Now that __weak_count is defined we can define this constructor:
template<_Lock_policy _Lp>
- inline __shared_count<_Lp>:: __shared_count(const __weak_count<_Lp>& __r)
+ inline
+ __shared_count<_Lp>::__shared_count(const __weak_count<_Lp>& __r)
: _M_pi(__r._M_pi)
{
- if (_M_pi != 0)
+ if (_M_pi != nullptr)
_M_pi->_M_add_ref_lock();
else
__throw_bad_weak_ptr();
}
+ // Now that __weak_count is defined we can define this constructor:
+ template<_Lock_policy _Lp>
+ inline
+ __shared_count<_Lp>::
+ __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t)
+ : _M_pi(__r._M_pi)
+ {
+ if (_M_pi != nullptr)
+ if (!_M_pi->_M_add_ref_lock_nothrow())
+ _M_pi = nullptr;
+ }
// Support for enable_shared_from_this.
: _M_ptr(__p), _M_refcount(__p)
{
__glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
- static_assert( !is_void<_Tp>::value, "incomplete type" );
+ static_assert( !is_void<_Tp1>::value, "incomplete type" );
static_assert( sizeof(_Tp1) > 0, "incomplete type" );
__enable_shared_from_this_helper(_M_refcount, __p, __p);
}
__shared_ptr(std::auto_ptr<_Tp1>&& __r);
#endif
- /* TODO: use delegating constructor */
- constexpr __shared_ptr(nullptr_t) noexcept
- : _M_ptr(0), _M_refcount()
- { }
+ constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
template<typename _Tp1>
__shared_ptr&
{
void operator()(_Tp* __ptr)
{
- typedef allocator_traits<_Alloc> _Alloc_traits;
- _Alloc_traits::destroy(_M_alloc, __ptr);
- _Alloc_traits::deallocate(_M_alloc, __ptr, 1);
+ __allocated_ptr<_Alloc> __guard{ _M_alloc, __ptr };
+ allocator_traits<_Alloc>::destroy(_M_alloc, __guard.get());
}
_Alloc _M_alloc;
};
__shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
_Args&&... __args)
: _M_ptr(), _M_refcount()
- {
- typedef typename _Alloc::template rebind<_Tp>::other _Alloc2;
- _Deleter<_Alloc2> __del = { _Alloc2(__a) };
- typedef allocator_traits<_Alloc2> __traits;
- _M_ptr = __traits::allocate(__del._M_alloc, 1);
- __try
- {
- // _GLIBCXX_RESOLVE_LIB_DEFECTS
- // 2070. allocate_shared should use allocator_traits<A>::construct
- __traits::construct(__del._M_alloc, _M_ptr,
- std::forward<_Args>(__args)...);
- }
- __catch(...)
- {
- __traits::deallocate(__del._M_alloc, _M_ptr, 1);
- __throw_exception_again;
- }
- __shared_count<_Lp> __count(_M_ptr, __del, __del._M_alloc);
- _M_refcount._M_swap(__count);
+ {
+ typedef typename allocator_traits<_Alloc>::template
+ rebind_traits<_Tp> __traits;
+ _Deleter<typename __traits::allocator_type> __del = { __a };
+ auto __guard = std::__allocate_guarded(__del._M_alloc);
+ _M_ptr = __guard.get();
+ // _GLIBCXX_RESOLVE_LIB_DEFECTS
+ // 2070. allocate_shared should use allocator_traits<A>::construct
+ __traits::construct(__del._M_alloc, _M_ptr,
+ std::forward<_Args>(__args)...);
+ __guard = nullptr;
+ __shared_count<_Lp> __count(_M_ptr, __del, __del._M_alloc);
+ _M_refcount._M_swap(__count);
__enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
- }
+ }
#endif
template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
friend __shared_ptr<_Tp1, _Lp1>
__allocate_shared(const _Alloc& __a, _Args&&... __args);
+ // This constructor is used by __weak_ptr::lock() and
+ // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t).
+ __shared_ptr(const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t)
+ : _M_refcount(__r._M_refcount, std::nothrow)
+ {
+ _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr : nullptr;
+ }
+
+ friend class __weak_ptr<_Tp, _Lp>;
+
private:
void*
_M_get_deleter(const std::type_info& __ti) const noexcept
__shared_ptr<_Tp, _Lp>
lock() const noexcept
- {
-#ifdef __GTHREADS
- // Optimization: avoid throw overhead.
- if (expired())
- return __shared_ptr<element_type, _Lp>();
-
- __try
- {
- return __shared_ptr<element_type, _Lp>(*this);
- }
- __catch(const bad_weak_ptr&)
- {
- // Q: How can we get here?
- // A: Another thread may have invalidated r after the
- // use_count test above.
- return __shared_ptr<element_type, _Lp>();
- }
-
-#else
- // Optimization: avoid try/catch overhead when single threaded.
- return expired() ? __shared_ptr<element_type, _Lp>()
- : __shared_ptr<element_type, _Lp>(*this);
-
-#endif
- } // XXX MT
+ { return __shared_ptr<element_type, _Lp>(*this, std::nothrow); }
long
use_count() const noexcept