|
| 1 | +#ifndef CACHE_H |
| 2 | +#define CACHE_H |
| 3 | + |
| 4 | +#include <stdint.h> |
| 5 | +#include <stdlib.h> |
| 6 | + |
| 7 | +typedef void *cache_key_t; |
| 8 | +typedef void *cache_value_t; |
| 9 | + |
| 10 | +typedef int (*cache_eq_fn)(cache_key_t, cache_key_t); |
| 11 | +typedef uint32_t (*cache_hash_fn)(cache_key_t); |
| 12 | +typedef void (*cache_freek_fn)(cache_key_t); |
| 13 | +typedef void (*cache_freev_fn)(cache_value_t); |
| 14 | + |
| 15 | +typedef struct _cache cache; |
| 16 | + |
| 17 | +struct _cache { |
| 18 | + /** |
| 19 | + * Add the specified value to the cache under the key k, replacing |
| 20 | + * any previous value. |
| 21 | + * |
| 22 | + * The value and key belong to the cache and will be freed with the |
| 23 | + * supplied free functions whether the add is successful or not. |
| 24 | + * |
| 25 | + * The key and value data must stay valid until they are explicitely |
| 26 | + * released by the cache when it calls the supplied free functions. |
| 27 | + * |
| 28 | + * NULL is not a valid value or key. |
| 29 | + * |
| 30 | + * Returns 0 if value was added sucessfully and some other value otherwise. |
| 31 | + */ |
| 32 | + int (*add)(cache *c, cache_key_t k, cache_value_t v); |
| 33 | + |
| 34 | + /** |
| 35 | + * Remove the data associated with k from the cache. The value and |
| 36 | + * the key will be free with the supplied free functions. |
| 37 | + * |
| 38 | + * The passed in key is not claimed by the cache and need only be |
| 39 | + * valid until the call returns. It will not be freed through the |
| 40 | + * key free function. |
| 41 | + * |
| 42 | + * Returns 1 if the key was in the cache and 0 if not. |
| 43 | + */ |
| 44 | + int (*del)(cache *c, const cache_key_t k); |
| 45 | + |
| 46 | + /** |
| 47 | + * Get the data entry associated with k. |
| 48 | + * |
| 49 | + * The passed in key is not claimed by the cache and need only be |
| 50 | + * valid until the call returns. It will not be freed through the |
| 51 | + * key free function. |
| 52 | + * |
| 53 | + * Returns NULL if the key is not found, a value otherwise. |
| 54 | + */ |
| 55 | + cache_value_t (*get)(cache *c, const cache_key_t k); |
| 56 | + |
| 57 | + /** |
| 58 | + * Releases all entries in the cache as well as all of the support |
| 59 | + * structures. |
| 60 | + * |
| 61 | + * This must NOT free the passed in pointer. |
| 62 | + */ |
| 63 | + void (*destroy)(cache *c); |
| 64 | + cache_eq_fn keq; |
| 65 | + cache_hash_fn khash; |
| 66 | + cache_freek_fn kfree; |
| 67 | + cache_freev_fn vfree; |
| 68 | + /* Extra data goes here depending on cache type */ |
| 69 | +}; |
| 70 | + |
| 71 | +static void nofree(void *v) {} |
| 72 | + |
| 73 | +cache *cache_lru(size_t max_size, size_t elasticity, |
| 74 | + cache_eq_fn keq, cache_hash_fn khash, |
| 75 | + cache_freek_fn kfree, cache_freev_fn vfree); |
| 76 | + |
| 77 | +cache *cache_twoq(size_t hot_size, size_t warm_size, |
| 78 | + size_t cold_size, size_t elasticity, |
| 79 | + cache_eq_fn keq, cache_hash_fn khash, |
| 80 | + cache_freek_fn kfree, cache_freev_fn vfree); |
| 81 | + |
| 82 | +/* API functions */ |
| 83 | +static inline int cache_add(cache *c, cache_key_t k, cache_value_t v) { |
| 84 | + return c->add(c, k, v); |
| 85 | +} |
| 86 | + |
| 87 | +static inline int cache_del(cache *c, cache_key_t k) { |
| 88 | + return c->del(c, k); |
| 89 | +} |
| 90 | + |
| 91 | +static inline cache_value_t cache_get(cache *c, cache_key_t k) { |
| 92 | + return c->get(c, k); |
| 93 | +} |
| 94 | + |
| 95 | +static inline void cache_destroy(cache *c) { |
| 96 | + c->destroy(c); |
| 97 | + free(c); |
| 98 | +} |
| 99 | + |
| 100 | +#endif |
0 commit comments