From 45bd14b392622cb58e967a24e4652c510b3d43e4 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Fri, 12 Jul 2019 05:23:52 -0500 Subject: Lua Add Emergency Garbage Collector Derivative of work by RobertGabrielJakabosky http://lua-users.org/wiki/EmergencyGarbageCollector I've only implemented the not enough memory part and expanded this idea to adding a mechanism to signal the OOM condition of the plugin buffer which allows us to only grab the playback buffer after garbage collection fails (SO THE MUSIC KEEPS PLAYING AS LONG AS POSSIBLE) Change-Id: I684fb98b540ffc01f7ba324ab5b761ceb59b9f9b --- apps/plugins/lua/lvm.c | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) (limited to 'apps/plugins/lua/lvm.c') diff --git a/apps/plugins/lua/lvm.c b/apps/plugins/lua/lvm.c index 35a931d7a1..4979b6a88e 100644 --- a/apps/plugins/lua/lvm.c +++ b/apps/plugins/lua/lvm.c @@ -49,9 +49,10 @@ int luaV_tostring (lua_State *L, StkId obj) { return 0; else { char s[LUAI_MAXNUMBER2STR]; + ptrdiff_t objr = savestack(L, obj); lua_Number n = nvalue(obj); lua_number2str(s, n); - setsvalue2s(L, obj, luaS_new(L, s)); + setsvalue2s(L, restorestack(L, objr), luaS_new(L, s)); return 1; } } @@ -134,6 +135,9 @@ void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { int loop; TValue temp; + setnilvalue(L->top); + L->top++; + fixedstack(L); for (loop = 0; loop < MAXTAGLOOP; loop++) { const TValue *tm; if (ttistable(t)) { /* `t' is a table? */ @@ -141,6 +145,8 @@ void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { TValue *oldval = luaH_set(L, h, key); /* do a primitive set */ if (!ttisnil(oldval) || /* result is no nil? */ (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */ + L->top--; + unfixedstack(L); setobj2t(L, oldval, val); h->flags = 0; luaC_barriert(L, h, val); @@ -151,12 +157,15 @@ void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) luaG_typeerror(L, t, "index"); if (ttisfunction(tm)) { + L->top--; + unfixedstack(L); callTM(L, tm, t, key, val); return; } /* else repeat with `tm' */ setobj(L, &temp, tm); /* avoid pointing inside table (may rehash) */ t = &temp; + setobj2s(L, L->top-1, t); /* need to protect value from EGC. */ } luaG_runerror(L, "loop in settable"); } @@ -284,8 +293,11 @@ void luaV_concat (lua_State *L, int total, int last) { StkId top = L->base + last + 1; int n = 2; /* number of elements handled in this pass (at least 2) */ if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) { - if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT)) + if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT)) { + /* restore 'top' pointer, since stack might have been reallocted */ + top = L->base + last + 1; luaG_concaterror(L, top-2, top-1); + } } else if (tsvalue(top-1)->len == 0) /* second op is empty? */ (void)tostring(L, top - 2); /* result is first op (as string) */ else { @@ -293,12 +305,14 @@ void luaV_concat (lua_State *L, int total, int last) { size_t tl = tsvalue(top-1)->len; char *buffer; int i; + fixedstack(L); /* collect total length */ for (n = 1; n < total && tostring(L, top-n-1); n++) { size_t l = tsvalue(top-n-1)->len; if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow"); tl += l; } + G(L)->buff.n = tl; buffer = luaZ_openspace(L, &G(L)->buff, tl); tl = 0; for (i=n; i>0; i--) { /* concat all strings */ @@ -307,6 +321,8 @@ void luaV_concat (lua_State *L, int total, int last) { tl += l; } setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl)); + luaZ_resetbuffer(&G(L)->buff); + unfixedstack(L); } total -= n-1; /* got `n' strings to create 1 new */ last -= n-1; @@ -332,8 +348,13 @@ static void Arith (lua_State *L, StkId ra, const TValue *rb, default: lua_assert(0); break; } } - else if (!call_binTM(L, rb, rc, ra, op)) - luaG_aritherror(L, rb, rc); + else { + ptrdiff_t br = savestack(L, rb); + ptrdiff_t cr = savestack(L, rc); + if (!call_binTM(L, rb, rc, ra, op)) { + luaG_aritherror(L, restorestack(L, br), restorestack(L, cr)); + } + } } @@ -461,7 +482,9 @@ void luaV_execute (lua_State *L, int nexeccalls) { case OP_NEWTABLE: { int b = GETARG_B(i); int c = GETARG_C(i); - sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c))); + Table *h; + Protect(h = luaH_new(L, luaO_fb2int(b), luaO_fb2int(c))); + sethvalue(L, RA(i), h); Protect(luaC_checkGC(L)); continue; } @@ -547,9 +570,10 @@ void luaV_execute (lua_State *L, int nexeccalls) { break; } default: { /* try metamethod */ + ptrdiff_t br = savestack(L, rb); Protect( if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN)) - luaG_typeerror(L, rb, "get length of"); + luaG_typeerror(L, restorestack(L, br), "get length of"); ) } } @@ -723,6 +747,7 @@ void luaV_execute (lua_State *L, int nexeccalls) { int c = GETARG_C(i); int last; Table *h; + fixedstack(L); if (n == 0) { n = cast_int(L->top - ra) - 1; L->top = L->ci->top; @@ -738,6 +763,7 @@ void luaV_execute (lua_State *L, int nexeccalls) { setobj2t(L, luaH_setnum(L, h, last--), val); luaC_barriert(L, h, val); } + unfixedstack(L); continue; } case OP_CLOSE: { @@ -750,7 +776,9 @@ void luaV_execute (lua_State *L, int nexeccalls) { int nup, j; p = cl->p->p[GETARG_Bx(i)]; nup = p->nups; + fixedstack(L); ncl = luaF_newLclosure(L, nup, cl->env); + setclvalue(L, ra, ncl); ncl->l.p = p; for (j=0; jl.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc)); } } - setclvalue(L, ra, ncl); + unfixedstack(L); Protect(luaC_checkGC(L)); continue; } -- cgit v1.2.3