commit be762f38bd8484514417c9d1feff8ce678e86ef9
parent c8a79057f723e1e77a85570893adde1d563e441f
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Tue, 19 Nov 2002 12:11:51 -0200
better organization of "malloc responsabilities"
Diffstat:
M | lstate.c | | | 119 | ++++++++++++++++++++++++++++++++++++++++++++----------------------------------- |
1 file changed, 66 insertions(+), 53 deletions(-)
diff --git a/lstate.c b/lstate.c
@@ -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
** See Copyright Notice in lua.h
*/
@@ -30,10 +30,18 @@ union UEXTRASPACE {L_Umaxalign a; LUA_USERSTATE b;};
#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);
if (block == NULL) return NULL;
else {
@@ -43,13 +51,9 @@ static lua_State *newthread (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 void freestate (lua_State *L, lua_State *L1) {
+ luaM_free(L, cast(lu_byte *, L1) - EXTRASPACE,
+ sizeof(lua_State) + EXTRASPACE);
}
@@ -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
*/
static void f_luaopen (lua_State *L, void *ud) {
- UNUSED(ud);
/* create a new global state */
- L->l_G = luaM_new(L, global_State);
- G(L)->mainthread = L;
- G(L)->GCthreshold = 0; /* mark it as unfinished state */
- G(L)->strt.size = 0;
- G(L)->strt.nuse = 0;
- G(L)->strt.hash = NULL;
+ global_State *g = luaM_new(L, global_State);
+ UNUSED(ud);
+ 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(L)->buff);
- G(L)->panic = &default_panic;
- G(L)->rootgc = NULL;
- G(L)->rootudata = NULL;
- G(L)->tmudata = NULL;
- setnilvalue(key(G(L)->dummynode));
- setnilvalue(val(G(L)->dummynode));
- G(L)->dummynode->next = NULL;
- G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
+ luaZ_initbuffer(L, &g->buff);
+ g->panic = &default_panic;
+ g->rootgc = NULL;
+ g->rootudata = NULL;
+ g->tmudata = NULL;
+ setnilvalue(key(g->dummynode));
+ setnilvalue(val(g->dummynode));
+ g->dummynode->next = NULL;
+ g->nblocks = sizeof(lua_State) + sizeof(global_State);
stack_init(L, L); /* init stack */
/* create default meta table with a dummy table, and then close the loop */
defaultmeta(L)->tt = LUA_TTABLE;
@@ -103,7 +114,7 @@ static void f_luaopen (lua_State *L, void *ud) {
luaT_init(L);
luaX_init(L);
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 *L1 = newthread(L);
+ lua_State *L1 = mallocstate(L);
luaC_link(L, valtogco(L1), LUA_TTHREAD);
preinit_state(L1);
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_State *L = newthread(NULL);
+ lua_State *L = mallocstate(NULL);
if (L) { /* allocation OK? */
L->tt = LUA_TTHREAD;
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_lock(L);
L = G(L)->mainthread; /* only the main thread can be closed */