-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.cpp
113 lines (92 loc) · 2.75 KB
/
heap.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
#include "heap.h"
#include <algorithm>
using namespace std;
set<vector<Var>*> Heap :: m_Heap;
void Heap :: Alloc ( Var & x, const ClassPool & classPool )
{
if ( x . m_Type != Type :: REFERENCE && x . m_Type != Type :: ARRAY )
return;
if ( x . m_Type == Type :: ARRAY )
{
x . m_Val . m_Array . m_Members = new vector<Var> ( x . m_Val . m_Array . m_Len );
for ( auto i = x . m_Val . m_Array . m_Members -> begin ();
i != x . m_Val . m_Array . m_Members -> end (); ++ i )
(*i) . m_Type = x . m_Val . m_Array . m_Type;
m_Heap . insert ( x . m_Val . m_Array . m_Members );
cout << "HEAP: Allocated new field." << endl;
//cout << x << endl;
}
else if ( x . m_Type == Type :: REFERENCE )
{
const vector<Var> * prototype = NULL;
try
{
prototype = & (classPool . getClass ( x . m_Val . m_Reference . m_Class ) . getMembers ());
/*
cout << "!!!!!!!!!!!" << endl;
for ( int i = 0; i < prototype -> size (); i ++ )
cout << (*prototype) [0] << " ";
cout << endl;
*/
}
catch ( out_of_range & )
{
throw runtime_error ( "Cant find class." );
}
x . m_Val . m_Reference . m_Members = new vector<Var> ( * prototype );
m_Heap . insert ( x . m_Val . m_Reference . m_Members );
}
}
void Heap :: Gc ( const Frame & frame )
{
set<vector<Var>*> reachable;
const Frame * current = & frame;
while ( current )
{
for ( int i = 0; i < current -> m_Vars . size (); i ++ )
{
if ( current -> m_Vars [i] . m_Type == Type :: ARRAY
&& current -> m_Vars [i] . m_Val . m_Array . m_Members )
{
reachable . insert ( current -> m_Vars [i] . m_Val . m_Array . m_Members );
}
else if ( current -> m_Vars [i] . m_Type == Type :: REFERENCE
&& current -> m_Vars [i] . m_Val . m_Reference . m_Members )
{
reachable . insert ( current -> m_Vars [i] . m_Val . m_Reference . m_Members );
}
}
for ( auto i : current -> m_Stack )
{
if ( i . m_Type == Type :: ARRAY
&& i . m_Val . m_Array . m_Members )
{
reachable . insert ( i . m_Val . m_Array . m_Members );
}
else if ( i . m_Type == Type :: REFERENCE
&& i . m_Val . m_Reference . m_Members )
{
reachable . insert ( i . m_Val . m_Reference . m_Members );
}
}
current = current -> m_Parent;
}
set<vector<Var> *> diff;
set_difference ( m_Heap . begin (), m_Heap . end (), reachable . begin (), reachable . end (), inserter ( diff, diff . end () ) );
cout << "GC:" << endl;
for ( auto i : diff )
{
cout << "(";
for ( auto j = i -> begin (); j != i -> end (); ++ j )
{
cout << *j;
auto k = j;
if ( ++ k != i -> end () )
cout << " ";
}
cout << ") ";
m_Heap . erase ( i );
delete i;
}
cout << endl << "**********" << endl;
}