From 69371c4b84becac09c445aae01d005b49658ef82 Mon Sep 17 00:00:00 2001
From: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Mon, 24 Apr 2017 13:59:26 -0300
Subject: 'KGC_NORMAL' -> 'KGC_INC' + emergency GC signalled by flag (instead
 of mode)

---
 lapi.c   |  4 ++--
 lgc.c    | 23 +++++++++++------------
 lstate.c |  5 +++--
 lstate.h |  6 +++---
 ltests.c |  4 ++--
 5 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/lapi.c b/lapi.c
index 29f3d265..2be52631 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lapi.c,v 2.263 2017/04/19 17:02:50 roberto Exp roberto $
+** $Id: lapi.c,v 2.264 2017/04/20 18:22:44 roberto Exp roberto $
 ** Lua API
 ** See Copyright Notice in lua.h
 */
@@ -1111,7 +1111,7 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
       break;
     }
     case LUA_GCINC: {
-      luaC_changemode(L, KGC_NORMAL);
+      luaC_changemode(L, KGC_INC);
       break;
     }
     default: res = -1;  /* invalid option */
diff --git a/lgc.c b/lgc.c
index 59b6dde1..d27ea42b 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lgc.c,v 2.223 2017/04/19 17:02:50 roberto Exp roberto $
+** $Id: lgc.c,v 2.224 2017/04/20 18:24:33 roberto Exp roberto $
 ** Garbage Collector
 ** See Copyright Notice in lua.h
 */
@@ -543,7 +543,7 @@ static lu_mem traversethread (global_State *g, lua_State *th) {
       g->twups = th;
     }
   }
-  else if (g->gckind != KGC_EMERGENCY)
+  else if (!g->gcemergency)
     luaD_shrinkstack(th); /* do not change stack in emergency cycle */
   return (sizeof(lua_State) + sizeof(TValue) * th->stacksize +
           sizeof(CallInfo) * th->nci);
