Added a counter of the total number of existing objects
It may simplify the control of the garbage collector.
This commit is contained in:
2
lgc.c
2
lgc.c
@@ -259,6 +259,7 @@ GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, size_t offset) {
|
|||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
char *p = cast_charp(luaM_newobject(L, novariant(tt), sz));
|
char *p = cast_charp(luaM_newobject(L, novariant(tt), sz));
|
||||||
GCObject *o = cast(GCObject *, p + offset);
|
GCObject *o = cast(GCObject *, p + offset);
|
||||||
|
g->totalobjs++;
|
||||||
o->marked = luaC_white(g);
|
o->marked = luaC_white(g);
|
||||||
o->tt = tt;
|
o->tt = tt;
|
||||||
o->next = g->allgc;
|
o->next = g->allgc;
|
||||||
@@ -768,6 +769,7 @@ static void freeupval (lua_State *L, UpVal *uv) {
|
|||||||
|
|
||||||
|
|
||||||
static void freeobj (lua_State *L, GCObject *o) {
|
static void freeobj (lua_State *L, GCObject *o) {
|
||||||
|
G(L)->totalobjs--;
|
||||||
switch (o->tt) {
|
switch (o->tt) {
|
||||||
case LUA_VPROTO:
|
case LUA_VPROTO:
|
||||||
luaF_freeproto(L, gco2p(o));
|
luaF_freeproto(L, gco2p(o));
|
||||||
|
|||||||
2
lstate.c
2
lstate.c
@@ -279,6 +279,7 @@ static void close_state (lua_State *L) {
|
|||||||
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
|
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
|
||||||
freestack(L);
|
freestack(L);
|
||||||
lua_assert(gettotalbytes(g) == sizeof(LG));
|
lua_assert(gettotalbytes(g) == sizeof(LG));
|
||||||
|
lua_assert(g->totalobjs == 1);
|
||||||
(*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
|
(*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -387,6 +388,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
|
|||||||
g->weak = g->ephemeron = g->allweak = NULL;
|
g->weak = g->ephemeron = g->allweak = NULL;
|
||||||
g->twups = NULL;
|
g->twups = NULL;
|
||||||
g->totalbytes = sizeof(LG);
|
g->totalbytes = sizeof(LG);
|
||||||
|
g->totalobjs = 1;
|
||||||
g->GCdebt = 0;
|
g->GCdebt = 0;
|
||||||
g->lastatomic = 0;
|
g->lastatomic = 0;
|
||||||
setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */
|
setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */
|
||||||
|
|||||||
1
lstate.h
1
lstate.h
@@ -250,6 +250,7 @@ typedef struct global_State {
|
|||||||
lua_Alloc frealloc; /* function to reallocate memory */
|
lua_Alloc frealloc; /* function to reallocate memory */
|
||||||
void *ud; /* auxiliary data to 'frealloc' */
|
void *ud; /* auxiliary data to 'frealloc' */
|
||||||
l_mem totalbytes; /* number of bytes currently allocated - GCdebt */
|
l_mem totalbytes; /* number of bytes currently allocated - GCdebt */
|
||||||
|
l_mem totalobjs; /* total number of objects allocated */
|
||||||
l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
|
l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
|
||||||
lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
|
lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
|
||||||
lu_mem lastatomic; /* see function 'genstep' in file 'lgc.c' */
|
lu_mem lastatomic; /* see function 'genstep' in file 'lgc.c' */
|
||||||
|
|||||||
Reference in New Issue
Block a user