aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-01-22 12:15:13 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-01-22 12:15:13 -0200
commite74b250d71c9a72758d7bb586bb906cacd8293f6 (patch)
treebee1df3715c456e7ee1e591f0734cce34fff3195
parentcd54c95ee179dd1578e127745354aa6a59b72eb7 (diff)
downloadlua-e74b250d71c9a72758d7bb586bb906cacd8293f6.tar.gz
lua-e74b250d71c9a72758d7bb586bb906cacd8293f6.tar.bz2
lua-e74b250d71c9a72758d7bb586bb906cacd8293f6.zip
memory overflow tries a garbage collection; if it fails then exit the
program.
-rw-r--r--luamem.c20
-rw-r--r--table.c17
-rw-r--r--table.h3
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 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_mem = "$Id: mem.c,v 1.4 1995/01/13 22:11:12 roberto Exp roberto $"; 6char *rcs_mem = "$Id: mem.c,v 1.5 1995/02/06 19:34:03 roberto Exp roberto $";
7 7
8#include <stdlib.h> 8#include <stdlib.h>
9#include <string.h> 9#include <string.h>
10#include <stdio.h>
10 11
11#include "mem.h" 12#include "mem.h"
12#include "lua.h" 13#include "lua.h"
14#include "table.h"
15
16static void mem_error (void)
17{
18 Long recovered = luaI_collectgarbage(); /* try to collect garbage */
19 if (recovered)
20 lua_error("not enough memory");
21 else
22 { /* if there is no garbage then must exit */
23 printf(stderr, "lua error: memory overflow - unable to recover\n");
24 exit(1);
25 }
26}
13 27
14void luaI_free (void *block) 28void luaI_free (void *block)
15{ 29{
@@ -22,7 +36,7 @@ void *luaI_malloc (unsigned long size)
22{ 36{
23 void *block = malloc((size_t)size); 37 void *block = malloc((size_t)size);
24 if (block == NULL) 38 if (block == NULL)
25 lua_error("not enough memory"); 39 mem_error();
26 return block; 40 return block;
27} 41}
28 42
@@ -31,7 +45,7 @@ void *luaI_realloc (void *oldblock, unsigned long size)
31{ 45{
32 void *block = realloc(oldblock, (size_t)size); 46 void *block = realloc(oldblock, (size_t)size);
33 if (block == NULL) 47 if (block == NULL)
34 lua_error("not enough memory"); 48 mem_error();
35 return block; 49 return block;
36} 50}
37 51
diff --git a/table.c b/table.c
index 41fbf8ab..794cb864 100644
--- a/table.c
+++ b/table.c
@@ -3,7 +3,7 @@
3** Module to control static tables 3** Module to control static tables
4*/ 4*/
5 5
6char *rcs_table="$Id: table.c,v 2.38 1995/11/03 15:30:50 roberto Exp roberto $"; 6char *rcs_table="$Id: table.c,v 2.39 1996/01/09 20:23:19 roberto Exp $";
7 7
8/*#include <string.h>*/ 8/*#include <string.h>*/
9 9
@@ -178,12 +178,9 @@ int lua_markobject (Object *o)
178** Garbage collection. 178** Garbage collection.
179** Delete all unused strings and arrays. 179** Delete all unused strings and arrays.
180*/ 180*/
181void lua_pack (void) 181Long luaI_collectgarbage (void)
182{ 182{
183 static Long block = GARBAGE_BLOCK; /* when garbage collector will be called */
184 static Long nentity = 0; /* counter of new entities (strings and arrays) */
185 Long recovered = 0; 183 Long recovered = 0;
186 if (nentity++ < block) return;
187 lua_travstack(lua_markobject); /* mark stack objects */ 184 lua_travstack(lua_markobject); /* mark stack objects */
188 lua_travsymbol(lua_markobject); /* mark symbol table objects */ 185 lua_travsymbol(lua_markobject); /* mark symbol table objects */
189 luaI_travlock(lua_markobject); /* mark locked objects */ 186 luaI_travlock(lua_markobject); /* mark locked objects */
@@ -191,6 +188,16 @@ void lua_pack (void)
191 recovered += lua_strcollector(); 188 recovered += lua_strcollector();
192 recovered += lua_hashcollector(); 189 recovered += lua_hashcollector();
193 recovered += luaI_funccollector(); 190 recovered += luaI_funccollector();
191 return recovered;
192}
193
194void lua_pack (void)
195{
196 static Long block = GARBAGE_BLOCK; /* when garbage collector will be called */
197 static Long nentity = 0; /* counter of new entities (strings and arrays) */
198 Long recovered = 0;
199 if (nentity++ < block) return;
200 recovered = luaI_collectgarbage();
194 nentity = 0; /* reset counter */ 201 nentity = 0; /* reset counter */
195 block=(16*block-7*recovered)/12; /* adapt block size */ 202 block=(16*block-7*recovered)/12; /* adapt block size */
196 if (block < MIN_GARBAGE_BLOCK) block = MIN_GARBAGE_BLOCK; 203 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 @@
1/* 1/*
2** Module to control static tables 2** Module to control static tables
3** TeCGraf - PUC-Rio 3** TeCGraf - PUC-Rio
4** $Id: table.h,v 2.12 1995/10/17 11:58:41 roberto Exp roberto $ 4** $Id: table.h,v 2.13 1995/10/26 14:21:56 roberto Exp roberto $
5*/ 5*/
6 6
7#ifndef table_h 7#ifndef table_h
@@ -23,6 +23,7 @@ Word luaI_findsymbol (TreeNode *t);
23Word luaI_findconstant (TreeNode *t); 23Word luaI_findconstant (TreeNode *t);
24Word luaI_findconstantbyname (char *name); 24Word luaI_findconstantbyname (char *name);
25int lua_markobject (Object *o); 25int lua_markobject (Object *o);
26Long luaI_collectgarbage (void);
26void lua_pack (void); 27void lua_pack (void);
27 28
28 29