Add arena to furlang

Signed-off-by: CHatingPython <chatingpython@gmail.com>
This commit is contained in:
CHatingPython
2026-05-25 14:59:42 +02:00
committed by CHatingPython
parent 2cf4ff90a5
commit d2cfbb6763
4 changed files with 128 additions and 0 deletions
+48
View File
@@ -0,0 +1,48 @@
#ifndef FURLANG_ARENA_HPP
#define FURLANG_ARENA_HPP
#include <cstddef>
#include <cstdint>
namespace furlang {
class arena {
private:
struct region {
using value_type = std::uintptr_t;
static region* create(std::size_t capacity);
region* next;
std::size_t capacity;
std::size_t used;
value_type storage[];
std::size_t free() const { return capacity - used; }
};
public:
arena(std::size_t minCapacity = 4 * 1024ULL);
~arena();
arena(arena&& other) noexcept;
arena(const arena&) = delete;
arena& operator=(arena&& other) noexcept;
arena& operator=(const arena&) = delete;
public:
template <typename T>
T* allocate(std::size_t count = 1) {
return reinterpret_cast<T*>(allocate(sizeof(T), count));
}
void reset();
private:
void* allocate(std::size_t size, std::size_t count);
private:
std::size_t m_minCapacity;
region* m_head = nullptr;
region* m_tail = nullptr;
};
} // namespace furlang
#endif // FURLANG_ARENA_HPP