-
Notifications
You must be signed in to change notification settings - Fork 6
/
cachehash.c
276 lines (244 loc) · 5.93 KB
/
cachehash.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
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
/*
* CacheHash Copyright 2014 Regents of the University of Michigan
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*/
#include "cachehash.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <Judy.h>
#define EVICTION_NEEDED 1
#define EVICTION_UNNEC 0
// doubly-linked-list node
typedef struct node {
struct node *next;
struct node *prev;
void *key;
size_t keylen;
void *data;
} node_t;
// data structure that contains the enterity of a linked list
// we typedef this as cachehash in cachehash.h s.t. external interface is clean
struct cachehash_s {
Pvoid_t judy;
void *malloced;
node_t *start;
node_t *end;
node_t *curr_end;
size_t maxsize;
size_t currsize;
cachehash_process_cb *evict_cb;
};
cachehash* cachehash_init(size_t maxitems, cachehash_process_cb *cb)
{
assert(maxitems > 0);
cachehash *retv = malloc(sizeof(cachehash));
assert(retv);
memset(retv, 0, sizeof(cachehash));
// allocate nodes all at once to avoid fragmented memory
node_t *nodes = calloc(maxitems, sizeof(node_t));
retv->malloced = nodes;
assert(nodes);
retv->start = nodes;
retv->curr_end = nodes;
retv->end = &nodes[maxitems-1];
retv->maxsize = maxitems;
retv->currsize = 0;
// initial node
nodes[0].next = &nodes[1];
// middle nodes
for (unsigned int i=1; i < maxitems - 1; i++) {
nodes[i].prev = &nodes[i-1];
nodes[i].next = &nodes[i+1];
}
// last node
nodes[maxitems-1].prev = &nodes[maxitems-2]; return retv;
}
void cachehash_set_evict_cb(cachehash *ch, cachehash_process_cb *cb)
{
ch->evict_cb = cb;
}
// is the hashcache full?
static inline int eviction_needed(cachehash *ch)
{
assert(ch);
return ch->currsize == ch->maxsize;
}
// completely evict the LRU object
// does not cb to user w/ object
static inline void* evict(cachehash *ch)
{
assert(ch);
node_t *last = ch->end;
// remove item from judy array
int rc;
JHSD(rc, ch->judy, last->key, last->keylen);
// we should never end up with something in the linked list
// that's not in the judy array.
assert(rc);
// reset linked list node
void *retv = last->data;
last->data = NULL;
free(last->key);
last->key = NULL;
last->keylen = 0;
ch->currsize--;
ch->curr_end = ch->end;
return retv;
}
static inline void use(cachehash *ch, node_t *n)
{
assert(ch);
assert(n);
//if first node, nothing to do and return
if (n == ch->start) {
return;
}
// remove from current spot in linked list
node_t *prev = n->prev;
n->prev->next = n->next;
// if last node then no next, but must update LL
if (n->next) {
n->next->prev = prev;
} else {
ch->end = prev;
}
// front of list
n->next = ch->start;
ch->start->prev = n;
ch->start = n;
n->prev = NULL;
}
static inline node_t* judy_get(cachehash *ch, void *key, size_t keylen)
{
assert(ch);
assert(key);
assert(keylen);
Word_t *v_;
JHSG(v_, ch->judy, key, keylen);
if (!v_) {
return NULL;
}
return (node_t*) *v_;
}
void* cachehash_has(cachehash *ch, const void *key, size_t keylen)
{
assert(ch);
assert(key);
assert(keylen);
node_t *n = judy_get(ch, key, keylen);
if (n) {
return n->data;
} else {
return NULL;
}
}
void* cachehash_get(cachehash *ch, const void *key, size_t keylen)
{
assert(ch);
assert(key);
assert(keylen);
node_t *n = judy_get(ch, key, keylen);
if (n) {
use(ch, n);
return n->data;
} else {
return NULL;
}
}
void* cachehash_evict_if_full(cachehash *ch)
{
assert(ch);
if (eviction_needed(ch) == EVICTION_UNNEC) {
return NULL;
}
return evict(ch);
}
void cachehash_put(cachehash *ch, const void *key, size_t keylen, void *value)
{
assert(ch);
assert(key);
assert(keylen);
void *evicted = cachehash_evict_if_full(ch);
if (evicted && ch->evict_cb) {
ch->evict_cb(evicted);
ch->curr_end = ch->end;
}
// create new node
node_t *n;
void *newkey = malloc(keylen);
n = ch->curr_end;
memcpy(newkey, key, keylen);
n->key = newkey;
n->keylen = keylen;
n->data = value;
//n->prev = ch->curr_end->prev;
//n->next = ch->curr_end->next;
if(ch->curr_end != ch->end){
ch->curr_end = ch->curr_end->next;
ch->curr_end->prev = n;
}
use(ch, n);
ch->currsize++;
// add to judy array
Word_t *v_;
JHSI(v_, ch->judy, key, keylen);
// key should not already be in hash table
assert(!*v_);
*v_ = (Word_t) n;
}
// print out entire state.
void cachehash_debug_dump(cachehash *ch)
{
printf("Statistics:\n");
printf("\tcurrent size: %lu\n", ch->currsize);
printf("\tmaximum size: %lu\n", ch->maxsize);
printf("\n");
printf("Linked List:\n");
size_t i = 0;
node_t *n = ch->start;
do {
if (n->key) {
printf("\t%lu: %s -> %s\n", i++, (char*) n->key, (char*) n->data);
} else {
printf("\t%lu: EMPTY\n", i++);
}
n = n->next;
} while (n);
}
void cachehash_free(cachehash *ch, cachehash_process_cb *cb)
{
assert(ch);
int rc;
JHSFA(rc, ch->judy);
node_t *n = ch->start;
do {
if (n->key) {
free(n->key);
if (cb) {
cb(n->data);
}
}
n = n->next;
} while(n);
free(ch->malloced);
free(ch);
}
void cachehash_iter(cachehash *ch, cachehash_process_cb *cb)
{
node_t *n = ch->start;
do {
if (n->key) {
cb(n->data);
} else {
break;
}
n = n->next;
} while (n);
}