-
Notifications
You must be signed in to change notification settings - Fork 0
/
garbageCollector.cpp
64 lines (49 loc) · 1.48 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
#include "garbageCollector.h"
namespace bunny
{
void GC::GCAdd(Object *_obj)
{
_obj->nextObject = head->nextObject; //Bisogna inserirlo dopo la head
head->nextObject = _obj;
}
void GC::GCMark(Object *_obj)
{
if(_obj->isMarked) //Già markato
return;
_obj->isMarked = true;
if(_obj->getType() == ARRAY_OBJ)
for(auto p: static_cast<Array *>(_obj)->elements)
GCMark(p); //Non sappiamo che tipo di oggetto, potrebbe essere anche un array quindi bisogna usare la funzione in maniera ricorsiva
}
void GC::GCMark(Environment *_env)
{
for(std::pair<std::string, Object *> p: _env->table) //Prendi la lista di variabili di uno scope
GCMark(p.second);
//Ci sono livelli (scope) superiori?
if(_env->outside != nullptr)
GCMark(_env->outside);
}
void GC::GCSweep()
{
Object *node = head;
while(node->nextObject != nullptr)
{
if(node->nextObject->isMarked)
{
Object *tmp = node->nextObject;
node->nextObject = tmp->nextObject;
delete tmp;
}
else
{
node = node->nextObject;
}
node = head;
while(node->nextObject != nullptr)
{
node->nextObject->isMarked = false;
node = node->nextObject;
}
}
}
}