1 创建的GC对象记录在了哪里?
字符串或table对象创建过程,会看到是通过luaC_newobj函数创建的,在此函数内会将对象指针放入g->allgc链表中。
/*
** create a new collectable object (with given type and size) and link
** it to 'allgc' list.
*/
GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) {
global_State *g = G(L);
GCObject *o = cast(GCObject *, luaM_newobject(L, novariant(tt), sz));
o->marked = luaC_white(g);
o->tt = tt;
o->next = g->allgc;
g->allgc = o;
return o;
}
通过搜索源码,可以看到全部GC对象的创建都会添加到g->allgc链表中。

allgc.png
通过下面的代码可以看到,allgc链表会在垃圾收集的sweep阶段被使用。
/*
** Enter first sweep phase.
** The call to 'sweeplist' tries to make pointer point to an object
** inside the list (instead of to the header), so that the real sweep do
** not need to skip objects created between "now" and the start of the
** real sweep.
*/
static void entersweep (lua_State *L) {
global_State *g = G(L);
g->gcstate = GCSswpallgc;
lua_assert(g->sweepgc == NULL);
g->sweepgc = sweeplist(L, &g->allgc, 1);
}