new option 'isrunning' for 'lua_gc' (and 'collectgarbage')

This commit is contained in:
Roberto Ierusalimschy
2009-11-09 16:55:17 -02:00
parent 88eb901f81
commit 1ce819333d
3 changed files with 15 additions and 9 deletions

6
lapi.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lapi.c,v 2.97 2009/11/06 17:03:37 roberto Exp roberto $ ** $Id: lapi.c,v 2.98 2009/11/09 18:29:21 roberto Exp roberto $
** Lua API ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -953,6 +953,10 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
g->gcstepmul = data; g->gcstepmul = data;
break; break;
} }
case LUA_GCISRUNNING: {
res = (g->GCthreshold != MAX_LUMEM);
break;
}
default: res = -1; /* invalid option */ default: res = -1; /* invalid option */
} }
lua_unlock(L); lua_unlock(L);

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lbaselib.c,v 1.220 2009/10/23 12:50:25 roberto Exp roberto $ ** $Id: lbaselib.c,v 1.221 2009/10/23 19:12:19 roberto Exp roberto $
** Basic library ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -177,20 +177,21 @@ 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", static const char *const opts[] = {"stop", "restart", "collect",
"count", "step", "setpause", "setstepmul", NULL}; "count", "step", "setpause", "setstepmul", "isrunning", NULL};
static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL}; LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
int o = luaL_checkoption(L, 1, "collect", opts); LUA_GCISRUNNING};
int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
int ex = luaL_optint(L, 2, 0); int ex = luaL_optint(L, 2, 0);
int res = lua_gc(L, optsnum[o], ex); int res = lua_gc(L, o, ex);
switch (optsnum[o]) { switch (o) {
case LUA_GCCOUNT: { case LUA_GCCOUNT: {
int b = lua_gc(L, LUA_GCCOUNTB, 0); int b = lua_gc(L, LUA_GCCOUNTB, 0);
lua_pushnumber(L, res + ((lua_Number)b/1024)); lua_pushnumber(L, res + ((lua_Number)b/1024));
lua_pushinteger(L, b); lua_pushinteger(L, b);
return 2; return 2;
} }
case LUA_GCSTEP: { case LUA_GCSTEP: case LUA_GCISRUNNING: {
lua_pushboolean(L, res); lua_pushboolean(L, res);
return 1; return 1;
} }

3
lua.h
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lua.h,v 1.247 2009/11/05 16:48:31 roberto Exp roberto $ ** $Id: lua.h,v 1.248 2009/11/05 17:26:00 roberto Exp roberto $
** Lua - A Scripting Language ** Lua - A Scripting Language
** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
** See Copyright Notice at the end of this file ** See Copyright Notice at the end of this file
@@ -267,6 +267,7 @@ LUA_API int (lua_status) (lua_State *L);
#define LUA_GCSTEP 5 #define LUA_GCSTEP 5
#define LUA_GCSETPAUSE 6 #define LUA_GCSETPAUSE 6
#define LUA_GCSETSTEPMUL 7 #define LUA_GCSETSTEPMUL 7
#define LUA_GCISRUNNING 8
LUA_API int (lua_gc) (lua_State *L, int what, int data); LUA_API int (lua_gc) (lua_State *L, int what, int data);