|
libarena
Arena memory management library
|
#include <stdbool.h>#include <stddef.h>#include <stdio.h>#include <string.h>Go to the source code of this file.
Data Structures | |
| struct | arena_block_s |
| struct | Arena |
| Arena structure. More... | |
Macros | |
| #define | ARENA_VERSION "unknown" |
| Version of libarena. | |
| #define | ARENA_TAG_NONE -1 |
| No tag placeholder. | |
| #define | ARENA_SUCCESS 0 |
| Success status. | |
| #define | ARENA_FAILURE -1 |
| Failure status. | |
| #define | ARENA_PTR(arena, block) ((void*) ((char*) arena->mem + block->idx)) |
| Get pointer from ArenaBlock. | |
| #define | ARENA_COPY(arena, dst, src) memcpy(ARENA_PTR(arena, dst), ARENA_PTR(arena, src), src->size) |
| Copy data from src to dst. | |
Typedefs | |
| typedef struct arena_block_s | ArenaBlock |
Enumerations | |
| enum | ArenaStatus { ARENA_STATUS_FREE = 0 , ARENA_STATUS_USED = 1 , ARENA_STATUS_UNDEFINED = 2 } |
| ArenaBlock status enum. More... | |
Functions | |
| Arena * | arena_init (size_t size, size_t blockCount, int managed) |
| Initializes an Arena with a given size. | |
| int | arena_destroy (Arena *arena) |
| Destroys the given Arena, freeing all associated memory. | |
| ArenaBlock * | arena_free_block (Arena *arena, ArenaBlock *block) |
| Free the given block of memory and return the next one. | |
| ArenaBlock * | arena_get_block (Arena *arena, void *p) |
| Retrieves the ArenaBlock corresponding to the given pointer. | |
| ArenaBlock * | arena_alloc (Arena *arena, size_t size) |
| Allocates a block of memory of the specified size within the arena. | |
| 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. | |
| 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 size, size_t num) |
| 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. | |
| ArenaBlock * | arena_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. | |
| #define ARENA_COPY | ( | arena, | |
| dst, | |||
| src | |||
| ) | memcpy(ARENA_PTR(arena, dst), ARENA_PTR(arena, src), src->size) |
Copy data from src to dst.
| #define ARENA_FAILURE -1 |
Failure status.
| #define ARENA_PTR | ( | arena, | |
| block | |||
| ) | ((void*) ((char*) arena->mem + block->idx)) |
Get pointer from ArenaBlock.
| #define ARENA_SUCCESS 0 |
Success status.
| #define ARENA_TAG_NONE -1 |
No tag placeholder.
| #define ARENA_VERSION "unknown" |
Version of libarena.
| typedef struct arena_block_s ArenaBlock |
| enum ArenaStatus |
ArenaBlock status enum.
| Enumerator | |
|---|---|
| ARENA_STATUS_FREE | |
| ARENA_STATUS_USED | |
| ARENA_STATUS_UNDEFINED | |
| ArenaBlock * arena_alloc | ( | Arena * | arena, |
| size_t | size | ||
| ) |
Allocates a block of memory of the specified size within the arena.
| arena | Pointer to the Arena structure. |
| size | Size of the memory block to allocate. |
| void * arena_calloc | ( | Arena * | arena, |
| size_t | num, | ||
| size_t | size | ||
| ) |
Allocates memory for an array of elements, initializing all bytes to zero.
| arena | Pointer to the Arena structure. |
| num | Number of elements to allocate. |
| size | Size of each element. |
| 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.
| arena | Pointer to the Arena structure. |
| tag | The tag value to collect. |
| int arena_destroy | ( | Arena * | arena | ) |
| void arena_dump | ( | Arena * | arena, |
| FILE * | f | ||
| ) |
Dumps the raw memory of the arena to the given file stream.
| arena | Pointer to the Arena structure. |
| f | File stream to write the memory dump to. |
| 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.
| arena | Pointer to the Arena structure. |
| p | Pointer to the memory block to free. |
| 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.
| arena | Pointer to the Arena structure. |
| block | Pointer to the ArenaBlock to free. |
| 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.
| arena | Pointer to the Arena structure. |
| p | Pointer to the memory block. |
| 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.
| arena | Pointer to the Arena structure. |
| tag | The tag value to search for. |
| n | The index of the block to retrieve. |
| 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.
| arena | Pointer to the Arena structure. |
| tag | The tag value to search for. |
| n | The index of the block to retrieve. |
| 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.
| arena | Pointer to the Arena structure. |
| p | Pointer to the memory block. |
| Arena * arena_init | ( | size_t | size, |
| size_t | maxBlocks, | ||
| int | managed | ||
| ) |
Initializes an Arena with a given size.
| size | The total size of the arena to allocate. |
| maxBlocks | The maximum number of blocks that can be allocated at once. |
| managed | If non-zero, the arena will manage blocks, allowing for freeing and dynamic reallocation. |
| void * arena_malloc | ( | Arena * | arena, |
| size_t | size | ||
| ) |
Allocates a block of memory of the specified size within the arena.
| arena | Pointer to the Arena structure. |
| size | Size of the memory block to allocate. |
| 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.
| arena | Pointer to the Arena structure. |
| void * arena_realloc | ( | Arena * | arena, |
| void * | p, | ||
| size_t | size | ||
| ) |
Reallocates a block of memory to a new size within the arena.
| arena | Pointer to the Arena structure. |
| p | Pointer to the existing memory block. |
| size | New size for the memory block. |
| 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.
| arena | Pointer to the Arena structure. |
| p | Pointer to the memory block. |
| tag | The tag value to set. |
| const char * arena_version | ( | void | ) |
Retrieves the version of the arena library.