diff options
Diffstat (limited to 'lstate.c')
-rw-r--r-- | lstate.c | 63 |
1 files changed, 55 insertions, 8 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstate.c,v 2.144 2017/11/03 12:12:30 roberto Exp roberto $ | 2 | ** $Id: lstate.c,v 2.143 2017/10/31 17:54:35 roberto Exp $ |
3 | ** Global State | 3 | ** Global State |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -97,8 +97,51 @@ void luaE_setdebt (global_State *g, l_mem debt) { | |||
97 | } | 97 | } |
98 | 98 | ||
99 | 99 | ||
100 | CallInfo *luaE_extendCI (lua_State *L) { | ||
101 | CallInfo *ci = luaM_new(L, CallInfo); | ||
102 | lua_assert(L->ci->next == NULL); | ||
103 | L->ci->next = ci; | ||
104 | ci->previous = L->ci; | ||
105 | ci->next = NULL; | ||
106 | L->nci++; | ||
107 | return ci; | ||
108 | } | ||
109 | |||
110 | |||
111 | /* | ||
112 | ** free all CallInfo structures not in use by a thread | ||
113 | */ | ||
114 | void luaE_freeCI (lua_State *L) { | ||
115 | CallInfo *ci = L->ci; | ||
116 | CallInfo *next = ci->next; | ||
117 | ci->next = NULL; | ||
118 | while ((ci = next) != NULL) { | ||
119 | next = ci->next; | ||
120 | luaM_free(L, ci); | ||
121 | L->nci--; | ||
122 | } | ||
123 | } | ||
124 | |||
125 | |||
126 | /* | ||
127 | ** free half of the CallInfo structures not in use by a thread | ||
128 | */ | ||
129 | void luaE_shrinkCI (lua_State *L) { | ||
130 | CallInfo *ci = L->ci; | ||
131 | CallInfo *next2; /* next's next */ | ||
132 | /* while there are two nexts */ | ||
133 | while (ci->next != NULL && (next2 = ci->next->next) != NULL) { | ||
134 | luaM_free(L, ci->next); /* free next */ | ||
135 | L->nci--; | ||
136 | ci->next = next2; /* remove 'next' from the list */ | ||
137 | next2->previous = ci; | ||
138 | ci = next2; /* keep next's next */ | ||
139 | } | ||
140 | } | ||
141 | |||
142 | |||
100 | static void stack_init (lua_State *L1, lua_State *L) { | 143 | static void stack_init (lua_State *L1, lua_State *L) { |
101 | int i; | 144 | int i; CallInfo *ci; |
102 | /* initialize stack array */ | 145 | /* initialize stack array */ |
103 | L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, StackValue); | 146 | L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, StackValue); |
104 | L1->stacksize = BASIC_STACK_SIZE; | 147 | L1->stacksize = BASIC_STACK_SIZE; |
@@ -106,19 +149,23 @@ static void stack_init (lua_State *L1, lua_State *L) { | |||
106 | setnilvalue(s2v(L1->stack + i)); /* erase new stack */ | 149 | setnilvalue(s2v(L1->stack + i)); /* erase new stack */ |
107 | L1->top = L1->stack; | 150 | L1->top = L1->stack; |
108 | L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK; | 151 | L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK; |
109 | /* initialize first 'function' */ | 152 | /* initialize first ci */ |
110 | L1->func = L1->stack; | 153 | ci = &L1->base_ci; |
111 | L1->func->stkci.previous = 0; /* end of linked list */ | 154 | ci->next = ci->previous = NULL; |
112 | L1->func->stkci.framesize = LUA_MINSTACK + 1; | 155 | ci->callstatus = 0; |
113 | callstatus(L1->func) = 0; | 156 | ci->func = L1->top; |
114 | setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */ | 157 | setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */ |
115 | L1->top++; | 158 | L1->top++; |
159 | ci->top = L1->top + LUA_MINSTACK; | ||
160 | L1->ci = ci; | ||
116 | } | 161 | } |
117 | 162 | ||
118 | 163 | ||
119 | static void freestack (lua_State *L) { | 164 | static void freestack (lua_State *L) { |
120 | if (L->stack == NULL) | 165 | if (L->stack == NULL) |
121 | return; /* stack not completely built yet */ | 166 | return; /* stack not completely built yet */ |
167 | L->ci = &L->base_ci; /* free the entire 'ci' list */ | ||
168 | luaE_freeCI(L); | ||
122 | lua_assert(L->nci == 0); | 169 | lua_assert(L->nci == 0); |
123 | luaM_freearray(L, L->stack, L->stacksize); /* free stack array */ | 170 | luaM_freearray(L, L->stack, L->stacksize); /* free stack array */ |
124 | } | 171 | } |
@@ -168,7 +215,7 @@ static void f_luaopen (lua_State *L, void *ud) { | |||
168 | static void preinit_thread (lua_State *L, global_State *g) { | 215 | static void preinit_thread (lua_State *L, global_State *g) { |
169 | G(L) = g; | 216 | G(L) = g; |
170 | L->stack = NULL; | 217 | L->stack = NULL; |
171 | L->func = NULL; | 218 | L->ci = NULL; |
172 | L->nci = 0; | 219 | L->nci = 0; |
173 | L->stacksize = 0; | 220 | L->stacksize = 0; |
174 | L->twups = L; /* thread has no upvalues */ | 221 | L->twups = L; /* thread has no upvalues */ |