include/hamt/hamt_abi_impl.h

filter: Statementstmt 99.7% 421 / 422 Branchbran 90.4% 273 / 304 (4 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 95.8% 694 / 726 (4 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/vector_transient.xs ↑ Summary xs/vector.xs »
1 #ifndef HAMT_ABI_IMPL_H
2 #define HAMT_ABI_IMPL_H
3  
4 /* What stands behind hamt_abi.h. Each function here is one entry of the
5 * table; the table itself is at the end, and after it the selftest that
6 * walks every entry.
7 *
8 * Nothing here croaks for want of memory, which is why none of it goes
9 * through the XS layer's own helpers: those croak, and a C caller holding
10 * resources of its own cannot be unwound past. */
11  
12 /* ---- keys ---------------------------------------------------------------
13 *
14 * The hash-key rule, applied to bytes and a flag. A flagged key that holds
15 * only characters below 256 is rewritten as its unflagged bytes, in a
16 * buffer this owns and the caller frees with hamt_abi_key_done. */
17  
18 typedef struct hamt_abi_key {
19 hamt_key k;
20 U8 *owned;
21 } hamt_abi_key;
22  
23 98189 static void hamt_abi_key_init(pTHX_ hamt_abi_key *ak, const char *key, STRLEN klen, int utf8) {
24 98189 ak->owned = NULL;
25 98189 ▶ if (utf8) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) { 2763 95426 98189
26 2763 const U8 *s = (const U8 *)key, *e = s + klen;
27 5553 ▶ while (s < e && !(*s & 0x80)) s++;
Branch: 2 branches
BranchTrueFalseTotal
while (s < e && !(*s & 0x80)) s++; 4995 558 5553
while (s < e && !(*s & 0x80)) s++; 2790 2205 4995
28 2763 ▶ if (s == e) utf8 = 0; /* plain ASCII: the flag means nothing */
Branch: 1 branch
BranchTrueFalseTotal
if (s == e) utf8 = 0; /* plain ASCII: the flag means nothing */ 558 2205 2763
29 else {
30 2205 bool still = TRUE;
31 2205 STRLEN len = klen;
32 2205 U8 *d = bytes_from_utf8((const U8 *)key, &len, &still);
33 2205 ▶ if (!still) { /* it fitted in bytes */
Branch: 1 branch
BranchTrueFalseTotal
if (!still) { /* it fitted in bytes */ 562 1643 2205
34 562 ak->owned = d;
35 562 key = (const char *)d;
36 562 klen = len;
37 562 utf8 = 0;
38 }
39 }
40 }
41 98189 ak->k.p = key;
42 98189 ak->k.len = klen;
43 98189 ak->k.utf8 = utf8 ? 1 : 0;
44 98189 ak->k.hash = hamt_hash(key, (size_t)klen, ak->k.utf8);
45 98189 }
46  
47 98189 static void hamt_abi_key_done(pTHX_ hamt_abi_key *ak) {
48 98189 ▶ if (ak->owned) Safefree(ak->owned);
Branch: 1 branch
BranchTrueFalseTotal
if (ak->owned) Safefree(ak->owned); 562 97627 98189
49 98189 ak->owned = NULL;
50 98189 }
51  
52 /* ---- the struct, for maps and sets -------------------------------------- */
53  
54 7668 static hamt_map *hamt_abi_map_new(pTHX) {
55 7668 hamt_map *m = (hamt_map *)HAMT_ALLOC(HAMT_CX, sizeof(hamt_map));
56 PERL_UNUSED_CONTEXT;
57 7668 ▶ if (!m) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!m) return NULL; 20 7648 7668
58 7648 m->root = NULL;
59 7648 m->count = 0;
60 7648 return m;
61 }
62  
63 2025 static hamt_map *hamt_abi_map_copy(pTHX_ const hamt_map *src) {
64 2025 hamt_map *m = hamt_abi_map_new(aTHX);
65 2025 ▶ if (!m) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!m) return NULL; 7 2018 2025
66 2018 ▶ m->root = src->root ? hamt_node_retain(src->root) : NULL; /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
m->root = src->root ? hamt_node_retain(src->root) : NULL; /* # uncoverable condition right count:1 */ 2018 0 2018
67 2018 m->count = src->count;
68 2018 return m;
69 }
70  
71 7336 static void hamt_abi_map_release(pTHX_ hamt_map *m) {
72 hamt_node *root;
73 7336 ▶ if (!m) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!m) return; 1134 6202 7336
74 6202 root = m->root;
75 6202 HAMT_FREE(HAMT_CX, m, sizeof(hamt_map));
76 6202 ▶ if (root) hamt_node_release(HAMT_CX, root);
Branch: 1 branch
BranchTrueFalseTotal
if (root) hamt_node_release(HAMT_CX, root); 5921 281 6202
77 }
78  
79 /* A struct built from a root the caller owns one reference to, which it
80 * takes over; or NULL, with the root released. */
81 2996 static hamt_map *hamt_abi_map_wrap(pTHX_ hamt_node *root, size_t count) {
82 2996 hamt_map *m = hamt_abi_map_new(aTHX);
83 2996 ▶ if (!m) { if (root) hamt_node_release(HAMT_CX, root); return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!m) { if (root) hamt_node_release(HAMT_CX, root); return NULL; } 8 2988 2996
if (!m) { if (root) hamt_node_release(HAMT_CX, root); return NULL; } 7 1 8
84 2988 m->root = root;
85 2988 m->count = count;
86 2988 return m;
87 }
88  
89 /* A new object around a struct that becomes the object's own. */
90 1480 static SV *hamt_abi_bless(pTHX_ const MGVTBL *vt, const char *klass, const char *deflt,
91 void *payload) {
92 1480 SV *inner = newSV(0), *rv;
93 1480 MAGIC *mg = sv_magicext(inner, NULL, PERL_MAGIC_ext, (MGVTBL *)vt, (const char *)payload, 0);
94 1480 mg->mg_flags |= MGf_DUP;
95 1480 rv = newRV_noinc(inner);
96 1480 ▶ sv_bless(rv, gv_stashpv(klass ? klass : deflt, GV_ADD));
Branch: 1 branch
BranchTrueFalseTotal
sv_bless(rv, gv_stashpv(klass ? klass : deflt, GV_ADD)); 725 755 1480
97 1480 return rv;
98 }
99  
100 2304 static const hamt_map *hamt_abi_map_from_sv(pTHX_ SV *obj) {
101 2304 ▶ return obj ? hamt_handle_try(aTHX_ obj, &hamt_map_vtbl) : NULL; /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
return obj ? hamt_handle_try(aTHX_ obj, &hamt_map_vtbl) : NULL; /* # uncoverable condition right count:1 */ 2304 0 2304
102 }
103 717 static const hamt_map *hamt_abi_set_from_sv(pTHX_ SV *obj) {
104 717 ▶ return obj ? hamt_handle_try(aTHX_ obj, &hamt_set_vtbl) : NULL; /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
return obj ? hamt_handle_try(aTHX_ obj, &hamt_set_vtbl) : NULL; /* # uncoverable condition right count:1 */ 717 0 717
105 }
106  
107 /* The struct inside an open transient of the kind `vt` marks, or NULL. */
108 2471 static hamt_txn *hamt_abi_txn(pTHX_ SV *obj, const MGVTBL *vt) {
109 /*
110 # uncoverable condition right count:1
111 # uncoverable condition right count:3 */
112 2471 ▶ if (obj && SvROK(obj) && SvTYPE(SvRV(obj)) >= SVt_PVMG) {
Branch: 3 branches
BranchTrueFalseTotal
if (obj && SvROK(obj) && SvTYPE(SvRV(obj)) >= SVt_PVMG) { 2471 0 2471
if (obj && SvROK(obj) && SvTYPE(SvRV(obj)) >= SVt_PVMG) { 1946 525 2471
if (obj && SvROK(obj) && SvTYPE(SvRV(obj)) >= SVt_PVMG) { 1946 0 1946
113 MAGIC *mg;
114 2658 ▶ for (mg = SvMAGIC(SvRV(obj)); mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(obj)); mg; mg = mg->mg_moremagic) { 1946 712 2658
115 hamt_txn *t;
116 1946 ▶ 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 */ 1946 0 1946
if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != vt) continue; /* # uncoverable condition left count:1 */ 712 1234 1946
117 1234 t = (hamt_txn *)mg->mg_ptr;
118 /*
119 # uncoverable condition right count:1
120 # uncoverable condition right count:2 */
121 1234 ▶ return (t && t->open) ? t : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (t && t->open) ? t : NULL; 1234 0 1234
return (t && t->open) ? t : NULL; 1234 0 1234
122 }
123 }
124 1237 return NULL;
125 }
126 2281 static hamt_map *hamt_abi_map_txn_from_sv(pTHX_ SV *obj) {
127 2281 hamt_txn *t = hamt_abi_txn(aTHX_ obj, &hamt_map_txn_vtbl);
128 2281 ▶ return t ? &t->m : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return t ? &t->m : NULL; 1044 1237 2281
129 }
130 190 static hamt_map *hamt_abi_set_txn_from_sv(pTHX_ SV *obj) {
131 190 hamt_txn *t = hamt_abi_txn(aTHX_ obj, &hamt_set_txn_vtbl);
132 190 ▶ return t ? &t->m : NULL; /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
return t ? &t->m : NULL; /* # uncoverable condition right count:1 */ 190 0 190
133 }
134  
135 533 static SV *hamt_abi_map_to_sv(pTHX_ hamt_map *m, const char *klass) {
136 533 return hamt_abi_bless(aTHX_ &hamt_map_vtbl, klass, "HAMT::Map", m);
137 }
138 200 static SV *hamt_abi_set_to_sv(pTHX_ hamt_map *m, const char *klass) {
139 200 return hamt_abi_bless(aTHX_ &hamt_set_vtbl, klass, "HAMT::Set", m);
140 }
141  
142 /* A transient is a larger struct, so this one allocates: NULL when there is
143 * no memory, and then the struct given is still the caller's. */
144 715 static SV *hamt_abi_txn_to_sv(pTHX_ hamt_map *m, const MGVTBL *vt, const char *txn_class,
145 const char *klass, const char *deflt) {
146 715 hamt_txn *t = (hamt_txn *)HAMT_ALLOC(HAMT_CX, sizeof(hamt_txn));
147 715 ▶ if (!t) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return NULL; 2 713 715
148 713 t->m = *m; /* the references move */
149 /*
150 # uncoverable condition left count:1
151 # uncoverable condition left count:2 */
152 713 ▶ t->origin = (HV *)SvREFCNT_inc_simple_NN((SV *)gv_stashpv(klass ? klass : deflt, GV_ADD));
Branch: 2 branches
BranchTrueFalseTotal
t->origin = (HV *)SvREFCNT_inc_simple_NN((SV *)gv_stashpv(klass ? klass : deflt, GV_ADD)); 0 713 713
t->origin = (HV *)SvREFCNT_inc_simple_NN((SV *)gv_stashpv(klass ? klass : deflt, GV_ADD)); 0 713 713
153 713 t->open = 1;
154 713 HAMT_FREE(HAMT_CX, m, sizeof(hamt_map));
155 713 return hamt_abi_bless(aTHX_ vt, txn_class, txn_class, t);
156 }
157 524 static SV *hamt_abi_map_to_txn_sv(pTHX_ hamt_map *m, const char *klass) {
158 524 return hamt_abi_txn_to_sv(aTHX_ m, &hamt_map_txn_vtbl, "HAMT::Map::Transient", klass, "HAMT::Map");
159 }
160 191 static SV *hamt_abi_set_to_txn_sv(pTHX_ hamt_map *m, const char *klass) {
161 191 return hamt_abi_txn_to_sv(aTHX_ m, &hamt_set_txn_vtbl, "HAMT::Set::Transient", klass, "HAMT::Set");
162 }
163  
164 /* ---- reading ------------------------------------------------------------ */
165  
166 9915 static size_t hamt_abi_map_count(pTHX_ const hamt_map *m) {
167 PERL_UNUSED_CONTEXT;
168 9915 return m->count;
169 }
170  
171 18497 static hamt_entry *hamt_abi_find(pTHX_ const hamt_map *m, const char *key, STRLEN klen, int utf8) {
172 hamt_abi_key ak;
173 hamt_entry *e;
174 18497 hamt_abi_key_init(aTHX_ &ak, key, klen, utf8);
175 18497 e = hamt_map_find(m->root, ak.k.p, (size_t)ak.k.len, ak.k.utf8, ak.k.hash);
176 18497 hamt_abi_key_done(aTHX_ &ak);
177 18497 return e;
178 }
179  
180 9877 static SV *hamt_abi_map_fetch(pTHX_ const hamt_map *m, const char *key, STRLEN klen, int utf8) {
181 9877 hamt_entry *e = hamt_abi_find(aTHX_ m, key, klen, utf8);
182 9877 ▶ return e ? (SV *)e->val : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return e ? (SV *)e->val : NULL; 9311 566 9877
183 }
184  
185 8620 static int hamt_abi_map_has(pTHX_ const hamt_map *m, const char *key, STRLEN klen, int utf8) {
186 8620 return hamt_abi_find(aTHX_ m, key, klen, utf8) != NULL;
187 }
188  
189 /* ---- updating in place -------------------------------------------------- */
190  
191 /* One entry into the struct given: a copy of `value`, or no value at all
192 * for a set. The displaced entry is released last, with the struct already
193 * holding its new root. */
194 81425 static int hamt_abi_put(pTHX_ hamt_map *m, const hamt_key *k, SV *value) {
195 hamt_entry *e, *dead;
196 81425 ▶ SV *copy = value ? hamt_sv_copy_in(aTHX_ value) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *copy = value ? hamt_sv_copy_in(aTHX_ value) : NULL; 9844 71581 81425
197 int added, rc;
198  
199 81425 e = hamt_entry_new(HAMT_CX, k->p, (size_t)k->len, k->utf8, k->hash, (void *)copy);
200 81425 ▶ if (copy) SvREFCNT_dec(copy);
Branch: 1 branch
BranchTrueFalseTotal
if (copy) SvREFCNT_dec(copy); 9844 71581 81425
201 81425 ▶ if (!e) return HAMT_ABI_NOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return HAMT_ABI_NOMEM; 205 81220 81425
202  
203 81220 rc = hamt_map_put_mut(HAMT_CX, &m->root, e, &added, &dead);
204 81220 hamt_entry_release(HAMT_CX, e);
205 81220 ▶ if (rc) return HAMT_ABI_NOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return HAMT_ABI_NOMEM; 96 81124 81220
206 81124 m->count += (size_t)added;
207 81124 ▶ if (dead) hamt_entry_release(HAMT_CX, dead);
Branch: 1 branch
BranchTrueFalseTotal
if (dead) hamt_entry_release(HAMT_CX, dead); 2194 78930 81124
208 81124 return HAMT_ABI_OK;
209 }
210  
211 75644 static int hamt_abi_store(pTHX_ hamt_map *m, const char *key, STRLEN klen, int utf8, SV *value) {
212 hamt_abi_key ak;
213 hamt_entry *old;
214 75644 int rc = HAMT_ABI_OK;
215  
216 75644 hamt_abi_key_init(aTHX_ &ak, key, klen, utf8);
217 /* The very scalar already stored: nothing to do, and nothing is copied
218 * to find that out. Only a read-only scalar can be one, so only then is
219 * the trie walked to see. */
220 6649 ▶ old = (value && SvREADONLY(value))
Branch: 1 branch
BranchTrueFalseTotal
old = (value && SvREADONLY(value)) 1114 5535 6649
221 82293 ▶ ? hamt_map_find(m->root, ak.k.p, (size_t)ak.k.len, ak.k.utf8, ak.k.hash) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? hamt_map_find(m->root, ak.k.p, (size_t)ak.k.len, ak.k.utf8, ak.k.hash) : NULL; 6649 68995 75644
222 75644 ▶ if (!old || old->val != (void *)value) rc = hamt_abi_put(aTHX_ m, &ak.k, value); /* # uncoverable condition left count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (!old || old->val != (void *)value) rc = hamt_abi_put(aTHX_ m, &ak.k, value); /* # uncoverable condition left count:2 */ 551 75093 75644
if (!old || old->val != (void *)value) rc = hamt_abi_put(aTHX_ m, &ak.k, value); /* # uncoverable condition left count:2 */ 0 551 551
223 75644 hamt_abi_key_done(aTHX_ &ak);
224 75644 return rc;
225 }
226  
227 6649 static int hamt_abi_map_store(pTHX_ hamt_map *m, const char *key, STRLEN klen, int utf8, SV *value) {
228 6649 ▶ return hamt_abi_store(aTHX_ m, key, klen, utf8, value ? value : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
return hamt_abi_store(aTHX_ m, key, klen, utf8, value ? value : &PL_sv_undef); 6086 563 6649
229 }
230 68995 static int hamt_abi_set_add(pTHX_ hamt_map *m, const char *key, STRLEN klen, int utf8) {
231 68995 return hamt_abi_store(aTHX_ m, key, klen, utf8, NULL);
232 }
233  
234 1854 static int hamt_abi_map_remove(pTHX_ hamt_map *m, const char *key, STRLEN klen, int utf8) {
235 hamt_abi_key ak;
236 hamt_entry *dead;
237 int status;
238  
239 1854 hamt_abi_key_init(aTHX_ &ak, key, klen, utf8);
240 1854 status = hamt_map_del_mut(HAMT_CX, &m->root, ak.k.p, (size_t)ak.k.len, ak.k.utf8, ak.k.hash, &dead);
241 1854 hamt_abi_key_done(aTHX_ &ak);
242 1854 ▶ if (status == HAMT_DEL_OOM) return HAMT_ABI_NOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (status == HAMT_DEL_OOM) return HAMT_ABI_NOMEM; 3 1851 1854
243 1851 ▶ if (status == HAMT_DEL_DONE) m->count--;
Branch: 1 branch
BranchTrueFalseTotal
if (status == HAMT_DEL_DONE) m->count--; 1299 552 1851
244 1851 ▶ if (dead) hamt_entry_release(HAMT_CX, dead);
Branch: 1 branch
BranchTrueFalseTotal
if (dead) hamt_entry_release(HAMT_CX, dead); 1299 552 1851
245 1851 return HAMT_ABI_OK;
246 }
247  
248 2194 static int hamt_abi_map_update(pTHX_ hamt_map *m, const char *key, STRLEN klen, int utf8,
249 hamt_abi_update_fn fn, void *ud) {
250 hamt_abi_key ak;
251 hamt_entry *old;
252 SV *now;
253 2194 int rc = HAMT_ABI_OK;
254  
255 2194 hamt_abi_key_init(aTHX_ &ak, key, klen, utf8);
256 2194 old = hamt_map_find(m->root, ak.k.p, (size_t)ak.k.len, ak.k.utf8, ak.k.hash);
257 2194 ▶ now = fn(aTHX_ ud, old ? (SV *)old->val : NULL);
Branch: 1 branch
BranchTrueFalseTotal
now = fn(aTHX_ ud, old ? (SV *)old->val : NULL); 1645 549 2194
258 2194 ▶ if (!now) rc = HAMT_ABI_STOPPED;
Branch: 1 branch
BranchTrueFalseTotal
if (!now) rc = HAMT_ABI_STOPPED; 547 1647 2194
259 else {
260 /* The callback may have changed the struct; look again. */
261 1647 old = hamt_map_find(m->root, ak.k.p, (size_t)ak.k.len, ak.k.utf8, ak.k.hash);
262 1647 ▶ if (!old || !old->val || !hamt_sv_eq_cx(HAMT_CX, now, (SV *)old->val)) /* # uncoverable condition left count:2 */
Branch: 3 branches
BranchTrueFalseTotal
if (!old || !old->val || !hamt_sv_eq_cx(HAMT_CX, now, (SV *)old->val)) /* # uncoverable condition left count:2 */ 1098 549 1647
if (!old || !old->val || !hamt_sv_eq_cx(HAMT_CX, now, (SV *)old->val)) /* # uncoverable condition left count:2 */ 1098 0 1098
if (!old || !old->val || !hamt_sv_eq_cx(HAMT_CX, now, (SV *)old->val)) /* # uncoverable condition left count:2 */ 551 547 1098
263 1100 rc = hamt_abi_put(aTHX_ m, &ak.k, now);
264 }
265 2194 hamt_abi_key_done(aTHX_ &ak);
266 2194 return rc;
267 }
268  
269 /* ---- walking ------------------------------------------------------------
270 *
271 * A walk holds its own reference to the root for as long as it runs, let go
272 * by the save stack: the callback may do anything to the struct, release it
273 * included, or croak. */
274  
275 1607 static int hamt_abi_map_walk(pTHX_ const hamt_map *m, hamt_abi_pair_fn fn, void *ud) {
276 1607 hamt_node *root = m->root;
277 hamt_iter it;
278 hamt_entry *e;
279 1607 int rc = HAMT_ABI_OK;
280  
281 1607 ENTER;
282 1607 hamt_xs_hold_root(aTHX_ root);
283 1607 hamt_iter_init(&it, root);
284 7454 ▶ while ((e = hamt_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hamt_iter_next(&it))) { 6394 1060 7454
285 6394 ▶ if (fn(aTHX_ ud, e->key, (STRLEN)e->klen, e->utf8, (SV *)e->val)) { rc = HAMT_ABI_STOPPED; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (fn(aTHX_ ud, e->key, (STRLEN)e->klen, e->utf8, (SV *)e->val)) { rc = HAMT_ABI_STOPPED; break; } 547 5847 6394
286 }
287 1607 LEAVE;
288 1607 return rc;
289 }
290  
291 /* ---- to and from a Perl hash -------------------------------------------- */
292  
293 533 static hamt_map *hamt_abi_map_from_hv(pTHX_ HV *hv) {
294 533 hamt_map *m = hamt_abi_map_new(aTHX);
295 HE *he;
296  
297 533 ▶ if (!m) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!m) return NULL; 1 532 533
298 532 hv_iterinit(hv);
299 3171 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 2646 525 3171
300 hamt_key k;
301 2646 SV *v = hv_iterval(hv, he);
302 2646 hamt_key_from_he(aTHX_ hv, he, &k);
303 2646 ▶ if (hamt_abi_put(aTHX_ m, &k, v ? v : &PL_sv_undef)) { /* # uncoverable condition left count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (hamt_abi_put(aTHX_ m, &k, v ? v : &PL_sv_undef)) { /* # uncoverable condition left count:1 */ 2646 0 2646
if (hamt_abi_put(aTHX_ m, &k, v ? v : &PL_sv_undef)) { /* # uncoverable condition left count:1 */ 7 2639 2646
304 7 hamt_abi_map_release(aTHX_ m);
305 7 return NULL;
306 }
307 }
308 525 return m;
309 }
310  
311 533 static HV *hamt_abi_map_to_hv(pTHX_ const hamt_map *m) {
312 533 HV *hv = newHV();
313 hamt_iter it;
314 hamt_entry *e;
315  
316 533 hamt_iter_init(&it, m->root);
317 3731 ▶ while ((e = hamt_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hamt_iter_next(&it))) { 2665 533 3198
318 SV *v;
319 /*
320 # uncoverable branch true
321 # uncoverable condition left count:1 */
322 2665 ▶ if (e->klen > (size_t)I32_MAX) { SvREFCNT_dec((SV *)hv); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (e->klen > (size_t)I32_MAX) { SvREFCNT_dec((SV *)hv); return NULL; } 0 2665 2665
323 2665 ▶ v = e->val ? newSVsv((SV *)e->val) : newSV(0); /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
v = e->val ? newSVsv((SV *)e->val) : newSV(0); /* # uncoverable condition right count:1 */ 2665 0 2665
324 /*
325 # uncoverable branch true
326 # uncoverable condition left count:2 */
327 2665 ▶ if (!hv_store(hv, e->key, e->utf8 ? -(I32)e->klen : (I32)e->klen, v, 0))
Branch: 2 branches
BranchTrueFalseTotal
if (!hv_store(hv, e->key, e->utf8 ? -(I32)e->klen : (I32)e->klen, v, 0)) 533 2132 2665
if (!hv_store(hv, e->key, e->utf8 ? -(I32)e->klen : (I32)e->klen, v, 0)) 2665 0 2665
328 0 SvREFCNT_dec(v); /* # uncoverable statement */
329 }
330 533 return hv;
331 }
332  
333 /* ---- two maps at once ---------------------------------------------------- */
334  
335 typedef struct hamt_abi_merge_ud {
336 hamt_abi_merge_fn fn;
337 void *ud;
338 } hamt_abi_merge_ud;
339  
340 4324 static int hamt_abi_resolve(void *ud, void *cx, hamt_entry *ea, hamt_entry *eb, hamt_entry **out) {
341 dTHXa(cx);
342 4324 hamt_abi_merge_ud *u = (hamt_abi_merge_ud *)ud;
343 4324 SV *res = u->fn(aTHX_ u->ud, ea->key, (STRLEN)ea->klen, ea->utf8, (SV *)ea->val, (SV *)eb->val);
344 SV *copy;
345  
346 4324 ▶ if (!res) return HAMT_ERR_ABORT;
Branch: 1 branch
BranchTrueFalseTotal
if (!res) return HAMT_ERR_ABORT; 1080 3244 4324
347 3244 ▶ if (ea->val && hamt_sv_eq_cx(cx, res, (SV *)ea->val)) { *out = hamt_entry_retain(ea); return HAMT_OK; } /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (ea->val && hamt_sv_eq_cx(cx, res, (SV *)ea->val)) { *out = hamt_entry_retain(ea); return HAMT_OK; } /* # uncoverable condition right count:1 */ 3244 0 3244
if (ea->val && hamt_sv_eq_cx(cx, res, (SV *)ea->val)) { *out = hamt_entry_retain(ea); return HAMT_OK; } /* # uncoverable condition right count:1 */ 1080 2164 3244
348 2164 ▶ if (eb->val && hamt_sv_eq_cx(cx, res, (SV *)eb->val)) { *out = hamt_entry_retain(eb); return HAMT_OK; } /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (eb->val && hamt_sv_eq_cx(cx, res, (SV *)eb->val)) { *out = hamt_entry_retain(eb); return HAMT_OK; } /* # uncoverable condition right count:1 */ 2164 0 2164
if (eb->val && hamt_sv_eq_cx(cx, res, (SV *)eb->val)) { *out = hamt_entry_retain(eb); return HAMT_OK; } /* # uncoverable condition right count:1 */ 1076 1088 2164
349 1088 copy = hamt_sv_copy_in(aTHX_ res);
350 1088 *out = hamt_entry_new(cx, ea->key, ea->klen, ea->utf8, ea->hash, (void *)copy);
351 1088 SvREFCNT_dec(copy);
352 1088 ▶ return *out ? HAMT_OK : HAMT_ERR_OOM;
Branch: 1 branch
BranchTrueFalseTotal
return *out ? HAMT_OK : HAMT_ERR_OOM; 1086 2 1088
353 }
354  
355 3498 static hamt_map *hamt_abi_map_merge(pTHX_ const hamt_map *a, const hamt_map *b,
356 hamt_abi_merge_fn fn, void *ud, int *status) {
357 hamt_merge_ctx c;
358 hamt_abi_merge_ud u;
359 hamt_node *r;
360 hamt_map *m;
361  
362 3498 u.fn = fn;
363 3498 u.ud = ud;
364 3498 c.cx = HAMT_CX;
365 3498 ▶ c.resolve = fn ? hamt_abi_resolve : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c.resolve = fn ? hamt_abi_resolve : NULL; 2703 795 3498
366 3498 c.ud = &u;
367 3498 r = hamt_map_merge(&c, a->root, b->root);
368 3498 ▶ if (c.err) {
Branch: 1 branch
BranchTrueFalseTotal
if (c.err) { 1108 2390 3498
369 1108 ▶ if (status) *status = c.err == HAMT_ERR_ABORT ? HAMT_ABI_STOPPED : HAMT_ABI_NOMEM;
Branch: 2 branches
BranchTrueFalseTotal
if (status) *status = c.err == HAMT_ERR_ABORT ? HAMT_ABI_STOPPED : HAMT_ABI_NOMEM; 547 561 1108
if (status) *status = c.err == HAMT_ERR_ABORT ? HAMT_ABI_STOPPED : HAMT_ABI_NOMEM; 540 7 547
370 1108 return NULL;
371 }
372 2390 m = hamt_abi_map_wrap(aTHX_ r, a->count + c.added);
373 2390 ▶ if (status) *status = m ? HAMT_ABI_OK : HAMT_ABI_NOMEM;
Branch: 2 branches
BranchTrueFalseTotal
if (status) *status = m ? HAMT_ABI_OK : HAMT_ABI_NOMEM; 2163 227 2390
if (status) *status = m ? HAMT_ABI_OK : HAMT_ABI_NOMEM; 2159 4 2163
374 2390 return m;
375 }
376  
377 typedef struct hamt_abi_eq_ud {
378 hamt_abi_equal_fn fn;
379 void *ud;
380 } hamt_abi_eq_ud;
381  
382 16600 static int hamt_abi_eq(void *ud, void *cx, void *va, void *vb) {
383 dTHXa(cx);
384 16600 hamt_abi_eq_ud *u = (hamt_abi_eq_ud *)ud;
385 16600 return u->fn(aTHX_ u->ud, (SV *)va, (SV *)vb);
386 }
387  
388 3588 static int hamt_abi_map_equal(pTHX_ const hamt_map *a, const hamt_map *b,
389 hamt_abi_equal_fn fn, void *ud) {
390 hamt_eq_ctx c;
391 hamt_abi_eq_ud u;
392 3588 hamt_node *ra = a->root, *rb = b->root;
393 int rc;
394  
395 3588 ▶ if (a->count != b->count) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (a->count != b->count) return 0; 732 2856 3588
396 2856 u.fn = fn;
397 2856 u.ud = ud;
398 2856 c.cx = HAMT_CX;
399 2856 ▶ c.eq = fn ? hamt_abi_eq : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c.eq = fn ? hamt_abi_eq : NULL; 1066 1790 2856
400 2856 c.ud = &u;
401 2856 c.visits = 0;
402 2856 ENTER;
403 2856 hamt_xs_hold_root(aTHX_ ra);
404 2856 hamt_xs_hold_root(aTHX_ rb);
405 2856 rc = hamt_node_equal(&c, ra, rb);
406 2856 LEAVE;
407 2856 ▶ return rc < 0 ? HAMT_ABI_STOPPED : rc;
Branch: 1 branch
BranchTrueFalseTotal
return rc < 0 ? HAMT_ABI_STOPPED : rc; 2323 533 2856
408 }
409 392 static int hamt_abi_set_equal(pTHX_ const hamt_map *a, const hamt_map *b) {
410 392 return hamt_abi_map_equal(aTHX_ a, b, NULL, NULL);
411 }
412  
413 typedef struct hamt_abi_diff_ud {
414 hamt_abi_diff_fn fn;
415 void *ud;
416 } hamt_abi_diff_ud;
417  
418 2665 static int hamt_abi_diff(void *ud, void *cx, int kind, hamt_entry *ea, hamt_entry *eb) {
419 dTHXa(cx);
420 2665 hamt_abi_diff_ud *u = (hamt_abi_diff_ud *)ud;
421 2665 ▶ const hamt_entry *k = ea ? ea : eb;
Branch: 1 branch
BranchTrueFalseTotal
const hamt_entry *k = ea ? ea : eb; 1599 1066 2665
422 2665 ▶ return u->fn(aTHX_ u->ud, kind, k->key, (STRLEN)k->klen, k->utf8,
Branch: 2 branches
BranchTrueFalseTotal
return u->fn(aTHX_ u->ud, kind, k->key, (STRLEN)k->klen, k->utf8, 2132 533 2665
return u->fn(aTHX_ u->ud, kind, k->key, (STRLEN)k->klen, k->utf8, 1599 1066 2665
423 ea ? (SV *)ea->val : NULL, eb ? (SV *)eb->val : NULL);
424 }
425  
426 2132 static int hamt_abi_map_diff(pTHX_ const hamt_map *a, const hamt_map *b,
427 hamt_abi_diff_fn fn, void *ud, size_t *count) {
428 hamt_diff_ctx c;
429 hamt_abi_diff_ud u;
430 2132 hamt_node *ra = a->root, *rb = b->root;
431 int rc;
432  
433 2132 u.fn = fn;
434 2132 u.ud = ud;
435 2132 c.cx = HAMT_CX;
436 2132 ▶ c.fn = fn ? hamt_abi_diff : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c.fn = fn ? hamt_abi_diff : NULL; 533 1599 2132
437 2132 c.ud = &u;
438 2132 c.eq.cx = HAMT_CX;
439 2132 c.eq.eq = NULL;
440 2132 c.eq.ud = NULL;
441 2132 ENTER;
442 2132 hamt_xs_hold_root(aTHX_ ra);
443 2132 hamt_xs_hold_root(aTHX_ rb);
444 2132 rc = hamt_map_diff(&c, ra, rb);
445 2132 LEAVE;
446 2132 ▶ if (count) *count = c.reported;
Branch: 1 branch
BranchTrueFalseTotal
if (count) *count = c.reported; 1599 533 2132
447 2132 ▶ return rc ? HAMT_ABI_STOPPED : HAMT_ABI_OK; /* # uncoverable condition left count:1 */
Branch: 1 branch
BranchTrueFalseTotal
return rc ? HAMT_ABI_STOPPED : HAMT_ABI_OK; /* # uncoverable condition left count:1 */ 0 2132 2132
448 }
449  
450 521 static hamt_map *hamt_abi_map_key_set(pTHX_ const hamt_map *m) {
451 521 hamt_map *s = hamt_abi_map_new(aTHX);
452 hamt_iter it;
453 hamt_entry *e;
454  
455 521 ▶ if (!s) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return NULL; 1 520 521
456 520 hamt_iter_init(&it, m->root);
457 3099 ▶ while ((e = hamt_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hamt_iter_next(&it))) { 2586 513 3099
458 hamt_key k;
459 2586 k.p = e->key; k.len = (STRLEN)e->klen; k.utf8 = e->utf8; k.hash = e->hash;
460 2586 ▶ if (hamt_abi_put(aTHX_ s, &k, NULL)) { hamt_abi_map_release(aTHX_ s); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_abi_put(aTHX_ s, &k, NULL)) { hamt_abi_map_release(aTHX_ s); return NULL; } 7 2579 2586
461 }
462 513 return s;
463 }
464  
465 3874 static int hamt_abi_map_check(pTHX_ const hamt_map *m) {
466 PERL_UNUSED_CONTEXT;
467 3874 return hamt_map_check(m->root, m->count);
468 }
469  
470 /* ---- set algebra --------------------------------------------------------- */
471  
472 248 static hamt_map *hamt_abi_set_union(pTHX_ const hamt_map *a, const hamt_map *b) {
473 248 return hamt_abi_map_merge(aTHX_ a, b, NULL, NULL, NULL);
474 }
475  
476 634 static hamt_map *hamt_abi_set_filter(pTHX_ const hamt_map *a, const hamt_map *b, int common) {
477 hamt_set_ctx c;
478 634 size_t count = 0;
479 hamt_node *r;
480  
481 634 c.cx = HAMT_CX;
482 226 r = common ? hamt_set_intersect(&c, a->root, b->root, a->count, b->count, &count)
483 634 ▶ : hamt_set_subtract(&c, a->root, b->root, a->count, b->count, &count);
Branch: 1 branch
BranchTrueFalseTotal
: hamt_set_subtract(&c, a->root, b->root, a->count, b->count, &count); 226 408 634
484 634 ▶ if (c.err) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (c.err) return NULL; 28 606 634
485 606 return hamt_abi_map_wrap(aTHX_ r, count);
486 }
487 226 static hamt_map *hamt_abi_set_intersect(pTHX_ const hamt_map *a, const hamt_map *b) {
488 226 return hamt_abi_set_filter(aTHX_ a, b, 1);
489 }
490 408 static hamt_map *hamt_abi_set_difference(pTHX_ const hamt_map *a, const hamt_map *b) {
491 408 return hamt_abi_set_filter(aTHX_ a, b, 0);
492 }
493  
494 452 static int hamt_abi_set_is_subset(pTHX_ const hamt_map *a, const hamt_map *b) {
495 size_t visits;
496 PERL_UNUSED_CONTEXT;
497 452 return hamt_set_subset(a->root, b->root, a->count, b->count, &visits);
498 }
499 392 static int hamt_abi_set_is_disjoint(pTHX_ const hamt_map *a, const hamt_map *b) {
500 size_t visits;
501 PERL_UNUSED_CONTEXT;
502 392 return hamt_set_disjoint(a->root, b->root, &visits);
503 }
504  
505 /* ---- vectors ------------------------------------------------------------- */
506  
507 1387 static hamt_vec *hamt_abi_vec_new(pTHX) {
508 1387 hamt_vec *v = (hamt_vec *)HAMT_ALLOC(HAMT_CX, sizeof(hamt_vec));
509 PERL_UNUSED_CONTEXT;
510 1387 ▶ if (!v) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) return NULL; 15 1372 1387
511 1372 hamt_vec_init(v);
512 1372 return v;
513 }
514  
515 173 static hamt_vec *hamt_abi_vec_copy(pTHX_ const hamt_vec *src) {
516 173 hamt_vec *v = hamt_abi_vec_new(aTHX);
517 173 ▶ if (!v) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) return NULL; 3 170 173
518 170 hamt_vec_share(v, src);
519 170 return v;
520 }
521  
522 1905 static void hamt_abi_vec_release(pTHX_ hamt_vec *v) {
523 hamt_vec gone;
524 1905 ▶ if (!v) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) return; 567 1338 1905
525 1338 gone = *v;
526 1338 HAMT_FREE(HAMT_CX, v, sizeof(hamt_vec));
527 1338 hamt_vec_clear(HAMT_CX, &gone);
528 }
529  
530 564 static const hamt_vec *hamt_abi_vec_from_sv(pTHX_ SV *obj) {
531 564 ▶ return obj ? hamt_vec_handle_try(aTHX_ obj) : NULL; /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
return obj ? hamt_vec_handle_try(aTHX_ obj) : NULL; /* # uncoverable condition right count:1 */ 564 0 564
532 }
533  
534 551 static hamt_vec *hamt_abi_vec_txn_from_sv(pTHX_ SV *obj) {
535 /*
536 # uncoverable condition right count:1
537 # uncoverable condition right count:3 */
538 551 ▶ if (obj && SvROK(obj) && SvTYPE(SvRV(obj)) >= SVt_PVMG) {
Branch: 3 branches
BranchTrueFalseTotal
if (obj && SvROK(obj) && SvTYPE(SvRV(obj)) >= SVt_PVMG) { 551 0 551
if (obj && SvROK(obj) && SvTYPE(SvRV(obj)) >= SVt_PVMG) { 26 525 551
if (obj && SvROK(obj) && SvTYPE(SvRV(obj)) >= SVt_PVMG) { 26 0 26
539 MAGIC *mg;
540 40 ▶ for (mg = SvMAGIC(SvRV(obj)); mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(obj)); mg; mg = mg->mg_moremagic) { 26 14 40
541 hamt_vtxn *t;
542 26 ▶ 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 */ 26 0 26
if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &hamt_vec_txn_vtbl) continue; /* # uncoverable condition left count:1 */ 14 12 26
543 12 t = (hamt_vtxn *)mg->mg_ptr;
544 /*
545 # uncoverable condition right count:1
546 # uncoverable condition right count:2 */
547 12 ▶ return (t && t->open) ? &t->v : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (t && t->open) ? &t->v : NULL; 12 0 12
return (t && t->open) ? &t->v : NULL; 12 0 12
548 }
549 }
550 539 return NULL;
551 }
552  
553 22 static SV *hamt_abi_vec_to_sv(pTHX_ hamt_vec *v, const char *klass) {
554 22 return hamt_abi_bless(aTHX_ &hamt_vec_vtbl, klass, "HAMT::Vector", v);
555 }
556  
557 13 static SV *hamt_abi_vec_to_txn_sv(pTHX_ hamt_vec *v, const char *klass) {
558 13 hamt_vtxn *t = (hamt_vtxn *)HAMT_ALLOC(HAMT_CX, sizeof(hamt_vtxn));
559 13 ▶ if (!t) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return NULL; 1 12 13
560 12 t->v = *v;
561 /*
562 # uncoverable condition left count:1
563 # uncoverable condition left count:2 */
564 12 ▶ t->origin = (HV *)SvREFCNT_inc_simple_NN((SV *)gv_stashpv(klass ? klass : "HAMT::Vector", GV_ADD));
Branch: 2 branches
BranchTrueFalseTotal
t->origin = (HV *)SvREFCNT_inc_simple_NN((SV *)gv_stashpv(klass ? klass : "HAMT::Vector", GV_ADD)); 0 12 12
t->origin = (HV *)SvREFCNT_inc_simple_NN((SV *)gv_stashpv(klass ? klass : "HAMT::Vector", GV_ADD)); 0 12 12
565 12 t->open = 1;
566 12 HAMT_FREE(HAMT_CX, v, sizeof(hamt_vec));
567 12 return hamt_abi_bless(aTHX_ &hamt_vec_txn_vtbl, "HAMT::Vector::Transient",
568 "HAMT::Vector::Transient", t);
569 }
570  
571 1462 static size_t hamt_abi_vec_count(pTHX_ const hamt_vec *v) {
572 PERL_UNUSED_CONTEXT;
573 1462 return v->count;
574 }
575  
576 3251 static SV *hamt_abi_vec_fetch(pTHX_ const hamt_vec *v, size_t index) {
577 PERL_UNUSED_CONTEXT;
578 3251 ▶ return index < v->count ? (SV *)hamt_vec_get(v, index) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return index < v->count ? (SV *)hamt_vec_get(v, index) : NULL; 2917 334 3251
579 }
580  
581 225420 static int hamt_abi_vec_push(pTHX_ hamt_vec *v, SV *value) {
582 225420 ▶ SV *copy = hamt_sv_copy_in(aTHX_ value ? value : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
SV *copy = hamt_sv_copy_in(aTHX_ value ? value : &PL_sv_undef); 225277 143 225420
583 225420 int rc = hamt_vec_push(HAMT_CX, v, (void *)copy);
584 225420 SvREFCNT_dec(copy);
585 225420 ▶ return rc ? HAMT_ABI_NOMEM : HAMT_ABI_OK;
Branch: 1 branch
BranchTrueFalseTotal
return rc ? HAMT_ABI_NOMEM : HAMT_ABI_OK; 78 225342 225420
586 }
587  
588 576 static int hamt_abi_vec_store(pTHX_ hamt_vec *v, size_t index, SV *value) {
589 SV *copy;
590 void *dead;
591 int rc;
592  
593 576 ▶ if (index >= v->count) return HAMT_ABI_RANGE;
Branch: 1 branch
BranchTrueFalseTotal
if (index >= v->count) return HAMT_ABI_RANGE; 144 432 576
594 432 ▶ if (!value) value = &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if (!value) value = &PL_sv_undef; 143 289 432
595 432 ▶ if (hamt_vec_get(v, index) == (void *)value) return HAMT_ABI_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_vec_get(v, index) == (void *)value) return HAMT_ABI_OK; 142 290 432
596 290 copy = hamt_sv_copy_in(aTHX_ value);
597 290 rc = hamt_vec_set(HAMT_CX, v, index, (void *)copy, &dead);
598 290 SvREFCNT_dec(copy);
599 290 ▶ if (rc) return HAMT_ABI_NOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return HAMT_ABI_NOMEM; 4 286 290
600 /*
601 # uncoverable branch false
602 # uncoverable condition right count:1 */
603 286 ▶ if (dead) HAMT_VAL_RELEASE(HAMT_CX, dead);
Branch: 1 branch
BranchTrueFalseTotal
if (dead) HAMT_VAL_RELEASE(HAMT_CX, dead); 286 0 286
604 286 return HAMT_ABI_OK;
605 }
606  
607 330 static int hamt_abi_vec_pop(pTHX_ hamt_vec *v) {
608 void *dead;
609 330 ▶ if (hamt_vec_pop(HAMT_CX, v, &dead)) return HAMT_ABI_NOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_vec_pop(HAMT_CX, v, &dead)) return HAMT_ABI_NOMEM; 1 329 330
610 329 ▶ if (dead) HAMT_VAL_RELEASE(HAMT_CX, dead);
Branch: 1 branch
BranchTrueFalseTotal
if (dead) HAMT_VAL_RELEASE(HAMT_CX, dead); 143 186 329
611 329 return HAMT_ABI_OK;
612 }
613  
614 283 static int hamt_abi_vec_walk(pTHX_ const hamt_vec *v, hamt_abi_item_fn fn, void *ud) {
615 hamt_vec *held;
616 hamt_vec_iter it;
617 void *const *run;
618 size_t start;
619 uint32_t n, i;
620 283 int rc = HAMT_ABI_OK;
621  
622 283 held = (hamt_vec *)HAMT_ALLOC(HAMT_CX, sizeof(hamt_vec));
623 283 ▶ if (!held) return HAMT_ABI_NOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!held) return HAMT_ABI_NOMEM; 2 281 283
624 281 ENTER;
625 281 hamt_vec_share(held, v);
626 281 SAVEDESTRUCTOR_X(hamt_xs_vec_unhold, held);
627 281 hamt_vec_iter_init(&it, held);
628 5777 ▶ while (rc == HAMT_ABI_OK && (run = hamt_vec_iter_next(&it, &start, &n)))
Branch: 2 branches
BranchTrueFalseTotal
while (rc == HAMT_ABI_OK && (run = hamt_vec_iter_next(&it, &start, &n))) 5356 140 5496
while (rc == HAMT_ABI_OK && (run = hamt_vec_iter_next(&it, &start, &n))) 5215 141 5356
629 165775 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 160700 5075 165775
630 160700 ▶ if (fn(aTHX_ ud, start + i, (SV *)run[i])) { rc = HAMT_ABI_STOPPED; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (fn(aTHX_ ud, start + i, (SV *)run[i])) { rc = HAMT_ABI_STOPPED; break; } 140 160560 160700
631 281 LEAVE;
632 281 return rc;
633 }
634  
635 57 static hamt_vec *hamt_abi_vec_from_av(pTHX_ AV *av) {
636 57 hamt_vec *v = hamt_abi_vec_new(aTHX);
637 57 SSize_t i, top = av_len(av);
638  
639 57 ▶ if (!v) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) return NULL; 1 56 57
640 41040 ▶ for (i = 0; i <= top; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= top; i++) { 41022 18 41040
641 41022 SV **svp = av_fetch(av, i, 0);
642 41022 ▶ if (hamt_abi_vec_push(aTHX_ v, svp ? *svp : &PL_sv_undef)) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (hamt_abi_vec_push(aTHX_ v, svp ? *svp : &PL_sv_undef)) { /* # uncoverable condition right count:1 */ 41022 0 41022
if (hamt_abi_vec_push(aTHX_ v, svp ? *svp : &PL_sv_undef)) { /* # uncoverable condition right count:1 */ 38 40984 41022
643 38 hamt_abi_vec_release(aTHX_ v);
644 38 return NULL;
645 }
646 }
647 18 return v;
648 }
649  
650 173 static AV *hamt_abi_vec_to_av(pTHX_ const hamt_vec *v) {
651 173 AV *av = newAV();
652 hamt_vec_iter it;
653 void *const *run;
654 size_t start;
655 uint32_t n, i;
656  
657 /*
658 # uncoverable branch true
659 # uncoverable condition left count:1 */
660 173 ▶ if (v->count > (size_t)I32_MAX) { SvREFCNT_dec((SV *)av); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (v->count > (size_t)I32_MAX) { SvREFCNT_dec((SV *)av); return NULL; } 0 173 173
661 173 ▶ if (v->count) av_extend(av, (I32)(v->count - 1));
Branch: 1 branch
BranchTrueFalseTotal
if (v->count) av_extend(av, (I32)(v->count - 1)); 57 116 173
662 173 hamt_vec_iter_init(&it, v);
663 2168 ▶ while ((run = hamt_vec_iter_next(&it, &start, &n)))
Branch: 1 branch
BranchTrueFalseTotal
while ((run = hamt_vec_iter_next(&it, &start, &n))) 1995 173 2168
664 64695 ▶ for (i = 0; i < n; i++) av_push(av, newSVsv((SV *)run[i]));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) av_push(av, newSVsv((SV *)run[i])); 62700 1995 64695
665 173 return av;
666 }
667  
668 /* A struct holding what `built` holds, which it takes over; or NULL, with
669 * `built` cleared. */
670 858 static hamt_vec *hamt_abi_vec_wrap(pTHX_ hamt_vec *built) {
671 858 hamt_vec *v = hamt_abi_vec_new(aTHX);
672 858 ▶ if (!v) { hamt_vec_clear(HAMT_CX, built); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!v) { hamt_vec_clear(HAMT_CX, built); return NULL; } 9 849 858
673 849 *v = *built;
674 849 return v;
675 }
676  
677 134 static hamt_vec *hamt_abi_vec_concat(pTHX_ const hamt_vec *a, const hamt_vec *b) {
678 hamt_vec res;
679 134 ▶ if (hamt_vec_concat(HAMT_CX, &res, a, b)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_vec_concat(HAMT_CX, &res, a, b)) return NULL; 7 127 134
680 127 return hamt_abi_vec_wrap(aTHX_ &res);
681 }
682  
683 705 static hamt_vec *hamt_abi_vec_slice(pTHX_ const hamt_vec *v, size_t from, size_t len, int *status) {
684 hamt_vec res;
685 hamt_vec *out;
686  
687 705 ▶ if (from > v->count || len > v->count - from) {
Branch: 2 branches
BranchTrueFalseTotal
if (from > v->count || len > v->count - from) { 476 229 705
if (from > v->count || len > v->count - from) { 117 359 476
688 346 ▶ if (status) *status = HAMT_ABI_RANGE;
Branch: 1 branch
BranchTrueFalseTotal
if (status) *status = HAMT_ABI_RANGE; 117 229 346
689 346 return NULL;
690 }
691 359 hamt_vec_init(&res);
692 359 ▶ if (len && hamt_vec_slice(HAMT_CX, &res, v, from, len)) {
Branch: 2 branches
BranchTrueFalseTotal
if (len && hamt_vec_slice(HAMT_CX, &res, v, from, len)) { 242 117 359
if (len && hamt_vec_slice(HAMT_CX, &res, v, from, len)) { 11 231 242
693 11 ▶ if (status) *status = HAMT_ABI_NOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (status) *status = HAMT_ABI_NOMEM; 8 3 11
694 11 return NULL;
695 }
696 348 out = hamt_abi_vec_wrap(aTHX_ &res);
697 348 ▶ if (status) *status = out ? HAMT_ABI_OK : HAMT_ABI_NOMEM;
Branch: 2 branches
BranchTrueFalseTotal
if (status) *status = out ? HAMT_ABI_OK : HAMT_ABI_NOMEM; 235 113 348
if (status) *status = out ? HAMT_ABI_OK : HAMT_ABI_NOMEM; 233 2 235
698 348 return out;
699 }
700  
701 /* `v` with `drop` values from `at` taken out and `mid` put in their place. */
702 651 static hamt_vec *hamt_abi_vec_splice(pTHX_ const hamt_vec *v, size_t at, size_t drop,
703 const hamt_vec *mid, int *status) {
704 hamt_vec left, right, joined, res;
705 hamt_vec *out;
706 size_t after;
707 651 int rc = 0;
708  
709 651 ▶ if (at > v->count || drop > v->count - at) {
Branch: 2 branches
BranchTrueFalseTotal
if (at > v->count || drop > v->count - at) { 563 88 651
if (at > v->count || drop > v->count - at) { 132 431 563
710 220 ▶ if (status) *status = HAMT_ABI_RANGE;
Branch: 1 branch
BranchTrueFalseTotal
if (status) *status = HAMT_ABI_RANGE; 163 57 220
711 220 return NULL;
712 }
713 431 after = at + drop;
714 431 hamt_vec_init(&left);
715 431 hamt_vec_init(&right);
716 431 hamt_vec_init(&joined);
717 431 hamt_vec_init(&res);
718  
719 431 ▶ 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); 290 141 431
720 431 ▶ 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); 423 8 431
if (!rc && after < v->count) rc = hamt_vec_slice(HAMT_CX, &right, v, after, v->count - after); 333 90 423
721 431 ▶ if (!rc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) { 400 31 431
722 400 ▶ 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); 254 146 400
723 146 else hamt_vec_share(&joined, &left);
724 }
725 431 ▶ 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); 398 33 431
726 431 hamt_vec_clear(HAMT_CX, &left);
727 431 hamt_vec_clear(HAMT_CX, &right);
728 431 hamt_vec_clear(HAMT_CX, &joined);
729 431 ▶ if (rc) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc) { 48 383 431
730 48 ▶ if (status) *status = HAMT_ABI_NOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (status) *status = HAMT_ABI_NOMEM; 32 16 48
731 48 return NULL;
732 }
733 383 out = hamt_abi_vec_wrap(aTHX_ &res);
734 383 ▶ if (status) *status = out ? HAMT_ABI_OK : HAMT_ABI_NOMEM;
Branch: 2 branches
BranchTrueFalseTotal
if (status) *status = out ? HAMT_ABI_OK : HAMT_ABI_NOMEM; 258 125 383
if (status) *status = out ? HAMT_ABI_OK : HAMT_ABI_NOMEM; 255 3 258
735 383 return out;
736 }
737  
738 356 static hamt_vec *hamt_abi_vec_insert(pTHX_ const hamt_vec *v, size_t at, const hamt_vec *mid, int *status) {
739 356 return hamt_abi_vec_splice(aTHX_ v, at, 0, mid, status);
740 }
741 295 static hamt_vec *hamt_abi_vec_remove(pTHX_ const hamt_vec *v, size_t at, size_t len, int *status) {
742 295 return hamt_abi_vec_splice(aTHX_ v, at, len, NULL, status);
743 }
744  
745 505 static int hamt_abi_vec_equal(pTHX_ const hamt_vec *a, const hamt_vec *b,
746 hamt_abi_equal_fn fn, void *ud) {
747 hamt_eq_ctx c;
748 hamt_abi_eq_ud u;
749 hamt_vec *ha, *hb;
750 int rc;
751  
752 505 ▶ if (a->count != b->count) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (a->count != b->count) return 0; 57 448 505
753 448 ha = (hamt_vec *)HAMT_ALLOC(HAMT_CX, sizeof(hamt_vec));
754 448 ▶ hb = ha ? (hamt_vec *)HAMT_ALLOC(HAMT_CX, sizeof(hamt_vec)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
hb = ha ? (hamt_vec *)HAMT_ALLOC(HAMT_CX, sizeof(hamt_vec)) : NULL; 443 5 448
755 448 ▶ if (!hb) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hb) { 10 438 448
756 10 ▶ if (ha) HAMT_FREE(HAMT_CX, ha, sizeof(hamt_vec));
Branch: 1 branch
BranchTrueFalseTotal
if (ha) HAMT_FREE(HAMT_CX, ha, sizeof(hamt_vec)); 5 5 10
757 10 return HAMT_ABI_NOMEM;
758 }
759 438 u.fn = fn;
760 438 u.ud = ud;
761 438 c.cx = HAMT_CX;
762 438 ▶ c.eq = fn ? hamt_abi_eq : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c.eq = fn ? hamt_abi_eq : NULL; 148 290 438
763 438 c.ud = &u;
764 438 c.visits = 0;
765 438 ENTER;
766 438 hamt_vec_share(ha, a);
767 438 SAVEDESTRUCTOR_X(hamt_xs_vec_unhold, ha);
768 438 hamt_vec_share(hb, b);
769 438 SAVEDESTRUCTOR_X(hamt_xs_vec_unhold, hb);
770 438 rc = hamt_vec_equal(&c, ha, hb);
771 438 LEAVE;
772 438 ▶ return rc < 0 ? HAMT_ABI_STOPPED : rc;
Branch: 1 branch
BranchTrueFalseTotal
return rc < 0 ? HAMT_ABI_STOPPED : rc; 304 134 438
773 }
774  
775 901 static int hamt_abi_vec_check(pTHX_ const hamt_vec *v) {
776 PERL_UNUSED_CONTEXT;
777 901 return hamt_vec_check(v);
778 }
779  
780 2268 static int hamt_abi_value_equal(pTHX_ SV *a, SV *b) {
781 2268 return hamt_sv_eq_cx(HAMT_CX, a, b);
782 }
783  
784 /* ---- the table ----------------------------------------------------------
785 *
786 * In the order hamt_abi.h declares it. Appended to, never reordered. */
787  
788 static const hamt_abi HAMT_ABI = {
789 HAMT_ABI_VERSION,
790  
791 hamt_abi_value_equal,
792  
793 hamt_abi_map_new,
794 hamt_abi_map_copy,
795 hamt_abi_map_release,
796 hamt_abi_map_from_sv,
797 hamt_abi_map_txn_from_sv,
798 hamt_abi_map_to_sv,
799 hamt_abi_map_to_txn_sv,
800 hamt_abi_map_from_hv,
801 hamt_abi_map_to_hv,
802 hamt_abi_map_count,
803 hamt_abi_map_fetch,
804 hamt_abi_map_has,
805 hamt_abi_map_store,
806 hamt_abi_map_remove,
807 hamt_abi_map_update,
808 hamt_abi_map_walk,
809 hamt_abi_map_merge,
810 hamt_abi_map_equal,
811 hamt_abi_map_diff,
812 hamt_abi_map_key_set,
813 hamt_abi_map_check,
814  
815 hamt_abi_map_new, /* set_new */
816 hamt_abi_map_copy, /* set_copy */
817 hamt_abi_map_release, /* set_release */
818 hamt_abi_map_count, /* set_count */
819 hamt_abi_map_has, /* set_has */
820 hamt_abi_map_remove, /* set_remove */
821 hamt_abi_map_walk, /* set_walk */
822 hamt_abi_map_check, /* set_check */
823 hamt_abi_set_from_sv,
824 hamt_abi_set_txn_from_sv,
825 hamt_abi_set_to_sv,
826 hamt_abi_set_to_txn_sv,
827 hamt_abi_set_add,
828 hamt_abi_set_union,
829 hamt_abi_set_intersect,
830 hamt_abi_set_difference,
831 hamt_abi_set_is_subset,
832 hamt_abi_set_is_disjoint,
833 hamt_abi_set_equal,
834  
835 hamt_abi_vec_new,
836 hamt_abi_vec_copy,
837 hamt_abi_vec_release,
838 hamt_abi_vec_from_sv,
839 hamt_abi_vec_txn_from_sv,
840 hamt_abi_vec_to_sv,
841 hamt_abi_vec_to_txn_sv,
842 hamt_abi_vec_from_av,
843 hamt_abi_vec_to_av,
844 hamt_abi_vec_count,
845 hamt_abi_vec_fetch,
846 hamt_abi_vec_push,
847 hamt_abi_vec_store,
848 hamt_abi_vec_pop,
849 hamt_abi_vec_walk,
850 hamt_abi_vec_concat,
851 hamt_abi_vec_slice,
852 hamt_abi_vec_insert,
853 hamt_abi_vec_remove,
854 hamt_abi_vec_equal,
855 hamt_abi_vec_check
856 };
857  
858 #endif
« xs/vector_transient.xs ↑ Summary xs/vector.xs »