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