Veritable Lasagna
An Allocator & Data Structure Library for C.
Loading...
Searching...
No Matches
vl_arena.c File Reference
#include "vl_arena.h"
#include <stdlib.h>
#include <string.h>
+ Include dependency graph for vl_arena.c:

Functions

void vlArenaInit (vl_arena *arena, vl_memsize_t initialSize)
 Initializes the vl_arena structure with the given initial size.
 
void vlArenaFree (vl_arena *arena)
 Frees memory allocated by an arena instance.
 
vl_arenavlArenaNew (vl_memsize_t initialSize)
 Creates a new arena with the specified initial size.
 
void vlArenaDelete (vl_arena *arena)
 Deletes the given VL arena, freeing all allocated memory.
 
void vlArenaClear (vl_arena *arena)
 Clears all the elements in the given arena.
 
vl_arenavlArenaClone (const vl_arena *src, vl_arena *dest)
 Clones the specified arena to another.
 
void vlArenaReserve (vl_arena *arena, vl_memsize_t numBytes)
 Reserves storage in the underlying allocation of the given arena.
 
vl_arena_ptr vlArenaMemAlloc (vl_arena *arena, vl_memsize_t size)
 Take memory from the given arena.
 
vl_arena_ptr vlArenaMemRealloc (vl_arena *arena, vl_arena_ptr ptr, vl_memsize_t size)
 Reallocates memory for the given pointer in the given arena.
 
void vlArenaMemFree (vl_arena *arena, vl_arena_ptr ptr)
 Frees a memory block allocated in a vl_arena.
 
vl_arena_ptr vlArenaMemPrepend (vl_arena *arena, vl_arena_ptr dstPtr, const void *src, vl_memsize_t length)
 Copies a block of memory to the end of the specified arena allocation.
 
vl_arena_ptr vlArenaMemAppend (vl_arena *arena, vl_arena_ptr dstPtr, const void *src, vl_memsize_t length)
 Copies a block of memory to the beginning of the specified arena allocation.
 
vl_transientvlArenaMemSample (vl_arena *arena, vl_arena_ptr ptr)
 Sampling function that calculates a transient pointer into the specified arena.
 
vl_memsize_t vlArenaMemSize (vl_arena *arena, vl_arena_ptr ptr)
 Get the size of a memory block allocated in a VL arena.
 
vl_memsize_t vlArenaTotalCapacity (vl_arena *arena)
 Get the total capacity of the arena.
 
vl_memsize_t vlArenaTotalFree (vl_arena *arena)
 Get the total amount of free memory in the arena.
 

Function Documentation

◆ vlArenaClear()

void vlArenaClear ( vl_arena * arena)

Clears all the elements in the given arena.

This function removes all the elements in the arena, making it empty. The memory allocated for the elements is not released by this function.

Parameters
arenaPointer to the vl_arena structure.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vlArenaClone()

vl_arena * vlArenaClone ( const vl_arena * src,
vl_arena * dest )

Clones the specified arena to another.

Clones the entirety of the src arena to the dest arena.

The 'src' arena pointer must be non-null and initialized. The 'dest' arena pointer may be null, but if it is not null it must be initialized.

If the 'dest' arena pointer is null, a new arena is created via vlArenaNew.

Parameters
srcpointer
destpointer
Returns
pointer to arena that was copied to or created.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vlArenaDelete()

void vlArenaDelete ( vl_arena * arena)

Deletes the given VL arena, freeing all allocated memory.

Parameters
arenaPointer to the VL arena to be deleted.
+ Here is the call graph for this function:

◆ vlArenaFree()

void vlArenaFree ( vl_arena * arena)

Frees memory allocated by an arena instance.

Parameters
arenaPointer to the arena instance to be freed.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vlArenaInit()

void vlArenaInit ( vl_arena * arena,
vl_memsize_t initialSize )

Initializes the vl_arena structure with the given initial size.

This function initializes a vl_arena structure with the specified initial size. The initial size determines the number of elements that the arena can hold initially.

Parameters
arenaThe vl_arena structure to be initialized.
initialSizeThe initial size of the arena.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vlArenaMemAlloc()

vl_arena_ptr vlArenaMemAlloc ( vl_arena * arena,
vl_memsize_t size )

Take memory from the given arena.

This function is used to allocate memory from the specified arena. Memory is allocated by finding a free block of memory of equal or greater size within the internally managed block of memory.

Parameters
arenaA pointer to the arena from which memory will be allocated.
sizeThe size of the memory to be allocated in bytes.
Returns
A pointer to the allocated memory, or NULL if allocation fails.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vlArenaMemAppend()

vl_arena_ptr vlArenaMemAppend ( vl_arena * arena,
vl_arena_ptr dst,
const void * src,
vl_memsize_t length )

