|
ginnOS
The reference: libc, the libraries and the kernel
|
#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). | |
| #define HEAP_INITIAL_PAGES 16 |
initial number of pages allocated for the kernel heap.
16 pages = 64 KiB.
| #define HEAP_MIN_BLOCK_DATA 16 |
minimum data size for a split block.
prevents creating uselessly small free blocks during splitting.
| #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.
| #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 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.
| 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().
| void * kmalloc | ( | size_t | size | ) |
allocate size bytes from the kernel heap.
the returned pointer is 4-byte aligned.
| size | number of bytes to allocate. |
allocate zeroed memory for an array.
| count | number of elements. |
| size | size of each element in bytes. |
| 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.
| ptr | pointer previously returned by kmalloc/kcalloc/krealloc, or NULL. |
| new_size | new size in bytes. if 0, equivalent to kfree(ptr). |
| void kfree | ( | void * | ptr | ) |
free a previously allocated block.
coalesces adjacent free blocks to reduce fragmentation.
| ptr | pointer previously returned by kmalloc(). NULL is a no-op. |
| 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).
intended for tests and debugging.