|
ginnOS
The reference: libc, the libraries and the kernel
|
#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). | |
| 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().
allocate zeroed memory for an array.
| count | number of elements. |
| size | size of each element in bytes. |
| 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. |
| 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.