浏览代码

small simplifications

Roberto Ierusalimschy 19 年之前
父节点
当前提交
2b5c1f99e5
共有 2 个文件被更改,包括 14 次插入22 次删除
  1. 13 15
      ltests.c
  2. 1 7
      ltests.h

+ 13 - 15
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

+ 1 - 7
ltests.h

@@ -1,5 +1,5 @@
 /*
-** $Id: ltests.h,v 2.16 2005/09/14 17:48:57 roberto Exp roberto $
+** $Id: ltests.h,v 2.17 2005/12/27 17:12:00 roberto Exp roberto $
 ** Internal Header for Debugging of the Lua Implementation
 ** See Copyright Notice in lua.h
 */
@@ -77,12 +77,6 @@ int luaB_opentests (lua_State *L);
 
 
 
-/* real main will be defined at `ltests.c' */
-int l_main (int argc, char *argv[]);
-#define main	l_main
-
-
-
 /* change some sizes to give some bugs a chance */
 
 #undef LUAL_BUFFERSIZE