** Garbage-collection function */
| 1168 | ** Garbage-collection function |
| 1169 | */ |
| 1170 | LUA_API int lua_gc (lua_State *L, int what, ...) { |
| 1171 | va_list argp; |
| 1172 | int res = 0; |
| 1173 | global_State *g = G(L); |
| 1174 | if (g->gcstp & (GCSTPGC | GCSTPCLS)) /* internal stop? */ |
| 1175 | return -1; /* all options are invalid when stopped */ |
| 1176 | lua_lock(L); |
| 1177 | va_start(argp, what); |
| 1178 | switch (what) { |
| 1179 | case LUA_GCSTOP: { |
| 1180 | g->gcstp = GCSTPUSR; /* stopped by the user */ |
| 1181 | break; |
| 1182 | } |
| 1183 | case LUA_GCRESTART: { |
| 1184 | luaE_setdebt(g, 0); |
| 1185 | g->gcstp = 0; /* (other bits must be zero here) */ |
| 1186 | break; |
| 1187 | } |
| 1188 | case LUA_GCCOLLECT: { |
| 1189 | luaC_fullgc(L, 0); |
| 1190 | break; |
| 1191 | } |
| 1192 | case LUA_GCCOUNT: { |
| 1193 | /* GC values are expressed in Kbytes: #bytes/2^10 */ |
| 1194 | res = cast_int(gettotalbytes(g) >> 10); |
| 1195 | break; |
| 1196 | } |
| 1197 | case LUA_GCCOUNTB: { |
| 1198 | res = cast_int(gettotalbytes(g) & 0x3ff); |
| 1199 | break; |
| 1200 | } |
| 1201 | case LUA_GCSTEP: { |
| 1202 | lu_byte oldstp = g->gcstp; |
| 1203 | l_mem n = cast(l_mem, va_arg(argp, size_t)); |
| 1204 | int work = 0; /* true if GC did some work */ |
| 1205 | g->gcstp = 0; /* allow GC to run (other bits must be zero here) */ |
| 1206 | if (n <= 0) |
| 1207 | n = g->GCdebt; /* force to run one basic step */ |
| 1208 | luaE_setdebt(g, g->GCdebt - n); |
| 1209 | luaC_condGC(L, (void)0, work = 1); |
| 1210 | if (work && g->gcstate == GCSpause) /* end of cycle? */ |
| 1211 | res = 1; /* signal it */ |
| 1212 | g->gcstp = oldstp; /* restore previous state */ |
| 1213 | break; |
| 1214 | } |
| 1215 | case LUA_GCISRUNNING: { |
| 1216 | res = gcrunning(g); |
| 1217 | break; |
| 1218 | } |
| 1219 | case LUA_GCGEN: { |
| 1220 | res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN; |
| 1221 | luaC_changemode(L, KGC_GENMINOR); |
| 1222 | break; |
| 1223 | } |
| 1224 | case LUA_GCINC: { |
| 1225 | res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN; |
| 1226 | luaC_changemode(L, KGC_INC); |
| 1227 | break; |
no test coverage detected