From e74b250d71c9a72758d7bb586bb906cacd8293f6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 22 Jan 1996 12:15:13 -0200 Subject: memory overflow tries a garbage collection; if it fails then exit the program. --- luamem.c | 20 +++++++++++++++++--- table.c | 17 ++++++++++++----- table.h | 3 ++- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/luamem.c b/luamem.c index a1428418..ce2ffff9 100644 --- a/luamem.c +++ b/luamem.c @@ -3,13 +3,27 @@ ** TecCGraf - PUC-Rio */ -char *rcs_mem = "$Id: mem.c,v 1.4 1995/01/13 22:11:12 roberto Exp roberto $"; +char *rcs_mem = "$Id: mem.c,v 1.5 1995/02/06 19:34:03 roberto Exp roberto $"; #include #include +#include #include "mem.h" #include "lua.h" +#include "table.h" + +static void mem_error (void) +{ + Long recovered = luaI_collectgarbage(); /* try to collect garbage */ + if (recovered) + lua_error("not enough memory"); + else + { /* if there is no garbage then must exit */ + printf(stderr, "lua error: memory overflow - unable to recover\n"); + exit(1); + } +} void luaI_free (void *block) { @@ -22,7 +36,7 @@ void *luaI_malloc (unsigned long size) { void *block = malloc((size_t)size); if (block == NULL) - lua_error("not enough memory"); + mem_error(); return block; } @@ -31,7 +45,7 @@ void *luaI_realloc (void *oldblock, unsigned long size) { void *block = realloc(oldblock, (size_t)size); if (block == NULL) - lua_error("not enough memory"); + mem_error(); return block; } diff --git a/table.c b/table.c index 41fbf8ab..794cb864 100644 --- a/table.c +++ b/table.c @@ -3,7 +3,7 @@ ** Module to control static tables */ -char *rcs_table="$Id: table.c,v 2.38 1995/11/03 15:30:50 roberto Exp roberto $"; +char *rcs_table="$Id: table.c,v 2.39 1996/01/09 20:23:19 roberto Exp $"; /*#include */ @@ -178,12 +178,9 @@ int lua_markobject (Object *o) ** Garbage collection. ** Delete all unused strings and arrays. */ -void lua_pack (void) +Long luaI_collectgarbage (void) { - static Long block = GARBAGE_BLOCK; /* when garbage collector will be called */ - static Long nentity = 0; /* counter of new entities (strings and arrays) */ Long recovered = 0; - if (nentity++ < block) return; lua_travstack(lua_markobject); /* mark stack objects */ lua_travsymbol(lua_markobject); /* mark symbol table objects */ luaI_travlock(lua_markobject); /* mark locked objects */ @@ -191,6 +188,16 @@ void lua_pack (void) recovered += lua_strcollector(); recovered += lua_hashcollector(); recovered += luaI_funccollector(); + return recovered; +} + +void lua_pack (void) +{ + static Long block = GARBAGE_BLOCK; /* when garbage collector will be called */ + static Long nentity = 0; /* counter of new entities (strings and arrays) */ + Long recovered = 0; + if (nentity++ < block) return; + recovered = luaI_collectgarbage(); nentity = 0; /* reset counter */ block=(16*block-7*recovered)/12; /* adapt block size */ if (block < MIN_GARBAGE_BLOCK) block = MIN_GARBAGE_BLOCK; diff --git a/table.h b/table.h index f38ea668..a3c10388 100644 --- a/table.h +++ b/table.h @@ -1,7 +1,7 @@ /* ** Module to control static tables ** TeCGraf - PUC-Rio -** $Id: table.h,v 2.12 1995/10/17 11:58:41 roberto Exp roberto $ +** $Id: table.h,v 2.13 1995/10/26 14:21:56 roberto Exp roberto $ */ #ifndef table_h @@ -23,6 +23,7 @@ Word luaI_findsymbol (TreeNode *t); Word luaI_findconstant (TreeNode *t); Word luaI_findconstantbyname (char *name); int lua_markobject (Object *o); +Long luaI_collectgarbage (void); void lua_pack (void); -- cgit v1.2.3-55-g6feb