Shared.xs

filter: Statementstmt 97.3% 182 / 187 Branchbran 51.3% 196 / 382 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 66.4% 378 / 569 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« reservoir.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 "reservoir.h"
7  
8 #define EXTRACT(sv) \
9 if (!sv_isobject(sv) || !sv_derived_from(sv, "Data::Reservoir::Shared")) \
10 croak("Expected a Data::Reservoir::Shared object"); \
11 RsvHandle *h = INT2PTR(RsvHandle*, SvIV(SvRV(sv))); \
12 if (!h) croak("Attempted to use a destroyed Data::Reservoir::Shared object"); \
13 RsvHandle *h0 = h; PERL_UNUSED_VAR(h0); \
14 sv_2mortal(SvREFCNT_inc(SvRV(sv)))
15  
16 /* The pin above only blocks REFCOUNT-driven destruction. Perl run from argument
17 * magic can still call $obj->DESTROY explicitly, which frees the handle and
18 * zeroes the IV, leaving the local `h` dangling. Re-read it wherever such magic
19 * can intervene before `h` is used: SvGETMAGIC on an argument, av_len on a TIED
20 * array (AvFILL -> mg_size -> FETCHSIZE), and element fetches.
21 * The same Perl can also REPLACE the invocant ($obj = 42 mutates ST(0), because
22 * Perl passes aliases), hence the SvROK re-check before SvRV. */
23 #define REEXTRACT(sv) \
24 if (!SvROK(sv)) \
25 croak("Data::Reservoir::Shared object was replaced during the call"); \
26 h = INT2PTR(RsvHandle*, SvIV(SvRV(sv))); \
27 if (h != h0) croak("Data::Reservoir::Shared object replaced or destroyed during the call")
28  
29 #define MAKE_OBJ(class, handle) \
30 SV *obj = newSViv(PTR2IV(handle)); \
31 SV *ref = newRV_noinc(obj); \
32 sv_bless(ref, gv_stashpv(class, GV_ADD)); \
33 RETVAL = ref
34  
35 MODULE = Data::Reservoir::Shared PACKAGE = Data::Reservoir::Shared
36  
37 PROTOTYPES: DISABLE
38  
39 SV *
40 new(class, path = &PL_sv_undef, ...)
41 const char *class
42 SV *path
43 PREINIT:
44 char errbuf[RSV_ERR_BUFLEN];
45 CODE:
46 /* k / item_size read here (not as typemap params) so an explicit undef falls
47 * through to the default instead of warning "uninitialized value". */
48 3030 ▶ UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0;
Branch: 4 branches
BranchTrueFalseTotal
UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0; 3030 0 3030
UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0; 0 3030 3030
UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0; 0 0 0
UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0; 3030 0 3030
49 3030 ▶ UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256;
Branch: 4 branches
BranchTrueFalseTotal
UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256; 3029 1 3030
UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256; 0 3029 3029
UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256; 0 0 0
UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256; 3029 0 3029
50 3030 ▶ if (k < 1) croak("Data::Reservoir::Shared->new: reservoir size (k) must be >= 1");
Branch: 1 branch
BranchTrueFalseTotal
if (k < 1) croak("Data::Reservoir::Shared->new: reservoir size (k) must be >= 1"); 1 3029 3030
51 /* Optional 5th arg: file mode for a newly-created file-backed segment.
52 * Resolve its magic (tied/overloaded) BEFORE capturing path's PV: a FETCH
53 * here could realloc/free path's buffer, dangling p before rsv_create. */
54 3029 ▶ 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; 0 3029 3029
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; 0 0 0
mode_t mode = (items > 4 && (SvGETMAGIC(ST(4)), SvOK(ST(4)))) ? (mode_t)SvUV(ST(4)) : 0600; 0 0 0
55 3044 ▶ 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 3029 3029
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; 15 3014 3029
56 3029 RsvHandle *hh = rsv_create(p, (uint64_t)k, (uint64_t)item_size, RSV_MODE_UNIFORM, mode, errbuf);
57 3029 ▶ if (!hh) croak("Data::Reservoir::Shared->new: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!hh) croak("Data::Reservoir::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 6 3023 3029
if (!hh) croak("Data::Reservoir::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 6 0 6
58 3023 MAKE_OBJ(class, hh);
59 OUTPUT:
60 RETVAL
61  
62 SV *
63 new_memfd(class, name = &PL_sv_undef, ...)
64 const char *class
65 SV *name
66 PREINIT:
67 char errbuf[RSV_ERR_BUFLEN];
68 CODE:
69 /* k / item_size read here (not as typemap params) so an explicit undef falls
70 * through to the default instead of warning "uninitialized value". */
71 1 ▶ UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0;
Branch: 4 branches
BranchTrueFalseTotal
UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0; 1 0 1
UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0; 0 1 1
UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0; 0 0 0
UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0; 1 0 1
72 1 ▶ UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256;
Branch: 4 branches
BranchTrueFalseTotal
UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256; 1 0 1
UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256; 0 1 1
UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256; 0 0 0
UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256; 1 0 1
73 2 ▶ const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL; 0 1 1
const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL; 0 0 0
const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL; 1 0 1
74 1 ▶ if (k < 1) croak("Data::Reservoir::Shared->new_memfd: reservoir size (k) must be >= 1");
Branch: 1 branch
BranchTrueFalseTotal
if (k < 1) croak("Data::Reservoir::Shared->new_memfd: reservoir size (k) must be >= 1"); 0 1 1
75 1 RsvHandle *hh = rsv_create_memfd(nm, (uint64_t)k, (uint64_t)item_size, RSV_MODE_UNIFORM, errbuf);
76 1 ▶ if (!hh) croak("Data::Reservoir::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!hh) croak("Data::Reservoir::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 1 1
if (!hh) croak("Data::Reservoir::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 0 0
77 1 MAKE_OBJ(class, hh);
78 OUTPUT:
79 RETVAL
80  
81 SV *
82 new_weighted(class, path = &PL_sv_undef, ...)
83 const char *class
84 SV *path
85 PREINIT:
86 char errbuf[RSV_ERR_BUFLEN];
87 CODE:
88 /* k / item_size read here (not as typemap params) so an explicit undef falls
89 * through to the default instead of warning "uninitialized value". */
90 22 ▶ UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0;
Branch: 4 branches
BranchTrueFalseTotal
UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0; 22 0 22
UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0; 0 22 22
UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0; 0 0 0
UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0; 22 0 22
91 22 ▶ UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256;
Branch: 4 branches
BranchTrueFalseTotal
UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256; 22 0 22
UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256; 0 22 22
UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256; 0 0 0
UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256; 22 0 22
92 22 ▶ if (k < 1) croak("Data::Reservoir::Shared->new_weighted: reservoir size (k) must be >= 1");
Branch: 1 branch
BranchTrueFalseTotal
if (k < 1) croak("Data::Reservoir::Shared->new_weighted: reservoir size (k) must be >= 1"); 0 22 22
93 /* Optional 5th arg: file mode for a newly-created file-backed segment.
94 * Resolve its magic (tied/overloaded) BEFORE capturing path's PV: a FETCH
95 * here could realloc/free path's buffer, dangling p before rsv_create. */
96 22 ▶ 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; 0 22 22
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; 0 0 0
mode_t mode = (items > 4 && (SvGETMAGIC(ST(4)), SvOK(ST(4)))) ? (mode_t)SvUV(ST(4)) : 0600; 0 0 0
97 26 ▶ 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 22 22
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; 4 18 22
98 22 RsvHandle *hh = rsv_create(p, (uint64_t)k, (uint64_t)item_size, RSV_MODE_WEIGHTED, mode, errbuf);
99 22 ▶ if (!hh) croak("Data::Reservoir::Shared->new_weighted: %s", errbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (!hh) croak("Data::Reservoir::Shared->new_weighted: %s", errbuf); 1 21 22
100 21 MAKE_OBJ(class, hh);
101 OUTPUT:
102 RETVAL
103  
104 SV *
105 new_weighted_memfd(class, name = &PL_sv_undef, ...)
106 const char *class
107 SV *name
108 PREINIT:
109 char errbuf[RSV_ERR_BUFLEN];
110 CODE:
111 /* k / item_size read here (not as typemap params) so an explicit undef falls
112 * through to the default instead of warning "uninitialized value". */
113 1 ▶ UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0;
Branch: 4 branches
BranchTrueFalseTotal
UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0; 1 0 1
UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0; 0 1 1
UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0; 0 0 0
UV k = (items > 2 && (SvGETMAGIC(ST(2)), SvOK(ST(2)))) ? SvUV(ST(2)) : 0; 1 0 1
114 1 ▶ UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256;
Branch: 4 branches
BranchTrueFalseTotal
UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256; 1 0 1
UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256; 0 1 1
UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256; 0 0 0
UV item_size = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? SvUV(ST(3)) : 256; 1 0 1
115 2 ▶ const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL; 0 1 1
const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL; 0 0 0
const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL; 1 0 1
116 1 ▶ if (k < 1) croak("Data::Reservoir::Shared->new_weighted_memfd: reservoir size (k) must be >= 1");
Branch: 1 branch
BranchTrueFalseTotal
if (k < 1) croak("Data::Reservoir::Shared->new_weighted_memfd: reservoir size (k) must be >= 1"); 0 1 1
117 1 RsvHandle *hh = rsv_create_memfd(nm, (uint64_t)k, (uint64_t)item_size, RSV_MODE_WEIGHTED, errbuf);
118 1 ▶ if (!hh) croak("Data::Reservoir::Shared->new_weighted_memfd: %s", errbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (!hh) croak("Data::Reservoir::Shared->new_weighted_memfd: %s", errbuf); 0 1 1
119 1 MAKE_OBJ(class, hh);
120 OUTPUT:
121 RETVAL
122  
123 SV *
124 new_from_fd(class, fd)
125 const char *class
126 int fd
127 PREINIT:
128 char errbuf[RSV_ERR_BUFLEN];
129 CODE:
130 3 RsvHandle *hh = rsv_open_fd(fd, errbuf);
131 3 ▶ if (!hh) croak("Data::Reservoir::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!hh) croak("Data::Reservoir::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 1 2 3
if (!hh) croak("Data::Reservoir::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 1 0 1
132 2 MAKE_OBJ(class, hh);
133 OUTPUT:
134 RETVAL
135  
136 void
137 DESTROY(self)
138 SV *self
139 CODE:
140 3050 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::Reservoir::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::Reservoir::Shared")) { 3050 0 3050
if (sv_isobject(self) && sv_derived_from(self, "Data::Reservoir::Shared")) { 3050 0 3050
141 3050 RsvHandle *h = INT2PTR(RsvHandle*, SvIV(SvRV(self)));
142 3050 ▶ if (h) { sv_setiv(SvRV(self), 0); rsv_destroy(h); }
Branch: 1 branch
BranchTrueFalseTotal
if (h) { sv_setiv(SvRV(self), 0); rsv_destroy(h); } 3048 2 3050
143 }
144  
145 int
146 add(self, item, weight = &PL_sv_undef)
147 SV *self
148 SV *item
149 SV *weight
150 PREINIT:
151 257217 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 257217 0 257217
EXTRACT(self); 0 257217 257217
EXTRACT(self); 0 257217 257217
152 STRLEN n;
153 const char *s;
154 257217 ▶ double w = 0;
Branch: 1 branch
BranchTrueFalseTotal
double w = 0; 257217 0 257217
155 CODE:
156 257217 ▶ if (h->mode == RSV_MODE_WEIGHTED) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->mode == RSV_MODE_WEIGHTED) { 194569 62648 257217
157 /* Resolve + validate the weight BEFORE capturing the item PV. SvNV(weight)
158 * can run the weight's numeric overload/tie = arbitrary Perl that may
159 * realloc or free the item SV, dangling a PV captured earlier. Do all the
160 * weight magic first, THEN grab the item bytes so nothing runs between the
161 * SvPVbyte and its use. (Also keeps any croak out from under the lock.) */
162 194569 ▶ if (!(SvGETMAGIC(weight), SvOK(weight)))
Branch: 3 branches
BranchTrueFalseTotal
if (!(SvGETMAGIC(weight), SvOK(weight))) 0 194569 194569
if (!(SvGETMAGIC(weight), SvOK(weight))) 0 0 0
if (!(SvGETMAGIC(weight), SvOK(weight))) 1 194568 194569
163 1 croak("Data::Reservoir::Shared: weighted add(item, weight) requires a weight");
164 194568 w = SvNV(weight);
165 194568 ▶ if (!(w > 0.0) || !isfinite(w))
Branch: 2 branches
BranchTrueFalseTotal
if (!(w > 0.0) || !isfinite(w)) 194565 3 194568
if (!(w > 0.0) || !isfinite(w)) 1 194564 194565
166 4 croak("Data::Reservoir::Shared: weight must be a finite number > 0");
167 }
168 257211 s = SvPVbyte(item, n); /* may croak (wide char) -- BEFORE the lock, AFTER weight magic */
169 /* SvNV(weight) above and SvPVbyte(item) just now both run magic = arbitrary
170 * Perl that can have destroyed self -- re-check before the first use of h. */
171 257211 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 257211 257211
REEXTRACT(self); 0 257211 257211
172 257211 rsv_rwlock_wrlock(h);
173 514422 RETVAL = (h->mode == RSV_MODE_WEIGHTED)
174 194564 ? rsv_add_weighted_locked(h, s, (uint64_t)n, w)
175 257211 ▶ : rsv_add_locked(h, s, (uint64_t)n);
Branch: 1 branch
BranchTrueFalseTotal
: rsv_add_locked(h, s, (uint64_t)n); 194564 62647 257211
176 257211 h->hdr->stat_ops++;
177 257211 rsv_rwlock_wrunlock(h);
178 OUTPUT:
179 RETVAL
180  
181 UV
182 add_many(self, items)
183 SV *self
184 SV *items
185 PREINIT:
186 16008 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 16008 0 16008
EXTRACT(self); 0 16008 16008
EXTRACT(self); 0 16008 16008
187 AV *av;
188 IV top;
189 16008 ▶ UV stored = 0;
Branch: 1 branch
BranchTrueFalseTotal
UV stored = 0; 16008 0 16008
190 CODE:
191 16008 ▶ SvGETMAGIC(items);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(items); 0 16008 16008
SvGETMAGIC(items); 0 0 0
192 16008 ▶ if (!SvROK(items) || SvTYPE(SvRV(items)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(items) || SvTYPE(SvRV(items)) != SVt_PVAV) 16007 1 16008
if (!SvROK(items) || SvTYPE(SvRV(items)) != SVt_PVAV) 0 16007 16007
193 1 croak("Data::Reservoir::Shared->add_many: expected an array reference");
194 16007 av = (AV *)SvRV(items);
195 /* Pin the array for the whole loop: an element's magic can drop the caller's
196 * last reference (undef $aref), freeing av and dangling later av_fetch calls. */
197 16007 SvREFCNT_inc((SV *)av); sv_2mortal((SV *)av);
198 16007 top = av_len(av);
199 /* SvGETMAGIC(items) above, and av_len on a tied array, both run Perl that
200 * can have destroyed self -- re-check before the first use of h below. */
201 16007 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 16007 16007
REEXTRACT(self); 0 16007 16007
202 {
203 16007 ▶ STRLEN cnt = (top >= 0) ? (STRLEN)(top + 1) : 0, i;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN cnt = (top >= 0) ? (STRLEN)(top + 1) : 0, i; 16007 0 16007
204 16007 const char **ps = NULL; STRLEN *ls = NULL; double *ws = NULL;
205 16007 int weighted = (h->mode == RSV_MODE_WEIGHTED);
206 16007 ▶ if (cnt) {
Branch: 1 branch
BranchTrueFalseTotal
if (cnt) { 16007 0 16007
207 16007 ▶ Newx(ps, cnt, const char *); SAVEFREEPV(ps);
Branch: 1 branch
BranchTrueFalseTotal
Newx(ps, cnt, const char *); SAVEFREEPV(ps); 0 16007 16007
208 16007 ▶ Newx(ls, cnt, STRLEN); SAVEFREEPV(ls);
Branch: 1 branch
BranchTrueFalseTotal
Newx(ls, cnt, STRLEN); SAVEFREEPV(ls); 0 16007 16007
209 16007 ▶ if (weighted) { Newx(ws, cnt, double); SAVEFREEPV(ws); }
Branch: 2 branches
BranchTrueFalseTotal
if (weighted) { Newx(ws, cnt, double); SAVEFREEPV(ws); } 16005 2 16007
if (weighted) { Newx(ws, cnt, double); SAVEFREEPV(ws); } 0 16005 16005
210 49717 ▶ for (i = 0; i < cnt; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) { 33711 16006 49717
211 33711 SV **el = av_fetch(av, (SSize_t)i, 0);
212 33711 ▶ if (weighted) {
Branch: 1 branch
BranchTrueFalseTotal
if (weighted) { 33511 200 33711
213 /* weighted mode: each element is a [item, weight] pair,
214 * validated here (before the lock; no croak while locked) */
215 33511 ▶ if (el && *el) SvGETMAGIC(*el); /* a tied-array element is a deferred-magic PVLV */
Branch: 4 branches
BranchTrueFalseTotal
if (el && *el) SvGETMAGIC(*el); /* a tied-array element is a deferred-magic PVLV */ 33511 0 33511
if (el && *el) SvGETMAGIC(*el); /* a tied-array element is a deferred-magic PVLV */ 33511 0 33511
if (el && *el) SvGETMAGIC(*el); /* a tied-array element is a deferred-magic PVLV */ 0 33511 33511
if (el && *el) SvGETMAGIC(*el); /* a tied-array element is a deferred-magic PVLV */ 0 0 0
216 33511 ▶ if (!el || !*el || !SvROK(*el) || SvTYPE(SvRV(*el)) != SVt_PVAV)
Branch: 4 branches
BranchTrueFalseTotal
if (!el || !*el || !SvROK(*el) || SvTYPE(SvRV(*el)) != SVt_PVAV) 33511 0 33511
if (!el || !*el || !SvROK(*el) || SvTYPE(SvRV(*el)) != SVt_PVAV) 33511 0 33511
if (!el || !*el || !SvROK(*el) || SvTYPE(SvRV(*el)) != SVt_PVAV) 33510 1 33511
if (!el || !*el || !SvROK(*el) || SvTYPE(SvRV(*el)) != SVt_PVAV) 0 33510 33510
217 1 croak("Data::Reservoir::Shared->add_many: weighted reservoir expects [item, weight] pairs");
218 33510 AV *pair = (AV *)SvRV(*el);
219 33510 SV **iv = av_fetch(pair, 0, 0);
220 33510 SV **wv = av_fetch(pair, 1, 0);
221 33510 ▶ if (!iv || !*iv || !wv || !*wv)
Branch: 4 branches
BranchTrueFalseTotal
if (!iv || !*iv || !wv || !*wv) 33510 0 33510
if (!iv || !*iv || !wv || !*wv) 33510 0 33510
if (!iv || !*iv || !wv || !*wv) 33510 0 33510
if (!iv || !*iv || !wv || !*wv) 0 33510 33510
222 0 croak("Data::Reservoir::Shared->add_many: each element must be [item, weight]");
223 /* Pin both element SVs by value BEFORE any magic runs: the item's
224 * SvPVbyte (or the weight's get-magic) can free or reassign the
225 * pair AV, dangling the other's raw SV** slot. The mortal INC
226 * keeps both alive for this branch even if the pair AV is freed. */
227 33510 SV *isv = *iv, *wsv = *wv;
228 33510 SvREFCNT_inc(isv); sv_2mortal(isv);
229 33510 SvREFCNT_inc(wsv); sv_2mortal(wsv);
230 {
231 STRLEN len;
232 33510 const char *src = SvPVbyte(isv, len); /* may run overload/tie/get-magic = arbitrary Perl */
233 /* Copy bytes into a private mortal SV NOW: a LATER element SvPVbyte can
234 * grow/free THIS element PV, dangling src before the locked loop uses it. */
235 33510 SV *copy = sv_2mortal(newSVpvn(src, len));
236 33510 ps[i] = SvPVX_const(copy);
237 33510 ls[i] = len;
238 }
239 33510 ▶ SvGETMAGIC(wsv);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(wsv); 0 33510 33510
SvGETMAGIC(wsv); 0 0 0
240 33510 ▶ { double w = SvOK(wsv) ? SvNV(wsv) : 0.0; /* undef -> 0 -> clean croak below, no warning */
Branch: 1 branch
BranchTrueFalseTotal
{ double w = SvOK(wsv) ? SvNV(wsv) : 0.0; /* undef -> 0 -> clean croak below, no warning */ 33510 0 33510
241 33510 ▶ if (!(w > 0.0) || !isfinite(w))
Branch: 2 branches
BranchTrueFalseTotal
if (!(w > 0.0) || !isfinite(w)) 33510 0 33510
if (!(w > 0.0) || !isfinite(w)) 0 33510 33510
242 0 croak("Data::Reservoir::Shared->add_many: weight must be a finite number > 0");
243 33510 ws[i] = w; }
244 200 ▶ } else if (el && *el) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (el && *el) { 200 0 200
} else if (el && *el) { 200 0 200
245 STRLEN len;
246 200 const char *src = SvPVbyte(*el, len); /* may run overload/tie/get-magic = arbitrary Perl */
247 /* Copy bytes into a private mortal SV NOW: a LATER element SvPVbyte can
248 * grow/free THIS element PV, dangling src before the locked loop uses it. */
249 200 SV *copy = sv_2mortal(newSVpvn(src, len));
250 200 ps[i] = SvPVX_const(copy);
251 200 ls[i] = len;
252 0 } else { ps[i] = ""; ls[i] = 0; }
253 }
254 }
255 /* Element get-magic in the resolve loop above (SvPVbyte / SvGETMAGIC on
256 * the weight) can also have destroyed self. Outside the `if (cnt)`
257 * block: an empty or tied size-0 array skips the loop but still
258 * reaches this lock. */
259 16006 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 16006 16006
REEXTRACT(self); 0 16006 16006
260 16006 rsv_rwlock_wrlock(h);
261 16006 ▶ if (weighted)
Branch: 1 branch
BranchTrueFalseTotal
if (weighted) 16004 2 16006
262 49514 ▶ for (i = 0; i < cnt; i++) stored += (UV)rsv_add_weighted_locked(h, ps[i], (uint64_t)ls[i], ws[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) stored += (UV)rsv_add_weighted_locked(h, ps[i], (uint64_t)ls[i], ws[i]); 33510 16004 49514
263 else
264 202 ▶ for (i = 0; i < cnt; i++) stored += (UV)rsv_add_locked(h, ps[i], (uint64_t)ls[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) stored += (UV)rsv_add_locked(h, ps[i], (uint64_t)ls[i]); 200 2 202
265 16006 h->hdr->stat_ops++;
266 16006 rsv_rwlock_wrunlock(h);
267 }
268 16006 ▶ RETVAL = stored;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = stored; 16001 5 16006
269 OUTPUT:
270 RETVAL
271  
272 # ---- reading the sample ----
273  
274 SV *
275 get(self, i)
276 SV *self
277 UV i
278 PREINIT:
279 11 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 11 0 11
EXTRACT(self); 0 11 11
EXTRACT(self); 0 11 11
280 const uint8_t *ptr; uint64_t len; int ok;
281 11 uint8_t *tmp = NULL;
282 CODE:
283 /* copy the item out under the read lock, then build the SV (no croak-capable
284 * Perl allocation while the lock is held) */
285 11 Newx(tmp, (size_t)h->item_size, uint8_t); SAVEFREEPV(tmp); /* cached: matches rsv_get_locked's clamp */
286 11 rsv_rwlock_rdlock(h);
287 11 ok = rsv_get_locked(h, (uint64_t)i, &ptr, &len);
288 11 ▶ if (ok) memcpy(tmp, ptr, (size_t)len);
Branch: 1 branch
BranchTrueFalseTotal
if (ok) memcpy(tmp, ptr, (size_t)len); 10 1 11
289 11 rsv_rwlock_rdunlock(h);
290 11 ▶ RETVAL = ok ? newSVpvn((char *)tmp, (STRLEN)len) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ok ? newSVpvn((char *)tmp, (STRLEN)len) : &PL_sv_undef; 10 1 11
291 OUTPUT:
292 RETVAL
293  
294 void
295 sample(self)
296 SV *self
297 PREINIT:
298 54021 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 54021 0 54021
EXTRACT(self); 0 54021 54021
EXTRACT(self); 0 54021 54021
299 PPCODE:
300 {
301 54021 uint64_t k = h->k, isz = h->stride, cnt, i; /* cached geometry: alloc + copy agree */
302 uint8_t *buf;
303 /* worst-case snapshot buffer (k slots) allocated BEFORE the lock */
304 54021 Newx(buf, (size_t)(k * isz), uint8_t); SAVEFREEPV(buf);
305 54021 rsv_rwlock_rdlock(h);
306 54021 cnt = rsv_count_locked(h);
307 {
308 54021 uint64_t smax = rsv_slots_max(h);
309 54021 ▶ if (cnt > smax) cnt = smax;
Branch: 1 branch
BranchTrueFalseTotal
if (cnt > smax) cnt = smax; 0 54021 54021
310 54021 memcpy(buf, rsv_slots(h), (size_t)(cnt * isz)); /* snapshot filled slots */
311 }
312 54021 rsv_rwlock_rdunlock(h);
313 54021 ▶ EXTEND(SP, (SSize_t)cnt);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, (SSize_t)cnt); 54021 0 54021
EXTEND(SP, (SSize_t)cnt); 0 54021 54021
314 126522 ▶ for (i = 0; i < cnt; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) { 72501 54021 126522
315 72501 uint8_t *slot = buf + i * isz;
316 72501 uint64_t len; memcpy(&len, slot, sizeof(uint64_t));
317 72501 ▶ if (len > h->item_size) len = h->item_size; /* cached clamp */
Branch: 1 branch
BranchTrueFalseTotal
if (len > h->item_size) len = h->item_size; /* cached clamp */ 0 72501 72501
318 72501 PUSHs(sv_2mortal(newSVpvn((char *)(slot + 8), (STRLEN)len)));
319 }
320 }
321  
322 UV
323 count(self)
324 SV *self
325 PREINIT:
326 21 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 21 0 21
EXTRACT(self); 0 21 21
EXTRACT(self); 1 20 21
327 UV c;
328 CODE:
329 20 rsv_rwlock_rdlock(h);
330 20 c = (UV)rsv_count_locked(h);
331 20 rsv_rwlock_rdunlock(h);
332 20 ▶ RETVAL = c;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = c; 4 16 20
333 OUTPUT:
334 RETVAL
335  
336 UV
337 seen(self)
338 SV *self
339 PREINIT:
340 21 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 21 0 21
EXTRACT(self); 0 21 21
EXTRACT(self); 0 21 21
341 UV s;
342 CODE:
343 21 rsv_rwlock_rdlock(h);
344 21 s = (UV)h->hdr->seen;
345 21 rsv_rwlock_rdunlock(h);
346 21 ▶ RETVAL = s;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = s; 5 16 21
347 OUTPUT:
348 RETVAL
349  
350 # ---- mutation / introspection ----
351  
352 void
353 clear(self)
354 SV *self
355 PREINIT:
356 51002 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 51002 0 51002
EXTRACT(self); 0 51002 51002
EXTRACT(self); 0 51002 51002
357 CODE:
358 51002 rsv_rwlock_wrlock(h);
359 51002 rsv_clear_locked(h);
360 51002 h->hdr->stat_ops++;
361 51002 rsv_rwlock_wrunlock(h);
362  
363 void
364 seed(self, s)
365 SV *self
366 UV s
367 PREINIT:
368 3018 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3018 0 3018
EXTRACT(self); 0 3018 3018
EXTRACT(self); 0 3018 3018
369 CODE:
370 /* set the RNG state (nonzero) -- for reproducible sampling in tests */
371 3018 rsv_rwlock_wrlock(h);
372 3018 ▶ h->hdr->rng_state = (uint64_t)s ? (uint64_t)s : 0x9E3779B97F4A7C15ULL;
Branch: 1 branch
BranchTrueFalseTotal
h->hdr->rng_state = (uint64_t)s ? (uint64_t)s : 0x9E3779B97F4A7C15ULL; 3018 0 3018
373 3018 rsv_rwlock_wrunlock(h);
374  
375 UV
376 capacity(self)
377 SV *self
378 PREINIT:
379 6 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 6 0 6
EXTRACT(self); 0 6 6
EXTRACT(self); 0 6 6
380 CODE:
381 6 ▶ RETVAL = (UV)h->k; /* cached: capacity == k, kept off the peer-writable header */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->k; /* cached: capacity == k, kept off the peer-writable header */ 0 6 6
382 OUTPUT:
383 RETVAL
384  
385 UV
386 item_size(self)
387 SV *self
388 PREINIT:
389 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
390 CODE:
391 3 ▶ RETVAL = (UV)h->item_size;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->item_size; 0 3 3
392 OUTPUT:
393 RETVAL
394  
395 int
396 is_weighted(self)
397 SV *self
398 PREINIT:
399 5 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 5 0 5
EXTRACT(self); 0 5 5
EXTRACT(self); 0 5 5
400 CODE:
401 5 ▶ RETVAL = (h->mode == RSV_MODE_WEIGHTED) ? 1 : 0; /* cached mode, no lock */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (h->mode == RSV_MODE_WEIGHTED) ? 1 : 0; /* cached mode, no lock */ 0 5 5
402 OUTPUT:
403 RETVAL
404  
405 SV *
406 stats(self)
407 SV *self
408 PREINIT:
409 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
410 CODE:
411 {
412 uint64_t k, isz, seen, cnt, ops, mmap_size;
413 3 k = h->k; /* cached geometry (no lock needed) */
414 3 isz = h->item_size;
415 3 rsv_rwlock_rdlock(h);
416 3 seen = h->hdr->seen;
417 3 cnt = rsv_count_locked(h);
418 3 ops = h->hdr->stat_ops;
419 3 rsv_rwlock_rdunlock(h);
420 3 mmap_size = (uint64_t)h->mmap_size;
421  
422 3 HV *hv = newHV();
423 3 hv_stores(hv, "size", newSVuv((UV)k));
424 3 hv_stores(hv, "item_size", newSVuv((UV)isz));
425 3 hv_stores(hv, "count", newSVuv((UV)cnt));
426 3 hv_stores(hv, "seen", newSVuv((UV)seen));
427 3 hv_stores(hv, "ops", newSVuv((UV)ops));
428 3 hv_stores(hv, "mmap_size", newSVuv((UV)mmap_size));
429 3 hv_stores(hv, "weighted", newSViv(h->mode == RSV_MODE_WEIGHTED ? 1 : 0));
430 3 RETVAL = newRV_noinc((SV *)hv);
431 }
432 OUTPUT:
433 RETVAL
434  
435 SV *
436 path(self)
437 SV *self
438 PREINIT:
439 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
440 CODE:
441 3 ▶ RETVAL = h->path ? newSVpv(h->path, 0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->path ? newSVpv(h->path, 0) : &PL_sv_undef; 3 0 3
442 OUTPUT:
443 RETVAL
444  
445 int
446 memfd(self)
447 SV *self
448 PREINIT:
449 2 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 2 0 2
EXTRACT(self); 0 2 2
EXTRACT(self); 0 2 2
450 CODE:
451 2 ▶ RETVAL = h->backing_fd;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->backing_fd; 0 2 2
452 OUTPUT:
453 RETVAL
454  
455 void
456 sync(self)
457 SV *self
458 PREINIT:
459 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
460 CODE:
461 3 ▶ if (rsv_msync(h) != 0) croak("sync: %s", strerror(errno));
Branch: 1 branch
BranchTrueFalseTotal
if (rsv_msync(h) != 0) croak("sync: %s", strerror(errno)); 0 3 3
462  
463 void
464 unlink(self, ...)
465 SV *self
466 CODE:
467 9 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::Reservoir::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::Reservoir::Shared")) { 4 1 5
if (sv_isobject(self) && sv_derived_from(self, "Data::Reservoir::Shared")) { 4 0 4
468 4 RsvHandle *h = INT2PTR(RsvHandle*, SvIV(SvRV(self)));
469 4 ▶ if (h && h->path) {
Branch: 2 branches
BranchTrueFalseTotal
if (h && h->path) { 4 0 4
if (h && h->path) { 4 0 4
470 4 ▶ const char *up = h->unlink_path ? h->unlink_path : h->path;
Branch: 1 branch
BranchTrueFalseTotal
const char *up = h->unlink_path ? h->unlink_path : h->path; 4 0 4
471 4 ▶ if (unlink(up) != 0 && errno != ENOENT)
Branch: 2 branches
BranchTrueFalseTotal
if (unlink(up) != 0 && errno != ENOENT) 2 2 4
if (unlink(up) != 0 && errno != ENOENT) 0 2 2
472 0 croak("Data::Reservoir::Shared->unlink(%s): %s", h->path, strerror(errno));
473 }
474 1 ▶ } else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) {
Branch: 4 branches
BranchTrueFalseTotal
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 1 0 1
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 0 1 1
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 0 0 0
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 1 0 1
475 {
476 1 const char *up = SvPV_nolen(ST(1));
477 1 ▶ if (unlink(up) != 0 && errno != ENOENT)
Branch: 2 branches
BranchTrueFalseTotal
if (unlink(up) != 0 && errno != ENOENT) 0 1 1
if (unlink(up) != 0 && errno != ENOENT) 0 0 0
478 0 croak("Data::Reservoir::Shared->unlink(%s): %s", up, strerror(errno));
479 }
480 }
« reservoir.h ↑ Summary