From 63cb4fbbb961da133c68865845eaf22d9b876700 Mon Sep 17 00:00:00 2001 From: chai Date: Sun, 27 Sep 2020 20:31:53 +0800 Subject: *misc --- src/lua51/lstate.h | 57 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 22 deletions(-) (limited to 'src/lua51/lstate.h') diff --git a/src/lua51/lstate.h b/src/lua51/lstate.h index 8da0640..cb4244b 100644 --- a/src/lua51/lstate.h +++ b/src/lua51/lstate.h @@ -69,7 +69,6 @@ typedef struct CallInfo { } CallInfo; - #define curr_func(L) (clvalue(L->ci->func)) #define ci_func(ci) (clvalue((ci)->func)) #define f_isLua(ci) (!ci_func(ci)->c.isC) @@ -84,46 +83,61 @@ typedef struct global_State { stringtable strt; /* hash table for strings */ lua_Alloc frealloc; /* function to reallocate memory */ void *ud; /* auxiliary data to `frealloc' */ - lu_byte currentwhite;//当前白 - //c 当前的GC状态,有5个,在lgc.h定义 + +//region 与GC相关 + + lu_byte currentwhite;//当前的白色类型,用于lgc.h>luaC_white() + // 当前的GC状态,有5个,在lgc.h定义 lu_byte gcstate; /* state of garbage collector */ - //c strt中字符串散列桶索引,字符串回收阶段每次回收一个散列桶的字符串, + // strt中字符串散列桶索引,字符串回收阶段每次回收一个散列桶的字符串,记录对应的散列桶索引 int sweepstrgc; /* position of sweep in `strt' */ - //c 白色链表。可回收的对象,会在回收阶段被回收 - //c 所有新建的对象都会暂存在这里,但不会被回收,因为lua有双白色机制 + // 所有新建的对象都会暂存在这里,在GC的清理阶段会增量地遍历整个链表。新建对象会加在最*前面*,见luaC_link() GCObject *rootgc; /* list of all collectable objects */ - //c 保存rootgc中当前回收到的位置,下次从这个位置继续回收 + // 保存rootgc中当前回收到的位置,下次从这个位置继续回收 GCObject **sweepgc; /* position of sweep in `rootgc' */ - //c 灰色链表 + // 灰色链表 GCObject *gray; /* list of gray objects */ - //c 不可被打断的对象的灰色链表,比如LUA_THREAD + // 需要一次性扫描处理的,不可被打断的对象的灰色链表,比如LUA_THREAD GCObject *grayagain; /* list of objects to be traversed atomically */ - //c 弱表 + // 弱表 GCObject *weak; /* list of weak tables (to be cleared) */ - //c 有__gc方法的userdata,会在GC阶段调用__gc释放native侧的引用 - GCObject *tmudata; /* last element of list of userdata to be GC */ + // 有__gc方法的userdata,会在GC阶段调用__gc释放native侧的引用。指向链表最后一个 + GCObject *tmudata; /* last element of list of userdata to be GC */ // taggedmethodudata带__gc的udata + +//endregion 与GC有关 + +//region 与内存管理有关的 + Mbuffer buff; /* temporary buffer for string concatentation */ + // GC开始的阈值, lu_mem GCthreshold; - //c 开始进行GC的阈值,当超过这个值时开始GC + // 开始进行GC的阈值,当超过这个值时开始GC lu_mem totalbytes; /* number of bytes currently allocated */ - //c 当前使用的内存大小的估计值 + // 当前使用的内存大小的估计值 lu_mem estimate; /* an estimate of number of bytes actually in use */ + // 待回收的内存大小 lu_mem gcdept; /* how much GC is `behind schedule' */ - //c 一个百分数,控制下一轮GC开始时机,越大,下次gc开始的时间越长 + // 一个百分数,控制下一轮GC开始时机,越大,离下次gc开始的时间越长 int gcpause; /* size of pause between successive GCs */ - //c 控制GC回收速度 + // 控制GC回收速度\gc的粒度 int gcstepmul; /* GC `granularity' */ + +//endregion 与内存管理有关的 + lua_CFunction panic; /* to be called in unprotected errors */ - //c 注册表 - TValue l_registry; - struct lua_State *mainthread; + + TValue l_registry; //全局唯一的注册表,所有lua_State共享一个 + struct lua_State *mainthread; // 主线程对象,不会被回收 UpVal uvhead; /* head of double-linked list of all open upvalues */ + + // 基本类型的元方法 struct Table *mt[NUM_TAGS]; /* metatables for basic types */ TString *tmname[TM_N]; /* array with tag-method names */ + } global_State; -//c StkId引用的永远是lua_State栈上的内容 +//c StkId引用的永远是lua_State栈上的内容,准确来说是base+bias /* ** `per thread' state */ @@ -157,8 +171,7 @@ struct lua_State { int basehookcount; int hookcount; lua_Hook hook; - //c _G global table - TValue l_gt; /* table of globals */ + TValue l_gt; /* table of globals */ //全局表 _G global table TValue env; /* temporary place for environments */ GCObject *openupval; /* list of open upvalues in this stack */ GCObject *gclist; -- cgit v1.1-26-g67d0