import java.util.ArrayList;
import java.util.List;
public class Memory {
public static final int memorySize = 4;// 内存是四个物理块
private List<Integer> list = new ArrayList<Integer>(memorySize);
private Swapping swapping;
public Memory(Swapping swapping) {
super();
this.swapping = swapping;
this.swapping.setMemory(this);
}
// -2表示内存还没满
// -1表示原来的页面本来就在内存里
// >0表示将几号页面换出了
public int loadPage(Integer chunk) {
int index = swapping.getLoacalLoadPage(chunk);
// System.out.println("index--->>>"+index);
Integer chunk1 = null;
try {
chunk1 = list.get(index);
} catch (Exception e) {
}
this.load(chunk, index);
if (getUsedMemoryCount() <= Memory.memorySize) {
if (getUsedMemoryCount() == Memory.memorySize && index == -1) {
return -1;
}
return -2;
} else {
// System.out.println("index--->>>"+index);
/*
* if(index==-1){
*
* }
*/
// System.out.println("chunk1--->>>"+chunk1);
list.remove(chunk1);
return chunk1;
}
}
public int getIndexOfExistedChunk(Integer chunk) {
return list.indexOf(chunk);
}
public int getUsedMemoryCount() {
return list.size();
}
public void load(int chunk, int index) {
if (index >= 0) {
list.add(index, chunk);
}
}
/*
* public static void main(String[] args) { Swapping swapping=new
* LRUSwapping(); Memory memory=new Memory(swapping);
* System.out.println(memory.loadPage(4));
* System.out.println(memory.loadPage(3));
* System.out.println(memory.loadPage(2));
* System.out.println(memory.loadPage(1));
* System.out.println(memory.loadPage(5)); }
*/
}