https://github.com/superpuero/kawa_arena_allocator
Fast, single-header and modern, allocator solution for c++ 17
https://github.com/superpuero/kawa_arena_allocator
arena arena-allocator cpp cpp17 game-development memory
Last synced: about 1 year ago
JSON representation
Fast, single-header and modern, allocator solution for c++ 17
- Host: GitHub
- URL: https://github.com/superpuero/kawa_arena_allocator
- Owner: superPuero
- License: mit
- Created: 2025-06-23T12:13:20.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-06-23T13:34:31.000Z (about 1 year ago)
- Last Synced: 2025-06-23T13:44:17.691Z (about 1 year ago)
- Topics: arena, arena-allocator, cpp, cpp17, game-development, memory
- Language: C++
- Homepage:
- Size: 16.6 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# π kawa::arena\_allocator



**kawa::arena\_allocator** is a **single-header**, *zero-overhead* arena (stack) allocator for modern C++.
It delivers lightning-fast **push / pop** semantics, automatic pointer
alignment, and a handly RAII **scoped** helper β no exceptions, no surprises.
---
## π§ Building & Integrating
1. Add `arena_allocator.h` to your include path.
2. Compile with **`-std=c++17`** (or `/std:c++17` on MSVC).
There are no other dependencies.
---
## β¨ Features
* **O(1) push / pop** for trivially predictable performance.
* **Alignmentβaware**: uses `std::align` internally β no UB.
* **`scoped` RAII helper** β automatic rollback of nested allocations.
* **Debugβfriendly**: assertions & platformβspecific `debugbreak()` in `_DEBUG` builds.
* Works with **C++17** and later (GCC / Clang / MSVC).
---
## π Quick Start
```cpp
#include "arena_allocator.h"
constexpr std::size_t BYTES = 1024; // 1 KiB
constexpr std::size_t ENTRIES = 32; // 32 push ops max
kawa::arena_allocator arena{BYTES, ENTRIES};
auto* i = arena.push(); // typed push (default ctor)
auto* v = arena.push>(10, 42); // ctor with (10, 42) as args
void* raw = arena.push(64); // untyped push (64 bytes)
arena.pop(); // β raw block (no dtor)
arena.pop(); // β vector (dtor is called because vector is not trivially-destructable!)
```
### Scoped guard usage
```cpp
{
kawa::arena_allocator::scoped scope = arena.scope();
auto* big = scope.push();
scope.push(256); // scratch buffer
// automatically rolled back when guard goes out of scope (calls destructors too)
} // β all allocations in scope are popped here
```
---
## π API Overview
| Member | Notes |
| ----------------------------------------------- | ---------------------------------------------------------|
| `arena_allocator(size_t bytes, size_t entries)` | create arena |
| `T* push(Args&&...)` | reserve & in place construction of T with provided args |
| `void* push(size_t bytes)` | raw memory block |
| `void pop()` | Pops the last push (LIFO) and calls destructor if needed |
| `arena_appocator::scoped scope()` | returns RAII guard for automatic roll-back |
| `size_t capacity() const` | total bytes available |
| `size_t occupied() const` | current bytes in use |
> **Note**: `pop()` calls the destructor of nonβtrivially destructible types.
> For raw memory allocations (`push(size)`), no destructor is called.
---
```
βββββββββββββ arena_allocator βββββββββββββ
β _data β
β βΌ β
β [ used ][ free ................. ] β β raw buffer (LIFO)
β β² β
β _current β
β β
β _entries β [16][32][8] β¦ β β stride stack
βββββββββββββββββββββββββββββββββββββββββββ
```
---