From 0e7460009ef0a08c1070788720728ca7b3737879 Mon Sep 17 00:00:00 2001 From: Indev Date: Mon, 7 Sep 2026 18:03:50 +0300 Subject: [PATCH] Change lua to not use z_zone allocator --- src/lua_script.c | 25 +++++++++++++++---------- src/lua_script.h | 3 +++ src/z_zone.cpp | 2 +- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/lua_script.c b/src/lua_script.c index 0db0bd1e0..432bb511b 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -62,14 +62,6 @@ static lua_CFunction liblist[] = { NULL }; -// Lua asks for memory using this. -static void *LUA_Alloc(void *ud, void *ptr, size_t osize, size_t nsize) -{ - (void)ud; - (void)osize; - return Z_Realloc(ptr, nsize, PU_LUA, NULL); -} - // Panic function Lua calls when there's an unprotected error. // This function cannot return. Lua would kill the application anyway if it did. FUNCNORETURN static int LUA_Panic(lua_State *L) @@ -197,7 +189,7 @@ void LUA_ClearState(void) CONS_Printf(M_GetText("Pardon me while I initialize the Lua scripting interface...\n")); // allocate state - L = lua_newstate(LUA_Alloc, NULL); + L = luaL_newstate(); lua_atpanic(L, LUA_Panic); // open base libraries @@ -373,7 +365,7 @@ void LUA_DumpFile(const char *filename) static void createMathLibState(void) { - mL = lua_newstate(LUA_Alloc, NULL); + mL = luaL_newstate(); lua_atpanic(mL, LUA_Panic); // open only enum lib @@ -1465,3 +1457,16 @@ int Lua_CreateFieldTable(lua_State *L, const char *const lst[]) return luaL_ref(L, LUA_REGISTRYINDEX); } + +size_t LUA_MemoryUsage(void) +{ + size_t usage = 0; + + if (gL) + usage += lua_gc(gL, LUA_GCCOUNT, 0); + + if (mL) + usage += lua_gc(mL, LUA_GCCOUNT, 0); + + return usage; +} diff --git a/src/lua_script.h b/src/lua_script.h index 35dd5b364..2d1209847 100644 --- a/src/lua_script.h +++ b/src/lua_script.h @@ -82,6 +82,9 @@ int Lua_optoption(lua_State *L, int narg, int def, int list_ref); int Lua_CreateFieldTable(lua_State *L, const char *const lst[]); void LUA_HookNetArchive(lua_CFunction archFunc, savebuffer_t *save); +// Returns lua memory usage in kbytes +size_t LUA_MemoryUsage(void); + // Console wrapper void COM_Lua_f(void); diff --git a/src/z_zone.cpp b/src/z_zone.cpp index 3a78c7079..fd93e6de8 100644 --- a/src/z_zone.cpp +++ b/src/z_zone.cpp @@ -664,7 +664,7 @@ static void Command_Memfree_f(void) Z_CheckHeap(-1); CONS_Printf("\x82%s", M_GetText("Memory Info\n")); CONS_Printf(M_GetText("Total heap used : %7s KB\n"), sizeu1(Z_TotalUsage()>>10)); - CONS_Printf(M_GetText("Lua : %7s KB\n"), sizeu1(Z_TagUsage(PU_LUA)>>10)); + CONS_Printf(M_GetText("Lua : %7s KB\n"), sizeu1(LUA_MemoryUsage())); // No longer uses z_zone memory but for consistency keep it here CONS_Printf(M_GetText("Static : %7s KB\n"), sizeu1(Z_TagUsage(PU_STATIC)>>10)); CONS_Printf(M_GetText("Static (sound) : %7s KB\n"), sizeu1(Z_TagUsage(PU_SOUND)>>10)); CONS_Printf(M_GetText("Static (music) : %7s KB\n"), sizeu1(Z_TagUsage(PU_MUSIC)>>10));