Go to the documentation of this file.
56 #if (CH_CFG_USE_OBJ_CACHES == TRUE) || defined(__DOXYGEN__)
63 #if !defined(OC_HASH_FUNCTION) || defined(__DOXYGEN__)
64 #define OC_HASH_FUNCTION(ocp, group, key) \
65 (((unsigned)(group) + (unsigned)(key)) & ((unsigned)(ocp)->hashn - 1U))
69 #define HASH_INSERT(ocp, objp, group, key) { \
70 oc_hash_header_t *hhp; \
71 (hhp) = &(ocp)->hashp[OC_HASH_FUNCTION(ocp, group, key)]; \
72 (objp)->hash_next = (hhp)->hash_next; \
73 (objp)->hash_prev = (oc_object_t *)(hhp); \
74 (hhp)->hash_next->hash_prev = (objp); \
75 (hhp)->hash_next = (objp); \
79 #define HASH_REMOVE(objp) { \
80 (objp)->hash_prev->hash_next = (objp)->hash_next; \
81 (objp)->hash_next->hash_prev = (objp)->hash_prev; \
85 #define LRU_INSERT_HEAD(ocp, objp) { \
86 (objp)->lru_next = (ocp)->lru.lru_next; \
87 (objp)->lru_prev = (oc_object_t *)&(ocp)->lru; \
88 (ocp)->lru.lru_next->lru_prev = (objp); \
89 (ocp)->lru.lru_next = (objp); \
93 #define LRU_INSERT_TAIL(ocp, objp) { \
94 (objp)->lru_prev = (ocp)->lru.lru_prev; \
95 (objp)->lru_next = (oc_object_t *)&(ocp)->lru; \
96 (ocp)->lru.lru_prev->lru_next = (objp); \
97 (ocp)->lru.lru_prev = (objp); \
101 #define LRU_REMOVE(objp) { \
102 (objp)->lru_prev->lru_next = (objp)->lru_next; \
103 (objp)->lru_next->lru_prev = (objp)->lru_prev; \
141 hhp = &ocp->
hashp[OC_HASH_FUNCTION(ocp, group, key)];
179 "semaphore counter not 1");
190 if ((objp->
obj_flags & OC_FLAG_LAZYWRITE) == 0U) {
193 if ((objp->
obj_flags & OC_FLAG_INHASH) != 0U) {
210 objp->
obj_flags = OC_FLAG_INHASH | OC_FLAG_FORGET;
211 (void) ocp->
writef(ocp, objp,
true);
250 chDbgCheck((ocp != NULL) && (hashp != NULL) && (objvp != NULL) &&
252 (objn > (
ucnt_t)0) && (hashn >= objn) &&
274 }
while (hashp < &ocp->hashp[ocp->
hashn]);
281 LRU_INSERT_HEAD(ocp, objp);
286 objvp = (
void *)((uint8_t *)objvp + objsz);
288 }
while (objn > (
ucnt_t)0);
352 objp->
obj_flags = OC_FLAG_INHASH | OC_FLAG_NOTSYNC;
353 HASH_INSERT(ocp, objp, group, key);
385 OC_FLAG_SHARED)) == OC_FLAG_INHASH,
386 "invalid object state");
388 "semaphore counter greater than 0");
396 objp->
obj_flags &= OC_FLAG_INHASH | OC_FLAG_NOTSYNC | OC_FLAG_LAZYWRITE;
403 if ((objp->
obj_flags & OC_FLAG_NOTSYNC) != 0U) {
405 LRU_INSERT_TAIL(ocp, objp);
412 if ((objp->
obj_flags & OC_FLAG_FORGET) == 0U) {
414 LRU_INSERT_HEAD(ocp, objp);
418 LRU_INSERT_TAIL(ocp, objp);
420 objp->
obj_flags &= OC_FLAG_INHASH | OC_FLAG_LAZYWRITE;
458 return ocp->
readf(ocp, objp, async);
486 return ocp->
writef(ocp, objp, async);
oc_writef_t writef
Writer functions for cached objects.
void * objvp
Pointer to the objects table.
uint32_t obj_key
Object key.
oc_readf_t readf
Reader functions for cached objects.
#define chSemFastSignalI(sp)
Increases the semaphore counter.
#define chSemFastWaitI(sp)
Decreases the semaphore counter.
#define chSemGetCounterI(sp)
Returns the semaphore counter current value.
bool(* oc_writef_t)(objects_cache_t *ocp, oc_object_t *objp, bool async)
Object write function.
static oc_object_t * lru_get_last_s(objects_cache_t *ocp)
Gets the least recently used object buffer from the LRU list.
#define chDbgAssert(c, r)
Condition assertion.
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.
oc_object_t * chCacheGetObject(objects_cache_t *ocp, uint32_t group, uint32_t key)
Retrieves an object from the cache.
ucnt_t objn
Number of elements in the objects table.
oc_lru_header_t lru
LRU list header.
bool(* oc_readf_t)(objects_cache_t *ocp, oc_object_t *objp, bool async)
Object read function.
oc_object_t * hash_next
Next in the collisions list.
#define chDbgCheck(c)
Function parameters check.
msg_t chSemWaitS(semaphore_t *sp)
Performs a wait operation on a semaphore.
Structure representing a cached object.
oc_hash_header_t * hashp
Pointer to the hash table.
void chSemObjectInit(semaphore_t *sp, cnt_t n)
Initializes a semaphore with the specified counter value.
#define PORT_NATURAL_ALIGN
Natural alignment constant.
static oc_object_t * hash_get_s(objects_cache_t *ocp, uint32_t group, uint32_t key)
Returns an object pointer from the cache, if present.
uint32_t obj_group
Object group.
bool chCacheReadObject(objects_cache_t *ocp, oc_object_t *objp, bool async)
Reads object data from the storage.
semaphore_t cache_sem
Semaphore for cache access.
semaphore_t obj_sem
Semaphore for object access.
Structure representing a cache object.
semaphore_t lru_sem
Semaphore for LRU access.
bool chCacheWriteObject(objects_cache_t *ocp, oc_object_t *objp, bool async)
Writes the object data back to storage.
void chCacheReleaseObjectI(objects_cache_t *ocp, oc_object_t *objp)
Releases an object into the cache.
oc_flags_t obj_flags
Object flags.
#define chSysUnlock()
Leaves the kernel lock state.
void chSemSignalI(semaphore_t *sp)
Performs a signal operation on a semaphore.
ucnt_t hashn
Number of elements in the hash table.
#define chSysLock()
Enters the kernel lock state.