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