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