dsa-c
 
Loading...
Searching...
No Matches
arena_template.h File Reference

Arena allocator. More...

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

Go to the source code of this file.

Classes

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

Macros

#define FUNCTION_DEFINITIONS
 Define the functions.
 
#define TYPE_DEFINITIONS
 Define the types.
 
#define FUNCTION_LINKAGE
 Specify function linkage e.g. static inline.
 
#define PASTE(a, b)
 Paste two tokens together.
 
#define XPASTE(a, b)
 First expand tokens, then paste them together.
 
#define JOIN(a, b)
 First expand tokens, then paste them together with a _ in between.
 
#define IS_POW2(X)
 Macro to check if a number is a power of two.
 

Functions

arena_state_type arena_state_save (arena_type *arena_ptr)
 Save the arena state temporarily.
 
void arena_state_restore (arena_state_type prev_state)
 Restore the arena state.
 
void arena_init (arena_type *self, const size_t len, unsigned char *backing_buf)
 Initialize the arena.
 
void arena_deallocate_all (arena_type *self)
 Deallocate all allocations in the arena.
 
void * arena_allocate_aligned (arena_type *self, const size_t alignment, const size_t size)
 Get the pointer to a chunk of the arena. With specific alignment.
 
void * arena_allocate (arena_type *self, const size_t size)
 Get the pointer to a chunk of the arena.
 
void * arena_reallocate_aligned (arena_type *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.
 
void * arena_reallocate (arena_type *self, void *old_ptr, const size_t old_size, const size_t new_size)
 Reallocate a previously allocated chunk in the arena.
 

Detailed Description

Macro Definition Documentation

◆ IS_POW2

#define IS_POW2 ( X)
Value:
((X) != 0 && ((X) & ((X) - 1)) == 0)

Macro to check if a number is a power of two.

Parameters
[in]XThe number at hand.
Returns
A boolean value indicating whether the number is a power of two.

◆ JOIN

#define JOIN ( a,
b )
Value:
XPASTE(a, XPASTE(_, b))
#define XPASTE(a, b)
First expand tokens, then paste them together.
Definition fstack_template.h:42

First expand tokens, then paste them together with a _ in between.

◆ PASTE

#define PASTE ( a,
b )
Value:
a##b

Paste two tokens together.

◆ XPASTE

#define XPASTE ( a,
b )
Value:
PASTE(a, b)
#define PASTE(a, b)
Paste two tokens together.
Definition fstack_template.h:34

First expand tokens, then paste them together.

Function Documentation

◆ arena_allocate()

void * arena_allocate ( arena_type * self,
const size_t size )

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.
Examples
arena_example.c.

◆ arena_allocate_aligned()

void * arena_allocate_aligned ( arena_type * self,
const size_t alignment,
const size_t size )

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.
Examples
arena_example.c.

◆ arena_deallocate_all()

void arena_deallocate_all ( arena_type * self)

Deallocate all allocations in the arena.

Parameters
[in]selfArena pointer.
Examples
arena_example.c.

◆ arena_init()

void arena_init ( arena_type * self,
const size_t len,
unsigned char * backing_buf )

Initialize the arena.

Parameters
[in]selfArena pointer.
[in]lenBacking buffer length.
[in]backing_bufBacking buffer.
Examples
arena_example.c.

◆ arena_reallocate()

void * arena_reallocate ( arena_type * self,
void * old_ptr,
const size_t old_size,
const size_t new_size )

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.
Examples
arena_example.c.

◆ arena_reallocate_aligned()

void * arena_reallocate_aligned ( arena_type * 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.

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.
Examples
arena_example.c.

◆ arena_state_restore()

void arena_state_restore ( arena_state_type prev_state)

Restore the arena state.

Parameters
[in]prev_stateStored arena state.
Examples
arena_example.c.

◆ arena_state_save()

arena_state_type arena_state_save ( arena_type * arena_ptr)

Save the arena state temporarily.

Parameters
[in]arena_ptrThe arena whose state to save.
Examples
arena_example.c.