lua 5.3.4 GC管理對象類型的變化

Lua 5.1.4

判斷是否需要GC:

#define ttype(o)    ((o)->tt)
#define iscollectable(o)    (ttype(o) >= LUA_TSTRING)

GC對象GCObject union:

/*
** Union of all collectable objects
*/
union GCObject {
  GCheader gch;
  union TString ts;
  union Udata u;
  union Closure cl;
  struct Table h;
  struct Proto p;
  struct UpVal uv;
  struct lua_State th;  /* thread */
};

作為 GC 對象被虛擬機的 標記-清除 GC 所管理的類型有:

  1. string
  2. userdata
  3. Closure(function)
  4. table
  5. thread
  6. Proto
  7. UpVal

Lua 5.3.4

#define TValuefields    Value value_; int tt_

struct lua_TValue {
  TValuefields;
};

typedef struct lua_TValue TValue;

==> TValue =

struct lua_TValue {
      Value value_; 
      int tt_;
    };

其中,tt_是tag type的簡寫,復合類型,用來表示類型,一共包含三個部分,分別是:

/*
** tags for Tagged Values have the following use of bits:
** bits 0-3: actual tag (a LUA_T* value) 
** bits 4-5: variant bits 
** bit 6: whether value is collectable 
*/
  1. 0-3表示大類型
  2. 4-5表示表示類型的變體,例如:字符串LUA_TSTRING有兩種變體(短字符串:LUA_TSHRSTR和長字符串:LUA_TLNGSTR)
  3. 6表示是否可以垃圾回收

標記為需標記-清除 GC 所管理的對象:

#define BIT_ISCOLLECTABLE   (1 << 6) = 64 = 0100 0000

/* mark a tag as collectable */
#define ctb(t)          ((t) | BIT_ISCOLLECTABLE)

判斷是否需要GC:

#define rttype(o)   ((o)->tt_)

#define iscollectable(o)    (rttype(o) & BIT_ISCOLLECTABLE)

GC對象union:

/*
** Union of all collectable objects (only for conversions)
*/
union GCUnion {
  GCObject gc;  /* common header */
  struct TString ts;
  struct Udata u;
  union Closure cl;
  struct Table h;
  struct Proto p;
  struct lua_State th;  /* thread */
};

作為 GC 對象被虛擬機的 標記-清除 GC 所管理的類型有:

  1. string
  2. userdata
  3. Closure(function)
  4. table
  5. thread
  6. Proto

要注意的是:

UpVal 對象不再作為 GC 對象被虛擬機的 標記-清除GC 所管理,而是單獨使用引用計數的方法管理。

鏈接:UpVal變為引用計數

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容