Browse Source

better support for extra user space associated with a Lua state

Roberto Ierusalimschy 11 years ago
parent
commit
baa0e23456
4 changed files with 23 additions and 11 deletions
  1. 5 4
      lstate.c
  2. 6 5
      ltests.h
  3. 3 1
      lua.h
  4. 9 1
      luaconf.h

+ 5 - 4
lstate.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lstate.c,v 2.122 2014/07/18 12:17:54 roberto Exp roberto $
+** $Id: lstate.c,v 2.123 2014/07/18 13:36:14 roberto Exp roberto $
 ** Global State
 ** Global State
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -53,9 +53,7 @@
 ** thread state + extra space
 ** thread state + extra space
 */
 */
 typedef struct LX {
 typedef struct LX {
-#if defined(LUAI_EXTRASPACE)
-  char buff[LUAI_EXTRASPACE];
-#endif
+  lu_byte extra_[LUA_EXTRASPACE];
   lua_State l;
   lua_State l;
 } LX;
 } LX;
 
 
@@ -263,6 +261,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
   /* link it on list 'allgc' */
   /* link it on list 'allgc' */
   L1->next = g->allgc;
   L1->next = g->allgc;
   g->allgc = obj2gco(L1);
   g->allgc = obj2gco(L1);
+  /* anchor it on L stack */
   setthvalue(L, L->top, L1);
   setthvalue(L, L->top, L1);
   api_incr_top(L);
   api_incr_top(L);
   preinit_thread(L1, g);
   preinit_thread(L1, g);
@@ -270,6 +269,8 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
   L1->basehookcount = L->basehookcount;
   L1->basehookcount = L->basehookcount;
   L1->hook = L->hook;
   L1->hook = L->hook;
   resethookcount(L1);
   resethookcount(L1);
+  /* initialize L1 extra space */
+  memcpy(lua_getextraspace(L1), lua_getextraspace(L), LUA_EXTRASPACE);
   luai_userstatethread(L, L1);
   luai_userstatethread(L, L1);
   stack_init(L1, L);  /* init stack */
   stack_init(L1, L);  /* init stack */
   lua_unlock(L);
   lua_unlock(L);

+ 6 - 5
ltests.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: ltests.h,v 2.36 2014/07/23 16:47:47 roberto Exp roberto $
+** $Id: ltests.h,v 2.37 2014/07/23 17:16:50 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
 */
 */
@@ -64,14 +64,15 @@ int lua_checkmemory (lua_State *L);
 /* test for lock/unlock */
 /* test for lock/unlock */
 
 
 struct L_EXTRA { int lock; int *plock; };
 struct L_EXTRA { int lock; int *plock; };
-/* extra space before a Lua state (+1 to make it unaligned) */
-#define LUAI_EXTRASPACE		(sizeof(struct L_EXTRA) + 1)
-#define getlock(l)	(cast(struct L_EXTRA *, l) - 1)
+#undef LUA_EXTRASPACE
+#define LUA_EXTRASPACE	sizeof(struct L_EXTRA)
+#define getlock(l)	cast(struct L_EXTRA*, lua_getextraspace(l))
 #define luai_userstateopen(l)  \
 #define luai_userstateopen(l)  \
 	(getlock(l)->lock = 0, getlock(l)->plock = &(getlock(l)->lock))
 	(getlock(l)->lock = 0, getlock(l)->plock = &(getlock(l)->lock))
 #define luai_userstateclose(l)  \
 #define luai_userstateclose(l)  \
   lua_assert(getlock(l)->lock == 1 && getlock(l)->plock == &(getlock(l)->lock))
   lua_assert(getlock(l)->lock == 1 && getlock(l)->plock == &(getlock(l)->lock))
-#define luai_userstatethread(l,l1)  (getlock(l1)->plock = getlock(l)->plock)
+#define luai_userstatethread(l,l1) \
+  lua_assert(getlock(l1)->plock == getlock(l)->plock)
 #define luai_userstatefree(l,l1) \
 #define luai_userstatefree(l,l1) \
   lua_assert(getlock(l)->plock == getlock(l1)->plock)
   lua_assert(getlock(l)->plock == getlock(l1)->plock)
 #define lua_lock(l)     lua_assert((*getlock(l)->plock)++ == 0)
 #define lua_lock(l)     lua_assert((*getlock(l)->plock)++ == 0)

+ 3 - 1
lua.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lua.h,v 1.309 2014/07/17 13:53:37 roberto Exp roberto $
+** $Id: lua.h,v 1.310 2014/07/22 18:07:47 roberto Exp roberto $
 ** Lua - A Scripting Language
 ** Lua - A Scripting Language
 ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
 ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
 ** See Copyright Notice at the end of this file
 ** See Copyright Notice at the end of this file
@@ -332,6 +332,8 @@ LUA_API void      (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
 ** ===============================================================
 ** ===============================================================
 */
 */
 
 
+#define lua_getextraspace(L)	((void *)((char *)(L) - LUA_EXTRASPACE))
+
 #define lua_tonumber(L,i)	lua_tonumberx(L,(i),NULL)
 #define lua_tonumber(L,i)	lua_tonumberx(L,(i),NULL)
 #define lua_tointeger(L,i)	lua_tointegerx(L,(i),NULL)
 #define lua_tointeger(L,i)	lua_tointegerx(L,(i),NULL)
 
 

+ 9 - 1
luaconf.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: luaconf.h,v 1.209 2014/06/26 18:30:27 roberto Exp roberto $
+** $Id: luaconf.h,v 1.210 2014/07/17 13:53:37 roberto Exp roberto $
 ** Configuration file for Lua
 ** Configuration file for Lua
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -210,6 +210,14 @@
 
 
 
 
 
 
+/*
+@@ LUA_EXTRASPACE defines the size of a raw memory area associated with
+** a Lua state with very fast access.
+** CHANGE it if you need a different size.
+*/
+#define LUA_EXTRASPACE		(sizeof(void *))
+
+
 /*
 /*
 @@ LUA_QL describes how error messages quote program elements.
 @@ LUA_QL describes how error messages quote program elements.
 ** CHANGE it if you want a different appearance.
 ** CHANGE it if you want a different appearance.