1*088332b5SXin Li /*
2*088332b5SXin Li ** $Id: lgc.c $
3*088332b5SXin Li ** Garbage Collector
4*088332b5SXin Li ** See Copyright Notice in lua.h
5*088332b5SXin Li */
6*088332b5SXin Li
7*088332b5SXin Li #define lgc_c
8*088332b5SXin Li #define LUA_CORE
9*088332b5SXin Li
10*088332b5SXin Li #include "lprefix.h"
11*088332b5SXin Li
12*088332b5SXin Li #include <stdio.h>
13*088332b5SXin Li #include <string.h>
14*088332b5SXin Li
15*088332b5SXin Li
16*088332b5SXin Li #include "lua.h"
17*088332b5SXin Li
18*088332b5SXin Li #include "ldebug.h"
19*088332b5SXin Li #include "ldo.h"
20*088332b5SXin Li #include "lfunc.h"
21*088332b5SXin Li #include "lgc.h"
22*088332b5SXin Li #include "lmem.h"
23*088332b5SXin Li #include "lobject.h"
24*088332b5SXin Li #include "lstate.h"
25*088332b5SXin Li #include "lstring.h"
26*088332b5SXin Li #include "ltable.h"
27*088332b5SXin Li #include "ltm.h"
28*088332b5SXin Li
29*088332b5SXin Li
30*088332b5SXin Li /*
31*088332b5SXin Li ** Maximum number of elements to sweep in each single step.
32*088332b5SXin Li ** (Large enough to dissipate fixed overheads but small enough
33*088332b5SXin Li ** to allow small steps for the collector.)
34*088332b5SXin Li */
35*088332b5SXin Li #define GCSWEEPMAX 100
36*088332b5SXin Li
37*088332b5SXin Li /*
38*088332b5SXin Li ** Maximum number of finalizers to call in each single step.
39*088332b5SXin Li */
40*088332b5SXin Li #define GCFINMAX 10
41*088332b5SXin Li
42*088332b5SXin Li
43*088332b5SXin Li /*
44*088332b5SXin Li ** Cost of calling one finalizer.
45*088332b5SXin Li */
46*088332b5SXin Li #define GCFINALIZECOST 50
47*088332b5SXin Li
48*088332b5SXin Li
49*088332b5SXin Li /*
50*088332b5SXin Li ** The equivalent, in bytes, of one unit of "work" (visiting a slot,
51*088332b5SXin Li ** sweeping an object, etc.)
52*088332b5SXin Li */
53*088332b5SXin Li #define WORK2MEM sizeof(TValue)
54*088332b5SXin Li
55*088332b5SXin Li
56*088332b5SXin Li /*
57*088332b5SXin Li ** macro to adjust 'pause': 'pause' is actually used like
58*088332b5SXin Li ** 'pause / PAUSEADJ' (value chosen by tests)
59*088332b5SXin Li */
60*088332b5SXin Li #define PAUSEADJ 100
61*088332b5SXin Li
62*088332b5SXin Li
63*088332b5SXin Li /* mask with all color bits */
64*088332b5SXin Li #define maskcolors (bitmask(BLACKBIT) | WHITEBITS)
65*088332b5SXin Li
66*088332b5SXin Li /* mask with all GC bits */
67*088332b5SXin Li #define maskgcbits (maskcolors | AGEBITS)
68*088332b5SXin Li
69*088332b5SXin Li
70*088332b5SXin Li /* macro to erase all color bits then set only the current white bit */
71*088332b5SXin Li #define makewhite(g,x) \
72*088332b5SXin Li (x->marked = cast_byte((x->marked & ~maskcolors) | luaC_white(g)))
73*088332b5SXin Li
74*088332b5SXin Li /* make an object gray (neither white nor black) */
75*088332b5SXin Li #define set2gray(x) resetbits(x->marked, maskcolors)
76*088332b5SXin Li
77*088332b5SXin Li
78*088332b5SXin Li /* make an object black (coming from any color) */
79*088332b5SXin Li #define set2black(x) \
80*088332b5SXin Li (x->marked = cast_byte((x->marked & ~WHITEBITS) | bitmask(BLACKBIT)))
81*088332b5SXin Li
82*088332b5SXin Li
83*088332b5SXin Li #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x)))
84*088332b5SXin Li
85*088332b5SXin Li #define keyiswhite(n) (keyiscollectable(n) && iswhite(gckey(n)))
86*088332b5SXin Li
87*088332b5SXin Li
88*088332b5SXin Li /*
89*088332b5SXin Li ** Protected access to objects in values
90*088332b5SXin Li */
91*088332b5SXin Li #define gcvalueN(o) (iscollectable(o) ? gcvalue(o) : NULL)
92*088332b5SXin Li
93*088332b5SXin Li
94*088332b5SXin Li #define markvalue(g,o) { checkliveness(g->mainthread,o); \
95*088332b5SXin Li if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); }
96*088332b5SXin Li
97*088332b5SXin Li #define markkey(g, n) { if keyiswhite(n) reallymarkobject(g,gckey(n)); }
98*088332b5SXin Li
99*088332b5SXin Li #define markobject(g,t) { if (iswhite(t)) reallymarkobject(g, obj2gco(t)); }
100*088332b5SXin Li
101*088332b5SXin Li /*
102*088332b5SXin Li ** mark an object that can be NULL (either because it is really optional,
103*088332b5SXin Li ** or it was stripped as debug info, or inside an uncompleted structure)
104*088332b5SXin Li */
105*088332b5SXin Li #define markobjectN(g,t) { if (t) markobject(g,t); }
106*088332b5SXin Li
107*088332b5SXin Li static void reallymarkobject (global_State *g, GCObject *o);
108*088332b5SXin Li static lu_mem atomic (lua_State *L);
109*088332b5SXin Li static void entersweep (lua_State *L);
110*088332b5SXin Li
111*088332b5SXin Li
112*088332b5SXin Li /*
113*088332b5SXin Li ** {======================================================
114*088332b5SXin Li ** Generic functions
115*088332b5SXin Li ** =======================================================
116*088332b5SXin Li */
117*088332b5SXin Li
118*088332b5SXin Li
119*088332b5SXin Li /*
120*088332b5SXin Li ** one after last element in a hash array
121*088332b5SXin Li */
122*088332b5SXin Li #define gnodelast(h) gnode(h, cast_sizet(sizenode(h)))
123*088332b5SXin Li
124*088332b5SXin Li
getgclist(GCObject * o)125*088332b5SXin Li static GCObject **getgclist (GCObject *o) {
126*088332b5SXin Li switch (o->tt) {
127*088332b5SXin Li case LUA_VTABLE: return &gco2t(o)->gclist;
128*088332b5SXin Li case LUA_VLCL: return &gco2lcl(o)->gclist;
129*088332b5SXin Li case LUA_VCCL: return &gco2ccl(o)->gclist;
130*088332b5SXin Li case LUA_VTHREAD: return &gco2th(o)->gclist;
131*088332b5SXin Li case LUA_VPROTO: return &gco2p(o)->gclist;
132*088332b5SXin Li case LUA_VUSERDATA: {
133*088332b5SXin Li Udata *u = gco2u(o);
134*088332b5SXin Li lua_assert(u->nuvalue > 0);
135*088332b5SXin Li return &u->gclist;
136*088332b5SXin Li }
137*088332b5SXin Li default: lua_assert(0); return 0;
138*088332b5SXin Li }
139*088332b5SXin Li }
140*088332b5SXin Li
141*088332b5SXin Li
142*088332b5SXin Li /*
143*088332b5SXin Li ** Link a collectable object 'o' with a known type into the list 'p'.
144*088332b5SXin Li ** (Must be a macro to access the 'gclist' field in different types.)
145*088332b5SXin Li */
146*088332b5SXin Li #define linkgclist(o,p) linkgclist_(obj2gco(o), &(o)->gclist, &(p))
147*088332b5SXin Li
linkgclist_(GCObject * o,GCObject ** pnext,GCObject ** list)148*088332b5SXin Li static void linkgclist_ (GCObject *o, GCObject **pnext, GCObject **list) {
149*088332b5SXin Li lua_assert(!isgray(o)); /* cannot be in a gray list */
150*088332b5SXin Li *pnext = *list;
151*088332b5SXin Li *list = o;
152*088332b5SXin Li set2gray(o); /* now it is */
153*088332b5SXin Li }
154*088332b5SXin Li
155*088332b5SXin Li
156*088332b5SXin Li /*
157*088332b5SXin Li ** Link a generic collectable object 'o' into the list 'p'.
158*088332b5SXin Li */
159*088332b5SXin Li #define linkobjgclist(o,p) linkgclist_(obj2gco(o), getgclist(o), &(p))
160*088332b5SXin Li
161*088332b5SXin Li
162*088332b5SXin Li
163*088332b5SXin Li /*
164*088332b5SXin Li ** Clear keys for empty entries in tables. If entry is empty
165*088332b5SXin Li ** and its key is not marked, mark its entry as dead. This allows the
166*088332b5SXin Li ** collection of the key, but keeps its entry in the table (its removal
167*088332b5SXin Li ** could break a chain). The main feature of a dead key is that it must
168*088332b5SXin Li ** be different from any other value, to do not disturb searches.
169*088332b5SXin Li ** Other places never manipulate dead keys, because its associated empty
170*088332b5SXin Li ** value is enough to signal that the entry is logically empty.
171*088332b5SXin Li */
clearkey(Node * n)172*088332b5SXin Li static void clearkey (Node *n) {
173*088332b5SXin Li lua_assert(isempty(gval(n)));
174*088332b5SXin Li if (keyiswhite(n))
175*088332b5SXin Li setdeadkey(n); /* unused and unmarked key; remove it */
176*088332b5SXin Li }
177*088332b5SXin Li
178*088332b5SXin Li
179*088332b5SXin Li /*
180*088332b5SXin Li ** tells whether a key or value can be cleared from a weak
181*088332b5SXin Li ** table. Non-collectable objects are never removed from weak
182*088332b5SXin Li ** tables. Strings behave as 'values', so are never removed too. for
183*088332b5SXin Li ** other objects: if really collected, cannot keep them; for objects
184*088332b5SXin Li ** being finalized, keep them in keys, but not in values
185*088332b5SXin Li */
iscleared(global_State * g,const GCObject * o)186*088332b5SXin Li static int iscleared (global_State *g, const GCObject *o) {
187*088332b5SXin Li if (o == NULL) return 0; /* non-collectable value */
188*088332b5SXin Li else if (novariant(o->tt) == LUA_TSTRING) {
189*088332b5SXin Li markobject(g, o); /* strings are 'values', so are never weak */
190*088332b5SXin Li return 0;
191*088332b5SXin Li }
192*088332b5SXin Li else return iswhite(o);
193*088332b5SXin Li }
194*088332b5SXin Li
195*088332b5SXin Li
196*088332b5SXin Li /*
197*088332b5SXin Li ** Barrier that moves collector forward, that is, marks the white object
198*088332b5SXin Li ** 'v' being pointed by the black object 'o'. In the generational
199*088332b5SXin Li ** mode, 'v' must also become old, if 'o' is old; however, it cannot
200*088332b5SXin Li ** be changed directly to OLD, because it may still point to non-old
201*088332b5SXin Li ** objects. So, it is marked as OLD0. In the next cycle it will become
202*088332b5SXin Li ** OLD1, and in the next it will finally become OLD (regular old). By
203*088332b5SXin Li ** then, any object it points to will also be old. If called in the
204*088332b5SXin Li ** incremental sweep phase, it clears the black object to white (sweep
205*088332b5SXin Li ** it) to avoid other barrier calls for this same object. (That cannot
206*088332b5SXin Li ** be done is generational mode, as its sweep does not distinguish
207*088332b5SXin Li ** whites from deads.)
208*088332b5SXin Li */
luaC_barrier_(lua_State * L,GCObject * o,GCObject * v)209*088332b5SXin Li void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) {
210*088332b5SXin Li global_State *g = G(L);
211*088332b5SXin Li lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));
212*088332b5SXin Li if (keepinvariant(g)) { /* must keep invariant? */
213*088332b5SXin Li reallymarkobject(g, v); /* restore invariant */
214*088332b5SXin Li if (isold(o)) {
215*088332b5SXin Li lua_assert(!isold(v)); /* white object could not be old */
216*088332b5SXin Li setage(v, G_OLD0); /* restore generational invariant */
217*088332b5SXin Li }
218*088332b5SXin Li }
219*088332b5SXin Li else { /* sweep phase */
220*088332b5SXin Li lua_assert(issweepphase(g));
221*088332b5SXin Li if (g->gckind == KGC_INC) /* incremental mode? */
222*088332b5SXin Li makewhite(g, o); /* mark 'o' as white to avoid other barriers */
223*088332b5SXin Li }
224*088332b5SXin Li }
225*088332b5SXin Li
226*088332b5SXin Li
227*088332b5SXin Li /*
228*088332b5SXin Li ** barrier that moves collector backward, that is, mark the black object
229*088332b5SXin Li ** pointing to a white object as gray again.
230*088332b5SXin Li */
luaC_barrierback_(lua_State * L,GCObject * o)231*088332b5SXin Li void luaC_barrierback_ (lua_State *L, GCObject *o) {
232*088332b5SXin Li global_State *g = G(L);
233*088332b5SXin Li lua_assert(isblack(o) && !isdead(g, o));
234*088332b5SXin Li lua_assert((g->gckind == KGC_GEN) == (isold(o) && getage(o) != G_TOUCHED1));
235*088332b5SXin Li if (getage(o) == G_TOUCHED2) /* already in gray list? */
236*088332b5SXin Li set2gray(o); /* make it gray to become touched1 */
237*088332b5SXin Li else /* link it in 'grayagain' and paint it gray */
238*088332b5SXin Li linkobjgclist(o, g->grayagain);
239*088332b5SXin Li if (isold(o)) /* generational mode? */
240*088332b5SXin Li setage(o, G_TOUCHED1); /* touched in current cycle */
241*088332b5SXin Li }
242*088332b5SXin Li
243*088332b5SXin Li
luaC_fix(lua_State * L,GCObject * o)244*088332b5SXin Li void luaC_fix (lua_State *L, GCObject *o) {
245*088332b5SXin Li global_State *g = G(L);
246*088332b5SXin Li lua_assert(g->allgc == o); /* object must be 1st in 'allgc' list! */
247*088332b5SXin Li set2gray(o); /* they will be gray forever */
248*088332b5SXin Li setage(o, G_OLD); /* and old forever */
249*088332b5SXin Li g->allgc = o->next; /* remove object from 'allgc' list */
250*088332b5SXin Li o->next = g->fixedgc; /* link it to 'fixedgc' list */
251*088332b5SXin Li g->fixedgc = o;
252*088332b5SXin Li }
253*088332b5SXin Li
254*088332b5SXin Li
255*088332b5SXin Li /*
256*088332b5SXin Li ** create a new collectable object (with given type and size) and link
257*088332b5SXin Li ** it to 'allgc' list.
258*088332b5SXin Li */
luaC_newobj(lua_State * L,int tt,size_t sz)259*088332b5SXin Li GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) {
260*088332b5SXin Li global_State *g = G(L);
261*088332b5SXin Li GCObject *o = cast(GCObject *, luaM_newobject(L, novariant(tt), sz));
262*088332b5SXin Li o->marked = luaC_white(g);
263*088332b5SXin Li o->tt = tt;
264*088332b5SXin Li o->next = g->allgc;
265*088332b5SXin Li g->allgc = o;
266*088332b5SXin Li return o;
267*088332b5SXin Li }
268*088332b5SXin Li
269*088332b5SXin Li /* }====================================================== */
270*088332b5SXin Li
271*088332b5SXin Li
272*088332b5SXin Li
273*088332b5SXin Li /*
274*088332b5SXin Li ** {======================================================
275*088332b5SXin Li ** Mark functions
276*088332b5SXin Li ** =======================================================
277*088332b5SXin Li */
278*088332b5SXin Li
279*088332b5SXin Li
280*088332b5SXin Li /*
281*088332b5SXin Li ** Mark an object. Userdata with no user values, strings, and closed
282*088332b5SXin Li ** upvalues are visited and turned black here. Open upvalues are
283*088332b5SXin Li ** already indirectly linked through their respective threads in the
284*088332b5SXin Li ** 'twups' list, so they don't go to the gray list; nevertheless, they
285*088332b5SXin Li ** are kept gray to avoid barriers, as their values will be revisited
286*088332b5SXin Li ** by the thread or by 'remarkupvals'. Other objects are added to the
287*088332b5SXin Li ** gray list to be visited (and turned black) later. Both userdata and
288*088332b5SXin Li ** upvalues can call this function recursively, but this recursion goes
289*088332b5SXin Li ** for at most two levels: An upvalue cannot refer to another upvalue
290*088332b5SXin Li ** (only closures can), and a userdata's metatable must be a table.
291*088332b5SXin Li */
reallymarkobject(global_State * g,GCObject * o)292*088332b5SXin Li static void reallymarkobject (global_State *g, GCObject *o) {
293*088332b5SXin Li switch (o->tt) {
294*088332b5SXin Li case LUA_VSHRSTR:
295*088332b5SXin Li case LUA_VLNGSTR: {
296*088332b5SXin Li set2black(o); /* nothing to visit */
297*088332b5SXin Li break;
298*088332b5SXin Li }
299*088332b5SXin Li case LUA_VUPVAL: {
300*088332b5SXin Li UpVal *uv = gco2upv(o);
301*088332b5SXin Li if (upisopen(uv))
302*088332b5SXin Li set2gray(uv); /* open upvalues are kept gray */
303*088332b5SXin Li else
304*088332b5SXin Li set2black(o); /* closed upvalues are visited here */
305*088332b5SXin Li markvalue(g, uv->v); /* mark its content */
306*088332b5SXin Li break;
307*088332b5SXin Li }
308*088332b5SXin Li case LUA_VUSERDATA: {
309*088332b5SXin Li Udata *u = gco2u(o);
310*088332b5SXin Li if (u->nuvalue == 0) { /* no user values? */
311*088332b5SXin Li markobjectN(g, u->metatable); /* mark its metatable */
312*088332b5SXin Li set2black(o); /* nothing else to mark */
313*088332b5SXin Li break;
314*088332b5SXin Li }
315*088332b5SXin Li /* else... */
316*088332b5SXin Li } /* FALLTHROUGH */
317*088332b5SXin Li case LUA_VLCL: case LUA_VCCL: case LUA_VTABLE:
318*088332b5SXin Li case LUA_VTHREAD: case LUA_VPROTO: {
319*088332b5SXin Li linkobjgclist(o, g->gray); /* to be visited later */
320*088332b5SXin Li break;
321*088332b5SXin Li }
322*088332b5SXin Li default: lua_assert(0); break;
323*088332b5SXin Li }
324*088332b5SXin Li }
325*088332b5SXin Li
326*088332b5SXin Li
327*088332b5SXin Li /*
328*088332b5SXin Li ** mark metamethods for basic types
329*088332b5SXin Li */
markmt(global_State * g)330*088332b5SXin Li static void markmt (global_State *g) {
331*088332b5SXin Li int i;
332*088332b5SXin Li for (i=0; i < LUA_NUMTAGS; i++)
333*088332b5SXin Li markobjectN(g, g->mt[i]);
334*088332b5SXin Li }
335*088332b5SXin Li
336*088332b5SXin Li
337*088332b5SXin Li /*
338*088332b5SXin Li ** mark all objects in list of being-finalized
339*088332b5SXin Li */
markbeingfnz(global_State * g)340*088332b5SXin Li static lu_mem markbeingfnz (global_State *g) {
341*088332b5SXin Li GCObject *o;
342*088332b5SXin Li lu_mem count = 0;
343*088332b5SXin Li for (o = g->tobefnz; o != NULL; o = o->next) {
344*088332b5SXin Li count++;
345*088332b5SXin Li markobject(g, o);
346*088332b5SXin Li }
347*088332b5SXin Li return count;
348*088332b5SXin Li }
349*088332b5SXin Li
350*088332b5SXin Li
351*088332b5SXin Li /*
352*088332b5SXin Li ** For each non-marked thread, simulates a barrier between each open
353*088332b5SXin Li ** upvalue and its value. (If the thread is collected, the value will be
354*088332b5SXin Li ** assigned to the upvalue, but then it can be too late for the barrier
355*088332b5SXin Li ** to act. The "barrier" does not need to check colors: A non-marked
356*088332b5SXin Li ** thread must be young; upvalues cannot be older than their threads; so
357*088332b5SXin Li ** any visited upvalue must be young too.) Also removes the thread from
358*088332b5SXin Li ** the list, as it was already visited. Removes also threads with no
359*088332b5SXin Li ** upvalues, as they have nothing to be checked. (If the thread gets an
360*088332b5SXin Li ** upvalue later, it will be linked in the list again.)
361*088332b5SXin Li */
remarkupvals(global_State * g)362*088332b5SXin Li static int remarkupvals (global_State *g) {
363*088332b5SXin Li lua_State *thread;
364*088332b5SXin Li lua_State **p = &g->twups;
365*088332b5SXin Li int work = 0; /* estimate of how much work was done here */
366*088332b5SXin Li while ((thread = *p) != NULL) {
367*088332b5SXin Li work++;
368*088332b5SXin Li if (!iswhite(thread) && thread->openupval != NULL)
369*088332b5SXin Li p = &thread->twups; /* keep marked thread with upvalues in the list */
370*088332b5SXin Li else { /* thread is not marked or without upvalues */
371*088332b5SXin Li UpVal *uv;
372*088332b5SXin Li lua_assert(!isold(thread) || thread->openupval == NULL);
373*088332b5SXin Li *p = thread->twups; /* remove thread from the list */
374*088332b5SXin Li thread->twups = thread; /* mark that it is out of list */
375*088332b5SXin Li for (uv = thread->openupval; uv != NULL; uv = uv->u.open.next) {
376*088332b5SXin Li lua_assert(getage(uv) <= getage(thread));
377*088332b5SXin Li work++;
378*088332b5SXin Li if (!iswhite(uv)) { /* upvalue already visited? */
379*088332b5SXin Li lua_assert(upisopen(uv) && isgray(uv));
380*088332b5SXin Li markvalue(g, uv->v); /* mark its value */
381*088332b5SXin Li }
382*088332b5SXin Li }
383*088332b5SXin Li }
384*088332b5SXin Li }
385*088332b5SXin Li return work;
386*088332b5SXin Li }
387*088332b5SXin Li
388*088332b5SXin Li
cleargraylists(global_State * g)389*088332b5SXin Li static void cleargraylists (global_State *g) {
390*088332b5SXin Li g->gray = g->grayagain = NULL;
391*088332b5SXin Li g->weak = g->allweak = g->ephemeron = NULL;
392*088332b5SXin Li }
393*088332b5SXin Li
394*088332b5SXin Li
395*088332b5SXin Li /*
396*088332b5SXin Li ** mark root set and reset all gray lists, to start a new collection
397*088332b5SXin Li */
restartcollection(global_State * g)398*088332b5SXin Li static void restartcollection (global_State *g) {
399*088332b5SXin Li cleargraylists(g);
400*088332b5SXin Li markobject(g, g->mainthread);
401*088332b5SXin Li markvalue(g, &g->l_registry);
402*088332b5SXin Li markmt(g);
403*088332b5SXin Li markbeingfnz(g); /* mark any finalizing object left from previous cycle */
404*088332b5SXin Li }
405*088332b5SXin Li
406*088332b5SXin Li /* }====================================================== */
407*088332b5SXin Li
408*088332b5SXin Li
409*088332b5SXin Li /*
410*088332b5SXin Li ** {======================================================
411*088332b5SXin Li ** Traverse functions
412*088332b5SXin Li ** =======================================================
413*088332b5SXin Li */
414*088332b5SXin Li
415*088332b5SXin Li
416*088332b5SXin Li /*
417*088332b5SXin Li ** Check whether object 'o' should be kept in the 'grayagain' list for
418*088332b5SXin Li ** post-processing by 'correctgraylist'. (It could put all old objects
419*088332b5SXin Li ** in the list and leave all the work to 'correctgraylist', but it is
420*088332b5SXin Li ** more efficient to avoid adding elements that will be removed.) Only
421*088332b5SXin Li ** TOUCHED1 objects need to be in the list. TOUCHED2 doesn't need to go
422*088332b5SXin Li ** back to a gray list, but then it must become OLD. (That is what
423*088332b5SXin Li ** 'correctgraylist' does when it finds a TOUCHED2 object.)
424*088332b5SXin Li */
genlink(global_State * g,GCObject * o)425*088332b5SXin Li static void genlink (global_State *g, GCObject *o) {
426*088332b5SXin Li lua_assert(isblack(o));
427*088332b5SXin Li if (getage(o) == G_TOUCHED1) { /* touched in this cycle? */
428*088332b5SXin Li linkobjgclist(o, g->grayagain); /* link it back in 'grayagain' */
429*088332b5SXin Li } /* everything else do not need to be linked back */
430*088332b5SXin Li else if (getage(o) == G_TOUCHED2)
431*088332b5SXin Li changeage(o, G_TOUCHED2, G_OLD); /* advance age */
432*088332b5SXin Li }
433*088332b5SXin Li
434*088332b5SXin Li
435*088332b5SXin Li /*
436*088332b5SXin Li ** Traverse a table with weak values and link it to proper list. During
437*088332b5SXin Li ** propagate phase, keep it in 'grayagain' list, to be revisited in the
438*088332b5SXin Li ** atomic phase. In the atomic phase, if table has any white value,
439*088332b5SXin Li ** put it in 'weak' list, to be cleared.
440*088332b5SXin Li */
traverseweakvalue(global_State * g,Table * h)441*088332b5SXin Li static void traverseweakvalue (global_State *g, Table *h) {
442*088332b5SXin Li Node *n, *limit = gnodelast(h);
443*088332b5SXin Li /* if there is array part, assume it may have white values (it is not
444*088332b5SXin Li worth traversing it now just to check) */
445*088332b5SXin Li int hasclears = (h->alimit > 0);
446*088332b5SXin Li for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */
447*088332b5SXin Li if (isempty(gval(n))) /* entry is empty? */
448*088332b5SXin Li clearkey(n); /* clear its key */
449*088332b5SXin Li else {
450*088332b5SXin Li lua_assert(!keyisnil(n));
451*088332b5SXin Li markkey(g, n);
452*088332b5SXin Li if (!hasclears && iscleared(g, gcvalueN(gval(n)))) /* a white value? */
453*088332b5SXin Li hasclears = 1; /* table will have to be cleared */
454*088332b5SXin Li }
455*088332b5SXin Li }
456*088332b5SXin Li if (g->gcstate == GCSatomic && hasclears)
457*088332b5SXin Li linkgclist(h, g->weak); /* has to be cleared later */
458*088332b5SXin Li else
459*088332b5SXin Li linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */
460*088332b5SXin Li }
461*088332b5SXin Li
462*088332b5SXin Li
463*088332b5SXin Li /*
464*088332b5SXin Li ** Traverse an ephemeron table and link it to proper list. Returns true
465*088332b5SXin Li ** iff any object was marked during this traversal (which implies that
466*088332b5SXin Li ** convergence has to continue). During propagation phase, keep table
467*088332b5SXin Li ** in 'grayagain' list, to be visited again in the atomic phase. In
468*088332b5SXin Li ** the atomic phase, if table has any white->white entry, it has to
469*088332b5SXin Li ** be revisited during ephemeron convergence (as that key may turn
470*088332b5SXin Li ** black). Otherwise, if it has any white key, table has to be cleared
471*088332b5SXin Li ** (in the atomic phase). In generational mode, some tables
472*088332b5SXin Li ** must be kept in some gray list for post-processing; this is done
473*088332b5SXin Li ** by 'genlink'.
474*088332b5SXin Li */
traverseephemeron(global_State * g,Table * h,int inv)475*088332b5SXin Li static int traverseephemeron (global_State *g, Table *h, int inv) {
476*088332b5SXin Li int marked = 0; /* true if an object is marked in this traversal */
477*088332b5SXin Li int hasclears = 0; /* true if table has white keys */
478*088332b5SXin Li int hasww = 0; /* true if table has entry "white-key -> white-value" */
479*088332b5SXin Li unsigned int i;
480*088332b5SXin Li unsigned int asize = luaH_realasize(h);
481*088332b5SXin Li unsigned int nsize = sizenode(h);
482*088332b5SXin Li /* traverse array part */
483*088332b5SXin Li for (i = 0; i < asize; i++) {
484*088332b5SXin Li if (valiswhite(&h->array[i])) {
485*088332b5SXin Li marked = 1;
486*088332b5SXin Li reallymarkobject(g, gcvalue(&h->array[i]));
487*088332b5SXin Li }
488*088332b5SXin Li }
489*088332b5SXin Li /* traverse hash part; if 'inv', traverse descending
490*088332b5SXin Li (see 'convergeephemerons') */
491*088332b5SXin Li for (i = 0; i < nsize; i++) {
492*088332b5SXin Li Node *n = inv ? gnode(h, nsize - 1 - i) : gnode(h, i);
493*088332b5SXin Li if (isempty(gval(n))) /* entry is empty? */
494*088332b5SXin Li clearkey(n); /* clear its key */
495*088332b5SXin Li else if (iscleared(g, gckeyN(n))) { /* key is not marked (yet)? */
496*088332b5SXin Li hasclears = 1; /* table must be cleared */
497*088332b5SXin Li if (valiswhite(gval(n))) /* value not marked yet? */
498*088332b5SXin Li hasww = 1; /* white-white entry */
499*088332b5SXin Li }
500*088332b5SXin Li else if (valiswhite(gval(n))) { /* value not marked yet? */
501*088332b5SXin Li marked = 1;
502*088332b5SXin Li reallymarkobject(g, gcvalue(gval(n))); /* mark it now */
503*088332b5SXin Li }
504*088332b5SXin Li }
505*088332b5SXin Li /* link table into proper list */
506*088332b5SXin Li if (g->gcstate == GCSpropagate)
507*088332b5SXin Li linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */
508*088332b5SXin Li else if (hasww) /* table has white->white entries? */
509*088332b5SXin Li linkgclist(h, g->ephemeron); /* have to propagate again */
510*088332b5SXin Li else if (hasclears) /* table has white keys? */
511*088332b5SXin Li linkgclist(h, g->allweak); /* may have to clean white keys */
512*088332b5SXin Li else
513*088332b5SXin Li genlink(g, obj2gco(h)); /* check whether collector still needs to see it */
514*088332b5SXin Li return marked;
515*088332b5SXin Li }
516*088332b5SXin Li
517*088332b5SXin Li
traversestrongtable(global_State * g,Table * h)518*088332b5SXin Li static void traversestrongtable (global_State *g, Table *h) {
519*088332b5SXin Li Node *n, *limit = gnodelast(h);
520*088332b5SXin Li unsigned int i;
521*088332b5SXin Li unsigned int asize = luaH_realasize(h);
522*088332b5SXin Li for (i = 0; i < asize; i++) /* traverse array part */
523*088332b5SXin Li markvalue(g, &h->array[i]);
524*088332b5SXin Li for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */
525*088332b5SXin Li if (isempty(gval(n))) /* entry is empty? */
526*088332b5SXin Li clearkey(n); /* clear its key */
527*088332b5SXin Li else {
528*088332b5SXin Li lua_assert(!keyisnil(n));
529*088332b5SXin Li markkey(g, n);
530*088332b5SXin Li markvalue(g, gval(n));
531*088332b5SXin Li }
532*088332b5SXin Li }
533*088332b5SXin Li genlink(g, obj2gco(h));
534*088332b5SXin Li }
535*088332b5SXin Li
536*088332b5SXin Li
traversetable(global_State * g,Table * h)537*088332b5SXin Li static lu_mem traversetable (global_State *g, Table *h) {
538*088332b5SXin Li const char *weakkey, *weakvalue;
539*088332b5SXin Li const TValue *mode = gfasttm(g, h->metatable, TM_MODE);
540*088332b5SXin Li markobjectN(g, h->metatable);
541*088332b5SXin Li if (mode && ttisstring(mode) && /* is there a weak mode? */
542*088332b5SXin Li (cast_void(weakkey = strchr(svalue(mode), 'k')),
543*088332b5SXin Li cast_void(weakvalue = strchr(svalue(mode), 'v')),
544*088332b5SXin Li (weakkey || weakvalue))) { /* is really weak? */
545*088332b5SXin Li if (!weakkey) /* strong keys? */
546*088332b5SXin Li traverseweakvalue(g, h);
547*088332b5SXin Li else if (!weakvalue) /* strong values? */
548*088332b5SXin Li traverseephemeron(g, h, 0);
549*088332b5SXin Li else /* all weak */
550*088332b5SXin Li linkgclist(h, g->allweak); /* nothing to traverse now */
551*088332b5SXin Li }
552*088332b5SXin Li else /* not weak */
553*088332b5SXin Li traversestrongtable(g, h);
554*088332b5SXin Li return 1 + h->alimit + 2 * allocsizenode(h);
555*088332b5SXin Li }
556*088332b5SXin Li
557*088332b5SXin Li
traverseudata(global_State * g,Udata * u)558*088332b5SXin Li static int traverseudata (global_State *g, Udata *u) {
559*088332b5SXin Li int i;
560*088332b5SXin Li markobjectN(g, u->metatable); /* mark its metatable */
561*088332b5SXin Li for (i = 0; i < u->nuvalue; i++)
562*088332b5SXin Li markvalue(g, &u->uv[i].uv);
563*088332b5SXin Li genlink(g, obj2gco(u));
564*088332b5SXin Li return 1 + u->nuvalue;
565*088332b5SXin Li }
566*088332b5SXin Li
567*088332b5SXin Li
568*088332b5SXin Li /*
569*088332b5SXin Li ** Traverse a prototype. (While a prototype is being build, its
570*088332b5SXin Li ** arrays can be larger than needed; the extra slots are filled with
571*088332b5SXin Li ** NULL, so the use of 'markobjectN')
572*088332b5SXin Li */
traverseproto(global_State * g,Proto * f)573*088332b5SXin Li static int traverseproto (global_State *g, Proto *f) {
574*088332b5SXin Li int i;
575*088332b5SXin Li markobjectN(g, f->source);
576*088332b5SXin Li for (i = 0; i < f->sizek; i++) /* mark literals */
577*088332b5SXin Li markvalue(g, &f->k[i]);
578*088332b5SXin Li for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */
579*088332b5SXin Li markobjectN(g, f->upvalues[i].name);
580*088332b5SXin Li for (i = 0; i < f->sizep; i++) /* mark nested protos */
581*088332b5SXin Li markobjectN(g, f->p[i]);
582*088332b5SXin Li for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */
583*088332b5SXin Li markobjectN(g, f->locvars[i].varname);
584*088332b5SXin Li return 1 + f->sizek + f->sizeupvalues + f->sizep + f->sizelocvars;
585*088332b5SXin Li }
586*088332b5SXin Li
587*088332b5SXin Li
traverseCclosure(global_State * g,CClosure * cl)588*088332b5SXin Li static int traverseCclosure (global_State *g, CClosure *cl) {
589*088332b5SXin Li int i;
590*088332b5SXin Li for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */
591*088332b5SXin Li markvalue(g, &cl->upvalue[i]);
592*088332b5SXin Li return 1 + cl->nupvalues;
593*088332b5SXin Li }
594*088332b5SXin Li
595*088332b5SXin Li /*
596*088332b5SXin Li ** Traverse a Lua closure, marking its prototype and its upvalues.
597*088332b5SXin Li ** (Both can be NULL while closure is being created.)
598*088332b5SXin Li */
traverseLclosure(global_State * g,LClosure * cl)599*088332b5SXin Li static int traverseLclosure (global_State *g, LClosure *cl) {
600*088332b5SXin Li int i;
601*088332b5SXin Li markobjectN(g, cl->p); /* mark its prototype */
602*088332b5SXin Li for (i = 0; i < cl->nupvalues; i++) { /* visit its upvalues */
603*088332b5SXin Li UpVal *uv = cl->upvals[i];
604*088332b5SXin Li markobjectN(g, uv); /* mark upvalue */
605*088332b5SXin Li }
606*088332b5SXin Li return 1 + cl->nupvalues;
607*088332b5SXin Li }
608*088332b5SXin Li
609*088332b5SXin Li
610*088332b5SXin Li /*
611*088332b5SXin Li ** Traverse a thread, marking the elements in the stack up to its top
612*088332b5SXin Li ** and cleaning the rest of the stack in the final traversal. That
613*088332b5SXin Li ** ensures that the entire stack have valid (non-dead) objects.
614*088332b5SXin Li ** Threads have no barriers. In gen. mode, old threads must be visited
615*088332b5SXin Li ** at every cycle, because they might point to young objects. In inc.
616*088332b5SXin Li ** mode, the thread can still be modified before the end of the cycle,
617*088332b5SXin Li ** and therefore it must be visited again in the atomic phase. To ensure
618*088332b5SXin Li ** these visits, threads must return to a gray list if they are not new
619*088332b5SXin Li ** (which can only happen in generational mode) or if the traverse is in
620*088332b5SXin Li ** the propagate phase (which can only happen in incremental mode).
621*088332b5SXin Li */
traversethread(global_State * g,lua_State * th)622*088332b5SXin Li static int traversethread (global_State *g, lua_State *th) {
623*088332b5SXin Li UpVal *uv;
624*088332b5SXin Li StkId o = th->stack;
625*088332b5SXin Li if (isold(th) || g->gcstate == GCSpropagate)
626*088332b5SXin Li linkgclist(th, g->grayagain); /* insert into 'grayagain' list */
627*088332b5SXin Li if (o == NULL)
628*088332b5SXin Li return 1; /* stack not completely built yet */
629*088332b5SXin Li lua_assert(g->gcstate == GCSatomic ||
630*088332b5SXin Li th->openupval == NULL || isintwups(th));
631*088332b5SXin Li for (; o < th->top; o++) /* mark live elements in the stack */
632*088332b5SXin Li markvalue(g, s2v(o));
633*088332b5SXin Li for (uv = th->openupval; uv != NULL; uv = uv->u.open.next)
634*088332b5SXin Li markobject(g, uv); /* open upvalues cannot be collected */
635*088332b5SXin Li if (g->gcstate == GCSatomic) { /* final traversal? */
636*088332b5SXin Li StkId lim = th->stack + th->stacksize; /* real end of stack */
637*088332b5SXin Li for (; o < lim; o++) /* clear not-marked stack slice */
638*088332b5SXin Li setnilvalue(s2v(o));
639*088332b5SXin Li /* 'remarkupvals' may have removed thread from 'twups' list */
640*088332b5SXin Li if (!isintwups(th) && th->openupval != NULL) {
641*088332b5SXin Li th->twups = g->twups; /* link it back to the list */
642*088332b5SXin Li g->twups = th;
643*088332b5SXin Li }
644*088332b5SXin Li }
645*088332b5SXin Li else if (!g->gcemergency)
646*088332b5SXin Li luaD_shrinkstack(th); /* do not change stack in emergency cycle */
647*088332b5SXin Li return 1 + th->stacksize;
648*088332b5SXin Li }
649*088332b5SXin Li
650*088332b5SXin Li
651*088332b5SXin Li /*
652*088332b5SXin Li ** traverse one gray object, turning it to black.
653*088332b5SXin Li */
propagatemark(global_State * g)654*088332b5SXin Li static lu_mem propagatemark (global_State *g) {
655*088332b5SXin Li GCObject *o = g->gray;
656*088332b5SXin Li nw2black(o);
657*088332b5SXin Li g->gray = *getgclist(o); /* remove from 'gray' list */
658*088332b5SXin Li switch (o->tt) {
659*088332b5SXin Li case LUA_VTABLE: return traversetable(g, gco2t(o));
660*088332b5SXin Li case LUA_VUSERDATA: return traverseudata(g, gco2u(o));
661*088332b5SXin Li case LUA_VLCL: return traverseLclosure(g, gco2lcl(o));
662*088332b5SXin Li case LUA_VCCL: return traverseCclosure(g, gco2ccl(o));
663*088332b5SXin Li case LUA_VPROTO: return traverseproto(g, gco2p(o));
664*088332b5SXin Li case LUA_VTHREAD: return traversethread(g, gco2th(o));
665*088332b5SXin Li default: lua_assert(0); return 0;
666*088332b5SXin Li }
667*088332b5SXin Li }
668*088332b5SXin Li
669*088332b5SXin Li
propagateall(global_State * g)670*088332b5SXin Li static lu_mem propagateall (global_State *g) {
671*088332b5SXin Li lu_mem tot = 0;
672*088332b5SXin Li while (g->gray)
673*088332b5SXin Li tot += propagatemark(g);
674*088332b5SXin Li return tot;
675*088332b5SXin Li }
676*088332b5SXin Li
677*088332b5SXin Li
678*088332b5SXin Li /*
679*088332b5SXin Li ** Traverse all ephemeron tables propagating marks from keys to values.
680*088332b5SXin Li ** Repeat until it converges, that is, nothing new is marked. 'dir'
681*088332b5SXin Li ** inverts the direction of the traversals, trying to speed up
682*088332b5SXin Li ** convergence on chains in the same table.
683*088332b5SXin Li **
684*088332b5SXin Li */
convergeephemerons(global_State * g)685*088332b5SXin Li static void convergeephemerons (global_State *g) {
686*088332b5SXin Li int changed;
687*088332b5SXin Li int dir = 0;
688*088332b5SXin Li do {
689*088332b5SXin Li GCObject *w;
690*088332b5SXin Li GCObject *next = g->ephemeron; /* get ephemeron list */
691*088332b5SXin Li g->ephemeron = NULL; /* tables may return to this list when traversed */
692*088332b5SXin Li changed = 0;
693*088332b5SXin Li while ((w = next) != NULL) { /* for each ephemeron table */
694*088332b5SXin Li Table *h = gco2t(w);
695*088332b5SXin Li next = h->gclist; /* list is rebuilt during loop */
696*088332b5SXin Li nw2black(h); /* out of the list (for now) */
697*088332b5SXin Li if (traverseephemeron(g, h, dir)) { /* marked some value? */
698*088332b5SXin Li propagateall(g); /* propagate changes */
699*088332b5SXin Li changed = 1; /* will have to revisit all ephemeron tables */
700*088332b5SXin Li }
701*088332b5SXin Li }
702*088332b5SXin Li dir = !dir; /* invert direction next time */
703*088332b5SXin Li } while (changed); /* repeat until no more changes */
704*088332b5SXin Li }
705*088332b5SXin Li
706*088332b5SXin Li /* }====================================================== */
707*088332b5SXin Li
708*088332b5SXin Li
709*088332b5SXin Li /*
710*088332b5SXin Li ** {======================================================
711*088332b5SXin Li ** Sweep Functions
712*088332b5SXin Li ** =======================================================
713*088332b5SXin Li */
714*088332b5SXin Li
715*088332b5SXin Li
716*088332b5SXin Li /*
717*088332b5SXin Li ** clear entries with unmarked keys from all weaktables in list 'l'
718*088332b5SXin Li */
clearbykeys(global_State * g,GCObject * l)719*088332b5SXin Li static void clearbykeys (global_State *g, GCObject *l) {
720*088332b5SXin Li for (; l; l = gco2t(l)->gclist) {
721*088332b5SXin Li Table *h = gco2t(l);
722*088332b5SXin Li Node *limit = gnodelast(h);
723*088332b5SXin Li Node *n;
724*088332b5SXin Li for (n = gnode(h, 0); n < limit; n++) {
725*088332b5SXin Li if (iscleared(g, gckeyN(n))) /* unmarked key? */
726*088332b5SXin Li setempty(gval(n)); /* remove entry */
727*088332b5SXin Li if (isempty(gval(n))) /* is entry empty? */
728*088332b5SXin Li clearkey(n); /* clear its key */
729*088332b5SXin Li }
730*088332b5SXin Li }
731*088332b5SXin Li }
732*088332b5SXin Li
733*088332b5SXin Li
734*088332b5SXin Li /*
735*088332b5SXin Li ** clear entries with unmarked values from all weaktables in list 'l' up
736*088332b5SXin Li ** to element 'f'
737*088332b5SXin Li */
clearbyvalues(global_State * g,GCObject * l,GCObject * f)738*088332b5SXin Li static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) {
739*088332b5SXin Li for (; l != f; l = gco2t(l)->gclist) {
740*088332b5SXin Li Table *h = gco2t(l);
741*088332b5SXin Li Node *n, *limit = gnodelast(h);
742*088332b5SXin Li unsigned int i;
743*088332b5SXin Li unsigned int asize = luaH_realasize(h);
744*088332b5SXin Li for (i = 0; i < asize; i++) {
745*088332b5SXin Li TValue *o = &h->array[i];
746*088332b5SXin Li if (iscleared(g, gcvalueN(o))) /* value was collected? */
747*088332b5SXin Li setempty(o); /* remove entry */
748*088332b5SXin Li }
749*088332b5SXin Li for (n = gnode(h, 0); n < limit; n++) {
750*088332b5SXin Li if (iscleared(g, gcvalueN(gval(n)))) /* unmarked value? */
751*088332b5SXin Li setempty(gval(n)); /* remove entry */
752*088332b5SXin Li if (isempty(gval(n))) /* is entry empty? */
753*088332b5SXin Li clearkey(n); /* clear its key */
754*088332b5SXin Li }
755*088332b5SXin Li }
756*088332b5SXin Li }
757*088332b5SXin Li
758*088332b5SXin Li
freeupval(lua_State * L,UpVal * uv)759*088332b5SXin Li static void freeupval (lua_State *L, UpVal *uv) {
760*088332b5SXin Li if (upisopen(uv))
761*088332b5SXin Li luaF_unlinkupval(uv);
762*088332b5SXin Li luaM_free(L, uv);
763*088332b5SXin Li }
764*088332b5SXin Li
765*088332b5SXin Li
freeobj(lua_State * L,GCObject * o)766*088332b5SXin Li static void freeobj (lua_State *L, GCObject *o) {
767*088332b5SXin Li switch (o->tt) {
768*088332b5SXin Li case LUA_VPROTO:
769*088332b5SXin Li luaF_freeproto(L, gco2p(o));
770*088332b5SXin Li break;
771*088332b5SXin Li case LUA_VUPVAL:
772*088332b5SXin Li freeupval(L, gco2upv(o));
773*088332b5SXin Li break;
774*088332b5SXin Li case LUA_VLCL:
775*088332b5SXin Li luaM_freemem(L, o, sizeLclosure(gco2lcl(o)->nupvalues));
776*088332b5SXin Li break;
777*088332b5SXin Li case LUA_VCCL:
778*088332b5SXin Li luaM_freemem(L, o, sizeCclosure(gco2ccl(o)->nupvalues));
779*088332b5SXin Li break;
780*088332b5SXin Li case LUA_VTABLE:
781*088332b5SXin Li luaH_free(L, gco2t(o));
782*088332b5SXin Li break;
783*088332b5SXin Li case LUA_VTHREAD:
784*088332b5SXin Li luaE_freethread(L, gco2th(o));
785*088332b5SXin Li break;
786*088332b5SXin Li case LUA_VUSERDATA: {
787*088332b5SXin Li Udata *u = gco2u(o);
788*088332b5SXin Li luaM_freemem(L, o, sizeudata(u->nuvalue, u->len));
789*088332b5SXin Li break;
790*088332b5SXin Li }
791*088332b5SXin Li case LUA_VSHRSTR:
792*088332b5SXin Li luaS_remove(L, gco2ts(o)); /* remove it from hash table */
793*088332b5SXin Li luaM_freemem(L, o, sizelstring(gco2ts(o)->shrlen));
794*088332b5SXin Li break;
795*088332b5SXin Li case LUA_VLNGSTR:
796*088332b5SXin Li luaM_freemem(L, o, sizelstring(gco2ts(o)->u.lnglen));
797*088332b5SXin Li break;
798*088332b5SXin Li default: lua_assert(0);
799*088332b5SXin Li }
800*088332b5SXin Li }
801*088332b5SXin Li
802*088332b5SXin Li
803*088332b5SXin Li /*
804*088332b5SXin Li ** sweep at most 'countin' elements from a list of GCObjects erasing dead
805*088332b5SXin Li ** objects, where a dead object is one marked with the old (non current)
806*088332b5SXin Li ** white; change all non-dead objects back to white, preparing for next
807*088332b5SXin Li ** collection cycle. Return where to continue the traversal or NULL if
808*088332b5SXin Li ** list is finished. ('*countout' gets the number of elements traversed.)
809*088332b5SXin Li */
sweeplist(lua_State * L,GCObject ** p,int countin,int * countout)810*088332b5SXin Li static GCObject **sweeplist (lua_State *L, GCObject **p, int countin,
811*088332b5SXin Li int *countout) {
812*088332b5SXin Li global_State *g = G(L);
813*088332b5SXin Li int ow = otherwhite(g);
814*088332b5SXin Li int i;
815*088332b5SXin Li int white = luaC_white(g); /* current white */
816*088332b5SXin Li for (i = 0; *p != NULL && i < countin; i++) {
817*088332b5SXin Li GCObject *curr = *p;
818*088332b5SXin Li int marked = curr->marked;
819*088332b5SXin Li if (isdeadm(ow, marked)) { /* is 'curr' dead? */
820*088332b5SXin Li *p = curr->next; /* remove 'curr' from list */
821*088332b5SXin Li freeobj(L, curr); /* erase 'curr' */
822*088332b5SXin Li }
823*088332b5SXin Li else { /* change mark to 'white' */
824*088332b5SXin Li curr->marked = cast_byte((marked & ~maskgcbits) | white);
825*088332b5SXin Li p = &curr->next; /* go to next element */
826*088332b5SXin Li }
827*088332b5SXin Li }
828*088332b5SXin Li if (countout)
829*088332b5SXin Li *countout = i; /* number of elements traversed */
830*088332b5SXin Li return (*p == NULL) ? NULL : p;
831*088332b5SXin Li }
832*088332b5SXin Li
833*088332b5SXin Li
834*088332b5SXin Li /*
835*088332b5SXin Li ** sweep a list until a live object (or end of list)
836*088332b5SXin Li */
sweeptolive(lua_State * L,GCObject ** p)837*088332b5SXin Li static GCObject **sweeptolive (lua_State *L, GCObject **p) {
838*088332b5SXin Li GCObject **old = p;
839*088332b5SXin Li do {
840*088332b5SXin Li p = sweeplist(L, p, 1, NULL);
841*088332b5SXin Li } while (p == old);
842*088332b5SXin Li return p;
843*088332b5SXin Li }
844*088332b5SXin Li
845*088332b5SXin Li /* }====================================================== */
846*088332b5SXin Li
847*088332b5SXin Li
848*088332b5SXin Li /*
849*088332b5SXin Li ** {======================================================
850*088332b5SXin Li ** Finalization
851*088332b5SXin Li ** =======================================================
852*088332b5SXin Li */
853*088332b5SXin Li
854*088332b5SXin Li /*
855*088332b5SXin Li ** If possible, shrink string table.
856*088332b5SXin Li */
checkSizes(lua_State * L,global_State * g)857*088332b5SXin Li static void checkSizes (lua_State *L, global_State *g) {
858*088332b5SXin Li if (!g->gcemergency) {
859*088332b5SXin Li if (g->strt.nuse < g->strt.size / 4) { /* string table too big? */
860*088332b5SXin Li l_mem olddebt = g->GCdebt;
861*088332b5SXin Li luaS_resize(L, g->strt.size / 2);
862*088332b5SXin Li g->GCestimate += g->GCdebt - olddebt; /* correct estimate */
863*088332b5SXin Li }
864*088332b5SXin Li }
865*088332b5SXin Li }
866*088332b5SXin Li
867*088332b5SXin Li
868*088332b5SXin Li /*
869*088332b5SXin Li ** Get the next udata to be finalized from the 'tobefnz' list, and
870*088332b5SXin Li ** link it back into the 'allgc' list.
871*088332b5SXin Li */
udata2finalize(global_State * g)872*088332b5SXin Li static GCObject *udata2finalize (global_State *g) {
873*088332b5SXin Li GCObject *o = g->tobefnz; /* get first element */
874*088332b5SXin Li lua_assert(tofinalize(o));
875*088332b5SXin Li g->tobefnz = o->next; /* remove it from 'tobefnz' list */
876*088332b5SXin Li o->next = g->allgc; /* return it to 'allgc' list */
877*088332b5SXin Li g->allgc = o;
878*088332b5SXin Li resetbit(o->marked, FINALIZEDBIT); /* object is "normal" again */
879*088332b5SXin Li if (issweepphase(g))
880*088332b5SXin Li makewhite(g, o); /* "sweep" object */
881*088332b5SXin Li else if (getage(o) == G_OLD1)
882*088332b5SXin Li g->firstold1 = o; /* it is the first OLD1 object in the list */
883*088332b5SXin Li return o;
884*088332b5SXin Li }
885*088332b5SXin Li
886*088332b5SXin Li
dothecall(lua_State * L,void * ud)887*088332b5SXin Li static void dothecall (lua_State *L, void *ud) {
888*088332b5SXin Li UNUSED(ud);
889*088332b5SXin Li luaD_callnoyield(L, L->top - 2, 0);
890*088332b5SXin Li }
891*088332b5SXin Li
892*088332b5SXin Li
GCTM(lua_State * L)893*088332b5SXin Li static void GCTM (lua_State *L) {
894*088332b5SXin Li global_State *g = G(L);
895*088332b5SXin Li const TValue *tm;
896*088332b5SXin Li TValue v;
897*088332b5SXin Li lua_assert(!g->gcemergency);
898*088332b5SXin Li setgcovalue(L, &v, udata2finalize(g));
899*088332b5SXin Li tm = luaT_gettmbyobj(L, &v, TM_GC);
900*088332b5SXin Li if (!notm(tm)) { /* is there a finalizer? */
901*088332b5SXin Li int status;
902*088332b5SXin Li lu_byte oldah = L->allowhook;
903*088332b5SXin Li int running = g->gcrunning;
904*088332b5SXin Li L->allowhook = 0; /* stop debug hooks during GC metamethod */
905*088332b5SXin Li g->gcrunning = 0; /* avoid GC steps */
906*088332b5SXin Li setobj2s(L, L->top++, tm); /* push finalizer... */
907*088332b5SXin Li setobj2s(L, L->top++, &v); /* ... and its argument */
908*088332b5SXin Li L->ci->callstatus |= CIST_FIN; /* will run a finalizer */
909*088332b5SXin Li status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0);
910*088332b5SXin Li L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */
911*088332b5SXin Li L->allowhook = oldah; /* restore hooks */
912*088332b5SXin Li g->gcrunning = running; /* restore state */
913*088332b5SXin Li if (unlikely(status != LUA_OK)) { /* error while running __gc? */
914*088332b5SXin Li luaE_warnerror(L, "__gc metamethod");
915*088332b5SXin Li L->top--; /* pops error object */
916*088332b5SXin Li }
917*088332b5SXin Li }
918*088332b5SXin Li }
919*088332b5SXin Li
920*088332b5SXin Li
921*088332b5SXin Li /*
922*088332b5SXin Li ** Call a few finalizers
923*088332b5SXin Li */
runafewfinalizers(lua_State * L,int n)924*088332b5SXin Li static int runafewfinalizers (lua_State *L, int n) {
925*088332b5SXin Li global_State *g = G(L);
926*088332b5SXin Li int i;
927*088332b5SXin Li for (i = 0; i < n && g->tobefnz; i++)
928*088332b5SXin Li GCTM(L); /* call one finalizer */
929*088332b5SXin Li return i;
930*088332b5SXin Li }
931*088332b5SXin Li
932*088332b5SXin Li
933*088332b5SXin Li /*
934*088332b5SXin Li ** call all pending finalizers
935*088332b5SXin Li */
callallpendingfinalizers(lua_State * L)936*088332b5SXin Li static void callallpendingfinalizers (lua_State *L) {
937*088332b5SXin Li global_State *g = G(L);
938*088332b5SXin Li while (g->tobefnz)
939*088332b5SXin Li GCTM(L);
940*088332b5SXin Li }
941*088332b5SXin Li
942*088332b5SXin Li
943*088332b5SXin Li /*
944*088332b5SXin Li ** find last 'next' field in list 'p' list (to add elements in its end)
945*088332b5SXin Li */
findlast(GCObject ** p)946*088332b5SXin Li static GCObject **findlast (GCObject **p) {
947*088332b5SXin Li while (*p != NULL)
948*088332b5SXin Li p = &(*p)->next;
949*088332b5SXin Li return p;
950*088332b5SXin Li }
951*088332b5SXin Li
952*088332b5SXin Li
953*088332b5SXin Li /*
954*088332b5SXin Li ** Move all unreachable objects (or 'all' objects) that need
955*088332b5SXin Li ** finalization from list 'finobj' to list 'tobefnz' (to be finalized).
956*088332b5SXin Li ** (Note that objects after 'finobjold1' cannot be white, so they
957*088332b5SXin Li ** don't need to be traversed. In incremental mode, 'finobjold1' is NULL,
958*088332b5SXin Li ** so the whole list is traversed.)
959*088332b5SXin Li */
separatetobefnz(global_State * g,int all)960*088332b5SXin Li static void separatetobefnz (global_State *g, int all) {
961*088332b5SXin Li GCObject *curr;
962*088332b5SXin Li GCObject **p = &g->finobj;
963*088332b5SXin Li GCObject **lastnext = findlast(&g->tobefnz);
964*088332b5SXin Li while ((curr = *p) != g->finobjold1) { /* traverse all finalizable objects */
965*088332b5SXin Li lua_assert(tofinalize(curr));
966*088332b5SXin Li if (!(iswhite(curr) || all)) /* not being collected? */
967*088332b5SXin Li p = &curr->next; /* don't bother with it */
968*088332b5SXin Li else {
969*088332b5SXin Li if (curr == g->finobjsur) /* removing 'finobjsur'? */
970*088332b5SXin Li g->finobjsur = curr->next; /* correct it */
971*088332b5SXin Li *p = curr->next; /* remove 'curr' from 'finobj' list */
972*088332b5SXin Li curr->next = *lastnext; /* link at the end of 'tobefnz' list */
973*088332b5SXin Li *lastnext = curr;
974*088332b5SXin Li lastnext = &curr->next;
975*088332b5SXin Li }
976*088332b5SXin Li }
977*088332b5SXin Li }
978*088332b5SXin Li
979*088332b5SXin Li
980*088332b5SXin Li /*
981*088332b5SXin Li ** If pointer 'p' points to 'o', move it to the next element.
982*088332b5SXin Li */
checkpointer(GCObject ** p,GCObject * o)983*088332b5SXin Li static void checkpointer (GCObject **p, GCObject *o) {
984*088332b5SXin Li if (o == *p)
985*088332b5SXin Li *p = o->next;
986*088332b5SXin Li }
987*088332b5SXin Li
988*088332b5SXin Li
989*088332b5SXin Li /*
990*088332b5SXin Li ** Correct pointers to objects inside 'allgc' list when
991*088332b5SXin Li ** object 'o' is being removed from the list.
992*088332b5SXin Li */
correctpointers(global_State * g,GCObject * o)993*088332b5SXin Li static void correctpointers (global_State *g, GCObject *o) {
994*088332b5SXin Li checkpointer(&g->survival, o);
995*088332b5SXin Li checkpointer(&g->old1, o);
996*088332b5SXin Li checkpointer(&g->reallyold, o);
997*088332b5SXin Li checkpointer(&g->firstold1, o);
998*088332b5SXin Li }
999*088332b5SXin Li
1000*088332b5SXin Li
1001*088332b5SXin Li /*
1002*088332b5SXin Li ** if object 'o' has a finalizer, remove it from 'allgc' list (must
1003*088332b5SXin Li ** search the list to find it) and link it in 'finobj' list.
1004*088332b5SXin Li */
luaC_checkfinalizer(lua_State * L,GCObject * o,Table * mt)1005*088332b5SXin Li void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
1006*088332b5SXin Li global_State *g = G(L);
1007*088332b5SXin Li if (tofinalize(o) || /* obj. is already marked... */
1008*088332b5SXin Li gfasttm(g, mt, TM_GC) == NULL) /* or has no finalizer? */
1009*088332b5SXin Li return; /* nothing to be done */
1010*088332b5SXin Li else { /* move 'o' to 'finobj' list */
1011*088332b5SXin Li GCObject **p;
1012*088332b5SXin Li if (issweepphase(g)) {
1013*088332b5SXin Li makewhite(g, o); /* "sweep" object 'o' */
1014*088332b5SXin Li if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */
1015*088332b5SXin Li g->sweepgc = sweeptolive(L, g->sweepgc); /* change 'sweepgc' */
1016*088332b5SXin Li }
1017*088332b5SXin Li else
1018*088332b5SXin Li correctpointers(g, o);
1019*088332b5SXin Li /* search for pointer pointing to 'o' */
1020*088332b5SXin Li for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ }
1021*088332b5SXin Li *p = o->next; /* remove 'o' from 'allgc' list */
1022*088332b5SXin Li o->next = g->finobj; /* link it in 'finobj' list */
1023*088332b5SXin Li g->finobj = o;
1024*088332b5SXin Li l_setbit(o->marked, FINALIZEDBIT); /* mark it as such */
1025*088332b5SXin Li }
1026*088332b5SXin Li }
1027*088332b5SXin Li
1028*088332b5SXin Li /* }====================================================== */
1029*088332b5SXin Li
1030*088332b5SXin Li
1031*088332b5SXin Li /*
1032*088332b5SXin Li ** {======================================================
1033*088332b5SXin Li ** Generational Collector
1034*088332b5SXin Li ** =======================================================
1035*088332b5SXin Li */
1036*088332b5SXin Li
1037*088332b5SXin Li static void setpause (global_State *g);
1038*088332b5SXin Li
1039*088332b5SXin Li
1040*088332b5SXin Li /*
1041*088332b5SXin Li ** Sweep a list of objects to enter generational mode. Deletes dead
1042*088332b5SXin Li ** objects and turns the non dead to old. All non-dead threads---which
1043*088332b5SXin Li ** are now old---must be in a gray list. Everything else is not in a
1044*088332b5SXin Li ** gray list. Open upvalues are also kept gray.
1045*088332b5SXin Li */
sweep2old(lua_State * L,GCObject ** p)1046*088332b5SXin Li static void sweep2old (lua_State *L, GCObject **p) {
1047*088332b5SXin Li GCObject *curr;
1048*088332b5SXin Li global_State *g = G(L);
1049*088332b5SXin Li while ((curr = *p) != NULL) {
1050*088332b5SXin Li if (iswhite(curr)) { /* is 'curr' dead? */
1051*088332b5SXin Li lua_assert(isdead(g, curr));
1052*088332b5SXin Li *p = curr->next; /* remove 'curr' from list */
1053*088332b5SXin Li freeobj(L, curr); /* erase 'curr' */
1054*088332b5SXin Li }
1055*088332b5SXin Li else { /* all surviving objects become old */
1056*088332b5SXin Li setage(curr, G_OLD);
1057*088332b5SXin Li if (curr->tt == LUA_VTHREAD) { /* threads must be watched */
1058*088332b5SXin Li lua_State *th = gco2th(curr);
1059*088332b5SXin Li linkgclist(th, g->grayagain); /* insert into 'grayagain' list */
1060*088332b5SXin Li }
1061*088332b5SXin Li else if (curr->tt == LUA_VUPVAL && upisopen(gco2upv(curr)))
1062*088332b5SXin Li set2gray(curr); /* open upvalues are always gray */
1063*088332b5SXin Li else /* everything else is black */
1064*088332b5SXin Li nw2black(curr);
1065*088332b5SXin Li p = &curr->next; /* go to next element */
1066*088332b5SXin Li }
1067*088332b5SXin Li }
1068*088332b5SXin Li }
1069*088332b5SXin Li
1070*088332b5SXin Li
1071*088332b5SXin Li /*
1072*088332b5SXin Li ** Sweep for generational mode. Delete dead objects. (Because the
1073*088332b5SXin Li ** collection is not incremental, there are no "new white" objects
1074*088332b5SXin Li ** during the sweep. So, any white object must be dead.) For
1075*088332b5SXin Li ** non-dead objects, advance their ages and clear the color of
1076*088332b5SXin Li ** new objects. (Old objects keep their colors.)
1077*088332b5SXin Li ** The ages of G_TOUCHED1 and G_TOUCHED2 objects cannot be advanced
1078*088332b5SXin Li ** here, because these old-generation objects are usually not swept
1079*088332b5SXin Li ** here. They will all be advanced in 'correctgraylist'. That function
1080*088332b5SXin Li ** will also remove objects turned white here from any gray list.
1081*088332b5SXin Li */
sweepgen(lua_State * L,global_State * g,GCObject ** p,GCObject * limit,GCObject ** pfirstold1)1082*088332b5SXin Li static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p,
1083*088332b5SXin Li GCObject *limit, GCObject **pfirstold1) {
1084*088332b5SXin Li static const lu_byte nextage[] = {
1085*088332b5SXin Li G_SURVIVAL, /* from G_NEW */
1086*088332b5SXin Li G_OLD1, /* from G_SURVIVAL */
1087*088332b5SXin Li G_OLD1, /* from G_OLD0 */
1088*088332b5SXin Li G_OLD, /* from G_OLD1 */
1089*088332b5SXin Li G_OLD, /* from G_OLD (do not change) */
1090*088332b5SXin Li G_TOUCHED1, /* from G_TOUCHED1 (do not change) */
1091*088332b5SXin Li G_TOUCHED2 /* from G_TOUCHED2 (do not change) */
1092*088332b5SXin Li };
1093*088332b5SXin Li int white = luaC_white(g);
1094*088332b5SXin Li GCObject *curr;
1095*088332b5SXin Li while ((curr = *p) != limit) {
1096*088332b5SXin Li if (iswhite(curr)) { /* is 'curr' dead? */
1097*088332b5SXin Li lua_assert(!isold(curr) && isdead(g, curr));
1098*088332b5SXin Li *p = curr->next; /* remove 'curr' from list */
1099*088332b5SXin Li freeobj(L, curr); /* erase 'curr' */
1100*088332b5SXin Li }
1101*088332b5SXin Li else { /* correct mark and age */
1102*088332b5SXin Li if (getage(curr) == G_NEW) { /* new objects go back to white */
1103*088332b5SXin Li int marked = curr->marked & ~maskgcbits; /* erase GC bits */
1104*088332b5SXin Li curr->marked = cast_byte(marked | G_SURVIVAL | white);
1105*088332b5SXin Li }
1106*088332b5SXin Li else { /* all other objects will be old, and so keep their color */
1107*088332b5SXin Li setage(curr, nextage[getage(curr)]);
1108*088332b5SXin Li if (getage(curr) == G_OLD1 && *pfirstold1 == NULL)
1109*088332b5SXin Li *pfirstold1 = curr; /* first OLD1 object in the list */
1110*088332b5SXin Li }
1111*088332b5SXin Li p = &curr->next; /* go to next element */
1112*088332b5SXin Li }
1113*088332b5SXin Li }
1114*088332b5SXin Li return p;
1115*088332b5SXin Li }
1116*088332b5SXin Li
1117*088332b5SXin Li
1118*088332b5SXin Li /*
1119*088332b5SXin Li ** Traverse a list making all its elements white and clearing their
1120*088332b5SXin Li ** age. In incremental mode, all objects are 'new' all the time,
1121*088332b5SXin Li ** except for fixed strings (which are always old).
1122*088332b5SXin Li */
whitelist(global_State * g,GCObject * p)1123*088332b5SXin Li static void whitelist (global_State *g, GCObject *p) {
1124*088332b5SXin Li int white = luaC_white(g);
1125*088332b5SXin Li for (; p != NULL; p = p->next)
1126*088332b5SXin Li p->marked = cast_byte((p->marked & ~maskgcbits) | white);
1127*088332b5SXin Li }
1128*088332b5SXin Li
1129*088332b5SXin Li
1130*088332b5SXin Li /*
1131*088332b5SXin Li ** Correct a list of gray objects. Return pointer to where rest of the
1132*088332b5SXin Li ** list should be linked.
1133*088332b5SXin Li ** Because this correction is done after sweeping, young objects might
1134*088332b5SXin Li ** be turned white and still be in the list. They are only removed.
1135*088332b5SXin Li ** 'TOUCHED1' objects are advanced to 'TOUCHED2' and remain on the list;
1136*088332b5SXin Li ** Non-white threads also remain on the list; 'TOUCHED2' objects become
1137*088332b5SXin Li ** regular old; they and anything else are removed from the list.
1138*088332b5SXin Li */
correctgraylist(GCObject ** p)1139*088332b5SXin Li static GCObject **correctgraylist (GCObject **p) {
1140*088332b5SXin Li GCObject *curr;
1141*088332b5SXin Li while ((curr = *p) != NULL) {
1142*088332b5SXin Li GCObject **next = getgclist(curr);
1143*088332b5SXin Li if (iswhite(curr))
1144*088332b5SXin Li goto remove; /* remove all white objects */
1145*088332b5SXin Li else if (getage(curr) == G_TOUCHED1) { /* touched in this cycle? */
1146*088332b5SXin Li lua_assert(isgray(curr));
1147*088332b5SXin Li nw2black(curr); /* make it black, for next barrier */
1148*088332b5SXin Li changeage(curr, G_TOUCHED1, G_TOUCHED2);
1149*088332b5SXin Li goto remain; /* keep it in the list and go to next element */
1150*088332b5SXin Li }
1151*088332b5SXin Li else if (curr->tt == LUA_VTHREAD) {
1152*088332b5SXin Li lua_assert(isgray(curr));
1153*088332b5SXin Li goto remain; /* keep non-white threads on the list */
1154*088332b5SXin Li }
1155*088332b5SXin Li else { /* everything else is removed */
1156*088332b5SXin Li lua_assert(isold(curr)); /* young objects should be white here */
1157*088332b5SXin Li if (getage(curr) == G_TOUCHED2) /* advance from TOUCHED2... */
1158*088332b5SXin Li changeage(curr, G_TOUCHED2, G_OLD); /* ... to OLD */
1159*088332b5SXin Li nw2black(curr); /* make object black (to be removed) */
1160*088332b5SXin Li goto remove;
1161*088332b5SXin Li }
1162*088332b5SXin Li remove: *p = *next; continue;
1163*088332b5SXin Li remain: p = next; continue;
1164*088332b5SXin Li }
1165*088332b5SXin Li return p;
1166*088332b5SXin Li }
1167*088332b5SXin Li
1168*088332b5SXin Li
1169*088332b5SXin Li /*
1170*088332b5SXin Li ** Correct all gray lists, coalescing them into 'grayagain'.
1171*088332b5SXin Li */
correctgraylists(global_State * g)1172*088332b5SXin Li static void correctgraylists (global_State *g) {
1173*088332b5SXin Li GCObject **list = correctgraylist(&g->grayagain);
1174*088332b5SXin Li *list = g->weak; g->weak = NULL;
1175*088332b5SXin Li list = correctgraylist(list);
1176*088332b5SXin Li *list = g->allweak; g->allweak = NULL;
1177*088332b5SXin Li list = correctgraylist(list);
1178*088332b5SXin Li *list = g->ephemeron; g->ephemeron = NULL;
1179*088332b5SXin Li correctgraylist(list);
1180*088332b5SXin Li }
1181*088332b5SXin Li
1182*088332b5SXin Li
1183*088332b5SXin Li /*
1184*088332b5SXin Li ** Mark black 'OLD1' objects when starting a new young collection.
1185*088332b5SXin Li ** Gray objects are already in some gray list, and so will be visited
1186*088332b5SXin Li ** in the atomic step.
1187*088332b5SXin Li */
markold(global_State * g,GCObject * from,GCObject * to)1188*088332b5SXin Li static void markold (global_State *g, GCObject *from, GCObject *to) {
1189*088332b5SXin Li GCObject *p;
1190*088332b5SXin Li for (p = from; p != to; p = p->next) {
1191*088332b5SXin Li if (getage(p) == G_OLD1) {
1192*088332b5SXin Li lua_assert(!iswhite(p));
1193*088332b5SXin Li changeage(p, G_OLD1, G_OLD); /* now they are old */
1194*088332b5SXin Li if (isblack(p))
1195*088332b5SXin Li reallymarkobject(g, p);
1196*088332b5SXin Li }
1197*088332b5SXin Li }
1198*088332b5SXin Li }
1199*088332b5SXin Li
1200*088332b5SXin Li
1201*088332b5SXin Li /*
1202*088332b5SXin Li ** Finish a young-generation collection.
1203*088332b5SXin Li */
finishgencycle(lua_State * L,global_State * g)1204*088332b5SXin Li static void finishgencycle (lua_State *L, global_State *g) {
1205*088332b5SXin Li correctgraylists(g);
1206*088332b5SXin Li checkSizes(L, g);
1207*088332b5SXin Li g->gcstate = GCSpropagate; /* skip restart */
1208*088332b5SXin Li if (!g->gcemergency)
1209*088332b5SXin Li callallpendingfinalizers(L);
1210*088332b5SXin Li }
1211*088332b5SXin Li
1212*088332b5SXin Li
1213*088332b5SXin Li /*
1214*088332b5SXin Li ** Does a young collection. First, mark 'OLD1' objects. Then does the
1215*088332b5SXin Li ** atomic step. Then, sweep all lists and advance pointers. Finally,
1216*088332b5SXin Li ** finish the collection.
1217*088332b5SXin Li */
youngcollection(lua_State * L,global_State * g)1218*088332b5SXin Li static void youngcollection (lua_State *L, global_State *g) {
1219*088332b5SXin Li GCObject **psurvival; /* to point to first non-dead survival object */
1220*088332b5SXin Li GCObject *dummy; /* dummy out parameter to 'sweepgen' */
1221*088332b5SXin Li lua_assert(g->gcstate == GCSpropagate);
1222*088332b5SXin Li if (g->firstold1) { /* are there regular OLD1 objects? */
1223*088332b5SXin Li markold(g, g->firstold1, g->reallyold); /* mark them */
1224*088332b5SXin Li g->firstold1 = NULL; /* no more OLD1 objects (for now) */
1225*088332b5SXin Li }
1226*088332b5SXin Li markold(g, g->finobj, g->finobjrold);
1227*088332b5SXin Li markold(g, g->tobefnz, NULL);
1228*088332b5SXin Li atomic(L);
1229*088332b5SXin Li
1230*088332b5SXin Li /* sweep nursery and get a pointer to its last live element */
1231*088332b5SXin Li g->gcstate = GCSswpallgc;
1232*088332b5SXin Li psurvival = sweepgen(L, g, &g->allgc, g->survival, &g->firstold1);
1233*088332b5SXin Li /* sweep 'survival' */
1234*088332b5SXin Li sweepgen(L, g, psurvival, g->old1, &g->firstold1);
1235*088332b5SXin Li g->reallyold = g->old1;
1236*088332b5SXin Li g->old1 = *psurvival; /* 'survival' survivals are old now */
1237*088332b5SXin Li g->survival = g->allgc; /* all news are survivals */
1238*088332b5SXin Li
1239*088332b5SXin Li /* repeat for 'finobj' lists */
1240*088332b5SXin Li dummy = NULL; /* no 'firstold1' optimization for 'finobj' lists */
1241*088332b5SXin Li psurvival = sweepgen(L, g, &g->finobj, g->finobjsur, &dummy);
1242*088332b5SXin Li /* sweep 'survival' */
1243*088332b5SXin Li sweepgen(L, g, psurvival, g->finobjold1, &dummy);
1244*088332b5SXin Li g->finobjrold = g->finobjold1;
1245*088332b5SXin Li g->finobjold1 = *psurvival; /* 'survival' survivals are old now */
1246*088332b5SXin Li g->finobjsur = g->finobj; /* all news are survivals */
1247*088332b5SXin Li
1248*088332b5SXin Li sweepgen(L, g, &g->tobefnz, NULL, &dummy);
1249*088332b5SXin Li finishgencycle(L, g);
1250*088332b5SXin Li }
1251*088332b5SXin Li
1252*088332b5SXin Li
1253*088332b5SXin Li /*
1254*088332b5SXin Li ** Clears all gray lists, sweeps objects, and prepare sublists to enter
1255*088332b5SXin Li ** generational mode. The sweeps remove dead objects and turn all
1256*088332b5SXin Li ** surviving objects to old. Threads go back to 'grayagain'; everything
1257*088332b5SXin Li ** else is turned black (not in any gray list).
1258*088332b5SXin Li */
atomic2gen(lua_State * L,global_State * g)1259*088332b5SXin Li static void atomic2gen (lua_State *L, global_State *g) {
1260*088332b5SXin Li cleargraylists(g);
1261*088332b5SXin Li /* sweep all elements making them old */
1262*088332b5SXin Li g->gcstate = GCSswpallgc;
1263*088332b5SXin Li sweep2old(L, &g->allgc);
1264*088332b5SXin Li /* everything alive now is old */
1265*088332b5SXin Li g->reallyold = g->old1 = g->survival = g->allgc;
1266*088332b5SXin Li g->firstold1 = NULL; /* there are no OLD1 objects anywhere */
1267*088332b5SXin Li
1268*088332b5SXin Li /* repeat for 'finobj' lists */
1269*088332b5SXin Li sweep2old(L, &g->finobj);
1270*088332b5SXin Li g->finobjrold = g->finobjold1 = g->finobjsur = g->finobj;
1271*088332b5SXin Li
1272*088332b5SXin Li sweep2old(L, &g->tobefnz);
1273*088332b5SXin Li
1274*088332b5SXin Li g->gckind = KGC_GEN;
1275*088332b5SXin Li g->lastatomic = 0;
1276*088332b5SXin Li g->GCestimate = gettotalbytes(g); /* base for memory control */
1277*088332b5SXin Li finishgencycle(L, g);
1278*088332b5SXin Li }
1279*088332b5SXin Li
1280*088332b5SXin Li
1281*088332b5SXin Li /*
1282*088332b5SXin Li ** Enter generational mode. Must go until the end of an atomic cycle
1283*088332b5SXin Li ** to ensure that all objects are correctly marked and weak tables
1284*088332b5SXin Li ** are cleared. Then, turn all objects into old and finishes the
1285*088332b5SXin Li ** collection.
1286*088332b5SXin Li */
entergen(lua_State * L,global_State * g)1287*088332b5SXin Li static lu_mem entergen (lua_State *L, global_State *g) {
1288*088332b5SXin Li lu_mem numobjs;
1289*088332b5SXin Li luaC_runtilstate(L, bitmask(GCSpause)); /* prepare to start a new cycle */
1290*088332b5SXin Li luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */
1291*088332b5SXin Li numobjs = atomic(L); /* propagates all and then do the atomic stuff */
1292*088332b5SXin Li atomic2gen(L, g);
1293*088332b5SXin Li return numobjs;
1294*088332b5SXin Li }
1295*088332b5SXin Li
1296*088332b5SXin Li
1297*088332b5SXin Li /*
1298*088332b5SXin Li ** Enter incremental mode. Turn all objects white, make all
1299*088332b5SXin Li ** intermediate lists point to NULL (to avoid invalid pointers),
1300*088332b5SXin Li ** and go to the pause state.
1301*088332b5SXin Li */
enterinc(global_State * g)1302*088332b5SXin Li static void enterinc (global_State *g) {
1303*088332b5SXin Li whitelist(g, g->allgc);
1304*088332b5SXin Li g->reallyold = g->old1 = g->survival = NULL;
1305*088332b5SXin Li whitelist(g, g->finobj);
1306*088332b5SXin Li whitelist(g, g->tobefnz);
1307*088332b5SXin Li g->finobjrold = g->finobjold1 = g->finobjsur = NULL;
1308*088332b5SXin Li g->gcstate = GCSpause;
1309*088332b5SXin Li g->gckind = KGC_INC;
1310*088332b5SXin Li g->lastatomic = 0;
1311*088332b5SXin Li }
1312*088332b5SXin Li
1313*088332b5SXin Li
1314*088332b5SXin Li /*
1315*088332b5SXin Li ** Change collector mode to 'newmode'.
1316*088332b5SXin Li */
luaC_changemode(lua_State * L,int newmode)1317*088332b5SXin Li void luaC_changemode (lua_State *L, int newmode) {
1318*088332b5SXin Li global_State *g = G(L);
1319*088332b5SXin Li if (newmode != g->gckind) {
1320*088332b5SXin Li if (newmode == KGC_GEN) /* entering generational mode? */
1321*088332b5SXin Li entergen(L, g);
1322*088332b5SXin Li else
1323*088332b5SXin Li enterinc(g); /* entering incremental mode */
1324*088332b5SXin Li }
1325*088332b5SXin Li g->lastatomic = 0;
1326*088332b5SXin Li }
1327*088332b5SXin Li
1328*088332b5SXin Li
1329*088332b5SXin Li /*
1330*088332b5SXin Li ** Does a full collection in generational mode.
1331*088332b5SXin Li */
fullgen(lua_State * L,global_State * g)1332*088332b5SXin Li static lu_mem fullgen (lua_State *L, global_State *g) {
1333*088332b5SXin Li enterinc(g);
1334*088332b5SXin Li return entergen(L, g);
1335*088332b5SXin Li }
1336*088332b5SXin Li
1337*088332b5SXin Li
1338*088332b5SXin Li /*
1339*088332b5SXin Li ** Set debt for the next minor collection, which will happen when
1340*088332b5SXin Li ** memory grows 'genminormul'%.
1341*088332b5SXin Li */
setminordebt(global_State * g)1342*088332b5SXin Li static void setminordebt (global_State *g) {
1343*088332b5SXin Li luaE_setdebt(g, -(cast(l_mem, (gettotalbytes(g) / 100)) * g->genminormul));
1344*088332b5SXin Li }
1345*088332b5SXin Li
1346*088332b5SXin Li
1347*088332b5SXin Li /*
1348*088332b5SXin Li ** Does a major collection after last collection was a "bad collection".
1349*088332b5SXin Li **
1350*088332b5SXin Li ** When the program is building a big structure, it allocates lots of
1351*088332b5SXin Li ** memory but generates very little garbage. In those scenarios,
1352*088332b5SXin Li ** the generational mode just wastes time doing small collections, and
1353*088332b5SXin Li ** major collections are frequently what we call a "bad collection", a
1354*088332b5SXin Li ** collection that frees too few objects. To avoid the cost of switching
1355*088332b5SXin Li ** between generational mode and the incremental mode needed for full
1356*088332b5SXin Li ** (major) collections, the collector tries to stay in incremental mode
1357*088332b5SXin Li ** after a bad collection, and to switch back to generational mode only
1358*088332b5SXin Li ** after a "good" collection (one that traverses less than 9/8 objects
1359*088332b5SXin Li ** of the previous one).
1360*088332b5SXin Li ** The collector must choose whether to stay in incremental mode or to
1361*088332b5SXin Li ** switch back to generational mode before sweeping. At this point, it
1362*088332b5SXin Li ** does not know the real memory in use, so it cannot use memory to
1363*088332b5SXin Li ** decide whether to return to generational mode. Instead, it uses the
1364*088332b5SXin Li ** number of objects traversed (returned by 'atomic') as a proxy. The
1365*088332b5SXin Li ** field 'g->lastatomic' keeps this count from the last collection.
1366*088332b5SXin Li ** ('g->lastatomic != 0' also means that the last collection was bad.)
1367*088332b5SXin Li */
stepgenfull(lua_State * L,global_State * g)1368*088332b5SXin Li static void stepgenfull (lua_State *L, global_State *g) {
1369*088332b5SXin Li lu_mem newatomic; /* count of traversed objects */
1370*088332b5SXin Li lu_mem lastatomic = g->lastatomic; /* count from last collection */
1371*088332b5SXin Li if (g->gckind == KGC_GEN) /* still in generational mode? */
1372*088332b5SXin Li enterinc(g); /* enter incremental mode */
1373*088332b5SXin Li luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */
1374*088332b5SXin Li newatomic = atomic(L); /* mark everybody */
1375*088332b5SXin Li if (newatomic < lastatomic + (lastatomic >> 3)) { /* good collection? */
1376*088332b5SXin Li atomic2gen(L, g); /* return to generational mode */
1377*088332b5SXin Li setminordebt(g);
1378*088332b5SXin Li }
1379*088332b5SXin Li else { /* another bad collection; stay in incremental mode */
1380*088332b5SXin Li g->GCestimate = gettotalbytes(g); /* first estimate */;
1381*088332b5SXin Li entersweep(L);
1382*088332b5SXin Li luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */
1383*088332b5SXin Li setpause(g);
1384*088332b5SXin Li g->lastatomic = newatomic;
1385*088332b5SXin Li }
1386*088332b5SXin Li }
1387*088332b5SXin Li
1388*088332b5SXin Li
1389*088332b5SXin Li /*
1390*088332b5SXin Li ** Does a generational "step".
1391*088332b5SXin Li ** Usually, this means doing a minor collection and setting the debt to
1392*088332b5SXin Li ** make another collection when memory grows 'genminormul'% larger.
1393*088332b5SXin Li **
1394*088332b5SXin Li ** However, there are exceptions. If memory grows 'genmajormul'%
1395*088332b5SXin Li ** larger than it was at the end of the last major collection (kept
1396*088332b5SXin Li ** in 'g->GCestimate'), the function does a major collection. At the
1397*088332b5SXin Li ** end, it checks whether the major collection was able to free a
1398*088332b5SXin Li ** decent amount of memory (at least half the growth in memory since
1399*088332b5SXin Li ** previous major collection). If so, the collector keeps its state,
1400*088332b5SXin Li ** and the next collection will probably be minor again. Otherwise,
1401*088332b5SXin Li ** we have what we call a "bad collection". In that case, set the field
1402*088332b5SXin Li ** 'g->lastatomic' to signal that fact, so that the next collection will
1403*088332b5SXin Li ** go to 'stepgenfull'.
1404*088332b5SXin Li **
1405*088332b5SXin Li ** 'GCdebt <= 0' means an explicit call to GC step with "size" zero;
1406*088332b5SXin Li ** in that case, do a minor collection.
1407*088332b5SXin Li */
genstep(lua_State * L,global_State * g)1408*088332b5SXin Li static void genstep (lua_State *L, global_State *g) {
1409*088332b5SXin Li if (g->lastatomic != 0) /* last collection was a bad one? */
1410*088332b5SXin Li stepgenfull(L, g); /* do a full step */
1411*088332b5SXin Li else {
1412*088332b5SXin Li lu_mem majorbase = g->GCestimate; /* memory after last major collection */
1413*088332b5SXin Li lu_mem majorinc = (majorbase / 100) * getgcparam(g->genmajormul);
1414*088332b5SXin Li if (g->GCdebt > 0 && gettotalbytes(g) > majorbase + majorinc) {
1415*088332b5SXin Li lu_mem numobjs = fullgen(L, g); /* do a major collection */
1416*088332b5SXin Li if (gettotalbytes(g) < majorbase + (majorinc / 2)) {
1417*088332b5SXin Li /* collected at least half of memory growth since last major
1418*088332b5SXin Li collection; keep doing minor collections */
1419*088332b5SXin Li setminordebt(g);
1420*088332b5SXin Li }
1421*088332b5SXin Li else { /* bad collection */
1422*088332b5SXin Li g->lastatomic = numobjs; /* signal that last collection was bad */
1423*088332b5SXin Li setpause(g); /* do a long wait for next (major) collection */
1424*088332b5SXin Li }
1425*088332b5SXin Li }
1426*088332b5SXin Li else { /* regular case; do a minor collection */
1427*088332b5SXin Li youngcollection(L, g);
1428*088332b5SXin Li setminordebt(g);
1429*088332b5SXin Li g->GCestimate = majorbase; /* preserve base value */
1430*088332b5SXin Li }
1431*088332b5SXin Li }
1432*088332b5SXin Li lua_assert(isdecGCmodegen(g));
1433*088332b5SXin Li }
1434*088332b5SXin Li
1435*088332b5SXin Li /* }====================================================== */
1436*088332b5SXin Li
1437*088332b5SXin Li
1438*088332b5SXin Li /*
1439*088332b5SXin Li ** {======================================================
1440*088332b5SXin Li ** GC control
1441*088332b5SXin Li ** =======================================================
1442*088332b5SXin Li */
1443*088332b5SXin Li
1444*088332b5SXin Li
1445*088332b5SXin Li /*
1446*088332b5SXin Li ** Set the "time" to wait before starting a new GC cycle; cycle will
1447*088332b5SXin Li ** start when memory use hits the threshold of ('estimate' * pause /
1448*088332b5SXin Li ** PAUSEADJ). (Division by 'estimate' should be OK: it cannot be zero,
1449*088332b5SXin Li ** because Lua cannot even start with less than PAUSEADJ bytes).
1450*088332b5SXin Li */
setpause(global_State * g)1451*088332b5SXin Li static void setpause (global_State *g) {
1452*088332b5SXin Li l_mem threshold, debt;
1453*088332b5SXin Li int pause = getgcparam(g->gcpause);
1454*088332b5SXin Li l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */
1455*088332b5SXin Li lua_assert(estimate > 0);
1456*088332b5SXin Li threshold = (pause < MAX_LMEM / estimate) /* overflow? */
1457*088332b5SXin Li ? estimate * pause /* no overflow */
1458*088332b5SXin Li : MAX_LMEM; /* overflow; truncate to maximum */
1459*088332b5SXin Li debt = gettotalbytes(g) - threshold;
1460*088332b5SXin Li if (debt > 0) debt = 0;
1461*088332b5SXin Li luaE_setdebt(g, debt);
1462*088332b5SXin Li }
1463*088332b5SXin Li
1464*088332b5SXin Li
1465*088332b5SXin Li /*
1466*088332b5SXin Li ** Enter first sweep phase.
1467*088332b5SXin Li ** The call to 'sweeptolive' makes the pointer point to an object
1468*088332b5SXin Li ** inside the list (instead of to the header), so that the real sweep do
1469*088332b5SXin Li ** not need to skip objects created between "now" and the start of the
1470*088332b5SXin Li ** real sweep.
1471*088332b5SXin Li */
entersweep(lua_State * L)1472*088332b5SXin Li static void entersweep (lua_State *L) {
1473*088332b5SXin Li global_State *g = G(L);
1474*088332b5SXin Li g->gcstate = GCSswpallgc;
1475*088332b5SXin Li lua_assert(g->sweepgc == NULL);
1476*088332b5SXin Li g->sweepgc = sweeptolive(L, &g->allgc);
1477*088332b5SXin Li }
1478*088332b5SXin Li
1479*088332b5SXin Li
1480*088332b5SXin Li /*
1481*088332b5SXin Li ** Delete all objects in list 'p' until (but not including) object
1482*088332b5SXin Li ** 'limit'.
1483*088332b5SXin Li */
deletelist(lua_State * L,GCObject * p,GCObject * limit)1484*088332b5SXin Li static void deletelist (lua_State *L, GCObject *p, GCObject *limit) {
1485*088332b5SXin Li while (p != limit) {
1486*088332b5SXin Li GCObject *next = p->next;
1487*088332b5SXin Li freeobj(L, p);
1488*088332b5SXin Li p = next;
1489*088332b5SXin Li }
1490*088332b5SXin Li }
1491*088332b5SXin Li
1492*088332b5SXin Li
1493*088332b5SXin Li /*
1494*088332b5SXin Li ** Call all finalizers of the objects in the given Lua state, and
1495*088332b5SXin Li ** then free all objects, except for the main thread.
1496*088332b5SXin Li */
luaC_freeallobjects(lua_State * L)1497*088332b5SXin Li void luaC_freeallobjects (lua_State *L) {
1498*088332b5SXin Li global_State *g = G(L);
1499*088332b5SXin Li luaC_changemode(L, KGC_INC);
1500*088332b5SXin Li separatetobefnz(g, 1); /* separate all objects with finalizers */
1501*088332b5SXin Li lua_assert(g->finobj == NULL);
1502*088332b5SXin Li callallpendingfinalizers(L);
1503*088332b5SXin Li deletelist(L, g->allgc, obj2gco(g->mainthread));
1504*088332b5SXin Li deletelist(L, g->finobj, NULL);
1505*088332b5SXin Li deletelist(L, g->fixedgc, NULL); /* collect fixed objects */
1506*088332b5SXin Li lua_assert(g->strt.nuse == 0);
1507*088332b5SXin Li }
1508*088332b5SXin Li
1509*088332b5SXin Li
atomic(lua_State * L)1510*088332b5SXin Li static lu_mem atomic (lua_State *L) {
1511*088332b5SXin Li global_State *g = G(L);
1512*088332b5SXin Li lu_mem work = 0;
1513*088332b5SXin Li GCObject *origweak, *origall;
1514*088332b5SXin Li GCObject *grayagain = g->grayagain; /* save original list */
1515*088332b5SXin Li g->grayagain = NULL;
1516*088332b5SXin Li lua_assert(g->ephemeron == NULL && g->weak == NULL);
1517*088332b5SXin Li lua_assert(!iswhite(g->mainthread));
1518*088332b5SXin Li g->gcstate = GCSatomic;
1519*088332b5SXin Li markobject(g, L); /* mark running thread */
1520*088332b5SXin Li /* registry and global metatables may be changed by API */
1521*088332b5SXin Li markvalue(g, &g->l_registry);
1522*088332b5SXin Li markmt(g); /* mark global metatables */
1523*088332b5SXin Li work += propagateall(g); /* empties 'gray' list */
1524*088332b5SXin Li /* remark occasional upvalues of (maybe) dead threads */
1525*088332b5SXin Li work += remarkupvals(g);
1526*088332b5SXin Li work += propagateall(g); /* propagate changes */
1527*088332b5SXin Li g->gray = grayagain;
1528*088332b5SXin Li work += propagateall(g); /* traverse 'grayagain' list */
1529*088332b5SXin Li convergeephemerons(g);
1530*088332b5SXin Li /* at this point, all strongly accessible objects are marked. */
1531*088332b5SXin Li /* Clear values from weak tables, before checking finalizers */
1532*088332b5SXin Li clearbyvalues(g, g->weak, NULL);
1533*088332b5SXin Li clearbyvalues(g, g->allweak, NULL);
1534*088332b5SXin Li origweak = g->weak; origall = g->allweak;
1535*088332b5SXin Li separatetobefnz(g, 0); /* separate objects to be finalized */
1536*088332b5SXin Li work += markbeingfnz(g); /* mark objects that will be finalized */
1537*088332b5SXin Li work += propagateall(g); /* remark, to propagate 'resurrection' */
1538*088332b5SXin Li convergeephemerons(g);
1539*088332b5SXin Li /* at this point, all resurrected objects are marked. */
1540*088332b5SXin Li /* remove dead objects from weak tables */
1541*088332b5SXin Li clearbykeys(g, g->ephemeron); /* clear keys from all ephemeron tables */
1542*088332b5SXin Li clearbykeys(g, g->allweak); /* clear keys from all 'allweak' tables */
1543*088332b5SXin Li /* clear values from resurrected weak tables */
1544*088332b5SXin Li clearbyvalues(g, g->weak, origweak);
1545*088332b5SXin Li clearbyvalues(g, g->allweak, origall);
1546*088332b5SXin Li luaS_clearcache(g);
1547*088332b5SXin Li g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */
1548*088332b5SXin Li lua_assert(g->gray == NULL);
1549*088332b5SXin Li return work; /* estimate of slots marked by 'atomic' */
1550*088332b5SXin Li }
1551*088332b5SXin Li
1552*088332b5SXin Li
sweepstep(lua_State * L,global_State * g,int nextstate,GCObject ** nextlist)1553*088332b5SXin Li static int sweepstep (lua_State *L, global_State *g,
1554*088332b5SXin Li int nextstate, GCObject **nextlist) {
1555*088332b5SXin Li if (g->sweepgc) {
1556*088332b5SXin Li l_mem olddebt = g->GCdebt;
1557*088332b5SXin Li int count;
1558*088332b5SXin Li g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX, &count);
1559*088332b5SXin Li g->GCestimate += g->GCdebt - olddebt; /* update estimate */
1560*088332b5SXin Li return count;
1561*088332b5SXin Li }
1562*088332b5SXin Li else { /* enter next state */
1563*088332b5SXin Li g->gcstate = nextstate;
1564*088332b5SXin Li g->sweepgc = nextlist;
1565*088332b5SXin Li return 0; /* no work done */
1566*088332b5SXin Li }
1567*088332b5SXin Li }
1568*088332b5SXin Li
1569*088332b5SXin Li
singlestep(lua_State * L)1570*088332b5SXin Li static lu_mem singlestep (lua_State *L) {
1571*088332b5SXin Li global_State *g = G(L);
1572*088332b5SXin Li switch (g->gcstate) {
1573*088332b5SXin Li case GCSpause: {
1574*088332b5SXin Li restartcollection(g);
1575*088332b5SXin Li g->gcstate = GCSpropagate;
1576*088332b5SXin Li return 1;
1577*088332b5SXin Li }
1578*088332b5SXin Li case GCSpropagate: {
1579*088332b5SXin Li if (g->gray == NULL) { /* no more gray objects? */
1580*088332b5SXin Li g->gcstate = GCSenteratomic; /* finish propagate phase */
1581*088332b5SXin Li return 0;
1582*088332b5SXin Li }
1583*088332b5SXin Li else
1584*088332b5SXin Li return propagatemark(g); /* traverse one gray object */
1585*088332b5SXin Li }
1586*088332b5SXin Li case GCSenteratomic: {
1587*088332b5SXin Li lu_mem work = atomic(L); /* work is what was traversed by 'atomic' */
1588*088332b5SXin Li entersweep(L);
1589*088332b5SXin Li g->GCestimate = gettotalbytes(g); /* first estimate */;
1590*088332b5SXin Li return work;
1591*088332b5SXin Li }
1592*088332b5SXin Li case GCSswpallgc: { /* sweep "regular" objects */
1593*088332b5SXin Li return sweepstep(L, g, GCSswpfinobj, &g->finobj);
1594*088332b5SXin Li }
1595*088332b5SXin Li case GCSswpfinobj: { /* sweep objects with finalizers */
1596*088332b5SXin Li return sweepstep(L, g, GCSswptobefnz, &g->tobefnz);
1597*088332b5SXin Li }
1598*088332b5SXin Li case GCSswptobefnz: { /* sweep objects to be finalized */
1599*088332b5SXin Li return sweepstep(L, g, GCSswpend, NULL);
1600*088332b5SXin Li }
1601*088332b5SXin Li case GCSswpend: { /* finish sweeps */
1602*088332b5SXin Li checkSizes(L, g);
1603*088332b5SXin Li g->gcstate = GCScallfin;
1604*088332b5SXin Li return 0;
1605*088332b5SXin Li }
1606*088332b5SXin Li case GCScallfin: { /* call remaining finalizers */
1607*088332b5SXin Li if (g->tobefnz && !g->gcemergency) {
1608*088332b5SXin Li int n = runafewfinalizers(L, GCFINMAX);
1609*088332b5SXin Li return n * GCFINALIZECOST;
1610*088332b5SXin Li }
1611*088332b5SXin Li else { /* emergency mode or no more finalizers */
1612*088332b5SXin Li g->gcstate = GCSpause; /* finish collection */
1613*088332b5SXin Li return 0;
1614*088332b5SXin Li }
1615*088332b5SXin Li }
1616*088332b5SXin Li default: lua_assert(0); return 0;
1617*088332b5SXin Li }
1618*088332b5SXin Li }
1619*088332b5SXin Li
1620*088332b5SXin Li
1621*088332b5SXin Li /*
1622*088332b5SXin Li ** advances the garbage collector until it reaches a state allowed
1623*088332b5SXin Li ** by 'statemask'
1624*088332b5SXin Li */
luaC_runtilstate(lua_State * L,int statesmask)1625*088332b5SXin Li void luaC_runtilstate (lua_State *L, int statesmask) {
1626*088332b5SXin Li global_State *g = G(L);
1627*088332b5SXin Li while (!testbit(statesmask, g->gcstate))
1628*088332b5SXin Li singlestep(L);
1629*088332b5SXin Li }
1630*088332b5SXin Li
1631*088332b5SXin Li
1632*088332b5SXin Li /*
1633*088332b5SXin Li ** Performs a basic incremental step. The debt and step size are
1634*088332b5SXin Li ** converted from bytes to "units of work"; then the function loops
1635*088332b5SXin Li ** running single steps until adding that many units of work or
1636*088332b5SXin Li ** finishing a cycle (pause state). Finally, it sets the debt that
1637*088332b5SXin Li ** controls when next step will be performed.
1638*088332b5SXin Li */
incstep(lua_State * L,global_State * g)1639*088332b5SXin Li static void incstep (lua_State *L, global_State *g) {
1640*088332b5SXin Li int stepmul = (getgcparam(g->gcstepmul) | 1); /* avoid division by 0 */
1641*088332b5SXin Li l_mem debt = (g->GCdebt / WORK2MEM) * stepmul;
1642*088332b5SXin Li l_mem stepsize = (g->gcstepsize <= log2maxs(l_mem))
1643*088332b5SXin Li ? ((cast(l_mem, 1) << g->gcstepsize) / WORK2MEM) * stepmul
1644*088332b5SXin Li : MAX_LMEM; /* overflow; keep maximum value */
1645*088332b5SXin Li do { /* repeat until pause or enough "credit" (negative debt) */
1646*088332b5SXin Li lu_mem work = singlestep(L); /* perform one single step */
1647*088332b5SXin Li debt -= work;
1648*088332b5SXin Li } while (debt > -stepsize && g->gcstate != GCSpause);
1649*088332b5SXin Li if (g->gcstate == GCSpause)
1650*088332b5SXin Li setpause(g); /* pause until next cycle */
1651*088332b5SXin Li else {
1652*088332b5SXin Li debt = (debt / stepmul) * WORK2MEM; /* convert 'work units' to bytes */
1653*088332b5SXin Li luaE_setdebt(g, debt);
1654*088332b5SXin Li }
1655*088332b5SXin Li }
1656*088332b5SXin Li
1657*088332b5SXin Li /*
1658*088332b5SXin Li ** performs a basic GC step if collector is running
1659*088332b5SXin Li */
luaC_step(lua_State * L)1660*088332b5SXin Li void luaC_step (lua_State *L) {
1661*088332b5SXin Li global_State *g = G(L);
1662*088332b5SXin Li lua_assert(!g->gcemergency);
1663*088332b5SXin Li if (g->gcrunning) { /* running? */
1664*088332b5SXin Li if(isdecGCmodegen(g))
1665*088332b5SXin Li genstep(L, g);
1666*088332b5SXin Li else
1667*088332b5SXin Li incstep(L, g);
1668*088332b5SXin Li }
1669*088332b5SXin Li }
1670*088332b5SXin Li
1671*088332b5SXin Li
1672*088332b5SXin Li /*
1673*088332b5SXin Li ** Perform a full collection in incremental mode.
1674*088332b5SXin Li ** Before running the collection, check 'keepinvariant'; if it is true,
1675*088332b5SXin Li ** there may be some objects marked as black, so the collector has
1676*088332b5SXin Li ** to sweep all objects to turn them back to white (as white has not
1677*088332b5SXin Li ** changed, nothing will be collected).
1678*088332b5SXin Li */
fullinc(lua_State * L,global_State * g)1679*088332b5SXin Li static void fullinc (lua_State *L, global_State *g) {
1680*088332b5SXin Li if (keepinvariant(g)) /* black objects? */
1681*088332b5SXin Li entersweep(L); /* sweep everything to turn them back to white */
1682*088332b5SXin Li /* finish any pending sweep phase to start a new cycle */
1683*088332b5SXin Li luaC_runtilstate(L, bitmask(GCSpause));
1684*088332b5SXin Li luaC_runtilstate(L, bitmask(GCScallfin)); /* run up to finalizers */
1685*088332b5SXin Li /* estimate must be correct after a full GC cycle */
1686*088332b5SXin Li lua_assert(g->GCestimate == gettotalbytes(g));
1687*088332b5SXin Li luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */
1688*088332b5SXin Li setpause(g);
1689*088332b5SXin Li }
1690*088332b5SXin Li
1691*088332b5SXin Li
1692*088332b5SXin Li /*
1693*088332b5SXin Li ** Performs a full GC cycle; if 'isemergency', set a flag to avoid
1694*088332b5SXin Li ** some operations which could change the interpreter state in some
1695*088332b5SXin Li ** unexpected ways (running finalizers and shrinking some structures).
1696*088332b5SXin Li */
luaC_fullgc(lua_State * L,int isemergency)1697*088332b5SXin Li void luaC_fullgc (lua_State *L, int isemergency) {
1698*088332b5SXin Li global_State *g = G(L);
1699*088332b5SXin Li lua_assert(!g->gcemergency);
1700*088332b5SXin Li g->gcemergency = isemergency; /* set flag */
1701*088332b5SXin Li if (g->gckind == KGC_INC)
1702*088332b5SXin Li fullinc(L, g);
1703*088332b5SXin Li else
1704*088332b5SXin Li fullgen(L, g);
1705*088332b5SXin Li g->gcemergency = 0;
1706*088332b5SXin Li }
1707*088332b5SXin Li
1708*088332b5SXin Li /* }====================================================== */
1709*088332b5SXin Li
1710*088332b5SXin Li
1711