mirror of https://github.com/rusefi/lua.git
when debugging, move and erase old blocks to realloc.
This commit is contained in:
parent
613b60c156
commit
abfc885079
64
lmem.c
64
lmem.c
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
** $Id: lmem.c,v 1.15 1999/05/11 14:18:40 roberto Exp roberto $
|
** $Id: lmem.c,v 1.16 1999/05/20 20:43:06 roberto Exp roberto $
|
||||||
** Interface to Memory Manager
|
** Interface to Memory Manager
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
@ -26,8 +26,6 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef DEBUG
|
|
||||||
|
|
||||||
|
|
||||||
static unsigned long power2 (unsigned long n) {
|
static unsigned long power2 (unsigned long n) {
|
||||||
unsigned long p = MINSIZE;
|
unsigned long p = MINSIZE;
|
||||||
|
@ -48,6 +46,8 @@ void *luaM_growaux (void *block, unsigned long nelems, int inc, int size,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef DEBUG
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** generic allocation routine.
|
** generic allocation routine.
|
||||||
*/
|
*/
|
||||||
|
@ -73,34 +73,39 @@ void *luaM_realloc (void *block, unsigned long size) {
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
void *luaM_growaux (void *block, unsigned long nelems, int inc, int size,
|
|
||||||
char *errormsg, unsigned long limit) {
|
|
||||||
unsigned long newn = nelems+inc;
|
|
||||||
if (newn >= limit)
|
|
||||||
lua_error(errormsg);
|
|
||||||
return luaM_realloc(block, newn*size);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#define HEADER (sizeof(double))
|
#define HEADER (sizeof(double))
|
||||||
#define MARKSIZE 32
|
#define MARKSIZE 16
|
||||||
|
|
||||||
#define MARK 55
|
#define MARK 55
|
||||||
|
|
||||||
|
|
||||||
|
#define blocksize(b) ((unsigned long *)((char *)(b) - HEADER))
|
||||||
|
|
||||||
unsigned long numblocks = 0;
|
unsigned long numblocks = 0;
|
||||||
unsigned long totalmem = 0;
|
unsigned long totalmem = 0;
|
||||||
|
|
||||||
|
|
||||||
static void *checkblock (void *block) {
|
static void *checkblock (void *block) {
|
||||||
unsigned long *b = (unsigned long *)((char *)block - HEADER);
|
if (block == NULL)
|
||||||
|
return NULL;
|
||||||
|
else {
|
||||||
|
unsigned long *b = blocksize(block);
|
||||||
unsigned long size = *b;
|
unsigned long size = *b;
|
||||||
int i;
|
int i;
|
||||||
for (i=0;i<MARKSIZE;i++)
|
for (i=0;i<MARKSIZE;i++)
|
||||||
LUA_ASSERT(*(((char *)b)+size+HEADER+i) == MARK+i, "corrupted block");
|
LUA_ASSERT(*(((char *)b)+HEADER+size+i) == MARK+i, "corrupted block");
|
||||||
numblocks--;
|
numblocks--;
|
||||||
totalmem -= size;
|
totalmem -= size;
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void freeblock (void *block) {
|
||||||
|
if (block)
|
||||||
|
memset(block, -1, *blocksize(block)); /* erase block */
|
||||||
|
free(checkblock(block));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void *luaM_realloc (void *block, unsigned long size) {
|
void *luaM_realloc (void *block, unsigned long size) {
|
||||||
|
@ -108,24 +113,27 @@ void *luaM_realloc (void *block, unsigned long size) {
|
||||||
if (realsize != (size_t)realsize)
|
if (realsize != (size_t)realsize)
|
||||||
lua_error("memory allocation error: block too big");
|
lua_error("memory allocation error: block too big");
|
||||||
if (size == 0) {
|
if (size == 0) {
|
||||||
if (block) {
|
freeblock(block);
|
||||||
unsigned long *b = (unsigned long *)((char *)block - HEADER);
|
|
||||||
memset(block, -1, *b); /* erase block */
|
|
||||||
block = checkblock(block);
|
|
||||||
}
|
|
||||||
free(block);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (block)
|
else {
|
||||||
block = checkblock(block);
|
char *newblock = malloc(realsize);
|
||||||
block = (unsigned long *)realloc(block, realsize);
|
int i;
|
||||||
if (block == NULL)
|
if (block) {
|
||||||
|
unsigned long oldsize = *blocksize(block);
|
||||||
|
if (oldsize > size) oldsize = size;
|
||||||
|
memcpy(newblock+HEADER, block, oldsize);
|
||||||
|
freeblock(block); /* erase (and check) old copy */
|
||||||
|
}
|
||||||
|
if (newblock == NULL)
|
||||||
lua_error(memEM);
|
lua_error(memEM);
|
||||||
totalmem += size;
|
totalmem += size;
|
||||||
numblocks++;
|
numblocks++;
|
||||||
*(unsigned long *)block = size;
|
*(unsigned long *)newblock = size;
|
||||||
{ int i; for (i=0;i<MARKSIZE;i++) *(((char *)block)+size+HEADER+i) = MARK+i; }
|
for (i=0;i<MARKSIZE;i++)
|
||||||
return (unsigned long *)((char *)block+HEADER);
|
*(newblock+HEADER+size+i) = MARK+i;
|
||||||
|
return newblock+HEADER;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue