|
| 1 | +#define _GNU_SOURCE |
| 2 | +/* |
| 3 | + * Original Author: Oliver Lorenz (ol), olli@olorenz.org, https://olorenz.org |
| 4 | + * License: This is licensed under the same terms as uthash itself |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdlib.h> |
| 8 | +#include <errno.h> |
| 9 | +#include <string.h> |
| 10 | + |
| 11 | +#include "cache.h" |
| 12 | + |
| 13 | +int |
| 14 | +cache_create(struct cache **dst, const size_t capacity, |
| 15 | + void (*free_cb)(void *element)) { |
| 16 | + struct cache *new = NULL; |
| 17 | + |
| 18 | + if (!dst) { |
| 19 | + return EINVAL; |
| 20 | + } |
| 21 | + |
| 22 | + if ((new = malloc(sizeof(*new))) == NULL) { |
| 23 | + return ENOMEM; |
| 24 | + } |
| 25 | + |
| 26 | + new->max_entries = capacity; |
| 27 | + new->entries = NULL; |
| 28 | + new->free_cb = free_cb; |
| 29 | + *dst = new; |
| 30 | + |
| 31 | + return 0; |
| 32 | +} |
| 33 | + |
| 34 | +int |
| 35 | +cache_delete(struct cache *cache, int keep_data) { |
| 36 | + struct cache_entry *entry, *tmp; |
| 37 | + |
| 38 | + if (!cache) { |
| 39 | + return EINVAL; |
| 40 | + } |
| 41 | + |
| 42 | + if (keep_data) { |
| 43 | + HASH_CLEAR(hh, cache->entries); |
| 44 | + } else { |
| 45 | + HASH_ITER(hh, cache->entries, entry, tmp){ |
| 46 | + HASH_DEL(cache->entries, entry); |
| 47 | + if (cache->free_cb) { |
| 48 | + cache->free_cb(entry->data); |
| 49 | + } |
| 50 | + free(entry); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + free(cache); |
| 55 | + cache = NULL; |
| 56 | + |
| 57 | + return 0; |
| 58 | +} |
| 59 | + |
| 60 | +int |
| 61 | +cache_remove(struct cache *cache, char *key) { |
| 62 | + struct cache_entry *tmp; |
| 63 | + |
| 64 | + if (!cache || !key) { |
| 65 | + return EINVAL; |
| 66 | + } |
| 67 | + |
| 68 | + HASH_FIND_STR(cache->entries, key, tmp); |
| 69 | + |
| 70 | + if (tmp) { |
| 71 | + HASH_DEL(cache->entries, tmp); |
| 72 | + if (cache->free_cb) { |
| 73 | + cache->free_cb(tmp->data); |
| 74 | + } |
| 75 | + free(tmp); |
| 76 | + } |
| 77 | + |
| 78 | + return 0; |
| 79 | +} |
| 80 | + |
| 81 | +int |
| 82 | +cache_removeall(struct cache *cache, void *opaque, int (*select_cb)(void *element, void *opaque)) { |
| 83 | + struct cache_entry *entry, *tmp; |
| 84 | + |
| 85 | + if (!cache) { |
| 86 | + return EINVAL; |
| 87 | + } |
| 88 | + |
| 89 | + HASH_ITER(hh, cache->entries, entry, tmp){ |
| 90 | + if (select_cb(entry->data, opaque)) { |
| 91 | + HASH_DEL(cache->entries, entry); |
| 92 | + if (cache->free_cb) { |
| 93 | + cache->free_cb(entry->data); |
| 94 | + } |
| 95 | + free(entry); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return 0; |
| 100 | +} |
| 101 | + |
| 102 | +/** Checks if a given key is in the cache |
| 103 | +
|
| 104 | + @param cache |
| 105 | + The cache object |
| 106 | +
|
| 107 | + @param key |
| 108 | + The key to look-up |
| 109 | +
|
| 110 | + @param result |
| 111 | + Where to store the result if key is found. |
| 112 | +
|
| 113 | + A warning: Even though result is just a pointer, |
| 114 | + you have to call this function with a **ptr, |
| 115 | + otherwise this will blow up in your face. |
| 116 | +
|
| 117 | + @return EINVAL if cache is NULL, 0 otherwise |
| 118 | + */ |
| 119 | +int |
| 120 | +cache_lookup(struct cache *cache, char *key, void *result) |
| 121 | +{ |
| 122 | + struct cache_entry *tmp = NULL; |
| 123 | + char **dirty_hack = result; |
| 124 | + |
| 125 | + if (!cache || !key || !result) { |
| 126 | + return EINVAL; |
| 127 | + } |
| 128 | + |
| 129 | + HASH_FIND_STR(cache->entries, key, tmp); |
| 130 | + if (tmp) { |
| 131 | + size_t key_len = strnlen(tmp->key, KEY_MAX_LENGTH); |
| 132 | + HASH_DELETE(hh, cache->entries, tmp); |
| 133 | + HASH_ADD_KEYPTR(hh, cache->entries, tmp->key, key_len, tmp); |
| 134 | + *dirty_hack = tmp->data; |
| 135 | + } else { |
| 136 | + *dirty_hack = result = NULL; |
| 137 | + } |
| 138 | + |
| 139 | + return 0; |
| 140 | +} |
| 141 | + |
| 142 | +int |
| 143 | +cache_insert(struct cache *cache, char *key, void *data) { |
| 144 | + struct cache_entry *entry = NULL; |
| 145 | + struct cache_entry *tmp_entry = NULL; |
| 146 | + size_t key_len = 0; |
| 147 | + |
| 148 | + if (!cache || !data) { |
| 149 | + return EINVAL; |
| 150 | + } |
| 151 | + |
| 152 | + if ((entry = malloc(sizeof(*entry))) == NULL) { |
| 153 | + return ENOMEM; |
| 154 | + } |
| 155 | + |
| 156 | + entry->key = key; |
| 157 | + entry->data = data; |
| 158 | + key_len = strnlen(entry->key, KEY_MAX_LENGTH); |
| 159 | + HASH_ADD_KEYPTR(hh, cache->entries, entry->key, key_len, entry); |
| 160 | + |
| 161 | + if (HASH_COUNT(cache->entries) >= cache->max_entries) { |
| 162 | + HASH_ITER(hh, cache->entries, entry, tmp_entry){ |
| 163 | + HASH_DELETE(hh, cache->entries, entry); |
| 164 | + if (cache->free_cb) { |
| 165 | + cache->free_cb(entry->data); |
| 166 | + } else { |
| 167 | + free(entry->data); |
| 168 | + } |
| 169 | + /* free(key->key) if data has been copied */ |
| 170 | + free(entry); |
| 171 | + break; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + return 0; |
| 176 | +} |
0 commit comments