1997-09-16 12:25:59 -07:00
|
|
|
/*
|
2013-08-20 10:46:34 -07:00
|
|
|
** $Id: lgc.h,v 2.62 2013/08/19 14:18:43 roberto Exp roberto $
|
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
|
|
|
/*
|
|
|
|
** 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 08:43:14 -07:00
|
|
|
** the collection cycle. 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
|
|
|
|
2012-05-23 08:43:14 -07: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 08:33:30 -08:00
|
|
|
/*
|
|
|
|
** Possible states of the Garbage Collector
|
|
|
|
*/
|
2010-05-03 04:24:30 -07:00
|
|
|
#define GCSpropagate 0
|
|
|
|
#define GCSatomic 1
|
|
|
|
#define GCSsweepstring 2
|
|
|
|
#define GCSsweepudata 3
|
|
|
|
#define GCSsweep 4
|
2010-05-03 10:33:39 -07:00
|
|
|
#define GCSpause 5
|
2008-02-19 10:55:09 -08:00
|
|
|
|
|
|
|
|
2010-04-29 10:32:40 -07:00
|
|
|
#define issweepphase(g) \
|
|
|
|
(GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep)
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
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-05 09:58:28 -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
|
|
|
*/
|
2008-06-26 12:42:45 -07: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 11:50:05 -08:00
|
|
|
|
|
|
|
|
2010-05-06 11:17:22 -07:00
|
|
|
/* Layout for bit use in `marked' field: */
|
|
|
|
#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 10:46:34 -07:00
|
|
|
#define FINALIZEDBIT 3 /* object has been marked for finalization */
|
|
|
|
#define FIXEDBIT 4 /* object is fixed (should not be collected) */
|
|
|
|
#define LOCALBIT 5 /* object is not local */
|
2010-05-10 09:46:49 -07:00
|
|
|
/* bit 7 is currently used by tests (luaL_checkmemory) */
|
2010-03-24 06:07:01 -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
|
|
|
|
2008-02-19 10:55:09 -08:00
|
|
|
#define iswhite(x) testbits((x)->gch.marked, WHITEBITS)
|
2003-12-09 08:56:11 -08:00
|
|
|
#define isblack(x) testbit((x)->gch.marked, BLACKBIT)
|
2010-05-07 11:43:51 -07:00
|
|
|
#define isgray(x) /* neither white nor black */ \
|
|
|
|
(!testbits((x)->gch.marked, WHITEBITS | bitmask(BLACKBIT)))
|
2013-08-16 11:55:49 -07:00
|
|
|
#define islocal(x) (!testbit((x)->gch.marked, LOCALBIT))
|
2003-12-09 08:56:11 -08:00
|
|
|
|
2013-08-20 10:46:34 -07:00
|
|
|
#define tofinalize(x) testbit((x)->gch.marked, FINALIZEDBIT)
|
2010-05-07 11:08:05 -07:00
|
|
|
|
2005-02-10 05:25:02 -08:00
|
|
|
#define otherwhite(g) (g->currentwhite ^ WHITEBITS)
|
2010-05-07 11:08:05 -07:00
|
|
|
#define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow)))
|
|
|
|
#define isdead(g,v) isdeadm(otherwhite(g), (v)->gch.marked)
|
2003-12-09 08:56:11 -08:00
|
|
|
|
2005-02-10 05:25:02 -08:00
|
|
|
#define changewhite(x) ((x)->gch.marked ^= WHITEBITS)
|
2005-02-23 09:30:22 -08:00
|
|
|
#define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT)
|
2003-12-09 08:56:11 -08:00
|
|
|
|
2013-08-16 11:55:49 -07:00
|
|
|
#define nolocal(x) l_setbit((x)->gch.marked, LOCALBIT)
|
|
|
|
#define valnolocal(v) { if (iscollectable(v)) nolocal(gcvalue(v)); }
|
|
|
|
|
2005-02-10 05:25:02 -08:00
|
|
|
#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
|
2003-11-17 11:50:05 -08:00
|
|
|
|
|
|
|
|
2010-12-17 04:02:29 -08:00
|
|
|
#define luaC_condGC(L,c) \
|
2012-05-21 06:18:10 -07:00
|
|
|
{if (G(L)->GCdebt > 0) {c;}; condchangemem(L);}
|
2011-01-26 08:30:02 -08:00
|
|
|
#define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);)
|
2003-12-09 08:56:11 -08:00
|
|
|
|
|
|
|
|
2013-08-13 10:36:44 -07:00
|
|
|
#define luaC_barrier(L,p,v) { \
|
2013-08-16 11:55:49 -07:00
|
|
|
if (iscollectable(v) && \
|
|
|
|
(nolocal(gcvalue(v)), isblack(obj2gco(p)) && iswhite(gcvalue(v)))) \
|
2010-06-04 06:25:10 -07:00
|
|
|
luaC_barrier_(L,obj2gco(p),gcvalue(v)); }
|
2001-06-05 12:41:24 -07:00
|
|
|
|
2013-08-13 10:36:44 -07:00
|
|
|
#define luaC_barrierback(L,p,v) { \
|
2013-08-16 11:55:49 -07:00
|
|
|
if (iscollectable(v) && \
|
|
|
|
(nolocal(gcvalue(v)), isblack(obj2gco(p)) && iswhite(gcvalue(v)))) \
|
2010-06-04 06:25:10 -07:00
|
|
|
luaC_barrierback_(L,p); }
|
2004-08-10 12:17:23 -07:00
|
|
|
|
2013-08-13 10:36:44 -07:00
|
|
|
#define luaC_objbarrier(L,p,o) { \
|
2013-08-16 11:55:49 -07:00
|
|
|
if (nolocal(obj2gco(o)), isblack(obj2gco(p)) && iswhite(obj2gco(o))) \
|
2010-06-04 06:25:10 -07:00
|
|
|
luaC_barrier_(L,obj2gco(p),obj2gco(o)); }
|
2001-06-05 12:41:24 -07:00
|
|
|
|
2010-06-04 06:25:10 -07:00
|
|
|
#define luaC_objbarrierback(L,p,o) \
|
2013-08-16 11:55:49 -07:00
|
|
|
{ if (nolocal(obj2gco(o)), isblack(obj2gco(p)) && iswhite(obj2gco(o))) \
|
|
|
|
luaC_barrierback_(L,p); }
|
2010-06-04 06:25:10 -07:00
|
|
|
|
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);
|
2010-12-29 10:00:23 -08:00
|
|
|
LUAI_FUNC void luaC_forcestep (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);
|
2009-12-17 07:46:44 -08:00
|
|
|
LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz,
|
|
|
|
GCObject **list, int offset);
|
2010-06-04 06:25:10 -07:00
|
|
|
LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
|
|
|
|
LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
|
2010-06-07 09:55:34 -07:00
|
|
|
LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c);
|
2010-11-26 06:32:31 -08:00
|
|
|
LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
|
2010-04-29 14:43:36 -07:00
|
|
|
LUAI_FUNC void luaC_checkupvalcolor (global_State *g, UpVal *uv);
|
1997-09-16 12:25:59 -07:00
|
|
|
|
|
|
|
#endif
|