commit 4c6dfc342b026a51bfe3e2d56b1032fb670a2577
parent 686e57cf9c61070ff961407e3304071e171542f4
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Tue, 17 Sep 2013 12:39:41 -0300
CallInfo lists shrinks together with their associated stacks
Diffstat:
4 files changed, 29 insertions(+), 6 deletions(-)
diff --git a/ldo.c b/ldo.c
@@ -1,5 +1,5 @@
/*
-** $Id: ldo.c,v 2.109 2013/04/19 21:05:04 roberto Exp roberto $
+** $Id: ldo.c,v 2.110 2013/08/27 18:53:35 roberto Exp roberto $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
@@ -206,7 +206,11 @@ void luaD_shrinkstack (lua_State *L) {
int inuse = stackinuse(L);
int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK;
if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK;
- if (inuse > LUAI_MAXSTACK || /* handling stack overflow? */
+ if (L->stacksize > LUAI_MAXSTACK) /* was handling stack overflow? */
+ luaE_freeCI(L); /* free all CIs (list grew because of an error) */
+ else
+ luaE_shrinkCI(L); /* shrink list */
+ if (inuse > LUAI_MAXSTACK || /* still handling stack overflow? */
goodsize >= L->stacksize) /* would grow instead of shrink? */
condmovestack(L); /* don't change stack (change only for debugging) */
else
diff --git a/lgc.c b/lgc.c
@@ -1,5 +1,5 @@
/*
-** $Id: lgc.c,v 2.164 2013/09/11 14:56:15 roberto Exp roberto $
+** $Id: lgc.c,v 2.165 2013/09/13 16:21:52 roberto Exp roberto $
** Garbage Collector
** See Copyright Notice in lua.h
*/
@@ -496,7 +496,6 @@ static lu_mem traversestack (global_State *g, lua_State *th) {
}
else {
CallInfo *ci;
- luaE_freeCI(th); /* free extra CallInfo slots */
for (ci = &th->base_ci; ci != th->ci; ci = ci->next)
n++; /* count call infos to compute size */
/* should not change the stack during an emergency gc cycle */
diff --git a/lstate.c b/lstate.c
@@ -1,5 +1,5 @@
/*
-** $Id: lstate.c,v 2.113 2013/09/11 14:09:55 roberto Exp roberto $
+** $Id: lstate.c,v 2.114 2013/09/13 16:21:52 roberto Exp roberto $
** Global State
** See Copyright Notice in lua.h
*/
@@ -119,6 +119,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 +133,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 */
diff --git a/lstate.h b/lstate.h
@@ -1,5 +1,5 @@
/*
-** $Id: lstate.h,v 2.95 2013/09/11 14:09:55 roberto Exp roberto $
+** $Id: lstate.h,v 2.96 2013/09/13 16:21:52 roberto Exp roberto $
** Global State
** See Copyright Notice in lua.h
*/
@@ -214,6 +214,7 @@ LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
LUAI_FUNC void luaE_freeCI (lua_State *L);
+LUAI_FUNC void luaE_shrinkCI (lua_State *L);
#endif