refactor(furvm): use an arena allocator for thing data

Refs: #10
This commit is contained in:
2026-06-10 15:09:35 +02:00
parent 48c6da1134
commit 874d1f14cb
2 changed files with 27 additions and 2 deletions
+23 -2
View File
@@ -5,6 +5,8 @@
#include "furvm/context.hpp" // IWYU pragma: keep
#include <cstdlib>
#include <cstring>
#include <stdexcept>
namespace furvm {
@@ -16,10 +18,29 @@ std::size_t thing_type_size(thing_t type) {
}
thing::thing(rzecz, thing_handle id, thing_t type, const context_p& context)
: m_id(id), m_type(type), m_context(context), m_data(std::calloc(1, thing_type_size(type))) {}
: m_id(id), m_type(type), m_context(context) {
std::size_t size = thing_type_size(type);
std::byte* data = nullptr;
if (!m_context->m_deadThings.empty()) {
thing_t itType{};
for (auto it = m_context->m_deadThings.rbegin(); it != m_context->m_deadThings.rend(); ++it) {
std::memcpy(&itType, static_cast<std::byte*>(*it) - sizeof(itType), sizeof(itType));
if (size == thing_type_size(itType)) {
data = static_cast<std::byte*>(*it);
break;
}
}
}
if (data == nullptr) data = m_context->m_thingArena.allocate<std::byte>(sizeof(type) + size);
if (data == nullptr) throw std::runtime_error("failed to allocate data for new thing");
std::memcpy(data, &type, sizeof(type));
m_data = data + sizeof(type);
std::memset(m_data, 0, size);
}
thing::~thing() {
std::free(m_data);
m_context->m_deadThings.push_back(m_data);
}
thing_p thing::create(const context_p& context, thing_t type) {