summaryrefslogtreecommitdiff
path: root/src/lstate.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lstate.c')
-rw-r--r--src/lstate.c85
1 files changed, 53 insertions, 32 deletions
diff --git a/src/lstate.c b/src/lstate.c
index 207a106d..1628ad91 100644
--- a/src/lstate.c
+++ b/src/lstate.c
@@ -1,5 +1,5 @@
/*
-** $Id: lstate.c,v 2.99 2012/10/02 17:40:53 roberto Exp $
+** $Id: lstate.c,v 2.121 2014/02/18 13:46:26 roberto Exp $
** Global State
** See Copyright Notice in lua.h
*/
@@ -30,10 +30,6 @@
#define LUAI_GCPAUSE 200 /* 200% */
#endif
-#if !defined(LUAI_GCMAJOR)
-#define LUAI_GCMAJOR 200 /* 200% */
-#endif
-
#if !defined(LUAI_GCMUL)
#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
#endif
@@ -119,6 +115,9 @@ CallInfo *luaE_extendCI (lua_State *L) {
}
+/*
+** free all CallInfo structures not in use by a thread
+*/
void luaE_freeCI (lua_State *L) {
CallInfo *ci = L->ci;
CallInfo *next = ci->next;
@@ -130,6 +129,22 @@ void luaE_freeCI (lua_State *L) {
}
+/*
+** free half of the CallInfo structures not in use by a thread
+*/
+void luaE_shrinkCI (lua_State *L) {
+ CallInfo *ci = L->ci;
+ while (ci->next != NULL) { /* while there is 'next' */
+ CallInfo *next2 = ci->next->next; /* next's next */
+ if (next2 == NULL) break;
+ luaM_free(L, ci->next); /* remove next */
+ ci->next = next2; /* remove 'next' from the list */
+ next2->previous = ci;
+ ci = next2;
+ }
+}
+
+
static void stack_init (lua_State *L1, lua_State *L) {
int i; CallInfo *ci;
/* initialize stack array */
@@ -163,22 +178,23 @@ static void freestack (lua_State *L) {
** Create registry table and its predefined values
*/
static void init_registry (lua_State *L, global_State *g) {
- TValue mt;
+ TValue temp;
/* create registry */
Table *registry = luaH_new(L);
sethvalue(L, &g->l_registry, registry);
luaH_resize(L, registry, LUA_RIDX_LAST, 0);
/* registry[LUA_RIDX_MAINTHREAD] = L */
- setthvalue(L, &mt, L);
- luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &mt);
+ setthvalue(L, &temp, L); /* temp = L */
+ luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp);
/* registry[LUA_RIDX_GLOBALS] = table of globals */
- sethvalue(L, &mt, luaH_new(L));
- luaH_setint(L, registry, LUA_RIDX_GLOBALS, &mt);
+ sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */
+ luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp);
}
/*
-** open parts of the state that may cause memory-allocation errors
+** open parts of the state that may cause memory-allocation errors.
+** ('g->version' != NULL flags that the state was completely build)
*/
static void f_luaopen (lua_State *L, void *ud) {
global_State *g = G(L);
@@ -190,20 +206,23 @@ static void f_luaopen (lua_State *L, void *ud) {
luaX_init(L);
/* pre-create memory-error message */
g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
- luaS_fix(g->memerrmsg); /* it should never be collected */
+ luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
g->gcrunning = 1; /* allow gc */
+ g->version = lua_version(NULL);
+ luai_userstateopen(L);
}
/*
-** preinitialize a state with consistent values without allocating
+** preinitialize a thread with consistent values without allocating
** any memory (to avoid errors)
*/
-static void preinit_state (lua_State *L, global_State *g) {
+static void preinit_thread (lua_State *L, global_State *g) {
G(L) = g;
L->stack = NULL;
L->ci = NULL;
L->stacksize = 0;
+ L->twups = L; /* thread has no upvalues */
L->errorJmp = NULL;
L->nCcalls = 0;
L->hook = NULL;
@@ -222,6 +241,8 @@ static void close_state (lua_State *L) {
global_State *g = G(L);
luaF_close(L, L->stack); /* close all upvalues for this thread */
luaC_freeallobjects(L); /* collect all objects */
+ if (g->version) /* closing a fully built state? */
+ luai_userstateclose(L);
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
luaZ_freebuffer(L, &g->buff);
freestack(L);
@@ -231,13 +252,20 @@ static void close_state (lua_State *L) {
LUA_API lua_State *lua_newthread (lua_State *L) {
+ global_State *g = G(L);
lua_State *L1;
lua_lock(L);
luaC_checkGC(L);
- L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX), NULL, offsetof(LX, l))->th;
+ /* create new thread */
+ L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l;
+ L1->marked = luaC_white(g);
+ L1->tt = LUA_TTHREAD;
+ /* link it on list 'allgc' */
+ L1->next = g->allgc;
+ g->allgc = obj2gco(L1);
setthvalue(L, L->top, L1);
api_incr_top(L);
- preinit_state(L1, G(L));
+ preinit_thread(L1, g);
L1->hookmask = L->hookmask;
L1->basehookcount = L->basehookcount;
L1->hook = L->hook;
@@ -269,36 +297,32 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
g = &l->g;
L->next = NULL;
L->tt = LUA_TTHREAD;
- g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
+ g->currentwhite = bitmask(WHITE0BIT);
L->marked = luaC_white(g);
- g->gckind = KGC_NORMAL;
- preinit_state(L, g);
+ preinit_thread(L, g);
g->frealloc = f;
g->ud = ud;
g->mainthread = L;
g->seed = makeseed(L);
- g->uvhead.u.l.prev = &g->uvhead;
- g->uvhead.u.l.next = &g->uvhead;
g->gcrunning = 0; /* no GC while building state */
g->GCestimate = 0;
- g->strt.size = 0;
- g->strt.nuse = 0;
+ g->strt.size = g->strt.nuse = 0;
g->strt.hash = NULL;
setnilvalue(&g->l_registry);
luaZ_initbuffer(L, &g->buff);
g->panic = NULL;
- g->version = lua_version(NULL);
+ g->version = NULL;
g->gcstate = GCSpause;
- g->allgc = NULL;
- g->finobj = NULL;
- g->tobefnz = NULL;
- g->sweepgc = g->sweepfin = NULL;
+ g->gckind = KGC_NORMAL;
+ g->allgc = g->finobj = g->tobefnz = g->fixedgc = NULL;
+ g->sweepgc = NULL;
g->gray = g->grayagain = NULL;
g->weak = g->ephemeron = g->allweak = NULL;
+ g->twups = NULL;
g->totalbytes = sizeof(LG);
g->GCdebt = 0;
+ g->gcfinnum = 0;
g->gcpause = LUAI_GCPAUSE;
- g->gcmajorinc = LUAI_GCMAJOR;
g->gcstepmul = LUAI_GCMUL;
for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
@@ -306,8 +330,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
close_state(L);
L = NULL;
}
- else
- luai_userstateopen(L);
return L;
}
@@ -315,7 +337,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
LUA_API void lua_close (lua_State *L) {
L = G(L)->mainthread; /* only the main thread can be closed */
lua_lock(L);
- luai_userstateclose(L);
close_state(L);
}