The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
heap.h File Reference
#include "common/stdint.h"

Go to the source code of this file.

Data Structures

struct  heap_block
 block header placed before each allocation. More...
struct  heap_stats_t
 heap allocation statistics. More...

Macros

#define HEAP_INITIAL_PAGES   16
 initial number of pages allocated for the kernel heap.
#define HEAP_MIN_BLOCK_DATA   16
 minimum data size for a split block.
#define HEAP_EXPAND_PAGES   16
 minimum number of pages to add when the heap needs to grow.
#define HEAP_MAX_SEGMENTS   64
 maximum number of separate physical regions (segments) the heap can span.

Typedefs

typedef struct heap_block heap_block_t
 block header placed before each allocation.

Functions

void heap_init (void)
 initialize the kernel heap.
void * kmalloc (size_t size)
 allocate size bytes from the kernel heap.
void * kcalloc (size_t count, size_t size)
 allocate zeroed memory for an array.
void * krealloc (void *ptr, size_t new_size)
 resize a previously allocated block.
void kfree (void *ptr)
 free a previously allocated block.
size_t heap_total_size (void)
 return the total heap size in bytes, summed over all segments.
uint32_t heap_segment_count (void)
 return the number of physical segments the heap currently spans.
size_t heap_free_size (void)
 return the number of free bytes available in the heap.
uint32_t heap_block_count (void)
 return the total number of blocks (free + used) in the heap.
heap_stats_t heap_get_stats (void)
 return a snapshot of the heap allocation statistics.
bool heap_check (void)
 walk the block list and check its invariants: every block lies entirely inside one segment, blocks inside a segment are back to back and cover it exactly, and no two adjacent blocks are both free (they would have been merged).

Macro Definition Documentation

◆ HEAP_INITIAL_PAGES

#define HEAP_INITIAL_PAGES   16

initial number of pages allocated for the kernel heap.

16 pages = 64 KiB.

◆ HEAP_MIN_BLOCK_DATA

#define HEAP_MIN_BLOCK_DATA   16

minimum data size for a split block.

prevents creating uselessly small free blocks during splitting.

◆ HEAP_EXPAND_PAGES

#define HEAP_EXPAND_PAGES   16

minimum number of pages to add when the heap needs to grow.

16 pages = 64 KiB per expansion, so the segment table fills slowly.

◆ HEAP_MAX_SEGMENTS

#define HEAP_MAX_SEGMENTS   64

maximum number of separate physical regions (segments) the heap can span.

the heap grows in place when the pages after its newest segment are free, otherwise it takes a new contiguous run of pages from anywhere in RAM. that works because all RAM is identity-mapped; a later virtual-memory heap (kernel plan K3) removes the limit.

Typedef Documentation

◆ heap_block_t

typedef struct heap_block heap_block_t

block header placed before each allocation.

the usable memory returned to the caller begins immediately after this header. all allocations are 4-byte aligned.

Function Documentation

◆ heap_init()

void heap_init ( void )

initialize the kernel heap.

allocates HEAP_INITIAL_PAGES pages from the PMM and sets up the free list. must be called after pmm_init().

◆ kmalloc()

void * kmalloc ( size_t size)

allocate size bytes from the kernel heap.

the returned pointer is 4-byte aligned.

Parameters
sizenumber of bytes to allocate.
Returns
pointer to the allocated memory, or NULL on failure.

◆ kcalloc()

void * kcalloc ( size_t count,
size_t size )

allocate zeroed memory for an array.

Parameters
countnumber of elements.
sizesize of each element in bytes.
Returns
pointer to zeroed memory, or NULL on failure.

◆ krealloc()

void * krealloc ( void * ptr,
size_t new_size )

resize a previously allocated block.

if possible, expands in place. otherwise allocates a new block, copies the old data, and frees the original.

Parameters
ptrpointer previously returned by kmalloc/kcalloc/krealloc, or NULL.
new_sizenew size in bytes. if 0, equivalent to kfree(ptr).
Returns
pointer to the resized memory, or NULL on failure (original unchanged).

◆ kfree()

void kfree ( void * ptr)

free a previously allocated block.

coalesces adjacent free blocks to reduce fragmentation.

Parameters
ptrpointer previously returned by kmalloc(). NULL is a no-op.

◆ heap_total_size()

size_t heap_total_size ( void )

return the total heap size in bytes, summed over all segments.

◆ heap_segment_count()

uint32_t heap_segment_count ( void )

return the number of physical segments the heap currently spans.

◆ heap_free_size()

size_t heap_free_size ( void )

return the number of free bytes available in the heap.

◆ heap_block_count()

uint32_t heap_block_count ( void )

return the total number of blocks (free + used) in the heap.

◆ heap_get_stats()

heap_stats_t heap_get_stats ( void )

return a snapshot of the heap allocation statistics.

◆ heap_check()

bool heap_check ( void )

walk the block list and check its invariants: every block lies entirely inside one segment, blocks inside a segment are back to back and cover it exactly, and no two adjacent blocks are both free (they would have been merged).

intended for tests and debugging.

Returns
true if the heap is consistent.