User:Bellezzasolo/Memory Management

From OSDev Wiki
Jump to: navigation, search

This is my take on memory management

Contents

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 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

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.
Personal tools
Namespaces
Variants
Actions
Navigation
About
Toolbox