-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.c
210 lines (181 loc) · 4.85 KB
/
cache.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <stdlib.h>
#include "cache.h"
/* static implementations */
static inline char static_pop_bucket(lru_cache_t *lru, lru_entry_t *entry) {
int index = HASH(entry->key,lru->bucket_count);
lru_entry_t *bucket = lru->bucket[index];
/* bucket is empty */
if (!bucket) {
return LRU_ERROR_NOT_FOUND;
}
/* entry is first in bucket */
else if (bucket == entry) {
lru->bucket[index] = bucket->chain;
}
/* searching for entry in bucket */
else {
while (bucket->chain) {
if (bucket->chain == entry) {
break;
}
else {
bucket = bucket->chain;
}
}
if (!bucket->chain && bucket != entry) {
return LRU_ERROR_NOT_FOUND;
}
bucket->chain = entry->chain;
}
entry->chain = NULL;
return LRU_SUCCESS;
}
static inline void static_pop_list(lru_cache_t *lru, lru_entry_t *entry) {
/* entry is the only data in cache */
if (entry == lru->head && entry == lru->tail) {
lru->head = NULL;
lru->tail = NULL;
}
/* entry is the head of the cache */
else if (entry == lru->head) {
lru->head = entry->next;
lru->head->prev = NULL;
}
/* entry is the tail of the cache */
else if (entry == lru->tail) {
lru->tail = entry->prev;
lru->tail->next = NULL;
}
/* entry is somewhere in the middle */
else {
entry->prev->next = entry->next;
entry->next->prev = entry->prev;
}
entry->next = NULL;
entry->prev = NULL;
lru->list_count--;
return;
}
/* low level implementations */
char lru_cache_pop(lru_cache_t *lru, lru_entry_t *entry) {
if (!lru || !entry) {
return LRU_ERROR_NULL;
}
char lru_error = static_pop_bucket(lru,entry);
if (lru_error) {
return lru_error;
}
static_pop_list(lru,entry);
return LRU_SUCCESS;
}
char lru_cache_push(lru_cache_t *lru, lru_entry_t *entry) {
if (!lru || !entry) {
return LRU_ERROR_NULL;
}
if (lru->list_count >= lru->list_limit) {
return LRU_ERROR_FULL;
}
int index = HASH(entry->key,lru->bucket_count);
/* adding to front of bucket */
entry->chain = lru->bucket[index];
lru->bucket[index] = entry;
/* adding to head of list */
if (lru->head == NULL && lru->tail == NULL) {
lru->head = entry;
lru->tail = entry;
}
else {
lru->head->prev = entry;
entry->next = lru->head;
lru->head = entry;
}
lru->list_count++;
return LRU_SUCCESS;
}
/* high level implementations */
lru_cache_t *lru_cache_create(int bucket_count, int entry_max) {
lru_cache_t *lru = calloc(1,sizeof(lru_cache_t));
if (!lru) {
return NULL;
}
lru->bucket = calloc(bucket_count,sizeof(lru_entry_t*));
if (!lru->bucket) {
free(lru);
return NULL;
}
lru->bucket_count = bucket_count;
lru->list_limit = entry_max;
return lru;
}
lru_entry_t *lru_cache_search(lru_cache_t *lru, int key) {
if (!lru) {
return NULL;
}
int index = HASH(key,lru->bucket_count);
lru_entry_t *entry = lru->bucket[index];
while (entry) {
if (entry->key == key) {
lru_cache_pop(lru,entry);
lru_cache_push(lru,entry);
break;
}
else {
entry = entry->chain;
}
}
return entry;
}
char lru_cache_add(lru_cache_t *lru, int key, int value) {
if (!lru) {
return LRU_ERROR_NULL;
}
lru_entry_t *entry = NULL;
/* creating new space */
if (lru->list_count < lru->list_limit) {
entry = calloc(1,sizeof(lru_entry_t));
}
if (entry) {
lru->list_count++;
}
/* reusing old space if new space cannot be allocated */
else if (lru->tail) {
entry = lru->tail;
lru_cache_pop(lru,entry);
}
else {
return LRU_ERROR_ALLOC;
}
/* adding to cache */
entry->key = key;
entry->value = value;
lru_cache_push(lru,entry);
return LRU_SUCCESS;
}
void lru_cache_remove(lru_cache_t *lru, int key) {
if (!lru) {
return;
}
lru_entry_t *entry = lru_cache_search(lru,key);
if (entry) {
lru_cache_pop(lru,entry);
free(entry);
lru->list_limit--;
}
return;
}
void lru_cache_free(lru_cache_t *lru) {
if (!lru) {
return;
}
lru_entry_t *entry, *temp;
for (int i = 0; i < lru->bucket_count; i++) {
entry = lru->bucket[i];
while (entry) {
temp = entry;
entry = entry->chain;
free(temp);
}
}
free(lru);
return;
}