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

Arena allocator. More...

#include <stddef.h>

Go to the source code of this file.

Macros

#define FUNCTION_LINKAGE
 Specify function linkage e.g. static inline.
#define IS_POW2(X)
 Macro to check if a number is a power of two.

Functions

struct arena_state arena_state_save (struct arena *arena_ptr)
 Save the arena state temporarily.
void arena_state_restore (struct arena_state prev_state)
 Restore the arena state.
void arena_init (void *self_, const size_t len, unsigned char *backing_buf)
 Initialize the arena.
void arena_deallocate_all (void *self_)
 Deallocate all allocations in the arena.
void arena_deallocate (void *self_, void *mem)
 Dummy no-op deallocate function.
void * arena_allocate_aligned (void *self_, const size_t alignment, const size_t size)
 Get the pointer to a chunk of the arena. With specific alignment.
void * arena_allocate (void *self_, const size_t size)
 Get the pointer to a chunk of the arena.
void * arena_reallocate_aligned (void *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 (void *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.

Function Documentation

◆ arena_allocate()

void * arena_allocate ( void * self_,
const size_t size )

Get the pointer to a chunk of the arena.

Parameters
[in]self_The 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, and fhashtable_example.c.

◆ arena_allocate_aligned()

void * arena_allocate_aligned ( void * self_,
const size_t alignment,
const size_t size )

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

Parameters
[in]self_arena 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, and fhashtable_example.c.

◆ arena_deallocate()

void arena_deallocate ( void * self_,
void * mem )

Dummy no-op deallocate function.

Parameters
[in]self_Arena pointer.
Examples
fhashtable_example.c.

◆ arena_deallocate_all()

void arena_deallocate_all ( void * self_)

Deallocate all allocations in the arena.

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

◆ arena_init()

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

Initialize the arena.

Parameters
[in]self_Arena pointer.
[in]lenBacking buffer length.
[in]backing_bufBacking buffer.
Examples
arena_example.c, and fhashtable_example.c.

◆ arena_reallocate()

void * arena_reallocate ( void * self_,
void * old_ptr,
const size_t old_size,
const size_t new_size )

Reallocate a previously allocated chunk in the arena.

Parameters
[in]self_The 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 ( void * 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]self_Arena 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 ( struct arena_state prev_state)

Restore the arena state.

Parameters
[in]prev_stateStored arena state.
Examples
arena_example.c, and fhashtable_example.c.

◆ arena_state_save()

struct arena_state arena_state_save ( struct arena * arena_ptr)

Save the arena state temporarily.

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