1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
/*
Copyright (c) 2017 Red Hat, Inc. <http://www.redhat.com>
This file is part of gluster-block.
This file is licensed to you under your choice of the GNU Lesser
General Public License, version 3 or any later version (LGPLv3 or
later), or the GNU General Public License, version 2 (GPLv2), in all
cases as published by the Free Software Foundation.
*/
# include "lru.h"
static struct list_head Cache;
static int lruCount;
size_t glfsLruCount = 5; /* default lru cache size */
typedef struct Entry {
char volume[255];
glfs_t *glfs;
struct list_head list;
} Entry;
static void
releaseColdEntry(void)
{
Entry *tmp;
struct list_head *pos, *q = &Cache;
list_for_each_prev(pos, q) {
tmp = list_entry(pos, Entry, list);
list_del(pos);
glfs_fini(tmp->glfs);
GB_FREE(tmp);
lruCount--;
break;
}
}
int
appendNewEntry(const char *volname, glfs_t *fs)
{
Entry *tmp;
if (lruCount == glfsLruCount) {
releaseColdEntry();
}
if (GB_ALLOC(tmp) < 0) {
return -1;
}
GB_STRCPYSTATIC(tmp->volume, volname);
tmp->glfs = fs;
list_add(&(tmp->list), &Cache);
lruCount++;
return 0;
}
static void
boostEntryWarmness(const char *volname)
{
Entry *tmp;
struct list_head *pos, *q;
list_for_each_safe(pos, q, &Cache){
tmp = list_entry(pos, Entry, list);
if (!strcmp(tmp->volume, volname)) {
list_del(pos);
list_add(&(tmp->list), &Cache);
break;
}
}
}
glfs_t *
queryCache(const char *volname)
{
Entry *tmp;
struct list_head *pos, *q, *r = &Cache;
list_for_each_safe(pos, q, r){
tmp = list_entry(pos, Entry, list);
if (!strcmp(tmp->volume, volname)) {
boostEntryWarmness(volname);
return tmp->glfs;
}
}
return NULL;
}
void
initCache(void)
{
INIT_LIST_HEAD(&Cache);
}
|