aboutsummaryrefslogtreecommitdiff
path: root/lgc.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2013-09-03 12:37:10 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2013-09-03 12:37:10 -0300
commitaeff4f79fa10caef29617652aa49b77055f4045e (patch)
tree8f411b76c7c0b496c01224a36852ed38527dba3d /lgc.c
parent1bf4faec64aca03e1036235e72675f0617124140 (diff)
downloadlua-aeff4f79fa10caef29617652aa49b77055f4045e.tar.gz
lua-aeff4f79fa10caef29617652aa49b77055f4045e.tar.bz2
lua-aeff4f79fa10caef29617652aa49b77055f4045e.zip
local collection now calls finalizers
Diffstat (limited to 'lgc.c')
-rw-r--r--lgc.c120
1 files changed, 87 insertions, 33 deletions
diff --git a/lgc.c b/lgc.c
index aaa57a07..da1d3fdc 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.c,v 2.156 2013/08/29 13:34:16 roberto Exp roberto $ 2** $Id: lgc.c,v 2.157 2013/08/30 19:14:26 roberto Exp roberto $
3** Garbage Collector 3** Garbage Collector
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -777,8 +777,16 @@ static GCObject *udata2finalize (global_State *g) {
777 GCObject *o = g->tobefnz; /* get first element */ 777 GCObject *o = g->tobefnz; /* get first element */
778 lua_assert(tofinalize(o)); 778 lua_assert(tofinalize(o));
779 g->tobefnz = gch(o)->next; /* remove it from 'tobefnz' list */ 779 g->tobefnz = gch(o)->next; /* remove it from 'tobefnz' list */
780 gch(o)->next = g->allgc; /* return it to 'allgc' list */ 780 if (islocal(o)) {
781 g->allgc = o; 781 lua_assert(!testbit(gch(o)->marked, LOCALMARK));
782 gch(o)->next = g->localgc; /* return it to 'localgc' list */
783 g->localgc = o;
784 }
785 else { /* return it to 'allgc' list */
786 gch(o)->next = g->allgc;
787 g->allgc = o;
788 l_setbit(gch(o)->marked, LOCALMARK);
789 }
782 resetbit(gch(o)->marked, FINALIZEDBIT); /* object is back in 'allgc' */ 790 resetbit(gch(o)->marked, FINALIZEDBIT); /* object is back in 'allgc' */
783 if (!keepinvariant(g)) /* not keeping invariant? */ 791 if (!keepinvariant(g)) /* not keeping invariant? */
784 makewhite(g, o); /* "sweep" object */ 792 makewhite(g, o); /* "sweep" object */
@@ -825,23 +833,38 @@ static void GCTM (lua_State *L, int propagateerrors) {
825 833
826 834
827/* 835/*
828** move all unreachable objects (or 'all' objects) that need 836** call all pending finalizers
829** finalization from list 'finobj' to list 'tobefnz' (to be finalized)
830*/ 837*/
831static void separatetobefnz (lua_State *L, int all) { 838static void callallpendingfinalizers (lua_State *L, int propagateerrors) {
832 global_State *g = G(L); 839 global_State *g = G(L);
833 GCObject **p = &g->finobj; 840 while (g->tobefnz)
841 GCTM(L, propagateerrors);
842}
843
844
845/*
846** find last 'next' field in list 'p' list (to add elements in its end)
847*/
848static GCObject **findlast (GCObject **p) {
849 while (*p != NULL)
850 p = &gch(*p)->next;
851 return p;
852}
853
854
855/*
856** move all unreachable objects (or 'all' objects) that need
857** finalization from list 'p' to list 'tobefnz' (to be finalized)
858*/
859static void separatetobefnz_aux (global_State *g, GCObject **p, int all) {
834 GCObject *curr; 860 GCObject *curr;
835 GCObject **lastnext = &g->tobefnz; 861 GCObject **lastnext = findlast(&g->tobefnz);
836 /* find last 'next' field in 'tobefnz' list (to add elements in its end) */
837 while (*lastnext != NULL)
838 lastnext = &gch(*lastnext)->next;
839 while ((curr = *p) != NULL) { /* traverse all finalizable objects */ 862 while ((curr = *p) != NULL) { /* traverse all finalizable objects */
840 lua_assert(tofinalize(curr)); 863 lua_assert(tofinalize(curr));
841 if (!(iswhite(curr) || all)) /* not being collected? */ 864 if (!(iswhite(curr) || all)) /* not being collected? */
842 p = &gch(curr)->next; /* don't bother with it */ 865 p = &gch(curr)->next; /* don't bother with it */
843 else { 866 else {
844 *p = gch(curr)->next; /* remove 'curr' from 'finobj' list */ 867 *p = gch(curr)->next; /* remove 'curr' from "fin" list */
845 gch(curr)->next = *lastnext; /* link at the end of 'tobefnz' list */ 868 gch(curr)->next = *lastnext; /* link at the end of 'tobefnz' list */
846 *lastnext = curr; 869 *lastnext = curr;
847 lastnext = &gch(curr)->next; 870 lastnext = &gch(curr)->next;
@@ -850,9 +873,15 @@ static void separatetobefnz (lua_State *L, int all) {
850} 873}
851 874
852 875
876static void separatetobefnz (global_State *g, int all) {
877 separatetobefnz_aux(g, &g->localfin, all);
878 separatetobefnz_aux(g, &g->finobj, all);
879}
880
881
853/* 882/*
854** if object 'o' has a finalizer, remove it from 'allgc' list (must 883** if object 'o' has a finalizer, remove it from 'allgc' list (must
855** search the list to find it) and link it in 'finobj' list. 884** search the list to find it) and link it in 'localfin' or 'finobj' list.
856*/ 885*/
857void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { 886void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
858 global_State *g = G(L); 887 global_State *g = G(L);
@@ -869,11 +898,11 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
869 /* search for pointer pointing to 'o' */ 898 /* search for pointer pointing to 'o' */
870 p = (testbit(ho->marked, LOCALMARK)) ? &g->allgc : &g->localgc; 899 p = (testbit(ho->marked, LOCALMARK)) ? &g->allgc : &g->localgc;
871 for (; *p != o; p = &gch(*p)->next) { /* empty */ } 900 for (; *p != o; p = &gch(*p)->next) { /* empty */ }
872 *p = ho->next; /* remove 'o' from 'allgc' list */ 901 *p = ho->next; /* remove 'o' from its list */
873 ho->next = g->finobj; /* link it in list 'finobj' */ 902 p = (testbit(ho->marked, LOCALMARK)) ? &g->finobj : &g->localfin;
874 g->finobj = o; 903 ho->next = *p; /* link it in a "fin" list */
904 *p = o;
875 l_setbit(ho->marked, FINALIZEDBIT); /* mark it as such */ 905 l_setbit(ho->marked, FINALIZEDBIT); /* mark it as such */
876 l_setbit(ho->marked, LOCALMARK); /* not in 'localgc' anymore */
877 if (!keepinvariant(g)) /* not keeping invariant? */ 906 if (!keepinvariant(g)) /* not keeping invariant? */
878 makewhite(g, o); /* "sweep" object */ 907 makewhite(g, o); /* "sweep" object */
879 } 908 }
@@ -918,7 +947,8 @@ static void localmark (global_State *g) {
918} 947}
919 948
920 949
921static void localsweep (lua_State *L, global_State *g, GCObject **p) { 950static void localsweep (lua_State *L, global_State *g) {
951 GCObject **p = &g->localgc;
922 while (*p != NULL) { 952 while (*p != NULL) {
923 GCObject *curr = *p; 953 GCObject *curr = *p;
924 if (!islocal(curr)) { /* is 'curr' no more local? */ 954 if (!islocal(curr)) { /* is 'curr' no more local? */
@@ -946,11 +976,41 @@ static void localsweep (lua_State *L, global_State *g, GCObject **p) {
946} 976}
947 977
948 978
979static void separatelocal (global_State *g, int all) {
980 GCObject **p = &g->localfin;
981 GCObject **lastnext = findlast(&g->tobefnz);
982 while (*p != NULL) {
983 GCObject *curr = *p;
984 if (!islocal(curr)) { /* is 'curr' no more local? */
985 *p = curr->gch.next; /* remove 'curr' from list */
986 curr->gch.next = g->finobj; /* link 'curr' in 'finobj' list */
987 g->finobj = curr;
988 /* mark it as out of local list */
989 l_setbit(curr->gch.marked, LOCALMARK);
990 }
991 else { /* still local */
992 if (testbit(curr->gch.marked, LOCALMARK) && !all) { /* locally alive? */
993 resetbit(curr->gch.marked, LOCALMARK);
994 p = &curr->gch.next; /* go to next element */
995 }
996 else { /* object is "dead" */
997 *p = curr->gch.next; /* remove 'curr' from list */
998 curr->gch.next = *lastnext; /* link at the end of 'tobefnz' list */
999 *lastnext = curr;
1000 lastnext = &curr->gch.next;
1001 }
1002 }
1003 }
1004}
1005
1006
949static void luaC_localcollection (lua_State *L) { 1007static void luaC_localcollection (lua_State *L) {
950 global_State *g = G(L); 1008 global_State *g = G(L);
951 lua_assert(g->gcstate == GCSpause); 1009 lua_assert(g->gcstate == GCSpause);
952 localmark(g); 1010 localmark(g);
953 localsweep(L, g, &g->localgc); 1011 localsweep(L, g);
1012 separatelocal(g, 0);
1013 callallpendingfinalizers(L, 1);
954} 1014}
955 1015
956/* }====================================================== */ 1016/* }====================================================== */
@@ -997,24 +1057,15 @@ static int entersweep (lua_State *L) {
997} 1057}
998 1058
999 1059
1000/*
1001** call all pending finalizers
1002*/
1003static void callallpendingfinalizers (lua_State *L, int propagateerrors) {
1004 global_State *g = G(L);
1005 while (g->tobefnz)
1006 GCTM(L, propagateerrors);
1007}
1008
1009
1010void luaC_freeallobjects (lua_State *L) { 1060void luaC_freeallobjects (lua_State *L) {
1011 global_State *g = G(L); 1061 global_State *g = G(L);
1012 separatetobefnz(L, 1); /* separate all objects with finalizers */ 1062 separatetobefnz(g, 1); /* separate all objects with finalizers */
1013 lua_assert(g->finobj == NULL); 1063 lua_assert(g->finobj == NULL && g->localfin == NULL);
1014 callallpendingfinalizers(L, 0); 1064 callallpendingfinalizers(L, 0);
1015 g->currentwhite = WHITEBITS; /* this "white" makes all objects look dead */ 1065 g->currentwhite = WHITEBITS; /* this "white" makes all objects look dead */
1016 g->gckind = KGC_NORMAL; 1066 g->gckind = KGC_NORMAL;
1017 sweepwholelist(L, &g->finobj); /* finalizers can create objs. in 'finobj' */ 1067 sweepwholelist(L, &g->localfin); /* finalizers can create objs. with fins. */
1068 sweepwholelist(L, &g->finobj);
1018 sweepwholelist(L, &g->localgc); 1069 sweepwholelist(L, &g->localgc);
1019 sweepwholelist(L, &g->allgc); 1070 sweepwholelist(L, &g->allgc);
1020 sweepwholelist(L, &g->fixedgc); /* collect fixed objects */ 1071 sweepwholelist(L, &g->fixedgc); /* collect fixed objects */
@@ -1045,7 +1096,7 @@ static l_mem atomic (lua_State *L) {
1045 clearvalues(g, g->allweak, NULL); 1096 clearvalues(g, g->allweak, NULL);
1046 origweak = g->weak; origall = g->allweak; 1097 origweak = g->weak; origall = g->allweak;
1047 work += g->GCmemtrav; /* stop counting (objects being finalized) */ 1098 work += g->GCmemtrav; /* stop counting (objects being finalized) */
1048 separatetobefnz(L, 0); /* separate objects to be finalized */ 1099 separatetobefnz(g, 0); /* separate objects to be finalized */
1049 markbeingfnz(g); /* mark objects that will be finalized */ 1100 markbeingfnz(g); /* mark objects that will be finalized */
1050 propagateall(g); /* remark, to propagate `preserveness' */ 1101 propagateall(g); /* remark, to propagate `preserveness' */
1051 work -= g->GCmemtrav; /* restart counting */ 1102 work -= g->GCmemtrav; /* restart counting */
@@ -1106,6 +1157,9 @@ static lu_mem singlestep (lua_State *L) {
1106 return work + sw * GCSWEEPCOST; 1157 return work + sw * GCSWEEPCOST;
1107 } 1158 }
1108 case GCSsweeplocal: { 1159 case GCSsweeplocal: {
1160 return sweepstep(L, g, GCSsweeplocfin, &g->localfin);
1161 }
1162 case GCSsweeplocfin: {
1109 return sweepstep(L, g, GCSsweepfin, &g->finobj); 1163 return sweepstep(L, g, GCSsweepfin, &g->finobj);
1110 } 1164 }
1111 case GCSsweepfin: { 1165 case GCSsweepfin: {