-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache_set.cpp
94 lines (77 loc) · 2.81 KB
/
cache_set.cpp
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
#include "cache_set.hpp"
CacheSet::CacheSet(size_t maxSize) {
this->maxSize = maxSize;
this->currentSize = 0;
}
bool CacheSet::checkCacheLine(size_t tag) {
return std::find_if(this->cacheSet.begin(), this->cacheSet.end(), [tag](const CacheLine &cl) {
return cl.tag == tag;
}) != this->cacheSet.end();
}
bool CacheSet::invalidateCacheLine(size_t tag) {
// invalidate the cacheline and remove out of the linked list
auto it = std::find_if(this->cacheSet.begin(), this->cacheSet.end(),
[tag](const CacheLine &cl) { return cl.tag == tag; });
if (it != this->cacheSet.end()) {
this->cacheSet.erase(it);
return true;
}
return false;
}
void CacheSet::addCacheLine(size_t tag, State state) {
// create a cacheline class
// add it to the back of the linked list if the size is less than max size
if (cacheSet.size() == maxSize) {
cacheSet.pop_front();
}
// else delete the LRU cache and add it to the back
CacheLine cl = CacheLine(tag, state);
// add it to linked list
cacheSet.push_back(cl);
}
bool CacheSet::readCacheLine(size_t tag) {
// remove from the current item and add it to the back fo the list
auto it = std::find_if(this->cacheSet.begin(), this->cacheSet.end(),
[tag](const CacheLine &cl) { return cl.tag == tag; });
if (it != this->cacheSet.end()) {
this->cacheSet.splice(this->cacheSet.end(), this->cacheSet, it);
return true;
}
return false;
}
bool CacheSet::updateCacheLine(size_t tag, State state) {
// update the cacheline
// need to pop out the cache and then update it then add it to the end of the list
for (auto it = this->cacheSet.begin(); it != this->cacheSet.end(); ++it) {
if (it->tag == tag) {
// Update the state of the cache line
it->state = state;
// Move the updated cache line to the end of the list
this->cacheSet.splice(this->cacheSet.end(), this->cacheSet, it);
// Cache line updated successfully
return true;
}
}
// Cache line not found
return false;
}
State CacheSet::checkCacheLineState(size_t tag) {
for (auto &cacheLine : cacheSet) {
if (cacheLine.tag == tag) {
return cacheLine.getState();
}
}
return I; // this will be similar to invalid state
}
bool CacheSet::setCacheLineState(size_t tag, State state) {
for (auto &cacheLine : cacheSet) {
if (cacheLine.tag == tag) {
cacheLine.setState(state);
return true;
}
}
return false;
}
bool CacheSet::checkCacheSetFull() { return cacheSet.size() == maxSize; }
bool CacheSet::isEmpty() { return cacheSet.empty(); }
CacheLine CacheSet::getFirst() { return cacheSet.front(); }