I had asked there how it would be possible to swap out or delete the pageframe-stack (when it is empty) and use it's physical memory for other purposes.
Now i wrote some physical memory manager - i just wanted to ask if i got the idea right and if this is how to do it...
When the stack is empty (sp=-1) and someone still wants to allocate a page, i hand him the adress of the stack and set sp to -2 to indicate that not even the stack itsself is available any more.
When the stack was deleted and someone frees memory i first use this memory to set up a new pagestack...
Code: Select all
unsigned long* page_stack; //"stack"
long page_sp; //and stack pointer for pageframes
//!!! maybe it would safe space to use the numbers of the pageframes
//!!! instead of their adresses, multiplying/dividing them by 4096 for use as adress
//How many pageframes are left?
unsigned int num_pageframes()
{
//add 1 to sp because sp is an index and not a counter
//add 1 more to sp because we can still use the mem for the stack itsself
return page_sp+2;
};
//Allocate a pageframe. If none are left allocate the pageframe the stack is in.
unsigned long alloc_pageframe()
{
unsigned long adress;
//no page left - give away the space for the stack
if(page_sp==-1)
{
page_sp=-2;
return (unsigned long)page_stack;
}
//not even the space of the table left - out of memory
//0 is used as retvalue, as everybody knows that 0x00000 is reserved
else if(page_sp==-2)
return 0;
//else pop some adress
else
{
adress=page_stack[page_sp];
page_sp--;
return adress;
};
};
//Free pageframe starting at adress. If the pagestack was deleted, create a new stack at the given adress instead
void free_pageframe(unsigned long adress)
{
//If the stack was empty and therefore deleted, create a new stack for the adress
if(page_sp==-2)
{
page_stack=(unsigned long*)adress;
page_sp++;
}
//Else just push the freed adress to the stack
else
{
page_sp++;
page_stack[page_sp]=adress;
};
};
//setup the physical memory manager before first use
void setup_physmm(mbinfo* mbi)
{
//use some page-aligned adress
page_stack= (unsigned long*) 0x9C000;
//no free pageframes present
page_sp=-1;
//read free pages using the multiboot memory map
//!!!(here i have to add code to also reserve kernel regions!)
puts("Reading multiboot memory map.\n");
if(mmap_to_pagestack(page_stack, mbi)==false)
puts("Failed!\n");
};
EDIT: i also missed out to reserve new pages when the stack gets larger then one page and needs more space.
more EDIT:
There is another problem: When the pagestack is larger then one page... how do i ensure that it gets as much continuous physical pages as it needs?
What if the page after the first one is already in use by some client?
Or is this done by virtual memory, ie mapping the pages the stack needs in a way that the stack thinks they were continuous - but then, wasn't physical memory management supposed to be one layer below paging?