diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-12-10 18:28:34 +0100 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-12-10 18:28:34 +0100 |
commit | 30e073f58659a8b24890c834cb8b342adc8c5feb (patch) | |
tree | 16650b0f1de890f063ea36bf483504600b82d0a4 | |
parent | e4543302fcacf871b319710e925919203f520f32 (diff) | |
download | lanes-30e073f58659a8b24890c834cb8b342adc8c5feb.tar.gz lanes-30e073f58659a8b24890c834cb8b342adc8c5feb.tar.bz2 lanes-30e073f58659a8b24890c834cb8b342adc8c5feb.zip |
DeepFactory counts the number of active Deep objects
-rw-r--r-- | deep_test/deep_test.cpp | 11 | ||||
-rw-r--r-- | src/deep.cpp | 2 | ||||
-rw-r--r-- | src/deep.hpp | 6 |
3 files changed, 19 insertions, 0 deletions
diff --git a/deep_test/deep_test.cpp b/deep_test/deep_test.cpp index 0c7ed0f..dbc0115 100644 --- a/deep_test/deep_test.cpp +++ b/deep_test/deep_test.cpp | |||
@@ -8,6 +8,8 @@ class MyDeepFactory : public DeepFactory | |||
8 | static MyDeepFactory Instance; | 8 | static MyDeepFactory Instance; |
9 | 9 | ||
10 | private: | 10 | private: |
11 | |||
12 | private: | ||
11 | void createMetatable(lua_State* const L_) const override | 13 | void createMetatable(lua_State* const L_) const override |
12 | { | 14 | { |
13 | luaL_getmetatable(L_, "deep"); | 15 | luaL_getmetatable(L_, "deep"); |
@@ -164,6 +166,14 @@ static luaL_Reg const deep_mt[] = { | |||
164 | 166 | ||
165 | // ################################################################################################# | 167 | // ################################################################################################# |
166 | 168 | ||
169 | int luaD_get_deep_count(lua_State* const L_) | ||
170 | { | ||
171 | lua_pushinteger(L_, MyDeepFactory::Instance.getObjectCount()); | ||
172 | return 1; | ||
173 | } | ||
174 | |||
175 | // ################################################################################################# | ||
176 | |||
167 | int luaD_new_deep(lua_State* L) | 177 | int luaD_new_deep(lua_State* L) |
168 | { | 178 | { |
169 | UserValueCount const _nuv{ static_cast<int>(luaL_optinteger(L, 1, 0)) }; | 179 | UserValueCount const _nuv{ static_cast<int>(luaL_optinteger(L, 1, 0)) }; |
@@ -296,6 +306,7 @@ int luaD_new_clonable(lua_State* L) | |||
296 | // ################################################################################################# | 306 | // ################################################################################################# |
297 | 307 | ||
298 | static luaL_Reg const deep_module[] = { | 308 | static luaL_Reg const deep_module[] = { |
309 | { "get_deep_count", luaD_get_deep_count }, | ||
299 | { "new_deep", luaD_new_deep }, | 310 | { "new_deep", luaD_new_deep }, |
300 | { "new_clonable", luaD_new_clonable }, | 311 | { "new_clonable", luaD_new_clonable }, |
301 | { nullptr, nullptr } | 312 | { nullptr, nullptr } |
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_) | |||
131 | void DeepFactory::DeleteDeepObject(lua_State* const L_, DeepPrelude* const o_) | 131 | void DeepFactory::DeleteDeepObject(lua_State* const L_, DeepPrelude* const o_) |
132 | { | 132 | { |
133 | STACK_CHECK_START_REL(L_, 0); | 133 | STACK_CHECK_START_REL(L_, 0); |
134 | o_->factory.deepObjectCount.fetch_sub(1, std::memory_order_relaxed); | ||
134 | o_->factory.deleteDeepObjectInternal(L_, o_); | 135 | o_->factory.deleteDeepObjectInternal(L_, o_); |
135 | STACK_CHECK(L_, 0); | 136 | STACK_CHECK(L_, 0); |
136 | } | 137 | } |
@@ -318,6 +319,7 @@ void DeepFactory::pushDeepUserdata(DestState const L_, UserValueCount const nuv_ | |||
318 | if (_prelude == nullptr) { | 319 | if (_prelude == nullptr) { |
319 | raise_luaL_error(L_, "DeepFactory::newDeepObjectInternal failed to create deep userdata (out of memory)"); | 320 | raise_luaL_error(L_, "DeepFactory::newDeepObjectInternal failed to create deep userdata (out of memory)"); |
320 | } | 321 | } |
322 | deepObjectCount.fetch_add(1, std::memory_order_relaxed); | ||
321 | 323 | ||
322 | if (_prelude->magic != kDeepVersion) { | 324 | if (_prelude->magic != kDeepVersion) { |
323 | // just in case, don't leak the newly allocated deep userdata object | 325 | // 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 | |||
50 | // external C modules should create a single object implementing that interface for each Deep userdata class they want to expose | 50 | // external C modules should create a single object implementing that interface for each Deep userdata class they want to expose |
51 | class DeepFactory | 51 | class DeepFactory |
52 | { | 52 | { |
53 | private: | ||
54 | // the current count of deep object instances | ||
55 | mutable std::atomic<int> deepObjectCount{}; | ||
56 | |||
53 | protected: | 57 | protected: |
54 | // protected non-virtual destructor: Lanes won't manage the Factory's lifetime | 58 | // protected non-virtual destructor: Lanes won't manage the Factory's lifetime |
55 | DeepFactory() = default; | 59 | DeepFactory() = default; |
@@ -80,6 +84,8 @@ class DeepFactory | |||
80 | // NVI: public interface | 84 | // NVI: public interface |
81 | static void DeleteDeepObject(lua_State* L_, DeepPrelude* o_); | 85 | static void DeleteDeepObject(lua_State* L_, DeepPrelude* o_); |
82 | [[nodiscard]] | 86 | [[nodiscard]] |
87 | int getObjectCount() const { return deepObjectCount.load(std::memory_order_relaxed); } | ||
88 | [[nodiscard]] | ||
83 | static bool IsDeepUserdata(lua_State* const L_, StackIndex const idx_); | 89 | static bool IsDeepUserdata(lua_State* const L_, StackIndex const idx_); |
84 | [[nodiscard]] | 90 | [[nodiscard]] |
85 | static DeepFactory* LookupFactory(lua_State* L_, StackIndex index_, LookupMode mode_); | 91 | static DeepFactory* LookupFactory(lua_State* L_, StackIndex index_, LookupMode mode_); |