aboutsummaryrefslogtreecommitdiff
path: root/src/lj_gc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lj_gc.c')
-rw-r--r--src/lj_gc.c180
1 files changed, 115 insertions, 65 deletions
diff --git a/src/lj_gc.c b/src/lj_gc.c
index 899b4e02..b35a0d44 100644
--- a/src/lj_gc.c
+++ b/src/lj_gc.c
@@ -12,6 +12,7 @@
12#include "lj_obj.h" 12#include "lj_obj.h"
13#include "lj_gc.h" 13#include "lj_gc.h"
14#include "lj_err.h" 14#include "lj_err.h"
15#include "lj_buf.h"
15#include "lj_str.h" 16#include "lj_str.h"
16#include "lj_tab.h" 17#include "lj_tab.h"
17#include "lj_func.h" 18#include "lj_func.h"
@@ -24,6 +25,7 @@
24#include "lj_cdata.h" 25#include "lj_cdata.h"
25#endif 26#endif
26#include "lj_trace.h" 27#include "lj_trace.h"
28#include "lj_dispatch.h"
27#include "lj_vm.h" 29#include "lj_vm.h"
28 30
29#define GCSTEPSIZE 1024u 31#define GCSTEPSIZE 1024u
@@ -40,7 +42,8 @@
40 42
41/* Mark a TValue (if needed). */ 43/* Mark a TValue (if needed). */
42#define gc_marktv(g, tv) \ 44#define gc_marktv(g, tv) \
43 { lua_assert(!tvisgcv(tv) || (~itype(tv) == gcval(tv)->gch.gct)); \ 45 { lj_assertG(!tvisgcv(tv) || (~itype(tv) == gcval(tv)->gch.gct), \
46 "TValue and GC type mismatch"); \
44 if (tviswhite(tv)) gc_mark(g, gcV(tv)); } 47 if (tviswhite(tv)) gc_mark(g, gcV(tv)); }
45 48
46/* Mark a GCobj (if needed). */ 49/* Mark a GCobj (if needed). */
@@ -54,21 +57,32 @@
54static void gc_mark(global_State *g, GCobj *o) 57static void gc_mark(global_State *g, GCobj *o)
55{ 58{
56 int gct = o->gch.gct; 59 int gct = o->gch.gct;
57 lua_assert(iswhite(o) && !isdead(g, o)); 60 lj_assertG(iswhite(o), "mark of non-white object");
61 lj_assertG(!isdead(g, o), "mark of dead object");
58 white2gray(o); 62 white2gray(o);
59 if (LJ_UNLIKELY(gct == ~LJ_TUDATA)) { 63 if (LJ_UNLIKELY(gct == ~LJ_TUDATA)) {
60 GCtab *mt = tabref(gco2ud(o)->metatable); 64 GCtab *mt = tabref(gco2ud(o)->metatable);
61 gray2black(o); /* Userdata are never gray. */ 65 gray2black(o); /* Userdata are never gray. */
62 if (mt) gc_markobj(g, mt); 66 if (mt) gc_markobj(g, mt);
63 gc_markobj(g, tabref(gco2ud(o)->env)); 67 gc_markobj(g, tabref(gco2ud(o)->env));
68 if (LJ_HASBUFFER && gco2ud(o)->udtype == UDTYPE_BUFFER) {
69 SBufExt *sbx = (SBufExt *)uddata(gco2ud(o));
70 if (sbufiscow(sbx) && gcref(sbx->cowref))
71 gc_markobj(g, gcref(sbx->cowref));
72 if (gcref(sbx->dict_str))
73 gc_markobj(g, gcref(sbx->dict_str));
74 if (gcref(sbx->dict_mt))
75 gc_markobj(g, gcref(sbx->dict_mt));
76 }
64 } else if (LJ_UNLIKELY(gct == ~LJ_TUPVAL)) { 77 } else if (LJ_UNLIKELY(gct == ~LJ_TUPVAL)) {
65 GCupval *uv = gco2uv(o); 78 GCupval *uv = gco2uv(o);
66 gc_marktv(g, uvval(uv)); 79 gc_marktv(g, uvval(uv));
67 if (uv->closed) 80 if (uv->closed)
68 gray2black(o); /* Closed upvalues are never gray. */ 81 gray2black(o); /* Closed upvalues are never gray. */
69 } else if (gct != ~LJ_TSTR && gct != ~LJ_TCDATA) { 82 } else if (gct != ~LJ_TSTR && gct != ~LJ_TCDATA) {
70 lua_assert(gct == ~LJ_TFUNC || gct == ~LJ_TTAB || 83 lj_assertG(gct == ~LJ_TFUNC || gct == ~LJ_TTAB ||
71 gct == ~LJ_TTHREAD || gct == ~LJ_TPROTO); 84 gct == ~LJ_TTHREAD || gct == ~LJ_TPROTO || gct == ~LJ_TTRACE,
85 "bad GC type %d", gct);
72 setgcrefr(o->gch.gclist, g->gc.gray); 86 setgcrefr(o->gch.gclist, g->gc.gray);
73 setgcref(g->gc.gray, o); 87 setgcref(g->gc.gray, o);
74 } 88 }
@@ -101,7 +115,8 @@ static void gc_mark_uv(global_State *g)
101{ 115{
102 GCupval *uv; 116 GCupval *uv;
103 for (uv = uvnext(&g->uvhead); uv != &g->uvhead; uv = uvnext(uv)) { 117 for (uv = uvnext(&g->uvhead); uv != &g->uvhead; uv = uvnext(uv)) {
104 lua_assert(uvprev(uvnext(uv)) == uv && uvnext(uvprev(uv)) == uv); 118 lj_assertG(uvprev(uvnext(uv)) == uv && uvnext(uvprev(uv)) == uv,
119 "broken upvalue chain");
105 if (isgray(obj2gco(uv))) 120 if (isgray(obj2gco(uv)))
106 gc_marktv(g, uvval(uv)); 121 gc_marktv(g, uvval(uv));
107 } 122 }
@@ -196,7 +211,7 @@ static int gc_traverse_tab(global_State *g, GCtab *t)
196 for (i = 0; i <= hmask; i++) { 211 for (i = 0; i <= hmask; i++) {
197 Node *n = &node[i]; 212 Node *n = &node[i];
198 if (!tvisnil(&n->val)) { /* Mark non-empty slot. */ 213 if (!tvisnil(&n->val)) { /* Mark non-empty slot. */
199 lua_assert(!tvisnil(&n->key)); 214 lj_assertG(!tvisnil(&n->key), "mark of nil key in non-empty slot");
200 if (!(weak & LJ_GC_WEAKKEY)) gc_marktv(g, &n->key); 215 if (!(weak & LJ_GC_WEAKKEY)) gc_marktv(g, &n->key);
201 if (!(weak & LJ_GC_WEAKVAL)) gc_marktv(g, &n->val); 216 if (!(weak & LJ_GC_WEAKVAL)) gc_marktv(g, &n->val);
202 } 217 }
@@ -211,7 +226,8 @@ static void gc_traverse_func(global_State *g, GCfunc *fn)
211 gc_markobj(g, tabref(fn->c.env)); 226 gc_markobj(g, tabref(fn->c.env));
212 if (isluafunc(fn)) { 227 if (isluafunc(fn)) {
213 uint32_t i; 228 uint32_t i;
214 lua_assert(fn->l.nupvalues <= funcproto(fn)->sizeuv); 229 lj_assertG(fn->l.nupvalues <= funcproto(fn)->sizeuv,
230 "function upvalues out of range");
215 gc_markobj(g, funcproto(fn)); 231 gc_markobj(g, funcproto(fn));
216 for (i = 0; i < fn->l.nupvalues; i++) /* Mark Lua function upvalues. */ 232 for (i = 0; i < fn->l.nupvalues; i++) /* Mark Lua function upvalues. */
217 gc_markobj(g, &gcref(fn->l.uvptr[i])->uv); 233 gc_markobj(g, &gcref(fn->l.uvptr[i])->uv);
@@ -227,7 +243,7 @@ static void gc_traverse_func(global_State *g, GCfunc *fn)
227static void gc_marktrace(global_State *g, TraceNo traceno) 243static void gc_marktrace(global_State *g, TraceNo traceno)
228{ 244{
229 GCobj *o = obj2gco(traceref(G2J(g), traceno)); 245 GCobj *o = obj2gco(traceref(G2J(g), traceno));
230 lua_assert(traceno != G2J(g)->cur.traceno); 246 lj_assertG(traceno != G2J(g)->cur.traceno, "active trace escaped");
231 if (iswhite(o)) { 247 if (iswhite(o)) {
232 white2gray(o); 248 white2gray(o);
233 setgcrefr(o->gch.gclist, g->gc.gray); 249 setgcrefr(o->gch.gclist, g->gc.gray);
@@ -244,6 +260,8 @@ static void gc_traverse_trace(global_State *g, GCtrace *T)
244 IRIns *ir = &T->ir[ref]; 260 IRIns *ir = &T->ir[ref];
245 if (ir->o == IR_KGC) 261 if (ir->o == IR_KGC)
246 gc_markobj(g, ir_kgc(ir)); 262 gc_markobj(g, ir_kgc(ir));
263 if (irt_is64(ir->t) && ir->o != IR_KNULL)
264 ref++;
247 } 265 }
248 if (T->link) gc_marktrace(g, T->link); 266 if (T->link) gc_marktrace(g, T->link);
249 if (T->nextroot) gc_marktrace(g, T->nextroot); 267 if (T->nextroot) gc_marktrace(g, T->nextroot);
@@ -274,12 +292,12 @@ static MSize gc_traverse_frames(global_State *g, lua_State *th)
274{ 292{
275 TValue *frame, *top = th->top-1, *bot = tvref(th->stack); 293 TValue *frame, *top = th->top-1, *bot = tvref(th->stack);
276 /* Note: extra vararg frame not skipped, marks function twice (harmless). */ 294 /* Note: extra vararg frame not skipped, marks function twice (harmless). */
277 for (frame = th->base-1; frame > bot; frame = frame_prev(frame)) { 295 for (frame = th->base-1; frame > bot+LJ_FR2; frame = frame_prev(frame)) {
278 GCfunc *fn = frame_func(frame); 296 GCfunc *fn = frame_func(frame);
279 TValue *ftop = frame; 297 TValue *ftop = frame;
280 if (isluafunc(fn)) ftop += funcproto(fn)->framesize; 298 if (isluafunc(fn)) ftop += funcproto(fn)->framesize;
281 if (ftop > top) top = ftop; 299 if (ftop > top) top = ftop;
282 gc_markobj(g, fn); /* Need to mark hidden function (or L). */ 300 if (!LJ_FR2) gc_markobj(g, fn); /* Need to mark hidden function (or L). */
283 } 301 }
284 top++; /* Correct bias of -1 (frame == base-1). */ 302 top++; /* Correct bias of -1 (frame == base-1). */
285 if (top > tvref(th->maxstack)) top = tvref(th->maxstack); 303 if (top > tvref(th->maxstack)) top = tvref(th->maxstack);
@@ -290,7 +308,7 @@ static MSize gc_traverse_frames(global_State *g, lua_State *th)
290static void gc_traverse_thread(global_State *g, lua_State *th) 308static void gc_traverse_thread(global_State *g, lua_State *th)
291{ 309{
292 TValue *o, *top = th->top; 310 TValue *o, *top = th->top;
293 for (o = tvref(th->stack)+1; o < top; o++) 311 for (o = tvref(th->stack)+1+LJ_FR2; o < top; o++)
294 gc_marktv(g, o); 312 gc_marktv(g, o);
295 if (g->gc.state == GCSatomic) { 313 if (g->gc.state == GCSatomic) {
296 top = tvref(th->stack) + th->stacksize; 314 top = tvref(th->stack) + th->stacksize;
@@ -306,7 +324,7 @@ static size_t propagatemark(global_State *g)
306{ 324{
307 GCobj *o = gcref(g->gc.gray); 325 GCobj *o = gcref(g->gc.gray);
308 int gct = o->gch.gct; 326 int gct = o->gch.gct;
309 lua_assert(isgray(o)); 327 lj_assertG(isgray(o), "propagation of non-gray object");
310 gray2black(o); 328 gray2black(o);
311 setgcrefr(g->gc.gray, o->gch.gclist); /* Remove from gray list. */ 329 setgcrefr(g->gc.gray, o->gch.gclist); /* Remove from gray list. */
312 if (LJ_LIKELY(gct == ~LJ_TTAB)) { 330 if (LJ_LIKELY(gct == ~LJ_TTAB)) {
@@ -338,7 +356,7 @@ static size_t propagatemark(global_State *g)
338 return ((sizeof(GCtrace)+7)&~7) + (T->nins-T->nk)*sizeof(IRIns) + 356 return ((sizeof(GCtrace)+7)&~7) + (T->nins-T->nk)*sizeof(IRIns) +
339 T->nsnap*sizeof(SnapShot) + T->nsnapmap*sizeof(SnapEntry); 357 T->nsnap*sizeof(SnapShot) + T->nsnapmap*sizeof(SnapEntry);
340#else 358#else
341 lua_assert(0); 359 lj_assertG(0, "bad GC type %d", gct);
342 return 0; 360 return 0;
343#endif 361#endif
344 } 362 }
@@ -355,15 +373,6 @@ static size_t gc_propagate_gray(global_State *g)
355 373
356/* -- Sweep phase --------------------------------------------------------- */ 374/* -- Sweep phase --------------------------------------------------------- */
357 375
358/* Try to shrink some common data structures. */
359static void gc_shrink(global_State *g, lua_State *L)
360{
361 if (g->strnum <= (g->strmask >> 2) && g->strmask > LJ_MIN_STRTAB*2-1)
362 lj_str_resize(L, g->strmask >> 1); /* Shrink string table. */
363 if (g->tmpbuf.sz > LJ_MIN_SBUF*2)
364 lj_str_resizebuf(L, &g->tmpbuf, g->tmpbuf.sz >> 1); /* Shrink temp buf. */
365}
366
367/* Type of GC free functions. */ 376/* Type of GC free functions. */
368typedef void (LJ_FASTCALL *GCFreeFunc)(global_State *g, GCobj *o); 377typedef void (LJ_FASTCALL *GCFreeFunc)(global_State *g, GCobj *o);
369 378
@@ -389,7 +398,7 @@ static const GCFreeFunc gc_freefunc[] = {
389}; 398};
390 399
391/* Full sweep of a GC list. */ 400/* Full sweep of a GC list. */
392#define gc_fullsweep(g, p) gc_sweep(g, (p), LJ_MAX_MEM) 401#define gc_fullsweep(g, p) gc_sweep(g, (p), ~(uint32_t)0)
393 402
394/* Partial sweep of a GC list. */ 403/* Partial sweep of a GC list. */
395static GCRef *gc_sweep(global_State *g, GCRef *p, uint32_t lim) 404static GCRef *gc_sweep(global_State *g, GCRef *p, uint32_t lim)
@@ -401,11 +410,13 @@ static GCRef *gc_sweep(global_State *g, GCRef *p, uint32_t lim)
401 if (o->gch.gct == ~LJ_TTHREAD) /* Need to sweep open upvalues, too. */ 410 if (o->gch.gct == ~LJ_TTHREAD) /* Need to sweep open upvalues, too. */
402 gc_fullsweep(g, &gco2th(o)->openupval); 411 gc_fullsweep(g, &gco2th(o)->openupval);
403 if (((o->gch.marked ^ LJ_GC_WHITES) & ow)) { /* Black or current white? */ 412 if (((o->gch.marked ^ LJ_GC_WHITES) & ow)) { /* Black or current white? */
404 lua_assert(!isdead(g, o) || (o->gch.marked & LJ_GC_FIXED)); 413 lj_assertG(!isdead(g, o) || (o->gch.marked & LJ_GC_FIXED),
414 "sweep of undead object");
405 makewhite(g, o); /* Value is alive, change to the current white. */ 415 makewhite(g, o); /* Value is alive, change to the current white. */
406 p = &o->gch.nextgc; 416 p = &o->gch.nextgc;
407 } else { /* Otherwise value is dead, free it. */ 417 } else { /* Otherwise value is dead, free it. */
408 lua_assert(isdead(g, o) || ow == LJ_GC_SFIXED); 418 lj_assertG(isdead(g, o) || ow == LJ_GC_SFIXED,
419 "sweep of unlive object");
409 setgcrefr(*p, o->gch.nextgc); 420 setgcrefr(*p, o->gch.nextgc);
410 if (o == gcref(g->gc.root)) 421 if (o == gcref(g->gc.root))
411 setgcrefr(g->gc.root, o->gch.nextgc); /* Adjust list anchor. */ 422 setgcrefr(g->gc.root, o->gch.nextgc); /* Adjust list anchor. */
@@ -415,6 +426,32 @@ static GCRef *gc_sweep(global_State *g, GCRef *p, uint32_t lim)
415 return p; 426 return p;
416} 427}
417 428
429/* Sweep one string interning table chain. Preserves hashalg bit. */
430static void gc_sweepstr(global_State *g, GCRef *chain)
431{
432 /* Mask with other white and LJ_GC_FIXED. Or LJ_GC_SFIXED on shutdown. */
433 int ow = otherwhite(g);
434 uintptr_t u = gcrefu(*chain);
435 GCRef q;
436 GCRef *p = &q;
437 GCobj *o;
438 setgcrefp(q, (u & ~(uintptr_t)1));
439 while ((o = gcref(*p)) != NULL) {
440 if (((o->gch.marked ^ LJ_GC_WHITES) & ow)) { /* Black or current white? */
441 lj_assertG(!isdead(g, o) || (o->gch.marked & LJ_GC_FIXED),
442 "sweep of undead string");
443 makewhite(g, o); /* String is alive, change to the current white. */
444 p = &o->gch.nextgc;
445 } else { /* Otherwise string is dead, free it. */
446 lj_assertG(isdead(g, o) || ow == LJ_GC_SFIXED,
447 "sweep of unlive string");
448 setgcrefr(*p, o->gch.nextgc);
449 lj_str_free(g, gco2str(o));
450 }
451 }
452 setgcrefp(*chain, (gcrefu(q) | (u & 1)));
453}
454
418/* Check whether we can clear a key or a value slot from a table. */ 455/* Check whether we can clear a key or a value slot from a table. */
419static int gc_mayclear(cTValue *o, int val) 456static int gc_mayclear(cTValue *o, int val)
420{ 457{
@@ -432,11 +469,12 @@ static int gc_mayclear(cTValue *o, int val)
432} 469}
433 470
434/* Clear collected entries from weak tables. */ 471/* Clear collected entries from weak tables. */
435static void gc_clearweak(GCobj *o) 472static void gc_clearweak(global_State *g, GCobj *o)
436{ 473{
474 UNUSED(g);
437 while (o) { 475 while (o) {
438 GCtab *t = gco2tab(o); 476 GCtab *t = gco2tab(o);
439 lua_assert((t->marked & LJ_GC_WEAK)); 477 lj_assertG((t->marked & LJ_GC_WEAK), "clear of non-weak table");
440 if ((t->marked & LJ_GC_WEAKVAL)) { 478 if ((t->marked & LJ_GC_WEAKVAL)) {
441 MSize i, asize = t->asize; 479 MSize i, asize = t->asize;
442 for (i = 0; i < asize; i++) { 480 for (i = 0; i < asize; i++) {
@@ -467,18 +505,21 @@ static void gc_call_finalizer(global_State *g, lua_State *L,
467{ 505{
468 /* Save and restore lots of state around the __gc callback. */ 506 /* Save and restore lots of state around the __gc callback. */
469 uint8_t oldh = hook_save(g); 507 uint8_t oldh = hook_save(g);
470 MSize oldt = g->gc.threshold; 508 GCSize oldt = g->gc.threshold;
471 int errcode; 509 int errcode;
472 TValue *top; 510 TValue *top;
473 lj_trace_abort(g); 511 lj_trace_abort(g);
474 top = L->top;
475 L->top = top+2;
476 hook_entergc(g); /* Disable hooks and new traces during __gc. */ 512 hook_entergc(g); /* Disable hooks and new traces during __gc. */
513 if (LJ_HASPROFILE && (oldh & HOOK_PROFILE)) lj_dispatch_update(g);
477 g->gc.threshold = LJ_MAX_MEM; /* Prevent GC steps. */ 514 g->gc.threshold = LJ_MAX_MEM; /* Prevent GC steps. */
478 copyTV(L, top, mo); 515 top = L->top;
479 setgcV(L, top+1, o, ~o->gch.gct); 516 copyTV(L, top++, mo);
480 errcode = lj_vm_pcall(L, top+1, 1+0, -1); /* Stack: |mo|o| -> | */ 517 if (LJ_FR2) setnilV(top++);
518 setgcV(L, top, o, ~o->gch.gct);
519 L->top = top+1;
520 errcode = lj_vm_pcall(L, top, 1+0, -1); /* Stack: |mo|o| -> | */
481 hook_restore(g, oldh); 521 hook_restore(g, oldh);
522 if (LJ_HASPROFILE && (oldh & HOOK_PROFILE)) lj_dispatch_update(g);
482 g->gc.threshold = oldt; /* Restore GC threshold. */ 523 g->gc.threshold = oldt; /* Restore GC threshold. */
483 if (errcode) 524 if (errcode)
484 lj_err_throw(L, errcode); /* Propagate errors. */ 525 lj_err_throw(L, errcode); /* Propagate errors. */
@@ -490,7 +531,7 @@ static void gc_finalize(lua_State *L)
490 global_State *g = G(L); 531 global_State *g = G(L);
491 GCobj *o = gcnext(gcref(g->gc.mmudata)); 532 GCobj *o = gcnext(gcref(g->gc.mmudata));
492 cTValue *mo; 533 cTValue *mo;
493 lua_assert(gcref(g->jit_L) == NULL); /* Must not be called on trace. */ 534 lj_assertG(tvref(g->jit_base) == NULL, "finalizer called on trace");
494 /* Unchain from list of userdata to be finalized. */ 535 /* Unchain from list of userdata to be finalized. */
495 if (o == gcref(g->gc.mmudata)) 536 if (o == gcref(g->gc.mmudata))
496 setgcrefnull(g->gc.mmudata); 537 setgcrefnull(g->gc.mmudata);
@@ -565,9 +606,9 @@ void lj_gc_freeall(global_State *g)
565 /* Free everything, except super-fixed objects (the main thread). */ 606 /* Free everything, except super-fixed objects (the main thread). */
566 g->gc.currentwhite = LJ_GC_WHITES | LJ_GC_SFIXED; 607 g->gc.currentwhite = LJ_GC_WHITES | LJ_GC_SFIXED;
567 gc_fullsweep(g, &g->gc.root); 608 gc_fullsweep(g, &g->gc.root);
568 strmask = g->strmask; 609 strmask = g->str.mask;
569 for (i = 0; i <= strmask; i++) /* Free all string hash chains. */ 610 for (i = 0; i <= strmask; i++) /* Free all string hash chains. */
570 gc_fullsweep(g, &g->strhash[i]); 611 gc_sweepstr(g, &g->str.tab[i]);
571} 612}
572 613
573/* -- Collector ----------------------------------------------------------- */ 614/* -- Collector ----------------------------------------------------------- */
@@ -582,7 +623,7 @@ static void atomic(global_State *g, lua_State *L)
582 623
583 setgcrefr(g->gc.gray, g->gc.weak); /* Empty the list of weak tables. */ 624 setgcrefr(g->gc.gray, g->gc.weak); /* Empty the list of weak tables. */
584 setgcrefnull(g->gc.weak); 625 setgcrefnull(g->gc.weak);
585 lua_assert(!iswhite(obj2gco(mainthread(g)))); 626 lj_assertG(!iswhite(obj2gco(mainthread(g))), "main thread turned white");
586 gc_markobj(g, L); /* Mark running thread. */ 627 gc_markobj(g, L); /* Mark running thread. */
587 gc_traverse_curtrace(g); /* Traverse current trace. */ 628 gc_traverse_curtrace(g); /* Traverse current trace. */
588 gc_mark_gcroot(g); /* Mark GC roots (again). */ 629 gc_mark_gcroot(g); /* Mark GC roots (again). */
@@ -597,13 +638,15 @@ static void atomic(global_State *g, lua_State *L)
597 udsize += gc_propagate_gray(g); /* And propagate the marks. */ 638 udsize += gc_propagate_gray(g); /* And propagate the marks. */
598 639
599 /* All marking done, clear weak tables. */ 640 /* All marking done, clear weak tables. */
600 gc_clearweak(gcref(g->gc.weak)); 641 gc_clearweak(g, gcref(g->gc.weak));
642
643 lj_buf_shrink(L, &g->tmpbuf); /* Shrink temp buffer. */
601 644
602 /* Prepare for sweep phase. */ 645 /* Prepare for sweep phase. */
603 g->gc.currentwhite = (uint8_t)otherwhite(g); /* Flip current white. */ 646 g->gc.currentwhite = (uint8_t)otherwhite(g); /* Flip current white. */
604 g->strempty.marked = g->gc.currentwhite; 647 g->strempty.marked = g->gc.currentwhite;
605 setmref(g->gc.sweep, &g->gc.root); 648 setmref(g->gc.sweep, &g->gc.root);
606 g->gc.estimate = g->gc.total - (MSize)udsize; /* Initial estimate. */ 649 g->gc.estimate = g->gc.total - (GCSize)udsize; /* Initial estimate. */
607} 650}
608 651
609/* GC state machine. Returns a cost estimate for each step performed. */ 652/* GC state machine. Returns a cost estimate for each step performed. */
@@ -620,28 +663,29 @@ static size_t gc_onestep(lua_State *L)
620 g->gc.state = GCSatomic; /* End of mark phase. */ 663 g->gc.state = GCSatomic; /* End of mark phase. */
621 return 0; 664 return 0;
622 case GCSatomic: 665 case GCSatomic:
623 if (gcref(g->jit_L)) /* Don't run atomic phase on trace. */ 666 if (tvref(g->jit_base)) /* Don't run atomic phase on trace. */
624 return LJ_MAX_MEM; 667 return LJ_MAX_MEM;
625 atomic(g, L); 668 atomic(g, L);
626 g->gc.state = GCSsweepstring; /* Start of sweep phase. */ 669 g->gc.state = GCSsweepstring; /* Start of sweep phase. */
627 g->gc.sweepstr = 0; 670 g->gc.sweepstr = 0;
628 return 0; 671 return 0;
629 case GCSsweepstring: { 672 case GCSsweepstring: {
630 MSize old = g->gc.total; 673 GCSize old = g->gc.total;
631 gc_fullsweep(g, &g->strhash[g->gc.sweepstr++]); /* Sweep one chain. */ 674 gc_sweepstr(g, &g->str.tab[g->gc.sweepstr++]); /* Sweep one chain. */
632 if (g->gc.sweepstr > g->strmask) 675 if (g->gc.sweepstr > g->str.mask)
633 g->gc.state = GCSsweep; /* All string hash chains sweeped. */ 676 g->gc.state = GCSsweep; /* All string hash chains sweeped. */
634 lua_assert(old >= g->gc.total); 677 lj_assertG(old >= g->gc.total, "sweep increased memory");
635 g->gc.estimate -= old - g->gc.total; 678 g->gc.estimate -= old - g->gc.total;
636 return GCSWEEPCOST; 679 return GCSWEEPCOST;
637 } 680 }
638 case GCSsweep: { 681 case GCSsweep: {
639 MSize old = g->gc.total; 682 GCSize old = g->gc.total;
640 setmref(g->gc.sweep, gc_sweep(g, mref(g->gc.sweep, GCRef), GCSWEEPMAX)); 683 setmref(g->gc.sweep, gc_sweep(g, mref(g->gc.sweep, GCRef), GCSWEEPMAX));
641 lua_assert(old >= g->gc.total); 684 lj_assertG(old >= g->gc.total, "sweep increased memory");
642 g->gc.estimate -= old - g->gc.total; 685 g->gc.estimate -= old - g->gc.total;
643 if (gcref(*mref(g->gc.sweep, GCRef)) == NULL) { 686 if (gcref(*mref(g->gc.sweep, GCRef)) == NULL) {
644 gc_shrink(g, L); 687 if (g->str.num <= (g->str.mask >> 2) && g->str.mask > LJ_MIN_STRTAB*2-1)
688 lj_str_resize(L, g->str.mask >> 1); /* Shrink string table. */
645 if (gcref(g->gc.mmudata)) { /* Need any finalizations? */ 689 if (gcref(g->gc.mmudata)) { /* Need any finalizations? */
646 g->gc.state = GCSfinalize; 690 g->gc.state = GCSfinalize;
647#if LJ_HASFFI 691#if LJ_HASFFI
@@ -656,7 +700,7 @@ static size_t gc_onestep(lua_State *L)
656 } 700 }
657 case GCSfinalize: 701 case GCSfinalize:
658 if (gcref(g->gc.mmudata) != NULL) { 702 if (gcref(g->gc.mmudata) != NULL) {
659 if (gcref(g->jit_L)) /* Don't call finalizers on trace. */ 703 if (tvref(g->jit_base)) /* Don't call finalizers on trace. */
660 return LJ_MAX_MEM; 704 return LJ_MAX_MEM;
661 gc_finalize(L); /* Finalize one userdata object. */ 705 gc_finalize(L); /* Finalize one userdata object. */
662 if (g->gc.estimate > GCFINALIZECOST) 706 if (g->gc.estimate > GCFINALIZECOST)
@@ -670,7 +714,7 @@ static size_t gc_onestep(lua_State *L)
670 g->gc.debt = 0; 714 g->gc.debt = 0;
671 return 0; 715 return 0;
672 default: 716 default:
673 lua_assert(0); 717 lj_assertG(0, "bad GC state");
674 return 0; 718 return 0;
675 } 719 }
676} 720}
@@ -679,7 +723,7 @@ static size_t gc_onestep(lua_State *L)
679int LJ_FASTCALL lj_gc_step(lua_State *L) 723int LJ_FASTCALL lj_gc_step(lua_State *L)
680{ 724{
681 global_State *g = G(L); 725 global_State *g = G(L);
682 MSize lim; 726 GCSize lim;
683 int32_t ostate = g->vmstate; 727 int32_t ostate = g->vmstate;
684 setvmstate(g, GC); 728 setvmstate(g, GC);
685 lim = (GCSTEPSIZE/100) * g->gc.stepmul; 729 lim = (GCSTEPSIZE/100) * g->gc.stepmul;
@@ -688,13 +732,13 @@ int LJ_FASTCALL lj_gc_step(lua_State *L)
688 if (g->gc.total > g->gc.threshold) 732 if (g->gc.total > g->gc.threshold)
689 g->gc.debt += g->gc.total - g->gc.threshold; 733 g->gc.debt += g->gc.total - g->gc.threshold;
690 do { 734 do {
691 lim -= (MSize)gc_onestep(L); 735 lim -= (GCSize)gc_onestep(L);
692 if (g->gc.state == GCSpause) { 736 if (g->gc.state == GCSpause) {
693 g->gc.threshold = (g->gc.estimate/100) * g->gc.pause; 737 g->gc.threshold = (g->gc.estimate/100) * g->gc.pause;
694 g->vmstate = ostate; 738 g->vmstate = ostate;
695 return 1; /* Finished a GC cycle. */ 739 return 1; /* Finished a GC cycle. */
696 } 740 }
697 } while ((int32_t)lim > 0); 741 } while (sizeof(lim) == 8 ? ((int64_t)lim > 0) : ((int32_t)lim > 0));
698 if (g->gc.debt < GCSTEPSIZE) { 742 if (g->gc.debt < GCSTEPSIZE) {
699 g->gc.threshold = g->gc.total + GCSTEPSIZE; 743 g->gc.threshold = g->gc.total + GCSTEPSIZE;
700 g->vmstate = ostate; 744 g->vmstate = ostate;
@@ -718,8 +762,8 @@ void LJ_FASTCALL lj_gc_step_fixtop(lua_State *L)
718/* Perform multiple GC steps. Called from JIT-compiled code. */ 762/* Perform multiple GC steps. Called from JIT-compiled code. */
719int LJ_FASTCALL lj_gc_step_jit(global_State *g, MSize steps) 763int LJ_FASTCALL lj_gc_step_jit(global_State *g, MSize steps)
720{ 764{
721 lua_State *L = gco2th(gcref(g->jit_L)); 765 lua_State *L = gco2th(gcref(g->cur_L));
722 L->base = mref(G(L)->jit_base, TValue); 766 L->base = tvref(G(L)->jit_base);
723 L->top = curr_topL(L); 767 L->top = curr_topL(L);
724 while (steps-- > 0 && lj_gc_step(L) == 0) 768 while (steps-- > 0 && lj_gc_step(L) == 0)
725 ; 769 ;
@@ -744,7 +788,8 @@ void lj_gc_fullgc(lua_State *L)
744 } 788 }
745 while (g->gc.state == GCSsweepstring || g->gc.state == GCSsweep) 789 while (g->gc.state == GCSsweepstring || g->gc.state == GCSsweep)
746 gc_onestep(L); /* Finish sweep. */ 790 gc_onestep(L); /* Finish sweep. */
747 lua_assert(g->gc.state == GCSfinalize || g->gc.state == GCSpause); 791 lj_assertG(g->gc.state == GCSfinalize || g->gc.state == GCSpause,
792 "bad GC state");
748 /* Now perform a full GC. */ 793 /* Now perform a full GC. */
749 g->gc.state = GCSpause; 794 g->gc.state = GCSpause;
750 do { gc_onestep(L); } while (g->gc.state != GCSpause); 795 do { gc_onestep(L); } while (g->gc.state != GCSpause);
@@ -757,9 +802,11 @@ void lj_gc_fullgc(lua_State *L)
757/* Move the GC propagation frontier forward. */ 802/* Move the GC propagation frontier forward. */
758void lj_gc_barrierf(global_State *g, GCobj *o, GCobj *v) 803void lj_gc_barrierf(global_State *g, GCobj *o, GCobj *v)
759{ 804{
760 lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); 805 lj_assertG(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o),
761 lua_assert(g->gc.state != GCSfinalize && g->gc.state != GCSpause); 806 "bad object states for forward barrier");
762 lua_assert(o->gch.gct != ~LJ_TTAB); 807 lj_assertG(g->gc.state != GCSfinalize && g->gc.state != GCSpause,
808 "bad GC state");
809 lj_assertG(o->gch.gct != ~LJ_TTAB, "barrier object is not a table");
763 /* Preserve invariant during propagation. Otherwise it doesn't matter. */ 810 /* Preserve invariant during propagation. Otherwise it doesn't matter. */
764 if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic) 811 if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic)
765 gc_mark(g, v); /* Move frontier forward. */ 812 gc_mark(g, v); /* Move frontier forward. */
@@ -796,7 +843,8 @@ void lj_gc_closeuv(global_State *g, GCupval *uv)
796 lj_gc_barrierf(g, o, gcV(&uv->tv)); 843 lj_gc_barrierf(g, o, gcV(&uv->tv));
797 } else { 844 } else {
798 makewhite(g, o); /* Make it white, i.e. sweep the upvalue. */ 845 makewhite(g, o); /* Make it white, i.e. sweep the upvalue. */
799 lua_assert(g->gc.state != GCSfinalize && g->gc.state != GCSpause); 846 lj_assertG(g->gc.state != GCSfinalize && g->gc.state != GCSpause,
847 "bad GC state");
800 } 848 }
801 } 849 }
802} 850}
@@ -813,27 +861,29 @@ void lj_gc_barriertrace(global_State *g, uint32_t traceno)
813/* -- Allocator ----------------------------------------------------------- */ 861/* -- Allocator ----------------------------------------------------------- */
814 862
815/* Call pluggable memory allocator to allocate or resize a fragment. */ 863/* Call pluggable memory allocator to allocate or resize a fragment. */
816void *lj_mem_realloc(lua_State *L, void *p, MSize osz, MSize nsz) 864void *lj_mem_realloc(lua_State *L, void *p, GCSize osz, GCSize nsz)
817{ 865{
818 global_State *g = G(L); 866 global_State *g = G(L);
819 lua_assert((osz == 0) == (p == NULL)); 867 lj_assertG((osz == 0) == (p == NULL), "realloc API violation");
820 p = g->allocf(g->allocd, p, osz, nsz); 868 p = g->allocf(g->allocd, p, osz, nsz);
821 if (p == NULL && nsz > 0) 869 if (p == NULL && nsz > 0)
822 lj_err_mem(L); 870 lj_err_mem(L);
823 lua_assert((nsz == 0) == (p == NULL)); 871 lj_assertG((nsz == 0) == (p == NULL), "allocf API violation");
824 lua_assert(checkptr32(p)); 872 lj_assertG(checkptrGC(p),
873 "allocated memory address %p outside required range", p);
825 g->gc.total = (g->gc.total - osz) + nsz; 874 g->gc.total = (g->gc.total - osz) + nsz;
826 return p; 875 return p;
827} 876}
828 877
829/* Allocate new GC object and link it to the root set. */ 878/* Allocate new GC object and link it to the root set. */
830void * LJ_FASTCALL lj_mem_newgco(lua_State *L, MSize size) 879void * LJ_FASTCALL lj_mem_newgco(lua_State *L, GCSize size)
831{ 880{
832 global_State *g = G(L); 881 global_State *g = G(L);
833 GCobj *o = (GCobj *)g->allocf(g->allocd, NULL, 0, size); 882 GCobj *o = (GCobj *)g->allocf(g->allocd, NULL, 0, size);
834 if (o == NULL) 883 if (o == NULL)
835 lj_err_mem(L); 884 lj_err_mem(L);
836 lua_assert(checkptr32(o)); 885 lj_assertG(checkptrGC(o),
886 "allocated memory address %p outside required range", o);
837 g->gc.total += size; 887 g->gc.total += size;
838 setgcrefr(o->gch.nextgc, g->gc.root); 888 setgcrefr(o->gch.nextgc, g->gc.root);
839 setgcref(g->gc.root, o); 889 setgcref(g->gc.root, o);