Skip to content

Commit

Permalink
Rename of fields in global state that control GC
Browse files Browse the repository at this point in the history
All fields in the global state that control the pace of the garbage
collector prefixed with 'GC'.
  • Loading branch information
roberto-ieru committed Sep 6, 2024
1 parent 007b8c7 commit a04e0ff
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
4 changes: 2 additions & 2 deletions lapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1190,11 +1190,11 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
}
case LUA_GCCOUNT: {
/* GC values are expressed in Kbytes: #bytes/2^10 */
res = cast_int(g->totalbytes >> 10);
res = cast_int(g->GCtotalbytes >> 10);
break;
}
case LUA_GCCOUNTB: {
res = cast_int(g->totalbytes & 0x3ff);
res = cast_int(g->GCtotalbytes & 0x3ff);
break;
}
case LUA_GCSTEP: {
Expand Down
28 changes: 14 additions & 14 deletions lgc.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ GCObject *luaC_newobj (lua_State *L, lu_byte tt, size_t sz) {
** (only closures can), and a userdata's metatable must be a table.
*/
static void reallymarkobject (global_State *g, GCObject *o) {
g->marked++;
g->GCmarked++;
switch (o->tt) {
case LUA_VSHRSTR:
case LUA_VLNGSTR: {
Expand Down Expand Up @@ -401,7 +401,7 @@ static void cleargraylists (global_State *g) {
*/
static void restartcollection (global_State *g) {
cleargraylists(g);
g->marked = NFIXED;
g->GCmarked = NFIXED;
markobject(g, g->mainthread);
markvalue(g, &g->l_registry);
markmt(g);
Expand Down Expand Up @@ -781,7 +781,7 @@ static void freeupval (lua_State *L, UpVal *uv) {


static void freeobj (lua_State *L, GCObject *o) {
G(L)->totalobjs--;
G(L)->GCtotalobjs--;
switch (o->tt) {
case LUA_VPROTO:
luaF_freeproto(L, gco2p(o));
Expand Down Expand Up @@ -1052,7 +1052,7 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
** approximately (marked * pause / 100).
*/
static void setpause (global_State *g) {
l_obj threshold = applygcparam(g, PAUSE, g->marked);
l_obj threshold = applygcparam(g, PAUSE, g->GCmarked);
l_obj debt = threshold - gettotalobjs(g);
if (debt < 0) debt = 0;
luaE_setdebt(g, debt);
Expand Down Expand Up @@ -1236,7 +1236,7 @@ static void finishgencycle (lua_State *L, global_State *g) {
** in generational mode.
*/
static void minor2inc (lua_State *L, global_State *g, lu_byte kind) {
g->GCmajorminor = g->marked; /* number of live objects */
g->GCmajorminor = g->GCmarked; /* number of live objects */
g->gckind = kind;
g->reallyold = g->old1 = g->survival = NULL;
g->finobjrold = g->finobjold1 = g->finobjsur = NULL;
Expand All @@ -1260,7 +1260,7 @@ static void minor2inc (lua_State *L, global_State *g, lu_byte kind) {
static int checkminormajor (global_State *g, l_obj addedold1) {
l_obj step = applygcparam(g, MINORMUL, g->GCmajorminor);
l_obj limit = applygcparam(g, MINORMAJOR, g->GCmajorminor);
return (addedold1 >= (step >> 1) || g->marked >= limit);
return (addedold1 >= (step >> 1) || g->GCmarked >= limit);
}

/*
Expand All @@ -1270,7 +1270,7 @@ static int checkminormajor (global_State *g, l_obj addedold1) {
*/
static void youngcollection (lua_State *L, global_State *g) {
l_obj addedold1 = 0;
l_obj marked = g->marked; /* preserve 'g->marked' */
l_obj marked = g->GCmarked; /* preserve 'g->GCmarked' */
GCObject **psurvival; /* to point to first non-dead survival object */
GCObject *dummy; /* dummy out parameter to 'sweepgen' */
lua_assert(g->gcstate == GCSpropagate);
Expand Down Expand Up @@ -1304,12 +1304,12 @@ static void youngcollection (lua_State *L, global_State *g) {
sweepgen(L, g, &g->tobefnz, NULL, &dummy, &addedold1);

/* keep total number of added old1 objects */
g->marked = marked + addedold1;
g->GCmarked = marked + addedold1;

/* decide whether to shift to major mode */
if (checkminormajor(g, addedold1)) {
minor2inc(L, g, KGC_GENMAJOR); /* go to major mode */
g->marked = 0; /* avoid pause in first major cycle */
g->GCmarked = 0; /* avoid pause in first major cycle */
}
else
finishgencycle(L, g); /* still in minor mode; finish it */
Expand Down Expand Up @@ -1338,8 +1338,8 @@ static void atomic2gen (lua_State *L, global_State *g) {
sweep2old(L, &g->tobefnz);

g->gckind = KGC_GENMINOR;
g->GCmajorminor = g->marked; /* "base" for number of objects */
g->marked = 0; /* to count the number of added old1 objects */
g->GCmajorminor = g->GCmarked; /* "base" for number of objects */
g->GCmarked = 0; /* to count the number of added old1 objects */
finishgencycle(L, g);
}

Expand Down Expand Up @@ -1407,14 +1407,14 @@ static int checkmajorminor (lua_State *L, global_State *g) {
l_obj numobjs = gettotalobjs(g);
l_obj addedobjs = numobjs - g->GCmajorminor;
l_obj limit = applygcparam(g, MAJORMINOR, addedobjs);
l_obj tobecollected = numobjs - g->marked;
l_obj tobecollected = numobjs - g->GCmarked;
if (tobecollected > limit) {
atomic2gen(L, g); /* return to generational mode */
setminordebt(g);
return 0; /* exit incremental collection */
}
}
g->GCmajorminor = g->marked; /* prepare for next collection */
g->GCmajorminor = g->GCmarked; /* prepare for next collection */
return 1; /* stay doing incremental collections */
}

Expand Down Expand Up @@ -1692,7 +1692,7 @@ static void fullinc (lua_State *L, global_State *g) {
luaC_runtilstate(L, GCSpause, 1);
luaC_runtilstate(L, GCScallfin, 1); /* run up to finalizers */
/* 'marked' must be correct after a full GC cycle */
lua_assert(g->marked == gettotalobjs(g));
lua_assert(g->GCmarked == gettotalobjs(g));
luaC_runtilstate(L, GCSpause, 1); /* finish collection */
setpause(g);
}
Expand Down
8 changes: 4 additions & 4 deletions lmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) {
global_State *g = G(L);
lua_assert((osize == 0) == (block == NULL));
callfrealloc(g, block, osize, 0);
g->totalbytes -= osize;
g->GCtotalbytes -= osize;
}


Expand Down Expand Up @@ -181,10 +181,10 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
if (l_unlikely(newblock == NULL && nsize > 0)) {
newblock = tryagain(L, block, osize, nsize);
if (newblock == NULL) /* still no memory? */
return NULL; /* do not update 'totalbytes' */
return NULL; /* do not update 'GCtotalbytes' */
}
lua_assert((nsize == 0) == (newblock == NULL));
g->totalbytes += nsize - osize;
g->GCtotalbytes += nsize - osize;
return newblock;
}

Expand All @@ -209,7 +209,7 @@ void *luaM_malloc_ (lua_State *L, size_t size, int tag) {
if (newblock == NULL)
luaM_error(L);
}
g->totalbytes += size;
g->GCtotalbytes += size;
return newblock;
}
}
16 changes: 8 additions & 8 deletions lstate.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ typedef struct LG {

/*
** set GCdebt to a new value keeping the real number of allocated
** objects (totalobjs - GCdebt) invariant and avoiding overflows in
** 'totalobjs'.
** objects (GCtotalobjs - GCdebt) invariant and avoiding overflows in
** 'GCtotalobjs'.
*/
void luaE_setdebt (global_State *g, l_obj debt) {
l_obj tb = gettotalobjs(g);
lua_assert(tb > 0);
if (debt > MAX_LOBJ - tb)
debt = MAX_LOBJ - tb; /* will make 'totalobjs == MAX_LMEM' */
g->totalobjs = tb + debt;
debt = MAX_LOBJ - tb; /* will make GCtotalobjs == MAX_LOBJ */
g->GCtotalobjs = tb + debt;
g->GCdebt = debt;
}

Expand Down Expand Up @@ -269,7 +269,7 @@ static void close_state (lua_State *L) {
}
luaM_freearray(L, G(L)->strt.hash, cast_sizet(G(L)->strt.size));
freestack(L);
lua_assert(g->totalbytes == sizeof(LG));
lua_assert(g->GCtotalbytes == sizeof(LG));
lua_assert(gettotalobjs(g) == 1);
(*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
}
Expand Down Expand Up @@ -378,9 +378,9 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) {
g->gray = g->grayagain = NULL;
g->weak = g->ephemeron = g->allweak = NULL;
g->twups = NULL;
g->totalbytes = sizeof(LG);
g->totalobjs = 1;
g->marked = 0;
g->GCtotalbytes = sizeof(LG);
g->GCtotalobjs = 1;
g->GCmarked = 0;
g->GCdebt = 0;
setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */
setgcparam(g, PAUSE, LUAI_GCPAUSE);
Expand Down
8 changes: 4 additions & 4 deletions lstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,10 @@ struct CallInfo {
typedef struct global_State {
lua_Alloc frealloc; /* function to reallocate memory */
void *ud; /* auxiliary data to 'frealloc' */
lu_mem totalbytes; /* number of bytes currently allocated */
l_obj totalobjs; /* total number of objects allocated + GCdebt */
lu_mem GCtotalbytes; /* number of bytes currently allocated */
l_obj GCtotalobjs; /* total number of objects allocated + GCdebt */
l_obj GCdebt; /* objects counted but not yet allocated */
l_obj marked; /* number of objects marked in a GC cycle */
l_obj GCmarked; /* number of objects marked in a GC cycle */
l_obj GCmajorminor; /* auxiliary counter to control major-minor shifts */
stringtable strt; /* hash table for strings */
TValue l_registry;
Expand Down Expand Up @@ -412,7 +412,7 @@ union GCUnion {


/* actual number of total objects allocated */
#define gettotalobjs(g) ((g)->totalobjs - (g)->GCdebt)
#define gettotalobjs(g) ((g)->GCtotalobjs - (g)->GCdebt)


LUAI_FUNC void luaE_setdebt (global_State *g, l_obj debt);
Expand Down

0 comments on commit a04e0ff

Please sign in to comment.