A bitmap based allocation technique
Posted: Mon Apr 26, 2004 5:33 pm
Hi,
as an amateur os developer, I'm still stuck in the world of memory management, confused as to what would be a reasonably good design and implementation method. Nevertheless, I decided to implement basic memory management using bitmaps just so that I could get a jump start and probably switch to something better as the code matures. My question is regarding my implemetation of a bitmap based allocation technique as shown below. I am using this function to allocate 4k slots in the kernel virtual address space which will be mapped to physical pages.
Its a function which returns an index (of an unset bit) in a bit map. It uses three static variables which keep track of free slots. There are three loops. The first one could loop a maximum of N/4 times, where N = size of bitmap in bytes. The second and the third, a maximum of 2 times. The idea is, once a 32bit word with free bits is tracked, the first loop won't execute at all, till the bits in it are used up. This narrows the subsequent searches down to 4 loops. Following this, the second loop won't enter till the 16bit word is not used up narrowing the search to 2 loops and so on to zero loops. Apart from the fact that there may be amazingly fast/good/efficient techniques out there, is there any reason why I shouldn't use "my" technique (as in, any pitfalls / design flaws / inefficiency / worst case) ?
Vivek
as an amateur os developer, I'm still stuck in the world of memory management, confused as to what would be a reasonably good design and implementation method. Nevertheless, I decided to implement basic memory management using bitmaps just so that I could get a jump start and probably switch to something better as the code matures. My question is regarding my implemetation of a bitmap based allocation technique as shown below. I am using this function to allocate 4k slots in the kernel virtual address space which will be mapped to physical pages.
Code: Select all
static void* mp_bitmap;
static uint32 mp_bitmap_i32 =0;
static uint16 mp_bitmap_i16 =0;
static uint8 mp_bitmap_i8 =0;
uint32 get_slot()
{
uint8 b, bit;
if ( ((uint32*)(mp_bitmap))[mp_bitmap_i32] == 0xFFFFFFFF)
for(mp_bitmap_i32 = 0;
((uint32*)(mp_bitmap))[mp_bitmap_i32] == 0xFFFFFFFF; ++mp_bitmap_i32);
if ( ((uint16*)(mp_bitmap))[mp_bitmap_i16] == 0xFFFF)
for(mp_bitmap_i16 = mp_bitmap_i32*4;
((uint16*)(mp_bitmap))[mp_bitmap_i16] == 0xFFFF; ++mp_bitmap_i16);
if ( ((uint8*)(mp_bitmap))[mp_bitmap_i8] == 0xFF)
for(mp_bitmap_i8 = mp_bitmap_i16*2;
((uint8*)(mp_bitmap))[mp_bitmap_i8] == 0xFF; ++mp_bitmap_i8);
b = ((uint8*)mp_bitmap)[mp_bitmap_i8];
for(bit = 7; b & 1; b = b >> 1)
bit--;
((uint8*)mp_bitmap)[mp_bitmap_i8] |= (0x80 >> bit);
return ((mp_bitmap_i8) * 8) + (bit);
}
Vivek