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

Fixed-size cache of items looked up by integer key, evicting the oldest item. More...

Data Structures

struct  KeyedCircularCache
 Keyed circular cache state. More...
 

Typedefs

typedef uint32_t KeyedCircularCacheKey
 Key of a keyed circular cache.
 

Functions

void keyed_circular_cache_init (KeyedCircularCache *c, KeyedCircularCacheKey *key_buffer, void *data_buffer, size_t item_size, size_t total_items)
 Initialize a keyed circular cache.
 
void * keyed_circular_cache_get (KeyedCircularCache *c, KeyedCircularCacheKey key)
 Find an item by key.
 
void keyed_circular_cache_push (KeyedCircularCache *c, KeyedCircularCacheKey key, const void *item)
 Copy an item into the cache, evicting the oldest one.
 

Detailed Description

Fixed-size cache of items looked up by integer key, evicting the oldest item.

Keys are stored apart from the items so lookups scan a compact array. Most recently pushed items are found first.

static KeyedCircularCacheKey s_keys[8];
static struct glyph s_glyphs[8];
static KeyedCircularCache s_cache;
keyed_circular_cache_init(&s_cache, s_keys, s_glyphs, sizeof(struct glyph), 8);
struct glyph *g = keyed_circular_cache_get(&s_cache, codepoint);
if (!g) {
load_glyph(codepoint, &tmp);
keyed_circular_cache_push(&s_cache, codepoint, &tmp);
}
Keyed circular cache state.
Definition keyed_circular_cache.h:39
void * keyed_circular_cache_get(KeyedCircularCache *c, KeyedCircularCacheKey key)
Find an item by key.
void keyed_circular_cache_init(KeyedCircularCache *c, KeyedCircularCacheKey *key_buffer, void *data_buffer, size_t item_size, size_t total_items)
Initialize a keyed circular cache.
void keyed_circular_cache_push(KeyedCircularCache *c, KeyedCircularCacheKey key, const void *item)
Copy an item into the cache, evicting the oldest one.
uint32_t KeyedCircularCacheKey
Key of a keyed circular cache.
Definition keyed_circular_cache.h:36

Data Structure Documentation

◆ KeyedCircularCache

struct KeyedCircularCache

Keyed circular cache state.

Data Fields
uint8_t * cache_data Item array.
KeyedCircularCacheKey * cache_keys Key array.
size_t item_size Size of an item in bytes.
size_t next_item_to_erase_idx Index of the next item to evict.
size_t total_items Number of items.

Typedef Documentation

◆ KeyedCircularCacheKey

typedef uint32_t KeyedCircularCacheKey

Key of a keyed circular cache.

Function Documentation

◆ keyed_circular_cache_get()

void * keyed_circular_cache_get ( KeyedCircularCache *  c,
KeyedCircularCacheKey  key 
)

Find an item by key.

Parameters
cCache.
keyKey.
Returns
Item in the cache, or NULL.

◆ keyed_circular_cache_init()

void keyed_circular_cache_init ( KeyedCircularCache *  c,
KeyedCircularCacheKey *  key_buffer,
void *  data_buffer,
size_t  item_size,
size_t  total_items 
)

Initialize a keyed circular cache.

The key array is used as is, so initialize it with keys that are never looked up.

Parameters
[out]cCache.
key_bufferKey array of total_items keys.
data_bufferItem array of total_items items.
item_sizeSize of an item in bytes.
total_itemsNumber of items.

◆ keyed_circular_cache_push()

void keyed_circular_cache_push ( KeyedCircularCache *  c,
KeyedCircularCacheKey  key,
const void *  item 
)

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

Parameters
cCache.
keyKey of the item.
itemItem, of the cache's item size.