1997-09-16 12:25:59 -07:00
|
|
|
/*
|
2018-08-23 10:26:12 -07:00
|
|
|
** $Id: lgc.h $
|
1997-09-16 12:25:59 -07:00
|
|
|
** Garbage Collector
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lgc_h
|
|
|
|
#define lgc_h
|
|
|
|
|
|
|
|
|
|
|
|
#include "lobject.h"
|
2010-04-29 14:43:36 -07:00
|
|
|
#include "lstate.h"
|
1997-09-16 12:25:59 -07:00
|
|
|
|
2010-04-30 11:36:45 -07:00
|
|
|
/*
|
2020-08-13 11:23:21 -07:00
|
|
|
** Collectable objects may have one of three colors: white, which means
|
|
|
|
** the object is not marked; gray, which means the object is marked, but
|
|
|
|
** its references may be not marked; and black, which means that the
|
|
|
|
** object and all its references are marked. The main invariant of the
|
|
|
|
** garbage collector, while marking objects, is that a black object can
|
|
|
|
** never point to a white one. Moreover, any gray object must be in a
|
|
|
|
** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it
|
|
|
|
** can be visited again before finishing the collection cycle. (Open
|
|
|
|
** upvalues are an exception to this rule.) These lists have no meaning
|
|
|
|
** when the invariant is not being enforced (e.g., sweep phase).
|
2010-04-30 11:36:45 -07:00
|
|
|
*/
|
|
|
|
|
1997-09-16 12:25:59 -07:00
|
|
|
|
2003-12-01 08:33:30 -08:00
|
|
|
/*
|
|
|
|
** Possible states of the Garbage Collector
|
|
|
|
*/
|
2010-05-03 04:24:30 -07:00
|
|
|
#define GCSpropagate 0
|
2017-04-05 09:50:51 -07:00
|
|
|
#define GCSenteratomic 1
|
|
|
|
#define GCSatomic 2
|
|
|
|
#define GCSswpallgc 3
|
|
|
|
#define GCSswpfinobj 4
|
|
|
|
#define GCSswptobefnz 5
|
|
|
|
#define GCSswpend 6
|
|
|
|
#define GCScallfin 7
|
|
|
|
#define GCSpause 8
|
2008-02-19 10:55:09 -08:00
|
|
|
|
|
|
|
|
2010-04-29 10:32:40 -07:00
|
|
|
#define issweepphase(g) \
|
2014-02-13 04:11:34 -08:00
|
|
|
(GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend)
|
2010-04-29 10:32:40 -07:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2013-08-05 09:58:28 -07:00
|
|
|
** macro to tell when main invariant (white objects cannot point to black
|
|
|
|
** ones) must be kept. During a collection, the sweep
|
2010-06-30 07:11:17 -07:00
|
|
|
** phase may break the invariant, as objects turned white may point to
|
2010-04-29 10:32:40 -07:00
|
|
|
** still-black objects. The invariant is restored when sweep ends and
|
2013-08-05 09:58:28 -07:00
|
|
|
** all objects are white again.
|
2010-04-29 10:32:40 -07:00
|
|
|
*/
|
2012-09-11 05:53:08 -07:00
|
|
|
|
2013-08-27 11:53:35 -07:00
|
|
|
#define keepinvariant(g) ((g)->gcstate <= GCSatomic)
|
2010-04-29 10:32:40 -07:00
|
|
|
|
|
|
|
|
2003-11-17 11:50:05 -08:00
|
|
|
/*
|
2009-11-26 03:39:20 -08:00
|
|
|
** some useful bit tricks
|
2003-12-03 12:03:07 -08:00
|
|
|
*/
|
2018-01-28 07:13:26 -08:00
|
|
|
#define resetbits(x,m) ((x) &= cast_byte(~(m)))
|
2008-06-26 12:42:45 -07:00
|
|
|
#define setbits(x,m) ((x) |= (m))
|
|
|
|
#define testbits(x,m) ((x) & (m))
|
|
|
|
#define bitmask(b) (1<<(b))
|
|
|
|
#define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
|
|
|
|
#define l_setbit(x,b) setbits(x, bitmask(b))
|
|
|
|
#define resetbit(x,b) resetbits(x, bitmask(b))
|
|
|
|
#define testbit(x,b) testbits(x, bitmask(b))
|
2003-11-17 11:50:05 -08:00
|
|
|
|
|
|
|
|
2017-04-05 09:50:51 -07:00
|
|
|
/*
|
|
|
|
** Layout for bit use in 'marked' field. First three bits are
|
2020-08-07 10:45:20 -07:00
|
|
|
** used for object "age" in generational mode. Last bit is used
|
|
|
|
** by tests.
|
2017-04-05 09:50:51 -07:00
|
|
|
*/
|
|
|
|
#define WHITE0BIT 3 /* object is white (type 0) */
|
|
|
|
#define WHITE1BIT 4 /* object is white (type 1) */
|
|
|
|
#define BLACKBIT 5 /* object is black */
|
|
|
|
#define FINALIZEDBIT 6 /* object has been marked for finalization */
|
2018-06-11 07:19:50 -07:00
|
|
|
|
2020-08-07 10:45:20 -07:00
|
|
|
#define TESTBIT 7
|
|
|
|
|
2010-03-24 06:07:01 -07:00
|
|
|
|
2017-04-05 09:50:51 -07:00
|
|
|
|
2005-02-10 05:25:02 -08:00
|
|
|
#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
|
2003-12-03 12:03:07 -08:00
|
|
|
|
2003-11-17 11:50:05 -08:00
|
|
|
|
2014-07-17 10:27:49 -07:00
|
|
|
#define iswhite(x) testbits((x)->marked, WHITEBITS)
|
|
|
|
#define isblack(x) testbit((x)->marked, BLACKBIT)
|
2010-05-07 11:43:51 -07:00
|
|
|
#define isgray(x) /* neither white nor black */ \
|
2014-07-17 10:27:49 -07:00
|
|
|
(!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT)))
|
2003-12-09 08:56:11 -08:00
|
|
|
|
2014-07-17 10:27:49 -07:00
|
|
|
#define tofinalize(x) testbit((x)->marked, FINALIZEDBIT)
|
2010-05-07 11:08:05 -07:00
|
|
|
|
2013-08-27 11:53:35 -07:00
|
|
|
#define otherwhite(g) ((g)->currentwhite ^ WHITEBITS)
|
2017-02-23 13:07:34 -08:00
|
|
|
#define isdeadm(ow,m) ((m) & (ow))
|
2014-07-17 10:27:49 -07:00
|
|
|
#define isdead(g,v) isdeadm(otherwhite(g), (v)->marked)
|
2003-12-09 08:56:11 -08:00
|
|
|
|
2014-07-17 10:27:49 -07:00
|
|
|
#define changewhite(x) ((x)->marked ^= WHITEBITS)
|
2020-08-13 11:23:21 -07:00
|
|
|
#define nw2black(x) \
|
|
|
|
check_exp(!iswhite(x), l_setbit((x)->marked, BLACKBIT))
|
2003-12-09 08:56:11 -08:00
|
|
|
|
2018-01-28 07:13:26 -08:00
|
|
|
#define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS)
|
2003-11-17 11:50:05 -08:00
|
|
|
|
|
|
|
|
2017-04-05 09:50:51 -07:00
|
|
|
/* object age in generational mode */
|
|
|
|
#define G_NEW 0 /* created in current cycle */
|
|
|
|
#define G_SURVIVAL 1 /* created in previous cycle */
|
2017-04-10 06:33:04 -07:00
|
|
|
#define G_OLD0 2 /* marked old by frw. barrier in this cycle */
|
|
|
|
#define G_OLD1 3 /* first full cycle as old */
|
2017-04-05 09:50:51 -07:00
|
|
|
#define G_OLD 4 /* really old object (not to be visited) */
|
|
|
|
#define G_TOUCHED1 5 /* old object touched this cycle */
|
|
|
|
#define G_TOUCHED2 6 /* old object touched in previous cycle */
|
|
|
|
|
|
|
|
#define AGEBITS 7 /* all age bits (111) */
|
|
|
|
|
|
|
|
#define getage(o) ((o)->marked & AGEBITS)
|
|
|
|
#define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a))
|
|
|
|
#define isold(o) (getage(o) > G_SURVIVAL)
|
|
|
|
|
|
|
|
#define changeage(o,f,t) \
|
|
|
|
check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t)))
|
|
|
|
|
|
|
|
|
2017-05-26 12:14:29 -07:00
|
|
|
/* Default Values for GC parameters */
|
|
|
|
#define LUAI_GENMAJORMUL 100
|
2018-02-05 09:14:29 -08:00
|
|
|
#define LUAI_GENMINORMUL 20
|
2017-10-11 05:38:45 -07:00
|
|
|
|
|
|
|
/* wait memory to double before starting new cycle */
|
2019-01-30 05:44:42 -08:00
|
|
|
#define LUAI_GCPAUSE 200
|
2017-10-11 05:38:45 -07:00
|
|
|
|
|
|
|
/*
|
2018-02-05 09:14:29 -08:00
|
|
|
** some gc parameters are stored divided by 4 to allow a maximum value
|
2019-05-03 06:14:25 -07:00
|
|
|
** up to 1023 in a 'lu_byte'.
|
2017-10-11 05:38:45 -07:00
|
|
|
*/
|
|
|
|
#define getgcparam(p) ((p) * 4)
|
|
|
|
#define setgcparam(p,v) ((p) = (v) / 4)
|
|
|
|
|
|
|
|
#define LUAI_GCMUL 100
|
2017-05-26 12:14:29 -07:00
|
|
|
|
|
|
|
/* how much to allocate before next GC step (log2) */
|
|
|
|
#define LUAI_GCSTEPSIZE 13 /* 8 KB */
|
|
|
|
|
|
|
|
|
2019-01-30 05:44:42 -08:00
|
|
|
/*
|
|
|
|
** Check whether the declared GC mode is generational. While in
|
|
|
|
** generational mode, the collector can go temporarily to incremental
|
|
|
|
** mode to improve performance. This is signaled by 'g->lastatomic != 0'.
|
|
|
|
*/
|
|
|
|
#define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0)
|
|
|
|
|
2021-12-13 05:41:17 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Control when GC is running:
|
|
|
|
*/
|
|
|
|
#define GCSTPUSR 1 /* bit true when GC stopped by user */
|
|
|
|
#define GCSTPGC 2 /* bit true when GC stopped by itself */
|
2021-12-22 04:00:52 -08:00
|
|
|
#define GCSTPCLS 4 /* bit true when closing Lua state */
|
2021-12-13 05:41:17 -08:00
|
|
|
#define gcrunning(g) ((g)->gcstp == 0)
|
|
|
|
|
|
|
|
|
2015-10-20 11:00:19 -07:00
|
|
|
/*
|
|
|
|
** Does one step of collection when debt becomes positive. 'pre'/'pos'
|
|
|
|
** allows some adjustments to be done only when needed. macro
|
|
|
|
** 'condchangemem' is used only for heavy tests (forcing a full
|
|
|
|
** GC cycle on every opportunity)
|
|
|
|
*/
|
2015-10-20 10:56:21 -07:00
|
|
|
#define luaC_condGC(L,pre,pos) \
|
2015-10-21 11:15:15 -07:00
|
|
|
{ if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \
|
|
|
|
condchangemem(L,pre,pos); }
|
2015-10-20 10:56:21 -07:00
|
|
|
|
2015-10-20 11:00:19 -07:00
|
|
|
/* more often than not, 'pre'/'pos' are empty */
|
2015-12-21 05:02:14 -08:00
|
|
|
#define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0)
|
2003-12-09 08:56:11 -08:00
|
|
|
|
|
|
|
|
2015-08-03 12:40:42 -07:00
|
|
|
#define luaC_objbarrier(L,p,o) ( \
|
|
|
|
(isblack(p) && iswhite(o)) ? \
|
|
|
|
luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
|
2001-06-05 12:41:24 -07:00
|
|
|
|
2022-12-15 09:18:03 -08:00
|
|
|
#define luaC_barrier(L,p,v) ( \
|
|
|
|
iscollectable(v) ? luaC_objbarrier(L,p,gcvalue(v)) : cast_void(0))
|
|
|
|
|
|
|
|
#define luaC_objbarrierback(L,p,o) ( \
|
|
|
|
(isblack(p) && iswhite(o)) ? luaC_barrierback_(L,p) : cast_void(0))
|
|
|
|
|
|
|
|
#define luaC_barrierback(L,p,v) ( \
|
|
|
|
iscollectable(v) ? luaC_objbarrierback(L, p, gcvalue(v)) : cast_void(0))
|
|
|
|
|
2013-08-21 13:09:51 -07:00
|
|
|
LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
|
2009-11-18 05:13:47 -08:00
|
|
|
LUAI_FUNC void luaC_freeallobjects (lua_State *L);
|
2005-04-25 12:24:10 -07:00
|
|
|
LUAI_FUNC void luaC_step (lua_State *L);
|
2009-12-11 13:31:14 -08:00
|
|
|
LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
|
2006-07-11 08:53:29 -07:00
|
|
|
LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
|
2013-09-11 05:26:14 -07:00
|
|
|
LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
|
2022-11-01 13:14:01 -07:00
|
|
|
LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz,
|
|
|
|
size_t offset);
|
2010-06-04 06:25:10 -07:00
|
|
|
LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
|
2018-02-19 12:06:56 -08:00
|
|
|
LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
|
2010-11-26 06:32:31 -08:00
|
|
|
LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
|
2017-02-23 13:07:34 -08:00
|
|
|
LUAI_FUNC void luaC_changemode (lua_State *L, int newmode);
|
2013-08-27 11:53:35 -07:00
|
|
|
|
1997-09-16 12:25:59 -07:00
|
|
|
|
|
|
|
#endif
|