Jump to content

Recommended Posts

Posted (edited)

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

 

please help

 

+--------+-------+
|address | size  |
|32      |8      |
|15      |11 	|
+--------+-------+ 
Edited by koky
  • 3 weeks later...
Posted (edited)

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
{
block = blockAddress[fitBlock];
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)
{
	block = blockAddress[fitBlock];
	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 or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.