浏览代码

added check for conversion 'obj2gco' (and corrections for small
problems detected by this check)

Roberto Ierusalimschy 11 年之前
父节点
当前提交
56137d58ff
共有 10 个文件被更改,包括 51 次插入39 次删除
  1. 3 3
      lapi.c
  2. 8 5
      lgc.c
  3. 3 3
      llex.c
  4. 3 3
      lobject.h
  5. 4 4
      lparser.c
  6. 2 2
      lstate.c
  7. 12 4
      lstate.h
  8. 3 3
      lstring.c
  9. 11 10
      ltests.c
  10. 2 2
      ltm.c

+ 3 - 3
lapi.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lapi.c,v 2.225 2014/07/15 21:26:50 roberto Exp roberto $
+** $Id: lapi.c,v 2.226 2014/07/17 13:53:37 roberto Exp roberto $
 ** Lua API
 ** See Copyright Notice in lua.h
 */
@@ -820,7 +820,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) {
     case LUA_TUSERDATA: {
       uvalue(obj)->metatable = mt;
       if (mt) {
-        luaC_objbarrier(L, rawuvalue(obj), mt);
+        luaC_objbarrier(L, uvalue(obj), mt);
         luaC_checkfinalizer(L, gcvalue(obj), mt);
       }
       break;
@@ -958,7 +958,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
       const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
       /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
       setobj(L, f->upvals[0]->v, gt);
-      luaC_barrier(L, f->upvals[0], gt);
+      luaC_upvalbarrier(L, f->upvals[0]);
     }
   }
   lua_unlock(L);

+ 8 - 5
lgc.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lgc.c,v 2.184 2014/06/30 19:48:08 roberto Exp roberto $
+** $Id: lgc.c,v 2.185 2014/07/17 17:27:49 roberto Exp roberto $
 ** Garbage Collector
 ** See Copyright Notice in lua.h
 */
@@ -83,6 +83,9 @@
 #define markobject(g,t) \
   { if ((t) && iswhite(obj2gco(t))) reallymarkobject(g, obj2gco(t)); }
 
+#define markstring(g,t) \
+  { if ((t) && iswhite(ts2gco(t))) reallymarkobject(g, ts2gco(t)); }
+
 static void reallymarkobject (global_State *g, GCObject *o);
 
 
