[169] | 1 | /* |
---|
[147] | 2 | * Copyright (C) 2008-2009 Timothy D. Morgan |
---|
[108] | 3 | * |
---|
| 4 | * This program is free software; you can redistribute it and/or modify |
---|
| 5 | * it under the terms of the GNU General Public License as published by |
---|
| 6 | * the Free Software Foundation; version 3 of the License. |
---|
| 7 | * |
---|
| 8 | * This program is distributed in the hope that it will be useful, |
---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 11 | * GNU General Public License for more details. |
---|
| 12 | * |
---|
| 13 | * You should have received a copy of the GNU General Public License |
---|
| 14 | * along with this program; if not, write to the Free Software |
---|
| 15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 16 | * |
---|
[122] | 17 | * $Id: lru_cache.c 184 2010-03-20 17:50:44Z tim $ |
---|
[108] | 18 | */ |
---|
| 19 | |
---|
[169] | 20 | /** |
---|
| 21 | * @file |
---|
| 22 | */ |
---|
| 23 | |
---|
[147] | 24 | #include "lru_cache.h" |
---|
[108] | 25 | |
---|
| 26 | |
---|
| 27 | #define LRU_CACHE_DEBUG 0 |
---|
| 28 | |
---|
| 29 | /* XXX: really should replace this with a real universal hash or other |
---|
| 30 | * fast HMAC. |
---|
| 31 | */ |
---|
| 32 | static uint32_t lru_cache_compute_hash(uint32_t num_buckets, |
---|
| 33 | uint32_t secret, |
---|
| 34 | const void* buf, |
---|
| 35 | uint32_t buf_len) |
---|
| 36 | { |
---|
| 37 | uint32_t i; |
---|
| 38 | uint32_t ret_val = 0x243f6a88; |
---|
| 39 | unsigned char* s = (unsigned char*)&secret; |
---|
| 40 | const unsigned char* b = (unsigned char*)buf; |
---|
| 41 | |
---|
| 42 | for(i=0; i<buf_len; i++) |
---|
| 43 | ret_val = (ret_val+(i^s[i%4])*b[i]) % num_buckets; |
---|
| 44 | |
---|
| 45 | return ret_val; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | /* Returns approximately floor(log_2(n)) (log base 2 of n, floored) |
---|
| 49 | * If n == 0, returns 0 |
---|
| 50 | */ |
---|
| 51 | static uint32_t lru_cache_floor_log2(uint32_t n) |
---|
| 52 | { |
---|
| 53 | uint32_t ret_val; |
---|
| 54 | |
---|
| 55 | for(ret_val=31; ret_val > 1; ret_val--) |
---|
| 56 | if((n & (1 << ret_val)) != 0) |
---|
| 57 | return ret_val; |
---|
| 58 | |
---|
| 59 | return 0; |
---|
| 60 | } |
---|
| 61 | |
---|
[112] | 62 | #if 0 |
---|
[108] | 63 | static void lru_cache_print(lru_cache* ht) |
---|
| 64 | { |
---|
| 65 | uint32_t i; |
---|
| 66 | lru_cache_element* cur; |
---|
| 67 | |
---|
| 68 | printf("from newest to oldest:\n"); |
---|
| 69 | for(cur=ht->newest; cur != NULL; cur=cur->older) |
---|
| 70 | { |
---|
| 71 | /* write(STDOUT_FILENO, cur->index, cur->index_len);*/ |
---|
| 72 | printf("%p", (void*)cur); |
---|
| 73 | printf("\n"); |
---|
| 74 | if(cur->older == ht->newest) |
---|
| 75 | { |
---|
| 76 | printf("??? Loop in LRU list!!"); |
---|
| 77 | break; |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | printf("\n"); |
---|
| 81 | |
---|
| 82 | printf("table:\n"); |
---|
| 83 | for(i=0; i<ht->num_buckets; i++) |
---|
| 84 | { |
---|
| 85 | printf("%.8X: ", i); |
---|
| 86 | for(cur=ht->table[i]; cur != NULL; cur=cur->next) |
---|
| 87 | { |
---|
| 88 | /* write(STDOUT_FILENO, cur->index, cur->index_len);*/ |
---|
| 89 | printf("%p", (void*)cur); |
---|
| 90 | printf("|"); |
---|
| 91 | |
---|
| 92 | if(cur->next == ht->table[i]) |
---|
| 93 | { |
---|
| 94 | printf("??? Loop in table chain!!"); |
---|
| 95 | break; |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | printf("\n"); |
---|
| 99 | } |
---|
| 100 | } |
---|
[112] | 101 | #endif |
---|
[108] | 102 | |
---|
[147] | 103 | |
---|
| 104 | lru_cache* lru_cache_create(uint32_t max_keys, uint32_t secret) |
---|
[108] | 105 | { |
---|
[147] | 106 | return lru_cache_create_ctx(NULL, max_keys, secret, false); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | lru_cache* lru_cache_create_ctx(void* talloc_ctx, uint32_t max_keys, |
---|
| 111 | uint32_t secret, bool talloc_data) |
---|
| 112 | { |
---|
[108] | 113 | lru_cache* ret_val; |
---|
| 114 | |
---|
[147] | 115 | ret_val = talloc(talloc_ctx, lru_cache); |
---|
[108] | 116 | if(ret_val == NULL) |
---|
| 117 | return NULL; |
---|
| 118 | |
---|
[111] | 119 | if(max_keys == 0) |
---|
[146] | 120 | ret_val->num_buckets = 1024; |
---|
[184] | 121 | else if(max_keys == 1) |
---|
| 122 | ret_val->num_buckets = 1; |
---|
[111] | 123 | else |
---|
| 124 | { |
---|
| 125 | ret_val->num_buckets = max_keys/lru_cache_floor_log2(max_keys); |
---|
| 126 | if(ret_val->num_buckets < 1) |
---|
| 127 | ret_val->num_buckets = 1; |
---|
| 128 | } |
---|
[184] | 129 | |
---|
[147] | 130 | ret_val->table = talloc_array(ret_val, |
---|
| 131 | lru_cache_element*, ret_val->num_buckets); |
---|
[108] | 132 | if(ret_val->table == NULL) |
---|
| 133 | { |
---|
[147] | 134 | talloc_free(ret_val); |
---|
[108] | 135 | return NULL; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | ret_val->oldest = NULL; |
---|
| 139 | ret_val->newest = NULL; |
---|
| 140 | ret_val->max_keys = max_keys; |
---|
| 141 | ret_val->secret = secret; |
---|
[147] | 142 | ret_val->talloc_data = talloc_data; |
---|
[108] | 143 | ret_val->num_keys = 0; |
---|
| 144 | memset(ret_val->table, 0, ret_val->num_buckets*sizeof(lru_cache_element*)); |
---|
| 145 | |
---|
| 146 | return ret_val; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | |
---|
| 150 | void lru_cache_destroy(lru_cache* ht) |
---|
| 151 | { |
---|
| 152 | ht->secret = 0; |
---|
[147] | 153 | talloc_free(ht); |
---|
[108] | 154 | } |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | bool lru_cache_update(lru_cache* ht, const void* index, |
---|
| 159 | uint32_t index_len, void* data) |
---|
| 160 | { |
---|
| 161 | uint32_t hash, lru_hash; |
---|
| 162 | lru_cache_element* cur; |
---|
| 163 | lru_cache_element* last = NULL; |
---|
| 164 | lru_cache_element* e = NULL; |
---|
| 165 | void* tmp_index; |
---|
| 166 | |
---|
| 167 | hash = lru_cache_compute_hash(ht->num_buckets, ht->secret, index, index_len); |
---|
| 168 | for(cur = ht->table[hash]; cur != NULL && e == NULL; cur=cur->next) |
---|
| 169 | { |
---|
| 170 | if((index_len == cur->index_len) |
---|
| 171 | && memcmp(cur->index, index, index_len) == 0) |
---|
| 172 | { e = cur; } |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | if(e != NULL) |
---|
| 176 | { /* We found the index, so we're going to overwrite the data. |
---|
| 177 | * We also need to reposition the element to the newest position, |
---|
| 178 | * so remove it from the list for now. |
---|
| 179 | */ |
---|
[147] | 180 | if(ht->talloc_data) |
---|
[184] | 181 | talloc_unlink(e, e->data); |
---|
[108] | 182 | |
---|
| 183 | if(e->newer == NULL) |
---|
| 184 | ht->newest = e->older; |
---|
| 185 | else |
---|
| 186 | e->newer->older = e->older; |
---|
| 187 | |
---|
| 188 | if(e->older == NULL) |
---|
| 189 | ht->oldest = e->newer; |
---|
| 190 | else |
---|
| 191 | e->older->newer = e->newer; |
---|
| 192 | } |
---|
| 193 | else |
---|
| 194 | { /* We didn't find an identical index. */ |
---|
| 195 | |
---|
[111] | 196 | if((ht->max_keys != 0) && (ht->num_keys >= ht->max_keys)) |
---|
[108] | 197 | { /* Eliminate the least recently used item, but reuse the element |
---|
| 198 | * structure to minimize reallocation. |
---|
| 199 | */ |
---|
| 200 | e = ht->oldest; |
---|
| 201 | if(ht->newest == ht->oldest) |
---|
| 202 | { |
---|
| 203 | ht->newest = NULL; |
---|
| 204 | ht->oldest = NULL; |
---|
| 205 | } |
---|
| 206 | else |
---|
| 207 | { |
---|
| 208 | ht->oldest = e->newer; |
---|
| 209 | e->newer->older = NULL; |
---|
| 210 | } |
---|
| 211 | e->newer = NULL; |
---|
| 212 | e->older = NULL; |
---|
| 213 | |
---|
| 214 | last = NULL; |
---|
| 215 | lru_hash = lru_cache_compute_hash(ht->num_buckets, ht->secret, |
---|
| 216 | e->index, e->index_len); |
---|
| 217 | for(cur = ht->table[lru_hash]; cur != e && cur != NULL; |
---|
| 218 | last=cur, cur=cur->next) |
---|
| 219 | { continue; } |
---|
| 220 | |
---|
| 221 | if(last == NULL) |
---|
| 222 | ht->table[lru_hash] = e->next; |
---|
| 223 | else |
---|
| 224 | last->next = e->next; |
---|
| 225 | e->next = NULL; |
---|
| 226 | |
---|
[147] | 227 | if(ht->talloc_data) |
---|
[184] | 228 | talloc_unlink(e, e->data); |
---|
[108] | 229 | |
---|
[147] | 230 | tmp_index = talloc_realloc_size(e, e->index, index_len); |
---|
[108] | 231 | if(tmp_index == NULL) |
---|
| 232 | { |
---|
[147] | 233 | talloc_free(e); |
---|
[108] | 234 | return false; |
---|
| 235 | } |
---|
[136] | 236 | else |
---|
| 237 | e->index = tmp_index; |
---|
[108] | 238 | } |
---|
| 239 | else |
---|
| 240 | { /* Brand new element because we have room to spare. */ |
---|
| 241 | |
---|
[147] | 242 | e = talloc(ht->table, lru_cache_element); |
---|
[108] | 243 | if(e == NULL) |
---|
| 244 | return false; |
---|
| 245 | |
---|
[147] | 246 | e->index = talloc_size(e, index_len); |
---|
[108] | 247 | if(e->index == NULL) |
---|
| 248 | { |
---|
[147] | 249 | talloc_free(e); |
---|
[108] | 250 | return false; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | /* New entry, increment counters. */ |
---|
| 254 | ht->num_keys++; |
---|
| 255 | } |
---|
| 256 | memcpy(e->index, index, index_len); |
---|
| 257 | e->index_len = index_len; |
---|
| 258 | |
---|
| 259 | /* Insert at beginning of chain, in a vaguely LRU style */ |
---|
| 260 | e->next = ht->table[hash]; |
---|
| 261 | ht->table[hash] = e; |
---|
| 262 | } |
---|
| 263 | e->data = data; |
---|
[147] | 264 | if(ht->talloc_data) |
---|
[184] | 265 | talloc_reference(e, e->data); |
---|
[108] | 266 | |
---|
| 267 | /* Finally, let's insert the element to the newest position in the LRU list.*/ |
---|
| 268 | if(ht->newest != NULL) |
---|
| 269 | ht->newest->newer = e; |
---|
| 270 | e->newer = NULL; |
---|
| 271 | e->older = ht->newest; |
---|
| 272 | ht->newest = e; |
---|
| 273 | if(ht->oldest == NULL) |
---|
| 274 | ht->oldest = e; |
---|
| 275 | |
---|
| 276 | return true; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | |
---|
| 280 | void* lru_cache_find(lru_cache* ht, const void* index, |
---|
| 281 | uint32_t index_len) |
---|
| 282 | { |
---|
| 283 | uint32_t hash; |
---|
| 284 | lru_cache_element* cur; |
---|
| 285 | |
---|
| 286 | hash = lru_cache_compute_hash(ht->num_buckets, ht->secret, index, index_len); |
---|
| 287 | for(cur = ht->table[hash]; (cur != NULL); cur = cur->next) |
---|
| 288 | { |
---|
| 289 | if((index_len == cur->index_len) |
---|
| 290 | && memcmp(cur->index, index, index_len) == 0) |
---|
| 291 | { break; } |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | if(cur != NULL && cur->newer != NULL) |
---|
| 295 | { /* Need to move this element up to the newest slot. */ |
---|
| 296 | |
---|
| 297 | cur->newer->older = cur->older; |
---|
| 298 | |
---|
| 299 | if(cur->older == NULL) |
---|
| 300 | ht->oldest = cur->newer; |
---|
| 301 | else |
---|
| 302 | cur->older->newer = cur->newer; |
---|
| 303 | |
---|
| 304 | cur->newer = NULL; |
---|
| 305 | cur->older = ht->newest; |
---|
| 306 | ht->newest->newer = cur; |
---|
| 307 | ht->newest = cur; |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | if(cur != NULL) |
---|
| 311 | return cur->data; |
---|
| 312 | else |
---|
| 313 | return NULL; |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | |
---|
| 317 | |
---|
| 318 | bool lru_cache_remove(lru_cache* ht, const void* index, |
---|
| 319 | uint32_t index_len) |
---|
| 320 | { |
---|
| 321 | uint32_t hash; |
---|
| 322 | lru_cache_element* cur; |
---|
| 323 | lru_cache_element* last = NULL; |
---|
| 324 | |
---|
| 325 | hash = lru_cache_compute_hash(ht->num_buckets, ht->secret, |
---|
| 326 | index, index_len); |
---|
| 327 | for(cur=ht->table[hash]; (cur != NULL); |
---|
| 328 | last=cur, cur=cur->next) |
---|
| 329 | { |
---|
| 330 | if((index_len == cur->index_len) |
---|
| 331 | && memcmp(cur->index, index, index_len) == 0) |
---|
| 332 | { break; } |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | if(cur == NULL) |
---|
| 336 | return false; |
---|
| 337 | |
---|
| 338 | /* Detach from list */ |
---|
| 339 | if(cur->newer == NULL) |
---|
| 340 | ht->newest = cur->older; |
---|
| 341 | else |
---|
| 342 | cur->newer->older = cur->older; |
---|
| 343 | |
---|
| 344 | if(cur->older == NULL) |
---|
| 345 | ht->oldest = cur->newer; |
---|
| 346 | else |
---|
| 347 | cur->older->newer = cur->newer; |
---|
| 348 | |
---|
| 349 | /* Detach from hash table */ |
---|
| 350 | if(last == NULL) |
---|
| 351 | ht->table[hash] = cur->next; |
---|
| 352 | else |
---|
| 353 | last->next = cur->next; |
---|
| 354 | |
---|
[147] | 355 | talloc_free(cur); |
---|
[108] | 356 | |
---|
| 357 | /* Removing entry, decrement counters. */ |
---|
| 358 | ht->num_keys--; |
---|
| 359 | |
---|
| 360 | return true; |
---|
| 361 | } |
---|