Repository navigation
3.2 Frame Allocator and Bitmap
Relevant source files
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
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

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
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:
-
0represents a free physical frame src/kernel/bitmap.zig7 -
1represents an allocated or reserved physical frame src/kernel/bitmap.zig8
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

Figure 2: Bitmap Storage Architecture mapping bitmap_storage and computeBitmapSize
Sources: src/kernel/bitmap.zig28-109
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 providedRegions, calculatesstart_pageandend_page, and callsmarkFree()for each page within the usable range, up tocapacity_pagessrc/kernel/bitmap.zig79-109 It also setstotal_pagesto the highest managed page index src/kernel/bitmap.zig104-107 -
bitmap.markUsedRange(start_phys, end_phys): Converts physical addresses to page indices, then iterates fromstart_pagetoend_pagecallingmarkUsed()for each src/kernel/bitmap.zig115-127 -
bitmap.getStorageRange(): Returns the physical address range occupied bybitmap_storageitself, preventing the allocator from handing out its own metadata structure. This involves converting virtual addresses ofbitmap_storageto physical addresses usingmem.virtToPhys()src/kernel/bitmap.zig129-141
Sources: src/kernel/bitmap.zig75-141
The runtime allocation interface provides single-page allocation and deallocation operations backed by first-free bit scanning src/kernel/bitmap.zig144-204
-
findFirstFree(): Iterates throughbitmapbytes. If a byte is not0xFF(meaning it contains at least one free bit), it then iterates through the bits within that byte to find the first0. The correspondingpage_indexis returned if it's withintotal_pagessrc/kernel/bitmap.zig148-168 -
allocFrame(): CallsfindFirstFree(). If a free page is found, it callsmarkUsed()for thatpage_indexand returns the physical base address (page_index * PAGE_SIZE) src/kernel/bitmap.zig184-190 -
freeFrame(phys_addr): Converts thephys_addrto apage_index. It performs a bounds check againsttotal_pagesand panics if invalid. Otherwise, it callsmarkFree()to set the corresponding bit to0src/kernel/bitmap.zig193-201

Figure 3: Frame Allocation and Deallocation Flow bridging allocFrame() and freeFrame() to findFirstFree() and markFree()
Sources: src/kernel/bitmap.zig144-204
- Overview
- Getting Started and Build System
- Kernel Configuration and Global State
- Project Governance and Contributing
- Boot and Early Kernel Initialization
- Bootloader Stages and Linking
- kmain Boot Sequence
- Splash Screen and Branding
- Interrupts, Scheduling, and Syscalls
- IDT, PIC, APIC and IRQ Stubs
- Scheduler and Task Lifecycle
- System Call Interface
- Demonstration Tasks
- CodaFS Filesystem and Storage
- On-Disk Format and Operations
- Space Manager and Block Devices
- Binary Format and Program Loading
- Shell, Terminal, and Display
- Shell Command Pipeline
- Terminal, Input Devices and Prompts
- Display: VGA, Framebuffer, Console and Serial