Fixed-size, array-backed cache that evicts its oldest item.
More...
|
| void | circular_cache_init (CircularCache *c, uint8_t *buffer, size_t item_size, int total_items, Comparator compare_cb) |
| | Initialize a circular cache.
|
| |
| void | circular_cache_set_item_destructor (CircularCache *c, CircularCacheItemDestructor destructor) |
| | Set the destructor called on items when they are evicted or flushed.
|
| |
| bool | circular_cache_contains (CircularCache *c, void *item) |
| | Check whether the cache contains an item.
|
| |
| void * | circular_cache_get (CircularCache *c, void *theirs) |
| | Find an item in the cache.
|
| |
| void | circular_cache_push (CircularCache *c, void *item) |
| | Copy an item into the cache, evicting the oldest one.
|
| |
| void | circular_cache_fill (CircularCache *c, uint8_t *item) |
| | Set every slot of the cache to a copy of an item.
|
| |
| void | circular_cache_flush (CircularCache *c) |
| | Call the destructor on every slot and restart eviction from the first one.
|
| |
Fixed-size, array-backed cache that evicts its oldest item.
Items are found by linear search with a comparator. See also Keyed circular cache.
◆ CircularCache
| Data Fields |
|
uint8_t * |
cache |
Item array. |
|
Comparator |
compare_cb |
Comparator, returns 0 for matching items. |
|
CircularCacheItemDestructor |
item_destructor |
Optional item destructor. |
|
size_t |
item_size |
Size of an item in bytes. |
|
int |
next_erased_item_idx |
Index of the next item to evict. |
|
int |
total_items |
Number of items in cache. |
◆ CircularCacheItemDestructor
| typedef void(* CircularCacheItemDestructor) (void *item) |
Destructor called on an item about to be evicted or flushed.
It is also called on slots that never held an item, so it must recognize those (e.g. all zeros).
- Parameters
-
◆ circular_cache_contains()
Check whether the cache contains an item.
- Parameters
-
| c | Circular cache. |
| item | Item to compare against, of the cache's item size. |
- Returns
- true if an item matches.
◆ circular_cache_fill()
Set every slot of the cache to a copy of an item.
Useful to clear a cache to a non-zero value. Asserts if an item destructor is set.
- Parameters
-
| c | Circular cache. |
| item | Item, of the cache's item size. |
◆ circular_cache_flush()
Call the destructor on every slot and restart eviction from the first one.
The item data is left in place.
- Parameters
-
◆ circular_cache_get()
Find an item in the cache.
- Parameters
-
| c | Circular cache. |
| theirs | Item to compare against, of the cache's item size. |
- Returns
- Matching item in the cache, or NULL.
◆ circular_cache_init()
| void circular_cache_init |
( |
CircularCache * |
c, |
|
|
uint8_t * |
buffer, |
|
|
size_t |
item_size, |
|
|
int |
total_items, |
|
|
Comparator |
compare_cb |
|
) |
| |
Initialize a circular cache.
- Parameters
-
| [out] | c | Circular cache. |
| buffer | Item array of total_items items of item_size bytes, initialized by the caller. |
| item_size | Size of an item in bytes. |
| total_items | Number of items. |
| compare_cb | Comparator, returns 0 for matching items. |
◆ circular_cache_push()
Copy an item into the cache, evicting the oldest one.
- Parameters
-
| c | Circular cache. |
| item | Item, of the cache's item size. |
◆ circular_cache_set_item_destructor()
Set the destructor called on items when they are evicted or flushed.
- Parameters
-
| c | Circular cache. |
| destructor | Destructor, NULL for none. |