** Garbage-collection function */
| 1091 | ** Garbage-collection function |
| 1092 | */ |
| 1093 | LUA_API int lua_gc (lua_State *L, int what, ...) { |
| 1094 | va_list argp; |
| 1095 | int res = 0; |
| 1096 | global_State *g = G(L); |
| 1097 | lua_lock(L); |
| 1098 | va_start(argp, what); |
| 1099 | switch (what) { |
| 1100 | case LUA_GCSTOP: { |
| 1101 | g->gcrunning = 0; |
| 1102 | break; |
| 1103 | } |
| 1104 | case LUA_GCRESTART: { |
| 1105 | luaE_setdebt(g, 0); |
| 1106 | g->gcrunning = 1; |
| 1107 | break; |
| 1108 | } |
| 1109 | case LUA_GCCOLLECT: { |
| 1110 | luaC_fullgc(L, 0); |
| 1111 | break; |
| 1112 | } |
| 1113 | case LUA_GCCOUNT: { |
| 1114 | /* GC values are expressed in Kbytes: #bytes/2^10 */ |
| 1115 | res = cast_int(gettotalbytes(g) >> 10); |
| 1116 | break; |
| 1117 | } |
| 1118 | case LUA_GCCOUNTB: { |
| 1119 | res = cast_int(gettotalbytes(g) & 0x3ff); |
| 1120 | break; |
| 1121 | } |
| 1122 | case LUA_GCSTEP: { |
| 1123 | int data = va_arg(argp, int); |
| 1124 | l_mem debt = 1; /* =1 to signal that it did an actual step */ |
| 1125 | lu_byte oldrunning = g->gcrunning; |
| 1126 | g->gcrunning = 1; /* allow GC to run */ |
| 1127 | if (data == 0) { |
| 1128 | luaE_setdebt(g, 0); /* do a basic step */ |
| 1129 | luaC_step(L); |
| 1130 | } |
| 1131 | else { /* add 'data' to total debt */ |
| 1132 | debt = cast(l_mem, data) * 1024 + g->GCdebt; |
| 1133 | luaE_setdebt(g, debt); |
| 1134 | luaC_checkGC(L); |
| 1135 | } |
| 1136 | g->gcrunning = oldrunning; /* restore previous state */ |
| 1137 | if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */ |
| 1138 | res = 1; /* signal it */ |
| 1139 | break; |
| 1140 | } |
| 1141 | case LUA_GCSETPAUSE: { |
| 1142 | int data = va_arg(argp, int); |
| 1143 | res = getgcparam(g->gcpause); |
| 1144 | setgcparam(g->gcpause, data); |
| 1145 | break; |
| 1146 | } |
| 1147 | case LUA_GCSETSTEPMUL: { |
| 1148 | int data = va_arg(argp, int); |
| 1149 | res = getgcparam(g->gcstepmul); |
| 1150 | setgcparam(g->gcstepmul, data); |
no test coverage detected