rlm@1: /* rlm@1: ** $Id: lstate.c,v 2.36.1.2 2008/01/03 15:20:39 roberto Exp $ rlm@1: ** Global State rlm@1: ** See Copyright Notice in lua.h rlm@1: */ rlm@1: rlm@1: rlm@1: #include rlm@1: rlm@1: #define lstate_c rlm@1: #define LUA_CORE rlm@1: rlm@1: #include "lua.h" rlm@1: rlm@1: #include "ldebug.h" rlm@1: #include "ldo.h" rlm@1: #include "lfunc.h" rlm@1: #include "lgc.h" rlm@1: #include "llex.h" rlm@1: #include "lmem.h" rlm@1: #include "lstate.h" rlm@1: #include "lstring.h" rlm@1: #include "ltable.h" rlm@1: #include "ltm.h" rlm@1: rlm@1: rlm@1: #define state_size(x) (sizeof(x) + LUAI_EXTRASPACE) rlm@1: #define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE) rlm@1: #define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE)) rlm@1: rlm@1: rlm@1: /* rlm@1: ** Main thread combines a thread state and the global state rlm@1: */ rlm@1: typedef struct LG { rlm@1: lua_State l; rlm@1: global_State g; rlm@1: } LG; rlm@1: rlm@1: rlm@1: rlm@1: static void stack_init (lua_State *L1, lua_State *L) { rlm@1: /* initialize CallInfo array */ rlm@1: L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo); rlm@1: L1->ci = L1->base_ci; rlm@1: L1->size_ci = BASIC_CI_SIZE; rlm@1: L1->end_ci = L1->base_ci + L1->size_ci - 1; rlm@1: /* initialize stack array */ rlm@1: L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue); rlm@1: L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK; rlm@1: L1->top = L1->stack; rlm@1: L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1; rlm@1: /* initialize first ci */ rlm@1: L1->ci->func = L1->top; rlm@1: setnilvalue(L1->top++); /* `function' entry for this `ci' */ rlm@1: L1->base = L1->ci->base = L1->top; rlm@1: L1->ci->top = L1->top + LUA_MINSTACK; rlm@1: } rlm@1: rlm@1: rlm@1: static void freestack (lua_State *L, lua_State *L1) { rlm@1: luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo); rlm@1: luaM_freearray(L, L1->stack, L1->stacksize, TValue); rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** open parts that may cause memory-allocation errors rlm@1: */ rlm@1: static void f_luaopen (lua_State *L, void *ud) { rlm@1: global_State *g = G(L); rlm@1: UNUSED(ud); rlm@1: stack_init(L, L); /* init stack */ rlm@1: sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */ rlm@1: sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */ rlm@1: luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ rlm@1: luaT_init(L); rlm@1: luaX_init(L); rlm@1: luaS_fix(luaS_newliteral(L, MEMERRMSG)); rlm@1: g->GCthreshold = 4*g->totalbytes; rlm@1: } rlm@1: rlm@1: rlm@1: static void preinit_state (lua_State *L, global_State *g) { rlm@1: G(L) = g; rlm@1: L->stack = NULL; rlm@1: L->stacksize = 0; rlm@1: L->errorJmp = NULL; rlm@1: L->hook = NULL; rlm@1: L->hookmask = 0; rlm@1: L->basehookcount = 0; rlm@1: L->allowhook = 1; rlm@1: resethookcount(L); rlm@1: L->openupval = NULL; rlm@1: L->size_ci = 0; rlm@1: L->nCcalls = L->baseCcalls = 0; rlm@1: L->status = 0; rlm@1: L->base_ci = L->ci = NULL; rlm@1: L->savedpc = NULL; rlm@1: L->errfunc = 0; rlm@1: setnilvalue(gt(L)); rlm@1: } rlm@1: rlm@1: rlm@1: static void close_state (lua_State *L) { rlm@1: global_State *g = G(L); rlm@1: luaF_close(L, L->stack); /* close all upvalues for this thread */ rlm@1: luaC_freeall(L); /* collect all objects */ rlm@1: lua_assert(g->rootgc == obj2gco(L)); rlm@1: lua_assert(g->strt.nuse == 0); rlm@1: luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *); rlm@1: luaZ_freebuffer(L, &g->buff); rlm@1: freestack(L, L); rlm@1: lua_assert(g->totalbytes == sizeof(LG)); rlm@1: (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0); rlm@1: } rlm@1: rlm@1: rlm@1: lua_State *luaE_newthread (lua_State *L) { rlm@1: lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State))); rlm@1: luaC_link(L, obj2gco(L1), LUA_TTHREAD); rlm@1: preinit_state(L1, G(L)); rlm@1: stack_init(L1, L); /* init stack */ rlm@1: setobj2n(L, gt(L1), gt(L)); /* share table of globals */ rlm@1: L1->hookmask = L->hookmask; rlm@1: L1->basehookcount = L->basehookcount; rlm@1: L1->hook = L->hook; rlm@1: resethookcount(L1); rlm@1: lua_assert(iswhite(obj2gco(L1))); rlm@1: return L1; rlm@1: } rlm@1: rlm@1: rlm@1: void luaE_freethread (lua_State *L, lua_State *L1) { rlm@1: luaF_close(L1, L1->stack); /* close all upvalues for this thread */ rlm@1: lua_assert(L1->openupval == NULL); rlm@1: luai_userstatefree(L1); rlm@1: freestack(L, L1); rlm@1: luaM_freemem(L, fromstate(L1), state_size(lua_State)); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { rlm@1: int i; rlm@1: lua_State *L; rlm@1: global_State *g; rlm@1: void *l = (*f)(ud, NULL, 0, state_size(LG)); rlm@1: if (l == NULL) return NULL; rlm@1: L = tostate(l); rlm@1: g = &((LG *)L)->g; rlm@1: L->next = NULL; rlm@1: L->tt = LUA_TTHREAD; rlm@1: g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); rlm@1: L->marked = luaC_white(g); rlm@1: set2bits(L->marked, FIXEDBIT, SFIXEDBIT); rlm@1: preinit_state(L, g); rlm@1: g->frealloc = f; rlm@1: g->ud = ud; rlm@1: g->mainthread = L; rlm@1: g->uvhead.u.l.prev = &g->uvhead; rlm@1: g->uvhead.u.l.next = &g->uvhead; rlm@1: g->GCthreshold = 0; /* mark it as unfinished state */ rlm@1: g->strt.size = 0; rlm@1: g->strt.nuse = 0; rlm@1: g->strt.hash = NULL; rlm@1: setnilvalue(registry(L)); rlm@1: luaZ_initbuffer(L, &g->buff); rlm@1: g->panic = NULL; rlm@1: g->gcstate = GCSpause; rlm@1: g->rootgc = obj2gco(L); rlm@1: g->sweepstrgc = 0; rlm@1: g->sweepgc = &g->rootgc; rlm@1: g->gray = NULL; rlm@1: g->grayagain = NULL; rlm@1: g->weak = NULL; rlm@1: g->tmudata = NULL; rlm@1: g->totalbytes = sizeof(LG); rlm@1: g->gcpause = LUAI_GCPAUSE; rlm@1: g->gcstepmul = LUAI_GCMUL; rlm@1: g->gcdept = 0; rlm@1: for (i=0; imt[i] = NULL; rlm@1: if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { rlm@1: /* memory allocation error: free partial state */ rlm@1: close_state(L); rlm@1: L = NULL; rlm@1: } rlm@1: else rlm@1: luai_userstateopen(L); rlm@1: return L; rlm@1: } rlm@1: rlm@1: rlm@1: static void callallgcTM (lua_State *L, void *ud) { rlm@1: UNUSED(ud); rlm@1: luaC_callGCTM(L); /* call GC metamethods for all udata */ rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_close (lua_State *L) { rlm@1: L = G(L)->mainthread; /* only the main thread can be closed */ rlm@1: lua_lock(L); rlm@1: luaF_close(L, L->stack); /* close all upvalues for this thread */ rlm@1: luaC_separateudata(L, 1); /* separate udata that have GC metamethods */ rlm@1: L->errfunc = 0; /* no error function during GC metamethods */ rlm@1: do { /* repeat until no more errors */ rlm@1: L->ci = L->base_ci; rlm@1: L->base = L->top = L->ci->base; rlm@1: L->nCcalls = L->baseCcalls = 0; rlm@1: } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0); rlm@1: lua_assert(G(L)->tmudata == NULL); rlm@1: luai_userstateclose(L); rlm@1: close_state(L); rlm@1: } rlm@1: