Shared.xs

filter: Statementstmt 96.2% 178 / 185 Branchbran 56.0% 195 / 348 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 69.9% 373 / 533 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« fenwick.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 "fenwick.h"
7  
8 #define EXTRACT(sv) \
9 if (!sv_isobject(sv) || !sv_derived_from(sv, "Data::Fenwick::Shared")) \
10 croak("Expected a Data::Fenwick::Shared object"); \
11 FenHandle *h = INT2PTR(FenHandle*, SvIV(SvRV(sv))); \
12 if (!h) croak("Attempted to use a destroyed Data::Fenwick::Shared object"); \
13 FenHandle *h0 = h; PERL_UNUSED_VAR(h0); \
14 sv_2mortal(SvREFCNT_inc(SvRV(sv)))
15  
16  
17 /* Re-read the handle after a call that can run Perl code. EXTRACT's
18 * sv_2mortal(SvREFCNT_inc(...)) pin only blocks REFCOUNT-driven destruction;
19 * an explicit $obj->DESTROY frees the handle regardless and zeroes the IV.
20 * sv_isobject/sv_derived_from both BEGIN with SvGETMAGIC, so a tied argument
21 * runs Perl there. The same Perl can also REPLACE the invocant ($obj = 42
22 * mutates ST(0), because Perl passes aliases), hence the SvROK re-check. */
23 #define REEXTRACT(sv) \
24 if (!SvROK(sv)) \
25 croak("Data::Fenwick::Shared object was replaced during the call"); \
26 h = INT2PTR(FenHandle*, SvIV(SvRV(sv))); \
27 if (h != h0) croak("Data::Fenwick::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 /* 1-based position bounds check (croaks with a clear message) */
36 #define CHECK_POS(i) \
37 do { if ((i) < 1 || (i) > h->hdr->n) \
38 croak("Data::Fenwick::Shared: position %" UVuf " out of range 1..%" UVuf, \
39 (UV)(i), (UV)h->hdr->n); } while (0)
40  
41 MODULE = Data::Fenwick::Shared PACKAGE = Data::Fenwick::Shared
42  
43 PROTOTYPES: DISABLE
44  
45 SV *
46 new(class, path = &PL_sv_undef, n = 0, ...)
47 const char *class
48 SV *path
49 UV n
50 PREINIT:
51 char errbuf[FEN_ERR_BUFLEN];
52 CODE:
53 35 ▶ if (n < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 1) 1 34 35
54 1 croak("Data::Fenwick::Shared->new: n (number of positions) must be >= 1");
55 /* Optional 4th arg: file mode for a newly-created file-backed segment
56 * (default 0600, owner-only). Pass e.g. 0660 to opt into cross-user
57 * sharing. Ignored for anonymous segments and existing files. */
58 34 ▶ 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 34 34
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
59 /* Capture the path PV last: the get-magic on ST(3) above may run code
60 * that reallocs/frees the path SV's buffer, dangling an earlier pointer. */
61 50 ▶ 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 34 34
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; 16 18 34
62 34 FenHandle *hh = fen_create(p, (uint64_t)n, FEN_MODE_POINT, mode, errbuf);
63 34 ▶ if (!hh) croak("Data::Fenwick::Shared->new: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!hh) croak("Data::Fenwick::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 5 29 34
if (!hh) croak("Data::Fenwick::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 5 0 5
64 29 MAKE_OBJ(class, hh);
65 OUTPUT:
66 RETVAL
67  
68 SV *
69 new_memfd(class, name = &PL_sv_undef, n = 0)
70 const char *class
71 SV *name
72 UV n
73 PREINIT:
74 char errbuf[FEN_ERR_BUFLEN];
75 CODE:
76 4 ▶ 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 2 2
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 0 2
77 2 ▶ if (n < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 1) 0 2 2
78 0 croak("Data::Fenwick::Shared->new_memfd: n must be >= 1");
79 2 FenHandle *hh = fen_create_memfd(nm, (uint64_t)n, FEN_MODE_POINT, errbuf);
80 2 ▶ if (!hh) croak("Data::Fenwick::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!hh) croak("Data::Fenwick::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 2 2
if (!hh) croak("Data::Fenwick::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 0 0
81 2 MAKE_OBJ(class, hh);
82 OUTPUT:
83 RETVAL
84  
85 SV *
86 new_range(class, path = &PL_sv_undef, n = 0, ...)
87 const char *class
88 SV *path
89 UV n
90 PREINIT:
91 char errbuf[FEN_ERR_BUFLEN];
92 CODE:
93 12 ▶ if (n < 1) croak("Data::Fenwick::Shared->new_range: n must be >= 1");
Branch: 1 branch
BranchTrueFalseTotal
if (n < 1) croak("Data::Fenwick::Shared->new_range: n must be >= 1"); 0 12 12
94 12 ▶ 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 12 12
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
95 /* Capture the path PV last: the get-magic on ST(3) above may run code
96 * that reallocs/frees the path SV's buffer, dangling an earlier pointer. */
97 17 ▶ 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 12 12
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 7 12
98 12 FenHandle *hh = fen_create(p, (uint64_t)n, FEN_MODE_RANGE, mode, errbuf);
99 12 ▶ if (!hh) croak("Data::Fenwick::Shared->new_range: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!hh) croak("Data::Fenwick::Shared->new_range: %s", errbuf[0] ? errbuf : "out of memory"); 0 12 12
if (!hh) croak("Data::Fenwick::Shared->new_range: %s", errbuf[0] ? errbuf : "out of memory"); 0 0 0
100 12 MAKE_OBJ(class, hh);
101 OUTPUT:
102 RETVAL
103  
104 SV *
105 new_range_memfd(class, name = &PL_sv_undef, n = 0)
106 const char *class
107 SV *name
108 UV n
109 PREINIT:
110 char errbuf[FEN_ERR_BUFLEN];
111 CODE:
112 4 ▶ 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 2 2
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; 2 0 2
113 2 ▶ if (n < 1) croak("Data::Fenwick::Shared->new_range_memfd: n must be >= 1");
Branch: 1 branch
BranchTrueFalseTotal
if (n < 1) croak("Data::Fenwick::Shared->new_range_memfd: n must be >= 1"); 0 2 2
114 2 FenHandle *hh = fen_create_memfd(nm, (uint64_t)n, FEN_MODE_RANGE, errbuf);
115 2 ▶ if (!hh) croak("Data::Fenwick::Shared->new_range_memfd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!hh) croak("Data::Fenwick::Shared->new_range_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 2 2
if (!hh) croak("Data::Fenwick::Shared->new_range_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 0 0
116 2 MAKE_OBJ(class, hh);
117 OUTPUT:
118 RETVAL
119  
120 SV *
121 new_from_fd(class, fd)
122 const char *class
123 int fd
124 PREINIT:
125 char errbuf[FEN_ERR_BUFLEN];
126 CODE:
127 3 FenHandle *hh = fen_open_fd(fd, errbuf);
128 3 ▶ if (!hh) croak("Data::Fenwick::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!hh) croak("Data::Fenwick::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 1 2 3
if (!hh) croak("Data::Fenwick::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 1 0 1
129 2 MAKE_OBJ(class, hh);
130 OUTPUT:
131 RETVAL
132  
133 SV *
134 new_readonly(class, path)
135 const char *class
136 SV *path
137 PREINIT:
138 char errbuf[FEN_ERR_BUFLEN];
139 CODE:
140 /* Open a FROZEN (sealed) file read-only: O_RDONLY + PROT_READ, no lock
141 ever. A sealed file is immutable and no read path writes the mapping,
142 so queries take no reader-slot / rwlock traffic and any number of
143 processes can share one PROT_READ mapping (same architecture; the
144 magic rejects a wrong-endian file). */
145 19 ▶ 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 10 10
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; 9 1 10
146 10 ▶ if (!p) croak("Data::Fenwick::Shared->new_readonly: path is required");
Branch: 1 branch
BranchTrueFalseTotal
if (!p) croak("Data::Fenwick::Shared->new_readonly: path is required"); 1 9 10
147 9 FenHandle *hh = fen_open_readonly(p, errbuf);
148 9 ▶ if (!hh) croak("Data::Fenwick::Shared->new_readonly: %s", errbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (!hh) croak("Data::Fenwick::Shared->new_readonly: %s", errbuf); 3 6 9
149 6 class = SvPV_nolen(ST(0)); /* re-read: path's get-magic above could have run Perl code */
150 6 MAKE_OBJ(class, hh);
151 OUTPUT:
152 RETVAL
153  
154 void
155 DESTROY(self)
156 SV *self
157 CODE:
158 55 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::Fenwick::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::Fenwick::Shared")) { 55 0 55
if (sv_isobject(self) && sv_derived_from(self, "Data::Fenwick::Shared")) { 55 0 55
159 55 FenHandle *h = INT2PTR(FenHandle*, SvIV(SvRV(self)));
160 55 ▶ if (h) { sv_setiv(SvRV(self), 0); fen_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); fen_destroy(h); } /* null first: activates EXTRACT's use-after-destroy croak + makes a double DESTROY a no-op */ 53 2 55
161 }
162  
163 # ---- mutation ----
164  
165 void
166 update(self, i, delta)
167 SV *self
168 UV i
169 IV delta
170 PREINIT:
171 1262 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 1262 0 1262
EXTRACT(self); 0 1262 1262
EXTRACT(self); 0 1262 1262
172 CODE:
173 1262 ▶ if (h->readonly) croak("Data::Fenwick::Shared->update: structure is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::Fenwick::Shared->update: structure is frozen (read-only)"); 3 1259 1262
174 1259 ▶ CHECK_POS(i);
Branch: 2 branches
BranchTrueFalseTotal
CHECK_POS(i); 1258 1 1259
CHECK_POS(i); 1 1257 1258
175 1257 fen_rwlock_wrlock(h);
176 1257 ▶ if (h->hdr->sealed) { fen_rwlock_wrunlock(h); croak("Data::Fenwick::Shared->update: structure is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { fen_rwlock_wrunlock(h); croak("Data::Fenwick::Shared->update: structure is frozen (read-only)"); } 1 1256 1257
177 1256 fen_add1_locked(h, (uint64_t)i, (int64_t)delta); /* point add (range mode: range_add(i,i)) */
178 1256 h->hdr->stat_ops++;
179 1256 fen_rwlock_wrunlock(h);
180  
181 void
182 range_add(self, l, r, delta)
183 SV *self
184 UV l
185 UV r
186 IV delta
187 PREINIT:
188 187 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 187 0 187
EXTRACT(self); 0 187 187
EXTRACT(self); 0 187 187
189 CODE:
190 187 ▶ if (h->readonly) croak("Data::Fenwick::Shared->range_add: structure is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::Fenwick::Shared->range_add: structure is frozen (read-only)"); 3 184 187
191 184 ▶ if (h->mode != FEN_MODE_RANGE)
Branch: 1 branch
BranchTrueFalseTotal
if (h->mode != FEN_MODE_RANGE) 1 183 184
192 1 croak("Data::Fenwick::Shared->range_add: requires a range-mode tree (new_range); a point tree only supports update()");
193 183 ▶ if (l < 1 || r > h->hdr->n || l > r)
Branch: 3 branches
BranchTrueFalseTotal
if (l < 1 || r > h->hdr->n || l > r) 183 0 183
if (l < 1 || r > h->hdr->n || l > r) 183 0 183
if (l < 1 || r > h->hdr->n || l > r) 0 183 183
194 0 croak("Data::Fenwick::Shared->range_add: bad range %" UVuf "..%" UVuf " (valid 1..%" UVuf ")", (UV)l, (UV)r, (UV)h->hdr->n);
195 183 fen_rwlock_wrlock(h);
196 183 ▶ if (h->hdr->sealed) { fen_rwlock_wrunlock(h); croak("Data::Fenwick::Shared->range_add: structure is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { fen_rwlock_wrunlock(h); croak("Data::Fenwick::Shared->range_add: structure is frozen (read-only)"); } 0 183 183
197 183 fen_range_add_locked(h, (uint64_t)l, (uint64_t)r, (int64_t)delta);
198 183 h->hdr->stat_ops++;
199 183 fen_rwlock_wrunlock(h);
200  
201 IV
202 set(self, i, value)
203 SV *self
204 UV i
205 IV value
206 PREINIT:
207 164 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 164 0 164
EXTRACT(self); 0 164 164
EXTRACT(self); 0 164 164
208 int64_t cur;
209 CODE:
210 164 ▶ if (h->readonly) croak("Data::Fenwick::Shared->set: structure is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::Fenwick::Shared->set: structure is frozen (read-only)"); 2 162 164
211 162 ▶ CHECK_POS(i);
Branch: 2 branches
BranchTrueFalseTotal
CHECK_POS(i); 162 0 162
CHECK_POS(i); 0 162 162
212 162 fen_rwlock_wrlock(h);
213 162 ▶ if (h->hdr->sealed) { fen_rwlock_wrunlock(h); croak("Data::Fenwick::Shared->set: structure is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { fen_rwlock_wrunlock(h); croak("Data::Fenwick::Shared->set: structure is frozen (read-only)"); } 0 162 162
214 162 cur = fen_rng_locked(h, (uint64_t)i, (uint64_t)i); /* current value at i */
215 162 fen_add1_locked(h, (uint64_t)i, (int64_t)((uint64_t)value - (uint64_t)cur)); /* set to the absolute value */
216 162 h->hdr->stat_ops++;
217 162 fen_rwlock_wrunlock(h);
218 162 ▶ RETVAL = (IV)cur; /* return the previous value */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (IV)cur; /* return the previous value */ 150 12 162
219 OUTPUT:
220 RETVAL
221  
222 void
223 clear(self)
224 SV *self
225 PREINIT:
226 5 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 5 0 5
EXTRACT(self); 0 5 5
EXTRACT(self); 0 5 5
227 CODE:
228 5 ▶ if (h->readonly) croak("Data::Fenwick::Shared->clear: structure is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::Fenwick::Shared->clear: structure is frozen (read-only)"); 2 3 5
229 3 fen_rwlock_wrlock(h);
230 3 ▶ if (h->hdr->sealed) { fen_rwlock_wrunlock(h); croak("Data::Fenwick::Shared->clear: structure is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { fen_rwlock_wrunlock(h); croak("Data::Fenwick::Shared->clear: structure is frozen (read-only)"); } 0 3 3
231 3 fen_clear_locked(h);
232 3 h->hdr->stat_ops++;
233 3 fen_rwlock_wrunlock(h);
234  
235 void
236 freeze(self)
237 SV *self
238 PREINIT:
239 5 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 5 0 5
EXTRACT(self); 0 5 5
EXTRACT(self); 0 5 5
240 CODE:
241 5 ▶ if (h->readonly) croak("Data::Fenwick::Shared->freeze: cannot freeze a read-only handle");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::Fenwick::Shared->freeze: cannot freeze a read-only handle"); 2 3 5
242 3 ▶ if (fen_freeze(h) != 0) croak("Data::Fenwick::Shared->freeze: msync: %s", strerror(errno));
Branch: 1 branch
BranchTrueFalseTotal
if (fen_freeze(h) != 0) croak("Data::Fenwick::Shared->freeze: msync: %s", strerror(errno)); 0 3 3
243 3 h->readonly = 1; /* this handle now rejects mutation too (the file is sealed) */
244  
245 UV
246 frozen(self)
247 SV *self
248 PREINIT:
249 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
250 CODE:
251 4 ▶ RETVAL = __atomic_load_n(&h->hdr->sealed, __ATOMIC_RELAXED) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = __atomic_load_n(&h->hdr->sealed, __ATOMIC_RELAXED) ? 1 : 0; 0 4 4
252 OUTPUT:
253 RETVAL
254  
255 UV
256 readonly(self)
257 SV *self
258 PREINIT:
259 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
260 CODE:
261 4 ▶ RETVAL = h->readonly ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->readonly ? 1 : 0; 0 4 4
262 OUTPUT:
263 RETVAL
264  
265 # ---- query ----
266  
267 IV
268 prefix(self, i)
269 SV *self
270 UV i
271 PREINIT:
272 728 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 728 0 728
EXTRACT(self); 0 728 728
EXTRACT(self); 0 728 728
273 int64_t s;
274 CODE:
275 728 ▶ if (i > h->hdr->n)
Branch: 1 branch
BranchTrueFalseTotal
if (i > h->hdr->n) 1 727 728
276 1 croak("Data::Fenwick::Shared->prefix: position %" UVuf " out of range 0..%" UVuf, (UV)i, (UV)h->hdr->n);
277 727 ▶ if (h->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { 4 723 727
278 4 s = fen_pref_locked(h, (uint64_t)i);
279 } else {
280 723 fen_rwlock_rdlock(h);
281 723 s = fen_pref_locked(h, (uint64_t)i);
282 723 fen_rwlock_rdunlock(h);
283 }
284 727 ▶ RETVAL = (IV)s;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (IV)s; 703 24 727
285 OUTPUT:
286 RETVAL
287  
288 IV
289 range(self, l, r)
290 SV *self
291 UV l
292 UV r
293 PREINIT:
294 615 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 615 0 615
EXTRACT(self); 0 615 615
EXTRACT(self); 0 615 615
295 int64_t s;
296 CODE:
297 615 ▶ if (l < 1 || r > h->hdr->n || l > r)
Branch: 3 branches
BranchTrueFalseTotal
if (l < 1 || r > h->hdr->n || l > r) 614 1 615
if (l < 1 || r > h->hdr->n || l > r) 613 1 614
if (l < 1 || r > h->hdr->n || l > r) 1 612 613
298 3 croak("Data::Fenwick::Shared->range: bad range %" UVuf "..%" UVuf " (valid 1..%" UVuf ")", (UV)l, (UV)r, (UV)h->hdr->n);
299 612 ▶ if (h->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { 2 610 612
300 2 s = fen_rng_locked(h, (uint64_t)l, (uint64_t)r);
301 } else {
302 610 fen_rwlock_rdlock(h);
303 610 s = fen_rng_locked(h, (uint64_t)l, (uint64_t)r);
304 610 fen_rwlock_rdunlock(h);
305 }
306 612 ▶ RETVAL = (IV)s;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (IV)s; 598 14 612
307 OUTPUT:
308 RETVAL
309  
310 IV
311 point(self, i)
312 SV *self
313 UV i
314 PREINIT:
315 538 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 538 0 538
EXTRACT(self); 0 538 538
EXTRACT(self); 0 538 538
316 int64_t s;
317 CODE:
318 538 ▶ CHECK_POS(i);
Branch: 2 branches
BranchTrueFalseTotal
CHECK_POS(i); 537 1 538
CHECK_POS(i); 0 537 537
319 537 ▶ if (h->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { 3 534 537
320 3 s = fen_rng_locked(h, (uint64_t)i, (uint64_t)i);
321 } else {
322 534 fen_rwlock_rdlock(h);
323 534 s = fen_rng_locked(h, (uint64_t)i, (uint64_t)i);
324 534 fen_rwlock_rdunlock(h);
325 }
326 537 ▶ RETVAL = (IV)s;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (IV)s; 504 33 537
327 OUTPUT:
328 RETVAL
329  
330 IV
331 total(self)
332 SV *self
333 PREINIT:
334 23 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 23 0 23
EXTRACT(self); 0 23 23
EXTRACT(self); 1 22 23
335 int64_t s;
336 CODE:
337 22 ▶ if (h->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { 4 18 22
338 4 s = fen_pref_locked(h, h->hdr->n);
339 } else {
340 18 fen_rwlock_rdlock(h);
341 18 s = fen_pref_locked(h, h->hdr->n);
342 18 fen_rwlock_rdunlock(h);
343 }
344 22 ▶ RETVAL = (IV)s;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (IV)s; 0 22 22
345 OUTPUT:
346 RETVAL
347  
348 UV
349 find(self, target)
350 SV *self
351 IV target
352 PREINIT:
353 11 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 11 0 11
EXTRACT(self); 0 11 11
EXTRACT(self); 0 11 11
354 uint64_t pos;
355 CODE:
356 /* smallest position whose prefix sum >= target; n+1 if none. Meaningful when
357 * all stored values are non-negative (rank / weighted lookup). */
358 11 ▶ if (h->mode == FEN_MODE_RANGE)
Branch: 1 branch
BranchTrueFalseTotal
if (h->mode == FEN_MODE_RANGE) 2 9 11
359 2 croak("Data::Fenwick::Shared->find: not supported on a range-mode tree (no single-BIT binary lift); use a point tree");
360 9 ▶ if (h->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { 1 8 9
361 1 pos = fen_lower_bound_locked(h, (int64_t)target);
362 } else {
363 8 fen_rwlock_rdlock(h);
364 8 pos = fen_lower_bound_locked(h, (int64_t)target);
365 8 fen_rwlock_rdunlock(h);
366 }
367 9 ▶ RETVAL = (UV)pos;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)pos; 0 9 9
368 OUTPUT:
369 RETVAL
370  
371 void
372 merge(self, other)
373 SV *self
374 SV *other
375 PREINIT:
376 7 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 7 0 7
EXTRACT(self); 0 7 7
EXTRACT(self); 0 7 7
377 CODE:
378 7 ▶ if (h->readonly) croak("Data::Fenwick::Shared->merge: structure is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::Fenwick::Shared->merge: structure is frozen (read-only)"); 2 5 7
379 5 ▶ if (!sv_isobject(other) || !sv_derived_from(other, "Data::Fenwick::Shared"))
Branch: 2 branches
BranchTrueFalseTotal
if (!sv_isobject(other) || !sv_derived_from(other, "Data::Fenwick::Shared")) 5 0 5
if (!sv_isobject(other) || !sv_derived_from(other, "Data::Fenwick::Shared")) 0 5 5
380 0 croak("Data::Fenwick::Shared->merge: expected a Data::Fenwick::Shared object");
381 5 FenHandle *o = INT2PTR(FenHandle*, SvIV(SvRV(other)));
382 5 ▶ if (!o) croak("Attempted to use a destroyed Data::Fenwick::Shared object");
Branch: 1 branch
BranchTrueFalseTotal
if (!o) croak("Attempted to use a destroyed Data::Fenwick::Shared object"); 0 5 5
383 /* sv_isobject/sv_derived_from above begin with SvGETMAGIC(other), so a
384 * tied `other` can have destroyed self before h is used below. `o` was
385 * read after that magic and needs no re-read. */
386 5 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 5 5
REEXTRACT(self); 0 5 5
387  
388 /* n is immutable after creation -- compare lock-free, croak BEFORE allocating */
389 5 ▶ if (h->mode == FEN_MODE_RANGE || o->mode == FEN_MODE_RANGE)
Branch: 2 branches
BranchTrueFalseTotal
if (h->mode == FEN_MODE_RANGE || o->mode == FEN_MODE_RANGE) 4 1 5
if (h->mode == FEN_MODE_RANGE || o->mode == FEN_MODE_RANGE) 0 4 4
390 1 croak("Data::Fenwick::Shared->merge: not supported for range-mode trees");
391 4 uint64_t on = o->hdr->n;
392 4 ▶ if (on != h->hdr->n)
Branch: 1 branch
BranchTrueFalseTotal
if (on != h->hdr->n) 1 3 4
393 1 croak("Data::Fenwick::Shared->merge: size mismatch (n=%" UVuf " vs n=%" UVuf ")",
394 (UV)h->hdr->n, (UV)on);
395  
396 /* Snapshot the other's tree slots under its read lock into a temp buffer,
397 * then release before taking self's write lock (deadlock-free). */
398 3 uint64_t slots = on + 1;
399 3 uint64_t o_slots_max = fen_tree_slots_max(o); /* Layer B: never read past o's mapping */
400 3 ▶ if (slots > o_slots_max) slots = o_slots_max;
Branch: 1 branch
BranchTrueFalseTotal
if (slots > o_slots_max) slots = o_slots_max; 0 3 3
401 int64_t *tmp;
402 3 ▶ Newx(tmp, (size_t)slots, int64_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(tmp, (size_t)slots, int64_t); 0 3 3
403 3 SAVEFREEPV(tmp); /* freed on normal return OR croak unwind */
404 3 ▶ if (o->readonly) { /* frozen other: immutable tree, no lock */
Branch: 1 branch
BranchTrueFalseTotal
if (o->readonly) { /* frozen other: immutable tree, no lock */ 1 2 3
405 1 memcpy(tmp, fen_tree(o), (size_t)slots * sizeof(int64_t));
406 } else {
407 2 fen_rwlock_rdlock(o);
408 2 memcpy(tmp, fen_tree(o), (size_t)slots * sizeof(int64_t));
409 2 fen_rwlock_rdunlock(o);
410 }
411  
412 3 fen_rwlock_wrlock(h);
413 3 ▶ if (h->hdr->sealed) { fen_rwlock_wrunlock(h); croak("Data::Fenwick::Shared->merge: structure is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { fen_rwlock_wrunlock(h); croak("Data::Fenwick::Shared->merge: structure is frozen (read-only)"); } 0 3 3
414 3 fen_merge_locked(h, tmp, slots);
415 3 h->hdr->stat_ops++;
416 3 fen_rwlock_wrunlock(h);
417  
418 # ---- introspection ----
419  
420 UV
421 size(self)
422 SV *self
423 PREINIT:
424 5 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 5 0 5
EXTRACT(self); 0 5 5
EXTRACT(self); 0 5 5
425 CODE:
426 5 ▶ RETVAL = (UV)h->hdr->n;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->hdr->n; 0 5 5
427 OUTPUT:
428 RETVAL
429  
430 UV
431 capacity(self)
432 SV *self
433 PREINIT:
434 2 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 2 0 2
EXTRACT(self); 0 2 2
EXTRACT(self); 0 2 2
435 CODE:
436 2 ▶ RETVAL = (UV)h->hdr->capacity;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->hdr->capacity; 0 2 2
437 OUTPUT:
438 RETVAL
439  
440 int
441 is_range(self)
442 SV *self
443 PREINIT:
444 7 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 7 0 7
EXTRACT(self); 0 7 7
EXTRACT(self); 0 7 7
445 CODE:
446 7 ▶ RETVAL = (h->mode == FEN_MODE_RANGE) ? 1 : 0; /* cached mode, no lock */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (h->mode == FEN_MODE_RANGE) ? 1 : 0; /* cached mode, no lock */ 0 7 7
447 OUTPUT:
448 RETVAL
449  
450 SV *
451 stats(self)
452 SV *self
453 PREINIT:
454 11 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 11 0 11
EXTRACT(self); 0 11 11
EXTRACT(self); 0 11 11
455 CODE:
456 {
457 uint64_t nn, ops, mmap_size;
458 int64_t tot;
459 11 ▶ if (!h->readonly) fen_rwlock_rdlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (!h->readonly) fen_rwlock_rdlock(h); 9 2 11
460 11 nn = h->hdr->n;
461 11 tot = fen_pref_locked(h, nn);
462 11 ops = h->hdr->stat_ops;
463 11 ▶ if (!h->readonly) fen_rwlock_rdunlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (!h->readonly) fen_rwlock_rdunlock(h); 9 2 11
464 11 mmap_size = (uint64_t)h->mmap_size;
465  
466 11 HV *hv = newHV();
467 11 hv_stores(hv, "size", newSVuv((UV)nn));
468 11 hv_stores(hv, "total", newSViv((IV)tot));
469 11 hv_stores(hv, "ops", newSVuv((UV)ops));
470 11 hv_stores(hv, "mmap_size", newSVuv((UV)mmap_size));
471 11 hv_stores(hv, "range", newSViv(h->mode == FEN_MODE_RANGE ? 1 : 0));
472 11 hv_stores(hv, "frozen", newSVuv(__atomic_load_n(&h->hdr->sealed, __ATOMIC_RELAXED) ? 1 : 0));
473 11 hv_stores(hv, "readonly", newSVuv(h->readonly ? 1 : 0));
474 11 RETVAL = newRV_noinc((SV *)hv);
475 }
476 OUTPUT:
477 RETVAL
478  
479 SV *
480 path(self)
481 SV *self
482 PREINIT:
483 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
484 CODE:
485 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
486 OUTPUT:
487 RETVAL
488  
489 int
490 memfd(self)
491 SV *self
492 PREINIT:
493 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
494 CODE:
495 3 ▶ RETVAL = h->backing_fd;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->backing_fd; 0 3 3
496 OUTPUT:
497 RETVAL
498  
499 void
500 sync(self)
501 SV *self
502 PREINIT:
503 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
504 CODE:
505 4 ▶ if (!h->readonly && fen_msync(h) != 0) croak("sync: %s", strerror(errno));
Branch: 2 branches
BranchTrueFalseTotal
if (!h->readonly && fen_msync(h) != 0) croak("sync: %s", strerror(errno)); 3 1 4
if (!h->readonly && fen_msync(h) != 0) croak("sync: %s", strerror(errno)); 0 3 3
506  
507 void
508 unlink(self, ...)
509 SV *self
510 CODE:
511 1 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::Fenwick::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::Fenwick::Shared")) { 0 1 1
if (sv_isobject(self) && sv_derived_from(self, "Data::Fenwick::Shared")) { 0 0 0
512 0 FenHandle *h = INT2PTR(FenHandle*, SvIV(SvRV(self)));
513 0 ▶ if (h && h->path && unlink(h->path) != 0 && errno != ENOENT)
Branch: 4 branches
BranchTrueFalseTotal
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 0 0 0
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 0 0 0
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 0 0 0
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 0 0 0
514 0 croak("Data::Fenwick::Shared->unlink(%s): %s", h->path, strerror(errno));
515 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
516 {
517 1 const char *up = SvPV_nolen(ST(1));
518 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
519 0 croak("Data::Fenwick::Shared->unlink(%s): %s", up, strerror(errno));
520 }
521 }
« fenwick.h ↑ Summary