Mstdlib-1.24.0
m_cache_strvp.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_STRVP_H__
25#define __M_CACHE_STRVP_H__
26
27/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
28
29#include <mstdlib/base/m_defs.h>
30#include <mstdlib/base/m_types.h>
31#include <mstdlib/base/m_cache.h>
32
33/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
34
35__BEGIN_DECLS
36
37/*! \defgroup m_cache_strvp Cache - String/Void Pointer
38 * \ingroup m_cache
39 *
40 * Hot cache meant for storing string keys and void pointer values.
41 *
42 * @{
43 */
44
45struct M_cache_strvp;
46typedef struct M_cache_strvp M_cache_strvp_t;
47
48
49/*! Flags for controlling the behavior of the hash */
50typedef enum {
51 M_CACHE_STRVP_NONE = 0, /*!< Default. */
52 M_CACHE_STRVP_CASECMP = 1 << 0, /*!< Compare keys case insensitive. */
54
55
56/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
57
58/*! Create a cache.
59 *
60 * \param[in] max_size Maximum number of entries in the cache.
61 * \param[in] flags M_hash_strvp_flags_t flags for modifying behavior.
62 * \param[in] destroy_func The function to be called to destroy value when removed.
63 *
64 * \return Allocated cache.
65 *
66 * \see M_cache_strvp_destroy
67 */
68M_API M_cache_strvp_t *M_cache_strvp_create(size_t max_size, M_uint32 flags, void (*destroy_func)(void *)) M_MALLOC_ALIASED;
69
70
71/*! Destroy the cache.
72 *
73 * \param[in] c Cache to destroy
74 */
76
77
78/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
79
80/*! Insert an entry into the cache.
81 *
82 * \param[in] c Cache being referenced.
83 * \param[in] key Key to insert.
84 * \param[in] value Value to insert into h.
85 * The c will take ownership of the value. Maybe NULL.
86 *
87 * \return M_TRUE on success, or M_FALSE on failure.
88 */
89M_API M_bool M_cache_strvp_insert(M_cache_strvp_t *c, const char *key, const void *value);
90
91
92/*! Remove an entry from the cache.
93 *
94 * \param[in] c Cache being referenced.
95 * \param[in] key Key to remove from the h.
96 *
97 * \return M_TRUE on success, or M_FALSE if key does not exist.
98 */
99M_API M_bool M_cache_strvp_remove(M_cache_strvp_t *c, const char *key);
100
101
102/*! Retrieve the value for a key from the cache.
103 *
104 * \param[in] c Cache being referenced.
105 * \param[in] key Key for value.
106 * \param[out] value Pointer to value stored in the h. Optional, pass NULL if not needed.
107 *
108 * \return M_TRUE if value retrieved, M_FALSE if key does not exist.
109 */
110M_API M_bool M_cache_strvp_get(const M_cache_strvp_t *c, const char *key, void **value);
111
112
113/*! Retrieve the value for a key from the cache, and return it directly as the return value.
114 *
115 * This cannot be used if you need to differentiate between a key that doesn't exist vs a key with a NULL value.
116 *
117 * \param[in] c Cache being referenced.
118 * \param[in] key Key for value to retrieve from the hashtable.
119 * A NULL or empty string is explicitly disallowed.
120 *
121 * \return NULL if key doesn't exist or NULL value on file, otherwise the value.
122 */
123M_API void *M_cache_strvp_get_direct(const M_cache_strvp_t *c, const char *key);
124
125
126/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
127
128/*! Get the number of items in the cache.
129 *
130 * \param[in] c Cache being referenced.
131 *
132 * \return Count.
133 */
134M_API size_t M_cache_strvp_size(const M_cache_strvp_t *c);
135
136
137/*! Get the maximum number of items allowed in the cache.
138 *
139 * \param[in] c Cache being referenced.
140 *
141 * \return Max.
142 */
144
145
146/*! Set the maximum number of items allowed in the cache.
147 *
148 * This can be used to increase or decrease the maximum size of the cache.
149 * If the max size is smaller than the number of items in the cache, older
150 * items will be removed.
151 *
152 * \param[in] c Cache being referenced.
153 * \param[in] max_size Maximum size.
154 *
155 * \return M_TRUE if the max size was changed, otherwise M_FALSE on error.
156 */
157M_API M_bool M_cache_strvp_set_max_size(M_cache_strvp_t *c, size_t max_size);
158
159/*! @} */
160
161__END_DECLS
162
163#endif /* __M_CACHE_STRVP_H__ */
164
M_bool M_cache_strvp_remove(M_cache_strvp_t *c, const char *key)
struct M_cache_strvp M_cache_strvp_t
Definition: m_cache_strvp.h:46
M_cache_strvp_t * M_cache_strvp_create(size_t max_size, M_uint32 flags, void(*destroy_func)(void *)) M_MALLOC_ALIASED
M_cache_strvp_flags_t
Definition: m_cache_strvp.h:50
M_bool M_cache_strvp_insert(M_cache_strvp_t *c, const char *key, const void *value)
void * M_cache_strvp_get_direct(const M_cache_strvp_t *c, const char *key)
void M_cache_strvp_destroy(M_cache_strvp_t *c)
M_bool M_cache_strvp_get(const M_cache_strvp_t *c, const char *key, void **value)
size_t M_cache_strvp_max_size(const M_cache_strvp_t *c)
M_bool M_cache_strvp_set_max_size(M_cache_strvp_t *c, size_t max_size)
size_t M_cache_strvp_size(const M_cache_strvp_t *c)
@ M_CACHE_STRVP_NONE
Definition: m_cache_strvp.h:51
@ M_CACHE_STRVP_CASECMP
Definition: m_cache_strvp.h:52