-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorstFitMemorySimulator.java
More file actions
59 lines (51 loc) · 1.36 KB
/
Copy pathWorstFitMemorySimulator.java
File metadata and controls
59 lines (51 loc) · 1.36 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
/**
* Memory strategy that puts a process in memory at the worst-fitting location
*/
public class WorstFitMemorySimulator extends MemorySimulatorBase {
/**
* Default constructor that initializes the sim using an input file
* @param fileName The input file
*/
public WorstFitMemorySimulator(String fileName) {
super(fileName);
}
/**
* Return the index of the first position of the next available slot
* in memory
* @param slotSize The size of the requested slot
* @return The index of the first position of an available requested block
*/
@Override
protected int getNextSlot(int slotSize) {
//Go through and find the index of the biggest gap
int best_start = -1;
int current_start = -1;
int biggest_size = 0;
int found_size = 0;
for (int i = 0; i < main_memory.length; i++) {
if (main_memory[i] == FREE_MEMORY) {
if (found_size == 0) {
current_start = i;
}
found_size++;
} else {
//Just hit non-free memory
if (found_size > biggest_size) {
biggest_size = found_size;
best_start = current_start;
}
found_size = 0;
}
}
//If the last slot is free, we take care of that here
if (found_size > biggest_size) {
biggest_size = found_size;
best_start = current_start;
}
if (slotSize > biggest_size) { //No slot available
return -1;
} else {
return best_start;
}
}
}