using a bitmap?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
com1
Member
Member
Posts: 105
Joined: Sat Apr 28, 2007 11:57 am
Location: TN

using a bitmap?

Post by com1 »

how would a use a bitmap for memory management? how do i even implement one in C?
oh microsoft, microsoft, what souls you have dismayed
User avatar
XCHG
Member
Member
Posts: 416
Joined: Sat Nov 25, 2006 3:55 am
Location: Wisconsin
Contact:

Post 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.
On the field with sword and shield amidst the din of dying of men's wails. War is waged and the battle will rage until only the righteous prevails.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post 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
com1
Member
Member
Posts: 105
Joined: Sat Apr 28, 2007 11:57 am
Location: TN

thanks

Post 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.
oh microsoft, microsoft, what souls you have dismayed
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post 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]
Post Reply