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