瀏覽代碼

memory overflow tries a garbage collection; if it fails then exit the
program.

Roberto Ierusalimschy 29 年之前
父節點
當前提交
e74b250d71
共有 3 個文件被更改,包括 31 次插入9 次删除
  1. 17 3
      luamem.c
  2. 12 5
      table.c
  3. 2 1
      table.h

+ 17 - 3
luamem.c

@@ -3,13 +3,27 @@
 ** TecCGraf - PUC-Rio
 ** 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 <stdlib.h>
 #include <stdlib.h>
 #include <string.h>
 #include <string.h>
+#include <stdio.h>
 
 
 #include "mem.h"
 #include "mem.h"
 #include "lua.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)
 void luaI_free (void *block)
 {
 {
@@ -22,7 +36,7 @@ void *luaI_malloc (unsigned long size)
 {
 {
   void *block = malloc((size_t)size);
   void *block = malloc((size_t)size);
   if (block == NULL)
   if (block == NULL)
-    lua_error("not enough memory");
+    mem_error();
   return block;
   return block;
 }
 }
 
 
@@ -31,7 +45,7 @@ void *luaI_realloc (void *oldblock, unsigned long size)
 {
 {
   void *block = realloc(oldblock, (size_t)size);
   void *block = realloc(oldblock, (size_t)size);
   if (block == NULL)
   if (block == NULL)
-    lua_error("not enough memory");
+    mem_error();
   return block;
   return block;
 }
 }
 
 

+ 12 - 5
table.c

@@ -3,7 +3,7 @@
 ** Module to control static tables
 ** 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 <string.h>*/
 /*#include <string.h>*/
 
 
@@ -178,12 +178,9 @@ int lua_markobject (Object *o)
 ** Garbage collection. 
 ** Garbage collection. 
 ** Delete all unused strings and arrays.
 ** 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;
   Long recovered = 0;
-  if (nentity++ < block) return;
   lua_travstack(lua_markobject); /* mark stack objects */
   lua_travstack(lua_markobject); /* mark stack objects */
   lua_travsymbol(lua_markobject); /* mark symbol table objects */
   lua_travsymbol(lua_markobject); /* mark symbol table objects */
   luaI_travlock(lua_markobject); /* mark locked objects */
   luaI_travlock(lua_markobject); /* mark locked objects */
@@ -191,6 +188,16 @@ void lua_pack (void)
   recovered += lua_strcollector();
   recovered += lua_strcollector();
   recovered += lua_hashcollector();
   recovered += lua_hashcollector();
   recovered += luaI_funccollector();
   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 */
   nentity = 0;				/* reset counter */
   block=(16*block-7*recovered)/12;	/* adapt block size */
   block=(16*block-7*recovered)/12;	/* adapt block size */
   if (block < MIN_GARBAGE_BLOCK) block = MIN_GARBAGE_BLOCK;
   if (block < MIN_GARBAGE_BLOCK) block = MIN_GARBAGE_BLOCK;

+ 2 - 1
table.h

@@ -1,7 +1,7 @@
 /*
 /*
 ** Module to control static tables
 ** Module to control static tables
 ** TeCGraf - PUC-Rio
 ** 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
 #ifndef table_h
@@ -23,6 +23,7 @@ Word  luaI_findsymbol      (TreeNode *t);
 Word  luaI_findconstant    (TreeNode *t);
 Word  luaI_findconstant    (TreeNode *t);
 Word  luaI_findconstantbyname (char *name);
 Word  luaI_findconstantbyname (char *name);
 int   lua_markobject      (Object *o);
 int   lua_markobject      (Object *o);
+Long luaI_collectgarbage (void);
 void  lua_pack            (void);
 void  lua_pack            (void);