Shared.xs

filter: Statementstmt 98.4% 185 / 188 Branchbran 62.1% 199 / 320 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.5% 384 / 508 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« cbf.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 "cbf.h"
7  
8 #define EXTRACT(sv) \
9 if (!sv_isobject(sv) || !sv_derived_from(sv, "Data::CountingBloomFilter::Shared")) \
10 croak("Expected a Data::CountingBloomFilter::Shared object"); \
11 CbfHandle *h = INT2PTR(CbfHandle*, SvIV(SvRV(sv))); \
12 if (!h) croak("Attempted to use a destroyed Data::CountingBloomFilter::Shared object"); \
13 CbfHandle *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 pins
19 * the referent only against refcount-driven destruction, not an explicit
20 * DESTROY, so the local `h` would dangle. Used only where magic can actually
21 * intervene between EXTRACT and the first use of h. */
22 #define REEXTRACT(sv) \
23 if (!SvROK(sv)) \
24 croak("Data::CountingBloomFilter::Shared object was replaced during the call"); \
25 h = INT2PTR(CbfHandle*, SvIV(SvRV(sv))); \
26 if (h != h0) croak("Data::CountingBloomFilter::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 /* Re-read the class-name PV immediately before blessing the new object.
35 * `class` is captured by the typemap in the INPUT section; a tied/overloaded
36 * later constructor argument can run get-magic that reallocs or frees ST(0)'s
37 * PV, dangling that pointer before it is used to bless. Same fix as
38 * Data::CuckooFilter::Shared already carries. */
39 #define REREAD_CLASS() \
40 class = SvPV_nolen(ST(0))
41  
42 MODULE = Data::CountingBloomFilter::Shared PACKAGE = Data::CountingBloomFilter::Shared
43  
44 PROTOTYPES: DISABLE
45  
46 SV *
47 new(class, path = &PL_sv_undef, capacity = 0, fp_rate = 0.01, ...)
48 const char *class
49 SV *path
50 UV capacity
51 double fp_rate
52 PREINIT:
53 char errbuf[CBF_ERR_BUFLEN];
54 CODE:
55 58 ▶ if (capacity < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (capacity < 1) 1 57 58
56 1 croak("Data::CountingBloomFilter::Shared->new: capacity must be >= 1");
57 57 ▶ if (!(fp_rate > 0.0 && fp_rate < 1.0))
Branch: 2 branches
BranchTrueFalseTotal
if (!(fp_rate > 0.0 && fp_rate < 1.0)) 56 1 57
if (!(fp_rate > 0.0 && fp_rate < 1.0)) 2 54 56
58 3 croak("Data::CountingBloomFilter::Shared->new: fp_rate must be between 0 and 1 (exclusive)");
59 /* Optional 5th arg: file mode for a newly-created file-backed segment
60 * (default 0600, owner-only). Pass e.g. 0660 to opt into cross-user
61 * sharing. Ignored for anonymous segments and existing files. */
62 54 ▶ mode_t mode = (items > 4 && (SvGETMAGIC(ST(4)), SvOK(ST(4)))) ? (mode_t)SvUV(ST(4)) : 0600;
Branch: 4 branches
BranchTrueFalseTotal
mode_t mode = (items > 4 && (SvGETMAGIC(ST(4)), SvOK(ST(4)))) ? (mode_t)SvUV(ST(4)) : 0600; 1 53 54
mode_t mode = (items > 4 && (SvGETMAGIC(ST(4)), SvOK(ST(4)))) ? (mode_t)SvUV(ST(4)) : 0600; 0 1 1
mode_t mode = (items > 4 && (SvGETMAGIC(ST(4)), SvOK(ST(4)))) ? (mode_t)SvUV(ST(4)) : 0600; 0 0 0
mode_t mode = (items > 4 && (SvGETMAGIC(ST(4)), SvOK(ST(4)))) ? (mode_t)SvUV(ST(4)) : 0600; 1 0 1
63 /* Capture the path PV LAST, after ST(4) get-magic above: that magic can run
64 * arbitrary Perl that reallocs/frees path's PV, dangling the pointer before
65 * cbf_create() uses it. */
66 74 ▶ 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 54 54
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; 20 34 54
67 54 CbfHandle *h = cbf_create(p, (uint64_t)capacity, fp_rate, mode, errbuf);
68 54 ▶ if (!h) croak("Data::CountingBloomFilter::Shared->new: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::CountingBloomFilter::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 6 48 54
if (!h) croak("Data::CountingBloomFilter::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 6 0 6
69 48 REREAD_CLASS();
70 48 MAKE_OBJ(class, h);
71 OUTPUT:
72 RETVAL
73  
74 SV *
75 new_memfd(class, name = &PL_sv_undef, capacity = 0, fp_rate = 0.01)
76 const char *class
77 SV *name
78 UV capacity
79 double fp_rate
80 PREINIT:
81 char errbuf[CBF_ERR_BUFLEN];
82 CODE:
83 9 ▶ 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 5 5
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 */ 4 1 5
84 5 ▶ if (capacity < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (capacity < 1) 1 4 5
85 1 croak("Data::CountingBloomFilter::Shared->new_memfd: capacity must be >= 1");
86 4 ▶ if (!(fp_rate > 0.0 && fp_rate < 1.0))
Branch: 2 branches
BranchTrueFalseTotal
if (!(fp_rate > 0.0 && fp_rate < 1.0)) 3 1 4
if (!(fp_rate > 0.0 && fp_rate < 1.0)) 1 2 3
87 2 croak("Data::CountingBloomFilter::Shared->new_memfd: fp_rate must be between 0 and 1 (exclusive)");
88 2 CbfHandle *h = cbf_create_memfd(nm, (uint64_t)capacity, fp_rate, errbuf);
89 2 ▶ if (!h) croak("Data::CountingBloomFilter::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::CountingBloomFilter::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 2 2
if (!h) croak("Data::CountingBloomFilter::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 0 0
90 2 REREAD_CLASS();
91 2 MAKE_OBJ(class, h);
92 OUTPUT:
93 RETVAL
94  
95 SV *
96 new_from_fd(class, fd)
97 const char *class
98 int fd
99 PREINIT:
100 char errbuf[CBF_ERR_BUFLEN];
101 CODE:
102 2 CbfHandle *h = cbf_open_fd(fd, errbuf);
103 2 ▶ if (!h) croak("Data::CountingBloomFilter::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::CountingBloomFilter::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 1 1 2
if (!h) croak("Data::CountingBloomFilter::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 1 0 1
104 1 REREAD_CLASS();
105 1 MAKE_OBJ(class, h);
106 OUTPUT:
107 RETVAL
108  
109 SV *
110 new_readonly(class, path)
111 const char *class
112 SV *path
113 PREINIT:
114 char errbuf[CBF_ERR_BUFLEN];
115 CODE:
116 /* Open a FROZEN (sealed) file read-only: O_RDONLY + PROT_READ, no lock
117 ever. A sealed file is immutable and no read path writes the mapping,
118 so queries take no reader-slot / rwlock traffic and any number of
119 processes can share one PROT_READ mapping (same architecture; the
120 magic rejects a wrong-endian file). */
121 13 ▶ 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 7 7
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; 6 1 7
122 7 ▶ if (!p) croak("Data::CountingBloomFilter::Shared->new_readonly: path is required");
Branch: 1 branch
BranchTrueFalseTotal
if (!p) croak("Data::CountingBloomFilter::Shared->new_readonly: path is required"); 1 6 7
123 6 CbfHandle *h = cbf_open_readonly(p, errbuf);
124 6 ▶ if (!h) croak("Data::CountingBloomFilter::Shared->new_readonly: %s", errbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) croak("Data::CountingBloomFilter::Shared->new_readonly: %s", errbuf); 2 4 6
125 4 REREAD_CLASS();
126 4 MAKE_OBJ(class, h);
127 OUTPUT:
128 RETVAL
129  
130 void
131 DESTROY(self)
132 SV *self
133 CODE:
134 57 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::CountingBloomFilter::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::CountingBloomFilter::Shared")) { 57 0 57
if (sv_isobject(self) && sv_derived_from(self, "Data::CountingBloomFilter::Shared")) { 57 0 57
135 57 CbfHandle *h = INT2PTR(CbfHandle*, SvIV(SvRV(self)));
136 57 ▶ if (h) { sv_setiv(SvRV(self), 0); cbf_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); cbf_destroy(h); } /* null first: activates EXTRACT's use-after-destroy croak + makes a double DESTROY a no-op */ 55 2 57
137 }
138  
139 int
140 add(self, item)
141 SV *self
142 SV *item
143 PREINIT:
144 35076 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 35076 0 35076
EXTRACT(self); 0 35076 35076
EXTRACT(self); 0 35076 35076
145 STRLEN n;
146 const char *s;
147 CODE:
148 35076 ▶ if (h->readonly) croak("Data::CountingBloomFilter::Shared->add: filter is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::CountingBloomFilter::Shared->add: filter is frozen (read-only)"); 2 35074 35076
149 35073 s = SvPVbyte(item, n); /* may croak (wide char) -- BEFORE the lock */
150 35073 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 35073 35073
REEXTRACT(self); 0 35073 35073
151 35073 cbf_rwlock_wrlock(h);
152 35073 ▶ if (h->hdr->sealed) { cbf_rwlock_wrunlock(h); croak("Data::CountingBloomFilter::Shared->add: filter is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { cbf_rwlock_wrunlock(h); croak("Data::CountingBloomFilter::Shared->add: filter is frozen (read-only)"); } 1 35072 35073
153 35072 RETVAL = cbf_add_locked(h, s, n);
154 35072 h->hdr->stat_ops++;
155 35072 cbf_rwlock_wrunlock(h);
156 OUTPUT:
157 RETVAL
158  
159 UV
160 add_many(self, items)
161 SV *self
162 SV *items
163 PREINIT:
164 15 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 15 0 15
EXTRACT(self); 0 15 15
EXTRACT(self); 0 15 15
165 AV *av;
166 IV top;
167 15 ▶ UV added = 0;
Branch: 1 branch
BranchTrueFalseTotal
UV added = 0; 15 0 15
168 CODE:
169 15 ▶ if (h->readonly) croak("Data::CountingBloomFilter::Shared->add_many: filter is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::CountingBloomFilter::Shared->add_many: filter is frozen (read-only)"); 2 13 15
170 13 ▶ SvGETMAGIC(items);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(items); 0 13 13
SvGETMAGIC(items); 0 0 0
171 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
172 2 croak("Data::CountingBloomFilter::Shared->add_many: expected an array reference");
173 11 av = (AV *)SvRV(items);
174 11 sv_2mortal(SvREFCNT_inc((SV *)av)); /* pin the arrayref: element magic below cannot free it mid-loop */
175 11 top = av_len(av); /* last index, -1 if empty */
176 {
177 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
178 11 const char **ps = NULL; STRLEN *ls = NULL;
179 11 ▶ if (cnt) { /* resolve all bytes BEFORE locking */
Branch: 1 branch
BranchTrueFalseTotal
if (cnt) { /* resolve all bytes BEFORE locking */ 10 1 11
180 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
181 10 ▶ Newx(ls, cnt, STRLEN); SAVEFREEPV(ls);
Branch: 1 branch
BranchTrueFalseTotal
Newx(ls, cnt, STRLEN); SAVEFREEPV(ls); 0 10 10
182 6815 ▶ for (i = 0; i < cnt; i++) { /* a croak here holds NO lock; SAVEFREEPV + mortals clean up */
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) { /* a croak here holds NO lock; SAVEFREEPV + mortals clean up */ 6806 9 6815
183 6806 SV **el = av_fetch(av, (SSize_t)i, 0);
184 6806 ▶ if (el && *el) {
Branch: 2 branches
BranchTrueFalseTotal
if (el && *el) { 6806 0 6806
if (el && *el) { 6806 0 6806
185 STRLEN len;
186 6806 const char *src = SvPVbyte(*el, len); /* may run overload/tie/get-magic = arbitrary Perl */
187 /* Copy the bytes into a private mortal SV NOW. SvPVbyte on a
188 * LATER element can run Perl that grows/frees THIS element's
189 * PV buffer, dangling `src` before the locked loop hashes it
190 * (heap-use-after-free). The mortal copy is immune and is
191 * freed on normal return or croak unwind. */
192 6805 SV *copy = sv_2mortal(newSVpvn(src, len));
193 6805 ps[i] = SvPVX_const(copy);
194 6805 ls[i] = len;
195 0 } else { ps[i] = ""; ls[i] = 0; }
196 }
197 }
198 10 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 10 10
REEXTRACT(self); 0 10 10
199 10 cbf_rwlock_wrlock(h); /* locked region: NO croak-capable calls */
200 10 ▶ if (h->hdr->sealed) { cbf_rwlock_wrunlock(h); croak("Data::CountingBloomFilter::Shared->add_many: filter is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { cbf_rwlock_wrunlock(h); croak("Data::CountingBloomFilter::Shared->add_many: filter is frozen (read-only)"); } 1 9 10
201 6812 ▶ for (i = 0; i < cnt; i++) added += (UV)cbf_add_locked(h, ps[i], ls[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) added += (UV)cbf_add_locked(h, ps[i], ls[i]); 6803 9 6812
202 9 h->hdr->stat_ops++; /* a call always counts, even an empty batch */
203 9 cbf_rwlock_wrunlock(h);
204 }
205 9 ▶ RETVAL = added;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = added; 0 9 9
206 OUTPUT:
207 RETVAL
208  
209 int
210 contains(self, item)
211 SV *self
212 SV *item
213 PREINIT:
214 29339 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 29339 0 29339
EXTRACT(self); 0 29339 29339
EXTRACT(self); 1 29338 29339
215 STRLEN n;
216 const char *s;
217 CODE:
218 29338 s = SvPVbyte(item, n); /* may croak (wide char) -- BEFORE the lock */
219 29338 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 29338 29338
REEXTRACT(self); 0 29338 29338
220 29338 ▶ if (h->readonly) { /* frozen: immutable counters, no lock needed */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: immutable counters, no lock needed */ 12 29326 29338
221 12 RETVAL = cbf_contains_locked(h, s, n);
222 } else {
223 29326 cbf_rwlock_rdlock(h);
224 29326 RETVAL = cbf_contains_locked(h, s, n);
225 29326 cbf_rwlock_rdunlock(h);
226 }
227 OUTPUT:
228 RETVAL
229  
230 int
231 count_of(self, item)
232 SV *self
233 SV *item
234 PREINIT:
235 2018 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 2018 0 2018
EXTRACT(self); 0 2018 2018
EXTRACT(self); 0 2018 2018
236 STRLEN n;
237 const char *s;
238 CODE:
239 2017 s = SvPVbyte(item, n); /* may croak (wide char) -- BEFORE the lock */
240 2017 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 2017 2017
REEXTRACT(self); 0 2017 2017
241 2017 ▶ if (h->readonly) { /* frozen: immutable counters, no lock needed */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: immutable counters, no lock needed */ 3 2014 2017
242 3 RETVAL = cbf_count_of_locked(h, s, n);
243 } else {
244 2014 cbf_rwlock_rdlock(h);
245 2014 RETVAL = cbf_count_of_locked(h, s, n);
246 2014 cbf_rwlock_rdunlock(h);
247 }
248 OUTPUT:
249 RETVAL
250  
251 int
252 remove(self, item)
253 SV *self
254 SV *item
255 PREINIT:
256 3536 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3536 0 3536
EXTRACT(self); 0 3536 3536
EXTRACT(self); 0 3536 3536
257 STRLEN n;
258 const char *s;
259 CODE:
260 3536 ▶ if (h->readonly) croak("Data::CountingBloomFilter::Shared->remove: filter is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::CountingBloomFilter::Shared->remove: filter is frozen (read-only)"); 2 3534 3536
261 3533 s = SvPVbyte(item, n); /* may croak (wide char) -- BEFORE the lock */
262 3533 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 3533 3533
REEXTRACT(self); 0 3533 3533
263 3533 cbf_rwlock_wrlock(h);
264 3533 ▶ if (h->hdr->sealed) { cbf_rwlock_wrunlock(h); croak("Data::CountingBloomFilter::Shared->remove: filter is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { cbf_rwlock_wrunlock(h); croak("Data::CountingBloomFilter::Shared->remove: filter is frozen (read-only)"); } 1 3532 3533
265 3532 RETVAL = cbf_remove_locked(h, s, n);
266 3532 h->hdr->stat_ops++;
267 3532 cbf_rwlock_wrunlock(h);
268 OUTPUT:
269 RETVAL
270  
271 void
272 merge(self, other)
273 SV *self
274 SV *other
275 PREINIT:
276 9 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 9 0 9
EXTRACT(self); 0 9 9
EXTRACT(self); 0 9 9
277 CODE:
278 9 ▶ if (h->readonly) croak("Data::CountingBloomFilter::Shared->merge: filter is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::CountingBloomFilter::Shared->merge: filter is frozen (read-only)"); 1 8 9
279 8 ▶ if (!sv_isobject(other) || !sv_derived_from(other, "Data::CountingBloomFilter::Shared"))
Branch: 2 branches
BranchTrueFalseTotal
if (!sv_isobject(other) || !sv_derived_from(other, "Data::CountingBloomFilter::Shared")) 8 0 8
if (!sv_isobject(other) || !sv_derived_from(other, "Data::CountingBloomFilter::Shared")) 0 8 8
280 0 croak("Data::CountingBloomFilter::Shared->merge: expected a Data::CountingBloomFilter::Shared object");
281 8 CbfHandle *o = INT2PTR(CbfHandle*, SvIV(SvRV(other)));
282 8 ▶ if (!o) croak("Attempted to use a destroyed Data::CountingBloomFilter::Shared object");
Branch: 1 branch
BranchTrueFalseTotal
if (!o) croak("Attempted to use a destroyed Data::CountingBloomFilter::Shared object"); 0 8 8
283 /* sv_isobject/sv_derived_from above begin with SvGETMAGIC(other), so a
284 * tied `other` can have destroyed self before h is used below. `o` was
285 * read after that magic and needs no re-read. */
286 8 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 8 8
REEXTRACT(self); 0 8 8
287  
288 /* m_ctr and k are immutable after creation -- compare lock-free, croak
289 * BEFORE allocating, so a mismatch holds no lock and leaks no buffer. */
290 8 uint64_t om = o->hdr->m_ctr;
291 8 uint32_t ok = o->hdr->k;
292 8 ▶ if (om != h->hdr->m_ctr || ok != h->hdr->k)
Branch: 2 branches
BranchTrueFalseTotal
if (om != h->hdr->m_ctr || ok != h->hdr->k) 8 0 8
if (om != h->hdr->m_ctr || ok != h->hdr->k) 1 7 8
293 1 croak("Data::CountingBloomFilter::Shared->merge: geometry mismatch (%llu counters/k=%u vs %llu counters/k=%u)",
294 (unsigned long long)h->hdr->m_ctr, h->hdr->k,
295 (unsigned long long)om, ok);
296  
297 /* Snapshot the other's counter bytes under its read lock into a temp buffer,
298 * then release before taking self's write lock. Copying to a temp avoids
299 * holding two locks at once (deadlock-free regardless of acquisition order
300 * between two processes merging each other). */
301 7 uint64_t bytes = om / 2;
302 /* Layer B: never read past o's real mapping when snapshotting -- o->hdr->m_ctr
303 * is in the shared segment and a peer could widen it after validation.
304 * Equals m_ctr/2 for a valid filter, so no behavior change. */
305 7 uint64_t o_bytes_max = cbf_ctr_bytes_max(o);
306 7 ▶ if (bytes > o_bytes_max) bytes = o_bytes_max;
Branch: 1 branch
BranchTrueFalseTotal
if (bytes > o_bytes_max) bytes = o_bytes_max; 0 7 7
307 uint8_t *tmp;
308 7 Newx(tmp, (size_t)bytes, uint8_t);
309 7 SAVEFREEPV(tmp); /* freed on normal return OR croak unwind */
310 7 ▶ if (o->readonly) { /* frozen other: immutable counters, no lock */
Branch: 1 branch
BranchTrueFalseTotal
if (o->readonly) { /* frozen other: immutable counters, no lock */ 2 5 7
311 2 memcpy(tmp, cbf_counters(o), (size_t)bytes);
312 } else {
313 5 cbf_rwlock_rdlock(o);
314 5 memcpy(tmp, cbf_counters(o), (size_t)bytes);
315 5 cbf_rwlock_rdunlock(o);
316 }
317  
318 7 cbf_rwlock_wrlock(h);
319 7 ▶ if (h->hdr->sealed) { cbf_rwlock_wrunlock(h); croak("Data::CountingBloomFilter::Shared->merge: filter is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { cbf_rwlock_wrunlock(h); croak("Data::CountingBloomFilter::Shared->merge: filter is frozen (read-only)"); } 1 6 7
320 6 cbf_merge_counters(h, tmp, bytes);
321 6 h->hdr->stat_ops++;
322 6 cbf_rwlock_wrunlock(h);
323  
324 void
325 clear(self)
326 SV *self
327 PREINIT:
328 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
329 CODE:
330 4 ▶ if (h->readonly) croak("Data::CountingBloomFilter::Shared->clear: filter is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::CountingBloomFilter::Shared->clear: filter is frozen (read-only)"); 2 2 4
331 2 cbf_rwlock_wrlock(h);
332 2 ▶ if (h->hdr->sealed) { cbf_rwlock_wrunlock(h); croak("Data::CountingBloomFilter::Shared->clear: filter is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { cbf_rwlock_wrunlock(h); croak("Data::CountingBloomFilter::Shared->clear: filter is frozen (read-only)"); } 1 1 2
333 1 cbf_clear_locked(h);
334 1 h->hdr->stat_ops++;
335 1 cbf_rwlock_wrunlock(h);
336  
337 void
338 freeze(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 ▶ if (h->readonly) croak("Data::CountingBloomFilter::Shared->freeze: cannot freeze a read-only handle");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::CountingBloomFilter::Shared->freeze: cannot freeze a read-only handle"); 2 2 4
344 2 ▶ if (cbf_freeze(h) != 0) croak("Data::CountingBloomFilter::Shared->freeze: msync: %s", strerror(errno));
Branch: 1 branch
BranchTrueFalseTotal
if (cbf_freeze(h) != 0) croak("Data::CountingBloomFilter::Shared->freeze: msync: %s", strerror(errno)); 0 2 2
345 2 h->readonly = 1; /* this handle now rejects mutation too (the file is sealed) */
346  
347 UV
348 frozen(self)
349 SV *self
350 PREINIT:
351 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
352 CODE:
353 3 ▶ 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 3 3
354 OUTPUT:
355 RETVAL
356  
357 UV
358 readonly(self)
359 SV *self
360 PREINIT:
361 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
362 CODE:
363 3 ▶ RETVAL = h->readonly ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->readonly ? 1 : 0; 0 3 3
364 OUTPUT:
365 RETVAL
366  
367 UV
368 capacity(self)
369 SV *self
370 PREINIT:
371 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
372 CODE:
373 4 ▶ RETVAL = (UV)h->hdr->capacity;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->hdr->capacity; 0 4 4
374 OUTPUT:
375 RETVAL
376  
377 UV
378 counters(self)
379 SV *self
380 PREINIT:
381 5 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 5 0 5
EXTRACT(self); 0 5 5
EXTRACT(self); 0 5 5
382 CODE:
383 5 ▶ RETVAL = (UV)h->hdr->m_ctr;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->hdr->m_ctr; 0 5 5
384 OUTPUT:
385 RETVAL
386  
387 UV
388 hashes(self)
389 SV *self
390 PREINIT:
391 7 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 7 0 7
EXTRACT(self); 0 7 7
EXTRACT(self); 0 7 7
392 CODE:
393 7 ▶ RETVAL = h->hdr->k;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->hdr->k; 0 7 7
394 OUTPUT:
395 RETVAL
396  
397 double
398 fp_rate(self)
399 SV *self
400 PREINIT:
401 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
402 CODE:
403 4 ▶ RETVAL = h->hdr->fp_rate;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->hdr->fp_rate; 0 4 4
404 OUTPUT:
405 RETVAL
406  
407 UV
408 count(self)
409 SV *self
410 PREINIT:
411 6 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 6 0 6
EXTRACT(self); 0 6 6
EXTRACT(self); 0 6 6
412 UV n;
413 CODE:
414 6 ▶ if (h->readonly) { /* frozen: immutable counters, no lock needed */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: immutable counters, no lock needed */ 2 4 6
415 2 n = (UV)cbf_count_locked(h);
416 } else {
417 4 cbf_rwlock_rdlock(h);
418 4 n = (UV)cbf_count_locked(h);
419 4 cbf_rwlock_rdunlock(h);
420 }
421 6 ▶ RETVAL = n;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = n; 0 6 6
422 OUTPUT:
423 RETVAL
424  
425 SV *
426 stats(self)
427 SV *self
428 PREINIT:
429 5 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 5 0 5
EXTRACT(self); 0 5 5
EXTRACT(self); 0 5 5
430 CODE:
431 {
432 uint64_t X, m_ctr, capacity, ops, n_est;
433 uint32_t k;
434 double fp_rate;
435 /* Snapshot under the lock; do all (croak-capable) Perl allocation after
436 releasing it -- so an OOM in newHV/newSVuv can never strand the lock. */
437 5 ▶ if (!h->readonly) cbf_rwlock_rdlock(h); /* frozen: immutable, no lock */
Branch: 1 branch
BranchTrueFalseTotal
if (!h->readonly) cbf_rwlock_rdlock(h); /* frozen: immutable, no lock */ 4 1 5
438 5 X = cbf_nonzero_count_locked(h);
439 5 n_est = cbf_count_from_nonzero(h, X); /* reuse X -- no second scan */
440 5 m_ctr = h->hdr->m_ctr;
441 5 k = h->hdr->k;
442 5 capacity = h->hdr->capacity;
443 5 fp_rate = h->hdr->fp_rate;
444 5 ops = h->hdr->stat_ops;
445 5 ▶ if (!h->readonly) cbf_rwlock_rdunlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (!h->readonly) cbf_rwlock_rdunlock(h); 4 1 5
446  
447 5 HV *hv = newHV();
448 5 hv_stores(hv, "capacity", newSVuv((UV)capacity));
449 5 hv_stores(hv, "fp_rate", newSVnv(fp_rate));
450 5 hv_stores(hv, "counters", newSVuv((UV)m_ctr));
451 5 hv_stores(hv, "hashes", newSVuv(k));
452 5 hv_stores(hv, "counters_set", newSVuv((UV)X));
453 5 hv_stores(hv, "count", newSVuv((UV)n_est));
454 5 hv_stores(hv, "fill_ratio", newSVnv((double)X / (double)m_ctr));
455 5 hv_stores(hv, "ops", newSVuv((UV)ops));
456 5 hv_stores(hv, "mmap_size", newSVuv((UV)h->mmap_size));
457 5 hv_stores(hv, "frozen", newSVuv(__atomic_load_n(&h->hdr->sealed, __ATOMIC_ACQUIRE) ? 1 : 0));
458 5 hv_stores(hv, "readonly", newSVuv(h->readonly ? 1 : 0));
459 5 RETVAL = newRV_noinc((SV *)hv);
460 }
461 OUTPUT:
462 RETVAL
463  
464 SV *
465 path(self)
466 SV *self
467 PREINIT:
468 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
469 CODE:
470 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
471 OUTPUT:
472 RETVAL
473  
474 int
475 memfd(self)
476 SV *self
477 PREINIT:
478 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
479 CODE:
480 4 ▶ RETVAL = h->backing_fd;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->backing_fd; 0 4 4
481 OUTPUT:
482 RETVAL
483  
484 void
485 sync(self)
486 SV *self
487 PREINIT:
488 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
489 CODE:
490 3 ▶ if (!h->readonly && cbf_msync(h) != 0) croak("sync: %s", strerror(errno));
Branch: 2 branches
BranchTrueFalseTotal
if (!h->readonly && cbf_msync(h) != 0) croak("sync: %s", strerror(errno)); 2 1 3
if (!h->readonly && cbf_msync(h) != 0) croak("sync: %s", strerror(errno)); 0 2 2
491  
492 void
493 unlink(self, ...)
494 SV *self
495 CODE:
496 9 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::CountingBloomFilter::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::CountingBloomFilter::Shared")) { 4 2 6
if (sv_isobject(self) && sv_derived_from(self, "Data::CountingBloomFilter::Shared")) { 4 0 4
497 4 CbfHandle *h = INT2PTR(CbfHandle*, SvIV(SvRV(self)));
498 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
499 1 croak("Data::CountingBloomFilter::Shared->unlink(%s): %s", h->path, strerror(errno));
500 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
501 {
502 2 const char *up = SvPV_nolen(ST(1));
503 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
504 0 croak("Data::CountingBloomFilter::Shared->unlink(%s): %s", up, strerror(errno));
505 }
506 }
« cbf.h ↑ Summary