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 FirstFitIndex = -1;
|
||||||
int WorstFitIndex = -1;
|
int WorstFitIndex = -1;
|
||||||
int BestFitIndex = -1;
|
int BestFitIndex = -1;
|
||||||
|
int startIndex = -1;
|
||||||
|
|
||||||
int size = 0;
|
int size = 0;
|
||||||
|
|
||||||
int smallestSoFar = memSize;
|
int smallestSoFar = memSize;
|
||||||
int biggestSoFar = 0;
|
int biggestSoFar = 0;
|
||||||
for (int i = 0; i < memSize; ++i) {
|
for (int i = 0; i < memSize; ++i) {
|
||||||
if (memory[i] == 0) {
|
if (memory[i] == 0) {
|
||||||
size++;
|
if (size == 0) {
|
||||||
// GTE, because we need to keep track of all algorithm indexes
|
startIndex = i;
|
||||||
if (size == howMuchToAllocate) {
|
|
||||||
// On first fit, set the index
|
|
||||||
if (FirstFitIndex == -1) {
|
|
||||||
FirstFitIndex = i - size + 1;
|
|
||||||
biggestSoFar = size;
|
|
||||||
smallestSoFar = size;
|
|
||||||
}
|
}
|
||||||
|
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 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) {
|
if (size > biggestSoFar) {
|
||||||
WorstFitIndex = i - size + 1;
|
WorstFitIndex = startIndex;
|
||||||
biggestSoFar = size;
|
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 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) {
|
if (size > 0 && smallestSoFar > size) {
|
||||||
BestFitIndex = i - size + 1;
|
BestFitIndex = startIndex;
|
||||||
smallestSoFar = size;
|
smallestSoFar = size;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
size = 0;
|
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
|
// Check if the last chunk is the smallest
|
||||||
if (smallestSoFar >= size) {
|
if (size > 0 && smallestSoFar >= size) {
|
||||||
BestFitIndex = memSize - size;
|
BestFitIndex = startIndex;
|
||||||
}
|
}
|
||||||
|
// Check if the last chunk is the biggest
|
||||||
if (howMuchToAllocate > memSize) {
|
if (size >= biggestSoFar) {
|
||||||
printf("Process %d failed to allocate %d memory\n", processId, howMuchToAllocate);
|
WorstFitIndex = startIndex;
|
||||||
// Statistics
|
|
||||||
totalFailed++;
|
|
||||||
|
|
||||||
// Early exit because there is no space, so don't touch memory
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Since first fit is always set if there is space, we can error on that for any algorithm
|
// 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);
|
printf("Process %d failed to allocate %d memory\n", processId, howMuchToAllocate);
|
||||||
// Statistics
|
// Statistics
|
||||||
totalFailed++;
|
totalFailed++;
|
||||||
@ -172,7 +174,7 @@ int getSmallest() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Check if the last chunk is the smallest
|
// Check if the last chunk is the smallest
|
||||||
if (smallestSize == memSize) {
|
if (smallestSize >= memSize) {
|
||||||
smallestSize = size;
|
smallestSize = size;
|
||||||
}
|
}
|
||||||
return smallestSize;
|
return smallestSize;
|
||||||
@ -184,15 +186,13 @@ int getBiggest() {
|
|||||||
for (int i = 0; i < memSize; ++i) {
|
for (int i = 0; i < memSize; ++i) {
|
||||||
if (memory[i] == 0) {
|
if (memory[i] == 0) {
|
||||||
size++;
|
size++;
|
||||||
} else {
|
} else if (size > biggestSize) {
|
||||||
if (size > biggestSize) {
|
|
||||||
biggestSize = size;
|
biggestSize = size;
|
||||||
}
|
|
||||||
size = 0;
|
size = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Check if the last chunk is the biggest
|
// Check if the last chunk is the biggest
|
||||||
if (biggestSize == 0) {
|
if (biggestSize <= size) {
|
||||||
biggestSize = size;
|
biggestSize = size;
|
||||||
}
|
}
|
||||||
return biggestSize;
|
return biggestSize;
|
||||||
|
Loading…
Reference in New Issue
Block a user