@@ -94,11 +94,6 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
thing_allocator<std::byte> thing_alloc() const { return m_thingAllocator; }
|
thing_allocator<std::byte> thing_alloc() const { return m_thingAllocator; }
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* @brief Removes unreferenced things from the thing list.
|
|
||||||
*/
|
|
||||||
void collect();
|
|
||||||
private:
|
private:
|
||||||
handle_container<mod_h> m_modules;
|
handle_container<mod_h> m_modules;
|
||||||
handle_container<thing_h> m_things;
|
handle_container<thing_h> m_things;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include "furvm/fwd.hpp"
|
#include "furvm/fwd.hpp"
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <functional>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
@@ -24,20 +25,24 @@ public:
|
|||||||
using id_type = Id;
|
using id_type = Id;
|
||||||
using refcount_type = std::uint32_t;
|
using refcount_type = std::uint32_t;
|
||||||
public:
|
public:
|
||||||
template <typename IdFwd>
|
template <typename IdFwd, typename Func>
|
||||||
refcount_header(IdFwd&& id, refcount_type refCount)
|
refcount_header(IdFwd&& id, refcount_type refCount, Func&& onRelease)
|
||||||
: m_id(std::forward<IdFwd>(id)), m_refCount(refCount) {}
|
: m_id(std::forward<IdFwd>(id)), m_refCount(refCount), m_onRelease(std::forward<Func>(onRelease)) {}
|
||||||
public:
|
public:
|
||||||
refcount_type reference_count() const { return m_refCount; }
|
refcount_type reference_count() const { return m_refCount; }
|
||||||
|
|
||||||
void acquire() { ++m_refCount; }
|
void acquire() { ++m_refCount; }
|
||||||
|
|
||||||
void release() { --m_refCount; }
|
void release() {
|
||||||
|
--m_refCount;
|
||||||
|
if (m_refCount == 0) m_onRelease(m_id);
|
||||||
|
}
|
||||||
public:
|
public:
|
||||||
id_type id() const { return m_id; }
|
id_type id() const { return m_id; }
|
||||||
private:
|
private:
|
||||||
id_type m_id;
|
id_type m_id;
|
||||||
std::atomic<refcount_type> m_refCount;
|
std::atomic<refcount_type> m_refCount;
|
||||||
|
std::function<void(const id_type&)> m_onRelease;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Id>
|
template <typename Id>
|
||||||
@@ -153,7 +158,7 @@ public:
|
|||||||
id_type idFwd = std::forward<IdFwd>(id);
|
id_type idFwd = std::forward<IdFwd>(id);
|
||||||
if (auto it = m_pairs.find(idFwd); it != m_pairs.end()) delete it->second;
|
if (auto it = m_pairs.find(idFwd); it != m_pairs.end()) delete it->second;
|
||||||
auto pair = new pair_type(std::piecewise_construct,
|
auto pair = new pair_type(std::piecewise_construct,
|
||||||
std::forward_as_tuple(idFwd, 0),
|
std::forward_as_tuple(idFwd, 0, [&](const id_type& id) { erase(id); }),
|
||||||
std::forward_as_tuple(std::forward<Args>(args)...));
|
std::forward_as_tuple(std::forward<Args>(args)...));
|
||||||
m_pairs.emplace(std::move(idFwd), pair);
|
m_pairs.emplace(std::move(idFwd), pair);
|
||||||
return { pair };
|
return { pair };
|
||||||
@@ -209,7 +214,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
pair_type* newPair = new pair_type(std::piecewise_construct,
|
pair_type* newPair = new pair_type(std::piecewise_construct,
|
||||||
std::forward_as_tuple(id, 0),
|
std::forward_as_tuple(id, 0, [&](const id_type& id) { erase(id); }),
|
||||||
std::forward_as_tuple(std::forward<Args>(args)...));
|
std::forward_as_tuple(std::forward<Args>(args)...));
|
||||||
|
|
||||||
m_pairs[id] = newPair;
|
m_pairs[id] = newPair;
|
||||||
|
|||||||
Reference in New Issue
Block a user