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