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