# lua/lgc.c File Reference

`#include <string.h>`
`#include "lua.h"`
`#include "ldebug.h"`
`#include "ldo.h"`
`#include "lfunc.h"`
`#include "lgc.h"`
`#include "lmem.h"`
`#include "lobject.h"`
`#include "lstate.h"`
`#include "lstring.h"`
`#include "ltable.h"`
`#include "ltm.h"`

Include dependency graph for lgc.c:

Go to the source code of this file.

## Defines
#define | lgc_c
#define | LUA_CORE
#define | GCSTEPSIZE 1024u
#define | GCSWEEPMAX 40
#define | GCSWEEPCOST 10
#define | GCFINALIZECOST 100
#define | maskmarks cast_byte(~(bitmask(BLACKBIT)|WHITEBITS))
#define | makewhite(g, x) ((x)->gch.marked = cast_byte(((x)->gch.marked & maskmarks) | luaC_white(g)))
#define | white2gray(x) reset2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)
#define | black2gray(x) resetbit((x)->gch.marked, BLACKBIT)
#define | stringmark(s) reset2bits((s)->tsv.marked, WHITE0BIT, WHITE1BIT)
#define | isfinalized(u) testbit((u)->marked, FINALIZEDBIT)
#define | markfinalized(u) l_setbit((u)->marked, FINALIZEDBIT)
#define | KEYWEAK bitmask(KEYWEAKBIT)
#define | VALUEWEAK bitmask(VALUEWEAKBIT)
#define | markvalue(g, o)
#define | markobject(g, t)
#define | setthreshold(g) (g->GCthreshold = (g->estimate/100) * g->gcpause)
#define | sweepwholelist(L, p) sweeplist(L,p,MAX_LUMEM)
## Functions
static void | removeentry (Node *n)
static void | reallymarkobject (global_State *g, GCObject *o)
static void | marktmu (global_State *g)
size_t | luaC_separateudata (lua_State *L, int all)
static int | traversetable (global_State *g, Table *h)
static void | traverseproto (global_State *g, Proto *f)
static void | traverseclosure (global_State *g, Closure *cl)
static void | checkstacksizes (lua_State *L, StkId max)
static void | traversestack (global_State *g, lua_State *l)
static l_mem | propagatemark (global_State *g)
static size_t | propagateall (global_State *g)
static int | iscleared (const TValue *o, int iskey)
static void | cleartable (GCObject *l)
static void | freeobj (lua_State *L, GCObject *o)
static GCObject ** | sweeplist (lua_State *L, GCObject **p, lu_mem count)
static void | checkSizes (lua_State *L)
static void | GCTM (lua_State *L)
void | luaC_callGCTM (lua_State *L)
void | luaC_freeall (lua_State *L)
static void | markmt (global_State *g)
static void | markroot (lua_State *L)
static void | remarkupvals (global_State *g)
static void | atomic (lua_State *L)
static l_mem | singlestep (lua_State *L)
void | luaC_step (lua_State *L)
void | luaC_fullgc (lua_State *L)
void | luaC_barrierf (lua_State *L, GCObject *o, GCObject *v)
void | luaC_barrierback (lua_State *L, Table *t)
void | luaC_link (lua_State *L, GCObject *o, lu_byte tt)
void | luaC_linkupval (lua_State *L, UpVal *uv)

## Define Documentation

#define black2gray | ( | x |  | ) | resetbit((x)->gch.marked, BLACKBIT)

Definition at line 38 of file lgc.c.

Referenced by luaC_barrierback(), and propagatemark().

#define GCFINALIZECOST 100

Definition at line 29 of file lgc.c.

Referenced by singlestep().

#define GCSTEPSIZE 1024u

Definition at line 26 of file lgc.c.

Referenced by luaC_step().

#define GCSWEEPCOST 10

Definition at line 28 of file lgc.c.

Referenced by singlestep().

#define GCSWEEPMAX 40

Definition at line 27 of file lgc.c.

Referenced by singlestep().

#define isfinalized | ( | u |  | ) | testbit((u)->marked, FINALIZEDBIT)

Definition at line 43 of file lgc.c.

Referenced by iscleared(), and luaC_separateudata().

#define KEYWEAK bitmask(KEYWEAKBIT)

Definition at line 47 of file lgc.c.

Referenced by traversetable().

#define lgc_c

Definition at line 9 of file lgc.c.

#define LUA_CORE

Definition at line 10 of file lgc.c.

#define makewhite | ( | g,
|  | x |  | ) | ((x)->gch.marked = cast_byte(((x)->gch.marked & maskmarks) | luaC_white(g)))

Definition at line 34 of file lgc.c.

Referenced by GCTM(), luaC_barrierf(), luaC_linkupval(), marktmu(), and sweeplist().

#define markfinalized | ( | u |  | ) | l_setbit((u)->marked, FINALIZEDBIT)

Definition at line 44 of file lgc.c.

Referenced by luaC_separateudata().

#define markobject | ( | g,
|  | t |  | ) |

Value:

    { if (iswhite(obj2gco(t))) \
                    reallymarkobject(g, obj2gco(t)); }

Definition at line 55 of file lgc.c.

Referenced by atomic(), markmt(), markroot(), reallymarkobject(), traverseclosure(), traverseproto(), and traversetable().

#define markvalue | ( | g,
|  | o |  | ) |

Value:

    { checkconsistency(o); \
      if (iscollectable(o) && iswhite(gcvalue(o))) reallymarkobject(g,gcvalue(o)); }

Definition at line 52 of file lgc.c.

Referenced by markroot(), reallymarkobject(), remarkupvals(), traverseclosure(), traverseproto(), traversestack(), and traversetable().

#define maskmarks cast_byte(~(bitmask(BLACKBIT)|WHITEBITS))

Definition at line 32 of file lgc.c.

#define setthreshold | ( | g |  | ) | (g->GCthreshold = (g->estimate/100) * g->gcpause)

Definition at line 59 of file lgc.c.

Referenced by luaC_fullgc(), and luaC_step().

#define stringmark | ( | s |  | ) | reset2bits((s)->tsv.marked, WHITE0BIT, WHITE1BIT)

Definition at line 40 of file lgc.c.

Referenced by iscleared(), and traverseproto().

#define sweepwholelist | ( | L,
|  | p |  | ) | sweeplist(L,p,MAX_LUMEM)

Definition at line 404 of file lgc.c.

Referenced by luaC_freeall(), singlestep(), and sweeplist().

#define VALUEWEAK bitmask(VALUEWEAKBIT)

Definition at line 48 of file lgc.c.

Referenced by traversetable().

#define white2gray | ( | x |  | ) | reset2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)

Definition at line 37 of file lgc.c.

Referenced by reallymarkobject().

## Function Documentation

static void atomic | ( | lua_State * | L | ) | `[static]`

Definition at line 525 of file lgc.c.

References cast_byte, cleartable(), global_State::currentwhite, global_State::estimate, G, GCSsweepstring, global_State::gcstate, global_State::gray, global_State::grayagain, iswhite, lua_assert, luaC_separateudata(), global_State::mainthread, markmt(), markobject, marktmu(), obj2gco, otherwhite, propagateall(), remarkupvals(), global_State::rootgc, global_State::sweepgc, global_State::sweepstrgc, global_State::totalbytes, and global_State::weak.

Referenced by singlestep().

static void checkSizes | ( | lua_State * | L | ) | `[static]`

Definition at line 431 of file lgc.c.

References global_State::buff, cast, G, LUA_MINBUFFER, luaS_resize(), luaZ_resizebuffer, luaZ_sizebuffer, MINSTRTABSIZE, stringtable::nuse, stringtable::size, and global_State::strt.

Referenced by singlestep().

