|
htils 1
A small set of utilities for C programming.
|
#include <stddef.h>#include <string.h>#include <htils/arena.h>#include <htils/assert.h>#include <htils/basictypes.h>#include <stdatomic.h>#include <threads.h>#include <htils/atomic_types.h>Macros | |
| #define | MIN(a, b) |
| #define | MAX(a, b) |
| #define | ALIGN_POW2(num, pow) |
| Aligns a number to a power of 2, if pow is a power of two. | |
| #define | ARENA_ALIGNMENT (_Alignof(max_align_t)) |
| #define | ARENA_BASE_POS (sizeof(arena_t)) |
Functions | |
| arena_t * | arena_new (u64 reserve_size, u64 commit_size) |
| Create a new arena. | |
| void | arena_free (arena_t *arena) |
| Free an arena. | |
| void * | __arena_alloc (struct arena *arena, u64 size) |
| Allocate a chunk of memory to the arena. | |
| void | __arena_dealloc (arena_t *arena, u64 size) |
| Deallocate a chunk of memory from the arena. | |
| void | arena_dealloc_to (arena_t *arena, u64 pos) |
Set arena position to pos. | |
| temp_arena_t | temp_arena_new (arena_t *arena) |
| Create a new temp_arena. | |
| void | temp_arena_free (temp_arena_t temp) |
| Free a temp_arena. | |
| void * | __arena_alloc_zeroed (struct arena *arena, u64 size) |
| Allocate a chunk of zeroed memory to the arena. | |
| void | arena_clear (arena_t *arena) |
| Clear the arena. | |
| #define ALIGN_POW2 | ( | num, | |
| pow ) |
Aligns a number to a power of 2, if pow is a power of two.
By adding pow - 1 to the number, and then masking out the rest to align properly.
| num | The number to align. |
| pow | The power of 2 to align to. |
| #define ARENA_ALIGNMENT (_Alignof(max_align_t)) |
The alignment of an arena, which is the alignment of max_align_t.
| #define ARENA_BASE_POS (sizeof(arena_t)) |
The base position of an arena.
| #define MAX | ( | a, | |
| b ) |
| #define MIN | ( | a, | |
| b ) |
Allocate a chunk of memory to the arena.
Advances the position and returns a size-byte chunk from the committed region, committing another chunk first if the request would cross it.
arena must be valid and cannot be null.size must be greater than 0.| arena | The arena to allocate from. |
| size | The size of the chunk to allocate. |
Allocate a chunk of zeroed memory to the arena.
Like __arena_alloc(), with the returned chunk zeroed.
arena must be valid and cannot be null.size must be greater than 0.| arena | The arena to allocate from. |
| size | The size of the chunk to allocate. |
Deallocate a chunk of memory from the arena.
Moves the position back by size, so the freed tail can be reused by later allocations.
arena must be valid and cannot be null.size must be greater than 0.| arena | The arena to deallocate from. |
| size | The size of the chunk to deallocate. |
| void arena_clear | ( | arena_t * | arena | ) |
Clear the arena.
Resets the position to the base with arena_dealloc_to(), then decommits everything past the initial commit, returning it to the OS. The initial commit_size region stays committed.
arena must be valid and cannot be null.| arena | The arena to clear. |
Set arena position to pos.
Moves the position back to pos, freeing everything allocated after it. This is what temp_arena_free() and arena_clear() are built on.
arena and pos must be valid and cannot be null.pos must be greater than 0.| arena | The arena to deallocate from. |
| pos | The position to deallocate to. |
| void arena_free | ( | arena_t * | arena | ) |
Free an arena.
Frees an arena using munmap() or VirtualFree(), depending on system, this is rarely needed cause the kernel usually frees these pages for you, but its useful if you use multiple arenas..
arena must be valid and cannot be null.| arena | The arena to free. |
Create a new arena.
Reserves reserve_size bytes of virtual address space (plus the arena header) and commits the first commit_size bytes. Later allocations commit further commit_size chunks as needed, up to the reserve. Backed by mmap() or VirtualAlloc(), depending on system.
reserve_size must be greater than 0.commit_size must be greater than 0 and less than reserve_size.| reserve_size | The size of the arena to reserve. |
| commit_size | The size of the arena to commit. |
| void temp_arena_free | ( | temp_arena_t | temp | ) |
Free a temp_arena.
Moves the arena back to the position captured by temp_arena_new().
temp must be valid and cannot be null.| temp | The temp_arena to free. |
| temp_arena_t temp_arena_new | ( | arena_t * | arena | ) |
Create a new temp_arena.
Snapshots the arena's current position, to be restored by temp_arena_free().
arena must be valid and cannot be null.| arena | The arena to create the temporary arena from. |