From 1f917e709ca3fe41cf07fd4bf99a080883521394 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 6 Feb 2001 14:01:29 -0200 Subject: better use of extra include files (both for tests and for old_ansi) --- ltests.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) (limited to 'ltests.c') diff --git a/ltests.c b/ltests.c index 814102ff..0fbf0426 100644 --- a/ltests.c +++ b/ltests.c @@ -1,11 +1,12 @@ /* -** $Id: ltests.c,v 1.61 2001/02/01 16:03:38 roberto Exp roberto $ +** $Id: ltests.c,v 1.62 2001/02/02 15:13:05 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ #include +#include #include #include #include @@ -34,6 +35,7 @@ */ #ifdef LUA_DEBUG + lua_State *lua_state = NULL; int islocked = 0; @@ -47,6 +49,88 @@ static void setnameval (lua_State *L, const char *name, int val) { } +/* +** {====================================================================== +** Controlled version for realloc. +** ======================================================================= +*/ + + +/* ensures maximum alignment for HEADER */ +#define HEADER (sizeof(union L_Umaxalign)) + +#define MARKSIZE 32 +#define MARK 0x55 /* 01010101 (a nice pattern) */ + + +#define blocksize(b) ((size_t *)((char *)(b) - HEADER)) + +unsigned long memdebug_numblocks = 0; +unsigned long memdebug_total = 0; +unsigned long memdebug_maxmem = 0; +unsigned long memdebug_memlimit = ULONG_MAX; + + +static void *checkblock (void *block) { + size_t *b = blocksize(block); + size_t size = *b; + int i; + for (i=0;i memdebug_memlimit) + return NULL; /* to test memory allocation errors */ + else { + char *newblock; + int i; + size_t realsize = HEADER+size+MARKSIZE; + if (realsize < size) return NULL; /* overflow! */ + newblock = (char *)malloc(realsize); /* alloc a new block */ + if (newblock == NULL) return NULL; + if (oldsize > size) oldsize = size; + if (block) { + memcpy(newblock+HEADER, block, oldsize); + freeblock(block); /* erase (and check) old copy */ + } + /* initialize new part of the block with something `weird' */ + memset(newblock+HEADER+oldsize, -MARK, size-oldsize); + memdebug_total += size; + if (memdebug_total > memdebug_maxmem) + memdebug_maxmem = memdebug_total; + memdebug_numblocks++; + *(size_t *)newblock = size; + for (i=0;i