فهرست منبع

cleaner way to add extra space in a lua state.

Roberto Ierusalimschy 15 سال پیش
والد
کامیت
15b823ce4f
3فایلهای تغییر یافته به همراه29 افزوده شده و 19 حذف شده
  1. 22 11
      lstate.c
  2. 1 2
      ltests.h
  3. 6 6
      luaconf.h

+ 22 - 11
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);

+ 1 - 2
ltests.h

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -58,7 +58,6 @@ int lua_checkmemory (lua_State *L);
 #undef luai_userstatethread
 #undef lua_lock
 #undef lua_unlock
-#undef LUAI_EXTRASPACE
 
 struct L_EXTRA { int lock; int *plock; };
 #define LUAI_EXTRASPACE		sizeof(struct L_EXTRA)

+ 6 - 6
luaconf.h

@@ -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
 ** 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
-@* (the data goes just *before* the lua_State pointer).
-** CHANGE (define) this if you really need that. This value must be
-** a multiple of the maximum alignment required for your machine.
+@@ LUAI_EXTRASPACE allows you to add user-specific data in a lua_State.
+@* (This data goes just *before* the lua_State pointer.)
+** CHANGE (define) this if you really need that. If defined, this value
+** cannot be zero.
 */
-#define LUAI_EXTRASPACE		0
+/* #define LUAI_EXTRASPACE		?? */
 
 
 /*