1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
/*
** $Id: lgc.c,v 1.90 2001/02/20 18:28:11 roberto Exp roberto $
** Garbage Collector
** See Copyright Notice in lua.h
*/
#include "lua.h"
#include "ldo.h"
#include "lfunc.h"
#include "lgc.h"
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
/*
** optional lock for GC
** (when Lua calls GC tag methods it unlocks the regular lock)
*/
#ifndef LUA_LOCKGC
#define LUA_LOCKGC(L) {
#endif
#ifndef LUA_UNLOCKGC
#define LUA_UNLOCKGC(L) }
#endif
typedef struct GCState {
Hash *tmark; /* list of marked tables to be visited */
Closure *cmark; /* list of marked closures to be visited */
} GCState;
static void markobject (GCState *st, TObject *o);
/* mark a string; marks larger than 1 cannot be changed */
#define strmark(s) {if ((s)->marked == 0) (s)->marked = 1;}
static void protomark (Proto *f) {
if (!f->marked) {
int i;
f->marked = 1;
strmark(f->source);
for (i=0; i<f->sizekstr; i++)
strmark(f->kstr[i]);
for (i=0; i<f->sizekproto; i++)
protomark(f->kproto[i]);
for (i=0; i<f->sizelocvars; i++) /* mark local-variable names */
strmark(f->locvars[i].varname);
}
}
static void markclosure (GCState *st, Closure *cl) {
if (!ismarked(cl)) {
if (!cl->isC) {
lua_assert(cl->nupvalues == cl->f.l->nupvalues);
protomark(cl->f.l);
}
cl->mark = st->cmark; /* chain it for later traversal */
st->cmark = cl;
}
}
static void marktable (GCState *st, Hash *h) {
if (!ismarked(h)) {
h->mark = st->tmark; /* chain it in list of marked */
st->tmark = h;
}
}
static void markobject (GCState *st, TObject *o) {
switch (ttype(o)) {
case LUA_TUSERDATA: case LUA_TSTRING:
strmark(tsvalue(o));
break;
case LUA_TMARK:
markclosure(st, infovalue(o)->func);
break;
case LUA_TFUNCTION:
markclosure(st, clvalue(o));
break;
case LUA_TTABLE: {
marktable(st, hvalue(o));
break;
}
default: break; /* numbers, etc */
}
}
static void markstacks (lua_State *L, GCState *st) {
lua_State *L1 = L;
do { /* for each thread */
StkId o, lim;
marktable(st, L1->gt); /* mark table of globals */
for (o=L1->stack; o<L1->top; o++)
markobject(st, o);
lim = (L1->stack_last - L1->top > MAXSTACK) ? L1->top+MAXSTACK
: L1->stack_last;
for (; o<=lim; o++) setnilvalue(o);
lua_assert(L1->previous->next == L1 && L1->next->previous == L1);
L1 = L1->next;
} while (L1 != L);
}
static void marklock (global_State *G, GCState *st) {
int i;
for (i=0; i<G->nref; i++) {
if (G->refArray[i].st == LOCK)
markobject(st, &G->refArray[i].o);
}
}
static void marktagmethods (global_State *G, GCState *st) {
int t;
for (t=0; t<G->ntag; t++) {
struct TM *tm = &G->TMtable[t];
int e;
if (tm->name) strmark(tm->name);
for (e=0; e<TM_N; e++) {
Closure *cl = tm->method[e];
if (cl) markclosure(st, cl);
}
}
}
static void traverseclosure (GCState *st, Closure *f) {
int i;
for (i=0; i<f->nupvalues; i++) /* mark its upvalues */
markobject(st, &f->upvalue[i]);
}
static void traversetable (GCState *st, Hash *h) {
int i;
for (i=0; i<h->size; i++) {
Node *n = node(h, i);
if (ttype(val(n)) == LUA_TNIL) {
if (ttype_key(n) != LUA_TNIL)
n->key_value.ts = NULL; /* dead key; remove it */
}
else {
lua_assert(ttype_key(n) != LUA_TNIL);
if (ttype_key(n) != LUA_TNUMBER) {
TObject o;
setkey2obj(&o, n);
markobject(st, &o);
}
markobject(st, &n->val);
}
}
}
static void markall (lua_State *L) {
GCState st;
st.cmark = NULL;
st.tmark = NULL;
marktagmethods(G(L), &st); /* mark tag methods */
markstacks(L, &st); /* mark all stacks */
marklock(G(L), &st); /* mark locked objects */
marktable(&st, G(L)->type2tag);
for (;;) { /* mark tables and closures */
if (st.cmark) {
Closure *f = st.cmark; /* get first closure from list */
st.cmark = f->mark; /* remove it from list */
traverseclosure(&st, f);
}
else if (st.tmark) {
Hash *h = st.tmark; /* get first table from list */
st.tmark = h->mark; /* remove it from list */
traversetable(&st, h);
}
else break; /* nothing else to mark */
}
}
static int hasmark (const TObject *o) {
/* valid only for locked objects */
switch (ttype(o)) {
case LUA_TSTRING: case LUA_TUSERDATA:
return tsvalue(o)->marked;
case LUA_TTABLE:
return ismarked(hvalue(o));
case LUA_TFUNCTION:
return ismarked(clvalue(o));
default: /* number */
return 1;
}
}
/* macro for internal debugging; check if a link of free refs is valid */
#define VALIDLINK(L, st,n) (NONEXT <= (st) && (st) < (n))
static void invalidaterefs (global_State *G) {
int n = G->nref;
int i;
for (i=0; i<n; i++) {
struct Ref *r = &G->refArray[i];
if (r->st == HOLD && !hasmark(&r->o))
r->st = COLLECTED;
lua_assert((r->st == LOCK && hasmark(&r->o)) ||
(r->st == HOLD && hasmark(&r->o)) ||
r->st == COLLECTED ||
r->st == NONEXT ||
(r->st < n && VALIDLINK(L, G->refArray[r->st].st, n)));
}
lua_assert(VALIDLINK(L, G->refFree, n));
}
static void collectproto (lua_State *L) {
Proto **p = &G(L)->rootproto;
Proto *next;
while ((next = *p) != NULL) {
if (next->marked) {
next->marked = 0;
p = &next->next;
}
else {
*p = next->next;
luaF_freeproto(L, next);
}
}
}
static void collectclosure (lua_State *L) {
Closure **p = &G(L)->rootcl;
Closure *next;
while ((next = *p) != NULL) {
if (ismarked(next)) {
next->mark = next; /* unmark */
p = &next->next;
}
else {
*p = next->next;
luaF_freeclosure(L, next);
}
}
}
static void collecttable (lua_State *L) {
Hash **p = &G(L)->roottable;
Hash *next;
while ((next = *p) != NULL) {
if (ismarked(next)) {
next->mark = next; /* unmark */
p = &next->next;
}
else {
*p = next->next;
luaH_free(L, next);
}
}
}
static void checktab (lua_State *L, stringtable *tb) {
if (tb->nuse < (ls_nstr)(tb->size/4) && tb->size > MINPOWER2)
luaS_resize(L, tb, tb->size/2); /* table is too big */
}
static void collectstrings (lua_State *L, int all) {
int i;
for (i=0; i<G(L)->strt.size; i++) { /* for each list */
TString **p = &G(L)->strt.hash[i];
TString *next;
while ((next = *p) != NULL) {
if (next->marked && !all) { /* preserve? */
if (next->marked < FIXMARK) /* does not change FIXMARKs */
next->marked = 0;
p = &next->nexthash;
}
else { /* collect */
*p = next->nexthash;
G(L)->strt.nuse--;
luaM_free(L, next, sizestring(next->len));
}
}
}
checktab(L, &G(L)->strt);
}
static void collectudata (lua_State *L, int all) {
int i;
for (i=0; i<G(L)->udt.size; i++) { /* for each list */
TString **p = &G(L)->udt.hash[i];
TString *next;
while ((next = *p) != NULL) {
lua_assert(next->marked <= 1);
if (next->marked && !all) { /* preserve? */
next->marked = 0;
p = &next->nexthash;
}
else { /* collect */
int tag = next->u.d.tag;
*p = next->nexthash;
next->nexthash = G(L)->TMtable[tag].collected; /* chain udata */
G(L)->TMtable[tag].collected = next;
G(L)->udt.nuse--;
}
}
}
checktab(L, &G(L)->udt);
}
#define MINBUFFER 256
static void checkMbuffer (lua_State *L) {
if (G(L)->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */
size_t newsize = G(L)->Mbuffsize/2; /* still larger than MINBUFFER */
luaM_reallocvector(L, G(L)->Mbuffer, G(L)->Mbuffsize, newsize, char);
G(L)->Mbuffsize = newsize;
}
}
static void callgcTM (lua_State *L, const TObject *obj) {
Closure *tm = luaT_gettmbyObj(G(L), obj, TM_GC);
if (tm != NULL) {
int oldah = L->allowhooks;
L->allowhooks = 0; /* stop debug hooks during GC tag methods */
luaD_checkstack(L, 2);
setclvalue(L->top, tm);
setobj(L->top+1, obj);
L->top += 2;
luaD_call(L, L->top-2, 0);
L->allowhooks = oldah; /* restore hooks */
}
}
static void callgcTMudata (lua_State *L) {
int tag;
G(L)->GCthreshold = 2*G(L)->nblocks; /* avoid GC during tag methods */
for (tag=G(L)->ntag-1; tag>=0; tag--) { /* for each tag (in reverse order) */
TString *udata;
while ((udata = G(L)->TMtable[tag].collected) != NULL) {
TObject obj;
G(L)->TMtable[tag].collected = udata->nexthash; /* remove it from list */
setuvalue(&obj, udata);
callgcTM(L, &obj);
luaM_free(L, udata, sizeudata(udata->len));
}
}
}
void luaC_collect (lua_State *L, int all) {
LUA_LOCKGC(L);
collectudata(L, all);
callgcTMudata(L);
collectstrings(L, all);
collecttable(L);
collectproto(L);
collectclosure(L);
LUA_UNLOCKGC(L);
}
void luaC_collectgarbage (lua_State *L) {
markall(L);
invalidaterefs(G(L)); /* check unlocked references */
luaC_collect(L, 0);
checkMbuffer(L);
G(L)->GCthreshold = 2*G(L)->nblocks; /* set new threshold */
callgcTM(L, &luaO_nilobject);
}
void luaC_checkGC (lua_State *L) {
if (G(L)->nblocks >= G(L)->GCthreshold)
luaC_collectgarbage(L);
}
|