File: cache.h

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (87 lines) | stat: -rw-r--r-- 2,597 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef AWS_COMMON_CACHE_H
#define AWS_COMMON_CACHE_H
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/common/linked_hash_table.h>

AWS_PUSH_SANE_WARNING_LEVEL

struct aws_cache;

struct aws_cache_vtable {
    void (*destroy)(struct aws_cache *cache);
    int (*find)(struct aws_cache *cache, const void *key, void **p_value);
    int (*put)(struct aws_cache *cache, const void *key, void *p_value);
    int (*remove)(struct aws_cache *cache, const void *key);
    void (*clear)(struct aws_cache *cache);
    size_t (*get_element_count)(const struct aws_cache *cache);
};

/**
 * Base stucture for caches, used the linked hash table implementation.
 */
struct aws_cache {
    struct aws_allocator *allocator;
    const struct aws_cache_vtable *vtable;
    struct aws_linked_hash_table table;
    size_t max_items;

    void *impl;
};

/* Default implementations */
void aws_cache_base_default_destroy(struct aws_cache *cache);
int aws_cache_base_default_find(struct aws_cache *cache, const void *key, void **p_value);
int aws_cache_base_default_remove(struct aws_cache *cache, const void *key);
void aws_cache_base_default_clear(struct aws_cache *cache);
size_t aws_cache_base_default_get_element_count(const struct aws_cache *cache);

AWS_EXTERN_C_BEGIN
/**
 * Cleans up the cache. Elements in the cache will be evicted and cleanup
 * callbacks will be invoked.
 */
AWS_COMMON_API
void aws_cache_destroy(struct aws_cache *cache);

/**
 * Finds element in the cache by key. If found, *p_value will hold the stored value, and AWS_OP_SUCCESS will be
 * returned. If not found, AWS_OP_SUCCESS will be returned and *p_value will be NULL.
 *
 * If any errors occur AWS_OP_ERR will be returned.
 */
AWS_COMMON_API
int aws_cache_find(struct aws_cache *cache, const void *key, void **p_value);

/**
 * Puts `p_value` at `key`. If an element is already stored at `key` it will be replaced. If the cache is already full,
 * an item will be removed based on the cache policy.
 */
AWS_COMMON_API
int aws_cache_put(struct aws_cache *cache, const void *key, void *p_value);

/**
 * Removes item at `key` from the cache.
 */
AWS_COMMON_API
int aws_cache_remove(struct aws_cache *cache, const void *key);

/**
 * Clears all items from the cache.
 */
AWS_COMMON_API
void aws_cache_clear(struct aws_cache *cache);

/**
 * Returns the number of elements in the cache.
 */
AWS_COMMON_API
size_t aws_cache_get_element_count(const struct aws_cache *cache);

AWS_EXTERN_C_END
AWS_POP_SANE_WARNING_LEVEL

#endif /* AWS_COMMON_CACHE_H */