static void checkstacksizes | ( | lua_State * | L,
|  | StkId | max |
| ) |  |  | `[static]`

Definition at line 241 of file lgc.c.

References lua_State::base_ci, BASIC_STACK_SIZE, cast_int, lua_State::ci, condhardstacktests, EXTRA_STACK, luaD_reallocCI(), luaD_reallocstack(), lua_State::size_ci, lua_State::stack, and lua_State::stacksize.

Referenced by traversestack().

static void cleartable | ( | GCObject * | l | ) | `[static]`

Definition at line 351 of file lgc.c.

References Table::array, Table::gclist, gco2h, gnode, gval, iscleared(), key2tval, KEYWEAKBIT, lua_assert, Table::marked, removeentry(), setnilvalue, Table::sizearray, sizenode, testbit, ttisnil, and VALUEWEAKBIT.

Referenced by atomic().

static void freeobj | ( | lua_State * | L,
|  | GCObject * | o |
| ) |  |  | `[static]`

Definition at line 378 of file lgc.c.

References G, GCObject::gch, gco2cl, gco2h, gco2p, gco2th, gco2ts, gco2u, gco2uv, lua_assert, LUA_TPROTO, LUA_TUPVAL, luaE_freethread(), luaF_freeclosure(), luaF_freeproto(), luaF_freeupval(), luaH_free(), luaM_freemem, sizestring, sizeudata, and GCheader::tt.

Referenced by sweeplist().

static void GCTM | ( | lua_State * | L | ) | `[static]`

Definition at line 445 of file lgc.c.

References lua_State::allowhook, fasttm, G, GCObject::gch, global_State::GCthreshold, luaD_call(), global_State::mainthread, makewhite, Udata::metatable, lua_State::next, Udata::next, GCheader::next, rawgco2u, setobj2s, setuvalue, TM_GC, global_State::tmudata, lua_State::top, global_State::totalbytes, and Udata::uv.

Referenced by luaC_callGCTM(), and singlestep().

static int iscleared | ( | const TValue * | o,
|  | int | iskey |
| ) |  |  | `[static]`

Definition at line 337 of file lgc.c.

References gcvalue, iscollectable, isfinalized, iswhite, rawtsvalue, stringmark, ttisstring, ttisuserdata, and uvalue.

Referenced by cleartable().

void luaC_barrierback | ( | lua_State * | L,
|  | Table * | t |
| ) |  |  |

Definition at line 675 of file lgc.c.

References black2gray, G, Table::gclist, GCSfinalize, GCSpause, global_State::gcstate, global_State::grayagain, isblack, isdead, lua_assert, and obj2gco.

void luaC_barrierf | ( | lua_State * | L,
|  | GCObject * | o,
|  | GCObject * | v |
| ) |  |  |

Definition at line 662 of file lgc.c.

References G, GCObject::gch, GCSfinalize, GCSpause, GCSpropagate, global_State::gcstate, isblack, isdead, iswhite, lua_assert, makewhite, reallymarkobject(), and ttype.

void luaC_callGCTM | ( | lua_State * | L | ) |

Definition at line 477 of file lgc.c.

References G, and GCTM().

Referenced by callallgcTM().

void luaC_freeall | ( | lua_State * | L | ) |

Definition at line 483 of file lgc.c.

References bitmask, global_State::currentwhite, G, stringtable::hash, global_State::rootgc, SFIXEDBIT, stringtable::size, global_State::strt, sweepwholelist, and WHITEBITS.

Referenced by close_state().

void luaC_fullgc | ( | lua_State * | L | ) |

Definition at line 636 of file lgc.c.

References G, GCSfinalize, GCSpause, GCSpropagate, GCSsweep, GCSsweepstring, global_State::gcstate, global_State::gray, global_State::grayagain, lua_assert, markroot(), global_State::rootgc, setthreshold, singlestep(), global_State::sweepgc, global_State::sweepstrgc, and global_State::weak.

Referenced by lua_gc().

