From 90b0ac6495cb5b4f06b795bef3da346a55581284 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 13 Feb 2014 15:25:20 -0200 Subject: [PATCH] limit to 'gcstepmul' imposed by 'lua_gc' (+ some details in 'lgc.c') --- lapi.c | 3 ++- lgc.c | 31 +++++++++++++++++++------------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/lapi.c b/lapi.c index 0fcb6eaa..d5765cc9 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.193 2014/01/27 13:34:32 roberto Exp roberto $ +** $Id: lapi.c,v 2.194 2014/02/13 12:11:34 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -1086,6 +1086,7 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { } case LUA_GCSETSTEPMUL: { res = g->gcstepmul; + if (data < 40) data = 40; /* avoid ridiculous low values (and 0) */ g->gcstepmul = data; break; } diff --git a/lgc.c b/lgc.c index 515267e7..ea0537df 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.171 2014/02/13 12:11:34 roberto Exp roberto $ +** $Id: lgc.c,v 2.172 2014/02/13 14:46:38 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -1060,9 +1060,9 @@ void luaC_runtilstate (lua_State *L, int statesmask) { /* -** run a few finalizers +** run a few (up to 'g->gcfinnum') finalizers */ -static int dosomefinalization (lua_State *L) { +static int runafewfinalizers (lua_State *L) { global_State *g = G(L); unsigned int i; lua_assert(!g->tobefnz || g->gcfinnum > 0); @@ -1074,20 +1074,27 @@ static int dosomefinalization (lua_State *L) { } +/* +** get GC debt and convert it from Kb to 'work units' (avoid zero debt +** and overflows) +*/ +static l_mem getdebt (global_State *g) { + l_mem debt = g->GCdebt; + int stepmul = g->gcstepmul; + debt = (debt / STEPMULADJ) + 1; + debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM; + return debt; +} + /* ** performs a basic GC step */ void luaC_forcestep (lua_State *L) { global_State *g = G(L); - l_mem debt = g->GCdebt; - int stepmul = g->gcstepmul; - if (stepmul < 40) stepmul = 40; /* avoid ridiculous low values (and 0) */ - /* convert debt from Kb to 'work units' (avoid zero debt and overflows) */ - debt = (debt / STEPMULADJ) + 1; - debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM; + l_mem debt = getdebt(g); do { if (g->gcstate == GCScallfin && g->tobefnz) { - unsigned int n = dosomefinalization(L); + unsigned int n = runafewfinalizers(L); debt -= (n * GCFINALIZECOST); } else { /* perform one single step */ @@ -1098,9 +1105,9 @@ void luaC_forcestep (lua_State *L) { if (g->gcstate == GCSpause) setpause(g, g->GCestimate); /* pause until next cycle */ else { - debt = (debt / stepmul) * STEPMULADJ; /* convert 'work units' to Kb */ + debt = (debt / g->gcstepmul) * STEPMULADJ; /* convert 'work units' to Kb */ luaE_setdebt(g, debt); - dosomefinalization(L); + runafewfinalizers(L); } }