aboutsummaryrefslogtreecommitdiff
path: root/lstate.c
diff options
context:
space:
mode:
Diffstat (limited to 'lstate.c')
-rw-r--r--lstate.c39
1 files changed, 9 insertions, 30 deletions
diff --git a/lstate.c b/lstate.c
index 18ab4900..69ddef40 100644
--- a/lstate.c
+++ b/lstate.c
@@ -29,25 +29,6 @@
29 29
30 30
31 31
32/*
33** thread state + extra space
34*/
35typedef struct LX {
36 lu_byte extra_[LUA_EXTRASPACE];
37 lua_State l;
38} LX;
39
40
41/*
42** Main thread combines a thread state and the global state
43*/
44typedef struct LG {
45 LX l;
46 global_State g;
47} LG;
48
49
50
51#define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) 32#define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
52 33
53 34
@@ -278,8 +259,8 @@ static void close_state (lua_State *L) {
278 } 259 }
279 luaM_freearray(L, G(L)->strt.hash, cast_sizet(G(L)->strt.size)); 260 luaM_freearray(L, G(L)->strt.hash, cast_sizet(G(L)->strt.size));
280 freestack(L); 261 freestack(L);
281 lua_assert(gettotalbytes(g) == sizeof(LG)); 262 lua_assert(gettotalbytes(g) == sizeof(global_State));
282 (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ 263 (*g->frealloc)(g->ud, g, sizeof(global_State), 0); /* free main block */
283} 264}
284 265
285 266
@@ -301,7 +282,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
301 L1->hook = L->hook; 282 L1->hook = L->hook;
302 resethookcount(L1); 283 resethookcount(L1);
303 /* initialize L1 extra space */ 284 /* initialize L1 extra space */
304 memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread), 285 memcpy(lua_getextraspace(L1), lua_getextraspace(mainthread(g)),
305 LUA_EXTRASPACE); 286 LUA_EXTRASPACE);
306 luai_userstatethread(L, L1); 287 luai_userstatethread(L, L1);
307 stack_init(L1, L); /* init stack */ 288 stack_init(L1, L); /* init stack */
@@ -352,11 +333,10 @@ LUA_API int lua_closethread (lua_State *L, lua_State *from) {
352LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) { 333LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) {
353 int i; 334 int i;
354 lua_State *L; 335 lua_State *L;
355 global_State *g; 336 global_State *g = cast(global_State*,
356 LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG))); 337 (*f)(ud, NULL, LUA_TTHREAD, sizeof(global_State)));
357 if (l == NULL) return NULL; 338 if (g == NULL) return NULL;
358 L = &l->l.l; 339 L = &g->mainth.l;
359 g = &l->g;
360 L->tt = LUA_VTHREAD; 340 L->tt = LUA_VTHREAD;
361 g->currentwhite = bitmask(WHITE0BIT); 341 g->currentwhite = bitmask(WHITE0BIT);
362 L->marked = luaC_white(g); 342 L->marked = luaC_white(g);
@@ -368,7 +348,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) {
368 g->ud = ud; 348 g->ud = ud;
369 g->warnf = NULL; 349 g->warnf = NULL;
370 g->ud_warn = NULL; 350 g->ud_warn = NULL;
371 g->mainthread = L;
372 g->seed = seed; 351 g->seed = seed;
373 g->gcstp = GCSTPGC; /* no GC while building state */ 352 g->gcstp = GCSTPGC; /* no GC while building state */
374 g->strt.size = g->strt.nuse = 0; 353 g->strt.size = g->strt.nuse = 0;
@@ -386,7 +365,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) {
386 g->gray = g->grayagain = NULL; 365 g->gray = g->grayagain = NULL;
387 g->weak = g->ephemeron = g->allweak = NULL; 366 g->weak = g->ephemeron = g->allweak = NULL;
388 g->twups = NULL; 367 g->twups = NULL;
389 g->GCtotalbytes = sizeof(LG); 368 g->GCtotalbytes = sizeof(global_State);
390 g->GCmarked = 0; 369 g->GCmarked = 0;
391 g->GCdebt = 0; 370 g->GCdebt = 0;
392 setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ 371 setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */
@@ -408,7 +387,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) {
408 387
409LUA_API void lua_close (lua_State *L) { 388LUA_API void lua_close (lua_State *L) {
410 lua_lock(L); 389 lua_lock(L);
411 L = G(L)->mainthread; /* only the main thread can be closed */ 390 L = mainthread(G(L)); /* only the main thread can be closed */
412 close_state(L); 391 close_state(L);
413} 392}
414 393