-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBestFitMemorySimulator.java
More file actions
59 lines (50 loc) · 1.45 KB
/
Copy pathBestFitMemorySimulator.java
File metadata and controls
59 lines (50 loc) · 1.45 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 a location which best
* fits the amount of memory requested.
*/
public class BestFitMemorySimulator extends MemorySimulatorBase {
/**
* Default constructor that initializes the sim using an input file
* @param fileName The input file
*/
public BestFitMemorySimulator(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
*/
public int getNextSlot(int slotSize) {
int best_start = -1;
int best_size = Integer.MAX_VALUE;
int current_start = -1;
int current_size = 0;
for (int i = 0; i < main_memory.length; i++) {
if (main_memory[i] == FREE_MEMORY) {
if (current_size == 0) {
current_start = i;
}
current_size++;
} else {
//We just hit in-use memory
//Only change placement info if we're coming off of an empty block
if (current_size > 0) {
if (current_size < best_size && current_size >= slotSize) {
best_start = current_start;
best_size = current_size;
}
current_size = 0;
}
}
}
//If the last spot is free, take that block into account
if (current_size < best_size && current_size >= slotSize) {
best_start = current_start;
best_size = current_size;
current_size = 0;
}
return best_start;
}
}