root / xseg / xtypes / xcache.h @ b4832a42
History | View | Annotate | Download (2.6 kB)
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 |
|
16 |
#define XCACHE_LRU_MAX (uint64_t)(-1) |
17 |
|
18 |
typedef xqindex xcache_handler;
|
19 |
#define NoEntry (xcache_handler)Noneidx
|
20 |
|
21 |
/*
|
22 |
* Called with out cache lock held:
|
23 |
*
|
24 |
* on_init: called on cache entry initialization.
|
25 |
* Should return negative on error to abort cache entry
|
26 |
* initialization.
|
27 |
*
|
28 |
* on_put: called when the last reference to the cache entry is put
|
29 |
*
|
30 |
* on_evict: called when a cache entry is evicted. It is called with the old
|
31 |
* cache entry that gets evicted and the new cache entry that
|
32 |
* trigger the eviction as arguments.
|
33 |
* Return value interpretation:
|
34 |
* < 0 : Failure.
|
35 |
* = 0 : Success. Finished with the old cache entry.
|
36 |
* > 0 : Success. Pending actions on the old cache entry.
|
37 |
*
|
38 |
* on_node_init: called on initial node preparation.
|
39 |
* Must return NULL on error, to abort cache initialization.
|
40 |
*/
|
41 |
struct xcache_ops {
|
42 |
int (*on_init)(void *cache_data, void *user_data); |
43 |
int (*on_evict)(void *cache_data, void *evicted_user_data, void *user_data); |
44 |
void (*on_put)(void *cache_data, void *user_data); |
45 |
void *(*on_node_init)(void *cache_data); |
46 |
}; |
47 |
|
48 |
struct xcache_entry {
|
49 |
struct xlock lock;
|
50 |
uint32_t ref; |
51 |
char name[XSEG_MAX_TARGETLEN + 1]; |
52 |
xbinheap_handler h; |
53 |
void *priv;
|
54 |
}; |
55 |
|
56 |
struct xcache {
|
57 |
struct xlock lock;
|
58 |
uint32_t size; |
59 |
uint32_t nr_nodes; |
60 |
struct xq free_nodes;
|
61 |
xhash_t *entries; |
62 |
struct xcache_entry *nodes;
|
63 |
uint64_t time; |
64 |
uint64_t *times; |
65 |
struct xbinheap binheap;
|
66 |
struct xcache_ops ops;
|
67 |
uint32_t flags; |
68 |
void *priv;
|
69 |
}; |
70 |
|
71 |
static int __validate_idx(struct xcache *cache, xqindex idx) |
72 |
{ |
73 |
return (idx < cache->nr_nodes);
|
74 |
} |
75 |
|
76 |
static void * get_cache_entry(struct xcache *cache, xcache_handler h) |
77 |
{ |
78 |
xqindex idx = (xqindex)h; |
79 |
//validate idx
|
80 |
if (!__validate_idx(cache, idx))
|
81 |
return NULL; |
82 |
return cache->nodes[idx].priv;
|
83 |
} |
84 |
|
85 |
int xcache_init(struct xcache *cache, uint32_t xcache_size, |
86 |
struct xcache_ops *ops, uint32_t flags, void *priv); |
87 |
void xcache_close(struct xcache *cache); |
88 |
void xcache_free(struct xcache *cache); |
89 |
xcache_handler xcache_lookup(struct xcache *cache, char *name); |
90 |
xcache_handler xcache_alloc_init(struct xcache *cache, char *name); |
91 |
xcache_handler xcache_insert(struct xcache *cache, xcache_handler h);
|
92 |
int xcache_remove(struct xcache *cache, xcache_handler h); |
93 |
int xcache_invalidate(struct xcache *cache, char *name); |
94 |
void xcache_put(struct xcache *cache, xcache_handler h); |
95 |
|
96 |
#endif /* __XCACHE_H */ |