xcache: Fix tab indendation in comments
[archipelago] / xseg / xtypes / xcache.h
1 #ifndef __XCACHE_H
2 #define __XCACHE_H
3
4 #include <xtypes/domain.h>
5 #include <xtypes/xlock.h>
6 #include <xtypes/xq.h>
7 #include <xtypes/xhash.h>
8 #include <xtypes/xbinheap.h>
9 #include <xseg/xseg.h>
10 #include <xseg/protocol.h>
11 #include <sys/util.h>
12
13 #define XCACHE_LRU_ARRAY      (1<<0)
14 #define XCACHE_LRU_HEAP       (1<<1)
15 #define XCACHE_USE_RMTABLE    (1<<2)
16
17 #define XCACHE_LRU_MAX   (uint64_t)(-1)
18
19 typedef xqindex xcache_handler;
20 #define NoEntry (xcache_handler)Noneidx
21
22 #define NODE_ACTIVE 0
23 #define NODE_EVICTED 1
24
25 /*
26  * Called with out cache lock held:
27  *
28  * on_init:     called on cache entry initialization.
29  *              Should return negative on error to abort cache entry
30  *              initialization.
31  *
32  * on_put:      called when the last reference to the cache entry is put
33  *
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:
38  *                      < 0 : Failure.
39  *                      = 0 : Success. Finished with the old cache entry.
40  *                      > 0 : Success. Pending actions on the old cache entry.
41  *
42  * on_node_init:
43  *              called on initial node preparation.
44  *              Must return NULL on error, to abort cache initialization.
45  *
46  * on_free:     called when a cache entry is freed.
47  *
48  * on_finalize: FILLME
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.
52  */
53 struct xcache_ops {
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);
61 };
62
63 /* FIXME: Does xcache_entry need lock? */
64 struct xcache_entry {
65         struct xlock lock;
66         volatile uint32_t ref;
67         uint32_t state;
68         char name[XSEG_MAX_TARGETLEN + 1];
69         xbinheap_handler h;
70         void *priv;
71 };
72
73 struct xcache {
74         struct xlock lock;
75         uint32_t size;
76         uint32_t nr_nodes;
77         struct xq free_nodes;
78         xhash_t *entries;
79         xhash_t *rm_entries;
80         struct xlock rm_lock;
81         struct xcache_entry *nodes;
82         uint64_t time;
83         uint64_t *times;
84         struct xbinheap binheap;
85         struct xcache_ops ops;
86         uint32_t flags;
87         void *priv;
88 };
89
90 static int __validate_idx(struct xcache *cache, xqindex idx)
91 {
92         return (idx < cache->nr_nodes);
93 }
94
95 /*
96  * Return a pointer to the associated cache entry.
97  */
98 static void *xcache_get_entry(struct xcache *cache, xcache_handler h)
99 {
100         xqindex idx = (xqindex)h;
101
102         if (!__validate_idx(cache, idx))
103                 return NULL;
104
105         return cache->nodes[idx].priv;
106 }
107
108 /*
109  * Return a pointer to a NULL terminated string holding the name of the
110  * associated cache entry.
111  */
112 static char *xcache_get_name(struct xcache *cache, xcache_handler h)
113 {
114         xqindex idx = (xqindex)h;
115
116         if (!__validate_idx(cache, idx))
117                 return NULL;
118
119         return cache->nodes[idx].name;
120 }
121
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);
135
136 #endif /* __XCACHE_H */