User:Bellezzasolo/Memory Management
This is my take on memory management
Memory Management
Introduction
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
Problem
How do you divide up physical memory? How do you mantain this memory (in memory)?
My Solution
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 page stack (or whatever you use to keep track of memory, say a bitmap). My stack contains physical addresses of all free blocks (access speed).
Virtual Memory Management
Problem
How do you deal with pages? swapping?
My Solution
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()
Problem
How do you keep track of free/used blocks? How do you keep track of the size of each block (size in malloc)?
My Solution
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 |
4 | (N.Pages*Page Size)/4 | Bitmap |
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|FR|...
Where:
- 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.