htils 1
A small set of utilities for C programming.
Loading...
Searching...
No Matches
arena.c File Reference
#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_tarena_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.

Macro Definition Documentation

◆ ALIGN_POW2

#define ALIGN_POW2 ( num,
pow )
Value:
(((u64)(num) + ((u64)(pow) - 1)) & (~((u64)(pow) - 1)))
uint64_t u64
Definition basictypes.h:22

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.

Parameters
numThe number to align.
powThe power of 2 to align to.

◆ ARENA_ALIGNMENT

#define ARENA_ALIGNMENT   (_Alignof(max_align_t))

The alignment of an arena, which is the alignment of max_align_t.

◆ ARENA_BASE_POS

#define ARENA_BASE_POS   (sizeof(arena_t))

The base position of an arena.

◆ MAX

#define MAX ( a,
b )
Value:
((a > b) ? a : b)

◆ MIN

#define MIN ( a,
b )
Value:
((a < b) ? a : b)

Function Documentation

◆ __arena_alloc()

void * __arena_alloc ( struct arena * arena,
u64 size )

Allocate a chunk of memory to the arena.

Note
This function is not meant to be run directly, and is called by the arena_alloc() macro.

Advances the position and returns a size-byte chunk from the committed region, committing another chunk first if the request would cross it.

Precondition
  • arena must be valid and cannot be null.
  • size must be greater than 0.
Parameters
arenaThe arena to allocate from.
sizeThe size of the chunk to allocate.
Returns
A pointer to the allocated chunk.
See also
arena_alloc()

◆ __arena_alloc_zeroed()

void * __arena_alloc_zeroed ( struct arena * arena,
u64 size )

Allocate a chunk of zeroed memory to the arena.

Note
This function is not meant to be run directly, and is called by the arena_alloc_zeroed() macro.

Like __arena_alloc(), with the returned chunk zeroed.

Precondition
  • arena must be valid and cannot be null.
  • size must be greater than 0.
Parameters
arenaThe arena to allocate from.
sizeThe size of the chunk to allocate.
Returns
A pointer to the allocated chunk.
See also
arena_alloc()

◆ __arena_dealloc()

void __arena_dealloc ( struct arena * arena,
u64 size )

Deallocate a chunk of memory from the arena.

Note
This function is not meant to be run directly, and is called by the arena_dealloc() macro.

Moves the position back by size, so the freed tail can be reused by later allocations.

Precondition
  • arena must be valid and cannot be null.
  • size must be greater than 0.
Parameters
arenaThe arena to deallocate from.
sizeThe size of the chunk to deallocate.
See also
arena_dealloc()

◆ arena_clear()

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.

Precondition
arena must be valid and cannot be null.
Parameters
arenaThe arena to clear.
See also
arena_dealloc_to()

◆ arena_dealloc_to()

void arena_dealloc_to ( arena_t * arena,
u64 pos )

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.

Precondition
  • arena and pos must be valid and cannot be null.
  • pos must be greater than 0.
Parameters
arenaThe arena to deallocate from.
posThe position to deallocate to.
See also
__arena_dealloc()

◆ arena_free()

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..

Precondition
arena must be valid and cannot be null.
Parameters
arenaThe arena to free.
See also
munmap(), VirtualFree()

◆ arena_new()

arena_t * arena_new ( u64 reserve_size,
u64 commit_size )

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.

Precondition
  • reserve_size must be greater than 0.
  • commit_size must be greater than 0 and less than reserve_size.
Parameters
reserve_sizeThe size of the arena to reserve.
commit_sizeThe size of the arena to commit.
Returns
A pointer to the new arena.
See also
mmap(), VirtualAlloc(), arena_free()

◆ temp_arena_free()

void temp_arena_free ( temp_arena_t temp)

Free a temp_arena.

Moves the arena back to the position captured by temp_arena_new().

Precondition
temp must be valid and cannot be null.
Parameters
tempThe temp_arena to free.
See also
temp_arena_new()

◆ temp_arena_new()

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().

Precondition
arena must be valid and cannot be null.
Parameters
arenaThe arena to create the temporary arena from.
Returns
The new temp_arena.
See also
temp_arena_free()