@@ -767,7 +767,7 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p) {
 ** If possible, shrink string table
 */
 static void checkSizes (lua_State *L, global_State *g) {
-  if (g->gckind != KGC_EMERGENCY) {
+  if (!g->gcemergency) {
     l_mem olddebt = g->GCdebt;
     if (g->strt.nuse < g->strt.size / 4)  /* string table too big? */
       luaS_resize(L, g->strt.size / 2);  /* shrink it a little */
@@ -1174,7 +1174,7 @@ static void enterinc (global_State *g) {
   g->finobjrold = g->finobjold = g->finobjsur = NULL;
   lua_assert(g->tobefnz == NULL);  /* no need to sweep */
   g->gcstate = GCSpause;
-  g->gckind = KGC_NORMAL;
+  g->gckind = KGC_INC;
 }
 
 
@@ -1278,7 +1278,7 @@ static void deletealllist (lua_State *L, GCObject *p, GCObject *limit) {
 
 void luaC_freeallobjects (lua_State *L) {
   global_State *g = G(L);
-  luaC_changemode(L, KGC_NORMAL);
+  luaC_changemode(L, KGC_INC);
   separatetobefnz(g, 1);  /* separate all objects with finalizers */
   lua_assert(g->finobj == NULL);
   callallpendingfinalizers(L);
@@ -1394,7 +1394,7 @@ static lu_mem singlestep (lua_State *L) {
       return 0;
     }
     case GCScallfin: {  /* call remaining finalizers */
-      if (g->tobefnz && g->gckind != KGC_EMERGENCY) {
+      if (g->tobefnz && !g->gcemergency) {
         int n = runafewfinalizers(L);
         return (n * GCFINALIZECOST);
       }
@@ -1459,7 +1459,7 @@ void luaC_step (lua_State *L) {
   global_State *g = G(L);
   if (!g->gcrunning)  /* not running? */
     luaE_setdebt(g, -GCSTEPSIZE * 10);  /* avoid being called too often */
-  else if (g->gckind == KGC_NORMAL)
+  else if (g->gckind == KGC_INC)
     incstep(L, g);
   else
     genstep(L, g);
@@ -1490,14 +1490,13 @@ static void fullinc (lua_State *L, global_State *g) {
 
 void luaC_fullgc (lua_State *L, int isemergency) {
   global_State *g = G(L);
-  int gckind = g->gckind;
-  if (isemergency)
-    g->gckind = KGC_EMERGENCY;  /* set flag */
-  if (gckind == KGC_NORMAL)
+  lua_assert(!g->gcemergency);
+  g->gcemergency = isemergency;  /* set flag */
+  if (g->gckind == KGC_INC)
     fullinc(L, g);
   else
     fullgen(L, g);
-  g->gckind = gckind;
+  g->gcemergency = 0;
 }
 
 /* }====================================================== */
diff --git a/lstate.c b/lstate.c
index adc1bce9..653c90f2 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.c,v 2.136 2017/04/11 19:00:27 roberto Exp roberto $
+** $Id: lstate.c,v 2.137 2017/04/12 18:56:25 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -209,6 +209,7 @@ static void f_luaopen (lua_State *L, void *ud) {
   luaT_init(L);
   luaX_init(L);
   g->gcrunning = 1;  /* allow gc */
+  g->gcemergency = 0;
   g->version = lua_version(NULL);
   luai_userstateopen(L);
 }
@@ -317,7 +318,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
   g->panic = NULL;
   g->version = NULL;
   g->gcstate = GCSpause;
-  g->gckind = KGC_NORMAL;
+  g->gckind = KGC_INC;
   g->finobj = g->tobefnz = g->fixedgc = NULL;
   g->survival = g->old = g->reallyold = NULL;
   g->finobjsur = g->finobjold = g->finobjrold = NULL;
diff --git a/lstate.h b/lstate.h
index d844fc8c..d2f23839 100644
--- a/lstate.h
+++ b/lstate.h
@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.h,v 2.137 2017/04/11 18:41:09 roberto Exp roberto $
+** $Id: lstate.h,v 2.138 2017/04/19 17:02:50 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -69,9 +69,8 @@ struct lua_longjmp;  /* defined in ldo.c */
 
 
 /* kinds of Garbage Collection */
-#define KGC_NORMAL	0
+#define KGC_INC		0	/* incremental gc */
 #define KGC_GEN		1	/* generational gc */
-#define KGC_EMERGENCY	2	/* gc was forced by an allocation failure */
 
 
 typedef struct stringtable {
@@ -151,6 +150,7 @@ typedef struct global_State {
   lu_byte genminormul;  /* control for minor generational collections */
   lu_byte genmajormul;  /* control for major generational collections */
   lu_byte gcrunning;  /* true if GC is running */
+  lu_byte gcemergency;  /* true if this is an emergency collection */
   GCObject *allgc;  /* list of all collectable objects */
   GCObject **sweepgc;  /* current position of sweep in list */
   GCObject *finobj;  /* list of collectable objects with finalizers */
diff --git a/ltests.c b/ltests.c
index 9679bee2..77f0163d 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
 /*
-** $Id: ltests.c,v 2.213 2017/04/18 19:42:12 roberto Exp roberto $
+** $Id: ltests.c,v 2.214 2017/04/19 18:46:47 roberto Exp roberto $
 ** Internal Module for Debugging of the Lua Implementation
 ** See Copyright Notice in lua.h
 */
@@ -199,7 +199,7 @@ static int testobjref1 (global_State *g, GCObject *f, GCObject *t) {
   if (isdead(g,t)) return 0;
   if (issweepphase(g))
     return 1;  /* no invariants */
-  else if (g->gckind == KGC_NORMAL)
+  else if (g->gckind == KGC_INC)
     return !(isblack(f) && iswhite(t));  /* basic incremental invariant */
   else {  /* generational mode */
     if ((getage(f) == G_OLD && isblack(f)) && !isold(t))
-- 
cgit v1.2.3-55-g6feb