get_frames() finished

This commit is contained in:
Alex 2017-06-19 10:29:37 -05:00
parent 7cfd4eb536
commit 1c011508c1

View file

@ -209,33 +209,100 @@ unsigned long ContFramePool::get_frames(unsigned int _n_frames)
// Find a frame that is not being used and return its frame index.
// Mark that frame as being used in the bitmap.
// NOTE: Must be updated to find a sequence of contiguous frames
// that are not being used and update the index of the head.
// that are not being used and return the index of the head.
unsigned int frame_no = base_frame_no;
// i is being used as the frame_no / 4
// j is frame_no % 4
// together, they will give the actual frame_no.
// c is used as a counter to count a squence of free frames.
unsigned int i = 0;
unsigned int j = 0;
unsigned int c = 0;
while (true) {
if(bitmap[i] != 0x0)
// used as a simple way to check if we failed to find a free
// frame for an index i in the bitmap.
bool failed = false;
while (true)
{
unsigned char mask = 0x80;
// check every 2 bits for a free frame in the bitmap
while((mask & bitmap[i]) == 0 || ((mask >> 1) & bitmap[i]) == 0)
{
if(mask != 0x00)
{
j++;
mask >> 2;
}
else
{
failed = true;
i++;
j = 0;
break;
}
}
// if frame is found, start checking for sequence
if(!failed)
{
unsigned int temp = i;
c++;
while(c < _n_frames)
{
if(mask != 0x00)
{
mask >> 2;
}
else
{
temp++;
mask = 0x80;
}
if((mask & bitmap[temp]) != 0 ^^ ((mask >> 1) & bitmap[temp]) != 0)
{
c++;
}
else
{
c = 0;
break;
}
}
i++;
if(c == _n_frames)
{
nFreeFrames -= _n_frames;
break;
}
}
failed = false;
}
frame_no += i * 8;
unsigned char mask = 0x80;
while ((mask & bitmap[i]) == 0) {
mask = mask >> 1;
frame_no++;
}
nFreeFrames--;
frame_no += i*4 + j;
// Update bitmap
bitmap[i] = bitmap[i] ^ mask;
// First: clear most significant bit to mark head of sequence.
bitmap[i] = bitmap[i] ^ (0x80 >> j*2);
// Second: clear both bits for all remaining frames in the sequence.
c = 1;
unsigned char mask = 0x80 >> j*2;
unsigned int temp = i;
while(c < _n_frames)
{
if(mask != 0x00)
{
mask >> 2;
}
else
{
temp++;
mask = 0x80;
}
bitmap[temp] = bitmap[temp] ^ mask;
bitmap[temp] = bitmap[temp] ^ (mask >> 1);
c++;
}
return (frame_no);
}