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

Macro Definition Documentation

◆ arena_alloc

#define arena_alloc ( arena,
type,
size )
Value:
__arena_alloc(arena, sizeof(type) * size);
void * __arena_alloc(struct arena *arena, u64 size)
Allocate a chunk of memory to the arena.
Definition arena.c:403
An atomic arena.
Definition arena.h:45

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

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

◆ arena_alloc_zeroed

#define arena_alloc_zeroed ( arena,
type,
size )
Value:
__arena_alloc_zeroed(arena, sizeof(type) * size);
void * __arena_alloc_zeroed(struct arena *arena, u64 size)
Allocate a chunk of zeroed memory to the arena.
Definition arena.c:474

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

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

◆ arena_dealloc

#define arena_dealloc ( arena,
type,
size )
Value:
__arena_dealloc(arena, sizeof(type) * size);
void __arena_dealloc(struct arena *arena, u64 size)
Deallocate a chunk of memory from the arena.
Definition arena.c:443

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.

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

◆ GiB

#define GiB ( bytes)
Value:
((u64)bytes << 30)
uint64_t u64
Definition basictypes.h:22

◆ KiB

#define KiB ( bytes)
Value:
((u64)bytes << 10)

◆ MiB

#define MiB ( bytes)
Value:
((u64)bytes << 20)

Typedef Documentation

◆ arena_t

typedef struct arena arena_t

An atomic arena.

Parameters
reservedThe size of the Atomic arena.
committedThe size of the committed heap.
posThe current atomic position of the heap.
commit_posThe current committed position of the heap.
commit_mtxThe mutex for the committed position.

◆ temp_arena_t

typedef struct temp_arena temp_arena_t

A temporary arena.

Parameters
arenaThe arena to create the temporary arena from.
start_posThe position to start the temporary arena from.

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