Skip to content

Instantly share code, notes, and snippets.

@sumtyme
Created July 31, 2012 19:00
Show Gist options
  • Save sumtyme/3219510 to your computer and use it in GitHub Desktop.
Save sumtyme/3219510 to your computer and use it in GitHub Desktop.
public abstract class BackedVirtualMemory extends SimpleVirtualMemory {
protected int currentPageIndex = 0;
protected int pageSize;
protected int maxPages;
public BackedVirtualMemory(){
this(100, 10);
}
public BackedVirtualMemory(int pageSize, int maxPages){
super.currentPage = new String[pageSize];
this.pageSize = pageSize;
this.maxPages = maxPages;
}
public String read(int address){
handlePageFault(address);
return super.read(address % pageSize);
}
public void write(int address, String value){
handlePageFault(address);
super.write(address % pageSize, value);
}
protected void handlePageFault(int address){
int correctPageIndex = address / pageSize;
if(correctPageIndex == currentPageIndex){
return; // Nothing to do as current page is correct page
}
writeOut(currentPageIndex, super.currentPage);
super.currentPage = readIn(correctPageIndex);
currentPageIndex = correctPageIndex;
}
protected abstract void writeOut(int pageNumber, String[] page);
protected abstract String[] readIn(int pageNumber);
}
public class FileBackedVirtualMemory extends BackedVirtualMemory {
@Override
protected void writeOut(int pageNumber, String[] page) {
// Write this page out to a file, doesn't matter how or where so long as we can
// use the pageNumber to find it again.
}
@Override
protected String[] readIn(int pageNumber) {
// Read the correct file into an array and return it.
return null;
}
}
public class MemoryBackedVirtualMemory extends BackedVirtualMemory {
String[][] pages;
public MemoryBackedVirtualMemory(){
super();
pages = new String[super.maxPages][];
}
public MemoryBackedVirtualMemory(int pageSize, int maxPages){
super(pageSize, maxPages);
pages = new String[maxPages][];
}
@Override
protected void writeOut(int pageNumber, String[] page) {
pages[pageNumber] = page;
}
@Override
protected String[] readIn(int pageNumber) {
if(pages[pageNumber] == null){
pages[pageNumber] = new String[super.pageSize];
}
return pages[pageNumber];
}
}
public class SimpleVirtualMemory implements VirtualMemory{
protected static final int DEFAULT_PAGE_SIZE = 100;
protected String[] currentPage;
public SimpleVirtualMemory(){
currentPage = new String[DEFAULT_PAGE_SIZE * 10];
}
public SimpleVirtualMemory(int size){
currentPage = new String[size];
}
public String read(int address){
return currentPage[address];
}
public void write(int address, String value){
currentPage[address] = value;
}
}
public interface VirtualMemory {
String read(int address);
void write(int address, String value);
}
public class VirtualMemoryTest {
public static void main(String[] args) {
int pageSize = 5;
int maxPages = 10;
VirtualMemory memory = new MemoryBackedVirtualMemory(pageSize, maxPages);
for(int i = 0; i < pageSize * maxPages; i++){
memory.write(i, "Value at " + i);
}
for(int i = 0; i < pageSize * maxPages; i++){
System.out.println(memory.read(i));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment