File: lru_cache.c

package info (click to toggle)
reglookup 1.0.1+svn287-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 952 kB
  • sloc: ansic: 6,676; python: 3,762; makefile: 40; sh: 27
file content (361 lines) | stat: -rw-r--r-- 8,111 bytes parent folder | download | duplicates (2)
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
356
357
358
359
360
361
/*
 * Copyright (C) 2008-2009 Timothy D. Morgan
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
 *
 * $Id$
 */

/**
 * @file
 */

#include "lru_cache.h"


#define LRU_CACHE_DEBUG 0

/* XXX: really should replace this with a real universal hash or other
 *      fast HMAC.
 */ 
static uint32_t lru_cache_compute_hash(uint32_t num_buckets,
				       uint32_t secret,
				       const void* buf,
				       uint32_t buf_len)
{
  uint32_t i;
  uint32_t ret_val = 0x243f6a88;
  unsigned char* s = (unsigned char*)&secret;
  const unsigned char* b = (unsigned char*)buf;

  for(i=0; i<buf_len; i++)
    ret_val = (ret_val+(i^s[i%4])*b[i]) % num_buckets;
  
  return ret_val;
}

/* Returns approximately floor(log_2(n)) (log base 2 of n, floored) 
 * If n == 0, returns 0
 */
static uint32_t lru_cache_floor_log2(uint32_t n)
{
  uint32_t ret_val;
  
  for(ret_val=31; ret_val > 1; ret_val--)
    if((n & (1 << ret_val)) != 0)
      return ret_val;

  return 0;
}

#if 0
_EXPORT()
void lru_cache_print(lru_cache* ht)
{
  uint32_t i;
  lru_cache_element* cur;

  printf("from newest to oldest:\n");
  for(cur=ht->newest; cur != NULL; cur=cur->older)
  {
    /*    write(STDOUT_FILENO, cur->index, cur->index_len);*/
    printf("%p", (void*)cur);
    printf("\n");
    if(cur->older == ht->newest)
    {
      printf("??? Loop in LRU list!!");
      break;
    }
  }
  printf("\n");

  printf("table:\n");
  for(i=0; i<ht->num_buckets; i++)
  {
    printf("%.8X: ", i);
    for(cur=ht->table[i]; cur != NULL; cur=cur->next)
    {
      /*      write(STDOUT_FILENO, cur->index, cur->index_len);*/
      printf("%p", (void*)cur);
      printf("|");

      if(cur->next == ht->table[i])
      {
	printf("??? Loop in table chain!!");
	break;
      }
    }
    printf("\n");
  }
}
#endif


lru_cache* lru_cache_create(uint32_t max_keys, uint32_t secret)
{
  return lru_cache_create_ctx(NULL, max_keys, secret, false);
}


lru_cache* lru_cache_create_ctx(void* talloc_ctx, uint32_t max_keys, 
				uint32_t secret, bool talloc_data)
{
  lru_cache* ret_val;

  ret_val = talloc(talloc_ctx, lru_cache);
  if(ret_val == NULL)
    return NULL;

  if(max_keys == 0)
    ret_val->num_buckets = 1024;
  else if(max_keys == 1)
    ret_val->num_buckets = 1;    
  else
  {
    ret_val->num_buckets = max_keys/lru_cache_floor_log2(max_keys);
    if(ret_val->num_buckets < 1)
      ret_val->num_buckets = 1;
  }
  
  ret_val->table = talloc_array(ret_val, 
				lru_cache_element*, ret_val->num_buckets);
  if(ret_val->table == NULL)
  {
    talloc_free(ret_val);
    return NULL;
  }
  
  ret_val->oldest = NULL;
  ret_val->newest = NULL;
  ret_val->max_keys = max_keys;
  ret_val->secret = secret;
  ret_val->talloc_data = talloc_data;
  ret_val->num_keys = 0;
  memset(ret_val->table, 0, ret_val->num_buckets*sizeof(lru_cache_element*));

  return ret_val;
}


void lru_cache_destroy(lru_cache* ht)
{
  ht->secret = 0;
  talloc_unlink(NULL, ht);
}



