Pārlūkot izejas kodu

CallInfo lists shrinks together with their associated stacks

Roberto Ierusalimschy 12 gadi atpakaļ
vecāks
revīzija
4c6dfc342b
4 mainītis faili ar 29 papildinājumiem un 6 dzēšanām
  1. 6 2
      ldo.c
  2. 1 2
      lgc.c
  3. 20 1
      lstate.c
  4. 2 1
      lstate.h

+ 6 - 2
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

+ 1 - 2
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 */

+ 20 - 1
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 */

+ 2 - 1
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