LibArena is a small and efficient C library for memory management using the arena allocation pattern. It is designed to simplify memory tracking, prevent common errors like memory leaks, and provide fine-grained control over allocation lifetimes.
This library is ideal for applications that allocate and deallocate memory in batches, such as compilers, game engines, and other systems where performance and memory control are critical.
- Arena-Based Allocation: Allocate memory blocks that are tracked by a central collector.
- Automatic Cleanup: Free all tracked allocations with a single call.
- Custom Finalizers: Register custom cleanup functions for specific allocations.
- Scoped Allocations: Create allocation checkpoints and roll back to a previous state.
- Safe Reallocation:
arena_reallochandles pointer updates within the arena. - Dynamic Growth: The arena automatically expands its tracking capacity as needed.
- A C compiler (e.g.,
gccorclang) makefor building the library
-
Clone the repository:
git clone https://github.com/your-username/libarena.git cd libarena -
Build the static library:
make
This will create the static library file
libarena.a.
To use LibArena in your project, include the lib_arena.h header and link against the libarena.a library.
Here is a simple example demonstrating how to use the core features of the library:
#include <stdio.h>
#include "lib_arena.h"
// A custom finalizer function
void custom_free(void *data) {
printf("Finalizer called for: %s\n", (char *)data);
}
int main() {
// 1. Create an arena with an initial capacity of 10
arena_t *arena = arena_create(10);
if (!arena) {
return 1;
}
// 2. Allocate memory
char *str1 = arena_alloc(arena, sizeof(char) * 12);
strcpy(str1, "Hello");
// 3. Allocate with a custom finalizer
char *str2 = arena_alloc(arena, sizeof(char) * 12);
strcpy(str2, "World");
arena_set_destructor(arena, str2, custom_free);
printf("Allocated: %s, %s\n", str1, str2);
// 4. Free all memory in the arena
arena_destroy(arena);
return 0;
}To compile this example, save it as example.c and run:
gcc example.c -L. -larena -I./include -o example
./example| Function | Description |
|---|---|
arena_create(capacity) |
Creates a new memory arena. |
arena_alloc(arena, size) |
Allocates a block of memory in the arena. |
arena_realloc(arena, ptr, new_size) |
Reallocates a tracked memory block. |
arena_free(arena, ptr) |
Frees a single tracked allocation. |
arena_destroy(arena) |
Frees all memory in the arena and destroys it. |
| Function | Description |
|---|---|
arena_set_destructor(arena, ptr, fn) |
Registers a custom cleanup function for an allocation. |
arena_snapshot(arena) |
Creates a checkpoint of the current allocation state. |
arena_restore(arena, checkpoint) |
Frees all allocations made after a checkpoint. |
For detailed information, see the Doxygen-style comments in include/lib_arena.h.
This project uses a Makefile with several targets:
make allormake: Compiles the static librarylibarena.a.make clean: Removes temporary object files.make fclean: Removes object files and the static library.make re: Rebuilds the project from scratch.make test: Builds the library and runs the test suite located in thetests/directory.make debug: Builds the library with debug symbols and address sanitizer flags.
This project is licensed under the MIT License. See the LICENSE file for details.