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.c171
1 files changed, 106 insertions, 65 deletions
diff --git a/src/lj_gc.c b/src/lj_gc.c
index 86fcd6eb..cc4232a6 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,7 +57,8 @@
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);
@@ -67,8 +71,9 @@ static void gc_mark(global_State *g, GCobj *o)
67 if (uv->closed) 71 if (uv->closed)
68 gray2black(o); /* Closed upvalues are never gray. */ 72 gray2black(o); /* Closed upvalues are never gray. */
69 } else if (gct != ~LJ_TSTR && gct != ~LJ_TCDATA) { 73 } else if (gct != ~LJ_TSTR && gct != ~LJ_TCDATA) {
70 lua_assert(gct == ~LJ_TFUNC || gct == ~LJ_TTAB || 74 lj_assertG(gct == ~LJ_TFUNC || gct == ~LJ_TTAB ||
71 gct == ~LJ_TTHREAD || gct == ~LJ_TPROTO); 75 gct == ~LJ_TTHREAD || gct == ~LJ_TPROTO || gct == ~LJ_TTRACE,
76 "bad GC type %d", gct);
72 setgcrefr(o->gch.gclist, g->gc.gray); 77 setgcrefr(o->gch.gclist, g->gc.gray);
73 setgcref(g->gc.gray, o); 78 setgcref(g->gc.gray, o);
74 } 79 }
@@ -101,7 +106,8 @@ static void gc_mark_uv(global_State *g)
101{ 106{
102 GCupval *uv; 107 GCupval *uv;
103 for (uv = uvnext(&g->uvhead); uv != &g->uvhead; uv = uvnext(uv)) { 108 for (uv = uvnext(&g->uvhead); uv != &g->uvhead; uv = uvnext(uv)) {
104 lua_assert(uvprev(uvnext(uv)) == uv && uvnext(uvprev(uv)) == uv); 109 lj_assertG(uvprev(uvnext(uv)) == uv && uvnext(uvprev(uv)) == uv,
110 "broken upvalue chain");
105 if (isgray(obj2gco(uv))) 111 if (isgray(obj2gco(uv)))
106 gc_marktv(g, uvval(uv)); 112 gc_marktv(g, uvval(uv));
107 } 113 }
@@ -196,7 +202,7 @@ static int gc_traverse_tab(global_State *g, GCtab *t)
196 for (i = 0; i <= hmask; i++) { 202 for (i = 0; i <= hmask; i++) {
197 Node *n = &node[i]; 203 Node *n = &node[i];
198 if (!tvisnil(&n->val)) { /* Mark non-empty slot. */ 204 if (!tvisnil(&n->val)) { /* Mark non-empty slot. */
199 lua_assert(!tvisnil(&n->key)); 205 lj_assertG(!tvisnil(&n->key), "mark of nil key in non-empty slot");
200 if (!(weak & LJ_GC_WEAKKEY)) gc_marktv(g, &n->key); 206 if (!(weak & LJ_GC_WEAKKEY)) gc_marktv(g, &n->key);
201 if (!(weak & LJ_GC_WEAKVAL)) gc_marktv(g, &n->val); 207 if (!(weak & LJ_GC_WEAKVAL)) gc_marktv(g, &n->val);
202 } 208 }
@@ -211,7 +217,8 @@ static void gc_traverse_func(global_State *g, GCfunc *fn)
211 gc_markobj(g, tabref(fn->c.env)); 217 gc_markobj(g, tabref(fn->c.env));
212 if (isluafunc(fn)) { 218 if (isluafunc(fn)) {
213 uint32_t i; 219 uint32_t i;
214 lua_assert(fn->l.nupvalues <= funcproto(fn)->sizeuv); 220 lj_assertG(fn->l.nupvalues <= funcproto(fn)->sizeuv,
221 "function upvalues out of range");
215 gc_markobj(g, funcproto(fn)); 222 gc_markobj(g, funcproto(fn));
216 for (i = 0; i < fn->l.nupvalues; i++) /* Mark Lua function upvalues. */ 223 for (i = 0; i < fn->l.nupvalues; i++) /* Mark Lua function upvalues. */
217 gc_markobj(g, &gcref(fn->l.uvptr[i])->uv); 224 gc_markobj(g, &gcref(fn->l.uvptr[i])->uv);
@@ -227,7 +234,7 @@ static void gc_traverse_func(global_State *g, GCfunc *fn)
227static void gc_marktrace(global_State *g, TraceNo traceno) 234static void gc_marktrace(global_State *g, TraceNo traceno)
228{ 235{
229 GCobj *o = obj2gco(traceref(G2J(g), traceno)); 236 GCobj *o = obj2gco(traceref(G2J(g), traceno));
230 lua_assert(traceno != G2J(g)->cur.traceno); 237 lj_assertG(traceno != G2J(g)->cur.traceno, "active trace escaped");
231 if (iswhite(o)) { 238 if (iswhite(o)) {
232 white2gray(o); 239 white2gray(o);
233 setgcrefr(o->gch.gclist, g->gc.gray); 240 setgcrefr(o->gch.gclist, g->gc.gray);
@@ -244,6 +251,8 @@ static void gc_traverse_trace(global_State *g, GCtrace *T)
244 IRIns *ir = &T->ir[ref]; 251 IRIns *ir = &T->ir[ref];
245 if (ir->o == IR_KGC) 252 if (ir->o == IR_KGC)
246 gc_markobj(g, ir_kgc(ir)); 253 gc_markobj(g, ir_kgc(ir));
254 if (irt_is64(ir->t) && ir->o != IR_KNULL)
255 ref++;
247 } 256 }
248 if (T->link) gc_marktrace(g, T->link); 257 if (T->link) gc_marktrace(g, T->link);
249 if (T->nextroot) gc_marktrace(g, T->nextroot); 258 if (T->nextroot) gc_marktrace(g, T->nextroot);
@@ -274,12 +283,12 @@ static MSize gc_traverse_frames(global_State *g, lua_State *th)
274{ 283{
275 TValue *frame, *top = th->top-1, *bot = tvref(th->stack); 284 TValue *frame, *top = th->top-1, *bot = tvref(th->stack);
276 /* Note: extra vararg frame not skipped, marks function twice (harmless). */ 285 /* Note: extra vararg frame not skipped, marks function twice (harmless). */
277 for (frame = th->base-1; frame > bot; frame = frame_prev(frame)) { 286 for (frame = th->base-1; frame > bot+LJ_FR2; frame = frame_prev(frame)) {
278 GCfunc *fn = frame_func(frame); 287 GCfunc *fn = frame_func(frame);
279 TValue *ftop = frame; 288 TValue *ftop = frame;
280 if (isluafunc(fn)) ftop += funcproto(fn)->framesize; 289 if (isluafunc(fn)) ftop += funcproto(fn)->framesize;
281 if (ftop > top) top = ftop; 290 if (ftop > top) top = ftop;
282 gc_markobj(g, fn); /* Need to mark hidden function (or L). */ 291 if (!LJ_FR2) gc_markobj(g, fn); /* Need to mark hidden function (or L). */
283 } 292 }
284 top++; /* Correct bias of -1 (frame == base-1). */ 293 top++; /* Correct bias of -1 (frame == base-1). */
285 if (top > tvref(th->maxstack)) top = tvref(th->maxstack); 294 if (top > tvref(th->maxstack)) top = tvref(th->maxstack);
@@ -290,7 +299,7 @@ static MSize gc_traverse_frames(global_State *g, lua_State *th)
290static void gc_traverse_thread(global_State *g, lua_State *th) 299static void gc_traverse_thread(global_State *g, lua_State *th)
291{ 300{
292 TValue *o, *top = th->top; 301 TValue *o, *top = th->top;
293 for (o = tvref(th->stack)+1; o < top; o++) 302 for (o = tvref(th->stack)+1+LJ_FR2; o < top; o++)
294 gc_marktv(g, o); 303 gc_marktv(g, o);
295 if (g->gc.state == GCSatomic) { 304 if (g->gc.state == GCSatomic) {
296 top = tvref(th->stack) + th->stacksize; 305 top = tvref(th->stack) + th->stacksize;
@@ -306,7 +315,7 @@ static size_t propagatemark(global_State *g)
306{ 315{
307 GCobj *o = gcref(g->gc.gray); 316 GCobj *o = gcref(g->gc.gray);
308 int gct = o->gch.gct; 317 int gct = o->gch.gct;
309 lua_assert(isgray(o)); 318 lj_assertG(isgray(o), "propagation of non-gray object");
310 gray2black(o); 319 gray2black(o);
311 setgcrefr(g->gc.gray, o->gch.gclist); /* Remove from gray list. */ 320 setgcrefr(g->gc.gray, o->gch.gclist); /* Remove from gray list. */
312 if (LJ_LIKELY(gct == ~LJ_TTAB)) { 321 if (LJ_LIKELY(gct == ~LJ_TTAB)) {
@@ -338,7 +347,7 @@ static size_t propagatemark(global_State *g)
338 return ((sizeof(GCtrace)+7)&~7) + (T->nins-T->nk)*sizeof(IRIns) + 347 return ((sizeof(GCtrace)+7)&~7) + (T->nins-T->nk)*sizeof(IRIns) +
339 T->nsnap*sizeof(SnapShot) + T->nsnapmap*sizeof(SnapEntry); 348 T->nsnap*sizeof(SnapShot) + T->nsnapmap*sizeof(SnapEntry);
340#else 349#else
341 lua_assert(0); 350 lj_assertG(0, "bad GC type %d", gct);
342 return 0; 351 return 0;
343#endif 352#endif
344 } 353 }
@@ -355,15 +364,6 @@ static size_t gc_propagate_gray(global_State *g)
355 364
356/* -- Sweep phase --------------------------------------------------------- */ 365/* -- Sweep phase --------------------------------------------------------- */
357 366
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. */ 367/* Type of GC free functions. */
368typedef void (LJ_FASTCALL *GCFreeFunc)(global_State *g, GCobj *o); 368typedef void (LJ_FASTCALL *GCFreeFunc)(global_State *g, GCobj *o);
369 369
@@ -389,7 +389,7 @@ static const GCFreeFunc gc_freefunc[] = {
389}; 389};
390 390
391/* Full sweep of a GC list. */ 391/* Full sweep of a GC list. */
392#define gc_fullsweep(g, p) gc_sweep(g, (p), LJ_MAX_MEM) 392#define gc_fullsweep(g, p) gc_sweep(g, (p), ~(uint32_t)0)
393 393
394/* Partial sweep of a GC list. */ 394/* Partial sweep of a GC list. */
395static GCRef *gc_sweep(global_State *g, GCRef *p, uint32_t lim) 395static GCRef *gc_sweep(global_State *g, GCRef *p, uint32_t lim)
@@ -401,11 +401,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. */ 401 if (o->gch.gct == ~LJ_TTHREAD) /* Need to sweep open upvalues, too. */
402 gc_fullsweep(g, &gco2th(o)->openupval); 402 gc_fullsweep(g, &gco2th(o)->openupval);
403 if (((o->gch.marked ^ LJ_GC_WHITES) & ow)) { /* Black or current white? */ 403 if (((o->gch.marked ^ LJ_GC_WHITES) & ow)) { /* Black or current white? */
404 lua_assert(!isdead(g, o) || (o->gch.marked & LJ_GC_FIXED)); 404 lj_assertG(!isdead(g, o) || (o->gch.marked & LJ_GC_FIXED),
405 "sweep of undead object");
405 makewhite(g, o); /* Value is alive, change to the current white. */ 406 makewhite(g, o); /* Value is alive, change to the current white. */
406 p = &o->gch.nextgc; 407 p = &o->gch.nextgc;
407 } else { /* Otherwise value is dead, free it. */ 408 } else { /* Otherwise value is dead, free it. */
408 lua_assert(isdead(g, o) || ow == LJ_GC_SFIXED); 409 lj_assertG(isdead(g, o) || ow == LJ_GC_SFIXED,
410 "sweep of unlive object");
409 setgcrefr(*p, o->gch.nextgc); 411 setgcrefr(*p, o->gch.nextgc);
410 if (o == gcref(g->gc.root)) 412 if (o == gcref(g->gc.root))
411 setgcrefr(g->gc.root, o->gch.nextgc); /* Adjust list anchor. */ 413 setgcrefr(g->gc.root, o->gch.nextgc); /* Adjust list anchor. */
@@ -415,6 +417,32 @@ static GCRef *gc_sweep(global_State *g, GCRef *p, uint32_t lim)
415 return p; 417 return p;
416} 418}
417 419
420/* Sweep one string interning table chain. Preserves hashalg bit. */
421static void gc_sweepstr(global_State *g, GCRef *chain)
422{
423 /* Mask with other white and LJ_GC_FIXED. Or LJ_GC_SFIXED on shutdown. */
424 int ow = otherwhite(g);
425 uintptr_t u = gcrefu(*chain);
426 GCRef q;
427 GCRef *p = &q;
428 GCobj *o;
429 setgcrefp(q, (u & ~(uintptr_t)1));
430 while ((o = gcref(*p)) != NULL) {
431 if (((o->gch.marked ^ LJ_GC_WHITES) & ow)) { /* Black or current white? */
432 lj_assertG(!isdead(g, o) || (o->gch.marked & LJ_GC_FIXED),
433 "sweep of undead string");
434 makewhite(g, o); /* String is alive, change to the current white. */
435 p = &o->gch.nextgc;
436 } else { /* Otherwise string is dead, free it. */
437 lj_assertG(isdead(g, o) || ow == LJ_GC_SFIXED,
438 "sweep of unlive string");
439 setgcrefr(*p, o->gch.nextgc);
440 lj_str_free(g, gco2str(o));
441 }
442 }
443 setgcrefp(*chain, (gcrefu(q) | (u & 1)));
444}
445
418/* Check whether we can clear a key or a value slot from a table. */ 446/* Check whether we can clear a key or a value slot from a table. */
419static int gc_mayclear(cTValue *o, int val) 447static int gc_mayclear(cTValue *o, int val)
420{ 448{
@@ -432,11 +460,12 @@ static int gc_mayclear(cTValue *o, int val)
432} 460}
433 461
434/* Clear collected entries from weak tables. */ 462/* Clear collected entries from weak tables. */
435static void gc_clearweak(GCobj *o) 463static void gc_clearweak(global_State *g, GCobj *o)
436{ 464{
465 UNUSED(g);
437 while (o) { 466 while (o) {
438 GCtab *t = gco2tab(o); 467 GCtab *t = gco2tab(o);
439 lua_assert((t->marked & LJ_GC_WEAK)); 468 lj_assertG((t->marked & LJ_GC_WEAK), "clear of non-weak table");
440 if ((t->marked & LJ_GC_WEAKVAL)) { 469 if ((t->marked & LJ_GC_WEAKVAL)) {
441 MSize i, asize = t->asize; 470 MSize i, asize = t->asize;
442 for (i = 0; i < asize; i++) { 471 for (i = 0; i < asize; i++) {
@@ -467,18 +496,21 @@ static void gc_call_finalizer(global_State *g, lua_State *L,
467{ 496{
468 /* Save and restore lots of state around the __gc callback. */ 497 /* Save and restore lots of state around the __gc callback. */
469 uint8_t oldh = hook_save(g); 498 uint8_t oldh = hook_save(g);
470 MSize oldt = g->gc.threshold; 499 GCSize oldt = g->gc.threshold;
471 int errcode; 500 int errcode;
472 TValue *top; 501 TValue *top;
473 lj_trace_abort(g); 502 lj_trace_abort(g);
474 top = L->top;
475 L->top = top+2;
476 hook_entergc(g); /* Disable hooks and new traces during __gc. */ 503 hook_entergc(g); /* Disable hooks and new traces during __gc. */
504 if (LJ_HASPROFILE && (oldh & HOOK_PROFILE)) lj_dispatch_update(g);
477 g->gc.threshold = LJ_MAX_MEM; /* Prevent GC steps. */ 505 g->gc.threshold = LJ_MAX_MEM; /* Prevent GC steps. */
478 copyTV(L, top, mo); 506 top = L->top;
479 setgcV(L, top+1, o, ~o->gch.gct); 507 copyTV(L, top++, mo);
480 errcode = lj_vm_pcall(L, top+1, 1+0, -1); /* Stack: |mo|o| -> | */ 508 if (LJ_FR2) setnilV(top++);
509 setgcV(L, top, o, ~o->gch.gct);
510 L->top = top+1;
511 errcode = lj_vm_pcall(L, top, 1+0, -1); /* Stack: |mo|o| -> | */
481 hook_restore(g, oldh); 512 hook_restore(g, oldh);
513 if (LJ_HASPROFILE && (oldh & HOOK_PROFILE)) lj_dispatch_update(g);
482 g->gc.threshold = oldt; /* Restore GC threshold. */ 514 g->gc.threshold = oldt; /* Restore GC threshold. */
483 if (errcode) 515 if (errcode)
484 lj_err_throw(L, errcode); /* Propagate errors. */ 516 lj_err_throw(L, errcode); /* Propagate errors. */
@@ -490,7 +522,7 @@ static void gc_finalize(lua_State *L)
490 global_State *g = G(L); 522 global_State *g = G(L);
491 GCobj *o = gcnext(gcref(g->gc.mmudata)); 523 GCobj *o = gcnext(gcref(g->gc.mmudata));
492 cTValue *mo; 524 cTValue *mo;
493 lua_assert(gcref(g->jit_L) == NULL); /* Must not be called on trace. */ 525 lj_assertG(tvref(g->jit_base) == NULL, "finalizer called on trace");
494 /* Unchain from list of userdata to be finalized. */ 526 /* Unchain from list of userdata to be finalized. */
495 if (o == gcref(g->gc.mmudata)) 527 if (o == gcref(g->gc.mmudata))
496 setgcrefnull(g->gc.mmudata); 528 setgcrefnull(g->gc.mmudata);
@@ -565,9 +597,9 @@ void lj_gc_freeall(global_State *g)
565 /* Free everything, except super-fixed objects (the main thread). */ 597 /* Free everything, except super-fixed objects (the main thread). */
566 g->gc.currentwhite = LJ_GC_WHITES | LJ_GC_SFIXED; 598 g->gc.currentwhite = LJ_GC_WHITES | LJ_GC_SFIXED;
567 gc_fullsweep(g, &g->gc.root); 599 gc_fullsweep(g, &g->gc.root);
568 strmask = g->strmask; 600 strmask = g->str.mask;
569 for (i = 0; i <= strmask; i++) /* Free all string hash chains. */ 601 for (i = 0; i <= strmask; i++) /* Free all string hash chains. */
570 gc_fullsweep(g, &g->strhash[i]); 602 gc_sweepstr(g, &g->str.tab[i]);
571} 603}
572 604
573/* -- Collector ----------------------------------------------------------- */ 605/* -- Collector ----------------------------------------------------------- */
@@ -582,7 +614,7 @@ static void atomic(global_State *g, lua_State *L)
582 614
583 setgcrefr(g->gc.gray, g->gc.weak); /* Empty the list of weak tables. */ 615 setgcrefr(g->gc.gray, g->gc.weak); /* Empty the list of weak tables. */
584 setgcrefnull(g->gc.weak); 616 setgcrefnull(g->gc.weak);
585 lua_assert(!iswhite(obj2gco(mainthread(g)))); 617 lj_assertG(!iswhite(obj2gco(mainthread(g))), "main thread turned white");
586 gc_markobj(g, L); /* Mark running thread. */ 618 gc_markobj(g, L); /* Mark running thread. */
587 gc_traverse_curtrace(g); /* Traverse current trace. */ 619 gc_traverse_curtrace(g); /* Traverse current trace. */
588 gc_mark_gcroot(g); /* Mark GC roots (again). */ 620 gc_mark_gcroot(g); /* Mark GC roots (again). */
@@ -597,13 +629,15 @@ static void atomic(global_State *g, lua_State *L)
597 udsize += gc_propagate_gray(g); /* And propagate the marks. */ 629 udsize += gc_propagate_gray(g); /* And propagate the marks. */
598 630
599 /* All marking done, clear weak tables. */ 631 /* All marking done, clear weak tables. */
600 gc_clearweak(gcref(g->gc.weak)); 632 gc_clearweak(g, gcref(g->gc.weak));
633
634 lj_buf_shrink(L, &g->tmpbuf); /* Shrink temp buffer. */
601 635
602 /* Prepare for sweep phase. */ 636 /* Prepare for sweep phase. */
603 g->gc.currentwhite = (uint8_t)otherwhite(g); /* Flip current white. */ 637 g->gc.currentwhite = (uint8_t)otherwhite(g); /* Flip current white. */
604 g->strempty.marked = g->gc.currentwhite; 638 g->strempty.marked = g->gc.currentwhite;
605 setmref(g->gc.sweep, &g->gc.root); 639 setmref(g->gc.sweep, &g->gc.root);
606 g->gc.estimate = g->gc.total - (MSize)udsize; /* Initial estimate. */ 640 g->gc.estimate = g->gc.total - (GCSize)udsize; /* Initial estimate. */
607} 641}
608 642
609/* GC state machine. Returns a cost estimate for each step performed. */ 643/* GC state machine. Returns a cost estimate for each step performed. */
@@ -620,28 +654,29 @@ static size_t gc_onestep(lua_State *L)
620 g->gc.state = GCSatomic; /* End of mark phase. */ 654 g->gc.state = GCSatomic; /* End of mark phase. */
621 return 0; 655 return 0;
622 case GCSatomic: 656 case GCSatomic:
623 if (gcref(g->jit_L)) /* Don't run atomic phase on trace. */ 657 if (tvref(g->jit_base)) /* Don't run atomic phase on trace. */
624 return LJ_MAX_MEM; 658 return LJ_MAX_MEM;
625 atomic(g, L); 659 atomic(g, L);
626 g->gc.state = GCSsweepstring; /* Start of sweep phase. */ 660 g->gc.state = GCSsweepstring; /* Start of sweep phase. */
627 g->gc.sweepstr = 0; 661 g->gc.sweepstr = 0;
628 return 0; 662 return 0;
629 case GCSsweepstring: { 663 case GCSsweepstring: {
630 MSize old = g->gc.total; 664 GCSize old = g->gc.total;
631 gc_fullsweep(g, &g->strhash[g->gc.sweepstr++]); /* Sweep one chain. */ 665 gc_sweepstr(g, &g->str.tab[g->gc.sweepstr++]); /* Sweep one chain. */
632 if (g->gc.sweepstr > g->strmask) 666 if (g->gc.sweepstr > g->str.mask)
633 g->gc.state = GCSsweep; /* All string hash chains sweeped. */ 667 g->gc.state = GCSsweep; /* All string hash chains sweeped. */
634 lua_assert(old >= g->gc.total); 668 lj_assertG(old >= g->gc.total, "sweep increased memory");
635 g->gc.estimate -= old - g->gc.total; 669 g->gc.estimate -= old - g->gc.total;
636 return GCSWEEPCOST; 670 return GCSWEEPCOST;
637 } 671 }
638 case GCSsweep: { 672 case GCSsweep: {
639 MSize old = g->gc.total; 673 GCSize old = g->gc.total;
640 setmref(g->gc.sweep, gc_sweep(g, mref(g->gc.sweep, GCRef), GCSWEEPMAX)); 674 setmref(g->gc.sweep, gc_sweep(g, mref(g->gc.sweep, GCRef), GCSWEEPMAX));
641 lua_assert(old >= g->gc.total); 675 lj_assertG(old >= g->gc.total, "sweep increased memory");
642 g->gc.estimate -= old - g->gc.total; 676 g->gc.estimate -= old - g->gc.total;
643 if (gcref(*mref(g->gc.sweep, GCRef)) == NULL) { 677 if (gcref(*mref(g->gc.sweep, GCRef)) == NULL) {
644 gc_shrink(g, L); 678 if (g->str.num <= (g->str.mask >> 2) && g->str.mask > LJ_MIN_STRTAB*2-1)
679 lj_str_resize(L, g->str.mask >> 1); /* Shrink string table. */
645 if (gcref(g->gc.mmudata)) { /* Need any finalizations? */ 680 if (gcref(g->gc.mmudata)) { /* Need any finalizations? */
646 g->gc.state = GCSfinalize; 681 g->gc.state = GCSfinalize;
647#if LJ_HASFFI 682#if LJ_HASFFI
@@ -656,7 +691,7 @@ static size_t gc_onestep(lua_State *L)
656 } 691 }
657 case GCSfinalize: 692 case GCSfinalize:
658 if (gcref(g->gc.mmudata) != NULL) { 693 if (gcref(g->gc.mmudata) != NULL) {
659 if (gcref(g->jit_L)) /* Don't call finalizers on trace. */ 694 if (tvref(g->jit_base)) /* Don't call finalizers on trace. */
660 return LJ_MAX_MEM; 695 return LJ_MAX_MEM;
661 gc_finalize(L); /* Finalize one userdata object. */ 696 gc_finalize(L); /* Finalize one userdata object. */
662 if (g->gc.estimate > GCFINALIZECOST) 697 if (g->gc.estimate > GCFINALIZECOST)
@@ -670,7 +705,7 @@ static size_t gc_onestep(lua_State *L)
670 g->gc.debt = 0; 705 g->gc.debt = 0;
671 return 0; 706 return 0;
672 default: 707 default:
673 lua_assert(0); 708 lj_assertG(0, "bad GC state");
674 return 0; 709 return 0;
675 } 710 }
676} 711}
@@ -679,7 +714,7 @@ static size_t gc_onestep(lua_State *L)
679int LJ_FASTCALL lj_gc_step(lua_State *L) 714int LJ_FASTCALL lj_gc_step(lua_State *L)
680{ 715{
681 global_State *g = G(L); 716 global_State *g = G(L);
682 MSize lim; 717 GCSize lim;
683 int32_t ostate = g->vmstate; 718 int32_t ostate = g->vmstate;
684 setvmstate(g, GC); 719 setvmstate(g, GC);
685 lim = (GCSTEPSIZE/100) * g->gc.stepmul; 720 lim = (GCSTEPSIZE/100) * g->gc.stepmul;
@@ -688,13 +723,13 @@ int LJ_FASTCALL lj_gc_step(lua_State *L)
688 if (g->gc.total > g->gc.threshold) 723 if (g->gc.total > g->gc.threshold)
689 g->gc.debt += g->gc.total - g->gc.threshold; 724 g->gc.debt += g->gc.total - g->gc.threshold;
690 do { 725 do {
691 lim -= (MSize)gc_onestep(L); 726 lim -= (GCSize)gc_onestep(L);
692 if (g->gc.state == GCSpause) { 727 if (g->gc.state == GCSpause) {
693 g->gc.threshold = (g->gc.estimate/100) * g->gc.pause; 728 g->gc.threshold = (g->gc.estimate/100) * g->gc.pause;
694 g->vmstate = ostate; 729 g->vmstate = ostate;
695 return 1; /* Finished a GC cycle. */ 730 return 1; /* Finished a GC cycle. */
696 } 731 }
697 } while ((int32_t)lim > 0); 732 } while (sizeof(lim) == 8 ? ((int64_t)lim > 0) : ((int32_t)lim > 0));
698 if (g->gc.debt < GCSTEPSIZE) { 733 if (g->gc.debt < GCSTEPSIZE) {
699 g->gc.threshold = g->gc.total + GCSTEPSIZE; 734 g->gc.threshold = g->gc.total + GCSTEPSIZE;
700 g->vmstate = ostate; 735 g->vmstate = ostate;
@@ -718,8 +753,8 @@ void LJ_FASTCALL lj_gc_step_fixtop(lua_State *L)
718/* Perform multiple GC steps. Called from JIT-compiled code. */ 753/* Perform multiple GC steps. Called from JIT-compiled code. */
719int LJ_FASTCALL lj_gc_step_jit(global_State *g, MSize steps) 754int LJ_FASTCALL lj_gc_step_jit(global_State *g, MSize steps)
720{ 755{
721 lua_State *L = gco2th(gcref(g->jit_L)); 756 lua_State *L = gco2th(gcref(g->cur_L));
722 L->base = mref(G(L)->jit_base, TValue); 757 L->base = tvref(G(L)->jit_base);
723 L->top = curr_topL(L); 758 L->top = curr_topL(L);
724 while (steps-- > 0 && lj_gc_step(L) == 0) 759 while (steps-- > 0 && lj_gc_step(L) == 0)
725 ; 760 ;
@@ -744,7 +779,8 @@ void lj_gc_fullgc(lua_State *L)
744 } 779 }
745 while (g->gc.state == GCSsweepstring || g->gc.state == GCSsweep) 780 while (g->gc.state == GCSsweepstring || g->gc.state == GCSsweep)
746 gc_onestep(L); /* Finish sweep. */ 781 gc_onestep(L); /* Finish sweep. */
747 lua_assert(g->gc.state == GCSfinalize || g->gc.state == GCSpause); 782 lj_assertG(g->gc.state == GCSfinalize || g->gc.state == GCSpause,
783 "bad GC state");
748 /* Now perform a full GC. */ 784 /* Now perform a full GC. */
749 g->gc.state = GCSpause; 785 g->gc.state = GCSpause;
750 do { gc_onestep(L); } while (g->gc.state != GCSpause); 786 do { gc_onestep(L); } while (g->gc.state != GCSpause);
@@ -757,9 +793,11 @@ void lj_gc_fullgc(lua_State *L)
757/* Move the GC propagation frontier forward. */ 793/* Move the GC propagation frontier forward. */
758void lj_gc_barrierf(global_State *g, GCobj *o, GCobj *v) 794void lj_gc_barrierf(global_State *g, GCobj *o, GCobj *v)
759{ 795{
760 lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); 796 lj_assertG(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o),
761 lua_assert(g->gc.state != GCSfinalize && g->gc.state != GCSpause); 797 "bad object states for forward barrier");
762 lua_assert(o->gch.gct != ~LJ_TTAB); 798 lj_assertG(g->gc.state != GCSfinalize && g->gc.state != GCSpause,
799 "bad GC state");
800 lj_assertG(o->gch.gct != ~LJ_TTAB, "barrier object is not a table");
763 /* Preserve invariant during propagation. Otherwise it doesn't matter. */ 801 /* Preserve invariant during propagation. Otherwise it doesn't matter. */
764 if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic) 802 if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic)
765 gc_mark(g, v); /* Move frontier forward. */ 803 gc_mark(g, v); /* Move frontier forward. */
@@ -796,7 +834,8 @@ void lj_gc_closeuv(global_State *g, GCupval *uv)
796 lj_gc_barrierf(g, o, gcV(&uv->tv)); 834 lj_gc_barrierf(g, o, gcV(&uv->tv));
797 } else { 835 } else {
798 makewhite(g, o); /* Make it white, i.e. sweep the upvalue. */ 836 makewhite(g, o); /* Make it white, i.e. sweep the upvalue. */
799 lua_assert(g->gc.state != GCSfinalize && g->gc.state != GCSpause); 837 lj_assertG(g->gc.state != GCSfinalize && g->gc.state != GCSpause,
838 "bad GC state");
800 } 839 }
801 } 840 }
802} 841}
@@ -813,27 +852,29 @@ void lj_gc_barriertrace(global_State *g, uint32_t traceno)
813/* -- Allocator ----------------------------------------------------------- */ 852/* -- Allocator ----------------------------------------------------------- */
814 853
815/* Call pluggable memory allocator to allocate or resize a fragment. */ 854/* Call pluggable memory allocator to allocate or resize a fragment. */
816void *lj_mem_realloc(lua_State *L, void *p, MSize osz, MSize nsz) 855void *lj_mem_realloc(lua_State *L, void *p, GCSize osz, GCSize nsz)
817{ 856{
818 global_State *g = G(L); 857 global_State *g = G(L);
819 lua_assert((osz == 0) == (p == NULL)); 858 lj_assertG((osz == 0) == (p == NULL), "realloc API violation");
820 p = g->allocf(g->allocd, p, osz, nsz); 859 p = g->allocf(g->allocd, p, osz, nsz);
821 if (p == NULL && nsz > 0) 860 if (p == NULL && nsz > 0)
822 lj_err_mem(L); 861 lj_err_mem(L);
823 lua_assert((nsz == 0) == (p == NULL)); 862 lj_assertG((nsz == 0) == (p == NULL), "allocf API violation");
824 lua_assert(checkptr32(p)); 863 lj_assertG(checkptrGC(p),
864 "allocated memory address %p outside required range", p);
825 g->gc.total = (g->gc.total - osz) + nsz; 865 g->gc.total = (g->gc.total - osz) + nsz;
826 return p; 866 return p;
827} 867}
828 868
829/* Allocate new GC object and link it to the root set. */ 869/* Allocate new GC object and link it to the root set. */
830void * LJ_FASTCALL lj_mem_newgco(lua_State *L, MSize size) 870void * LJ_FASTCALL lj_mem_newgco(lua_State *L, GCSize size)
831{ 871{
832 global_State *g = G(L); 872 global_State *g = G(L);
833 GCobj *o = (GCobj *)g->allocf(g->allocd, NULL, 0, size); 873 GCobj *o = (GCobj *)g->allocf(g->allocd, NULL, 0, size);
834 if (o == NULL) 874 if (o == NULL)
835 lj_err_mem(L); 875 lj_err_mem(L);
836 lua_assert(checkptr32(o)); 876 lj_assertG(checkptrGC(o),
877 "allocated memory address %p outside required range", o);
837 g->gc.total += size; 878 g->gc.total += size;
838 setgcrefr(o->gch.nextgc, g->gc.root); 879 setgcrefr(o->gch.nextgc, g->gc.root);
839 setgcref(g->gc.root, o); 880 setgcref(g->gc.root, o);