|
| 1 | +#include "../include/cache.h" |
| 2 | + |
| 3 | +cache_arc *cache_arc_create(int capacity) |
| 4 | +{ |
| 5 | + cache_arc *arc = malloc(sizeof(cache_arc)); |
| 6 | + |
| 7 | + arc->t1 = list_create(); |
| 8 | + arc->t2 = list_create(); |
| 9 | + arc->b1 = list_create(); |
| 10 | + arc->b2 = list_create(); |
| 11 | + arc->pages = map_create_n(capacity); |
| 12 | + arc->ghosts = map_create_n(capacity); |
| 13 | + |
| 14 | + arc->p = 0.0; |
| 15 | + arc->capacity = capacity; |
| 16 | + |
| 17 | + return arc; |
| 18 | +} |
| 19 | + |
| 20 | +void replace(cache_arc *arc, int addr) |
| 21 | +{ |
| 22 | + page *ghost = map_get(arc->ghosts, addr); |
| 23 | + |
| 24 | + if (arc->t1->size != 0 && // |t1| is not empty |
| 25 | + (arc->t1->size > arc->p || // |t1| exceeds target p |
| 26 | + (ghost != NULL && ghost->l == arc->b2 && // x_t is in b2 |
| 27 | + arc->t1->size == arc->p))) |
| 28 | + { // |t1| = p |
| 29 | + // delete LRU in t1 |
| 30 | + page *p = list_pop_back(arc->t1); |
| 31 | + if (p != NULL) |
| 32 | + { |
| 33 | + // remove LRU from cache |
| 34 | + map_unset(arc->pages, p->addr); |
| 35 | + |
| 36 | + // move lru into mru of b1 |
| 37 | + list_push_front(arc->b1, p); |
| 38 | + map_set(arc->ghosts, p->addr, p); |
| 39 | + } |
| 40 | + } |
| 41 | + else |
| 42 | + { |
| 43 | + // delete LRU in t2 |
| 44 | + page *p = list_pop_back(arc->t2); |
| 45 | + if (p != NULL) |
| 46 | + { |
| 47 | + // remove LRU from cache |
| 48 | + map_unset(arc->pages, p->addr); |
| 49 | + |
| 50 | + // move lru into mru of b2 |
| 51 | + list_push_front(arc->b2, p); |
| 52 | + map_set(arc->ghosts, p->addr, p); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +#define min(a, b) ((a < b) ? a : b) |
| 58 | +#define max(a, b) ((a > b) ? a : b) |
| 59 | + |
| 60 | +#define sigma1(b1, b2) ((b1->size >= b2->size) ? 1 : b2->size / (double)b1->size) |
| 61 | +#define sigma2(b1, b2) ((b2->size >= b1->size) ? 1 : b1->size / (double)b2->size) |
| 62 | + |
| 63 | +int cache_arc_get(cache_arc *arc, int addr) |
| 64 | +{ |
| 65 | + |
| 66 | + // Case I |
| 67 | + // page is in cache - hit |
| 68 | + // - in t1 or t2 |
| 69 | + if (map_has(arc->pages, addr)) |
| 70 | + { |
| 71 | + |
| 72 | + page *p = map_get(arc->pages, addr); |
| 73 | + list_remove(p->l, p); |
| 74 | + list_push_front(arc->t2, p); |
| 75 | + |
| 76 | + return 1; |
| 77 | + } |
| 78 | + |
| 79 | + // Case II-III |
| 80 | + // page is in ghost cache |
| 81 | + if (map_has(arc->ghosts, addr)) |
| 82 | + { |
| 83 | + page *p = map_get(arc->ghosts, addr); |
| 84 | + |
| 85 | + // in b2 |
| 86 | + if (p->l == arc->b1) |
| 87 | + { |
| 88 | + arc->p = min((arc->p + sigma1(arc->b1, arc->b2)), arc->capacity); |
| 89 | + // in b2 |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + arc->p = max((arc->p - sigma2(arc->b1, arc->b2)), 0); |
| 94 | + } |
| 95 | + |
| 96 | + replace(arc, addr); |
| 97 | + |
| 98 | + // move page into lru of t2 |
| 99 | + list_remove(p->l, p); |
| 100 | + list_push_front(arc->t2, p); |
| 101 | + |
| 102 | + // fetch page into cache |
| 103 | + map_unset(arc->ghosts, addr); |
| 104 | + map_set(arc->pages, addr, p); |
| 105 | + |
| 106 | + return 0; |
| 107 | + } |
| 108 | + |
| 109 | + // Case IV |
| 110 | + |
| 111 | + // - Case A |
| 112 | + if (arc->t1->size + arc->b1->size == arc->capacity) |
| 113 | + { |
| 114 | + if (arc->t1->size < arc->capacity) |
| 115 | + { |
| 116 | + page *p = list_pop_back(arc->b1); |
| 117 | + if (p != NULL) |
| 118 | + { |
| 119 | + map_unset(arc->ghosts, p->addr); |
| 120 | + page_free(p); |
| 121 | + } |
| 122 | + replace(arc, addr); |
| 123 | + } |
| 124 | + else |
| 125 | + { |
| 126 | + page *p = list_pop_back(arc->t1); |
| 127 | + if (p != NULL) |
| 128 | + { |
| 129 | + map_unset(arc->pages, p->addr); |
| 130 | + page_free(p); |
| 131 | + } |
| 132 | + } |
| 133 | + // - Case B |
| 134 | + } |
| 135 | + else |
| 136 | + { |
| 137 | + int len = arc->t1->size + |
| 138 | + arc->t2->size + |
| 139 | + arc->b1->size + |
| 140 | + arc->b2->size; |
| 141 | + if (len >= arc->capacity) |
| 142 | + { |
| 143 | + if (len == 2 * arc->capacity) |
| 144 | + { |
| 145 | + page *p = list_pop_back(arc->b2); |
| 146 | + if (p != NULL) |
| 147 | + { |
| 148 | + map_unset(arc->ghosts, p->addr); |
| 149 | + page_free(p); |
| 150 | + } |
| 151 | + } |
| 152 | + replace(arc, addr); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + page *p = page_create(addr); |
| 157 | + list_push_front(arc->t1, p); |
| 158 | + map_set(arc->pages, addr, p); |
| 159 | + |
| 160 | + return 0; |
| 161 | +} |
| 162 | + |
| 163 | +void cache_arc_free(cache_arc *arc) |
| 164 | +{ |
| 165 | + list_free(arc->t1); |
| 166 | + list_free(arc->t2); |
| 167 | + list_free(arc->b1); |
| 168 | + list_free(arc->b2); |
| 169 | + |
| 170 | + map_free(arc->pages); |
| 171 | + map_free(arc->ghosts); |
| 172 | + |
| 173 | + free(arc); |
| 174 | +} |
0 commit comments