annotate lib/lua/lua-5.2.2/lgc.c @ 2575:a76d408c5132 tip

DrawTranslucent -> DrawTextureGrayShade Removed old texture drawing stuff
author a.parshin
date Wed, 09 Mar 2016 01:39:52 +0200
parents 41cc4dd3c122
children
rev   line source
1866
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
2 ** $Id: lgc.c,v 2.140 2013/03/16 21:10:18 roberto Exp $
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
3 ** Garbage Collector
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
4 ** See Copyright Notice in lua.h
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
5 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
6
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
7 #include <string.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
8
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
9 #define lgc_c
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
10 #define LUA_CORE
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
11
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
12 #include "lua.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
13
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
14 #include "ldebug.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
15 #include "ldo.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
16 #include "lfunc.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
17 #include "lgc.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
18 #include "lmem.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
19 #include "lobject.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
20 #include "lstate.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
21 #include "lstring.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
22 #include "ltable.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
23 #include "ltm.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
24
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
25
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
26
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
27 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
28 ** cost of sweeping one element (the size of a small object divided
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
29 ** by some adjust for the sweep speed)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
30 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
31 #define GCSWEEPCOST ((sizeof(TString) + 4) / 4)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
32
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
33 /* maximum number of elements to sweep in each single step */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
34 #define GCSWEEPMAX (cast_int((GCSTEPSIZE / GCSWEEPCOST) / 4))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
35
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
36 /* maximum number of finalizers to call in each GC step */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
37 #define GCFINALIZENUM 4
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
38
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
39
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
40 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
41 ** macro to adjust 'stepmul': 'stepmul' is actually used like
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
42 ** 'stepmul / STEPMULADJ' (value chosen by tests)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
43 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
44 #define STEPMULADJ 200
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
45
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
46
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
47 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
48 ** macro to adjust 'pause': 'pause' is actually used like
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
49 ** 'pause / PAUSEADJ' (value chosen by tests)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
50 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
51 #define PAUSEADJ 100
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
52
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
53
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
54 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
55 ** 'makewhite' erases all color bits plus the old bit and then
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
56 ** sets only the current white bit
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
57 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
58 #define maskcolors (~(bit2mask(BLACKBIT, OLDBIT) | WHITEBITS))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
59 #define makewhite(g,x) \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
60 (gch(x)->marked = cast_byte((gch(x)->marked & maskcolors) | luaC_white(g)))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
61
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
62 #define white2gray(x) resetbits(gch(x)->marked, WHITEBITS)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
63 #define black2gray(x) resetbit(gch(x)->marked, BLACKBIT)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
64
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
65
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
66 #define isfinalized(x) testbit(gch(x)->marked, FINALIZEDBIT)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
67
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
68 #define checkdeadkey(n) lua_assert(!ttisdeadkey(gkey(n)) || ttisnil(gval(n)))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
69
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
70
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
71 #define checkconsistency(obj) \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
72 lua_longassert(!iscollectable(obj) || righttt(obj))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
73
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
74
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
75 #define markvalue(g,o) { checkconsistency(o); \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
76 if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
77
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
78 #define markobject(g,t) { if ((t) && iswhite(obj2gco(t))) \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
79 reallymarkobject(g, obj2gco(t)); }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
80
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
81 static void reallymarkobject (global_State *g, GCObject *o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
82
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
83
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
84 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
85 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
86 ** Generic functions
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
87 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
88 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
89
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
90
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
91 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
92 ** one after last element in a hash array
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
93 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
94 #define gnodelast(h) gnode(h, cast(size_t, sizenode(h)))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
95
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
96
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
97 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
98 ** link table 'h' into list pointed by 'p'
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
99 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
100 #define linktable(h,p) ((h)->gclist = *(p), *(p) = obj2gco(h))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
101
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
102
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
103 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
104 ** if key is not marked, mark its entry as dead (therefore removing it
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
105 ** from the table)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
106 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
107 static void removeentry (Node *n) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
108 lua_assert(ttisnil(gval(n)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
109 if (valiswhite(gkey(n)))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
110 setdeadvalue(gkey(n)); /* unused and unmarked key; remove it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
111 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
112
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
113
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
114 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
115 ** tells whether a key or value can be cleared from a weak
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
116 ** table. Non-collectable objects are never removed from weak
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
117 ** tables. Strings behave as `values', so are never removed too. for
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
118 ** other objects: if really collected, cannot keep them; for objects
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
119 ** being finalized, keep them in keys, but not in values
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
120 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
121 static int iscleared (global_State *g, const TValue *o) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
122 if (!iscollectable(o)) return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
123 else if (ttisstring(o)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
124 markobject(g, rawtsvalue(o)); /* strings are `values', so are never weak */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
125 return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
126 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
127 else return iswhite(gcvalue(o));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
128 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
129
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
130
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
131 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
132 ** barrier that moves collector forward, that is, mark the white object
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
133 ** being pointed by a black object.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
134 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
135 void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
136 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
137 lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
138 lua_assert(g->gcstate != GCSpause);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
139 lua_assert(gch(o)->tt != LUA_TTABLE);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
140 if (keepinvariantout(g)) /* must keep invariant? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
141 reallymarkobject(g, v); /* restore invariant */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
142 else { /* sweep phase */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
143 lua_assert(issweepphase(g));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
144 makewhite(g, o); /* mark main obj. as white to avoid other barriers */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
145 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
146 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
147
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
148
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
149 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
150 ** barrier that moves collector backward, that is, mark the black object
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
151 ** pointing to a white object as gray again. (Current implementation
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
152 ** only works for tables; access to 'gclist' is not uniform across
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
153 ** different types.)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
154 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
155 void luaC_barrierback_ (lua_State *L, GCObject *o) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
156 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
157 lua_assert(isblack(o) && !isdead(g, o) && gch(o)->tt == LUA_TTABLE);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
158 black2gray(o); /* make object gray (again) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
159 gco2t(o)->gclist = g->grayagain;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
160 g->grayagain = o;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
161 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
162
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
163
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
164 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
165 ** barrier for prototypes. When creating first closure (cache is
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
166 ** NULL), use a forward barrier; this may be the only closure of the
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
167 ** prototype (if it is a "regular" function, with a single instance)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
168 ** and the prototype may be big, so it is better to avoid traversing
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
169 ** it again. Otherwise, use a backward barrier, to avoid marking all
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
170 ** possible instances.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
171 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
172 LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
173 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
174 lua_assert(isblack(obj2gco(p)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
175 if (p->cache == NULL) { /* first time? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
176 luaC_objbarrier(L, p, c);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
177 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
178 else { /* use a backward barrier */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
179 black2gray(obj2gco(p)); /* make prototype gray (again) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
180 p->gclist = g->grayagain;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
181 g->grayagain = obj2gco(p);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
182 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
183 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
184
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
185
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
186 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
187 ** check color (and invariants) for an upvalue that was closed,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
188 ** i.e., moved into the 'allgc' list
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
189 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
190 void luaC_checkupvalcolor (global_State *g, UpVal *uv) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
191 GCObject *o = obj2gco(uv);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
192 lua_assert(!isblack(o)); /* open upvalues are never black */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
193 if (isgray(o)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
194 if (keepinvariant(g)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
195 resetoldbit(o); /* see MOVE OLD rule */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
196 gray2black(o); /* it is being visited now */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
197 markvalue(g, uv->v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
198 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
199 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
200 lua_assert(issweepphase(g));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
201 makewhite(g, o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
202 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
203 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
204 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
205
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
206
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
207 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
208 ** create a new collectable object (with given type and size) and link
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
209 ** it to '*list'. 'offset' tells how many bytes to allocate before the
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
210 ** object itself (used only by states).
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
211 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
212 GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, GCObject **list,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
213 int offset) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
214 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
215 char *raw = cast(char *, luaM_newobject(L, novariant(tt), sz));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
216 GCObject *o = obj2gco(raw + offset);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
217 if (list == NULL)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
218 list = &g->allgc; /* standard list for collectable objects */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
219 gch(o)->marked = luaC_white(g);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
220 gch(o)->tt = tt;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
221 gch(o)->next = *list;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
222 *list = o;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
223 return o;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
224 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
225
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
226 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
227
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
228
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
229
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
230 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
231 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
232 ** Mark functions
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
233 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
234 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
235
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
236
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
237 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
238 ** mark an object. Userdata, strings, and closed upvalues are visited
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
239 ** and turned black here. Other objects are marked gray and added
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
240 ** to appropriate list to be visited (and turned black) later. (Open
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
241 ** upvalues are already linked in 'headuv' list.)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
242 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
243 static void reallymarkobject (global_State *g, GCObject *o) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
244 lu_mem size;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
245 white2gray(o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
246 switch (gch(o)->tt) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
247 case LUA_TSHRSTR:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
248 case LUA_TLNGSTR: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
249 size = sizestring(gco2ts(o));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
250 break; /* nothing else to mark; make it black */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
251 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
252 case LUA_TUSERDATA: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
253 Table *mt = gco2u(o)->metatable;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
254 markobject(g, mt);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
255 markobject(g, gco2u(o)->env);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
256 size = sizeudata(gco2u(o));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
257 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
258 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
259 case LUA_TUPVAL: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
260 UpVal *uv = gco2uv(o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
261 markvalue(g, uv->v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
262 if (uv->v != &uv->u.value) /* open? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
263 return; /* open upvalues remain gray */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
264 size = sizeof(UpVal);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
265 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
266 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
267 case LUA_TLCL: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
268 gco2lcl(o)->gclist = g->gray;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
269 g->gray = o;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
270 return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
271 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
272 case LUA_TCCL: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
273 gco2ccl(o)->gclist = g->gray;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
274 g->gray = o;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
275 return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
276 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
277 case LUA_TTABLE: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
278 linktable(gco2t(o), &g->gray);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
279 return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
280 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
281 case LUA_TTHREAD: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
282 gco2th(o)->gclist = g->gray;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
283 g->gray = o;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
284 return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
285 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
286 case LUA_TPROTO: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
287 gco2p(o)->gclist = g->gray;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
288 g->gray = o;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
289 return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
290 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
291 default: lua_assert(0); return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
292 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
293 gray2black(o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
294 g->GCmemtrav += size;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
295 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
296
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
297
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
298 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
299 ** mark metamethods for basic types
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
300 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
301 static void markmt (global_State *g) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
302 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
303 for (i=0; i < LUA_NUMTAGS; i++)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
304 markobject(g, g->mt[i]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
305 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
306
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
307
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
308 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
309 ** mark all objects in list of being-finalized
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
310 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
311 static void markbeingfnz (global_State *g) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
312 GCObject *o;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
313 for (o = g->tobefnz; o != NULL; o = gch(o)->next) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
314 makewhite(g, o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
315 reallymarkobject(g, o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
316 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
317 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
318
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
319
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
320 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
321 ** mark all values stored in marked open upvalues. (See comment in
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
322 ** 'lstate.h'.)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
323 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
324 static void remarkupvals (global_State *g) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
325 UpVal *uv;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
326 for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
327 if (isgray(obj2gco(uv)))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
328 markvalue(g, uv->v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
329 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
330 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
331
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
332
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
333 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
334 ** mark root set and reset all gray lists, to start a new
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
335 ** incremental (or full) collection
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
336 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
337 static void restartcollection (global_State *g) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
338 g->gray = g->grayagain = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
339 g->weak = g->allweak = g->ephemeron = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
340 markobject(g, g->mainthread);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
341 markvalue(g, &g->l_registry);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
342 markmt(g);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
343 markbeingfnz(g); /* mark any finalizing object left from previous cycle */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
344 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
345
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
346 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
347
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
348
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
349 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
350 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
351 ** Traverse functions
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
352 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
353 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
354
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
355 static void traverseweakvalue (global_State *g, Table *h) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
356 Node *n, *limit = gnodelast(h);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
357 /* if there is array part, assume it may have white values (do not
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
358 traverse it just to check) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
359 int hasclears = (h->sizearray > 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
360 for (n = gnode(h, 0); n < limit; n++) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
361 checkdeadkey(n);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
362 if (ttisnil(gval(n))) /* entry is empty? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
363 removeentry(n); /* remove it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
364 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
365 lua_assert(!ttisnil(gkey(n)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
366 markvalue(g, gkey(n)); /* mark key */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
367 if (!hasclears && iscleared(g, gval(n))) /* is there a white value? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
368 hasclears = 1; /* table will have to be cleared */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
369 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
370 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
371 if (hasclears)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
372 linktable(h, &g->weak); /* has to be cleared later */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
373 else /* no white values */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
374 linktable(h, &g->grayagain); /* no need to clean */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
375 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
376
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
377
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
378 static int traverseephemeron (global_State *g, Table *h) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
379 int marked = 0; /* true if an object is marked in this traversal */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
380 int hasclears = 0; /* true if table has white keys */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
381 int prop = 0; /* true if table has entry "white-key -> white-value" */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
382 Node *n, *limit = gnodelast(h);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
383 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
384 /* traverse array part (numeric keys are 'strong') */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
385 for (i = 0; i < h->sizearray; i++) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
386 if (valiswhite(&h->array[i])) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
387 marked = 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
388 reallymarkobject(g, gcvalue(&h->array[i]));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
389 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
390 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
391 /* traverse hash part */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
392 for (n = gnode(h, 0); n < limit; n++) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
393 checkdeadkey(n);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
394 if (ttisnil(gval(n))) /* entry is empty? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
395 removeentry(n); /* remove it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
396 else if (iscleared(g, gkey(n))) { /* key is not marked (yet)? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
397 hasclears = 1; /* table must be cleared */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
398 if (valiswhite(gval(n))) /* value not marked yet? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
399 prop = 1; /* must propagate again */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
400 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
401 else if (valiswhite(gval(n))) { /* value not marked yet? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
402 marked = 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
403 reallymarkobject(g, gcvalue(gval(n))); /* mark it now */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
404 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
405 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
406 if (prop)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
407 linktable(h, &g->ephemeron); /* have to propagate again */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
408 else if (hasclears) /* does table have white keys? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
409 linktable(h, &g->allweak); /* may have to clean white keys */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
410 else /* no white keys */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
411 linktable(h, &g->grayagain); /* no need to clean */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
412 return marked;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
413 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
414
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
415
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
416 static void traversestrongtable (global_State *g, Table *h) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
417 Node *n, *limit = gnodelast(h);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
418 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
419 for (i = 0; i < h->sizearray; i++) /* traverse array part */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
420 markvalue(g, &h->array[i]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
421 for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
422 checkdeadkey(n);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
423 if (ttisnil(gval(n))) /* entry is empty? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
424 removeentry(n); /* remove it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
425 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
426 lua_assert(!ttisnil(gkey(n)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
427 markvalue(g, gkey(n)); /* mark key */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
428 markvalue(g, gval(n)); /* mark value */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
429 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
430 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
431 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
432
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
433
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
434 static lu_mem traversetable (global_State *g, Table *h) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
435 const char *weakkey, *weakvalue;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
436 const TValue *mode = gfasttm(g, h->metatable, TM_MODE);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
437 markobject(g, h->metatable);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
438 if (mode && ttisstring(mode) && /* is there a weak mode? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
439 ((weakkey = strchr(svalue(mode), 'k')),
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
440 (weakvalue = strchr(svalue(mode), 'v')),
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
441 (weakkey || weakvalue))) { /* is really weak? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
442 black2gray(obj2gco(h)); /* keep table gray */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
443 if (!weakkey) /* strong keys? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
444 traverseweakvalue(g, h);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
445 else if (!weakvalue) /* strong values? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
446 traverseephemeron(g, h);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
447 else /* all weak */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
448 linktable(h, &g->allweak); /* nothing to traverse now */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
449 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
450 else /* not weak */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
451 traversestrongtable(g, h);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
452 return sizeof(Table) + sizeof(TValue) * h->sizearray +
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
453 sizeof(Node) * cast(size_t, sizenode(h));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
454 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
455
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
456
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
457 static int traverseproto (global_State *g, Proto *f) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
458 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
459 if (f->cache && iswhite(obj2gco(f->cache)))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
460 f->cache = NULL; /* allow cache to be collected */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
461 markobject(g, f->source);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
462 for (i = 0; i < f->sizek; i++) /* mark literals */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
463 markvalue(g, &f->k[i]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
464 for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
465 markobject(g, f->upvalues[i].name);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
466 for (i = 0; i < f->sizep; i++) /* mark nested protos */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
467 markobject(g, f->p[i]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
468 for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
469 markobject(g, f->locvars[i].varname);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
470 return sizeof(Proto) + sizeof(Instruction) * f->sizecode +
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
471 sizeof(Proto *) * f->sizep +
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
472 sizeof(TValue) * f->sizek +
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
473 sizeof(int) * f->sizelineinfo +
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
474 sizeof(LocVar) * f->sizelocvars +
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
475 sizeof(Upvaldesc) * f->sizeupvalues;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
476 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
477
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
478
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
479 static lu_mem traverseCclosure (global_State *g, CClosure *cl) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
480 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
481 for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
482 markvalue(g, &cl->upvalue[i]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
483 return sizeCclosure(cl->nupvalues);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
484 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
485
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
486 static lu_mem traverseLclosure (global_State *g, LClosure *cl) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
487 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
488 markobject(g, cl->p); /* mark its prototype */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
489 for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
490 markobject(g, cl->upvals[i]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
491 return sizeLclosure(cl->nupvalues);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
492 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
493
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
494
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
495 static lu_mem traversestack (global_State *g, lua_State *th) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
496 StkId o = th->stack;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
497 if (o == NULL)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
498 return 1; /* stack not completely built yet */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
499 for (; o < th->top; o++)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
500 markvalue(g, o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
501 if (g->gcstate == GCSatomic) { /* final traversal? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
502 StkId lim = th->stack + th->stacksize; /* real end of stack */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
503 for (; o < lim; o++) /* clear not-marked stack slice */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
504 setnilvalue(o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
505 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
506 return sizeof(lua_State) + sizeof(TValue) * th->stacksize;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
507 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
508
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
509
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
510 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
511 ** traverse one gray object, turning it to black (except for threads,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
512 ** which are always gray).
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
513 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
514 static void propagatemark (global_State *g) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
515 lu_mem size;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
516 GCObject *o = g->gray;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
517 lua_assert(isgray(o));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
518 gray2black(o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
519 switch (gch(o)->tt) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
520 case LUA_TTABLE: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
521 Table *h = gco2t(o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
522 g->gray = h->gclist; /* remove from 'gray' list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
523 size = traversetable(g, h);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
524 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
525 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
526 case LUA_TLCL: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
527 LClosure *cl = gco2lcl(o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
528 g->gray = cl->gclist; /* remove from 'gray' list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
529 size = traverseLclosure(g, cl);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
530 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
531 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
532 case LUA_TCCL: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
533 CClosure *cl = gco2ccl(o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
534 g->gray = cl->gclist; /* remove from 'gray' list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
535 size = traverseCclosure(g, cl);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
536 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
537 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
538 case LUA_TTHREAD: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
539 lua_State *th = gco2th(o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
540 g->gray = th->gclist; /* remove from 'gray' list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
541 th->gclist = g->grayagain;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
542 g->grayagain = o; /* insert into 'grayagain' list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
543 black2gray(o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
544 size = traversestack(g, th);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
545 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
546 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
547 case LUA_TPROTO: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
548 Proto *p = gco2p(o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
549 g->gray = p->gclist; /* remove from 'gray' list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
550 size = traverseproto(g, p);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
551 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
552 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
553 default: lua_assert(0); return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
554 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
555 g->GCmemtrav += size;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
556 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
557
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
558
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
559 static void propagateall (global_State *g) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
560 while (g->gray) propagatemark(g);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
561 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
562
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
563
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
564 static void propagatelist (global_State *g, GCObject *l) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
565 lua_assert(g->gray == NULL); /* no grays left */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
566 g->gray = l;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
567 propagateall(g); /* traverse all elements from 'l' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
568 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
569
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
570 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
571 ** retraverse all gray lists. Because tables may be reinserted in other
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
572 ** lists when traversed, traverse the original lists to avoid traversing
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
573 ** twice the same table (which is not wrong, but inefficient)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
574 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
575 static void retraversegrays (global_State *g) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
576 GCObject *weak = g->weak; /* save original lists */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
577 GCObject *grayagain = g->grayagain;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
578 GCObject *ephemeron = g->ephemeron;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
579 g->weak = g->grayagain = g->ephemeron = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
580 propagateall(g); /* traverse main gray list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
581 propagatelist(g, grayagain);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
582 propagatelist(g, weak);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
583 propagatelist(g, ephemeron);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
584 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
585
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
586
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
587 static void convergeephemerons (global_State *g) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
588 int changed;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
589 do {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
590 GCObject *w;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
591 GCObject *next = g->ephemeron; /* get ephemeron list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
592 g->ephemeron = NULL; /* tables will return to this list when traversed */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
593 changed = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
594 while ((w = next) != NULL) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
595 next = gco2t(w)->gclist;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
596 if (traverseephemeron(g, gco2t(w))) { /* traverse marked some value? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
597 propagateall(g); /* propagate changes */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
598 changed = 1; /* will have to revisit all ephemeron tables */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
599 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
600 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
601 } while (changed);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
602 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
603
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
604 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
605
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
606
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
607 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
608 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
609 ** Sweep Functions
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
610 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
611 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
612
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
613
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
614 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
615 ** clear entries with unmarked keys from all weaktables in list 'l' up
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
616 ** to element 'f'
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
617 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
618 static void clearkeys (global_State *g, GCObject *l, GCObject *f) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
619 for (; l != f; l = gco2t(l)->gclist) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
620 Table *h = gco2t(l);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
621 Node *n, *limit = gnodelast(h);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
622 for (n = gnode(h, 0); n < limit; n++) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
623 if (!ttisnil(gval(n)) && (iscleared(g, gkey(n)))) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
624 setnilvalue(gval(n)); /* remove value ... */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
625 removeentry(n); /* and remove entry from table */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
626 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
627 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
628 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
629 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
630
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
631
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
632 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
633 ** clear entries with unmarked values from all weaktables in list 'l' up
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
634 ** to element 'f'
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
635 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
636 static void clearvalues (global_State *g, GCObject *l, GCObject *f) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
637 for (; l != f; l = gco2t(l)->gclist) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
638 Table *h = gco2t(l);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
639 Node *n, *limit = gnodelast(h);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
640 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
641 for (i = 0; i < h->sizearray; i++) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
642 TValue *o = &h->array[i];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
643 if (iscleared(g, o)) /* value was collected? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
644 setnilvalue(o); /* remove value */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
645 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
646 for (n = gnode(h, 0); n < limit; n++) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
647 if (!ttisnil(gval(n)) && iscleared(g, gval(n))) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
648 setnilvalue(gval(n)); /* remove value ... */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
649 removeentry(n); /* and remove entry from table */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
650 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
651 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
652 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
653 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
654
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
655
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
656 static void freeobj (lua_State *L, GCObject *o) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
657 switch (gch(o)->tt) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
658 case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
659 case LUA_TLCL: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
660 luaM_freemem(L, o, sizeLclosure(gco2lcl(o)->nupvalues));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
661 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
662 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
663 case LUA_TCCL: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
664 luaM_freemem(L, o, sizeCclosure(gco2ccl(o)->nupvalues));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
665 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
666 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
667 case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
668 case LUA_TTABLE: luaH_free(L, gco2t(o)); break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
669 case LUA_TTHREAD: luaE_freethread(L, gco2th(o)); break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
670 case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
671 case LUA_TSHRSTR:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
672 G(L)->strt.nuse--;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
673 /* go through */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
674 case LUA_TLNGSTR: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
675 luaM_freemem(L, o, sizestring(gco2ts(o)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
676 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
677 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
678 default: lua_assert(0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
679 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
680 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
681
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
682
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
683 #define sweepwholelist(L,p) sweeplist(L,p,MAX_LUMEM)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
684 static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
685
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
686
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
687 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
688 ** sweep the (open) upvalues of a thread and resize its stack and
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
689 ** list of call-info structures.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
690 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
691 static void sweepthread (lua_State *L, lua_State *L1) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
692 if (L1->stack == NULL) return; /* stack not completely built yet */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
693 sweepwholelist(L, &L1->openupval); /* sweep open upvalues */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
694 luaE_freeCI(L1); /* free extra CallInfo slots */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
695 /* should not change the stack during an emergency gc cycle */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
696 if (G(L)->gckind != KGC_EMERGENCY)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
697 luaD_shrinkstack(L1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
698 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
699
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
700
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
701 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
702 ** sweep at most 'count' elements from a list of GCObjects erasing dead
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
703 ** objects, where a dead (not alive) object is one marked with the "old"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
704 ** (non current) white and not fixed.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
705 ** In non-generational mode, change all non-dead objects back to white,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
706 ** preparing for next collection cycle.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
707 ** In generational mode, keep black objects black, and also mark them as
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
708 ** old; stop when hitting an old object, as all objects after that
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
709 ** one will be old too.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
710 ** When object is a thread, sweep its list of open upvalues too.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
711 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
712 static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
713 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
714 int ow = otherwhite(g);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
715 int toclear, toset; /* bits to clear and to set in all live objects */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
716 int tostop; /* stop sweep when this is true */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
717 if (isgenerational(g)) { /* generational mode? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
718 toclear = ~0; /* clear nothing */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
719 toset = bitmask(OLDBIT); /* set the old bit of all surviving objects */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
720 tostop = bitmask(OLDBIT); /* do not sweep old generation */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
721 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
722 else { /* normal mode */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
723 toclear = maskcolors; /* clear all color bits + old bit */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
724 toset = luaC_white(g); /* make object white */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
725 tostop = 0; /* do not stop */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
726 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
727 while (*p != NULL && count-- > 0) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
728 GCObject *curr = *p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
729 int marked = gch(curr)->marked;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
730 if (isdeadm(ow, marked)) { /* is 'curr' dead? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
731 *p = gch(curr)->next; /* remove 'curr' from list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
732 freeobj(L, curr); /* erase 'curr' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
733 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
734 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
735 if (testbits(marked, tostop))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
736 return NULL; /* stop sweeping this list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
737 if (gch(curr)->tt == LUA_TTHREAD)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
738 sweepthread(L, gco2th(curr)); /* sweep thread's upvalues */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
739 /* update marks */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
740 gch(curr)->marked = cast_byte((marked & toclear) | toset);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
741 p = &gch(curr)->next; /* go to next element */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
742 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
743 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
744 return (*p == NULL) ? NULL : p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
745 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
746
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
747
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
748 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
749 ** sweep a list until a live object (or end of list)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
750 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
751 static GCObject **sweeptolive (lua_State *L, GCObject **p, int *n) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
752 GCObject ** old = p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
753 int i = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
754 do {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
755 i++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
756 p = sweeplist(L, p, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
757 } while (p == old);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
758 if (n) *n += i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
759 return p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
760 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
761
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
762 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
763
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
764
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
765 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
766 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
767 ** Finalization
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
768 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
769 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
770
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
771 static void checkSizes (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
772 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
773 if (g->gckind != KGC_EMERGENCY) { /* do not change sizes in emergency */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
774 int hs = g->strt.size / 2; /* half the size of the string table */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
775 if (g->strt.nuse < cast(lu_int32, hs)) /* using less than that half? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
776 luaS_resize(L, hs); /* halve its size */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
777 luaZ_freebuffer(L, &g->buff); /* free concatenation buffer */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
778 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
779 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
780
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
781
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
782 static GCObject *udata2finalize (global_State *g) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
783 GCObject *o = g->tobefnz; /* get first element */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
784 lua_assert(isfinalized(o));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
785 g->tobefnz = gch(o)->next; /* remove it from 'tobefnz' list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
786 gch(o)->next = g->allgc; /* return it to 'allgc' list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
787 g->allgc = o;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
788 resetbit(gch(o)->marked, SEPARATED); /* mark that it is not in 'tobefnz' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
789 lua_assert(!isold(o)); /* see MOVE OLD rule */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
790 if (!keepinvariantout(g)) /* not keeping invariant? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
791 makewhite(g, o); /* "sweep" object */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
792 return o;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
793 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
794
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
795
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
796 static void dothecall (lua_State *L, void *ud) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
797 UNUSED(ud);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
798 luaD_call(L, L->top - 2, 0, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
799 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
800
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
801
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
802 static void GCTM (lua_State *L, int propagateerrors) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
803 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
804 const TValue *tm;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
805 TValue v;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
806 setgcovalue(L, &v, udata2finalize(g));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
807 tm = luaT_gettmbyobj(L, &v, TM_GC);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
808 if (tm != NULL && ttisfunction(tm)) { /* is there a finalizer? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
809 int status;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
810 lu_byte oldah = L->allowhook;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
811 int running = g->gcrunning;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
812 L->allowhook = 0; /* stop debug hooks during GC metamethod */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
813 g->gcrunning = 0; /* avoid GC steps */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
814 setobj2s(L, L->top, tm); /* push finalizer... */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
815 setobj2s(L, L->top + 1, &v); /* ... and its argument */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
816 L->top += 2; /* and (next line) call the finalizer */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
817 status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
818 L->allowhook = oldah; /* restore hooks */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
819 g->gcrunning = running; /* restore state */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
820 if (status != LUA_OK && propagateerrors) { /* error while running __gc? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
821 if (status == LUA_ERRRUN) { /* is there an error object? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
822 const char *msg = (ttisstring(L->top - 1))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
823 ? svalue(L->top - 1)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
824 : "no message";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
825 luaO_pushfstring(L, "error in __gc metamethod (%s)", msg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
826 status = LUA_ERRGCMM; /* error in __gc metamethod */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
827 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
828 luaD_throw(L, status); /* re-throw error */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
829 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
830 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
831 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
832
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
833
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
834 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
835 ** move all unreachable objects (or 'all' objects) that need
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
836 ** finalization from list 'finobj' to list 'tobefnz' (to be finalized)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
837 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
838 static void separatetobefnz (lua_State *L, int all) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
839 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
840 GCObject **p = &g->finobj;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
841 GCObject *curr;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
842 GCObject **lastnext = &g->tobefnz;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
843 /* find last 'next' field in 'tobefnz' list (to add elements in its end) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
844 while (*lastnext != NULL)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
845 lastnext = &gch(*lastnext)->next;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
846 while ((curr = *p) != NULL) { /* traverse all finalizable objects */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
847 lua_assert(!isfinalized(curr));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
848 lua_assert(testbit(gch(curr)->marked, SEPARATED));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
849 if (!(iswhite(curr) || all)) /* not being collected? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
850 p = &gch(curr)->next; /* don't bother with it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
851 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
852 l_setbit(gch(curr)->marked, FINALIZEDBIT); /* won't be finalized again */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
853 *p = gch(curr)->next; /* remove 'curr' from 'finobj' list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
854 gch(curr)->next = *lastnext; /* link at the end of 'tobefnz' list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
855 *lastnext = curr;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
856 lastnext = &gch(curr)->next;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
857 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
858 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
859 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
860
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
861
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
862 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
863 ** if object 'o' has a finalizer, remove it from 'allgc' list (must
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
864 ** search the list to find it) and link it in 'finobj' list.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
865 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
866 void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
867 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
868 if (testbit(gch(o)->marked, SEPARATED) || /* obj. is already separated... */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
869 isfinalized(o) || /* ... or is finalized... */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
870 gfasttm(g, mt, TM_GC) == NULL) /* or has no finalizer? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
871 return; /* nothing to be done */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
872 else { /* move 'o' to 'finobj' list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
873 GCObject **p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
874 GCheader *ho = gch(o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
875 if (g->sweepgc == &ho->next) { /* avoid removing current sweep object */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
876 lua_assert(issweepphase(g));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
877 g->sweepgc = sweeptolive(L, g->sweepgc, NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
878 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
879 /* search for pointer pointing to 'o' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
880 for (p = &g->allgc; *p != o; p = &gch(*p)->next) { /* empty */ }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
881 *p = ho->next; /* remove 'o' from root list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
882 ho->next = g->finobj; /* link it in list 'finobj' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
883 g->finobj = o;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
884 l_setbit(ho->marked, SEPARATED); /* mark it as such */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
885 if (!keepinvariantout(g)) /* not keeping invariant? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
886 makewhite(g, o); /* "sweep" object */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
887 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
888 resetoldbit(o); /* see MOVE OLD rule */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
889 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
890 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
891
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
892 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
893
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
894
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
895 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
896 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
897 ** GC control
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
898 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
899 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
900
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
901
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
902 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
903 ** set a reasonable "time" to wait before starting a new GC cycle;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
904 ** cycle will start when memory use hits threshold
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
905 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
906 static void setpause (global_State *g, l_mem estimate) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
907 l_mem debt, threshold;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
908 estimate = estimate / PAUSEADJ; /* adjust 'estimate' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
909 threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
910 ? estimate * g->gcpause /* no overflow */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
911 : MAX_LMEM; /* overflow; truncate to maximum */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
912 debt = -cast(l_mem, threshold - gettotalbytes(g));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
913 luaE_setdebt(g, debt);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
914 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
915
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
916
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
917 #define sweepphases \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
918 (bitmask(GCSsweepstring) | bitmask(GCSsweepudata) | bitmask(GCSsweep))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
919
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
920
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
921 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
922 ** enter first sweep phase (strings) and prepare pointers for other
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
923 ** sweep phases. The calls to 'sweeptolive' make pointers point to an
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
924 ** object inside the list (instead of to the header), so that the real
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
925 ** sweep do not need to skip objects created between "now" and the start
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
926 ** of the real sweep.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
927 ** Returns how many objects it swept.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
928 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
929 static int entersweep (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
930 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
931 int n = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
932 g->gcstate = GCSsweepstring;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
933 lua_assert(g->sweepgc == NULL && g->sweepfin == NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
934 /* prepare to sweep strings, finalizable objects, and regular objects */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
935 g->sweepstrgc = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
936 g->sweepfin = sweeptolive(L, &g->finobj, &n);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
937 g->sweepgc = sweeptolive(L, &g->allgc, &n);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
938 return n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
939 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
940
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
941
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
942 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
943 ** change GC mode
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
944 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
945 void luaC_changemode (lua_State *L, int mode) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
946 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
947 if (mode == g->gckind) return; /* nothing to change */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
948 if (mode == KGC_GEN) { /* change to generational mode */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
949 /* make sure gray lists are consistent */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
950 luaC_runtilstate(L, bitmask(GCSpropagate));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
951 g->GCestimate = gettotalbytes(g);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
952 g->gckind = KGC_GEN;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
953 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
954 else { /* change to incremental mode */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
955 /* sweep all objects to turn them back to white
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
956 (as white has not changed, nothing extra will be collected) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
957 g->gckind = KGC_NORMAL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
958 entersweep(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
959 luaC_runtilstate(L, ~sweepphases);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
960 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
961 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
962
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
963
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
964 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
965 ** call all pending finalizers
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
966 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
967 static void callallpendingfinalizers (lua_State *L, int propagateerrors) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
968 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
969 while (g->tobefnz) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
970 resetoldbit(g->tobefnz);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
971 GCTM(L, propagateerrors);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
972 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
973 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
974
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
975
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
976 void luaC_freeallobjects (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
977 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
978 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
979 separatetobefnz(L, 1); /* separate all objects with finalizers */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
980 lua_assert(g->finobj == NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
981 callallpendingfinalizers(L, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
982 g->currentwhite = WHITEBITS; /* this "white" makes all objects look dead */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
983 g->gckind = KGC_NORMAL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
984 sweepwholelist(L, &g->finobj); /* finalizers can create objs. in 'finobj' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
985 sweepwholelist(L, &g->allgc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
986 for (i = 0; i < g->strt.size; i++) /* free all string lists */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
987 sweepwholelist(L, &g->strt.hash[i]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
988 lua_assert(g->strt.nuse == 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
989 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
990
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
991
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
992 static l_mem atomic (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
993 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
994 l_mem work = -cast(l_mem, g->GCmemtrav); /* start counting work */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
995 GCObject *origweak, *origall;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
996 lua_assert(!iswhite(obj2gco(g->mainthread)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
997 markobject(g, L); /* mark running thread */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
998 /* registry and global metatables may be changed by API */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
999 markvalue(g, &g->l_registry);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1000 markmt(g); /* mark basic metatables */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1001 /* remark occasional upvalues of (maybe) dead threads */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1002 remarkupvals(g);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1003 propagateall(g); /* propagate changes */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1004 work += g->GCmemtrav; /* stop counting (do not (re)count grays) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1005 /* traverse objects caught by write barrier and by 'remarkupvals' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1006 retraversegrays(g);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1007 work -= g->GCmemtrav; /* restart counting */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1008 convergeephemerons(g);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1009 /* at this point, all strongly accessible objects are marked. */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1010 /* clear values from weak tables, before checking finalizers */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1011 clearvalues(g, g->weak, NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1012 clearvalues(g, g->allweak, NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1013 origweak = g->weak; origall = g->allweak;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1014 work += g->GCmemtrav; /* stop counting (objects being finalized) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1015 separatetobefnz(L, 0); /* separate objects to be finalized */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1016 markbeingfnz(g); /* mark objects that will be finalized */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1017 propagateall(g); /* remark, to propagate `preserveness' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1018 work -= g->GCmemtrav; /* restart counting */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1019 convergeephemerons(g);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1020 /* at this point, all resurrected objects are marked. */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1021 /* remove dead objects from weak tables */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1022 clearkeys(g, g->ephemeron, NULL); /* clear keys from all ephemeron tables */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1023 clearkeys(g, g->allweak, NULL); /* clear keys from all allweak tables */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1024 /* clear values from resurrected weak tables */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1025 clearvalues(g, g->weak, origweak);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1026 clearvalues(g, g->allweak, origall);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1027 g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1028 work += g->GCmemtrav; /* complete counting */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1029 return work; /* estimate of memory marked by 'atomic' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1030 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1031
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1032
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1033 static lu_mem singlestep (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1034 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1035 switch (g->gcstate) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1036 case GCSpause: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1037 /* start to count memory traversed */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1038 g->GCmemtrav = g->strt.size * sizeof(GCObject*);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1039 lua_assert(!isgenerational(g));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1040 restartcollection(g);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1041 g->gcstate = GCSpropagate;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1042 return g->GCmemtrav;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1043 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1044 case GCSpropagate: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1045 if (g->gray) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1046 lu_mem oldtrav = g->GCmemtrav;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1047 propagatemark(g);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1048 return g->GCmemtrav - oldtrav; /* memory traversed in this step */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1049 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1050 else { /* no more `gray' objects */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1051 lu_mem work;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1052 int sw;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1053 g->gcstate = GCSatomic; /* finish mark phase */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1054 g->GCestimate = g->GCmemtrav; /* save what was counted */;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1055 work = atomic(L); /* add what was traversed by 'atomic' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1056 g->GCestimate += work; /* estimate of total memory traversed */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1057 sw = entersweep(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1058 return work + sw * GCSWEEPCOST;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1059 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1060 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1061 case GCSsweepstring: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1062 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1063 for (i = 0; i < GCSWEEPMAX && g->sweepstrgc + i < g->strt.size; i++)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1064 sweepwholelist(L, &g->strt.hash[g->sweepstrgc + i]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1065 g->sweepstrgc += i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1066 if (g->sweepstrgc >= g->strt.size) /* no more strings to sweep? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1067 g->gcstate = GCSsweepudata;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1068 return i * GCSWEEPCOST;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1069 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1070 case GCSsweepudata: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1071 if (g->sweepfin) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1072 g->sweepfin = sweeplist(L, g->sweepfin, GCSWEEPMAX);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1073 return GCSWEEPMAX*GCSWEEPCOST;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1074 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1075 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1076 g->gcstate = GCSsweep;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1077 return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1078 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1079 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1080 case GCSsweep: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1081 if (g->sweepgc) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1082 g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1083 return GCSWEEPMAX*GCSWEEPCOST;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1084 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1085 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1086 /* sweep main thread */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1087 GCObject *mt = obj2gco(g->mainthread);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1088 sweeplist(L, &mt, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1089 checkSizes(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1090 g->gcstate = GCSpause; /* finish collection */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1091 return GCSWEEPCOST;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1092 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1093 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1094 default: lua_assert(0); return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1095 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1096 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1097
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1098
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1099 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1100 ** advances the garbage collector until it reaches a state allowed
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1101 ** by 'statemask'
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1102 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1103 void luaC_runtilstate (lua_State *L, int statesmask) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1104 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1105 while (!testbit(statesmask, g->gcstate))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1106 singlestep(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1107 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1108
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1109
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1110 static void generationalcollection (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1111 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1112 lua_assert(g->gcstate == GCSpropagate);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1113 if (g->GCestimate == 0) { /* signal for another major collection? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1114 luaC_fullgc(L, 0); /* perform a full regular collection */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1115 g->GCestimate = gettotalbytes(g); /* update control */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1116 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1117 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1118 lu_mem estimate = g->GCestimate;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1119 luaC_runtilstate(L, bitmask(GCSpause)); /* run complete (minor) cycle */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1120 g->gcstate = GCSpropagate; /* skip restart */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1121 if (gettotalbytes(g) > (estimate / 100) * g->gcmajorinc)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1122 g->GCestimate = 0; /* signal for a major collection */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1123 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1124 g->GCestimate = estimate; /* keep estimate from last major coll. */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1125
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1126 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1127 setpause(g, gettotalbytes(g));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1128 lua_assert(g->gcstate == GCSpropagate);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1129 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1130
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1131
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1132 static void incstep (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1133 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1134 l_mem debt = g->GCdebt;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1135 int stepmul = g->gcstepmul;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1136 if (stepmul < 40) stepmul = 40; /* avoid ridiculous low values (and 0) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1137 /* convert debt from Kb to 'work units' (avoid zero debt and overflows) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1138 debt = (debt / STEPMULADJ) + 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1139 debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1140 do { /* always perform at least one single step */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1141 lu_mem work = singlestep(L); /* do some work */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1142 debt -= work;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1143 } while (debt > -GCSTEPSIZE && g->gcstate != GCSpause);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1144 if (g->gcstate == GCSpause)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1145 setpause(g, g->GCestimate); /* pause until next cycle */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1146 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1147 debt = (debt / stepmul) * STEPMULADJ; /* convert 'work units' to Kb */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1148 luaE_setdebt(g, debt);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1149 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1150 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1151
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1152
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1153 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1154 ** performs a basic GC step
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1155 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1156 void luaC_forcestep (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1157 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1158 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1159 if (isgenerational(g)) generationalcollection(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1160 else incstep(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1161 /* run a few finalizers (or all of them at the end of a collect cycle) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1162 for (i = 0; g->tobefnz && (i < GCFINALIZENUM || g->gcstate == GCSpause); i++)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1163 GCTM(L, 1); /* call one finalizer */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1164 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1165
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1166
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1167 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1168 ** performs a basic GC step only if collector is running
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1169 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1170 void luaC_step (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1171 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1172 if (g->gcrunning) luaC_forcestep(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1173 else luaE_setdebt(g, -GCSTEPSIZE); /* avoid being called too often */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1174 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1175
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1176
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1177
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1178 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1179 ** performs a full GC cycle; if "isemergency", does not call
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1180 ** finalizers (which could change stack positions)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1181 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1182 void luaC_fullgc (lua_State *L, int isemergency) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1183 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1184 int origkind = g->gckind;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1185 lua_assert(origkind != KGC_EMERGENCY);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1186 if (isemergency) /* do not run finalizers during emergency GC */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1187 g->gckind = KGC_EMERGENCY;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1188 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1189 g->gckind = KGC_NORMAL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1190 callallpendingfinalizers(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1191 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1192 if (keepinvariant(g)) { /* may there be some black objects? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1193 /* must sweep all objects to turn them back to white
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1194 (as white has not changed, nothing will be collected) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1195 entersweep(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1196 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1197 /* finish any pending sweep phase to start a new cycle */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1198 luaC_runtilstate(L, bitmask(GCSpause));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1199 luaC_runtilstate(L, ~bitmask(GCSpause)); /* start new collection */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1200 luaC_runtilstate(L, bitmask(GCSpause)); /* run entire collection */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1201 if (origkind == KGC_GEN) { /* generational mode? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1202 /* generational mode must be kept in propagate phase */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1203 luaC_runtilstate(L, bitmask(GCSpropagate));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1204 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1205 g->gckind = origkind;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1206 setpause(g, gettotalbytes(g));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1207 if (!isemergency) /* do not run finalizers during emergency GC */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1208 callallpendingfinalizers(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1209 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1210
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1211 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1212
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1213