Mstdlib-1.24.0
m_cache.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2017 Monetra Technologies, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_CACHE_H__
25#define __M_CACHE_H__
26
27/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
28
29#include <mstdlib/base/m_defs.h>
30#include <mstdlib/base/m_types.h>
31#include <mstdlib/base/m_hashtable.h>
32#include <mstdlib/base/m_sort.h>
33
34/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
35
36__BEGIN_DECLS
37
38/*! \defgroup m_cache Cache
39 * \ingroup m_datastructures
40 *
41 * Cache (Hot)
42 */
43
44/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
45
46/*! \addtogroup m_cache_generic Cache
47 * \ingroup m_cache
48 *
49 * Hot cache.
50 *
51 * @{
52 */
53
54struct M_cache;
55typedef struct M_cache M_cache_t;
56
57
58/*! Function definition to duplicate a value. */
59typedef void *(*M_cache_duplicate_func)(const void *);
60
61
62/*! Function definition to free a value. */
63typedef void (*M_cache_free_func)(void *);
64
65
66/*! Flags for controlling the behavior of the hash */
67typedef enum {
68 M_CACHE_NONE = 0, /*!< Default. */
70
71
72/*! Structure of callbacks that can be registered to override default
73 * behavior for implementation. */
75 M_cache_duplicate_func key_duplicate; /*!< Callback to duplicate a key. Default if
76 * NULL is pass-thru pointer */
77 M_cache_free_func key_free; /*!< Callback to free a key. Default if NULL
78 * is no-op */
79 M_cache_duplicate_func value_duplicate; /*!< Callback to duplicate a value. Default
80 * if NULL is pass-thru pointer */
81 M_cache_free_func value_free; /*!< Callback to free a value. Default if
82 * NULL is a no-op */
83};
84
85/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
86
87/*! Create a cache.
88 *
89 * \param[in] max_size Maximum number of entries in the cache.
90 * \param[in] key_hash The function to use for hashing a key. If not specified will use
91 * the pointer address as the key.
92 * \param[in] key_equality The function to use to determine if two keys are equal. If not
93 * specified, will compare pointer addresses.
94 * \param[in] flags M_hash_strvp_flags_t flags for modifying behavior.
95 * \param[in] callbacks Register callbacks for overriding default behavior.
96 *
97 * \return Allocated cache.
98 *
99 * \see M_cache_destroy
100 */
101M_API M_cache_t *M_cache_create(size_t max_size,
102 M_hashtable_hash_func key_hash, M_sort_compar_t key_equality,
103 M_uint32 flags, const struct M_cache_callbacks *callbacks) M_MALLOC;
104
105
106/*! Destroy the cache.
107 *
108 * \param[in] c Cache to destroy
109 */
111
112
113/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
114
115/*! Insert an entry into the cache.
116 *
117 * \param[in] c Cache being referenced.
118 * \param[in] key Key to insert.
119 * \param[in] value Value to insert into h.
120 * The c will take ownership of the value. Maybe NULL.
121 *
122 * \return M_TRUE on success, or M_FALSE on failure.
123 */
124M_API M_bool M_cache_insert(M_cache_t *c, const void *key, const void *value);
125
126
127/*! Remove an entry from the cache.
128 *
129 * \param[in] c Cache being referenced.
130 * \param[in] key Key to remove from the h.
131 *
132 * \return M_TRUE on success, or M_FALSE if key does not exist.
133 */
134M_API M_bool M_cache_remove(M_cache_t *c, const void *key);
135
136
137/*! Retrieve the value for a key from the cache.
138 *
139 * \param[in] c Cache being referenced.
140 * \param[in] key Key for value.
141 * \param[out] value Pointer to value stored in the h. Optional, pass NULL if not needed.
142 *
143 * \return M_TRUE if value retrieved, M_FALSE if key does not exist.
144 */
145M_API M_bool M_cache_get(const M_cache_t *c, const void *key, void **value);
146
147/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
148
149/*! Get the number of items in the cache.
150 *
151 * \param[in] c Cache being referenced.
152 *
153 * \return Count.
154 */
155M_API size_t M_cache_size(const M_cache_t *c);
156
157
158/*! Get the maximum number of items allowed in the cache.
159 *
160 * \param[in] c Cache being referenced.
161 *
162 * \return Max.
163 */
164M_API size_t M_cache_max_size(const M_cache_t *c);
165
166
167/*! Set the maximum number of items allowed in the cache.
168 *
169 * This can be used to increase or decrease the maximum size of the cache.
170 * If the max size is smaller than the number of items in the cache, older
171 * items will be removed.
172 *
173 * \param[in] c Cache being referenced.
174 * \param[in] max_size Maximum size.
175 *
176 * \return M_TRUE if the max size was changed, otherwise M_FALSE on error.
177 */
178M_API M_bool M_cache_set_max_size(M_cache_t *c, size_t max_size);
179
180/*! @} */
181
182__END_DECLS
183
184#endif /* __M_CACHE_H__ */
M_cache_duplicate_func key_duplicate
Definition: m_cache.h:75
M_cache_duplicate_func value_duplicate
Definition: m_cache.h:79
M_cache_free_func value_free
Definition: m_cache.h:81
M_cache_free_func key_free
Definition: m_cache.h:77
size_t M_cache_max_size(const M_cache_t *c)
void(* M_cache_free_func)(void *)
Definition: m_cache.h:63
M_bool M_cache_set_max_size(M_cache_t *c, size_t max_size)
M_cache_flags_t
Definition: m_cache.h:67
void *(* M_cache_duplicate_func)(const void *)
Definition: m_cache.h:59
void M_cache_destroy(M_cache_t *c)
M_bool M_cache_remove(M_cache_t *c, const void *key)
struct M_cache M_cache_t
Definition: m_cache.h:55
M_bool M_cache_insert(M_cache_t *c, const void *key, const void *value)
M_cache_t * M_cache_create(size_t max_size, M_hashtable_hash_func key_hash, M_sort_compar_t key_equality, M_uint32 flags, const struct M_cache_callbacks *callbacks) M_MALLOC
size_t M_cache_size(const M_cache_t *c)
M_bool M_cache_get(const M_cache_t *c, const void *key, void **value)
@ M_CACHE_NONE
Definition: m_cache.h:68
Definition: m_cache.h:74
M_uint32(* M_hashtable_hash_func)(const void *, M_uint32)
Definition: m_hashtable.h:147
int(* M_sort_compar_t)(const void *arg1, const void *arg2, void *thunk)
Definition: m_sort.h:78