Skip to content

some sort of understanding when it comes to memory management and how it used when creating your own Application such as an engine

Notifications You must be signed in to change notification settings

bitTone/memoryallocator

Repository files navigation

MemoryAllocator

some sort of understanding when it comes to memory management and how it used when creating your own Application such as an engine image This flowchart shows the main components and processes of the memory allocator:

  1. Initialization:
    • System checks available memory and sets up a pool size (25-60% of system memory)
    • Initializes the memory pool and free lists for buddy allocation
    • Sets up a separate stack region
  2. Memory Allocation (game_malloc):
    • Handles allocation requests, routing large allocations (>8MB) to system malloc
    • Uses buddy allocation system for normal requests
    • Splits blocks as needed to efficiently use memory
  3. Stack Allocation:
    • Provides fast allocation for temporary memory needs
    • Limited to 1MB total size
    • Allocates from top down
  4. Deallocation:
    • Checks if memory belongs to our pool
    • Either uses buddy system to free and merge blocks
    • Or delegates to system free for external allocations

The buddy system used here is particularly interesting as it maintains different size classes of memory blocks and can merge adjacent free blocks (buddies) to reduce fragmentation.

image

Free List

The free lists array is a fundamental part of the buddy allocation system. Here's how it works:

  1. Structure:
    • It's defined in the code as FreeBlock* free_lists[64]
    • Each index in the array corresponds to a specific block size
    • Index 0 holds blocks of 16 bytes (MIN_BLOCK_SIZE)
    • Each subsequent index represents double the size of the previous index
  2. Size Calculation:
    • Size at index i = 2^i * MIN_BLOCK_SIZE
    • For example:
      • Index 0: 16 bytes (2^0 * 16)
      • Index 1: 32 bytes (2^1 * 16)
      • Index 2: 64 bytes (2^2 * 16)
      • And so on...
  3. Implementation Details:
typedef struct FreeBlock {
    size_t size;
    struct FreeBlock* next;
} FreeBlock;

- Each block in the free lists is a linked list node
- It contains its size and a pointer to the next free block of the same size

1. Usage:
    - When memory is allocated:
        - The system finds the appropriate size index
        - If a block is available, it's removed from the list
        - If no block is available, a larger block is split into two buddies
    - When memory is freed:
        - The block is added back to the appropriate size list
        - The system checks if its "buddy" is also free
        - If the buddy is free, the blocks are merged and moved to the next size up

The power of this system is that it prevents memory fragmentation by keeping track of free blocks by size and allowing them to be split or merged as needed. When a block is freed, it can be merged with its buddy (adjacent block of the same size) to form a larger block, helping to maintain larger continuous spaces in memory.

About

some sort of understanding when it comes to memory management and how it used when creating your own Application such as an engine

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages