1 /**
2  * @file lv_lru.h
3  *
4  */
5 
6 #ifndef LV_LRU_H
7 #define LV_LRU_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*********************
14  *      INCLUDES
15  *********************/
16 
17 #include "../lv_conf_internal.h"
18 
19 #include "lv_types.h"
20 
21 #include <stdint.h>
22 #include <stddef.h>
23 
24 /*********************
25  *      DEFINES
26  *********************/
27 
28 /**********************
29  *      TYPEDEFS
30  **********************/
31 
32 typedef enum {
33     LV_LRU_OK = 0,
34     LV_LRU_MISSING_CACHE,
35     LV_LRU_MISSING_KEY,
36     LV_LRU_MISSING_VALUE,
37     LV_LRU_LOCK_ERROR,
38     LV_LRU_VALUE_TOO_LARGE
39 } lv_lru_res_t;
40 
41 typedef void (lv_lru_free_t)(void * v);
42 typedef struct _lv_lru_item_t lv_lru_item_t;
43 
44 typedef struct lv_lru_t {
45     lv_lru_item_t ** items;
46     uint64_t access_count;
47     size_t free_memory;
48     size_t total_memory;
49     size_t average_item_length;
50     size_t hash_table_size;
51     uint32_t seed;
52     lv_lru_free_t * value_free;
53     lv_lru_free_t * key_free;
54     lv_lru_item_t * free_items;
55 } lv_lru_t;
56 
57 /**********************
58  * GLOBAL PROTOTYPES
59  **********************/
60 
61 lv_lru_t * lv_lru_create(size_t cache_size, size_t average_length, lv_lru_free_t * value_free,
62                          lv_lru_free_t * key_free);
63 
64 void lv_lru_del(lv_lru_t * cache);
65 
66 lv_lru_res_t lv_lru_set(lv_lru_t * cache, const void * key, size_t key_length, void * value, size_t value_length);
67 
68 lv_lru_res_t lv_lru_get(lv_lru_t * cache, const void * key, size_t key_size, void ** value);
69 
70 lv_lru_res_t lv_lru_remove(lv_lru_t * cache, const void * key, size_t key_size);
71 
72 /**
73  * remove the least recently used item
74  *
75  * @todo we can optimise this by finding the n lru items, where n = required_space / average_length
76  */
77 void lv_lru_remove_lru_item(lv_lru_t * cache);
78 /**********************
79  *      MACROS
80  **********************/
81 #ifdef __cplusplus
82 } /*extern "C"*/
83 #endif
84 
85 #endif /*LV_LRU_H*/
86