ChibiOS  21.6.0
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 /*===========================================================================*/
58 /* Module data structures and types. */
59 /*===========================================================================*/
60 
61 /**
62  * @brief Flags of cached objects.
63  */
64 typedef uint32_t oc_flags_t;
65 
66 /**
67  * @brief Type of an hash element header.
68  */
70 
71 /**
72  * @brief Type of an LRU element header.
73  */
75 
76 /**
77  * @brief Type of a cached object.
78  */
79 typedef struct ch_oc_object oc_object_t;
80 
81 /**
82  * @brief Type of a cache object.
83  */
85 
86 /**
87  * @brief Object read function.
88  *
89  * @param[in] ocp pointer to the @p objects_cache_t structure
90  * @param[in] async requests an asynchronous operation if supported, the
91  * function is then responsible for releasing the
92  * object
93  */
94 typedef bool (*oc_readf_t)(objects_cache_t *ocp,
95  oc_object_t *objp,
96  bool async);
97 
98 /**
99  * @brief Object write function.
100  *
101  * @param[in] ocp pointer to the @p objects_cache_t structure
102  * @param[in] async requests an asynchronous operation if supported, the
103  * function is then responsible for releasing the
104  * object
105  */
106 typedef bool (*oc_writef_t)(objects_cache_t *ocp,
107  oc_object_t *objp,
108  bool async);
109 
110 /**
111  * @brief Structure representing an hash table element.
112  */
114  /**
115  * @brief Next in the collisions list.
116  */
118  /**
119  * @brief Previous in the collisions list.
120  */
122 };
123 
124 /**
125  * @brief Structure representing an hash table element.
126  */
128  /**
129  * @brief Next in the collisions list.
130  */
132  /**
133  * @brief Previous in the collisions list.
134  */
136  /**
137  * @brief Next in the LRU list.
138  */
140  /**
141  * @brief Previous in the LRU list.
142  */
144 };
145 
146 /**
147  * @brief Structure representing a cached object.
148  */
149 struct ch_oc_object {
150  /**
151  * @brief Next in the collisions list.
152  */
154  /**
155  * @brief Previous in the collisions list.
156  */
158  /**
159  * @brief Next in the LRU list.
160  */
162  /**
163  * @brief Previous in the LRU list.
164  */
166  /**
167  * @brief Object group.
168  */
169  uint32_t obj_group;
170  /**
171  * @brief Object key.
172  */
173  uint32_t obj_key;
174  /**
175  * @brief Semaphore for object access.
176  */
178  /**
179  * @brief Object flags.
180  */
182  /**
183  * @brief User pointer.
184  * @note This pointer can be used to refer to external buffers,
185  * @p chCacheObjectInit() initializes it to @p NULL.
186  */
187  void *dptr;
188 };
189 
190 /**
191  * @brief Structure representing a cache object.
192  */
194  /**
195  * @brief Number of elements in the hash table.
196  */
198  /**
199  * @brief Pointer to the hash table.
200  */
202  /**
203  * @brief Number of elements in the objects table.
204  */
206  /**
207  * @brief Size of elements in the objects table.
208  */
209  size_t objsz;
210  /**
211  * @brief Pointer to the objects table.
212  */
213  void *objvp;
214  /**
215  * @brief LRU list header.
216  */
218  /**
219  * @brief Semaphore for cache access.
220  */
222  /**
223  * @brief Semaphore for LRU access.
224  */
226  /**
227  * @brief Reader functions for cached objects.
228  */
230  /**
231  * @brief Writer functions for cached objects.
232  */
234 };
235 
236 /*===========================================================================*/
237 /* Module macros. */
238 /*===========================================================================*/
239 
240 /*===========================================================================*/
241 /* External declarations. */
242 /*===========================================================================*/
243 
244 #ifdef __cplusplus
245 extern "C" {
246 #endif
248  ucnt_t hashn,
249  oc_hash_header_t *hashp,
250  ucnt_t objn,
251  size_t objsz,
252  void *objvp,
253  oc_readf_t readf,
254  oc_writef_t writef);
256  uint32_t group,
257  uint32_t key);
259  oc_object_t *objp);
261  oc_object_t *objp,
262  bool async);
264  oc_object_t *objp,
265  bool async);
266 #ifdef __cplusplus
267 }
268 #endif
269 
270 /*===========================================================================*/
271 /* Module inline functions. */
272 /*===========================================================================*/
273 
274 /**
275  * @brief Releases an object into the cache.
276  * @note This function gives a meaning to the following flags:
277  * - @p OC_FLAG_INLRU must be cleared.
278  * - @p OC_FLAG_INHASH must be set.
279  * - @p OC_FLAG_SHARED must be cleared.
280  * - @p OC_FLAG_NOTSYNC invalidates the object and queues it on
281  * the LRU tail.
282  * - @p OC_FLAG_LAZYWRITE is ignored and kept, a write will occur
283  * when the object is removed from the LRU list (lazy write).
284  * .
285  *
286  * @param[in] ocp pointer to the @p objects_cache_t structure
287  * @param[in] objp pointer to the @p oc_object_t structure
288  *
289  * @api
290  */
291 static inline void chCacheReleaseObject(objects_cache_t *ocp,
292  oc_object_t *objp) {
293 
294  chSysLock();
295  chCacheReleaseObjectI(ocp, objp);
297  chSysUnlock();
298 }
299 
300 #endif /* CH_CFG_USE_OBJ_CACHES == TRUE */
301 
302 #endif /* CHOBJCACHES_H */
303 
304 /** @} */
ch_objects_cache::writef
oc_writef_t writef
Writer functions for cached objects.
Definition: chobjcaches.h:233
ch_objects_cache::objvp
void * objvp
Pointer to the objects table.
Definition: chobjcaches.h:213
ch_oc_object::obj_key
uint32_t obj_key
Object key.
Definition: chobjcaches.h:173
ch_objects_cache::readf
oc_readf_t readf
Reader functions for cached objects.
Definition: chobjcaches.h:229
ch_oc_lru_header::hash_prev
oc_object_t * hash_prev
Previous in the collisions list.
Definition: chobjcaches.h:135
oc_writef_t
bool(* oc_writef_t)(objects_cache_t *ocp, oc_object_t *objp, bool async)
Object write function.
Definition: chobjcaches.h:106
chCacheObjectInit
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.
Definition: chobjcaches.c:241
ch_oc_hash_header::hash_next
oc_object_t * hash_next
Next in the collisions list.
Definition: chobjcaches.h:117
chCacheGetObject
oc_object_t * chCacheGetObject(objects_cache_t *ocp, uint32_t group, uint32_t key)
Retrieves an object from the cache.
Definition: chobjcaches.c:304
ch_objects_cache::objn
ucnt_t objn
Number of elements in the objects table.
Definition: chobjcaches.h:205
ch_objects_cache::lru
oc_lru_header_t lru
LRU list header.
Definition: chobjcaches.h:217
oc_readf_t
bool(* oc_readf_t)(objects_cache_t *ocp, oc_object_t *objp, bool async)
Object read function.
Definition: chobjcaches.h:94
ch_oc_object::hash_next
oc_object_t * hash_next
Next in the collisions list.
Definition: chobjcaches.h:153
ucnt_t
uint32_t ucnt_t
Definition: chearly.h:93
ch_oc_object
Structure representing a cached object.
Definition: chobjcaches.h:149
ch_oc_object::lru_prev
oc_object_t * lru_prev
Previous in the LRU list.
Definition: chobjcaches.h:165
ch_oc_lru_header::hash_next
oc_object_t * hash_next
Next in the collisions list.
Definition: chobjcaches.h:131
ch_objects_cache::hashp
oc_hash_header_t * hashp
Pointer to the hash table.
Definition: chobjcaches.h:201
ch_oc_lru_header::lru_prev
oc_object_t * lru_prev
Previous in the LRU list.
Definition: chobjcaches.h:143
ch_semaphore
Semaphore structure.
Definition: rt/include/chsem.h:52
oc_flags_t
uint32_t oc_flags_t
Flags of cached objects.
Definition: chobjcaches.h:64
chSchRescheduleS
void chSchRescheduleS(void)
Performs a reschedule if a higher priority thread is runnable.
Definition: chschd.c:454
ch_oc_lru_header::lru_next
oc_object_t * lru_next
Next in the LRU list.
Definition: chobjcaches.h:139
ch_oc_object::obj_group
uint32_t obj_group
Object group.
Definition: chobjcaches.h:169
ch_oc_hash_header::hash_prev
oc_object_t * hash_prev
Previous in the collisions list.
Definition: chobjcaches.h:121
chCacheReadObject
bool chCacheReadObject(objects_cache_t *ocp, oc_object_t *objp, bool async)
Reads object data from the storage.
Definition: chobjcaches.c:449
chCacheReleaseObject
static void chCacheReleaseObject(objects_cache_t *ocp, oc_object_t *objp)
Releases an object into the cache.
Definition: chobjcaches.h:291
ch_oc_lru_header
Structure representing an hash table element.
Definition: chobjcaches.h:127
ch_objects_cache::cache_sem
semaphore_t cache_sem
Semaphore for cache access.
Definition: chobjcaches.h:221
ch_oc_object::obj_sem
semaphore_t obj_sem
Semaphore for object access.
Definition: chobjcaches.h:177
ch_objects_cache
Structure representing a cache object.
Definition: chobjcaches.h:193
ch_objects_cache::lru_sem
semaphore_t lru_sem
Semaphore for LRU access.
Definition: chobjcaches.h:225
ch_oc_object::dptr
void * dptr
User pointer.
Definition: chobjcaches.h:187
chCacheWriteObject
bool chCacheWriteObject(objects_cache_t *ocp, oc_object_t *objp, bool async)
Writes the object data back to storage.
Definition: chobjcaches.c:478
chCacheReleaseObjectI
void chCacheReleaseObjectI(objects_cache_t *ocp, oc_object_t *objp)
Releases an object into the cache.
Definition: chobjcaches.c:379
ch_oc_object::obj_flags
oc_flags_t obj_flags
Object flags.
Definition: chobjcaches.h:181
ch_oc_object::lru_next
oc_object_t * lru_next
Next in the LRU list.
Definition: chobjcaches.h:161
ch_objects_cache::objsz
size_t objsz
Size of elements in the objects table.
Definition: chobjcaches.h:209
ch_oc_object::hash_prev
oc_object_t * hash_prev
Previous in the collisions list.
Definition: chobjcaches.h:157
chSysUnlock
#define chSysUnlock()
Leaves the kernel lock state.
Definition: nil/include/ch.h:1053
ch_objects_cache::hashn
ucnt_t hashn
Number of elements in the hash table.
Definition: chobjcaches.h:197
ch_oc_hash_header
Structure representing an hash table element.
Definition: chobjcaches.h:113
chSysLock
#define chSysLock()
Enters the kernel lock state.
Definition: nil/include/ch.h:1043