So I've followed the barebones tutorial, written a screen function, keyboard driver etc and am now at the point where I want to write a physical memory manager. I'm not sure how I should really go about doing it. I've read everything on the wiki and I'm still pretty stuck.
Basically, what I'm thinking, is having a doubly linked list to keep track of all of the blocks of memory, so each block points to the block before it and the block after it. Each block has a flags variable that tells the memory manager about the block. However, I can see two problems with this.
1. I don't know where to start the first block. I assume it would be somewhere after my kernel, but I don't know where the kernel is located. If it's high up, I could waste a heap of memory.
2. Freeing blocks could be a pain and I could have memory fragmentation up the whazoo.
Currently, the code I have looks something like this (note it's not finished):
Code: Select all
void* kalloc_block(uint32_t size)
{
block_t* block = head;
//Whoa... Something's wrong..
if(head == NULL)
{
kpanic("memmmngr: Head is NULL", NULL);
}
while(block != NULL || block->flags & ~BLOCK_FREE)
{
//IMPORTANT NOTE: WE NEED TO ITERATE CURRENT POINTER LOCATION + CURRENT BLOCK SIZE!!!!!!!!
//Looks like we've found a free block!
if(block->next == NULL)
break; //Get me outta here!
kprintc("This block is taken!\n", 0x0C);
block = block + block->size;
}
kprintc("Allocating a block!\n", 0x0A);
}
Code: Select all
struct memblock
{
struct memblock* prev; //Link to previous block
/*BLOCK HEADER; 9-bytes wasted...*/
uint32_t size; //Size of the block. This should be aligned
uint8_t flags; //Block flags
struct memblock* next;
};
Sorry for such a basic question!