1*06c3fb27SDimitry Andric // -*- C++ -*- 2*06c3fb27SDimitry Andric //===----------------------------------------------------------------------===// 3*06c3fb27SDimitry Andric // 4*06c3fb27SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5*06c3fb27SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 6*06c3fb27SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7*06c3fb27SDimitry Andric // 8*06c3fb27SDimitry Andric //===----------------------------------------------------------------------===// 9*06c3fb27SDimitry Andric 10*06c3fb27SDimitry Andric #ifndef _LIBCPP___STOP_TOKEN_INTRUSIVE_SHARED_PTR_H 11*06c3fb27SDimitry Andric #define _LIBCPP___STOP_TOKEN_INTRUSIVE_SHARED_PTR_H 12*06c3fb27SDimitry Andric 13*06c3fb27SDimitry Andric #include <__atomic/atomic.h> 14*06c3fb27SDimitry Andric #include <__atomic/memory_order.h> 15*06c3fb27SDimitry Andric #include <__config> 16*06c3fb27SDimitry Andric #include <__type_traits/is_reference.h> 17*06c3fb27SDimitry Andric #include <__utility/move.h> 18*06c3fb27SDimitry Andric #include <__utility/swap.h> 19*06c3fb27SDimitry Andric #include <cstddef> 20*06c3fb27SDimitry Andric 21*06c3fb27SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 22*06c3fb27SDimitry Andric # pragma GCC system_header 23*06c3fb27SDimitry Andric #endif 24*06c3fb27SDimitry Andric 25*06c3fb27SDimitry Andric _LIBCPP_PUSH_MACROS 26*06c3fb27SDimitry Andric #include <__undef_macros> 27*06c3fb27SDimitry Andric 28*06c3fb27SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 29*06c3fb27SDimitry Andric 30*06c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20 31*06c3fb27SDimitry Andric 32*06c3fb27SDimitry Andric // For intrusive_shared_ptr to work with a type T, specialize __intrusive_shared_ptr_traits<T> and implement 33*06c3fb27SDimitry Andric // the following function: 34*06c3fb27SDimitry Andric // 35*06c3fb27SDimitry Andric // static std::atomic<U>& __get_atomic_ref_count(T&); 36*06c3fb27SDimitry Andric // 37*06c3fb27SDimitry Andric // where U must be an integral type representing the number of references to the object. 38*06c3fb27SDimitry Andric template <class _Tp> 39*06c3fb27SDimitry Andric struct __intrusive_shared_ptr_traits; 40*06c3fb27SDimitry Andric 41*06c3fb27SDimitry Andric // A reference counting shared_ptr for types whose reference counter 42*06c3fb27SDimitry Andric // is stored inside the class _Tp itself. 43*06c3fb27SDimitry Andric // When the reference count goes to zero, the destructor of _Tp will be called 44*06c3fb27SDimitry Andric template <class _Tp> 45*06c3fb27SDimitry Andric struct __intrusive_shared_ptr { 46*06c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr() = default; 47*06c3fb27SDimitry Andric __intrusive_shared_ptr__intrusive_shared_ptr48*06c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit __intrusive_shared_ptr(_Tp* __raw_ptr) : __raw_ptr_(__raw_ptr) { 49*06c3fb27SDimitry Andric if (__raw_ptr_) 50*06c3fb27SDimitry Andric __increment_ref_count(*__raw_ptr_); 51*06c3fb27SDimitry Andric } 52*06c3fb27SDimitry Andric __intrusive_shared_ptr__intrusive_shared_ptr53*06c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr(const __intrusive_shared_ptr& __other) noexcept 54*06c3fb27SDimitry Andric : __raw_ptr_(__other.__raw_ptr_) { 55*06c3fb27SDimitry Andric if (__raw_ptr_) 56*06c3fb27SDimitry Andric __increment_ref_count(*__raw_ptr_); 57*06c3fb27SDimitry Andric } 58*06c3fb27SDimitry Andric __intrusive_shared_ptr__intrusive_shared_ptr59*06c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr(__intrusive_shared_ptr&& __other) noexcept 60*06c3fb27SDimitry Andric : __raw_ptr_(__other.__raw_ptr_) { 61*06c3fb27SDimitry Andric __other.__raw_ptr_ = nullptr; 62*06c3fb27SDimitry Andric } 63*06c3fb27SDimitry Andric 64*06c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr& operator=(const __intrusive_shared_ptr& __other) noexcept { 65*06c3fb27SDimitry Andric if (__other.__raw_ptr_ != __raw_ptr_) { 66*06c3fb27SDimitry Andric if (__other.__raw_ptr_) { 67*06c3fb27SDimitry Andric __increment_ref_count(*__other.__raw_ptr_); 68*06c3fb27SDimitry Andric } 69*06c3fb27SDimitry Andric if (__raw_ptr_) { 70*06c3fb27SDimitry Andric __decrement_ref_count(*__raw_ptr_); 71*06c3fb27SDimitry Andric } 72*06c3fb27SDimitry Andric __raw_ptr_ = __other.__raw_ptr_; 73*06c3fb27SDimitry Andric } 74*06c3fb27SDimitry Andric return *this; 75*06c3fb27SDimitry Andric } 76*06c3fb27SDimitry Andric 77*06c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr& operator=(__intrusive_shared_ptr&& __other) noexcept { 78*06c3fb27SDimitry Andric __intrusive_shared_ptr(std::move(__other)).swap(*this); 79*06c3fb27SDimitry Andric return *this; 80*06c3fb27SDimitry Andric } 81*06c3fb27SDimitry Andric ~__intrusive_shared_ptr__intrusive_shared_ptr82*06c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI ~__intrusive_shared_ptr() { 83*06c3fb27SDimitry Andric if (__raw_ptr_) { 84*06c3fb27SDimitry Andric __decrement_ref_count(*__raw_ptr_); 85*06c3fb27SDimitry Andric } 86*06c3fb27SDimitry Andric } 87*06c3fb27SDimitry Andric 88*06c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp* operator->() const noexcept { return __raw_ptr_; } 89*06c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp& operator*() const noexcept { return *__raw_ptr_; } 90*06c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit operator bool() const noexcept { return __raw_ptr_ != nullptr; } 91*06c3fb27SDimitry Andric swap__intrusive_shared_ptr92*06c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI void swap(__intrusive_shared_ptr& __other) { std::swap(__raw_ptr_, __other.__raw_ptr_); } 93*06c3fb27SDimitry Andric swap__intrusive_shared_ptr94*06c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI friend void swap(__intrusive_shared_ptr& __lhs, __intrusive_shared_ptr& __rhs) { 95*06c3fb27SDimitry Andric __lhs.swap(__rhs); 96*06c3fb27SDimitry Andric } 97*06c3fb27SDimitry Andric 98*06c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI friend bool constexpr 99*06c3fb27SDimitry Andric operator==(const __intrusive_shared_ptr&, const __intrusive_shared_ptr&) = default; 100*06c3fb27SDimitry Andric 101*06c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI friend bool constexpr operator==(const __intrusive_shared_ptr& __ptr, std::nullptr_t) { 102*06c3fb27SDimitry Andric return __ptr.__raw_ptr_ == nullptr; 103*06c3fb27SDimitry Andric } 104*06c3fb27SDimitry Andric 105*06c3fb27SDimitry Andric private: 106*06c3fb27SDimitry Andric _Tp* __raw_ptr_ = nullptr; 107*06c3fb27SDimitry Andric 108*06c3fb27SDimitry Andric // the memory order for increment/decrement the counter is the same for shared_ptr 109*06c3fb27SDimitry Andric // increment is relaxed and decrement is acq_rel __increment_ref_count__intrusive_shared_ptr110*06c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI static void __increment_ref_count(_Tp& __obj) { 111*06c3fb27SDimitry Andric __get_atomic_ref_count(__obj).fetch_add(1, std::memory_order_relaxed); 112*06c3fb27SDimitry Andric } 113*06c3fb27SDimitry Andric __decrement_ref_count__intrusive_shared_ptr114*06c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI static void __decrement_ref_count(_Tp& __obj) { 115*06c3fb27SDimitry Andric if (__get_atomic_ref_count(__obj).fetch_sub(1, std::memory_order_acq_rel) == 1) { 116*06c3fb27SDimitry Andric delete &__obj; 117*06c3fb27SDimitry Andric } 118*06c3fb27SDimitry Andric } 119*06c3fb27SDimitry Andric decltype__intrusive_shared_ptr120*06c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI static decltype(auto) __get_atomic_ref_count(_Tp& __obj) { 121*06c3fb27SDimitry Andric using __ret_type = decltype(__intrusive_shared_ptr_traits<_Tp>::__get_atomic_ref_count(__obj)); 122*06c3fb27SDimitry Andric static_assert( 123*06c3fb27SDimitry Andric std::is_reference_v<__ret_type>, "__get_atomic_ref_count should return a reference to the atomic counter"); 124*06c3fb27SDimitry Andric return __intrusive_shared_ptr_traits<_Tp>::__get_atomic_ref_count(__obj); 125*06c3fb27SDimitry Andric } 126*06c3fb27SDimitry Andric }; 127*06c3fb27SDimitry Andric 128*06c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20 129*06c3fb27SDimitry Andric 130*06c3fb27SDimitry Andric _LIBCPP_END_NAMESPACE_STD 131*06c3fb27SDimitry Andric 132*06c3fb27SDimitry Andric _LIBCPP_POP_MACROS 133*06c3fb27SDimitry Andric 134*06c3fb27SDimitry Andric #endif // _LIBCPP___STOP_TOKEN_INTRUSIVE_SHARED_PTR_H 135