-
Notifications
You must be signed in to change notification settings - Fork 1
/
CacheSet.cpp
76 lines (70 loc) · 1.47 KB
/
CacheSet.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
/*
* CacheSet.cpp
*
* Created on: Sep 26, 2015
* Author: quamber
*/
#include "CacheSet.h"
#include <memory>
CacheSet::CacheSet(int blocksize, int tagbits, int indexbits, int offsetbits,
int linesinSet, int indexNo) {
noofblocks = linesinSet;
index = indexNo;
init(blocksize, tagbits, indexbits, offsetbits, linesinSet);
lru = new unsigned char*[noofblocks];
for (int i = 0; i < noofblocks; ++i) {
lru[i] = new unsigned char[noofblocks];
}
for (int i = 0; i < noofblocks; ++i) {
for (int j = 0; j < noofblocks; ++j) {
lru[i][j] = 0;
}
}
}
void CacheSet::init(int blocksize, int tagbits, int indexbits, int offsetbits,
int sets) {
set = (cacheline*) malloc((sizeof(cacheline) * sets));
ptrdiff_t k = 0;
try {
for (; k < sets; k++)
new (set + k) cacheline(blocksize, tagbits, indexbits, offsetbits);
}
catch (...) {
for (; k > 0; k--)
(this->set + k)->~cacheline();
throw;
}
}
int CacheSet::minimumLRUBlock() {
int min = 4;
int temp;
for (int i = 0; i < noofblocks; ++i)
{temp=0;
for(int j = 0; j < noofblocks; ++j)
{
temp+=(short)lru[i][j];
}
if(temp<min){min=temp;}
}
return min;
}
void CacheSet::updateLRU(int index) {
for (int i = 0; i < noofblocks; ++i) {
lru[index][i] = 1;
lru[i][index] = 0;
}
}
int CacheSet::getLRU(int index) {
for (int i = 0; i < noofblocks; ++i) {
int temp =0;
temp=0;
for(int j = 0; j < noofblocks; ++j)
{
temp+=(short)lru[index][j];
}
return temp;
}
}
CacheSet::~CacheSet() {
free(set);
}