-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTinyGC.cpp
180 lines (159 loc) · 5.16 KB
/
TinyGC.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
//
// Created by v4kst1z.
//
#include "TinyGC.h"
#include <cassert>
#include "Visitor.h"
TinyGC::TinyGC()
: visitor_(new Visitor()),
gc_phase_(GCPhase::kNone),
bytes_allocated_(0),
size_of_objects_(0),
gc_count_threshold_(120),
gc_bytes_threshold_(0x1000) {}
void TinyGC::Mark() {
std::unique_lock<std::mutex> lck(ts_mxt_);
LOG("\nMark Start~");
SetGCPhase(GCPhase::kMarking);
visitor_->SetMark(true);
for (auto &ts_ : thread_to_stack_) {
ThreadState *ts = ts_.second;
ts->GetCurrentStackPosition();
intptr_t **start = reinterpret_cast<intptr_t **>(ts->GetStackEndAddr());
intptr_t **end = reinterpret_cast<intptr_t **>(ts->GetStackStartAddr());
for (; start < end; start++) {
if (*start > (intptr_t *)0x100000) {
GarbageCollectedBase *ptr =
reinterpret_cast<GarbageCollectedBase *>(*start);
if (objs_addr_.find(ptr) != objs_addr_.end()) {
visitor_->ObjTrace((GarbageCollectedBase *)ptr);
LOG("find object at " << std::hex << ptr);
}
}
}
}
}
void TinyGC::Sweep() {
std::unique_lock<std::mutex> lck(ts_mxt_);
LOG("\nSweep Start~");
SetGCPhase(GCPhase::kSweeping);
visitor_->SetMark(false);
std::unordered_set<GarbageCollectedBase *> roots = objs_addr_;
for (auto &root : roots) {
if (!root->mark_) {
objs_addr_.erase(root);
bytes_allocated_ -= root->obj_size_;
size_of_objects_--;
LOG("Object " << root << " is deleted!");
delete root;
} else {
visitor_->ObjTrace((GarbageCollectedBase *)root);
}
}
SetGCPhase(GCPhase::kNone);
}
TinyGC::~TinyGC() { delete this->visitor_; }
void TinyGC::MarkSweepGC() {
LOG("Mark Sweep GC Start~");
Mark();
Sweep();
}
void TinyGC::SetGCPhase(GCPhase gc_phase) {
switch (gc_phase) {
case GCPhase::kNone:
assert(gc_phase_ == GCPhase::kSweeping);
break;
case GCPhase::kMarking:
assert(gc_phase_ == GCPhase::kNone);
break;
case GCPhase::kSweeping:
assert(gc_phase_ == GCPhase::kMarking);
break;
}
gc_phase_ = gc_phase;
}
void TinyGC::AttachCurrentThread() {
std::unique_lock<std::mutex> lck(ts_mxt_);
auto thread_state = new ThreadState();
thread_to_stack_.insert(std::pair<std::thread::id, ThreadState *>(
thread_state->GetThreadId(), thread_state));
// LOG("thread id is " << thread_state->GetThreadId());
// LOG("stack start address " << thread_state->GetStackStartAddr());
// LOG("stack end address " << thread_state->GetStackEndAddr());
}
void TinyGC::AttachMainThread() {
std::unique_lock<std::mutex> lck(ts_mxt_);
auto thread_state = new ThreadState(true);
thread_to_stack_.insert(std::pair<std::thread::id, ThreadState *>(
thread_state->GetThreadId(), thread_state));
// LOG("main thread~");
// LOG("stack start address " << thread_state->GetStackStartAddr());
// LOG("stack end address " << thread_state->GetStackEndAddr());
}
void TinyGC::DetachMainThread() {
LOG("Detach Main Thread");
std::unique_lock<std::mutex> lck(ts_mxt_);
assert(thread_to_stack_.size() == 1);
ThreadState *ts = thread_to_stack_[std::this_thread::get_id()];
thread_to_stack_.erase(std::this_thread::get_id());
delete ts;
lck.unlock();
MarkSweepGC();
}
void TinyGC::DetachCurrentThread() {
std::unique_lock<std::mutex> lck(ts_mxt_);
ThreadState *ts = thread_to_stack_[std::this_thread::get_id()];
thread_to_stack_.erase(std::this_thread::get_id());
delete ts;
}
void TinyGC::CheckThreshold() {
if (size_of_objects_ >= gc_count_threshold_) {
LOG("Object amout > gc amount threshold~");
MarkSweepGC();
}
if (size_of_objects_ >= gc_count_threshold_) gc_count_threshold_ *= 2;
if (bytes_allocated_ >= gc_bytes_threshold_) {
LOG("bytes allocated > gc bytes threshold~");
MarkSweepGC();
}
if (bytes_allocated_ >= gc_bytes_threshold_) gc_bytes_threshold_ *= 2;
}
void TinyGC::ThreadState::GetCurrentStackPosition() {
stack_end_ = GetStackEnd();
}
void *TinyGC::ThreadState::GetStackStart(bool main) {
#if defined(__GLIBC__)
if (main)
return __libc_stack_end;
else {
pthread_attr_t attr;
int ret = pthread_attr_init(&attr);
assert(!ret);
int error = pthread_getattr_np(pthread_self(), &attr);
if (!error) {
void *base;
size_t size;
error = pthread_attr_getstack(&attr, &base, &size);
assert(!error);
pthread_attr_destroy(&attr);
return reinterpret_cast<uint8_t *>(base) + size;
} else {
perror("pthread_getattr_np");
exit(EXIT_FAILURE);
}
}
#elif defined(_M_X64) || defined(__x86_64__)
return reinterpret_cast<void *>(
reinterpret_cast<NT_TIB64 *>(NtCurrentTeb())->StackBase);
#elif defined(_M_IX86) || defined(__i386__)
return reinterpret_cast<void *>(
reinterpret_cast<NT_TIB *>(NtCurrentTeb())->StackBase);
#endif
}
void *TinyGC::ThreadState::GetStackEnd() {
#if defined(_MSC_VER)
return reinterpret_cast<void *>(_AddressOfReturnAddress());
#else
return reinterpret_cast<void *>(__builtin_frame_address(0));
#endif
}