1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
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
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___STOP_TOKEN_INTRUSIVE_SHARED_PTR_H
11 #define _LIBCPP___STOP_TOKEN_INTRUSIVE_SHARED_PTR_H
12 
13 #include <__atomic/atomic.h>
14 #include <__atomic/memory_order.h>
15 #include <__config>
16 #include <__type_traits/is_reference.h>
17 #include <__utility/move.h>
18 #include <__utility/swap.h>
19 #include <cstddef>
20 
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 #  pragma GCC system_header
23 #endif
24 
25 _LIBCPP_PUSH_MACROS
26 #include <__undef_macros>
27 
28 _LIBCPP_BEGIN_NAMESPACE_STD
29 
30 #if _LIBCPP_STD_VER >= 20
31 
32 // For intrusive_shared_ptr to work with a type T, specialize __intrusive_shared_ptr_traits<T> and implement
33 // the following function:
34 //
35 // static std::atomic<U>& __get_atomic_ref_count(T&);
36 //
37 // where U must be an integral type representing the number of references to the object.
38 template <class _Tp>
39 struct __intrusive_shared_ptr_traits;
40 
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
44 template <class _Tp>
45 struct __intrusive_shared_ptr {
46   _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr() = default;
47 
__intrusive_shared_ptr__intrusive_shared_ptr48   _LIBCPP_HIDE_FROM_ABI explicit __intrusive_shared_ptr(_Tp* __raw_ptr) : __raw_ptr_(__raw_ptr) {
49     if (__raw_ptr_)
50       __increment_ref_count(*__raw_ptr_);
51   }
52 
__intrusive_shared_ptr__intrusive_shared_ptr53   _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr(const __intrusive_shared_ptr& __other) noexcept
54       : __raw_ptr_(__other.__raw_ptr_) {
55     if (__raw_ptr_)
56       __increment_ref_count(*__raw_ptr_);
57   }
58 
__intrusive_shared_ptr__intrusive_shared_ptr59   _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr(__intrusive_shared_ptr&& __other) noexcept
60       : __raw_ptr_(__other.__raw_ptr_) {
61     __other.__raw_ptr_ = nullptr;
62   }
63 
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_);
68       }
69       if (__raw_ptr_) {
70         __decrement_ref_count(*__raw_ptr_);
71       }
72       __raw_ptr_ = __other.__raw_ptr_;
73     }
74     return *this;
75   }
76 
77   _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr& operator=(__intrusive_shared_ptr&& __other) noexcept {
78     __intrusive_shared_ptr(std::move(__other)).swap(*this);
79     return *this;
80   }
81 
~__intrusive_shared_ptr__intrusive_shared_ptr82   _LIBCPP_HIDE_FROM_ABI ~__intrusive_shared_ptr() {
83     if (__raw_ptr_) {
84       __decrement_ref_count(*__raw_ptr_);
85     }
86   }
87 
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; }
91 
swap__intrusive_shared_ptr92   _LIBCPP_HIDE_FROM_ABI void swap(__intrusive_shared_ptr& __other) { std::swap(__raw_ptr_, __other.__raw_ptr_); }
93 
swap__intrusive_shared_ptr94   _LIBCPP_HIDE_FROM_ABI friend void swap(__intrusive_shared_ptr& __lhs, __intrusive_shared_ptr& __rhs) {
95     __lhs.swap(__rhs);
96   }
97 
98   _LIBCPP_HIDE_FROM_ABI friend bool constexpr
99   operator==(const __intrusive_shared_ptr&, const __intrusive_shared_ptr&) = default;
100 
101   _LIBCPP_HIDE_FROM_ABI friend bool constexpr operator==(const __intrusive_shared_ptr& __ptr, std::nullptr_t) {
102     return __ptr.__raw_ptr_ == nullptr;
103   }
104 
105 private:
106   _Tp* __raw_ptr_ = nullptr;
107 
108   // the memory order for increment/decrement the counter is the same for shared_ptr
109   // increment is relaxed and decrement is acq_rel
__increment_ref_count__intrusive_shared_ptr110   _LIBCPP_HIDE_FROM_ABI static void __increment_ref_count(_Tp& __obj) {
111     __get_atomic_ref_count(__obj).fetch_add(1, std::memory_order_relaxed);
112   }
113 
__decrement_ref_count__intrusive_shared_ptr114   _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) {
116       delete &__obj;
117     }
118   }
119 
decltype__intrusive_shared_ptr120   _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));
122     static_assert(
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);
125   }
126 };
127 
128 #endif // _LIBCPP_STD_VER >= 20
129 
130 _LIBCPP_END_NAMESPACE_STD
131 
132 _LIBCPP_POP_MACROS
133 
134 #endif // _LIBCPP___STOP_TOKEN_INTRUSIVE_SHARED_PTR_H
135