-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRU.hpp
More file actions
156 lines (145 loc) · 3.18 KB
/
Copy pathLRU.hpp
File metadata and controls
156 lines (145 loc) · 3.18 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#ifndef LRU
#define LRU
#include "cache.hpp"
#include <map>
struct pageNode
{
page thisPage;
pageNode *next;
pageNode *prev;
pageNode(ulong address, cacheState initialState) : thisPage(address, initialState), next(NULL), prev(NULL) {}
pageNode(){}
};
class LRUCache : public Cache
{
protected:
pageNode *pageHead;
pageNode *pageTail;
std::map<ulong, pageNode*> pageTable;
public:
LRUCache(ulong cacheSize, ulong blockSize) : Cache(cacheSize, blockSize), pageHead(NULL), pageTail(NULL) {}
virtual void onMiss(ulong address, cacheState state)
{
missCount++;
insert(address, state);
}
virtual void onHit(ulong, cacheState )
{
hitCount++;
}
virtual void handleEviction(Cache *upperCache, Cache* , page replacedPage)
{
if(replacedPage.state == 'M')
{
//messy state, need to writeback
upperCache->write(replacedPage.addr);
}
}
bool inCache(ulong address)
{
return pageTable.find(address) != pageTable.end();
}
page evict(ulong address)
{
pageNode *replacedPage = pageTable[address];
pageTable.erase(replacedPage->thisPage.addr);
page returnPage = replacedPage->thisPage;
if(pageTail == pageHead)
{
pageHead=NULL;
pageTail=NULL;
}
else if(replacedPage == pageTail)
{
pageTail->prev->next = NULL;
pageTail = pageTail->prev;
}
else if(replacedPage == pageHead)
{
pageHead->next->prev = NULL;
pageHead = pageHead->next;
}
else
{
replacedPage->prev->next = replacedPage->next;
replacedPage->next->prev = replacedPage->prev;
}
currentSize_--;
delete replacedPage;
return returnPage;
}
virtual void insert(ulong address, cacheState state)
{
pageNode *newPage = new pageNode(address, state);
newPage->next = pageHead;
pageTable[address] = newPage;
if(cacheSize_ == currentSize_)
{
//something has to be replaced
page victim = evict(pageTail->thisPage.addr);
handleEviction(upperCache, lowerCache, victim);
pageHead->prev = newPage;
pageHead = newPage;
}
else
{
if(pageHead == NULL)
{
pageHead = pageTail = newPage;
}
else
{
pageHead->prev = newPage;
pageHead = newPage;
}
}
currentSize_++;
}
void replace(ulong address, cacheState state)
{
if(!inCache(address) || pageTable[address]->thisPage.state == INVALID)
{
//not currently in the cache
//std::cout << "onMiss" << std::endl;
onMiss(address, state);
}
else
{
//std::cout << "beforehit" << std::endl;
onHit(address, state);
//std::cout << "afterhit" << std::endl;
pageNode *accessedPage = pageTable[address];
if(state == MESSY)
accessedPage->thisPage.state = state;
if(accessedPage != pageHead)
{
if(accessedPage == pageTail)
{
pageTail = pageTail->prev;
}
else
{
accessedPage->next->prev = accessedPage->prev;
}
accessedPage->prev->next = accessedPage->next;
accessedPage->prev = NULL;
accessedPage->next = pageHead;
pageHead->prev = accessedPage;
pageHead = accessedPage;
}
}
}
virtual void read(ulong address)
{
readCount++;
if(!inCache(address))
upperCache->read(address);
replace(address, CLEAN);
}
virtual void write(ulong address)
{
writeCount++;
replace(address, MESSY);
}
};
#endif