ba8b5c17 |
1 | /* |
2 | ** $Id: lstate.c,v 2.99 2012/10/02 17:40:53 roberto Exp $ |
3 | ** Global State |
4 | ** See Copyright Notice in lua.h |
5 | */ |
6 | |
7 | |
8 | #include <stddef.h> |
9 | #include <string.h> |
10 | |
11 | #define lstate_c |
12 | #define LUA_CORE |
13 | |
14 | #include "lua.h" |
15 | |
16 | #include "lapi.h" |
17 | #include "ldebug.h" |
18 | #include "ldo.h" |
19 | #include "lfunc.h" |
20 | #include "lgc.h" |
21 | #include "llex.h" |
22 | #include "lmem.h" |
23 | #include "lstate.h" |
24 | #include "lstring.h" |
25 | #include "ltable.h" |
26 | #include "ltm.h" |
27 | |
28 | |
29 | #if !defined(LUAI_GCPAUSE) |
30 | #define LUAI_GCPAUSE 200 /* 200% */ |
31 | #endif |
32 | |
33 | #if !defined(LUAI_GCMAJOR) |
34 | #define LUAI_GCMAJOR 200 /* 200% */ |
35 | #endif |
36 | |
37 | #if !defined(LUAI_GCMUL) |
38 | #define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */ |
39 | #endif |
40 | |
41 | |
42 | #define MEMERRMSG "not enough memory" |
43 | |
44 | |
45 | /* |
46 | ** a macro to help the creation of a unique random seed when a state is |
47 | ** created; the seed is used to randomize hashes. |
48 | */ |
49 | #if !defined(luai_makeseed) |
50 | #include <time.h> |
51 | #define luai_makeseed() cast(unsigned int, time(NULL)) |
52 | #endif |
53 | |
54 | |
55 | |
56 | /* |
57 | ** thread state + extra space |
58 | */ |
59 | typedef struct LX { |
60 | #if defined(LUAI_EXTRASPACE) |
61 | char buff[LUAI_EXTRASPACE]; |
62 | #endif |
63 | lua_State l; |
64 | } LX; |
65 | |
66 | |
67 | /* |
68 | ** Main thread combines a thread state and the global state |
69 | */ |
70 | typedef struct LG { |
71 | LX l; |
72 | global_State g; |
73 | } LG; |
74 | |
75 | |
76 | |
77 | #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) |
78 | |
79 | |
80 | /* |
81 | ** Compute an initial seed as random as possible. In ANSI, rely on |
82 | ** Address Space Layout Randomization (if present) to increase |
83 | ** randomness.. |
84 | */ |
85 | #define addbuff(b,p,e) \ |
86 | { size_t t = cast(size_t, e); \ |
87 | memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); } |
88 | |
89 | static unsigned int makeseed (lua_State *L) { |
90 | char buff[4 * sizeof(size_t)]; |
91 | unsigned int h = luai_makeseed(); |
92 | int p = 0; |
93 | addbuff(buff, p, L); /* heap variable */ |
94 | addbuff(buff, p, &h); /* local variable */ |
95 | addbuff(buff, p, luaO_nilobject); /* global variable */ |
96 | addbuff(buff, p, &lua_newstate); /* public function */ |
97 | lua_assert(p == sizeof(buff)); |
98 | return luaS_hash(buff, p, h); |
99 | } |
100 | |
101 | |
102 | /* |
103 | ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) |
104 | ** invariant |
105 | */ |
106 | void luaE_setdebt (global_State *g, l_mem debt) { |
107 | g->totalbytes -= (debt - g->GCdebt); |
108 | g->GCdebt = debt; |
109 | } |
110 | |
111 | |
112 | CallInfo *luaE_extendCI (lua_State *L) { |
113 | CallInfo *ci = luaM_new(L, CallInfo); |
114 | lua_assert(L->ci->next == NULL); |
115 | L->ci->next = ci; |
116 | ci->previous = L->ci; |
117 | ci->next = NULL; |
118 | return ci; |
119 | } |
120 | |
121 | |
122 | void luaE_freeCI (lua_State *L) { |
123 | CallInfo *ci = L->ci; |
124 | CallInfo *next = ci->next; |
125 | ci->next = NULL; |
126 | while ((ci = next) != NULL) { |
127 | next = ci->next; |
128 | luaM_free(L, ci); |
129 | } |
130 | } |
131 | |
132 | |
133 | static void stack_init (lua_State *L1, lua_State *L) { |
134 | int i; CallInfo *ci; |
135 | /* initialize stack array */ |
136 | L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue); |
137 | L1->stacksize = BASIC_STACK_SIZE; |
138 | for (i = 0; i < BASIC_STACK_SIZE; i++) |
139 | setnilvalue(L1->stack + i); /* erase new stack */ |
140 | L1->top = L1->stack; |
141 | L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK; |
142 | /* initialize first ci */ |
143 | ci = &L1->base_ci; |
144 | ci->next = ci->previous = NULL; |
145 | ci->callstatus = 0; |
146 | ci->func = L1->top; |
147 | setnilvalue(L1->top++); /* 'function' entry for this 'ci' */ |
148 | ci->top = L1->top + LUA_MINSTACK; |
149 | L1->ci = ci; |
150 | } |
151 | |
152 | |
153 | static void freestack (lua_State *L) { |
154 | if (L->stack == NULL) |
155 | return; /* stack not completely built yet */ |
156 | L->ci = &L->base_ci; /* free the entire 'ci' list */ |
157 | luaE_freeCI(L); |
158 | luaM_freearray(L, L->stack, L->stacksize); /* free stack array */ |
159 | } |
160 | |
161 | |
162 | /* |
163 | ** Create registry table and its predefined values |
164 | */ |
165 | static void init_registry (lua_State *L, global_State *g) { |
166 | TValue mt; |
167 | /* create registry */ |
168 | Table *registry = luaH_new(L); |
169 | sethvalue(L, &g->l_registry, registry); |
170 | luaH_resize(L, registry, LUA_RIDX_LAST, 0); |
171 | /* registry[LUA_RIDX_MAINTHREAD] = L */ |
172 | setthvalue(L, &mt, L); |
173 | luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &mt); |
174 | /* registry[LUA_RIDX_GLOBALS] = table of globals */ |
175 | sethvalue(L, &mt, luaH_new(L)); |
176 | luaH_setint(L, registry, LUA_RIDX_GLOBALS, &mt); |
177 | } |
178 | |
179 | |
180 | /* |
181 | ** open parts of the state that may cause memory-allocation errors |
182 | */ |
183 | static void f_luaopen (lua_State *L, void *ud) { |
184 | global_State *g = G(L); |
185 | UNUSED(ud); |
186 | stack_init(L, L); /* init stack */ |
187 | init_registry(L, g); |
188 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ |
189 | luaT_init(L); |
190 | luaX_init(L); |
191 | /* pre-create memory-error message */ |
192 | g->memerrmsg = luaS_newliteral(L, MEMERRMSG); |
193 | luaS_fix(g->memerrmsg); /* it should never be collected */ |
194 | g->gcrunning = 1; /* allow gc */ |
195 | } |
196 | |
197 | |
198 | /* |
199 | ** preinitialize a state with consistent values without allocating |
200 | ** any memory (to avoid errors) |
201 | */ |
202 | static void preinit_state (lua_State *L, global_State *g) { |
203 | G(L) = g; |
204 | L->stack = NULL; |
205 | L->ci = NULL; |
206 | L->stacksize = 0; |
207 | L->errorJmp = NULL; |
208 | L->nCcalls = 0; |
209 | L->hook = NULL; |
210 | L->hookmask = 0; |
211 | L->basehookcount = 0; |
212 | L->allowhook = 1; |
213 | resethookcount(L); |
214 | L->openupval = NULL; |
215 | L->nny = 1; |
216 | L->status = LUA_OK; |
217 | L->errfunc = 0; |
218 | } |
219 | |
220 | |
221 | static void close_state (lua_State *L) { |
222 | global_State *g = G(L); |
223 | luaF_close(L, L->stack); /* close all upvalues for this thread */ |
224 | luaC_freeallobjects(L); /* collect all objects */ |
225 | luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); |
226 | luaZ_freebuffer(L, &g->buff); |
227 | freestack(L); |
228 | lua_assert(gettotalbytes(g) == sizeof(LG)); |
229 | (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ |
230 | } |
231 | |
232 | |
233 | LUA_API lua_State *lua_newthread (lua_State *L) { |
234 | lua_State *L1; |
235 | lua_lock(L); |
236 | luaC_checkGC(L); |
237 | L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX), NULL, offsetof(LX, l))->th; |
238 | setthvalue(L, L->top, L1); |
239 | api_incr_top(L); |
240 | preinit_state(L1, G(L)); |
241 | L1->hookmask = L->hookmask; |
242 | L1->basehookcount = L->basehookcount; |
243 | L1->hook = L->hook; |
244 | resethookcount(L1); |
245 | luai_userstatethread(L, L1); |
246 | stack_init(L1, L); /* init stack */ |
247 | lua_unlock(L); |
248 | return L1; |
249 | } |
250 | |
251 | |
252 | void luaE_freethread (lua_State *L, lua_State *L1) { |
253 | LX *l = fromstate(L1); |
254 | luaF_close(L1, L1->stack); /* close all upvalues for this thread */ |
255 | lua_assert(L1->openupval == NULL); |
256 | luai_userstatefree(L, L1); |
257 | freestack(L1); |
258 | luaM_free(L, l); |
259 | } |
260 | |
261 | |
262 | LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { |
263 | int i; |
264 | lua_State *L; |
265 | global_State *g; |
266 | LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG))); |
267 | if (l == NULL) return NULL; |
268 | L = &l->l.l; |
269 | g = &l->g; |
270 | L->next = NULL; |
271 | L->tt = LUA_TTHREAD; |
272 | g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); |
273 | L->marked = luaC_white(g); |
274 | g->gckind = KGC_NORMAL; |
275 | preinit_state(L, g); |
276 | g->frealloc = f; |
277 | g->ud = ud; |
278 | g->mainthread = L; |
279 | g->seed = makeseed(L); |
280 | g->uvhead.u.l.prev = &g->uvhead; |
281 | g->uvhead.u.l.next = &g->uvhead; |
282 | g->gcrunning = 0; /* no GC while building state */ |
283 | g->GCestimate = 0; |
284 | g->strt.size = 0; |
285 | g->strt.nuse = 0; |
286 | g->strt.hash = NULL; |
287 | setnilvalue(&g->l_registry); |
288 | luaZ_initbuffer(L, &g->buff); |
289 | g->panic = NULL; |
290 | g->version = lua_version(NULL); |
291 | g->gcstate = GCSpause; |
292 | g->allgc = NULL; |
293 | g->finobj = NULL; |
294 | g->tobefnz = NULL; |
295 | g->sweepgc = g->sweepfin = NULL; |
296 | g->gray = g->grayagain = NULL; |
297 | g->weak = g->ephemeron = g->allweak = NULL; |
298 | g->totalbytes = sizeof(LG); |
299 | g->GCdebt = 0; |
300 | g->gcpause = LUAI_GCPAUSE; |
301 | g->gcmajorinc = LUAI_GCMAJOR; |
302 | g->gcstepmul = LUAI_GCMUL; |
303 | for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; |
304 | if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { |
305 | /* memory allocation error: free partial state */ |
306 | close_state(L); |
307 | L = NULL; |
308 | } |
309 | else |
310 | luai_userstateopen(L); |
311 | return L; |
312 | } |
313 | |
314 | |
315 | LUA_API void lua_close (lua_State *L) { |
316 | L = G(L)->mainthread; /* only the main thread can be closed */ |
317 | lua_lock(L); |
318 | luai_userstateclose(L); |
319 | close_state(L); |
320 | } |
321 | |
322 | |