Browse Source

'l_mem' renamed to 'l_obj' to count objects

pull/33/head
Roberto Ierusalimschy 2 years ago
parent
commit
ec61be9a7e
  1. 6
      lapi.c
  2. 50
      lgc.c
  3. 14
      llimits.h
  4. 4
      lstate.c
  5. 10
      lstate.h
  6. 16
      ltests.c

6
lapi.c

@ -1163,7 +1163,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
}
case LUA_GCSTEP: {
int data = va_arg(argp, int);
l_mem debt = 1; /* =1 to signal that it did an actual step */
l_obj debt = 1; /* =1 to signal that it did an actual step */
lu_byte oldstp = g->gcstp;
g->gcstp = 0; /* allow GC to run (GCSTPGC must be zero here) */
if (data == 0) {
@ -1217,8 +1217,8 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
if (stepmul != 0)
setgcparam(g->gcstepmul, stepmul);
if (stepsize != 0)
g->gcstepsize = (stepsize <= log2maxs(l_mem)) ? stepsize
: log2maxs(l_mem);
g->gcstepsize = (stepsize <= log2maxs(l_obj)) ? stepsize
: log2maxs(l_obj);
luaC_changemode(L, KGC_INC);
break;
}

50
lgc.c

@ -93,7 +93,7 @@
#define markobjectN(g,t) { if (t) markobject(g,t); }
static void reallymarkobject (global_State *g, GCObject *o);
static l_mem atomic (lua_State *L);
static l_obj atomic (lua_State *L);
static void entersweep (lua_State *L);
@ -335,9 +335,9 @@ static void markmt (global_State *g) {
/*
** mark all objects in list of being-finalized
*/
static l_mem markbeingfnz (global_State *g) {
static l_obj markbeingfnz (global_State *g) {
GCObject *o;
l_mem count = 0;
l_obj count = 0;
for (o = g->tobefnz; o != NULL; o = o->next) {
count++;
markobject(g, o);
@ -357,8 +357,8 @@ static l_mem markbeingfnz (global_State *g) {
** upvalues, as they have nothing to be checked. (If the thread gets an
** upvalue later, it will be linked in the list again.)
*/
static l_mem remarkupvals (global_State *g) {
l_mem work = 0;
static l_obj remarkupvals (global_State *g) {
l_obj work = 0;
lua_State *thread;
lua_State **p = &g->twups;
while ((thread = *p) != NULL) {
@ -662,8 +662,8 @@ static void propagatemark (global_State *g) {
}
static l_mem propagateall (global_State *g) {
l_mem work = 0;
static l_obj propagateall (global_State *g) {
l_obj work = 0;
while (g->gray) {
propagatemark(g);
work++;
@ -678,9 +678,9 @@ static l_mem propagateall (global_State *g) {
** inverts the direction of the traversals, trying to speed up
** convergence on chains in the same table.
*/
static l_mem convergeephemerons (global_State *g) {
static l_obj convergeephemerons (global_State *g) {
int changed;
l_mem work = 0;
l_obj work = 0;
int dir = 0;
do {
GCObject *w;
@ -715,8 +715,8 @@ static l_mem convergeephemerons (global_State *g) {
/*
** clear entries with unmarked keys from all weaktables in list 'l'
*/
static l_mem clearbykeys (global_State *g, GCObject *l) {
l_mem work = 0;
static l_obj clearbykeys (global_State *g, GCObject *l) {
l_obj work = 0;
for (; l; l = gco2t(l)->gclist) {
Table *h = gco2t(l);
Node *limit = gnodelast(h);
@ -737,8 +737,8 @@ static l_mem clearbykeys (global_State *g, GCObject *l) {
** clear entries with unmarked values from all weaktables in list 'l' up
** to element 'f'
*/
static l_mem clearbyvalues (global_State *g, GCObject *l, GCObject *f) {
l_mem work = 0;
static l_obj clearbyvalues (global_State *g, GCObject *l, GCObject *f) {
l_obj work = 0;
for (; l != f; l = gco2t(l)->gclist) {
Table *h = gco2t(l);
Node *n, *limit = gnodelast(h);
@ -1054,7 +1054,7 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
static void setpause (global_State *g) {
unsigned int pause = getgcparam(g->gcpause);
lu_mem threshold = g->marked / 8 * pause / 12;
l_mem debt = gettotalobjs(g) - threshold;
l_obj debt = gettotalobjs(g) - threshold;
if (debt > 0) debt = 0;
luaE_setdebt(g, debt);
}
@ -1306,7 +1306,7 @@ static void atomic2gen (lua_State *L, global_State *g) {
** total number of objects grows 'genminormul'%.
*/
static void setminordebt (global_State *g) {
luaE_setdebt(g, -(cast(l_mem, (gettotalobjs(g) / 100)) * g->genminormul));
luaE_setdebt(g, -(cast(l_obj, (gettotalobjs(g) / 100)) * g->genminormul));
}
@ -1431,8 +1431,8 @@ static void genstep (lua_State *L, global_State *g) {
if (g->lastatomic != 0) /* last collection was a bad one? */
stepgenfull(L, g); /* do a full step */
else {
l_mem majorbase = g->GCestimate; /* objects after last major collection */
l_mem majorinc = (majorbase / 100) * getgcparam(g->genmajormul);
l_obj majorbase = g->GCestimate; /* objects after last major collection */
l_obj majorinc = (majorbase / 100) * getgcparam(g->genmajormul);
if (g->GCdebt > 0 && gettotalobjs(g) > majorbase + majorinc) {
g->marked = 0;
fullgen(L, g); /* do a major collection */
@ -1512,8 +1512,8 @@ void luaC_freeallobjects (lua_State *L) {
}
static l_mem atomic (lua_State *L) {
l_mem work = 0;
static l_obj atomic (lua_State *L) {
l_obj work = 0;
global_State *g = G(L);
GCObject *origweak, *origall;
GCObject *grayagain = g->grayagain; /* save original list */
@ -1558,7 +1558,7 @@ static l_mem atomic (lua_State *L) {
static void sweepstep (lua_State *L, global_State *g,
int nextstate, GCObject **nextlist) {
if (g->sweepgc) {
l_mem olddebt = g->GCdebt;
l_obj olddebt = g->GCdebt;
g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);
g->GCestimate += g->GCdebt - olddebt; /* update estimate */
}
@ -1569,9 +1569,9 @@ static void sweepstep (lua_State *L, global_State *g,
}
static l_mem singlestep (lua_State *L) {
static l_obj singlestep (lua_State *L) {
global_State *g = G(L);
l_mem work;
l_obj work;
lua_assert(!g->gcstopem); /* collector is not reentrant */
g->gcstopem = 1; /* no emergency collections while collecting */
switch (g->gcstate) {
@ -1658,10 +1658,10 @@ void luaC_runtilstate (lua_State *L, int statesmask) {
*/
static void incstep (lua_State *L, global_State *g) {
int stepmul = (getgcparam(g->gcstepmul) | 1); /* avoid division by 0 */
l_mem debt = (g->GCdebt / 100) * stepmul;
l_mem stepsize = cast(l_mem, 1) << g->gcstepsize;
l_obj debt = (g->GCdebt / 100) * stepmul;
l_obj stepsize = cast(l_obj, 1) << g->gcstepsize;
do { /* repeat until pause or enough "credit" (negative debt) */
l_mem work = singlestep(L); /* perform one single step */
l_obj work = singlestep(L); /* perform one single step */
debt -= work;
} while (debt > -stepsize && g->gcstate != GCSpause);
if (g->gcstate == GCSpause)

14
llimits.h

@ -16,19 +16,21 @@
/*
** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count
** the total memory used by Lua (in bytes). Usually, 'size_t' and
** 'lu_mem' is an unsigned integer big enough to count the total memory
** used by Lua (in bytes). 'l_obj' is a signed integer big enough to
** count the total number of objects used by Lua. (It is negative due
** to the use of debt in several computations.) Usually, 'size_t' and
** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines.
*/
#if defined(LUAI_MEM) /* { external definitions? */
typedef LUAI_UMEM lu_mem;
typedef LUAI_MEM l_mem;
typedef LUAI_MEM l_obj;
#elif LUAI_IS32INT /* }{ */
typedef size_t lu_mem;
typedef ptrdiff_t l_mem;
typedef ptrdiff_t l_obj;
#else /* 16-bit ints */ /* }{ */
typedef unsigned long lu_mem;
typedef long l_mem;
typedef long l_obj;
#endif /* } */
@ -47,7 +49,7 @@ typedef signed char ls_byte;
#define MAX_LUMEM ((lu_mem)(~(lu_mem)0))
#define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1))
#define MAX_LMEM ((l_obj)(MAX_LUMEM >> 1))
#define MAX_INT INT_MAX /* maximum value of an int */

4
lstate.c

@ -86,8 +86,8 @@ static unsigned int luai_makeseed (lua_State *L) {
** set GCdebt to a new value keeping the value (totalobjs + GCdebt)
** invariant (and avoiding underflows in 'totalobjs')
*/
void luaE_setdebt (global_State *g, l_mem debt) {
l_mem tb = gettotalobjs(g);
void luaE_setdebt (global_State *g, l_obj debt) {
l_obj tb = gettotalobjs(g);
lua_assert(tb > 0);
if (debt < tb - MAX_LMEM)
debt = tb - MAX_LMEM; /* will make 'totalobjs == MAX_LMEM' */

10
lstate.h

@ -249,10 +249,10 @@ typedef struct CallInfo {
typedef struct global_State {
lua_Alloc frealloc; /* function to reallocate memory */
void *ud; /* auxiliary data to 'frealloc' */
l_mem totalbytes; /* number of bytes currently allocated */
l_mem totalobjs; /* total number of objects allocated - GCdebt */
l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
lu_mem marked; /* number of objects marked in a GC cycle */
lu_mem totalbytes; /* number of bytes currently allocated */
l_obj totalobjs; /* total number of objects allocated - GCdebt */
l_obj GCdebt; /* bytes allocated not yet compensated by the collector */
l_obj marked; /* number of objects marked in a GC cycle */
lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
lu_mem lastatomic; /* see function 'genstep' in file 'lgc.c' */
stringtable strt; /* hash table for strings */
@ -391,7 +391,7 @@ union GCUnion {
#define gettotalobjs(g) ((g)->totalobjs + (g)->GCdebt)
LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
LUAI_FUNC void luaE_setdebt (global_State *g, l_obj debt);
LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
LUAI_FUNC void luaE_freeCI (lua_State *L);

16
ltests.c

@ -531,7 +531,7 @@ static void checkobject (global_State *g, GCObject *o, int maybedead,
}
static lu_mem checkgraylist (global_State *g, GCObject *o) {
static l_obj checkgraylist (global_State *g, GCObject *o) {
int total = 0; /* count number of elements in the list */
cast_void(g); /* better to keep it if we need to print an object */
while (o) {
@ -560,7 +560,7 @@ static lu_mem checkgraylist (global_State *g, GCObject *o) {
/*
** Check objects in gray lists.
*/
static lu_mem checkgrays (global_State *g) {
static l_obj checkgrays (global_State *g) {
int total = 0; /* count number of elements in all lists */
if (!keepinvariant(g)) return total;
total += checkgraylist(g, g->gray);
@ -577,7 +577,7 @@ static lu_mem checkgrays (global_State *g) {
** 'count' and check its TESTBIT. (It must have been previously set by
** 'checkgraylist'.)
*/
static void incifingray (global_State *g, GCObject *o, lu_mem *count) {
static void incifingray (global_State *g, GCObject *o, l_obj *count) {
if (!keepinvariant(g))
return; /* gray lists not being kept in these phases */
if (o->tt == LUA_VUPVAL) {
@ -594,10 +594,10 @@ static void incifingray (global_State *g, GCObject *o, lu_mem *count) {
}
static lu_mem checklist (global_State *g, int maybedead, int tof,
static l_obj checklist (global_State *g, int maybedead, int tof,
GCObject *newl, GCObject *survival, GCObject *old, GCObject *reallyold) {
GCObject *o;
lu_mem total = 0; /* number of object that should be in gray lists */
l_obj total = 0; /* number of object that should be in gray lists */
for (o = newl; o != survival; o = o->next) {
checkobject(g, o, maybedead, G_NEW);
incifingray(g, o, &total);
@ -626,8 +626,8 @@ int lua_checkmemory (lua_State *L) {
global_State *g = G(L);
GCObject *o;
int maybedead;
lu_mem totalin; /* total of objects that are in gray lists */
lu_mem totalshould; /* total of objects that should be in gray lists */
l_obj totalin; /* total of objects that are in gray lists */
l_obj totalshould; /* total of objects that should be in gray lists */
if (keepinvariant(g)) {
assert(!iswhite(g->mainthread));
assert(!iswhite(gcvalue(&g->l_registry)));
@ -1033,7 +1033,7 @@ static int query_inc (lua_State *L) {
lua_pushinteger(L, g->GCdebt);
lua_pushinteger(L, getgcparam(g->gcpause));
lua_pushinteger(L, getgcparam(g->gcstepmul));
lua_pushinteger(L, cast(l_mem, 1) << g->gcstepsize);
lua_pushinteger(L, cast(l_obj, 1) << g->gcstepsize);
return 5;
}

Loading…
Cancel
Save