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