From 0183b8030c80f57b87874ff7867ccdb172d9d3dc Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 28 Dec 2000 10:55:41 -0200 Subject: `free' gets size of the block: complete control over memory use --- lmem.c | 71 ++++++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 28 deletions(-) (limited to 'lmem.c') diff --git a/lmem.c b/lmem.c index 830f6f1d..3d34e313 100644 --- a/lmem.c +++ b/lmem.c @@ -1,5 +1,5 @@ /* -** $Id: lmem.c,v 1.40 2000/11/24 17:39:56 roberto Exp roberto $ +** $Id: lmem.c,v 1.41 2000/12/26 18:46:09 roberto Exp roberto $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -29,15 +29,14 @@ #include #include -#define realloc(b, s) debug_realloc(b, s) -#define malloc(b) debug_realloc(NULL, b) -#define free(b) debug_realloc(b, 0) +#define basicrealloc(b, os, s) debug_realloc(b, os, s) +#define basicfree(b, s) debug_realloc(b, s, 0) /* ensures maximum alignment for HEADER */ #define HEADER (sizeof(union L_Umaxalign)) -#define MARKSIZE 16 +#define MARKSIZE 32 #define MARK 0x55 /* 01010101 (a nice pattern) */ @@ -55,8 +54,6 @@ static void *checkblock (void *block) { int i; for (i=0;i memdebug_memlimit) return NULL; /* to test memory allocation errors */ else { - size_t realsize = HEADER+size+MARKSIZE; - char *newblock = (char *)(malloc)(realsize); /* alloc a new block */ + 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) { - size_t oldsize = *blocksize(block); - if (oldsize > size) oldsize = size; 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; + if (memdebug_total > memdebug_maxmem) + memdebug_maxmem = memdebug_total; memdebug_numblocks++; *(size_t *)newblock = size; for (i=0;i= MAX_SIZET) lua_error(L, "memory allocation error: block too big"); - block = realloc(block, size); - if (block == NULL) { - if (L) - luaD_breakrun(L, LUA_ERRMEM); /* break run without error message */ - else return NULL; /* error before creating state! */ + else { + block = basicrealloc(block, oldsize, size); + if (block == NULL) { + if (L) + luaD_breakrun(L, LUA_ERRMEM); /* break run without error message */ + else return NULL; /* error before creating state! */ + } + } + if (L) { + L->nblocks -= oldsize; + L->nblocks += size; } return block; } - -- cgit v1.2.3-55-g6feb