diff --git a/furvm/include/furvm/context.hpp b/furvm/include/furvm/context.hpp index e48c673..f9ae6ae 100644 --- a/furvm/include/furvm/context.hpp +++ b/furvm/include/furvm/context.hpp @@ -93,6 +93,11 @@ public: * @return The module count. */ constexpr size_t size() const { return m_modules.size(); } +public: + /** + * @brief Removes unreferenced things from the thing list. + */ + void collect(); private: std::vector m_modules; std::vector m_things; diff --git a/furvm/src/context.cpp b/furvm/src/context.cpp index f1b3877..6dcb1c4 100644 --- a/furvm/src/context.cpp +++ b/furvm/src/context.cpp @@ -1,7 +1,16 @@ #include "furvm/context.hpp" +#include "furvm/thing.hpp" // IWYU pragma: keep + namespace furvm { context::context() {} +void context::collect() { + for (auto& ref : m_things) { + if (ref->reference_count() != 0) continue; + ref = nullptr; + } +} + } // namespace furvm \ No newline at end of file diff --git a/furvm/src/main.cpp b/furvm/src/main.cpp index 9f1d18c..d09fff2 100644 --- a/furvm/src/main.cpp +++ b/furvm/src/main.cpp @@ -25,8 +25,12 @@ int main(void) { auto executor = furvm::executor::create(context); executor->push_frame(mainModule, 0); + static constexpr std::size_t FPC = 3; // Frames per collection + + std::size_t count = 0; while ((executor->flags() & furvm::executor_flags::Done) != furvm::executor_flags::Done) { executor->step(); + if ((++count % FPC) == 0) context->collect(); } return 0;