@@ -7,9 +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>
|
||||
#include <vector>
|
||||
|
||||
@@ -22,8 +20,7 @@ public:
|
||||
/**
|
||||
* @brief Constructs a context.
|
||||
*/
|
||||
context()
|
||||
: m_thingAllocator(m_thingArena) {}
|
||||
context() {}
|
||||
|
||||
~context() = default;
|
||||
|
||||
@@ -70,21 +67,13 @@ public:
|
||||
|
||||
const std::vector<executor>& executors() const { return m_executors; }
|
||||
public:
|
||||
/**
|
||||
* @brief Returns context's thing allocator.
|
||||
*
|
||||
* @return The thing allocator.
|
||||
*/
|
||||
thing_allocator<std::byte> thing_alloc() const { return m_thingAllocator; }
|
||||
|
||||
thing_type_store& tt_store() { return m_thingTypeStore; }
|
||||
private:
|
||||
handle_container<mod_h> m_modules;
|
||||
std::vector<executor> m_executors;
|
||||
|
||||
furlang::arena m_thingArena;
|
||||
thing_allocator<std::byte> m_thingAllocator;
|
||||
class thing_type_store m_thingTypeStore;
|
||||
furlang::arena m_thingArena;
|
||||
class thing_type_store m_thingTypeStore;
|
||||
};
|
||||
|
||||
} // namespace furvm
|
||||
|
||||
@@ -140,11 +140,6 @@ using mod_id = std::string;
|
||||
*/
|
||||
using mod_h = handle<mod, refcount_header<mod_id>>;
|
||||
|
||||
// thing_allocator.hpp
|
||||
|
||||
template <typename T>
|
||||
class thing_allocator;
|
||||
|
||||
// thing.hpp
|
||||
|
||||
/**
|
||||
@@ -161,7 +156,7 @@ using thing_type_id = std::uint32_t;
|
||||
*
|
||||
* A stack element. Think of it like of a value in C++ or I guess a class in java.
|
||||
*/
|
||||
template <template <typename> typename Allocator = thing_allocator>
|
||||
template <template <typename> typename Allocator = std::allocator>
|
||||
class thing;
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
#include "furlang/utility/hash.hpp"
|
||||
#include "furvm/exceptions.hpp"
|
||||
#include "furvm/fwd.hpp"
|
||||
#include "furvm/thing_allocator.hpp" // IWYU pragma: keep
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
|
||||
@@ -1,113 +0,0 @@
|
||||
#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:
|
||||
thing_allocator() = default;
|
||||
|
||||
/**
|
||||
* @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;
|
||||
T* data = it->first;
|
||||
m_deadThings->erase(it);
|
||||
return data;
|
||||
}
|
||||
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 = nullptr;
|
||||
|
||||
std::shared_ptr<dead_things> m_deadThings;
|
||||
};
|
||||
|
||||
} // namespace furvm
|
||||
|
||||
#endif // FURVM_THING_ALLOCATOR_HPP
|
||||
Reference in New Issue
Block a user