-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgc_ptr.hpp
309 lines (260 loc) · 8.95 KB
/
gc_ptr.hpp
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/**
* @file gc_ptr.hpp
* @author Denis Kotov
* @date 31 Jul 2019
* @brief Contains gc_ptr class.
* It is thread safe Deterministic Garbage Pointer Collector
* @copyright Denis Kotov, MIT License. Open source: https://github.com/redradist/DeterministicGarbagePointer
*/
#ifndef DETERMINISTIC_GARBAGE_COLLECTOR_POINTER_HPP
#define DETERMINISTIC_GARBAGE_COLLECTOR_POINTER_HPP
namespace memory {
namespace sync {
#define GC_TRACE __attribute__((annotate("gc::Trace")))
template <typename T>
class has_use_gc_ptr;
}
template <typename TBase, typename TDerived>
void call_ConnectBaseToRoot(TDerived * derivedPtr, const void * rootPtr);
template <typename TExact, typename TDeduced>
void call_ConnectFieldToRoot(TDeduced & t, const void * rootPtr);
template <typename TBase, typename TDerived>
void call_DisconnectBaseFromRoot(TDerived * derivedPtr, const bool isRoot, const void * rootPtr);
template <typename TExact, typename TDeduced>
void call_DisconnectFieldFromRoot(TDeduced & t, const bool isRoot, const void * rootPtr);
}
#include <atomic>
#include <unordered_set>
#include <unordered_map>
#include <mutex>
#include <type_traits>
namespace memory {
namespace sync {
class SpinLock {
public:
SpinLock(std::atomic_flag & lockObject)
: lock_object_{lockObject} {
while (lock_object_.test_and_set(std::memory_order_acquire));
}
~SpinLock() {
lock_object_.clear(std::memory_order_release);
}
private:
std::atomic_flag & lock_object_;
};
}
template <typename T>
class has_use_gc_ptr
{
typedef char one;
struct two { char x[2]; };
template <typename C> static one testConnectToRoot( typeof(&C::connectToRoot) ) ;
template <typename C> static two testConnectToRoot(...);
template <typename C> static one testDisconnectFromRoot( typeof(&C::disconnectFromRoot) ) ;
template <typename C> static two testDisconnectFromRoot(...);
public:
enum { value = sizeof(testConnectToRoot<T>(0)) == sizeof(char) &&
sizeof(testDisconnectFromRoot<T>(0)) == sizeof(char)};
};
struct gc_object_control_block {
const bool is_aligned_memory_ = false;
std::atomic_flag lock_object_ = ATOMIC_FLAG_INIT;
std::unordered_map<const void *, uint32_t> root_ptrs_;
};
template <typename TObject>
struct gc_object_aligned_storage {
TObject object_;
gc_object_control_block control_block_;
};
template <typename TBase, typename TDerived>
inline void call_ConnectBaseToRoot(TDerived * derivedPtr, const void * rootPtr) {
if constexpr (memory::has_use_gc_ptr<TBase>::value) {
derivedPtr->TBase::connectToRoot(rootPtr);
}
}
template <typename TExact, typename TDeduced>
inline void call_ConnectFieldToRoot(TDeduced & t, const void * rootPtr) {
if constexpr (!std::is_pointer<TExact>::value &&
!std::is_reference<TExact>::value &&
memory::has_use_gc_ptr<TExact>::value) {
t.connectToRoot(rootPtr);
}
}
template <typename TBase, typename TDerived>
inline void call_DisconnectBaseFromRoot(TDerived * derivedPtr, const bool isRoot, const void * rootPtr) {
if constexpr (memory::has_use_gc_ptr<TBase>::value) {
derivedPtr->TBase::disconnectFromRoot(isRoot, rootPtr);
}
}
template <typename TExact, typename TDeduced>
inline void call_DisconnectFieldFromRoot(TDeduced & t, const bool isRoot, const void * rootPtr) {
if constexpr (!std::is_pointer<TExact>::value &&
!std::is_reference<TExact>::value &&
memory::has_use_gc_ptr<TExact>::value) {
t.disconnectFromRoot(isRoot, rootPtr);
}
}
template <typename TObject>
class gc_ptr {
static_assert(!std::is_pointer<TObject>::value, "TObject should not be pointer type !!");
static_assert(!std::is_reference<TObject>::value, "TObject should not be reference type !!");
public:
gc_ptr()
: root_ptrs_{this} {
static_assert(has_use_gc_ptr<TObject>::value, "TObject should not be marked with GC_TRACE annotation !!");
}
explicit gc_ptr(TObject * objectPtr)
: root_ptrs_{this} {
static_assert(has_use_gc_ptr<TObject>::value, "TObject should not be marked with GC_TRACE annotation !!");
object_ptr_ = objectPtr;
object_control_block_ptr_ = new gc_object_control_block{};
for (auto & rootRefPtr : this->root_ptrs_) {
addRootPtrToObject(rootRefPtr);
}
}
gc_ptr(const gc_ptr & gcPtr)
: root_ptrs_{this} {
static_assert(has_use_gc_ptr<TObject>::value, "TObject should not be marked with GC_TRACE annotation !!");
this->operator=(gcPtr);
}
~gc_ptr() {
removeAllRoots();
}
template <typename T, typename ... TArgs>
friend gc_ptr<T> make_gc(TArgs && ... args);
explicit operator bool() const noexcept {
return object_ptr_ != nullptr;
}
TObject & operator *() const {
return *object_ptr_;
}
TObject * operator->() const noexcept {
return object_ptr_;
}
gc_ptr & operator=(TObject * const objectPtr) {
removeAllRoots();
object_ptr_ = objectPtr;
if (object_ptr_ != nullptr) {
object_control_block_ptr_ = new gc_object_control_block{};
} else {
object_control_block_ptr_ = nullptr;
}
addAllRoots();
return *this;
}
gc_ptr & operator=(const gc_ptr & objectPtr) {
removeAllRoots();
object_ptr_ = objectPtr.object_ptr_;
object_control_block_ptr_ = objectPtr.object_control_block_ptr_;
addAllRoots();
return *this;
}
void connectToRoot(const void * rootPtr) const {
root_ptrs_.insert(rootPtr);
addRootPtrToObject(rootPtr);
if (is_root_) {
is_root_ = false;
disconnectFromRoot(true, this);
}
}
void disconnectFromRoot(const bool isRoot, const void * rootPtr) const {
root_ptrs_.erase(rootPtr);
removeRootPtrFromObject(isRoot, rootPtr);
}
protected:
void addAllRoots() {
if (object_control_block_ptr_ != nullptr) {
auto rootPtrs = root_ptrs_;
for (auto &rootRefPtr : rootPtrs) {
addRootPtrToObject(rootRefPtr);
}
}
}
void removeAllRoots() {
if (object_control_block_ptr_ != nullptr) {
auto rootPtrs = root_ptrs_;
for (auto &rootRefPtr : rootPtrs) {
root_ptrs_.erase(rootRefPtr);
removeRootPtrFromObject(is_root_, rootRefPtr);
}
}
}
void addRootPtrToObject(const void *rootPtr) const {
if (object_control_block_ptr_ != nullptr) {
bool isNewRoot;
{
sync::SpinLock lock{object_control_block_ptr_->lock_object_};
isNewRoot = object_control_block_ptr_->root_ptrs_.count(rootPtr) == 0;
if (isNewRoot) {
object_control_block_ptr_->root_ptrs_[rootPtr] = 1;
} else {
object_control_block_ptr_->root_ptrs_[rootPtr] += 1;
}
}
if (isNewRoot) {
if constexpr (has_use_gc_ptr<TObject>::value) {
object_ptr_->connectToRoot(rootPtr);
}
}
}
}
void removeRootPtrFromObject(const bool isRoot, const void *rootPtr) const {
if (object_control_block_ptr_ != nullptr) {
bool isRemovedRoot = false;
bool isNoRoots;
{
sync::SpinLock lock{object_control_block_ptr_->lock_object_};
if (isRoot) {
if (object_control_block_ptr_->root_ptrs_.count(rootPtr) > 0) {
object_control_block_ptr_->root_ptrs_.erase(rootPtr);
isRemovedRoot = true;
}
} else if (object_control_block_ptr_->root_ptrs_.count(rootPtr) > 0) {
object_control_block_ptr_->root_ptrs_[rootPtr] -= 1;
if (object_control_block_ptr_->root_ptrs_[rootPtr] == 0) {
object_control_block_ptr_->root_ptrs_.erase(rootPtr);
isRemovedRoot = true;
}
}
isNoRoots = object_control_block_ptr_->root_ptrs_.empty();
}
if (isRemovedRoot) {
if constexpr (has_use_gc_ptr<TObject>::value) {
object_ptr_->disconnectFromRoot(isRoot, rootPtr);
}
}
if (isRemovedRoot && isNoRoots) {
if (object_control_block_ptr_->is_aligned_memory_) {
auto gcObjectAlignedStoragePtr = reinterpret_cast<gc_object_aligned_storage<TObject>*>(object_ptr_);
delete gcObjectAlignedStoragePtr;
} else {
delete object_ptr_;
delete object_control_block_ptr_;
}
object_ptr_ = nullptr;
object_control_block_ptr_ = nullptr;
} else if (root_ptrs_.empty()) {
object_ptr_ = nullptr;
object_control_block_ptr_ = nullptr;
}
}
}
mutable bool is_root_ = true;
mutable std::unordered_set<const void *> root_ptrs_;
mutable TObject * object_ptr_ = nullptr;
mutable gc_object_control_block * object_control_block_ptr_ = nullptr;
};
template <typename TObject, typename ... TArgs>
gc_ptr<TObject> make_gc(TArgs && ... args) {
gc_ptr<TObject> ptr{};
ptr.removeAllRoots();
auto gcObjectAlignedStoragePtr = new gc_object_aligned_storage<TObject>{
{std::forward<TObject>(args)...},
{true, ATOMIC_FLAG_INIT, {}}
};
ptr.object_ptr_ = &gcObjectAlignedStoragePtr->object_;
ptr.object_control_block_ptr_ = &gcObjectAlignedStoragePtr->control_block_;
ptr.addAllRoots();
}
}
#endif //DETERMINISTIC_GARBAGE_COLLECTOR_POINTER_HPP