first version of "lua_close"

This commit is contained in:
Roberto Ierusalimschy 1997-12-01 18:31:25 -02:00
parent 00c122cc29
commit 3393fd7f25
7 changed files with 84 additions and 12 deletions

10
lgc.c
View File

@ -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
** 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;
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;
ttype(&o) = LUA_T_USERDATA;
@ -259,8 +259,8 @@ long lua_collectgarbage (long limit)
freefunc = (TProtoFunc *)listcollect(&(L->rootproto));
freeclos = (Closure *)listcollect(&(L->rootcl));
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) */
luaH_free(freetable);
luaS_free(freestr);

4
lgc.h
View File

@ -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
** See Copyright Notice in lua.h
*/
@ -14,6 +14,8 @@
void luaC_checkGC (void);
TObject* luaC_getref (int ref);
int luaC_ref (TObject *o, int lock);
void luaC_hashcallIM (Hash *l);
void luaC_strcallIM (TaggedString *l);
#endif

View File

@ -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
** See Copyright Notice in lua.h
*/
@ -7,6 +7,8 @@
#include "lbuiltin.h"
#include "ldo.h"
#include "lfunc.h"
#include "lgc.h"
#include "llex.h"
#include "lmem.h"
#include "lstate.h"
@ -48,3 +50,27 @@ void lua_open (void)
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
}

View File

@ -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)
** 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)
{
ts->u.globalval = *newval;

View File

@ -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)
** See Copyright Notice in lua.h
*/
@ -20,5 +20,8 @@ TaggedString *luaS_newfixedstring (char *str);
void luaS_rawsetglobal (TaggedString *ts, TObject *newval);
char *luaS_travsymbol (int (*fn)(TObject *));
int luaS_globaldefined (char *name);
TaggedString *luaS_collectudata (void);
void luaS_freeall (void);
#endif

6
lua.c
View File

@ -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
** 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;
}

3
lua.h
View File

@ -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
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
** e-mail: lua@tecgraf.puc-rio.br
@ -55,6 +55,7 @@ typedef unsigned int lua_Object;
void lua_open (void);
void lua_close (void);
lua_Object lua_settagmethod (int tag, char *event); /* In: new method */
lua_Object lua_gettagmethod (int tag, char *event);