ways to measure number of `blocks' for GC + details

This commit is contained in:
Roberto Ierusalimschy 1999-11-10 13:39:35 -02:00
parent 53fb65d394
commit d915cf4f9d
6 changed files with 35 additions and 30 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lfunc.c,v 1.12 1999/10/04 17:51:04 roberto Exp roberto $ ** $Id: lfunc.c,v 1.13 1999/10/14 19:46:57 roberto Exp roberto $
** Auxiliary functions to manipulate prototypes and closures ** Auxiliary functions to manipulate prototypes and closures
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -11,8 +11,8 @@
#include "lmem.h" #include "lmem.h"
#include "lstate.h" #include "lstate.h"
#define gcsizeproto(p) 5 /* approximate "weight" for a prototype */ #define gcsizeproto(p) numblocks(0, sizeof(TProtoFunc))
#define gcsizeclosure(c) 1 /* approximate "weight" for a closure */ #define gcsizeclosure(c) numblocks(c->nelems, sizeof(Closure))
@ -21,8 +21,8 @@ Closure *luaF_newclosure (int nelems) {
c->next = L->rootcl; c->next = L->rootcl;
L->rootcl = c; L->rootcl = c;
c->marked = 0; c->marked = 0;
L->nblocks += gcsizeclosure(c);
c->nelems = nelems; c->nelems = nelems;
L->nblocks += gcsizeclosure(c);
return c; return c;
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.h,v 1.34 1999/10/19 13:33:22 roberto Exp roberto $ ** $Id: lobject.h,v 1.35 1999/11/04 17:22:26 roberto Exp roberto $
** Type definitions for Lua objects ** Type definitions for Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -43,7 +43,17 @@ typedef unsigned char Byte; /* unsigned 8 bits */
#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
typedef unsigned int IntPoint; /* unsigned with same size as a pointer (for hashing) */
/* convertion of pointer to int (for hashing only) */
#define IntPoint(p) ((unsigned int)(p))
/*
** number of `blocks' for garbage collection: each reference to other
** objects count 1, and each 32 bytes of `raw' memory count 1; we add
** 2 to the total as a minimum (and also to count the overhead of malloc)
*/
#define numblocks(o,b) ((o)+(b)/32+2)
/* /*

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstate.c,v 1.14 1999/10/04 17:51:04 roberto Exp $ ** $Id: lstate.c,v 1.15 1999/10/14 17:53:35 roberto Exp roberto $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -10,6 +10,7 @@
#include "lgc.h" #include "lgc.h"
#include "llex.h" #include "llex.h"
#include "lmem.h" #include "lmem.h"
#include "lref.h"
#include "lstate.h" #include "lstate.h"
#include "lstring.h" #include "lstring.h"
#include "ltm.h" #include "ltm.h"
@ -41,13 +42,15 @@ void lua_open (void) {
L->IMtable = NULL; L->IMtable = NULL;
L->refArray = NULL; L->refArray = NULL;
L->refSize = 0; L->refSize = 0;
L->GCthreshold = GARBAGE_BLOCK; L->refFree = NONEXT;
L->nblocks = 0; L->nblocks = 0;
L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
luaD_init(); luaD_init();
luaS_init(); luaS_init();
luaX_init(); luaX_init();
luaT_init(); luaT_init();
luaB_predefine(); luaB_predefine();
L->GCthreshold = L->nblocks*4;
} }
@ -70,4 +73,3 @@ void lua_close (void) {
L = NULL; L = NULL;
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstate.h,v 1.20 1999/10/04 17:51:04 roberto Exp roberto $ ** $Id: lstate.h,v 1.21 1999/11/04 17:22:26 roberto Exp roberto $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -14,8 +14,6 @@
#include "luadebug.h" #include "luadebug.h"
#define GARBAGE_BLOCK 150
typedef int StkId; /* index to stack elements */ typedef int StkId; /* index to stack elements */
@ -50,13 +48,6 @@ typedef struct stringtable {
} stringtable; } stringtable;
enum Status {LOCK, HOLD, FREE, COLLECTED};
struct ref {
TObject o;
enum Status status;
};
struct lua_State { struct lua_State {
/* thread-specific state */ /* thread-specific state */
@ -82,6 +73,7 @@ struct lua_State {
int last_tag; /* last used tag in IMtable */ int last_tag; /* last used tag in IMtable */
struct ref *refArray; /* locked objects */ struct ref *refArray; /* locked objects */
int refSize; /* size of refArray */ int refSize; /* size of refArray */
int refFree; /* list of free positions in refArray */
unsigned long GCthreshold; unsigned long GCthreshold;
unsigned long nblocks; /* number of 'blocks' currently allocated */ unsigned long nblocks; /* number of 'blocks' currently allocated */
}; };

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstring.c,v 1.25 1999/10/19 13:33:22 roberto Exp roberto $ ** $Id: lstring.c,v 1.26 1999/11/04 17:22:26 roberto Exp roberto $
** String table (keeps all strings handled by Lua) ** String table (keeps all strings handled by Lua)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -15,7 +15,8 @@
#define gcsizestring(l) (1+(l/64)) /* "weight" for a string with length 'l' */ #define gcsizestring(l) numblocks(0, sizeof(TaggedString)+l)
#define gcsizeudata gcsizestring(0)
@ -109,7 +110,7 @@ static TaggedString *newone_u (void *buff, int tag, unsigned long h) {
ts->u.d.value = buff; ts->u.d.value = buff;
ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag; ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag;
ts->constindex = -1; /* tag -> this is a userdata */ ts->constindex = -1; /* tag -> this is a userdata */
L->nblocks++; L->nblocks += gcsizeudata;
return ts; return ts;
} }
@ -147,7 +148,7 @@ TaggedString *luaS_newlstr (const char *str, long l) {
TaggedString *luaS_createudata (void *udata, int tag) { TaggedString *luaS_createudata (void *udata, int tag) {
unsigned long h = (IntPoint)udata; unsigned long h = IntPoint(udata);
stringtable *tb = &L->string_root[(h%NUM_HASHUDATA)+NUM_HASHSTR]; stringtable *tb = &L->string_root[(h%NUM_HASHUDATA)+NUM_HASHSTR];
int h1 = h%tb->size; int h1 = h%tb->size;
TaggedString *ts; TaggedString *ts;
@ -175,7 +176,7 @@ TaggedString *luaS_newfixedstring (const char *str) {
void luaS_free (TaggedString *t) { void luaS_free (TaggedString *t) {
if (t->constindex == -1) /* is userdata? */ if (t->constindex == -1) /* is userdata? */
L->nblocks--; L->nblocks -= gcsizeudata;
else { /* is string */ else { /* is string */
L->nblocks -= gcsizestring(t->u.s.len); L->nblocks -= gcsizestring(t->u.s.len);
luaM_free(t->u.s.gv); luaM_free(t->u.s.gv);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.c,v 1.27 1999/10/19 13:33:22 roberto Exp roberto $ ** $Id: ltable.c,v 1.28 1999/10/26 10:53:40 roberto Exp roberto $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -26,7 +26,7 @@
#include "lua.h" #include "lua.h"
#define gcsize(n) (1+(n/16)) #define gcsize(n) numblocks(n*2, sizeof(Hash))
@ -48,16 +48,16 @@ Node *luaH_mainposition (const Hash *t, const TObject *key) {
h = tsvalue(key)->hash; h = tsvalue(key)->hash;
break; break;
case LUA_T_ARRAY: case LUA_T_ARRAY:
h = (IntPoint)avalue(key); h = IntPoint(avalue(key));
break; break;
case LUA_T_PROTO: case LUA_T_PROTO:
h = (IntPoint)tfvalue(key); h = IntPoint(tfvalue(key));
break; break;
case LUA_T_CPROTO: case LUA_T_CPROTO:
h = (IntPoint)fvalue(key); h = IntPoint(fvalue(key));
break; break;
case LUA_T_CLOSURE: case LUA_T_CLOSURE:
h = (IntPoint)clvalue(key); h = IntPoint(clvalue(key));
break; break;
default: default:
lua_error("unexpected type to index table"); lua_error("unexpected type to index table");