Skip to content

Commit baa0e23

Browse files
committed
better support for extra user space associated with a Lua state
1 parent 55a7105 commit baa0e23

4 files changed

Lines changed: 23 additions & 11 deletions

File tree

lstate.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lstate.c,v 2.122 2014/07/18 12:17:54 roberto Exp roberto $
2+
** $Id: lstate.c,v 2.123 2014/07/18 13:36:14 roberto Exp roberto $
33
** Global State
44
** See Copyright Notice in lua.h
55
*/
@@ -53,9 +53,7 @@
5353
** thread state + extra space
5454
*/
5555
typedef struct LX {
56-
#if defined(LUAI_EXTRASPACE)
57-
char buff[LUAI_EXTRASPACE];
58-
#endif
56+
lu_byte extra_[LUA_EXTRASPACE];
5957
lua_State l;
6058
} LX;
6159

@@ -263,13 +261,16 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
263261
/* link it on list 'allgc' */
264262
L1->next = g->allgc;
265263
g->allgc = obj2gco(L1);
264+
/* anchor it on L stack */
266265
setthvalue(L, L->top, L1);
267266
api_incr_top(L);
268267
preinit_thread(L1, g);
269268
L1->hookmask = L->hookmask;
270269
L1->basehookcount = L->basehookcount;
271270
L1->hook = L->hook;
272271
resethookcount(L1);
272+
/* initialize L1 extra space */
273+
memcpy(lua_getextraspace(L1), lua_getextraspace(L), LUA_EXTRASPACE);
273274
luai_userstatethread(L, L1);
274275
stack_init(L1, L); /* init stack */
275276
lua_unlock(L);

ltests.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: ltests.h,v 2.36 2014/07/23 16:47:47 roberto Exp roberto $
2+
** $Id: ltests.h,v 2.37 2014/07/23 17:16:50 roberto Exp roberto $
33
** Internal Header for Debugging of the Lua Implementation
44
** See Copyright Notice in lua.h
55
*/
@@ -64,14 +64,15 @@ int lua_checkmemory (lua_State *L);
6464
/* test for lock/unlock */
6565

6666
struct L_EXTRA { int lock; int *plock; };
67-
/* extra space before a Lua state (+1 to make it unaligned) */
68-
#define LUAI_EXTRASPACE (sizeof(struct L_EXTRA) + 1)
69-
#define getlock(l) (cast(struct L_EXTRA *, l) - 1)
67+
#undef LUA_EXTRASPACE
68+
#define LUA_EXTRASPACE sizeof(struct L_EXTRA)
69+
#define getlock(l) cast(struct L_EXTRA*, lua_getextraspace(l))
7070
#define luai_userstateopen(l) \
7171
(getlock(l)->lock = 0, getlock(l)->plock = &(getlock(l)->lock))
7272
#define luai_userstateclose(l) \
7373
lua_assert(getlock(l)->lock == 1 && getlock(l)->plock == &(getlock(l)->lock))
74-
#define luai_userstatethread(l,l1) (getlock(l1)->plock = getlock(l)->plock)
74+
#define luai_userstatethread(l,l1) \
75+
lua_assert(getlock(l1)->plock == getlock(l)->plock)
7576
#define luai_userstatefree(l,l1) \
7677
lua_assert(getlock(l)->plock == getlock(l1)->plock)
7778
#define lua_lock(l) lua_assert((*getlock(l)->plock)++ == 0)

lua.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lua.h,v 1.309 2014/07/17 13:53:37 roberto Exp roberto $
2+
** $Id: lua.h,v 1.310 2014/07/22 18:07:47 roberto Exp roberto $
33
** Lua - A Scripting Language
44
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
55
** 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);
332332
** ===============================================================
333333
*/
334334

335+
#define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE))
336+
335337
#define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL)
336338
#define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL)
337339

luaconf.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: luaconf.h,v 1.209 2014/06/26 18:30:27 roberto Exp roberto $
2+
** $Id: luaconf.h,v 1.210 2014/07/17 13:53:37 roberto Exp roberto $
33
** Configuration file for Lua
44
** See Copyright Notice in lua.h
55
*/
@@ -210,6 +210,14 @@
210210

211211

212212

213+
/*
214+
@@ LUA_EXTRASPACE defines the size of a raw memory area associated with
215+
** a Lua state with very fast access.
216+
** CHANGE it if you need a different size.
217+
*/
218+
#define LUA_EXTRASPACE (sizeof(void *))
219+
220+
213221
/*
214222
@@ LUA_QL describes how error messages quote program elements.
215223
** CHANGE it if you want a different appearance.

0 commit comments

Comments
 (0)