htils 1
A small set of utilities for C programming.
Loading...
Searching...
No Matches
arena.h File Reference
#include <htils/basictypes.h>

Go to the source code of this file.

Data Structures

struct  arena
 An arena. More...
 
struct  temp_arena
 A temporary arena. More...
 

Macros

#define KiB(bytes)   ((u64)bytes << 10)
 
#define MiB(bytes)   ((u64)bytes << 20)
 
#define GiB(bytes)   ((u64)bytes << 30)
 
#define arena_alloc(arena, type, size)    __arena_alloc(arena, sizeof(type) * size);
 Allocate a chunk of memory from the arena.
 
#define arena_dealloc(arena, type, size)    __arena_dealloc(arena, sizeof(type) * size);
 Deallocate a chunk of memory from the arena.
 

Typedefs

typedef struct arena arena_t
 An 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_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 
)     __arena_alloc(arena, sizeof(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().

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

◆ arena_dealloc

#define arena_dealloc (   arena,
  type,
  size 
)     __arena_dealloc(arena, sizeof(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.

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

◆ GiB

#define GiB (   bytes)    ((u64)bytes << 30)

◆ KiB

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

◆ MiB

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

Typedef Documentation

◆ arena_t

typedef struct arena arena_t

An arena.

Parameters
reservedThe size of the arena.
committedThe size of the committed heap.
posThe current position of the heap.
commit_posThe current position of the committed heap.

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

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