include/hamt/hamt_abi_impl.h

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