Internal Memory Management

the first part of the fourth chapter in our series on How to Write an Operating System


The Memory Heap

Code for several different types of memory heaps can be found at Ben Zorn's page of malloc()/free() implementations.

C++ Considerations

The preferred memory management mechanisms in C++ are the new and delete operators. Since you aren't linking with the C++ standard library, however, you need to define them yourself if you want to use them inside the kernel. Most likely, they'll just be wrappers that call the real kernel memory allocation functions and return:

void *kalloc(int size);     // allocate a chunk of kernel memory 
void kfree(void *ptr);       // free a chunk of kernel memory

void *operator new(int size) {
   return kalloc(size);
}

void operator delete(void *ptr) {
   kfree(ptr);
}