Skip to content

Commit

Permalink
Improved GC
Browse files Browse the repository at this point in the history
Switched to gmod's way of handling userdata.  This should stop ARC crashing servers when uint64/int64s are GC'd by collectgarbage
  • Loading branch information
oubliette32 committed Jul 10, 2017
1 parent 65082a7 commit 3d29fe3
Showing 1 changed file with 10 additions and 38 deletions.
48 changes: 10 additions & 38 deletions streams/src/gm_streams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64*>(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<int64*>(ud->data)) = value;
}
#pragma endregion

Expand Down Expand Up @@ -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
///
Expand Down Expand Up @@ -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
///
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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");

Expand Down

0 comments on commit 3d29fe3

Please sign in to comment.