main thread and global state are allocated in a single block

This commit is contained in:
Roberto Ierusalimschy 2003-09-04 17:19:07 -03:00
parent 30e51f09b9
commit 7dae7899b1
1 changed files with 53 additions and 52 deletions

105
lstate.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstate.c,v 1.124 2003/07/16 20:49:02 roberto Exp roberto $ ** $Id: lstate.c,v 1.125 2003/07/16 20:51:47 roberto Exp roberto $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -34,10 +34,19 @@ union UEXTRASPACE {L_Umaxalign a; LUA_USERSTATE b;};
#endif #endif
/*
** Main thread combines a thread state and the global state
*/
typedef struct LG {
lua_State l;
global_State g;
} LG;
static lua_State *mallocstate (lua_State *L) {
lu_byte *block = (lu_byte *)luaM_malloc(L, sizeof(lua_State) + EXTRASPACE);
static lua_State *mallocstate (lua_State *L, size_t size) {
lu_byte *block = (lu_byte *)luaM_malloc(L, size + EXTRASPACE);
if (block == NULL) return NULL; if (block == NULL) return NULL;
else { else {
block += EXTRASPACE; block += EXTRASPACE;
@ -46,9 +55,9 @@ static lua_State *mallocstate (lua_State *L) {
} }
static void freestate (lua_State *L, lua_State *L1) { static void freestate (lua_State *L, lua_State *L1, size_t size) {
luaM_free(L, cast(lu_byte *, L1) - EXTRASPACE, luaM_free(L, cast(lu_byte *, L1) - EXTRASPACE,
sizeof(lua_State) + EXTRASPACE); size + EXTRASPACE);
} }
@ -77,27 +86,7 @@ static void freestack (lua_State *L, lua_State *L1) {
** open parts that may cause memory-allocation errors ** open parts that may cause memory-allocation errors
*/ */
static void f_luaopen (lua_State *L, void *ud) { static void f_luaopen (lua_State *L, void *ud) {
/* create a new global state */
global_State *g = luaM_new(NULL, global_State);
UNUSED(ud); UNUSED(ud);
if (g == NULL) luaD_throw(L, LUA_ERRMEM);
L->l_G = g;
g->mainthread = L;
g->GCthreshold = 0; /* mark it as unfinished state */
g->strt.size = 0;
g->strt.nuse = 0;
g->strt.hash = NULL;
setnilvalue(defaultmeta(L));
setnilvalue(registry(L));
luaZ_initbuffer(L, &g->buff);
g->panic = NULL;
g->rootgc = NULL;
g->rootudata = NULL;
g->tmudata = NULL;
setnilvalue(gkey(g->dummynode));
setnilvalue(gval(g->dummynode));
g->dummynode->next = NULL;
g->nblocks = sizeof(lua_State) + sizeof(global_State);
stack_init(L, L); /* init stack */ stack_init(L, L); /* init stack */
/* create default meta table with a dummy table, and then close the loop */ /* create default meta table with a dummy table, and then close the loop */
defaultmeta(L)->tt = LUA_TTABLE; defaultmeta(L)->tt = LUA_TTABLE;
@ -109,7 +98,7 @@ static void f_luaopen (lua_State *L, void *ud) {
luaT_init(L); luaT_init(L);
luaX_init(L); luaX_init(L);
luaS_fix(luaS_newliteral(L, MEMERRMSG)); luaS_fix(luaS_newliteral(L, MEMERRMSG));
g->GCthreshold = 4*G(L)->nblocks; G(L)->GCthreshold = 4*G(L)->nblocks;
} }
@ -134,24 +123,19 @@ static void preinit_state (lua_State *L) {
static void close_state (lua_State *L) { static void close_state (lua_State *L) {
luaF_close(L, L->stack); /* close all upvalues for this thread */ luaF_close(L, L->stack); /* close all upvalues for this thread */
if (G(L)) { /* close global state */ luaC_sweep(L, 1); /* collect all elements */
luaC_sweep(L, 1); /* collect all elements */ lua_assert(G(L)->rootgc == NULL);
lua_assert(G(L)->rootgc == NULL); lua_assert(G(L)->rootudata == NULL);
lua_assert(G(L)->rootudata == NULL); luaS_freeall(L);
luaS_freeall(L); luaZ_freebuffer(L, &G(L)->buff);
luaZ_freebuffer(L, &G(L)->buff);
}
freestack(L, L); freestack(L, L);
if (G(L)) { lua_assert(G(L)->nblocks == sizeof(LG));
lua_assert(G(L)->nblocks == sizeof(lua_State) + sizeof(global_State)); freestate(NULL, L, sizeof(LG));
luaM_freelem(NULL, G(L));
}
freestate(NULL, L);
} }
lua_State *luaE_newthread (lua_State *L) { lua_State *luaE_newthread (lua_State *L) {
lua_State *L1 = mallocstate(L); lua_State *L1 = mallocstate(L, sizeof(lua_State));
luaC_link(L, valtogco(L1), LUA_TTHREAD); luaC_link(L, valtogco(L1), LUA_TTHREAD);
preinit_state(L1); preinit_state(L1);
L1->l_G = L->l_G; L1->l_G = L->l_G;
@ -165,23 +149,40 @@ void luaE_freethread (lua_State *L, lua_State *L1) {
luaF_close(L1, L1->stack); /* close all upvalues for this thread */ luaF_close(L1, L1->stack); /* close all upvalues for this thread */
lua_assert(L1->openupval == NULL); lua_assert(L1->openupval == NULL);
freestack(L, L1); freestack(L, L1);
freestate(L, L1); freestate(L, L1, sizeof(lua_State));
} }
LUA_API lua_State *lua_open (void) { LUA_API lua_State *lua_open (void) {
lua_State *L = mallocstate(NULL); lua_State *L = mallocstate(NULL, sizeof(LG));
if (L) { /* allocation OK? */ global_State *g;
L->tt = LUA_TTHREAD; if (L == NULL) return NULL;
L->marked = 0; g = &((LG *)L)->g;
L->next = L->gclist = NULL; L->tt = LUA_TTHREAD;
preinit_state(L); L->marked = 0;
L->l_G = NULL; L->next = L->gclist = NULL;
if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { preinit_state(L);
/* memory allocation error: free partial state */ L->l_G = g;
close_state(L); g->mainthread = L;
L = NULL; g->GCthreshold = 0; /* mark it as unfinished state */
} g->strt.size = 0;
g->strt.nuse = 0;
g->strt.hash = NULL;
setnilvalue(defaultmeta(L));
setnilvalue(registry(L));
luaZ_initbuffer(L, &g->buff);
g->panic = NULL;
g->rootgc = NULL;
g->rootudata = NULL;
g->tmudata = NULL;
setnilvalue(gkey(g->dummynode));
setnilvalue(gval(g->dummynode));
g->dummynode->next = NULL;
g->nblocks = sizeof(LG);
if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
/* memory allocation error: free partial state */
close_state(L);
L = NULL;
} }
lua_userstateopen(L); lua_userstateopen(L);
return L; return L;