Shared.xs

filter: Statementstmt 97.5% 201 / 206 Branchbran 57.5% 205 / 356 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 72.2% 406 / 562 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« kdtree.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 <math.h>
7 #include "kdtree.h"
8  
9 #define EXTRACT(sv) \
10 if (!sv_isobject(sv) || !sv_derived_from(sv, "Data::KDTree::Shared")) \
11 croak("Expected a Data::KDTree::Shared object"); \
12 KdHandle *h = INT2PTR(KdHandle*, SvIV(SvRV(sv))); \
13 if (!h) croak("Attempted to use a destroyed Data::KDTree::Shared object"); \
14 KdHandle *h0 = h; PERL_UNUSED_VAR(h0); \
15 sv_2mortal(SvREFCNT_inc(SvRV(sv)))
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 * Magic reaches us through kd_read_point: SvGETMAGIC on the arrayref, av_len
21 * on a TIED array (AvFILL -> mg_size -> FETCHSIZE), and SvNV on each element.
22 * The same Perl can also REPLACE the invocant ($obj = 42 mutates ST(0),
23 * because Perl passes aliases), hence the SvROK re-check before SvRV. */
24 #define REEXTRACT(sv) \
25 if (!SvROK(sv)) \
26 croak("Data::KDTree::Shared object was replaced during the call"); \
27 h = INT2PTR(KdHandle*, SvIV(SvRV(sv))); \
28 if (h != h0) croak("Data::KDTree::Shared object replaced or destroyed during the call")
29  
30 #define MAKE_OBJ(class, handle) \
31 SV *obj = newSViv(PTR2IV(handle)); \
32 SV *ref = newRV_noinc(obj); \
33 sv_bless(ref, gv_stashpv(class, GV_ADD)); \
34 RETVAL = ref
35  
36 /* Take a lock suitable for a query: read lock if the tree is already built,
37 * otherwise upgrade to the write lock and (re)build a balanced tree. Returns 1
38 * if holding the write lock, 0 if holding the read lock. */
39 190 static int kd_query_lock(KdHandle *h) {
40 190 kd_rwlock_rdlock(h);
41 190 ▶ if (!__atomic_load_n(&h->hdr->dirty, __ATOMIC_ACQUIRE)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!__atomic_load_n(&h->hdr->dirty, __ATOMIC_ACQUIRE)) return 0; 169 21 190
42 21 kd_rwlock_rdunlock(h);
43 21 kd_rwlock_wrlock(h);
44 21 ▶ if (__atomic_load_n(&h->hdr->dirty, __ATOMIC_ACQUIRE)) kd_build_locked(h);
Branch: 1 branch
BranchTrueFalseTotal
if (__atomic_load_n(&h->hdr->dirty, __ATOMIC_ACQUIRE)) kd_build_locked(h); 21 0 21
45 21 return 1;
46 }
47 190 static void kd_query_unlock(KdHandle *h, int wr) {
48 190 ▶ if (wr) kd_rwlock_wrunlock(h); else kd_rwlock_rdunlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (wr) kd_rwlock_wrunlock(h); else kd_rwlock_rdunlock(h); 21 169 190
49 190 }
50  
51 /* read an arrayref of exactly dims finite coordinates into buf (croaks on error) */
52 /* `dims` is immutable geometry, so it is snapshotted ONCE up front rather than
53 * re-read from the handle after each magic-capable step: the arrayref's
54 * get-magic, a tied array's FETCHSIZE, and every element's SvNV can all run
55 * Perl that destroys the object. The caller re-reads its own handle after this
56 * returns (see REEXTRACT at each call site) -- `h` here is by value, so a
57 * re-read inside this function would not reach the caller's copy. */
58 2681 static void kd_read_point(pTHX_ KdHandle *h, SV *aref, double *buf, const char *what) {
59 2681 uint32_t dims = h->dims;
60 2681 ▶ SvGETMAGIC(aref); /* a tied/overloaded container FETCHes its arrayref here */
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(aref); /* a tied/overloaded container FETCHes its arrayref here */ 0 2681 2681
SvGETMAGIC(aref); /* a tied/overloaded container FETCHes its arrayref here */ 0 0 0
61 2681 ▶ if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV) 2680 1 2681
if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV) 0 2680 2680
62 1 croak("Data::KDTree::Shared->%s: expected an array reference of %u coordinates", what, (unsigned)dims);
63 2680 AV *av = (AV *)SvRV(aref);
64 2680 sv_2mortal(SvREFCNT_inc((SV *)av)); /* pin the arrayref: element magic below cannot free it mid-loop */
65 2680 ▶ if (av_len(av) + 1 != (IV)dims)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(av) + 1 != (IV)dims) 3 2677 2680
66 3 croak("Data::KDTree::Shared->%s: expected %u coordinates, got %ld", what, (unsigned)dims, (long)(av_len(av) + 1));
67 10679 ▶ for (uint32_t d = 0; d < dims; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (uint32_t d = 0; d < dims; d++) { 8016 2663 10679
68 8016 SV **el = av_fetch(av, (SSize_t)d, 0);
69 8016 ▶ if (!el || !*el)
Branch: 2 branches
BranchTrueFalseTotal
if (!el || !*el) 8010 6 8016
if (!el || !*el) 0 8010 8010
70 6 croak("Data::KDTree::Shared->%s: coordinate %u is missing", what, (unsigned)d);
71 8010 SV *coord = *el;
72 8010 ▶ SvGETMAGIC(coord);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(coord); 2 8008 8010
SvGETMAGIC(coord); 0 2 2
73 8010 ▶ if (!SvOK(coord))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(coord)) 7 8003 8010
74 7 croak("Data::KDTree::Shared->%s: coordinate %u is missing", what, (unsigned)d);
75 8003 double v = SvNV_nomg(coord);
76 8003 ▶ if (!isfinite(v)) croak("Data::KDTree::Shared->%s: coordinate %u is not finite", what, (unsigned)d);
Branch: 1 branch
BranchTrueFalseTotal
if (!isfinite(v)) croak("Data::KDTree::Shared->%s: coordinate %u is not finite", what, (unsigned)d); 1 8002 8003
77 8002 buf[d] = v;
78 }
79 2663 }
80  
81 /* sort KdRes ascending by distance (tiebreak by id) for knn/radius results */
82 3424 static int kd_res_cmp(const void *pa, const void *pb) {
83 3424 const KdRes *a = (const KdRes *)pa, *b = (const KdRes *)pb;
84 3424 ▶ if (a->dist2 < b->dist2) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (a->dist2 < b->dist2) return -1; 1416 2008 3424
85 2008 ▶ if (a->dist2 > b->dist2) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (a->dist2 > b->dist2) return 1; 1936 72 2008
86 72 ▶ return (a->id < b->id) ? -1 : (a->id > b->id ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
return (a->id < b->id) ? -1 : (a->id > b->id ? 1 : 0); 42 30 72
87 }
88  
89 MODULE = Data::KDTree::Shared PACKAGE = Data::KDTree::Shared
90  
91 PROTOTYPES: DISABLE
92  
93 SV *
94 new(class, path = &PL_sv_undef, dims = 2, capacity = 0, ...)
95 const char *class
96 SV *path
97 UV dims
98 UV capacity
99 PREINIT:
100 char errbuf[KD_ERR_BUFLEN];
101 CODE:
102 37 ▶ if (capacity < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (capacity < 1) 1 36 37
103 1 croak("Data::KDTree::Shared->new: capacity must be >= 1");
104 /* Optional 5th arg: file mode for a newly-created file-backed segment
105 * (default 0600, owner-only). Pass e.g. 0660 for cross-user sharing. */
106 36 ▶ mode_t mode = (items > 4 && (SvGETMAGIC(ST(4)), SvOK(ST(4)))) ? (mode_t)SvUV(ST(4)) : 0600;
Branch: 4 branches
BranchTrueFalseTotal
mode_t mode = (items > 4 && (SvGETMAGIC(ST(4)), SvOK(ST(4)))) ? (mode_t)SvUV(ST(4)) : 0600; 0 36 36
mode_t mode = (items > 4 && (SvGETMAGIC(ST(4)), SvOK(ST(4)))) ? (mode_t)SvUV(ST(4)) : 0600; 0 0 0
mode_t mode = (items > 4 && (SvGETMAGIC(ST(4)), SvOK(ST(4)))) ? (mode_t)SvUV(ST(4)) : 0600; 0 0 0
mode_t mode = (items > 4 && (SvGETMAGIC(ST(4)), SvOK(ST(4)))) ? (mode_t)SvUV(ST(4)) : 0600; 0 0 0
107 /* Capture the path PV only after every trailing arg is resolved: the
108 * get-magic above can run Perl code that reallocs/frees path's PV. */
109 58 ▶ 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 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; 22 14 36
110 36 KdHandle *h = kd_create(p, (uint64_t)dims, (uint64_t)capacity, mode, errbuf);
111 36 ▶ if (!h) croak("Data::KDTree::Shared->new: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::KDTree::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 7 29 36
if (!h) croak("Data::KDTree::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 7 0 7
112 29 MAKE_OBJ(class, h);
113 OUTPUT:
114 RETVAL
115  
116 SV *
117 new_memfd(class, name = &PL_sv_undef, dims = 2, capacity = 0)
118 const char *class
119 SV *name
120 UV dims
121 UV capacity
122 PREINIT:
123 char errbuf[KD_ERR_BUFLEN];
124 CODE:
125 2 ▶ 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 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
126 1 ▶ if (capacity < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (capacity < 1) 0 1 1
127 0 croak("Data::KDTree::Shared->new_memfd: capacity must be >= 1");
128 1 KdHandle *h = kd_create_memfd(nm, (uint64_t)dims, (uint64_t)capacity, errbuf);
129 1 ▶ if (!h) croak("Data::KDTree::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::KDTree::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 1 1
if (!h) croak("Data::KDTree::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 0 0
130 1 MAKE_OBJ(class, h);
131 OUTPUT:
132 RETVAL
133  
134 SV *
135 new_from_fd(class, fd)
136 const char *class
137 int fd
138 PREINIT:
139 char errbuf[KD_ERR_BUFLEN];
140 CODE:
141 1 KdHandle *h = kd_open_fd(fd, errbuf);
142 1 ▶ if (!h) croak("Data::KDTree::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::KDTree::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 0 1 1
if (!h) croak("Data::KDTree::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 0 0 0
143 1 MAKE_OBJ(class, h);
144 OUTPUT:
145 RETVAL
146  
147 SV *
148 new_readonly(class, path)
149 const char *class
150 SV *path
151 PREINIT:
152 char errbuf[KD_ERR_BUFLEN];
153 CODE:
154 /* Open a FROZEN (sealed) file read-only: O_RDONLY + PROT_READ, no lock
155 ever. A sealed file is immutable and no read path writes the mapping,
156 so queries take no reader-slot / rwlock traffic and any number of
157 processes can share one PROT_READ mapping (same architecture; the
158 magic rejects a wrong-endian file). */
159 27 ▶ 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 14 14
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; 13 1 14
160 14 ▶ if (!p) croak("Data::KDTree::Shared->new_readonly: path is required");
Branch: 1 branch
BranchTrueFalseTotal
if (!p) croak("Data::KDTree::Shared->new_readonly: path is required"); 1 13 14
161 13 KdHandle *h = kd_open_readonly(p, errbuf);
162 13 ▶ if (!h) croak("Data::KDTree::Shared->new_readonly: %s", errbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) croak("Data::KDTree::Shared->new_readonly: %s", errbuf); 2 11 13
163 11 class = SvPV_nolen(ST(0)); /* re-read the class PV after path's get-magic (may realloc ST(0)) */
164 11 MAKE_OBJ(class, h);
165 OUTPUT:
166 RETVAL
167  
168 void
169 DESTROY(self)
170 SV *self
171 CODE:
172 44 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::KDTree::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::KDTree::Shared")) { 44 0 44
if (sv_isobject(self) && sv_derived_from(self, "Data::KDTree::Shared")) { 44 0 44
173 44 KdHandle *h = INT2PTR(KdHandle*, SvIV(SvRV(self)));
174 44 ▶ if (h) { sv_setiv(SvRV(self), 0); kd_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); kd_destroy(h); } /* null first: activates EXTRACT's use-after-destroy croak + makes a double DESTROY a no-op */ 42 2 44
175 }
176  
177 UV
178 add(self, coords, id = &PL_sv_undef)
179 SV *self
180 SV *coords
181 SV *id
182 PREINIT:
183 2270 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 2270 0 2270
EXTRACT(self); 0 2270 2270
EXTRACT(self); 0 2270 2270
184 double buf[KD_MAX_DIMS];
185 int64_t slot;
186 uint64_t payload;
187 CODE:
188 2270 ▶ if (h->readonly) croak("Data::KDTree::Shared->add: tree is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::KDTree::Shared->add: tree is frozen (read-only)"); 2 2268 2270
189 2268 kd_read_point(aTHX_ h, coords, buf, "add"); /* may croak -- BEFORE the lock */
190 2261 ▶ REEXTRACT(self); /* kd_read_point ran arbitrary Perl */
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); /* kd_read_point ran arbitrary Perl */ 0 2261 2261
REEXTRACT(self); /* kd_read_point ran arbitrary Perl */ 0 2261 2261
191 /* Resolve the id BEFORE locking: SvUV on a tied/overloaded SV can run Perl
192 * code that dies, and a longjmp past the wrlock would strand it on a live
193 * PID (dead-owner recovery never fires) -- a permanent shared-segment deadlock. */
194 2261 ▶ int have_id = (SvGETMAGIC(id), SvOK(id));
Branch: 2 branches
BranchTrueFalseTotal
int have_id = (SvGETMAGIC(id), SvOK(id)); 0 2261 2261
int have_id = (SvGETMAGIC(id), SvOK(id)); 0 0 0
195 2261 ▶ uint64_t id_val = have_id ? (uint64_t)SvUV(id) : 0;
Branch: 1 branch
BranchTrueFalseTotal
uint64_t id_val = have_id ? (uint64_t)SvUV(id) : 0; 2233 28 2261
196 2261 ▶ REEXTRACT(self); /* the id's get-magic is a second window */
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); /* the id's get-magic is a second window */ 0 2261 2261
REEXTRACT(self); /* the id's get-magic is a second window */ 0 2261 2261
197 2261 kd_rwlock_wrlock(h);
198 2261 ▶ if (h->hdr->sealed) { kd_rwlock_wrunlock(h); croak("Data::KDTree::Shared->add: tree is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { kd_rwlock_wrunlock(h); croak("Data::KDTree::Shared->add: tree is frozen (read-only)"); } 0 2261 2261
199 2261 ▶ payload = have_id ? id_val : h->hdr->count; /* default id = insertion index */
Branch: 1 branch
BranchTrueFalseTotal
payload = have_id ? id_val : h->hdr->count; /* default id = insertion index */ 28 2233 2261
200 2261 slot = kd_add_locked(h, buf, payload);
201 2261 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
202 2261 kd_rwlock_wrunlock(h);
203 2261 ▶ if (slot < 0) croak("Data::KDTree::Shared->add: tree is full (capacity %u)", (unsigned)h->capacity);
Branch: 1 branch
BranchTrueFalseTotal
if (slot < 0) croak("Data::KDTree::Shared->add: tree is full (capacity %u)", (unsigned)h->capacity); 1 2260 2261
204 2260 ▶ RETVAL = (UV)slot;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)slot; 2232 28 2260
205 OUTPUT:
206 RETVAL
207  
208 void
209 build(self)
210 SV *self
211 PREINIT:
212 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
213 CODE:
214 3 ▶ if (h->readonly) croak("Data::KDTree::Shared->build: tree is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::KDTree::Shared->build: tree is frozen (read-only)"); 2 1 3
215 1 kd_rwlock_wrlock(h);
216 1 ▶ if (h->hdr->sealed) { kd_rwlock_wrunlock(h); croak("Data::KDTree::Shared->build: tree is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { kd_rwlock_wrunlock(h); croak("Data::KDTree::Shared->build: tree is frozen (read-only)"); } 0 1 1
217 1 ▶ if (h->hdr->dirty) kd_build_locked(h);
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->dirty) kd_build_locked(h); 1 0 1
218 1 kd_rwlock_wrunlock(h);
219  
220 SV *
221 nearest(self, coords)
222 SV *self
223 SV *coords
224 PREINIT:
225 145 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 145 0 145
EXTRACT(self); 0 145 145
EXTRACT(self); 0 145 145
226 double buf[KD_MAX_DIMS];
227 KdRes best;
228 uint64_t found;
229 CODE:
230 145 kd_read_point(aTHX_ h, coords, buf, "nearest");
231 142 ▶ REEXTRACT(self); /* kd_read_point ran arbitrary Perl */
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); /* kd_read_point ran arbitrary Perl */ 0 142 142
REEXTRACT(self); /* kd_read_point ran arbitrary Perl */ 0 142 142
232 142 ▶ if (h->readonly) { /* frozen: freeze() force-built the tree, so it is never dirty here -- lock-free, no build */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: freeze() force-built the tree, so it is never dirty here -- lock-free, no build */ 64 78 142
233 64 found = kd_knn_locked(h, buf, 1, &best);
234 } else {
235 78 int wr = kd_query_lock(h);
236 78 found = kd_knn_locked(h, buf, 1, &best);
237 78 kd_query_unlock(h, wr);
238 }
239 142 ▶ if (found) {
Branch: 1 branch
BranchTrueFalseTotal
if (found) { 138 4 142
240 138 HV *hv = newHV();
241 138 hv_stores(hv, "id", newSVuv((UV)best.id));
242 138 hv_stores(hv, "dist", newSVnv(sqrt(best.dist2)));
243 138 RETVAL = newRV_noinc((SV *)hv);
244 } else {
245 4 RETVAL = &PL_sv_undef; /* empty tree */
246 }
247 OUTPUT:
248 RETVAL
249  
250 void
251 knn(self, coords, m)
252 SV *self
253 SV *coords
254 UV m
255 PREINIT:
256 65 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 65 0 65
EXTRACT(self); 0 65 65
EXTRACT(self); 0 65 65
257 double buf[KD_MAX_DIMS];
258 PPCODE:
259 65 kd_read_point(aTHX_ h, coords, buf, "knn");
260 63 ▶ REEXTRACT(self); /* kd_read_point ran arbitrary Perl */
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); /* kd_read_point ran arbitrary Perl */ 0 63 63
REEXTRACT(self); /* kd_read_point ran arbitrary Perl */ 0 63 63
261 {
262 63 KdRes *res = NULL;
263 63 uint64_t got = 0, i;
264 63 ▶ if (m > h->capacity) m = h->capacity; /* at most all points */
Branch: 1 branch
BranchTrueFalseTotal
if (m > h->capacity) m = h->capacity; /* at most all points */ 0 63 63
265 63 ▶ if (m) { Newx(res, (size_t)m, KdRes); SAVEFREEPV(res); } /* alloc BEFORE the lock */
Branch: 2 branches
BranchTrueFalseTotal
if (m) { Newx(res, (size_t)m, KdRes); SAVEFREEPV(res); } /* alloc BEFORE the lock */ 63 0 63
if (m) { Newx(res, (size_t)m, KdRes); SAVEFREEPV(res); } /* alloc BEFORE the lock */ 0 63 63
266 63 ▶ if (h->readonly) { /* frozen: never dirty -- lock-free, no build */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: never dirty -- lock-free, no build */ 31 32 63
267 31 ▶ got = m ? kd_knn_locked(h, buf, m, res) : 0;
Branch: 1 branch
BranchTrueFalseTotal
got = m ? kd_knn_locked(h, buf, m, res) : 0; 31 0 31
268 } else {
269 32 int wr = kd_query_lock(h);
270 32 ▶ got = m ? kd_knn_locked(h, buf, m, res) : 0;
Branch: 1 branch
BranchTrueFalseTotal
got = m ? kd_knn_locked(h, buf, m, res) : 0; 32 0 32
271 32 kd_query_unlock(h, wr);
272 }
273 63 ▶ if (got) qsort(res, (size_t)got, sizeof(KdRes), kd_res_cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (got) qsort(res, (size_t)got, sizeof(KdRes), kd_res_cmp); 61 2 63
274 63 ▶ EXTEND(SP, (SSize_t)got);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, (SSize_t)got); 63 0 63
EXTEND(SP, (SSize_t)got); 0 63 63
275 613 ▶ for (i = 0; i < got; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < got; i++) { 550 63 613
276 550 HV *hv = newHV();
277 550 hv_stores(hv, "id", newSVuv((UV)res[i].id));
278 550 hv_stores(hv, "dist", newSVnv(sqrt(res[i].dist2)));
279 550 PUSHs(sv_2mortal(newRV_noinc((SV *)hv)));
280 }
281 }
282  
283 void
284 range(self, lo, hi)
285 SV *self
286 SV *lo
287 SV *hi
288 PREINIT:
289 57 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 57 0 57
EXTRACT(self); 0 57 57
EXTRACT(self); 0 57 57
290 double blo[KD_MAX_DIMS], bhi[KD_MAX_DIMS];
291 PPCODE:
292 57 kd_read_point(aTHX_ h, lo, blo, "range");
293 55 ▶ REEXTRACT(self); /* kd_read_point ran arbitrary Perl */
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); /* kd_read_point ran arbitrary Perl */ 0 55 55
REEXTRACT(self); /* kd_read_point ran arbitrary Perl */ 0 55 55
294 55 kd_read_point(aTHX_ h, hi, bhi, "range");
295 53 ▶ REEXTRACT(self); /* kd_read_point ran arbitrary Perl */
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); /* kd_read_point ran arbitrary Perl */ 0 53 53
REEXTRACT(self); /* kd_read_point ran arbitrary Perl */ 0 53 53
296 {
297 53 uint64_t *ids = NULL, got = 0, i, cap = h->capacity;
298 53 ▶ if (cap) { Newx(ids, (size_t)cap, uint64_t); SAVEFREEPV(ids); } /* alloc BEFORE the lock */
Branch: 2 branches
BranchTrueFalseTotal
if (cap) { Newx(ids, (size_t)cap, uint64_t); SAVEFREEPV(ids); } /* alloc BEFORE the lock */ 53 0 53
if (cap) { Newx(ids, (size_t)cap, uint64_t); SAVEFREEPV(ids); } /* alloc BEFORE the lock */ 0 53 53
299 53 ▶ if (h->readonly) { /* frozen: never dirty -- lock-free, no build */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: never dirty -- lock-free, no build */ 22 31 53
300 22 ▶ got = cap ? kd_range_locked(h, blo, bhi, ids, cap) : 0;
Branch: 1 branch
BranchTrueFalseTotal
got = cap ? kd_range_locked(h, blo, bhi, ids, cap) : 0; 22 0 22
301 } else {
302 31 int wr = kd_query_lock(h);
303 31 ▶ got = cap ? kd_range_locked(h, blo, bhi, ids, cap) : 0;
Branch: 1 branch
BranchTrueFalseTotal
got = cap ? kd_range_locked(h, blo, bhi, ids, cap) : 0; 31 0 31
304 31 kd_query_unlock(h, wr);
305 }
306 53 ▶ EXTEND(SP, (SSize_t)got);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, (SSize_t)got); 53 0 53
EXTEND(SP, (SSize_t)got); 0 53 53
307 130 ▶ for (i = 0; i < got; i++) PUSHs(sv_2mortal(newSVuv((UV)ids[i])));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < got; i++) PUSHs(sv_2mortal(newSVuv((UV)ids[i]))); 77 53 130
308 }
309  
310 void
311 radius(self, coords, r)
312 SV *self
313 SV *coords
314 double r
315 PREINIT:
316 92 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 92 0 92
EXTRACT(self); 0 92 92
EXTRACT(self); 0 92 92
317 double buf[KD_MAX_DIMS];
318 PPCODE:
319 92 ▶ if (r < 0) croak("Data::KDTree::Shared->radius: radius must be >= 0");
Branch: 1 branch
BranchTrueFalseTotal
if (r < 0) croak("Data::KDTree::Shared->radius: radius must be >= 0"); 1 91 92
320 91 kd_read_point(aTHX_ h, coords, buf, "radius");
321 89 ▶ REEXTRACT(self); /* kd_read_point ran arbitrary Perl */
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); /* kd_read_point ran arbitrary Perl */ 0 89 89
REEXTRACT(self); /* kd_read_point ran arbitrary Perl */ 0 89 89
322 {
323 89 KdRes *res = NULL;
324 89 uint64_t got = 0, i, cap = h->capacity;
325 89 ▶ if (cap) { Newx(res, (size_t)cap, KdRes); SAVEFREEPV(res); } /* alloc BEFORE the lock */
Branch: 2 branches
BranchTrueFalseTotal
if (cap) { Newx(res, (size_t)cap, KdRes); SAVEFREEPV(res); } /* alloc BEFORE the lock */ 89 0 89
if (cap) { Newx(res, (size_t)cap, KdRes); SAVEFREEPV(res); } /* alloc BEFORE the lock */ 0 89 89
326 89 ▶ if (h->readonly) { /* frozen: never dirty -- lock-free, no build */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: never dirty -- lock-free, no build */ 40 49 89
327 40 ▶ got = cap ? kd_radius_locked(h, buf, r, res, cap) : 0;
Branch: 1 branch
BranchTrueFalseTotal
got = cap ? kd_radius_locked(h, buf, r, res, cap) : 0; 40 0 40
328 } else {
329 49 int wr = kd_query_lock(h);
330 49 ▶ got = cap ? kd_radius_locked(h, buf, r, res, cap) : 0;
Branch: 1 branch
BranchTrueFalseTotal
got = cap ? kd_radius_locked(h, buf, r, res, cap) : 0; 49 0 49
331 49 kd_query_unlock(h, wr);
332 }
333 89 ▶ if (got) qsort(res, (size_t)got, sizeof(KdRes), kd_res_cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (got) qsort(res, (size_t)got, sizeof(KdRes), kd_res_cmp); 72 17 89
334 89 ▶ EXTEND(SP, (SSize_t)got);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, (SSize_t)got); 89 0 89
EXTEND(SP, (SSize_t)got); 0 89 89
335 864 ▶ for (i = 0; i < got; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < got; i++) { 775 89 864
336 775 HV *hv = newHV();
337 775 hv_stores(hv, "id", newSVuv((UV)res[i].id));
338 775 hv_stores(hv, "dist", newSVnv(sqrt(res[i].dist2)));
339 775 PUSHs(sv_2mortal(newRV_noinc((SV *)hv)));
340 }
341 }
342  
343 void
344 clear(self)
345 SV *self
346 PREINIT:
347 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
348 CODE:
349 3 ▶ if (h->readonly) croak("Data::KDTree::Shared->clear: tree is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::KDTree::Shared->clear: tree is frozen (read-only)"); 2 1 3
350 1 kd_rwlock_wrlock(h);
351 1 ▶ if (h->hdr->sealed) { kd_rwlock_wrunlock(h); croak("Data::KDTree::Shared->clear: tree is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { kd_rwlock_wrunlock(h); croak("Data::KDTree::Shared->clear: tree is frozen (read-only)"); } 0 1 1
352 1 kd_clear_locked(h);
353 1 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
354 1 kd_rwlock_wrunlock(h);
355  
356 void
357 freeze(self)
358 SV *self
359 PREINIT:
360 11 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 11 0 11
EXTRACT(self); 0 11 11
EXTRACT(self); 0 11 11
361 CODE:
362 11 ▶ if (h->readonly) croak("Data::KDTree::Shared->freeze: cannot freeze a read-only handle");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::KDTree::Shared->freeze: cannot freeze a read-only handle"); 2 9 11
363 9 ▶ if (kd_freeze(h) != 0) croak("Data::KDTree::Shared->freeze: msync: %s", strerror(errno));
Branch: 1 branch
BranchTrueFalseTotal
if (kd_freeze(h) != 0) croak("Data::KDTree::Shared->freeze: msync: %s", strerror(errno)); 0 9 9
364 9 h->readonly = 1; /* this handle now rejects mutation too (the file is sealed) */
365  
366 UV
367 frozen(self)
368 SV *self
369 PREINIT:
370 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
371 CODE:
372 3 ▶ RETVAL = __atomic_load_n(&h->hdr->sealed, __ATOMIC_ACQUIRE) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = __atomic_load_n(&h->hdr->sealed, __ATOMIC_ACQUIRE) ? 1 : 0; 0 3 3
373 OUTPUT:
374 RETVAL
375  
376 UV
377 readonly(self)
378 SV *self
379 PREINIT:
380 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
381 CODE:
382 3 ▶ RETVAL = h->readonly ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->readonly ? 1 : 0; 0 3 3
383 OUTPUT:
384 RETVAL
385  
386 UV
387 count(self)
388 SV *self
389 PREINIT:
390 18 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 18 0 18
EXTRACT(self); 0 18 18
EXTRACT(self); 1 17 18
391 UV n;
392 CODE:
393 17 ▶ if (h->readonly) { /* frozen: immutable, no lock needed */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: immutable, no lock needed */ 3 14 17
394 3 n = (UV)h->hdr->count;
395 } else {
396 14 kd_rwlock_rdlock(h);
397 14 n = (UV)h->hdr->count;
398 14 kd_rwlock_rdunlock(h);
399 }
400 17 ▶ RETVAL = n;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = n; 1 16 17
401 OUTPUT:
402 RETVAL
403  
404 UV
405 capacity(self)
406 SV *self
407 PREINIT:
408 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
409 CODE:
410 4 ▶ RETVAL = (UV)h->hdr->capacity;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->hdr->capacity; 0 4 4
411 OUTPUT:
412 RETVAL
413  
414 UV
415 dims(self)
416 SV *self
417 PREINIT:
418 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
419 CODE:
420 3 ▶ RETVAL = (UV)h->hdr->dims;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->hdr->dims; 0 3 3
421 OUTPUT:
422 RETVAL
423  
424 SV *
425 stats(self)
426 SV *self
427 PREINIT:
428 8 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 8 0 8
EXTRACT(self); 0 8 8
EXTRACT(self); 0 8 8
429 CODE:
430 {
431 uint64_t count, ops;
432 uint32_t dims, cap, dirty;
433 /* Snapshot under the lock; do all (croak-capable) Perl allocation after
434 releasing it -- so an OOM in newHV/newSVuv can never strand the lock. */
435 8 ▶ if (!h->readonly) kd_rwlock_rdlock(h); /* frozen: immutable, no lock */
Branch: 1 branch
BranchTrueFalseTotal
if (!h->readonly) kd_rwlock_rdlock(h); /* frozen: immutable, no lock */ 5 3 8
436 8 count = h->hdr->count;
437 8 dims = h->hdr->dims;
438 8 cap = h->hdr->capacity;
439 8 dirty = h->hdr->dirty;
440 8 ops = h->hdr->stat_ops;
441 8 ▶ if (!h->readonly) kd_rwlock_rdunlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (!h->readonly) kd_rwlock_rdunlock(h); 5 3 8
442 8 HV *hv = newHV();
443 8 hv_stores(hv, "count", newSVuv((UV)count));
444 8 hv_stores(hv, "dims", newSVuv((UV)dims));
445 8 hv_stores(hv, "capacity", newSVuv((UV)cap));
446 8 hv_stores(hv, "dirty", newSVuv((UV)dirty));
447 8 hv_stores(hv, "ops", newSVuv((UV)ops));
448 8 hv_stores(hv, "mmap_size", newSVuv((UV)h->mmap_size));
449 8 hv_stores(hv, "frozen", newSVuv(h->hdr->sealed ? 1 : 0));
450 8 hv_stores(hv, "readonly", newSVuv(h->readonly ? 1 : 0));
451 8 RETVAL = newRV_noinc((SV *)hv);
452 }
453 OUTPUT:
454 RETVAL
455  
456 SV *
457 path(self)
458 SV *self
459 PREINIT:
460 2 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 2 0 2
EXTRACT(self); 0 2 2
EXTRACT(self); 0 2 2
461 CODE:
462 2 ▶ 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 0 2
463 OUTPUT:
464 RETVAL
465  
466 int
467 memfd(self)
468 SV *self
469 PREINIT:
470 2 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 2 0 2
EXTRACT(self); 0 2 2
EXTRACT(self); 0 2 2
471 CODE:
472 2 ▶ RETVAL = h->backing_fd;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->backing_fd; 0 2 2
473 OUTPUT:
474 RETVAL
475  
476 void
477 sync(self)
478 SV *self
479 PREINIT:
480 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
481 CODE:
482 3 ▶ if (!h->readonly && kd_msync(h) != 0) croak("sync: %s", strerror(errno));
Branch: 2 branches
BranchTrueFalseTotal
if (!h->readonly && kd_msync(h) != 0) croak("sync: %s", strerror(errno)); 2 1 3
if (!h->readonly && kd_msync(h) != 0) croak("sync: %s", strerror(errno)); 0 2 2
483  
484 void
485 unlink(self, ...)
486 SV *self
487 CODE:
488 1 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::KDTree::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::KDTree::Shared")) { 0 1 1
if (sv_isobject(self) && sv_derived_from(self, "Data::KDTree::Shared")) { 0 0 0
489 0 KdHandle *h = INT2PTR(KdHandle*, SvIV(SvRV(self)));
490 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
491 0 croak("Data::KDTree::Shared->unlink(%s): %s", h->path, strerror(errno));
492 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
493 {
494 1 const char *up = SvPV_nolen(ST(1));
495 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
496 0 croak("Data::KDTree::Shared->unlink(%s): %s", up, strerror(errno));
497 }
498 }
« kdtree.h ↑ Summary