void luaC_link | ( | lua_State * | L,
|  | GCObject * | o,
|  | lu_byte | tt |
| ) |  |  |

Definition at line 686 of file lgc.c.

References G, GCObject::gch, luaC_white, GCheader::marked, GCheader::next, global_State::rootgc, and GCheader::tt.

Referenced by luaE_newthread(), luaF_newCclosure(), luaF_newLclosure(), luaF_newproto(), luaF_newupval(), and luaH_new().

void luaC_linkupval | ( | lua_State * | L,
|  | UpVal * | uv |
| ) |  |  |

Definition at line 695 of file lgc.c.

References G, GCObject::gch, GCSfinalize, GCSpause, GCSpropagate, global_State::gcstate, gray2black, isgray, lua_assert, luaC_barrier, makewhite, GCheader::next, obj2gco, global_State::rootgc, and UpVal::v.

Referenced by luaF_close().

size_t luaC_separateudata | ( | lua_State * | L,
|  | int | all |
| ) |  |  |

Definition at line 128 of file lgc.c.

References fasttm, G, GCObject::gch, gco2u, isfinalized, iswhite, global_State::mainthread, markfinalized, GCheader::next, lua_State::next, sizeudata, TM_GC, and global_State::tmudata.

Referenced by atomic(), and lua_close().

void luaC_step | ( | lua_State * | L | ) |

Definition at line 610 of file lgc.c.

References global_State::estimate, G, global_State::gcdept, GCSpause, global_State::gcstate, global_State::gcstepmul, GCSTEPSIZE, global_State::GCthreshold, lua_assert, MAX_LUMEM, setthreshold, singlestep(), and global_State::totalbytes.

Referenced by lua_gc().

static void markmt | ( | global_State * | g | ) | `[static]`

Definition at line 493 of file lgc.c.

References markobject, global_State::mt, and NUM_TAGS.

Referenced by atomic(), and markroot().

static void markroot | ( | lua_State * | L | ) | `[static]`

Definition at line 501 of file lgc.c.

References G, GCSpropagate, global_State::gcstate, global_State::gray, global_State::grayagain, gt, global_State::mainthread, markmt(), markobject, markvalue, registry, and global_State::weak.

Referenced by luaC_fullgc(), and singlestep().

static void marktmu | ( | global_State * | g | ) | `[static]`

Definition at line 115 of file lgc.c.

References GCObject::gch, makewhite, GCheader::next, reallymarkobject(), and global_State::tmudata.

Referenced by atomic().

static size_t propagateall | ( | global_State * | g | ) | `[static]`

Definition at line 323 of file lgc.c.

References global_State::gray, and propagatemark().

Referenced by atomic().

static l_mem propagatemark | ( | global_State * | g | ) | `[static]`

Definition at line 277 of file lgc.c.

References black2gray, Closure::c, GCObject::gch, Proto::gclist, lua_State::gclist, CClosure::gclist, Table::gclist, gco2cl, gco2h, gco2p, gco2th, global_State::gray, gray2black, global_State::grayagain, CClosure::isC, isgray, Closure::l, lua_assert, LUA_TPROTO, LClosure::nupvalues, CClosure::nupvalues, lua_State::size_ci, Table::sizearray, sizeCclosure, Proto::sizecode, Proto::sizek, sizeLclosure, Proto::sizelineinfo, Proto::sizelocvars, sizenode, Proto::sizep, Proto::sizeupvalues, lua_State::stacksize, traverseclosure(), traverseproto(), traversestack(), traversetable(), and GCheader::tt.

Referenced by propagateall(), and singlestep().

static void reallymarkobject | ( | global_State * | g,
|  | GCObject * | o |
| ) |  |  | `[static]`

Definition at line 69 of file lgc.c.

