From 15b823ce4fde81d20486428e52a5a608d62f2465 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 14 Dec 2009 13:27:30 -0200 Subject: cleaner way to add extra space in a lua state. --- lstate.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'lstate.c') diff --git a/lstate.c b/lstate.c index f02c17ed..5e1ca9a6 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.63 2009/10/23 19:12:19 roberto Exp roberto $ +** $Id: lstate.c,v 2.64 2009/11/18 13:13:47 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -25,21 +25,31 @@ #include "ltm.h" -#define state_size(x) (sizeof(x) + LUAI_EXTRASPACE) -#define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE) -#define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE)) +/* +** thread state + extra space +*/ +typedef struct LX { +#if defined(LUAI_EXTRASPACE) + char buff[LUAI_EXTRASPACE]; +#endif + lua_State l; +} LX; /* ** Main thread combines a thread state and the global state */ typedef struct LG { - lua_State l; + LX l; global_State g; } LG; +#define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) + + + /* ** maximum number of nested calls made by error-handling function */ @@ -174,7 +184,7 @@ static void close_state (lua_State *L) { luaZ_freebuffer(L, &g->buff); freestack(L); lua_assert(g->totalbytes == sizeof(LG)); - (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0); + (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); } @@ -182,7 +192,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) { lua_State *L1; lua_lock(L); luaC_checkGC(L); - L1 = tostate(luaM_malloc(L, state_size(lua_State))); + L1 = &luaM_new(L, LX)->l; luaC_link(L, obj2gco(L1), LUA_TTHREAD); setthvalue(L, L->top, L1); api_incr_top(L); @@ -200,11 +210,12 @@ LUA_API lua_State *lua_newthread (lua_State *L) { void luaE_freethread (lua_State *L, lua_State *L1) { + LX *l = fromstate(L1); luaF_close(L1, L1->stack); /* close all upvalues for this thread */ lua_assert(L1->openupval == NULL); luai_userstatefree(L1); freestack(L1); - luaM_freemem(L, fromstate(L1), state_size(lua_State)); + luaM_free(L, l); } @@ -212,10 +223,10 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { int i; lua_State *L; global_State *g; - void *l = (*f)(ud, NULL, 0, state_size(LG)); + LG *l = cast(LG *, (*f)(ud, NULL, 0, sizeof(LG))); if (l == NULL) return NULL; - L = tostate(l); - g = &((LG *)L)->g; + L = &l->l.l; + g = &l->g; L->next = NULL; L->tt = LUA_TTHREAD; g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); -- cgit v1.2.3-55-g6feb