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: lru_cache.h 122 2008-08-09 20:24:01Z tim $
|
---|
18 | */
|
---|
19 |
|
---|
20 | #ifndef LRU_CACHE_H
|
---|
21 | #define LRU_CACHE_H
|
---|
22 |
|
---|
23 | #include <stdbool.h>
|
---|
24 | #include <stdint.h>
|
---|
25 | #include <stdlib.h>
|
---|
26 | #include <stdio.h>
|
---|
27 | #include <string.h>
|
---|
28 | #include <unistd.h>
|
---|
29 |
|
---|
30 | struct lru_cache_element;
|
---|
31 | typedef struct lru_cache_element lru_cache_element;
|
---|
32 |
|
---|
33 | struct lru_cache_element
|
---|
34 | {
|
---|
35 | void* index;
|
---|
36 | uint32_t index_len;
|
---|
37 | void* data;
|
---|
38 | lru_cache_element* next;
|
---|
39 | lru_cache_element* older;
|
---|
40 | lru_cache_element* newer;
|
---|
41 | };
|
---|
42 |
|
---|
43 | typedef struct _lru_cache
|
---|
44 | {
|
---|
45 | uint32_t secret;
|
---|
46 | uint32_t num_keys;
|
---|
47 | uint32_t num_buckets;
|
---|
48 | uint32_t max_keys;
|
---|
49 | lru_cache_element* oldest;
|
---|
50 | lru_cache_element* newest;
|
---|
51 | lru_cache_element** table;
|
---|
52 | bool free_data;
|
---|
53 | } lru_cache;
|
---|
54 |
|
---|
55 |
|
---|
56 | lru_cache* lru_cache_create(uint32_t max_keys, uint32_t secret, bool free_data);
|
---|
57 | void lru_cache_destroy(lru_cache* ht);
|
---|
58 |
|
---|
59 | /* Returns a pointer to the old, replaced data stored at index.
|
---|
60 | * Returns NULL if no entry was overwritten.
|
---|
61 | */
|
---|
62 | bool lru_cache_update(lru_cache* ht, const void* index,
|
---|
63 | uint32_t index_len, void* data);
|
---|
64 |
|
---|
65 | /* Returns pointer to data previously stored at index.
|
---|
66 | * If no data was found at index, NULL is returned.
|
---|
67 | */
|
---|
68 | void* lru_cache_find(lru_cache* ht, const void* index,
|
---|
69 | uint32_t index_len);
|
---|
70 |
|
---|
71 | /* Removes entry from table at index.
|
---|
72 | * Returns pointer to data that was there previously.
|
---|
73 | * Returns NULL if no entry is at index.
|
---|
74 | */
|
---|
75 | bool lru_cache_remove(lru_cache* ht, const void* index,
|
---|
76 | uint32_t index_len);
|
---|
77 |
|
---|
78 | #endif
|
---|