sdk-world v2026.11.05
PS2 SDK - world tier (copyleft-allowed, non-essential packages)
Loading...
Searching...
No Matches
lgc.h
Go to the documentation of this file.
1/*
2** $Id: lgc.h $
3** Garbage Collector
4** See Copyright Notice in lua.h
5*/
6
11#ifndef lgc_h
12#define lgc_h
13
14
15#include "lobject.h"
16#include "lstate.h"
17
18/*
19** Collectable objects may have one of three colors: white, which means
20** the object is not marked; gray, which means the object is marked, but
21** its references may be not marked; and black, which means that the
22** object and all its references are marked. The main invariant of the
23** garbage collector, while marking objects, is that a black object can
24** never point to a white one. Moreover, any gray object must be in a
25** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it
26** can be visited again before finishing the collection cycle. (Open
27** upvalues are an exception to this rule.) These lists have no meaning
28** when the invariant is not being enforced (e.g., sweep phase).
29*/
30
31
32/*
33** Possible states of the Garbage Collector
34*/
35#define GCSpropagate 0
36#define GCSenteratomic 1
37#define GCSatomic 2
38#define GCSswpallgc 3
39#define GCSswpfinobj 4
40#define GCSswptobefnz 5
41#define GCSswpend 6
42#define GCScallfin 7
43#define GCSpause 8
44
45
46#define issweepphase(g) \
47 (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend)
48
49
50/*
51** macro to tell when main invariant (white objects cannot point to black
52** ones) must be kept. During a collection, the sweep
53** phase may break the invariant, as objects turned white may point to
54** still-black objects. The invariant is restored when sweep ends and
55** all objects are white again.
56*/
57
58#define keepinvariant(g) ((g)->gcstate <= GCSatomic)
59
60
61/*
62** some useful bit tricks
63*/
64#define resetbits(x,m) ((x) &= cast_byte(~(m)))
65#define setbits(x,m) ((x) |= (m))
66#define testbits(x,m) ((x) & (m))
67#define bitmask(b) (1<<(b))
68#define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
69#define l_setbit(x,b) setbits(x, bitmask(b))
70#define resetbit(x,b) resetbits(x, bitmask(b))
71#define testbit(x,b) testbits(x, bitmask(b))
72
73
74/*
75** Layout for bit use in 'marked' field. First three bits are
76** used for object "age" in generational mode. Last bit is used
77** by tests.
78*/
79#define WHITE0BIT 3 /* object is white (type 0) */
80#define WHITE1BIT 4 /* object is white (type 1) */
81#define BLACKBIT 5 /* object is black */
82#define FINALIZEDBIT 6 /* object has been marked for finalization */
83
84#define TESTBIT 7
85
86
87
88#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
89
90
91#define iswhite(x) testbits((x)->marked, WHITEBITS)
92#define isblack(x) testbit((x)->marked, BLACKBIT)
93#define isgray(x) /* neither white nor black */ \
94 (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT)))
95
96#define tofinalize(x) testbit((x)->marked, FINALIZEDBIT)
97
98#define otherwhite(g) ((g)->currentwhite ^ WHITEBITS)
99#define isdeadm(ow,m) ((m) & (ow))
100#define isdead(g,v) isdeadm(otherwhite(g), (v)->marked)
101
102#define changewhite(x) ((x)->marked ^= WHITEBITS)
103#define nw2black(x) \
104 check_exp(!iswhite(x), l_setbit((x)->marked, BLACKBIT))
105
106#define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS)
107
108
109/* object age in generational mode */
110#define G_NEW 0 /* created in current cycle */
111#define G_SURVIVAL 1 /* created in previous cycle */
112#define G_OLD0 2 /* marked old by frw. barrier in this cycle */
113#define G_OLD1 3 /* first full cycle as old */
114#define G_OLD 4 /* really old object (not to be visited) */
115#define G_TOUCHED1 5 /* old object touched this cycle */
116#define G_TOUCHED2 6 /* old object touched in previous cycle */
117
118#define AGEBITS 7 /* all age bits (111) */
119
120#define getage(o) ((o)->marked & AGEBITS)
121#define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a))
122#define isold(o) (getage(o) > G_SURVIVAL)
123
124#define changeage(o,f,t) \
125 check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t)))
126
127
128/* Default Values for GC parameters */
129#define LUAI_GENMAJORMUL 100
130#define LUAI_GENMINORMUL 20
131
132/* wait memory to double before starting new cycle */
133#define LUAI_GCPAUSE 200
134
135/*
136** some gc parameters are stored divided by 4 to allow a maximum value
137** up to 1023 in a 'lu_byte'.
138*/
139#define getgcparam(p) ((p) * 4)
140#define setgcparam(p,v) ((p) = (v) / 4)
141
142#define LUAI_GCMUL 100
143
144/* how much to allocate before next GC step (log2) */
145#define LUAI_GCSTEPSIZE 13 /* 8 KB */
146
147
148/*
149** Check whether the declared GC mode is generational. While in
150** generational mode, the collector can go temporarily to incremental
151** mode to improve performance. This is signaled by 'g->lastatomic != 0'.
152*/
153#define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0)
154
155
156/*
157** Control when GC is running:
158*/
159#define GCSTPUSR 1 /* bit true when GC stopped by user */
160#define GCSTPGC 2 /* bit true when GC stopped by itself */
161#define GCSTPCLS 4 /* bit true when closing Lua state */
162#define gcrunning(g) ((g)->gcstp == 0)
163
164
165/*
166** Does one step of collection when debt becomes positive. 'pre'/'pos'
167** allows some adjustments to be done only when needed. macro
168** 'condchangemem' is used only for heavy tests (forcing a full
169** GC cycle on every opportunity)
170*/
171#define luaC_condGC(L,pre,pos) \
172 { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \
173 condchangemem(L,pre,pos); }
174
175/* more often than not, 'pre'/'pos' are empty */
176#define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0)
177
178
179#define luaC_objbarrier(L,p,o) ( \
180 (isblack(p) && iswhite(o)) ? \
181 luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
182
183#define luaC_barrier(L,p,v) ( \
184 iscollectable(v) ? luaC_objbarrier(L,p,gcvalue(v)) : cast_void(0))
185
186#define luaC_objbarrierback(L,p,o) ( \
187 (isblack(p) && iswhite(o)) ? luaC_barrierback_(L,p) : cast_void(0))
188
189#define luaC_barrierback(L,p,v) ( \
190 iscollectable(v) ? luaC_objbarrierback(L, p, gcvalue(v)) : cast_void(0))
191
192LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
193LUAI_FUNC void luaC_freeallobjects (lua_State *L);
194LUAI_FUNC void luaC_step (lua_State *L);
195LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
196LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
197LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
198LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz,
199 size_t offset);
200LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
201LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
202LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
203LUAI_FUNC void luaC_changemode (lua_State *L, int newmode);
204
205
206#endif