Compare commits

..

4 Commits

Author SHA1 Message Date
CHatingPython 100c542fe1 refactor: serialize types in module
Refs: #23
2026-07-04 16:15:40 +02:00
CHatingPython cf284592fd fix: fix handle's copy constructors 2026-07-04 16:14:46 +02:00
CHatingPython f5ebc767ab fix: fix module's function serialization
Refs: #23
2026-07-04 13:20:08 +02:00
CHatingPython 484e16963f refactor: move thing allocator to a separate file
Closes: #44
2026-07-03 19:51:09 +02:00
6 changed files with 176 additions and 117 deletions
+1
View File
@@ -7,6 +7,7 @@
#include "furvm/handle.hpp"
#include "furvm/module.hpp" // IWYU pragma: keep
#include "furvm/thing.hpp" // IWYU pragma: keep
#include "furvm/thing_allocator.hpp"
#include <cstddef>
#include <utility>
+23
View File
@@ -148,8 +148,10 @@ public:
*/
handle(const handle& other)
: m_value(other.m_value) {
if constexpr (detail::header_has_refcount_v<Header>) {
m_value->first.acquire();
}
}
/**
* @brief Copy constructor.
@@ -157,7 +159,9 @@ public:
handle& operator=(const handle& other) {
if (this == &other) return *this;
m_value = other.m_value;
if constexpr (detail::header_has_refcount_v<Header>) {
m_value->first.acquire();
}
return *this;
}
public:
@@ -308,6 +312,17 @@ public:
delete it->second;
m_pairs.erase(it);
}
/**
* @brief Checks whether a handle exists inside.
*
* @param id Identifier of the handle.
* @return true if the handle exists insdie of this container.
*/
template <typename IdFwd>
constexpr bool contains(IdFwd&& id) const {
return m_pairs.find(std::forward<IdFwd>(id)) != m_pairs.end();
}
private:
std::unordered_map<id_type, pair_type*> m_pairs;
};
@@ -395,6 +410,14 @@ public:
delete m_pairs[id];
m_pairs[id] = nullptr;
}
/**
* @brief Checks whether a handle exists inside.
*
* @param id Identifier of the handle.
* @return true if the handle exists insdie of this container.
*/
constexpr bool contains(id_type id) const { return id < m_pairs.size() && m_pairs[id] != nullptr; }
public:
auto begin() { return m_pairs.begin(); }
auto begin() const { return m_pairs.begin(); }
+5 -104
View File
@@ -1,7 +1,6 @@
#ifndef FURVM_THING_HPP
#define FURVM_THING_HPP
#include "furlang/arena.hpp"
#include "furvm/exceptions.hpp"
#include "furvm/fwd.hpp"
#include "furvm/module.hpp"
@@ -102,7 +101,7 @@ public:
std::memcpy(res.m_data, m_data, m_size);
} break;
case type_t::List: {
copy_list(resolve_type(m_type->list, m_modules), res.get<list_t>(), get<list_t>());
copy_list(resolve_type(*m_type->list, m_modules), res.get<list_t>(), get<list_t>());
} break;
case type_t::Import: throw std::runtime_error("unreachable");
}
@@ -275,10 +274,10 @@ public:
if (m_type->t != type_t::List) throw bad_thing_access();
auto& list = get<list_t>();
if (newSize < 0 || newSize == list.size) return;
std::byte* newData = new std::byte[compute_size_na(resolve_type(m_type->list, m_modules)) * newSize];
std::byte* newData = new std::byte[compute_size_na(resolve_type(*m_type->list, m_modules)) * newSize];
std::memcpy(newData,
list.data,
compute_size_na(resolve_type(m_type->list, m_modules)) * std::min(list.size, newSize));
compute_size_na(resolve_type(*m_type->list, m_modules)) * std::min(list.size, newSize));
list.size = newSize;
delete[] list.data;
list.data = newData;
@@ -288,7 +287,7 @@ public:
if (m_type->t != type_t::List) throw bad_thing_access();
auto& list = get<list_t>();
if (index < 0 || index >= list.size) throw std::out_of_range("index out of range");
thing res = { m_type->list, m_modules, m_allocator };
thing res = { *m_type->list, m_modules, m_allocator };
res.get<reference_t>() = list.data; // TODO: Account for padding, alignment and stuff
return std::move(res);
}
@@ -315,7 +314,7 @@ private:
case type_t::Reference: std::memcpy(dst.data, src.data, size); break;
case type_t::List:
for (std::size_t i = 0; i < size; ++i) {
copy_list(innerType->list,
copy_list(*innerType->list,
*std::launder(reinterpret_cast<list_t*>(dst.data)),
*std::launder(reinterpret_cast<list_t*>(src.data)));
}
@@ -373,104 +372,6 @@ private:
allocator_type m_allocator;
};
template <typename T>
class thing_allocator {
template <typename>
friend class thing_allocator;
using dead_things = std::vector<std::pair<T*, std::size_t>>;
public:
using value_type = T; /**< Value type. */
public:
/**
* @brief Constructs a thing allocator.
*
* @param arena Base arena allocator.
*/
explicit thing_allocator(furlang::arena& arena) noexcept
: m_arena(&arena), m_deadThings(std::make_shared<dead_things>()) {}
/**
* @brief Move constructor.
*/
template <typename U>
thing_allocator(thing_allocator<U>&& other) noexcept
: m_arena(std::move(other.m_arena)), m_deadThings(std::move(other.m_deadThings)) {}
/**
* @brief Move constructor.
*/
template <typename U>
thing_allocator& operator=(thing_allocator<U>&& other) noexcept {
if (this == &other) return *this;
m_arena = std::move(other.m_arena);
m_deadThings = std::move(other.m_deadThings);
return *this;
}
/**
* @brief Copy constructor.
*/
template <typename U>
thing_allocator(const thing_allocator<U>& other) noexcept
: m_arena(other.m_arena), m_deadThings(other.m_deadThings) {}
/**
* @brief Copy constructor.
*/
template <typename U>
thing_allocator& operator=(const thing_allocator<U>& other) noexcept {
if (this == &other) return *this;
m_arena = other.m_arena;
m_deadThings = other.m_deadThings;
return *this;
}
public:
/**
* @brief Returns a free chunk of memory.
*
* @param count Count of the things that must fit inside the chunk.
* @return The chunk.
*/
[[nodiscard]] T* allocate(std::size_t count = 1) {
for (auto it = m_deadThings->begin(); it != m_deadThings->end(); ++it) {
if (it->second != count) continue;
m_deadThings->erase(it);
return it->first;
}
return m_arena->allocate<T>(count);
}
/**
* @brief Recycles the pointer.
*/
void deallocate(T* ptr, std::size_t count) noexcept { m_deadThings->emplace_back(ptr, count); }
public:
/**
* @brief Compares two thing allocators for equality.
*
* @return true if the two things are equal.
*/
template <typename U>
bool operator==(const thing_allocator<U>& other) const noexcept {
return m_arena == other.m_arena && m_deadThings == other.m_deadThings;
}
/**
* @brief Compares two thing allocators for inequality.
*
* @return true if the two things are not equal.
*/
template <typename U>
bool operator!=(const thing_allocator<U>& other) const noexcept {
return m_arena != other.m_arena || m_deadThings != other.m_deadThings;
}
private:
furlang::arena* m_arena;
std::shared_ptr<dead_things> m_deadThings;
};
} // namespace furvm
#endif // FURVM_THING_HPP
+110
View File
@@ -0,0 +1,110 @@
#ifndef FURVM_THING_ALLOCATOR_HPP
#define FURVM_THING_ALLOCATOR_HPP
#include "furlang/arena.hpp"
#include <vector>
namespace furvm {
template <typename T>
class thing_allocator {
template <typename>
friend class thing_allocator;
using dead_things = std::vector<std::pair<T*, std::size_t>>;
public:
using value_type = T; /**< Value type. */
public:
/**
* @brief Constructs a thing allocator.
*
* @param arena Base arena allocator.
*/
explicit thing_allocator(furlang::arena& arena) noexcept
: m_arena(&arena), m_deadThings(std::make_shared<dead_things>()) {}
/**
* @brief Move constructor.
*/
template <typename U>
thing_allocator(thing_allocator<U>&& other) noexcept
: m_arena(std::move(other.m_arena)), m_deadThings(std::move(other.m_deadThings)) {}
/**
* @brief Move constructor.
*/
template <typename U>
thing_allocator& operator=(thing_allocator<U>&& other) noexcept {
if (this == &other) return *this;
m_arena = std::move(other.m_arena);
m_deadThings = std::move(other.m_deadThings);
return *this;
}
/**
* @brief Copy constructor.
*/
template <typename U>
thing_allocator(const thing_allocator<U>& other) noexcept
: m_arena(other.m_arena), m_deadThings(other.m_deadThings) {}
/**
* @brief Copy constructor.
*/
template <typename U>
thing_allocator& operator=(const thing_allocator<U>& other) noexcept {
if (this == &other) return *this;
m_arena = other.m_arena;
m_deadThings = other.m_deadThings;
return *this;
}
public:
/**
* @brief Returns a free chunk of memory.
*
* @param count Count of the things that must fit inside the chunk.
* @return The chunk.
*/
[[nodiscard]] T* allocate(std::size_t count = 1) {
for (auto it = m_deadThings->begin(); it != m_deadThings->end(); ++it) {
if (it->second != count) continue;
m_deadThings->erase(it);
return it->first;
}
return m_arena->allocate<T>(count);
}
/**
* @brief Recycles the pointer.
*/
void deallocate(T* ptr, std::size_t count) noexcept { m_deadThings->emplace_back(ptr, count); }
public:
/**
* @brief Compares two thing allocators for equality.
*
* @return true if the two things are equal.
*/
template <typename U>
bool operator==(const thing_allocator<U>& other) const noexcept {
return m_arena == other.m_arena && m_deadThings == other.m_deadThings;
}
/**
* @brief Compares two thing allocators for inequality.
*
* @return true if the two things are not equal.
*/
template <typename U>
bool operator!=(const thing_allocator<U>& other) const noexcept {
return m_arena != other.m_arena || m_deadThings != other.m_deadThings;
}
private:
furlang::arena* m_arena;
std::shared_ptr<dead_things> m_deadThings;
};
} // namespace furvm
#endif // FURVM_THING_ALLOCATOR_HPP
+5 -9
View File
@@ -2,10 +2,9 @@
#define FURVM_TYPE_HPP
#include "furvm/fwd.hpp"
#include "furvm/handle.hpp" // IWYU pragma: keep
#include <cstdint>
#include <memory>
#include <utility>
namespace furvm {
@@ -18,8 +17,8 @@ enum class type_t : std::uint32_t {
};
using primitive_type = std::uint64_t;
using reference_type = std::shared_ptr<type>;
using list_type = std::shared_ptr<type>;
using reference_type = type_p;
using list_type = type_h;
struct import_type {
mod_id mod;
@@ -44,11 +43,8 @@ struct type {
type(const reference_type& reference)
: t(type_t::Reference), reference(reference) {}
static type make_list(const list_type& list) {
type type(type_t::List);
new (&type.list) list_type(list);
return type;
}
type(const list_type& list)
: t(type_t::List), list(list) {}
type(const import_type& imp)
: t(type_t::Import), imp(imp) {}
+30 -2
View File
@@ -2,8 +2,10 @@
#include "furvm/detail/serialization.hpp"
#include "furvm/fwd.hpp"
#include "furvm/type.hpp"
#include <ios>
#include <stdexcept>
namespace furvm {
@@ -12,8 +14,14 @@ std::ostream& mod::serialize(std::ostream& os) const {
detail::serialize(os, std::uint32_t(0)); // version
detail::serialize(os, function_id(m_functionMap.size()));
for (auto* pair : m_functions) {
function_h func = { pair };
for (function_id id = 0; id < m_functions.cend() - m_functions.cbegin(); ++id) {
if (!m_functions.contains(id)) {
detail::serialize(os, "");
detail::serialize(os, std::uint8_t(0xFF)); // null function
continue;
}
function_h func = m_functions.at(id);
bool isPublic = m_publicFunctions.find(func->name()) != m_publicFunctions.end();
detail::serialize(os, isPublic ? func->name() : ""); // private functions have empty names
detail::serialize(os, std::uint8_t(func->type()));
@@ -29,6 +37,26 @@ std::ostream& mod::serialize(std::ostream& os) const {
}
}
type_id typeCount = type_id(m_types.cend() - m_types.cbegin());
detail::serialize(os, typeCount);
for (type_id id = 0; id < typeCount; ++id) {
if (!m_types.contains(id)) {
detail::serialize(os, std::uint8_t(0xFF)); // null type
continue;
}
auto type = *m_types.at(id);
switch (type->t) {
case type_t::Primitive: detail::serialize(os, type->primitive); break;
case type_t::Reference: throw std::runtime_error("reference type serialization is unimplemented");
case type_t::List: detail::serialize(os, type->list.id()); break;
case type_t::Import: {
detail::serialize(os, type->imp.mod);
detail::serialize(os, type->imp.type);
} break;
}
}
detail::serialize(os, std::uint64_t(m_bytecode.size()));
return os.write(reinterpret_cast<const char*>(m_bytecode.data()), static_cast<std::streamsize>(m_bytecode.size()));
}