2023-03-15 23:02:09 +00:00
---
title: "heap.h"
summary: "dynamic memory allocator"
---
2023-03-16 10:04:14 +00:00
# `heap_segment_T` - struct
2023-03-15 23:02:09 +00:00
This header lies in memory, directly before the accessible buffer of the related segment.
| Name | Type | Description |
|-------|---------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------|
| magic | uint32_t | An out of bounds memory write would override this, what would indicate a heap corruption |
| size | uint64_t | The size of the segment |
| free | bool | If this is set, the segment is reclaimable |
| next | [heap_segment_T ](https://nerdcult.net/projects/noxos/docs/codebase/mm/heap.h/#heap_segment_t---struct )* | The next segment in the heap |
| prev | [heap_segment_T ](https://nerdcult.net/projects/noxos/docs/codebase/mm/heap.h/#heap_segment_t---struct )* | The previous segment in the heap |
2023-03-16 10:04:14 +00:00
# `heap_T` - struct
2023-03-15 23:02:09 +00:00
| Name | Type | Description |
|--------------|---------------------------------------------------------------------------------------------------------|-------------------------------------------------------------|
| start | void* | The start of the heaps segment pool |
| end | void* | The current end of the heaps segment pool (growing upwards) |
| last_segment | [heap_segment_T ](https://nerdcult.net/projects/noxos/docs/codebase/mm/heap.h/#heap_segment_t---struct )* | The current last segment in the heaps segment pool |
2023-03-16 10:04:14 +00:00
# `heap_init(heap*, base)` - function (void)
2023-03-15 23:02:09 +00:00
Initializes **_heap_** at **_base_** (virtual address).
It will automatically map some page frames to that address.
2023-03-16 10:04:14 +00:00
# `heap_memory_allocate(heap*, size)` - function (void)
2023-03-15 23:02:09 +00:00
Returns a pointer to a free usable memory location, that has at least the given **_size_** .
It will return `NULL` and log an error, if the heap is corrupted.
Because this function iterates over the entire heap to find a free segment, it is kinda slow.
2023-03-16 10:04:14 +00:00
# `heap_memory_free(heap*, address)` - function (void)
Frees a with [heap_memory_allocate ](https://nerdcult.net/projects/noxos/docs/codebase/mm/heap.h/#heap_memory_allocateheap-size---function-void ) created heap segment, and makes it usable again.
2023-03-15 23:02:09 +00:00
Does nothing, if the address doesn't point to a valid heap segment.
2023-03-16 10:04:14 +00:00
# `heap_dump_segments(heap*)` - function (void)
2023-03-15 23:02:09 +00:00
Logs a complete list, of all heap segments.
Useful, when debugging / testing the heap.
2023-03-16 10:04:14 +00:00
# `heap_destruct(heap*)` - function (void)
2023-03-15 23:02:09 +00:00
Invalidates all segments of a heap, frees all used page frames and unmaps them.