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