Skip to content

Commit d6e4807

Browse files
committed
Avoid allocating lua state through allocator?
This is easy enough to do and effectively moves that state to a static global for us (thru Emulator), which could have some potential benefits. Might revert if too problematic
1 parent f11ff89 commit d6e4807

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

lstate.cpp

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,6 @@
5353
#endif
5454

5555

56-
57-
/*
58-
** thread state + extra space
59-
*/
60-
typedef struct LX {
61-
#if defined(LUAI_EXTRASPACE)
62-
char buff[LUAI_EXTRASPACE];
63-
#endif
64-
lua_State l;
65-
} LX;
66-
67-
68-
/*
69-
** Main thread combines a thread state and the global state
70-
*/
71-
typedef struct LG {
72-
LX l;
73-
global_State g;
74-
} LG;
75-
76-
77-
7856
#define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
7957

8058

@@ -231,7 +209,6 @@ static void close_state (lua_State *L) {
231209
luaZ_freebuffer(L, &g->buff);
232210
freestack(L);
233211
lua_assert(gettotalbytes(g) == sizeof(LG));
234-
y8_lua_realloc(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
235212
}
236213

237214

@@ -264,11 +241,10 @@ void luaE_freethread (lua_State *L, lua_State *L1) {
264241
}
265242

266243

267-
LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, uint8_t *y8_mem) {
244+
LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, LG *l, uint8_t *y8_mem) {
268245
int i;
269246
lua_State *L;
270247
global_State *g;
271-
LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG), true));
272248
if (l == NULL) return NULL;
273249
L = &l->l.l;
274250
g = &l->g;

lstate.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,24 @@ struct lua_State {
178178
CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
179179
};
180180

181+
/*
182+
** thread state + extra space
183+
*/
184+
typedef struct LX {
185+
#if defined(LUAI_EXTRASPACE)
186+
char buff[LUAI_EXTRASPACE];
187+
#endif
188+
lua_State l;
189+
} LX;
190+
191+
192+
/*
193+
** Main thread combines a thread state and the global state
194+
*/
195+
typedef struct LG {
196+
LX l;
197+
global_State g;
198+
} LG;
181199

182200
#define G(L) (L->l_G)
183201

lua.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ extern const char lua_ident[];
128128
/*
129129
** state manipulation
130130
*/
131-
LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud, uint8_t *y8_buf);
131+
struct LG;
132+
LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud, LG *l, uint8_t *y8_buf);
132133
LUA_API void (lua_close) (lua_State *L);
133134
LUA_API lua_State *(lua_newthread) (lua_State *L);
134135

0 commit comments

Comments
 (0)