| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/common/lru_cache.h> |
| 6 |
static int s_lru_cache_put(struct aws_cache *cache, const void *key, void *p_value); |
| 7 |
static int s_lru_cache_find(struct aws_cache *cache, const void *key, void **p_value); |
| 8 |
static void *s_lru_cache_use_lru_element(struct aws_cache *cache); |
| 9 |
static void *s_lru_cache_get_mru_element(const struct aws_cache *cache); |
| 10 |
|
| 11 |
struct lru_cache_impl_vtable { |
| 12 |
void *(*use_lru_element)(struct aws_cache *cache); |
| 13 |
void *(*get_mru_element)(const struct aws_cache *cache); |
| 14 |
}; |
| 15 |
|
| 16 |
static struct aws_cache_vtable s_lru_cache_vtable = { |
| 17 |
.destroy = aws_cache_base_default_destroy, |
| 18 |
.find = s_lru_cache_find, |
| 19 |
.put = s_lru_cache_put, |
| 20 |
.remove = aws_cache_base_default_remove, |
| 21 |
.clear = aws_cache_base_default_clear, |
| 22 |
.get_element_count = aws_cache_base_default_get_element_count, |
| 23 |
}; |
| 24 |
|
| 25 |
struct aws_cache *aws_cache_new_lru( |
| 26 |
struct aws_allocator *allocator, |
| 27 |
aws_hash_fn *hash_fn, |
| 28 |
aws_hash_callback_eq_fn *equals_fn, |
| 29 |
aws_hash_callback_destroy_fn *destroy_key_fn, |
| 30 |
aws_hash_callback_destroy_fn *destroy_value_fn, |
| 31 |
size_t max_items) { |
| 32 |
AWS_ASSERT(allocator); |
| 33 |
AWS_ASSERT(max_items); |
| 34 |
struct aws_cache *lru_cache = NULL; |
| 35 |
struct lru_cache_impl_vtable *impl = NULL; |
| 36 |
|
| 37 |
if (!aws_mem_acquire_many( |
| 38 |
allocator, 2, &lru_cache, sizeof(struct aws_cache), &impl, sizeof(struct lru_cache_impl_vtable))) { |
| 39 |
return NULL; |
| 40 |
} |
| 41 |
impl->use_lru_element = s_lru_cache_use_lru_element; |
| 42 |
impl->get_mru_element = s_lru_cache_get_mru_element; |
| 43 |
lru_cache->allocator = allocator; |
| 44 |
lru_cache->max_items = max_items; |
| 45 |
lru_cache->vtable = &s_lru_cache_vtable; |
| 46 |
lru_cache->impl = impl; |
| 47 |
if (aws_linked_hash_table_init( |
| 48 |
&lru_cache->table, allocator, hash_fn, equals_fn, destroy_key_fn, destroy_value_fn, max_items)) { |
| 49 |
return NULL; |
| 50 |
} |
| 51 |
return lru_cache; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
static int s_lru_cache_put(struct aws_cache *cache, const void *key, void *p_value) { |
| 56 |
|
| 57 |
if (aws_linked_hash_table_put(&cache->table, key, p_value)) { |
| 58 |
return AWS_OP_ERR; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
if (aws_linked_hash_table_get_element_count(&cache->table) > cache->max_items) { |
| 63 |
|
| 64 |
|
| 65 |
const struct aws_linked_list *list = aws_linked_hash_table_get_iteration_list(&cache->table); |
| 66 |
struct aws_linked_list_node *node = aws_linked_list_front(list); |
| 67 |
struct aws_linked_hash_table_node *table_node = AWS_CONTAINER_OF(node, struct aws_linked_hash_table_node, node); |
| 68 |
return aws_linked_hash_table_remove(&cache->table, table_node->key); |
| 69 |
} |
| 70 |
|
| 71 |
return AWS_OP_SUCCESS; |
| 72 |
} |
| 73 |
|
| 74 |
static int s_lru_cache_find(struct aws_cache *cache, const void *key, void **p_value) { |
| 75 |
return (aws_linked_hash_table_find_and_move_to_back(&cache->table, key, p_value)); |
| 76 |
} |
| 77 |
|
| 78 |
static void *s_lru_cache_use_lru_element(struct aws_cache *cache) { |
| 79 |
const struct aws_linked_list *list = aws_linked_hash_table_get_iteration_list(&cache->table); |
| 80 |
if (aws_linked_list_empty(list)) { |
| 81 |
return NULL; |
| 82 |
} |
| 83 |
struct aws_linked_list_node *node = aws_linked_list_front(list); |
| 84 |
struct aws_linked_hash_table_node *lru_node = AWS_CONTAINER_OF(node, struct aws_linked_hash_table_node, node); |
| 85 |
|
| 86 |
aws_linked_hash_table_move_node_to_end_of_list(&cache->table, lru_node); |
| 87 |
return lru_node->value; |
| 88 |
} |
| 89 |
static void *s_lru_cache_get_mru_element(const struct aws_cache *cache) { |
| 90 |
const struct aws_linked_list *list = aws_linked_hash_table_get_iteration_list(&cache->table); |
| 91 |
if (aws_linked_list_empty(list)) { |
| 92 |
return NULL; |
| 93 |
} |
| 94 |
struct aws_linked_list_node *node = aws_linked_list_back(list); |
| 95 |
struct aws_linked_hash_table_node *mru_node = AWS_CONTAINER_OF(node, struct aws_linked_hash_table_node, node); |
| 96 |
return mru_node->value; |
| 97 |
} |
| 98 |
|
| 99 |
void *aws_lru_cache_use_lru_element(struct aws_cache *cache) { |
| 100 |
AWS_PRECONDITION(cache); |
| 101 |
AWS_PRECONDITION(cache->impl); |
| 102 |
struct lru_cache_impl_vtable *impl_vtable = cache->impl; |
| 103 |
return impl_vtable->use_lru_element(cache); |
| 104 |
} |
| 105 |
|
| 106 |
void *aws_lru_cache_get_mru_element(const struct aws_cache *cache) { |
| 107 |
AWS_PRECONDITION(cache); |
| 108 |
AWS_PRECONDITION(cache->impl); |
| 109 |
struct lru_cache_impl_vtable *impl_vtable = cache->impl; |
| 110 |
return impl_vtable->get_mru_element(cache); |
| 111 |
} |