1*eda14cbcSMatt Macy /* 2*eda14cbcSMatt Macy ** $Id: lstate.h,v 2.82.1.1 2013/04/12 18:48:47 roberto Exp $ 3*eda14cbcSMatt Macy ** Global State 4*eda14cbcSMatt Macy ** See Copyright Notice in lua.h 5*eda14cbcSMatt Macy */ 6*eda14cbcSMatt Macy 7*eda14cbcSMatt Macy #ifndef lstate_h 8*eda14cbcSMatt Macy #define lstate_h 9*eda14cbcSMatt Macy 10*eda14cbcSMatt Macy #include <sys/lua/lua.h> 11*eda14cbcSMatt Macy 12*eda14cbcSMatt Macy #include "lobject.h" 13*eda14cbcSMatt Macy #include "ltm.h" 14*eda14cbcSMatt Macy #include "lzio.h" 15*eda14cbcSMatt Macy 16*eda14cbcSMatt Macy 17*eda14cbcSMatt Macy /* 18*eda14cbcSMatt Macy 19*eda14cbcSMatt Macy ** Some notes about garbage-collected objects: All objects in Lua must 20*eda14cbcSMatt Macy ** be kept somehow accessible until being freed. 21*eda14cbcSMatt Macy ** 22*eda14cbcSMatt Macy ** Lua keeps most objects linked in list g->allgc. The link uses field 23*eda14cbcSMatt Macy ** 'next' of the CommonHeader. 24*eda14cbcSMatt Macy ** 25*eda14cbcSMatt Macy ** Strings are kept in several lists headed by the array g->strt.hash. 26*eda14cbcSMatt Macy ** 27*eda14cbcSMatt Macy ** Open upvalues are not subject to independent garbage collection. They 28*eda14cbcSMatt Macy ** are collected together with their respective threads. Lua keeps a 29*eda14cbcSMatt Macy ** double-linked list with all open upvalues (g->uvhead) so that it can 30*eda14cbcSMatt Macy ** mark objects referred by them. (They are always gray, so they must 31*eda14cbcSMatt Macy ** be remarked in the atomic step. Usually their contents would be marked 32*eda14cbcSMatt Macy ** when traversing the respective threads, but the thread may already be 33*eda14cbcSMatt Macy ** dead, while the upvalue is still accessible through closures.) 34*eda14cbcSMatt Macy ** 35*eda14cbcSMatt Macy ** Objects with finalizers are kept in the list g->finobj. 36*eda14cbcSMatt Macy ** 37*eda14cbcSMatt Macy ** The list g->tobefnz links all objects being finalized. 38*eda14cbcSMatt Macy 39*eda14cbcSMatt Macy */ 40*eda14cbcSMatt Macy 41*eda14cbcSMatt Macy 42*eda14cbcSMatt Macy struct lua_longjmp; /* defined in ldo.c */ 43*eda14cbcSMatt Macy 44*eda14cbcSMatt Macy 45*eda14cbcSMatt Macy 46*eda14cbcSMatt Macy /* extra stack space to handle TM calls and some other extras */ 47*eda14cbcSMatt Macy #define EXTRA_STACK 5 48*eda14cbcSMatt Macy 49*eda14cbcSMatt Macy 50*eda14cbcSMatt Macy #define BASIC_STACK_SIZE (2*LUA_MINSTACK) 51*eda14cbcSMatt Macy 52*eda14cbcSMatt Macy 53*eda14cbcSMatt Macy /* kinds of Garbage Collection */ 54*eda14cbcSMatt Macy #define KGC_NORMAL 0 55*eda14cbcSMatt Macy #define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */ 56*eda14cbcSMatt Macy #define KGC_GEN 2 /* generational collection */ 57*eda14cbcSMatt Macy 58*eda14cbcSMatt Macy 59*eda14cbcSMatt Macy typedef struct stringtable { 60*eda14cbcSMatt Macy GCObject **hash; 61*eda14cbcSMatt Macy lu_int32 nuse; /* number of elements */ 62*eda14cbcSMatt Macy int size; 63*eda14cbcSMatt Macy } stringtable; 64*eda14cbcSMatt Macy 65*eda14cbcSMatt Macy 66*eda14cbcSMatt Macy /* 67*eda14cbcSMatt Macy ** information about a call 68*eda14cbcSMatt Macy */ 69*eda14cbcSMatt Macy typedef struct CallInfo { 70*eda14cbcSMatt Macy StkId func; /* function index in the stack */ 71*eda14cbcSMatt Macy StkId top; /* top for this function */ 72*eda14cbcSMatt Macy struct CallInfo *previous, *next; /* dynamic call link */ 73*eda14cbcSMatt Macy short nresults; /* expected number of results from this function */ 74*eda14cbcSMatt Macy lu_byte callstatus; 75*eda14cbcSMatt Macy ptrdiff_t extra; 76*eda14cbcSMatt Macy union { 77*eda14cbcSMatt Macy struct { /* only for Lua functions */ 78*eda14cbcSMatt Macy StkId base; /* base for this function */ 79*eda14cbcSMatt Macy const Instruction *savedpc; 80*eda14cbcSMatt Macy } l; 81*eda14cbcSMatt Macy struct { /* only for C functions */ 82*eda14cbcSMatt Macy int ctx; /* context info. in case of yields */ 83*eda14cbcSMatt Macy lua_CFunction k; /* continuation in case of yields */ 84*eda14cbcSMatt Macy ptrdiff_t old_errfunc; 85*eda14cbcSMatt Macy lu_byte old_allowhook; 86*eda14cbcSMatt Macy lu_byte status; 87*eda14cbcSMatt Macy } c; 88*eda14cbcSMatt Macy } u; 89*eda14cbcSMatt Macy } CallInfo; 90*eda14cbcSMatt Macy 91*eda14cbcSMatt Macy 92*eda14cbcSMatt Macy /* 93*eda14cbcSMatt Macy ** Bits in CallInfo status 94*eda14cbcSMatt Macy */ 95*eda14cbcSMatt Macy #define CIST_LUA (1<<0) /* call is running a Lua function */ 96*eda14cbcSMatt Macy #define CIST_HOOKED (1<<1) /* call is running a debug hook */ 97*eda14cbcSMatt Macy #define CIST_REENTRY (1<<2) /* call is running on same invocation of 98*eda14cbcSMatt Macy luaV_execute of previous call */ 99*eda14cbcSMatt Macy #define CIST_YIELDED (1<<3) /* call reentered after suspension */ 100*eda14cbcSMatt Macy #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ 101*eda14cbcSMatt Macy #define CIST_STAT (1<<5) /* call has an error status (pcall) */ 102*eda14cbcSMatt Macy #define CIST_TAIL (1<<6) /* call was tail called */ 103*eda14cbcSMatt Macy #define CIST_HOOKYIELD (1<<7) /* last hook called yielded */ 104*eda14cbcSMatt Macy 105*eda14cbcSMatt Macy 106*eda14cbcSMatt Macy #define isLua(ci) ((ci)->callstatus & CIST_LUA) 107*eda14cbcSMatt Macy 108*eda14cbcSMatt Macy 109*eda14cbcSMatt Macy /* 110*eda14cbcSMatt Macy ** `global state', shared by all threads of this state 111*eda14cbcSMatt Macy */ 112*eda14cbcSMatt Macy typedef struct global_State { 113*eda14cbcSMatt Macy lua_Alloc frealloc; /* function to reallocate memory */ 114*eda14cbcSMatt Macy void *ud; /* auxiliary data to `frealloc' */ 115*eda14cbcSMatt Macy lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */ 116*eda14cbcSMatt Macy l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ 117*eda14cbcSMatt Macy lu_mem GCmemtrav; /* memory traversed by the GC */ 118*eda14cbcSMatt Macy lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ 119*eda14cbcSMatt Macy stringtable strt; /* hash table for strings */ 120*eda14cbcSMatt Macy TValue l_registry; 121*eda14cbcSMatt Macy unsigned int seed; /* randomized seed for hashes */ 122*eda14cbcSMatt Macy lu_byte currentwhite; 123*eda14cbcSMatt Macy lu_byte gcstate; /* state of garbage collector */ 124*eda14cbcSMatt Macy lu_byte gckind; /* kind of GC running */ 125*eda14cbcSMatt Macy lu_byte gcrunning; /* true if GC is running */ 126*eda14cbcSMatt Macy int sweepstrgc; /* position of sweep in `strt' */ 127*eda14cbcSMatt Macy GCObject *allgc; /* list of all collectable objects */ 128*eda14cbcSMatt Macy GCObject *finobj; /* list of collectable objects with finalizers */ 129*eda14cbcSMatt Macy GCObject **sweepgc; /* current position of sweep in list 'allgc' */ 130*eda14cbcSMatt Macy GCObject **sweepfin; /* current position of sweep in list 'finobj' */ 131*eda14cbcSMatt Macy GCObject *gray; /* list of gray objects */ 132*eda14cbcSMatt Macy GCObject *grayagain; /* list of objects to be traversed atomically */ 133*eda14cbcSMatt Macy GCObject *weak; /* list of tables with weak values */ 134*eda14cbcSMatt Macy GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ 135*eda14cbcSMatt Macy GCObject *allweak; /* list of all-weak tables */ 136*eda14cbcSMatt Macy GCObject *tobefnz; /* list of userdata to be GC */ 137*eda14cbcSMatt Macy UpVal uvhead; /* head of double-linked list of all open upvalues */ 138*eda14cbcSMatt Macy Mbuffer buff; /* temporary buffer for string concatenation */ 139*eda14cbcSMatt Macy int gcpause; /* size of pause between successive GCs */ 140*eda14cbcSMatt Macy int gcmajorinc; /* pause between major collections (only in gen. mode) */ 141*eda14cbcSMatt Macy int gcstepmul; /* GC `granularity' */ 142*eda14cbcSMatt Macy lua_CFunction panic; /* to be called in unprotected errors */ 143*eda14cbcSMatt Macy struct lua_State *mainthread; 144*eda14cbcSMatt Macy const lua_Number *version; /* pointer to version number */ 145*eda14cbcSMatt Macy TString *memerrmsg; /* memory-error message */ 146*eda14cbcSMatt Macy TString *tmname[TM_N]; /* array with tag-method names */ 147*eda14cbcSMatt Macy struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ 148*eda14cbcSMatt Macy } global_State; 149*eda14cbcSMatt Macy 150*eda14cbcSMatt Macy 151*eda14cbcSMatt Macy /* 152*eda14cbcSMatt Macy ** `per thread' state 153*eda14cbcSMatt Macy */ 154*eda14cbcSMatt Macy struct lua_State { 155*eda14cbcSMatt Macy CommonHeader; 156*eda14cbcSMatt Macy lu_byte status; 157*eda14cbcSMatt Macy StkId top; /* first free slot in the stack */ 158*eda14cbcSMatt Macy global_State *l_G; 159*eda14cbcSMatt Macy CallInfo *ci; /* call info for current function */ 160*eda14cbcSMatt Macy const Instruction *oldpc; /* last pc traced */ 161*eda14cbcSMatt Macy StkId stack_last; /* last free slot in the stack */ 162*eda14cbcSMatt Macy StkId stack; /* stack base */ 163*eda14cbcSMatt Macy int stacksize; 164*eda14cbcSMatt Macy unsigned short nny; /* number of non-yieldable calls in stack */ 165*eda14cbcSMatt Macy unsigned short nCcalls; /* number of nested C calls */ 166*eda14cbcSMatt Macy lu_byte hookmask; 167*eda14cbcSMatt Macy lu_byte allowhook; 168*eda14cbcSMatt Macy lu_byte runerror; /* handling a runtime error */ 169*eda14cbcSMatt Macy int basehookcount; 170*eda14cbcSMatt Macy int hookcount; 171*eda14cbcSMatt Macy lua_Hook hook; 172*eda14cbcSMatt Macy GCObject *openupval; /* list of open upvalues in this stack */ 173*eda14cbcSMatt Macy GCObject *gclist; 174*eda14cbcSMatt Macy struct lua_longjmp *errorJmp; /* current error recover point */ 175*eda14cbcSMatt Macy ptrdiff_t errfunc; /* current error handling function (stack index) */ 176*eda14cbcSMatt Macy CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ 177*eda14cbcSMatt Macy }; 178*eda14cbcSMatt Macy 179*eda14cbcSMatt Macy 180*eda14cbcSMatt Macy #define G(L) (L->l_G) 181*eda14cbcSMatt Macy 182*eda14cbcSMatt Macy 183*eda14cbcSMatt Macy /* 184*eda14cbcSMatt Macy ** Union of all collectable objects 185*eda14cbcSMatt Macy */ 186*eda14cbcSMatt Macy union GCObject { 187*eda14cbcSMatt Macy GCheader gch; /* common header */ 188*eda14cbcSMatt Macy union TString ts; 189*eda14cbcSMatt Macy union Udata u; 190*eda14cbcSMatt Macy union Closure cl; 191*eda14cbcSMatt Macy struct Table h; 192*eda14cbcSMatt Macy struct Proto p; 193*eda14cbcSMatt Macy struct UpVal uv; 194*eda14cbcSMatt Macy struct lua_State th; /* thread */ 195*eda14cbcSMatt Macy }; 196*eda14cbcSMatt Macy 197*eda14cbcSMatt Macy 198*eda14cbcSMatt Macy #define gch(o) (&(o)->gch) 199*eda14cbcSMatt Macy 200*eda14cbcSMatt Macy /* macros to convert a GCObject into a specific value */ 201*eda14cbcSMatt Macy #define rawgco2ts(o) \ 202*eda14cbcSMatt Macy check_exp(novariant((o)->gch.tt) == LUA_TSTRING, &((o)->ts)) 203*eda14cbcSMatt Macy #define gco2ts(o) (&rawgco2ts(o)->tsv) 204*eda14cbcSMatt Macy #define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) 205*eda14cbcSMatt Macy #define gco2u(o) (&rawgco2u(o)->uv) 206*eda14cbcSMatt Macy #define gco2lcl(o) check_exp((o)->gch.tt == LUA_TLCL, &((o)->cl.l)) 207*eda14cbcSMatt Macy #define gco2ccl(o) check_exp((o)->gch.tt == LUA_TCCL, &((o)->cl.c)) 208*eda14cbcSMatt Macy #define gco2cl(o) \ 209*eda14cbcSMatt Macy check_exp(novariant((o)->gch.tt) == LUA_TFUNCTION, &((o)->cl)) 210*eda14cbcSMatt Macy #define gco2t(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) 211*eda14cbcSMatt Macy #define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) 212*eda14cbcSMatt Macy #define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 213*eda14cbcSMatt Macy #define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) 214*eda14cbcSMatt Macy 215*eda14cbcSMatt Macy /* macro to convert any Lua object into a GCObject */ 216*eda14cbcSMatt Macy #define obj2gco(v) (cast(GCObject *, (v))) 217*eda14cbcSMatt Macy 218*eda14cbcSMatt Macy 219*eda14cbcSMatt Macy /* actual number of total bytes allocated */ 220*eda14cbcSMatt Macy #define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt) 221*eda14cbcSMatt Macy 222*eda14cbcSMatt Macy LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); 223*eda14cbcSMatt Macy LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 224*eda14cbcSMatt Macy LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); 225*eda14cbcSMatt Macy LUAI_FUNC void luaE_freeCI (lua_State *L); 226*eda14cbcSMatt Macy 227*eda14cbcSMatt Macy 228*eda14cbcSMatt Macy #endif 229