1
0
mirror of https://github.com/lua/lua.git synced 2025-01-28 06:03:00 +08:00

cleaner way to add extra space in a lua state.

This commit is contained in:
Roberto Ierusalimschy 2009-12-14 13:27:30 -02:00
parent 8da245bfd2
commit 15b823ce4f
3 changed files with 29 additions and 19 deletions

View File

@ -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 ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -25,21 +25,31 @@
#include "ltm.h" #include "ltm.h"
#define state_size(x) (sizeof(x) + LUAI_EXTRASPACE) /*
#define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE) ** thread state + extra space
#define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE)) */
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 ** Main thread combines a thread state and the global state
*/ */
typedef struct LG { typedef struct LG {
lua_State l; LX l;
global_State g; global_State g;
} LG; } LG;
#define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
/* /*
** maximum number of nested calls made by error-handling function ** 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); luaZ_freebuffer(L, &g->buff);
freestack(L); freestack(L);
lua_assert(g->totalbytes == sizeof(LG)); 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_State *L1;
lua_lock(L); lua_lock(L);
luaC_checkGC(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); luaC_link(L, obj2gco(L1), LUA_TTHREAD);
setthvalue(L, L->top, L1); setthvalue(L, L->top, L1);
api_incr_top(L); 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) { void luaE_freethread (lua_State *L, lua_State *L1) {
LX *l = fromstate(L1);
luaF_close(L1, L1->stack); /* close all upvalues for this thread */ luaF_close(L1, L1->stack); /* close all upvalues for this thread */
lua_assert(L1->openupval == NULL); lua_assert(L1->openupval == NULL);
luai_userstatefree(L1); luai_userstatefree(L1);
freestack(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; int i;
lua_State *L; lua_State *L;
global_State *g; 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; if (l == NULL) return NULL;
L = tostate(l); L = &l->l.l;
g = &((LG *)L)->g; g = &l->g;
L->next = NULL; L->next = NULL;
L->tt = LUA_TTHREAD; L->tt = LUA_TTHREAD;
g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltests.h,v 2.25 2009/04/17 22:00:01 roberto Exp roberto $ ** $Id: ltests.h,v 2.26 2009/11/19 19:06:52 roberto Exp roberto $
** Internal Header for Debugging of the Lua Implementation ** Internal Header for Debugging of the Lua Implementation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -58,7 +58,6 @@ int lua_checkmemory (lua_State *L);
#undef luai_userstatethread #undef luai_userstatethread
#undef lua_lock #undef lua_lock
#undef lua_unlock #undef lua_unlock
#undef LUAI_EXTRASPACE
struct L_EXTRA { int lock; int *plock; }; struct L_EXTRA { int lock; int *plock; };
#define LUAI_EXTRASPACE sizeof(struct L_EXTRA) #define LUAI_EXTRASPACE sizeof(struct L_EXTRA)

View File

@ -1,5 +1,5 @@
/* /*
** $Id: luaconf.h,v 1.119 2009/11/26 17:34:49 roberto Exp roberto $ ** $Id: luaconf.h,v 1.120 2009/12/10 19:00:33 roberto Exp roberto $
** Configuration file for Lua ** Configuration file for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -753,12 +753,12 @@ union luai_Cast { double l_d; long l_l; };
/* /*
@@ LUAI_EXTRASPACE allows you to add user-specific data in a lua_State @@ LUAI_EXTRASPACE allows you to add user-specific data in a lua_State.
@* (the data goes just *before* the lua_State pointer). @* (This data goes just *before* the lua_State pointer.)
** CHANGE (define) this if you really need that. This value must be ** CHANGE (define) this if you really need that. If defined, this value
** a multiple of the maximum alignment required for your machine. ** cannot be zero.
*/ */
#define LUAI_EXTRASPACE 0 /* #define LUAI_EXTRASPACE ?? */
/* /*