forked from Theano/libgpuarray
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoq.c
More file actions
355 lines (324 loc) · 7.68 KB
/
twoq.c
File metadata and controls
355 lines (324 loc) · 7.68 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include <assert.h>
#include <stdlib.h>
#include "cache.h"
#include "private_config.h"
typedef struct _node node;
typedef struct _list list;
typedef struct _hash hash;
typedef struct _twoq_cache twoq_cache;
#define HOT 0
#define WARM 1
#define COLD 2
struct _node {
node *prev;
node *next;
node *h_next;
cache_key_t key;
cache_value_t val;
int temp;
};
static inline void node_init(node *n, const cache_key_t k,
const cache_value_t v) {
n->prev = NULL;
n->next = NULL;
n->h_next = NULL;
n->key = k;
n->val = v;
n->temp = HOT;
}
static inline node *node_alloc(const cache_key_t key,
const cache_value_t val) {
node *res = malloc(sizeof(node));
if (res != NULL)
node_init(res, key, val);
return res;
}
static inline void node_free(node *n, cache_freek_fn kfree,
cache_freev_fn vfree) {
kfree(n->key);
vfree(n->val);
if (n->h_next != NULL)
node_free(n->h_next, kfree, vfree);
free(n);
}
static inline void node_unlink(node *n) {
if (n->next != NULL)
n->next->prev = n->prev;
if (n->prev != NULL)
n->prev->next = n->next;
n->next = NULL;
n->prev = NULL;
}
struct _list {
node *head;
node *tail;
size_t size;
};
static inline void list_init(list *l) {
l->head = NULL;
l->tail = NULL;
l->size = 0;
}
static inline void list_clear(list *l) {
l->head = NULL;
l->tail = NULL;
l->size = 0;
}
static inline node *list_pop(list *l) {
if (l->head == NULL)
return NULL;
else {
node *oldHead = l->head;
l->head = l->head->next;
node_unlink(oldHead);
l->size--;
if (l->size == 0) {
l->tail = NULL;
}
return oldHead;
}
}
static inline node *list_remove(list *l, node *n) {
if (n == l->head)
l->head = n->next;
if (n == l->tail)
l->tail = n->prev;
node_unlink(n);
l->size--;
return n;
}
static inline void list_push(list *l, node *n) {
node_unlink(n);
if (l->head == NULL) {
l->head = n;
} else if (l->head == l->tail) {
l->head->next = n;
n->prev = l->head;
} else {
l->tail->next = n;
n->prev = l->tail;
}
l->tail = n;
l->size++;
}
struct _hash {
node **keyval;
size_t nbuckets;
size_t size;
};
static inline size_t roundup2(size_t s) {
s--;
s |= s >> 1;
s |= s >> 2;
s |= s >> 4;
s |= s >> 8;
s |= s >> 16;
if (sizeof(size_t) >= 8)
s |= s >> 32;
s++;
return s;
}
static inline int hash_init(hash *h, size_t size) {
h->nbuckets = roundup2(size + (size/6));
h->keyval = calloc(h->nbuckets, sizeof(*h->keyval));
if (h->keyval == NULL) {
return -1;
}
h->size = 0;
return 0;
}
static inline void hash_clear(hash *h, cache_freek_fn kfree,
cache_freev_fn vfree) {
size_t i;
for (i = 0; i < h->nbuckets; i++) {
if (h->keyval[i] != NULL)
node_free(h->keyval[i], kfree, vfree);
}
free(h->keyval);
h->nbuckets = 0;
h->size = 0;
h->keyval = NULL;
}
static inline node *hash_find(hash *h, const cache_key_t key,
cache_eq_fn keq, cache_hash_fn khash) {
size_t p = khash(key) & (h->nbuckets - 1);
node *n;
if (h->keyval[p] != NULL) {
n = h->keyval[p];
do {
if (keq(n->key, key))
return n;
n = n->h_next;
} while (n != NULL);
}
return NULL;
}
static inline node *hash_add(hash *h, const cache_key_t key,
const cache_value_t val,
cache_hash_fn khash) {
size_t p = khash(key) & (h->nbuckets - 1);
node *n = node_alloc(key, val);
if (n == NULL) return NULL;
if (h->keyval[p] == NULL) {
h->keyval[p] = n;
} else {
n->h_next = h->keyval[p];
h->keyval[p] = n;
}
h->size++;
return n;
}
static inline void hash_del(hash *h, node *n,
cache_freek_fn kfree,
cache_freev_fn vfree,
cache_hash_fn khash) {
size_t p = khash(n->key) & (h->nbuckets - 1);
node *np;
if (n == h->keyval[p]) {
h->keyval[p] = n->h_next;
n->h_next = NULL;
node_free(n, kfree, vfree);
h->size--;
} else {
np = h->keyval[p];
while (np->h_next != NULL) {
if (np->h_next == n) {
np->h_next = n->h_next;
n->h_next = NULL;
node_free(n, kfree, vfree);
h->size--;
break;
}
np = np->h_next;
}
}
}
struct _twoq_cache {
cache c;
hash data;
list hot;
list warm;
list cold;
size_t hot_size;
size_t warm_size;
size_t cold_size;
size_t elasticity;
};
static inline void twoq_prune(twoq_cache *c) {
while (c->hot.size > c->hot_size) {
node *n = list_pop(&c->hot);
n->temp = COLD;
list_push(&c->cold, n);
}
if (c->cold.size > c->cold_size + c->elasticity) {
while (c->cold.size > c->cold_size) {
node *n = list_pop(&c->cold);
hash_del(&c->data, n, c->c.kfree, c->c.vfree, c->c.khash);
}
}
}
static int twoq_del(cache *_c, const cache_key_t k) {
twoq_cache *c = (twoq_cache *)_c;
node *n = hash_find(&c->data, k, c->c.keq, c->c.khash);
if (n != NULL) {
switch (n->temp) {
case HOT:
list_remove(&c->hot, n);
break;
case WARM:
list_remove(&c->warm, n);
break;
case COLD:
list_remove(&c->cold, n);
break;
default:
assert(0 && "node temperature is not within expected values");
}
hash_del(&c->data, n, c->c.kfree, c->c.vfree, c->c.khash);
return 1;
}
return 0;
}
static int twoq_add(cache *_c, cache_key_t key, cache_value_t val) {
twoq_cache *c = (twoq_cache *)_c;
node *n;
/* XXX: possible optimization here to combine remove and add.
currently needs to be done this way since hash_add does not
overwrite previous values */
twoq_del(_c, key);
n = hash_add(&c->data, key, val, c->c.khash);
if (n == NULL) {
return -1;
}
list_push(&c->hot, n);
twoq_prune(c);
return 0;
}
static cache_value_t twoq_get(cache *_c, const cache_key_t key) {
twoq_cache *c = (twoq_cache *)_c;
node *nn;
node *n = hash_find(&c->data, key, c->c.keq, c->c.khash);
if (n == NULL) {
return NULL;
} else {
switch (n->temp) {
case HOT:
list_remove(&c->hot, n);
list_push(&c->hot, n);
break;
case WARM:
list_remove(&c->warm, n);
list_push(&c->warm, n);
break;
case COLD:
list_remove(&c->cold, n);
n->temp = WARM;
list_push(&c->warm, n);
if (c->warm.size > c->warm_size) {
nn = list_pop(&c->warm);
nn->temp = COLD;
list_push(&c->cold, nn);
}
break;
default:
assert(0 && "node temperature is not within expected values");
}
return n->val;
}
}
static void twoq_destroy(cache *_c) {
twoq_cache *c = (twoq_cache *)_c;
hash_clear(&c->data, c->c.kfree, c->c.vfree);
list_clear(&c->hot);
list_clear(&c->warm);
list_clear(&c->cold);
}
cache *cache_twoq(size_t hot_size, size_t warm_size, size_t cold_size,
size_t elasticity, cache_eq_fn keq, cache_hash_fn khash,
cache_freek_fn kfree, cache_freev_fn vfree) {
twoq_cache *res;
if (hot_size == 0 || warm_size == 0 || cold_size == 0)
return NULL;
res = malloc(sizeof(*res));
if (res == NULL) return NULL;
if (hash_init(&res->data, hot_size+warm_size+cold_size+elasticity)) {
free(res);
return NULL;
}
list_init(&res->hot);
list_init(&res->warm);
list_init(&res->cold);
res->hot_size = hot_size;
res->warm_size = warm_size;
res->cold_size = cold_size;
res->elasticity = elasticity;
res->c.add = twoq_add;
res->c.del = twoq_del;
res->c.get = twoq_get;
res->c.destroy = twoq_destroy;
res->c.keq = keq;
res->c.khash = khash;
res->c.kfree = kfree;
res->c.vfree = vfree;
return (cache *)res;
}