# Operating system memory allocation?

## Recommended Posts

Lets say I have a Variable Partitioning with free space: at address 32k a free space of 8k, and at address 15k a free space of size 11k.

I need to create a first fit free table. and a best fit free table

Am I doing the right thing for both of the tables

+--------+-------+
|32      |8      |
|15      |11 	|
+--------+-------+

Edited by koky
##### Share on other sites

• 3 weeks later...

**deleted**

Edited by khaled
##### Share on other sites

Your issue is unclear... In C, I figure that a first-fit block allocation algorithm can be done like this:

void* block = 0;
unsigned int fitBlock = 0;
for ( ; fitBlock != blockQuantity; ++fitBlock)
{
if (blockFree[fitBlock] && blockSize[fitBlock] >= minimumSize) break;
}

if (fitBlock == blockQuantity)
{
// Couldn't make allocation as expected. Handle situation with alternatives ... (e.g. pagefile management).
}
else
{
blockFree[fitBlock] = 0;
// Block allocated.
}

Best fit: (a rudimentary implementation)

void* block = 0;
unsigned int bestFitIndex = 0;
unsigned int bestFitSize = sizeOfLargestBlock;

unsigned int fitBlock = 0;
for ( ; fitBlock != blockQuantity; ++fitBlock)
{
if (blockFree[fitBlock] && blockSize[fitBlock] >= minimumSize && blockSize[fitBlock] <= bestFitSize)
{
bestFitSize = blockSize[fitBlock];
bestFitIndex = fitBlock;
}
}

if (blockFree[bestFitIndex] && bestFitSize >= minimumSize)
{
blockFree[bestFitIndex] = 0;
// Block allocated.
}
else
{
// Couldn't make allocation as expected. Handle situation with alternatives ... (e.g. pagefile management).
}

Edited by Ben Bowen

## Create an account

Register a new account