Page 1 of 1

using a bitmap?

Posted: Fri Jul 06, 2007 5:33 am
by com1
how would a use a bitmap for memory management? how do i even implement one in C?

Posted: Fri Jul 06, 2007 6:57 am
by XCHG
Each bit in the bitmap would represent a PTE (Page Table Entry) or a PDE (Page Directory Entry). The bit should be set to 1 if and only if its corresponding PTE/PDE is allocated/present/etc.

I don't recommend using a bitmap because as small as it might seem to be, you can still use the 3 available bits of each PTE/PDE to determine whether it is allocated or not. Those bits are Bit 9, 10 and 11.

I use them to determine if that particular PTE is Moveable/Committed/Reserved.

Posted: Fri Jul 06, 2007 8:56 am
by AJ
In C, use bit operations as follows:

Code: Select all

unsigned long my_bitmap[size];

;to set a bit:
my_bitmap[x] |= (1<<bittoset);

;to clear a bit:
my_bitmap[x]&=~(1<<bittoclear);

;to test a bit:
if(my_bitmap[x]&(1<<bittotest))
{
}
Cheers,
Adam

thanks

Posted: Fri Jul 06, 2007 2:47 pm
by com1
thanks for all the info. i know about page table entries and i am going to make the pages 4MB each, with a array of structures containg information on register saving, stack, etc.

Posted: Fri Jul 06, 2007 4:51 pm
by Kevin McGuire
my_bitmap[bitnum/8] |= (1<<(bitnum%8 )); // set bit high
my_bitmap[bitnum/8] &= ~(1<<(bitnum%8 )); // set bit low
if(my_bitmap[bitnum/8] & (1<<(bitnum%8 ))); // check bit high

Code: Select all

AllocPageFromBitmap(uint8_t *my_bitmap, uint32_t pageAddr)
{
    if(my_bitmap[(pageAddr>>12)/8] & (1<<((pageAddr>>12)%8 ) ) )
    {
         /// already allocated!
    }
    /// allocate
    my_bitmap[(pageAddr>>12)/8] &= ~(1<<((pageAddr>>12)%8 ));
}
my_bitmap[(4GB>>12)/8]
my_bitmap[(0xFFFFFFFF>>12)/8]
my_bitmap[(TotalFreeMemoryFromGrubInBytes>>12)/8]