From 1cce3e6842fd76099b1973dabd8504566610f068 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 14 Feb 2014 14:43:14 -0200 Subject: [PATCH] change in the way 'collectgarbage("step", size)' interprets 'size' (mimicking the way the GC itself behaves when Lua allocates 'size' Kbytes) --- lapi.c | 22 +++++++++++++++------- lgc.c | 22 +++++++--------------- lgc.h | 3 +-- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/lapi.c b/lapi.c index d5765cc9..cb38ffba 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.194 2014/02/13 12:11:34 roberto Exp roberto $ +** $Id: lapi.c,v 2.195 2014/02/13 17:25:20 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -1070,12 +1070,20 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { break; } case LUA_GCSTEP: { - lu_mem debt = cast(lu_mem, data) * 1024 - GCSTEPSIZE; - if (g->gcrunning) - debt += g->GCdebt; /* include current debt */ - luaE_setdebt(g, debt); - luaC_forcestep(L); - if (g->gcstate == GCSpause) /* end of cycle? */ + l_mem debt = 1; /* =1 to signal that it did an actual step */ + int oldrunning = g->gcrunning; + g->gcrunning = 1; /* force GC to run */ + if (data == 0) { + luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */ + luaC_step(L); + } + else { /* add 'data' to total debt */ + debt = cast(l_mem, data) * 1024 + g->GCdebt; + luaE_setdebt(g, debt); + luaC_checkGC(L); + } + g->gcrunning = oldrunning; /* restore previous state */ + if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */ res = 1; /* signal it */ break; } diff --git a/lgc.c b/lgc.c index ea0537df..5c365e32 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.172 2014/02/13 14:46:38 roberto Exp roberto $ +** $Id: lgc.c,v 2.173 2014/02/13 17:25:20 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -1087,11 +1087,15 @@ static l_mem getdebt (global_State *g) { } /* -** performs a basic GC step +** performs a basic GC step when collector is running */ -void luaC_forcestep (lua_State *L) { +void luaC_step (lua_State *L) { global_State *g = G(L); l_mem debt = getdebt(g); + if (!g->gcrunning) { /* not running? */ + luaE_setdebt(g, -GCSTEPSIZE * 10); /* avoid being called too often */ + return; + } do { if (g->gcstate == GCScallfin && g->tobefnz) { unsigned int n = runafewfinalizers(L); @@ -1112,18 +1116,6 @@ void luaC_forcestep (lua_State *L) { } -/* -** performs a basic GC step when collector is running -*/ -void luaC_step (lua_State *L) { - if (!G(L)->gcrunning) - luaE_setdebt(G(L), -GCSTEPSIZE); /* avoid being called too often */ - else - luaC_forcestep(L); -} - - - /* ** performs a full GC cycle; if "isemergency", does not call ** finalizers (which could change stack positions) diff --git a/lgc.h b/lgc.h index a9e497c7..0e24d2c3 100644 --- a/lgc.h +++ b/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.78 2014/02/13 12:11:34 roberto Exp roberto $ +** $Id: lgc.h,v 2.79 2014/02/13 14:46:38 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -126,7 +126,6 @@ LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); LUAI_FUNC void luaC_freeallobjects (lua_State *L); LUAI_FUNC void luaC_step (lua_State *L); -LUAI_FUNC void luaC_forcestep (lua_State *L); LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);