@@ -126,7 +129,7 @@ static void removeentry (Node *n) {
 static int iscleared (global_State *g, const TValue *o) {
   if (!iscollectable(o)) return 0;
   else if (ttisstring(o)) {
-    markobject(g, rawtsvalue(o));  /* strings are `values', so are never weak */
+    markobject(g, tsvalue(o));  /* strings are `values', so are never weak */
     return 0;
   }
   else return iswhite(gcvalue(o));
@@ -448,15 +451,15 @@ static int traverseproto (global_State *g, Proto *f) {
   int i;
   if (f->cache && iswhite(obj2gco(f->cache)))
     f->cache = NULL;  /* allow cache to be collected */
-  markobject(g, f->source);
+  markstring(g, f->source);
   for (i = 0; i < f->sizek; i++)  /* mark literals */
     markvalue(g, &f->k[i]);
   for (i = 0; i < f->sizeupvalues; i++)  /* mark upvalue names */
-    markobject(g, f->upvalues[i].name);
+    markstring(g, f->upvalues[i].name);
   for (i = 0; i < f->sizep; i++)  /* mark nested protos */
     markobject(g, f->p[i]);
   for (i = 0; i < f->sizelocvars; i++)  /* mark local-variable names */
-    markobject(g, f->locvars[i].varname);
+    markstring(g, f->locvars[i].varname);
   return sizeof(Proto) + sizeof(Instruction) * f->sizecode +
                          sizeof(Proto *) * f->sizep +
                          sizeof(TValue) * f->sizek +

+ 3 - 3
llex.c

@@ -1,5 +1,5 @@
 /*
-** $Id: llex.c,v 2.77 2014/05/11 14:45:43 roberto Exp roberto $
+** $Id: llex.c,v 2.78 2014/05/21 15:22:02 roberto Exp roberto $
 ** Lexical Analyzer
 ** See Copyright Notice in lua.h
 */
@@ -67,10 +67,10 @@ static void save (LexState *ls, int c) {
 void luaX_init (lua_State *L) {
   int i;
   TString *e = luaS_new(L, LUA_ENV);  /* create env name */
-  luaC_fix(L, obj2gco(e));  /* never collect this name */
+  luaC_fix(L, ts2gco(e));  /* never collect this name */
   for (i=0; i<NUM_RESERVED; i++) {
     TString *ts = luaS_new(L, luaX_tokens[i]);
-    luaC_fix(L, obj2gco(ts));  /* reserved words are never collected */
+    luaC_fix(L, ts2gco(ts));  /* reserved words are never collected */
     ts->tsv.extra = cast_byte(i+1);  /* reserved word */
   }
 }

+ 3 - 3
lobject.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lobject.h,v 2.95 2014/07/17 17:09:50 roberto Exp roberto $
+** $Id: lobject.h,v 2.96 2014/07/17 17:27:49 roberto Exp roberto $
 ** Type definitions for Lua objects
 ** See Copyright Notice in lua.h
 */
@@ -210,12 +210,12 @@ typedef struct lua_TValue TValue;
 
 #define setsvalue(L,obj,x) \
   { TValue *io = (obj); TString *x_ = (x); \
-    val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tsv.tt)); \
+    val_(io).gc = obj2gco(&x_->tsv); settt_(io, ctb(x_->tsv.tt)); \
     checkliveness(G(L),io); }
 
 #define setuvalue(L,obj,x) \
   { TValue *io = (obj); Udata *x_ = (x); \
-    val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \
+    val_(io).gc = obj2gco(&x_->uv); settt_(io, ctb(LUA_TUSERDATA)); \
     checkliveness(G(L),io); }
 
 #define setthvalue(L,obj,x) \

+ 4 - 4
lparser.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lparser.c,v 2.138 2013/12/30 20:47:58 roberto Exp roberto $
+** $Id: lparser.c,v 2.139 2014/06/19 18:27:20 roberto Exp roberto $
 ** Lua Parser
 ** See Copyright Notice in lua.h
 */
@@ -164,7 +164,7 @@ static int registerlocalvar (LexState *ls, TString *varname) {
                   LocVar, SHRT_MAX, "local variables");
   while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL;
   f->locvars[fs->nlocvars].varname = varname;
-  luaC_objbarrier(ls->L, f, varname);
+  luaC_objbarrier(ls->L, f, ts2gco(varname));
   return fs->nlocvars++;
 }
 
@@ -232,7 +232,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) {
   f->upvalues[fs->nups].instack = (v->k == VLOCAL);
   f->upvalues[fs->nups].idx = cast_byte(v->u.info);
   f->upvalues[fs->nups].name = name;
-  luaC_objbarrier(fs->ls->L, f, name);
+  luaC_objbarrier(fs->ls->L, f, ts2gco(name));
   return fs->nups++;
 }
 
@@ -1630,7 +1630,7 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
   incr_top(L);
   funcstate.f = cl->p = luaF_newproto(L);
   funcstate.f->source = luaS_new(L, name);  /* create and anchor TString */
-  luaC_objbarrier(L, funcstate.f, funcstate.f->source);
+  luaC_objbarrier(L, funcstate.f, ts2gco(funcstate.f->source));
   lexstate.buff = buff;
   lexstate.dyd = dyd;
   dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;

