2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___STOP_TOKEN_INTRUSIVE_SHARED_PTR_H
11 #define _LIBCPP___STOP_TOKEN_INTRUSIVE_SHARED_PTR_H
13 #include <__atomic/atomic.h>
14 #include <__atomic/memory_order.h>
16 #include <__cstddef/nullptr_t.h>
17 #include <__type_traits/is_reference.h>
18 #include <__utility/move.h>
19 #include <__utility/swap.h>
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 # pragma GCC system_header
26 #include <__undef_macros>
28 _LIBCPP_BEGIN_NAMESPACE_STD
30 #if _LIBCPP_STD_VER >= 20
32 // For intrusive_shared_ptr to work with a type T, specialize __intrusive_shared_ptr_traits<T> and implement
33 // the following function:
35 // static std::atomic<U>& __get_atomic_ref_count(T&);
37 // where U must be an integral type representing the number of references to the object.
39 struct __intrusive_shared_ptr_traits
;
41 // A reference counting shared_ptr for types whose reference counter
42 // is stored inside the class _Tp itself.
43 // When the reference count goes to zero, the destructor of _Tp will be called
45 struct __intrusive_shared_ptr
{
46 _LIBCPP_HIDE_FROM_ABI
__intrusive_shared_ptr() = default;
48 _LIBCPP_HIDE_FROM_ABI
explicit __intrusive_shared_ptr(_Tp
* __raw_ptr
) : __raw_ptr_(__raw_ptr
) {
50 __increment_ref_count(*__raw_ptr_
);
53 _LIBCPP_HIDE_FROM_ABI
__intrusive_shared_ptr(const __intrusive_shared_ptr
& __other
) noexcept
54 : __raw_ptr_(__other
.__raw_ptr_
) {
56 __increment_ref_count(*__raw_ptr_
);
59 _LIBCPP_HIDE_FROM_ABI
__intrusive_shared_ptr(__intrusive_shared_ptr
&& __other
) noexcept
60 : __raw_ptr_(__other
.__raw_ptr_
) {
61 __other
.__raw_ptr_
= nullptr;
64 _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr
& operator=(const __intrusive_shared_ptr
& __other
) noexcept
{
65 if (__other
.__raw_ptr_
!= __raw_ptr_
) {
66 if (__other
.__raw_ptr_
) {
67 __increment_ref_count(*__other
.__raw_ptr_
);
70 __decrement_ref_count(*__raw_ptr_
);
72 __raw_ptr_
= __other
.__raw_ptr_
;
77 _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr
& operator=(__intrusive_shared_ptr
&& __other
) noexcept
{
78 __intrusive_shared_ptr(std::move(__other
)).swap(*this);
82 _LIBCPP_HIDE_FROM_ABI
~__intrusive_shared_ptr() {
84 __decrement_ref_count(*__raw_ptr_
);
88 _LIBCPP_HIDE_FROM_ABI _Tp
* operator->() const noexcept
{ return __raw_ptr_
; }
89 _LIBCPP_HIDE_FROM_ABI _Tp
& operator*() const noexcept
{ return *__raw_ptr_
; }
90 _LIBCPP_HIDE_FROM_ABI
explicit operator bool() const noexcept
{ return __raw_ptr_
!= nullptr; }
92 _LIBCPP_HIDE_FROM_ABI
void swap(__intrusive_shared_ptr
& __other
) { std::swap(__raw_ptr_
, __other
.__raw_ptr_
); }
94 _LIBCPP_HIDE_FROM_ABI
friend void swap(__intrusive_shared_ptr
& __lhs
, __intrusive_shared_ptr
& __rhs
) {
98 _LIBCPP_HIDE_FROM_ABI
friend bool constexpr
99 operator==(const __intrusive_shared_ptr
&, const __intrusive_shared_ptr
&) = default;
101 _LIBCPP_HIDE_FROM_ABI
friend bool constexpr operator==(const __intrusive_shared_ptr
& __ptr
, std::nullptr_t
) {
102 return __ptr
.__raw_ptr_
== nullptr;
106 _Tp
* __raw_ptr_
= nullptr;
108 // the memory order for increment/decrement the counter is the same for shared_ptr
109 // increment is relaxed and decrement is acq_rel
110 _LIBCPP_HIDE_FROM_ABI
static void __increment_ref_count(_Tp
& __obj
) {
111 __get_atomic_ref_count(__obj
).fetch_add(1, std::memory_order_relaxed
);
114 _LIBCPP_HIDE_FROM_ABI
static void __decrement_ref_count(_Tp
& __obj
) {
115 if (__get_atomic_ref_count(__obj
).fetch_sub(1, std::memory_order_acq_rel
) == 1) {
120 _LIBCPP_HIDE_FROM_ABI
static decltype(auto) __get_atomic_ref_count(_Tp
& __obj
) {
121 using __ret_type
= decltype(__intrusive_shared_ptr_traits
<_Tp
>::__get_atomic_ref_count(__obj
));
123 std::is_reference_v
<__ret_type
>, "__get_atomic_ref_count should return a reference to the atomic counter");
124 return __intrusive_shared_ptr_traits
<_Tp
>::__get_atomic_ref_count(__obj
);
128 #endif // _LIBCPP_STD_VER >= 20
130 _LIBCPP_END_NAMESPACE_STD
134 #endif // _LIBCPP___STOP_TOKEN_INTRUSIVE_SHARED_PTR_H