Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 98 lines (68 sloc) 2.361 kB
e4c40a1 @believe3301 init repo
authored
1 #include <stdlib.h>
2 #include <stdint.h>
d6c4f11 @believe3301 need more test, and add stat output
authored
3 #include <string.h>
4
8845eee @believe3301 change stat and fix bug
authored
5 #define MAX(a, b) (((a) > (b))?(a) : (b))
ffbe165 @believe3301 stat enabled and valgrind passed
authored
6 #define MIN(a, b) (((a) > (b))?(b) : (a))
d6c4f11 @believe3301 need more test, and add stat output
authored
7 #define MAXBYTE_DEDAULT 64 * 1024 * 1024 /* 64M */
c65e142 @believe3301 support mul-lru
authored
8 #define HASH_POWER_DEFAULT 16
9
10 #define hashsize(n) ((size_t)1<<(n))
11 #define hashmask(n) (hashsize(n)-1)
12
d6c4f11 @believe3301 need more test, and add stat output
authored
13
14 #define ITEM_size (sizeof(lru_item))
15
16 #define ITEM_key(item) ((char*)((item)->data)) /* terminal null */
17
18 #define ITEM_data(item) (char*) ((item)->data) + (item)->nkey + 1
e4c40a1 @believe3301 init repo
authored
19
20 /* item */
d6c4f11 @believe3301 need more test, and add stat output
authored
21 typedef struct lru_item_ {
22 struct lru_item_ *next;
23 struct lru_item_ *prev;
24 struct lru_item_ *h_next; /* hash chain next */
25 int nbytes; /* size of total items, ITEM_size + nkey + 1 + nvalue*/
26 uint8_t nkey; /* key len, not include terminal null */
27 char data[]; /* key and value, include terminal null */
e4c40a1 @believe3301 init repo
authored
28 }lru_item;
29
30
31 /* stat */
32 typedef struct lru_stat {
33 uint64_t total_items;
34 uint64_t curr_items;
35
36 uint64_t total_bytes;
37 uint64_t malloc;
d6c4f11 @believe3301 need more test, and add stat output
authored
38 uint64_t malloc_failed;
39
e4c40a1 @believe3301 init repo
authored
40 uint64_t free_bytes;
41 uint64_t free;
42 uint64_t curr_bytes;
43
44 uint64_t get_cmds;
45 uint64_t get_hits;
46 uint64_t get_misses;
d6c4f11 @believe3301 need more test, and add stat output
authored
47
e4c40a1 @believe3301 init repo
authored
48 uint64_t set_cmds;
d6c4f11 @believe3301 need more test, and add stat output
authored
49 uint64_t set_failed;
50
51 uint64_t del_cmds;
52 uint64_t del_hits;
53 uint64_t del_misses;
54
8845eee @believe3301 change stat and fix bug
authored
55 unsigned int hash_find_depth;
56 uint64_t hash_bytes;
57
e4c40a1 @believe3301 init repo
authored
58
59 uint64_t evictions;
60 }lru_stat;
61
c65e142 @believe3301 support mul-lru
authored
62 typedef struct lru{
63 unsigned int hashpower;
64 uint64_t max_bytes; /* max used bytes, include item size, not include hash bytes */
65
66 lru_item **table; /* hash */
67 lru_item *head; /* double link head */
68 lru_item *tail; /* double link tail */
69
70 lru_stat stat; /* stat */
71 }lru;
72
8845eee @believe3301 change stat and fix bug
authored
73
d6c4f11 @believe3301 need more test, and add stat output
authored
74 #include "hash.h"
75
c65e142 @believe3301 support mul-lru
authored
76 lru* lru_init(const uint64_t maxbytes, const unsigned int hashpower);
e4c40a1 @believe3301 init repo
authored
77
ffbe165 @believe3301 stat enabled and valgrind passed
authored
78 /*
79 0 success
80 1 failed
81 alloc value buf by caller or add refcount by item...
82 */
c65e142 @believe3301 support mul-lru
authored
83 int item_get(lru *l, const char *key, const size_t nkey, char *buf, const size_t nbuf, size_t *nvalue);
e4c40a1 @believe3301 init repo
authored
84
d6c4f11 @believe3301 need more test, and add stat output
authored
85 /* 0 success , 1 failed */
c65e142 @believe3301 support mul-lru
authored
86 int item_set(lru *l, const char *key, const size_t nkey, const char *value, const size_t nvalue);
e4c40a1 @believe3301 init repo
authored
87
40971dd @believe3301 添加test
authored
88 /* 0 hit, 1 miss */
c65e142 @believe3301 support mul-lru
authored
89 int item_delete(lru *l, const char *key, const size_t nkey);
e4c40a1 @believe3301 init repo
authored
90
c65e142 @believe3301 support mul-lru
authored
91 #define stat_reset(l) do { \
92 memset(&(l->stat), 0, sizeof(struct lru_stat)); \
93 } while(0)
ffbe165 @believe3301 stat enabled and valgrind passed
authored
94
c65e142 @believe3301 support mul-lru
authored
95 void stat_print(lru *l, char *buf, const int nbuf);
e4c40a1 @believe3301 init repo
authored
96
c65e142 @believe3301 support mul-lru
authored
97 void lru_free(lru *l);
Something went wrong with that request. Please try again.