Skip to content

Commit

Permalink
tweak(scripting/lua): remove custom allocator
Browse files Browse the repository at this point in the history
Upstream rpmalloc has received a few fixes since our implementation was
worked on. Revert back to using the (slow) CRT malloc implementation.
  • Loading branch information
gottfriedleibniz committed Apr 30, 2024
1 parent eb1e6d8 commit 4aa380e
Show file tree
Hide file tree
Showing 8 changed files with 2 additions and 142 deletions.
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,6 @@
path = vendor/lmprof
url = https://github.com/citizenfx/lmprof.git
branch = grit
[submodule "vendor/rpmalloc"]
path = vendor/rpmalloc
url = https://github.com/citizenfx/rpmalloc.git
branch = lua
[submodule "vendor/wil"]
path = vendor/wil
url = https://github.com/microsoft/wil.git
Expand Down
3 changes: 1 addition & 2 deletions code/components/citizen-scripting-lua/component.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"dependencies": [
"fx[2]",
"citizen:scripting:core",
"vendor:lua",
"vendor:rpmalloc"
"vendor:lua"
],
"provides": []
}
31 changes: 0 additions & 31 deletions code/components/citizen-scripting-lua/include/LuaScriptRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,41 +98,15 @@ struct PointerField
PointerFieldEntry data[64];
};

#if LUA_VERSION_NUM >= 504 && defined(_WIN32)
#define LUA_USE_RPMALLOC
#endif

class LuaStateHolder
{
private:
lua_State* m_state;

#if defined(LUA_USE_RPMALLOC)
/// <summary>
/// Create a lua_State instance with a rpmalloc allocator.
/// </summary>
static lua_State* lua_rpmalloc_state(void*& opaque);

/// <summary>
/// Free/Dispose any additional resources associated with the Lua state.
/// </summary>
static void lua_rpmalloc_free(void* opaque);

/// <summary>
/// Reference to the heap_t pointer. At the time of destruction lua_getallocf
/// may point to the profiler allocator hook.
/// </summary>
void* rpmalloc_data = nullptr;
#endif

public:
LuaStateHolder()
{
#if defined(LUA_USE_RPMALLOC)
m_state = lua_rpmalloc_state(rpmalloc_data);
#else
m_state = luaL_newstate();
#endif
#if LUA_VERSION_NUM >= 504
lua_gc(m_state, LUA_GCGEN, 0, 0); /* GC in generational mode */
#endif
Expand All @@ -148,11 +122,6 @@ class LuaStateHolder
if (m_state)
{
lua_close(m_state);

#if defined(LUA_USE_RPMALLOC)
lua_rpmalloc_free(rpmalloc_data);
rpmalloc_data = nullptr;
#endif
m_state = nullptr;
}
}
Expand Down
68 changes: 0 additions & 68 deletions code/components/citizen-scripting-lua/src/LuaScriptRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,74 +93,6 @@ static IScriptHost* g_lastScriptHost;
uint64_t g_tickTime;
bool g_hadProfiler;

#if defined(LUA_USE_RPMALLOC)
#include "rpmalloc/rpmalloc.h"

// Global static object handling rpmalloc initializing and finalizing
struct rpmalloc_GlobalGuard
{
rpmalloc_GlobalGuard() { rpmalloc_initialize(); }
~rpmalloc_GlobalGuard() { rpmalloc_finalize(); }
} static rp_global_guard;

lua_State* fx::LuaStateHolder::lua_rpmalloc_state(void*& opaque)
{
static auto lua_rpmalloc = [](void* ud, void* ptr, size_t osize, size_t nsize) -> void*
{
heap_t* heap = static_cast<heap_t*>(ud);
if (nsize == 0)
{
rpmalloc_heap_free(heap, ptr);
return NULL;
}
else if (ptr == NULL)
{
return rpmalloc_heap_aligned_alloc(heap, 16, nsize);
}
else
{
return rpmalloc_heap_aligned_realloc(heap, ptr, 16, nsize, osize, 0);
}
};

static auto lua_panic = [](lua_State* L) -> int
{
const char* msg = lua_tostring(L, -1);
if (msg == NULL)
{
msg = "error object is not a string";
}

lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", msg);
return 0; /* return to Lua to abort */
};

heap_t* rpmalloc_heap = rpmalloc_heap_acquire();
if (rpmalloc_heap != nullptr)
{
lua_State* L = lua_newstate(lua_rpmalloc, static_cast<void*>(rpmalloc_heap));
if (L != nullptr)
{
opaque = static_cast<void*>(rpmalloc_heap);
lua_atpanic(L, lua_panic);
}
else
{
rpmalloc_heap_release(rpmalloc_heap);
}
return L;
}
return nullptr;
}

void fx::LuaStateHolder::lua_rpmalloc_free(void* opaque)
{
rpmalloc_heap_free_all(static_cast<heap_t*>(opaque));
rpmalloc_heap_release(static_cast<heap_t*>(opaque));
}

#endif

// luaL_openlibs version without io/os libs
static const luaL_Reg lualibs[] = {
{ "_G", luaopen_base },
Expand Down
3 changes: 1 addition & 2 deletions code/components/citizen-scripting-lua54/component.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"dependencies": [
"fx[2]",
"citizen:scripting:core",
"vendor:lua54",
"vendor:rpmalloc"
"vendor:lua54"
],
"provides": []
}
1 change: 0 additions & 1 deletion code/vendor/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ vendor_component 'curl-static'
vendor_component 'lua'
vendor_component 'lua54'
vendor_component 'lua54-noglm'
vendor_component 'rpmalloc'
vendor_component 'leveldb'
vendor_component 'rocksdb'
vendor_component 'botan'
Expand Down
33 changes: 0 additions & 33 deletions code/vendor/rpmalloc.lua

This file was deleted.

1 change: 0 additions & 1 deletion vendor/rpmalloc
Submodule rpmalloc deleted from 94f4f4

0 comments on commit 4aa380e

Please sign in to comment.