+ 2 - 2
lstate.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.c,v 2.120 2014/02/18 13:39:37 roberto Exp roberto $
+** $Id: lstate.c,v 2.121 2014/02/18 13:46:26 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -206,7 +206,7 @@ static void f_luaopen (lua_State *L, void *ud) {
   luaX_init(L);
   /* pre-create memory-error message */
   g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
-  luaC_fix(L, obj2gco(g->memerrmsg));  /* it should never be collected */
+  luaC_fix(L, ts2gco(g->memerrmsg));  /* it should never be collected */
   g->gcrunning = 1;  /* allow gc */
   g->version = lua_version(NULL);
   luai_userstateopen(L);

+ 12 - 4
lstate.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.h,v 2.109 2014/07/17 17:09:50 roberto Exp roberto $
+** $Id: lstate.h,v 2.110 2014/07/17 17:27:49 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -170,7 +170,7 @@ struct lua_State {
 
 
 /*
-** Union of all collectable objects
+** Union of all collectable objects (only for conversions)
 */
 union GCUnion {
   GCObject gc;  /* common header */
@@ -200,8 +200,16 @@ union GCUnion {
 #define gco2th(o)  check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th))
 
 
-/* macro to convert any Lua object into a GCObject */
-#define obj2gco(v)	(&(cast_u(v)->gc))
+/* macro to convert a Lua object into a GCObject */
+#define obj2gco(v) \
+	check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc)))
+
+/*
+** macro to convert a TString into a GCObject.
+** (TString is a union, and therefore needs an access slightly different
+** from the other objects.)
+*/
+#define ts2gco(v)	(obj2gco(&(v)->tsv))
 
 
 /* actual number of total bytes allocated */

+ 3 - 3
lstring.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lstring.c,v 2.39 2014/04/02 16:44:42 roberto Exp roberto $
+** $Id: lstring.c,v 2.40 2014/06/18 22:59:29 roberto Exp roberto $
 ** String table (keeps all strings handled by Lua)
 ** See Copyright Notice in lua.h
 */
@@ -126,8 +126,8 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
     if (l == ts->tsv.len &&
         (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
       /* found! */
-      if (isdead(g, obj2gco(ts)))  /* dead (but not collected yet)? */
-        changewhite(obj2gco(ts));  /* resurrect it */
+      if (isdead(g, ts2gco(ts)))  /* dead (but not collected yet)? */
+        changewhite(ts2gco(ts));  /* resurrect it */
       return ts;
     }
   }

+ 11 - 10
ltests.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ltests.c,v 2.176 2014/07/17 13:53:37 roberto Exp roberto $
+** $Id: ltests.c,v 2.177 2014/07/17 17:27:49 roberto Exp roberto $
 ** Internal Module for Debugging of the Lua Implementation
 ** See Copyright Notice in lua.h
 */
@@ -207,10 +207,11 @@ static int testobjref (global_State *g, GCObject *f, GCObject *t) {
   return r1;
 }
 
-#define checkobjref(g,f,t)	checkobjref_(g,f,obj2gco(t))
-static void checkobjref_ (global_State *g, GCObject *f, GCObject *t) {
-  lua_assert((t) == NULL || testobjref(g,f,obj2gco(t)));
-}
+#define checkobjref(g,f,t)  \
+	{ if (t) lua_longassert(testobjref(g,f,obj2gco(t))); }
+
+#define checkstrref(g,f,t)  \
+	{ if (t) lua_longassert(testobjref(g,f,ts2gco(t))); }
 
 
 static void checkvalref (global_State *g, GCObject *f, const TValue *t) {
@@ -244,17 +245,17 @@ static void checkproto (global_State *g, Proto *f) {
   int i;
   GCObject *fgc = obj2gco(f);
   checkobjref(g, fgc, f->cache);
-  checkobjref(g, fgc, f->source);
+  checkstrref(g, fgc, f->source);
   for (i=0; i<f->sizek; i++) {
-    if (ttisstring(f->k+i))
-      checkobjref(g, fgc, rawtsvalue(f->k+i));
+    if (ttisstring(f->k + i))
+      checkobjref(g, fgc, tsvalue(f->k + i));
   }
   for (i=0; i<f->sizeupvalues; i++)
-    checkobjref(g, fgc, f->upvalues[i].name);
+    checkstrref(g, fgc, f->upvalues[i].name);
   for (i=0; i<f->sizep; i++)
     checkobjref(g, fgc, f->p[i]);
   for (i=0; i<f->sizelocvars; i++)
-    checkobjref(g, fgc, f->locvars[i].varname);
+    checkstrref(g, fgc, f->locvars[i].varname);
 }
 
 

+ 2 - 2
ltm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ltm.c,v 2.26 2014/04/11 20:17:39 roberto Exp roberto $
+** $Id: ltm.c,v 2.27 2014/06/10 18:53:18 roberto Exp roberto $
 ** Tag methods
 ** See Copyright Notice in lua.h
 */
@@ -45,7 +45,7 @@ void luaT_init (lua_State *L) {
   int i;
   for (i=0; i<TM_N; i++) {
     G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
-    luaC_fix(L, obj2gco(G(L)->tmname[i]));  /* never collect these names */
+    luaC_fix(L, ts2gco(G(L)->tmname[i]));  /* never collect these names */
   }
 }