PebbleOS
Loading...
Searching...
No Matches
Data Structures | Typedefs | Functions
Circular cache

Fixed-size, array-backed cache that evicts its oldest item. More...

Data Structures

struct  CircularCache
 Circular cache state. More...
 

Typedefs

typedef void(* CircularCacheItemDestructor) (void *item)
 Destructor called on an item about to be evicted or flushed.
 

Functions

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.
 

Detailed Description

Fixed-size, array-backed cache that evicts its oldest item.

Items are found by linear search with a comparator. See also Keyed circular cache.


Data Structure Documentation

◆ CircularCache

struct CircularCache

Circular cache state.

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.

Typedef Documentation

◆ 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
itemItem.

Function Documentation

◆ circular_cache_contains()

bool circular_cache_contains ( CircularCache *  c,
void *  item 
)

Check whether the cache contains an item.

Parameters
cCircular cache.
itemItem to compare against, of the cache's item size.
Returns
true if an item matches.

◆ circular_cache_fill()

void circular_cache_fill ( CircularCache *  c,
uint8_t *  item 
)

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
cCircular cache.
itemItem, of the cache's item size.

◆ circular_cache_flush()

void circular_cache_flush ( CircularCache *  c)

Call the destructor on every slot and restart eviction from the first one.

The item data is left in place.

Parameters
cCircular cache.

◆ circular_cache_get()

void * circular_cache_get ( CircularCache *  c,
void *  theirs 
)

Find an item in the cache.

Parameters
cCircular cache.
theirsItem 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]cCircular cache.
bufferItem array of total_items items of item_size bytes, initialized by the caller.
item_sizeSize of an item in bytes.
total_itemsNumber of items.
compare_cbComparator, returns 0 for matching items.

◆ circular_cache_push()

void circular_cache_push ( CircularCache *  c,
void *  item 
)

Copy an item into the cache, evicting the oldest one.

Parameters
cCircular cache.
itemItem, of the cache's item size.

◆ circular_cache_set_item_destructor()

void circular_cache_set_item_destructor ( CircularCache *  c,
CircularCacheItemDestructor  destructor 
)

Set the destructor called on items when they are evicted or flushed.

Parameters
cCircular cache.
destructorDestructor, NULL for none.