|
htils 1
A small set of utilities for C programming.
|
#include <htils/atomic_types.h>#include <stdatomic.h>#include <threads.h>#include <htils/basictypes.h>Go to the source code of this file.
Data Structures | |
| struct | arena |
| An atomic arena. More... | |
| struct | temp_arena |
| A temporary arena. More... | |
Macros | |
| #define | KiB(bytes) |
| #define | MiB(bytes) |
| #define | GiB(bytes) |
| #define | arena_alloc(arena, type, size) |
| Allocate a chunk of memory from the arena. | |
| #define | arena_alloc_zeroed(arena, type, size) |
| Allocate a chunk of zeroed memory from the arena. | |
| #define | arena_dealloc(arena, type, size) |
| Deallocate a chunk of memory from the arena. | |
Typedefs | |
| typedef struct arena | arena_t |
| An atomic arena. | |
| typedef struct temp_arena | temp_arena_t |
| A temporary arena. | |
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 arena_alloc | ( | arena, | |
| type, | |||
| size ) |
Allocate a chunk of memory from the arena.
Through pushing the commit position of arena, and returning the allocated chunk that you specify, the reason for it being a macro is due to being able to specify the type, this will automatically grow the commit size if it's too small, all this logic resides in __arena_alloc().
arena must be valid and cannot be null.type must be a valid type and size greater than 0.| arena | The arena to allocate from. |
| type | The type of the chunk to allocate. |
| size | The size of the chunk to allocate. |
| #define arena_alloc_zeroed | ( | arena, | |
| type, | |||
| size ) |
Allocate a chunk of zeroed memory from the arena.
Through pushing the commit position of arena, and returning the allocated chunk that you specify, the reason for it being a macro is due to being able to specify the type, this will automatically grow the commit size if it's too small, all this logic resides in __arena_alloc_zeroed().
arena must be valid and cannot be null.type must be a valid type and size greater than 0.| arena | The arena to allocate from. |
| type | The type of the chunk to allocate. |
| size | The size of the chunk to allocate. |
| #define arena_dealloc | ( | arena, | |
| type, | |||
| size ) |
Deallocate a chunk of memory from the arena.
Through popping the position from the arena, using __arena_dealloc(), the reason this is a macro is to be able to pass type.
arena must be valid and cannot be null.type must be a valid type and size greater than 0.| arena | The arena to deallocate from. |
| type | The type of the chunk to deallocate. |
| size | The size of the chunk to deallocate. |
| #define KiB | ( | bytes | ) |
| #define MiB | ( | bytes | ) |
An atomic arena.
| reserved | The size of the Atomic arena. |
| committed | The size of the committed heap. |
| pos | The current atomic position of the heap. |
| commit_pos | The current committed position of the heap. |
| commit_mtx | The mutex for the committed position. |
| typedef struct temp_arena temp_arena_t |
A temporary arena.
| arena | The arena to create the temporary arena from. |
| start_pos | The position to start the temporary arena from. |
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. |