xref: /freebsd/contrib/lua/src/lstate.h (revision e112e9d255e25e8377d107841b263eefab2f13a0)
18e3e3a7aSWarner Losh /*
2*e112e9d2SKyle Evans ** $Id: lstate.h,v 2.133.1.1 2017/04/19 17:39:34 roberto Exp $
38e3e3a7aSWarner Losh ** Global State
48e3e3a7aSWarner Losh ** See Copyright Notice in lua.h
58e3e3a7aSWarner Losh */
68e3e3a7aSWarner Losh 
78e3e3a7aSWarner Losh #ifndef lstate_h
88e3e3a7aSWarner Losh #define lstate_h
98e3e3a7aSWarner Losh 
108e3e3a7aSWarner Losh #include "lua.h"
118e3e3a7aSWarner Losh 
128e3e3a7aSWarner Losh #include "lobject.h"
138e3e3a7aSWarner Losh #include "ltm.h"
148e3e3a7aSWarner Losh #include "lzio.h"
158e3e3a7aSWarner Losh 
168e3e3a7aSWarner Losh 
178e3e3a7aSWarner Losh /*
188e3e3a7aSWarner Losh 
198e3e3a7aSWarner Losh ** Some notes about garbage-collected objects: All objects in Lua must
208e3e3a7aSWarner Losh ** be kept somehow accessible until being freed, so all objects always
218e3e3a7aSWarner Losh ** belong to one (and only one) of these lists, using field 'next' of
228e3e3a7aSWarner Losh ** the 'CommonHeader' for the link:
238e3e3a7aSWarner Losh **
248e3e3a7aSWarner Losh ** 'allgc': all objects not marked for finalization;
258e3e3a7aSWarner Losh ** 'finobj': all objects marked for finalization;
268e3e3a7aSWarner Losh ** 'tobefnz': all objects ready to be finalized;
278e3e3a7aSWarner Losh ** 'fixedgc': all objects that are not to be collected (currently
288e3e3a7aSWarner Losh ** only small strings, such as reserved words).
29*e112e9d2SKyle Evans **
30*e112e9d2SKyle Evans ** Moreover, there is another set of lists that control gray objects.
31*e112e9d2SKyle Evans ** These lists are linked by fields 'gclist'. (All objects that
32*e112e9d2SKyle Evans ** can become gray have such a field. The field is not the same
33*e112e9d2SKyle Evans ** in all objects, but it always has this name.)  Any gray object
34*e112e9d2SKyle Evans ** must belong to one of these lists, and all objects in these lists
35*e112e9d2SKyle Evans ** must be gray:
36*e112e9d2SKyle Evans **
37*e112e9d2SKyle Evans ** 'gray': regular gray objects, still waiting to be visited.
38*e112e9d2SKyle Evans ** 'grayagain': objects that must be revisited at the atomic phase.
39*e112e9d2SKyle Evans **   That includes
40*e112e9d2SKyle Evans **   - black objects got in a write barrier;
41*e112e9d2SKyle Evans **   - all kinds of weak tables during propagation phase;
42*e112e9d2SKyle Evans **   - all threads.
43*e112e9d2SKyle Evans ** 'weak': tables with weak values to be cleared;
44*e112e9d2SKyle Evans ** 'ephemeron': ephemeron tables with white->white entries;
45*e112e9d2SKyle Evans ** 'allweak': tables with weak keys and/or weak values to be cleared.
46*e112e9d2SKyle Evans ** The last three lists are used only during the atomic phase.
478e3e3a7aSWarner Losh 
488e3e3a7aSWarner Losh */
498e3e3a7aSWarner Losh 
508e3e3a7aSWarner Losh 
518e3e3a7aSWarner Losh struct lua_longjmp;  /* defined in ldo.c */
528e3e3a7aSWarner Losh 
538e3e3a7aSWarner Losh 
548e3e3a7aSWarner Losh /*
558e3e3a7aSWarner Losh ** Atomic type (relative to signals) to better ensure that 'lua_sethook'
568e3e3a7aSWarner Losh ** is thread safe
578e3e3a7aSWarner Losh */
588e3e3a7aSWarner Losh #if !defined(l_signalT)
598e3e3a7aSWarner Losh #include <signal.h>
608e3e3a7aSWarner Losh #define l_signalT	sig_atomic_t
618e3e3a7aSWarner Losh #endif
628e3e3a7aSWarner Losh 
638e3e3a7aSWarner Losh 
648e3e3a7aSWarner Losh /* extra stack space to handle TM calls and some other extras */
658e3e3a7aSWarner Losh #define EXTRA_STACK   5
668e3e3a7aSWarner Losh 
678e3e3a7aSWarner Losh 
688e3e3a7aSWarner Losh #define BASIC_STACK_SIZE        (2*LUA_MINSTACK)
698e3e3a7aSWarner Losh 
708e3e3a7aSWarner Losh 
718e3e3a7aSWarner Losh /* kinds of Garbage Collection */
728e3e3a7aSWarner Losh #define KGC_NORMAL	0
738e3e3a7aSWarner Losh #define KGC_EMERGENCY	1	/* gc was forced by an allocation failure */
748e3e3a7aSWarner Losh 
758e3e3a7aSWarner Losh 
768e3e3a7aSWarner Losh typedef struct stringtable {
778e3e3a7aSWarner Losh   TString **hash;
788e3e3a7aSWarner Losh   int nuse;  /* number of elements */
798e3e3a7aSWarner Losh   int size;
808e3e3a7aSWarner Losh } stringtable;
818e3e3a7aSWarner Losh 
828e3e3a7aSWarner Losh 
838e3e3a7aSWarner Losh /*
848e3e3a7aSWarner Losh ** Information about a call.
858e3e3a7aSWarner Losh ** When a thread yields, 'func' is adjusted to pretend that the
868e3e3a7aSWarner Losh ** top function has only the yielded values in its stack; in that
878e3e3a7aSWarner Losh ** case, the actual 'func' value is saved in field 'extra'.
888e3e3a7aSWarner Losh ** When a function calls another with a continuation, 'extra' keeps
898e3e3a7aSWarner Losh ** the function index so that, in case of errors, the continuation
908e3e3a7aSWarner Losh ** function can be called with the correct top.
918e3e3a7aSWarner Losh */
928e3e3a7aSWarner Losh typedef struct CallInfo {
938e3e3a7aSWarner Losh   StkId func;  /* function index in the stack */
948e3e3a7aSWarner Losh   StkId	top;  /* top for this function */
958e3e3a7aSWarner Losh   struct CallInfo *previous, *next;  /* dynamic call link */
968e3e3a7aSWarner Losh   union {
978e3e3a7aSWarner Losh     struct {  /* only for Lua functions */
988e3e3a7aSWarner Losh       StkId base;  /* base for this function */
998e3e3a7aSWarner Losh       const Instruction *savedpc;
1008e3e3a7aSWarner Losh     } l;
1018e3e3a7aSWarner Losh     struct {  /* only for C functions */
1028e3e3a7aSWarner Losh       lua_KFunction k;  /* continuation in case of yields */
1038e3e3a7aSWarner Losh       ptrdiff_t old_errfunc;
1048e3e3a7aSWarner Losh       lua_KContext ctx;  /* context info. in case of yields */
1058e3e3a7aSWarner Losh     } c;
1068e3e3a7aSWarner Losh   } u;
1078e3e3a7aSWarner Losh   ptrdiff_t extra;
1088e3e3a7aSWarner Losh   short nresults;  /* expected number of results from this function */
1098e3e3a7aSWarner Losh   unsigned short callstatus;
1108e3e3a7aSWarner Losh } CallInfo;
1118e3e3a7aSWarner Losh 
1128e3e3a7aSWarner Losh 
1138e3e3a7aSWarner Losh /*
1148e3e3a7aSWarner Losh ** Bits in CallInfo status
1158e3e3a7aSWarner Losh */
1168e3e3a7aSWarner Losh #define CIST_OAH	(1<<0)	/* original value of 'allowhook' */
1178e3e3a7aSWarner Losh #define CIST_LUA	(1<<1)	/* call is running a Lua function */
1188e3e3a7aSWarner Losh #define CIST_HOOKED	(1<<2)	/* call is running a debug hook */
1198e3e3a7aSWarner Losh #define CIST_FRESH	(1<<3)	/* call is running on a fresh invocation
1208e3e3a7aSWarner Losh                                    of luaV_execute */
1218e3e3a7aSWarner Losh #define CIST_YPCALL	(1<<4)	/* call is a yieldable protected call */
1228e3e3a7aSWarner Losh #define CIST_TAIL	(1<<5)	/* call was tail called */
1238e3e3a7aSWarner Losh #define CIST_HOOKYIELD	(1<<6)	/* last hook called yielded */
1248e3e3a7aSWarner Losh #define CIST_LEQ	(1<<7)  /* using __lt for __le */
1258e3e3a7aSWarner Losh #define CIST_FIN	(1<<8)  /* call is running a finalizer */
1268e3e3a7aSWarner Losh 
1278e3e3a7aSWarner Losh #define isLua(ci)	((ci)->callstatus & CIST_LUA)
1288e3e3a7aSWarner Losh 
1298e3e3a7aSWarner Losh /* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */
1308e3e3a7aSWarner Losh #define setoah(st,v)	((st) = ((st) & ~CIST_OAH) | (v))
1318e3e3a7aSWarner Losh #define getoah(st)	((st) & CIST_OAH)
1328e3e3a7aSWarner Losh 
1338e3e3a7aSWarner Losh 
1348e3e3a7aSWarner Losh /*
1358e3e3a7aSWarner Losh ** 'global state', shared by all threads of this state
1368e3e3a7aSWarner Losh */
1378e3e3a7aSWarner Losh typedef struct global_State {
1388e3e3a7aSWarner Losh   lua_Alloc frealloc;  /* function to reallocate memory */
1398e3e3a7aSWarner Losh   void *ud;         /* auxiliary data to 'frealloc' */
1408e3e3a7aSWarner Losh   l_mem totalbytes;  /* number of bytes currently allocated - GCdebt */
1418e3e3a7aSWarner Losh   l_mem GCdebt;  /* bytes allocated not yet compensated by the collector */
1428e3e3a7aSWarner Losh   lu_mem GCmemtrav;  /* memory traversed by the GC */
1438e3e3a7aSWarner Losh   lu_mem GCestimate;  /* an estimate of the non-garbage memory in use */
1448e3e3a7aSWarner Losh   stringtable strt;  /* hash table for strings */
1458e3e3a7aSWarner Losh   TValue l_registry;
1468e3e3a7aSWarner Losh   unsigned int seed;  /* randomized seed for hashes */
1478e3e3a7aSWarner Losh   lu_byte currentwhite;
1488e3e3a7aSWarner Losh   lu_byte gcstate;  /* state of garbage collector */
1498e3e3a7aSWarner Losh   lu_byte gckind;  /* kind of GC running */
1508e3e3a7aSWarner Losh   lu_byte gcrunning;  /* true if GC is running */
1518e3e3a7aSWarner Losh   GCObject *allgc;  /* list of all collectable objects */
1528e3e3a7aSWarner Losh   GCObject **sweepgc;  /* current position of sweep in list */
1538e3e3a7aSWarner Losh   GCObject *finobj;  /* list of collectable objects with finalizers */
1548e3e3a7aSWarner Losh   GCObject *gray;  /* list of gray objects */
1558e3e3a7aSWarner Losh   GCObject *grayagain;  /* list of objects to be traversed atomically */
1568e3e3a7aSWarner Losh   GCObject *weak;  /* list of tables with weak values */
1578e3e3a7aSWarner Losh   GCObject *ephemeron;  /* list of ephemeron tables (weak keys) */
1588e3e3a7aSWarner Losh   GCObject *allweak;  /* list of all-weak tables */
1598e3e3a7aSWarner Losh   GCObject *tobefnz;  /* list of userdata to be GC */
1608e3e3a7aSWarner Losh   GCObject *fixedgc;  /* list of objects not to be collected */
1618e3e3a7aSWarner Losh   struct lua_State *twups;  /* list of threads with open upvalues */
1628e3e3a7aSWarner Losh   unsigned int gcfinnum;  /* number of finalizers to call in each GC step */
1638e3e3a7aSWarner Losh   int gcpause;  /* size of pause between successive GCs */
1648e3e3a7aSWarner Losh   int gcstepmul;  /* GC 'granularity' */
1658e3e3a7aSWarner Losh   lua_CFunction panic;  /* to be called in unprotected errors */
1668e3e3a7aSWarner Losh   struct lua_State *mainthread;
1678e3e3a7aSWarner Losh   const lua_Number *version;  /* pointer to version number */
1688e3e3a7aSWarner Losh   TString *memerrmsg;  /* memory-error message */
1698e3e3a7aSWarner Losh   TString *tmname[TM_N];  /* array with tag-method names */
1708e3e3a7aSWarner Losh   struct Table *mt[LUA_NUMTAGS];  /* metatables for basic types */
1718e3e3a7aSWarner Losh   TString *strcache[STRCACHE_N][STRCACHE_M];  /* cache for strings in API */
1728e3e3a7aSWarner Losh } global_State;
1738e3e3a7aSWarner Losh 
1748e3e3a7aSWarner Losh 
1758e3e3a7aSWarner Losh /*
1768e3e3a7aSWarner Losh ** 'per thread' state
1778e3e3a7aSWarner Losh */
1788e3e3a7aSWarner Losh struct lua_State {
1798e3e3a7aSWarner Losh   CommonHeader;
1808e3e3a7aSWarner Losh   unsigned short nci;  /* number of items in 'ci' list */
1818e3e3a7aSWarner Losh   lu_byte status;
1828e3e3a7aSWarner Losh   StkId top;  /* first free slot in the stack */
1838e3e3a7aSWarner Losh   global_State *l_G;
1848e3e3a7aSWarner Losh   CallInfo *ci;  /* call info for current function */
1858e3e3a7aSWarner Losh   const Instruction *oldpc;  /* last pc traced */
1868e3e3a7aSWarner Losh   StkId stack_last;  /* last free slot in the stack */
1878e3e3a7aSWarner Losh   StkId stack;  /* stack base */
1888e3e3a7aSWarner Losh   UpVal *openupval;  /* list of open upvalues in this stack */
1898e3e3a7aSWarner Losh   GCObject *gclist;
1908e3e3a7aSWarner Losh   struct lua_State *twups;  /* list of threads with open upvalues */
1918e3e3a7aSWarner Losh   struct lua_longjmp *errorJmp;  /* current error recover point */
1928e3e3a7aSWarner Losh   CallInfo base_ci;  /* CallInfo for first level (C calling Lua) */
1938e3e3a7aSWarner Losh   volatile lua_Hook hook;
1948e3e3a7aSWarner Losh   ptrdiff_t errfunc;  /* current error handling function (stack index) */
1958e3e3a7aSWarner Losh   int stacksize;
1968e3e3a7aSWarner Losh   int basehookcount;
1978e3e3a7aSWarner Losh   int hookcount;
1988e3e3a7aSWarner Losh   unsigned short nny;  /* number of non-yieldable calls in stack */
1998e3e3a7aSWarner Losh   unsigned short nCcalls;  /* number of nested C calls */
2008e3e3a7aSWarner Losh   l_signalT hookmask;
2018e3e3a7aSWarner Losh   lu_byte allowhook;
2028e3e3a7aSWarner Losh };
2038e3e3a7aSWarner Losh 
2048e3e3a7aSWarner Losh 
2058e3e3a7aSWarner Losh #define G(L)	(L->l_G)
2068e3e3a7aSWarner Losh 
2078e3e3a7aSWarner Losh 
2088e3e3a7aSWarner Losh /*
2098e3e3a7aSWarner Losh ** Union of all collectable objects (only for conversions)
2108e3e3a7aSWarner Losh */
2118e3e3a7aSWarner Losh union GCUnion {
2128e3e3a7aSWarner Losh   GCObject gc;  /* common header */
2138e3e3a7aSWarner Losh   struct TString ts;
2148e3e3a7aSWarner Losh   struct Udata u;
2158e3e3a7aSWarner Losh   union Closure cl;
2168e3e3a7aSWarner Losh   struct Table h;
2178e3e3a7aSWarner Losh   struct Proto p;
2188e3e3a7aSWarner Losh   struct lua_State th;  /* thread */
2198e3e3a7aSWarner Losh };
2208e3e3a7aSWarner Losh 
2218e3e3a7aSWarner Losh 
2228e3e3a7aSWarner Losh #define cast_u(o)	cast(union GCUnion *, (o))
2238e3e3a7aSWarner Losh 
2248e3e3a7aSWarner Losh /* macros to convert a GCObject into a specific value */
2258e3e3a7aSWarner Losh #define gco2ts(o)  \
2268e3e3a7aSWarner Losh 	check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts))
2278e3e3a7aSWarner Losh #define gco2u(o)  check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u))
2288e3e3a7aSWarner Losh #define gco2lcl(o)  check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l))
2298e3e3a7aSWarner Losh #define gco2ccl(o)  check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c))
2308e3e3a7aSWarner Losh #define gco2cl(o)  \
2318e3e3a7aSWarner Losh 	check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl))
2328e3e3a7aSWarner Losh #define gco2t(o)  check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h))
2338e3e3a7aSWarner Losh #define gco2p(o)  check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p))
2348e3e3a7aSWarner Losh #define gco2th(o)  check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th))
2358e3e3a7aSWarner Losh 
2368e3e3a7aSWarner Losh 
2378e3e3a7aSWarner Losh /* macro to convert a Lua object into a GCObject */
2388e3e3a7aSWarner Losh #define obj2gco(v) \
2398e3e3a7aSWarner Losh 	check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc)))
2408e3e3a7aSWarner Losh 
2418e3e3a7aSWarner Losh 
2428e3e3a7aSWarner Losh /* actual number of total bytes allocated */
2438e3e3a7aSWarner Losh #define gettotalbytes(g)	cast(lu_mem, (g)->totalbytes + (g)->GCdebt)
2448e3e3a7aSWarner Losh 
2458e3e3a7aSWarner Losh LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
2468e3e3a7aSWarner Losh LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
2478e3e3a7aSWarner Losh LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
2488e3e3a7aSWarner Losh LUAI_FUNC void luaE_freeCI (lua_State *L);
2498e3e3a7aSWarner Losh LUAI_FUNC void luaE_shrinkCI (lua_State *L);
2508e3e3a7aSWarner Losh 
2518e3e3a7aSWarner Losh 
2528e3e3a7aSWarner Losh #endif
2538e3e3a7aSWarner Losh 
254