Debian Codegraph @

crt/aws-c-common/include/aws/common/cache.h

raw symbols SCIP
1 #ifndef AWS_COMMON_CACHE_H
2 #define AWS_COMMON_CACHE_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7
8 #include <aws/common/linked_hash_table.h>
9
10 AWS_PUSH_SANE_WARNING_LEVEL
11
12 struct aws_cache;
13
14 struct aws_cache_vtable {
15 void (*destroy)(struct aws_cache *cache);
16 int (*find)(struct aws_cache *cache, const void *key, void **p_value);
17 int (*put)(struct aws_cache *cache, const void *key, void *p_value);
18 int (*remove)(struct aws_cache *cache, const void *key);
19 void (*clear)(struct aws_cache *cache);
20 size_t (*get_element_count)(const struct aws_cache *cache);
21 };
22
23 /**
24 * Base stucture for caches, used the linked hash table implementation.
25 */
26 struct aws_cache {
27 struct aws_allocator *allocator;
28 const struct aws_cache_vtable *vtable;
29 struct aws_linked_hash_table table;
30 size_t max_items;
31
32 void *impl;
33 };
34
35 /* Default implementations */
36 void aws_cache_base_default_destroy(struct aws_cache *cache);
37 int aws_cache_base_default_find(struct aws_cache *cache, const void *key, void **p_value);
38 int aws_cache_base_default_remove(struct aws_cache *cache, const void *key);
39 void aws_cache_base_default_clear(struct aws_cache *cache);
40 size_t aws_cache_base_default_get_element_count(const struct aws_cache *cache);
41
42 AWS_EXTERN_C_BEGIN
43 /**
44 * Cleans up the cache. Elements in the cache will be evicted and cleanup
45 * callbacks will be invoked.
46 */
47 AWS_COMMON_API
48 void aws_cache_destroy(struct aws_cache *cache);
49
50 /**
51 * Finds element in the cache by key. If found, *p_value will hold the stored value, and AWS_OP_SUCCESS will be
52 * returned. If not found, AWS_OP_SUCCESS will be returned and *p_value will be NULL.
53 *
54 * If any errors occur AWS_OP_ERR will be returned.
55 */
56 AWS_COMMON_API
57 int aws_cache_find(struct aws_cache *cache, const void *key, void **p_value);
58
59 /**
60 * Puts `p_value` at `key`. If an element is already stored at `key` it will be replaced. If the cache is already full,
61 * an item will be removed based on the cache policy.
62 */
63 AWS_COMMON_API
64 int aws_cache_put(struct aws_cache *cache, const void *key, void *p_value);
65
66 /**
67 * Removes item at `key` from the cache.
68 */
69 AWS_COMMON_API
70 int aws_cache_remove(struct aws_cache *cache, const void *key);
71
72 /**
73 * Clears all items from the cache.
74 */
75 AWS_COMMON_API
76 void aws_cache_clear(struct aws_cache *cache);
77
78 /**
79 * Returns the number of elements in the cache.
80 */
81 AWS_COMMON_API
82 size_t aws_cache_get_element_count(const struct aws_cache *cache);
83
84 AWS_EXTERN_C_END
85 AWS_POP_SANE_WARNING_LEVEL
86
87 #endif /* AWS_COMMON_CACHE_H */