ChibiOS/RT 7.0.6
chobjcaches.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006-2026 Giovanni Di Sirio.
3
4 This file is part of ChibiOS.
5
6 ChibiOS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation version 3 of the License.
9
10 ChibiOS is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/**
20 * @file oslib/include/chobjcaches.h
21 * @brief Objects Caches macros and structures.
22 *
23 * @addtogroup oslib_objcaches
24 * @{
25 */
26
27#ifndef CHOBJCACHES_H
28#define CHOBJCACHES_H
29
30#if (CH_CFG_USE_OBJ_CACHES == TRUE) || defined(__DOXYGEN__)
31
32/*===========================================================================*/
33/* Module constants. */
34/*===========================================================================*/
35
36/**
37 * @name Cached objects flags
38 * @{
39 */
40#define OC_FLAG_INLRU 0x00000001U
41#define OC_FLAG_INHASH 0x00000002U
42#define OC_FLAG_SHARED 0x00000004U
43#define OC_FLAG_NOTSYNC 0x00000008U
44#define OC_FLAG_LAZYWRITE 0x00000010U
45#define OC_FLAG_FORGET 0x00000020U
46/** @} */
47
48/*===========================================================================*/
49/* Module pre-compile time settings. */
50/*===========================================================================*/
51
52/*===========================================================================*/
53/* Derived constants and error checks. */
54/*===========================================================================*/
55
56#if CH_CFG_USE_SEMAPHORES == FALSE
57#error "CH_CFG_USE_OBJ_CACHES requires CH_CFG_USE_SEMAPHORES"
58#endif
59
60/*===========================================================================*/
61/* Module data structures and types. */
62/*===========================================================================*/
63
64/**
65 * @brief Flags of cached objects.
66 */
67typedef uint32_t oc_flags_t;
68
69/**
70 * @brief Type of an hash element header.
71 */
73
74/**
75 * @brief Type of an LRU element header.
76 */
78
79/**
80 * @brief Type of a cached object.
81 */
83
84/**
85 * @brief Type of a cache object.
86 */
88
89/**
90 * @brief Object read function.
91 *
92 * @param[in] ocp pointer to the @p objects_cache_t structure
93 * @param[in] async requests an asynchronous operation if supported, the
94 * function is then responsible for releasing the
95 * object
96 */
97typedef bool (*oc_readf_t)(objects_cache_t *ocp,
98 oc_object_t *objp,
99 bool async);
100
101/**
102 * @brief Object write function.
103 *
104 * @param[in] ocp pointer to the @p objects_cache_t structure
105 * @param[in] async requests an asynchronous operation if supported, the
106 * function is then responsible for releasing the
107 * object
108 */
109typedef bool (*oc_writef_t)(objects_cache_t *ocp,
110 oc_object_t *objp,
111 bool async);
112
113/**
114 * @brief Structure representing an hash table element.
115 */
117 /**
118 * @brief Next in the collisions list.
119 */
121 /**
122 * @brief Previous in the collisions list.
123 */
125};
126
127/**
128 * @brief Structure representing an hash table element.
129 */
131 /**
132 * @brief Next in the collisions list.
133 */
135 /**
136 * @brief Previous in the collisions list.
137 */
139 /**
140 * @brief Next in the LRU list.
141 */
143 /**
144 * @brief Previous in the LRU list.
145 */
147};
148
149/**
150 * @brief Structure representing a cached object.
151 */
153 /**
154 * @brief Next in the collisions list.
155 */
157 /**
158 * @brief Previous in the collisions list.
159 */
161 /**
162 * @brief Next in the LRU list.
163 */
165 /**
166 * @brief Previous in the LRU list.
167 */
169 /**
170 * @brief Object group.
171 */
172 uint32_t obj_group;
173 /**
174 * @brief Object key.
175 */
176 uint32_t obj_key;
177 /**
178 * @brief Semaphore for object access.
179 */
181 /**
182 * @brief Object flags.
183 */
185 /**
186 * @brief User pointer.
187 * @note This pointer can be used to refer to external buffers,
188 * @p chCacheObjectInit() initializes it to @p NULL.
189 */
190 void *dptr;
191};
192
193/**
194 * @brief Structure representing a cache object.
195 */
197 /**
198 * @brief Number of elements in the hash table.
199 */
201 /**
202 * @brief Pointer to the hash table.
203 */
205 /**
206 * @brief Number of elements in the objects table.
207 */
209 /**
210 * @brief Size of elements in the objects table.
211 */
212 size_t objsz;
213 /**
214 * @brief Pointer to the objects table.
215 */
216 void *objvp;
217 /**
218 * @brief LRU list header.
219 */
221 /**
222 * @brief Semaphore for LRU access.
223 */
225 /**
226 * @brief Reader functions for cached objects.
227 */
229 /**
230 * @brief Writer functions for cached objects.
231 */
233};
234
235/*===========================================================================*/
236/* Module macros. */
237/*===========================================================================*/
238
239/*===========================================================================*/
240/* External declarations. */
241/*===========================================================================*/
242
243#ifdef __cplusplus
244extern "C" {
245#endif
247 ucnt_t hashn,
248 oc_hash_header_t *hashp,
249 ucnt_t objn,
250 size_t objsz,
251 void *objvp,
252 oc_readf_t readf,
253 oc_writef_t writef);
255 uint32_t group,
256 uint32_t key);
258 oc_object_t *objp);
260 oc_object_t *objp,
261 bool async);
263 oc_object_t *objp,
264 bool async);
265#ifdef __cplusplus
266}
267#endif
268
269/*===========================================================================*/
270/* Module inline functions. */
271/*===========================================================================*/
272
273/**
274 * @brief Releases an object into the cache.
275 * @note This function gives a meaning to the following flags:
276 * - @p OC_FLAG_INLRU must be cleared.
277 * - @p OC_FLAG_INHASH must be set.
278 * - @p OC_FLAG_SHARED must be cleared.
279 * - @p OC_FLAG_NOTSYNC invalidates the object and queues it on
280 * the LRU tail.
281 * - @p OC_FLAG_LAZYWRITE is ignored and kept, a write will occur
282 * when the object is removed from the LRU list (lazy write).
283 * .
284 *
285 * @param[in] ocp pointer to the @p objects_cache_t structure
286 * @param[in] objp pointer to the @p oc_object_t structure
287 *
288 * @api
289 */
290static inline void chCacheReleaseObject(objects_cache_t *ocp,
291 oc_object_t *objp) {
292
293 chSysLock();
294 chCacheReleaseObjectI(ocp, objp);
296 chSysUnlock();
297}
298
299#endif /* CH_CFG_USE_OBJ_CACHES == TRUE */
300
301#endif /* CHOBJCACHES_H */
302
303/** @} */
uint32_t ucnt_t
Definition chearly.h:92
bool chCacheReadObject(objects_cache_t *ocp, oc_object_t *objp, bool async)
Reads object data from the storage.
void chCacheObjectInit(objects_cache_t *ocp, ucnt_t hashn, oc_hash_header_t *hashp, ucnt_t objn, size_t objsz, void *objvp, oc_readf_t readf, oc_writef_t writef)
Initializes a objects_cache_t object.
struct ch_oc_object oc_object_t
Type of a cached object.
Definition chobjcaches.h:82
bool chCacheWriteObject(objects_cache_t *ocp, oc_object_t *objp, bool async)
Writes the object data back to storage.
struct ch_oc_hash_header oc_hash_header_t
Type of an hash element header.
Definition chobjcaches.h:72
bool(* oc_readf_t)(objects_cache_t *ocp, oc_object_t *objp, bool async)
Object read function.
Definition chobjcaches.h:97
oc_object_t * chCacheGetObject(objects_cache_t *ocp, uint32_t group, uint32_t key)
Retrieves an object from the cache.
struct ch_objects_cache objects_cache_t
Type of a cache object.
Definition chobjcaches.h:87
void chCacheReleaseObjectI(objects_cache_t *ocp, oc_object_t *objp)
Releases an object into the cache.
bool(* oc_writef_t)(objects_cache_t *ocp, oc_object_t *objp, bool async)
Object write function.
struct ch_oc_lru_header oc_lru_header_t
Type of an LRU element header.
Definition chobjcaches.h:77
static void chCacheReleaseObject(objects_cache_t *ocp, oc_object_t *objp)
Releases an object into the cache.
uint32_t oc_flags_t
Flags of cached objects.
Definition chobjcaches.h:67
void chSchRescheduleS(void)
Performs a reschedule if a higher priority thread is runnable.
Definition chschd.c:457
struct ch_semaphore semaphore_t
Semaphore structure.
static void chSysLock(void)
Enters the kernel lock state.
Definition chsys.h:406
static void chSysUnlock(void)
Leaves the kernel lock state.
Definition chsys.h:420
Structure representing a cache object.
oc_writef_t writef
Writer functions for cached objects.
void * objvp
Pointer to the objects table.
ucnt_t objn
Number of elements in the objects table.
oc_hash_header_t * hashp
Pointer to the hash table.
ucnt_t hashn
Number of elements in the hash table.
semaphore_t lru_sem
Semaphore for LRU access.
size_t objsz
Size of elements in the objects table.
oc_readf_t readf
Reader functions for cached objects.
oc_lru_header_t lru
LRU list header.
Structure representing an hash table element.
oc_object_t * hash_prev
Previous in the collisions list.
oc_object_t * hash_next
Next in the collisions list.
Structure representing an hash table element.
oc_object_t * lru_next
Next in the LRU list.
oc_object_t * hash_next
Next in the collisions list.
oc_object_t * lru_prev
Previous in the LRU list.
oc_object_t * hash_prev
Previous in the collisions list.
Structure representing a cached object.
uint32_t obj_key
Object key.
oc_flags_t obj_flags
Object flags.
oc_object_t * hash_next
Next in the collisions list.
oc_object_t * hash_prev
Previous in the collisions list.
void * dptr
User pointer.
oc_object_t * lru_prev
Previous in the LRU list.
semaphore_t obj_sem
Semaphore for object access.
oc_object_t * lru_next
Next in the LRU list.
uint32_t obj_group
Object group.