The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lstate.cpp
Go to the documentation of this file.
1 /*
2 ** Global State
3 ** See Copyright Notice in lua.h
4 */
5 
6 
7 #include <stddef.h>
8 #include <string.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 #include <time.h>
50 #define luai_makeseed() cast(unsigned int, time(NULL))
51 #endif
52 
53 
54 
55 /*
56 ** thread state + extra space
57 */
58 typedef struct LX {
59 #if defined(LUAI_EXTRASPACE)
61 #endif
63 } LX;
64 
65 
66 /*
67 ** Main thread combines a thread state and the global state
68 */
69 typedef struct LG {
70  LX l;
72 } LG;
73 
74 
75 
76 #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
77 
78 
79 /*
80 ** Compute an initial seed as random as possible. In ANSI, rely on
81 ** Address Space Layout Randomization (if present) to increase
82 ** randomness..
83 */
84 #define addbuff(b,p,e) \
85  { size_t t = cast(size_t, e); \
86  memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); }
87 
88 static unsigned int makeseed (lua_State *L) {
89  char buff[4 * sizeof(size_t)];
90  unsigned int h = luai_makeseed();
91  int p = 0;
92  addbuff(buff, p, L); /* heap variable */
93  addbuff(buff, p, &h); /* local variable */
94  addbuff(buff, p, luaO_nilobject); /* global variable */
95  addbuff(buff, p, &lua_newstate); /* public function */
96  lua_assert(p == sizeof(buff));
97  return luaS_hash(buff, p, h);
98 }
99 
100 
101 /*
102 ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
103 ** invariant
104 */
106  g->totalbytes -= (debt - g->GCdebt);
107  g->GCdebt = debt;
108 }
109 
110 
112  CallInfo *ci = luaM_new(L, CallInfo);
113  lua_assert(L->ci->next == NULL);
114  L->ci->next = ci;
115  ci->previous = L->ci;
116  ci->next = NULL;
117  return ci;
118 }
119 
120 
122  CallInfo *ci = L->ci;
123  CallInfo *next = ci->next;
124  ci->next = NULL;
125  while ((ci = next) != NULL) {
126  next = ci->next;
127  luaM_free(L, ci);
128  }
129 }
130 
131 
132 static void stack_init (lua_State *L1, lua_State *L) {
133  int i; CallInfo *ci;
134  /* initialize stack array */
137  for (i = 0; i < BASIC_STACK_SIZE; i++)
138  setnilvalue(L1->stack + i); /* erase new stack */
139  L1->top = L1->stack;
140  L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
141  /* initialize first ci */
142  ci = &L1->base_ci;
143  ci->next = ci->previous = NULL;
144  ci->callstatus = 0;
145  ci->func = L1->top;
146  setnilvalue(L1->top++); /* 'function' entry for this 'ci' */
147  ci->top = L1->top + LUA_MINSTACK;
148  L1->ci = ci;
149 }
150 
151 
152 static void freestack (lua_State *L) {
153  if (L->stack == NULL)
154  return; /* stack not completely built yet */
155  L->ci = &L->base_ci; /* free the entire 'ci' list */
156  luaE_freeCI(L);
157  luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
158 }
159 
160 
161 /*
162 ** Create registry table and its predefined values
163 */
164 static void init_registry (lua_State *L, global_State *g) {
165  TValue mt;
166  /* create registry */
167  Table *registry = luaH_new(L);
168  sethvalue(L, &g->l_registry, registry);
169  luaH_resize(L, registry, LUA_RIDX_LAST, 0);
170  /* registry[LUA_RIDX_MAINTHREAD] = L */
171  setthvalue(L, &mt, L);
172  luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &mt);
173  /* registry[LUA_RIDX_GLOBALS] = table of globals */
174  sethvalue(L, &mt, luaH_new(L));
175  luaH_setint(L, registry, LUA_RIDX_GLOBALS, &mt);
176 }
177 
178 
179 /*
180 ** open parts of the state that may cause memory-allocation errors
181 */
182 static void f_luaopen (lua_State *L, void *ud) {
183  global_State *g = G(L);
184  UNUSED(ud);
185  stack_init(L, L); /* init stack */
186  init_registry(L, g);
187  luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
188  luaT_init(L);
189  luaX_init(L);
190  /* pre-create memory-error message */
192  luaS_fix(g->memerrmsg); /* it should never be collected */
193  g->gcrunning = 1; /* allow gc */
194  g->version = lua_version(NULL);
196 }
197 
198 
199 /*
200 ** preinitialize a state with consistent values without allocating
201 ** any memory (to avoid errors)
202 */
203 static void preinit_state (lua_State *L, global_State *g) {
204  G(L) = g;
205  L->stack = NULL;
206  L->ci = NULL;
207  L->stacksize = 0;
208  L->errorJmp = NULL;
209  L->nCcalls = 0;
210  L->hook = NULL;
211  L->hookmask = 0;
212  L->basehookcount = 0;
213  L->allowhook = 1;
214  resethookcount(L);
215  L->openupval = NULL;
216  L->nny = 1;
217  L->status = LUA_OK;
218  L->errfunc = 0;
219 }
220 
221 
222 static void close_state (lua_State *L) {
223  global_State *g = G(L);
224  luaF_close(L, L->stack); /* close all upvalues for this thread */
225  luaC_freeallobjects(L); /* collect all objects */
226  if (g->version) /* closing a fully built state? */
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 
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 
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 
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;
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 = 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;
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  return L;
313 }
314 
315 
317  L = G(L)->mainthread; /* only the main thread can be closed */
318  lua_lock(L);
319  close_state(L);
320 }
321 
322 
#define LUAI_GCPAUSE
Definition: lstate.cpp:29
lu_byte allowhook
Definition: lstate.h:166
GCObject ** sweepfin
Definition: lstate.h:129
#define luaZ_freebuffer(L, buff)
Definition: lzio.h:41
void luaE_setdebt(global_State *g, l_mem debt)
Definition: lstate.cpp:105
void luaE_freethread(lua_State *L, lua_State *L1)
Definition: lstate.cpp:255
static unsigned int makeseed(lua_State *L)
Definition: lstate.cpp:88
#define LUAI_GCMAJOR
Definition: lstate.cpp:33
#define luaZ_initbuffer(L, buff)
Definition: lzio.h:28
#define LUA_TTHREAD
Definition: lua.h:85
l_mem GCdebt
Definition: lstate.h:115
ptrdiff_t errfunc
Definition: lstate.h:173
#define luai_userstateclose(L)
Definition: llimits.h:173
Definition: lstate.cpp:69
#define luai_userstatefree(L, L1)
Definition: llimits.h:181
void luaF_close(lua_State *L, StkId level)
Definition: lfunc.cpp:88
struct LX LX
#define luaM_newvector(L, n, t)
Definition: lmem.h:34
Definition: lobject.h:559
struct UpVal::@9::@10 l
lu_byte hookmask
Definition: lstate.h:165
unsigned int luaS_hash(const char *str, size_t l, unsigned int seed)
Definition: lstring.cpp:50
struct lua_State th
Definition: lstate.h:192
void luaH_resize(lua_State *L, Table *t, int nasize, int nhsize)
Definition: ltable.cpp:303
#define luaM_freearray(L, b, n)
Definition: lmem.h:30
void *(* lua_Alloc)(void *ud, void *ptr, size_t osize, size_t nsize)
Definition: lua.h:69
StkId stack
Definition: lstate.h:161
#define LUAI_EXTRASPACE
Definition: luaconf.h:573
LUAI_MEM l_mem
Definition: llimits.h:21
GCObject ** hash
Definition: lstate.h:59
struct LG LG
#define GCSpause
Definition: lgc.h:43
int gcmajorinc
Definition: lstate.h:139
lua_Alloc frealloc
Definition: lstate.h:112
int basehookcount
Definition: lstate.h:167
LUA_API const lua_Number * lua_version(lua_State *L)
Definition: lapi.cpp:131
GCObject * luaC_newobj(lua_State *L, int tt, size_t sz, GCObject **list, int offset)
Definition: lgc.cpp:211
#define luaS_fix(s)
Definition: lstring.h:21
StkId top
Definition: lstate.h:70
GCObject * finobj
Definition: lstate.h:127
#define cast(t, exp)
Definition: llimits.h:92
#define setnilvalue(obj)
Definition: lobject.h:189
#define MINSTRTABSIZE
Definition: llimits.h:143
#define G(L)
Definition: lstate.h:178
GCObject * tobefnz
Definition: lstate.h:135
void luaC_freeallobjects(lua_State *L)
Definition: lgc.cpp:982
#define lua_unlock(L)
Definition: llimits.h:155
GLboolean GLboolean g
Definition: glew.h:7319
#define luai_makeseed()
Definition: lstate.cpp:50
lu_byte callstatus
Definition: lstate.h:73
void luaE_freeCI(lua_State *L)
Definition: lstate.cpp:121
static void stack_init(lua_State *L1, lua_State *L)
Definition: lstate.cpp:132
static void close_state(lua_State *L)
Definition: lstate.cpp:222
#define fromstate(L)
Definition: lstate.cpp:76
GCObject * allgc
Definition: lstate.h:126
GLdouble l
Definition: glew.h:6966
const lua_Number * version
Definition: lstate.h:143
static void f_luaopen(lua_State *L, void *ud)
Definition: lstate.cpp:182
#define luaO_nilobject
Definition: lobject.h:587
#define LUA_RIDX_MAINTHREAD
Definition: lua.h:96
#define luaM_new(L, t)
Definition: lmem.h:33
StkId top
Definition: lstate.h:156
lu_byte gckind
Definition: lstate.h:123
#define luaS_newliteral(L, s)
Definition: lstring.h:18
StkId stack_last
Definition: lstate.h:160
#define sethvalue(L, obj, x)
Definition: lobject.h:230
#define MEMERRMSG
Definition: lstate.cpp:41
#define lua_lock(L)
Definition: llimits.h:154
#define gettotalbytes(g)
Definition: lstate.h:218
void * ud
Definition: lstate.h:113
struct lua_State * mainthread
Definition: lstate.h:142
unsigned short nny
Definition: lstate.h:163
#define LUA_RIDX_LAST
Definition: lua.h:98
#define luai_userstateopen(L)
Definition: llimits.h:169
unsigned short nCcalls
Definition: lstate.h:164
CallInfo * ci
Definition: lstate.h:158
lua_CFunction panic
Definition: lstate.h:141
void luaT_init(lua_State *L)
Definition: ltm.cpp:31
lu_byte currentwhite
Definition: lstate.h:121
#define api_incr_top(L)
Definition: lapi.h:13
LUA_API void lua_close(lua_State *L)
Definition: lstate.cpp:316
void luaX_init(lua_State *L)
Definition: llex.cpp:64
global_State g
Definition: lstate.cpp:71
#define LUA_API
Definition: luaconf.h:156
StkId func
Definition: lstate.h:69
TString * memerrmsg
Definition: lstate.h:144
#define luaM_free(L, b)
Definition: lmem.h:29
GLfloat GLfloat p
Definition: glew.h:12766
struct Table * mt[LUA_NUMTAGS]
Definition: lstate.h:146
#define bit2mask(b1, b2)
Definition: lgc.h:79
#define resethookcount(L)
Definition: ldebug.h:17
CallInfo base_ci
Definition: lstate.h:174
static void init_registry(lua_State *L, global_State *g)
Definition: lstate.cpp:164
GCObject * weak
Definition: lstate.h:132
Table * luaH_new(lua_State *L)
Definition: ltable.cpp:367
#define FIXEDBIT
Definition: lgc.h:91
GCObject * gray
Definition: lstate.h:130
#define LUAI_GCMUL
Definition: lstate.cpp:37
#define UNUSED(x)
Definition: global.hpp:56
#define WHITE0BIT
Definition: lgc.h:86
#define lua_assert(c)
Definition: llimits.h:65
static void freestack(lua_State *L)
Definition: lstate.cpp:152
Mbuffer buff
Definition: lstate.h:137
struct CallInfo * next
Definition: lstate.h:71
#define LUA_NUMTAGS
Definition: lua.h:87
int size
Definition: lstate.h:61
GCObject ** sweepgc
Definition: lstate.h:128
UpVal uvhead
Definition: lstate.h:136
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:5910
GCObject * grayagain
Definition: lstate.h:131
size_t i
Definition: function.cpp:1057
GCObject * allweak
Definition: lstate.h:134
void luaH_setint(lua_State *L, Table *t, int key, TValue *value)
Definition: ltable.cpp:517
#define LUA_RIDX_GLOBALS
Definition: lua.h:97
LUA_API lua_State * lua_newstate(lua_Alloc f, void *ud)
Definition: lstate.cpp:265
#define KGC_NORMAL
Definition: lstate.h:53
#define LUA_MINSTACK
Definition: lua.h:92
int gcpause
Definition: lstate.h:138
int luaD_rawrunprotected(lua_State *L, Pfunc f, void *ud)
Definition: ldo.cpp:147
#define addbuff(b, p, e)
Definition: lstate.cpp:84
#define next(ls)
Definition: llex.cpp:27
char buff[LUAI_EXTRASPACE]
Definition: lstate.cpp:60
unsigned int seed
Definition: lstate.h:120
struct CallInfo * previous
Definition: lstate.h:71
#define LUA_OK
Definition: lua.h:44
Definition: lstate.cpp:58
#define g
Definition: glew.h:12730
lu_int32 nuse
Definition: lstate.h:60
void luaS_resize(lua_State *L, int newsize)
Definition: lstring.cpp:63
stringtable strt
Definition: lstate.h:118
lu_byte gcstate
Definition: lstate.h:122
#define luaC_checkGC(L)
Definition: lgc.h:123
int stacksize
Definition: lstate.h:162
CALLABLE_WRAPPER_INPUT_END if(key=="terrain")
lu_mem totalbytes
Definition: lstate.h:114
#define luai_userstatethread(L, L1)
Definition: llimits.h:177
LX l
Definition: lstate.cpp:70
#define setthvalue(L, obj, x)
Definition: lobject.h:215
static void preinit_state(lua_State *L, global_State *g)
Definition: lstate.cpp:203
LUA_API lua_State * lua_newthread(lua_State *L)
Definition: lstate.cpp:236
lua_Hook hook
Definition: lstate.h:169
GCObject * ephemeron
Definition: lstate.h:133
union UpVal::@9 u
int gcstepmul
Definition: lstate.h:140
GCObject * openupval
Definition: lstate.h:170
lu_byte gcrunning
Definition: lstate.h:124
lua_State l
Definition: lstate.cpp:62
#define BASIC_STACK_SIZE
Definition: lstate.h:49
lu_mem GCestimate
Definition: lstate.h:117
struct lua_longjmp * errorJmp
Definition: lstate.h:172
lu_byte status
Definition: lstate.h:155
#define EXTRA_STACK
Definition: lstate.h:46
GLclampf f
Definition: glew.h:3024
TValue l_registry
Definition: lstate.h:119
#define luaC_white(g)
Definition: lgc.h:118
CallInfo * luaE_extendCI(lua_State *L)
Definition: lstate.cpp:111