From 77ac95b9985f5669d6659bfb54728786d28c2ef0 Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 20 Jul 2020 09:42:30 +0800 Subject: *misc --- src/lua51/lstate.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) (limited to 'src/lua51/lstate.h') diff --git a/src/lua51/lstate.h b/src/lua51/lstate.h index 3bc575b..1ec30a8 100644 --- a/src/lua51/lstate.h +++ b/src/lua51/lstate.h @@ -35,20 +35,31 @@ struct lua_longjmp; /* defined in ldo.c */ +//c 散列桶 +//c 全局存放字符串的地方。如果一个字符串在此存在,不用重新生成。 +//c 如果表扩张的太厉害,每个桶的字符串太多,会进行一次rehash,重新分配每个桶的数据量 +//c rehash在lstring.c -> luaS_resize typedef struct stringtable { - GCObject **hash; - lu_int32 nuse; /* number of elements */ - int size; + GCObject **hash; //c 字符串,因为是散列桶,所以是** + lu_int32 nuse; //c 桶用到的容量,因为不一定size都用到了 /* number of elements */ + int size; //c 桶的总容量,是常值 + // nuse和size是用来动态控制桶容量的关键 } stringtable; +//c 当前函数的调用信息,和lua_State的调用部分类似 +//c 都有top, base俩个与栈相关的成员 +//c StkId引用的永远是lua_State栈上的内容 /* ** informations about a call */ typedef struct CallInfo { + //c 当前调用的base值,即在栈上操作的开始位置 StkId base; /* base for this function */ + //c 函数原型在lua_State数据栈上的位置 StkId func; /* function index in the stack */ StkId top; /* top for this function */ + //c 当前执行到的指令位置 const Instruction *savedpc; int nresults; /* expected number of results from this function */ int tailcalls; /* number of tail calls lost under this entry */ @@ -61,31 +72,46 @@ typedef struct CallInfo { #define f_isLua(ci) (!ci_func(ci)->c.isC) #define isLua(ci) (ttisfunction((ci)->func) && f_isLua(ci)) - +//c 当前虚拟机所有协程共用的内容 /* ** `global state', shared by all threads of this state */ typedef struct global_State { + //c string table,开散列表,存储所有的字符串 stringtable strt; /* hash table for strings */ lua_Alloc frealloc; /* function to reallocate memory */ void *ud; /* auxiliary data to `frealloc' */ - lu_byte currentwhite; + lu_byte currentwhite;//当前白 + //c 当前的GC状态,有5个,在lgc.h定义 lu_byte gcstate; /* state of garbage collector */ + //c strt中字符串散列桶索引,字符串回收阶段每次回收一个散列桶的字符串, int sweepstrgc; /* position of sweep in `strt' */ + //c 白色链表。可回收的对象,会在回收阶段被回收 + //c 所有新建的对象都会暂存在这里,但不会被回收,因为lua有双白色机制 GCObject *rootgc; /* list of all collectable objects */ + //c 保存rootgc中当前回收到的位置,下次从这个位置继续回收 GCObject **sweepgc; /* position of sweep in `rootgc' */ + //c 灰色链表 GCObject *gray; /* list of gray objects */ + //c 不可被打断的对象的灰色链表 GCObject *grayagain; /* list of objects to be traversed atomically */ + //c 弱表 GCObject *weak; /* list of weak tables (to be cleared) */ + //c 有__gc方法的userdata GCObject *tmudata; /* last element of list of userdata to be GC */ Mbuffer buff; /* temporary buffer for string concatentation */ lu_mem GCthreshold; + //c 开始进行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开始时机 int gcpause; /* size of pause between successive GCs */ + //c 控制GC回收速度 int gcstepmul; /* GC `granularity' */ lua_CFunction panic; /* to be called in unprotected errors */ + //c 注册表 TValue l_registry; struct lua_State *mainthread; UpVal uvhead; /* head of double-linked list of all open upvalues */ @@ -94,20 +120,29 @@ typedef struct global_State { } global_State; +//c 一个lua_state,可以看做是一个 +//c StkId引用的永远是lua_State栈上的内容 /* ** `per thread' state */ struct lua_State { CommonHeader; lu_byte status; + //c 当前栈的下一个可用位置 StkId top; /* first free slot in the stack */ + //c 当前*函数栈*的基地址,给某个函数用,在luad_precall函数中设置L->base = ci->base = ci->func + 1; StkId base; /* base of current function */ global_State *l_G; + //c 当前函数的调用信息,是base_ci数组中的某个 CallInfo *ci; /* call info for current function */ const Instruction *savedpc; /* `savedpc' of current function */ + //c lua_State数据栈的终点 StkId stack_last; /* last free slot in the stack */ + //c 数据栈(栈数组的起始地址)(数据栈的起始点) StkId stack; /* stack base */ + //c callinfo数组的终点 CallInfo *end_ci; /* points after end of ci array*/ + //c callinfo 数组, 沿着base_ci遍历可以得到完整的lua调用链 CallInfo *base_ci; /* array of CallInfo's */ int stacksize; int size_ci; /* size of array `base_ci' */ @@ -118,6 +153,7 @@ struct lua_State { int basehookcount; int hookcount; lua_Hook hook; + //c _G global table TValue l_gt; /* table of globals */ TValue env; /* temporary place for environments */ GCObject *openupval; /* list of open upvalues in this stack */ @@ -130,6 +166,10 @@ struct lua_State { #define G(L) (L->l_G) +//c 需要GC的类型基类,通过GCObject能够引用到GCheader中定义的/ +//c #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked +//c 这种写法是一种实现C继承的方式 +//c 每个 /* ** Union of all collectable objects */ @@ -144,7 +184,7 @@ union GCObject { struct lua_State th; /* thread */ }; - +//c TValue\GCObject 获取union中的内容 /* macros to convert a GCObject into a specific value */ #define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts)) #define gco2ts(o) (&rawgco2ts(o)->tsv) -- cgit v1.1-26-g67d0