From e47baca4f23db43ec91fbf64d5d06d7c0dbee495 Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 6 Apr 2019 07:39:49 +0800 Subject: *misc --- source/3rd-party/Luax/luax_class.hpp | 42 ++++++++++++++++----------------- source/3rd-party/Luax/luax_class.inl | 9 ++++--- source/3rd-party/Luax/luax_dog.cpp | 0 source/3rd-party/Luax/luax_dog.h | 33 -------------------------- source/3rd-party/Luax/luax_watchdog.cpp | 0 source/3rd-party/Luax/luax_watchdog.h | 33 ++++++++++++++++++++++++++ 6 files changed, 60 insertions(+), 57 deletions(-) delete mode 100644 source/3rd-party/Luax/luax_dog.cpp delete mode 100644 source/3rd-party/Luax/luax_dog.h create mode 100644 source/3rd-party/Luax/luax_watchdog.cpp create mode 100644 source/3rd-party/Luax/luax_watchdog.h (limited to 'source/3rd-party') diff --git a/source/3rd-party/Luax/luax_class.hpp b/source/3rd-party/Luax/luax_class.hpp index c41adbd..e6e0696 100644 --- a/source/3rd-party/Luax/luax_class.hpp +++ b/source/3rd-party/Luax/luax_class.hpp @@ -12,7 +12,7 @@ #include "luax_ref.h" #include "luax_memberref.h" #include "luax_cfunctions.h" -#include "luax_dog.h" +#include "luax_watchdog.h" namespace Luax { @@ -24,8 +24,9 @@ namespace Luax /// LuaxGetFactoryName 获得工厂的类名,同时用来避免注册时错误注册为了singleton,通过编译 /// 时报错避免 /// -#define LUAX_DECL_FACTORY(type) \ +#define LUAX_DECL_FACTORY(type, ...) \ friend class Luax::LuaxState; \ + friend class Luax::LuaxNativeClass; \ static void RegisterLuaxClass(Luax::LuaxState&); \ static void RegisterLuaxPostprocess(Luax::LuaxState&); \ static const char* GetLuaxFactoryName() { return #type; };\ @@ -44,8 +45,9 @@ namespace Luax /// RegisterLuaxClass 注册类的方法和成员,比如枚举、常量等到class table /// LuaxGetSingletonName 获得单例的类名 /// -#define LUAX_DECL_SINGLETON(type) \ +#define LUAX_DECL_SINGLETON(type, ...) \ friend class Luax::LuaxState; \ + friend class Luax::LuaxNativeClass; \ static void RegisterLuaxClass(Luax::LuaxState&); \ static void RegisterLuaxPostprocess(Luax::LuaxState&); \ static const char* GetLuaxSingletonName() { return #type; }; \ @@ -114,13 +116,14 @@ namespace Luax virtual bool PushLuaxRefTable(LuaxState& state) = 0; /// - /// 被LuaxNativeClass实现。 + /// 被LuaxNativeClass实现。保持和释放native资源。 /// virtual void Retain() = 0; virtual void Release() = 0; }; + // TODO: 将公共部分提取出来,不要重复生成代码 //class LuaxNativeClassBase //{ //} @@ -139,12 +142,12 @@ namespace Luax /// 相比较member ref,这个用在实体会被多次被不同其他实体引用的情况,并频繁销毁这些实体, /// 避免lua频繁的调用gc检测。 /// - template void LuaxRetain(LuaxState& state, USERDATA* userdata); + template void LuaxRetain(LuaxState& state, DATATYPE* userdata); /// /// 对userdata减少一个引用在ref table里,以尝试回收userdata。 /// - template void LuaxRelease(LuaxState& state, USERDATA* userdata); + template void LuaxRelease(LuaxState& state, DATATYPE* userdata); /// /// 将userdata push到栈顶,如果没有初始化mUserdata,初始化设置好元表并把初始化好的 @@ -162,8 +165,8 @@ namespace Luax /// /// 这两个函数是native接口。 /// - void Retain() override; - void Release() override; + void Retain() override final; + void Release() override final; #if LUAX_PROFILER // 对堆上创建的实例进行delete保险检查 @@ -201,21 +204,18 @@ namespace Luax //------------------------------------------------------------------------------// // 公共内容 - LUAX_DECL_METHOD(__tostring); - LUAX_DECL_METHOD(_GetClass); - LUAX_DECL_METHOD(_GetClassName); - + static int __tostring (lua_State*); + static int _GetClass (lua_State*); + static int _GetClassName (lua_State*); + // 工厂类相关 - LUAX_DECL_METHOD(__gc); -#if LUAX_ENABLE_NATIVE_EXTEND - LUAX_DECL_METHOD(_ExtendFactory); -#endif - LUAX_DECL_METHOD(_GetRefTable); - LUAX_DECL_METHOD(_New); + static int __gc (lua_State*); + static int _GetRefTable (lua_State*); + static int _New (lua_State*); - // 单例类相关 #if LUAX_ENABLE_NATIVE_EXTEND - LUAX_DECL_METHOD(_ExtendSingleton); + static int _ExtendFactory (lua_State*); + static int _ExtendSingleton (lua_State*); #endif //--------------------------------------------------------------------------------// @@ -244,7 +244,7 @@ namespace Luax /// /// 通过后才能删除 /// - LuaxDog mWatchDog; + LuaxWatchDog mWatchDog; #if LUAX_PROFILER // 托管此对象的虚拟机 diff --git a/source/3rd-party/Luax/luax_class.inl b/source/3rd-party/Luax/luax_class.inl index 95965ff..1d6a89f 100644 --- a/source/3rd-party/Luax/luax_class.inl +++ b/source/3rd-party/Luax/luax_class.inl @@ -95,7 +95,7 @@ namespace Luax if (pdead == nullptr) return; // 堆上创建的实例必须使用Release释放。 - LuaxNativeClass* p = static_cast(pdead); + TYPE* p = static_cast(pdead); assert(p->mSafer); ::operator delete(pdead, size); } @@ -384,6 +384,7 @@ namespace Luax int LuaxNativeClass::__gc(lua_State* L) { LUAX_STATE(L); + TYPE* self = state.GetUserdata(1); assert(self); @@ -391,8 +392,10 @@ namespace Luax std::cout << "Luax: GC<" << TYPE::GetLuaxClassName() << ">\n"; #endif - --self->mWatchDog.mVMRef; - self->LuaxNativeClass::Release(); + if(self->mWatchDog.mVMRef > 0) + --self->mWatchDog.mVMRef; + + self->Release(); return 0; } diff --git a/source/3rd-party/Luax/luax_dog.cpp b/source/3rd-party/Luax/luax_dog.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/source/3rd-party/Luax/luax_dog.h b/source/3rd-party/Luax/luax_dog.h deleted file mode 100644 index f6d95d5..0000000 --- a/source/3rd-party/Luax/luax_dog.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef __LUAX_DOG_H__ -#define __LUAX_DOG_H__ - -#include "luax_config.h" - -namespace Luax -{ - - /// - /// LuaxNativeClass实例的引用计数额watch dog,只有在watch dog通过时才可以delete。 - /// - class LuaxDog - { - public: - LuaxDog() - : mVMRef(0) - , mNativeRef(0) - { - } - - inline operator bool() - { - return mVMRef == 0 && mNativeRef == 0; - } - - uint mVMRef; // 托管给的虚拟机数 - uint mNativeRef; // 本地引用数 - - }; - -} - -#endif \ No newline at end of file diff --git a/source/3rd-party/Luax/luax_watchdog.cpp b/source/3rd-party/Luax/luax_watchdog.cpp new file mode 100644 index 0000000..e69de29 diff --git a/source/3rd-party/Luax/luax_watchdog.h b/source/3rd-party/Luax/luax_watchdog.h new file mode 100644 index 0000000..b07b007 --- /dev/null +++ b/source/3rd-party/Luax/luax_watchdog.h @@ -0,0 +1,33 @@ +#ifndef __LUAX_DOG_H__ +#define __LUAX_DOG_H__ + +#include "luax_config.h" + +namespace Luax +{ + + /// + /// LuaxNativeClass实例的引用计数额watch dog,只有在watch dog通过时才可以delete。 + /// + class LuaxWatchDog + { + public: + LuaxWatchDog() + : mVMRef(0) + , mNativeRef(0) + { + } + + inline operator bool() + { + return mVMRef == 0 && mNativeRef == 0; + } + + uint mVMRef; // 托管给的虚拟机数 + uint mNativeRef; // 本地引用数 + + }; + +} + +#endif \ No newline at end of file -- cgit v1.1-26-g67d0