resurect userdata before calling its GC tag method
This commit is contained in:
41
lgc.c
41
lgc.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lgc.c,v 1.105 2001/06/15 19:17:33 roberto Exp roberto $
|
** $Id: lgc.c,v 1.106 2001/06/15 20:36:57 roberto Exp roberto $
|
||||||
** Garbage Collector
|
** Garbage Collector
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -268,7 +268,7 @@ static void collecttable (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaC_collectudata (lua_State *L) {
|
static void collectudata (lua_State *L, int keep) {
|
||||||
Udata **p = &G(L)->rootudata;
|
Udata **p = &G(L)->rootudata;
|
||||||
Udata *curr;
|
Udata *curr;
|
||||||
while ((curr = *p) != NULL) {
|
while ((curr = *p) != NULL) {
|
||||||
@@ -279,8 +279,13 @@ void luaC_collectudata (lua_State *L) {
|
|||||||
else { /* collect */
|
else { /* collect */
|
||||||
int tag = curr->uv.tag;
|
int tag = curr->uv.tag;
|
||||||
*p = curr->uv.next;
|
*p = curr->uv.next;
|
||||||
curr->uv.next = G(L)->TMtable[tag].collected; /* chain udata */
|
if (keep || /* must keep all of them (to close state)? */
|
||||||
G(L)->TMtable[tag].collected = curr;
|
luaT_gettm(G(L), tag, TM_GC)) { /* or is there a GC tag method? */
|
||||||
|
curr->uv.next = G(L)->TMtable[tag].collected; /* chain udata ... */
|
||||||
|
G(L)->TMtable[tag].collected = curr; /* ... to call its TM later */
|
||||||
|
}
|
||||||
|
else /* no tag method; delete udata */
|
||||||
|
luaM_free(L, curr, sizeudata(curr->uv.len));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -327,7 +332,6 @@ static void callgcTM (lua_State *L, const TObject *obj) {
|
|||||||
int oldah = L->allowhooks;
|
int oldah = L->allowhooks;
|
||||||
StkId top = L->top;
|
StkId top = L->top;
|
||||||
L->allowhooks = 0; /* stop debug hooks during GC tag methods */
|
L->allowhooks = 0; /* stop debug hooks during GC tag methods */
|
||||||
luaD_checkstack(L, 2);
|
|
||||||
setclvalue(top, tm);
|
setclvalue(top, tm);
|
||||||
setobj(top+1, obj);
|
setobj(top+1, obj);
|
||||||
L->top += 2;
|
L->top += 2;
|
||||||
@@ -338,23 +342,35 @@ static void callgcTM (lua_State *L, const TObject *obj) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaC_callgcTMudata (lua_State *L) {
|
static void callgcTMudata (lua_State *L) {
|
||||||
int tag;
|
int tag;
|
||||||
|
luaD_checkstack(L, 3);
|
||||||
for (tag=G(L)->ntag-1; tag>=0; tag--) { /* for each tag (in reverse order) */
|
for (tag=G(L)->ntag-1; tag>=0; tag--) { /* for each tag (in reverse order) */
|
||||||
Udata *udata;
|
Udata *udata;
|
||||||
while ((udata = G(L)->TMtable[tag].collected) != NULL) {
|
while ((udata = G(L)->TMtable[tag].collected) != NULL) {
|
||||||
TObject obj;
|
|
||||||
G(L)->TMtable[tag].collected = udata->uv.next; /* remove it from list */
|
G(L)->TMtable[tag].collected = udata->uv.next; /* remove it from list */
|
||||||
setuvalue(&obj, udata);
|
udata->uv.next = G(L)->rootudata; /* resurect it */
|
||||||
callgcTM(L, &obj);
|
G(L)->rootudata = udata;
|
||||||
luaM_free(L, udata, sizeudata(udata->uv.len));
|
setuvalue(L->top, udata);
|
||||||
|
L->top++; /* keep it in stack to avoid being (recursively) collected */
|
||||||
|
callgcTM(L, L->top-1);
|
||||||
|
uvalue(L->top-1)->uv.tag = 0; /* default tag (udata is `finalized') */
|
||||||
|
L->top--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaC_callallgcTM (lua_State *L) {
|
||||||
|
if (G(L)->rootudata) { /* avoid problems with incomplete states */
|
||||||
|
collectudata(L, 1); /* collect all udata into tag lists */
|
||||||
|
callgcTMudata(L); /* call their GC tag methods */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaC_collect (lua_State *L, int all) {
|
void luaC_collect (lua_State *L, int all) {
|
||||||
luaC_collectudata(L);
|
collectudata(L, 0);
|
||||||
collectstrings(L, all);
|
collectstrings(L, all);
|
||||||
collecttable(L);
|
collecttable(L);
|
||||||
collectproto(L);
|
collectproto(L);
|
||||||
@@ -367,9 +383,8 @@ void luaC_collectgarbage (lua_State *L) {
|
|||||||
cleartables(G(L));
|
cleartables(G(L));
|
||||||
luaC_collect(L, 0);
|
luaC_collect(L, 0);
|
||||||
checkMbuffer(L);
|
checkMbuffer(L);
|
||||||
G(L)->GCthreshold = 2*G(L)->nblocks; /* temporary threshold (for TM) */
|
|
||||||
luaC_callgcTMudata(L);
|
|
||||||
G(L)->GCthreshold = 2*G(L)->nblocks; /* new threshold */
|
G(L)->GCthreshold = 2*G(L)->nblocks; /* new threshold */
|
||||||
|
callgcTMudata(L);
|
||||||
callgcTM(L, &luaO_nilobject);
|
callgcTM(L, &luaO_nilobject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
5
lgc.h
5
lgc.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lgc.h,v 1.10 2001/06/05 19:27:32 roberto Exp roberto $
|
** $Id: lgc.h,v 1.11 2001/06/12 18:43:13 roberto Exp roberto $
|
||||||
** Garbage Collector
|
** Garbage Collector
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -15,8 +15,7 @@
|
|||||||
luaC_collectgarbage(L)
|
luaC_collectgarbage(L)
|
||||||
|
|
||||||
|
|
||||||
void luaC_collectudata (lua_State *L);
|
void luaC_callallgcTM (lua_State *L);
|
||||||
void luaC_callgcTMudata (lua_State *L);
|
|
||||||
void luaC_collect (lua_State *L, int all);
|
void luaC_collect (lua_State *L, int all);
|
||||||
void luaC_collectgarbage (lua_State *L);
|
void luaC_collectgarbage (lua_State *L);
|
||||||
|
|
||||||
|
|||||||
10
lstate.c
10
lstate.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lstate.c,v 1.63 2001/06/06 18:00:19 roberto Exp roberto $
|
** $Id: lstate.c,v 1.64 2001/06/12 18:43:13 roberto Exp roberto $
|
||||||
** Global State
|
** Global State
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -112,12 +112,10 @@ static void close_state (lua_State *L, lua_State *OL) {
|
|||||||
L->next->previous = L->previous;
|
L->next->previous = L->previous;
|
||||||
}
|
}
|
||||||
else if (G(L)) { /* last thread; close global state */
|
else if (G(L)) { /* last thread; close global state */
|
||||||
while (G(L)->rootudata) {
|
luaC_callallgcTM(L); /* call GC tag methods for all udata */
|
||||||
luaC_collectudata(L); /* collect all user data */
|
luaC_collect(L, 1); /* collect all elements */
|
||||||
luaC_callgcTMudata(L); /* call their tag methods */
|
|
||||||
} /* repeat, as tag methods may create new userdata objects */
|
|
||||||
luaC_collect(L, 1); /* collect all other elements */
|
|
||||||
lua_assert(G(L)->rootproto == NULL);
|
lua_assert(G(L)->rootproto == NULL);
|
||||||
|
lua_assert(G(L)->rootudata == NULL);
|
||||||
lua_assert(G(L)->rootcl == NULL);
|
lua_assert(G(L)->rootcl == NULL);
|
||||||
lua_assert(G(L)->roottable == NULL);
|
lua_assert(G(L)->roottable == NULL);
|
||||||
luaS_freeall(L);
|
luaS_freeall(L);
|
||||||
|
|||||||
Reference in New Issue
Block a user