References GCObject::gch, gco2cl, gco2h, gco2p, gco2th, gco2u, gco2uv, global_State::gray, gray2black, isdead, iswhite, lua_assert, LUA_TPROTO, LUA_TUPVAL, markobject, markvalue, GCheader::tt, UpVal::u, UpVal::v, UpVal::value, and white2gray.

Referenced by luaC_barrierf(), and marktmu().

static void remarkupvals | ( | global_State * | g | ) | `[static]`

Definition at line 515 of file lgc.c.

References isgray, UpVal::l, lua_assert, markvalue, obj2gco, UpVal::u, global_State::uvhead, and UpVal::v.

Referenced by atomic().

static void removeentry | ( | Node * | n | ) | `[static]`

Definition at line 62 of file lgc.c.

References gkey, gval, iscollectable, lua_assert, LUA_TDEADKEY, setttype, and ttisnil.

Referenced by cleartable(), and traversetable().

static l_mem singlestep | ( | lua_State * | L | ) | `[static]`

Definition at line 556 of file lgc.c.

References atomic(), checkSizes(), global_State::estimate, G, global_State::gcdept, GCFINALIZECOST, GCSfinalize, GCSpause, GCSpropagate, GCSsweep, GCSsweepstring, global_State::gcstate, GCSWEEPCOST, GCSWEEPMAX, GCTM(), global_State::gray, stringtable::hash, lua_assert, markroot(), propagatemark(), stringtable::size, global_State::strt, global_State::sweepgc, sweeplist(), global_State::sweepstrgc, sweepwholelist, global_State::tmudata, and global_State::totalbytes.

Referenced by luaC_fullgc(), and luaC_step().

static GCObject** sweeplist | ( | lua_State * | L,
|  | GCObject ** | p,
|  | lu_mem | count |
| ) |  |  | `[static]`

Definition at line 407 of file lgc.c.

References bitmask, FIXEDBIT, freeobj(), G, GCObject::gch, gco2th, isdead, lua_assert, makewhite, GCheader::marked, GCheader::next, otherwhite, global_State::rootgc, SFIXEDBIT, sweepwholelist, testbit, GCheader::tt, and WHITEBITS.

Referenced by singlestep().

static void traverseclosure | ( | global_State * | g,
|  | Closure * | cl |
| ) |  |  | `[static]`

Definition at line 224 of file lgc.c.

References Closure::c, CClosure::env, CClosure::isC, Closure::l, lua_assert, markobject, markvalue, Proto::nups, LClosure::nupvalues, CClosure::nupvalues, LClosure::p, LClosure::upvals, and CClosure::upvalue.

Referenced by propagatemark().

static void traverseproto | ( | global_State * | g,
|  | Proto * | f |
| ) |  |  | `[static]`

Definition at line 203 of file lgc.c.

References Proto::k, Proto::locvars, markobject, markvalue, Proto::p, Proto::sizek, Proto::sizelocvars, Proto::sizep, Proto::sizeupvalues, Proto::source, stringmark, Proto::upvalues, and LocVar::varname.

Referenced by propagatemark().

static void traversestack | ( | global_State * | g,
|  | lua_State * | l |
| ) |  |  | `[static]`

Definition at line 256 of file lgc.c.

References lua_State::base_ci, checkstacksizes(), gt, lua_assert, markvalue, setnilvalue, lua_State::stack, lua_State::stack_last, CallInfo::top, and lua_State::top.

Referenced by propagatemark().

static int traversetable | ( | global_State * | g,
|  | Table * | h |
| ) |  |  | `[static]`

Definition at line 158 of file lgc.c.

References Table::array, cast_byte, Table::gclist, gfasttm, gkey, gnode, gval, KEYWEAK, KEYWEAKBIT, lua_assert, LUA_TDEADKEY, Table::marked, markobject, markvalue, Table::metatable, obj2gco, removeentry(), Table::sizearray, sizenode, svalue, TM_MODE, ttisnil, ttisstring, ttype, VALUEWEAK, VALUEWEAKBIT, and global_State::weak.

Referenced by propagatemark().