mirror of
https://github.com/mpv-player/mpv
synced 2024-11-14 22:48:35 +01:00
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.
This commit is contained in:
parent
1e780251ae
commit
a67bda2840
28
player/lua.c
28
player/lua.c
@ -90,6 +90,8 @@ struct script_ctx {
|
|||||||
struct mpv_handle *client;
|
struct mpv_handle *client;
|
||||||
struct MPContext *mpctx;
|
struct MPContext *mpctx;
|
||||||
size_t lua_malloc_size;
|
size_t lua_malloc_size;
|
||||||
|
lua_Alloc lua_allocf;
|
||||||
|
void *lua_alloc_ud;
|
||||||
struct stats_ctx *stats;
|
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));
|
talloc_steal(tmp, node_get_alloc(node));
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef HAVE_LUAJIT
|
// lua_Alloc compatible. Serves only to track memory usage. This wraps the
|
||||||
// lua_Alloc compatible. Serves only to retrieve memory usage.
|
// 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)
|
static void *mp_lua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
|
||||||
{
|
{
|
||||||
struct script_ctx *ctx = ud;
|
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)
|
if (!ptr)
|
||||||
osize = 0;
|
osize = 0;
|
||||||
|
|
||||||
if (nsize) {
|
ptr = ctx->lua_allocf(ctx->lua_alloc_ud, ptr, osize, nsize);
|
||||||
ptr = realloc(ptr, nsize);
|
if (nsize && !ptr)
|
||||||
if (!ptr)
|
return NULL; // allocation failed, so original memory left untouched
|
||||||
return NULL;
|
|
||||||
} else {
|
|
||||||
free(ptr);
|
|
||||||
ptr = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx->lua_malloc_size = ctx->lua_malloc_size - osize + nsize;
|
ctx->lua_malloc_size = ctx->lua_malloc_size - osize + nsize;
|
||||||
stats_size_value(ctx->stats, "mem", ctx->lua_malloc_size);
|
stats_size_value(ctx->stats, "mem", ctx->lua_malloc_size);
|
||||||
|
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
static struct script_ctx *get_ctx(lua_State *L)
|
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;
|
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();
|
lua_State *L = ctx->state = luaL_newstate();
|
||||||
#else
|
|
||||||
lua_State *L = ctx->state = lua_newstate(mp_lua_alloc, ctx);
|
|
||||||
#endif
|
|
||||||
if (!L) {
|
if (!L) {
|
||||||
MP_FATAL(ctx, "Could not initialize Lua.\n");
|
MP_FATAL(ctx, "Could not initialize Lua.\n");
|
||||||
goto error_out;
|
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)) {
|
if (mp_cpcall(L, run_lua, ctx)) {
|
||||||
const char *err = "unknown error";
|
const char *err = "unknown error";
|
||||||
if (lua_type(L, -1) == LUA_TSTRING) // avoid allocation
|
if (lua_type(L, -1) == LUA_TSTRING) // avoid allocation
|
||||||
|
Loading…
Reference in New Issue
Block a user