# Frame Allocator and Bitmap Relevant source files - [src/kernel/E820Store.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820Store.zig) - [src/kernel/bitmap.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig) - [src/kernel/frame_allocator.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/frame_allocator.zig) - [src/kernel/idt.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/idt.zig) ## 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-16](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/frame_allocator.zig#L1-L16)[src/kernel/bitmap.zig1-21](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L1-L21) --- ## 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/frame_allocator.zig#L1-L16) 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/frame_allocator.zig#L32-L45) 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-14](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820Store.zig#L1-L14)`frame_allocator.zig` then reads from this stable table [src/kernel/frame_allocator.zig98-99](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/frame_allocator.zig#L98-L99) ![diagram](mermaids/3.2-frame-allocator-and-bitmap-diagram-1.png) *Figure 1: E820 Data Flow and Region Discovery bridging natural language requirements to `E820Store.init()` and `FrameAllocator.parseUsableMemory()`* Sources: [src/kernel/frame_allocator.zig32-135](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/frame_allocator.zig#L32-L135)[src/kernel/E820Store.zig1-74](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820Store.zig#L1-L74) --- ## 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L28-L41) In this bitmap convention: - `0` represents a **free** physical frame [src/kernel/bitmap.zig7](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L7-L7) - `1` represents an **allocated or reserved** physical frame [src/kernel/bitmap.zig8](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L8-L8) By default, all bits in `bitmap_storage` are initialized to `0xFF` (all memory marked as used/reserved) [src/kernel/bitmap.zig37-39](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L37-L39) During initialization, valid regions discovered by `frame_allocator` have their corresponding bits cleared to `0` via `markFree()`[src/kernel/bitmap.zig55-61](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L55-L61)[src/kernel/bitmap.zig75-109](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L75-L109) ![diagram](mermaids/3.2-frame-allocator-and-bitmap-diagram-2.png) *Figure 2: Bitmap Storage Architecture mapping `bitmap_storage` and `computeBitmapSize`* Sources: [src/kernel/bitmap.zig28-109](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L28-L109) --- ## 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-50](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/frame_allocator.zig#L48-L50)[src/kernel/bitmap.zig79-109](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L79-L109) Key initialization and marking functions include: - `bitmap.init(regions)`: Iterates through provided `Region`s, calculates `start_page` and `end_page`, and calls `markFree()` for each page within the usable range, up to `capacity_pages`[src/kernel/bitmap.zig79-109](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L79-L109) It also sets `total_pages` to the highest managed page index [src/kernel/bitmap.zig104-107](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L104-L107) - `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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L115-L127) - `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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L129-L141) Sources: [src/kernel/bitmap.zig75-141](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L75-L141) --- ## 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L144-L204) - `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_pages`[src/kernel/bitmap.zig148-168](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L148-L168) - `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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L184-L190) - `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 `0`[src/kernel/bitmap.zig193-201](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L193-L201) ![diagram](mermaids/3.2-frame-allocator-and-bitmap-diagram-3.png) *Figure 3: Frame Allocation and Deallocation Flow bridging `allocFrame()` and `freeFrame()` to `findFirstFree()` and `markFree()`* Sources: [src/kernel/bitmap.zig144-204](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L144-L204)