Created
July 31, 2012 19:00
-
-
Save sumtyme/3219510 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface VirtualMemory { | |
String read(int address); | |
void write(int address, String value); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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