-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstFitMemorySimulator.java
More file actions
60 lines (52 loc) · 1.28 KB
/
FirstFitMemorySimulator.java
File metadata and controls
60 lines (52 loc) · 1.28 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
/**
* Memory strategy that puts a process in memory at the
* first location available in memory
*/
public class FirstFitMemorySimulator extends MemorySimulatorBase {
/**
* Default constructor that initializes the sim using an input file
* @param fileName The input file
*/
public FirstFitMemorySimulator(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
public int getNextSlot(int slotSize) {
int blocksize = 0;
int i;
for(i = 0; i < main_memory.length - slotSize; i++)
{
if(main_memory[i] == FREE_MEMORY && blocksize < slotSize)
blocksize++;
else
{
if(blocksize >= slotSize)
return i - blocksize;
blocksize = 0;
}
}
return -1;
/*
System.out.println("Finding open slot of size " + slotSize);
for (int i = 0; i < main_memory.length-slotSize; i++) {
//TODO: Are the bounds correct here? ^^^^^^^
boolean blockIsFree = true;
for (int j = 0; j < slotSize; j++) {
if (main_memory[i+j] != '.') {
blockIsFree = false;
break;
}
}
if (blockIsFree) {
return i;
}
}
return -1;*/
}
}