diff --git a/src/arena.cpp b/src/arena.cpp new file mode 100644 index 0000000..3358bbd --- /dev/null +++ b/src/arena.cpp @@ -0,0 +1,27 @@ +#include "artic/arena.h" + +#include + +Arena::Arena() : _block_size(4096) { + _data = { malloc(_block_size) }; + _available = _block_size; +} + +Arena::~Arena() { + for (auto& ptr : _data) + free(ptr); +} + +void Arena::grow() { + _block_size *= 2; + _data.push_back( malloc(_block_size) ); + _available = _block_size; +} + +void* Arena::alloc(size_t size) { + while (size > _available) + grow(); + size_t ptr = reinterpret_cast(_data.back()) + _block_size - _available; + _available -= size; + return reinterpret_cast(ptr); +}