xref: /illumos-gate/usr/src/uts/common/fs/zfs/lua/lgc.c (revision 46ac8fdf)
1dfc11533SChris Williamson /*
2dfc11533SChris Williamson ** $Id: lgc.c,v 2014/09/01 16:55:08 roberto Exp $
3dfc11533SChris Williamson ** Garbage Collector
4dfc11533SChris Williamson ** See Copyright Notice in lua.h
5dfc11533SChris Williamson */
6dfc11533SChris Williamson 
7dfc11533SChris Williamson #include <sys/zfs_context.h>
8dfc11533SChris Williamson 
9dfc11533SChris Williamson #define lgc_c
10dfc11533SChris Williamson #define LUA_CORE
11dfc11533SChris Williamson 
12dfc11533SChris Williamson #include "lua.h"
13dfc11533SChris Williamson 
14dfc11533SChris Williamson #include "ldebug.h"
15dfc11533SChris Williamson #include "ldo.h"
16dfc11533SChris Williamson #include "lfunc.h"
17dfc11533SChris Williamson #include "lgc.h"
18dfc11533SChris Williamson #include "lmem.h"
19dfc11533SChris Williamson #include "lobject.h"
20dfc11533SChris Williamson #include "lstate.h"
21dfc11533SChris Williamson #include "lstring.h"
22dfc11533SChris Williamson #include "ltable.h"
23dfc11533SChris Williamson #include "ltm.h"
24dfc11533SChris Williamson 
25dfc11533SChris Williamson 
26dfc11533SChris Williamson 
27dfc11533SChris Williamson /*
28dfc11533SChris Williamson ** cost of sweeping one element (the size of a small object divided
29dfc11533SChris Williamson ** by some adjust for the sweep speed)
30dfc11533SChris Williamson */
31dfc11533SChris Williamson #define GCSWEEPCOST	((sizeof(TString) + 4) / 4)
32dfc11533SChris Williamson 
33dfc11533SChris Williamson /* maximum number of elements to sweep in each single step */
34dfc11533SChris Williamson #define GCSWEEPMAX	(cast_int((GCSTEPSIZE / GCSWEEPCOST) / 4))
35dfc11533SChris Williamson 
36dfc11533SChris Williamson /* maximum number of finalizers to call in each GC step */
37dfc11533SChris Williamson #define GCFINALIZENUM	4
38dfc11533SChris Williamson 
39dfc11533SChris Williamson 
40dfc11533SChris Williamson /*
41dfc11533SChris Williamson ** macro to adjust 'stepmul': 'stepmul' is actually used like
42dfc11533SChris Williamson ** 'stepmul / STEPMULADJ' (value chosen by tests)
43dfc11533SChris Williamson */
44dfc11533SChris Williamson #define STEPMULADJ		200
45dfc11533SChris Williamson 
46dfc11533SChris Williamson 
47dfc11533SChris Williamson /*
48dfc11533SChris Williamson ** macro to adjust 'pause': 'pause' is actually used like
49dfc11533SChris Williamson ** 'pause / PAUSEADJ' (value chosen by tests)
50dfc11533SChris Williamson */
51dfc11533SChris Williamson #define PAUSEADJ		100
52dfc11533SChris Williamson 
53dfc11533SChris Williamson 
54dfc11533SChris Williamson /*
55dfc11533SChris Williamson ** 'makewhite' erases all color bits plus the old bit and then
56dfc11533SChris Williamson ** sets only the current white bit
57dfc11533SChris Williamson */
58dfc11533SChris Williamson #define maskcolors	(~(bit2mask(BLACKBIT, OLDBIT) | WHITEBITS))
59dfc11533SChris Williamson #define makewhite(g,x)	\
60dfc11533SChris Williamson  (gch(x)->marked = cast_byte((gch(x)->marked & maskcolors) | luaC_white(g)))
61dfc11533SChris Williamson 
62dfc11533SChris Williamson #define white2gray(x)	resetbits(gch(x)->marked, WHITEBITS)
63dfc11533SChris Williamson #define black2gray(x)	resetbit(gch(x)->marked, BLACKBIT)
64dfc11533SChris Williamson 
65dfc11533SChris Williamson 
66dfc11533SChris Williamson #define isfinalized(x)		testbit(gch(x)->marked, FINALIZEDBIT)
67dfc11533SChris Williamson 
68dfc11533SChris Williamson #define checkdeadkey(n)	lua_assert(!ttisdeadkey(gkey(n)) || ttisnil(gval(n)))
69dfc11533SChris Williamson 
70dfc11533SChris Williamson 
71dfc11533SChris Williamson #define checkconsistency(obj)  \
72dfc11533SChris Williamson   lua_longassert(!iscollectable(obj) || righttt(obj))
73dfc11533SChris Williamson 
74dfc11533SChris Williamson 
75dfc11533SChris Williamson #define markvalue(g,o) { checkconsistency(o); \
76dfc11533SChris Williamson   if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); }
77dfc11533SChris Williamson 
78dfc11533SChris Williamson #define markobject(g,t) { if ((t) && iswhite(obj2gco(t))) \
79dfc11533SChris Williamson 		reallymarkobject(g, obj2gco(t)); }
80dfc11533SChris Williamson 
81dfc11533SChris Williamson static void reallymarkobject (global_State *g, GCObject *o);
82dfc11533SChris Williamson 
83dfc11533SChris Williamson 
84dfc11533SChris Williamson /*
85dfc11533SChris Williamson ** {======================================================
86dfc11533SChris Williamson ** Generic functions
87dfc11533SChris Williamson ** =======================================================
88dfc11533SChris Williamson */
89dfc11533SChris Williamson 
90dfc11533SChris Williamson 
91dfc11533SChris Williamson /*
92dfc11533SChris Williamson ** one after last element in a hash array
93dfc11533SChris Williamson */
94dfc11533SChris Williamson #define gnodelast(h)	gnode(h, cast(size_t, sizenode(h)))
95dfc11533SChris Williamson 
96dfc11533SChris Williamson 
97dfc11533SChris Williamson /*
98dfc11533SChris Williamson ** link table 'h' into list pointed by 'p'
99dfc11533SChris Williamson */
100dfc11533SChris Williamson #define linktable(h,p)	((h)->gclist = *(p), *(p) = obj2gco(h))
101dfc11533SChris Williamson 
102dfc11533SChris Williamson 
103dfc11533SChris Williamson /*
104dfc11533SChris Williamson ** if key is not marked, mark its entry as dead (therefore removing it
105dfc11533SChris Williamson ** from the table)
106dfc11533SChris Williamson */
removeentry(Node * n)107dfc11533SChris Williamson static void removeentry (Node *n) {
108dfc11533SChris Williamson   lua_assert(ttisnil(gval(n)));
109dfc11533SChris Williamson   if (valiswhite(gkey(n)))
110dfc11533SChris Williamson     setdeadvalue(gkey(n));  /* unused and unmarked key; remove it */
111dfc11533SChris Williamson }
112dfc11533SChris Williamson 
113dfc11533SChris Williamson 
114dfc11533SChris Williamson /*
115dfc11533SChris Williamson ** tells whether a key or value can be cleared from a weak
116dfc11533SChris Williamson ** table. Non-collectable objects are never removed from weak
117dfc11533SChris Williamson ** tables. Strings behave as `values', so are never removed too. for
118dfc11533SChris Williamson ** other objects: if really collected, cannot keep them; for objects
119dfc11533SChris Williamson ** being finalized, keep them in keys, but not in values
120dfc11533SChris Williamson */
iscleared(global_State * g,const TValue * o)121dfc11533SChris Williamson static int iscleared (global_State *g, const TValue *o) {
122dfc11533SChris Williamson   if (!iscollectable(o)) return 0;
123dfc11533SChris Williamson   else if (ttisstring(o)) {
124dfc11533SChris Williamson     markobject(g, rawtsvalue(o));  /* strings are `values', so are never weak */
125dfc11533SChris Williamson     return 0;
126dfc11533SChris Williamson   }
127dfc11533SChris Williamson   else return iswhite(gcvalue(o));
128dfc11533SChris Williamson }
129dfc11533SChris Williamson 
130dfc11533SChris Williamson 
131dfc11533SChris Williamson /*
132dfc11533SChris Williamson ** barrier that moves collector forward, that is, mark the white object
133dfc11533SChris Williamson ** being pointed by a black object.
134dfc11533SChris Williamson */
luaC_barrier_(lua_State * L,GCObject * o,GCObject * v)135dfc11533SChris Williamson void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) {
136dfc11533SChris Williamson   global_State *g = G(L);
137dfc11533SChris Williamson   lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));
138dfc11533SChris Williamson   lua_assert(g->gcstate != GCSpause);
139dfc11533SChris Williamson   lua_assert(gch(o)->tt != LUA_TTABLE);
140dfc11533SChris Williamson   if (keepinvariantout(g))  /* must keep invariant? */
141dfc11533SChris Williamson     reallymarkobject(g, v);  /* restore invariant */
142dfc11533SChris Williamson   else {  /* sweep phase */
143dfc11533SChris Williamson     lua_assert(issweepphase(g));
144dfc11533SChris Williamson     makewhite(g, o);  /* mark main obj. as white to avoid other barriers */
145dfc11533SChris Williamson   }
146dfc11533SChris Williamson }
147dfc11533SChris Williamson 
148dfc11533SChris Williamson 
149dfc11533SChris Williamson /*
150dfc11533SChris Williamson ** barrier that moves collector backward, that is, mark the black object
151dfc11533SChris Williamson ** pointing to a white object as gray again. (Current implementation
152dfc11533SChris Williamson ** only works for tables; access to 'gclist' is not uniform across
153dfc11533SChris Williamson ** different types.)
154dfc11533SChris Williamson */
luaC_barrierback_(lua_State * L,GCObject * o)155dfc11533SChris Williamson void luaC_barrierback_ (lua_State *L, GCObject *o) {
156dfc11533SChris Williamson   global_State *g = G(L);
157dfc11533SChris Williamson   lua_assert(isblack(o) && !isdead(g, o) && gch(o)->tt == LUA_TTABLE);
158dfc11533SChris Williamson   black2gray(o);  /* make object gray (again) */
159dfc11533SChris Williamson   gco2t(o)->gclist = g->grayagain;
160dfc11533SChris Williamson   g->grayagain = o;
161dfc11533SChris Williamson }
162dfc11533SChris Williamson 
163dfc11533SChris Williamson 
164dfc11533SChris Williamson /*
165dfc11533SChris Williamson ** barrier for prototypes. When creating first closure (cache is
166dfc11533SChris Williamson ** NULL), use a forward barrier; this may be the only closure of the
167dfc11533SChris Williamson ** prototype (if it is a "regular" function, with a single instance)
168dfc11533SChris Williamson ** and the prototype may be big, so it is better to avoid traversing
169dfc11533SChris Williamson ** it again. Otherwise, use a backward barrier, to avoid marking all
170dfc11533SChris Williamson ** possible instances.
171dfc11533SChris Williamson */
luaC_barrierproto_(lua_State * L,Proto * p,Closure * c)172dfc11533SChris Williamson LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c) {
173dfc11533SChris Williamson   global_State *g = G(L);
174dfc11533SChris Williamson   lua_assert(isblack(obj2gco(p)));
175dfc11533SChris Williamson   if (p->cache == NULL) {  /* first time? */
176dfc11533SChris Williamson     luaC_objbarrier(L, p, c);
177dfc11533SChris Williamson   }
178dfc11533SChris Williamson   else {  /* use a backward barrier */
179dfc11533SChris Williamson     black2gray(obj2gco(p));  /* make prototype gray (again) */
180dfc11533SChris Williamson     p->gclist = g->grayagain;
181dfc11533SChris Williamson     g->grayagain = obj2gco(p);
182dfc11533SChris Williamson   }
183dfc11533SChris Williamson }
184dfc11533SChris Williamson 
185dfc11533SChris Williamson 
186dfc11533SChris Williamson /*
187dfc11533SChris Williamson ** check color (and invariants) for an upvalue that was closed,
188dfc11533SChris Williamson ** i.e., moved into the 'allgc' list
189dfc11533SChris Williamson */
luaC_checkupvalcolor(global_State * g,UpVal * uv)190dfc11533SChris Williamson void luaC_checkupvalcolor (global_State *g, UpVal *uv) {
191dfc11533SChris Williamson   GCObject *o = obj2gco(uv);
192dfc11533SChris Williamson   lua_assert(!isblack(o));  /* open upvalues are never black */
193dfc11533SChris Williamson   if (isgray(o)) {
194dfc11533SChris Williamson     if (keepinvariant(g)) {
195dfc11533SChris Williamson       resetoldbit(o);  /* see MOVE OLD rule */
196dfc11533SChris Williamson       gray2black(o);  /* it is being visited now */
197dfc11533SChris Williamson       markvalue(g, uv->v);
198dfc11533SChris Williamson     }
199dfc11533SChris Williamson     else {
200dfc11533SChris Williamson       lua_assert(issweepphase(g));
201dfc11533SChris Williamson       makewhite(g, o);
202dfc11533SChris Williamson     }
203dfc11533SChris Williamson   }
204dfc11533SChris Williamson }
205dfc11533SChris Williamson 
206dfc11533SChris Williamson 
207dfc11533SChris Williamson /*
208dfc11533SChris Williamson ** create a new collectable object (with given type and size) and link
209dfc11533SChris Williamson ** it to '*list'. 'offset' tells how many bytes to allocate before the
210dfc11533SChris Williamson ** object itself (used only by states).
211dfc11533SChris Williamson */
luaC_newobj(lua_State * L,int tt,size_t sz,GCObject ** list,int offset)212dfc11533SChris Williamson GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, GCObject **list,
213dfc11533SChris Williamson                        int offset) {
214dfc11533SChris Williamson   global_State *g = G(L);
215dfc11533SChris Williamson   char *raw = cast(char *, luaM_newobject(L, novariant(tt), sz));
216dfc11533SChris Williamson   GCObject *o = obj2gco(raw + offset);
217dfc11533SChris Williamson   if (list == NULL)
218dfc11533SChris Williamson     list = &g->allgc;  /* standard list for collectable objects */
219dfc11533SChris Williamson   gch(o)->marked = luaC_white(g);
220dfc11533SChris Williamson   gch(o)->tt = tt;
221dfc11533SChris Williamson   gch(o)->next = *list;
222dfc11533SChris Williamson   *list = o;
223dfc11533SChris Williamson   return o;
224dfc11533SChris Williamson }
225dfc11533SChris Williamson 
226dfc11533SChris Williamson /* }====================================================== */
227dfc11533SChris Williamson 
228dfc11533SChris Williamson 
229dfc11533SChris Williamson 
230dfc11533SChris Williamson /*
231dfc11533SChris Williamson ** {======================================================
232dfc11533SChris Williamson ** Mark functions
233dfc11533SChris Williamson ** =======================================================
234dfc11533SChris Williamson */
235dfc11533SChris Williamson 
236dfc11533SChris Williamson 
237dfc11533SChris Williamson /*
238dfc11533SChris Williamson ** mark an object. Userdata, strings, and closed upvalues are visited
239dfc11533SChris Williamson ** and turned black here. Other objects are marked gray and added
240dfc11533SChris Williamson ** to appropriate list to be visited (and turned black) later. (Open
241dfc11533SChris Williamson ** upvalues are already linked in 'headuv' list.)
242dfc11533SChris Williamson */
reallymarkobject(global_State * g,GCObject * o)243dfc11533SChris Williamson static void reallymarkobject (global_State *g, GCObject *o) {
244dfc11533SChris Williamson   lu_mem size;
245dfc11533SChris Williamson   white2gray(o);
246dfc11533SChris Williamson   switch (gch(o)->tt) {
247dfc11533SChris Williamson     case LUA_TSHRSTR:
248dfc11533SChris Williamson     case LUA_TLNGSTR: {
249dfc11533SChris Williamson       size = sizestring(gco2ts(o));
250dfc11533SChris Williamson       break;  /* nothing else to mark; make it black */
251dfc11533SChris Williamson     }
252dfc11533SChris Williamson     case LUA_TUSERDATA: {
253dfc11533SChris Williamson       Table *mt = gco2u(o)->metatable;
254dfc11533SChris Williamson       markobject(g, mt);
255dfc11533SChris Williamson       markobject(g, gco2u(o)->env);
256dfc11533SChris Williamson       size = sizeudata(gco2u(o));
257dfc11533SChris Williamson       break;
258dfc11533SChris Williamson     }
259dfc11533SChris Williamson     case LUA_TUPVAL: {
260dfc11533SChris Williamson       UpVal *uv = gco2uv(o);
261dfc11533SChris Williamson       markvalue(g, uv->v);
262dfc11533SChris Williamson       if (uv->v != &uv->u.value)  /* open? */
263dfc11533SChris Williamson         return;  /* open upvalues remain gray */
264dfc11533SChris Williamson       size = sizeof(UpVal);
265dfc11533SChris Williamson       break;
266dfc11533SChris Williamson     }
267dfc11533SChris Williamson     case LUA_TLCL: {
268dfc11533SChris Williamson       gco2lcl(o)->gclist = g->gray;
269dfc11533SChris Williamson       g->gray = o;
270dfc11533SChris Williamson       return;
271dfc11533SChris Williamson     }
272dfc11533SChris Williamson     case LUA_TCCL: {
273dfc11533SChris Williamson       gco2ccl(o)->gclist = g->gray;
274dfc11533SChris Williamson       g->gray = o;
275dfc11533SChris Williamson       return;
276dfc11533SChris Williamson     }
277dfc11533SChris Williamson     case LUA_TTABLE: {
278dfc11533SChris Williamson       linktable(gco2t(o), &g->gray);
279dfc11533SChris Williamson       return;
280dfc11533SChris Williamson     }
281dfc11533SChris Williamson     case LUA_TTHREAD: {
282dfc11533SChris Williamson       gco2th(o)->gclist = g->gray;
283dfc11533SChris Williamson       g->gray = o;
284dfc11533SChris Williamson       return;
285dfc11533SChris Williamson     }
286dfc11533SChris Williamson     case LUA_TPROTO: {
287dfc11533SChris Williamson       gco2p(o)->gclist = g->gray;
288dfc11533SChris Williamson       g->gray = o;
289dfc11533SChris Williamson       return;
290dfc11533SChris Williamson     }
291dfc11533SChris Williamson     default: lua_assert(0); return;
292dfc11533SChris Williamson   }
293dfc11533SChris Williamson   gray2black(o);
294dfc11533SChris Williamson   g->GCmemtrav += size;
295dfc11533SChris Williamson }
296dfc11533SChris Williamson 
297dfc11533SChris Williamson 
298dfc11533SChris Williamson /*
299dfc11533SChris Williamson ** mark metamethods for basic types
300dfc11533SChris Williamson */
markmt(global_State * g)301dfc11533SChris Williamson static void markmt (global_State *g) {
302dfc11533SChris Williamson   int i;
303dfc11533SChris Williamson   for (i=0; i < LUA_NUMTAGS; i++)
304dfc11533SChris Williamson     markobject(g, g->mt[i]);
305dfc11533SChris Williamson }
306dfc11533SChris Williamson 
307dfc11533SChris Williamson 
308dfc11533SChris Williamson /*
309dfc11533SChris Williamson ** mark all objects in list of being-finalized
310dfc11533SChris Williamson */
markbeingfnz(global_State * g)311dfc11533SChris Williamson static void markbeingfnz (global_State *g) {
312dfc11533SChris Williamson   GCObject *o;
313dfc11533SChris Williamson   for (o = g->tobefnz; o != NULL; o = gch(o)->next) {
314dfc11533SChris Williamson     makewhite(g, o);
315dfc11533SChris Williamson     reallymarkobject(g, o);
316dfc11533SChris Williamson   }
317dfc11533SChris Williamson }
318dfc11533SChris Williamson 
319dfc11533SChris Williamson 
320dfc11533SChris Williamson /*
321dfc11533SChris Williamson ** mark all values stored in marked open upvalues. (See comment in
322dfc11533SChris Williamson ** 'lstate.h'.)
323dfc11533SChris Williamson */
remarkupvals(global_State * g)324dfc11533SChris Williamson static void remarkupvals (global_State *g) {
325dfc11533SChris Williamson   UpVal *uv;
326dfc11533SChris Williamson   for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) {
327dfc11533SChris Williamson     if (isgray(obj2gco(uv)))
328dfc11533SChris Williamson       markvalue(g, uv->v);
329dfc11533SChris Williamson   }
330dfc11533SChris Williamson }
331dfc11533SChris Williamson 
332dfc11533SChris Williamson 
333dfc11533SChris Williamson /*
334dfc11533SChris Williamson ** mark root set and reset all gray lists, to start a new
335dfc11533SChris Williamson ** incremental (or full) collection
336dfc11533SChris Williamson */
restartcollection(global_State * g)337dfc11533SChris Williamson static void restartcollection (global_State *g) {
338dfc11533SChris Williamson   g->gray = g->grayagain = NULL;
339dfc11533SChris Williamson   g->weak = g->allweak = g->ephemeron = NULL;
340dfc11533SChris Williamson   markobject(g, g->mainthread);
341dfc11533SChris Williamson   markvalue(g, &g->l_registry);
342dfc11533SChris Williamson   markmt(g);
343dfc11533SChris Williamson   markbeingfnz(g);  /* mark any finalizing object left from previous cycle */
344dfc11533SChris Williamson }
345dfc11533SChris Williamson 
346dfc11533SChris Williamson /* }====================================================== */
347dfc11533SChris Williamson 
348dfc11533SChris Williamson 
349dfc11533SChris Williamson /*
350dfc11533SChris Williamson ** {======================================================
351dfc11533SChris Williamson ** Traverse functions
352dfc11533SChris Williamson ** =======================================================
353dfc11533SChris Williamson */
354dfc11533SChris Williamson 
traverseweakvalue(global_State * g,Table * h)355dfc11533SChris Williamson static void traverseweakvalue (global_State *g, Table *h) {
356dfc11533SChris Williamson   Node *n, *limit = gnodelast(h);
357dfc11533SChris Williamson   /* if there is array part, assume it may have white values (do not
358dfc11533SChris Williamson      traverse it just to check) */
359dfc11533SChris Williamson   int hasclears = (h->sizearray > 0);
360dfc11533SChris Williamson   for (n = gnode(h, 0); n < limit; n++) {
361dfc11533SChris Williamson     checkdeadkey(n);
362dfc11533SChris Williamson     if (ttisnil(gval(n)))  /* entry is empty? */
363dfc11533SChris Williamson       removeentry(n);  /* remove it */
364dfc11533SChris Williamson     else {
365dfc11533SChris Williamson       lua_assert(!ttisnil(gkey(n)));
366dfc11533SChris Williamson       markvalue(g, gkey(n));  /* mark key */
367dfc11533SChris Williamson       if (!hasclears && iscleared(g, gval(n)))  /* is there a white value? */
368dfc11533SChris Williamson         hasclears = 1;  /* table will have to be cleared */
369dfc11533SChris Williamson     }
370dfc11533SChris Williamson   }
371dfc11533SChris Williamson   if (hasclears)
372dfc11533SChris Williamson     linktable(h, &g->weak);  /* has to be cleared later */
373dfc11533SChris Williamson   else  /* no white values */
374dfc11533SChris Williamson     linktable(h, &g->grayagain);  /* no need to clean */
375dfc11533SChris Williamson }
376dfc11533SChris Williamson 
377dfc11533SChris Williamson 
traverseephemeron(global_State * g,Table * h)378dfc11533SChris Williamson static int traverseephemeron (global_State *g, Table *h) {
379dfc11533SChris Williamson   int marked = 0;  /* true if an object is marked in this traversal */
380dfc11533SChris Williamson   int hasclears = 0;  /* true if table has white keys */
381dfc11533SChris Williamson   int prop = 0;  /* true if table has entry "white-key -> white-value" */
382dfc11533SChris Williamson   Node *n, *limit = gnodelast(h);
383dfc11533SChris Williamson   int i;
384dfc11533SChris Williamson   /* traverse array part (numeric keys are 'strong') */
385dfc11533SChris Williamson   for (i = 0; i < h->sizearray; i++) {
386dfc11533SChris Williamson     if (valiswhite(&h->array[i])) {
387dfc11533SChris Williamson       marked = 1;
388dfc11533SChris Williamson       reallymarkobject(g, gcvalue(&h->array[i]));
389dfc11533SChris Williamson     }
390dfc11533SChris Williamson   }
391dfc11533SChris Williamson   /* traverse hash part */
392dfc11533SChris Williamson   for (n = gnode(h, 0); n < limit; n++) {
393dfc11533SChris Williamson     checkdeadkey(n);
394dfc11533SChris Williamson     if (ttisnil(gval(n)))  /* entry is empty? */
395dfc11533SChris Williamson       removeentry(n);  /* remove it */
396dfc11533SChris Williamson     else if (iscleared(g, gkey(n))) {  /* key is not marked (yet)? */
397dfc11533SChris Williamson       hasclears = 1;  /* table must be cleared */
398dfc11533SChris Williamson       if (valiswhite(gval(n)))  /* value not marked yet? */
399dfc11533SChris Williamson         prop = 1;  /* must propagate again */
400dfc11533SChris Williamson     }
401dfc11533SChris Williamson     else if (valiswhite(gval(n))) {  /* value not marked yet? */
402dfc11533SChris Williamson       marked = 1;
403dfc11533SChris Williamson       reallymarkobject(g, gcvalue(gval(n)));  /* mark it now */
404dfc11533SChris Williamson     }
405dfc11533SChris Williamson   }
406dfc11533SChris Williamson   if (g->gcstate != GCSatomic || prop)
407dfc11533SChris Williamson     linktable(h, &g->ephemeron);  /* have to propagate again */
408dfc11533SChris Williamson   else if (hasclears)  /* does table have white keys? */
409dfc11533SChris Williamson     linktable(h, &g->allweak);  /* may have to clean white keys */
410dfc11533SChris Williamson   else  /* no white keys */
411dfc11533SChris Williamson     linktable(h, &g->grayagain);  /* no need to clean */
412dfc11533SChris Williamson   return marked;
413dfc11533SChris Williamson }
414dfc11533SChris Williamson 
415dfc11533SChris Williamson 
traversestrongtable(global_State * g,Table * h)416dfc11533SChris Williamson static void traversestrongtable (global_State *g, Table *h) {
417dfc11533SChris Williamson   Node *n, *limit = gnodelast(h);
418dfc11533SChris Williamson   int i;
419dfc11533SChris Williamson   for (i = 0; i < h->sizearray; i++)  /* traverse array part */
420dfc11533SChris Williamson     markvalue(g, &h->array[i]);
421dfc11533SChris Williamson   for (n = gnode(h, 0); n < limit; n++) {  /* traverse hash part */
422dfc11533SChris Williamson     checkdeadkey(n);
423dfc11533SChris Williamson     if (ttisnil(gval(n)))  /* entry is empty? */
424dfc11533SChris Williamson       removeentry(n);  /* remove it */
425dfc11533SChris Williamson     else {
426dfc11533SChris Williamson       lua_assert(!ttisnil(gkey(n)));
427dfc11533SChris Williamson       markvalue(g, gkey(n));  /* mark key */
428dfc11533SChris Williamson       markvalue(g, gval(n));  /* mark value */
429dfc11533SChris Williamson     }
430dfc11533SChris Williamson   }
431dfc11533SChris Williamson }
432dfc11533SChris Williamson 
433dfc11533SChris Williamson 
traversetable(global_State * g,Table * h)434dfc11533SChris Williamson static lu_mem traversetable (global_State *g, Table *h) {
435dfc11533SChris Williamson   const char *weakkey, *weakvalue;
436dfc11533SChris Williamson   const TValue *mode = gfasttm(g, h->metatable, TM_MODE);
437dfc11533SChris Williamson   markobject(g, h->metatable);
438dfc11533SChris Williamson   if (mode && ttisstring(mode) &&  /* is there a weak mode? */
439dfc11533SChris Williamson       ((weakkey = strchr(svalue(mode), 'k')),
440dfc11533SChris Williamson        (weakvalue = strchr(svalue(mode), 'v')),
441dfc11533SChris Williamson        (weakkey || weakvalue))) {  /* is really weak? */
442dfc11533SChris Williamson     black2gray(obj2gco(h));  /* keep table gray */
443dfc11533SChris Williamson     if (!weakkey)  /* strong keys? */
444dfc11533SChris Williamson       traverseweakvalue(g, h);
445dfc11533SChris Williamson     else if (!weakvalue)  /* strong values? */
446dfc11533SChris Williamson       traverseephemeron(g, h);
447dfc11533SChris Williamson     else  /* all weak */
448dfc11533SChris Williamson       linktable(h, &g->allweak);  /* nothing to traverse now */
449dfc11533SChris Williamson   }
450dfc11533SChris Williamson   else  /* not weak */
451dfc11533SChris Williamson     traversestrongtable(g, h);
452dfc11533SChris Williamson   return sizeof(Table) + sizeof(TValue) * h->sizearray +
453dfc11533SChris Williamson                          sizeof(Node) * cast(size_t, sizenode(h));
454dfc11533SChris Williamson }
455dfc11533SChris Williamson 
456dfc11533SChris Williamson 
traverseproto(global_State * g,Proto * f)457dfc11533SChris Williamson static int traverseproto (global_State *g, Proto *f) {
458dfc11533SChris Williamson   int i;
459dfc11533SChris Williamson   if (f->cache && iswhite(obj2gco(f->cache)))
460dfc11533SChris Williamson     f->cache = NULL;  /* allow cache to be collected */
461dfc11533SChris Williamson   markobject(g, f->source);
462dfc11533SChris Williamson   for (i = 0; i < f->sizek; i++)  /* mark literals */
463dfc11533SChris Williamson     markvalue(g, &f->k[i]);
464dfc11533SChris Williamson   for (i = 0; i < f->sizeupvalues; i++)  /* mark upvalue names */
465dfc11533SChris Williamson     markobject(g, f->upvalues[i].name);
466dfc11533SChris Williamson   for (i = 0; i < f->sizep; i++)  /* mark nested protos */
467dfc11533SChris Williamson     markobject(g, f->p[i]);
468dfc11533SChris Williamson   for (i = 0; i < f->sizelocvars; i++)  /* mark local-variable names */
469dfc11533SChris Williamson     markobject(g, f->locvars[i].varname);
470dfc11533SChris Williamson   return sizeof(Proto) + sizeof(Instruction) * f->sizecode +
471dfc11533SChris Williamson                          sizeof(Proto *) * f->sizep +
472dfc11533SChris Williamson                          sizeof(TValue) * f->sizek +
473dfc11533SChris Williamson                          sizeof(int) * f->sizelineinfo +
474dfc11533SChris Williamson                          sizeof(LocVar) * f->sizelocvars +
475dfc11533SChris Williamson                          sizeof(Upvaldesc) * f->sizeupvalues;
476dfc11533SChris Williamson }
477dfc11533SChris Williamson 
478dfc11533SChris Williamson 
traverseCclosure(global_State * g,CClosure * cl)479dfc11533SChris Williamson static lu_mem traverseCclosure (global_State *g, CClosure *cl) {
480dfc11533SChris Williamson   int i;
481dfc11533SChris Williamson   for (i = 0; i < cl->nupvalues; i++)  /* mark its upvalues */
482dfc11533SChris Williamson     markvalue(g, &cl->upvalue[i]);
483dfc11533SChris Williamson   return sizeCclosure(cl->nupvalues);
484dfc11533SChris Williamson }
485dfc11533SChris Williamson 
traverseLclosure(global_State * g,LClosure * cl)486dfc11533SChris Williamson static lu_mem traverseLclosure (global_State *g, LClosure *cl) {
487dfc11533SChris Williamson   int i;
488dfc11533SChris Williamson   markobject(g, cl->p);  /* mark its prototype */
489dfc11533SChris Williamson   for (i = 0; i < cl->nupvalues; i++)  /* mark its upvalues */
490dfc11533SChris Williamson     markobject(g, cl->upvals[i]);
491dfc11533SChris Williamson   return sizeLclosure(cl->nupvalues);
492dfc11533SChris Williamson }
493dfc11533SChris Williamson 
494dfc11533SChris Williamson 
traversestack(global_State * g,lua_State * th)495dfc11533SChris Williamson static lu_mem traversestack (global_State *g, lua_State *th) {
496dfc11533SChris Williamson   int n = 0;
497dfc11533SChris Williamson   StkId o = th->stack;
498dfc11533SChris Williamson   if (o == NULL)
499dfc11533SChris Williamson     return 1;  /* stack not completely built yet */
500dfc11533SChris Williamson   for (; o < th->top; o++)  /* mark live elements in the stack */
501dfc11533SChris Williamson     markvalue(g, o);
502dfc11533SChris Williamson   if (g->gcstate == GCSatomic) {  /* final traversal? */
503dfc11533SChris Williamson     StkId lim = th->stack + th->stacksize;  /* real end of stack */
504dfc11533SChris Williamson     for (; o < lim; o++)  /* clear not-marked stack slice */
505dfc11533SChris Williamson       setnilvalue(o);
506dfc11533SChris Williamson   }
507dfc11533SChris Williamson   else {  /* count call infos to compute size */
508dfc11533SChris Williamson     CallInfo *ci;
509dfc11533SChris Williamson     for (ci = &th->base_ci; ci != th->ci; ci = ci->next)
510dfc11533SChris Williamson       n++;
511dfc11533SChris Williamson   }
512dfc11533SChris Williamson   return sizeof(lua_State) + sizeof(TValue) * th->stacksize +
513dfc11533SChris Williamson          sizeof(CallInfo) * n;
514dfc11533SChris Williamson }
515dfc11533SChris Williamson 
516dfc11533SChris Williamson 
517dfc11533SChris Williamson /*
518dfc11533SChris Williamson ** traverse one gray object, turning it to black (except for threads,
519dfc11533SChris Williamson ** which are always gray).
520dfc11533SChris Williamson */
propagatemark(global_State * g)521dfc11533SChris Williamson static void propagatemark (global_State *g) {
522dfc11533SChris Williamson   lu_mem size;
523dfc11533SChris Williamson   GCObject *o = g->gray;
524dfc11533SChris Williamson   lua_assert(isgray(o));
525dfc11533SChris Williamson   gray2black(o);
526dfc11533SChris Williamson   switch (gch(o)->tt) {
527dfc11533SChris Williamson     case LUA_TTABLE: {
528dfc11533SChris Williamson       Table *h = gco2t(o);
529dfc11533SChris Williamson       g->gray = h->gclist;  /* remove from 'gray' list */
530dfc11533SChris Williamson       size = traversetable(g, h);
531dfc11533SChris Williamson       break;
532dfc11533SChris Williamson     }
533dfc11533SChris Williamson     case LUA_TLCL: {
534dfc11533SChris Williamson       LClosure *cl = gco2lcl(o);
535dfc11533SChris Williamson       g->gray = cl->gclist;  /* remove from 'gray' list */
536dfc11533SChris Williamson       size = traverseLclosure(g, cl);
537dfc11533SChris Williamson       break;
538dfc11533SChris Williamson     }
539dfc11533SChris Williamson     case LUA_TCCL: {
540dfc11533SChris Williamson       CClosure *cl = gco2ccl(o);
541dfc11533SChris Williamson       g->gray = cl->gclist;  /* remove from 'gray' list */
542dfc11533SChris Williamson       size = traverseCclosure(g, cl);
543dfc11533SChris Williamson       break;
544dfc11533SChris Williamson     }
545dfc11533SChris Williamson     case LUA_TTHREAD: {
546dfc11533SChris Williamson       lua_State *th = gco2th(o);
547dfc11533SChris Williamson       g->gray = th->gclist;  /* remove from 'gray' list */
548dfc11533SChris Williamson       th->gclist = g->grayagain;
549dfc11533SChris Williamson       g->grayagain = o;  /* insert into 'grayagain' list */
550dfc11533SChris Williamson       black2gray(o);
551dfc11533SChris Williamson       size = traversestack(g, th);
552dfc11533SChris Williamson       break;
553dfc11533SChris Williamson     }
554dfc11533SChris Williamson     case LUA_TPROTO: {
555dfc11533SChris Williamson       Proto *p = gco2p(o);
556dfc11533SChris Williamson       g->gray = p->gclist;  /* remove from 'gray' list */
557dfc11533SChris Williamson       size = traverseproto(g, p);
558dfc11533SChris Williamson       break;
559dfc11533SChris Williamson     }
560dfc11533SChris Williamson     default: lua_assert(0); return;
561dfc11533SChris Williamson   }
562dfc11533SChris Williamson   g->GCmemtrav += size;
563dfc11533SChris Williamson }
564dfc11533SChris Williamson 
565dfc11533SChris Williamson 
propagateall(global_State * g)566dfc11533SChris Williamson static void propagateall (global_State *g) {
567dfc11533SChris Williamson   while (g->gray) propagatemark(g);
568dfc11533SChris Williamson }
569dfc11533SChris Williamson 
570dfc11533SChris Williamson 
propagatelist(global_State * g,GCObject * l)571dfc11533SChris Williamson static void propagatelist (global_State *g, GCObject *l) {
572dfc11533SChris Williamson   lua_assert(g->gray == NULL);  /* no grays left */
573dfc11533SChris Williamson   g->gray = l;
574dfc11533SChris Williamson   propagateall(g);  /* traverse all elements from 'l' */
575dfc11533SChris Williamson }
576dfc11533SChris Williamson 
577dfc11533SChris Williamson /*
578dfc11533SChris Williamson ** retraverse all gray lists. Because tables may be reinserted in other
579dfc11533SChris Williamson ** lists when traversed, traverse the original lists to avoid traversing
580dfc11533SChris Williamson ** twice the same table (which is not wrong, but inefficient)
581dfc11533SChris Williamson */
retraversegrays(global_State * g)582dfc11533SChris Williamson static void retraversegrays (global_State *g) {
583dfc11533SChris Williamson   GCObject *weak = g->weak;  /* save original lists */
584dfc11533SChris Williamson   GCObject *grayagain = g->grayagain;
585dfc11533SChris Williamson   GCObject *ephemeron = g->ephemeron;
586dfc11533SChris Williamson   g->weak = g->grayagain = g->ephemeron = NULL;
587dfc11533SChris Williamson   propagateall(g);  /* traverse main gray list */
588dfc11533SChris Williamson   propagatelist(g, grayagain);
589dfc11533SChris Williamson   propagatelist(g, weak);
590dfc11533SChris Williamson   propagatelist(g, ephemeron);
591dfc11533SChris Williamson }
592dfc11533SChris Williamson 
593dfc11533SChris Williamson 
convergeephemerons(global_State * g)594dfc11533SChris Williamson static void convergeephemerons (global_State *g) {
595dfc11533SChris Williamson   int changed;
596dfc11533SChris Williamson   do {
597dfc11533SChris Williamson     GCObject *w;
598dfc11533SChris Williamson     GCObject *next = g->ephemeron;  /* get ephemeron list */
599dfc11533SChris Williamson     g->ephemeron = NULL;  /* tables will return to this list when traversed */
600dfc11533SChris Williamson     changed = 0;
601dfc11533SChris Williamson     while ((w = next) != NULL) {
602dfc11533SChris Williamson       next = gco2t(w)->gclist;
603dfc11533SChris Williamson       if (traverseephemeron(g, gco2t(w))) {  /* traverse marked some value? */
604dfc11533SChris Williamson         propagateall(g);  /* propagate changes */
605dfc11533SChris Williamson         changed = 1;  /* will have to revisit all ephemeron tables */
606dfc11533SChris Williamson       }
607dfc11533SChris Williamson     }
608dfc11533SChris Williamson   } while (changed);
609dfc11533SChris Williamson }
610dfc11533SChris Williamson 
611dfc11533SChris Williamson /* }====================================================== */
612dfc11533SChris Williamson 
613dfc11533SChris Williamson 
614dfc11533SChris Williamson /*
615dfc11533SChris Williamson ** {======================================================
616dfc11533SChris Williamson ** Sweep Functions
617dfc11533SChris Williamson ** =======================================================
618dfc11533SChris Williamson */
619dfc11533SChris Williamson 
620dfc11533SChris Williamson 
621dfc11533SChris Williamson /*
622dfc11533SChris Williamson ** clear entries with unmarked keys from all weaktables in list 'l' up
623dfc11533SChris Williamson ** to element 'f'
624dfc11533SChris Williamson */
clearkeys(global_State * g,GCObject * l,GCObject * f)625dfc11533SChris Williamson static void clearkeys (global_State *g, GCObject *l, GCObject *f) {
626dfc11533SChris Williamson   for (; l != f; l = gco2t(l)->gclist) {
627dfc11533SChris Williamson     Table *h = gco2t(l);
628dfc11533SChris Williamson     Node *n, *limit = gnodelast(h);
629dfc11533SChris Williamson     for (n = gnode(h, 0); n < limit; n++) {
630dfc11533SChris Williamson       if (!ttisnil(gval(n)) && (iscleared(g, gkey(n)))) {
631dfc11533SChris Williamson         setnilvalue(gval(n));  /* remove value ... */
632dfc11533SChris Williamson         removeentry(n);  /* and remove entry from table */
633dfc11533SChris Williamson       }
634dfc11533SChris Williamson     }
635dfc11533SChris Williamson   }
636dfc11533SChris Williamson }
637dfc11533SChris Williamson 
638dfc11533SChris Williamson 
639dfc11533SChris Williamson /*
640dfc11533SChris Williamson ** clear entries with unmarked values from all weaktables in list 'l' up
641dfc11533SChris Williamson ** to element 'f'
642dfc11533SChris Williamson */
clearvalues(global_State * g,GCObject * l,GCObject * f)643dfc11533SChris Williamson static void clearvalues (global_State *g, GCObject *l, GCObject *f) {
644dfc11533SChris Williamson   for (; l != f; l = gco2t(l)->gclist) {
645dfc11533SChris Williamson     Table *h = gco2t(l);
646dfc11533SChris Williamson     Node *n, *limit = gnodelast(h);
647dfc11533SChris Williamson     int i;
648dfc11533SChris Williamson     for (i = 0; i < h->sizearray; i++) {
649dfc11533SChris Williamson       TValue *o = &h->array[i];
650dfc11533SChris Williamson       if (iscleared(g, o))  /* value was collected? */
651dfc11533SChris Williamson         setnilvalue(o);  /* remove value */
652dfc11533SChris Williamson     }
653dfc11533SChris Williamson     for (n = gnode(h, 0); n < limit; n++) {
654dfc11533SChris Williamson       if (!ttisnil(gval(n)) && iscleared(g, gval(n))) {
655dfc11533SChris Williamson         setnilvalue(gval(n));  /* remove value ... */
656dfc11533SChris Williamson         removeentry(n);  /* and remove entry from table */
657dfc11533SChris Williamson       }
658dfc11533SChris Williamson     }
659dfc11533SChris Williamson   }
660dfc11533SChris Williamson }
661dfc11533SChris Williamson 
662dfc11533SChris Williamson 
freeobj(lua_State * L,GCObject * o)663dfc11533SChris Williamson static void freeobj (lua_State *L, GCObject *o) {
664dfc11533SChris Williamson   switch (gch(o)->tt) {
665dfc11533SChris Williamson     case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break;
666dfc11533SChris Williamson     case LUA_TLCL: {