miniOS
x86_64 hobby kernel with SMP, VFS, and POSIX process model
Loading...
Searching...
No Matches
Kernel Heap (kmalloc/kfree)

Boundary-tag kernel heap allocator at HEAP_START (0xFFFF820000000000). More...

Functions

void heap_init (void)
 Maps the first heap page at HEAP_START via vmm_map_page() and writes the initial free-block boundary tag covering the entire heap region. Must be called exactly once during boot before any call to kmalloc(). Called by kernel_main() after vmm setup.
void * kmalloc (size_t size)
 Uses a first-fit boundary-tag strategy. Acquires heap_lock (spinlock_irqsave) before scanning. Maps additional pages via vmm_map_page() if the current heap extent is exhausted (up to HEAP_MAX).
void kfree (void *ptr)
 Marks the block free in its boundary tag and coalesces adjacent free blocks (forward and backward). Acquires heap_lock before modifying heap state.
size_t heap_free_bytes (void)
 Walks the boundary-tag chain and sums the sizes of all free blocks. O(n) in the number of heap blocks. Intended for diagnostic/debug output only.

Detailed Description

Boundary-tag kernel heap allocator at HEAP_START (0xFFFF820000000000).

Implements a simple first-fit boundary-tag heap that grows upward from HEAP_START. Maximum size is HEAP_MAX - HEAP_START = 4 MiB. Not thread-safe on its own; callers must hold heap_lock (a spinlock in heap.c) in SMP context.

Function Documentation

◆ heap_free_bytes()

size_t heap_free_bytes ( void )

Walks the boundary-tag chain and sums the sizes of all free blocks. O(n) in the number of heap blocks. Intended for diagnostic/debug output only.

heap_free_bytes() - Query the total free bytes currently available in the heap.

Returns
Total free bytes across all free blocks in the heap.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ heap_init()

void heap_init ( void )

Maps the first heap page at HEAP_START via vmm_map_page() and writes the initial free-block boundary tag covering the entire heap region. Must be called exactly once during boot before any call to kmalloc(). Called by kernel_main() after vmm setup.

heap_init() - Initialise the kernel heap allocator.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ kfree()

void kfree ( void * ptr)

Marks the block free in its boundary tag and coalesces adjacent free blocks (forward and backward). Acquires heap_lock before modifying heap state.

kfree() - Return a kmalloc'd allocation to the kernel heap free list.

Parameters
ptrPointer returned by a previous kmalloc() call. Passing NULL is a no-op.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kmalloc()

void * kmalloc ( size_t size)

Uses a first-fit boundary-tag strategy. Acquires heap_lock (spinlock_irqsave) before scanning. Maps additional pages via vmm_map_page() if the current heap extent is exhausted (up to HEAP_MAX).

kmalloc() - Allocate @size bytes from the kernel heap.

Parameters
sizeNumber of bytes to allocate. Rounded up to 8-byte alignment internally.
Returns
8-byte-aligned pointer to allocated memory, or NULL on OOM.
Here is the call graph for this function:
Here is the caller graph for this function: