Classes and Instances 27
This commit is contained in:
parent
6d5dff6e3d
commit
d9dc02b92f
13 changed files with 271 additions and 63 deletions
|
|
@ -12,6 +12,7 @@ import clox.value;
|
|||
import clox.vm;
|
||||
import clox.container.dynarray;
|
||||
import clox.container.table;
|
||||
import conf = clox.conf;
|
||||
|
||||
void collectGarbage(){
|
||||
debug(disableGC)
|
||||
|
|
@ -34,14 +35,23 @@ void collectGarbage(){
|
|||
markRoots();
|
||||
traceReferences();
|
||||
vm.strings.removeWhite();
|
||||
auto sweepStats = sweep();
|
||||
debug(logGC)
|
||||
printf(colour!(" -- GC end %zu/%zu\n", Colour.Yellow).ptr, sweepStats.collected, sweepStats.total);
|
||||
debug(memTrace){
|
||||
import clox.memorydbg : totalGCScanned, totalGCollected, totalGCollections;
|
||||
auto sweepStats = sweep();
|
||||
totalGCollections++;
|
||||
totalGCollected += sweepStats.collected;
|
||||
totalGCScanned += sweepStats.total;
|
||||
debug(logGC)
|
||||
printf(colour!(" -- GC end %zu/%zu\n", Colour.Yellow).ptr, sweepStats.collected, sweepStats.total);
|
||||
} else {
|
||||
sweep();
|
||||
}
|
||||
debug(stressGC) {} else {
|
||||
vm.nextGC = cast(size_t)(vm.bytesAllocated * 2);
|
||||
vm.nextGC = conf.nextGCGrowthFunc(vm.bytesAllocated);
|
||||
}
|
||||
}
|
||||
void mark(Obj* object){
|
||||
void mark(O)(O* o) if(is(O == Obj) || __traits(hasMember, O, "obj")){
|
||||
Obj* object = cast(Obj*)o;
|
||||
if(object is null || object.isMarked)
|
||||
return;
|
||||
debug(logGC)
|
||||
|
|
@ -53,14 +63,14 @@ void mark(Value value){
|
|||
if(value.isType(Value.Type.Obj))
|
||||
mark(value.asObj);
|
||||
}
|
||||
void mark(Table!(Obj.String*, Value) table){
|
||||
foreach(entry; table.pool){
|
||||
mark(cast(Obj*)entry.key);
|
||||
void mark(K, V)(ref Table!(K, V) table){
|
||||
foreach(ref entry; table.pool){
|
||||
mark(entry.key);
|
||||
mark(entry.val);
|
||||
}
|
||||
}
|
||||
void mark(T)(DynArray!T darray){
|
||||
foreach(item; darray[]){
|
||||
void mark(T)(ref DynArray!T darray){
|
||||
foreach(ref item; darray[]){
|
||||
mark(item);
|
||||
}
|
||||
}
|
||||
|
|
@ -69,10 +79,10 @@ private void markRoots(){
|
|||
mark(*slot);
|
||||
}
|
||||
foreach(frame; vm.frames[0 .. vm.frameCount]){
|
||||
mark(cast(Obj*)frame.closure);
|
||||
mark(frame.closure);
|
||||
}
|
||||
for(Obj.Upvalue* upvalue = vm.openUpvalues; upvalue !is null; upvalue = upvalue.next){
|
||||
mark(cast(Obj*)upvalue);
|
||||
mark(upvalue);
|
||||
}
|
||||
mark(vm.globals);
|
||||
markCompilerRoots();
|
||||
|
|
@ -89,14 +99,23 @@ private void blacken(Obj* object){
|
|||
break;
|
||||
case Obj.Type.Function:
|
||||
Obj.Function* func = object.asFunc;
|
||||
mark(cast(Obj*)func.name);
|
||||
mark(func.name);
|
||||
mark(func.chunk.constants);
|
||||
break;
|
||||
case Obj.Type.Closure:
|
||||
Obj.Closure* closure = object.asClosure;
|
||||
mark(cast(Obj*)closure.func);
|
||||
mark(closure.func);
|
||||
foreach(upvalue; closure.upvalues)
|
||||
mark(cast(Obj*)upvalue);
|
||||
mark(upvalue);
|
||||
break;
|
||||
case Obj.Type.Class:
|
||||
Obj.Class* cls = object.asClass;
|
||||
mark(cls.name);
|
||||
break;
|
||||
case Obj.Type.Instance:
|
||||
Obj.Instance* ins = object.asInstance;
|
||||
mark(ins.cls);
|
||||
mark(ins.fields);
|
||||
break;
|
||||
case Obj.Type.None: assert(0);
|
||||
}
|
||||
|
|
@ -108,11 +127,13 @@ private void traceReferences(){
|
|||
}
|
||||
}
|
||||
private auto sweep(){
|
||||
size_t nCollected, total;
|
||||
debug(memTrace)
|
||||
size_t nCollected, total;
|
||||
Obj* previous = null;
|
||||
Obj* object = vm.objects;
|
||||
while(object !is null){
|
||||
total++;
|
||||
debug(memTrace)
|
||||
total++;
|
||||
if(object.isMarked){
|
||||
object.isMarked = false;
|
||||
previous = object;
|
||||
|
|
@ -126,9 +147,11 @@ private auto sweep(){
|
|||
vm.objects = object;
|
||||
}
|
||||
freeObject(unreached);
|
||||
nCollected++;
|
||||
debug(memTrace)
|
||||
nCollected++;
|
||||
}
|
||||
}
|
||||
return tuple!("collected", "total")(nCollected, total);
|
||||
debug(memTrace)
|
||||
return tuple!("collected", "total")(nCollected, total);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue