From a67bda28409dd893617ef47f6e089fd753d7de78 Mon Sep 17 00:00:00 2001 From: Niklas Haas Date: Thu, 9 Apr 2020 02:18:48 +0200 Subject: lua: wrap existing allocator instead of reimplementing it This is the proper fix for 1e7802. Turns out the solution is dead simple: we can still set the allocator with lua_getallocf / lua_setalloc. This commit makes memory accounting work on luajit as well. --- player/lua.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'player') diff --git a/player/lua.c b/player/lua.c index abc270900b..0b7a1a9489 100644 --- a/player/lua.c +++ b/player/lua.c @@ -90,6 +90,8 @@ struct script_ctx { struct mpv_handle *client; struct MPContext *mpctx; size_t lua_malloc_size; + lua_Alloc lua_allocf; + void *lua_alloc_ud; struct stats_ctx *stats; }; @@ -159,8 +161,9 @@ static void steal_node_alloctions(void *tmp, mpv_node *node) talloc_steal(tmp, node_get_alloc(node)); } -#ifndef HAVE_LUAJIT -// lua_Alloc compatible. Serves only to retrieve memory usage. +// lua_Alloc compatible. Serves only to track memory usage. This wraps the +// existing allocator, partly because luajit requires the use of its internal +// allocator on 64-bit platforms. static void *mp_lua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) { struct script_ctx *ctx = ud; @@ -169,21 +172,15 @@ static void *mp_lua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) if (!ptr) osize = 0; - if (nsize) { - ptr = realloc(ptr, nsize); - if (!ptr) - return NULL; - } else { - free(ptr); - ptr = NULL; - } + ptr = ctx->lua_allocf(ctx->lua_alloc_ud, ptr, osize, nsize); + if (nsize && !ptr) + return NULL; // allocation failed, so original memory left untouched ctx->lua_malloc_size = ctx->lua_malloc_size - osize + nsize; stats_size_value(ctx->stats, "mem", ctx->lua_malloc_size); return ptr; } -#endif static struct script_ctx *get_ctx(lua_State *L) { @@ -436,17 +433,16 @@ static int load_lua(struct mp_script_args *args) goto error_out; } -#if HAVE_LUAJIT - // luajit forces the use of its internal allocator, at least on 64-bit lua_State *L = ctx->state = luaL_newstate(); -#else - lua_State *L = ctx->state = lua_newstate(mp_lua_alloc, ctx); -#endif if (!L) { MP_FATAL(ctx, "Could not initialize Lua.\n"); goto error_out; } + // Wrap the internal allocator with our version that does accounting + ctx->lua_allocf = lua_getallocf(L, &ctx->lua_alloc_ud); + lua_setallocf(L, mp_lua_alloc, ctx); + if (mp_cpcall(L, run_lua, ctx)) { const char *err = "unknown error"; if (lua_type(L, -1) == LUA_TSTRING) // avoid allocation -- cgit v1.2.3