** Garbage-collection function */
| 1124 | ** Garbage-collection function |
| 1125 | */ |
| 1126 | LUA_API int lua_gc (lua_State *L, int what, ...) { |
| 1127 | va_list argp; |
| 1128 | int res = 0; |
| 1129 | global_State *g; |
| 1130 | lua_lock(L); |
| 1131 | g = G(L); |
| 1132 | va_start(argp, what); |
| 1133 | switch (what) { |
| 1134 | case LUA_GCSTOP: { |
| 1135 | g->gcrunning = 0; |
| 1136 | break; |
| 1137 | } |
| 1138 | case LUA_GCRESTART: { |
| 1139 | luaE_setdebt(g, 0); |
| 1140 | g->gcrunning = 1; |
| 1141 | break; |
| 1142 | } |
| 1143 | case LUA_GCCOLLECT: { |
| 1144 | luaC_fullgc(L, 0); |
| 1145 | break; |
| 1146 | } |
| 1147 | case LUA_GCCOUNT: { |
| 1148 | /* GC values are expressed in Kbytes: #bytes/2^10 */ |
| 1149 | res = cast_int(gettotalbytes(g) >> 10); |
| 1150 | break; |
| 1151 | } |
| 1152 | case LUA_GCCOUNTB: { |
| 1153 | res = cast_int(gettotalbytes(g) & 0x3ff); |
| 1154 | break; |
| 1155 | } |
| 1156 | case LUA_GCSTEP: { |
| 1157 | int data = va_arg(argp, int); |
| 1158 | l_mem debt = 1; /* =1 to signal that it did an actual step */ |
| 1159 | lu_byte oldrunning = g->gcrunning; |
| 1160 | g->gcrunning = 1; /* allow GC to run */ |
| 1161 | if (data == 0) { |
| 1162 | luaE_setdebt(g, 0); /* do a basic step */ |
| 1163 | luaC_step(L); |
| 1164 | } |
| 1165 | else { /* add 'data' to total debt */ |
| 1166 | debt = cast(l_mem, data) * 1024 + g->GCdebt; |
| 1167 | luaE_setdebt(g, debt); |
| 1168 | luaC_checkGC(L); |
| 1169 | } |
| 1170 | g->gcrunning = oldrunning; /* restore previous state */ |
| 1171 | if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */ |
| 1172 | res = 1; /* signal it */ |
| 1173 | break; |
| 1174 | } |
| 1175 | case LUA_GCSETPAUSE: { |
| 1176 | int data = va_arg(argp, int); |
| 1177 | res = getgcparam(g->gcpause); |
| 1178 | setgcparam(g->gcpause, data); |
| 1179 | break; |
| 1180 | } |
| 1181 | case LUA_GCSETSTEPMUL: { |
| 1182 | int data = va_arg(argp, int); |
| 1183 | res = getgcparam(g->gcstepmul); |
no test coverage detected