thewrongchristian wrote:
You can use a simple bump-allocator. Initialise a pointer to the end of your kernel bss, then when you need to allocate, the pointer becomes what you return, and you advance the next pointer beyond the memory just allocated.
Some important data structure might be located right after kernel BSS, such as multiboot info, ELF symbol table, memory map, etc. Advancing kernel_end pointer might overwrite those data.
I reserve some space at the end of kernel BSS, which I call early_alloc_buff. During system startup, I allocate memory inside this buffer, kernel_end pointer cannot go beyond the end of early_alloc_buff. Since early_alloc_buff is inside kernel BSS, GRUB won't place data there. When all important data is consumed or backed up (into that early_alloc_buff), I can then remove the buffer limit, and kernel_end pointer can go beyond the end of early_alloc_buff.
This kind of allocation is only used in startup, to allocate space for data structures whose size is unknown at compile time, such as GDT, struct page array, etc. And those memory is never freed.
Once page allocator and kernel heap is initialized, the early bump alloc should be disabled.
PS. actually I reserve two early buffers, one for read-only data, one for read-write data. Const data section is mapped with RW bit off.