From 3d29fe3021fd80942a2de0ff9228778425ebce4f Mon Sep 17 00:00:00 2001 From: Big Coder Date: Mon, 10 Jul 2017 16:07:45 +0200 Subject: [PATCH] Improved GC Switched to gmod's way of handling userdata. This should stop ARC crashing servers when uint64/int64s are GC'd by collectgarbage --- streams/src/gm_streams.cpp | 48 ++++++++------------------------------ 1 file changed, 10 insertions(+), 38 deletions(-) diff --git a/streams/src/gm_streams.cpp b/streams/src/gm_streams.cpp index 28fd250..4f13418 100644 --- a/streams/src/gm_streams.cpp +++ b/streams/src/gm_streams.cpp @@ -64,24 +64,26 @@ typedef int int32; #pragma region Utility void PushUInt64(lua_State* state, uint64 value) { - uint64* copy = new uint64(value); - - UserData* ud = (UserData*)LUA->NewUserdata(sizeof(UserData)); - ud->data = copy; + UserData* ud = (UserData*)LUA->NewUserdata(sizeof(UserData) + sizeof(uint64)); + ud->data = ud + 1; ud->type = TYPE_UINT64; + LUA->ReferencePush(g_iUInt64MetaTable); LUA->SetMetaTable(-2); + + *(reinterpret_cast(ud->data)) = value; } void PushInt64(lua_State* state, int64 value) { - int64* copy = new int64(value); - - UserData* ud = (UserData*)LUA->NewUserdata(sizeof(UserData)); - ud->data = copy; + UserData* ud = (UserData*)LUA->NewUserdata(sizeof(UserData) + sizeof(int64)); + ud->data = ud + 1; ud->type = TYPE_INT64; + LUA->ReferencePush(g_iUInt64MetaTable); LUA->SetMetaTable(-2); + + *(reinterpret_cast(ud->data)) = value; } #pragma endregion @@ -410,15 +412,6 @@ int UInt64_ToString(lua_State* state) return 1; } -/// -/// __gc -/// -int UInt64_GC(lua_State* state) -{ - delete ((uint64*)((UserData*)LUA->GetUserdata(1))->data); - return 0; -} - /// /// __unm /// @@ -614,15 +607,6 @@ int Int64_ToString(lua_State* state) return 1; } -/// -/// __gc -/// -int Int64_GC(lua_State* state) -{ - delete ((int64*)((UserData*)LUA->GetUserdata(1))->data); - return 0; -} - /// /// __unm /// @@ -926,12 +910,6 @@ GMOD_MODULE_OPEN() #pragma region UInt64 Meta Methods LUA->CreateTable(); - LUA->PushCFunction(UInt64_GC); - LUA->SetField(-2, "__gc"); - - LUA->PushCFunction(UInt64_ToString); - LUA->SetField(-2, "__tostring"); - LUA->PushCFunction(UInt64_ToString); LUA->SetField(-2, "__tostring"); @@ -970,12 +948,6 @@ GMOD_MODULE_OPEN() #pragma region Int64 Meta Methods LUA->CreateTable(); - LUA->PushCFunction(Int64_GC); - LUA->SetField(-2, "__gc"); - - LUA->PushCFunction(Int64_ToString); - LUA->SetField(-2, "__tostring"); - LUA->PushCFunction(Int64_ToString); LUA->SetField(-2, "__tostring");