libarena
Arena memory management library
Loading...
Searching...
No Matches
arena.c File Reference
#include "arena.h"
#include <stdlib.h>
#include <string.h>

Functions

Arenaarena_init (size_t size, size_t maxBlocks, int managed)
 Initializes an Arena with a given size.
 
int arena_destroy (Arena *arena)
 Destroys the given Arena, freeing all associated memory.
 
void arena_dump (Arena *arena, FILE *f)
 Dumps the raw memory of the arena to the given file stream.
 
void arena_print (Arena *arena)
 Prints a human-readable representation of the arena's blocks to stdout.
 
ArenaBlockarena_free_block (Arena *arena, ArenaBlock *block)
 Free the given block of memory and return the next one.
 
ArenaBlockarena_get_block (Arena *arena, void *p)
 Retrieves the ArenaBlock corresponding to the given pointer.
 
ArenaBlockarena_alloc (Arena *arena, size_t size)
 Allocates a block of memory of the specified size within the arena.
 
void * arena_malloc (Arena *arena, size_t size)
 Allocates a block of memory of the specified size within the arena.
 
void * arena_calloc (Arena *arena, size_t num, size_t size)
 Allocates memory for an array of elements, initializing all bytes to zero.
 
void * arena_realloc (Arena *arena, void *p, size_t size)
 Reallocates a block of memory to a new size within the arena.
 
int arena_free (Arena *arena, void *p)
 Frees a block of memory within the arena.
 
int arena_get_tag (Arena *arena, void *p)
 Retrieves the tag associated with a memory block.
 
int arena_set_tag (Arena *arena, void *p, int tag)
 Sets the tag for a memory block.
 
void arena_collect_tag (Arena *arena, int tag)
 Frees all memory blocks with the specified tag.
 
ArenaBlockarena_get_block_by_tag (Arena *arena, int tag, int n)
 Retrieves the n-th block with the specified tag.
 
void * arena_get_ptr_by_tag (Arena *arena, int tag, int n)
 Retrieves the pointer to the n-th block with the specified tag.
 
const char * arena_version (void)
 Retrieves the version of the arena library.
 

Function Documentation

◆ arena_alloc()

ArenaBlock * arena_alloc ( Arena arena,
size_t  size 
)

Allocates a block of memory of the specified size within the arena.

Parameters
arenaPointer to the Arena structure.
sizeSize of the memory block to allocate.
Returns
Pointer to the allocated ArenaBlock, or NULL if allocation fails.

◆ arena_calloc()

void * arena_calloc ( Arena arena,
size_t  num,
size_t  size 
)

Allocates memory for an array of elements, initializing all bytes to zero.

Parameters
arenaPointer to the Arena structure.
numNumber of elements to allocate.
sizeSize of each element.
Returns
Pointer to the allocated memory, or NULL on failure.

◆ arena_collect_tag()

void arena_collect_tag ( Arena arena,
int  tag 
)

Frees all memory blocks with the specified tag.

This function can only be used if the arena is in managed mode.

Parameters
arenaPointer to the Arena structure.
tagThe tag value to collect.

◆ arena_destroy()

int arena_destroy ( Arena arena)

Destroys the given Arena, freeing all associated memory.

Parameters
arenaPointer to the Arena structure to destroy.
Returns
ARENA_SUCCESS on success.

◆ arena_dump()

void arena_dump ( Arena arena,
FILE *  f 
)

Dumps the raw memory of the arena to the given file stream.

Parameters
arenaPointer to the Arena structure.
fFile stream to write the memory dump to.

◆ arena_free()

int arena_free ( Arena arena,
void *  p 
)

Frees a block of memory within the arena.

This function can only be used if the arena is in managed mode.

Parameters
arenaPointer to the Arena structure.
pPointer to the memory block to free.
Returns
1 on success, 0 on failure.

◆ arena_free_block()

ArenaBlock * arena_free_block ( Arena arena,
ArenaBlock block 
)

Free the given block of memory and return the next one.

This function can only be used if the arena is in managed mode.

Parameters
arenaPointer to the Arena structure.
blockPointer to the ArenaBlock to free.
Returns
Pointer to the next ArenaBlock after the freed block.

◆ arena_get_block()

ArenaBlock * arena_get_block ( Arena arena,
void *  p 
)

Retrieves the ArenaBlock corresponding to the given pointer.

This function can only be used if the arena is in managed mode.

Parameters
arenaPointer to the Arena structure.
pPointer to the memory block.
Returns
Pointer to the corresponding ArenaBlock, or NULL if not found.

◆ arena_get_block_by_tag()

ArenaBlock * arena_get_block_by_tag ( Arena arena,
int  tag,
int  n 
)

Retrieves the n-th block with the specified tag.

This function can only be used if the arena is in managed mode.

Parameters
arenaPointer to the Arena structure.
tagThe tag value to search for.
nThe index of the block to retrieve.
Returns
Pointer to the n-th ArenaBlock with the specified tag, or NULL if not found.

◆ arena_get_ptr_by_tag()

void * arena_get_ptr_by_tag ( Arena arena,
int  tag,
int  n 
)

Retrieves the pointer to the n-th block with the specified tag.

This function can only be used if the arena is in managed mode.

Parameters
arenaPointer to the Arena structure.
tagThe tag value to search for.
nThe index of the block to retrieve.
Returns
Pointer to the n-th memory block with the specified tag, or NULL if not found.

◆ arena_get_tag()

int arena_get_tag ( Arena arena,
void *  p 
)

Retrieves the tag associated with a memory block.

This function can only be used if the arena is in managed mode.

Parameters
arenaPointer to the Arena structure.
pPointer to the memory block.
Returns
The tag of the block, or ARENA_FAILURE if the block is not found.

◆ arena_init()

Arena * arena_init ( size_t  size,
size_t  maxBlocks,
int  managed 
)

Initializes an Arena with a given size.

Parameters
sizeThe total size of the arena to allocate.
maxBlocksThe maximum number of blocks that can be allocated at once.
managedIf non-zero, the arena will manage blocks, allowing for freeing and dynamic reallocation.
Returns
A pointer to the initialized Arena structure, or NULL on failure.

◆ arena_malloc()

void * arena_malloc ( Arena arena,
size_t  size 
)

Allocates a block of memory of the specified size within the arena.

Parameters
arenaPointer to the Arena structure.
sizeSize of the memory block to allocate.
Returns
Pointer to the allocated memory, or NULL if allocation fails.

◆ arena_print()

void arena_print ( Arena arena)

Prints a human-readable representation of the arena's blocks to stdout.

This function can only be used if the arena is in managed mode.

Parameters
arenaPointer to the Arena structure.

◆ arena_realloc()

void * arena_realloc ( Arena arena,
void *  p,
size_t  size 
)

Reallocates a block of memory to a new size within the arena.

Parameters
arenaPointer to the Arena structure.
pPointer to the existing memory block.
sizeNew size for the memory block.
Returns
Pointer to the reallocated memory, or NULL on failure.

◆ arena_set_tag()

int arena_set_tag ( Arena arena,
void *  p,
int  tag 
)

Sets the tag for a memory block.

This function can only be used if the arena is in managed mode.

Parameters
arenaPointer to the Arena structure.
pPointer to the memory block.
tagThe tag value to set.
Returns
ARENA_SUCCESS on success, ARENA_FAILURE if the block is not found.

◆ arena_version()

const char * arena_version ( void  )

Retrieves the version of the arena library.

Returns
String containing the version information.