1 Star 1 Fork 0

蒙海康/Lua-5.1.1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lstate.c 8.10 KB
一键复制 编辑 原始数据 按行查看 历史
蒙海康 提交于 2024-04-05 20:04 . add notes
/*
** $Id: lstate.c,v 2.35 2005/10/06 20:46:25 roberto Exp roberto $
** Global State
** See Copyright Notice in lua.h
*/
#include <stddef.h>
#define lstate_c
#define LUA_CORE
#include "lua.h"
#include "ldebug.h"
#include "ldo.h"
#include "lfunc.h"
#include "lgc.h"
#include "llex.h"
#include "lmem.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
#define state_size(x) (sizeof(x) + LUAI_EXTRASPACE)
#define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE)
#define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE))
/*
** Main thread combines a thread state and the global state
*/
typedef struct LG {
lua_State l;
global_State g;
} LG;
// 以L作为操作的发起方,初始化L1的 CallInfo array、虚拟栈
static void stack_init (lua_State *L1, lua_State *L) {
/* initialize CallInfo array */
L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo);
L1->ci = L1->base_ci;
L1->size_ci = BASIC_CI_SIZE;
L1->end_ci = L1->base_ci + L1->size_ci - 1;
/* initialize stack array */
L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue);
L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK;
L1->top = L1->stack;
L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1;
/* initialize first ci */
// CallInfo array[0].func = 虚拟栈首个槽位的地址
L1->ci->func = L1->top;
// CallInfo array[0].func = 虚拟栈首个槽位的地址 = lua nil, top指针上移
setnilvalue(L1->top++); /* `function' entry for this `ci' */
// 此时栈底为nil,top指向自栈底向上的第2个槽位,两段连续赋值,让 CallInfo array[0].base 和 lua_State.base都指向虚拟栈的第2个槽位
L1->base = L1->ci->base = L1->top;
// CallInfo array[0].top = 虚拟栈自底向上的第22个槽位
L1->ci->top = L1->top + LUA_MINSTACK;
// CallInfo array[0].func = lua nil,且虚拟栈的第一个槽位不会被使用到,它就是永远空着在那,原因和“用一个虚节点作为链表头”类似
// 当执行某个函数层级时报错,要将异常上抛,我们就不用判断当前是否已经处于最上的函数层级中,有虚节点存在,那么代码实现上就无需多一步判断,直接上抛即可
}
// 回收thread的虚拟栈,和CallInfo数组
static void freestack (lua_State *L, lua_State *L1) {
luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo);
luaM_freearray(L, L1->stack, L1->stacksize, TValue);
}
/*
** open parts that may cause memory-allocation errors
** 执行可能引起内存分配错误的语句(这里通常就是内存不够才会报错啦)
*/
static void f_luaopen (lua_State *L, void *ud) {
global_State *g = G(L);
UNUSED(ud);
stack_init(L, L); /* init stack */
// 创建2个lua table,一个作为global全局表(_G),一个作为global_State的注册表
sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */
sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */
// 初始化lua vm内部化字符串所用的hash数组
luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
// 初始化global_State的tmname, 一组短字符串,TString,元表key值,不会被gc流程collect
luaT_init(L);
// 构建一组TString,lua语法中的关键字,不会被gc流程collect
luaX_init(L);
// 内部化字符串 MEMERRMSG
luaS_fix(luaS_newliteral(L, MEMERRMSG));
// 初始的gc门槛?
g->GCthreshold = 4*g->totalbytes;
}
static void preinit_state (lua_State *L, global_State *g) {
G(L) = g;
L->stack = NULL;
L->stacksize = 0;
L->errorJmp = NULL;
L->hook = NULL;
L->hookmask = 0;
L->basehookcount = 0;
L->allowhook = 1;
resethookcount(L);
L->openupval = NULL;
L->size_ci = 0;
L->nCcalls = 0;
L->status = 0;
L->base_ci = L->ci = NULL;
L->savedpc = NULL;
L->errfunc = 0;
setnilvalue(gt(L));
}
static void close_state (lua_State *L) {
global_State *g = G(L);
luaF_close(L, L->stack); /* close all upvalues for this thread */
luaC_freeall(L); /* collect all objects */
lua_assert(g->rootgc == obj2gco(L));
lua_assert(g->strt.nuse == 0);
// 回收掉字符串内部化数组的内存占用
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
// 回收字符串拼接缓冲区
luaZ_freebuffer(L, &g->buff);
// 回收mainthread的虚拟栈,和CallInfo数组
freestack(L, L);
lua_assert(g->totalbytes == sizeof(LG));
// 最后回收掉struct mainthread和global_State
(*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0);
}
lua_State *luaE_newthread (lua_State *L) {
lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State)));
luaC_link(L, obj2gco(L1), LUA_TTHREAD);
preinit_state(L1, G(L));
stack_init(L1, L); /* init stack */
setobj2n(L, gt(L1), gt(L)); /* share table of globals */
L1->hookmask = L->hookmask;
L1->basehookcount = L->basehookcount;
L1->hook = L->hook;
resethookcount(L1);
lua_assert(iswhite(obj2gco(L1)));
return L1;
}
// 回收1个lua_State(协程)
void luaE_freethread (lua_State *L, lua_State *L1) {
luaF_close(L1, L1->stack); /* close all upvalues for this thread */
lua_assert(L1->openupval == NULL);
luai_userstatefree(L1);
// 回收虚拟栈和CallInfo数组
freestack(L, L1);
// 回收掉lua_State结构
luaM_freemem(L, fromstate(L1), state_size(lua_State));
}
LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
int i;
lua_State *L; // mainthread
global_State *g;
// 申请堆内存来放struct LG实例
void *l = (*f)(ud, NULL, 0, state_size(LG));
if (l == NULL) return NULL;
L = tostate(l); // 内存地址偏移,令L指向LG实例l域的起始地址
g = &((LG *)L)->g; // LG实例l域的起始地址 = LG实例的起始地址,所以 (LG *)L 的取址写法是ok的
L->next = NULL;
L->tt = LUA_TTHREAD;
g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); // 初始currentwhite为WHITE0BIT,0010 0001
L->marked = luaC_white(g);
set2bits(L->marked, FIXEDBIT, SFIXEDBIT);
preinit_state(L, g); // 初始化 mainthread
g->frealloc = f;
g->ud = ud;
g->mainthread = L;
g->uvhead.u.l.prev = &g->uvhead;
g->uvhead.u.l.next = &g->uvhead;
g->GCthreshold = 0; /* mark it as unfinished state */
g->strt.size = 0;
g->strt.nuse = 0;
g->strt.hash = NULL;
setnilvalue(registry(L));
luaZ_initbuffer(L, &g->buff);
g->panic = NULL;
g->gcstate = GCSpause; // gc流程的初始状态
g->rootgc = obj2gco(L); // mainthread被作为gc链表的链表头
g->sweepstrgc = 0;
g->sweepgc = &g->rootgc;
g->gray = NULL;
g->grayagain = NULL;
g->weak = NULL;
g->tmudata = NULL;
g->totalbytes = sizeof(LG);
g->gcpause = LUAI_GCPAUSE;
g->gcstepmul = LUAI_GCMUL;
g->gcdept = 0;
for (i=0; i<NUM_TAGS; i++) g->mt[i] = NULL; // 把整个数组置NULL而已
// 初始,maintread的errorJmp = NULL,在保护模式下执行f_luaopen;luaD_rawrunprotected()返回0表示成功
if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
/* memory allocation error: free partial state */
close_state(L);
L = NULL;
}
else
luai_userstateopen(L); // 允许用户进行自定义的接口,具体看注释吧,作者说得很清楚了hh
return L;
}
static void callallgcTM (lua_State *L, void *ud) {
UNUSED(ud);
luaC_callGCTM(L); /* call GC metamethods for all udata */
}
LUA_API void lua_close (lua_State *L) {
L = G(L)->mainthread; /* only the main thread can be closed */
lua_lock(L);
// 又是upvalues,立个flag在这先
luaF_close(L, L->stack); /* close all upvalues for this thread */
// 所有的full userdata都需要回收了
luaC_separateudata(L, 1); /* separate udata that have GC metamethods */
L->errfunc = 0; /* no error function during GC metamethods */
// 以保护模式执行所有__gc元方法,因为__gc元方法执行过程中可能因为报错抛出异常而中断掉callallgcTM的执行,所以在有报错的时候while重试,继续执行
do { /* repeat until no more errors */
L->ci = L->base_ci;
L->base = L->top = L->ci->base;
L->nCcalls = 0;
} while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0);
lua_assert(G(L)->tmudata == NULL);
luai_userstateclose(L); // 用户自定行为接口
close_state(L);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/menghaikang/Lua-5.1.1.git
git@gitee.com:menghaikang/Lua-5.1.1.git
menghaikang
Lua-5.1.1
Lua-5.1.1
main

搜索帮助

0d507c66 1850385 C8b1a773 1850385