better organization of "malloc responsabilities"

This commit is contained in:
Roberto Ierusalimschy 2002-11-19 12:12:13 -02:00
parent c8a79057f7
commit be762f38bd
1 changed files with 66 additions and 53 deletions

119
lstate.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstate.c,v 1.111 2002/11/14 16:15:53 roberto Exp roberto $ ** $Id: lstate.c,v 1.112 2002/11/18 11:01:55 roberto Exp roberto $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -30,10 +30,18 @@ union UEXTRASPACE {L_Umaxalign a; LUA_USERSTATE b;};
#endif #endif
static void close_state (lua_State *L);
/*
** you can change this function through the official API:
** call `lua_setpanicf'
*/
static int default_panic (lua_State *L) {
UNUSED(L);
return 0;
}
static lua_State *newthread (lua_State *L) { static lua_State *mallocstate (lua_State *L) {
lu_byte *block = (lu_byte *)luaM_malloc(L, sizeof(lua_State) + EXTRASPACE); lu_byte *block = (lu_byte *)luaM_malloc(L, sizeof(lua_State) + EXTRASPACE);
if (block == NULL) return NULL; if (block == NULL) return NULL;
else { else {
@ -43,13 +51,9 @@ static lua_State *newthread (lua_State *L) {
} }
/* static void freestate (lua_State *L, lua_State *L1) {
** you can change this function through the official API: luaM_free(L, cast(lu_byte *, L1) - EXTRASPACE,
** call `lua_setpanicf' sizeof(lua_State) + EXTRASPACE);
*/
static int default_panic (lua_State *L) {
UNUSED(L);
return 0;
} }
@ -69,29 +73,36 @@ static void stack_init (lua_State *L1, lua_State *L) {
} }
static void freestack (lua_State *L, lua_State *L1) {
luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo);
luaM_freearray(L, L1->stack, L1->stacksize, TObject);
}
/* /*
** 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) {
UNUSED(ud);
/* create a new global state */ /* create a new global state */
L->l_G = luaM_new(L, global_State); global_State *g = luaM_new(L, global_State);
G(L)->mainthread = L; UNUSED(ud);
G(L)->GCthreshold = 0; /* mark it as unfinished state */ L->l_G = g;
G(L)->strt.size = 0; g->mainthread = L;
G(L)->strt.nuse = 0; g->GCthreshold = 0; /* mark it as unfinished state */
G(L)->strt.hash = NULL; g->strt.size = 0;
g->strt.nuse = 0;
g->strt.hash = NULL;
setnilvalue(defaultmeta(L)); setnilvalue(defaultmeta(L));
setnilvalue(registry(L)); setnilvalue(registry(L));
luaZ_initbuffer(L, &G(L)->buff); luaZ_initbuffer(L, &g->buff);
G(L)->panic = &default_panic; g->panic = &default_panic;
G(L)->rootgc = NULL; g->rootgc = NULL;
G(L)->rootudata = NULL; g->rootudata = NULL;
G(L)->tmudata = NULL; g->tmudata = NULL;
setnilvalue(key(G(L)->dummynode)); setnilvalue(key(g->dummynode));
setnilvalue(val(G(L)->dummynode)); setnilvalue(val(g->dummynode));
G(L)->dummynode->next = NULL; g->dummynode->next = NULL;
G(L)->nblocks = sizeof(lua_State) + sizeof(global_State); 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;
@ -103,7 +114,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(L)->GCthreshold = 4*G(L)->nblocks; g->GCthreshold = 4*G(L)->nblocks;
} }
@ -124,8 +135,26 @@ static void preinit_state (lua_State *L) {
} }
static void close_state (lua_State *L) {
luaF_close(L, L->stack); /* close all upvalues for this thread */
if (G(L)) { /* close global state */
luaC_sweep(L, 1); /* collect all elements */
lua_assert(G(L)->rootgc == NULL);
lua_assert(G(L)->rootudata == NULL);
luaS_freeall(L);
luaZ_freebuffer(L, &G(L)->buff);
}
freestack(L, L);
if (G(L)) {
lua_assert(G(L)->nblocks == sizeof(lua_State) + sizeof(global_State));
luaM_freelem(L, G(L));
}
freestate(NULL, L);
}
lua_State *luaE_newthread (lua_State *L) { lua_State *luaE_newthread (lua_State *L) {
lua_State *L1 = newthread(L); lua_State *L1 = mallocstate(L);
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;
@ -135,8 +164,16 @@ lua_State *luaE_newthread (lua_State *L) {
} }
void luaE_freethread (lua_State *L, lua_State *L1) {
luaF_close(L1, L1->stack); /* close all upvalues for this thread */
lua_assert(L1->openupval == NULL);
freestack(L, L1);
freestate(L, L1);
}
LUA_API lua_State *lua_open (void) { LUA_API lua_State *lua_open (void) {
lua_State *L = newthread(NULL); lua_State *L = mallocstate(NULL);
if (L) { /* allocation OK? */ if (L) { /* allocation OK? */
L->tt = LUA_TTHREAD; L->tt = LUA_TTHREAD;
preinit_state(L); preinit_state(L);
@ -152,30 +189,6 @@ LUA_API lua_State *lua_open (void) {
} }
void luaE_freethread (lua_State *L, lua_State *L1) {
luaF_close(L1, L1->stack); /* close all upvalues for this thread */
lua_assert(L1->openupval == NULL);
luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo);
luaM_freearray(L, L1->stack, L1->stacksize, TObject);
luaM_free(L, cast(lu_byte *, L1) - EXTRASPACE,
sizeof(lua_State) + EXTRASPACE);
}
static void close_state (lua_State *L) {
luaF_close(L, L->stack); /* close all upvalues for this thread */
if (G(L)) { /* close global state */
luaC_sweep(L, 1); /* collect all elements */
lua_assert(G(L)->rootgc == NULL);
lua_assert(G(L)->rootudata == NULL);
luaS_freeall(L);
luaZ_freebuffer(L, &G(L)->buff);
luaM_freelem(NULL, L->l_G);
}
luaE_freethread(NULL, L);
}
LUA_API void lua_close (lua_State *L) { LUA_API void lua_close (lua_State *L) {
lua_lock(L); lua_lock(L);
L = G(L)->mainthread; /* only the main thread can be closed */ L = G(L)->mainthread; /* only the main thread can be closed */