package s3.core.services.memory;

import ovm.core.services.memory.VM_Address;
import ovm.core.domain.Blueprint;
import ovm.util.NumberRanges;
import ovm.core.execution.Native;

public class SpaceScanner{
    private VM_Address ptr;
    private VM_Address end;
    public SpaceScanner(VM_Address base, VM_Address end){
	ptr = base;
	this.end = end;
    }

    public void updateExtent(VM_Address newend){
	end = newend;
    }

    public boolean hasNext(){
	//Native.print_string("ptr: ");
	//Native.print_int(ptr.asInt());
	//Native.print_string(" end: ");
	//Native.print_int(end.asInt());
	//Native.print_string("\n");
	return end.sGT(ptr);
    }

    public VM_Address next(){
	VM_Address retval = ptr;
	//	ptr = ptr.add(ptr.asOop().getBlueprint().getVariableSize(ptr.asOop()));
	//Native.print_string("Getting ptr as oop\n");
	ovm.core.domain.Oop oopptr = ptr.asOop();
	//Native.print_string("Getting blueprint\n");
	Blueprint bp = oopptr.getBlueprint();
	if(bp == null){
	    Native.print_string("BP is null for address ");
	    Native.print_int(ptr.asInt());
	    Native.print_string("\n");
	}
	int size = bp.getVariableSize(oopptr);
	//Native.print_string("Getting size. It's ");
	//Native.print_int(size);
	//Native.print_string("\n");
	ptr = ptr.add(size);
	return retval;
    }
}
