Browse Source

option for garbage-collector `step'

Roberto Ierusalimschy 21 years ago
parent
commit
345379b5ff
3 changed files with 17 additions and 18 deletions
  1. 10 1
      lapi.c
  2. 5 16
      lbaselib.c
  3. 2 1
      lua.h

+ 10 - 1
lapi.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lapi.c,v 2.9 2004/05/14 19:25:09 roberto Exp roberto $
+** $Id: lapi.c,v 2.10 2004/05/31 19:41:52 roberto Exp roberto $
 ** Lua API
 ** Lua API
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -833,6 +833,15 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
       /* GC values are expressed in Kbytes: #bytes/2^10 */
       /* GC values are expressed in Kbytes: #bytes/2^10 */
       return cast(int, g->nblocks >> 10);
       return cast(int, g->nblocks >> 10);
     }
     }
+    case LUA_GCSTEP: {
+      lu_mem a = (cast(lu_mem, data) << 10);
+      if (a <= g->nblocks)
+        g->GCthreshold = g->nblocks - a;
+      else
+        g->GCthreshold = 0;
+      luaC_step(L);
+      return 0;
+    }
     default: return -1;  /* invalid option */
     default: return -1;  /* invalid option */
   }
   }
 }
 }

+ 5 - 16
lbaselib.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lbaselib.c,v 1.144 2004/05/31 18:50:30 roberto Exp roberto $
+** $Id: lbaselib.c,v 1.145 2004/06/02 14:20:08 roberto Exp roberto $
 ** Basic library
 ** Basic library
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -180,22 +180,11 @@ static int luaB_gcinfo (lua_State *L) {
 
 
 static int luaB_collectgarbage (lua_State *L) {
 static int luaB_collectgarbage (lua_State *L) {
   static const char *const opts[] = {"stop", "restart", "collect", "count",
   static const char *const opts[] = {"stop", "restart", "collect", "count",
-                                     NULL};
+                                     "step", NULL};
   static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART,
   static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART,
-                                LUA_GCCOLLECT, LUA_GCCOUNT};
-  int o;
-  int ex;
-#if 1
-  if (lua_isnumber(L, 1)) {
-    int v = lua_tointeger(L, 1);
-    lua_settop(L, 0);
-    if (v == 0) lua_pushstring(L, "collect");
-    else if (v >= 10000) lua_pushstring(L, "stop");
-    else lua_pushstring(L, "restart");
-  }
-#endif
-  o = luaL_findstring(luaL_optstring(L, 1, "collect"), opts);
-  ex = luaL_optint(L, 2, 0);
+                                LUA_GCCOLLECT, LUA_GCCOUNT, LUA_GCSTEP};
+  int o = luaL_findstring(luaL_optstring(L, 1, "collect"), opts);
+  int ex = luaL_optint(L, 2, 0);
   luaL_argcheck(L, o >= 0, 1, "invalid option");
   luaL_argcheck(L, o >= 0, 1, "invalid option");
   lua_pushinteger(L, lua_gc(L, optsnum[o], ex));
   lua_pushinteger(L, lua_gc(L, optsnum[o], ex));
   return 1;
   return 1;

+ 2 - 1
lua.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lua.h,v 1.190 2004/05/31 19:41:52 roberto Exp roberto $
+** $Id: lua.h,v 1.191 2004/06/02 17:37:03 roberto Exp roberto $
 ** Lua - An Extensible Extension Language
 ** Lua - An Extensible Extension Language
 ** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
 ** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
 ** http://www.lua.org	mailto:[email protected]
 ** http://www.lua.org	mailto:[email protected]
@@ -217,6 +217,7 @@ LUA_API int  lua_resume (lua_State *L, int narg);
 #define LUA_GCRESTART	1
 #define LUA_GCRESTART	1
 #define LUA_GCCOLLECT	2
 #define LUA_GCCOLLECT	2
 #define LUA_GCCOUNT	3
 #define LUA_GCCOUNT	3
+#define LUA_GCSTEP	4
 
 
 LUA_API int lua_gc (lua_State *L, int what, int data);
 LUA_API int lua_gc (lua_State *L, int what, int data);