From 2f91f95d94d3a27fee6b45c31ea9ab631924a8bf Mon Sep 17 00:00:00 2001
From: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Wed, 13 Nov 2002 09:32:26 -0200
Subject: better control over GCObjects

---
 ldo.c     |  4 ++--
 lfunc.c   | 23 ++++++++++++-----------
 lstate.c  |  5 +++--
 lstate.h  | 18 +++++++++++++++++-
 lstring.c | 21 +++++++++++----------
 ltable.c  |  4 ++--
 ltests.c  |  4 ++--
 7 files changed, 49 insertions(+), 30 deletions(-)

diff --git a/ldo.c b/ldo.c
index 0c7b4e14..a3e084b0 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.c,v 1.198 2002/11/06 19:08:00 roberto Exp roberto $
+** $Id: ldo.c,v 1.199 2002/11/07 15:37:10 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -105,7 +105,7 @@ static void correctstack (lua_State *L, TObject *oldstack) {
   GCObject *up;
   L->top = (L->top - oldstack) + L->stack;
   for (up = L->openupval; up != NULL; up = up->gch.next)
-    (&up->uv)->v = ((&up->uv)->v - oldstack) + L->stack;
+    gcotouv(up)->v = (gcotouv(up)->v - oldstack) + L->stack;
   for (ci = L->base_ci; ci <= L->ci; ci++) {
     ci->base = (ci->base - oldstack) + L->stack;
     ci->top = (ci->top - oldstack) + L->stack;
diff --git a/lfunc.c b/lfunc.c
index 433075e6..a34f31aa 100644
--- a/lfunc.c
+++ b/lfunc.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lfunc.c,v 1.60 2002/10/16 20:40:58 roberto Exp roberto $
+** $Id: lfunc.c,v 1.61 2002/10/21 20:41:46 roberto Exp roberto $
 ** Auxiliary functions to manipulate prototypes and closures
 ** See Copyright Notice in lua.h
 */
@@ -26,7 +26,7 @@
 
 Closure *luaF_newCclosure (lua_State *L, int nelems) {
   Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems)));
-  luaC_link(L, cast(GCObject *, c), LUA_TFUNCTION);
+  luaC_link(L, valtogco(c), LUA_TFUNCTION);
   c->c.isC = 1;
   c->c.nupvalues = cast(lu_byte, nelems);
   return c;
@@ -35,7 +35,7 @@ Closure *luaF_newCclosure (lua_State *L, int nelems) {
 
 Closure *luaF_newLclosure (lua_State *L, int nelems, TObject *gt) {
   Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));
-  luaC_link(L, cast(GCObject *, c), LUA_TFUNCTION);
+  luaC_link(L, valtogco(c), LUA_TFUNCTION);
   c->l.isC = 0;
   c->l.g = *gt;
   c->l.nupvalues = cast(lu_byte, nelems);
