source: trunk/src/lua/lstate.c @ 2542

Last change on this file since 2542 was 2542, checked in by sam, 10 years ago

lua: compilation fixes for non-quite-POSIX platforms.

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