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