-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryManagementSystem.java
95 lines (81 loc) · 3.64 KB
/
MemoryManagementSystem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.util.Arrays;
public class MemoryManagementSystem{
// ---------------------- fields ----------------------
public String[] secondaryMemory;
private boolean useLRU;
private LinkedList.Link<Page>[] mainCurrePages; //pointer array used to manage main memory
private Queue<Page> mainMemory; //main memory data structure
// ---------------------- constructors ----------------------
public MemoryManagementSystem(int mainMemorySize, int secondaryMemorySize, boolean useLRU) {
this.secondaryMemory=new String[secondaryMemorySize];
this.mainMemory=new QueueAsLinkedList<>();
this.mainCurrePages=new LinkedList.Link[secondaryMemorySize];
this.useLRU=useLRU;
for(int i=0;i<secondaryMemorySize;i++) //initializing main&secondary memory data structures
{
if(i<mainMemorySize) //adding elements to the queue as maximum main memory size
{
Page p=new Page("",i); //creating empty pages
mainCurrePages[i]=this.mainMemory.enqueue(p); //adding empty pages to the pointers array
}
else this.mainCurrePages[i]=null; //any elements currently not in the main memory are pointed to null.
this.secondaryMemory[i]=""; //initializing empty string for secondary memory
}
}
@Override
public String toString() {
return "secondaryMemory=" + Arrays.toString(secondaryMemory);
}
//function returns page data
public String read(int index) {
LinkedList.Link<Page> toRead=getMainCurrePages()[index];
if(toRead!=null) //checks if requested data is currently in the main memory
{
if(isUseLRU()) //checks LRU/FIFO mode toggling
{
if(toRead.getPrev() == null) { //checks if requested data is the queue head's
getMainMemory().dequeue();
getMainCurrePages()[index] = getMainMemory().enqueue(toRead.getData()); //adding read data to the end of the queue(LRU method)
}
else if(!(toRead.getNext()==null)) { //checks if requested data is the queue tail
toRead.getNext().setPrev(toRead.getPrev()); //changing nodes order "cutting" requested data from the queue
if(toRead.getPrev()!=null)
toRead.getPrev().setNext(toRead.getNext());//changing nodes order "cutting" requested data from the queue
getMainCurrePages()[index]=getMainMemory().enqueue(toRead.getData()); //adding read data to the end of the queue(LRU method)
}
}
return toRead.getData().getPageData();
}
else //requested data is not in the queue
{
Page toDequeue = getMainMemory().dequeue(); //removing queues head page
int indToDequeue = toDequeue.getIndex(); //getting page index
getMainCurrePages()[indToDequeue] = null; //pointing removed page to null (removed from the queue)
getSecondaryMemory()[indToDequeue] = toDequeue.getPageData(); //saving back removed page to the secondary memory
//adding requested data to the main memory
Page p = new Page(getSecondaryMemory()[index], index);
getMainCurrePages()[index]=getMainMemory().enqueue(p); //adding to the queue and to the pointers array
return p.getPageData();
}
}
//function writes data into the memory
public void write(int index, char c) {
String s = read(index); //calling the read function to deal with the whole in\out of memories methods
Object o=getMainCurrePages()[index].getData(); //given page is already in the memory
Page p=(Page)o; //casting to page
p.setPageData(s+c); //adding given data into the page data
}
//------utility methods (getters)-------------
private Queue<Page> getMainMemory() {
return this.mainMemory;
}
private String[] getSecondaryMemory() {
return this.secondaryMemory;
}
private LinkedList.Link[] getMainCurrePages(){
return this.mainCurrePages;
}
private boolean isUseLRU(){
return this.useLRU;
}
}