-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgarbagecollector.cpp
197 lines (176 loc) · 5.84 KB
/
garbagecollector.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
/**
* Copyright 2010 by Benjamin J. Land (a.k.a. BenLand100)
*
* This file is part of SJVM the Simple Java Virtual Machine.
*
* SJVM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SJVM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SJVM. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ClassFile.h"
#include <set>
#include <iostream>
#include <cstring>
#include <vector>
#include "ClassFile.h"
#include "GarbageCollector.h"
#include "Object.h"
#include "Thread.h"
#include "Arrays.h"
#define WARNING_OUTPUT
//#define DEBUG_OUTPUT
#ifdef DEBUG_OUTPUT
#define debug(v) std::cout << v;
#else
#define debug(v)
#endif
#ifdef WARNING_OUTPUT
#define warn(v) std::cout << "WARNING: " << v;
#else
#define warn(v)
#endif
LocalGC::LocalGC(GlobalGC *gc, Thread *thread) {
parent = gc;
gc->mutex.lock();
oldLocal = thread->nativeLocal;
thread->nativeLocal = this;
this->thread = thread;
gc->locals.insert(this);
gc->mutex.unlock();
debug("Local GC Init\n");
}
LocalGC::~LocalGC() {
parent->mutex.lock();
thread->nativeLocal = oldLocal;
parent->locals.erase(this);
for (std::set<Object*>::iterator iter = roots.begin(); iter != roots.end(); iter++) {
Object *obj = *iter;
debug("GC: " << obj->refCount << ' ' << obj->staticCount << ' ' << obj->localCount << ' ' << (int)obj << '\n');
obj->localCount--;
if (obj->refCount || obj->staticCount || obj->localCount)
continue;
parent->clean(obj);
}
roots.clear();
parent->mutex.unlock();
debug("Local GC Out of Focus\n");
}
void LocalGC::addLocal(Object* obj) {
if (!obj) return;
debug("Local Added: " << (int)obj << '\n');
debug("GC: " << obj->refCount << ' ' << obj->staticCount << ' ' << obj->localCount << ' ' << (int)obj << '\n');
parent->mutex.lock();
if (roots.find(obj) == roots.end()) {
roots.insert(obj);
obj->localCount++;
}
parent->mutex.unlock();
}
void LocalGC::removeLocal(Object* obj) {
if (!obj) return;
debug("Local Removed: " << (int)obj << '\n');
debug("GC: " << obj->refCount << ' ' << obj->staticCount << ' ' << obj->localCount << ' ' << (int)obj << '\n');
parent->mutex.lock();
if (roots.find(obj) != roots.end()) {
debug("haha\n");
roots.erase(obj);
obj->localCount--;
}
parent->mutex.unlock();
}
GlobalGC::GlobalGC() {
}
GlobalGC::~GlobalGC() {
sweep();
mutex.lock();
for (std::set<LocalGC*>::iterator iter = locals.begin(); iter != locals.end(); iter = locals.begin()) {
delete (*iter); //dangerous to force, but thread safe
}
for (std::set<Object*>::iterator iter = objects.begin(); iter != objects.end(); iter++) {
delete *iter;
}
mutex.unlock();
}
void GlobalGC::sweep() {
}
void GlobalGC::clean(Object* object) {
if (!object) return;
debug("Cleaning " << (int) object << ' ');
if (object->type) {
if (object->type->arrayType && object->type->componentType) {
JOBJECTARRAY array = (JOBJECTARRAY)object;
for (int i = 0; i < array->size; i++) {
decrRef(array->array[i]);
}
}
debug(object->type->name << '\n');
std::vector<unsigned int> indexes = object->type->objectIndexes;
Variable *fields = new Variable[object->type->instanceFields];
memcpy(fields,object->fields,object->type->instanceFields*sizeof(Variable));
for (int i = 0; i < indexes.size(); i++) {
debug("decrref1\n");
decrRef(fields[indexes[i]].l);
debug("decrref2\n");
}
} else {
debug("Untyped\n");
}
debug("cleaned\n");
if (objects.find(object) != objects.end()) {
objects.erase(object);
debug("deleting\n");
delete object;
debug("deleted\n");
}
}
void GlobalGC::incrPersist(Object* obj) {
if (!obj) return;
debug("Incr Persist: " << (int)obj << '\n');
debug("GC: " << obj->refCount << ' ' << obj->staticCount << ' ' << obj->localCount << ' ' << (int)obj << '\n');
mutex.lock();
obj->staticCount++;
mutex.unlock();
}
void GlobalGC::decrPersist(Object* obj) {
if (!obj) return;
mutex.lock();
debug("Decr Persist: " << (int)obj << '\n');
debug("GC: " << obj->refCount << ' ' << obj->staticCount << ' ' << obj->localCount << ' ' << (int)obj << '\n');
mutex.lock();
obj->staticCount--;
if (!(obj->refCount || obj->staticCount || obj->localCount)) {
clean(obj);
}
mutex.unlock();
}
void GlobalGC::incrRef(Object* obj) {
if (!obj) return;
debug("Incr Ref: " << (int)obj << '\n');
debug("GC: " << obj->refCount << ' ' << obj->staticCount << ' ' << obj->localCount << ' ' << (int)obj << '\n');
mutex.lock();
obj->refCount++;
mutex.unlock();
}
void GlobalGC::decrRef(Object* obj) {
if (!obj) return;
debug("Decr Ref: " << (int)obj << '\n');
debug("GC: " << obj->refCount << ' ' << obj->staticCount << ' ' << obj->localCount << ' ' << (int)obj << '\n');
mutex.lock();
obj->refCount--;
if (!(obj->refCount || obj->staticCount || obj->localCount)) {
clean(obj);
}
mutex.unlock();
}
void GlobalGC::reg(Object *obj) {
objects.insert(obj);
}