A lightweight, single-header C++ arena allocator. Simply drop arena.hpp into your project's include directory.
For a detailed demonstration see example.cpp
#include <cstdint>
#define ARENA_IMPLEMENTATION
#include "arena.hpp"
#include <cstdlib>
#include <print>
struct app {
char name[16];
uint16_t id;
};
int main(int argc, char *argv[]) {
void *mem = malloc(6500);
Arena a = {0};
arena_init(&a, mem, 6500);
int *x;
app *info;
x = (int *)arena_alloc(&a, sizeof(int));
info = (app *)arena_alloc(&a, sizeof(app));
*x = 5;
*info = {.name = "some text\0", .id = 1235};
std::println("{:p}: {:d}", static_cast<const void *>(x), (int)a.buffer[0]);
std::println("{:p}: {:s}\t{:d}", static_cast<const void *>(info), info->name,
info->id);
std::println("{}", a.offset);
arena_reset(&a);
//do something else with the memory
arena_free(&a);
free(mem);
return 0;
}This arena allocator implementation is based on the concepts and implementation described in Ginger Bill's article "Memory Allocation Strategies - Part 2: Linear (Arena) Allocators."
Original article: https://www.gingerbill.org/article/2019/02/08/memory-allocation-strategies-002/