Some lua state allocator fixes

Run GC on the themeroom lua states, as they're not freed
until end of game.

Allocate the exact amount of data we use instead of padding it.

Free our state data after closing the lua state; doing it
the other way is the way to madness, which was kept at bay
by the padded allocation amount.
This commit is contained in:
Pasi Kallinen
2024-01-17 17:01:36 +02:00
parent 6b8079a16f
commit 5d28e24477
2 changed files with 11 additions and 16 deletions

View File

@@ -335,6 +335,7 @@ makerooms(void)
wallification(1, 0, COLNO - 1, ROWNO - 1); wallification(1, 0, COLNO - 1, ROWNO - 1);
free(gc.coder); free(gc.coder);
gc.coder = NULL; gc.coder = NULL;
lua_gc(themes, LUA_GCCOLLECT);
} }
} }

View File

@@ -2146,9 +2146,9 @@ nhl_done(lua_State *L)
nud->name, (long unsigned) nud->inuse); nud->name, (long unsigned) nud->inuse);
} }
} }
lua_close(L);
if (nud) if (nud)
nhl_alloc(NULL, nud, 0, 0); // free nud nhl_alloc(NULL, nud, 0, 0); // free nud
lua_close(L);
} }
iflags.in_lua = FALSE; iflags.in_lua = FALSE;
} }
@@ -2250,7 +2250,7 @@ RESTORE_WARNING_CONDEXPR_IS_CONSTANT
* PMEM memory in use after lua_pcall returns * PMEM memory in use after lua_pcall returns
* SID a small integer identifying the Lua VM instance * SID a small integer identifying the Lua VM instance
* TAG a string from the nhl_luapcall call * TAG a string from the nhl_luapcall call
* DATA memory: rough number of bytes in use by the VM(see NHL_ALLOC_ADJUST) * DATA memory: rough number of bytes in use by the VM
* steps: rough number of steps by the VM(see NHL_SB_STEPSIZE) * steps: rough number of steps by the VM(see NHL_SB_STEPSIZE)
*/ */
#ifdef NHL_SANDBOX #ifdef NHL_SANDBOX
@@ -2726,37 +2726,32 @@ UNSAFEIO:
RESTORE_WARNING_CONDEXPR_IS_CONSTANT RESTORE_WARNING_CONDEXPR_IS_CONSTANT
/*
* All we can do is approximate the amount of storage used. Every allocator
* has different overhead and uses that overhead differently. Since we're
* really just trying to prevent egregious use, we default to a minimum
* allocation size of 16 and if you know better about your allocator (and
* it's worth the processing time), it can be overridden.
*/
#ifndef NHL_ALLOC_ADJUST
#define NHL_ALLOC_ADJUST(d) d = (((d) + 15) & ~15)
#endif
static void * static void *
nhl_alloc(void *ud, void *ptr, size_t osize, size_t nsize) nhl_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
{ {
nhl_user_data *nud = ud; nhl_user_data *nud = ud;
if (nsize == 0) { if (nsize == 0) {
if (ptr != NULL) if (ptr != NULL) {
if (nud && nud->memlimit)
nud->inuse -= osize;
free(ptr); free(ptr);
}
return NULL; return NULL;
} }
if (nud && nud->memlimit) { /* this state is size limited */ if (nud && nud->memlimit) { /* this state is size limited */
uint32_t delta = !ptr ? nsize : nsize - osize; uint32_t delta = !ptr ? nsize : nsize - osize;
NHL_ALLOC_ADJUST(delta);
nud->inuse += delta; nud->inuse += delta;
if (nud->inuse > nud->memlimit) if (nud->inuse > nud->memlimit)
return NULL; return NULL;
} }
return re_alloc(ptr, nsize); if (ptr && nsize)
return re_alloc(ptr, nsize);
/* NOTE: (!ptr && nsize && osize) is valid as per lua docs */
return alloc(nsize);
} }
DISABLE_WARNING_UNREACHABLE_CODE DISABLE_WARNING_UNREACHABLE_CODE
@@ -2829,7 +2824,6 @@ nhlL_newstate(nhl_sandbox_info *sbi, const char *name)
nud->flags = sbi->flags; /* save reporting flags */ nud->flags = sbi->flags; /* save reporting flags */
nud->statctr = 0; nud->statctr = 0;
uint32_t sz = sizeof (struct nhl_user_data); uint32_t sz = sizeof (struct nhl_user_data);
NHL_ALLOC_ADJUST(sz);
nud->inuse = sz; nud->inuse = sz;
if (name) { if (name) {