1 /**
2  * @file lv_cache.h
3  *
4  */
5 
6 #ifndef LV_CACHE_H
7 #define LV_CACHE_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*********************
14  *      INCLUDES
15  *********************/
16 #include "lv_cache_entry.h"
17 #include "../lv_types.h"
18 
19 #include "lv_cache_lru_rb.h"
20 
21 #include "lv_image_cache.h"
22 #include "lv_image_header_cache.h"
23 
24 /*********************
25  *      DEFINES
26  *********************/
27 
28 /**********************
29  *      TYPEDEFS
30  **********************/
31 
32 /**********************
33  * GLOBAL PROTOTYPES
34  **********************/
35 
36 /**
37  * Create a cache object with the given parameters.
38  * @param cache_class   The class of the cache. Currently only support one two builtin classes:
39  *                        - lv_cache_class_lru_rb_count for LRU-based cache with count-based eviction policy.
40  *                        - lv_cache_class_lru_rb_size for LRU-based cache with size-based eviction policy.
41  * @param node_size     The node size is the size of the data stored in the cache..
42  * @param max_size      The max size is the maximum amount of memory or count that the cache can hold.
43  *                        - lv_cache_class_lru_rb_count: max_size is the maximum count of nodes in the cache.
44  *                        - lv_cache_class_lru_rb_size: max_size is the maximum size of the cache in bytes.
45  * @param ops           A set of operations that can be performed on the cache. See lv_cache_ops_t for details.
46  * @return              Returns a pointer to the created cache object on success, `NULL` on error.
47  */
48 lv_cache_t * lv_cache_create(const lv_cache_class_t * cache_class,
49                              size_t node_size, size_t max_size,
50                              lv_cache_ops_t ops);
51 
52 /**
53  * Destroy a cache object.
54  * @param cache         The cache object pointer to destroy.
55  * @param user_data     A user data pointer that will be passed to the free callback.
56  */
57 void lv_cache_destroy(lv_cache_t * cache, void * user_data);
58 
59 /**
60  * Acquire a cache entry with the given key. If entry not in cache, it will return `NULL` (not found).
61  * 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.
62  * @param cache         The cache object pointer to acquire the entry.
63  * @param key           The key of the entry to acquire.
64  * @param user_data     A user data pointer that will be passed to the create callback.
65  * @return              Returns a pointer to the acquired cache entry on success with `lv_cache_entry_t::ref_cnt` incremented, `NULL` on error.
66  */
67 lv_cache_entry_t * lv_cache_acquire(lv_cache_t * cache, const void * key, void * user_data);
68 
69 /**
70  * 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.
71  * 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.
72  * 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.
73  * 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.
74  * And the internal impact on cache is also consistent with these two APIs.
75  * @param cache         The cache object pointer to acquire the entry.
76  * @param key           The key of the entry to acquire or create.
77  * @param user_data     A user data pointer that will be passed to the create callback.
78  * @return              Returns a pointer to the acquired or created cache entry on success with `lv_cache_entry_t::ref_cnt` incremented, `NULL` on error.
79  */
80 lv_cache_entry_t * lv_cache_acquire_or_create(lv_cache_t * cache, const void * key, void * user_data);
81 
82 /**
83  * 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.
84  * @param cache         The cache object pointer to add the entry.
85  * @param key           The key of the entry to add.
86  * @param user_data     A user data pointer that will be passed to the create callback.
87  * @return              Returns a pointer to the added cache entry on success with `lv_cache_entry_t::ref_cnt` incremented, `NULL` on error.
88  */
89 lv_cache_entry_t * lv_cache_add(lv_cache_t * cache, const void * key, void * user_data);
90 
91 /**
92  * 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.
93  * 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.
94  * @param cache         The cache object pointer to release the entry.
95  * @param entry         The cache entry pointer to release.
96  * @param user_data     A user data pointer that will be passed to the free callback.
97  */
98 void lv_cache_release(lv_cache_t * cache, lv_cache_entry_t * entry, void * user_data);
99 
100 /**
101  * 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,
102  * for example, when you know that you will need it later.
103  * When the current cache size is max than the reserved size, the function will evict entries until the reserved size is reached.
104  * @param cache         The cache object pointer to reserve.
105  * @param reserved_size The amount of memory/count to reserve.
106  * @param user_data     A user data pointer that will be passed to the free callback.
107  */
108 void lv_cache_reserve(lv_cache_t * cache, uint32_t reserved_size, void * user_data);
109 
110 /**
111  * Drop a cache entry with the given key. If the entry is not in the cache, nothing will happen to it.
112  * 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.
113  * @note 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().
114  *       If you want cache a same key again, you should use lv_cache_add() or lv_cache_acquire_or_create().
115  * @param cache         The cache object pointer to drop the entry.
116  * @param key           The key of the entry to drop.
117  * @param user_data     A user data pointer that will be passed to the free callback.
118  */
119 void lv_cache_drop(lv_cache_t * cache, const void * key, void * user_data);
120 
121 /**
122  * 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.
123  * @note If some entries are still referenced by other objects, it will issue an error. And this case shouldn't happen in normal cases..
124  * @param cache         The cache object pointer to drop all entries.
125  * @param user_data     A user data pointer that will be passed to the free callback.
126  */
127 void lv_cache_drop_all(lv_cache_t * cache, void * user_data);
128 
129 /**
130  * Evict one entry from the cache. The eviction policy will be used to select the entry to evict.
131  * @param cache         The cache object pointer to evict an entry.
132  * @param user_data     A user data pointer that will be passed to the free callback.
133  * @return              Returns true if an entry is evicted, false if no entry is evicted.
134  */
135 bool lv_cache_evict_one(lv_cache_t * cache, void * user_data);
136 
137 /**
138  * Set the maximum size of the cache.
139  * 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.
140  * If set to 0, the cache will be disabled.
141  * @note But this behavior will happen only new entries are added to the cache.
142  * @param cache         The cache object pointer to set the maximum size.
143  * @param max_size      The new maximum size of the cache.
144  * @param user_data     A user data pointer that will be passed to the free callback.
145  */
146 void   lv_cache_set_max_size(lv_cache_t * cache, size_t max_size, void * user_data);
147 
148 /**
149  * Get the maximum size of the cache.
150  * @param cache         The cache object pointer to get the maximum size.
151  * @param user_data     A user data pointer that will be passed to the free callback.
152  * @return              Returns the maximum size of the cache.
153  */
154 size_t lv_cache_get_max_size(lv_cache_t * cache, void * user_data);
155 
156 /**
157  * Get the current size of the cache.
158  * @param cache         The cache object pointer to get the current size.
159  * @param user_data     A user data pointer that will be passed to the free callback.
160  * @return              Returns the current size of the cache.
161  */
162 size_t lv_cache_get_size(lv_cache_t * cache, void * user_data);
163 
164 /**
165  * Get the free size of the cache.
166  * @param cache         The cache object pointer to get the free size.
167  * @param user_data     A user data pointer that will be passed to the free callback.
168  * @return              Returns the free size of the cache.
169  */
170 size_t lv_cache_get_free_size(lv_cache_t * cache, void * user_data);
171 
172 /**
173  * Return true if the cache is enabled.
174  * Disabled cache means that when the max_size of the cache is 0. In this case, all cache operations will be no-op.
175  * @param cache         The cache object pointer to check if it's disabled.
176  * @return              Returns true if the cache is enabled, false otherwise.
177  */
178 bool lv_cache_is_enabled(lv_cache_t * cache);
179 
180 /**
181  * Set the compare callback of the cache.
182  * @param cache         The cache object pointer to set the compare callback.
183  * @param compare_cb    The compare callback to set.
184  * @param user_data     A user data pointer.
185  */
186 void   lv_cache_set_compare_cb(lv_cache_t * cache, lv_cache_compare_cb_t compare_cb, void * user_data);
187 
188 /**
189  * Set the create callback of the cache.
190  * @param cache         The cache object pointer to set the create callback.
191  * @param alloc_cb      The create callback to set.
192  * @param user_data     A user data pointer.
193  */
194 void   lv_cache_set_create_cb(lv_cache_t * cache, lv_cache_create_cb_t alloc_cb, void * user_data);
195 
196 /**
197  * Set the free callback of the cache.
198  * @param cache         The cache object pointer to set the free callback.
199  * @param free_cb       The free callback to set.
200  * @param user_data     A user data pointer.
201  */
202 void   lv_cache_set_free_cb(lv_cache_t * cache, lv_cache_free_cb_t free_cb, void * user_data);
203 
204 /**
205  * Give a name for a cache object. Only the pointer of the string is saved.
206  * @param cache         The cache object pointer to set the name.
207  * @param name          The name of the cache.
208  */
209 void lv_cache_set_name(lv_cache_t * cache, const char * name);
210 
211 /**
212  * Get the name of a cache object.
213  * @param cache         The cache object pointer to get the name.
214  * @return              Returns the name of the cache.
215  */
216 const char * lv_cache_get_name(lv_cache_t * cache);
217 
218 /**
219  * Create an iterator for the cache object. The iterator is used to iterate over all cache entries.
220  * @param cache         The cache object pointer to create the iterator.
221  * @return              Returns a pointer to the created iterator on success, `NULL` on error.
222  */
223 lv_iter_t * lv_cache_iter_create(lv_cache_t * cache);
224 
225 /*************************
226  *    GLOBAL VARIABLES
227  *************************/
228 
229 /**********************
230  *      MACROS
231  **********************/
232 
233 #ifdef __cplusplus
234 } /*extern "C"*/
235 #endif
236 
237 #endif /* LV_CACHE_H */
238