lua/lstate.h

Go to the documentation of this file.
00001 /*
00002 ** $Id: lstate.h,v 2.74 2011/09/30 12:45:07 roberto Exp $
00003 ** Global State
00004 ** See Copyright Notice in lua.h
00005 */
00006 
00007 #ifndef lstate_h
00008 #define lstate_h
00009 
00010 #include "lua.h"
00011 
00012 #include "lobject.h"
00013 #include "ltm.h"
00014 #include "lzio.h"
00015 
00016 
00017 /*
00018 
00019 ** Some notes about garbage-collected objects:  All objects in Lua must
00020 ** be kept somehow accessible until being freed.
00021 **
00022 ** Lua keeps most objects linked in list g->allgc. The link uses field
00023 ** 'next' of the CommonHeader.
00024 **
00025 ** Strings are kept in several lists headed by the array g->strt.hash.
00026 **
00027 ** Open upvalues are not subject to independent garbage collection. They
00028 ** are collected together with their respective threads. Lua keeps a
00029 ** double-linked list with all open upvalues (g->uvhead) so that it can
00030 ** mark objects referred by them. (They are always gray, so they must
00031 ** be remarked in the atomic step. Usually their contents would be marked
00032 ** when traversing the respective threads, but the thread may already be
00033 ** dead, while the upvalue is still accessible through closures.)
00034 **
00035 ** Objects with finalizers are kept in the list g->finobj.
00036 **
00037 ** The list g->tobefnz links all objects being finalized.
00038 
00039 */
00040 
00041 
00042 struct lua_longjmp;  /* defined in ldo.c */
00043 
00044 
00045 
00046 /* extra stack space to handle TM calls and some other extras */
00047 #define EXTRA_STACK   5
00048 
00049 
00050 #define BASIC_STACK_SIZE        (2*LUA_MINSTACK)
00051 
00052 
00053 /* kinds of Garbage Collection */
00054 #define KGC_NORMAL  0
00055 #define KGC_EMERGENCY   1   /* gc was forced by an allocation failure */
00056 #define KGC_GEN     2   /* generational collection */
00057 
00058 
00059 typedef struct stringtable {
00060   GCObject **hash;
00061   lu_int32 nuse;  /* number of elements */
00062   int size;
00063 } stringtable;
00064 
00065 
00066 /*
00067 ** information about a call
00068 */
00069 typedef struct CallInfo {
00070   StkId func;  /* function index in the stack */
00071   StkId top;  /* top for this function */
00072   struct CallInfo *previous, *next;  /* dynamic call link */
00073   short nresults;  /* expected number of results from this function */
00074   lu_byte callstatus;
00075   union {
00076     struct {  /* only for Lua functions */
00077       StkId base;  /* base for this function */
00078       const Instruction *savedpc;
00079     } l;
00080     struct {  /* only for C functions */
00081       int ctx;  /* context info. in case of yields */
00082       lua_CFunction k;  /* continuation in case of yields */
00083       ptrdiff_t old_errfunc;
00084       ptrdiff_t extra;
00085       lu_byte old_allowhook;
00086       lu_byte status;
00087     } c;
00088   } u;
00089 } CallInfo;
00090 
00091 
00092 /*
00093 ** Bits in CallInfo status
00094 */
00095 #define CIST_LUA    (1<<0)  /* call is running a Lua function */
00096 #define CIST_HOOKED (1<<1)  /* call is running a debug hook */
00097 #define CIST_REENTRY    (1<<2)  /* call is running on same invocation of
00098                                    luaV_execute of previous call */
00099 #define CIST_YIELDED    (1<<3)  /* call reentered after suspension */
00100 #define CIST_YPCALL (1<<4)  /* call is a yieldable protected call */
00101 #define CIST_STAT   (1<<5)  /* call has an error status (pcall) */
00102 #define CIST_TAIL   (1<<6)  /* call was tail called */
00103 
00104 
00105 #define isLua(ci)   ((ci)->callstatus & CIST_LUA)
00106 
00107 
00108 /*
00109 ** `global state', shared by all threads of this state
00110 */
00111 typedef struct global_State {
00112   lua_Alloc frealloc;  /* function to reallocate memory */
00113   void *ud;         /* auxiliary data to `frealloc' */
00114   lu_mem totalbytes;  /* number of bytes currently allocated - GCdebt */
00115   l_mem GCdebt;  /* bytes allocated not yet compensated by the collector */
00116   lu_mem lastmajormem;  /* memory in use after last major collection */
00117   stringtable strt;  /* hash table for strings */
00118   TValue l_registry;
00119   lu_byte currentwhite;
00120   lu_byte gcstate;  /* state of garbage collector */
00121   lu_byte gckind;  /* kind of GC running */
00122   lu_byte gcrunning;  /* true if GC is running */
00123   int sweepstrgc;  /* position of sweep in `strt' */
00124   GCObject *allgc;  /* list of all collectable objects */
00125   GCObject *finobj;  /* list of collectable objects with finalizers */
00126   GCObject **sweepgc;  /* current position of sweep */
00127   GCObject *gray;  /* list of gray objects */
00128   GCObject *grayagain;  /* list of objects to be traversed atomically */
00129   GCObject *weak;  /* list of tables with weak values */
00130   GCObject *ephemeron;  /* list of ephemeron tables (weak keys) */
00131   GCObject *allweak;  /* list of all-weak tables */
00132   GCObject *tobefnz;  /* list of userdata to be GC */
00133   UpVal uvhead;  /* head of double-linked list of all open upvalues */
00134   Mbuffer buff;  /* temporary buffer for string concatenation */
00135   int gcpause;  /* size of pause between successive GCs */
00136   int gcmajorinc;  /* how much to wait for a major GC (only in gen. mode) */
00137   int gcstepmul;  /* GC `granularity' */
00138   lua_CFunction panic;  /* to be called in unprotected errors */
00139   struct lua_State *mainthread;
00140   const lua_Number *version;  /* pointer to version number */
00141   TString *memerrmsg;  /* memory-error message */
00142   TString *tmname[TM_N];  /* array with tag-method names */
00143   struct Table *mt[LUA_NUMTAGS];  /* metatables for basic types */
00144 } global_State;
00145 
00146 
00147 /*
00148 ** `per thread' state
00149 */
00150 struct lua_State {
00151   CommonHeader;
00152   lu_byte status;
00153   StkId top;  /* first free slot in the stack */
00154   global_State *l_G;
00155   CallInfo *ci;  /* call info for current function */
00156   const Instruction *oldpc;  /* last pc traced */
00157   StkId stack_last;  /* last free slot in the stack */
00158   StkId stack;  /* stack base */
00159   int stacksize;
00160   unsigned short nny;  /* number of non-yieldable calls in stack */
00161   unsigned short nCcalls;  /* number of nested C calls */
00162   lu_byte hookmask;
00163   lu_byte allowhook;
00164   int basehookcount;
00165   int hookcount;
00166   lua_Hook hook;
00167   GCObject *openupval;  /* list of open upvalues in this stack */
00168   GCObject *gclist;
00169   struct lua_longjmp *errorJmp;  /* current error recover point */
00170   ptrdiff_t errfunc;  /* current error handling function (stack index) */
00171   CallInfo base_ci;  /* CallInfo for first level (C calling Lua) */
00172 };
00173 
00174 
00175 #define G(L)    (L->l_G)
00176 
00177 
00178 /*
00179 ** Union of all collectable objects
00180 */
00181 union GCObject {
00182   GCheader gch;  /* common header */
00183   union TString ts;
00184   union Udata u;
00185   union Closure cl;
00186   struct Table h;
00187   struct Proto p;
00188   struct UpVal uv;
00189   struct lua_State th;  /* thread */
00190 };
00191 
00192 
00193 #define gch(o)      (&(o)->gch)
00194 
00195 /* macros to convert a GCObject into a specific value */
00196 #define rawgco2ts(o)    check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
00197 #define gco2ts(o)   (&rawgco2ts(o)->tsv)
00198 #define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
00199 #define gco2u(o)    (&rawgco2u(o)->uv)
00200 #define gco2cl(o)   check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
00201 #define gco2t(o)    check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
00202 #define gco2p(o)    check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
00203 #define gco2uv(o)   check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
00204 #define gco2th(o)   check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
00205 
00206 /* macro to convert any Lua object into a GCObject */
00207 #define obj2gco(v)  (cast(GCObject *, (v)))
00208 
00209 
00210 /* actual number of total bytes allocated */
00211 #define gettotalbytes(g)    ((g)->totalbytes + (g)->GCdebt)
00212 
00213 LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
00214 LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
00215 LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
00216 LUAI_FUNC void luaE_freeCI (lua_State *L);
00217 
00218 
00219 #endif
00220 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:04 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs