|
@@ -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 <limits.h>
|
|
|
#include <string.h>
|
|
|
|
|
|
-#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<MARKSIZE;i++)
|
|
|
assert(*(((char *)b)+HEADER+size+i) == MARK+i); /* corrupted block? */
|
|
|
- memdebug_numblocks--;
|
|
|
- memdebug_total -= size;
|
|
|
return b;
|
|
|
}
|
|
|
|
|
@@ -66,12 +63,15 @@ static void freeblock (void *block) {
|
|
|
size_t size = *blocksize(block);
|
|
|
block = checkblock(block);
|
|
|
memset(block, -1, size+HEADER+MARKSIZE); /* erase block */
|
|
|
- (free)(block); /* free original block */
|
|
|
+ free(block); /* free original block */
|
|
|
+ memdebug_numblocks--;
|
|
|
+ memdebug_total -= size;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
-static void *debug_realloc (void *block, size_t size) {
|
|
|
+static void *debug_realloc (void *block, size_t oldsize, size_t size) {
|
|
|
+ assert((oldsize == 0) ? block == NULL : oldsize == *blocksize(block));
|
|
|
if (size == 0) {
|
|
|
freeblock(block);
|
|
|
return NULL;
|
|
@@ -79,19 +79,22 @@ static void *debug_realloc (void *block, size_t size) {
|
|
|
else if (memdebug_total+size > 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<MARKSIZE;i++)
|
|
@@ -102,20 +105,26 @@ static void *debug_realloc (void *block, size_t size) {
|
|
|
|
|
|
|
|
|
/* }====================================================================== */
|
|
|
-#endif
|
|
|
-
|
|
|
|
|
|
+#else
|
|
|
+/* no debug */
|
|
|
|
|
|
/*
|
|
|
** Real ISO (ANSI) systems do not need these tests;
|
|
|
** but some systems (Sun OS) are not that ISO...
|
|
|
*/
|
|
|
#ifdef OLD_ANSI
|
|
|
-#define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s))
|
|
|
-#define free(b) if (b) (free)(b)
|
|
|
+#define basicrealloc(b,os,s) ((b) == NULL ? malloc(s) : realloc(b, s))
|
|
|
+#define basicfree(b,s) if (b) free(b)
|
|
|
+#else
|
|
|
+#define basicrealloc(b,os,s) realloc(b,s)
|
|
|
+#define basicfree(b,s) free(b)
|
|
|
+
|
|
|
#endif
|
|
|
|
|
|
|
|
|
+#endif
|
|
|
+
|
|
|
void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
|
|
|
int limit, const char *errormsg) {
|
|
|
void *newblock;
|
|
@@ -127,7 +136,8 @@ void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
|
|
|
newsize = limit; /* still have at least MINPOWER2 free places */
|
|
|
else lua_error(L, errormsg);
|
|
|
}
|
|
|
- newblock = luaM_realloc(L, block, (luint32)newsize*(luint32)size_elems);
|
|
|
+ newblock = luaM_realloc(L, block, (luint32)(*size)*(luint32)size_elems,
|
|
|
+ (luint32)newsize*(luint32)size_elems);
|
|
|
*size = newsize; /* update only when everything else is OK */
|
|
|
return newblock;
|
|
|
}
|
|
@@ -136,20 +146,25 @@ void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
|
|
|
/*
|
|
|
** generic allocation routine.
|
|
|
*/
|
|
|
-void *luaM_realloc (lua_State *L, void *block, luint32 size) {
|
|
|
+void *luaM_realloc (lua_State *L, void *block, luint32 oldsize, luint32 size) {
|
|
|
if (size == 0) {
|
|
|
- free(block); /* block may be NULL; that is OK for free */
|
|
|
- return NULL;
|
|
|
+ basicfree(block, oldsize); /* block may be NULL; that is OK for free */
|
|
|
+ block = NULL;
|
|
|
}
|
|
|
else if (size >= 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;
|
|
|
}
|
|
|
|
|
|
-
|