aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--lapi.c8
-rw-r--r--lgc.c16
-rw-r--r--lgc.h4
-rw-r--r--llimits.h7
-rw-r--r--lstate.c6
-rw-r--r--lstate.h6
6 files changed, 22 insertions, 25 deletions
diff --git a/lapi.c b/lapi.c
index 8c70bd4c..b2ac0c57 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1168,8 +1168,8 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
1168 g->gcstp = 0; /* allow GC to run (bit GCSTPGC must be zero here) */ 1168 g->gcstp = 0; /* allow GC to run (bit GCSTPGC must be zero here) */
1169 if (todo == 0) 1169 if (todo == 0)
1170 todo = 1 << g->gcstepsize; /* standard step size */ 1170 todo = 1 << g->gcstepsize; /* standard step size */
1171 while (todo + g->GCdebt > 0) { /* enough to run a step? */ 1171 while (todo >= g->GCdebt) { /* enough to run a step? */
1172 todo += g->GCdebt; /* decrement 'todo' (debt is usually negative) */ 1172 todo -= g->GCdebt; /* decrement 'todo' */
1173 luaC_step(L); /* run one basic step */ 1173 luaC_step(L); /* run one basic step */
1174 didsomething = 1; 1174 didsomething = 1;
1175 if (g->gckind == KGC_GEN) /* minor collections? */ 1175 if (g->gckind == KGC_GEN) /* minor collections? */
@@ -1177,8 +1177,8 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
1177 else if (g->gcstate == GCSpause) 1177 else if (g->gcstate == GCSpause)
1178 break; /* don't run more than one cycle */ 1178 break; /* don't run more than one cycle */
1179 } 1179 }
1180 /* add remaining 'todo' to total debt */ 1180 /* remove remaining 'todo' from total debt */
1181 luaE_setdebt(g, todo + g->GCdebt); 1181 luaE_setdebt(g, g->GCdebt - todo);
1182 g->gcstp = oldstp; /* restore previous state */ 1182 g->gcstp = oldstp; /* restore previous state */
1183 if (didsomething && g->gcstate == GCSpause) /* end of cycle? */ 1183 if (didsomething && g->gcstate == GCSpause) /* end of cycle? */
1184 res = 1; /* signal it */ 1184 res = 1; /* signal it */
diff --git a/lgc.c b/lgc.c
index 90a49091..27856650 100644
--- a/lgc.c
+++ b/lgc.c
@@ -242,7 +242,7 @@ GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, size_t offset) {
242 global_State *g = G(L); 242 global_State *g = G(L);
243 char *p = cast_charp(luaM_newobject(L, novariant(tt), sz)); 243 char *p = cast_charp(luaM_newobject(L, novariant(tt), sz));
244 GCObject *o = cast(GCObject *, p + offset); 244 GCObject *o = cast(GCObject *, p + offset);
245 g->GCdebt++; 245 g->GCdebt--;
246 o->marked = luaC_white(g); 246 o->marked = luaC_white(g);
247 o->tt = tt; 247 o->tt = tt;
248 o->next = g->allgc; 248 o->next = g->allgc;
@@ -1034,8 +1034,8 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
1034*/ 1034*/
1035static void setpause (global_State *g) { 1035static void setpause (global_State *g) {
1036 l_obj threshold = applygcparam(g, gcpause, g->marked); 1036 l_obj threshold = applygcparam(g, gcpause, g->marked);
1037 l_obj debt = gettotalobjs(g) - threshold; 1037 l_obj debt = threshold - gettotalobjs(g);
1038 if (debt > 0) debt = 0; 1038 if (debt < 0) debt = 0;
1039 luaE_setdebt(g, debt); 1039 luaE_setdebt(g, debt);
1040} 1040}
1041 1041
@@ -1285,7 +1285,7 @@ static void atomic2gen (lua_State *L, global_State *g) {
1285** total number of objects grows 'genminormul'%. 1285** total number of objects grows 'genminormul'%.
1286*/ 1286*/
1287static void setminordebt (global_State *g) { 1287static void setminordebt (global_State *g) {
1288 luaE_setdebt(g, -applygcparam(g, genminormul, gettotalobjs(g))); 1288 luaE_setdebt(g, applygcparam(g, genminormul, gettotalobjs(g)));
1289} 1289}
1290 1290
1291 1291
@@ -1378,13 +1378,13 @@ static void genmajorstep (lua_State *L, global_State *g) {
1378** Does a generational "step". If the total number of objects grew 1378** Does a generational "step". If the total number of objects grew
1379** more than 'majormul'% since the last major collection, does a 1379** more than 'majormul'% since the last major collection, does a
1380** major collection. Otherwise, does a minor collection. The test 1380** major collection. Otherwise, does a minor collection. The test
1381** ('GCdebt' > 0) avoids major collections when the step originated from 1381** ('GCdebt' != 0) avoids major collections when the step originated from
1382** 'collectgarbage("step")'. 1382** 'collectgarbage("step")'.
1383*/ 1383*/
1384static void genstep (lua_State *L, global_State *g) { 1384static void genstep (lua_State *L, global_State *g) {
1385 l_obj majorbase = g->GClastmajor; /* count after last major collection */ 1385 l_obj majorbase = g->GClastmajor; /* count after last major collection */
1386 l_obj majorinc = applygcparam(g, genmajormul, majorbase); 1386 l_obj majorinc = applygcparam(g, genmajormul, majorbase);
1387 if (g->GCdebt > 0 && gettotalobjs(g) > majorbase + majorinc) { 1387 if (g->GCdebt != 0 && gettotalobjs(g) > majorbase + majorinc) {
1388 /* do a major collection */ 1388 /* do a major collection */
1389 enterinc(g); 1389 enterinc(g);
1390 g->gckind = KGC_GENMAJOR; 1390 g->gckind = KGC_GENMAJOR;
@@ -1605,7 +1605,7 @@ static void incstep (lua_State *L, global_State *g) {
1605 if (g->gcstate == GCSpause) 1605 if (g->gcstate == GCSpause)
1606 setpause(g); /* pause until next cycle */ 1606 setpause(g); /* pause until next cycle */
1607 else { 1607 else {
1608 luaE_setdebt(g, -stepsize); 1608 luaE_setdebt(g, stepsize);
1609 } 1609 }
1610} 1610}
1611 1611
@@ -1618,7 +1618,7 @@ void luaC_step (lua_State *L) {
1618 global_State *g = G(L); 1618 global_State *g = G(L);
1619 lua_assert(!g->gcemergency); 1619 lua_assert(!g->gcemergency);
1620 if (!gcrunning(g)) /* not running? */ 1620 if (!gcrunning(g)) /* not running? */
1621 luaE_setdebt(g, -2000); 1621 luaE_setdebt(g, 2000);
1622 else { 1622 else {
1623 switch (g->gckind) { 1623 switch (g->gckind) {
1624 case KGC_INC: 1624 case KGC_INC:
diff --git a/lgc.h b/lgc.h
index 6d82690d..c8f7c6e6 100644
--- a/lgc.h
+++ b/lgc.h
@@ -175,13 +175,13 @@
175 175
176 176
177/* 177/*
178** Does one step of collection when debt becomes positive. 'pre'/'pos' 178** Does one step of collection when debt becomes zero. 'pre'/'pos'
179** allows some adjustments to be done only when needed. macro 179** allows some adjustments to be done only when needed. macro
180** 'condchangemem' is used only for heavy tests (forcing a full 180** 'condchangemem' is used only for heavy tests (forcing a full
181** GC cycle on every opportunity) 181** GC cycle on every opportunity)
182*/ 182*/
183#define luaC_condGC(L,pre,pos) \ 183#define luaC_condGC(L,pre,pos) \
184 { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \ 184 { if (G(L)->GCdebt <= 0) { pre; luaC_step(L); pos;}; \
185 condchangemem(L,pre,pos); } 185 condchangemem(L,pre,pos); }
186 186
187/* more often than not, 'pre'/'pos' are empty */ 187/* more often than not, 'pre'/'pos' are empty */
diff --git a/llimits.h b/llimits.h
index e4948791..246dca8b 100644
--- a/llimits.h
+++ b/llimits.h
@@ -33,6 +33,8 @@ typedef unsigned long lu_mem;
33typedef long l_obj; 33typedef long l_obj;
34#endif /* } */ 34#endif /* } */
35 35
36#define MAX_LOBJ cast(l_obj, ~cast(lu_mem, 0) >> 1)
37
36 38
37/* chars used as small naturals (so that 'char' is reserved for characters) */ 39/* chars used as small naturals (so that 'char' is reserved for characters) */
38typedef unsigned char lu_byte; 40typedef unsigned char lu_byte;
@@ -47,11 +49,6 @@ typedef signed char ls_byte;
47 : (size_t)(LUA_MAXINTEGER)) 49 : (size_t)(LUA_MAXINTEGER))
48 50
49 51
50#define MAX_LUMEM ((lu_mem)(~(lu_mem)0))
51
52#define MAX_LMEM ((l_obj)(MAX_LUMEM >> 1))
53
54
55#define MAX_INT INT_MAX /* maximum value of an int */ 52#define MAX_INT INT_MAX /* maximum value of an int */
56 53
57 54
diff --git a/lstate.c b/lstate.c
index b9897d96..bee3bf66 100644
--- a/lstate.c
+++ b/lstate.c
@@ -89,9 +89,9 @@ static unsigned int luai_makeseed (lua_State *L) {
89void luaE_setdebt (global_State *g, l_obj debt) { 89void luaE_setdebt (global_State *g, l_obj debt) {
90 l_obj tb = gettotalobjs(g); 90 l_obj tb = gettotalobjs(g);
91 lua_assert(tb > 0); 91 lua_assert(tb > 0);
92 if (debt < tb - MAX_LMEM) 92 if (debt > MAX_LOBJ - tb)
93 debt = tb - MAX_LMEM; /* will make 'totalobjs == MAX_LMEM' */ 93 debt = MAX_LOBJ - tb; /* will make 'totalobjs == MAX_LMEM' */
94 g->totalobjs = tb - debt; 94 g->totalobjs = tb + debt;
95 g->GCdebt = debt; 95 g->GCdebt = debt;
96} 96}
97 97
diff --git a/lstate.h b/lstate.h
index c290ff32..1aef2f75 100644
--- a/lstate.h
+++ b/lstate.h
@@ -251,8 +251,8 @@ typedef struct global_State {
251 lua_Alloc frealloc; /* function to reallocate memory */ 251 lua_Alloc frealloc; /* function to reallocate memory */
252 void *ud; /* auxiliary data to 'frealloc' */ 252 void *ud; /* auxiliary data to 'frealloc' */
253 lu_mem totalbytes; /* number of bytes currently allocated */ 253 lu_mem totalbytes; /* number of bytes currently allocated */
254 l_obj totalobjs; /* total number of objects allocated - GCdebt */ 254 l_obj totalobjs; /* total number of objects allocated + GCdebt */
255 l_obj GCdebt; /* bytes allocated not yet compensated by the collector */ 255 l_obj GCdebt; /* objects counted but not yet allocated */
256 l_obj marked; /* number of objects marked in a GC cycle */ 256 l_obj marked; /* number of objects marked in a GC cycle */
257 l_obj GClastmajor; /* objects at last major collection */ 257 l_obj GClastmajor; /* objects at last major collection */
258 stringtable strt; /* hash table for strings */ 258 stringtable strt; /* hash table for strings */
@@ -388,7 +388,7 @@ union GCUnion {
388 388
389 389
390/* actual number of total objects allocated */ 390/* actual number of total objects allocated */
391#define gettotalobjs(g) ((g)->totalobjs + (g)->GCdebt) 391#define gettotalobjs(g) ((g)->totalobjs - (g)->GCdebt)
392 392
393 393
394LUAI_FUNC void luaE_setdebt (global_State *g, l_obj debt); 394LUAI_FUNC void luaE_setdebt (global_State *g, l_obj debt);