4 #include <xtypes/domain.h>
5 #include <xtypes/xlock.h>
7 #include <xtypes/xhash.h>
8 #include <xtypes/xbinheap.h>
10 #include <xseg/protocol.h>
13 #define XCACHE_LRU_ARRAY (1<<0)
14 #define XCACHE_LRU_HEAP (1<<1)
15 #define XCACHE_USE_RMTABLE (1<<2)
17 #define XCACHE_LRU_MAX (uint64_t)(-1)
19 typedef xqindex xcache_handler;
20 #define NoEntry (xcache_handler)Noneidx
23 #define NODE_EVICTED 1
26 * Called with out cache lock held:
28 * on_init: called on cache entry initialization.
29 * Should return negative on error to abort cache entry
32 * on_put: called when the last reference to the cache entry is put
34 * on_evict: called when a cache entry is evicted. It is called with the old
35 * cache entry that gets evicted and the new cache entry that
36 * trigger the eviction as arguments.
37 * Return value interpretation:
39 * = 0 : Success. Finished with the old cache entry.
40 * > 0 : Success. Pending actions on the old cache entry.
43 * called on initial node preparation.
44 * Must return NULL on error, to abort cache initialization.
46 * on_free: called when a cache entry is freed.
49 * Must return 0 if there are no pending actions to the entry.
50 * On non-zero value, user should get the entry which will be put
51 * to the evicted table.
54 int (*on_init)(void *cache_data, void *user_data);
55 int (*on_evict)(void *cache_data, void *evicted_user_data);
56 int (*on_finalize)(void *cache_data, void *evicted_user_data);
57 void (*on_reinsert)(void *cache_data, void *user_data);
58 void (*on_put)(void *cache_data, void *user_data);
59 void (*on_free)(void *cache_data, void *user_data);
60 void *(*on_node_init)(void *cache_data, void *data_handler);
63 /* FIXME: Does xcache_entry need lock? */
66 volatile uint32_t ref;
68 char name[XSEG_MAX_TARGETLEN + 1];
81 struct xcache_entry *nodes;
84 struct xbinheap binheap;
85 struct xcache_ops ops;
90 static int __validate_idx(struct xcache *cache, xqindex idx)
92 return (idx < cache->nr_nodes);
96 * Return a pointer to the associated cache entry.
98 static void *xcache_get_entry(struct xcache *cache, xcache_handler h)
100 xqindex idx = (xqindex)h;
102 if (!__validate_idx(cache, idx))
105 return cache->nodes[idx].priv;
109 * Return a pointer to a NULL terminated string holding the name of the
110 * associated cache entry.
112 static char *xcache_get_name(struct xcache *cache, xcache_handler h)
114 xqindex idx = (xqindex)h;
116 if (!__validate_idx(cache, idx))
119 return cache->nodes[idx].name;
122 int xcache_init(struct xcache *cache, uint32_t xcache_size,
123 struct xcache_ops *ops, uint32_t flags, void *priv);
124 void xcache_close(struct xcache *cache);
125 void xcache_free(struct xcache *cache);
126 xcache_handler xcache_lookup(struct xcache *cache, char *name);
127 xcache_handler xcache_alloc_init(struct xcache *cache, char *name);
128 xcache_handler xcache_insert(struct xcache *cache, xcache_handler h);
129 int xcache_remove(struct xcache *cache, xcache_handler h);
130 int xcache_invalidate(struct xcache *cache, char *name);
131 void xcache_put(struct xcache *cache, xcache_handler h);
132 void xcache_get(struct xcache *cache, xcache_handler h);
133 uint64_t xcache_free_nodes(struct xcache *cache);
134 void xcache_free_new(struct xcache *cache, xcache_handler h);
136 #endif /* __XCACHE_H */