@@ -45,35 +45,36 @@ Closure *luaF_newLclosure (lua_State *L, int nelems, TObject *gt) {
 
 UpVal *luaF_findupval (lua_State *L, StkId level) {
   GCObject **pp = &L->openupval;
-  GCObject *p;
+  UpVal *p;
   UpVal *v;
-  while ((p = *pp) != NULL && (&p->uv)->v >= level) {
-    if ((&p->uv)->v == level) return &p->uv;
-    pp = &p->gch.next;
+  while ((p = ngcotouv(*pp)) != NULL && p->v >= level) {
+    if (p->v == level) return p;
+    pp = &p->next;
   }
   v = luaM_new(L, UpVal);  /* not found: create a new one */
+  v->tt = LUA_TUPVAL;
   v->marked = 1;  /* open upvalues should not be collected */
   v->v = level;  /* current value lives in the stack */
   v->next = *pp;  /* chain it in the proper position */
-  *pp = cast(GCObject *, v);
+  *pp = valtogco(v);
   return v;
 }
 
 
 void luaF_close (lua_State *L, StkId level) {
   UpVal *p;
-  while ((p = &(L->openupval)->uv) != NULL && p->v >= level) {
+  while ((p = ngcotouv(L->openupval)) != NULL && p->v >= level) {
     setobj(&p->value, p->v);  /* save current value */
     p->v = &p->value;  /* now current value lives here */
     L->openupval = p->next;  /* remove from `open' list */
-    luaC_link(L, cast(GCObject *, p), LUA_TUPVAL);
+    luaC_link(L, valtogco(p), LUA_TUPVAL);
   }
 }
 
 
 Proto *luaF_newproto (lua_State *L) {
   Proto *f = luaM_new(L, Proto);
-  luaC_link(L, cast(GCObject *, f), LUA_TPROTO);
+  luaC_link(L, valtogco(f), LUA_TPROTO);
   f->k = NULL;
   f->sizek = 0;
   f->p = NULL;
diff --git a/lstate.c b/lstate.c
index f84dcbb8..0213a0b4 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.c,v 1.108 2002/10/25 20:05:28 roberto Exp roberto $
+** $Id: lstate.c,v 1.109 2002/10/25 21:30:00 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -125,7 +125,7 @@ static void preinit_state (lua_State *L) {
 
 lua_State *luaE_newthread (lua_State *L) {
   lua_State *L1 = newthread(L);
-  luaC_link(L, cast(GCObject *, L1), LUA_TTHREAD);
+  luaC_link(L, valtogco(L1), LUA_TTHREAD);
   preinit_state(L1);
   L1->l_G = L->l_G;
   stack_init(L1, L);  /* init stack */
@@ -137,6 +137,7 @@ lua_State *luaE_newthread (lua_State *L) {
 LUA_API lua_State *lua_open (void) {
   lua_State *L = newthread(NULL);
   if (L) {  /* allocation OK? */
+    L->tt = LUA_TTHREAD;
     preinit_state(L);
     L->l_G = NULL;
     if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
diff --git a/lstate.h b/lstate.h
index f14738ba..e8bdb32b 100644
--- a/lstate.h
+++ b/lstate.h
@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.h,v 1.99 2002/10/25 20:05:28 roberto Exp roberto $
+** $Id: lstate.h,v 1.100 2002/11/06 19:08:00 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -144,6 +144,7 @@ struct lua_State {
   lua_Hook hook;
   TObject _gt;  /* table of globals */
   GCObject *openupval;  /* list of open upvalues in this stack */
+  GCObject *gclist;
   struct lua_longjmp *errorJmp;  /* current error recover point */
   ptrdiff_t errfunc;  /* current error handling function (stack index) */
 };
@@ -167,6 +168,21 @@ union GCObject {
 };
 
 
+/* macros to convert a GCObject into a specific value */
+#define gcotots(o)	check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
+#define gcotou(o)	check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
+#define gcotocl(o)	check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
+#define gcotoh(o)	check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
+#define gcotop(o)	check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
+#define gcotouv(o)	check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
+#define ngcotouv(o) \
+	check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))
+#define gcototh(o)	check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
+
+/* macro to convert any value into a GCObject */
+#define valtogco(v)	(cast(GCObject *, (v)))
+
+
 lua_State *luaE_newthread (lua_State *L);
 void luaE_freethread (lua_State *L, lua_State *L1);
 
diff --git a/lstring.c b/lstring.c
index c36839e3..66817103 100644
--- a/lstring.c
+++ b/lstring.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lstring.c,v 1.75 2002/08/16 14:45:55 roberto Exp roberto $
+** $Id: lstring.c,v 1.76 2002/08/30 19:09:21 roberto Exp roberto $
 ** String table (keeps all strings handled by Lua)
 ** See Copyright Notice in lua.h
 */
@@ -32,7 +32,7 @@ void luaS_resize (lua_State *L, int newsize) {
     GCObject *p = tb->hash[i];
     while (p) {  /* for each node in the list */
       GCObject *next = p->gch.next;  /* save next */
-      lu_hash h = (&p->ts)->tsv.hash;
+      lu_hash h = gcotots(p)->tsv.hash;
       int h1 = lmod(h, newsize);  /* new position */
       lua_assert(cast(int, h%newsize) == lmod(h, newsize));
       p->gch.next = newhash[h1];  /* chain it */
@@ -59,7 +59,7 @@ static TString *newlstr (lua_State *L, const char *str, size_t l, lu_hash h) {
   tb = &G(L)->strt;
   h = lmod(h, tb->size);
   ts->tsv.next = tb->hash[h];  /* chain new entry */
-  tb->hash[h] = cast(GCObject *, ts);
+  tb->hash[h] = valtogco(ts);
   tb->nuse++;
   if (tb->nuse > cast(ls_nstr, tb->size) && tb->size <= MAX_INT/2)
     luaS_resize(L, tb->size*2);  /* too crowded */
@@ -68,17 +68,18 @@ static TString *newlstr (lua_State *L, const char *str, size_t l, lu_hash h) {
 
 
 TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
-  GCObject *ts;
+  GCObject *o;
   lu_hash h = (lu_hash)l;  /* seed */
   size_t step = (l>>5)+1;  /* if string is too long, don't hash all its chars */
   size_t l1;
   for (l1=l; l1>=step; l1-=step)  /* compute hash */
     h = h ^ ((h<<5)+(h>>2)+(unsigned char)(str[l1-1]));
-  for (ts = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
-       ts != NULL;
-       ts = ts->gch.next) {
-    if ((&ts->ts)->tsv.len == l && (memcmp(str, getstr(&ts->ts), l) == 0))
-      return &ts->ts;
+  for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
+       o != NULL;
+       o = o->gch.next) {
+    TString *ts = gcotots(o);
+    if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0))
+      return ts;
   }
   return newlstr(L, str, l, h);  /* not found */
 }
@@ -93,7 +94,7 @@ Udata *luaS_newudata (lua_State *L, size_t s) {
   u->uv.metatable = hvalue(defaultmeta(L));
   /* chain it on udata list */
   u->uv.next = G(L)->rootudata;
-  G(L)->rootudata = cast(GCObject *, u);
+  G(L)->rootudata = valtogco(u);
   return u;
 }
 
diff --git a/ltable.c b/ltable.c
index 2866ed25..f58d6dfb 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
 /*
-** $Id: ltable.c,v 1.119 2002/09/02 19:54:49 roberto Exp roberto $
+** $Id: ltable.c,v 1.120 2002/11/07 15:37:10 roberto Exp roberto $
 ** Lua tables (hash)
 ** See Copyright Notice in lua.h
 */
@@ -301,7 +301,7 @@ static void rehash (lua_State *L, Table *t) {
 
 Table *luaH_new (lua_State *L, int narray, int lnhash) {
   Table *t = luaM_new(L, Table);
-  luaC_link(L, cast(GCObject *, t), LUA_TTABLE);
+  luaC_link(L, valtogco(t), LUA_TTABLE);
   t->metatable = hvalue(defaultmeta(L));
   t->flags = cast(lu_byte, ~0);
   t->mode = 0;
diff --git a/ltests.c b/ltests.c
index 91186da3..eecc6209 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
 /*
-** $Id: ltests.c,v 1.139 2002/10/25 21:29:20 roberto Exp roberto $
+** $Id: ltests.c,v 1.140 2002/11/07 15:37:10 roberto Exp roberto $
 ** Internal Module for Debugging of the Lua Implementation
 ** See Copyright Notice in lua.h
 */
@@ -326,7 +326,7 @@ static int string_query (lua_State *L) {
     GCObject *ts;
     int n = 0;
     for (ts = tb->hash[s]; ts; ts = ts->gch.next) {
-      setsvalue2s(L->top, &ts->ts);
+      setsvalue2s(L->top, gcotots(ts));
       incr_top(L);
       n++;
     }
-- 
cgit v1.2.3-55-g6feb