From 2b5c1f99e52fd725b3c3c58d80596dbb4a342da2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 5 Jun 2006 16:35:57 -0300 Subject: small simplifications --- ltests.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'ltests.c') diff --git a/ltests.c b/ltests.c index 3c077a1c..7ae9ba6f 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.35 2006/01/10 12:50:00 roberto Exp roberto $ +** $Id: ltests.c,v 2.36 2006/01/10 13:13:06 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -82,7 +82,7 @@ static void setnameval (lua_State *L, const char *name, int val) { #endif -Memcontrol memcontrol = {0L, 0L, 0L, ULONG_MAX}; +Memcontrol memcontrol = {0L, 0L, 0L, 0L}; static void *checkblock (void *block, size_t size) { @@ -109,6 +109,10 @@ static void freeblock (Memcontrol *mc, void *block, size_t size) { void *debug_realloc (void *ud, void *block, size_t oldsize, size_t size) { Memcontrol *mc = cast(Memcontrol *, ud); lua_assert(oldsize == 0 || checkblocksize(block, oldsize)); + if (mc->memlimit == 0) { /* first time? */ + char *limit = getenv("MEMLIMIT"); /* initialize memory limit */ + mc->memlimit = limit ? strtoul(limit, NULL, 10) : ULONG_MAX; + } if (size == 0) { freeblock(mc, block, oldsize); return NULL; @@ -1121,8 +1125,15 @@ static const struct luaL_Reg tests_funcs[] = { }; +static void checkfinalmem (void) { + lua_assert(memcontrol.numblocks == 0); + lua_assert(memcontrol.total == 0); +} + + int luaB_opentests (lua_State *L) { void *ud; + atexit(checkfinalmem); lua_assert(lua_getallocf(L, &ud) == debug_realloc); lua_assert(ud == cast(void *, &memcontrol)); lua_setallocf(L, lua_getallocf(L, NULL), ud); @@ -1131,17 +1142,4 @@ int luaB_opentests (lua_State *L) { return 0; } - -#undef main -int main (int argc, char *argv[]) { - int ret; - char *limit = getenv("MEMLIMIT"); - if (limit) - memcontrol.memlimit = strtoul(limit, NULL, 10); - ret = l_main(argc, argv); - lua_assert(memcontrol.numblocks == 0); - lua_assert(memcontrol.total == 0); - return ret; -} - #endif -- cgit v1.2.3-55-g6feb