瀏覽代碼

limit to 'gcstepmul' imposed by 'lua_gc' (+ some details in 'lgc.c')

Roberto Ierusalimschy 11 年之前
父節點
當前提交
90b0ac6495
共有 2 個文件被更改,包括 20 次插入12 次删除
  1. 2 1
      lapi.c
  2. 18 11
      lgc.c

+ 2 - 1
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;
     }

+ 18 - 11
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);
@@ -1075,19 +1075,26 @@ static int dosomefinalization (lua_State *L) {
 
 
 /*
-** performs a basic GC step
+** get GC debt and convert it from Kb to 'work units' (avoid zero debt
+** and overflows)
 */
-void luaC_forcestep (lua_State *L) {
-  global_State *g = G(L);
+static l_mem getdebt (global_State *g) {
   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;
+  return debt;
+}
+
+/*
+** performs a basic GC step
+*/
+void luaC_forcestep (lua_State *L) {
+  global_State *g = G(L);
+  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);
   }
 }