Lines Matching refs:cache
43 static uint32_t lv_lru_hash(lv_lru_t * cache, const void * key, uint32_t key_length);
49 static void lv_lru_remove_item(lv_lru_t * cache, lv_lru_item_t * prev, lv_lru_item_t * item, uint32…
52 static lv_lru_item_t * lv_lru_pop_or_create_item(lv_lru_t * cache);
64 #define test_for_missing_cache() error_for(!cache, LV_LRU_MISSING_CACHE)
67 #define test_for_value_too_large() error_for(value_length > cache->total_memory, LV_LRU_VALUE_TO…
77 lv_lru_t * cache = (lv_lru_t *) lv_mem_alloc(sizeof(lv_lru_t)); in lv_lru_create() local
78 lv_memset_00(cache, sizeof(lv_lru_t)); in lv_lru_create()
79 if(!cache) { in lv_lru_create()
83 cache->hash_table_size = cache_size / average_length; in lv_lru_create()
84 cache->average_item_length = average_length; in lv_lru_create()
85 cache->free_memory = cache_size; in lv_lru_create()
86 cache->total_memory = cache_size; in lv_lru_create()
87 cache->seed = lv_rand(1, UINT32_MAX); in lv_lru_create()
88 cache->value_free = value_free ? value_free : lv_mem_free; in lv_lru_create()
89 cache->key_free = key_free ? key_free : lv_mem_free; in lv_lru_create()
92 … cache->items = (lv_lru_item_t **) lv_mem_alloc(sizeof(lv_lru_item_t *) * cache->hash_table_size); in lv_lru_create()
93 lv_memset_00(cache->items, sizeof(lv_lru_item_t *) * cache->hash_table_size); in lv_lru_create()
94 if(!cache->items) { in lv_lru_create()
96 lv_mem_free(cache); in lv_lru_create()
99 return cache; in lv_lru_create()
102 void lv_lru_del(lv_lru_t * cache) in lv_lru_del() argument
104 LV_ASSERT_NULL(cache); in lv_lru_del()
109 if(cache->items) { in lv_lru_del()
110 for(; i < cache->hash_table_size; i++) { in lv_lru_del()
111 item = cache->items[i]; in lv_lru_del()
114 cache->value_free(item->value); in lv_lru_del()
115 cache->key_free(item->key); in lv_lru_del()
116 cache->free_memory += item->value_length; in lv_lru_del()
121 lv_mem_free(cache->items); in lv_lru_del()
124 if(cache->free_items) { in lv_lru_del()
125 item = cache->free_items; in lv_lru_del()
134 lv_mem_free(cache); in lv_lru_del()
137 lv_lru_res_t lv_lru_set(lv_lru_t * cache, const void * key, size_t key_length, void * value, size_t… in lv_lru_set() argument
145 uint32_t hash_index = lv_lru_hash(cache, key, key_length); in lv_lru_set()
148 item = cache->items[hash_index]; in lv_lru_set()
158 cache->value_free(item->value); in lv_lru_set()
165 item = lv_lru_pop_or_create_item(cache); in lv_lru_set()
176 cache->items[hash_index] = item; in lv_lru_set()
178 item->access_count = ++cache->access_count; in lv_lru_set()
181 if(required > 0 && (size_t) required > cache->free_memory) { in lv_lru_set()
182 while(cache->free_memory < (size_t) required) in lv_lru_set()
183 lv_lru_remove_lru_item(cache); in lv_lru_set()
185 cache->free_memory -= required; in lv_lru_set()
189 lv_lru_res_t lv_lru_get(lv_lru_t * cache, const void * key, size_t key_size, void ** value) in lv_lru_get() argument
195 uint32_t hash_index = lv_lru_hash(cache, key, key_size); in lv_lru_get()
196 lv_lru_item_t * item = cache->items[hash_index]; in lv_lru_get()
203 item->access_count = ++cache->access_count; in lv_lru_get()
212 lv_lru_res_t lv_lru_remove(lv_lru_t * cache, const void * key, size_t key_size) in lv_lru_remove() argument
219 uint32_t hash_index = lv_lru_hash(cache, key, key_size); in lv_lru_remove()
220 item = cache->items[hash_index]; in lv_lru_remove()
228 lv_lru_remove_item(cache, prev, item, hash_index); in lv_lru_remove()
234 void lv_lru_remove_lru_item(lv_lru_t * cache) in lv_lru_remove_lru_item() argument
241 for(; i < cache->hash_table_size; i++) { in lv_lru_remove_lru_item()
242 item = cache->items[i]; in lv_lru_remove_lru_item()
258 lv_lru_remove_item(cache, min_prev, min_item, min_index); in lv_lru_remove_lru_item()
266 static uint32_t lv_lru_hash(lv_lru_t * cache, const void * key, uint32_t key_length) in lv_lru_hash() argument
270 uint32_t h = cache->seed ^ key_length; in lv_lru_hash()
298 return h % cache->hash_table_size; in lv_lru_hash()
311 static void lv_lru_remove_item(lv_lru_t * cache, lv_lru_item_t * prev, lv_lru_item_t * item, uint32… in lv_lru_remove_item() argument
317 cache->items[hash_index] = (lv_lru_item_t *) item->next; in lv_lru_remove_item()
321 cache->free_memory += item->value_length; in lv_lru_remove_item()
322 cache->value_free(item->value); in lv_lru_remove_item()
323 cache->key_free(item->key); in lv_lru_remove_item()
327 item->next = cache->free_items; in lv_lru_remove_item()
328 cache->free_items = item; in lv_lru_remove_item()
331 static lv_lru_item_t * lv_lru_pop_or_create_item(lv_lru_t * cache) in lv_lru_pop_or_create_item() argument
335 if(cache->free_items) { in lv_lru_pop_or_create_item()
336 item = cache->free_items; in lv_lru_pop_or_create_item()
337 cache->free_items = item->next; in lv_lru_pop_or_create_item()