source: releases/0.12.0/lib/lru_cache.c @ 273

Last change on this file since 273 was 169, checked in by tim, 15 years ago

filled in additional, minimal documentation

  • Property svn:keywords set to Id
File size: 7.9 KB
Line 
1/*
2 * Copyright (C) 2008-2009 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: lru_cache.c 169 2010-03-03 19:24:58Z tim $
18 */
19
20/**
21 * @file
22 */
23
24#include "lru_cache.h"
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 */ 
32static 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 */
51static 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
62#if 0
63static 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}
101#endif
102
103
104lru_cache* lru_cache_create(uint32_t max_keys, uint32_t secret)
105{
106  return lru_cache_create_ctx(NULL, max_keys, secret, false);
107}
108
109
110lru_cache* lru_cache_create_ctx(void* talloc_ctx, uint32_t max_keys, 
111                                uint32_t secret, bool talloc_data)
112{
113  lru_cache* ret_val;
114
115  ret_val = talloc(talloc_ctx, lru_cache);
116  if(ret_val == NULL)
117    return NULL;
118
119  if(max_keys == 0)
120    ret_val->num_buckets = 1024;
121  else
122  {
123    ret_val->num_buckets = max_keys/lru_cache_floor_log2(max_keys);
124    if(ret_val->num_buckets < 1)
125      ret_val->num_buckets = 1;
126  }
127
128  ret_val->table = talloc_array(ret_val, 
129                                lru_cache_element*, ret_val->num_buckets);
130  if(ret_val->table == NULL)
131  {
132    talloc_free(ret_val);
133    return NULL;
134  }
135 
136  ret_val->oldest = NULL;
137  ret_val->newest = NULL;
138  ret_val->max_keys = max_keys;
139  ret_val->secret = secret;
140  ret_val->talloc_data = talloc_data;
141  ret_val->num_keys = 0;
142  memset(ret_val->table, 0, ret_val->num_buckets*sizeof(lru_cache_element*));
143
144  return ret_val;
145}
146
147
148void lru_cache_destroy(lru_cache* ht)
149{
150  ht->secret = 0;
151  talloc_free(ht);
152}
153
154
155
156bool lru_cache_update(lru_cache* ht, const void* index, 
157                      uint32_t index_len, void* data)
158{
159  uint32_t hash, lru_hash;
160  lru_cache_element* cur;
161  lru_cache_element* last = NULL;
162  lru_cache_element* e = NULL;
163  void* tmp_index;
164
165  hash = lru_cache_compute_hash(ht->num_buckets, ht->secret, index, index_len);
166  for(cur = ht->table[hash]; cur != NULL && e == NULL; cur=cur->next)
167  {
168    if((index_len == cur->index_len) 
169       && memcmp(cur->index, index, index_len) == 0)
170    { e = cur; }
171  }
172 
173  if(e != NULL)
174  { /* We found the index, so we're going to overwrite the data.
175     * We also need to reposition the element to the newest position,
176     * so remove it from the list for now.
177     */
178    if(ht->talloc_data)
179      talloc_free(e->data);
180
181    if(e->newer == NULL)
182      ht->newest = e->older;
183    else
184      e->newer->older = e->older;
185
186    if(e->older == NULL)
187      ht->oldest = e->newer;
188    else
189      e->older->newer = e->newer;
190  }
191  else
192  { /* We didn't find an identical index. */
193   
194    if((ht->max_keys != 0) && (ht->num_keys >= ht->max_keys))
195    { /* Eliminate the least recently used item, but reuse the element
196       * structure to minimize reallocation.
197       */
198      e = ht->oldest;
199      if(ht->newest == ht->oldest)
200      {
201        ht->newest = NULL;
202        ht->oldest = NULL;
203      }
204      else
205      {
206        ht->oldest = e->newer;
207        e->newer->older = NULL;
208      }
209      e->newer = NULL;
210      e->older = NULL;
211
212      last = NULL;
213      lru_hash = lru_cache_compute_hash(ht->num_buckets, ht->secret, 
214                                        e->index, e->index_len);
215      for(cur = ht->table[lru_hash]; cur != e && cur != NULL; 
216          last=cur, cur=cur->next)
217      { continue; }
218
219      if(last == NULL)
220        ht->table[lru_hash] = e->next;
221      else
222        last->next = e->next;
223      e->next = NULL;
224
225      if(ht->talloc_data)
226        talloc_free(e->data);
227
228      tmp_index = talloc_realloc_size(e, e->index, index_len);
229      if(tmp_index == NULL)
230      {
231        talloc_free(e);
232        return false;
233      }
234      else
235        e->index = tmp_index;
236    }
237    else
238    { /* Brand new element because we have room to spare. */
239
240      e = talloc(ht->table, lru_cache_element);
241      if(e == NULL)
242        return false;
243     
244      e->index = talloc_size(e, index_len);
245      if(e->index == NULL)
246      {
247        talloc_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  if(ht->talloc_data)
263    talloc_steal(e, e->data);
264
265  /* Finally, let's insert the element to the newest position in the LRU list.*/
266  if(ht->newest != NULL)
267    ht->newest->newer = e;
268  e->newer = NULL;
269  e->older = ht->newest;
270  ht->newest = e;
271  if(ht->oldest == NULL)
272    ht->oldest = e;
273
274  return true;
275}
276
277
278void* lru_cache_find(lru_cache* ht, const void* index,
279                     uint32_t index_len)
280{
281  uint32_t hash;
282  lru_cache_element* cur;
283
284  hash = lru_cache_compute_hash(ht->num_buckets, ht->secret, index, index_len);
285  for(cur = ht->table[hash]; (cur != NULL); cur = cur->next)
286  {
287    if((index_len == cur->index_len)
288       && memcmp(cur->index, index, index_len) == 0)
289    { break; }
290  }
291 
292  if(cur != NULL && cur->newer != NULL)
293  { /* Need to move this element up to the newest slot. */
294
295    cur->newer->older = cur->older;
296
297    if(cur->older == NULL)
298      ht->oldest = cur->newer;
299    else
300      cur->older->newer = cur->newer;
301
302    cur->newer = NULL;
303    cur->older = ht->newest;
304    ht->newest->newer = cur;
305    ht->newest = cur;
306  }
307
308  if(cur != NULL)
309    return cur->data;
310  else
311    return NULL;
312}
313
314
315
316bool lru_cache_remove(lru_cache* ht, const void* index, 
317                      uint32_t index_len)
318{
319  uint32_t hash;
320  lru_cache_element* cur;
321  lru_cache_element* last = NULL;
322
323  hash = lru_cache_compute_hash(ht->num_buckets, ht->secret,
324                                index, index_len);
325  for(cur=ht->table[hash]; (cur != NULL);
326      last=cur, cur=cur->next)
327  {
328    if((index_len == cur->index_len) 
329       && memcmp(cur->index, index, index_len) == 0)
330    { break; }
331  }
332
333  if(cur == NULL)
334    return false;
335
336  /* Detach from list */
337  if(cur->newer == NULL)
338    ht->newest = cur->older;
339  else
340    cur->newer->older = cur->older;
341 
342  if(cur->older == NULL)
343    ht->oldest = cur->newer;
344  else
345    cur->older->newer = cur->newer;
346
347  /* Detach from hash table */
348  if(last == NULL)
349    ht->table[hash] = cur->next;
350  else
351    last->next = cur->next;
352
353  talloc_free(cur);
354 
355  /* Removing entry, decrement counters. */
356  ht->num_keys--;
357 
358  return true;
359}
Note: See TracBrowser for help on using the repository browser.