-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopy.java
77 lines (70 loc) · 2.03 KB
/
Copy.java
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
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
public class Copy extends GarbageCollector {
private int nextMemoryAddress = 0;
@Override
protected void mark() {
Node dummy = new Node(true, -1, -1);
List<Node> temp = new LinkedList<>();
for (Integer root : roots)
temp.add(objects.get(root));
dummy.setChildren(temp);
bfs(dummy);
}
private void bfs(Node node) {
Queue<Node> queue = new LinkedList<>();
queue.add(node);
while (queue.size() > 0) {
node = queue.poll();
for (Node child : node.getChildren()) {
if (!child.getVisited()) {
child.setVisited(true);
copy(child);
queue.add(child);
}
}
}
}
private void copy(Node node) {
int size = node.getEnd() - node.getStart();
node.setStart(nextMemoryAddress);
node.setEnd(node.getStart() + size);
nextMemoryAddress = node.getEnd() + 1;
}
@Override
protected void writeNewHeap(String path) {
File file = new File(path);
PriorityQueue<Map.Entry<Integer, Node>> newHeap = new PriorityQueue<>((i, j) -> i.getValue().getStart() - j.getValue().getStart());
for (Map.Entry<Integer, Node> object : objects.entrySet()) {
if (object.getValue().getVisited())
newHeap.add(object);
}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write("object-identifier,memory-start,memory-end\n");
while (newHeap.size() > 0) {
Entry<Integer, Node> object = newHeap.poll();
writer.write(object.getKey() + "," + object.getValue().getStart() + "," + object.getValue().getEnd() + "\r\n");
}
writer.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
GarbageCollector copy = new Copy();
copy.readHeap(args[0]);
copy.readRoots(args[1]);
copy.readPointers(args[2]);
copy.mark();
copy.writeNewHeap(args[3]);
}
}