-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgarbage_collector.cpp
More file actions
84 lines (80 loc) · 2.19 KB
/
garbage_collector.cpp
File metadata and controls
84 lines (80 loc) · 2.19 KB
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
#include "garbage_collector.h"
#include "error.h"
#include "functions.h"
#include "object.h"
#include <unordered_set>
void Dfs(Object* v, std::unordered_set<Object*>& used) {
used.insert(v);
if (!v) {
return;
}
if (Is<Cell>(v)) {
auto ptr = As<Cell>(v);
if (!used.count(ptr->GetFirst())) {
Dfs(ptr->GetFirst(), used);
}
if (!used.count(ptr->GetSecond())) {
Dfs(ptr->GetSecond(), used);
}
} else if (Is<Variable>(v)) {
auto ptr = As<Variable>(v);
if (!used.count(ptr->var_)) {
Dfs(ptr->var_, used);
}
} else if (Is<Variable>(v)) {
auto ptr = As<Variable>(v);
if (!used.count(ptr->var_)) {
Dfs(ptr->var_, used);
}
} else if (Is<LambdaGenerator>(v)) {
auto ptr = As<LambdaGenerator>(v);
if (!used.count(ptr->scope_)) {
Dfs(ptr->scope_, used);
}
} else if (Is<Lambda>(v)) {
auto ptr = As<Lambda>(v);
if (!used.count(ptr->par_scope_)) {
Dfs(ptr->par_scope_, used);
}
for (auto el : ptr->params_) {
if (!used.count(el)) {
Dfs(el, used);
}
}
for (auto el : ptr->actions_) {
if (!used.count(el)) {
Dfs(el, used);
}
}
} else if (Is<Scope>(v)) {
auto ptr = As<Scope>(v);
if (!used.count(ptr->par_scope_)) {
Dfs(ptr->par_scope_, used);
}
for (const auto& el : ptr->mp_) {
if (!used.count(el.second)) {
Dfs(el.second, used);
}
}
} else if (Is<Number>(v)) {
} else if (Is<Bool>(v)) {
} else if (Is<Symbol>(v)) {
}
}
void GarbageCollector::CleanUp() {
std::unordered_set<Object*> used;
Dfs(memory_.front().get(), used);
for (size_t i = 0; i < memory_.size(); ++i) {
while (memory_.size() > i && !used.count(memory_[i].get())) {
std::swap(memory_[i], memory_.back());
memory_.pop_back();
}
}
}
void GarbageCollector::ClearAll() {
memory_.clear();
}
GarbageCollector& GetGC() {
static GarbageCollector gc;
return gc;
}