bool lru_cache_update(lru_cache* ht, const void* index, 
		      uint32_t index_len, void* data)
{
  uint32_t hash, lru_hash;
  lru_cache_element* cur;
  lru_cache_element* last = NULL;
  lru_cache_element* e = NULL;
  void* tmp_index;

  hash = lru_cache_compute_hash(ht->num_buckets, ht->secret, index, index_len);
  for(cur = ht->table[hash]; cur != NULL && e == NULL; cur=cur->next)
  {
    if((index_len == cur->index_len) 
       && memcmp(cur->index, index, index_len) == 0)
    { e = cur; }
  }
  
  if(e != NULL)
  { /* We found the index, so we're going to overwrite the data.
     * We also need to reposition the element to the newest position,
     * so remove it from the list for now.
     */
    if(ht->talloc_data)
      talloc_unlink(e, e->data);

    if(e->newer == NULL)
      ht->newest = e->older;
    else
      e->newer->older = e->older;

    if(e->older == NULL)
      ht->oldest = e->newer;
    else
      e->older->newer = e->newer;
  }
  else
  { /* We didn't find an identical index. */
    
    if((ht->max_keys != 0) && (ht->num_keys >= ht->max_keys))
    { /* Eliminate the least recently used item, but reuse the element
       * structure to minimize reallocation. 
       */
      e = ht->oldest;
      if(ht->newest == ht->oldest)
      {
	ht->newest = NULL;
	ht->oldest = NULL;
      }
      else
      {
	ht->oldest = e->newer;
	e->newer->older = NULL;
      }
      e->newer = NULL;
      e->older = NULL;

      last = NULL;
      lru_hash = lru_cache_compute_hash(ht->num_buckets, ht->secret, 
					e->index, e->index_len);
      for(cur = ht->table[lru_hash]; cur != e && cur != NULL; 
	  last=cur, cur=cur->next)
      {	continue; }

      if(last == NULL)
	ht->table[lru_hash] = e->next;
      else
	last->next = e->next;
      e->next = NULL;

      if(ht->talloc_data)
	talloc_unlink(e, e->data);

      tmp_index = talloc_realloc_size(e, e->index, index_len);
      if(tmp_index == NULL)
      {
	talloc_free(e);
	return false;
      }
      else
	e->index = tmp_index;
    }
    else
    { /* Brand new element because we have room to spare. */

      e = talloc(ht->table, lru_cache_element);
      if(e == NULL)
	return false;
      
      e->index = talloc_size(e, index_len);
      if(e->index == NULL)
      {
	talloc_free(e);
	return false;
      }
      
      /* New entry, increment counters. */
      ht->num_keys++;
    }
    memcpy(e->index, index, index_len);
    e->index_len = index_len;

    e->next = ht->table[hash];
    ht->table[hash] = e;
  }
  if(ht->talloc_data)
    data = talloc_reference(e, data);
  e->data = data;

  /* Finally, let's insert the element to the newest position in the LRU list.*/
  if(ht->newest != NULL)
    ht->newest->newer = e;
  e->newer = NULL;
  e->older = ht->newest;
  ht->newest = e;
  if(ht->oldest == NULL)
    ht->oldest = e;

  return true;
}


void* lru_cache_find(lru_cache* ht, const void* index,
		     uint32_t index_len)
{
  uint32_t hash;
  lru_cache_element* cur;

  hash = lru_cache_compute_hash(ht->num_buckets, ht->secret, index, index_len);
  for(cur = ht->table[hash]; (cur != NULL); cur = cur->next)
  {
    if((index_len == cur->index_len)
       && memcmp(cur->index, index, index_len) == 0)
    { break; }
  }
  
  if(cur != NULL && cur->newer != NULL)
  { /* Need to move this element up to the newest slot. */

    cur->newer->older = cur->older;

    if(cur->older == NULL)
      ht->oldest = cur->newer;
    else
      cur->older->newer = cur->newer;

    cur->newer = NULL;
    cur->older = ht->newest;
    ht->newest->newer = cur;
    ht->newest = cur;
  }

  if(cur != NULL)
    return cur->data;
  else
    return NULL;
}



bool lru_cache_remove(lru_cache* ht, const void* index, 
		      uint32_t index_len)
{
  uint32_t hash;
  lru_cache_element* cur;
  lru_cache_element* last = NULL;

  hash = lru_cache_compute_hash(ht->num_buckets, ht->secret,
				index, index_len);
  for(cur=ht->table[hash]; (cur != NULL);
      last=cur, cur=cur->next)
  {
    if((index_len == cur->index_len) 
       && memcmp(cur->index, index, index_len) == 0)
    { break; }
  }

  if(cur == NULL)
    return false;

  /* Detach from list */
  if(cur->newer == NULL)
    ht->newest = cur->older;
  else
    cur->newer->older = cur->older;
  
  if(cur->older == NULL)
    ht->oldest = cur->newer;
  else
    cur->older->newer = cur->newer;

  /* Detach from hash table */
  if(last == NULL)
    ht->table[hash] = cur->next;
  else
    last->next = cur->next;

  talloc_free(cur);
  
  /* Removing entry, decrement counters. */
  ht->num_keys--;
  
  return true;
}