-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryBlock.java
62 lines (53 loc) · 2.28 KB
/
MemoryBlock.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
///////////////////
// MemoryBlock: Class used to store the memory block functions and states
///////////////////
public class MemoryBlock extends OS {
private int blockLocation = 0; //the block's current cursor location
private int maxBlockSize = 0; //the maximum size a block can be, set using setMaxBlockSize
private int numResets = 0; //the number of times the memory block has had to reset
private TaskStackQueue allocationBuffer = new TaskStackQueue("stack"); //the buffer of blocks stored in a stack for reference
//setMaxBlockSize - sets the max block size; can be increased but not decreased
public void setMaxBlockSize(int inputMaxBlockSize) {
if (inputMaxBlockSize > maxBlockSize) {
maxBlockSize = inputMaxBlockSize;
} else {
console.error("Can not lower the maxBlockSize value after it has been initialized.");
}
}
//allocate - allocates the given number of bytes to the memory block, if possible
public void allocate(int numBytes) {
//if the number of bytes is larger than the maximum block size
if (numBytes > maxBlockSize) {
console.error("Trying to allocate a block larger than " + maxBlockSize + " bytes.");
}
//if the number of bytes is larger than the available number of bytes
if (numBytes > (maxBlockSize - blockLocation)) {
empty();
}
//set the block "task" length
Task block = new Task();
block.length = numBytes;
blockLocation += numBytes;
allocationBuffer.add(block);
}
//empty - empties the bytes in the memory block
public void empty() {
blockLocation = 0;
numResets++;
allocationBuffer.erase();
}
//getNumBytesAvailable - returns the number of bytes still available
public int getNumBytesAvailable() {
return maxBlockSize - blockLocation;
}
//getHex - returns the hex representation of the blockLocation
public String getHex() {
String returnHex = Integer.toHexString(blockLocation);
//ensure the string is always 8 characters in length
for (int i = returnHex.length(); i < 8; i++) {
returnHex = "0" + returnHex;
}
returnHex = "0x" + returnHex;
return returnHex;
}
}