source: trunk/lib/lru_cache.c @ 110

Last change on this file since 110 was 108, checked in by tim, 17 years ago

added least-recently-used cache structure.

set up SK records to use this instead of the linked list.

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