This is my take on memory management
Memory mangaement is an essential part of any OS. Without it an OS would be non-functional. There are sevaral different problems and solutions, some of which are listed here.
Problems and Solutions
Physical Memory Management
How do you divide up physical memory? How do you mantain this memory (in memory)?
For the division, 4K blocks (or whatever page size) seems logical. For the second problem you have the bootloader set up enough page size for your bitmap (or whatever you use to keep track of memory, say a page stack). I will use two levels of bitmaps (or on X64, 3) with the first level managing the actual memory (4096B blocks per bit), second level managing this for access speed, 1 bit = 128B. The third level will use the same quotient, wheras the second level will be 1 bit to 32B on X64 to keep table level 3 to 128B.
Virtual Memory Management
How do you deal with pages? swapping?
There is no special secret. I have separated code for X86 and X86-64 (#ifdef). My memory manager will swap pages to disk once I have an HDD driver.
malloc() and free()
How do you keep track of free/used blocks? How do you keep track of the size of each block (size in malloc)?
For the division of each individual page, I use a block size along the line of 16 or 32 bytes (#define) as malloc() is generally for large blocks. Each set of pages (so as not to get in the way of applications or data access) has a header that follows this structure:
|Offset (B)||Size (B)||Name & Description|
|0||4||Length - Number of pages accounted for|
Each page or set of pages has a bitmap that follows this structure (one set of bits represents a block's size (16B typical):
- FT is follow through (if not set it is end of malloc()'ed block). For free() purposes.
- FR is free (if set it is used). For malloc() to see wether available.