- Have a "stack" of free addresses starting at the beggining of the kernel heap
- Have a variable holding the end of the kernel heap
Since my low level PMM only gives out 4KB Pages of phsyical memory it just becomes a process of checking if the pointer is past the end of the heap and if not incrementing it and returning the address. (Which will act as the pool malloc reads from when dishing out smaller portions for allocation)
Freeing is simply decrementing this counter and returning
To better state my idea, consider the following code:
Code: Select all
unsigned long *pmm_stack;
unsigned long stack_end;
void pmm_init()
{
*pmm_stack = 0xKernelHeapAddrHere
stack_end = 0xEndOfKernelHeapHere
return;
}
unsigned long pmm_alloc() // Returns a 4KB page
{
unsigned long retval;
if (*pmm_stack > stack_end) {
return -1; // Out of Memory
}
*pmm_stack += 4096 // 4KB
retval = *pmm_stack;
return retval;
}
void pmm_free()
{
*pmm_stack -= 4096 // 4KB
return;
}
Thanks,
Nelson