-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtgc.cpp
366 lines (314 loc) · 9.31 KB
/
tgc.cpp
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "tgc.h"
#ifdef _WIN32
#include <crtdbg.h>
#endif
namespace tgc {
namespace details {
#ifndef TGC_MULTI_THREADED
shared_mutex ClassMeta::mutex;
#endif
atomic<int> ClassMeta::isCreatingObj = 0;
ClassMeta ClassMeta::dummy;
char* ObjMeta::dummyObjPtr = nullptr;
Collector* Collector::inst = nullptr;
static const char* StateStr[(int)Collector::State::MaxCnt] = {
"RootMarking", "LeafMarking", "Sweeping"};
//////////////////////////////////////////////////////////////////////////
char* ObjMeta::objPtr() const {
return klass == &ClassMeta::dummy ? dummyObjPtr
: (char*)this + sizeof(ObjMeta);
}
void ObjMeta::destroy() {
if (!arrayLength)
return;
klass->memHandler(klass, ClassMeta::MemRequest::Dctor, this);
arrayLength = 0;
}
void ObjMeta::operator delete(void* p) {
auto* m = (ObjMeta*)p;
m->klass->memHandler(m->klass, ClassMeta::MemRequest::Dealloc, m);
}
bool ObjMeta::operator<(ObjMeta& r) const {
return objPtr() + klass->size * arrayLength <
r.objPtr() + r.klass->size * r.arrayLength;
}
bool ObjMeta::containsPtr(char* p) {
auto* o = objPtr();
return o <= p && p < o + klass->size * arrayLength;
}
//////////////////////////////////////////////////////////////////////////
const PtrBase* ObjPtrEnumerator::getNext() {
if (auto* subPtrs = meta->klass->subPtrOffsets) {
if (arrayElemIdx < meta->arrayLength && subPtrIdx < subPtrs->size()) {
auto* klass = meta->klass;
auto* obj = meta->objPtr() + arrayElemIdx * klass->size;
auto* subPtr = obj + (*klass->subPtrOffsets)[subPtrIdx];
if (subPtrIdx++ >= klass->subPtrOffsets->size())
arrayElemIdx++;
return (PtrBase*)subPtr;
}
}
return nullptr;
}
//////////////////////////////////////////////////////////////////////////
PtrBase::PtrBase() : isRoot(1) {
auto* c = Collector::inst ? Collector::inst : Collector::get();
c->registerPtr(this);
}
PtrBase::PtrBase(void* obj) : isRoot(1) {
auto* c = Collector::inst ? Collector::inst : Collector::get();
meta = c->globalFindOwnerMeta(obj);
c->registerPtr(this);
}
PtrBase::~PtrBase() {
Collector::inst->unregisterPtr(this);
}
void PtrBase::onPtrChanged() {
Collector::inst->onPointerChanged(this);
}
//////////////////////////////////////////////////////////////////////////
ObjMeta* ClassMeta::newMeta(size_t objCnt) {
assert(memHandler && "should not be called in global scope (before main)");
auto* meta = (ObjMeta*)memHandler(this, MemRequest::Alloc,
reinterpret_cast<void*>(objCnt));
try {
auto* c = Collector::inst ? Collector::inst : Collector::get();
// Allow using gc_from(this) in the constructor of the creating object.
c->addMeta(meta);
} catch (std::bad_alloc&) {
memHandler(this, MemRequest::Dealloc, meta);
throw;
}
isCreatingObj++;
return meta;
}
void ClassMeta::endNewMeta(ObjMeta* meta, bool failed) {
isCreatingObj--;
if (!failed) {
unique_lock lk{mutex};
state = ClassMeta::State::Registered;
}
{
auto* c = Collector::inst;
unique_lock lk{c->mutex, try_to_lock};
c->creatingObjs.remove(meta);
if (failed) {
c->metaSet.erase(meta);
memHandler(this, MemRequest::Dealloc, meta);
}
}
}
void ClassMeta::registerSubPtr(ObjMeta* owner, PtrBase* p) {
auto offset = (OffsetType)((char*)p - owner->objPtr());
{
shared_lock lk{mutex};
if (state == ClassMeta::State::Registered)
return;
// constructor recursed.
if (subPtrOffsets && offset <= subPtrOffsets->back())
return;
}
unique_lock lk{mutex};
if (!subPtrOffsets)
subPtrOffsets = new vector<OffsetType>();
subPtrOffsets->push_back(offset);
}
//////////////////////////////////////////////////////////////////////////
Collector::Collector() {
pointers.reserve(1024 * 5);
grayObjs.reserve(1024 * 2);
metaSet.reserve(1024 * 5);
}
Collector::~Collector() {
for (auto i = metaSet.begin(); i != metaSet.end();) {
delete *i;
i = metaSet.erase(i);
}
}
Collector* Collector::get() {
if (!inst) {
#ifdef _WIN32
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
inst = new Collector();
atexit([] { delete inst; });
}
return inst;
}
void Collector::addMeta(ObjMeta* meta) {
unique_lock lk{mutex, try_to_lock};
metaSet.insert(meta);
creatingObjs.push_back(meta);
}
void Collector::registerPtr(PtrBase* p) {
p->index = pointers.size();
{
unique_lock lk{mutex, try_to_lock};
pointers.push_back(p);
}
if (ClassMeta::isCreatingObj > 0) {
if (auto* owner = findCreatingObj(p)) {
p->isRoot = 0;
owner->klass->registerSubPtr(owner, p);
}
}
}
void Collector::unregisterPtr(PtrBase* p) {
PtrBase* pointer;
{
unique_lock lk{mutex, try_to_lock};
if (p == pointers.back()) {
pointers.pop_back();
return;
} else {
swap(pointers[p->index], pointers.back());
pointer = pointers[p->index];
pointers.pop_back();
pointer->index = p->index;
}
}
if (!pointer->meta)
return;
shared_lock lk{mutex, try_to_lock};
if (state == State::RootMarking) {
if (p->index < nextRootMarking) {
tryMarkRoot(pointer);
}
}
}
void Collector::tryMarkRoot(PtrBase* p) {
if (p->isRoot == 1) {
if (p->meta->color == ObjMeta::Color::White) {
p->meta->color = ObjMeta::Color::Gray;
unique_lock lk{mutex, try_to_lock};
grayObjs.push_back(p->meta);
}
}
}
void Collector::onPointerChanged(PtrBase* p) {
if (!p->meta)
return;
shared_lock lk{mutex, try_to_lock};
switch (state) {
case State::RootMarking:
if (p->index < nextRootMarking)
tryMarkRoot(p);
break;
case State::LeafMarking:
tryMarkRoot(p);
break;
case State::Sweeping:
if (p->meta->color == ObjMeta::Color::White) {
// if (*p->meta < **nextSweeping) {
// already passed sweeping stage.
//} else {
// delay to the next collection.
p->meta->color = ObjMeta::Color::Black;
//}
}
break;
}
}
ObjMeta* Collector::findCreatingObj(PtrBase* p) {
shared_lock lk{mutex, try_to_lock};
// owner may not be the current one(e.g. constructor recursed)
for (auto i = creatingObjs.rbegin(); i != creatingObjs.rend(); ++i) {
if ((*i)->containsPtr((char*)p))
return *i;
}
return nullptr;
}
ObjMeta* Collector::globalFindOwnerMeta(void* obj) {
shared_lock lk{mutex, try_to_lock};
auto* meta = (ObjMeta*)((char*)obj - sizeof(ObjMeta));
return meta;
}
void Collector::collect(int stepCnt) {
unique_lock lk{mutex};
freeObjCntOfPrevGc = 0;
switch (state) {
_RootMarking:
case State::RootMarking:
for (; nextRootMarking < pointers.size() && stepCnt-- > 0;
nextRootMarking++) {
auto p = pointers[nextRootMarking];
auto meta = p->meta;
if (!meta)
continue;
// for containers
auto it = meta->klass->enumPtrs(meta);
for (; auto* ptr = it->getNext(); stepCnt--) {
ptr->isRoot = 0;
}
delete it;
tryMarkRoot(p);
}
if (nextRootMarking >= pointers.size()) {
state = State::LeafMarking;
nextRootMarking = 0;
goto _ChildMarking;
}
break;
_ChildMarking:
case State::LeafMarking:
while (grayObjs.size() && stepCnt-- > 0) {
ObjMeta* o = grayObjs.back();
grayObjs.pop_back();
o->color = ObjMeta::Color::Black;
auto cls = o->klass;
auto it = cls->enumPtrs(o);
for (; auto* ptr = it->getNext(); stepCnt--) {
auto* meta = ptr->meta;
if (!meta)
continue;
if (meta->color == ObjMeta::Color::White) {
meta->color = ObjMeta::Color::Gray;
grayObjs.push_back(meta);
}
}
delete it;
}
if (!grayObjs.size()) {
state = State::Sweeping;
nextSweeping = metaSet.begin();
goto _Sweeping;
}
break;
_Sweeping:
case State::Sweeping:
for (; nextSweeping != metaSet.end() && stepCnt-- > 0;) {
ObjMeta* meta = *nextSweeping;
if (meta->color == ObjMeta::Color::White) {
nextSweeping = metaSet.erase(nextSweeping);
delete meta;
freeObjCntOfPrevGc++;
continue;
}
meta->color = ObjMeta::Color::White;
++nextSweeping;
}
if (nextSweeping == metaSet.end()) {
state = State::RootMarking;
if (metaSet.size())
goto _RootMarking;
}
break;
}
}
void Collector::dumpStats() {
shared_lock lk{mutex, try_to_lock};
printf("========= [gc] ========\n");
printf("[total pointers ] %3d\n", (unsigned)pointers.size());
printf("[total meta ] %3d\n", (unsigned)metaSet.size());
printf("[total gray meta] %3d\n", (unsigned)grayObjs.size());
auto liveCnt = 0;
for (auto i : metaSet)
if (i->arrayLength)
liveCnt++;
printf("[live objects ] %3d\n", liveCnt);
printf("[last freed objs] %3d\n", freeObjCntOfPrevGc);
printf("[collector state] %s\n", StateStr[(int)state]);
printf("=======================\n");
}
} // namespace details
} // namespace tgc