-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryManager.java
More file actions
80 lines (68 loc) · 1.96 KB
/
Copy pathMemoryManager.java
File metadata and controls
80 lines (68 loc) · 1.96 KB
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
public class MemoryManager {
public class VAddress implements IValue {
final int addr;
VAddress(int rep) {
addr = rep;
}
private int get()
{ return addr;}
@Override
public String toStr() {
return "@stack_" + addr;
}
}
final private int DEFAULT_SIZE = 10000;
private IValue[] store;
private int sp;
private int limit;
private final String panicstr = "PANIC: ";
MemoryManager(int size)
{
limit = size;
store = new IValue[limit];
sp = limit;
}
MemoryManager()
{
limit = DEFAULT_SIZE;
store = new IValue[limit];
sp = limit;
// invariant sp>=0 && sp <= limit
}
VAddress push(IValue v) throws MemoryPanicError {
if (sp == 0)
throw new MemoryPanicError(panicstr+"out of memory");
sp = sp - 1;
// sp >= 0 && sp < limit
if (store[sp]!=null)
throw new MemoryPanicError(panicstr+"push overwrite");
store[sp] = v;
return new VAddress(sp);
}
IValue pop() throws MemoryPanicError {
if (sp==limit)
throw new MemoryPanicError(panicstr+"read out of bounds");
// sp < limit
IValue v = store[sp];
store[sp] = null;
sp = sp + 1;
// sp >= limit
return v;
}
IValue memrd(VAddress address) throws MemoryPanicError{
int addr = address.get();
if (addr<sp || addr>=limit) {
throw new MemoryPanicError(panicstr+"read out of bounds");
}
if (store[addr]==null)
throw new MemoryPanicError(panicstr+"read empty location");
return store[addr];
}
void memwrt(VAddress address, IValue v) throws MemoryPanicError{
int addr = address.get();
if (addr<sp || addr>=limit) {
throw new MemoryPanicError(panicstr+ "write out of bounds");
}
store[addr]=v;
}
}