xref: /freebsd/sys/contrib/openzfs/module/lua/lstate.c (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
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 #define lstate_c
9 #define LUA_CORE
10 
11 #include <sys/lua/lua.h>
12 
13 #include "lapi.h"
14 #include "ldebug.h"
15 #include "ldo.h"
16 #include "lfunc.h"
17 #include "lgc.h"
18 #include "llex.h"
19 #include "lmem.h"
20 #include "lstate.h"
21 #include "lstring.h"
22 #include "ltable.h"
23 #include "ltm.h"
24 
25 
26 #if !defined(LUAI_GCPAUSE)
27 #define LUAI_GCPAUSE	200  /* 200% */
28 #endif
29 
30 #if !defined(LUAI_GCMAJOR)
31 #define LUAI_GCMAJOR	200  /* 200% */
32 #endif
33 
34 #if !defined(LUAI_GCMUL)
35 #define LUAI_GCMUL	200 /* GC runs 'twice the speed' of memory allocation */
36 #endif
37 
38 
39 #define MEMERRMSG	"not enough memory"
40 
41 
42 /*
43 ** a macro to help the creation of a unique random seed when a state is
44 ** created; the seed is used to randomize hashes.
45 */
46 #if !defined(luai_makeseed)
47 #define luai_makeseed()		cast(unsigned int, gethrtime())
48 #endif
49 
50 
51 
52 /*
53 ** thread state + extra space
54 */
55 typedef struct LX {
56 #if defined(LUAI_EXTRASPACE)
57   char buff[LUAI_EXTRASPACE];
58 #endif
59   lua_State l;
60 } LX;
61 
62 
63 /*
64 ** Main thread combines a thread state and the global state
65 */
66 typedef struct LG {
67   LX l;
68   global_State g;
69 } LG;
70 
71 
72 
73 #define fromstate(L)	(cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
74 
75 
76 /*
77 ** Compute an initial seed as random as possible. In ANSI, rely on
78 ** Address Space Layout Randomization (if present) to increase
79 ** randomness..
80 */
81 #define addbuff(b,p,e) \
82   { size_t t = cast(size_t, e); \
83     memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); }
84 
85 static unsigned int makeseed (lua_State *L) {
86   char buff[4 * sizeof(size_t)];
87   unsigned int h = luai_makeseed();
88   int p = 0;
89   addbuff(buff, p, L);  /* heap variable */
90   addbuff(buff, p, &h);  /* local variable */
91   addbuff(buff, p, luaO_nilobject);  /* global variable */
92   addbuff(buff, p, &lua_newstate);  /* public function */
93   lua_assert(p == sizeof(buff));
94   return luaS_hash(buff, p, h);
95 }
96 
97 
98 /*
99 ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
100 ** invariant
101 */
102 void luaE_setdebt (global_State *g, l_mem debt) {
103   g->totalbytes -= (debt - g->GCdebt);
104   g->GCdebt = debt;
105 }
106 
107 
108 CallInfo *luaE_extendCI (lua_State *L) {
109   CallInfo *ci = luaM_new(L, CallInfo);
110   lua_assert(L->ci->next == NULL);
111   L->ci->next = ci;
112   ci->previous = L->ci;
113   ci->next = NULL;
114   return ci;
115 }
116 
117 
118 void luaE_freeCI (lua_State *L) {
119   CallInfo *ci = L->ci;
120   CallInfo *next = ci->next;
121   ci->next = NULL;
122   while ((ci = next) != NULL) {
123     next = ci->next;
124     luaM_free(L, ci);
125   }
126 }
127 
128 
129 static void stack_init (lua_State *L1, lua_State *L) {
130   int i; CallInfo *ci;
131   /* initialize stack array */
132   L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue);
133   L1->stacksize = BASIC_STACK_SIZE;
134   for (i = 0; i < BASIC_STACK_SIZE; i++)
135     setnilvalue(L1->stack + i);  /* erase new stack */
136   L1->top = L1->stack;
137   L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
138   /* initialize first ci */
139   ci = &L1->base_ci;
140   ci->next = ci->previous = NULL;
141   ci->callstatus = 0;
142   ci->func = L1->top;
143   setnilvalue(L1->top++);  /* 'function' entry for this 'ci' */
144   ci->top = L1->top + LUA_MINSTACK;
145   L1->ci = ci;
146 }
147 
148 
149 static void freestack (lua_State *L) {
150   if (L->stack == NULL)
151     return;  /* stack not completely built yet */
152   L->ci = &L->base_ci;  /* free the entire 'ci' list */
153   luaE_freeCI(L);
154   luaM_freearray(L, L->stack, L->stacksize);  /* free stack array */
155 }
156 
157 
158 /*
159 ** Create registry table and its predefined values
160 */
161 static void init_registry (lua_State *L, global_State *g) {
162   TValue mt;
163   /* create registry */
164   Table *registry = luaH_new(L);
165   sethvalue(L, &g->l_registry, registry);
166   luaH_resize(L, registry, LUA_RIDX_LAST, 0);
167   /* registry[LUA_RIDX_MAINTHREAD] = L */
168   setthvalue(L, &mt, L);
169   luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &mt);
170   /* registry[LUA_RIDX_GLOBALS] = table of globals */
171   sethvalue(L, &mt, luaH_new(L));
172   luaH_setint(L, registry, LUA_RIDX_GLOBALS, &mt);
173 }
174 
175 
176 /*
177 ** open parts of the state that may cause memory-allocation errors
178 */
179 static void f_luaopen (lua_State *L, void *ud) {
180   global_State *g = G(L);
181   UNUSED(ud);
182   stack_init(L, L);  /* init stack */
183   init_registry(L, g);
184   luaS_resize(L, MINSTRTABSIZE);  /* initial size of string table */
185   luaT_init(L);
186   luaX_init(L);
187   /* pre-create memory-error message */
188   g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
189   luaS_fix(g->memerrmsg);  /* it should never be collected */
190   g->gcrunning = 1;  /* allow gc */
191   g->version = lua_version(NULL);
192   luai_userstateopen(L);
193 }
194 
195 
196 /*
197 ** preinitialize a state with consistent values without allocating
198 ** any memory (to avoid errors)
199 */
200 static void preinit_state (lua_State *L, global_State *g) {
201   G(L) = g;
202   L->stack = NULL;
203   L->ci = NULL;
204   L->stacksize = 0;
205   L->errorJmp = NULL;
206   L->nCcalls = 0;
207   L->hook = NULL;
208   L->hookmask = 0;
209   L->basehookcount = 0;
210   L->allowhook = 1;
211   resethookcount(L);
212   L->openupval = NULL;
213   L->nny = 1;
214   L->status = LUA_OK;
215   L->errfunc = 0;
216   L->runerror = 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