1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package jTrolog.engine;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 class GarbageCan {
43
44 private int depth = 8;
45 private int width = Engine.STARTUP_STACK_SIZE;
46
47 private int[][] trashCanVarNr = new int[depth][width];
48 private int[][] trashCanVarCtx = new int[depth][width];
49
50 private int[] depthCounter = new int[width];
51
52 private LinkTable links;
53
54 GarbageCan(LinkTable links) {
55 this.links = links;
56 for (int i = 0; i < depth; i++) {
57 trashCanVarNr[i] = new int[width];
58 trashCanVarCtx[i] = new int[width];
59 }
60 }
61
62 void addToTrashCan(int vNr, int vCtx, int execCtx) {
63
64 int currentDepth = depthCounter[execCtx];
65 depthCounter[execCtx] = currentDepth + 1;
66
67
68 if (currentDepth == depth)
69 doubleTrashCanDepth(depth * 2);
70
71
72 trashCanVarNr[currentDepth][execCtx] = vNr;
73 trashCanVarCtx[currentDepth][execCtx] = vCtx;
74 }
75
76 private void doubleTrashCanDepth(final int newSize) {
77 trashCanVarNr = LinkTable.expandMap(trashCanVarNr, newSize);
78 trashCanVarCtx = LinkTable.expandMap(trashCanVarCtx, newSize);
79 for (int i = depth; i < newSize; i++) {
80 trashCanVarNr[i] = new int[width];
81 trashCanVarCtx[i] = new int[width];
82 }
83 depth = newSize;
84 }
85
86 void doubleTrashCanWidth(final int newSize) {
87 for (int i = 0; i < depth; i++) {
88 trashCanVarNr[i] = LinkTable.expandArray(trashCanVarNr[i], newSize);
89 trashCanVarCtx[i] = LinkTable.expandArray(trashCanVarCtx[i], newSize);
90 }
91 depthCounter = LinkTable.expandArray(depthCounter, newSize);
92 width *= 2;
93 }
94
95 void collectGarbageLinks(int owner) {
96 int currentDepth = depthCounter[owner];
97 depthCounter[owner] = 0;
98
99 for (int i = 0; i < currentDepth; i++) {
100 int trashThisLink = trashCanVarNr[i][owner];
101 int trashThisCtx = trashCanVarCtx[i][owner];
102 links.reset(trashThisLink, trashThisCtx);
103 }
104 }
105
106 public int[][] getGarbageLinks(int owner) {
107 int[][] ints = new int[depth][2];
108 for (int i = 0; i < depthCounter[owner]; i++) {
109 int varNr = trashCanVarNr[i][owner];
110 int varCtx = trashCanVarCtx[i][owner];
111 ints[i] = new int[] { varNr, varCtx };
112 }
113 return ints;
114 }
115 }