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>

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

◆ __STDC_WANT_LIB_EXT1__

#define __STDC_WANT_LIB_EXT1__   1

◆ ALIGN_POW2

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

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,
 
)    ((a > b) ? a : b)

◆ MIN

#define MIN (   a,
 
)    ((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.

Through basically pushing the position forwards and returning a small chunk of memory from the committed heap.

Parameters
arenaThe arena to allocate from.
sizeThe size of the chunk to allocate.
Precondition
  • arena and size must be valid and cannot be null.
  • size must be greater than 0.
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.

Through basically pushing the position forwards and returning a small chunk of memory from the committed heap.

Parameters
arenaThe arena to allocate from.
sizeThe size of the chunk to allocate.
Precondition
  • arena and size must be valid and cannot be null.
  • size must be greater than 0.
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.

Through basically popping back the position, so that it may be reused for other allocations.

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

◆ arena_clear()

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

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

◆ arena_dealloc_to()

void arena_dealloc_to ( arena_t arena,
u64  pos 
)

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.

Parameters
arenaThe arena to deallocate from.
posThe position to deallocate to.
Precondition
  • arena and pos must be valid and cannot be null.
  • pos must be greater than 0.
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..

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

◆ arena_new()

arena_t * arena_new ( u64  reserve_size,
u64  commit_size 
)

Create a new arena.

Initializes a new arena using mmap() or VirtualAlloc(), depending on system.

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

◆ temp_arena_free()

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

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

◆ temp_arena_new()

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

Parameters
arenaThe arena to create the temporary arena from.
Precondition
arena must be valid and cannot be null.

®return The new temp_arena.

See also
temp_arena_free()