| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
#define PERL_NO_GET_CONTEXT /* we want efficiency */ |
|
2
|
|
|
|
|
|
|
#include "EXTERN.h" |
|
3
|
|
|
|
|
|
|
#include "perl.h" |
|
4
|
|
|
|
|
|
|
#include "XSUB.h" |
|
5
|
|
|
|
|
|
|
#include "ppport.h" |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
#include "lru.h" |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
#define CACHE_DEFAULT_SIZE 1000 |
|
10
|
|
|
|
|
|
|
|
|
11
|
27
|
|
|
|
|
|
static int cache_dtor(pTHX_ SV *sv, MAGIC *mg) { |
|
12
|
27
|
|
|
|
|
|
Cache* cache = (Cache*) mg->mg_ptr; |
|
13
|
27
|
|
|
|
|
|
cache_destroy(aTHX_ cache); |
|
14
|
27
|
|
|
|
|
|
return 0; |
|
15
|
|
|
|
|
|
|
} |
|
16
|
|
|
|
|
|
|
|
|
17
|
15
|
|
|
|
|
|
static void cache_visit(Cache* cache, CacheEntry* entry, void* arg) |
|
18
|
|
|
|
|
|
|
{ |
|
19
|
|
|
|
|
|
|
/* TODO: find out if we really need to call all these magic macros */ |
|
20
|
|
|
|
|
|
|
dTHX; |
|
21
|
15
|
|
|
|
|
|
dSP; |
|
22
|
15
|
50
|
|
|
|
|
if (!cache || !entry || !arg) { |
|
|
|
50
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
23
|
0
|
|
|
|
|
|
return; |
|
24
|
|
|
|
|
|
|
} |
|
25
|
15
|
|
|
|
|
|
ENTER; |
|
26
|
15
|
|
|
|
|
|
SAVETMPS; |
|
27
|
|
|
|
|
|
|
|
|
28
|
15
|
50
|
|
|
|
|
EXTEND(SP, 2); |
|
29
|
15
|
50
|
|
|
|
|
PUSHMARK(SP); |
|
30
|
15
|
|
|
|
|
|
PUSHs(newSVsv(entry->key)); |
|
31
|
15
|
|
|
|
|
|
PUSHs(entry->val); |
|
32
|
15
|
|
|
|
|
|
PUTBACK; |
|
33
|
|
|
|
|
|
|
|
|
34
|
15
|
|
|
|
|
|
SV* cb = (SV*) arg; |
|
35
|
15
|
|
|
|
|
|
call_sv(cb, G_SCALAR | G_EVAL | G_DISCARD); |
|
36
|
|
|
|
|
|
|
|
|
37
|
15
|
|
|
|
|
|
SPAGAIN; |
|
38
|
|
|
|
|
|
|
|
|
39
|
15
|
|
|
|
|
|
PUTBACK; |
|
40
|
15
|
50
|
|
|
|
|
FREETMPS; |
|
41
|
15
|
|
|
|
|
|
LEAVE; |
|
42
|
|
|
|
|
|
|
} |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
static MGVTBL session_magic_vtbl = { .svt_free = cache_dtor }; |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
MODULE = Cache::utLRU PACKAGE = Cache::utLRU |
|
47
|
|
|
|
|
|
|
PROTOTYPES: DISABLE |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
################################################################# |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Cache* |
|
52
|
|
|
|
|
|
|
new(char* CLASS, int size = 0) |
|
53
|
|
|
|
|
|
|
CODE: |
|
54
|
|
|
|
|
|
|
{ |
|
55
|
27
|
100
|
|
|
|
|
if (size <= 0) { |
|
56
|
5
|
|
|
|
|
|
size = CACHE_DEFAULT_SIZE; |
|
57
|
|
|
|
|
|
|
} |
|
58
|
27
|
|
|
|
|
|
RETVAL = cache_build(aTHX_ size); |
|
59
|
27
|
50
|
|
|
|
|
if (!RETVAL) { |
|
60
|
0
|
|
|
|
|
|
croak("could not create cache"); |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
} |
|
63
|
|
|
|
|
|
|
OUTPUT: RETVAL |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
int |
|
66
|
|
|
|
|
|
|
size(Cache* cache) |
|
67
|
|
|
|
|
|
|
CODE: |
|
68
|
|
|
|
|
|
|
{ |
|
69
|
9
|
|
|
|
|
|
RETVAL = cache_size(aTHX_ cache); |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
OUTPUT: RETVAL |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
int |
|
74
|
|
|
|
|
|
|
capacity(Cache* cache) |
|
75
|
|
|
|
|
|
|
CODE: |
|
76
|
|
|
|
|
|
|
{ |
|
77
|
2
|
|
|
|
|
|
RETVAL = cache_capacity(aTHX_ cache); |
|
78
|
|
|
|
|
|
|
} |
|
79
|
|
|
|
|
|
|
OUTPUT: RETVAL |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
void |
|
82
|
|
|
|
|
|
|
clear(Cache* cache) |
|
83
|
|
|
|
|
|
|
CODE: |
|
84
|
|
|
|
|
|
|
{ |
|
85
|
2
|
|
|
|
|
|
cache_clear(aTHX_ cache); |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
void |
|
89
|
|
|
|
|
|
|
add(Cache* cache, SV* key, SV* val) |
|
90
|
|
|
|
|
|
|
CODE: |
|
91
|
|
|
|
|
|
|
{ |
|
92
|
2435
|
50
|
|
|
|
|
if (!key || !SvOK(key) || !SvPOK(key)) { |
|
|
|
50
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
croak("add key argument must be a string"); |
|
94
|
|
|
|
|
|
|
} |
|
95
|
2435
|
50
|
|
|
|
|
if (!val || !SvOK(val)) { |
|
|
|
50
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
croak("add value argument must be an actual value"); |
|
97
|
|
|
|
|
|
|
} |
|
98
|
2435
|
50
|
|
|
|
|
if (!cache_add(aTHX_ cache, key, val)) { |
|
99
|
0
|
|
|
|
|
|
croak("could not add element to cache"); |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
void |
|
104
|
|
|
|
|
|
|
find(Cache* cache, SV* key) |
|
105
|
|
|
|
|
|
|
PPCODE: |
|
106
|
|
|
|
|
|
|
{ |
|
107
|
|
|
|
|
|
|
/* We use PPCODE because XS mortalizes the return value */ |
|
108
|
2430
|
50
|
|
|
|
|
if (!key || !SvOK(key) || !SvPOK(key)) { |
|
|
|
50
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
croak("find key argument must be a string"); |
|
110
|
|
|
|
|
|
|
} |
|
111
|
2430
|
|
|
|
|
|
ST(0) = cache_find(aTHX_ cache, key); |
|
112
|
2430
|
|
|
|
|
|
XSRETURN(1); |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
void |
|
116
|
|
|
|
|
|
|
visit(Cache* cache, SV* cb) |
|
117
|
|
|
|
|
|
|
CODE: |
|
118
|
|
|
|
|
|
|
{ |
|
119
|
2
|
50
|
|
|
|
|
if (!cb || !SvOK(cb) || !SvROK(cb) || SvTYPE(SvRV(cb)) != SVt_PVCV) { |
|
|
|
50
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
croak("visit cb argument must be a coderef"); |
|
121
|
|
|
|
|
|
|
} |
|
122
|
2
|
|
|
|
|
|
cache_iterate(aTHX_ cache, cache_visit, cb); |
|
123
|
|
|
|
|
|
|
} |