Cottontail Mem Management/Implementation
Posted: Fri Mar 25, 2005 11:17 am
Hows this look? Just for note, I am in protected mode with GDT set up and A20 enabled. Memory map set to 0 already.
Cottontail Mem Management tutorial as found at http://osdever.net/tutorials/cottontailmm.php?the_id=47.
Heres code (Alloc only, I commented it tediously):
Cottontail Mem Management tutorial as found at http://osdever.net/tutorials/cottontailmm.php?the_id=47.
Heres code (Alloc only, I commented it tediously):
Code: Select all
#define MAP_BASE 0x10000000 //Start at 256mb mark
#define PAGE_BASE 0x10020000 //Start page map
#define MEM_BASE 0x10020400 //There are 2^20 pages, starting here
#define MICRO_PAGES 0x00100000 //Micro Pages/Mem Base (4kb/each)
#define SUPER_PAGES 0x00000400 //Super Pages/Start of each page (4mb/each)
unsigned long *MicroMap = MAP_BASE;
unsigned long *PageMap = PAGE_BASE;
const int CheckAND[] = { //So we can & to see what page is used in the longs (set of 32)
2^0, 2^1, 2^2, 2^3, 2^4, 2^5, 2^6, 2^7, 2^8, 2^9, 2^10, 2^11,
2^12, 2^13, 2^14, 2^15, 2^16, 2^17, 2^18, 2^19, 2^20, 2^21,
2^22, 2^23, 2^24, 2^25, 2^26, 2^27, 2^28, 2^29, 2^30, 2^31};
void* AllocPage(){ //Allocate 4kb block
int i = 0, x = 0, Page = 0; //i and x are counting vars, Page is superpage to use
void* newpage; //so we can return the final pointer
while(PageMap[i] == 0xFFFFFFFF){ //While the Super Page is used in this list 1-32...
i++; //Keep searching
if(i >= SUPER_PAGES)return NULL; //No more room, abort!
} //We leave 'i' with set of 32 that has free superpage
for(x=0; x<32; x++){ //Now search through set of 32 for that free one
if( !(PageMap[i] & CheckAND[x]) ){ //if this is a free of the 32 super pages....
Page = x + i*32; //make this our page, (rem., 'x' is within 32,
break; // so make it like a two-dim array like [super][mini])
}
}
i = 0; //Reset counter caus' we saved it in Page
while(MicroMap[i+Page*1024] == 0xFFFFFFFF){ //Search for free Micropage set in Superpage
i++; //Haven't found one yet
if(i >= MICRO_PAGES/SUPER_PAGES)return NULL; //Should never get her unless weird error
} //Since it said there was 1(or more) free
//up there in the for loop
for(x=0; x<32; x++){ //Find Page within set of 32 micropages
if(! (MicroMap[i+Page*1024] & CheckAND[x]) ){ //Found one?
newpage = (void*)(MEM_BASE+((i+Page*1024)*32+x)*4096); //Make ptr. to the actual mem
MicroMap[i+Page*1024] |= CheckAND[x]; //Mark it as used....
return newpage; //Return a page of free memory!
} //(Finally)
}
}