| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| #include "ppport.h" |
| 6 |
|
| #include "intervaltree.h" |
| 7 |
|
| |
| 8 |
|
| #define EXTRACT(sv) \ |
| 9 |
|
| if (!sv_isobject(sv) || !sv_derived_from(sv, "Data::IntervalTree::Shared")) \ |
| 10 |
|
| croak("Expected a Data::IntervalTree::Shared object"); \ |
| 11 |
|
| ItHandle *h = INT2PTR(ItHandle*, SvIV(SvRV(sv))); \ |
| 12 |
|
| if (!h) croak("Attempted to use a destroyed Data::IntervalTree::Shared object"); \ |
| 13 |
|
| ItHandle *h0 = h; PERL_UNUSED_VAR(h0); \ |
| 14 |
|
| sv_2mortal(SvREFCNT_inc(SvRV(sv))) |
| 15 |
|
| |
| 16 |
|
| /* Re-read the handle after a call that can run Perl code (tied/overloaded |
| 17 |
|
| * argument magic, tied-array fetches). That code may call $obj->DESTROY |
| 18 |
|
| * explicitly, which frees the handle and zeroes the IV; EXTRACT's mortal |
| 19 |
|
| * pins the referent only against refcount-driven destruction, not an |
| 20 |
|
| * explicit DESTROY, so the local `h` would dangle. Used only where magic |
| 21 |
|
| * can actually intervene between EXTRACT and the first use of h. */ |
| 22 |
|
| #define REEXTRACT(sv) \ |
| 23 |
|
| if (!SvROK(sv)) \ |
| 24 |
|
| croak("Data::IntervalTree::Shared object was replaced during the call"); \ |
| 25 |
|
| h = INT2PTR(ItHandle*, SvIV(SvRV(sv))); \ |
| 26 |
|
| if (h != h0) croak("Data::IntervalTree::Shared object replaced or destroyed during the call") |
| 27 |
|
| |
| 28 |
|
| #define MAKE_OBJ(class, handle) \ |
| 29 |
|
| SV *obj = newSViv(PTR2IV(handle)); \ |
| 30 |
|
| SV *ref = newRV_noinc(obj); \ |
| 31 |
|
| sv_bless(ref, gv_stashpv(class, GV_ADD)); \ |
| 32 |
|
| RETVAL = ref |
| 33 |
|
| |
| 34 |
|
| /* Take a lock suitable for a query: read lock if the tree is already built, |
| 35 |
|
| * otherwise upgrade to the write lock and (re)build a balanced tree. Returns 1 |
| 36 |
|
| * if holding the write lock, 0 if holding the read lock. */ |
| 37 |
232 |
| static int it_query_lock(ItHandle *h) { |
| 38 |
232 |
| it_rwlock_rdlock(h); |
| 39 |
232 |
▶ | if (!h->hdr->dirty) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h->hdr->dirty) return 0; |
216 |
16 |
232 |
|
| 40 |
16 |
| it_rwlock_rdunlock(h); |
| 41 |
16 |
| it_rwlock_wrlock(h); |
| 42 |
16 |
▶ | if (h->hdr->dirty) it_build_locked(h); |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->hdr->dirty) it_build_locked(h); |
16 |
0 |
16 |
|
| 43 |
16 |
| return 1; |
| 44 |
|
| } |
| 45 |
232 |
| static void it_query_unlock(ItHandle *h, int wr) { |
| 46 |
232 |
▶ | if (wr) it_rwlock_wrunlock(h); else it_rwlock_rdunlock(h); |
Branch: 1 branch
| Branch | True | False | Total |
| if (wr) it_rwlock_wrunlock(h); else it_rwlock_rdunlock(h); |
16 |
216 |
232 |
|
| 47 |
232 |
| } |
| 48 |
|
| |
| 49 |
|
| /* sort matching intervals by lo ascending (tiebreak hi, then id) */ |
| 50 |
31371 |
| static int it_res_cmp(const void *pa, const void *pb) { |
| 51 |
31371 |
| const ItRes *a = (const ItRes *)pa, *b = (const ItRes *)pb; |
| 52 |
31371 |
▶ | if (a->lo != b->lo) return a->lo < b->lo ? -1 : 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (a->lo != b->lo) return a->lo < b->lo ? -1 : 1; |
30722 |
649 |
31371 |
| if (a->lo != b->lo) return a->lo < b->lo ? -1 : 1; |
30722 |
0 |
30722 |
|
| 53 |
649 |
▶ | if (a->hi != b->hi) return a->hi < b->hi ? -1 : 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (a->hi != b->hi) return a->hi < b->hi ? -1 : 1; |
639 |
10 |
649 |
| if (a->hi != b->hi) return a->hi < b->hi ? -1 : 1; |
267 |
372 |
639 |
|
| 54 |
10 |
▶ | return (a->id < b->id) ? -1 : (a->id > b->id ? 1 : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| return (a->id < b->id) ? -1 : (a->id > b->id ? 1 : 0); |
0 |
10 |
10 |
|
| 55 |
|
| } |
| 56 |
|
| |
| 57 |
|
| /* push the matched intervals (sorted) as { id, lo, hi } hashrefs; must run after |
| 58 |
|
| * the query lock is released (builds Perl values). */ |
| 59 |
|
| #define PUSH_RESULTS(res, got) STMT_START { \ |
| 60 |
|
| uint64_t _i; \ |
| 61 |
|
| if (got) qsort(res, (size_t)(got), sizeof(ItRes), it_res_cmp); \ |
| 62 |
|
| EXTEND(SP, (SSize_t)(got)); \ |
| 63 |
|
| for (_i = 0; _i < (got); _i++) { \ |
| 64 |
|
| HV *hv = newHV(); \ |
| 65 |
|
| hv_stores(hv, "id", newSVuv((UV)(res)[_i].id)); \ |
| 66 |
|
| hv_stores(hv, "lo", newSViv((IV)(res)[_i].lo)); \ |
| 67 |
|
| hv_stores(hv, "hi", newSViv((IV)(res)[_i].hi)); \ |
| 68 |
|
| PUSHs(sv_2mortal(newRV_noinc((SV *)hv))); \ |
| 69 |
|
| } \ |
| 70 |
|
| } STMT_END |
| 71 |
|
| |
| 72 |
|
| MODULE = Data::IntervalTree::Shared PACKAGE = Data::IntervalTree::Shared |
| 73 |
|
| |
| 74 |
|
| PROTOTYPES: DISABLE |
| 75 |
|
| |
| 76 |
|
| SV * |
| 77 |
|
| new(class, path = &PL_sv_undef, capacity = 0, ...) |
| 78 |
|
| const char *class |
| 79 |
|
| SV *path |
| 80 |
|
| UV capacity |
| 81 |
|
| PREINIT: |
| 82 |
|
| char errbuf[IT_ERR_BUFLEN]; |
| 83 |
|
| CODE: |
| 84 |
32 |
▶ | if (capacity < 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (capacity < 1) |
1 |
31 |
32 |
|
| 85 |
1 |
| croak("Data::IntervalTree::Shared->new: capacity must be >= 1"); |
| 86 |
|
| /* Optional 4th arg: file mode for a newly-created file-backed segment |
| 87 |
|
| * (default 0600, owner-only). Pass e.g. 0660 for cross-user sharing. |
| 88 |
|
| * Resolved BEFORE the path PV is captured: this get-magic runs arbitrary |
| 89 |
|
| * Perl that could realloc/free that PV, so the path is captured last. */ |
| 90 |
31 |
▶ | mode_t mode = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? (mode_t)SvUV(ST(3)) : 0600; |
Branch: 4 branches
| Branch | True | False | Total |
| mode_t mode = (items > 3 && (SvGETMAGIC(ST(3)), SvOK(ST(3)))) ? (mode_t)SvUV(ST(3)) : 0600; |
0 |
31 |
31 |
| 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 |
|
| 91 |
50 |
▶ | 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 |
31 |
31 |
| 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; |
19 |
12 |
31 |
|
| 92 |
31 |
| ItHandle *h = it_create(p, (uint64_t)capacity, mode, errbuf); |
| 93 |
31 |
▶ | if (!h) croak("Data::IntervalTree::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!h) croak("Data::IntervalTree::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); |
5 |
26 |
31 |
| if (!h) croak("Data::IntervalTree::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); |
5 |
0 |
5 |
|
| 94 |
|
| /* Re-read the class PV from ST(0) now that all argument magic (capacity, |
| 95 |
|
| * mode, path) has run: xsubpp captured `class` in the INPUT section, |
| 96 |
|
| * before that magic, and the magic can realloc or free the PV it points |
| 97 |
|
| * into. SvPV_nolen, NOT SvPV_nomg: an overloaded class must stringify |
| 98 |
|
| * through its '""' overload. */ |
| 99 |
26 |
| class = SvPV_nolen(ST(0)); |
| 100 |
26 |
| MAKE_OBJ(class, h); |
| 101 |
|
| OUTPUT: |
| 102 |
|
| RETVAL |
| 103 |
|
| |
| 104 |
|
| SV * |
| 105 |
|
| new_memfd(class, name = &PL_sv_undef, capacity = 0) |
| 106 |
|
| const char *class |
| 107 |
|
| SV *name |
| 108 |
|
| UV capacity |
| 109 |
|
| PREINIT: |
| 110 |
|
| char errbuf[IT_ERR_BUFLEN]; |
| 111 |
|
| CODE: |
| 112 |
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 |
|
| 113 |
1 |
▶ | if (capacity < 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (capacity < 1) |
0 |
1 |
1 |
|
| 114 |
0 |
| croak("Data::IntervalTree::Shared->new_memfd: capacity must be >= 1"); |
| 115 |
1 |
| ItHandle *h = it_create_memfd(nm, (uint64_t)capacity, errbuf); |
| 116 |
1 |
▶ | if (!h) croak("Data::IntervalTree::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!h) croak("Data::IntervalTree::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); |
0 |
1 |
1 |
| if (!h) croak("Data::IntervalTree::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); |
0 |
0 |
0 |
|
| 117 |
1 |
| class = SvPV_nolen(ST(0)); /* re-read after argument magic; see new() */ |
| 118 |
1 |
| MAKE_OBJ(class, h); |
| 119 |
|
| OUTPUT: |
| 120 |
|
| RETVAL |
| 121 |
|
| |
| 122 |
|
| SV * |
| 123 |
|
| new_from_fd(class, fd) |
| 124 |
|
| const char *class |
| 125 |
|
| int fd |
| 126 |
|
| PREINIT: |
| 127 |
|
| char errbuf[IT_ERR_BUFLEN]; |
| 128 |
|
| CODE: |
| 129 |
2 |
| ItHandle *h = it_open_fd(fd, errbuf); |
| 130 |
2 |
▶ | if (!h) croak("Data::IntervalTree::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!h) croak("Data::IntervalTree::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); |
0 |
2 |
2 |
| if (!h) croak("Data::IntervalTree::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); |
0 |
0 |
0 |
|
| 131 |
2 |
| class = SvPV_nolen(ST(0)); /* re-read after argument magic; see new() */ |
| 132 |
2 |
| MAKE_OBJ(class, h); |
| 133 |
|
| OUTPUT: |
| 134 |
|
| RETVAL |
| 135 |
|
| |
| 136 |
|
| SV * |
| 137 |
|
| new_readonly(class, path) |
| 138 |
|
| const char *class |
| 139 |
|
| SV *path |
| 140 |
|
| PREINIT: |
| 141 |
|
| char errbuf[IT_ERR_BUFLEN]; |
| 142 |
|
| CODE: |
| 143 |
|
| /* Open a FROZEN (sealed) file read-only: O_RDONLY + PROT_READ, no lock |
| 144 |
|
| ever. A sealed file is immutable and no read path writes the mapping, |
| 145 |
|
| so queries take no reader-slot / rwlock traffic and any number of |
| 146 |
|
| processes can share one PROT_READ mapping (same architecture; the |
| 147 |
|
| magic rejects a wrong-endian file). */ |
| 148 |
13 |
▶ | 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 |
7 |
7 |
| const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL; |
0 |
0 |
0 |
| const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL; |
6 |
1 |
7 |
|
| 149 |
7 |
▶ | if (!p) croak("Data::IntervalTree::Shared->new_readonly: path is required"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) croak("Data::IntervalTree::Shared->new_readonly: path is required"); |
1 |
6 |
7 |
|
| 150 |
6 |
| ItHandle *h = it_open_readonly(p, errbuf); |
| 151 |
6 |
▶ | if (!h) croak("Data::IntervalTree::Shared->new_readonly: %s", errbuf); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) croak("Data::IntervalTree::Shared->new_readonly: %s", errbuf); |
2 |
4 |
6 |
|
| 152 |
4 |
| class = SvPV_nolen(ST(0)); /* re-read after argument magic; see new() */ |
| 153 |
4 |
| MAKE_OBJ(class, h); |
| 154 |
|
| OUTPUT: |
| 155 |
|
| RETVAL |
| 156 |
|
| |
| 157 |
|
| void |
| 158 |
|
| DESTROY(self) |
| 159 |
|
| SV *self |
| 160 |
|
| CODE: |
| 161 |
35 |
▶ | if (sv_isobject(self) && sv_derived_from(self, "Data::IntervalTree::Shared")) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (sv_isobject(self) && sv_derived_from(self, "Data::IntervalTree::Shared")) { |
35 |
0 |
35 |
| if (sv_isobject(self) && sv_derived_from(self, "Data::IntervalTree::Shared")) { |
35 |
0 |
35 |
|
| 162 |
35 |
| ItHandle *h = INT2PTR(ItHandle*, SvIV(SvRV(self))); |
| 163 |
35 |
▶ | if (h) { sv_setiv(SvRV(self), 0); it_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); it_destroy(h); } /* null first: activates EXTRACT's use-after-destroy croak + makes a double DESTROY a no-op */ |
33 |
2 |
35 |
|
| 164 |
|
| } |
| 165 |
|
| |
| 166 |
|
| UV |
| 167 |
|
| add(self, lo, hi, id = &PL_sv_undef) |
| 168 |
|
| SV *self |
| 169 |
|
| IV lo |
| 170 |
|
| IV hi |
| 171 |
|
| SV *id |
| 172 |
|
| PREINIT: |
| 173 |
1946 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
1946 |
0 |
1946 |
| EXTRACT(self); |
0 |
1946 |
1946 |
| EXTRACT(self); |
0 |
1946 |
1946 |
|
| 174 |
|
| int64_t slot; |
| 175 |
|
| uint64_t payload; |
| 176 |
|
| CODE: |
| 177 |
1946 |
▶ | if (h->readonly) croak("Data::IntervalTree::Shared->add: tree is frozen (read-only)"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->readonly) croak("Data::IntervalTree::Shared->add: tree is frozen (read-only)"); |
2 |
1944 |
1946 |
|
| 178 |
1944 |
▶ | if (lo > hi) croak("Data::IntervalTree::Shared->add: lo (%" IVdf ") > hi (%" IVdf ")", (IV)lo, (IV)hi); |
Branch: 1 branch
| Branch | True | False | Total |
| if (lo > hi) croak("Data::IntervalTree::Shared->add: lo (%" IVdf ") > hi (%" IVdf ")", (IV)lo, (IV)hi); |
1 |
1943 |
1944 |
|
| 179 |
|
| /* resolve the id BEFORE locking: SvUV on a tied/overloaded SV can run Perl |
| 180 |
|
| * code that dies, and a longjmp past the wrlock would strand it on a live PID. */ |
| 181 |
1943 |
▶ | int have_id = (SvGETMAGIC(id), SvOK(id)); |
Branch: 2 branches
| Branch | True | False | Total |
| int have_id = (SvGETMAGIC(id), SvOK(id)); |
0 |
1943 |
1943 |
| int have_id = (SvGETMAGIC(id), SvOK(id)); |
0 |
0 |
0 |
|
| 182 |
1943 |
▶ | uint64_t id_val = have_id ? (uint64_t)SvUV(id) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| uint64_t id_val = have_id ? (uint64_t)SvUV(id) : 0; |
1934 |
9 |
1943 |
|
| 183 |
1942 |
▶ | REEXTRACT(self); |
Branch: 2 branches
| Branch | True | False | Total |
| REEXTRACT(self); |
0 |
1942 |
1942 |
| REEXTRACT(self); |
0 |
1942 |
1942 |
|
| 184 |
1942 |
| it_rwlock_wrlock(h); |
| 185 |
1942 |
▶ | if (h->hdr->sealed) { it_rwlock_wrunlock(h); croak("Data::IntervalTree::Shared->add: tree is frozen (read-only)"); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->hdr->sealed) { it_rwlock_wrunlock(h); croak("Data::IntervalTree::Shared->add: tree is frozen (read-only)"); } |
1 |
1941 |
1942 |
|
| 186 |
1941 |
▶ | payload = have_id ? id_val : h->hdr->count; /* default id = insertion index */ |
Branch: 1 branch
| Branch | True | False | Total |
| payload = have_id ? id_val : h->hdr->count; /* default id = insertion index */ |
8 |
1933 |
1941 |
|
| 187 |
1941 |
| slot = it_add_locked(h, (int64_t)lo, (int64_t)hi, payload); |
| 188 |
1941 |
| h->hdr->stat_ops++; /* protected by the write lock */ |
| 189 |
1941 |
| it_rwlock_wrunlock(h); |
| 190 |
1941 |
▶ | if (slot < 0) croak("Data::IntervalTree::Shared->add: tree is full (capacity %u)", (unsigned)h->capacity); |
Branch: 1 branch
| Branch | True | False | Total |
| if (slot < 0) croak("Data::IntervalTree::Shared->add: tree is full (capacity %u)", (unsigned)h->capacity); |
1 |
1940 |
1941 |
|
| 191 |
1940 |
▶ | RETVAL = (UV)slot; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (UV)slot; |
1906 |
34 |
1940 |
|
| 192 |
|
| OUTPUT: |
| 193 |
|
| RETVAL |
| 194 |
|
| |
| 195 |
|
| void |
| 196 |
|
| build(self) |
| 197 |
|
| SV *self |
| 198 |
|
| PREINIT: |
| 199 |
4 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
4 |
0 |
4 |
| EXTRACT(self); |
0 |
4 |
4 |
| EXTRACT(self); |
0 |
4 |
4 |
|
| 200 |
|
| CODE: |
| 201 |
4 |
▶ | if (h->readonly) croak("Data::IntervalTree::Shared->build: tree is frozen (read-only)"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->readonly) croak("Data::IntervalTree::Shared->build: tree is frozen (read-only)"); |
2 |
2 |
4 |
|
| 202 |
2 |
| it_rwlock_wrlock(h); |
| 203 |
2 |
▶ | if (h->hdr->sealed) { it_rwlock_wrunlock(h); croak("Data::IntervalTree::Shared->build: tree is frozen (read-only)"); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->hdr->sealed) { it_rwlock_wrunlock(h); croak("Data::IntervalTree::Shared->build: tree is frozen (read-only)"); } |
1 |
1 |
2 |
|
| 204 |
1 |
▶ | if (h->hdr->dirty) it_build_locked(h); |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->hdr->dirty) it_build_locked(h); |
1 |
0 |
1 |
|
| 205 |
1 |
| it_rwlock_wrunlock(h); |
| 206 |
|
| |
| 207 |
|
| void |
| 208 |
|
| stab(self, point) |
| 209 |
|
| SV *self |
| 210 |
|
| IV point |
| 211 |
|
| PREINIT: |
| 212 |
139 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
139 |
0 |
139 |
| EXTRACT(self); |
0 |
139 |
139 |
| EXTRACT(self); |
0 |
139 |
139 |
|
| 213 |
|
| PPCODE: |
| 214 |
|
| { |
| 215 |
139 |
| ItRes *res = NULL; |
| 216 |
139 |
| uint64_t got = 0, cap = h->capacity; |
| 217 |
139 |
▶ | if (cap) { Newx(res, (size_t)cap, ItRes); SAVEFREEPV(res); } /* alloc BEFORE the lock */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (cap) { Newx(res, (size_t)cap, ItRes); SAVEFREEPV(res); } /* alloc BEFORE the lock */ |
139 |
0 |
139 |
| if (cap) { Newx(res, (size_t)cap, ItRes); SAVEFREEPV(res); } /* alloc BEFORE the lock */ |
0 |
139 |
139 |
|
| 218 |
139 |
▶ | if (h->readonly) { /* frozen: freeze() force-built the tree, so it is never dirty here -- lock-free, no build */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->readonly) { /* frozen: freeze() force-built the tree, so it is never dirty here -- lock-free, no build */ |
10 |
129 |
139 |
|
| 219 |
10 |
▶ | got = cap ? it_overlaps_locked(h, (int64_t)point, (int64_t)point, res, cap) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| got = cap ? it_overlaps_locked(h, (int64_t)point, (int64_t)point, res, cap) : 0; |
10 |
0 |
10 |
|
| 220 |
|
| } else { |
| 221 |
129 |
| int wr = it_query_lock(h); |
| 222 |
129 |
▶ | got = cap ? it_overlaps_locked(h, (int64_t)point, (int64_t)point, res, cap) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| got = cap ? it_overlaps_locked(h, (int64_t)point, (int64_t)point, res, cap) : 0; |
129 |
0 |
129 |
|
| 223 |
129 |
| it_query_unlock(h, wr); |
| 224 |
|
| } |
| 225 |
3194 |
▶ | PUSH_RESULTS(res, got); |
Branch: 4 branches
| Branch | True | False | Total |
| PUSH_RESULTS(res, got); |
132 |
7 |
139 |
| PUSH_RESULTS(res, got); |
139 |
0 |
139 |
| PUSH_RESULTS(res, got); |
0 |
139 |
139 |
| PUSH_RESULTS(res, got); |
3055 |
139 |
3194 |
|
| 226 |
|
| } |
| 227 |
|
| |
| 228 |
|
| void |
| 229 |
|
| overlaps(self, lo, hi) |
| 230 |
|
| SV *self |
| 231 |
|
| IV lo |
| 232 |
|
| IV hi |
| 233 |
|
| PREINIT: |
| 234 |
110 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
110 |
0 |
110 |
| EXTRACT(self); |
0 |
110 |
110 |
| EXTRACT(self); |
0 |
110 |
110 |
|
| 235 |
|
| PPCODE: |
| 236 |
|
| { |
| 237 |
110 |
| ItRes *res = NULL; |
| 238 |
110 |
| uint64_t got = 0, cap = h->capacity; |
| 239 |
110 |
▶ | if (lo > hi) croak("Data::IntervalTree::Shared->overlaps: lo (%" IVdf ") > hi (%" IVdf ")", (IV)lo, (IV)hi); |
Branch: 1 branch
| Branch | True | False | Total |
| if (lo > hi) croak("Data::IntervalTree::Shared->overlaps: lo (%" IVdf ") > hi (%" IVdf ")", (IV)lo, (IV)hi); |
2 |
108 |
110 |
|
| 240 |
108 |
▶ | if (cap) { Newx(res, (size_t)cap, ItRes); SAVEFREEPV(res); } /* alloc BEFORE the lock */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (cap) { Newx(res, (size_t)cap, ItRes); SAVEFREEPV(res); } /* alloc BEFORE the lock */ |
108 |
0 |
108 |
| if (cap) { Newx(res, (size_t)cap, ItRes); SAVEFREEPV(res); } /* alloc BEFORE the lock */ |
0 |
108 |
108 |
|
| 241 |
108 |
▶ | if (h->readonly) { /* frozen: freeze() force-built the tree, so it is never dirty here -- lock-free, no build */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->readonly) { /* frozen: freeze() force-built the tree, so it is never dirty here -- lock-free, no build */ |
5 |
103 |
108 |
|
| 242 |
5 |
▶ | got = cap ? it_overlaps_locked(h, (int64_t)lo, (int64_t)hi, res, cap) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| got = cap ? it_overlaps_locked(h, (int64_t)lo, (int64_t)hi, res, cap) : 0; |
5 |
0 |
5 |
|
| 243 |
|
| } else { |
| 244 |
103 |
| int wr = it_query_lock(h); |
| 245 |
103 |
▶ | got = cap ? it_overlaps_locked(h, (int64_t)lo, (int64_t)hi, res, cap) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| got = cap ? it_overlaps_locked(h, (int64_t)lo, (int64_t)hi, res, cap) : 0; |
103 |
0 |
103 |
|
| 246 |
103 |
| it_query_unlock(h, wr); |
| 247 |
|
| } |
| 248 |
8046 |
▶ | PUSH_RESULTS(res, got); |
Branch: 4 branches
| Branch | True | False | Total |
| PUSH_RESULTS(res, got); |
107 |
1 |
108 |
| PUSH_RESULTS(res, got); |
108 |
0 |
108 |
| PUSH_RESULTS(res, got); |
0 |
108 |
108 |
| PUSH_RESULTS(res, got); |
7938 |
108 |
8046 |
|
| 249 |
|
| } |
| 250 |
|
| |
| 251 |
|
| void |
| 252 |
|
| clear(self) |
| 253 |
|
| SV *self |
| 254 |
|
| PREINIT: |
| 255 |
5 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
5 |
0 |
5 |
| EXTRACT(self); |
0 |
5 |
5 |
| EXTRACT(self); |
0 |
5 |
5 |
|
| 256 |
|
| CODE: |
| 257 |
5 |
▶ | if (h->readonly) croak("Data::IntervalTree::Shared->clear: tree is frozen (read-only)"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->readonly) croak("Data::IntervalTree::Shared->clear: tree is frozen (read-only)"); |
2 |
3 |
5 |
|
| 258 |
3 |
| it_rwlock_wrlock(h); |
| 259 |
3 |
▶ | if (h->hdr->sealed) { it_rwlock_wrunlock(h); croak("Data::IntervalTree::Shared->clear: tree is frozen (read-only)"); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->hdr->sealed) { it_rwlock_wrunlock(h); croak("Data::IntervalTree::Shared->clear: tree is frozen (read-only)"); } |
1 |
2 |
3 |
|
| 260 |
2 |
| it_clear_locked(h); |
| 261 |
2 |
| h->hdr->stat_ops++; |
| 262 |
2 |
| it_rwlock_wrunlock(h); |
| 263 |
|
| |
| 264 |
|
| void |
| 265 |
|
| freeze(self) |
| 266 |
|
| SV *self |
| 267 |
|
| PREINIT: |
| 268 |
5 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
5 |
0 |
5 |
| EXTRACT(self); |
0 |
5 |
5 |
| EXTRACT(self); |
0 |
5 |
5 |
|
| 269 |
|
| CODE: |
| 270 |
5 |
▶ | if (h->readonly) croak("Data::IntervalTree::Shared->freeze: cannot freeze a read-only handle"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->readonly) croak("Data::IntervalTree::Shared->freeze: cannot freeze a read-only handle"); |
2 |
3 |
5 |
|
| 271 |
3 |
▶ | if (it_freeze(h) != 0) croak("Data::IntervalTree::Shared->freeze: msync: %s", strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (it_freeze(h) != 0) croak("Data::IntervalTree::Shared->freeze: msync: %s", strerror(errno)); |
0 |
3 |
3 |
|
| 272 |
3 |
| h->readonly = 1; /* this handle now rejects mutation too (the file is sealed) */ |
| 273 |
|
| |
| 274 |
|
| UV |
| 275 |
|
| frozen(self) |
| 276 |
|
| SV *self |
| 277 |
|
| PREINIT: |
| 278 |
4 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
4 |
0 |
4 |
| EXTRACT(self); |
0 |
4 |
4 |
| EXTRACT(self); |
0 |
4 |
4 |
|
| 279 |
|
| CODE: |
| 280 |
4 |
▶ | RETVAL = __atomic_load_n(&h->hdr->sealed, __ATOMIC_ACQUIRE) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = __atomic_load_n(&h->hdr->sealed, __ATOMIC_ACQUIRE) ? 1 : 0; |
0 |
4 |
4 |
|
| 281 |
|
| OUTPUT: |
| 282 |
|
| RETVAL |
| 283 |
|
| |
| 284 |
|
| UV |
| 285 |
|
| readonly(self) |
| 286 |
|
| SV *self |
| 287 |
|
| PREINIT: |
| 288 |
4 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
4 |
0 |
4 |
| EXTRACT(self); |
0 |
4 |
4 |
| EXTRACT(self); |
0 |
4 |
4 |
|
| 289 |
|
| CODE: |
| 290 |
4 |
▶ | RETVAL = h->readonly ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = h->readonly ? 1 : 0; |
0 |
4 |
4 |
|
| 291 |
|
| OUTPUT: |
| 292 |
|
| RETVAL |
| 293 |
|
| |
| 294 |
|
| UV |
| 295 |
|
| count(self) |
| 296 |
|
| SV *self |
| 297 |
|
| PREINIT: |
| 298 |
16 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
16 |
0 |
16 |
| EXTRACT(self); |
0 |
16 |
16 |
| EXTRACT(self); |
1 |
15 |
16 |
|
| 299 |
|
| UV n; |
| 300 |
|
| CODE: |
| 301 |
15 |
▶ | if (h->readonly) { /* frozen: immutable, no lock needed */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->readonly) { /* frozen: immutable, no lock needed */ |
2 |
13 |
15 |
|
| 302 |
2 |
| n = (UV)h->hdr->count; |
| 303 |
|
| } else { |
| 304 |
13 |
| it_rwlock_rdlock(h); |
| 305 |
13 |
| n = (UV)h->hdr->count; |
| 306 |
13 |
| it_rwlock_rdunlock(h); |
| 307 |
|
| } |
| 308 |
15 |
▶ | RETVAL = n; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = n; |
0 |
15 |
15 |
|
| 309 |
|
| OUTPUT: |
| 310 |
|
| RETVAL |
| 311 |
|
| |
| 312 |
|
| UV |
| 313 |
|
| capacity(self) |
| 314 |
|
| SV *self |
| 315 |
|
| PREINIT: |
| 316 |
4 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
4 |
0 |
4 |
| EXTRACT(self); |
0 |
4 |
4 |
| EXTRACT(self); |
0 |
4 |
4 |
|
| 317 |
|
| CODE: |
| 318 |
4 |
▶ | RETVAL = (UV)h->hdr->capacity; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (UV)h->hdr->capacity; |
0 |
4 |
4 |
|
| 319 |
|
| OUTPUT: |
| 320 |
|
| RETVAL |
| 321 |
|
| |
| 322 |
|
| SV * |
| 323 |
|
| stats(self) |
| 324 |
|
| SV *self |
| 325 |
|
| PREINIT: |
| 326 |
12 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
12 |
0 |
12 |
| EXTRACT(self); |
0 |
12 |
12 |
| EXTRACT(self); |
0 |
12 |
12 |
|
| 327 |
|
| CODE: |
| 328 |
|
| { |
| 329 |
|
| uint64_t count, ops; |
| 330 |
|
| uint32_t cap, dirty; |
| 331 |
|
| uint8_t sealed; |
| 332 |
|
| /* Snapshot under the lock; do all (croak-capable) Perl allocation after |
| 333 |
|
| releasing it -- so an OOM in newHV/newSVuv can never strand the lock. */ |
| 334 |
12 |
▶ | if (!h->readonly) it_rwlock_rdlock(h); /* frozen: immutable, no lock */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h->readonly) it_rwlock_rdlock(h); /* frozen: immutable, no lock */ |
9 |
3 |
12 |
|
| 335 |
12 |
| count = h->hdr->count; |
| 336 |
12 |
| cap = h->hdr->capacity; |
| 337 |
12 |
| dirty = h->hdr->dirty; |
| 338 |
12 |
| ops = h->hdr->stat_ops; |
| 339 |
12 |
| sealed = __atomic_load_n(&h->hdr->sealed, __ATOMIC_ACQUIRE); |
| 340 |
12 |
▶ | if (!h->readonly) it_rwlock_rdunlock(h); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h->readonly) it_rwlock_rdunlock(h); |
9 |
3 |
12 |
|
| 341 |
12 |
| HV *hv = newHV(); |
| 342 |
12 |
| hv_stores(hv, "count", newSVuv((UV)count)); |
| 343 |
12 |
| hv_stores(hv, "capacity", newSVuv((UV)cap)); |
| 344 |
12 |
| hv_stores(hv, "dirty", newSVuv((UV)dirty)); |
| 345 |
12 |
| hv_stores(hv, "ops", newSVuv((UV)ops)); |
| 346 |
12 |
| hv_stores(hv, "mmap_size", newSVuv((UV)h->mmap_size)); |
| 347 |
12 |
| hv_stores(hv, "frozen", newSVuv(sealed ? 1 : 0)); |
| 348 |
12 |
| hv_stores(hv, "readonly", newSVuv(h->readonly ? 1 : 0)); |
| 349 |
12 |
| RETVAL = newRV_noinc((SV *)hv); |
| 350 |
|
| } |
| 351 |
|
| OUTPUT: |
| 352 |
|
| RETVAL |
| 353 |
|
| |
| 354 |
|
| SV * |
| 355 |
|
| path(self) |
| 356 |
|
| SV *self |
| 357 |
|
| PREINIT: |
| 358 |
2 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
2 |
0 |
2 |
| EXTRACT(self); |
0 |
2 |
2 |
| EXTRACT(self); |
0 |
2 |
2 |
|
| 359 |
|
| CODE: |
| 360 |
2 |
▶ | 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; |
2 |
0 |
2 |
|
| 361 |
|
| OUTPUT: |
| 362 |
|
| RETVAL |
| 363 |
|
| |
| 364 |
|
| int |
| 365 |
|
| memfd(self) |
| 366 |
|
| SV *self |
| 367 |
|
| PREINIT: |
| 368 |
2 |
▶ | EXTRACT(self); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTRACT(self); |
2 |
0 |
2 |
| EXTRACT(self); |
0 |
2 |
2 |
| EXTRACT(self); |
0 |
2 |
2 |
|
| 369 |
|
| CODE: |
| 370 |
2 |
▶ | RETVAL = h->backing_fd; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = h->backing_fd; |
0 |
2 |
2 |
|
| 371 |
|
| OUTPUT: |
| 372 |
|
| RETVAL |
| 373 |
|
| |
| 374 |
|
| void |
| 375 |
|
| sync(self) |
| 376 |
|
| SV *self |
| 377 |
|
| PREINIT: |
| 378 |
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 |
|
| 379 |
|
| CODE: |
| 380 |
3 |
▶ | if (!h->readonly && it_msync(h) != 0) croak("sync: %s", strerror(errno)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!h->readonly && it_msync(h) != 0) croak("sync: %s", strerror(errno)); |
2 |
1 |
3 |
| if (!h->readonly && it_msync(h) != 0) croak("sync: %s", strerror(errno)); |
0 |
2 |
2 |
|
| 381 |
|
| |
| 382 |
|
| void |
| 383 |
|
| unlink(self, ...) |
| 384 |
|
| SV *self |
| 385 |
|
| CODE: |
| 386 |
1 |
▶ | if (sv_isobject(self) && sv_derived_from(self, "Data::IntervalTree::Shared")) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (sv_isobject(self) && sv_derived_from(self, "Data::IntervalTree::Shared")) { |
0 |
1 |
1 |
| if (sv_isobject(self) && sv_derived_from(self, "Data::IntervalTree::Shared")) { |
0 |
0 |
0 |
|
| 387 |
0 |
| ItHandle *h = INT2PTR(ItHandle*, SvIV(SvRV(self))); |
| 388 |
0 |
▶ | if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) |
Branch: 4 branches
| Branch | True | False | Total |
| 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 |
|
| 389 |
0 |
| croak("Data::IntervalTree::Shared->unlink(%s): %s", h->path, strerror(errno)); |
| 390 |
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 |
|
| 391 |
|
| { |
| 392 |
1 |
| const char *up = SvPV_nolen(ST(1)); |
| 393 |
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 |
|
| 394 |
0 |
| croak("Data::IntervalTree::Shared->unlink(%s): %s", up, strerror(errno)); |
| 395 |
|
| } |
| 396 |
|
| } |