aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-12-14 13:27:30 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-12-14 13:27:30 -0200
commit15b823ce4fde81d20486428e52a5a608d62f2465 (patch)
tree05cc1aa379caa41eac41c161aaf54a5be12df3a2
parent8da245bfd2a767e1a738c2a85492d1f64d68f016 (diff)
downloadlua-15b823ce4fde81d20486428e52a5a608d62f2465.tar.gz
lua-15b823ce4fde81d20486428e52a5a608d62f2465.tar.bz2
lua-15b823ce4fde81d20486428e52a5a608d62f2465.zip
cleaner way to add extra space in a lua state.
-rw-r--r--lstate.c33
-rw-r--r--ltests.h3
-rw-r--r--luaconf.h12
3 files changed, 29 insertions, 19 deletions
diff --git a/lstate.c b/lstate.c
index f02c17ed..5e1ca9a6 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.c,v 2.63 2009/10/23 19:12:19 roberto Exp roberto $ 2** $Id: lstate.c,v 2.64 2009/11/18 13:13:47 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -25,21 +25,31 @@
25#include "ltm.h" 25#include "ltm.h"
26 26
27 27
28#define state_size(x) (sizeof(x) + LUAI_EXTRASPACE) 28/*
29#define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE) 29** thread state + extra space
30#define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE)) 30*/
31typedef struct LX {
32#if defined(LUAI_EXTRASPACE)
33 char buff[LUAI_EXTRASPACE];
34#endif
35 lua_State l;
36} LX;
31 37
32 38
33/* 39/*
34** Main thread combines a thread state and the global state 40** Main thread combines a thread state and the global state
35*/ 41*/
36typedef struct LG { 42typedef struct LG {
37 lua_State l; 43 LX l;
38 global_State g; 44 global_State g;
39} LG; 45} LG;
40 46
41 47
42 48
49#define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
50
51
52
43/* 53/*
44** maximum number of nested calls made by error-handling function 54** maximum number of nested calls made by error-handling function
45*/ 55*/
@@ -174,7 +184,7 @@ static void close_state (lua_State *L) {
174 luaZ_freebuffer(L, &g->buff); 184 luaZ_freebuffer(L, &g->buff);
175 freestack(L); 185 freestack(L);
176 lua_assert(g->totalbytes == sizeof(LG)); 186 lua_assert(g->totalbytes == sizeof(LG));
177 (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0); 187 (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0);
178} 188}
179 189
180 190
@@ -182,7 +192,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
182 lua_State *L1; 192 lua_State *L1;
183 lua_lock(L); 193 lua_lock(L);
184 luaC_checkGC(L); 194 luaC_checkGC(L);
185 L1 = tostate(luaM_malloc(L, state_size(lua_State))); 195 L1 = &luaM_new(L, LX)->l;
186 luaC_link(L, obj2gco(L1), LUA_TTHREAD); 196 luaC_link(L, obj2gco(L1), LUA_TTHREAD);
187 setthvalue(L, L->top, L1); 197 setthvalue(L, L->top, L1);
188 api_incr_top(L); 198 api_incr_top(L);
@@ -200,11 +210,12 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
200 210
201 211
202void luaE_freethread (lua_State *L, lua_State *L1) { 212void luaE_freethread (lua_State *L, lua_State *L1) {
213 LX *l = fromstate(L1);
203 luaF_close(L1, L1->stack); /* close all upvalues for this thread */ 214 luaF_close(L1, L1->stack); /* close all upvalues for this thread */
204 lua_assert(L1->openupval == NULL); 215 lua_assert(L1->openupval == NULL);
205 luai_userstatefree(L1); 216 luai_userstatefree(L1);
206 freestack(L1); 217 freestack(L1);
207 luaM_freemem(L, fromstate(L1), state_size(lua_State)); 218 luaM_free(L, l);
208} 219}
209 220
210 221
@@ -212,10 +223,10 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
212 int i; 223 int i;
213 lua_State *L; 224 lua_State *L;
214 global_State *g; 225 global_State *g;
215 void *l = (*f)(ud, NULL, 0, state_size(LG)); 226 LG *l = cast(LG *, (*f)(ud, NULL, 0, sizeof(LG)));
216 if (l == NULL) return NULL; 227 if (l == NULL) return NULL;
217 L = tostate(l); 228 L = &l->l.l;
218 g = &((LG *)L)->g; 229 g = &l->g;
219 L->next = NULL; 230 L->next = NULL;
220 L->tt = LUA_TTHREAD; 231 L->tt = LUA_TTHREAD;
221 g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); 232 g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
diff --git a/ltests.h b/ltests.h
index f2abb234..42b3f277 100644
--- a/ltests.h
+++ b/ltests.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltests.h,v 2.25 2009/04/17 22:00:01 roberto Exp roberto $ 2** $Id: ltests.h,v 2.26 2009/11/19 19:06:52 roberto Exp roberto $
3** Internal Header for Debugging of the Lua Implementation 3** Internal Header for Debugging of the Lua Implementation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -58,7 +58,6 @@ int lua_checkmemory (lua_State *L);
58#undef luai_userstatethread 58#undef luai_userstatethread
59#undef lua_lock 59#undef lua_lock
60#undef lua_unlock 60#undef lua_unlock
61#undef LUAI_EXTRASPACE
62 61
63struct L_EXTRA { int lock; int *plock; }; 62struct L_EXTRA { int lock; int *plock; };
64#define LUAI_EXTRASPACE sizeof(struct L_EXTRA) 63#define LUAI_EXTRASPACE sizeof(struct L_EXTRA)
diff --git a/luaconf.h b/luaconf.h
index af1e11d5..0e512761 100644
--- a/luaconf.h
+++ b/luaconf.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: luaconf.h,v 1.119 2009/11/26 17:34:49 roberto Exp roberto $ 2** $Id: luaconf.h,v 1.120 2009/12/10 19:00:33 roberto Exp roberto $
3** Configuration file for Lua 3** Configuration file for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -753,12 +753,12 @@ union luai_Cast { double l_d; long l_l; };
753 753
754 754
755/* 755/*
756@@ LUAI_EXTRASPACE allows you to add user-specific data in a lua_State 756@@ LUAI_EXTRASPACE allows you to add user-specific data in a lua_State.
757@* (the data goes just *before* the lua_State pointer). 757@* (This data goes just *before* the lua_State pointer.)
758** CHANGE (define) this if you really need that. This value must be 758** CHANGE (define) this if you really need that. If defined, this value
759** a multiple of the maximum alignment required for your machine. 759** cannot be zero.
760*/ 760*/
761#define LUAI_EXTRASPACE 0 761/* #define LUAI_EXTRASPACE ?? */
762 762
763 763
764/* 764/*