1997-09-16 16:25:59 -03:00
|
|
|
/*
|
2015-08-03 16:40:42 -03:00
|
|
|
** $Id: lgc.h,v 2.86 2014/10/25 11:50:46 roberto Exp roberto $
|
1997-09-16 16:25:59 -03:00
|
|
|
** Garbage Collector
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lgc_h
|
|
|
|
#define lgc_h
|
|
|
|
|
|
|
|
|
|
|
|
#include "lobject.h"
|
2010-04-29 18:43:36 -03:00
|
|
|
#include "lstate.h"
|
1997-09-16 16:25:59 -03:00
|
|
|
|
2010-04-30 15:36:45 -03: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
|
2012-05-23 12:43:14 -03:00
|
|
|
** the collection cycle. These lists have no meaning when the invariant
|
|
|
|
** is not being enforced (e.g., sweep phase).
|
2010-04-30 15:36:45 -03:00
|
|
|
*/
|
|
|
|
|
1997-09-16 16:25:59 -03:00
|
|
|
|
2012-05-23 12:43:14 -03:00
|
|
|
|
|
|
|
/* how much to allocate before next GC step */
|
|
|
|
#if !defined(GCSTEPSIZE)
|
|
|
|
/* ~100 small strings */
|
|
|
|
#define GCSTEPSIZE (cast_int(100 * sizeof(TString)))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2003-12-01 14:33:30 -02:00
|
|
|
/*
|
|
|
|
** Possible states of the Garbage Collector
|
|
|
|
*/
|
2010-05-03 08:24:30 -03:00
|
|
|
#define GCSpropagate 0
|
|
|
|
#define GCSatomic 1
|
2014-02-13 10:11:34 -02:00
|
|
|
#define GCSswpallgc 2
|
2014-02-18 10:46:26 -03:00
|
|
|
#define GCSswpfinobj 3
|
|
|
|
#define GCSswptobefnz 4
|
|
|
|
#define GCSswpend 5
|
|
|
|
#define GCScallfin 6
|
|
|
|
#define GCSpause 7
|
2008-02-19 15:55:09 -03:00
|
|
|
|
|
|
|
|
2010-04-29 14:32:40 -03:00
|
|
|
#define issweepphase(g) \
|
2014-02-13 10:11:34 -02:00
|
|
|
(GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend)
|
2010-04-29 14:32:40 -03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2013-08-05 13:58:28 -03: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 11:11:17 -03:00
|
|
|
** phase may break the invariant, as objects turned white may point to
|
2010-04-29 14:32:40 -03:00
|
|
|
** still-black objects. The invariant is restored when sweep ends and
|
2013-08-05 13:58:28 -03:00
|
|
|
** all objects are white again.
|
2010-04-29 14:32:40 -03:00
|
|
|
*/
|
2012-09-11 09:53:08 -03:00
|
|
|
|
2013-08-27 15:53:35 -03:00
|
|
|
#define keepinvariant(g) ((g)->gcstate <= GCSatomic)
|
2010-04-29 14:32:40 -03:00
|
|
|
|
|
|
|
|
2003-11-17 17:50:05 -02:00
|
|
|
/*
|
2009-11-26 09:39:20 -02:00
|
|
|
** some useful bit tricks
|
2003-12-03 18:03:07 -02:00
|
|
|
*/
|
2008-06-26 16:42:45 -03:00
|
|
|
#define resetbits(x,m) ((x) &= cast(lu_byte, ~(m)))
|
|
|
|
#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 17:50:05 -02:00
|
|
|
|
|
|
|
|
2014-10-25 09:50:46 -02:00
|
|
|
/* Layout for bit use in 'marked' field: */
|
2010-05-06 15:17:22 -03:00
|
|
|
#define WHITE0BIT 0 /* object is white (type 0) */
|
|
|
|
#define WHITE1BIT 1 /* object is white (type 1) */
|
|
|
|
#define BLACKBIT 2 /* object is black */
|
2013-08-20 14:46:34 -03:00
|
|
|
#define FINALIZEDBIT 3 /* object has been marked for finalization */
|
2010-05-10 13:46:49 -03:00
|
|
|
/* bit 7 is currently used by tests (luaL_checkmemory) */
|
2010-03-24 10:07:01 -03:00
|
|
|
|
2005-02-10 11:25:02 -02:00
|
|
|
#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
|
2003-12-03 18:03:07 -02:00
|
|
|
|
2003-11-17 17:50:05 -02:00
|
|
|
|
2014-07-17 14:27:49 -03:00
|
|
|
#define iswhite(x) testbits((x)->marked, WHITEBITS)
|
|
|
|
#define isblack(x) testbit((x)->marked, BLACKBIT)
|
2010-05-07 15:43:51 -03:00
|
|
|
#define isgray(x) /* neither white nor black */ \
|
2014-07-17 14:27:49 -03:00
|
|
|
(!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT)))
|
2003-12-09 14:56:11 -02:00
|
|
|
|
2014-07-17 14:27:49 -03:00
|
|
|
#define tofinalize(x) testbit((x)->marked, FINALIZEDBIT)
|
2010-05-07 15:08:05 -03:00
|
|
|
|
2013-08-27 15:53:35 -03:00
|
|
|
#define otherwhite(g) ((g)->currentwhite ^ WHITEBITS)
|
2010-05-07 15:08:05 -03:00
|
|
|
#define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow)))
|
2014-07-17 14:27:49 -03:00
|
|
|
#define isdead(g,v) isdeadm(otherwhite(g), (v)->marked)
|
2003-12-09 14:56:11 -02:00
|
|
|
|
2014-07-17 14:27:49 -03:00
|
|
|
#define changewhite(x) ((x)->marked ^= WHITEBITS)
|
|
|
|
#define gray2black(x) l_setbit((x)->marked, BLACKBIT)
|
2003-12-09 14:56:11 -02:00
|
|
|
|
2005-02-10 11:25:02 -02:00
|
|
|
#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
|
2003-11-17 17:50:05 -02:00
|
|
|
|
|
|
|
|
2010-12-17 10:02:29 -02:00
|
|
|
#define luaC_condGC(L,c) \
|
2012-05-21 10:18:10 -03:00
|
|
|
{if (G(L)->GCdebt > 0) {c;}; condchangemem(L);}
|
2011-01-26 14:30:02 -02:00
|
|
|
#define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);)
|
2003-12-09 14:56:11 -02:00
|
|
|
|
|
|
|
|
2015-08-03 16:40:42 -03:00
|
|
|
#define luaC_barrier(L,p,v) ( \
|
|
|
|
(iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
|
|
|
|
luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0))
|
2001-06-05 16:41:24 -03:00
|
|
|
|
2015-08-03 16:40:42 -03:00
|
|
|
#define luaC_barrierback(L,p,v) ( \
|
|
|
|
(iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
|
|
|
|
luaC_barrierback_(L,p) : cast_void(0))
|
2004-08-10 16:17:23 -03:00
|
|
|
|
2015-08-03 16:40:42 -03:00
|
|
|
#define luaC_objbarrier(L,p,o) ( \
|
|
|
|
(isblack(p) && iswhite(o)) ? \
|
|
|
|
luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
|
2001-06-05 16:41:24 -03:00
|
|
|
|
2015-08-03 16:40:42 -03:00
|
|
|
#define luaC_upvalbarrier(L,uv) ( \
|
|
|
|
(iscollectable((uv)->v) && !upisopen(uv)) ? \
|
|
|
|
luaC_upvalbarrier_(L,uv) : cast_void(0))
|
2013-08-27 15:53:35 -03:00
|
|
|
|
2013-08-21 17:09:51 -03:00
|
|
|
LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
|
2009-11-18 11:13:47 -02:00
|
|
|
LUAI_FUNC void luaC_freeallobjects (lua_State *L);
|
2005-04-25 16:24:10 -03:00
|
|
|
LUAI_FUNC void luaC_step (lua_State *L);
|
2009-12-11 19:31:14 -02:00
|
|
|
LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
|
2006-07-11 12:53:29 -03:00
|
|
|
LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
|
2013-09-11 09:26:14 -03:00
|
|
|
LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
|
2010-06-04 10:25:10 -03:00
|
|
|
LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
|
2014-07-19 12:09:37 -03:00
|
|
|
LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o);
|
2013-08-27 15:53:35 -03:00
|
|
|
LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv);
|
2010-11-26 12:32:31 -02:00
|
|
|
LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
|
2013-08-27 15:53:35 -03:00
|
|
|
LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv);
|
|
|
|
|
1997-09-16 16:25:59 -03:00
|
|
|
|
|
|
|
#endif
|