small simplifications

This commit is contained in:
Roberto Ierusalimschy 2006-06-05 16:35:57 -03:00
parent e2dc5f5d15
commit 2b5c1f99e5
2 changed files with 14 additions and 22 deletions

View File

@ -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 ** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -82,7 +82,7 @@ static void setnameval (lua_State *L, const char *name, int val) {
#endif #endif
Memcontrol memcontrol = {0L, 0L, 0L, ULONG_MAX}; Memcontrol memcontrol = {0L, 0L, 0L, 0L};
static void *checkblock (void *block, size_t size) { 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) { void *debug_realloc (void *ud, void *block, size_t oldsize, size_t size) {
Memcontrol *mc = cast(Memcontrol *, ud); Memcontrol *mc = cast(Memcontrol *, ud);
lua_assert(oldsize == 0 || checkblocksize(block, oldsize)); 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) { if (size == 0) {
freeblock(mc, block, oldsize); freeblock(mc, block, oldsize);
return NULL; 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) { int luaB_opentests (lua_State *L) {
void *ud; void *ud;
atexit(checkfinalmem);
lua_assert(lua_getallocf(L, &ud) == debug_realloc); lua_assert(lua_getallocf(L, &ud) == debug_realloc);
lua_assert(ud == cast(void *, &memcontrol)); lua_assert(ud == cast(void *, &memcontrol));
lua_setallocf(L, lua_getallocf(L, NULL), ud); lua_setallocf(L, lua_getallocf(L, NULL), ud);
@ -1131,17 +1142,4 @@ int luaB_opentests (lua_State *L) {
return 0; 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 #endif

View File

@ -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 ** Internal Header for Debugging of the Lua Implementation
** See Copyright Notice in lua.h ** 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 */ /* change some sizes to give some bugs a chance */
#undef LUAL_BUFFERSIZE #undef LUAL_BUFFERSIZE