when debuging, blocks must be kept in double allignment.

This commit is contained in:
Roberto Ierusalimschy 1998-06-29 19:03:06 -03:00
parent e869d17eb1
commit 994aba062b
1 changed files with 10 additions and 7 deletions

17
lmem.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lmem.c,v 1.5 1998/03/09 21:49:52 roberto Exp roberto $ ** $Id: lmem.c,v 1.6 1998/06/19 16:14:09 roberto Exp roberto $
** Interface to Memory Manager ** Interface to Memory Manager
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -60,6 +60,8 @@ void *luaM_realloc (void *block, unsigned long size)
#include <string.h> #include <string.h>
#define HEADER (sizeof(double))
#define MARK 55 #define MARK 55
unsigned long numblocks = 0; unsigned long numblocks = 0;
@ -68,9 +70,9 @@ unsigned long totalmem = 0;
static void *checkblock (void *block) static void *checkblock (void *block)
{ {
unsigned long *b = (unsigned long *)block - 1; unsigned long *b = (unsigned long *)((char *)block - HEADER);
unsigned long size = *b; unsigned long size = *b;
LUA_ASSERT(*(((char *)b)+size+sizeof(unsigned long)) == MARK, LUA_ASSERT(*(((char *)b)+size+HEADER) == MARK,
"corrupted block"); "corrupted block");
numblocks--; numblocks--;
totalmem -= size; totalmem -= size;
@ -80,12 +82,13 @@ static void *checkblock (void *block)
void *luaM_realloc (void *block, unsigned long size) void *luaM_realloc (void *block, unsigned long size)
{ {
unsigned long realsize = sizeof(unsigned long)+size+sizeof(char); unsigned long realsize = HEADER+size+1;
if (realsize != (size_t)realsize) if (realsize != (size_t)realsize)
lua_error("Allocation Error: Block too big"); lua_error("Allocation Error: Block too big");
if (size == 0) { /* ANSI dosen't need this, but some machines... */ if (size == 0) { /* ANSI dosen't need this, but some machines... */
if (block) { if (block) {
memset(block, -1, *((unsigned long *)block-1)); /* erase block */ unsigned long *b = (unsigned long *)((char *)block - HEADER);
memset(block, -1, *b); /* erase block */
block = checkblock(block); block = checkblock(block);
free(block); free(block);
} }
@ -102,8 +105,8 @@ void *luaM_realloc (void *block, unsigned long size)
totalmem += size; totalmem += size;
numblocks++; numblocks++;
*(unsigned long *)block = size; *(unsigned long *)block = size;
*(((char *)block)+size+sizeof(unsigned long)) = MARK; *(((char *)block)+size+HEADER) = MARK;
return (unsigned long *)block+1; return (unsigned long *)((char *)block+HEADER);
} }