Skip to content

3.2 Frame Allocator and Bitmap

Glen edited this page Nov 14, 2026 · 2 revisions

Frame Allocator and Bitmap

Relevant source files

Purpose and Scope

This page details the physical memory discovery and frame allocation subsystems of CadenzaOS, implemented across src/kernel/frame_allocator.zig and src/kernel/bitmap.zig. The subsystem's responsibility is to translate raw BIOS E820 memory maps into tracked 4 KiB physical page frames, manage state via a compact bitmap, and provide allocation primitives for the kernel paging and memory systems.

Sources: src/kernel/frame_allocator.zig1-16src/kernel/bitmap.zig1-21


E820 Region Discovery

The FrameAllocator module parses raw firmware memory map entries provided by the bootloader via e820.zig, filtering out non-usable ranges and low-memory regions below 1 MiB src/kernel/frame_allocator.zig1-16

Usable regions are collected into a fixed-size internal array usable_regions of type Region to avoid dynamic allocation during early boot initialization src/kernel/frame_allocator.zig32-45 The E820Store module (src/kernel/E820Store.zig) is responsible for copying the bootloader-provided E820 map into kernel-owned memory (table), ensuring its stability src/kernel/E820Store.zig1-14frame_allocator.zig then reads from this stable table src/kernel/frame_allocator.zig98-99

diagram

Figure 1: E820 Data Flow and Region Discovery bridging natural language requirements to E820Store.init() and FrameAllocator.parseUsableMemory()

Sources: src/kernel/frame_allocator.zig32-135src/kernel/E820Store.zig1-74


Bitmap Allocator Data Structures

Physical frame tracking is managed by src/kernel/bitmap.zig using a fixed 32 KiB array bitmap_storage, which maps up to 1 GiB of physical RAM at a granularity of 4 KiB (PAGE_SIZE) per bit src/kernel/bitmap.zig28-41

In this bitmap convention:

By default, all bits in bitmap_storage are initialized to 0xFF (all memory marked as used/reserved) src/kernel/bitmap.zig37-39 During initialization, valid regions discovered by frame_allocator have their corresponding bits cleared to 0 via markFree()src/kernel/bitmap.zig55-61src/kernel/bitmap.zig75-109

diagram

Figure 2: Bitmap Storage Architecture mapping bitmap_storage and computeBitmapSize

Sources: src/kernel/bitmap.zig28-109


Initialization and Range Marking

The bitmap allocator must be initialized with the slice of usable Region structs returned by frame_allocator.getUsableRegions()src/kernel/frame_allocator.zig48-50src/kernel/bitmap.zig79-109

Key initialization and marking functions include:

  • bitmap.init(regions): Iterates through provided Regions, calculates start_page and end_page, and calls markFree() for each page within the usable range, up to capacity_pagessrc/kernel/bitmap.zig79-109 It also sets total_pages to the highest managed page index src/kernel/bitmap.zig104-107
  • bitmap.markUsedRange(start_phys, end_phys): Converts physical addresses to page indices, then iterates from start_page to end_page calling markUsed() for each src/kernel/bitmap.zig115-127
  • bitmap.getStorageRange(): Returns the physical address range occupied by bitmap_storage itself, preventing the allocator from handing out its own metadata structure. This involves converting virtual addresses of bitmap_storage to physical addresses using mem.virtToPhys()src/kernel/bitmap.zig129-141

Sources: src/kernel/bitmap.zig75-141


Frame Allocation and Deallocation Operations

The runtime allocation interface provides single-page allocation and deallocation operations backed by first-free bit scanning src/kernel/bitmap.zig144-204

  • findFirstFree(): Iterates through bitmap bytes. If a byte is not 0xFF (meaning it contains at least one free bit), it then iterates through the bits within that byte to find the first 0. The corresponding page_index is returned if it's within total_pagessrc/kernel/bitmap.zig148-168
  • allocFrame(): Calls findFirstFree(). If a free page is found, it calls markUsed() for that page_index and returns the physical base address (page_index * PAGE_SIZE) src/kernel/bitmap.zig184-190
  • freeFrame(phys_addr): Converts the phys_addr to a page_index. It performs a bounds check against total_pages and panics if invalid. Otherwise, it calls markFree() to set the corresponding bit to 0src/kernel/bitmap.zig193-201

diagram

Figure 3: Frame Allocation and Deallocation Flow bridging allocFrame() and freeFrame() to findFirstFree() and markFree()

Sources: src/kernel/bitmap.zig144-204

Clone this wiki locally