root / xseg / xtypes / xcache.h @ 3b145fa0
History | View | Annotate | Download (1.9 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 <xseg/xseg.h> |
9 |
#include <xseg/protocol.h> |
10 |
#include <sys/util.h> |
11 |
|
12 |
/*
|
13 |
* on_init: called on cache entry initialization.
|
14 |
* Should return negative on error to abort cache entry initialization.
|
15 |
* on_put: called when the last reference to the cache entry is put
|
16 |
*
|
17 |
* on_node_init: called on initial node preparation.
|
18 |
* Must return NULL on error, to abort cache initialization.
|
19 |
*/
|
20 |
struct xcache_ops {
|
21 |
int (*on_init)(void *cache_data, void *user_data); |
22 |
void (*on_put)(void *cache_data, void *user_data); |
23 |
void *(*on_node_init)(void *cache_data); |
24 |
}; |
25 |
|
26 |
struct xcache_entry {
|
27 |
struct xlock lock;
|
28 |
uint32_t ref; |
29 |
char name[XSEG_MAX_TARGETLEN + 1]; |
30 |
void *priv;
|
31 |
}; |
32 |
|
33 |
struct xcache {
|
34 |
struct xlock lock;
|
35 |
uint32_t size; |
36 |
uint32_t nr_nodes; |
37 |
struct xq free_nodes;
|
38 |
xhash_t *entries; |
39 |
struct xcache_entry *nodes;
|
40 |
uint64_t time; |
41 |
uint64_t *times; |
42 |
struct xcache_ops ops;
|
43 |
void *priv;
|
44 |
}; |
45 |
|
46 |
typedef xqindex xcache_handler;
|
47 |
#define NoEntry (xcache_handler)Noneidx
|
48 |
|
49 |
static int __validate_idx(struct xcache *cache, xqindex idx) |
50 |
{ |
51 |
return (idx < cache->nr_nodes);
|
52 |
} |
53 |
|
54 |
static void * get_cache_entry(struct xcache *cache, xcache_handler h) |
55 |
{ |
56 |
xqindex idx = (xqindex)h; |
57 |
//validate idx
|
58 |
if (!__validate_idx(cache, idx))
|
59 |
return NULL; |
60 |
return cache->nodes[idx].priv;
|
61 |
} |
62 |
|
63 |
int xcache_init(struct xcache *cache, uint32_t xcache_size, |
64 |
struct xcache_ops *ops, void *priv); |
65 |
void xcache_close(struct xcache *cache); |
66 |
xcache_handler xcache_lookup(struct xcache *cache, char *name); |
67 |
xcache_handler xcache_alloc_init(struct xcache *cache, char *name); |
68 |
int xcache_insert(struct xcache *cache, xcache_handler h); |
69 |
int xcache_remove(struct xcache *cache, xcache_handler h); |
70 |
int xcache_invalidate(struct xcache *cache, char *name); |
71 |
void xcache_put(struct xcache *cache, xcache_handler h); |
72 |
|
73 |
#endif /* __XCACHE_H */ |