-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGarbageCollector.C
147 lines (119 loc) · 3.54 KB
/
GarbageCollector.C
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
/****************************************************************
GarbageCollector.C
Copyright (C)2013 William H. Majoros ([email protected]).
This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
License (GPL) version 3, as described at www.opensource.org.
****************************************************************/
#include "GarbageCollector.H"
#include "Edge.H"
#include "Signal.H"
#include <iostream>
#undef USE_SMART_POINTERS
GarbageCollector::~GarbageCollector()
{
// dtor
#ifdef EXPLICIT_GRAPHS
BOOM::Set<SignalPtr>::iterator cur=reachableSignals.begin(),
end=reachableSignals.end();
for(; cur!=end ; ++cur) delete *cur;
#endif
}
void GarbageCollector::addSignal(SignalPtr s)
{
#ifdef EXPLICIT_GRAPHS
unreachableSignals.insert(s);
#endif
}
void GarbageCollector::markRight(SignalPtr origin)
{
#ifdef EXPLICIT_GRAPHS
SignalPtr s=origin;
if(!signalsReachableFromRight.isMember(origin)) return;
signalsReachableFromRight.erase(origin);
reachableSignals.insert(origin);
BOOM::Set<Edge*> &edgesOut=s->getEdgesOut();
BOOM::Set<Edge*>::iterator oCur=edgesOut.begin(), oEnd=edgesOut.end();
for(; oCur!=oEnd ; ++oCur)
{
Edge *edge=*oCur;
SignalPtr r=edge->getRight();
markRight(r);
}
#endif
}
void GarbageCollector::markLeft(SignalPtr origin)
{
#ifdef EXPLICIT_GRAPHS
SignalPtr s=origin;
if(!unreachableSignals.isMember(origin)) return;
unreachableSignals.erase(origin);
signalsReachableFromRight.insert(origin);
BOOM::Set<Edge*> &edgesIn=s->getEdgesIn();
BOOM::Set<Edge*>::iterator iCur=edgesIn.begin(), iEnd=edgesIn.end();
for(; iCur!=iEnd ; ++iCur) {
Edge *edge=*iCur;
SignalPtr l=edge->getLeft();
markLeft(l);
}
#endif
}
void GarbageCollector::sweep()
{
#ifdef EXPLICIT_GRAPHS
cerr<<"Collecting & deleting garbage..."<<endl;
int R=reachableSignals.size();
int U=unreachableSignals.size();
int L=signalsReachableFromRight.size();
int N=R+U+L;
float percentDeleted=int((U+L)/float(N)*100+5/9.0);
BOOM::Set<SignalPtr>::iterator cur=unreachableSignals.begin(),
end=unreachableSignals.end();
for(; cur!=end ; ++cur) delete *cur;
cur=signalsReachableFromRight.begin(),
end=signalsReachableFromRight.end();
for(; cur!=end ; ++cur) delete *cur;
unreachableSignals.clear();
signalsReachableFromRight.clear();
cerr<<percentDeleted<<"% of "<<N<<" vertices deleted, "<<R<<" remaining"<<endl;
#endif
}
void GarbageCollector::purge()
{
#ifdef EXPLICIT_GRAPHS
BOOM::Set<SignalPtr>::iterator cur=unreachableSignals.begin(),
end=unreachableSignals.end();
for(; cur!=end ; ++cur) delete *cur;
cur=reachableSignals.begin(), end=reachableSignals.end();
for(; cur!=end ; ++cur) delete *cur;
cur=signalsReachableFromRight.begin(), end=signalsReachableFromRight.end();
for(; cur!=end ; ++cur) delete *cur;
unreachableSignals.clear();
signalsReachableFromRight.clear();
reachableSignals.clear();
#endif
}
BOOM::Set<SignalPtr>::iterator GarbageCollector::signalsBegin()
{
#ifdef EXPLICIT_GRAPHS
return reachableSignals.begin();
#else
throw "EXPLICIT_GRAPHS is not defined in GarbageColletor::signalsBegin()";
#endif
}
BOOM::Set<SignalPtr>::iterator GarbageCollector::signalsEnd()
{
#ifdef EXPLICIT_GRAPHS
return reachableSignals.end();
#else
throw "EXPLICIT_GRAPHS is not defined in GarbageColletor::signalsEnd()";
#endif
}
void GarbageCollector::drop(SignalPtr s)
{
#ifdef EXPLICIT_GRAPHS
if(reachableSignals.isMember(s))
reachableSignals.remove(s);
else if(unreachableSignals.isMember(s))
unreachableSignals.remove(s);
#endif
}