#pragma once #include #include #include #include class Arena { public: Arena(size_t size) : buffer(size) { } template size_t Align() const { size_t alignment = alignof(T); return (offset + alignment - 1) & ~(alignment - 1); } template T& Allocate(Args&&... args) { offset = Align(); assert(offset + sizeof(T) <= buffer.size() && "Arena out of memory"); T* ptr = new (&buffer[offset]) T(std::forward(args)...); objects.emplace_back(ptr, [](void* p) { static_cast(p)->~T(); }); offset += sizeof(T); return *ptr; } void Reset() { for (auto& [ptr, dtor] : objects) { dtor(ptr); } objects.clear(); offset = 0; } size_t offset = 0; std::vector buffer; std::vector> objects; };