Browse Source

first version of "lua_close"

Roberto Ierusalimschy 28 years ago
parent
commit
3393fd7f25
7 changed files with 84 additions and 12 deletions
  1. 5 5
      lgc.c
  2. 3 1
      lgc.h
  3. 27 1
      lstate.c
  4. 39 1
      lstring.c
  5. 4 1
      lstring.h
  6. 4 2
      lua.c
  7. 2 1
      lua.h

+ 5 - 5
lgc.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lgc.c,v 1.8 1997/11/19 17:29:23 roberto Exp roberto $
+** $Id: lgc.c,v 1.9 1997/11/27 15:59:25 roberto Exp roberto $
 ** Garbage Collector
 ** Garbage Collector
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -108,7 +108,7 @@ static void invalidaterefs (void)
 
 
 
 
 
 
-static void hashcallIM (Hash *l)
+void luaC_hashcallIM (Hash *l)
 {
 {
   TObject t;
   TObject t;
   ttype(&t) = LUA_T_ARRAY;
   ttype(&t) = LUA_T_ARRAY;
@@ -119,7 +119,7 @@ static void hashcallIM (Hash *l)
 }
 }
 
 
 
 
-static void strcallIM (TaggedString *l)
+void luaC_strcallIM (TaggedString *l)
 {
 {
   TObject o;
   TObject o;
   ttype(&o) = LUA_T_USERDATA;
   ttype(&o) = LUA_T_USERDATA;
@@ -259,8 +259,8 @@ long lua_collectgarbage (long limit)
   freefunc = (TProtoFunc *)listcollect(&(L->rootproto));
   freefunc = (TProtoFunc *)listcollect(&(L->rootproto));
   freeclos = (Closure *)listcollect(&(L->rootcl));
   freeclos = (Closure *)listcollect(&(L->rootcl));
   L->GCthreshold *= 4;  /* to avoid GC during GC */
   L->GCthreshold *= 4;  /* to avoid GC during GC */
-  hashcallIM(freetable);  /* GC tag methods for tables */
-  strcallIM(freestr);  /* GC tag methods for userdata */
+  luaC_hashcallIM(freetable);  /* GC tag methods for tables */
+  luaC_strcallIM(freestr);  /* GC tag methods for userdata */
   luaD_gcIM(&luaO_nilobject);  /* GC tag method for nil (signal end of GC) */
   luaD_gcIM(&luaO_nilobject);  /* GC tag method for nil (signal end of GC) */
   luaH_free(freetable);
   luaH_free(freetable);
   luaS_free(freestr);
   luaS_free(freestr);

+ 3 - 1
lgc.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lgc.h,v 1.2 1997/10/23 16:26:37 roberto Exp roberto $
+** $Id: lgc.h,v 1.3 1997/11/19 17:29:23 roberto Exp roberto $
 ** Garbage Collector
 ** Garbage Collector
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -14,6 +14,8 @@
 void luaC_checkGC (void);
 void luaC_checkGC (void);
 TObject* luaC_getref (int ref);
 TObject* luaC_getref (int ref);
 int luaC_ref (TObject *o, int lock);
 int luaC_ref (TObject *o, int lock);
+void luaC_hashcallIM (Hash *l);
+void luaC_strcallIM (TaggedString *l);
 
 
 
 
 #endif
 #endif

+ 27 - 1
lstate.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lstate.c,v 1.1 1997/11/19 17:31:19 roberto Exp roberto $
+** $Id: lstate.c,v 1.2 1997/11/27 15:59:25 roberto Exp roberto $
 ** Global State
 ** Global State
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -7,6 +7,8 @@
 
 
 #include "lbuiltin.h"
 #include "lbuiltin.h"
 #include "ldo.h"
 #include "ldo.h"
+#include "lfunc.h"
+#include "lgc.h"
 #include "llex.h"
 #include "llex.h"
 #include "lmem.h"
 #include "lmem.h"
 #include "lstate.h"
 #include "lstate.h"
@@ -48,3 +50,27 @@ void lua_open (void)
   luaB_predefine();
   luaB_predefine();
 }
 }
 
 
+
+void lua_close (void)
+{
+  TaggedString *alludata = luaS_collectudata();
+  L->GCthreshold = MAX_INT;  /* to avoid GC during GC */
+  luaC_hashcallIM((Hash *)L->roottable.next);  /* GC t.methods for tables */
+  luaC_strcallIM(alludata);  /* GC tag methods for userdata */
+  luaD_gcIM(&luaO_nilobject);  /* GC tag method for nil (signal end of GC) */
+  luaH_free((Hash *)L->roottable.next);
+  luaF_freeproto((TProtoFunc *)L->rootproto.next);
+  luaF_freeclosure((Closure *)L->rootcl.next);
+  luaS_free(alludata);
+  luaS_freeall();
+  luaM_free(L->stack.stack);
+  luaM_free(L->IMtable);
+  luaM_free(L->refArray);
+  luaM_free(L->Mbuffer);
+  luaM_free(L);
+  L = NULL;
+#if DEBUG
+  printf("total de blocos: %ld\n", numblocks);
+  printf("total de memoria: %ld\n", totalmem);
+#endif
+}

