Flash Translation Layer  1.0
lru.h
Go to the documentation of this file.
1 
10 #ifndef LRU_H
11 #define LRU_H
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 #include <stdint.h>
18 #include <stdlib.h>
19 
20 #include "log.h"
21 
25 typedef int (*lru_dealloc_fn)(const uint64_t, uintptr_t);
26 
30 struct lru_node {
31  uint64_t key;
32  uintptr_t value;
33  struct lru_node *next;
34  struct lru_node *prev;
35 };
36 
40 struct lru_cache {
41  size_t capacity;
42  size_t size;
43  struct lru_node *head;
45  struct lru_node nil;
46 };
47 
48 struct lru_cache *lru_init(const size_t capacity, lru_dealloc_fn deallocate);
49 int lru_put(struct lru_cache *cache, const uint64_t key, uintptr_t value);
50 uintptr_t lru_get(struct lru_cache *cache, const uint64_t key);
51 int lru_free(struct lru_cache *cache);
52 
63 static inline size_t lru_get_evict_size(struct lru_cache *cache)
64 {
65  pr_debug("evict size ==> %zu\n", (size_t)(cache->capacity));
66  return cache->capacity;
67 }
68 
69 #ifdef __cplusplus
70 }
71 #endif
72 
73 #endif
int lru_free(struct lru_cache *cache)
deallocate the LRU cache structure
Definition: lru.c:260
size_t capacity
Definition: lru.h:41
struct lru_node * next
Definition: lru.h:33
struct lru_node * head
Definition: lru.h:43
size_t size
Definition: lru.h:42
struct lru_cache * lru_init(const size_t capacity, lru_dealloc_fn deallocate)
initialize the LRU cache data strcture
Definition: lru.c:24
doubly-linked list data structure
Definition: lru.h:30
uintptr_t value
Definition: lru.h:32
lru_dealloc_fn deallocate
Definition: lru.h:44
main LRU cache data structure
Definition: lru.h:40
uintptr_t lru_get(struct lru_cache *cache, const uint64_t key)
get data from the LRU cache
Definition: lru.c:237
struct lru_node * prev
Definition: lru.h:34
int lru_put(struct lru_cache *cache, const uint64_t key, uintptr_t value)
inser the key, value to the LRU cache
Definition: lru.c:185
uint64_t key
Definition: lru.h:31
#define pr_debug(...)
Definition: log.h:19
int(* lru_dealloc_fn)(const uint64_t, uintptr_t)
deallocate the value for eviction function type. 0 means successfully evicted
Definition: lru.h:25
static size_t lru_get_evict_size(struct lru_cache *cache)
get evict size of the LRU cache
Definition: lru.h:63