This repository was archived by the owner on Apr 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkgc.h
72 lines (55 loc) · 1.49 KB
/
kgc.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
#ifndef KAT_GC_H_INCLUDED
#define KAT_GC_H_INCLUDED
#include <vector>
#include "kvalue.h"
#define INITIAL_GC_THRESHOLD 256
class GcGuard;
class Kgc
{
public:
explicit Kgc(unsigned int maxObjects = INITIAL_GC_THRESHOLD)
: numObjects_(0), maxObjects_(maxObjects) {}
~Kgc();
void pushStackRoot(const Value *v) { stackRoots_.push_back(v); }
void pushLocalStackRoot(const Value **v) { localStackRoots_.push_back(v); }
void popLocalStackRoot() { localStackRoots_.pop_back(); }
void collect();
Value* allocValue(ValueType type);
private:
void mark(const Value *v);
void sweep();
void markAll();
void dealloc(const Value *v);
Value* allocSpecial(ValueType type);
Value* allocNew(ValueType type);
unsigned int numObjects_;
unsigned int maxObjects_;
const Value* firstObject_ = nullptr;
unsigned int totalObjects_[(int)ValueType::MAX] = {0};
std::vector<Value *> reserved[(int)ValueType::MAX];
std::vector<const Value *> stackRoots_;
std::vector<const Value **> localStackRoots_;
friend class GcGuard;
};
class GcGuard
{
public:
explicit GcGuard(Kgc &gc) : gc_(gc) {}
~GcGuard()
{
while (times_)
{
gc_.popLocalStackRoot();
--times_;
}
}
void pushLocalStackRoot(const Value **local)
{
gc_.pushLocalStackRoot(local);
++times_;
}
private:
Kgc &gc_;
long times_ = 0;
};
#endif /* KAT_GC_H_INCLUDED */