+ 39 - 1
lstring.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lstring.c,v 1.5 1997/11/19 17:29:23 roberto Exp roberto $
+** $Id: lstring.c,v 1.6 1997/11/21 19:00:46 roberto Exp roberto $
 ** String table (keep all strings handled by Lua)
 ** String table (keep all strings handled by Lua)
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -195,6 +195,44 @@ TaggedString *luaS_collector (void)
 }
 }
 
 
 
 
+TaggedString *luaS_collectudata (void)
+{
+  TaggedString *frees = NULL;
+  int i;
+  L->rootglobal.next = NULL;  /* empty list of globals */
+  for (i=0; i<NUM_HASHS; i++) {
+    stringtable *tb = &L->string_root[i];
+    int j;
+    for (j=0; j<tb->size; j++) {
+      TaggedString *t = tb->hash[j];
+      if (t == NULL || t == &EMPTY || t->constindex != -1)
+        continue;  /* get only user datas */
+      t->head.next = (GCnode *)frees;
+      frees = t;
+      tb->hash[j] = &EMPTY;
+    }
+  }
+  return frees;
+}
+
+
+void luaS_freeall (void)
+{
+  int i;
+  for (i=0; i<NUM_HASHS; i++) {
+    stringtable *tb = &L->string_root[i];
+    int j;
+    for (j=0; j<tb->size; j++) {
+      TaggedString *t = tb->hash[j];
+      if (t == &EMPTY) continue;
+      luaM_free(t);
+    }
+    luaM_free(tb->hash);
+  }
+  luaM_free(L->string_root);
+}
+
+
 void luaS_rawsetglobal (TaggedString *ts, TObject *newval)
 void luaS_rawsetglobal (TaggedString *ts, TObject *newval)
 {
 {
   ts->u.globalval = *newval;
   ts->u.globalval = *newval;

+ 4 - 1
lstring.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lstring.h,v 1.4 1997/11/19 17:29:23 roberto Exp roberto $
+** $Id: lstring.h,v 1.5 1997/11/26 18:53:45 roberto Exp roberto $
 ** String table (keep all strings handled by Lua)
 ** String table (keep all strings handled by Lua)
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -20,5 +20,8 @@ TaggedString *luaS_newfixedstring (char *str);
 void luaS_rawsetglobal (TaggedString *ts, TObject *newval);
 void luaS_rawsetglobal (TaggedString *ts, TObject *newval);
 char *luaS_travsymbol (int (*fn)(TObject *));
 char *luaS_travsymbol (int (*fn)(TObject *));
 int luaS_globaldefined (char *name);
 int luaS_globaldefined (char *name);
+TaggedString *luaS_collectudata (void);
+void luaS_freeall (void);
+
 
 
 #endif
 #endif

+ 4 - 2
lua.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lua.c,v 1.4 1997/11/19 17:29:23 roberto Exp roberto $
+** $Id: lua.c,v 1.5 1997/11/21 19:00:46 roberto Exp roberto $
 ** Lua stand-alone interpreter
 ** Lua stand-alone interpreter
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -82,7 +82,9 @@ int main (int argc, char *argv[])
       }
       }
     }
     }
   }
   }
-/*  lua_close(); */
+#if DEBUG
+  lua_close();
+#endif
   return 0;
   return 0;
 }
 }
 
 

+ 2 - 1
lua.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lua.h,v 1.6 1997/11/27 15:59:25 roberto Exp roberto $
+** $Id: lua.h,v 1.7 1997/11/27 18:25:14 roberto Exp roberto $
 ** Lua - An Extensible Extension Language
 ** Lua - An Extensible Extension Language
 ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
 ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
 ** e-mail: [email protected]
 ** e-mail: [email protected]
@@ -55,6 +55,7 @@ typedef unsigned int lua_Object;
 
 
 
 
 void	       lua_open			(void);
 void	       lua_open			(void);
+void           lua_close		(void);
 
 
 lua_Object     lua_settagmethod	(int tag, char *event); /* In: new method */
 lua_Object     lua_settagmethod	(int tag, char *event); /* In: new method */
 lua_Object     lua_gettagmethod	(int tag, char *event);
 lua_Object     lua_gettagmethod	(int tag, char *event);