代码拉取完成,页面将自动刷新
/*
** $Id: lstate.h,v 2.133.1.1 2017/04/19 17:39:34 roberto Exp $
** Global State
** See Copyright Notice in lua.h
*/
#ifndef lstate_h
#define lstate_h
#include "lua.h"
#include "lobject.h"
#include "ltm.h"
#include "lzio.h"
/*
** Some notes about garbage-collected objects: All objects in Lua must
** be kept somehow accessible until being freed, so all objects always
** belong to one (and only one) of these lists, using field 'next' of
** the 'CommonHeader' for the link:
**
** 'allgc': all objects not marked for finalization;
** 'finobj': all objects marked for finalization;
** 'tobefnz': all objects ready to be finalized;
** 'fixedgc': all objects that are not to be collected (currently
** only small strings, such as reserved words).
**
** Moreover, there is another set of lists that control gray objects.
** These lists are linked by fields 'gclist'. (All objects that
** can become gray have such a field. The field is not the same
** in all objects, but it always has this name.) Any gray object
** must belong to one of these lists, and all objects in these lists
** must be gray:
**
** 'gray': regular gray objects, still waiting to be visited.
** 'grayagain': objects that must be revisited at the atomic phase.
** That includes
** - black objects got in a write barrier;
** - all kinds of weak tables during propagation phase;
** - all threads.
** 'weak': tables with weak values to be cleared;
** 'ephemeron': ephemeron tables with white->white entries;
** 'allweak': tables with weak keys and/or weak values to be cleared.
** The last three lists are used only during the atomic phase.
*/
struct lua_longjmp; /* defined in ldo.c */
/*
** Atomic type (relative to signals) to better ensure that 'lua_sethook'
** is thread safe
*/
#if !defined(l_signalT)
#include <signal.h>
#define l_signalT sig_atomic_t
#endif
/* extra stack space to handle TM calls and some other extras */
#define EXTRA_STACK 5
#define BASIC_STACK_SIZE (2*LUA_MINSTACK)
/* kinds of Garbage Collection */
#define KGC_NORMAL 0
#define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */
typedef struct stringtable {
TString **hash;
int nuse; /* number of elements */
int size; // 散列表数组的长度
} stringtable;
/*
** Information about a call.
** When a thread yields, 'func' is adjusted to pretend that the
** top function has only the yielded values in its stack; in that
** case, the actual 'func' value is saved in field 'extra'.
** When a function calls another with a continuation, 'extra' keeps
** the function index so that, in case of errors, the continuation
** function can be called with the correct top.
*/
typedef struct CallInfo {
// 该栈位置保存调用关联的函数所在栈位置引用
StkId func; /* function index in the stack */
// 该函数的栈顶引用,[func, top]就是这个函数栈范围
StkId top; /* top for this function */
// 调用的双向链表
struct CallInfo *previous, *next; /* dynamic call link */
union {
// Lua函数
struct { /* only for Lua functions */
// 栈基址,base往下这部分为函数的可变参数,base往上为函数的固定参数,和本地变量
StkId base; /* base for this function */
// 正在执行指令
const Instruction *savedpc;
} l;
// C函数, 延续函数的信息
struct { /* only for C functions */
// 延续函数
lua_KFunction k; /* continuation in case of yields */
ptrdiff_t old_errfunc;
// 延续函数环境
lua_KContext ctx; /* context info. in case of yields */
} c;
} u;
ptrdiff_t extra;
short nresults; /* expected number of results from this function */
// 调用状态
unsigned short callstatus;
} CallInfo;
/*
** Bits in CallInfo status
*/
#define CIST_OAH (1<<0) /* original value of 'allowhook' */
#define CIST_LUA (1<<1) /* call is running a Lua function */
#define CIST_HOOKED (1<<2) /* call is running a debug hook */
#define CIST_FRESH (1<<3) /* call is running on a fresh invocation
of luaV_execute */
#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */
#define CIST_TAIL (1<<5) /* call was tail called */
#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
#define CIST_LEQ (1<<7) /* using __lt for __le */
#define CIST_FIN (1<<8) /* call is running a finalizer 调用一个 __gc 元方法 */
#define isLua(ci) ((ci)->callstatus & CIST_LUA)
/* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */
#define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v))
#define getoah(st) ((st) & CIST_OAH)
/*
** 'global state', shared by all threads of this state
*/
typedef struct global_State {
// 内存分配函数,以及关联的用户数据
lua_Alloc frealloc; /* function to reallocate memory */
void *ud; /* auxiliary data to 'frealloc' */
// Lua当前已分配的内存总量 - GCdebt,这个词有歧义,并不准确反映当前内存使用量
// 新分配内存增加的是 GCdebt, 这个值更像为了配合 GCdebt 来计算 gettotalbytes
l_mem totalbytes; /* number of bytes currently allocated - GCdebt */
// 债务内存量,即GC要做的工作量。其值为正时,表示当前 GC“欠债”,需要进行回收以恢复内存平衡;其为负数时,则表示没有垃圾回收工作需要进行。
l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
lu_mem GCmemtrav; /* memory traversed by the GC gc扫描过的内存量(工作量),统计用的中间量 */
lu_mem GCestimate; /* an estimate of the non-garbage memory in use 上轮 GC 回收完成时的内存占用估算值,通常等于总内存大小 totalbytes 加上一定的缓冲区值。*/
// 短字符串哈希表
stringtable strt; /* hash table for strings */
// 全局注册表
TValue l_registry;
// 随机函数种子
unsigned int seed; /* randomized seed for hashes */
lu_byte currentwhite;
// 当前的gc阶段
lu_byte gcstate; /* state of garbage collector */
lu_byte gckind; /* kind of GC running */
lu_byte gcrunning; /* true if GC is running 开启 GC ? */
GCObject *allgc; /* list of all collectable objects */
GCObject **sweepgc; /* current position of sweep in list */
GCObject *finobj; /* list of collectable objects with finalizers */
GCObject *gray; /* list of gray objects */
GCObject *grayagain; /* list of objects to be traversed atomically */
GCObject *weak; /* list of tables with weak values */
GCObject *ephemeron; /* list of ephemeron tables (weak keys) */
GCObject *allweak; /* list of all-weak tables */
GCObject *tobefnz; /* list of userdata to be GC */
GCObject *fixedgc; /* list of objects not to be collected */
struct lua_State *twups; /* list of threads with open upvalues 有 upvalue 的 lua_State */
unsigned int gcfinnum; /* number of finalizers to call in each GC step */
int gcpause; /* size of pause between successive GCs 默认值200 */
int gcstepmul; /* GC 'granularity' 垃圾回收的“步长倍率”,表示单步回收工作的规模。倍率值越高,singlestep 执行得越多。默认 200 */
// 终止函数
lua_CFunction panic; /* to be called in unprotected errors */
// 主线程
struct lua_State *mainthread;
// 版本号
const lua_Number *version; /* pointer to version number */
// 错误消息
TString *memerrmsg; /* memory-error message */
// 元方法名,初始化在luaT_init
TString *tmname[TM_N]; /* array with tag-method names */
// 基本类型的元方法,表和userdata之外的元表放这儿
struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */
// 字符串的二级缓存容器
TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */
} global_State;
/*
** 'per thread' state
*/
struct lua_State {
// lua_State 也是一个GC obj
CommonHeader;
// CallInfo数量,一个CallInfo代表一层函数调用
unsigned short nci; /* number of items in 'ci' list */
// 线程状态
lu_byte status;
// 栈顶地址,StkId是TValue指针
StkId top; /* first free slot in the stack */
// 全局状态
global_State *l_G;
// 当前函数的调用信息
CallInfo *ci; /* call info for current function */
const Instruction *oldpc; /* last pc traced */
// 栈的最后地址
StkId stack_last; /* last free slot in the stack */
// 栈的起始地址,可以看作是栈数组
StkId stack; /* stack base */
UpVal *openupval; /* list of open upvalues in this stack */
GCObject *gclist;
struct lua_State *twups; /* list of threads with open upvalues */
// 错误处理链表:当前的恢复点,看 luaD_rawrunprotected
struct lua_longjmp *errorJmp; /* current error recover point */
// 初始调用信息
CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
volatile lua_Hook hook;
ptrdiff_t errfunc; /* current error handling function (stack index) */
// 栈大小
int stacksize;
int basehookcount;
int hookcount;
unsigned short nny; /* number of non-yieldable calls in stack */
unsigned short nCcalls; /* number of nested C calls */
l_signalT hookmask;
lu_byte allowhook;
};
#define G(L) (L->l_G)
/*
** 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 */
};
#define cast_u(o) cast(union GCUnion *, (o))
/* macros to convert a GCObject into a specific value */
// 这些宏是将对象由 GCObject 强转为实际的类型
#define gco2ts(o) \
check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts))
#define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u))
#define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l))
#define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c))
#define gco2cl(o) \
check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl))
#define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h))
#define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p))
#define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th))
/* macro to convert a Lua object into a GCObject */
// 和上面相反,对象强转为 GCObject 类型
#define obj2gco(v) \
check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc)))
/* actual number of total bytes allocated */
#define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt)
LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
LUAI_FUNC void luaE_freeCI (lua_State *L);
LUAI_FUNC void luaE_shrinkCI (lua_State *L);
#endif
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。