From 30e073f58659a8b24890c834cb8b342adc8c5feb Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Tue, 10 Dec 2024 18:28:34 +0100 Subject: DeepFactory counts the number of active Deep objects --- src/deep.cpp | 2 ++ src/deep.hpp | 6 ++++++ 2 files changed, 8 insertions(+) (limited to 'src') diff --git a/src/deep.cpp b/src/deep.cpp index 398e13f..bc0b73c 100644 --- a/src/deep.cpp +++ b/src/deep.cpp @@ -131,6 +131,7 @@ int DeepFactory::DeepGC(lua_State* const L_) void DeepFactory::DeleteDeepObject(lua_State* const L_, DeepPrelude* const o_) { STACK_CHECK_START_REL(L_, 0); + o_->factory.deepObjectCount.fetch_sub(1, std::memory_order_relaxed); o_->factory.deleteDeepObjectInternal(L_, o_); STACK_CHECK(L_, 0); } @@ -318,6 +319,7 @@ void DeepFactory::pushDeepUserdata(DestState const L_, UserValueCount const nuv_ if (_prelude == nullptr) { raise_luaL_error(L_, "DeepFactory::newDeepObjectInternal failed to create deep userdata (out of memory)"); } + deepObjectCount.fetch_add(1, std::memory_order_relaxed); if (_prelude->magic != kDeepVersion) { // just in case, don't leak the newly allocated deep userdata object diff --git a/src/deep.hpp b/src/deep.hpp index 6bc3b20..8196e66 100644 --- a/src/deep.hpp +++ b/src/deep.hpp @@ -50,6 +50,10 @@ class DeepPrelude // external C modules should create a single object implementing that interface for each Deep userdata class they want to expose class DeepFactory { + private: + // the current count of deep object instances + mutable std::atomic deepObjectCount{}; + protected: // protected non-virtual destructor: Lanes won't manage the Factory's lifetime DeepFactory() = default; @@ -80,6 +84,8 @@ class DeepFactory // NVI: public interface static void DeleteDeepObject(lua_State* L_, DeepPrelude* o_); [[nodiscard]] + int getObjectCount() const { return deepObjectCount.load(std::memory_order_relaxed); } + [[nodiscard]] static bool IsDeepUserdata(lua_State* const L_, StackIndex const idx_); [[nodiscard]] static DeepFactory* LookupFactory(lua_State* L_, StackIndex index_, LookupMode mode_); -- cgit v1.2.3-55-g6feb