1 /* $NetBSD: lstate.h,v 1.8 2017/04/26 13:17:33 mbalmer Exp $ */ 2 3 /* 4 ** Id: lstate.h,v 2.133 2016/12/22 13:08:50 roberto Exp 5 ** Global State 6 ** See Copyright Notice in lua.h 7 */ 8 9 #ifndef lstate_h 10 #define lstate_h 11 12 #include "lua.h" 13 14 #include "lobject.h" 15 #include "ltm.h" 16 #include "lzio.h" 17 18 19 /* 20 21 ** Some notes about garbage-collected objects: All objects in Lua must 22 ** be kept somehow accessible until being freed, so all objects always 23 ** belong to one (and only one) of these lists, using field 'next' of 24 ** the 'CommonHeader' for the link: 25 ** 26 ** 'allgc': all objects not marked for finalization; 27 ** 'finobj': all objects marked for finalization; 28 ** 'tobefnz': all objects ready to be finalized; 29 ** 'fixedgc': all objects that are not to be collected (currently 30 ** only small strings, such as reserved words). 31 32 */ 33 34 35 struct lua_longjmp; /* defined in ldo.c */ 36 37 38 /* 39 ** Atomic type (relative to signals) to better ensure that 'lua_sethook' 40 ** is thread safe 41 */ 42 #if !defined(l_signalT) 43 #include <signal.h> 44 #define l_signalT sig_atomic_t 45 #endif 46 47 48 /* extra stack space to handle TM calls and some other extras */ 49 #define EXTRA_STACK 5 50 51 52 #define BASIC_STACK_SIZE (2*LUA_MINSTACK) 53 54 55 /* kinds of Garbage Collection */ 56 #define KGC_NORMAL 0 57 #define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */ 58 59 60 typedef struct stringtable { 61 TString **hash; 62 int nuse; /* number of elements */ 63 int size; 64 } stringtable; 65 66 67 /* 68 ** Information about a call. 69 ** When a thread yields, 'func' is adjusted to pretend that the 70 ** top function has only the yielded values in its stack; in that 71 ** case, the actual 'func' value is saved in field 'extra'. 72 ** When a function calls another with a continuation, 'extra' keeps 73 ** the function index so that, in case of errors, the continuation 74 ** function can be called with the correct top. 75 */ 76 typedef struct CallInfo { 77 StkId func; /* function index in the stack */ 78 StkId top; /* top for this function */ 79 struct CallInfo *previous, *next; /* dynamic call link */ 80 union { 81 struct { /* only for Lua functions */ 82 StkId base; /* base for this function */ 83 const Instruction *savedpc; 84 } l; 85 struct { /* only for C functions */ 86 lua_KFunction k; /* continuation in case of yields */ 87 ptrdiff_t old_errfunc; 88 lua_KContext ctx; /* context info. in case of yields */ 89 } c; 90 } u; 91 ptrdiff_t extra; 92 short nresults; /* expected number of results from this function */ 93 unsigned short callstatus; 94 } CallInfo; 95 96 97 /* 98 ** Bits in CallInfo status 99 */ 100 #define CIST_OAH (1<<0) /* original value of 'allowhook' */ 101 #define CIST_LUA (1<<1) /* call is running a Lua function */ 102 #define CIST_HOOKED (1<<2) /* call is running a debug hook */ 103 #define CIST_FRESH (1<<3) /* call is running on a fresh invocation 104 of luaV_execute */ 105 #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ 106 #define CIST_TAIL (1<<5) /* call was tail called */ 107 #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ 108 #define CIST_LEQ (1<<7) /* using __lt for __le */ 109 #define CIST_FIN (1<<8) /* call is running a finalizer */ 110 111 #define isLua(ci) ((ci)->callstatus & CIST_LUA) 112 113 /* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */ 114 #define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) 115 #define getoah(st) ((st) & CIST_OAH) 116 117 118 /* 119 ** 'global state', shared by all threads of this state 120 */ 121 typedef struct global_State { 122 lua_Alloc frealloc; /* function to reallocate memory */ 123 void *ud; /* auxiliary data to 'frealloc' */ 124 l_mem totalbytes; /* number of bytes currently allocated - GCdebt */ 125 l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ 126 lu_mem GCmemtrav; /* memory traversed by the GC */ 127 lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ 128 stringtable strt; /* hash table for strings */ 129 TValue l_registry; 130 unsigned int seed; /* randomized seed for hashes */ 131 lu_byte currentwhite; 132 lu_byte gcstate; /* state of garbage collector */ 133 lu_byte gckind; /* kind of GC running */ 134 lu_byte gcrunning; /* true if GC is running */ 135 GCObject *allgc; /* list of all collectable objects */ 136 GCObject **sweepgc; /* current position of sweep in list */ 137 GCObject *finobj; /* list of collectable objects with finalizers */ 138 GCObject *gray; /* list of gray objects */ 139 GCObject *grayagain; /* list of objects to be traversed atomically */ 140 GCObject *weak; /* list of tables with weak values */ 141 GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ 142 GCObject *allweak; /* list of all-weak tables */ 143 GCObject *tobefnz; /* list of userdata to be GC */ 144 GCObject *fixedgc; /* list of objects not to be collected */ 145 struct lua_State *twups; /* list of threads with open upvalues */ 146 unsigned int gcfinnum; /* number of finalizers to call in each GC step */ 147 int gcpause; /* size of pause between successive GCs */ 148 int gcstepmul; /* GC 'granularity' */ 149 lua_CFunction panic; /* to be called in unprotected errors */ 150 struct lua_State *mainthread; 151 const lua_Number *version; /* pointer to version number */ 152 TString *memerrmsg; /* memory-error message */ 153 TString *tmname[TM_N]; /* array with tag-method names */ 154 struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ 155 TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ 156 } global_State; 157 158 159 /* 160 ** 'per thread' state 161 */ 162 struct lua_State { 163 CommonHeader; 164 unsigned short nci; /* number of items in 'ci' list */ 165 lu_byte status; 166 StkId top; /* first free slot in the stack */ 167 global_State *l_G; 168 CallInfo *ci; /* call info for current function */ 169 const Instruction *oldpc; /* last pc traced */ 170 StkId stack_last; /* last free slot in the stack */ 171 StkId stack; /* stack base */ 172 UpVal *openupval; /* list of open upvalues in this stack */ 173 GCObject *gclist; 174 struct lua_State *twups; /* list of threads with open upvalues */ 175 struct lua_longjmp *errorJmp; /* current error recover point */ 176 CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ 177 volatile lua_Hook hook; 178 ptrdiff_t errfunc; /* current error handling function (stack index) */ 179 int stacksize; 180 int basehookcount; 181 int hookcount; 182 unsigned short nny; /* number of non-yieldable calls in stack */ 183 unsigned short nCcalls; /* number of nested C calls */ 184 l_signalT hookmask; 185 lu_byte allowhook; 186 }; 187 188 189 #define G(L) (L->l_G) 190 191 192 /* 193 ** Union of all collectable objects (only for conversions) 194 */ 195 union GCUnion { 196 GCObject gc; /* common header */ 197 struct TString ts; 198 struct Udata u; 199 union Closure cl; 200 struct Table h; 201 struct Proto p; 202 struct lua_State th; /* thread */ 203 }; 204 205 206 #define cast_u(o) cast(union GCUnion *, (o)) 207 208 /* macros to convert a GCObject into a specific value */ 209 #define gco2ts(o) \ 210 check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts)) 211 #define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u)) 212 #define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l)) 213 #define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c)) 214 #define gco2cl(o) \ 215 check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl)) 216 #define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h)) 217 #define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p)) 218 #define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th)) 219 220 221 /* macro to convert a Lua object into a GCObject */ 222 #define obj2gco(v) \ 223 check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc))) 224 225 226 /* actual number of total bytes allocated */ 227 #define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt) 228 229 LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); 230 LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 231 LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); 232 LUAI_FUNC void luaE_freeCI (lua_State *L); 233 LUAI_FUNC void luaE_shrinkCI (lua_State *L); 234 235 236 #endif 237 238