lstate.c

Go to the documentation of this file.
00001 /*
00002 ** $Id: lstate.c,v 2.36.1.2 2008/01/03 15:20:39 roberto Exp $
00003 ** Global State
00004 ** See Copyright Notice in lua.h
00005 */
00006 
00007 
00008 #include <stddef.h>
00009 
00010 #define lstate_c
00011 #define LUA_CORE
00012 
00013 #include "lua.h"
00014 
00015 #include "ldebug.h"
00016 #include "ldo.h"
00017 #include "lfunc.h"
00018 #include "lgc.h"
00019 #include "llex.h"
00020 #include "lmem.h"
00021 #include "lstate.h"
00022 #include "lstring.h"
00023 #include "ltable.h"
00024 #include "ltm.h"
00025 
00026 
00027 #define state_size(x) (sizeof(x) + LUAI_EXTRASPACE)
00028 #define fromstate(l)  (cast(lu_byte *, (l)) - LUAI_EXTRASPACE)
00029 #define tostate(l)   (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE))
00030 
00031 
00032 /*
00033 ** Main thread combines a thread state and the global state
00034 */
00035 typedef struct LG {
00036   lua_State l;
00037   global_State g;
00038 } LG;
00039   
00040 
00041 
00042 static void stack_init (lua_State *L1, lua_State *L) {
00043   /* initialize CallInfo array */
00044   L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo);
00045   L1->ci = L1->base_ci;
00046   L1->size_ci = BASIC_CI_SIZE;
00047   L1->end_ci = L1->base_ci + L1->size_ci - 1;
00048   /* initialize stack array */
00049   L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue);
00050   L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK;
00051   L1->top = L1->stack;
00052   L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1;
00053   /* initialize first ci */
00054   L1->ci->func = L1->top;
00055   setnilvalue(L1->top++);  /* `function' entry for this `ci' */
00056   L1->base = L1->ci->base = L1->top;
00057   L1->ci->top = L1->top + LUA_MINSTACK;
00058 }
00059 
00060 
00061 static void freestack (lua_State *L, lua_State *L1) {
00062   luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo);
00063   luaM_freearray(L, L1->stack, L1->stacksize, TValue);
00064 }
00065 
00066 
00067 /*
00068 ** open parts that may cause memory-allocation errors
00069 */
00070 static void f_luaopen (lua_State *L, void *ud) {
00071   global_State *g = G(L);
00072   UNUSED(ud);
00073   stack_init(L, L);  /* init stack */
00074   sethvalue(L, gt(L), luaH_new(L, 0, 2));  /* table of globals */
00075   sethvalue(L, registry(L), luaH_new(L, 0, 2));  /* registry */
00076   luaS_resize(L, MINSTRTABSIZE);  /* initial size of string table */
00077   luaT_init(L);
00078   luaX_init(L);
00079   luaS_fix(luaS_newliteral(L, MEMERRMSG));
00080   g->GCthreshold = 4*g->totalbytes;
00081 }
00082 
00083 
00084 static void preinit_state (lua_State *L, global_State *g) {
00085   G(L) = g;
00086   L->stack = NULL;
00087   L->stacksize = 0;
00088   L->errorJmp = NULL;
00089   L->hook = NULL;
00090   L->hookmask = 0;
00091   L->basehookcount = 0;
00092   L->allowhook = 1;
00093   resethookcount(L);
00094   L->openupval = NULL;
00095   L->size_ci = 0;
00096   L->nCcalls = L->baseCcalls = 0;
00097   L->status = 0;
00098   L->base_ci = L->ci = NULL;
00099   L->savedpc = NULL;
00100   L->errfunc = 0;
00101   setnilvalue(gt(L));
00102 }
00103 
00104 
00105 static void close_state (lua_State *L) {
00106   global_State *g = G(L);
00107   luaF_close(L, L->stack);  /* close all upvalues for this thread */
00108   luaC_freeall(L);  /* collect all objects */
00109   lua_assert(g->rootgc == obj2gco(L));
00110   lua_assert(g->strt.nuse == 0);
00111   luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
00112   luaZ_freebuffer(L, &g->buff);
00113   freestack(L, L);
00114   lua_assert(g->totalbytes == sizeof(LG));
00115   (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0);
00116 }
00117 
00118 
00119 lua_State *luaE_newthread (lua_State *L) {
00120   lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State)));
00121   luaC_link(L, obj2gco(L1), LUA_TTHREAD);
00122   preinit_state(L1, G(L));
00123   stack_init(L1, L);  /* init stack */
00124   setobj2n(L, gt(L1), gt(L));  /* share table of globals */
00125   L1->hookmask = L->hookmask;
00126   L1->basehookcount = L->basehookcount;
00127   L1->hook = L->hook;
00128   resethookcount(L1);
00129   lua_assert(iswhite(obj2gco(L1)));
00130   return L1;
00131 }
00132 
00133 
00134 void luaE_freethread (lua_State *L, lua_State *L1) {
00135   luaF_close(L1, L1->stack);  /* close all upvalues for this thread */
00136   lua_assert(L1->openupval == NULL);
00137   luai_userstatefree(L1);
00138   freestack(L, L1);
00139   luaM_freemem(L, fromstate(L1), state_size(lua_State));
00140 }
00141 
00142 
00143 LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
00144   int i;
00145   lua_State *L;
00146   global_State *g;
00147   void *l = (*f)(ud, NULL, 0, state_size(LG));
00148   if (l == NULL) return NULL;
00149   L = tostate(l);
00150   g = &((LG *)L)->g;
00151   L->next = NULL;
00152   L->tt = LUA_TTHREAD;
00153   g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
00154   L->marked = luaC_white(g);
00155   set2bits(L->marked, FIXEDBIT, SFIXEDBIT);
00156   preinit_state(L, g);
00157   g->frealloc = f;
00158   g->ud = ud;
00159   g->mainthread = L;
00160   g->uvhead.u.l.prev = &g->uvhead;
00161   g->uvhead.u.l.next = &g->uvhead;
00162   g->GCthreshold = 0;  /* mark it as unfinished state */
00163   g->strt.size = 0;
00164   g->strt.nuse = 0;
00165   g->strt.hash = NULL;
00166   setnilvalue(registry(L));
00167   luaZ_initbuffer(L, &g->buff);
00168   g->panic = NULL;
00169   g->gcstate = GCSpause;
00170   g->rootgc = obj2gco(L);
00171   g->sweepstrgc = 0;
00172   g->sweepgc = &g->rootgc;
00173   g->gray = NULL;
00174   g->grayagain = NULL;
00175   g->weak = NULL;
00176   g->tmudata = NULL;
00177   g->totalbytes = sizeof(LG);
00178   g->gcpause = LUAI_GCPAUSE;
00179   g->gcstepmul = LUAI_GCMUL;
00180   g->gcdept = 0;
00181   for (i=0; i<NUM_TAGS; i++) g->mt[i] = NULL;
00182   if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
00183     /* memory allocation error: free partial state */
00184     close_state(L);
00185     L = NULL;
00186   }
00187   else
00188     luai_userstateopen(L);
00189   return L;
00190 }
00191 
00192 
00193 static void callallgcTM (lua_State *L, void *ud) {
00194   UNUSED(ud);
00195   luaC_callGCTM(L);  /* call GC metamethods for all udata */
00196 }
00197 
00198 
00199 LUA_API void lua_close (lua_State *L) {
00200   L = G(L)->mainthread;  /* only the main thread can be closed */
00201   lua_lock(L);
00202   luaF_close(L, L->stack);  /* close all upvalues for this thread */
00203   luaC_separateudata(L, 1);  /* separate udata that have GC metamethods */
00204   L->errfunc = 0;  /* no error function during GC metamethods */
00205   do {  /* repeat until no more errors */
00206     L->ci = L->base_ci;
00207     L->base = L->top = L->ci->base;
00208     L->nCcalls = L->baseCcalls = 0;
00209   } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0);
00210   lua_assert(G(L)->tmudata == NULL);
00211   luai_userstateclose(L);
00212   close_state(L);
00213 }
00214 

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:54 2011 by Doxygen 1.6.1