miniOS
x86_64 hobby kernel with SMP, VFS, and POSIX process model
Loading...
Searching...
No Matches
heap.c File Reference
#include <miniOS/mm/heap.h>
#include <miniOS/mm/pmm.h>
#include <miniOS/mm/vmm.h>
#include <miniOS/types.h>
#include <miniOS/arch/x86_64/spinlock.h>
Include dependency graph for heap.c:

Classes

struct  heap_hdr
struct  heap_ftr

Macros

#define HEAP_START   0xFFFF820000000000ULL
#define HEAP_MAX   (HEAP_START + 64ULL * 1024 * 1024) /* 64MB limit */
#define HDR_SIZE   16ULL /* sizeof aligned header */
#define FTR_SIZE   8ULL /* sizeof footer */
#define OVERHEAD   (HDR_SIZE + FTR_SIZE)
#define MIN_BLOCK   32ULL /* minimum allocation unit (8 bytes of usable payload) */

Functions

static struct heap_ftrblk_ftr (struct heap_hdr *hdr)
static struct heap_hdrblk_next (struct heap_hdr *hdr)
static void * blk_payload (struct heap_hdr *hdr)
static struct heap_hdrhdr_from_payload (void *ptr)
static void blk_init (uint64_t addr, size_t payload_size, uint8_t used)
static struct heap_hdrheap_expand (void)
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.

Variables

static uint64_t heap_top = 0
static spinlock_t heap_lock = SPINLOCK_INIT

Macro Definition Documentation

◆ FTR_SIZE

#define FTR_SIZE   8ULL /* sizeof footer */

◆ HDR_SIZE

#define HDR_SIZE   16ULL /* sizeof aligned header */

◆ HEAP_MAX

#define HEAP_MAX   (HEAP_START + 64ULL * 1024 * 1024) /* 64MB limit */

◆ HEAP_START

#define HEAP_START   0xFFFF820000000000ULL

◆ MIN_BLOCK

#define MIN_BLOCK   32ULL /* minimum allocation unit (8 bytes of usable payload) */

◆ OVERHEAD

#define OVERHEAD   (HDR_SIZE + FTR_SIZE)

Function Documentation

◆ blk_ftr()

struct heap_ftr * blk_ftr ( struct heap_hdr * hdr)
inlinestatic
Here is the caller graph for this function:

◆ blk_init()

void blk_init ( uint64_t addr,
size_t payload_size,
uint8_t used )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blk_next()

struct heap_hdr * blk_next ( struct heap_hdr * hdr)
inlinestatic
Here is the caller graph for this function:

◆ blk_payload()

void * blk_payload ( struct heap_hdr * hdr)
inlinestatic
Here is the caller graph for this function:

◆ hdr_from_payload()

struct heap_hdr * hdr_from_payload ( void * ptr)
inlinestatic
Here is the caller graph for this function:

◆ heap_expand()

struct heap_hdr * heap_expand ( void )
static
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ heap_lock

spinlock_t heap_lock = SPINLOCK_INIT
static

◆ heap_top

uint64_t heap_top = 0
static