The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
heap.c File Reference
#include "heap.h"
#include "pmm.h"
#include "pmm_layout.h"
#include "reservations.h"
#include "kernel/panic.h"
#include "kernel/sync/spinlock.h"
#include "common/stdio.h"
#include "common/memory.h"

Functions

void heap_init (void)
 initialize the kernel heap.
void * kcalloc (size_t count, size_t size)
 allocate zeroed memory for an array.
void * kmalloc (size_t size)
 allocate size bytes from the kernel heap.
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).

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().

◆ 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.

◆ 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.

◆ 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.