| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| #include "ppport.h" |
| 6 |
|
| #include "segtree.h" |
| 7 |
|
| |
| 8 |
|
| #define EXTRACT(sv) \ |
| 9 |
|
| if (!sv_isobject(sv) || !sv_derived_from(sv, "Data::SegmentTree::Shared")) \ |
| 10 |
|
| croak("Expected a Data::SegmentTree::Shared object"); \ |
| 11 |
|
| StHandle *h = INT2PTR(StHandle*, SvIV(SvRV(sv))); \ |
| 12 |
|
| if (!h) croak("Attempted to use a destroyed Data::SegmentTree::Shared object"); \ |
| 13 |
|
| StHandle *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. EXTRACT's |
| 17 |
|
| * sv_2mortal(SvREFCNT_inc(...)) pin only blocks REFCOUNT-driven destruction; |
| 18 |
|
| * an explicit $obj->DESTROY frees the handle regardless and zeroes the IV. |
| 19 |
|
| * A position/value argument's SvUV/SvIV runs tie/overload magic; that same Perl |
| 20 |
|
| * can destroy the invocant, or REPLACE it ($obj = <other> mutates ST(0) in place, |
| 21 |
|
| * because Perl passes aliases), hence the SvROK re-check and the h0 identity |
| 22 |
|
| * compare. Re-validate before any dereference of h that follows a conversion. */ |
| 23 |
|
| #define REEXTRACT(sv) \ |
| 24 |
|
| if (!SvROK(sv)) \ |
| 25 |
|
| croak("Data::SegmentTree::Shared object was replaced during the call"); \ |
| 26 |
|
| h = INT2PTR(StHandle*, SvIV(SvRV(sv))); \ |
| 27 |
|
| if (h != h0) croak("Data::SegmentTree::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 |
|
| /* Validate + fetch a 0-based position, croaking on out-of-range (h->n is the |
| 36 |
|
| * process-local, attach-validated count -- not the peer-writable header). |
| 37 |
|
| * REEXTRACT(self) must run BEFORE the h->n dereference below (SvUV(sv) can run |
| 38 |
|
| * Perl that destroys/replaces the invocant -- see REEXTRACT above) -- the |
| 39 |
|
| * guard lives inside this macro because POS itself dereferences h. Every |
| 40 |
|
| * method that uses POS names its invocant `self`. */ |
| 41 |
|
| #define POS(nm, sv) \ |
| 42 |
|
| UV nm = SvUV(sv); \ |
| 43 |
|
| REEXTRACT(self); \ |
| 44 |
|
| if (nm >= h->n) croak("Data::SegmentTree::Shared: position %" UVuf " out of range (n=%" UVuf ")", nm, (UV)h->n) |
| 45 |
|
| |
| 46 |
|
| MODULE = Data::SegmentTree::Shared PACKAGE = Data::SegmentTree::Shared |
| 47 |
|
| |
| 48 |
|
| PROTOTYPES: DISABLE |
| 49 |
|
| |
| 50 |
|
| SV * |
| 51 |
|
| new(class, path = &PL_sv_undef, n = 0, ...) |
| 52 |
|
| const char *class |
| 53 |
|
| SV *path |
| 54 |
|
| UV n |
| 55 |
|
| PREINIT: |
| 56 |
|
| char errbuf[ST_ERR_BUFLEN]; |
| 57 |
|
| CODE: |
| 58 |
37 |
▶ | if (n < 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 1) |
1 |
36 |
37 |
|
| 59 |
1 |
| croak("Data::SegmentTree::Shared->new: number of positions must be >= 1"); |
| 60 |
|
| /* Optional 4th arg: file mode for a newly-created file-backed segment |
| 61 |
|
| * (default 0600, owner-only). Pass e.g. 0660 for cross-user sharing. |
| 62 |
|
| * Resolve it before capturing the path PV: SvGETMAGIC on ST(3) may |
| 63 |
|
| * realloc/free the PV that SvPV_nolen(path) would return. */ |
| 64 |
36 |
▶ | mode_t mode = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? ((mode_t)SvUV(ST(3)) & 0777) : 0600; /* mask off setuid/setgid/sticky bits on a data file */ |
Branch: 4 branches
| Branch | True | False | Total |
| mode_t mode = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? ((mode_t)SvUV(ST(3)) & 0777) : 0600; /* mask off setuid/setgid/sticky bits on a data file */ |
0 |
36 |
36 |
| mode_t mode = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? ((mode_t)SvUV(ST(3)) & 0777) : 0600; /* mask off setuid/setgid/sticky bits on a data file */ |
0 |
0 |
0 |
| mode_t mode = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? ((mode_t)SvUV(ST(3)) & 0777) : 0600; /* mask off setuid/setgid/sticky bits on a data file */ |
0 |
0 |
0 |
| mode_t mode = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? ((mode_t)SvUV(ST(3)) & 0777) : 0600; /* mask off setuid/setgid/sticky bits on a data file */ |
0 |
0 |
0 |
|
| 65 |
|
| /* capture the path PV last, after all get-magic on other args has run */ |
| 66 |
52 |
▶ | const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL; |
0 |
36 |
36 |
| 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 |
20 |
36 |
|
| 67 |
36 |
| StHandle *h = st_create(p, (uint64_t)n, mode, errbuf); |
| 68 |
36 |
▶ | if (!h) croak("Data::SegmentTree::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!h) croak("Data::SegmentTree::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); |
4 |
32 |
36 |
| if (!h) croak("Data::SegmentTree::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); |
4 |
0 |
4 |
|
| 69 |
32 |
| MAKE_OBJ(class, h); |
| 70 |
|
| OUTPUT: |
| 71 |
|
| RETVAL |
| 72 |
|
| |
| 73 |
|
| SV * |
| 74 |
|
| new_memfd(class, name = &PL_sv_undef, n = 0) |
| 75 |
|
| const char *class |
| 76 |
|
| SV *name |
| 77 |
|
| UV n |
| 78 |
|
| PREINIT: |
| 79 |
|
| char errbuf[ST_ERR_BUFLEN]; |
| 80 |
|
| CODE: |
| 81 |
2 |
▶ | const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL; /* undef -> default label */ |
Branch: 3 branches
| Branch | True | False | Total |
| const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL; /* undef -> default label */ |
0 |
1 |
1 |
| 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 */ |
1 |
0 |
1 |
|
| 82 |
1 |
▶ | if (n < 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 1) |
0 |
1 |
1 |
|
| 83 |
0 |
| croak("Data::SegmentTree::Shared->new_memfd: number of positions must be >= 1"); |
| 84 |
1 |
| StHandle *h = st_create_memfd(nm, (uint64_t)n, errbuf); |
| 85 |
1 |
▶ | if (!h) croak("Data::SegmentTree::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!h) croak("Data::SegmentTree::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); |
0 |
1 |
1 |
| if (!h) croak("Data::SegmentTree::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); |
0 |
0 |
0 |
|
| 86 |
1 |
| MAKE_OBJ(class, h); |
| 87 |
|
| OUTPUT: |
| 88 |
|
| RETVAL |
| 89 |
|
| |
| 90 |
|
| SV * |
| 91 |
|
| new_from_fd(class, fd) |
| 92 |
|
| const char *class |
| 93 |
|
| int fd |
| 94 |
|
| PREINIT: |
| 95 |
|
| char errbuf[ST_ERR_BUFLEN]; |
| 96 |
|
| CODE: |
| 97 |
1 |
| StHandle *h = st_open_fd(fd, errbuf); |
| 98 |
1 |
▶ | if (!h) croak("Data::SegmentTree::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!h) croak("Data::SegmentTree::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); |
0 |
1 |
1 |
| if (!h) croak("Data::SegmentTree::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); |
0 |
0 |
0 |
|
| 99 |
1 |
| MAKE_OBJ(class, h); |
| 100 |
|
| OUTPUT: |
| 101 |
|
| RETVAL |
| 102 |
|
| |
| 103 |
|
| void |
| 104 |
|
| DESTROY(self) |
| 105 |
|
| SV *self |
| 106 |
|
| CODE: |
| 107 |
36 |
▶ | if (sv_isobject(self) && sv_derived_from(self, "Data::SegmentTree::Shared")) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (sv_isobject(self) && sv_derived_from(self, "Data::SegmentTree::Shared")) { |
36 |
0 |
36 |
| if (sv_isobject(self) && sv_derived_from(self, "Data::SegmentTree::Shared")) { |
36 |
0 |
36 |
|
| 108 |
36 |
| StHandle *h = INT2PTR(StHandle*, SvIV(SvRV(self))); |
| 109 |
36 |
▶ | if (h) { sv_setiv(SvRV(self), 0); st_destroy(h); } /* null first: activates EXTRACT's use-after-destroy croak + makes a double DESTROY a no-op */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (h) { sv_setiv(SvRV(self), 0); st_destroy(h); } /* null first: activates EXTRACT's use-after-destroy croak + makes a double DESTROY a no-op */ |
34 |
2 |
36 |
|
| 110 |
|
| } |
| 111 |
|
| |
| 112 |
|
| void |
| 113 |
|
| set(self, i, value) |
| 114 |
|
| SV *self |
| 115 |
|
| SV *i |
| 116 |
|
| IV value |
| 117 |
|
| PREINIT: |
| 118 |
2132 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
2132 |
0 |
2132 |
| EXTRACT(self); |
0 |
2132 |
2132 |
| EXTRACT(self); |
0 |
2132 |
2132 |
|
| 119 |
|
| CODE: |
| 120 |
2132 |
▶ | POS(pos, i); |
Branch: 3 branches
| Branch | True | False | Total |
| POS(pos, i); |
0 |
2132 |
2132 |
| POS(pos, i); |
0 |
2132 |
2132 |
| POS(pos, i); |
1 |
2131 |
2132 |
|
| 121 |
2131 |
| st_rwlock_wrlock(h); |
| 122 |
2131 |
| st_set_locked(h, (uint64_t)pos, (int64_t)value); |
| 123 |
2131 |
| __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED); |
| 124 |
2131 |
| st_rwlock_wrunlock(h); |
| 125 |
|
| |
| 126 |
|
| IV |
| 127 |
|
| add(self, i, delta) |
| 128 |
|
| SV *self |
| 129 |
|
| SV *i |
| 130 |
|
| IV delta |
| 131 |
|
| PREINIT: |
| 132 |
1594 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
1594 |
0 |
1594 |
| EXTRACT(self); |
0 |
1594 |
1594 |
| EXTRACT(self); |
0 |
1594 |
1594 |
|
| 133 |
|
| int64_t v; |
| 134 |
|
| CODE: |
| 135 |
1594 |
▶ | POS(pos, i); |
Branch: 3 branches
| Branch | True | False | Total |
| POS(pos, i); |
0 |
1594 |
1594 |
| POS(pos, i); |
0 |
1594 |
1594 |
| POS(pos, i); |
0 |
1594 |
1594 |
|
| 136 |
1594 |
| st_rwlock_wrlock(h); |
| 137 |
1594 |
| st_range_add_locked(h, (uint64_t)pos, (uint64_t)pos, (int64_t)delta); |
| 138 |
1594 |
| v = st_get_locked(h, (uint64_t)pos); /* new value, under the same lock */ |
| 139 |
1594 |
| __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED); |
| 140 |
1594 |
| st_rwlock_wrunlock(h); |
| 141 |
1594 |
▶ | RETVAL = (IV)v; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (IV)v; |
1590 |
4 |
1594 |
|
| 142 |
|
| OUTPUT: |
| 143 |
|
| RETVAL |
| 144 |
|
| |
| 145 |
|
| void |
| 146 |
|
| range_add(self, l, r, delta) |
| 147 |
|
| SV *self |
| 148 |
|
| SV *l |
| 149 |
|
| SV *r |
| 150 |
|
| IV delta |
| 151 |
|
| PREINIT: |
| 152 |
1757 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
1757 |
0 |
1757 |
| EXTRACT(self); |
0 |
1757 |
1757 |
| EXTRACT(self); |
0 |
1757 |
1757 |
|
| 153 |
|
| CODE: |
| 154 |
1757 |
▶ | POS(lo, l); |
Branch: 3 branches
| Branch | True | False | Total |
| POS(lo, l); |
0 |
1757 |
1757 |
| POS(lo, l); |
0 |
1757 |
1757 |
| POS(lo, l); |
0 |
1757 |
1757 |
|
| 155 |
1757 |
▶ | POS(hi, r); |
Branch: 3 branches
| Branch | True | False | Total |
| POS(hi, r); |
0 |
1757 |
1757 |
| POS(hi, r); |
0 |
1757 |
1757 |
| POS(hi, r); |
0 |
1757 |
1757 |
|
| 156 |
1757 |
▶ | if (lo > hi) croak("Data::SegmentTree::Shared->range_add: l (%" UVuf ") > r (%" UVuf ")", lo, hi); |
Branch: 1 branch
| Branch | True | False | Total |
| if (lo > hi) croak("Data::SegmentTree::Shared->range_add: l (%" UVuf ") > r (%" UVuf ")", lo, hi); |
1 |
1756 |
1757 |
|
| 157 |
1756 |
| st_rwlock_wrlock(h); |
| 158 |
1756 |
| st_range_add_locked(h, (uint64_t)lo, (uint64_t)hi, (int64_t)delta); |
| 159 |
1756 |
| __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED); |
| 160 |
1756 |
| st_rwlock_wrunlock(h); |
| 161 |
|
| |
| 162 |
|
| void |
| 163 |
|
| range_assign(self, l, r, value) |
| 164 |
|
| SV *self |
| 165 |
|
| SV *l |
| 166 |
|
| SV *r |
| 167 |
|
| IV value |
| 168 |
|
| PREINIT: |
| 169 |
607 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
607 |
0 |
607 |
| EXTRACT(self); |
0 |
607 |
607 |
| EXTRACT(self); |
0 |
607 |
607 |
|
| 170 |
|
| CODE: |
| 171 |
607 |
▶ | POS(lo, l); |
Branch: 3 branches
| Branch | True | False | Total |
| POS(lo, l); |
0 |
607 |
607 |
| POS(lo, l); |
0 |
607 |
607 |
| POS(lo, l); |
0 |
607 |
607 |
|
| 172 |
607 |
▶ | POS(hi, r); |
Branch: 3 branches
| Branch | True | False | Total |
| POS(hi, r); |
0 |
607 |
607 |
| POS(hi, r); |
0 |
607 |
607 |
| POS(hi, r); |
0 |
607 |
607 |
|
| 173 |
607 |
▶ | if (lo > hi) croak("Data::SegmentTree::Shared->range_assign: l (%" UVuf ") > r (%" UVuf ")", lo, hi); |
Branch: 1 branch
| Branch | True | False | Total |
| if (lo > hi) croak("Data::SegmentTree::Shared->range_assign: l (%" UVuf ") > r (%" UVuf ")", lo, hi); |
0 |
607 |
607 |
|
| 174 |
607 |
| st_rwlock_wrlock(h); |
| 175 |
607 |
| st_range_assign_locked(h, (uint64_t)lo, (uint64_t)hi, (int64_t)value); |
| 176 |
607 |
| __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED); |
| 177 |
607 |
| st_rwlock_wrunlock(h); |
| 178 |
|
| |
| 179 |
|
| IV |
| 180 |
|
| get(self, i) |
| 181 |
|
| SV *self |
| 182 |
|
| SV *i |
| 183 |
|
| PREINIT: |
| 184 |
118 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
118 |
0 |
118 |
| EXTRACT(self); |
0 |
118 |
118 |
| EXTRACT(self); |
1 |
117 |
118 |
|
| 185 |
|
| int64_t v; |
| 186 |
|
| CODE: |
| 187 |
117 |
▶ | POS(pos, i); |
Branch: 3 branches
| Branch | True | False | Total |
| POS(pos, i); |
0 |
117 |
117 |
| POS(pos, i); |
0 |
117 |
117 |
| POS(pos, i); |
1 |
116 |
117 |
|
| 188 |
116 |
| st_rwlock_rdlock(h); |
| 189 |
116 |
| v = st_get_locked(h, (uint64_t)pos); |
| 190 |
116 |
| st_rwlock_rdunlock(h); |
| 191 |
116 |
▶ | RETVAL = (IV)v; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (IV)v; |
89 |
27 |
116 |
|
| 192 |
|
| OUTPUT: |
| 193 |
|
| RETVAL |
| 194 |
|
| |
| 195 |
|
| # range aggregate helpers: each returns one value over [l, r] |
| 196 |
|
| IV |
| 197 |
|
| sum(self, l, r) |
| 198 |
|
| SV *self |
| 199 |
|
| SV *l |
| 200 |
|
| SV *r |
| 201 |
|
| PREINIT: |
| 202 |
713 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
713 |
0 |
713 |
| EXTRACT(self); |
0 |
713 |
713 |
| EXTRACT(self); |
0 |
713 |
713 |
|
| 203 |
|
| int64_t s, mn, mx; |
| 204 |
|
| CODE: |
| 205 |
713 |
▶ | POS(lo, l); |
Branch: 3 branches
| Branch | True | False | Total |
| POS(lo, l); |
0 |
713 |
713 |
| POS(lo, l); |
0 |
713 |
713 |
| POS(lo, l); |
0 |
713 |
713 |
|
| 206 |
713 |
▶ | POS(hi, r); |
Branch: 3 branches
| Branch | True | False | Total |
| POS(hi, r); |
0 |
713 |
713 |
| POS(hi, r); |
0 |
713 |
713 |
| POS(hi, r); |
1 |
712 |
713 |
|
| 207 |
712 |
▶ | if (lo > hi) croak("Data::SegmentTree::Shared->sum: l > r"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (lo > hi) croak("Data::SegmentTree::Shared->sum: l > r"); |
0 |
712 |
712 |
|
| 208 |
712 |
| st_rwlock_rdlock(h); |
| 209 |
712 |
| st_query_locked(h, (uint64_t)lo, (uint64_t)hi, &s, &mn, &mx); |
| 210 |
712 |
| st_rwlock_rdunlock(h); |
| 211 |
712 |
▶ | RETVAL = (IV)s; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (IV)s; |
698 |
14 |
712 |
|
| 212 |
|
| OUTPUT: |
| 213 |
|
| RETVAL |
| 214 |
|
| |
| 215 |
|
| IV |
| 216 |
|
| min(self, l, r) |
| 217 |
|
| SV *self |
| 218 |
|
| SV *l |
| 219 |
|
| SV *r |
| 220 |
|
| PREINIT: |
| 221 |
706 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
706 |
0 |
706 |
| EXTRACT(self); |
0 |
706 |
706 |
| EXTRACT(self); |
0 |
706 |
706 |
|
| 222 |
|
| int64_t s, mn, mx; |
| 223 |
|
| CODE: |
| 224 |
706 |
▶ | POS(lo, l); |
Branch: 3 branches
| Branch | True | False | Total |
| POS(lo, l); |
0 |
706 |
706 |
| POS(lo, l); |
0 |
706 |
706 |
| POS(lo, l); |
0 |
706 |
706 |
|
| 225 |
706 |
▶ | POS(hi, r); |
Branch: 3 branches
| Branch | True | False | Total |
| POS(hi, r); |
0 |
706 |
706 |
| POS(hi, r); |
0 |
706 |
706 |
| POS(hi, r); |
0 |
706 |
706 |
|
| 226 |
706 |
▶ | if (lo > hi) croak("Data::SegmentTree::Shared->min: l > r"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (lo > hi) croak("Data::SegmentTree::Shared->min: l > r"); |
0 |
706 |
706 |
|
| 227 |
706 |
| st_rwlock_rdlock(h); |
| 228 |
706 |
| st_query_locked(h, (uint64_t)lo, (uint64_t)hi, &s, &mn, &mx); |
| 229 |
706 |
| st_rwlock_rdunlock(h); |
| 230 |
706 |
▶ | RETVAL = (IV)mn; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (IV)mn; |
698 |
8 |
706 |
|
| 231 |
|
| OUTPUT: |
| 232 |
|
| RETVAL |
| 233 |
|
| |
| 234 |
|
| IV |
| 235 |
|
| max(self, l, r) |
| 236 |
|
| SV *self |
| 237 |
|
| SV *l |
| 238 |
|
| SV *r |
| 239 |
|
| PREINIT: |
| 240 |
707 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
707 |
0 |
707 |
| EXTRACT(self); |
0 |
707 |
707 |
| EXTRACT(self); |
0 |
707 |
707 |
|
| 241 |
|
| int64_t s, mn, mx; |
| 242 |
|
| CODE: |
| 243 |
707 |
▶ | POS(lo, l); |
Branch: 3 branches
| Branch | True | False | Total |
| POS(lo, l); |
0 |
707 |
707 |
| POS(lo, l); |
0 |
707 |
707 |
| POS(lo, l); |
0 |
707 |
707 |
|
| 244 |
707 |
▶ | POS(hi, r); |
Branch: 3 branches
| Branch | True | False | Total |
| POS(hi, r); |
0 |
707 |
707 |
| POS(hi, r); |
0 |
707 |
707 |
| POS(hi, r); |
0 |
707 |
707 |
|
| 245 |
707 |
▶ | if (lo > hi) croak("Data::SegmentTree::Shared->max: l > r"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (lo > hi) croak("Data::SegmentTree::Shared->max: l > r"); |
0 |
707 |
707 |
|
| 246 |
707 |
| st_rwlock_rdlock(h); |
| 247 |
707 |
| st_query_locked(h, (uint64_t)lo, (uint64_t)hi, &s, &mn, &mx); |
| 248 |
707 |
| st_rwlock_rdunlock(h); |
| 249 |
707 |
▶ | RETVAL = (IV)mx; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (IV)mx; |
698 |
9 |
707 |
|
| 250 |
|
| OUTPUT: |
| 251 |
|
| RETVAL |
| 252 |
|
| |
| 253 |
|
| # query(l, r) -> { sum, min, max, count } over [l, r] in one lock |
| 254 |
|
| SV * |
| 255 |
|
| query(self, l, r) |
| 256 |
|
| SV *self |
| 257 |
|
| SV *l |
| 258 |
|
| SV *r |
| 259 |
|
| PREINIT: |
| 260 |
3165 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
3165 |
0 |
3165 |
| EXTRACT(self); |
0 |
3165 |
3165 |
| EXTRACT(self); |
0 |
3165 |
3165 |
|
| 261 |
|
| int64_t s, mn, mx; |
| 262 |
|
| CODE: |
| 263 |
3165 |
▶ | POS(lo, l); |
Branch: 3 branches
| Branch | True | False | Total |
| POS(lo, l); |
0 |
3165 |
3165 |
| POS(lo, l); |
0 |
3165 |
3165 |
| POS(lo, l); |
0 |
3165 |
3165 |
|
| 264 |
3165 |
▶ | POS(hi, r); |
Branch: 3 branches
| Branch | True | False | Total |
| POS(hi, r); |
0 |
3165 |
3165 |
| POS(hi, r); |
0 |
3165 |
3165 |
| POS(hi, r); |
0 |
3165 |
3165 |
|
| 265 |
3165 |
▶ | if (lo > hi) croak("Data::SegmentTree::Shared->query: l > r"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (lo > hi) croak("Data::SegmentTree::Shared->query: l > r"); |
1 |
3164 |
3165 |
|
| 266 |
3164 |
| st_rwlock_rdlock(h); |
| 267 |
3164 |
| st_query_locked(h, (uint64_t)lo, (uint64_t)hi, &s, &mn, &mx); |
| 268 |
3164 |
| st_rwlock_rdunlock(h); |
| 269 |
|
| { |
| 270 |
3164 |
| HV *hv = newHV(); |
| 271 |
3164 |
| hv_stores(hv, "sum", newSViv((IV)s)); |
| 272 |
3164 |
| hv_stores(hv, "min", newSViv((IV)mn)); |
| 273 |
3164 |
| hv_stores(hv, "max", newSViv((IV)mx)); |
| 274 |
3164 |
| hv_stores(hv, "count", newSVuv((UV)(hi - lo + 1))); |
| 275 |
3164 |
| RETVAL = newRV_noinc((SV *)hv); |
| 276 |
|
| } |
| 277 |
|
| OUTPUT: |
| 278 |
|
| RETVAL |
| 279 |
|
| |
| 280 |
|
| # gcd/product monoids: valid only while no range_add/add has run on the tree |
| 281 |
|
| # (they provably cannot be maintained under range-add). product croaks on int64 |
| 282 |
|
| # overflow. Both read the add_used gate under the lock and croak after unlock. |
| 283 |
|
| UV |
| 284 |
|
| gcd(self, l, r) |
| 285 |
|
| SV *self |
| 286 |
|
| SV *l |
| 287 |
|
| SV *r |
| 288 |
|
| PREINIT: |
| 289 |
410 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
410 |
0 |
410 |
| EXTRACT(self); |
0 |
410 |
410 |
| EXTRACT(self); |
0 |
410 |
410 |
|
| 290 |
410 |
▶ | int64_t g = 0; int used; |
Branch: 1 branch
| Branch | True | False | Total |
| int64_t g = 0; int used; |
410 |
0 |
410 |
|
| 291 |
|
| CODE: |
| 292 |
410 |
▶ | POS(lo, l); |
Branch: 3 branches
| Branch | True | False | Total |
| POS(lo, l); |
0 |
410 |
410 |
| POS(lo, l); |
0 |
410 |
410 |
| POS(lo, l); |
0 |
410 |
410 |
|
| 293 |
410 |
▶ | POS(hi, r); |
Branch: 3 branches
| Branch | True | False | Total |
| POS(hi, r); |
0 |
410 |
410 |
| POS(hi, r); |
0 |
410 |
410 |
| POS(hi, r); |
0 |
410 |
410 |
|
| 294 |
410 |
▶ | if (lo > hi) croak("Data::SegmentTree::Shared->gcd: l > r"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (lo > hi) croak("Data::SegmentTree::Shared->gcd: l > r"); |
0 |
410 |
410 |
|
| 295 |
410 |
| st_rwlock_rdlock(h); |
| 296 |
410 |
| used = (int)__atomic_load_n(&h->hdr->add_used, __ATOMIC_RELAXED); |
| 297 |
410 |
▶ | if (!used) g = st_gcd_locked(h, (uint64_t)lo, (uint64_t)hi); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!used) g = st_gcd_locked(h, (uint64_t)lo, (uint64_t)hi); |
408 |
2 |
410 |
|
| 298 |
410 |
| st_rwlock_rdunlock(h); |
| 299 |
410 |
▶ | if (used) croak("Data::SegmentTree::Shared->gcd: unavailable after range_add/add (gcd does not compose with range-add); use range_assign/set, or clear()"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (used) croak("Data::SegmentTree::Shared->gcd: unavailable after range_add/add (gcd does not compose with range-add); use range_assign/set, or clear()"); |
2 |
408 |
410 |
|
| 300 |
408 |
▶ | RETVAL = (UV)g; /* gcd is a non-negative magnitude; UV also holds the 2^63 case (all INT64_MIN) */ |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (UV)g; /* gcd is a non-negative magnitude; UV also holds the 2^63 case (all INT64_MIN) */ |
399 |
9 |
408 |
|
| 301 |
|
| OUTPUT: |
| 302 |
|
| RETVAL |
| 303 |
|
| |
| 304 |
|
| IV |
| 305 |
|
| product(self, l, r) |
| 306 |
|
| SV *self |
| 307 |
|
| SV *l |
| 308 |
|
| SV *r |
| 309 |
|
| PREINIT: |
| 310 |
312 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
312 |
0 |
312 |
| EXTRACT(self); |
0 |
312 |
312 |
| EXTRACT(self); |
0 |
312 |
312 |
|
| 311 |
312 |
▶ | int64_t p = 0; int used; int ovf = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| int64_t p = 0; int used; int ovf = 0; |
312 |
0 |
312 |
|
| 312 |
|
| CODE: |
| 313 |
312 |
▶ | POS(lo, l); |
Branch: 3 branches
| Branch | True | False | Total |
| POS(lo, l); |
0 |
312 |
312 |
| POS(lo, l); |
0 |
312 |
312 |
| POS(lo, l); |
0 |
312 |
312 |
|
| 314 |
312 |
▶ | POS(hi, r); |
Branch: 3 branches
| Branch | True | False | Total |
| POS(hi, r); |
0 |
312 |
312 |
| POS(hi, r); |
0 |
312 |
312 |
| POS(hi, r); |
0 |
312 |
312 |
|
| 315 |
312 |
▶ | if (lo > hi) croak("Data::SegmentTree::Shared->product: l > r"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (lo > hi) croak("Data::SegmentTree::Shared->product: l > r"); |
0 |
312 |
312 |
|
| 316 |
312 |
| st_rwlock_rdlock(h); |
| 317 |
312 |
| used = (int)__atomic_load_n(&h->hdr->add_used, __ATOMIC_RELAXED); |
| 318 |
312 |
▶ | if (!used) p = st_prod_locked(h, (uint64_t)lo, (uint64_t)hi, &ovf); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!used) p = st_prod_locked(h, (uint64_t)lo, (uint64_t)hi, &ovf); |
310 |
2 |
312 |
|
| 319 |
312 |
| st_rwlock_rdunlock(h); |
| 320 |
312 |
▶ | if (used) croak("Data::SegmentTree::Shared->product: unavailable after range_add/add; use range_assign/set, or clear()"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (used) croak("Data::SegmentTree::Shared->product: unavailable after range_add/add; use range_assign/set, or clear()"); |
2 |
310 |
312 |
|
| 321 |
310 |
▶ | if (ovf) croak("Data::SegmentTree::Shared->product: overflowed a signed 64-bit integer over [%" UVuf ", %" UVuf "]", lo, hi); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ovf) croak("Data::SegmentTree::Shared->product: overflowed a signed 64-bit integer over [%" UVuf ", %" UVuf "]", lo, hi); |
2 |
308 |
310 |
|
| 322 |
308 |
▶ | RETVAL = (IV)p; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (IV)p; |
299 |
9 |
308 |
|
| 323 |
|
| OUTPUT: |
| 324 |
|
| RETVAL |
| 325 |
|
| |
| 326 |
|
| int |
| 327 |
|
| monoids_valid(self) |
| 328 |
|
| SV *self |
| 329 |
|
| PREINIT: |
| 330 |
6 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
6 |
0 |
6 |
| EXTRACT(self); |
0 |
6 |
6 |
| EXTRACT(self); |
0 |
6 |
6 |
|
| 331 |
|
| CODE: |
| 332 |
6 |
▶ | RETVAL = __atomic_load_n(&h->hdr->add_used, __ATOMIC_RELAXED) ? 0 : 1; /* gcd/product usable? (unlocked read -> atomic) */ |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = __atomic_load_n(&h->hdr->add_used, __ATOMIC_RELAXED) ? 0 : 1; /* gcd/product usable? (unlocked read -> atomic) */ |
0 |
6 |
6 |
|
| 333 |
|
| OUTPUT: |
| 334 |
|
| RETVAL |
| 335 |
|
| |
| 336 |
|
| void |
| 337 |
|
| clear(self) |
| 338 |
|
| SV *self |
| 339 |
|
| PREINIT: |
| 340 |
3 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
3 |
0 |
3 |
| EXTRACT(self); |
0 |
3 |
3 |
| EXTRACT(self); |
0 |
3 |
3 |
|
| 341 |
|
| CODE: |
| 342 |
3 |
| st_rwlock_wrlock(h); |
| 343 |
3 |
| st_clear_locked(h); |
| 344 |
3 |
| __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED); |
| 345 |
3 |
| st_rwlock_wrunlock(h); |
| 346 |
|
| |
| 347 |
|
| UV |
| 348 |
|
| size(self) |
| 349 |
|
| SV *self |
| 350 |
|
| PREINIT: |
| 351 |
3 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
3 |
0 |
3 |
| EXTRACT(self); |
0 |
3 |
3 |
| EXTRACT(self); |
0 |
3 |
3 |
|
| 352 |
|
| CODE: |
| 353 |
3 |
▶ | RETVAL = (UV)h->n; /* attach-validated cache, not the peer-writable header */ |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (UV)h->n; /* attach-validated cache, not the peer-writable header */ |
0 |
3 |
3 |
|
| 354 |
|
| OUTPUT: |
| 355 |
|
| RETVAL |
| 356 |
|
| |
| 357 |
|
| SV * |
| 358 |
|
| stats(self) |
| 359 |
|
| SV *self |
| 360 |
|
| PREINIT: |
| 361 |
1 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
1 |
0 |
1 |
| EXTRACT(self); |
0 |
1 |
1 |
| EXTRACT(self); |
0 |
1 |
1 |
|
| 362 |
|
| CODE: |
| 363 |
|
| { |
| 364 |
|
| uint64_t n, sz, ops; |
| 365 |
1 |
| n = h->n; /* attach-validated caches, not the peer-writable header */ |
| 366 |
1 |
| sz = h->size; |
| 367 |
1 |
| ops = __atomic_load_n(&h->hdr->stat_ops, __ATOMIC_RELAXED); |
| 368 |
1 |
| HV *hv = newHV(); |
| 369 |
1 |
| hv_stores(hv, "n", newSVuv((UV)n)); |
| 370 |
1 |
| hv_stores(hv, "size", newSVuv((UV)n)); |
| 371 |
1 |
| hv_stores(hv, "tree_size", newSVuv((UV)sz)); |
| 372 |
1 |
| hv_stores(hv, "ops", newSVuv((UV)ops)); |
| 373 |
1 |
| hv_stores(hv, "mmap_size", newSVuv((UV)h->mmap_size)); |
| 374 |
1 |
| RETVAL = newRV_noinc((SV *)hv); |
| 375 |
|
| } |
| 376 |
|
| OUTPUT: |
| 377 |
|
| RETVAL |
| 378 |
|
| |
| 379 |
|
| SV * |
| 380 |
|
| path(self) |
| 381 |
|
| SV *self |
| 382 |
|
| PREINIT: |
| 383 |
3 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
3 |
0 |
3 |
| EXTRACT(self); |
0 |
3 |
3 |
| EXTRACT(self); |
0 |
3 |
3 |
|
| 384 |
|
| CODE: |
| 385 |
3 |
▶ | RETVAL = h->path ? newSVpv(h->path, 0) : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = h->path ? newSVpv(h->path, 0) : &PL_sv_undef; |
3 |
0 |
3 |
|
| 386 |
|
| OUTPUT: |
| 387 |
|
| RETVAL |
| 388 |
|
| |
| 389 |
|
| int |
| 390 |
|
| memfd(self) |
| 391 |
|
| SV *self |
| 392 |
|
| PREINIT: |
| 393 |
1 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
1 |
0 |
1 |
| EXTRACT(self); |
0 |
1 |
1 |
| EXTRACT(self); |
0 |
1 |
1 |
|
| 394 |
|
| CODE: |
| 395 |
1 |
▶ | RETVAL = h->backing_fd; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = h->backing_fd; |
0 |
1 |
1 |
|
| 396 |
|
| OUTPUT: |
| 397 |
|
| RETVAL |
| 398 |
|
| |
| 399 |
|
| void |
| 400 |
|
| sync(self) |
| 401 |
|
| SV *self |
| 402 |
|
| PREINIT: |
| 403 |
3 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
3 |
0 |
3 |
| EXTRACT(self); |
0 |
3 |
3 |
| EXTRACT(self); |
0 |
3 |
3 |
|
| 404 |
|
| CODE: |
| 405 |
3 |
▶ | if (st_msync(h) != 0) croak("sync: %s", strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st_msync(h) != 0) croak("sync: %s", strerror(errno)); |
0 |
3 |
3 |
|
| 406 |
|
| |
| 407 |
|
| void |
| 408 |
|
| unlink(self, ...) |
| 409 |
|
| SV *self |
| 410 |
|
| CODE: |
| 411 |
9 |
▶ | if (sv_isobject(self) && sv_derived_from(self, "Data::SegmentTree::Shared")) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (sv_isobject(self) && sv_derived_from(self, "Data::SegmentTree::Shared")) { |
4 |
1 |
5 |
| if (sv_isobject(self) && sv_derived_from(self, "Data::SegmentTree::Shared")) { |
4 |
0 |
4 |
|
| 412 |
4 |
| StHandle *h = INT2PTR(StHandle*, SvIV(SvRV(self))); |
| 413 |
4 |
▶ | if (h && h->path) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (h && h->path) { |
4 |
0 |
4 |
| if (h && h->path) { |
4 |
0 |
4 |
|
| 414 |
4 |
▶ | const char *up = h->unlink_path ? h->unlink_path : h->path; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *up = h->unlink_path ? h->unlink_path : h->path; |
4 |
0 |
4 |
|
| 415 |
4 |
▶ | if (unlink(up) != 0 && errno != ENOENT) |
Branch: 2 branches
| Branch | True | False | Total |
| if (unlink(up) != 0 && errno != ENOENT) |
2 |
2 |
4 |
| if (unlink(up) != 0 && errno != ENOENT) |
0 |
2 |
2 |
|
| 416 |
0 |
| croak("Data::SegmentTree::Shared->unlink(%s): %s", h->path, strerror(errno)); |
| 417 |
|
| } |
| 418 |
1 |
▶ | } else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { |
Branch: 4 branches
| Branch | True | False | Total |
| } 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 |
|
| 419 |
|
| { |
| 420 |
1 |
| const char *up = SvPV_nolen(ST(1)); |
| 421 |
1 |
▶ | if (unlink(up) != 0 && errno != ENOENT) |
Branch: 2 branches
| Branch | True | False | Total |
| if (unlink(up) != 0 && errno != ENOENT) |
0 |
1 |
1 |
| if (unlink(up) != 0 && errno != ENOENT) |
0 |
0 |
0 |
|
| 422 |
0 |
| croak("Data::SegmentTree::Shared->unlink(%s): %s", up, strerror(errno)); |
| 423 |
|
| } |
| 424 |
|
| } |