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) {
|
||||||
|
if (size == 0) {
|
||||||
|
startIndex = i;
|
||||||
|
}
|
||||||
size++;
|
size++;
|
||||||
// GTE, because we need to keep track of all algorithm indexes
|
} else {
|
||||||
if (size == howMuchToAllocate) {
|
if (size >= howMuchToAllocate) {
|
||||||
// On first fit, set the index
|
// On first fit, set the start index
|
||||||
if (FirstFitIndex == -1) {
|
if (FirstFitIndex == -1) {
|
||||||
FirstFitIndex = i - size + 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 = startIndex;
|
||||||
biggestSoFar = size;
|
biggestSoFar = 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 (size > 0 && smallestSoFar > size) {
|
||||||
|
BestFitIndex = startIndex;
|
||||||
smallestSoFar = size;
|
smallestSoFar = 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) {
|
|
||||||
WorstFitIndex = i - size + 1;
|
|
||||||
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;
|
|
||||||
smallestSoFar = size;
|
|
||||||
}
|
|
||||||
size = 0;
|
size = 0;
|
||||||
|
startIndex = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Check if the last chunk is the smallest
|
// Check if the last chunk is the first fit
|
||||||
if (smallestSoFar >= size) {
|
if (FirstFitIndex == -1 && size >= howMuchToAllocate) {
|
||||||
BestFitIndex = memSize - size;
|
FirstFitIndex = startIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (howMuchToAllocate > memSize) {
|
// Check if the last chunk is the smallest
|
||||||
printf("Process %d failed to allocate %d memory\n", processId, howMuchToAllocate);
|
if (size > 0 && smallestSoFar >= size) {
|
||||||
// Statistics
|
BestFitIndex = startIndex;
|
||||||
totalFailed++;
|
}
|
||||||
|
// Check if the last chunk is the biggest
|
||||||
// Early exit because there is no space, so don't touch memory
|
if (size >= biggestSoFar) {
|
||||||
return false;
|
WorstFitIndex = startIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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