|
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>Macros | |
| #define | __STDC_WANT_LIB_EXT1__ 1 |
| #define | MIN(a, b) ((a < b) ? a : b) |
| #define | MAX(a, b) ((a > b) ? a : b) |
| #define | ALIGN_POW2(num, pow) (((u64)(num) + ((u64)(pow) - 1)) & (~((u64)(pow) - 1))) |
| Aligns a number to a power of 2, if pow is a power of two. | |
| #define | ARENA_BASE_POS (sizeof(arena_t)) |
| #define | ARENA_ALIGNMENT (_Alignof(max_align_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_alloc_zeroed (struct arena *arena, u64 size) |
| Allocate a chunk of zeroed memory to the arena. | |
| void | __arena_dealloc (struct arena *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. | |
| void | arena_clear (arena_t *arena) |
| Clear the arena. | |
| 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. | |
| #define __STDC_WANT_LIB_EXT1__ 1 |
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 | |||
| ) | ((a > b) ? a : b) |
| #define MIN | ( | a, | |
| b | |||
| ) | ((a < b) ? a : b) |
Allocate a chunk of memory to the arena.
Through basically pushing the position forwards and returning a small chunk of memory from the committed heap.
| arena | The arena to allocate from. |
| size | The size of the chunk to allocate. |
arena and size must be valid and cannot be null.size must be greater than 0.Allocate a chunk of zeroed memory to the arena.
Through basically pushing the position forwards and returning a small chunk of memory from the committed heap.
| arena | The arena to allocate from. |
| size | The size of the chunk to allocate. |
arena and size must be valid and cannot be null.size must be greater than 0.Deallocate a chunk of memory from the arena.
Through basically popping back the position, so that it may be reused for other allocations.
| arena | The arena to deallocate from. |
| size | The size of the chunk to deallocate. |
arena and size must be valid and cannot be null.size must be greater than 0.| void arena_clear | ( | arena_t * | arena | ) |
Clear the arena.
Pops the arena to the base position of ARENA_BASE_POS using arena_dealloc_to(), then flushes the commit position using mem_decommit() and mem_commit().
| arena | The arena to clear. |
arena must be valid and cannot be null.Set arena position to @pos.
Through setting the position of the arena to pos, useful for clearing or removing a chunk of data you know you dont need any more. Uses __arena_dealloc() internally.
| arena | The arena to deallocate from. |
| pos | The position to deallocate to. |
arena and pos must be valid and cannot be null.pos must be greater than 0.| 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 | The arena to free. |
arena must be valid and cannot be null.Create a new arena.
Initializes a new arena using mmap() or VirtualAlloc(), depending on system.
| reserve_size | The size of the arena to reserve. |
| commit_size | The size of the arena to commit. |
reserve_size must be greater than 0.commit_size must be greater than 0 and less than or equal to reserve_size.| void temp_arena_free | ( | temp_arena_t | temp | ) |
Free a temp_arena.
By popping the arena back to the position it was at when the temp arena was initialized using temp_arena_new().
| temp | The temp_arena to free. |
temp must be valid and cannot be null.| temp_arena_t temp_arena_new | ( | arena_t * | arena | ) |
Create a new temp_arena.
Through saving a snapshot of the current pos param of the arena, to then pop back to when you run temp_arena_free().
| arena | The arena to create the temporary arena from. |
arena must be valid and cannot be null.®return The new temp_arena.