Copies a block of memory to the beginning of the specified arena allocation.

This function handles the case wherein the specified source pointer resides within the arena.

Parameters
arenapointer
dstarena pointer
srcmemory to copy from
lengthnumber of bytes from src
Complexity of O(n) linear.
Returns
new dst arena pointer
+ Here is the call graph for this function:

◆ vlArenaMemFree()

void vlArenaMemFree ( vl_arena * arena,
vl_arena_ptr ptr )

Frees a memory block allocated in a vl_arena.

This function frees the memory block referenced by the given pointer from the specified vl_arena. The memory block must have been previously allocated using the vlArenaMemAlloc() or vlArenaMemRealloc() functions.

Parameters
arenaThe vl_arena structure representing the arena.
ptrThe pointer to the memory block to be freed.
Note
The behavior is undefined if ptr does not point to a memory block allocated in the specified arena.
See also
vlArenaMemAlloc()
vlArenaMemRealloc()
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vlArenaMemPrepend()

vl_arena_ptr vlArenaMemPrepend ( vl_arena * arena,
vl_arena_ptr dst,
const void * src,
vl_memsize_t length )

Copies a block of memory to the end of the specified arena allocation.

This function handles the case wherein the specified source pointer resides within the arena.

Parameters
arenapointer
dstarena pointer
srcmemory to copy from
lengthnumber of bytes from src
Complexity of O(n) linear.
Returns
new dst arena pointer
+ Here is the call graph for this function:

◆ vlArenaMemRealloc()

vl_arena_ptr vlArenaMemRealloc ( vl_arena * arena,
vl_arena_ptr ptr,
vl_memsize_t size )

Reallocates memory for the given pointer in the given arena.

This function reallocates memory for a previously allocated block of memory in the specified arena. If the reallocation is successful, the old block of memory will be freed and the new block will have a size specified by the 'size' parameter.

Parameters
arenaThe arena to perform the reallocation on.
ptrThe pointer to the previously allocated block of memory.
sizeThe desired size of the newly reallocated block of memory.
Returns
A pointer to the newly reallocated block of memory, or NULL if reallocation fails.
Note
The contents of the memory block pointed to by 'ptr' are preserved up to the lesser of the new and old sizes.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vlArenaMemSample()

vl_transient * vlArenaMemSample ( vl_arena * arena,
vl_arena_ptr ptr )

Sampling function that calculates a transient pointer into the specified arena.

Parameters
arenaThe arena on which the operation is performed.
ptrThe arena pointer indicating the memory location on the arena.
+ Here is the caller graph for this function:

◆ vlArenaMemSize()

vl_memsize_t vlArenaMemSize ( vl_arena * arena,
vl_arena_ptr ptr )

Get the size of a memory block allocated in a VL arena.

This function returns the size of the memory block allocated at the specified pointer in a VL arena. The size is calculated based on the arena's metadata and may not represent the exact size of the data stored in the block.

Parameters
arenaPointer to the VL arena.
ptrPointer to the memory block.
Returns
Size of the memory block allocated at the specified pointer in the VL arena.
Note
This function assumes that the provided pointer is a valid memory block allocated in the specified VL arena. Passing an invalid or previously freed pointer to this function may result in undefined behavior.
+ Here is the caller graph for this function:

◆ vlArenaNew()

vl_arena * vlArenaNew ( vl_memsize_t initialSize)

Creates a new arena with the specified initial size.

This function initializes a new arena with the specified initial size. The arena is used to dynamically allocate memory and manage it efficiently using a memory region structure.

Parameters
initialSizeThe initial size of the arena.
Returns
A pointer to the newly created arena, or NULL if the arena creation failed.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vlArenaReserve()

void vlArenaReserve ( vl_arena * arena,
vl_memsize_t numBytes )

Reserves storage in the underlying allocation of the given arena.

This is done by doubling the size until the requested growth is met or exceeded. This function will always result in the reallocation of the underlying memory.

Parameters
arenapointer
numBytestotal bytes to reserve
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vlArenaTotalCapacity()

vl_memsize_t vlArenaTotalCapacity ( vl_arena * arena)

Get the total capacity of the arena.

This function returns the total capacity of the given arena, in bytes. The capacity represents the maximum number of elements that the arena can store without requiring reallocation.

Parameters
arenaPointer to the arena structure.
Returns
The total capacity of the arena.
+ Here is the call graph for this function:

◆ vlArenaTotalFree()

vl_memsize_t vlArenaTotalFree ( vl_arena * arena)

Get the total amount of free memory in the arena.

This function returns the total amount of free memory in the specified arena.

Parameters
arenaThe pointer to the arena structure.
Returns
The total amount of free memory in the arena.
+ Here is the call graph for this function:
+ Here is the caller graph for this function: