https://github.com/thehxdev/libarena
Single-header Arena allocator library with multi-buffer support
https://github.com/thehxdev/libarena
allocator c memory memory-allocator
Last synced: 3 months ago
JSON representation
Single-header Arena allocator library with multi-buffer support
- Host: GitHub
- URL: https://github.com/thehxdev/libarena
- Owner: thehxdev
- Created: 2024-12-03T20:57:21.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-02-06T13:13:35.000Z (5 months ago)
- Last Synced: 2025-03-28T04:30:16.225Z (3 months ago)
- Topics: allocator, c, memory, memory-allocator
- Language: C
- Homepage:
- Size: 12.7 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Libarena
Easy to use, cross-platform and Single-header Arena allocator.## API
Application programming interface for `libarena`.### Include `arena.h`
Since the library is single-header, include `arena.h` file in **_JUST ONE SOURCE FILE_** with `LIBARENA_ARENA_IMPLEMENTATION` defined.
If you want Multi-Buffer support, define `LIBARENA_MULTI_BUFFER` too in that source file just **_once_**.
You can include `arena.h` in all source files. But the implementation details MUST be in one file.
```c
// include the implementation
#define LIBARENA_ARENA_IMPLEMENTATION
// enable multi-buffer support
#define LIBARENA_MULTI_BUFFER
#include "arena.h"
```
You can use multi-buffer mode if you want growable buffers. otherwise one fixed buffer will be used.### Construct a new `Arena_t`
Construct a new `Arena_t` instance. This type holds and manages all information about your arena.
`arena_new` function accepts buffer's size. The default size is one page size (4096 bytes).
Since `libarena` uses `mmap` on \*nix systems and `VirtualAlloc` on windows, it's better to use the default size.
```c
// pass 0 to use default size
Arena_t a = arena_new(0);
```### Allocate memory
Allocate on arena and get a pointer.
```c
// allocate 10 integers
int *xs = arena_alloc(&a, sizeof(int) * 10);
```### Clear arena
Clear and reset arena.
```c
arena_clear(&a);
```### Destroy arena
Destroy the arena and deallocate all allocated memory.
```c
arena_destroy(&a);
```