include/hamt/hamt_xs.h

filter: Statementstmt 98.9% 362 / 366 Branchbran 85.7% 246 / 288 (1 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 93.1% 608 / 654 (1 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hamt/hamt_sv.h ↑ Summary include/hamt/hamt_ops.h »
1 #ifndef HAMT_XS_H
2 #define HAMT_XS_H
3  
4 /* Between the XSUBs and the core: what a handle is, what a key is, and how
5 * a value goes in. Comes after every core header. */
6  
7 /* ---- the handle --------------------------------------------------------
8 *
9 * A container is a blessed reference to a scalar that carries the C struct
10 * in ext magic, marked by the address of a vtable in this file.
11 *
12 * Not a blessed integer. A class whose objects are an address in an IV, with
13 * a destructor that frees it, will free whatever integer a caller blesses
14 * into it - and a deserialiser fed somebody else's bytes will do that
15 * blessing for them. Magic cannot be forged from Perl or carried through a
16 * serialiser: an object without it is simply not a container, and every
17 * method says so.
18 *
19 * The struct is released when the scalar is freed, by the vtable, so there
20 * is no DESTROY method to call twice or to shadow in a subclass.
21 *
22 * A new thread does not get the handle. CLONE_SKIP on each class keeps the
23 * object from being cloned at all; and if a clone is made some other way,
24 * the dup hook empties its magic, so it is dead rather than a second owner
25 * of nodes that hold the first interpreter's scalars. */
26  
27 89603 static int hamt_handle_free(pTHX_ SV *sv, MAGIC *mg) {
28 89603 hamt_map *m = (hamt_map *)mg->mg_ptr;
29 PERL_UNUSED_ARG(sv);
30 89603 ▶ if (m) {
Branch: 1 branch
BranchTrueFalseTotal
if (m) { 89603 0 89603
31 89603 hamt_node *root = m->root;
32 89603 mg->mg_ptr = NULL;
33 /* The struct goes before the trie: releasing the trie can run a
34 * value's DESTROY, and by then there must be nothing half-gone. */
35 89603 HAMT_FREE(HAMT_CX, m, sizeof(hamt_map));
36 89603 ▶ if (root) hamt_node_release(HAMT_CX, root);
Branch: 1 branch
BranchTrueFalseTotal
if (root) hamt_node_release(HAMT_CX, root); 88569 1034 89603
37 }
38 89603 return 0;
39 }
40  
41 0 static int hamt_handle_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) {
42 PERL_UNUSED_CONTEXT;
43 PERL_UNUSED_ARG(param);
44 0 mg->mg_ptr = NULL;
45 0 return 0;
46 }
47  
48 static MGVTBL hamt_map_vtbl = {
49 NULL, NULL, NULL, NULL, hamt_handle_free, NULL, hamt_handle_dup, NULL
50 };
51  
52 /* A set is the same struct over the same trie, with entries that carry no
53 * value. It has a vtable of its own so that a set is not a map: each class's
54 * methods look for their own, and refuse the other's objects by name. */
55 static MGVTBL hamt_set_vtbl = {
56 NULL, NULL, NULL, NULL, hamt_handle_free, NULL, hamt_handle_dup, NULL
57 };
58  
59 /* The struct behind an object, or a croak that names the method. `what` is
60 * the method, "HAMT::Map::get"; `kind` is the word for the thing, "map". */
61 1504026 static hamt_map *hamt_handle(pTHX_ SV *sv, const MGVTBL *vt, const char *what,
62 const char *kind) {
63 1504026 ▶ if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { /* # uncoverable condition right count:2 */ 1503991 35 1504026
if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { /* # uncoverable condition right count:2 */ 1503991 0 1503991
64 MAGIC *mg;
65 1504013 ▶ for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { 1503935 78 1504013
66 1503935 ▶ if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != vt) continue; /* # uncoverable condition left count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != vt) continue; /* # uncoverable condition left count:1 */ 1503935 0 1503935
if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != vt) continue; /* # uncoverable condition left count:1 */ 22 1503913 1503935
67 1503913 ▶ if (!mg->mg_ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (!mg->mg_ptr) 0 1503913 1503913
68 0 croak("%s: this %s belongs to another thread", what, kind);
69 1503913 return (hamt_map *)mg->mg_ptr;
70 }
71 }
72 113 croak("%s: not a %s", what, kind);
73 return NULL;
74 }
75  
76 /* A new object of the class behind `stash`, mortal, holding its own
77 * reference to `root`. */
78 88919 static SV *hamt_handle_new(pTHX_ const MGVTBL *vt, HV *stash, hamt_node *root,
79 size_t count, hamt_map **out, const char *what) {
80 88919 hamt_map *m = (hamt_map *)HAMT_ALLOC(HAMT_CX, sizeof(hamt_map));
81 SV *inner, *rv;
82 MAGIC *mg;
83  
84 88919 ▶ if (!m) croak("%s: out of memory", what);
Branch: 1 branch
BranchTrueFalseTotal
if (!m) croak("%s: out of memory", what); 49 88870 88919
85 88870 ▶ m->root = root ? hamt_node_retain(root) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
m->root = root ? hamt_node_retain(root) : NULL; 82590 6280 88870
86 88870 m->count = count;
87  
88 88870 inner = newSV(0);
89 88870 mg = sv_magicext(inner, NULL, PERL_MAGIC_ext, (MGVTBL *)vt, (const char *)m, 0);
90 88870 mg->mg_flags |= MGf_DUP;
91 88870 rv = sv_2mortal(newRV_noinc(inner));
92 88870 sv_bless(rv, stash);
93 88870 *out = m;
94 88870 return rv;
95 }
96  
97 /* The class to bless into: the one named, or the one an object is in. */
98 8100 static HV *hamt_class_stash(pTHX_ SV *klass) {
99 8100 ▶ if (SvROK(klass) && SvOBJECT(SvRV(klass))) return SvSTASH(SvRV(klass)); /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(klass) && SvOBJECT(SvRV(klass))) return SvSTASH(SvRV(klass)); /* # uncoverable condition right count:2 */ 3 8097 8100
if (SvROK(klass) && SvOBJECT(SvRV(klass))) return SvSTASH(SvRV(klass)); /* # uncoverable condition right count:2 */ 3 0 3
100 8097 return gv_stashsv(klass, GV_ADD);
101 }
102  
103 /* An update's result was thrown away. Lexical, category `void`. */
104 24 static void hamt_void_warn(pTHX_ const char *what) {
105 24 ▶ if (ckWARN(WARN_VOID))
Branch: 1 branch
BranchTrueFalseTotal
if (ckWARN(WARN_VOID)) 17 7 24
106 17 Perl_warner(aTHX_ packWARN(WARN_VOID),
107 "Useless use of %s in void context", what);
108 24 }
109  
110 /* ---- keys --------------------------------------------------------------
111 *
112 * A key is a string, and two keys are one key exactly when a Perl hash
113 * would say so. A hash treats the Latin-1 and the UTF-8 spelling of a
114 * string as the same key, by downgrading every key that can be; so does
115 * this. What reaches the core is bytes and a flag, and the flag is set only
116 * for a key with a character above 0xFF, which cannot be bytes.
117 *
118 * The scalar is read once: get magic runs, an object's string overload
119 * runs, undef warns as it does anywhere. */
120  
121 typedef struct hamt_key {
122 const char *p;
123 STRLEN len;
124 int utf8;
125 uint64_t hash;
126 } hamt_key;
127  
128 2269300 static void hamt_key_from_sv(pTHX_ SV *sv, hamt_key *k) {
129 STRLEN len;
130 2269299 const char *p = SvPV(sv, len);
131 2269299 int utf8 = 0;
132  
133 2269299 ▶ if (SvUTF8(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(sv)) { 28 2269271 2269299
134 28 const U8 *s = (const U8 *)p, *e = s + len;
135 36 ▶ while (s < e && !(*s & 0x80)) s++;
Branch: 2 branches
BranchTrueFalseTotal
while (s < e && !(*s & 0x80)) s++; 35 1 36
while (s < e && !(*s & 0x80)) s++; 8 27 35
136 28 ▶ if (s < e) {
Branch: 1 branch
BranchTrueFalseTotal
if (s < e) { 27 1 28
137 /* A copy, because downgrading in place would change the
138 * caller's scalar. Mortal, so a croak later cannot leak it. */
139 27 SV *tmp = sv_2mortal(newSVpvn(p, len));
140 27 SvUTF8_on(tmp);
141 27 ▶ if (sv_utf8_downgrade(tmp, TRUE))
Branch: 1 branch
BranchTrueFalseTotal
if (sv_utf8_downgrade(tmp, TRUE)) 11 16 27
142 11 p = SvPV(tmp, len);
143 else
144 16 utf8 = 1;
145 }
146 }
147 2269299 k->p = p;
148 2269299 k->len = len;
149 2269299 k->utf8 = utf8;
150 2269299 k->hash = hamt_hash(p, (size_t)len, utf8);
151 2269299 }
152  
153 /* HeUTF8 arrived in 5.10.1 and the floor is 5.10.0. This is ppport.h's. */
154 #ifndef HeUTF8
155 # define HeUTF8(he) ((HeKLEN(he) == HEf_SVKEY) ? SvUTF8(HeKEY_sv(he)) : (U32)HeKUTF8(he))
156 #endif
157  
158 /* The key of a hash entry. A hash already keeps its keys by the rule this
159 * library keeps them by - downgraded wherever they can be - so the bytes
160 * and the flag are taken as they stand, with no scalar made to hold them.
161 * A tied hash hands its key over as a scalar, and that goes the long way. */
162 69976 static void hamt_key_from_he(pTHX_ HV *hv, HE *he, hamt_key *k) {
163 PERL_UNUSED_ARG(hv);
164 69976 ▶ if (HeKLEN(he) == HEf_SVKEY) { hamt_key_from_sv(aTHX_ HeKEY_sv(he), k); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (HeKLEN(he) == HEf_SVKEY) { hamt_key_from_sv(aTHX_ HeKEY_sv(he), k); return; } 3 69973 69976
165 69973 k->p = HeKEY(he);
166 69973 k->len = (STRLEN)HeKLEN(he);
167 69973 k->utf8 = HeKUTF8(he) ? 1 : 0;
168 69973 k->hash = hamt_hash(k->p, (size_t)k->len, k->utf8);
169 }
170  
171 1876514 static SV *hamt_key_to_sv(pTHX_ const hamt_entry *e) {
172 1876514 SV *sv = newSVpvn(e->key, (STRLEN)e->klen);
173 1876514 ▶ if (e->utf8) SvUTF8_on(sv);
Branch: 1 branch
BranchTrueFalseTotal
if (e->utf8) SvUTF8_on(sv); 9 1876505 1876514
174 1876514 return sv;
175 }
176  
177 /* ---- values ------------------------------------------------------------
178 *
179 * A value is copied on the way in and the copy is made read-only. Every
180 * version of the container that holds the entry shares that one scalar, and
181 * `get` hands out the scalar itself, so the flag is what makes "this map
182 * never changes" true for a caller holding an alias to a value.
183 *
184 * It is shallow. A stored reference is a read-only reference to something
185 * the caller can still change.
186 *
187 * Before 5.20 a copy-on-write string was marked with the read-only flag
188 * and another beside it, and a scalar carrying both was WRITABLE: perl
189 * unshared it at the first write. If the copy made here could come back
190 * like that, the flag would already be on and would mean nothing. It cannot:
191 * newSVsv gives the copy a buffer of its own even when the source is a
192 * shared hash key, on every perl from 5.10. That was measured rather than
193 * assumed, and t/04-readonly.t reads the stored scalar's flags so that it
194 * stays measured. */
195 1322436 static SV *hamt_sv_copy_in(pTHX_ SV *in) {
196 1322436 SV *c = newSVsv(in);
197 1322431 SvREADONLY_on(c);
198 1322431 return c;
199 }
200  
201 /* One pair into a handle the caller owns outright: one nobody else has seen
202 * yet, or a transient. The trie is changed where it stands wherever the
203 * handle is its only holder, and copied where it is shared, so a handle
204 * that starts out sharing a root with the map it came from leaves that map
205 * alone. The entry it displaces is released last, when the handle already
206 * holds the new root: releasing it can run a value's DESTROY, and that may
207 * use this very handle.
208 *
209 * A value that IS the scalar already stored under the key - which is what
210 * `$m->set(k => $m->get('k'))` passes - is recognised before anything is
211 * copied, and nothing changes. A member already in a set is recognised by
212 * the trie itself, which gives the same root back. */
213 1050961 static void hamt_xs_put_key(pTHX_ hamt_map *m, const hamt_key *kp, SV *valsv,
214 const char *what) {
215 1050961 hamt_key k = *kp;
216 hamt_entry *e, *old, *dead;
217 SV *copy;
218 int added, rc;
219  
220 /* Is this the very scalar already stored under the key? Only a scalar
221 * that is read-only can be, since every stored one is; so only then is
222 * it worth a walk of the trie to find out. */
223 1050961 ▶ if (valsv && SvREADONLY(valsv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (valsv && SvREADONLY(valsv)) { 481420 569541 1050961
if (valsv && SvREADONLY(valsv)) { 1853 479567 481420
224 1853 old = hamt_map_find(m->root, k.p, (size_t)k.len, k.utf8, k.hash);
225 1853 ▶ if (old && old->val == (void *)valsv) return;
Branch: 2 branches
BranchTrueFalseTotal
if (old && old->val == (void *)valsv) return; 150 1703 1853
if (old && old->val == (void *)valsv) return; 4 146 150
226 }
227  
228 1050957 ▶ copy = valsv ? hamt_sv_copy_in(aTHX_ valsv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
copy = valsv ? hamt_sv_copy_in(aTHX_ valsv) : NULL; 481416 569541 1050957
229 1050954 e = hamt_entry_new(HAMT_CX, k.p, (size_t)k.len, k.utf8, k.hash, (void *)copy);
230 1050954 ▶ if (copy) SvREFCNT_dec(copy);
Branch: 1 branch
BranchTrueFalseTotal
if (copy) SvREFCNT_dec(copy); 481413 569541 1050954
231 1050954 ▶ if (!e) croak("%s: out of memory", what);
Branch: 1 branch
BranchTrueFalseTotal
if (!e) croak("%s: out of memory", what); 1971 1048983 1050954
232  
233 1048983 rc = hamt_map_put_mut(HAMT_CX, &m->root, e, &added, &dead);
234 1048983 hamt_entry_release(HAMT_CX, e);
235 1048983 ▶ if (rc) croak("%s: out of memory", what);
Branch: 1 branch
BranchTrueFalseTotal
if (rc) croak("%s: out of memory", what); 988 1047995 1048983
236  
237 1047995 m->count += (size_t)added;
238 1047995 ▶ if (dead) hamt_entry_release(HAMT_CX, dead);
Branch: 1 branch
BranchTrueFalseTotal
if (dead) hamt_entry_release(HAMT_CX, dead); 62432 985563 1047995
239 }
240  
241 917361 static void hamt_xs_put(pTHX_ hamt_map *m, SV *keysv, SV *valsv, const char *what) {
242 hamt_key k;
243 917361 hamt_key_from_sv(aTHX_ keysv, &k);
244 917361 hamt_xs_put_key(aTHX_ m, &k, valsv, what);
245 915255 }
246  
247 /* One key out of a handle the caller owns outright, the same way. */
248 62210 static void hamt_xs_del(pTHX_ hamt_map *m, SV *keysv, const char *what) {
249 hamt_key k;
250 hamt_entry *dead;
251 int status;
252  
253 62210 hamt_key_from_sv(aTHX_ keysv, &k);
254 62210 status = hamt_map_del_mut(HAMT_CX, &m->root, k.p, (size_t)k.len, k.utf8, k.hash, &dead);
255 62210 ▶ if (status == HAMT_DEL_OOM) croak("%s: out of memory", what);
Branch: 1 branch
BranchTrueFalseTotal
if (status == HAMT_DEL_OOM) croak("%s: out of memory", what); 257 61953 62210
256 61953 ▶ if (status == HAMT_DEL_DONE) m->count--;
Branch: 1 branch
BranchTrueFalseTotal
if (status == HAMT_DEL_DONE) m->count--; 42615 19338 61953
257 61953 ▶ if (dead) hamt_entry_release(HAMT_CX, dead);
Branch: 1 branch
BranchTrueFalseTotal
if (dead) hamt_entry_release(HAMT_CX, dead); 42615 19338 61953
258 61953 }
259  
260 /* ---- calling back into Perl -------------------------------------------
261 *
262 * A method that calls a sub while it walks holds its own reference to every
263 * root it is walking, released by the save stack. The receiver's reference
264 * is not enough: the argument stack does not own what is on it, so a
265 * callback that drops the last variable holding the map would free the trie
266 * under the walk; and the save stack rather than code after the call,
267 * because the sub may die.
268 *
269 * Each call gets a temps frame of its own, so a walk over a million entries
270 * holds one call's mortals at a time and not a million calls'. */
271  
272 18892 static void hamt_xs_root_unref(pTHX_ void *p) {
273 18892 hamt_node_release(HAMT_CX, (hamt_node *)p);
274 18892 }
275  
276 /* Between an ENTER and its LEAVE. */
277 16763 static void hamt_xs_hold_root(pTHX_ hamt_node *root) {
278 16763 ▶ if (!root) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!root) return; 623 16140 16763
279 16140 hamt_node_retain(root);
280 16140 SAVEDESTRUCTOR_X(hamt_xs_root_unref, root);
281 }
282  
283 /* A walk hands its sub a key, or an index, in a scalar. Making a new one
284 * for every call is most of what a walk costs beyond the call itself, so
285 * one is made for the walk and written over each time - unless the sub
286 * kept hold of it, or did something to it that writing over would not
287 * undo, in which case that one is the sub's and the walk takes another. So
288 * a sub always sees a scalar that is its own to keep or change, as it
289 * would if each were fresh. The scalar is a temporary of the walk's frame. */
290 18852 static SV *hamt_xs_scratch(pTHX_ SV *sv) {
291 /*
292 # uncoverable condition right count:4
293 # uncoverable condition right count:5 */
294 18852 ▶ if (sv && SvREFCNT(sv) == 1 && !SvMAGICAL(sv) && !SvREADONLY(sv) && !SvROK(sv)) return sv;
Branch: 5 branches
BranchTrueFalseTotal
if (sv && SvREFCNT(sv) == 1 && !SvMAGICAL(sv) && !SvREADONLY(sv) && !SvROK(sv)) return sv; 18767 85 18852
if (sv && SvREFCNT(sv) == 1 && !SvMAGICAL(sv) && !SvREADONLY(sv) && !SvROK(sv)) return sv; 17528 1239 18767
if (sv && SvREFCNT(sv) == 1 && !SvMAGICAL(sv) && !SvREADONLY(sv) && !SvROK(sv)) return sv; 17527 1 17528
if (sv && SvREFCNT(sv) == 1 && !SvMAGICAL(sv) && !SvREADONLY(sv) && !SvROK(sv)) return sv; 17527 0 17527
if (sv && SvREFCNT(sv) == 1 && !SvMAGICAL(sv) && !SvREADONLY(sv) && !SvROK(sv)) return sv; 17527 0 17527
295 1325 return sv_2mortal(newSV(0));
296 }
297  
298 1932 static void hamt_xs_need_code(pTHX_ SV *code, const char *what) {
299 1932 SvGETMAGIC(code);
300 1932 ▶ if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV) 1924 8 1932
if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV) 1 1923 1924
301 9 croak("%s: not a code reference", what);
302 1923 }
303  
304 409454 static SV *hamt_xs_val_sv(pTHX_ const hamt_entry *e) {
305 409454 ▶ return (e && e->val) ? (SV *)e->val : sv_newmortal(); /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
return (e && e->val) ? (SV *)e->val : sv_newmortal(); /* # uncoverable condition right count:2 */ 221181 188273 409454
return (e && e->val) ? (SV *)e->val : sv_newmortal(); /* # uncoverable condition right count:2 */ 221181 0 221181
306 }
307  
308 /* ---- calling a sub -------------------------------------------------------
309 *
310 * Every sub HAMT is given is called on a stack of its own, as perl's sort
311 * calls its comparison. Without that, `last` or `next` inside the sub finds
312 * the caller's loop on the context stack and jumps to it straight through
313 * this C, which never gets control back. On a stack of its own there is no
314 * loop to find, so either is an error, caught by G_EVAL like a die; a walk
315 * reads it as "stop" or "skip", and anything else throws it again.
316 *
317 * hamt_xs_call_enter switches to the stack, with $@ localised so that calls
318 * that work leave the caller's $@ as it was; hamt_xs_call_leave switches
319 * back. A walk does that once around all its calls. */
320 #define HAMT_XS_RETURNED 0
321 #define HAMT_XS_LAST 1
322 #define HAMT_XS_NEXT 2
323 #define HAMT_XS_DIED 3
324  
325 4235 static void hamt_xs_call_enter(pTHX) {
326 4235 dSP;
327 4235 ENTER;
328 4235 save_scalar(PL_errgv);
329 4235 PUSHSTACKi(PERLSI_UNKNOWN);
330 PERL_UNUSED_VAR(sp);
331 4235 }
332  
333 4235 static void hamt_xs_call_leave(pTHX) {
334 4235 dSP;
335 4235 POPSTACK;
336 PERL_UNUSED_VAR(sp);
337 4235 LEAVE;
338 4235 }
339  
340 /* A walk takes `last` and `next` from its sub, so it enters with a warning
341 * filter in $SIG{__WARN__}: perl warns "Exiting eval via last" about the
342 * G_EVAL frame, which is this library's and not the sub's. The filter drops
343 * that and hands everything else to the hook it replaced, kept in
344 * $HAMT::_warn_prev, or to perl. "Exiting subroutine via last" is perl's
345 * warning for any sub that does it, and is kept. */
346 #ifndef PERL_WARNHOOK_FATAL
347 # define PERL_WARNHOOK_FATAL (&PL_sv_placeholder)
348 #endif
349  
350 1336 static void hamt_xs_walk_enter(pTHX) {
351 1336 CV *filter = get_cv("HAMT::_warn_filter", 0);
352 1336 hamt_xs_call_enter(aTHX);
353 /*
354 # uncoverable condition right count:1
355 # uncoverable condition right count:2
356 # uncoverable condition left count:4 */
357 1336 ▶ if (filter && PL_warnhook != PERL_WARNHOOK_FATAL
Branch: 2 branches
BranchTrueFalseTotal
if (filter && PL_warnhook != PERL_WARNHOOK_FATAL 1336 0 1336
if (filter && PL_warnhook != PERL_WARNHOOK_FATAL 1336 0 1336
358 1336 ▶ && !(PL_warnhook && SvROK(PL_warnhook) && SvRV(PL_warnhook) == (SV *)filter)) {
Branch: 3 branches
BranchTrueFalseTotal
&& !(PL_warnhook && SvROK(PL_warnhook) && SvRV(PL_warnhook) == (SV *)filter)) { 27 1309 1336
&& !(PL_warnhook && SvROK(PL_warnhook) && SvRV(PL_warnhook) == (SV *)filter)) { 27 0 27
&& !(PL_warnhook && SvROK(PL_warnhook) && SvRV(PL_warnhook) == (SV *)filter)) { 24 3 27
359 1333 GV *gv = gv_fetchpvs("HAMT::_warn_prev", GV_ADD, SVt_PV);
360 1333 save_scalar(gv);
361 1333 ▶ if (PL_warnhook) sv_setsv(GvSV(gv), PL_warnhook);
Branch: 1 branch
BranchTrueFalseTotal
if (PL_warnhook) sv_setsv(GvSV(gv), PL_warnhook); 24 1309 1333
362 1333 SAVEGENERICSV(PL_warnhook);
363 1333 PL_warnhook = newRV_inc((SV *)filter);
364 }
365 1336 }
366  
367 1722 static int hamt_xs_loop_error(pTHX_ SV *err, const char *what, STRLEN len) {
368 STRLEN elen;
369 const char *p;
370 1722 ▶ if (SvROK(err)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(err)) return 0; 4 1718 1722
371 1718 p = SvPV(err, elen);
372 1718 ▶ return elen >= len && memEQ(p, what, len);
Branch: 2 branches
BranchTrueFalseTotal
return elen >= len && memEQ(p, what, len); 1590 128 1718
return elen >= len && memEQ(p, what, len); 1406 184 1590
373 }
374  
375 #define HAMT_XS_ERR_LAST "Can't \"last\" outside a loop block"
376 #define HAMT_XS_ERR_NEXT "Can't \"next\" outside a loop block"
377  
378 /* Calls the sub with the arguments, between hamt_xs_call_enter and _leave.
379 * When `res` is given the call is in scalar context and the result is put
380 * there, a temporary of the caller's frame. A die leaves its error in $@. */
381 220735 static int hamt_xs_call(pTHX_ SV *code, SV **args, int nargs, SV **res) {
382 220735 dSP;
383 I32 n;
384 int i;
385 SV *err;
386  
387 220735 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 220735 220735
388 220735 ▶ EXTEND(SP, nargs);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, nargs); 220735 0 220735
EXTEND(SP, nargs); 0 220735 220735
389 1061271 ▶ for (i = 0; i < nargs; i++) PUSHs(args[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nargs; i++) PUSHs(args[i]); 840536 220735 1061271
390 220735 PUTBACK;
391 220735 ▶ n = call_sv(code, (res ? G_SCALAR : G_DISCARD | G_VOID) | G_EVAL);
Branch: 1 branch
BranchTrueFalseTotal
n = call_sv(code, (res ? G_SCALAR : G_DISCARD | G_VOID) | G_EVAL); 2899 217836 220735
392 220735 SPAGAIN;
393 220735 ▶ if (res) *res = n > 0 ? *SP : &PL_sv_undef; /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (res) *res = n > 0 ? *SP : &PL_sv_undef; /* # uncoverable condition right count:2 */ 2899 217836 220735
if (res) *res = n > 0 ? *SP : &PL_sv_undef; /* # uncoverable condition right count:2 */ 2899 0 2899
394 220735 SP -= n;
395 220735 PUTBACK;
396 220735 ▶ err = ERRSV;
Branch: 1 branch
BranchTrueFalseTotal
err = ERRSV; 220735 0 220735
397 220735 ▶ if (!SvTRUE(err)) return HAMT_XS_RETURNED;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvTRUE(err)) return HAMT_XS_RETURNED; 219259 1476 220735
398 1476 ▶ if (hamt_xs_loop_error(aTHX_ err, HAMT_XS_ERR_LAST, sizeof(HAMT_XS_ERR_LAST) - 1))
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_xs_loop_error(aTHX_ err, HAMT_XS_ERR_LAST, sizeof(HAMT_XS_ERR_LAST) - 1)) 1230 246 1476
399 1230 return HAMT_XS_LAST;
400 246 ▶ if (hamt_xs_loop_error(aTHX_ err, HAMT_XS_ERR_NEXT, sizeof(HAMT_XS_ERR_NEXT) - 1))
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_xs_loop_error(aTHX_ err, HAMT_XS_ERR_NEXT, sizeof(HAMT_XS_ERR_NEXT) - 1)) 176 70 246
401 176 return HAMT_XS_NEXT;
402 70 return HAMT_XS_DIED;
403 }
404  
405 /* A copy of $@ that outlives hamt_xs_call_leave, or NULL when the call
406 * returned or a walk takes `last` and `next` as stop and skip. */
407 220720 static SV *hamt_xs_call_err(pTHX_ int how, int walk) {
408 220720 ▶ if (how == HAMT_XS_RETURNED || (walk && how != HAMT_XS_DIED)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (how == HAMT_XS_RETURNED || (walk && how != HAMT_XS_DIED)) return NULL; 1461 219259 220720
if (how == HAMT_XS_RETURNED || (walk && how != HAMT_XS_DIED)) return NULL; 1418 43 1461
if (how == HAMT_XS_RETURNED || (walk && how != HAMT_XS_DIED)) return NULL; 1381 37 1418
409 80 ▶ return newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
return newSVsv(ERRSV); 80 0 80
410 }
411  
412 /* Throws a copy from hamt_xs_call_err, object or string, exactly as it was. */
413 73 static void hamt_xs_rethrow(pTHX_ SV *err) {
414 73 ▶ sv_setsv(ERRSV, err);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, err); 73 0 73
415 73 SvREFCNT_dec(err);
416 73 croak(NULL);
417 }
418  
419 /* equals with a comparison sub: called with the two values, true or false.
420 * Nothing is allocated across the call, so an error is thrown again from
421 * here, through the comparison. */
422 448 static int hamt_xs_eq_cb(void *ud, void *cx, void *va, void *vb) {
423 dTHXa(cx);
424 SV *args[2], *res, *err;
425 int truth;
426  
427 PERL_UNUSED_ARG(cx);
428 448 args[0] = (SV *)va;
429 448 args[1] = (SV *)vb;
430 448 ENTER;
431 448 SAVETMPS;
432 448 hamt_xs_call_enter(aTHX);
433 448 err = hamt_xs_call_err(aTHX_ hamt_xs_call(aTHX_ (SV *)ud, args, 2, &res), 0);
434 448 ▶ truth = !err && SvTRUE(res) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
truth = !err && SvTRUE(res) ? 1 : 0; 437 11 448
truth = !err && SvTRUE(res) ? 1 : 0; 436 1 437
435 448 hamt_xs_call_leave(aTHX);
436 448 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 448 0 448
437 448 LEAVE;
438 448 ▶ if (err) hamt_xs_rethrow(aTHX_ err);
Branch: 1 branch
BranchTrueFalseTotal
if (err) hamt_xs_rethrow(aTHX_ err); 11 437 448
439 437 return truth;
440 }
441  
442 /* merge with a resolver: called with the key and the two values, and what
443 * it returns is the value the result holds.
444 *
445 * This one runs under G_EVAL. The merge is half way through building nodes
446 * when it calls, and a die that jumped out past it would leak every one of
447 * them; so the error is caught and kept, the merge is told to stop and
448 * unwinds what it built, and the caller dies again with the same error.
449 * diff keeps its error the same way. */
450 typedef struct hamt_xs_sub {
451 SV *code;
452 SV *err;
453 } hamt_xs_sub;
454  
455 2380 static int hamt_xs_resolve_cb(void *ud, void *cx, hamt_entry *ea, hamt_entry *eb,
456 hamt_entry **out) {
457 dTHXa(cx);
458 2380 hamt_xs_sub *r = (hamt_xs_sub *)ud;
459 SV *args[3], *res;
460 2380 int rc = HAMT_OK;
461  
462 2380 ENTER;
463 2380 SAVETMPS;
464 2380 args[0] = sv_2mortal(hamt_key_to_sv(aTHX_ ea));
465 2380 args[1] = hamt_xs_val_sv(aTHX_ ea);
466 2380 args[2] = hamt_xs_val_sv(aTHX_ eb);
467 2380 hamt_xs_call_enter(aTHX);
468 2380 r->err = hamt_xs_call_err(aTHX_ hamt_xs_call(aTHX_ r->code, args, 3, &res), 0);
469 2380 hamt_xs_call_leave(aTHX);
470  
471 2380 ▶ if (r->err) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) { 7 2373 2380
472 7 rc = HAMT_ERR_ABORT;
473 }
474 /* What a sub returns is always a copy, so "it gave back the one it was
475 * handed" cannot be seen by address. It is seen by value, as `equals`
476 * compares values, and then the entry already there is kept: that is
477 * what lets a merge that changes nothing return the map it started from. */
478 2373 ▶ else if (ea->val && hamt_sv_eq_cx(cx, res, (SV *)ea->val)) *out = hamt_entry_retain(ea); /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
else if (ea->val && hamt_sv_eq_cx(cx, res, (SV *)ea->val)) *out = hamt_entry_retain(ea); /* # uncoverable condition right count:1 */ 2373 0 2373
else if (ea->val && hamt_sv_eq_cx(cx, res, (SV *)ea->val)) *out = hamt_entry_retain(ea); /* # uncoverable condition right count:1 */ 103 2270 2373
479 2270 ▶ else if (eb->val && hamt_sv_eq_cx(cx, res, (SV *)eb->val)) *out = hamt_entry_retain(eb); /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
else if (eb->val && hamt_sv_eq_cx(cx, res, (SV *)eb->val)) *out = hamt_entry_retain(eb); /* # uncoverable condition right count:1 */ 2270 0 2270
else if (eb->val && hamt_sv_eq_cx(cx, res, (SV *)eb->val)) *out = hamt_entry_retain(eb); /* # uncoverable condition right count:1 */ 501 1769 2270
480 else {
481 1769 SV *copy = hamt_sv_copy_in(aTHX_ res);
482 1769 *out = hamt_entry_new(cx, ea->key, ea->klen, ea->utf8, ea->hash, (void *)copy);
483 1769 SvREFCNT_dec(copy);
484 1769 ▶ if (!*out) rc = HAMT_ERR_OOM;
Branch: 1 branch
BranchTrueFalseTotal
if (!*out) rc = HAMT_ERR_OOM; 11 1758 1769
485 }
486  
487 2380 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 2380 0 2380
488 2380 LEAVE;
489 2380 return rc;
490 }
491  
492 /* diff: called with the key, the value it had, the value it has, and one of
493 * 'add', 'remove', 'change'. A walk: `last` stops it, `next` goes on. The
494 * caller is between hamt_xs_call_enter and _leave; an error stops the walk
495 * and is kept for the caller to throw once it has left. */
496 198984 static int hamt_xs_diff_cb(void *ud, void *cx, int kind, hamt_entry *ea,
497 hamt_entry *eb) {
498 dTHXa(cx);
499 static const char *const name[4] = { "", "add", "remove", "change" };
500 198984 hamt_xs_sub *r = (hamt_xs_sub *)ud;
501 SV *args[4];
502 int how;
503  
504 PERL_UNUSED_ARG(cx);
505 198984 ENTER;
506 198984 SAVETMPS;
507 198984 ▶ args[0] = sv_2mortal(hamt_key_to_sv(aTHX_ ea ? ea : eb));
Branch: 1 branch
BranchTrueFalseTotal
args[0] = sv_2mortal(hamt_key_to_sv(aTHX_ ea ? ea : eb)); 103847 95137 198984
508 198984 args[1] = hamt_xs_val_sv(aTHX_ ea);
509 198984 args[2] = hamt_xs_val_sv(aTHX_ eb);
510 198984 args[3] = sv_2mortal(newSVpv(name[kind], 0));
511 198984 how = hamt_xs_call(aTHX_ r->code, args, 4, NULL);
512 198984 r->err = hamt_xs_call_err(aTHX_ how, 1);
513 198984 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 198984 0 198984
514 198984 LEAVE;
515 198984 ▶ return how == HAMT_XS_LAST || r->err;
Branch: 2 branches
BranchTrueFalseTotal
return how == HAMT_XS_LAST || r->err; 197774 1210 198984
return how == HAMT_XS_LAST || r->err; 3 197771 197774
516 }
517  
518 /* The struct behind an object, or NULL: for a method that takes "something
519 * that may be a map" and has an answer when it is not. */
520 5026 static hamt_map *hamt_handle_try(pTHX_ SV *sv, const MGVTBL *vt) {
521 5026 ▶ if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { /* # uncoverable condition right count:2 */ 4497 529 5026
if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { /* # uncoverable condition right count:2 */ 4497 0 4497
522 MAGIC *mg;
523 5752 ▶ for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic)
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) 4494 1258 5752
524 4494 ▶ if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == vt) /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == vt) /* # uncoverable condition right count:1 */ 4494 0 4494
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == vt) /* # uncoverable condition right count:1 */ 3239 1255 4494
525 3239 return (hamt_map *)mg->mg_ptr;
526 }
527 1787 return NULL;
528 }
529  
530 /* ---- transients ---------------------------------------------------------
531 *
532 * A transient is a handle that IS updated in place: the same put and delete
533 * as everywhere else, on a handle that lives across calls. It starts out
534 * sharing its root with the map it was opened from, so its first updates
535 * copy and that map is untouched; `persistent` hands the root to a new map
536 * and closes the transient, so nothing can reach the root through it again.
537 *
538 * It remembers the class of the map it came from, to make its result one. */
539  
540 typedef struct hamt_txn {
541 hamt_map m; /* first, so a transient can be updated as a map is */
542 HV *origin; /* the class `persistent` blesses into */
543 int open;
544 } hamt_txn;
545  
546 1479 static int hamt_txn_free(pTHX_ SV *sv, MAGIC *mg) {
547 1479 hamt_txn *t = (hamt_txn *)mg->mg_ptr;
548 PERL_UNUSED_ARG(sv);
549 1479 ▶ if (t) {
Branch: 1 branch
BranchTrueFalseTotal
if (t) { 1479 0 1479
550 1479 hamt_node *root = t->m.root;
551 1479 HV *origin = t->origin;
552 1479 mg->mg_ptr = NULL;
553 1479 HAMT_FREE(HAMT_CX, t, sizeof(hamt_txn));
554 1479 ▶ if (origin && !PL_dirty) SvREFCNT_dec((SV *)origin); /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (origin && !PL_dirty) SvREFCNT_dec((SV *)origin); /* # uncoverable condition right count:2 */ 1254 225 1479
if (origin && !PL_dirty) SvREFCNT_dec((SV *)origin); /* # uncoverable condition right count:2 */ 1254 0 1254
555 1479 ▶ if (root) hamt_node_release(HAMT_CX, root);
Branch: 1 branch
BranchTrueFalseTotal
if (root) hamt_node_release(HAMT_CX, root); 1253 226 1479
556 }
557 1479 return 0;
558 }
559  
560 static MGVTBL hamt_map_txn_vtbl = {
561 NULL, NULL, NULL, NULL, hamt_txn_free, NULL, hamt_handle_dup, NULL
562 };
563  
564 static MGVTBL hamt_set_txn_vtbl = {
565 NULL, NULL, NULL, NULL, hamt_txn_free, NULL, hamt_handle_dup, NULL
566 };
567  
568 /* The struct behind a transient that is still open, or a croak. */
569 252475 static hamt_txn *hamt_txn_handle(pTHX_ SV *sv, const MGVTBL *vt, const char *what) {
570 252475 ▶ if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { /* # uncoverable condition right count:2 */ 252473 2 252475
if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { /* # uncoverable condition right count:2 */ 252473 0 252473
571 MAGIC *mg;
572 252475 ▶ for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { 252470 5 252475
573 hamt_txn *t;
574 252470 ▶ if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != vt) continue; /* # uncoverable condition left count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != vt) continue; /* # uncoverable condition left count:1 */ 252470 0 252470
if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != vt) continue; /* # uncoverable condition left count:1 */ 2 252468 252470
575 252468 t = (hamt_txn *)mg->mg_ptr;
576 252468 ▶ if (!t) croak("%s: this transient belongs to another thread", what);
Branch: 1 branch
BranchTrueFalseTotal
if (!t) croak("%s: this transient belongs to another thread", what); 0 252468 252468
577 252468 ▶ if (!t->open) croak("%s: this transient is closed", what);
Branch: 1 branch
BranchTrueFalseTotal
if (!t->open) croak("%s: this transient is closed", what); 12 252456 252468
578 252456 return t;
579 }
580 }
581 7 croak("%s: not a transient", what);
582 return NULL;
583 }
584  
585 769 static SV *hamt_txn_new(pTHX_ const MGVTBL *vt, const char *klass, HV *origin,
586 hamt_node *root, size_t count, const char *what) {
587 769 hamt_txn *t = (hamt_txn *)HAMT_ALLOC(HAMT_CX, sizeof(hamt_txn));
588 SV *inner, *rv;
589 MAGIC *mg;
590  
591 769 ▶ if (!t) croak("%s: out of memory", what);
Branch: 1 branch
BranchTrueFalseTotal
if (!t) croak("%s: out of memory", what); 3 766 769
592 766 ▶ t->m.root = root ? hamt_node_retain(root) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
t->m.root = root ? hamt_node_retain(root) : NULL; 756 10 766
593 766 t->m.count = count;
594 766 t->origin = (HV *)SvREFCNT_inc_simple_NN((SV *)origin);
595 766 t->open = 1;
596  
597 766 inner = newSV(0);
598 766 mg = sv_magicext(inner, NULL, PERL_MAGIC_ext, (MGVTBL *)vt, (const char *)t, 0);
599 766 mg->mg_flags |= MGf_DUP;
600 766 rv = sv_2mortal(newRV_noinc(inner));
601 766 sv_bless(rv, gv_stashpv(klass, GV_ADD));
602 766 return rv;
603 }
604  
605 /* ---- the vector's handle -------------------------------------------------
606 *
607 * The same arrangement as a map's, around a hamt_vec: a blessed reference
608 * whose scalar carries the struct in magic marked by a vtable of its own. */
609  
610 259976 static int hamt_vec_handle_free(pTHX_ SV *sv, MAGIC *mg) {
611 259976 hamt_vec *v = (hamt_vec *)mg->mg_ptr;
612 PERL_UNUSED_ARG(sv);
613 259976 ▶ if (v) {
Branch: 1 branch
BranchTrueFalseTotal
if (v) { 259976 0 259976
614 259976 hamt_vec gone = *v;
615 259976 mg->mg_ptr = NULL;
616 259976 HAMT_FREE(HAMT_CX, v, sizeof(hamt_vec));
617 259976 hamt_vec_clear(HAMT_CX, &gone);
618 }
619 259976 return 0;
620 }
621  
622 static MGVTBL hamt_vec_vtbl = {
623 NULL, NULL, NULL, NULL, hamt_vec_handle_free, NULL, hamt_handle_dup, NULL
624 };
625  
626 686928 static hamt_vec *hamt_vec_handle(pTHX_ SV *sv, const char *what) {
627 686928 ▶ if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { /* # uncoverable condition right count:2 */ 686905 23 686928
if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { /* # uncoverable condition right count:2 */ 686905 0 686905
628 MAGIC *mg;
629 686918 ▶ for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { 686867 51 686918
630 686867 ▶ if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &hamt_vec_vtbl) continue; /* # uncoverable condition left count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &hamt_vec_vtbl) continue; /* # uncoverable condition left count:1 */ 686867 0 686867
if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &hamt_vec_vtbl) continue; /* # uncoverable condition left count:1 */ 13 686854 686867
631 686854 ▶ if (!mg->mg_ptr) croak("%s: this vector belongs to another thread", what);
Branch: 1 branch
BranchTrueFalseTotal
if (!mg->mg_ptr) croak("%s: this vector belongs to another thread", what); 0 686854 686854
632 686854 return (hamt_vec *)mg->mg_ptr;
633 }
634 }
635 74 croak("%s: not a vector", what);
636 return NULL;
637 }
638  
639 604 static hamt_vec *hamt_vec_handle_try(pTHX_ SV *sv) {
640 604 ▶ if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { /* # uncoverable condition right count:2 */ 602 2 604
if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { /* # uncoverable condition right count:2 */ 602 0 602
641 MAGIC *mg;
642 1138 ▶ for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic)
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) 601 537 1138
643 601 ▶ if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &hamt_vec_vtbl) /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &hamt_vec_vtbl) /* # uncoverable condition right count:1 */ 601 0 601
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &hamt_vec_vtbl) /* # uncoverable condition right count:1 */ 65 536 601
644 65 return (hamt_vec *)mg->mg_ptr;
645 }
646 539 return NULL;
647 }
648  
649 /* A new object, mortal, onto the same vector as `src`, or onto an empty one. */
650 263314 static SV *hamt_vec_handle_new(pTHX_ HV *stash, const hamt_vec *src, hamt_vec **out,
651 const char *what) {
652 263314 hamt_vec *v = (hamt_vec *)HAMT_ALLOC(HAMT_CX, sizeof(hamt_vec));
653 SV *inner, *rv;
654 MAGIC *mg;
655  
656 263314 ▶ if (!v) croak("%s: out of memory", what);
Branch: 1 branch
BranchTrueFalseTotal
if (!v) croak("%s: out of memory", what); 3360 259954 263314
657 259954 ▶ if (src) hamt_vec_share(v, src);
Branch: 1 branch
BranchTrueFalseTotal
if (src) hamt_vec_share(v, src); 164980 94974 259954
658 94974 else hamt_vec_init(v);
659  
660 259954 inner = newSV(0);
661 259954 mg = sv_magicext(inner, NULL, PERL_MAGIC_ext, &hamt_vec_vtbl, (const char *)v, 0);
662 259954 mg->mg_flags |= MGf_DUP;
663 259954 rv = sv_2mortal(newRV_noinc(inner));
664 259954 sv_bless(rv, stash);
665 259954 *out = v;
666 259954 return rv;
667 }
668  
669 /* A walk that calls Perl holds a struct of its own onto the vector, released
670 * by the save stack: what the sub does to the object, or to the last
671 * variable holding it, cannot reach the nodes being walked. Between an
672 * ENTER and its LEAVE. */
673 1249 static void hamt_xs_vec_unhold(pTHX_ void *p) {
674 1249 hamt_vec *v = (hamt_vec *)p;
675 1249 hamt_vec gone = *v;
676 1249 HAMT_FREE(HAMT_CX, v, sizeof(hamt_vec));
677 1249 hamt_vec_clear(HAMT_CX, &gone);
678 1249 }
679  
680 95 static hamt_vec *hamt_xs_hold_vec(pTHX_ const hamt_vec *src, const char *what) {
681 95 hamt_vec *v = (hamt_vec *)HAMT_ALLOC(HAMT_CX, sizeof(hamt_vec));
682 95 ▶ if (!v) croak("%s: out of memory", what);
Branch: 1 branch
BranchTrueFalseTotal
if (!v) croak("%s: out of memory", what); 3 92 95
683 92 hamt_vec_share(v, src);
684 92 SAVEDESTRUCTOR_X(hamt_xs_vec_unhold, v);
685 92 return v;
686 }
687  
688 /* An index as an array takes one: negative counts from the end. 1 and the
689 * position when it names an element, 0 when it does not. */
690 77514 static int hamt_xs_index(pTHX_ SV *sv, size_t count, size_t *out) {
691 77514 IV i = SvIV(sv);
692 77514 ▶ if (i < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < 0) { 215 77299 77514
693 215 ▶ if ((UV)(-(i + 1)) >= (UV)count) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ((UV)(-(i + 1)) >= (UV)count) return 0; 10 205 215
694 205 *out = count - (size_t)(-(i + 1)) - 1;
695 205 return 1;
696 }
697 77299 ▶ if ((UV)i >= (UV)count) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ((UV)i >= (UV)count) return 0; 271 77028 77299
698 77028 *out = (size_t)i;
699 77028 return 1;
700 }
701  
702 /* One value onto the end of a vector the caller owns outright. Copied and
703 * made read-only on the way in, as a map's values are. */
704 559656 static void hamt_xs_vec_push(pTHX_ hamt_vec *v, SV *valsv, const char *what) {
705 559656 SV *copy = hamt_sv_copy_in(aTHX_ valsv);
706 559654 int rc = hamt_vec_push(HAMT_CX, v, (void *)copy);
707 559654 SvREFCNT_dec(copy);
708 559654 ▶ if (rc) croak("%s: out of memory", what);
Branch: 1 branch
BranchTrueFalseTotal
if (rc) croak("%s: out of memory", what); 2013 557641 559654
709 557641 }
710  
711 /* One value replaced, likewise. A value that IS the scalar already there
712 * changes nothing. The scalar it displaces is released last. */
713 42954 static void hamt_xs_vec_set(pTHX_ hamt_vec *v, size_t i, SV *valsv, const char *what) {
714 SV *copy;
715 void *dead;
716 int rc;
717  
718 42954 ▶ if (hamt_vec_get(v, i) == (void *)valsv) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_vec_get(v, i) == (void *)valsv) return; 1 42953 42954
719 42953 copy = hamt_sv_copy_in(aTHX_ valsv);
720 42953 rc = hamt_vec_set(HAMT_CX, v, i, (void *)copy, &dead);
721 42953 SvREFCNT_dec(copy);
722 42953 ▶ if (rc) croak("%s: out of memory", what);
Branch: 1 branch
BranchTrueFalseTotal
if (rc) croak("%s: out of memory", what); 1999 40954 42953
723 /*
724 # uncoverable branch false
725 # uncoverable condition right count:1 */
726 40954 ▶ if (dead) HAMT_VAL_RELEASE(HAMT_CX, dead);
Branch: 1 branch
BranchTrueFalseTotal
if (dead) HAMT_VAL_RELEASE(HAMT_CX, dead); 40954 0 40954
727 }
728  
729 104679 static void hamt_xs_vec_pop(pTHX_ hamt_vec *v, const char *what) {
730 void *dead;
731 104679 ▶ if (hamt_vec_pop(HAMT_CX, v, &dead)) croak("%s: out of memory", what);
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_vec_pop(HAMT_CX, v, &dead)) croak("%s: out of memory", what); 633 104046 104679
732 104046 ▶ if (dead) HAMT_VAL_RELEASE(HAMT_CX, dead);
Branch: 1 branch
BranchTrueFalseTotal
if (dead) HAMT_VAL_RELEASE(HAMT_CX, dead); 104044 2 104046
733 104046 }
734  
735 /* `out`, which must be empty, becomes `v` with the `drop` values from `at`
736 * taken out and everything in `mid` put in their place: the one operation
737 * behind insert and remove. `mid` may be NULL. It is two cuts and two
738 * joins, and no Perl runs in it, so the pieces are plain structs cleaned up
739 * here. */
740 12895 static void hamt_xs_vec_splice(pTHX_ hamt_vec *out, const hamt_vec *v, size_t at,
741 size_t drop, const hamt_vec *mid, const char *what) {
742 hamt_vec left, right, joined, res;
743 12895 size_t after = at + drop;
744 12895 int rc = 0;
745  
746 12895 hamt_vec_init(&left);
747 12895 hamt_vec_init(&right);
748 12895 hamt_vec_init(&joined);
749 12895 hamt_vec_init(&res);
750  
751 12895 ▶ if (at > 0) rc = hamt_vec_slice(HAMT_CX, &left, v, 0, at);
Branch: 1 branch
BranchTrueFalseTotal
if (at > 0) rc = hamt_vec_slice(HAMT_CX, &left, v, 0, at); 11893 1002 12895
752 12895 ▶ if (!rc && after < v->count) rc = hamt_vec_slice(HAMT_CX, &right, v, after, v->count - after);
Branch: 2 branches
BranchTrueFalseTotal
if (!rc && after < v->count) rc = hamt_vec_slice(HAMT_CX, &right, v, after, v->count - after); 11979 916 12895
if (!rc && after < v->count) rc = hamt_vec_slice(HAMT_CX, &right, v, after, v->count - after); 10669 1310 11979
753 12895 ▶ if (!rc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) { 10231 2664 12895
754 10231 ▶ if (mid) rc = hamt_vec_concat(HAMT_CX, &joined, &left, mid);
Branch: 1 branch
BranchTrueFalseTotal
if (mid) rc = hamt_vec_concat(HAMT_CX, &joined, &left, mid); 7800 2431 10231
755 2431 else hamt_vec_share(&joined, &left);
756 }
757 12895 ▶ if (!rc) rc = hamt_vec_concat(HAMT_CX, &res, &joined, &right);
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) rc = hamt_vec_concat(HAMT_CX, &res, &joined, &right); 9885 3010 12895
758  
759 12895 hamt_vec_clear(HAMT_CX, &left);
760 12895 hamt_vec_clear(HAMT_CX, &right);
761 12895 hamt_vec_clear(HAMT_CX, &joined);
762 12895 ▶ if (rc) croak("%s: out of memory", what);
Branch: 1 branch
BranchTrueFalseTotal
if (rc) croak("%s: out of memory", what); 5399 7496 12895
763 7496 *out = res;
764 7496 }
765  
766 /* Where an insert goes: 0 up to the count itself, which is the end; a
767 * negative one counts from the end, so that -1 is before the last value,
768 * as splice has it. 1 and the position, or 0. */
769 11402 static int hamt_xs_gap(pTHX_ SV *sv, size_t count, size_t *out) {
770 11402 IV i = SvIV(sv);
771 11402 ▶ if (i < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < 0) { 3 11399 11402
772 3 ▶ if ((UV)(-(i + 1)) >= (UV)count) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ((UV)(-(i + 1)) >= (UV)count) return 0; 1 2 3
773 2 *out = count - (size_t)(-(i + 1)) - 1;
774 2 return 1;
775 }
776 11399 ▶ if ((UV)i > (UV)count) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ((UV)i > (UV)count) return 0; 3 11396 11399
777 11396 *out = (size_t)i;
778 11396 return 1;
779 }
780  
781 /* The vector's transient: the struct, the class it closes into, and whether
782 * it is still open. */
783 typedef struct hamt_vtxn {
784 hamt_vec v;
785 HV *origin;
786 int open;
787 } hamt_vtxn;
788  
789 331 static int hamt_vtxn_free(pTHX_ SV *sv, MAGIC *mg) {
790 331 hamt_vtxn *t = (hamt_vtxn *)mg->mg_ptr;
791 PERL_UNUSED_ARG(sv);
792 331 ▶ if (t) {
Branch: 1 branch
BranchTrueFalseTotal
if (t) { 331 0 331
793 331 hamt_vec gone = t->v;
794 331 HV *origin = t->origin;
795 331 mg->mg_ptr = NULL;
796 331 HAMT_FREE(HAMT_CX, t, sizeof(hamt_vtxn));
797 331 ▶ if (origin && !PL_dirty) SvREFCNT_dec((SV *)origin); /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (origin && !PL_dirty) SvREFCNT_dec((SV *)origin); /* # uncoverable condition right count:2 */ 30 301 331
if (origin && !PL_dirty) SvREFCNT_dec((SV *)origin); /* # uncoverable condition right count:2 */ 30 0 30
798 331 hamt_vec_clear(HAMT_CX, &gone);
799 }
800 331 return 0;
801 }
802  
803 static MGVTBL hamt_vec_txn_vtbl = {
804 NULL, NULL, NULL, NULL, hamt_vtxn_free, NULL, hamt_handle_dup, NULL
805 };
806  
807 350069 static hamt_vtxn *hamt_vtxn_handle(pTHX_ SV *sv, const char *what) {
808 350069 ▶ if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { /* # uncoverable condition right count:2 */ 350068 1 350069
if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { /* # uncoverable condition right count:2 */ 350068 0 350068
809 MAGIC *mg;
810 350069 ▶ for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { 350068 1 350069
811 hamt_vtxn *t;
812 350068 ▶ if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &hamt_vec_txn_vtbl) continue; /* # uncoverable condition left count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &hamt_vec_txn_vtbl) continue; /* # uncoverable condition left count:1 */ 350068 0 350068
if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &hamt_vec_txn_vtbl) continue; /* # uncoverable condition left count:1 */ 1 350067 350068
813 350067 t = (hamt_vtxn *)mg->mg_ptr;
814 350067 ▶ if (!t) croak("%s: this transient belongs to another thread", what);
Branch: 1 branch
BranchTrueFalseTotal
if (!t) croak("%s: this transient belongs to another thread", what); 0 350067 350067
815 350067 ▶ if (!t->open) croak("%s: this transient is closed", what);
Branch: 1 branch
BranchTrueFalseTotal
if (!t->open) croak("%s: this transient is closed", what); 6 350061 350067
816 350061 return t;
817 }
818 }
819 2 croak("%s: not a transient", what);
820 return NULL;
821 }
822  
823 #endif
« include/hamt/hamt_sv.h ↑ Summary include/hamt/hamt_ops.h »