LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { int i; lua_State *L; global_State *g; void *l = (*f)(ud, NULL, 0, state_size(LG)); if (l == NULL) return NULL; L = tostate(l); g = &((LG *)L)->g; L->next = NULL; L->tt = LUA_TTHREAD; g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); L->marked = luaC_white(g); set2bits(L->marked, FIXEDBIT, SFIXEDBIT); preinit_state(L, g); 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; g->rootgc = obj2gco(L); 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; if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { /* memory allocation error: free partial state */ close_state(L); L = NULL; } else luai_userstateopen(L); return L; }
LUA_API void lua_close (lua_State *L) { L = G(L)->mainthread; /* only the main thread can be closed */ lua_lock(L); luaF_close(L, L->stack); /* close all upvalues for this thread */ luaC_separateudata(L, 1); /* separate udata that have GC metamethods */ L->errfunc = 0; /* no error function during GC metamethods */ do { /* repeat until no more errors */ L->ci = L->base_ci; L->base = L->top = L->ci->base; L->nCcalls = L->baseCcalls = 0; } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0); lua_assert(G(L)->tmudata == NULL); luai_userstateclose(L); close_state(L); }
static void cmd_list(const char *pf, char **args) { const char *name = args[0]; struct string_list *list; int i; close_state(); if (no_syntax()) return; list = find_string_list(current_syntax, name); if (list == NULL) { list = xnew0(struct string_list, 1); list->name = xstrdup(name); ptr_array_add(¤t_syntax->string_lists, list); } else if (list->defined) {
LUA_API lua_State *lua_open (void) { lua_State *L = mallocstate(NULL); if (L) { /* allocation OK? */ L->tt = LUA_TTHREAD; L->marked = 0; L->next = L->gclist = NULL; preinit_state(L); L->l_G = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { /* memory allocation error: free partial state */ close_state(L); L = NULL; } } lua_userstateopen(L); return L; }
/** * 创建主线程 和 gloable_state, 初始化其结构; 接着开始进行分配内存, 若分配 * 内存出错, 则 close 该 lua_state (以确保释放已分配的内存), 并返回 NULL. * * luaL_newstate 函数封装了这个函数,lauxlib.c 中提供了一个 lua_Alloc 函数 */ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { int i; lua_State *L; global_State *g; LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG))); if (l == NULL) return NULL; L = &l->l.l; g = &l->g; L->next = NULL; L->tt = LUA_TTHREAD; g->currentwhite = bitmask(WHITE0BIT); L->marked = luaC_white(g); preinit_thread(L, g); g->frealloc = f; g->ud = ud; g->mainthread = L; g->seed = makeseed(L); g->gcrunning = 0; /* no GC while building state */ g->GCestimate = 0; g->strt.size = g->strt.nuse = 0; g->strt.hash = NULL; setnilvalue(&g->l_registry); luaZ_initbuffer(L, &g->buff); g->panic = NULL; g->version = NULL; g->gcstate = GCSpause; g->gckind = KGC_NORMAL; g->allgc = g->finobj = g->tobefnz = g->fixedgc = NULL; g->sweepgc = NULL; g->gray = g->grayagain = NULL; g->weak = g->ephemeron = g->allweak = NULL; g->twups = NULL; g->totalbytes = sizeof(LG); g->GCdebt = 0; g->gcfinnum = 0; g->gcpause = LUAI_GCPAUSE; g->gcstepmul = LUAI_GCMUL; for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { /* memory allocation error: free partial state */ Dlog("lua state init error, free partial state.") close_state(L); L = NULL; } return L; }
LUA_API lua_State *lua_open (void) { lua_State *L; global_State globalState; lua_State luaState; #ifdef _DEBUG luaState.allocName = "Lua_lua_State"; #endif _DEBUG luaState.l_G = &globalState; globalState.reallocFunc = luaHelper_Realloc; globalState.freeFunc = luaHelper_Free; globalState.memData = luaHelper_memData; globalState.nblocks = sizeof(lua_State) + sizeof(global_State); // Bogus. L = mallocstate(&luaState); if (L) { /* allocation OK? */ L->tt = LUA_TTHREAD; L->marked = 0; L->next = NULL; #if LUA_REFCOUNT L->prev = NULL; L->gclist_head.next = (GCObject*)&L->gclist_tail; L->gclist_head.prev = NULL; L->gclist_tail.next = NULL; L->gclist_tail.prev = (GCObject*)&L->gclist_head; L->gclist_head.tt = LUA_TNIL; L->gclist_head.marked = 0; L->gclist_head.ref = 0; L->gclist_tail.tt = LUA_TNIL; L->gclist_tail.marked = 0; L->gclist_tail.ref = 0; L->ref = 0; #else !LUA_REFCOUNT L->gclist = NULL; #endif LUA_REFCOUNT preinit_state(L); L->l_G = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { /* memory allocation error: free partial state */ close_state(L); L = NULL; } } lua_userstateopen(L); return L; }
/** * Update the current service list with data from the state file. We * do *only* change services found in *both* the monitrc file and in * the state file. The algorithm: * * Assume the control file was changed and a new service (B) was added * so the monitrc file now contains the services: A B and C. The * running monit daemon only knows the services A and C. Upon restart * after a crash the monit daemon first read the monitrc file and * creates the service list structure with A B and C. We then read the * state file and update the service A and C since they are found in * the state file, B is not found in this file and therefore not * changed. * * The same strategy is used if a service was removed, e.g. if the * service A was removed from monitrc; when reading the state file, * service A is not found in the current service list (the list is * always generated from monitrc) and therefore A is simply discarded. * * Finally, after the monit service state is updated this function * writes the new state file. */ void State_update() { int i; int l= 0; State_T s; FILE *S= NULL; Service_T service; int has_error= FALSE; if(! (S= open_state("r"))) return; errno= 0; if(fread(&l, 1, sizeof (int), S) != sizeof(int)) { LogError("%s: Unable to read monit state information from '%s'\n", prog, Run.statefile); has_error= TRUE; goto error; } if(l > 0) { for(i=0; i<l; i++) { if(fread(&s, 1, sizeof(State_T), S) != sizeof(State_T)) { LogError("%s: An error occured when updating monit state information\n", prog); has_error= TRUE; goto error; } if((service= Util_getService(s.name))) { update_service_state(service, &s); } } } error: close_state(S); if(!has_error) State_save(); }
LUA_API void lua_close (lua_State *L) { L = G(L)->mainthread; /* only the main thread can be closed */ lua_lock(L); close_state(L); }
KILLA_API void killa_close (killa_State *L) { L = KILLA_G(L)->mainthread; /* only the main thread can be closed */ killa_lock(L); killai_userstateclose(L); close_state(L); }
LUA_API void LUA_close (LUA_State *L) { L = G(L)->mainthread; /* only the main thread can be closed */ LUA_lock(L); LUAi_userstateclose(L); close_state(L); }