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