Shared.xs

filter: Statementstmt 98.6% 151 / 153 Branchbran 60.8% 168 / 276 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 74.3% 319 / 429 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« cuckoo.h ↑ Summary
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include "ppport.h"
6 #include "cuckoo.h"
7  
8 #define EXTRACT(sv) \
9 if (!sv_isobject(sv) || !sv_derived_from(sv, "Data::CuckooFilter::Shared")) \
10 croak("Expected a Data::CuckooFilter::Shared object"); \
11 CfHandle *h = INT2PTR(CfHandle*, SvIV(SvRV(sv))); \
12 if (!h) croak("Attempted to use a destroyed Data::CuckooFilter::Shared object"); \
13 CfHandle *h0 = h; PERL_UNUSED_VAR(h0); \
14 sv_2mortal(SvREFCNT_inc(SvRV(sv)))
15  
16 /* Re-read the handle after a call that can run Perl code (tied/overloaded
17 * argument magic, tied-array fetches). That code may call $obj->DESTROY
18 * explicitly, which frees the handle and zeroes the IV; EXTRACT's mortal
19 * pins the referent only against refcount-driven destruction, not an
20 * explicit DESTROY, so the local `h` would dangle. Used only where magic
21 * can actually intervene between EXTRACT and the first use of h. */
22 #define REEXTRACT(sv) \
23 if (!SvROK(sv)) \
24 croak("Data::CuckooFilter::Shared object was replaced during the call"); \
25 h = INT2PTR(CfHandle*, SvIV(SvRV(sv))); \
26 if (h != h0) croak("Data::CuckooFilter::Shared object replaced or destroyed during the call")
27  
28 #define MAKE_OBJ(class, handle) \
29 SV *obj = newSViv(PTR2IV(handle)); \
30 SV *ref = newRV_noinc(obj); \
31 sv_bless(ref, gv_stashpv(class, GV_ADD)); \
32 RETVAL = ref
33  
34 /* The constructors re-read the class PV from ST(0) immediately before
35 * MAKE_OBJ, at the point of use: xsubpp captured `class` (typemap
36 * `const char *`) in the INPUT section, BEFORE any get-magic on the later
37 * arguments ran (the UV/int conversions of capacity/fd and the explicit
38 * SvGETMAGICs in CODE). That magic is arbitrary Perl and can realloc or free
39 * the class PV, so the INPUT capture may already dangle when gv_stashpv uses
40 * it. SvPV_nolen, not SvPV_nomg: the re-read must dispatch overload
41 * stringification exactly as the original INPUT conversion did. */
42 #define REREAD_CLASS() \
43 class = SvPV_nolen(ST(0))
44  
45 MODULE = Data::CuckooFilter::Shared PACKAGE = Data::CuckooFilter::Shared
46  
47 PROTOTYPES: DISABLE
48  
49 SV *
50 new(class, path = &PL_sv_undef, capacity = 0, ...)
51 const char *class
52 SV *path
53 UV capacity
54 PREINIT:
55 char errbuf[CF_ERR_BUFLEN];
56 CODE:
57 /* Optional 4th arg: file mode for the exclusive create of a NEW backing
58 file (default 0600, owner-only). Pass e.g. 0660 to opt into group
59 sharing. Ignored for anonymous mappings and when attaching an existing
60 file. Applied exactly: cf_secure_open fchmods after the O_EXCL create,
61 so the process umask does not narrow it. */
62 /* Resolve the trailing optional args BEFORE capturing path's PV: get-magic
63 on ST(3) can realloc/free path's PV, dangling p before cf_create() uses it. */
64 40 ▶ mode_t mode = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? (mode_t)SvUV(ST(3)) : 0600;
Branch: 4 branches
BranchTrueFalseTotal
mode_t mode = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? (mode_t)SvUV(ST(3)) : 0600; 0 40 40
mode_t mode = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? (mode_t)SvUV(ST(3)) : 0600; 0 0 0
mode_t mode = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? (mode_t)SvUV(ST(3)) : 0600; 0 0 0
mode_t mode = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? (mode_t)SvUV(ST(3)) : 0600; 0 0 0
65 62 ▶ const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL; 0 40 40
const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL; 0 0 0
const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL; 22 18 40
66 40 ▶ if (capacity < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (capacity < 1) 1 39 40
67 1 croak("Data::CuckooFilter::Shared->new: capacity must be >= 1");
68 39 CfHandle *h = cf_create(p, (uint64_t)capacity, mode, errbuf);
69 39 ▶ if (!h) croak("Data::CuckooFilter::Shared->new: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::CuckooFilter::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 6 33 39
if (!h) croak("Data::CuckooFilter::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 6 0 6
70 33 REREAD_CLASS(); /* after all argument magic: capacity SvUV, mode/path SvGETMAGIC */
71 33 MAKE_OBJ(class, h);
72 OUTPUT:
73 RETVAL
74  
75 SV *
76 new_memfd(class, name = &PL_sv_undef, capacity = 0)
77 const char *class
78 SV *name
79 UV capacity
80 PREINIT:
81 char errbuf[CF_ERR_BUFLEN];
82 CODE:
83 5 ▶ const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL; /* undef -> default label */
Branch: 3 branches
BranchTrueFalseTotal
const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL; /* undef -> default label */ 0 3 3
const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL; /* undef -> default label */ 0 0 0
const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL; /* undef -> default label */ 2 1 3
84 3 ▶ if (capacity < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (capacity < 1) 1 2 3
85 1 croak("Data::CuckooFilter::Shared->new_memfd: capacity must be >= 1");
86 2 CfHandle *h = cf_create_memfd(nm, (uint64_t)capacity, errbuf);
87 2 ▶ if (!h) croak("Data::CuckooFilter::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::CuckooFilter::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 2 2
if (!h) croak("Data::CuckooFilter::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 0 0
88 2 REREAD_CLASS(); /* after all argument magic: capacity SvUV, name SvGETMAGIC */
89 2 MAKE_OBJ(class, h);
90 OUTPUT:
91 RETVAL
92  
93 SV *
94 new_from_fd(class, fd)
95 const char *class
96 int fd
97 PREINIT:
98 char errbuf[CF_ERR_BUFLEN];
99 CODE:
100 3 CfHandle *h = cf_open_fd(fd, errbuf);
101 3 ▶ if (!h) croak("Data::CuckooFilter::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::CuckooFilter::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 2 1 3
if (!h) croak("Data::CuckooFilter::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 2 0 2
102 1 REREAD_CLASS(); /* after all argument magic: fd SvIV conversion */
103 1 MAKE_OBJ(class, h);
104 OUTPUT:
105 RETVAL
106  
107 SV *
108 new_readonly(class, path)
109 const char *class
110 SV *path
111 PREINIT:
112 char errbuf[CF_ERR_BUFLEN];
113 CODE:
114 /* Open a FROZEN (sealed) file read-only: O_RDONLY + PROT_READ, no lock
115 ever. A sealed file is immutable and no read path writes the mapping,
116 so queries take no reader-slot / rwlock traffic and any number of
117 processes can share one PROT_READ mapping (same architecture; the
118 magic rejects a wrong-endian file). */
119 11 ▶ const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL; 0 6 6
const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL; 0 0 0
const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL; 5 1 6
120 6 ▶ if (!p) croak("Data::CuckooFilter::Shared->new_readonly: path is required");
Branch: 1 branch
BranchTrueFalseTotal
if (!p) croak("Data::CuckooFilter::Shared->new_readonly: path is required"); 1 5 6
121 5 CfHandle *h = cf_open_readonly(p, errbuf);
122 5 ▶ if (!h) croak("Data::CuckooFilter::Shared->new_readonly: %s", errbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) croak("Data::CuckooFilter::Shared->new_readonly: %s", errbuf); 2 3 5
123 3 REREAD_CLASS();
124 3 MAKE_OBJ(class, h);
125 OUTPUT:
126 RETVAL
127  
128 void
129 DESTROY(self)
130 SV *self
131 CODE:
132 41 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::CuckooFilter::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::CuckooFilter::Shared")) { 41 0 41
if (sv_isobject(self) && sv_derived_from(self, "Data::CuckooFilter::Shared")) { 41 0 41
133 41 CfHandle *h = INT2PTR(CfHandle*, SvIV(SvRV(self)));
134 41 ▶ if (h) { sv_setiv(SvRV(self), 0); cf_destroy(h); } /* null first: activates EXTRACT's use-after-destroy croak + makes a double DESTROY a no-op */
Branch: 1 branch
BranchTrueFalseTotal
if (h) { sv_setiv(SvRV(self), 0); cf_destroy(h); } /* null first: activates EXTRACT's use-after-destroy croak + makes a double DESTROY a no-op */ 39 2 41
135 }
136  
137 int
138 add(self, item)
139 SV *self
140 SV *item
141 PREINIT:
142 21845 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 21845 0 21845
EXTRACT(self); 0 21845 21845
EXTRACT(self); 0 21845 21845
143 STRLEN n;
144 const char *s;
145 CODE:
146 21845 ▶ if (h->readonly) croak("Data::CuckooFilter::Shared->add: filter is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::CuckooFilter::Shared->add: filter is frozen (read-only)"); 2 21843 21845
147 21842 s = SvPVbyte(item, n); /* may croak (wide char) -- BEFORE the lock */
148 21842 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 21842 21842
REEXTRACT(self); 0 21842 21842
149 21842 cf_rwlock_wrlock(h);
150 21842 ▶ if (h->hdr->sealed) { cf_rwlock_wrunlock(h); croak("Data::CuckooFilter::Shared->add: filter is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { cf_rwlock_wrunlock(h); croak("Data::CuckooFilter::Shared->add: filter is frozen (read-only)"); } 1 21841 21842
151 21841 RETVAL = cf_add_locked(h, s, n);
152 21841 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
153 21841 cf_rwlock_wrunlock(h);
154 OUTPUT:
155 RETVAL
156  
157 UV
158 add_many(self, items)
159 SV *self
160 SV *items
161 PREINIT:
162 15 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 15 0 15
EXTRACT(self); 0 15 15
EXTRACT(self); 0 15 15
163 AV *av;
164 IV top;
165 15 ▶ UV added = 0;
Branch: 1 branch
BranchTrueFalseTotal
UV added = 0; 15 0 15
166 CODE:
167 15 ▶ if (h->readonly) croak("Data::CuckooFilter::Shared->add_many: filter is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::CuckooFilter::Shared->add_many: filter is frozen (read-only)"); 2 13 15
168 13 ▶ SvGETMAGIC(items);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(items); 0 13 13
SvGETMAGIC(items); 0 0 0
169 13 ▶ if (!SvROK(items) || SvTYPE(SvRV(items)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(items) || SvTYPE(SvRV(items)) != SVt_PVAV) 12 1 13
if (!SvROK(items) || SvTYPE(SvRV(items)) != SVt_PVAV) 1 11 12
170 2 croak("Data::CuckooFilter::Shared->add_many: expected an array reference");
171 11 av = (AV *)SvRV(items);
172 11 sv_2mortal(SvREFCNT_inc((SV *)av)); /* pin the arrayref: element magic below cannot free it mid-loop */
173 11 top = av_len(av); /* last index, -1 if empty */
174 {
175 11 ▶ STRLEN cnt = (top >= 0) ? (STRLEN)(top + 1) : 0, i;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN cnt = (top >= 0) ? (STRLEN)(top + 1) : 0, i; 10 1 11
176 11 const char **ps = NULL; STRLEN *ls = NULL;
177 11 ▶ if (cnt) { /* resolve all bytes BEFORE locking */
Branch: 1 branch
BranchTrueFalseTotal
if (cnt) { /* resolve all bytes BEFORE locking */ 10 1 11
178 10 ▶ Newx(ps, cnt, const char *); SAVEFREEPV(ps); /* freed on return OR unwind */
Branch: 1 branch
BranchTrueFalseTotal
Newx(ps, cnt, const char *); SAVEFREEPV(ps); /* freed on return OR unwind */ 0 10 10
179 10 ▶ Newx(ls, cnt, STRLEN); SAVEFREEPV(ls);
Branch: 1 branch
BranchTrueFalseTotal
Newx(ls, cnt, STRLEN); SAVEFREEPV(ls); 0 10 10
180 13531 ▶ for (i = 0; i < cnt; i++) { /* a croak here holds NO lock; SAVEFREEPV cleans up */
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) { /* a croak here holds NO lock; SAVEFREEPV cleans up */ 13522 9 13531
181 13522 SV **el = av_fetch(av, (SSize_t)i, 0);
182 13522 ▶ if (el && *el) {
Branch: 2 branches
BranchTrueFalseTotal
if (el && *el) { 13522 0 13522
if (el && *el) { 13522 0 13522
183 STRLEN len;
184 13522 const char *src = SvPVbyte(*el, len); /* may run overload/tie/get-magic = arbitrary Perl */
185 /* Copy bytes into a private mortal SV NOW: a LATER element SvPVbyte can
186 * grow/free THIS element PV, dangling src before the locked loop uses it. */
187 13521 SV *copy = sv_2mortal(newSVpvn(src, len));
188 13521 ps[i] = SvPVX_const(copy);
189 13521 ls[i] = len;
190 0 } else { ps[i] = ""; ls[i] = 0; }
191 }
192 }
193 10 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 10 10
REEXTRACT(self); 0 10 10
194 10 cf_rwlock_wrlock(h); /* locked region: NO croak-capable calls */
195 10 ▶ if (h->hdr->sealed) { cf_rwlock_wrunlock(h); croak("Data::CuckooFilter::Shared->add_many: filter is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { cf_rwlock_wrunlock(h); croak("Data::CuckooFilter::Shared->add_many: filter is frozen (read-only)"); } 1 9 10
196 13528 ▶ for (i = 0; i < cnt; i++) added += (UV)cf_add_locked(h, ps[i], ls[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) added += (UV)cf_add_locked(h, ps[i], ls[i]); 13519 9 13528
197 9 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED); /* a call always counts, even an empty batch */
198 9 cf_rwlock_wrunlock(h);
199 }
200 9 ▶ RETVAL = added;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = added; 1 8 9
201 OUTPUT:
202 RETVAL
203  
204 int
205 contains(self, item)
206 SV *self
207 SV *item
208 PREINIT:
209 26845 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 26845 0 26845
EXTRACT(self); 0 26845 26845
EXTRACT(self); 1 26844 26845
210 STRLEN n;
211 const char *s;
212 CODE:
213 26843 s = SvPVbyte(item, n); /* may croak (wide char) -- BEFORE the lock */
214 26843 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 26843 26843
REEXTRACT(self); 0 26843 26843
215 26843 ▶ if (h->readonly) { /* frozen: immutable table, no lock needed */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: immutable table, no lock needed */ 12 26831 26843
216 12 RETVAL = cf_contains_locked(h, s, n);
217 } else {
218 26831 cf_rwlock_rdlock(h);
219 26831 RETVAL = cf_contains_locked(h, s, n);
220 26831 cf_rwlock_rdunlock(h);
221 }
222 OUTPUT:
223 RETVAL
224  
225 int
226 count_of(self, item)
227 SV *self
228 SV *item
229 PREINIT:
230 24 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 24 0 24
EXTRACT(self); 0 24 24
EXTRACT(self); 0 24 24
231 STRLEN n;
232 const char *s;
233 CODE:
234 23 s = SvPVbyte(item, n); /* may croak (wide char) -- BEFORE the lock */
235 23 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 23 23
REEXTRACT(self); 0 23 23
236 23 ▶ if (h->readonly) { /* frozen: immutable table, no lock needed */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: immutable table, no lock needed */ 3 20 23
237 3 RETVAL = cf_count_of_locked(h, s, n);
238 } else {
239 20 cf_rwlock_rdlock(h);
240 20 RETVAL = cf_count_of_locked(h, s, n);
241 20 cf_rwlock_rdunlock(h);
242 }
243 OUTPUT:
244 RETVAL
245  
246 int
247 remove(self, item)
248 SV *self
249 SV *item
250 PREINIT:
251 4017 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4017 0 4017
EXTRACT(self); 0 4017 4017
EXTRACT(self); 0 4017 4017
252 STRLEN n;
253 const char *s;
254 CODE:
255 4017 ▶ if (h->readonly) croak("Data::CuckooFilter::Shared->remove: filter is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::CuckooFilter::Shared->remove: filter is frozen (read-only)"); 2 4015 4017
256 4014 s = SvPVbyte(item, n); /* may croak (wide char) -- BEFORE the lock */
257 4014 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 4014 4014
REEXTRACT(self); 0 4014 4014
258 4014 cf_rwlock_wrlock(h);
259 4014 ▶ if (h->hdr->sealed) { cf_rwlock_wrunlock(h); croak("Data::CuckooFilter::Shared->remove: filter is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { cf_rwlock_wrunlock(h); croak("Data::CuckooFilter::Shared->remove: filter is frozen (read-only)"); } 1 4013 4014
260 4013 RETVAL = cf_remove_locked(h, s, n);
261 4013 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
262 4013 cf_rwlock_wrunlock(h);
263 OUTPUT:
264 RETVAL
265  
266 void
267 clear(self)
268 SV *self
269 PREINIT:
270 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
271 CODE:
272 4 ▶ if (h->readonly) croak("Data::CuckooFilter::Shared->clear: filter is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::CuckooFilter::Shared->clear: filter is frozen (read-only)"); 2 2 4
273 2 cf_rwlock_wrlock(h);
274 2 ▶ if (h->hdr->sealed) { cf_rwlock_wrunlock(h); croak("Data::CuckooFilter::Shared->clear: filter is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { cf_rwlock_wrunlock(h); croak("Data::CuckooFilter::Shared->clear: filter is frozen (read-only)"); } 1 1 2
275 1 cf_clear_locked(h);
276 1 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
277 1 cf_rwlock_wrunlock(h);
278  
279 void
280 freeze(self)
281 SV *self
282 PREINIT:
283 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
284 CODE:
285 4 ▶ if (h->readonly) croak("Data::CuckooFilter::Shared->freeze: cannot freeze a read-only handle");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::CuckooFilter::Shared->freeze: cannot freeze a read-only handle"); 2 2 4
286 2 ▶ if (cf_freeze(h) != 0) croak("Data::CuckooFilter::Shared->freeze: msync: %s", strerror(errno));
Branch: 1 branch
BranchTrueFalseTotal
if (cf_freeze(h) != 0) croak("Data::CuckooFilter::Shared->freeze: msync: %s", strerror(errno)); 0 2 2
287 2 h->readonly = 1; /* this handle now rejects mutation too (the file is sealed) */
288  
289 UV
290 frozen(self)
291 SV *self
292 PREINIT:
293 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
294 CODE:
295 4 ▶ RETVAL = __atomic_load_n(&h->hdr->sealed, __ATOMIC_ACQUIRE) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = __atomic_load_n(&h->hdr->sealed, __ATOMIC_ACQUIRE) ? 1 : 0; 0 4 4
296 OUTPUT:
297 RETVAL
298  
299 UV
300 readonly(self)
301 SV *self
302 PREINIT:
303 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
304 CODE:
305 4 ▶ RETVAL = h->readonly ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->readonly ? 1 : 0; 0 4 4
306 OUTPUT:
307 RETVAL
308  
309 UV
310 count(self)
311 SV *self
312 PREINIT:
313 21 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 21 0 21
EXTRACT(self); 0 21 21
EXTRACT(self); 0 21 21
314 UV n;
315 CODE:
316 21 ▶ if (h->readonly) { /* frozen: immutable, no lock needed */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: immutable, no lock needed */ 2 19 21
317 2 n = (UV)h->hdr->count;
318 } else {
319 19 cf_rwlock_rdlock(h);
320 19 n = (UV)h->hdr->count;
321 19 cf_rwlock_rdunlock(h);
322 }
323 21 ▶ RETVAL = n;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = n; 2 19 21
324 OUTPUT:
325 RETVAL
326  
327 UV
328 capacity(self)
329 SV *self
330 PREINIT:
331 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
332 CODE:
333 4 ▶ RETVAL = (UV)h->hdr->capacity;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->hdr->capacity; 0 4 4
334 OUTPUT:
335 RETVAL
336  
337 UV
338 buckets(self)
339 SV *self
340 PREINIT:
341 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
342 CODE:
343 4 ▶ RETVAL = (UV)h->hdr->num_buckets;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->hdr->num_buckets; 0 4 4
344 OUTPUT:
345 RETVAL
346  
347 UV
348 slots(self)
349 SV *self
350 PREINIT:
351 5 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 5 0 5
EXTRACT(self); 0 5 5
EXTRACT(self); 0 5 5
352 CODE:
353 5 ▶ RETVAL = (UV)(h->hdr->num_buckets * (uint64_t)CF_SLOTS);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)(h->hdr->num_buckets * (uint64_t)CF_SLOTS); 0 5 5
354 OUTPUT:
355 RETVAL
356  
357 SV *
358 stats(self)
359 SV *self
360 PREINIT:
361 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
362 CODE:
363 {
364 uint64_t count, capacity, num_buckets, slots_total, ops, mmap_size;
365 /* Snapshot under the lock; do all (croak-capable) Perl allocation after
366 releasing it -- so an OOM in newHV/newSVuv can never strand the lock. */
367 4 ▶ if (!h->readonly) cf_rwlock_rdlock(h); /* frozen: immutable, no lock */
Branch: 1 branch
BranchTrueFalseTotal
if (!h->readonly) cf_rwlock_rdlock(h); /* frozen: immutable, no lock */ 3 1 4
368 4 count = h->hdr->count;
369 4 capacity = h->hdr->capacity;
370 4 num_buckets = h->hdr->num_buckets;
371 4 ops = h->hdr->stat_ops;
372 4 ▶ if (!h->readonly) cf_rwlock_rdunlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (!h->readonly) cf_rwlock_rdunlock(h); 3 1 4
373 4 slots_total = num_buckets * (uint64_t)CF_SLOTS;
374 4 mmap_size = (uint64_t)h->mmap_size;
375  
376 4 HV *hv = newHV();
377 4 hv_stores(hv, "capacity", newSVuv((UV)capacity));
378 4 hv_stores(hv, "buckets", newSVuv((UV)num_buckets));
379 4 hv_stores(hv, "slots", newSVuv((UV)slots_total));
380 4 hv_stores(hv, "count", newSVuv((UV)count));
381 4 hv_stores(hv, "fill_ratio", newSVnv((double)count / (double)slots_total)); /* slots_total >= CF_MIN_BUCKETS*CF_SLOTS, never 0 */
382 4 hv_stores(hv, "ops", newSVuv((UV)ops));
383 4 hv_stores(hv, "mmap_size", newSVuv((UV)mmap_size));
384 4 hv_stores(hv, "frozen", newSVuv(__atomic_load_n(&h->hdr->sealed, __ATOMIC_ACQUIRE) ? 1 : 0));
385 4 hv_stores(hv, "readonly", newSVuv(h->readonly ? 1 : 0));
386 4 RETVAL = newRV_noinc((SV *)hv);
387 }
388 OUTPUT:
389 RETVAL
390  
391 SV *
392 path(self)
393 SV *self
394 PREINIT:
395 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
396 CODE:
397 4 ▶ RETVAL = h->path ? newSVpv(h->path, 0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->path ? newSVpv(h->path, 0) : &PL_sv_undef; 2 2 4
398 OUTPUT:
399 RETVAL
400  
401 int
402 memfd(self)
403 SV *self
404 PREINIT:
405 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
406 CODE:
407 4 ▶ RETVAL = h->backing_fd;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->backing_fd; 0 4 4
408 OUTPUT:
409 RETVAL
410  
411 void
412 sync(self)
413 SV *self
414 PREINIT:
415 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
416 CODE:
417 3 ▶ if (!h->readonly && cf_msync(h) != 0) croak("sync: %s", strerror(errno));
Branch: 2 branches
BranchTrueFalseTotal
if (!h->readonly && cf_msync(h) != 0) croak("sync: %s", strerror(errno)); 2 1 3
if (!h->readonly && cf_msync(h) != 0) croak("sync: %s", strerror(errno)); 0 2 2
418  
419 void
420 unlink(self, ...)
421 SV *self
422 CODE:
423 9 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::CuckooFilter::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::CuckooFilter::Shared")) { 4 2 6
if (sv_isobject(self) && sv_derived_from(self, "Data::CuckooFilter::Shared")) { 4 0 4
424 4 CfHandle *h = INT2PTR(CfHandle*, SvIV(SvRV(self)));
425 4 ▶ if (h && h->path && unlink(h->path) != 0 && errno != ENOENT)
Branch: 4 branches
BranchTrueFalseTotal
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 4 0 4
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 4 0 4
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 2 2 4
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 1 1 2
426 1 croak("Data::CuckooFilter::Shared->unlink(%s): %s", h->path, strerror(errno));
427 2 ▶ } else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) {
Branch: 4 branches
BranchTrueFalseTotal
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 2 0 2
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 0 2 2
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 0 0 0
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 2 0 2
428 {
429 2 const char *up = SvPV_nolen(ST(1));
430 2 ▶ if (unlink(up) != 0 && errno != ENOENT)
Branch: 2 branches
BranchTrueFalseTotal
if (unlink(up) != 0 && errno != ENOENT) 1 1 2
if (unlink(up) != 0 && errno != ENOENT) 0 1 1
431 0 croak("Data::CuckooFilter::Shared->unlink(%s): %s", up, strerror(errno));
432 }
433 }
« cuckoo.h ↑ Summary