Pass all tests
This commit is contained in:
parent
dbca562065
commit
71342900db
@ -33,53 +33,55 @@ bool doAllocate(int howMuchToAllocate, int processId) {
|
||||
int FirstFitIndex = -1;
|
||||
int WorstFitIndex = -1;
|
||||
int BestFitIndex = -1;
|
||||
int startIndex = -1;
|
||||
|
||||
int size = 0;
|
||||
|
||||
int smallestSoFar = memSize;
|
||||
int biggestSoFar = 0;
|
||||
for (int i = 0; i < memSize; ++i) {
|
||||
if (memory[i] == 0) {
|
||||
size++;
|
||||
// GTE, because we need to keep track of all algorithm indexes
|
||||
if (size == howMuchToAllocate) {
|
||||
// On first fit, set the index
|
||||
if (FirstFitIndex == -1) {
|
||||
FirstFitIndex = i - size + 1;
|
||||
biggestSoFar = size;
|
||||
smallestSoFar = size;
|
||||
if (size == 0) {
|
||||
startIndex = i;
|
||||
}
|
||||
size++;
|
||||
} else {
|
||||
if (size >= howMuchToAllocate) {
|
||||
// On first fit, set the start index
|
||||
if (FirstFitIndex == -1) {
|
||||
FirstFitIndex = startIndex;
|
||||
}
|
||||
// If the size of the contiguous empty memory is bigger than the largest contiguous empty memory so far, set the index and update the size
|
||||
if (size >= biggestSoFar) {
|
||||
WorstFitIndex = i - size + 1;
|
||||
if (size > biggestSoFar) {
|
||||
WorstFitIndex = startIndex;
|
||||
biggestSoFar = size;
|
||||
}
|
||||
|
||||
} else {
|
||||
// If the size of the contiguous empty memory is smaller than the smallest contiguous empty memory so far, set the index and update the size
|
||||
if (smallestSoFar >= size) {
|
||||
BestFitIndex = i - size + 1;
|
||||
if (size > 0 && smallestSoFar > size) {
|
||||
BestFitIndex = startIndex;
|
||||
smallestSoFar = size;
|
||||
}
|
||||
}
|
||||
size = 0;
|
||||
startIndex = -1;
|
||||
}
|
||||
}
|
||||
// Check if the last chunk is the first fit
|
||||
if (FirstFitIndex == -1 && size >= howMuchToAllocate) {
|
||||
FirstFitIndex = startIndex;
|
||||
}
|
||||
|
||||
// Check if the last chunk is the smallest
|
||||
if (smallestSoFar >= size) {
|
||||
BestFitIndex = memSize - size;
|
||||
if (size > 0 && smallestSoFar >= size) {
|
||||
BestFitIndex = startIndex;
|
||||
}
|
||||
|
||||
if (howMuchToAllocate > memSize) {
|
||||
printf("Process %d failed to allocate %d memory\n", processId, howMuchToAllocate);
|
||||
// Statistics
|
||||
totalFailed++;
|
||||
|
||||
// Early exit because there is no space, so don't touch memory
|
||||
return false;
|
||||
// Check if the last chunk is the biggest
|
||||
if (size >= biggestSoFar) {
|
||||
WorstFitIndex = startIndex;
|
||||
}
|
||||
|
||||
// Since first fit is always set if there is space, we can error on that for any algorithm
|
||||
if (FirstFitIndex == -1) {
|
||||
if (FirstFitIndex == -1 || howMuchToAllocate > memSize) {
|
||||
printf("Process %d failed to allocate %d memory\n", processId, howMuchToAllocate);
|
||||
// Statistics
|
||||
totalFailed++;
|
||||
@ -172,7 +174,7 @@ int getSmallest() {
|
||||
}
|
||||
}
|
||||
// Check if the last chunk is the smallest
|
||||
if (smallestSize == memSize) {
|
||||
if (smallestSize >= memSize) {
|
||||
smallestSize = size;
|
||||
}
|
||||
return smallestSize;
|
||||
@ -184,15 +186,13 @@ int getBiggest() {
|
||||
for (int i = 0; i < memSize; ++i) {
|
||||
if (memory[i] == 0) {
|
||||
size++;
|
||||
} else {
|
||||
if (size > biggestSize) {
|
||||
} else if (size > biggestSize) {
|
||||
biggestSize = size;
|
||||
}
|
||||
size = 0;
|
||||
}
|
||||
}
|
||||
// Check if the last chunk is the biggest
|
||||
if (biggestSize == 0) {
|
||||
if (biggestSize <= size) {
|
||||
biggestSize = size;
|
||||
}
|
||||
return biggestSize;
|
||||
|
Loading…
Reference in New Issue
Block a user