The Battle for Wesnoth  1.15.0-dev
lstate.cpp
Go to the documentation of this file.
1 /*
2 ** $Id: lstate.c,v 2.133.1.1 2017/04/19 17:39:34 roberto Exp $
3 ** Global State
4 ** See Copyright Notice in lua.h
5 */
6 
7 #define lstate_c
8 #define LUA_CORE
9 
10 #include "lprefix.h"
11 
12 
13 #include <stddef.h>
14 #include <string.h>
15 
16 #include "lua.h"
17 
18 #include "lapi.h"
19 #include "ldebug.h"
20 #include "ldo.h"
21 #include "lfunc.h"
22 #include "lgc.h"
23 #include "llex.h"
24 #include "lmem.h"
25 #include "lstate.h"
26 #include "lstring.h"
27 #include "ltable.h"
28 #include "ltm.h"
29 
30 
31 #if !defined(LUAI_GCPAUSE)
32 #define LUAI_GCPAUSE 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 /*
41 ** a macro to help the creation of a unique random seed when a state is
42 ** created; the seed is used to randomize hashes.
43 */
44 #if !defined(luai_makeseed)
45 #include <time.h>
46 #define luai_makeseed() cast(unsigned int, time(NULL))
47 #endif
48 
49 
50 
51 /*
52 ** thread state + extra space
53 */
54 typedef struct LX {
57 } LX;
58 
59 
60 /*
61 ** Main thread combines a thread state and the global state
62 */
63 typedef struct LG {
64  LX l;
66 } LG;
67 
68 
69 
70 #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
71 
72 
73 /*
74 ** Compute an initial seed as random as possible. Rely on Address Space
75 ** Layout Randomization (if present) to increase randomness..
76 */
77 #define addbuff(b,p,e) \
78  { size_t t = cast(size_t, e); \
79  memcpy(b + p, &t, sizeof(t)); p += sizeof(t); }
80 
81 static unsigned int makeseed (lua_State *L) {
82  char buff[4 * sizeof(size_t)];
83  unsigned int h = luai_makeseed();
84  int p = 0;
85  addbuff(buff, p, L); /* heap variable */
86  addbuff(buff, p, &h); /* local variable */
87  addbuff(buff, p, luaO_nilobject); /* global variable */
88  addbuff(buff, p, &lua_newstate); /* public function */
89  lua_assert(p == sizeof(buff));
90  return luaS_hash(buff, p, h);
91 }
92 
93 
94 /*
95 ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
96 ** invariant (and avoiding underflows in 'totalbytes')
97 */
99  l_mem tb = gettotalbytes(g);
100  lua_assert(tb > 0);
101  if (debt < tb - MAX_LMEM)
102  debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */
103  g->totalbytes = tb - debt;
104  g->GCdebt = debt;
105 }
106 
107 
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  L->nci++;
115  return ci;
116 }
117 
118 
119 /*
120 ** free all CallInfo structures not in use by a thread
121 */
123  CallInfo *ci = L->ci;
124  CallInfo *next = ci->next;
125  ci->next = NULL;
126  while ((ci = next) != NULL) {
127  next = ci->next;
128  luaM_free(L, ci);
129  L->nci--;
130  }
131 }
132 
133 
134 /*
135 ** free half of the CallInfo structures not in use by a thread
136 */
138  CallInfo *ci = L->ci;
139  CallInfo *next2; /* next's next */
140  /* while there are two nexts */
141  while (ci->next != NULL && (next2 = ci->next->next) != NULL) {
142  luaM_free(L, ci->next); /* free next */
143  L->nci--;
144  ci->next = next2; /* remove 'next' from the list */
145  next2->previous = ci;
146  ci = next2; /* keep next's next */
147  }
148 }
149 
150 
151 static void stack_init (lua_State *L1, lua_State *L) {
152  int i; CallInfo *ci;
153  /* initialize stack array */
156  for (i = 0; i < BASIC_STACK_SIZE; i++)
157  setnilvalue(L1->stack + i); /* erase new stack */
158  L1->top = L1->stack;
159  L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
160  /* initialize first ci */
161  ci = &L1->base_ci;
162  ci->next = ci->previous = NULL;
163  ci->callstatus = 0;
164  ci->func = L1->top;
165  setnilvalue(L1->top++); /* 'function' entry for this 'ci' */
166  ci->top = L1->top + LUA_MINSTACK;
167  L1->ci = ci;
168 }
169 
170 
171 static void freestack (lua_State *L) {
172  if (L->stack == NULL)
173  return; /* stack not completely built yet */
174  L->ci = &L->base_ci; /* free the entire 'ci' list */
175  luaE_freeCI(L);
176  lua_assert(L->nci == 0);
177  luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
178 }
179 
180 
181 /*
182 ** Create registry table and its predefined values
183 */
184 static void init_registry (lua_State *L, global_State *g) {
185  TValue temp;
186  /* create registry */
187  Table *registry = luaH_new(L);
188  sethvalue(L, &g->l_registry, registry);
189  luaH_resize(L, registry, LUA_RIDX_LAST, 0);
190  /* registry[LUA_RIDX_MAINTHREAD] = L */
191  setthvalue(L, &temp, L); /* temp = L */
192  luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp);
193  /* registry[LUA_RIDX_GLOBALS] = table of globals */
194  sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */
195  luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp);
196 }
197 
198 
199 /*
200 ** open parts of the state that may cause memory-allocation errors.
201 ** ('g->version' != NULL flags that the state was completely build)
202 */
203 static void f_luaopen (lua_State *L, void *ud) {
204  global_State *g = G(L);
205  UNUSED(ud);
206  stack_init(L, L); /* init stack */
207  init_registry(L, g);
208  luaS_init(L);
209  luaT_init(L);
210  luaX_init(L);
211  g->gcrunning = 1; /* allow gc */
212  g->version = lua_version(NULL);
214 }
215 
216 
217 /*
218 ** preinitialize a thread with consistent values without allocating
219 ** any memory (to avoid errors)
220 */
222  G(L) = g;
223  L->stack = NULL;
224  L->ci = NULL;
225  L->nci = 0;
226  L->stacksize = 0;
227  L->twups = L; /* thread has no upvalues */
228  L->errorJmp = NULL;
229  L->nCcalls = 0;
230  L->hook = NULL;
231  L->hookmask = 0;
232  L->basehookcount = 0;
233  L->allowhook = 1;
234  resethookcount(L);
235  L->openupval = NULL;
236  L->nny = 1;
237  L->status = LUA_OK;
238  L->errfunc = 0;
239 }
240 
241 
242 static void close_state (lua_State *L) {
243  global_State *g = G(L);
244  luaF_close(L, L->stack); /* close all upvalues for this thread */
245  luaC_freeallobjects(L); /* collect all objects */
246  if (g->version) /* closing a fully built state? */
248  luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
249  freestack(L);
250  lua_assert(gettotalbytes(g) == sizeof(LG));
251  (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
252 }
253 
254 
256  global_State *g = G(L);
257  lua_State *L1;
258  lua_lock(L);
259  luaC_checkGC(L);
260  /* create new thread */
261  L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l;
262  L1->marked = luaC_white(g);
263  L1->tt = LUA_TTHREAD;
264  /* link it on list 'allgc' */
265  L1->next = g->allgc;
266  g->allgc = obj2gco(L1);
267  /* anchor it on L stack */
268  setthvalue(L, L->top, L1);
269  api_incr_top(L);
270  preinit_thread(L1, g);
271  L1->hookmask = L->hookmask;
272  L1->basehookcount = L->basehookcount;
273  L1->hook = L->hook;
274  resethookcount(L1);
275  /* initialize L1 extra space */
278  luai_userstatethread(L, L1);
279  stack_init(L1, L); /* init stack */
280  lua_unlock(L);
281  return L1;
282 }
283 
284 
286  LX *l = fromstate(L1);
287  luaF_close(L1, L1->stack); /* close all upvalues for this thread */
288  lua_assert(L1->openupval == NULL);
289  luai_userstatefree(L, L1);
290  freestack(L1);
291  luaM_free(L, l);
292 }
293 
294 
296  int i;
297  lua_State *L;
298  global_State *g;
299  LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
300  if (l == NULL) return NULL;
301  L = &l->l.l;
302  g = &l->g;
303  L->next = NULL;
304  L->tt = LUA_TTHREAD;
306  L->marked = luaC_white(g);
307  preinit_thread(L, g);
308  g->frealloc = f;
309  g->ud = ud;
310  g->mainthread = L;
311  g->seed = makeseed(L);
312  g->gcrunning = 0; /* no GC while building state */
313  g->GCestimate = 0;
314  g->strt.size = g->strt.nuse = 0;
315  g->strt.hash = NULL;
316  setnilvalue(&g->l_registry);
317  g->panic = NULL;
318  g->version = NULL;
319  g->gcstate = GCSpause;
320  g->gckind = KGC_NORMAL;
321  g->allgc = g->finobj = g->tobefnz = g->fixedgc = NULL;
322  g->sweepgc = NULL;
323  g->gray = g->grayagain = NULL;
324  g->weak = g->ephemeron = g->allweak = NULL;
325  g->twups = NULL;
326  g->totalbytes = sizeof(LG);
327  g->GCdebt = 0;
328  g->gcfinnum = 0;
329  g->gcpause = LUAI_GCPAUSE;
330  g->gcstepmul = LUAI_GCMUL;
331  for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
332  if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
333  /* memory allocation error: free partial state */
334  close_state(L);
335  L = NULL;
336  }
337  return L;
338 }
339 
340 
342  L = G(L)->mainthread; /* only the main thread can be closed */
343  lua_lock(L);
344  close_state(L);
345 }
346 
347 
unsigned short callstatus
Definition: lstate.h:109
#define LUAI_GCPAUSE
Definition: lstate.cpp:32
lu_byte allowhook
Definition: lstate.h:201
void luaE_setdebt(global_State *g, l_mem debt)
Definition: lstate.cpp:98
void luaE_freethread(lua_State *L, lua_State *L1)
Definition: lstate.cpp:285
static unsigned int makeseed(lua_State *L)
Definition: lstate.cpp:81
#define LUA_TTHREAD
Definition: lua.h:72
l_mem GCdebt
Definition: lstate.h:141
ptrdiff_t errfunc
Definition: lstate.h:194
#define luai_userstateclose(L)
Definition: llimits.h:237
Definition: lstate.cpp:63
#define luai_userstatefree(L, L1)
Definition: llimits.h:245
void luaF_close(lua_State *L, StkId level)
Definition: lfunc.cpp:83
struct LX LX
#define luaM_newvector(L, n, t)
Definition: lmem.h:47
void luaE_shrinkCI(lua_State *L)
Definition: lstate.cpp:137
Definition: lobject.h:497
#define obj2gco(v)
Definition: lstate.h:238
unsigned int luaS_hash(const char *str, size_t l, unsigned int seed)
Definition: lstring.cpp:49
#define luaM_freearray(L, b, n)
Definition: lmem.h:43
StkId stack
Definition: lstate.h:187
struct LG LG
#define GCSpause
Definition: lgc.h:46
lua_Alloc frealloc
Definition: lstate.h:138
unsigned short nci
Definition: lstate.h:180
int basehookcount
Definition: lstate.h:196
LUA_API const lua_Number * lua_version(lua_State *L)
Definition: lapi.cpp:144
StkId top
Definition: lstate.h:94
GCObject * finobj
Definition: lstate.h:153
#define cast(t, exp)
Definition: llimits.h:111
#define setnilvalue(obj)
Definition: lobject.h:210
#define G(L)
Definition: lstate.h:205
GCObject * tobefnz
Definition: lstate.h:159
#define h
void luaC_freeallobjects(lua_State *L)
Definition: lgc.cpp:967
#define lua_unlock(L)
Definition: llimits.h:215
#define luai_makeseed()
Definition: lstate.cpp:46
void luaE_freeCI(lua_State *L)
Definition: lstate.cpp:122
static void stack_init(lua_State *L1, lua_State *L)
Definition: lstate.cpp:151
static void close_state(lua_State *L)
Definition: lstate.cpp:242
#define fromstate(L)
Definition: lstate.cpp:70
GCObject * allgc
Definition: lstate.h:151
const lua_Number * version
Definition: lstate.h:167
static void f_luaopen(lua_State *L, void *ud)
Definition: lstate.cpp:203
#define luaO_nilobject
Definition: lobject.h:525
#define LUA_RIDX_MAINTHREAD
Definition: lua.h:83
l_mem totalbytes
Definition: lstate.h:140
#define luaM_new(L, t)
Definition: lmem.h:46
StkId top
Definition: lstate.h:182
lu_byte gckind
Definition: lstate.h:149
StkId stack_last
Definition: lstate.h:186
long l_mem
Definition: llimits.h:30
#define sethvalue(L, obj, x)
Definition: lobject.h:250
#define lua_lock(L)
Definition: llimits.h:214
#define gettotalbytes(g)
Definition: lstate.h:243
unsigned char lu_byte
Definition: llimits.h:35
int nuse
Definition: lstate.h:78
void * ud
Definition: lstate.h:139
struct lua_State * mainthread
Definition: lstate.h:166
unsigned short nny
Definition: lstate.h:198
#define LUA_RIDX_LAST
Definition: lua.h:85
#define luai_userstateopen(L)
Definition: llimits.h:233
unsigned short nCcalls
Definition: lstate.h:199
void *(* lua_Alloc)(void *ud, void *ptr, size_t osize, size_t nsize)
Definition: lua.h:124
CallInfo * ci
Definition: lstate.h:184
lua_CFunction panic
Definition: lstate.h:165
lu_byte extra_[LUA_EXTRASPACE]
Definition: lstate.cpp:55
#define luaM_newobject(L, tag, s)
Definition: lmem.h:50
void luaT_init(lua_State *L)
Definition: ltm.cpp:37
lu_byte currentwhite
Definition: lstate.h:147
#define api_incr_top(L)
Definition: lapi.h:14
LUA_API void lua_close(lua_State *L)
Definition: lstate.cpp:341
#define lua_getextraspace(L)
Definition: lua.h:339
void luaX_init(lua_State *L)
Definition: llex.cpp:70
global_State g
Definition: lstate.cpp:65
#define LUA_API
Definition: luaconf.h:254
StkId func
Definition: lstate.h:93
if(win_locale=="af") win_locale
#define luaM_free(L, b)
Definition: lmem.h:42
l_signalT hookmask
Definition: lstate.h:200
struct Table * mt[LUA_NUMTAGS]
Definition: lstate.h:170
#define resethookcount(L)
Definition: ldebug.h:18
CallInfo base_ci
Definition: lstate.h:192
static void init_registry(lua_State *L, global_State *g)
Definition: lstate.cpp:184
GCObject * weak
Definition: lstate.h:156
Table * luaH_new(lua_State *L)
Definition: ltable.cpp:421
GCObject * gray
Definition: lstate.h:154
#define LUAI_GCMUL
Definition: lstate.cpp:36
void luaH_resize(lua_State *L, Table *t, unsigned int nasize, unsigned int nhsize)
Definition: ltable.cpp:347
#define UNUSED(x)
Definition: global.hpp:34
#define WHITE0BIT
Definition: lgc.h:78
#define lua_assert(c)
Definition: llimits.h:89
std::size_t i
Definition: function.cpp:933
static void freestack(lua_State *L)
Definition: lstate.cpp:171
struct CallInfo * next
Definition: lstate.h:95
mock_party p
#define LUA_NUMTAGS
Definition: lua.h:74
int size
Definition: lstate.h:79
TString ** hash
Definition: lstate.h:77
GCObject ** sweepgc
Definition: lstate.h:152
double g
Definition: astarsearch.cpp:64
GCObject * grayagain
Definition: lstate.h:155
UpVal * openupval
Definition: lstate.h:188
GCObject * allweak
Definition: lstate.h:158
#define LUA_RIDX_GLOBALS
Definition: lua.h:84
#define MAX_LMEM
Definition: llimits.h:48
#define LUA_EXTRASPACE
Definition: luaconf.h:740
struct lua_State * twups
Definition: lstate.h:161
struct lua_State * twups
Definition: lstate.h:190
LUA_API lua_State * lua_newstate(lua_Alloc f, void *ud)
Definition: lstate.cpp:295
#define KGC_NORMAL
Definition: lstate.h:72
#define LUA_MINSTACK
Definition: lua.h:79
int gcpause
Definition: lstate.h:163
int luaD_rawrunprotected(lua_State *L, Pfunc f, void *ud)
Definition: ldo.cpp:136
#define addbuff(b, p, e)
Definition: lstate.cpp:77
GCObject * fixedgc
Definition: lstate.h:160
unsigned int gcfinnum
Definition: lstate.h:162
static void preinit_thread(lua_State *L, global_State *g)
Definition: lstate.cpp:221
#define next(ls)
Definition: llex.cpp:32
unsigned int seed
Definition: lstate.h:146
#define f
struct CallInfo * previous
Definition: lstate.h:95
#define LUA_OK
Definition: lua.h:47
Definition: lstate.cpp:54
#define bitmask(b)
Definition: lgc.h:70
stringtable strt
Definition: lstate.h:144
lu_byte gcstate
Definition: lstate.h:148
#define luaC_checkGC(L)
Definition: lgc.h:115
int stacksize
Definition: lstate.h:195
#define luai_userstatethread(L, L1)
Definition: llimits.h:241
LX l
Definition: lstate.cpp:64
#define setthvalue(L, obj, x)
Definition: lobject.h:235
LUA_API lua_State * lua_newthread(lua_State *L)
Definition: lstate.cpp:255
volatile lua_Hook hook
Definition: lstate.h:193
GCObject * ephemeron
Definition: lstate.h:157
int gcstepmul
Definition: lstate.h:164
lu_byte gcrunning
Definition: lstate.h:150
void luaH_setint(lua_State *L, Table *t, lua_Integer key, TValue *value)
Definition: ltable.cpp:618
lua_State l
Definition: lstate.cpp:56
#define BASIC_STACK_SIZE
Definition: lstate.h:68
lu_mem GCestimate
Definition: lstate.h:143
struct lua_longjmp * errorJmp
Definition: lstate.h:191
void luaS_init(lua_State *L)
Definition: lstring.cpp:116
lu_byte status
Definition: lstate.h:181
#define EXTRA_STACK
Definition: lstate.h:65
TValue l_registry
Definition: lstate.h:145
#define luaC_white(g)
Definition: lgc.h:101
CallInfo * luaE_extendCI(lua_State *L)
Definition: lstate.cpp:108