aboutsummaryrefslogtreecommitdiff
path: root/lstate.c
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 /lstate.c
parent8da245bfd2a767e1a738c2a85492d1f64d68f016 (diff)
downloadlua-15b823ce4fde81d20486428e52a5a608d62f2465.tar.gz
lua-15b823ce4fde81d20486428e52a5a608d62f2465.tar.bz2
lua-15b823ce4fde81d20486428e52a5a608d62f2465.zip
cleaner way to add extra space in a lua state.
Diffstat (limited to 'lstate.c')
-rw-r--r--lstate.c33
1 files changed, 22 insertions, 11 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);