Ahh, memory management my favorite
Basically, a memory manager has many layers. It appears you are talking about the physical layer. This layer allocates blocks of physical memory which in turn are used by the virtual memory manager. If you are going without paging, this is the only layer you have to worry about. Also, physical memory is managed in blocks for efficiency's sake. On x86, the block size normally would be 4K (although there isn't anything requiring this, it is just more convenient when you implement paging) As far as algorithms go, there are 4 main algorithms that are used for PMMs
Bitmap - this one is easy to understand. You have a big array, where each bit represents a particular block. Bit 0 represents block 0, bit represents block 1, and so on. Allocation involves doing a linear search for this first bit set to 0 and then setting it to 1. Deallocation sets a given bit to 0.
Advantages
- Easy to understand
- Allows for contiguous memory blocks (which would be a necessity without paging)
- Many tutorials on it
- Takes up little memory
Disadvantages
- The slowest algorithm used. To improve performance, normally bits are compared in uint32_ts instead of smaller units. Also, caching the most recent allocated block as a hint could somewhat help as well
- Has a high overhead to implement
Free list - basically, you just have a linked list. At startup, you loop through every page that is free, and you add a pointer to the next free page at the first uintptr_t in that page. The head of the list is stored. Allocation involves grabbing the page at the head of the list, freeing involves placing a page at the head of the list
Advantages
- Very fast, O(1) in fact
- Takes up no extra memory, as free info data is stored in the actual free blocks
- Easy to implement allocation and freeing, not so easy to initialize
Disadvantages
- Impractical on 32 bit system where paging is used, as the physical memory needs to be mapped somewhere
- Would be very slow to allocate contingious blocks
Free stack - Basically, you allocate a big array to store all addresses of all free physical addresses. You loop through every free page, and add its address to the list. This creates a stack like data structure, where the highest address of the array is the stack top. Allocation involves popping a page of off the stack top. Freeing involves pushing a page on the stack top.
Advantages
- Very fast, O(1)
- Easy to implement
Disadvantages
- Takes up lots of extra memory
- Would be very slow to allocate contingious blocks
Finally, we have the buddy allocator. This one is more complex to explain, so I will point you to the
wiki. Basically, it combines a free list / stack and a bitmap to allow fast allocation of larger contiguous blocks. I will implement this one in my OS.
In reality, a buddy system is good as it gives you better cache performance for contiguous blocks, but has a high overhead. Note that Linux also has multiple buddies, one for ISA DMA (which must be 64K aligned), one for normal pages, and I believe one for large pages.
For a hobby OS, a free stack will be good enough. This is fast, simple, and works great. If you want a more sense of professional-ism, then go with a buddy system. I would recommend googling about Lazy Buddy as well, as it has some performance advantages over normal buddy.
I hope I didn't bore you with loads of theory
nexos