blob: cab50e4d53392820d9e2d5bbce2ca276db882438 (
plain) (
blame)
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
|
package NET.worlds.scape;
import NET.worlds.console.Main;
import NET.worlds.console.MainCallback;
public class Recycler implements MainCallback {
private static int minCapacity = 50;
private Object[] list = new Object[minCapacity];
private int numFilled = 0;
private int numAllocked = 0;
private int maxAllockedRecently = 0;
private int howRecently = 0;
Recycler() {
Main.register(this);
}
private void resizeTo(int newCapacity) {
assert newCapacity > this.numAllocked;
if (newCapacity < minCapacity) {
newCapacity = minCapacity;
}
if (newCapacity < this.list.length && this.list.length < 2 * newCapacity) {
while (this.numFilled > newCapacity) {
this.list[--this.numFilled] = null;
}
} else {
Object[] newList = new Object[newCapacity];
try {
System.arraycopy(this.list, 0, newList, 0, this.numAllocked);
} catch (Exception var4) {
throw new Error(var4.toString());
}
this.list = newList;
this.numFilled = this.numAllocked;
}
}
@Override
public void mainCallback() {
if (this.numAllocked > this.maxAllockedRecently) {
this.maxAllockedRecently = this.numAllocked;
}
if (++this.howRecently == 1000) {
int newCapacity = this.maxAllockedRecently + minCapacity;
if (newCapacity < this.list.length) {
this.resizeTo(newCapacity);
}
this.howRecently = 0;
this.maxAllockedRecently = this.numAllocked;
}
this.numAllocked = 0;
}
public Object alloc() {
assert Main.isMainThread();
return this.numAllocked == this.numFilled ? null : this.list[this.numAllocked++];
}
public void recycle(Object o) {
if (this.numFilled == this.list.length) {
this.resizeTo(this.list.length * 2);
}
this.list[this.numFilled++] = o;
}
}
|