-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
/
intrusive_ptr.h
217 lines (188 loc) · 5.86 KB
/
intrusive_ptr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*!
* Copyright (c) by Contributors 2020
* \file intrusive_ptr.h
* \brief Implementation of Intrusive Ptr.
*/
#ifndef XGBOOST_INTRUSIVE_PTR_H_
#define XGBOOST_INTRUSIVE_PTR_H_
#include <atomic>
#include <cinttypes>
#include <functional>
#include <ostream>
namespace xgboost {
/*!
* \brief Helper class for embedding reference counting into client objects. See
* https://www.boost.org/doc/libs/1_74_0/doc/html/atomic/usage_examples.html for
* discussions of memory order.
*/
class IntrusivePtrCell {
private:
std::atomic<int32_t> count_ {0};
template <typename T> friend class IntrusivePtr;
std::int32_t IncRef() noexcept {
return count_.fetch_add(1, std::memory_order_relaxed);
}
std::int32_t DecRef() noexcept {
return count_.fetch_sub(1, std::memory_order_release);
}
bool IsZero() const { return Count() == 0; }
public:
IntrusivePtrCell() noexcept = default;
int32_t Count() const { return count_.load(std::memory_order_relaxed); }
};
/*!
* \brief User defined function for returning embedded reference count.
*/
template <typename T> IntrusivePtrCell &IntrusivePtrRefCount(T const *ptr) noexcept;
/*!
* \brief Implementation of Intrusive Pointer. A smart pointer that points to an object
* with an embedded reference counter. The underlying object must implement a
* friend function IntrusivePtrRefCount() that returns the ref counter (of type
* IntrusivePtrCell). The intrusive pointer is faster than std::shared_ptr<>:
* std::shared_ptr<> makes an extra memory allocation for the ref counter whereas
* the intrusive pointer does not.
*
* \code
*
* class ForIntrusivePtrTest {
* public:
* mutable class IntrusivePtrCell ref;
* float data { 0 };
*
* friend IntrusivePtrCell &
* IntrusivePtrRefCount(ForIntrusivePtrTest const *t) noexcept { // NOLINT
* return t->ref;
* }
*
* ForIntrusivePtrTest() = default;
* ForIntrusivePtrTest(float a, int32_t b) : data{a + static_cast<float>(b)} {}
*
* explicit ForIntrusivePtrTest(NotCopyConstructible a) : data{a.data} {}
* };
*
* IntrusivePtr<ForIntrusivePtrTest> ptr {new ForIntrusivePtrTest};
*
* \endcode
*/
template <typename T> class IntrusivePtr {
private:
void IncRef(T *ptr) {
if (ptr) {
IntrusivePtrRefCount(ptr).IncRef();
}
}
void DecRef(T *ptr) {
if (ptr) {
if (IntrusivePtrRefCount(ptr).DecRef() == 1) {
std::atomic_thread_fence(std::memory_order_acquire);
delete ptr;
}
}
}
protected:
T *ptr_{nullptr};
public:
using element_type = T; // NOLINT
struct Hash {
std::size_t operator()(IntrusivePtr<element_type> const &ptr) const noexcept {
return std::hash<element_type *>()(ptr.get());
}
};
/*!
* \brief Contruct an IntrusivePtr from raw pointer. IntrusivePtr takes the ownership.
*
* \param p Raw pointer to object
*/
explicit IntrusivePtr(T *p) : ptr_{p} {
if (ptr_) {
IncRef(ptr_);
}
}
IntrusivePtr() noexcept = default;
IntrusivePtr(IntrusivePtr const &that) : ptr_{that.ptr_} { IncRef(ptr_); }
IntrusivePtr(IntrusivePtr &&that) noexcept : ptr_{that.ptr_} { that.ptr_ = nullptr; }
~IntrusivePtr() { DecRef(ptr_); }
IntrusivePtr<T> &operator=(IntrusivePtr<T> const &that) {
IntrusivePtr<T>{that}.swap(*this);
return *this;
}
IntrusivePtr<T> &operator=(IntrusivePtr<T> &&that) noexcept {
std::swap(ptr_, that.ptr_);
return *this;
}
void reset() { // NOLINT
DecRef(ptr_);
ptr_ = nullptr;
}
void reset(element_type *that) { IntrusivePtr{that}.swap(*this); } // NOLINT
element_type &operator*() const noexcept { return *ptr_; }
element_type *operator->() const noexcept { return ptr_; }
element_type *get() const noexcept { return ptr_; } // NOLINT
explicit operator bool() const noexcept { return static_cast<bool>(ptr_); }
int32_t use_count() noexcept { // NOLINT
return ptr_ ? IntrusivePtrRefCount(ptr_).Count() : 0;
}
/*
* \brief Helper function for swapping 2 pointers.
*/
void swap(IntrusivePtr<T> &that) noexcept { // NOLINT
std::swap(ptr_, that.ptr_);
}
};
template <class T, class U>
bool operator==(IntrusivePtr<T> const &x, IntrusivePtr<U> const &y) noexcept {
return x.get() == y.get();
}
template <class T, class U>
bool operator!=(IntrusivePtr<T> const &x, IntrusivePtr<U> const &y) noexcept {
return x.get() != y.get();
}
template <class T, class U>
bool operator==(IntrusivePtr<T> const &x, U *y) noexcept {
return x.get() == y;
}
template <class T, class U>
bool operator!=(IntrusivePtr<T> const &x, U *y) noexcept {
return x.get() != y;
}
template <class T, class U>
bool operator==(T *x, IntrusivePtr<U> const &y) noexcept {
return y == x;
}
template <class T, class U>
bool operator!=(T *x, IntrusivePtr<U> const &y) noexcept {
return y != x;
}
template <class T>
bool operator<(IntrusivePtr<T> const &x, IntrusivePtr<T> const &y) noexcept {
return std::less<T*>{}(x.get(), y.get());
}
template <class T>
bool operator<=(IntrusivePtr<T> const &x, IntrusivePtr<T> const &y) noexcept {
return std::less_equal<T*>{}(x.get(), y.get());
}
template <class T>
bool operator>(IntrusivePtr<T> const &x, IntrusivePtr<T> const &y) noexcept {
return !(x <= y);
}
template <class T>
bool operator>=(IntrusivePtr<T> const &x, IntrusivePtr<T> const &y) noexcept {
return !(x < y);
}
template <class E, class T, class Y>
std::basic_ostream<E, T> &operator<<(std::basic_ostream<E, T> &os,
IntrusivePtr<Y> const &p) {
os << p.get();
return os;
}
} // namespace xgboost
namespace std {
template <class T>
void swap(xgboost::IntrusivePtr<T> &x, // NOLINT
xgboost::IntrusivePtr<T> &y) noexcept {
x.swap(y);
}
template <typename T>
struct hash<xgboost::IntrusivePtr<T>> : public xgboost::IntrusivePtr<T>::Hash {};
} // namespace std
#endif // XGBOOST_INTRUSIVE_PTR_H_