lv_cache.h
API reference for lv_cache.h
Functions
lv_cache_set_max_size
Set the maximum size of the cache. If the current cache size is greater than the new maximum size, the cache's policy will be used to evict entries until the new maximum size is reached. If set to 0, the cache will be disabled.
void lv_cache_set_max_size(lv_cache_t *cache, size_t max_size, void *user_data)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to set the maximum size. |
max_size | size_t | The new maximum size of the cache. |
user_data | void * | A user data pointer that will be passed to the free callback. May be NULL. |
But this behavior will happen only new entries are added to the cache.
lv_cache_set_compare_cb
Set the compare callback of the cache.
void lv_cache_set_compare_cb(lv_cache_t *cache, lv_cache_compare_cb_t compare_cb, void *user_data)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to set the compare callback. |
compare_cb | lv_cache_compare_cb_t | The compare callback to set. |
user_data | void * | A user data pointer. May be NULL. |
lv_cache_set_create_cb
Set the create callback of the cache.
void lv_cache_set_create_cb(lv_cache_t *cache, lv_cache_create_cb_t alloc_cb, void *user_data)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to set the create callback. |
alloc_cb | lv_cache_create_cb_t | The create callback to set. |
user_data | void * | A user data pointer. May be NULL. |
lv_cache_set_free_cb
Set the free callback of the cache.
void lv_cache_set_free_cb(lv_cache_t *cache, lv_cache_free_cb_t free_cb, void *user_data)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to set the free callback. |
free_cb | lv_cache_free_cb_t | The free callback to set. |
user_data | void * | A user data pointer. May be NULL. |
lv_cache_set_name
Give a name for a cache object. Only the pointer of the string is saved.
void lv_cache_set_name(lv_cache_t *cache, const char *name)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to set the name. |
name | const char * | The name of the cache. |
lv_cache_get_max_size
Get the maximum size of the cache.
size_t lv_cache_get_max_size(lv_cache_t *cache, void *user_data)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to get the maximum size. |
user_data | void * | A user data pointer that will be passed to the free callback. May be NULL. |
Returns: size_t — Returns the maximum size of the cache.
lv_cache_get_size
Get the current size of the cache.
size_t lv_cache_get_size(lv_cache_t *cache, void *user_data)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to get the current size. |
user_data | void * | A user data pointer that will be passed to the free callback. May be NULL. |
Returns: size_t — Returns the current size of the cache.
lv_cache_get_free_size
Get the free size of the cache.
size_t lv_cache_get_free_size(lv_cache_t *cache, void *user_data)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to get the free size. |
user_data | void * | A user data pointer that will be passed to the free callback. May be NULL. |
Returns: size_t — Returns the free size of the cache.
lv_cache_get_name
Get the name of a cache object.
const char * lv_cache_get_name(lv_cache_t *cache)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to get the name. |
Returns: const char * — Returns the name of the cache.
lv_cache_entry_get_cache
Get the cache instance of a cache entry.
const lv_cache_t * lv_cache_entry_get_cache(const lv_cache_entry_t *entry)| Name | Type | Description |
|---|---|---|
entry | const lv_cache_entry_t * | The cache entry to get the cache instance of. |
Returns: const lv_cache_t * — The pointer to the cache instance of the cache entry.
lv_cache_create
Create a cache object with the given parameters.
lv_cache_t * lv_cache_create(const lv_cache_class_t *cache_class, size_t node_size, size_t max_size, lv_cache_ops_t ops)| Name | Type | Description |
|---|---|---|
cache_class | const lv_cache_class_t * | The class of the cache. Currently only support one two builtin classes: - lv_cache_class_lru_rb_count for LRU-based cache with count-based eviction policy. - lv_cache_class_lru_rb_size for LRU-based cache with size-based eviction policy. |
node_size | size_t | The node size is the size of the data stored in the cache.. |
max_size | size_t | The max size is the maximum amount of memory or count that the cache can hold. - lv_cache_class_lru_rb_count: max_size is the maximum count of nodes in the cache. - lv_cache_class_lru_rb_size: max_size is the maximum size of the cache in bytes. |
ops | lv_cache_ops_t | A set of operations that can be performed on the cache. See lv_cache_ops_t for details. |
Returns: lv_cache_t * — Returns a pointer to the created cache object on success, NULL on error.
lv_cache_destroy
Destroy a cache object.
void lv_cache_destroy(lv_cache_t *cache, void *user_data)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to destroy. |
user_data | void * | A user data pointer that will be passed to the free callback. May be NULL. |
lv_cache_acquire
Acquire a cache entry with the given key. If entry not in cache, it will return NULL (not found). If the entry is found, it's priority will be changed by the cache's policy. And the lv_cache_entry_t::ref_cnt will be incremented.
lv_cache_entry_t * lv_cache_acquire(lv_cache_t *cache, const void *key, void *user_data)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to acquire the entry. |
key | const void * | The key of the entry to acquire. |
user_data | void * | A user data pointer that will be passed to the create callback. May be NULL. |
Returns: lv_cache_entry_t * — Returns a pointer to the acquired cache entry on success with lv_cache_entry_t::ref_cnt incremented, NULL on error.
lv_cache_acquire_or_create
Acquire a cache entry with the given key. If the entry is not in the cache, it will create a new entry with the given key. If the entry is found, it's priority will be changed by the cache's policy. And the lv_cache_entry_t::ref_cnt will be incremented. If you want to use this API to simplify the code, you should provide a lv_cache_ops_t::create_cb that creates a new entry with the given key. This API is a combination of lv_cache_acquire() and lv_cache_add(). The effect is the same as calling lv_cache_acquire() and lv_cache_add() separately. And the internal impact on cache is also consistent with these two APIs.
lv_cache_entry_t * lv_cache_acquire_or_create(lv_cache_t *cache, const void *key, void *user_data)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to acquire the entry. |
key | const void * | The key of the entry to acquire or create. |
user_data | void * | A user data pointer that will be passed to the create callback. May be NULL. |
Returns: lv_cache_entry_t * — Returns a pointer to the acquired or created cache entry on success with lv_cache_entry_t::ref_cnt incremented, NULL on error.
lv_cache_add
Add a new cache entry with the given key and data. If the cache is full, the cache's policy will be used to evict an entry.
lv_cache_entry_t * lv_cache_add(lv_cache_t *cache, const void *key, void *user_data)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to add the entry. |
key | const void * | The key of the entry to add. |
user_data | void * | A user data pointer that will be passed to the create callback. May be NULL. |
Returns: lv_cache_entry_t * — Returns a pointer to the added cache entry on success with lv_cache_entry_t::ref_cnt incremented, NULL on error.
lv_cache_release
Release a cache entry. The lv_cache_entry_t::ref_cnt will be decremented. If the lv_cache_entry_t::ref_cnt is zero, it will issue an error. If the entry passed to this function is the last reference to the data and the entry is marked as invalid, the cache's policy will be used to evict the entry.
void lv_cache_release(lv_cache_t *cache, lv_cache_entry_t *entry, void *user_data)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to release the entry. |
entry | lv_cache_entry_t * | The cache entry pointer to release. |
user_data | void * | A user data pointer that will be passed to the free callback. May be NULL. |
lv_cache_reserve
Reserve a certain amount of memory/count in the cache. This function is useful when you want to reserve a certain amount of memory/count in advance, for example, when you know that you will need it later. When the current cache size is max than the reserved size, the function will evict entries until the reserved size is reached.
void lv_cache_reserve(lv_cache_t *cache, uint32_t reserved_size, void *user_data)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to reserve. |
reserved_size | uint32_t | The amount of memory/count to reserve. |
user_data | void * | A user data pointer that will be passed to the free callback. May be NULL. |
lv_cache_drop
Drop a cache entry with the given key. If the entry is not in the cache, nothing will happen to it. If the entry is found, it will be removed from the cache and its data will be freed when the last reference to it is released.
void lv_cache_drop(lv_cache_t *cache, const void *key, void *user_data)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to drop the entry. |
key | const void * | The key of the entry to drop. |
user_data | void * | A user data pointer that will be passed to the free callback. May be NULL. |
The data will not be freed immediately but when the last reference to it is released. But this entry will not be found by lv_cache_acquire(). If you want cache a same key again, you should use lv_cache_add() or lv_cache_acquire_or_create().
lv_cache_drop_all
Drop all cache entries. All entries will be removed from the cache and their data will be freed when the last reference to them is released.
void lv_cache_drop_all(lv_cache_t *cache, void *user_data)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to drop all entries. |
user_data | void * | A user data pointer that will be passed to the free callback. May be NULL. |
If some entries are still referenced by other objects, it will issue an error. And this case shouldn't happen in normal cases..
lv_cache_evict_one
Evict one entry from the cache. The eviction policy will be used to select the entry to evict.
bool lv_cache_evict_one(lv_cache_t *cache, void *user_data)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to evict an entry. |
user_data | void * | A user data pointer that will be passed to the free callback. May be NULL. |
Returns: bool — Returns true if an entry is evicted, false if no entry is evicted.
lv_cache_is_enabled
Return true if the cache is enabled. Disabled cache means that when the max_size of the cache is 0. In this case, all cache operations will be no-op.
bool lv_cache_is_enabled(lv_cache_t *cache)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to check if it's disabled. |
Returns: bool — Returns true if the cache is enabled, false otherwise.
lv_cache_iter_create
Create an iterator for the cache object. The iterator is used to iterate over all cache entries.
lv_iter_t * lv_cache_iter_create(lv_cache_t *cache)| Name | Type | Description |
|---|---|---|
cache | lv_cache_t * | The cache object pointer to create the iterator. |
Returns: lv_iter_t * — Returns a pointer to the created iterator on success, NULL on error.
lv_cache_entry_alloc
Allocate a cache entry.
lv_cache_entry_t * lv_cache_entry_alloc(const uint32_t node_size, const lv_cache_t *cache)| Name | Type | Description |
|---|---|---|
node_size | const uint32_t | The size of the node in the cache. |
cache | const lv_cache_t * | The cache instance to allocate the cache entry from. |
Returns: lv_cache_entry_t * — The pointer to the allocated cache entry.
lv_cache_entry_init
Initialize a cache entry.
void lv_cache_entry_init(lv_cache_entry_t *entry, const lv_cache_t *cache, const uint32_t node_size)| Name | Type | Description |
|---|---|---|
entry | lv_cache_entry_t * | The cache entry to initialize. |
cache | const lv_cache_t * | The cache instance to allocate the cache entry from. |
node_size | const uint32_t | The size of the node in the cache. |
lv_cache_entry_delete
Deallocate a cache entry. And the data of the cache entry will be freed.
void lv_cache_entry_delete(lv_cache_entry_t *entry)| Name | Type | Description |
|---|---|---|
entry | lv_cache_entry_t * | The cache entry to deallocate. |
Enums
lv_cache_reserve_cond_res_t
The result of the cache reserve condition callback
| Name | Description |
|---|---|
LV_CACHE_RESERVE_COND_OK | The condition is met and no entries need to be evicted |
LV_CACHE_RESERVE_COND_TOO_LARGE | The condition is not met and the reserve size is too large |
LV_CACHE_RESERVE_COND_NEED_VICTIM | The condition is not met and a victim is needed to be evicted |
LV_CACHE_RESERVE_COND_ERROR | An error occurred while checking the condition |
Structs
_lv_cache_ops_t
The cache operations struct
| Member | Type | Description |
|---|---|---|
compare_cb | lv_cache_compare_cb_t | Compare function for keys |
create_cb | lv_cache_create_cb_t | Create function for nodes |
free_cb | lv_cache_free_cb_t | Free function for nodes |
_lv_cache_t
The cache entry struct
| Member | Type | Description |
|---|---|---|
clz | const lv_cache_class_t * | Cache class. There are two built-in classes: - lv_cache_class_lru_rb_count for LRU-based cache with count-based eviction policy. - lv_cache_class_lru_rb_size for LRU-based cache with size-based eviction policy. |
node_size | uint32_t | Size of a node |
max_size | uint32_t | Maximum size of the cache |
size | uint32_t | Current size of the cache |
ops | lv_cache_ops_t | Cache operations struct _lv_cache_ops_t |
lock | lv_mutex_t | Cache lock used to protect the cache in multithreading environments |
name | const char * | Name of the cache |
_lv_cache_class_t
Cache class struct for building custom cache classes
Examples:
- lv_cache_class_lru_rb_count for LRU-based cache with count-based eviction policy.
- lv_cache_class_lru_rb_size for LRU-based cache with size-based eviction policy.
| Member | Type | Description |
|---|---|---|
alloc_cb | lv_cache_alloc_cb_t | The allocation function for cache entries |
init_cb | lv_cache_init_cb_t | The initialization function for cache entries |
destroy_cb | lv_cache_destroy_cb_t | The destruction function for cache entries |
get_cb | lv_cache_get_cb_t | The get function for cache entries |
add_cb | lv_cache_add_cb_t | The add function for cache entries |
remove_cb | lv_cache_remove_cb_t | The remove function for cache entries |
drop_cb | lv_cache_drop_cb_t | The drop function for cache entries |
drop_all_cb | lv_cache_drop_all_cb_t | The drop all function for cache entries |
get_victim_cb | lv_cache_get_victim_cb | The get victim function for cache entries |
reserve_cond_cb | lv_cache_reserve_cond_cb | The reserve condition function for cache entries |
iter_create_cb | lv_cache_iter_create_cb | The iterator creation function for cache entries |
_lv_cache_slot_size_t
Cache entry slot struct
To add new fields to the cache entry, add them to a new struct and add it to the first field of the cache data struct. And this one is a size slot for the cache entry.
| Member | Type | Description |
|---|---|---|
size | size_t |
Typedefs
lv_cache_entry_t
typedef struct _lv_cache_entry_t lv_cache_entry_tUsed by 20 functions
lv_vg_lite_stroke_get_path— paramcache_entrylv_vg_lite_stroke_drop— paramcache_entrylv_cache_release— paramentrylv_cache_entry_get_cache— paramentrylv_cache_entry_init— paramentrylv_cache_entry_delete— paramentrylv_cache_entry_get_ref— paramentrylv_cache_entry_get_node_size— paramentrylv_cache_entry_is_invalid— paramentrylv_cache_entry_get_data— paramentrylv_cache_entry_reset_ref— paramentrylv_cache_entry_inc_ref— paramentrylv_cache_entry_dec_ref— paramentrylv_cache_entry_set_node_size— paramentrylv_cache_entry_set_cache— paramentrylv_cache_entry_acquire_data— paramentrylv_cache_entry_release_data— paramentrylv_cache_entry_set_flag— paramentrylv_cache_entry_remove_flag— paramentrylv_cache_entry_has_flag— paramentry
lv_cache_t
typedef struct _lv_cache_t lv_cache_tUsed by 23 functions
lv_cache_destroy— paramcachelv_cache_acquire— paramcachelv_cache_acquire_or_create— paramcachelv_cache_add— paramcachelv_cache_release— paramcachelv_cache_reserve— paramcachelv_cache_drop— paramcachelv_cache_drop_all— paramcachelv_cache_evict_one— paramcachelv_cache_set_max_size— paramcachelv_cache_get_max_size— paramcachelv_cache_get_size— paramcachelv_cache_get_free_size— paramcachelv_cache_is_enabled— paramcachelv_cache_set_compare_cb— paramcachelv_cache_set_create_cb— paramcachelv_cache_set_free_cb— paramcachelv_cache_set_name— paramcachelv_cache_get_name— paramcachelv_cache_iter_create— paramcachelv_cache_entry_alloc— paramcachelv_cache_entry_init— paramcachelv_cache_entry_set_cache— paramcache
lv_cache_ops_t
typedef struct _lv_cache_ops_t lv_cache_ops_tUsed by 1 function
lv_cache_create— paramops
lv_cache_class_t
typedef struct _lv_cache_class_t lv_cache_class_tUsed by 1 function
lv_cache_create— paramcache_class
lv_cache_compare_res_t
typedef int32_t lv_cache_compare_res_tlv_cache_create_cb_t
typedef bool(* lv_cache_create_cb_t) (void *node, void *user_data)Used by 1 function
lv_cache_set_create_cb— paramalloc_cb
lv_cache_free_cb_t
typedef void(* lv_cache_free_cb_t) (void *node, void *user_data)Used by 1 function
lv_cache_set_free_cb— paramfree_cb
lv_cache_compare_cb_t
typedef lv_cache_compare_res_t(* lv_cache_compare_cb_t) (const void *a, const void *b)Used by 1 function
lv_cache_set_compare_cb— paramcompare_cb
lv_cache_alloc_cb_t
typedef void *(* lv_cache_alloc_cb_t) (void)The cache instance allocation function, used by the cache class to allocate memory for cache instances.
lv_cache_init_cb_t
typedef bool(* lv_cache_init_cb_t) (lv_cache_t *cache)The cache instance initialization function, used by the cache class to initialize the cache instance.
lv_cache_destroy_cb_t
typedef void(* lv_cache_destroy_cb_t) (lv_cache_t *cache, void *user_data)The cache instance destruction function, used by the cache class to destroy the cache instance.
lv_cache_get_cb_t
typedef lv_cache_entry_t *(* lv_cache_get_cb_t) (lv_cache_t *cache, const void *key, void *user_data)The cache get function, used by the cache class to get a cache entry by its key.
lv_cache_add_cb_t
typedef lv_cache_entry_t *(* lv_cache_add_cb_t) (lv_cache_t *cache, const void *key, void *user_data)The cache add function, used by the cache class to add a cache entry with a given key. This function only cares about how to add the entry, it doesn't check if the entry already exists and doesn't care about is it a victim or not.
lv_cache_remove_cb_t
typedef void(* lv_cache_remove_cb_t) (lv_cache_t *cache, lv_cache_entry_t *entry, void *user_data)The cache remove function, used by the cache class to remove a cache entry from the cache but doesn't free the memory.. This function only cares about how to remove the entry, it doesn't care about is it a victim or not.
lv_cache_drop_cb_t
typedef void(* lv_cache_drop_cb_t) (lv_cache_t *cache, const void *key, void *user_data)The cache drop function, used by the cache class to remove a cache entry from the cache and free the memory.
lv_cache_drop_all_cb_t
typedef void(* lv_cache_drop_all_cb_t) (lv_cache_t *cache, void *user_data)The cache drop all function, used by the cache class to remove all cache entries from the cache and free the memory.
lv_cache_get_victim_cb
typedef lv_cache_entry_t *(* lv_cache_get_victim_cb) (lv_cache_t *cache, void *user_data)The cache get victim function, used by the cache class to get a victim entry to be evicted.
lv_cache_reserve_cond_cb
typedef lv_cache_reserve_cond_res_t(* lv_cache_reserve_cond_cb) (lv_cache_t *cache, const void *key, size_t size, void *user_data)The cache reserve condition function, used by the cache class to check if a new entry can be added to the cache without exceeding its maximum size. See lv_cache_reserve_cond_res_t for the possible results.
lv_cache_iter_create_cb
typedef lv_iter_t *(* lv_cache_iter_create_cb) (lv_cache_t *cache)The cache iterator creation function, used by the cache class to create an iterator for the cache.
lv_cache_slot_size_t
typedef struct _lv_cache_slot_size_t lv_cache_slot_size_tDependencies
Indirect dependencies
Last updated on