data-structures-c
Loading...
Searching...
No Matches
arena.h File Reference

Arena allocator. More...

#include "align.h"
#include "is_pow2.h"
#include <assert.h>
#include <stdalign.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Classes

struct  arena
 Arena data struct. More...
 
struct  temp_arena_state
 Tempory arena state struct. More...
 

Functions

static void arena_init (struct arena *self, const size_t len, unsigned char *backing_buf)
 Initialize the arena.
 
static void arena_deallocate_all (struct arena *self)
 Deallocate all allocations in the arena.
 
static void * arena_allocate_aligned (struct arena *self, const size_t alignment, const size_t size)
 Get the pointer to a chunk of the arena. With specific alignment.
 
static void * arena_allocate (struct arena *self, const size_t size)
 Get the pointer to a chunk of the arena.
 
static void * arena_reallocate_aligned (struct arena *self, void *old_ptr_, const size_t alignment, const size_t old_size, const size_t new_size)
 Reallocate a previously allocated chunk in the arena. With specific aligment.
 
static void * arena_reallocate (struct arena *self, void *old_ptr, const size_t old_size, const size_t new_size)
 Reallocate a previously allocated chunk in the arena.
 
static struct temp_arena_state temp_arena_state_save (struct arena *arena_ptr)
 Save the arena state temporarily.
 
static void temp_arena_state_restore (struct temp_arena_state prev_state)
 Restore the arena state.
 

Detailed Description

Function Documentation

◆ arena_allocate()

static void * arena_allocate ( struct arena * self,
const size_t size )
inlinestatic

Get the pointer to a chunk of the arena.

Parameters
[in]selfThe arena pointer.
[in]sizeThe section size in bytes.
Returns
A pointer to a zeroed-out memory chunk.
Return values
NULLIf the arena doesn't have enough memory for the allocation.

◆ arena_allocate_aligned()

static void * arena_allocate_aligned ( struct arena * self,
const size_t alignment,
const size_t size )
inlinestatic

Get the pointer to a chunk of the arena. With specific alignment.

Parameters
[in]selfarena pointer.
[in]alignmentalignment size
[in]sizechunk size
Returns
A pointer to a zeroed-out memory chunk.
Return values
NULLIf the arena doesn't have enough memory for the allocation.

◆ arena_deallocate_all()

static void arena_deallocate_all ( struct arena * self)
inlinestatic

Deallocate all allocations in the arena.

Parameters
[in]selfArena pointer.

◆ arena_init()

static void arena_init ( struct arena * self,
const size_t len,
unsigned char * backing_buf )
inlinestatic

Initialize the arena.

Parameters
[in]selfArena pointer.
[in]lenBacking buffer length.
[in]backing_bufBacking buffer.

◆ arena_reallocate()

static void * arena_reallocate ( struct arena * self,
void * old_ptr,
const size_t old_size,
const size_t new_size )
inlinestatic

Reallocate a previously allocated chunk in the arena.

Parameters
[in]selfThe arena pointer.
[in]old_ptrPointer to the buffer to reallocate
[in]old_sizeOld size.
[in]new_sizeNew size to grow/shrink to.
Returns
A pointer to the reallocated memory chunk.
Return values
NULLIf arena doesn't have enough memory for the reallocation or invalid parameters are given.

◆ arena_reallocate_aligned()

static void * arena_reallocate_aligned ( struct arena * self,
void * old_ptr_,
const size_t alignment,
const size_t old_size,
const size_t new_size )
inlinestatic

Reallocate a previously allocated chunk in the arena. With specific aligment.

Parameters
[in]selfArena pointer.
[in]old_ptr_Pointer to the buffer to reallocate
[in]alignmentAlignment size.
[in]old_sizeOld size.
[in]new_sizeNew size to grow/shrink to.
Returns
A pointer to the reallocated memory chunk.
Return values
NULLIf arena doesn't have enough memory for the reallocation or invalid parameters are given.

◆ temp_arena_state_restore()

static void temp_arena_state_restore ( struct temp_arena_state prev_state)
inlinestatic

Restore the arena state.

Parameters
[in]prev_stateStored arena state.

◆ temp_arena_state_save()

static struct temp_arena_state temp_arena_state_save ( struct arena * arena_ptr)
inlinestatic

Save the arena state temporarily.

Parameters
[in]arena_ptrThe arena whose state to save.