Kernelite 0.1.0
Kernelite is a minimalist, educational operating system kernel built from scratch. The project aims to recreate core components of the Linux architecture to explore the fundamentals of operating system design and hardware-software interaction.
Loading...
Searching...
No Matches
Macros | Functions
page_allocator.h File Reference

Physical page allocator for managing system memory blocks. More...

#include "mem_layout/mem_layout.h"
#include "page_table/page_table.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
Include dependency graph for page_allocator.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define PHYS_ADDR_NULL   ((phy_addr)NULL)
 A null physical address value.
 

Functions

bool page_allocator_add_region (phy_addr mem_start, size_t mem_size, bool check_kernel_overlap)
 Registers a new physical memory region with the allocator.
 
bool page_allocator_remove_region (phy_addr mem_start)
 Removes a previously registered memory region.
 
void fixup_page_allocator (void)
 Rewrites allocator bitmap pointers after the switch to high virtual addresses.
 
bool reserve_page (phy_addr start, size_t num_pages)
 Reserves a specific number of contiguous pages.
 
phy_addr page_alloc (size_t num_pages)
 Allocates a contiguous block of physical pages.
 
void page_free (phy_addr start, size_t num_pages)
 Frees a previously allocated block of physical pages.
 
void page_dump_status (void)
 Scans the bitmap and prints the status of all memory regions.
 

Detailed Description

Physical page allocator for managing system memory blocks.

This module handles allocation and deallocation of fixed-size 4 KiB pages, tracks page usage with a bitmap, and exposes the kernel page allocator API.

Author
Abhin Parekadan Jose
Date
2026-05-16

Function Documentation

◆ page_alloc()

phy_addr page_alloc ( size_t  num_pages)

Allocates a contiguous block of physical pages.

Searches for a free span of memory large enough to hold num_pages.

Parameters
num_pagesThe number of contiguous 4 KiB pages requested.
Returns
phy_addr start address of the allocated block, or PHYS_ADDR_NULL if insufficient contiguous memory exists.

◆ page_allocator_add_region()

bool page_allocator_add_region ( phy_addr  mem_start,
size_t  mem_size,
bool  check_kernel_overlap 
)

Registers a new physical memory region with the allocator.

Initializes a new memory chunk for the range starting at mem_start and spanning mem_size bytes. If check_kernel_overlap is true, the region is rejected when it overlaps the kernel image.

Parameters
mem_startThe physical address of the beginning of the region.
mem_sizeTotal size of the region, in bytes.
check_kernel_overlapWhether to reject regions that overlap with the kernel image.
Returns
true if the region was added successfully, false otherwise.

◆ page_allocator_remove_region()

bool page_allocator_remove_region ( phy_addr  mem_start)

Removes a previously registered memory region.

Marks the region that starts at mem_start as no longer managed by the allocator.

Parameters
mem_startThe physical base address of the region to remove.
Returns
true if the region was found and removed, false otherwise.

◆ page_dump_status()

void page_dump_status ( void  )

Scans the bitmap and prints the status of all memory regions.

Iterates through the managed page pool and groups contiguous pages with the same status (FREE or USED) into blocks for concise UART output.

◆ page_free()

void page_free ( phy_addr  start,
size_t  num_pages 
)

Frees a previously allocated block of physical pages.

Marks the specified range of pages as available for future allocations.

Parameters
startThe physical address of the first page to free.
num_pagesThe number of contiguous pages to release.

◆ reserve_page()

bool reserve_page ( phy_addr  start,
size_t  num_pages 
)

Reserves a specific number of contiguous pages.

Marks the specified range of pages as reserved.

Parameters
startThe physical address of the first page to reserve.
num_pagesThe number of contiguous pages to reserve.
Returns
bool True if the pages were successfully reserved, false otherwise.