13#define ARENA_VERSION "unknown"
28#define ARENA_TAG_NONE -1
33#define ARENA_SUCCESS 0
38#define ARENA_FAILURE -1
43#define ARENA_PTR(arena, block) ((void*) ((char*) arena->mem + block->idx))
48#define ARENA_COPY(arena, dst, src) memcpy(ARENA_PTR(arena, dst), ARENA_PTR(arena, src), src->size)
ArenaBlock * arena_alloc(Arena *arena, size_t size)
Allocates a block of memory of the specified size within the arena.
Definition: arena.c:201
struct arena_block_s ArenaBlock
int arena_destroy(Arena *arena)
Destroys the given Arena, freeing all associated memory.
Definition: arena.c:67
void arena_dump(Arena *arena, FILE *f)
Dumps the raw memory of the arena to the given file stream.
Definition: arena.c:86
ArenaBlock * arena_get_block_by_tag(Arena *arena, int tag, int n)
Retrieves the n-th block with the specified tag.
Definition: arena.c:439
ArenaBlock * arena_free_block(Arena *arena, ArenaBlock *block)
Free the given block of memory and return the next one.
Definition: arena.c:136
ArenaBlock * arena_get_block(Arena *arena, void *p)
Retrieves the ArenaBlock corresponding to the given pointer.
Definition: arena.c:176
Arena * arena_init(size_t size, size_t blockCount, int managed)
Initializes an Arena with a given size.
Definition: arena.c:16
void arena_collect_tag(Arena *arena, int tag)
Frees all memory blocks with the specified tag.
Definition: arena.c:418
int arena_get_tag(Arena *arena, void *p)
Retrieves the tag associated with a memory block.
Definition: arena.c:374
void arena_print(Arena *arena)
Prints a human-readable representation of the arena's blocks to stdout.
Definition: arena.c:95
void * arena_get_ptr_by_tag(Arena *arena, int tag, int n)
Retrieves the pointer to the n-th block with the specified tag.
Definition: arena.c:468
void * arena_realloc(Arena *arena, void *p, size_t size)
Reallocates a block of memory to a new size within the arena.
Definition: arena.c:284
void * arena_calloc(Arena *arena, size_t size, size_t num)
Allocates memory for an array of elements, initializing all bytes to zero.
Definition: arena.c:267
ArenaStatus
ArenaBlock status enum.
Definition: arena.h:19
@ ARENA_STATUS_USED
Definition: arena.h:21
@ ARENA_STATUS_FREE
Definition: arena.h:20
@ ARENA_STATUS_UNDEFINED
Definition: arena.h:22
int arena_free(Arena *arena, void *p)
Frees a block of memory within the arena.
Definition: arena.c:349
int arena_set_tag(Arena *arena, void *p, int tag)
Sets the tag for a memory block.
Definition: arena.c:396
const char * arena_version(void)
Retrieves the version of the arena library.
Definition: arena.c:485
void * arena_malloc(Arena *arena, size_t size)
Allocates a block of memory of the specified size within the arena.
Definition: arena.c:240
Arena structure.
Definition: arena.h:74
size_t idx
The index of the current block within the arena.
Definition: arena.h:78
void * mem
A pointer to the memory block of the arena.
Definition: arena.h:75
size_t size
The size of the memory block in bytes.
Definition: arena.h:79
ArenaBlock * head
A pointer to the head block of the arena.
Definition: arena.h:77
bool managed
A flag indicating whether the arena is managed or not.
Definition: arena.h:81
void * ptr
A pointer to the current position in the memory block.
Definition: arena.h:76
size_t maxBlocks
The maximum number of blocks that can be allocated in the arena.
Definition: arena.h:80
struct arena_block_s * next
A pointer to the next block in the arena.
Definition: arena.h:62
size_t idx
The index of the block within the arena.
Definition: arena.h:58
struct arena_block_s * prev
A pointer to the previous block in the arena.
Definition: arena.h:63
size_t size
The size of the block in bytes.
Definition: arena.h:59
int tag
An optional tag associated with the block.
Definition: arena.h:60
ArenaStatus status
The status of the block (free, used, or undefined).
Definition: arena.h:61