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