-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathset.h
More file actions
186 lines (159 loc) · 4.83 KB
/
set.h
File metadata and controls
186 lines (159 loc) · 4.83 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include "queryRecord.h"
#include "dll.h"
class Set
{
public:
int index;
int n_blocks;
virtual int search(QueryRecord* q) //return nullptr if not-found(miss), or corresponding Block* if found
{
std::cout<< "entered Base type set search\n";
return -1;
}
virtual void writeExistingToCache(QueryRecord* q)
{
std::cout<< "entered Base type set writeExistingToCache\n";
return;
}
virtual bool writeNewToCache(QueryRecord* q, bool isWrite)
{
std::cout<< "entered Base type set writeNewToCache\n";
return false;
}
virtual void updateMRU(int tag)
{
std::cout<< "entered Base type set updateMRU\n";
return;
}
virtual ~Set()
{
}
};
class LRUSet : public Set
{
DLL list; //DLL contains only valid entries
std::unordered_map<int, Node*> table; //map from tag to Node containing Block, use for search
public:
LRUSet(int i, int n)
{
// list = new DLL();
index = i;
n_blocks = n;
}
//For new allocation, DLL size == n_blocks,
//then need to evict,
//otherwise search for empty space
void updateMRU(int tag) override
{
Node* blockNode = table[tag];
list.moveToHead(blockNode);
}
int search (QueryRecord* q) override //return nullptr if not-found(miss), or corresponding Block* if found
{
// std::cout<< "entered LRU set search\n";
int tag = q->tag;
if (table.find(tag) != table.end())
{
updateMRU(tag);
return table[tag]->block.getTag();
}
return -1;
}
bool writeNewToCache(QueryRecord* q, bool isWrite) override
{
//when tag was already not present
// std::cout<< "entered LRU writeNewToCache\n";
bool dirtyEviction = false;
if (list.getSize() == n_blocks) //evict LRU
{
int LRUtag = list.getLRUTag();
dirtyEviction = list.getLRUDirty();
list.removeFromTail();
// delete table[LRUtag];
table[LRUtag] = nullptr;
table.erase(LRUtag);
}
// Node* newNode = new Node(Block(q->tag, isWrite)); //can remove bytes field from block!
list.insertAtHead(new Node(Block(q->tag, isWrite)));
table[q->tag] = list.getHead();
return dirtyEviction;
}
void writeExistingToCache(QueryRecord* q) override
{
//when tag was already present --> move to front
// std::cout<< "entered LRU writeExistingToCache\n";
Node* MRUNode = table[q->tag];
MRUNode->block.setDirty();
list.moveToHead(MRUNode);
}
~LRUSet() override
{
// delete list;
Node* current = list.getHead();
while (current != nullptr) {
Node* next = current->next;
delete current; // Free the node
current = next;
}
list.head = list.tail = nullptr;
table.clear();
}
};
class FIFOSet : public Set
{
std::queue<int> queue; //for tags that are currently occupying, if st.size == n_blocks, evict
std::unordered_map<int, Block> table; //map from tag to Block, use for search
public:
FIFOSet(int i, int n)
{
index = i;
n_blocks = n;
}
int search(QueryRecord* q) override //return nullptr if not-found(miss), or corresponding Block* if found
{
// std::cout<< "entered FIFO set search\n";
int tag = q->tag;
if (table.find(tag) != table.end())
{
return tag;
}
return -1;
}
bool writeNewToCache(QueryRecord* q, bool isWrite) override
{
//when tag was already not present
// std::cout<< "entered FIFO writeNewToCache\n";
bool dirtyEviction = false;
if (queue.size() == n_blocks)
{
int firstTag = queue.front();
Block blk = table[firstTag];
dirtyEviction = blk.isDirty();
// delete blk;
// blk = nullptr;
table.erase(firstTag);
queue.pop();
}
queue.push(q->tag);
table[q->tag] = Block(q->tag, isWrite);
return dirtyEviction;
}
void writeExistingToCache(QueryRecord* q) override
{
table[q->tag].setDirty();
//when tag was already present --> do nothing, just increase cycles
}
~FIFOSet() override
{
// for (auto itr = table.begin(); itr != table.end(); itr++)
// {
// delete itr->second;
// itr->second = nullptr;
// }
table.clear();
}
};