removed debugging cruft, added README and stub headers for standalone testing
[fb-resolver] / lru_cache.h
1 #ifndef LRU_CACHE_H
2 #define LRU_CACHE_H
3
4 #include <stddef.h>
5
6 struct lru_cache_entry {
7 struct lru_cache_entry *next;
8 struct lru_cache_entry *prev;
9 struct lru_cache_entry *hash_next;
10 unsigned long hash_slot;
11 };
12 typedef struct lru_cache_entry lru_entry_t;
13
14
15 /*
16 A function which, given an entry, returns a pointer to a buffer
17 and a number of bytes to use to generate a hash of the entry.
18 */
19 typedef void (lru_hash_feed_fn)(lru_entry_t *, char **, size_t *);
20
21 /*
22 A function which compares two entries for equality.
23 */
24 typedef int (lru_entry_cmp_fn)(lru_entry_t *, lru_entry_t *);
25
26 struct lru_cache_hashslot_ {
27 size_t tally;
28 struct lru_cache_entry *entry;
29 };
30
31 struct lru_cache {
32 const size_t hash_sz;
33 const size_t capacity;
34
35 lru_hash_feed_fn *feed_fn;
36 lru_entry_cmp_fn *cmp_fn;
37
38 size_t num_entries;
39 struct lru_cache_entry *newest;
40 struct lru_cache_entry *oldest;
41 struct lru_cache_hashslot_ hash[1];
42 };
43
44 struct lru_cache *lru_cache_new(size_t, size_t, lru_hash_feed_fn *, lru_entry_cmp_fn *);
45 void lru_cache_insert(struct lru_cache *, lru_entry_t *, lru_entry_t **);
46 void lru_cache_extract(struct lru_cache *, lru_entry_t *);
47 lru_entry_t *lru_cache_locate(struct lru_cache *, lru_entry_t *);
48 void lru_cache_foreach(struct lru_cache *, void (*)(lru_entry_t *, size_t, void *), void *);
49
50 #endif /* LRU_CACHE_H */