-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.c
58 lines (53 loc) · 1.33 KB
/
set.c
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
#include "common-types.h"
#include <stdlib.h>
#include "util.h"
result_t MISS = { 0, 1, 0 };
result_t HIT = { 1, 0, 0 };
result_t MISS_EVICTION = { 0, 1, 1 };
set_t Set_init(int E) {
set_t set;
set.E = E;
set.len = 0;
set.lines = calloc(E, sizeof(line_t));
return set;
}
int Set_find(set_t set, addr_t tag) {
for (int i = 0; i < set.len; i++) {
if (set.lines[i].tag == tag) {
return i;
}
}
return -1;
}
int Set_LRU(set_t set) {
int LRU = INF, pos;
for (int i = 0; i < set.len; i++) {
if (set.lines[i].timestamp < LRU) {
LRU = set.lines[i].timestamp;
pos = i;
}
}
if (LRU == INF) {
err("cannot find a timestamp less than INF");
}
return pos;
}
result_t Set_visit(set_t* set, addr_t tag, int timestamp) {
result_t result = { 0, 0, 0 };
int pos = -1;
if ((pos = Set_find(*set, tag)) != -1) {
result = Result_add(result, HIT);
set->lines[pos].timestamp = timestamp;
}
else if (set->len < set->E) {
result = Result_add(result, MISS);
set->lines[set->len] = Line_init(tag, timestamp);
(set->len)++;
}
else {
result = Result_add(result, MISS_EVICTION);
pos = Set_LRU(*set);
set->lines[pos] = Line_init(tag, timestamp);
}
return result;
}