package s3.services.memory.mostlyCopying;

import ovm.core.execution.Native;
import ovm.core.services.memory.ExtentWalker;
import ovm.core.services.memory.VM_Address;

/*
 * A debugging mixin.  Make unallocated blocks unreadable so that bad
 * memory accesses result in SEGVs
 */
public class PageCheckingManager extends Manager {
    /*
     * If we never give blocks back to the freelist, a GCed block will
     * never become readable again.  We can get through the regession
     * tests without returning blocks to the freelist, but something
     * like SpecJVM is another story.
     */
    static final boolean REALLY_FREE = true;

    final int nblocks;
    
    public PageCheckingManager(String heapSize, String blockSize,
			       String dumpOop) {
	super(heapSize, "4k", dumpOop);
	nblocks = this.heapSize/this.blockSize;
    }

    int pagesize;

    public void boot() {
	super.boot();
	pagesize = NativeHelpers.getpagesize();
	block.walkFree(protector);
    }

    VM_Address getBlock(int count) {
	VM_Address ret = super.getBlock(count);
	if (ret != null)
	    Native.mprotect(ret, count*pagesize, PROT_READ|PROT_WRITE);
	return ret;
    }

    private final ExtentWalker protector1 = new ExtentWalker() {
	    public void walk(VM_Address start, VM_Address end) {
		Native.mprotect(start, end.asInt() - start.asInt(), 0);
	    }
	};
    private final BlockWalker protector = new BlockWalker(protector1);

    public void doGC() {
	super.doGC();
	block.walkFree(protector);
    }
}    
    
	
