| 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 |
85850 |
| static int hamt_handle_free(pTHX_ SV *sv, MAGIC *mg) { |
| 28 |
85850 |
| hamt_map *m = (hamt_map *)mg->mg_ptr; |
| 29 |
|
| PERL_UNUSED_ARG(sv); |
| 30 |
85850 |
▶ | if (m) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (m) { |
85850 |
0 |
85850 |
|
| 31 |
85850 |
| hamt_node *root = m->root; |
| 32 |
85850 |
| 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 |
85850 |
| HAMT_FREE(HAMT_CX, m, sizeof(hamt_map)); |
| 36 |
85850 |
▶ | if (root) hamt_node_release(HAMT_CX, root); |
Branch: 1 branch
| Branch | True | False | Total |
| if (root) hamt_node_release(HAMT_CX, root); |
84858 |
992 |
85850 |
|
| 37 |
|
| } |
| 38 |
85850 |
| 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 |
1484895 |
| static hamt_map *hamt_handle(pTHX_ SV *sv, const MGVTBL *vt, const char *what, |
| 62 |
|
| const char *kind) { |
| 63 |
1484895 |
▶ | if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
1484860 |
35 |
1484895 |
| if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
1484860 |
0 |
1484860 |
|
| 64 |
|
| MAGIC *mg; |
| 65 |
1484882 |
▶ | for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { |
1484804 |
78 |
1484882 |
|
| 66 |
1484804 |
▶ | if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != vt) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != vt) continue; |
1484804 |
0 |
1484804 |
| if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != vt) continue; |
22 |
1484782 |
1484804 |
|
| 67 |
1484782 |
▶ | if (!mg->mg_ptr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!mg->mg_ptr) |
0 |
1484782 |
1484782 |
|
| 68 |
0 |
| croak("%s: this %s belongs to another thread", what, kind); |
| 69 |
1484782 |
| 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 |
85822 |
| 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 |
85822 |
| hamt_map *m = (hamt_map *)HAMT_ALLOC(HAMT_CX, sizeof(hamt_map)); |
| 81 |
|
| SV *inner, *rv; |
| 82 |
|
| MAGIC *mg; |
| 83 |
|
| |
| 84 |
85822 |
▶ | if (!m) croak("%s: out of memory", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!m) croak("%s: out of memory", what); |
0 |
85822 |
85822 |
|
| 85 |
85822 |
▶ | m->root = root ? hamt_node_retain(root) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| m->root = root ? hamt_node_retain(root) : NULL; |
81388 |
4434 |
85822 |
|
| 86 |
85822 |
| m->count = count; |
| 87 |
|
| |
| 88 |
85822 |
| inner = newSV(0); |
| 89 |
85822 |
| mg = sv_magicext(inner, NULL, PERL_MAGIC_ext, (MGVTBL *)vt, (const char *)m, 0); |
| 90 |
85822 |
| mg->mg_flags |= MGf_DUP; |
| 91 |
85822 |
| rv = sv_2mortal(newRV_noinc(inner)); |
| 92 |
85822 |
| sv_bless(rv, stash); |
| 93 |
85822 |
| *out = m; |
| 94 |
85822 |
| return rv; |
| 95 |
|
| } |
| 96 |
|
| |
| 97 |
|
| /* The class to bless into: the one named, or the one an object is in. */ |
| 98 |
6545 |
| static HV *hamt_class_stash(pTHX_ SV *klass) { |
| 99 |
6545 |
▶ | if (SvROK(klass) && SvOBJECT(SvRV(klass))) return SvSTASH(SvRV(klass)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(klass) && SvOBJECT(SvRV(klass))) return SvSTASH(SvRV(klass)); |
3 |
6542 |
6545 |
| if (SvROK(klass) && SvOBJECT(SvRV(klass))) return SvSTASH(SvRV(klass)); |
3 |
0 |
3 |
|
| 100 |
6542 |
| 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
| Branch | True | False | Total |
| 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 |
2018815 |
| static void hamt_key_from_sv(pTHX_ SV *sv, hamt_key *k) { |
| 129 |
|
| STRLEN len; |
| 130 |
2018814 |
| const char *p = SvPV(sv, len); |
| 131 |
2018814 |
| int utf8 = 0; |
| 132 |
|
| |
| 133 |
2018814 |
▶ | if (SvUTF8(sv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvUTF8(sv)) { |
27 |
2018787 |
2018814 |
|
| 134 |
27 |
| const U8 *s = (const U8 *)p, *e = s + len; |
| 135 |
30 |
▶ | while (s < e && !(*s & 0x80)) s++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (s < e && !(*s & 0x80)) s++; |
30 |
0 |
30 |
| while (s < e && !(*s & 0x80)) s++; |
3 |
27 |
30 |
|
| 136 |
27 |
▶ | if (s < e) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s < e) { |
27 |
0 |
27 |
|
| 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
| Branch | True | False | Total |
| if (sv_utf8_downgrade(tmp, TRUE)) |
11 |
16 |
27 |
|
| 142 |
11 |
| p = SvPV(tmp, len); |
| 143 |
|
| else |
| 144 |
16 |
| utf8 = 1; |
| 145 |
|
| } |
| 146 |
|
| } |
| 147 |
2018814 |
| k->p = p; |
| 148 |
2018814 |
| k->len = len; |
| 149 |
2018814 |
| k->utf8 = utf8; |
| 150 |
2018814 |
| k->hash = hamt_hash(p, (size_t)len, utf8); |
| 151 |
2018814 |
| } |
| 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 |
5051 |
| static void hamt_key_from_he(pTHX_ HV *hv, HE *he, hamt_key *k) { |
| 163 |
|
| PERL_UNUSED_ARG(hv); |
| 164 |
5051 |
▶ | if (HeKLEN(he) == HEf_SVKEY) { hamt_key_from_sv(aTHX_ HeKEY_sv(he), k); return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (HeKLEN(he) == HEf_SVKEY) { hamt_key_from_sv(aTHX_ HeKEY_sv(he), k); return; } |
3 |
5048 |
5051 |
|
| 165 |
5048 |
| k->p = HeKEY(he); |
| 166 |
5048 |
| k->len = (STRLEN)HeKLEN(he); |
| 167 |
5048 |
| k->utf8 = HeKUTF8(he) ? 1 : 0; |
| 168 |
5048 |
| k->hash = hamt_hash(k->p, (size_t)k->len, k->utf8); |
| 169 |
|
| } |
| 170 |
|
| |
| 171 |
1692717 |
| static SV *hamt_key_to_sv(pTHX_ const hamt_entry *e) { |
| 172 |
1692717 |
| SV *sv = newSVpvn(e->key, (STRLEN)e->klen); |
| 173 |
1692717 |
▶ | if (e->utf8) SvUTF8_on(sv); |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->utf8) SvUTF8_on(sv); |
9 |
1692708 |
1692717 |
|
| 174 |
1692717 |
| 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 |
797923 |
| static SV *hamt_sv_copy_in(pTHX_ SV *in) { |
| 196 |
797923 |
| SV *c = newSVsv(in); |
| 197 |
797918 |
| SvREADONLY_on(c); |
| 198 |
797918 |
| 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 |
692721 |
| static void hamt_xs_put_key(pTHX_ hamt_map *m, const hamt_key *kp, SV *valsv, |
| 214 |
|
| const char *what) { |
| 215 |
692721 |
| 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 |
692721 |
▶ | if (valsv && SvREADONLY(valsv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (valsv && SvREADONLY(valsv)) { |
290795 |
401926 |
692721 |
| if (valsv && SvREADONLY(valsv)) { |
1444 |
289351 |
290795 |
|
| 224 |
1444 |
| old = hamt_map_find(m->root, k.p, (size_t)k.len, k.utf8, k.hash); |
| 225 |
1444 |
▶ | if (old && old->val == (void *)valsv) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (old && old->val == (void *)valsv) return; |
141 |
1303 |
1444 |
| if (old && old->val == (void *)valsv) return; |
4 |
137 |
141 |
|
| 226 |
|
| } |
| 227 |
|
| |
| 228 |
692717 |
▶ | copy = valsv ? hamt_sv_copy_in(aTHX_ valsv) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| copy = valsv ? hamt_sv_copy_in(aTHX_ valsv) : NULL; |
290791 |
401926 |
692717 |
|
| 229 |
692714 |
| e = hamt_entry_new(HAMT_CX, k.p, (size_t)k.len, k.utf8, k.hash, (void *)copy); |
| 230 |
692714 |
▶ | if (copy) SvREFCNT_dec(copy); |
Branch: 1 branch
| Branch | True | False | Total |
| if (copy) SvREFCNT_dec(copy); |
290788 |
401926 |
692714 |
|
| 231 |
692714 |
▶ | if (!e) croak("%s: out of memory", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) croak("%s: out of memory", what); |
0 |
692714 |
692714 |
|
| 232 |
|
| |
| 233 |
692714 |
| rc = hamt_map_put_mut(HAMT_CX, &m->root, e, &added, &dead); |
| 234 |
692714 |
| hamt_entry_release(HAMT_CX, e); |
| 235 |
692714 |
▶ | if (rc) croak("%s: out of memory", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc) croak("%s: out of memory", what); |
0 |
692714 |
692714 |
|
| 236 |
|
| |
| 237 |
692714 |
| m->count += (size_t)added; |
| 238 |
692714 |
▶ | if (dead) hamt_entry_release(HAMT_CX, dead); |
Branch: 1 branch
| Branch | True | False | Total |
| if (dead) hamt_entry_release(HAMT_CX, dead); |
40441 |
652273 |
692714 |
|
| 239 |
|
| } |
| 240 |
|
| |
| 241 |
684182 |
| static void hamt_xs_put(pTHX_ hamt_map *m, SV *keysv, SV *valsv, const char *what) { |
| 242 |
|
| hamt_key k; |
| 243 |
684182 |
| hamt_key_from_sv(aTHX_ keysv, &k); |
| 244 |
684182 |
| hamt_xs_put_key(aTHX_ m, &k, valsv, what); |
| 245 |
684179 |
| } |
| 246 |
|
| |
| 247 |
|
| /* One key out of a handle the caller owns outright, the same way. */ |
| 248 |
44968 |
| 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 |
44968 |
| hamt_key_from_sv(aTHX_ keysv, &k); |
| 254 |
44968 |
| status = hamt_map_del_mut(HAMT_CX, &m->root, k.p, (size_t)k.len, k.utf8, k.hash, &dead); |
| 255 |
44968 |
▶ | if (status == HAMT_DEL_OOM) croak("%s: out of memory", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (status == HAMT_DEL_OOM) croak("%s: out of memory", what); |
0 |
44968 |
44968 |
|
| 256 |
44968 |
▶ | if (status == HAMT_DEL_DONE) m->count--; |
Branch: 1 branch
| Branch | True | False | Total |
| if (status == HAMT_DEL_DONE) m->count--; |
25840 |
19128 |
44968 |
|
| 257 |
44968 |
▶ | if (dead) hamt_entry_release(HAMT_CX, dead); |
Branch: 1 branch
| Branch | True | False | Total |
| if (dead) hamt_entry_release(HAMT_CX, dead); |
25840 |
19128 |
44968 |
|
| 258 |
44968 |
| } |
| 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 |
3696 |
| static void hamt_xs_root_unref(pTHX_ void *p) { |
| 273 |
3696 |
| hamt_node_release(HAMT_CX, (hamt_node *)p); |
| 274 |
3696 |
| } |
| 275 |
|
| |
| 276 |
|
| /* Between an ENTER and its LEAVE. */ |
| 277 |
996 |
| static void hamt_xs_hold_root(pTHX_ hamt_node *root) { |
| 278 |
996 |
▶ | if (!root) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!root) return; |
13 |
983 |
996 |
|
| 279 |
983 |
| hamt_node_retain(root); |
| 280 |
983 |
| 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 |
17150 |
| static SV *hamt_xs_scratch(pTHX_ SV *sv) { |
| 291 |
17150 |
▶ | if (sv && SvREFCNT(sv) == 1 && !SvMAGICAL(sv) && !SvREADONLY(sv) && !SvROK(sv)) return sv; |
Branch: 5 branches
| Branch | True | False | Total |
| if (sv && SvREFCNT(sv) == 1 && !SvMAGICAL(sv) && !SvREADONLY(sv) && !SvROK(sv)) return sv; |
17070 |
80 |
17150 |
| if (sv && SvREFCNT(sv) == 1 && !SvMAGICAL(sv) && !SvREADONLY(sv) && !SvROK(sv)) return sv; |
15831 |
1239 |
17070 |
| if (sv && SvREFCNT(sv) == 1 && !SvMAGICAL(sv) && !SvREADONLY(sv) && !SvROK(sv)) return sv; |
15830 |
1 |
15831 |
| if (sv && SvREFCNT(sv) == 1 && !SvMAGICAL(sv) && !SvREADONLY(sv) && !SvROK(sv)) return sv; |
15830 |
0 |
15830 |
| if (sv && SvREFCNT(sv) == 1 && !SvMAGICAL(sv) && !SvREADONLY(sv) && !SvROK(sv)) return sv; |
15830 |
0 |
15830 |
|
| 292 |
1320 |
| return sv_2mortal(newSV(0)); |
| 293 |
|
| } |
| 294 |
|
| |
| 295 |
217 |
| static void hamt_xs_need_code(pTHX_ SV *code, const char *what) { |
| 296 |
217 |
| SvGETMAGIC(code); |
| 297 |
217 |
▶ | if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV) |
209 |
8 |
217 |
| if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV) |
1 |
208 |
209 |
|
| 298 |
9 |
| croak("%s: not a code reference", what); |
| 299 |
208 |
| } |
| 300 |
|
| |
| 301 |
43335 |
| static SV *hamt_xs_val_sv(pTHX_ const hamt_entry *e) { |
| 302 |
43335 |
▶ | return (e && e->val) ? (SV *)e->val : sv_newmortal(); |
Branch: 2 branches
| Branch | True | False | Total |
| return (e && e->val) ? (SV *)e->val : sv_newmortal(); |
37479 |
5856 |
43335 |
| return (e && e->val) ? (SV *)e->val : sv_newmortal(); |
37479 |
0 |
37479 |
|
| 303 |
|
| } |
| 304 |
|
| |
| 305 |
|
| /* ---- calling a sub ------------------------------------------------------- |
| 306 |
|
| * |
| 307 |
|
| * Every sub HAMT is given is called on a stack of its own, as perl's sort |
| 308 |
|
| * calls its comparison. Without that, `last` or `next` inside the sub finds |
| 309 |
|
| * the caller's loop on the context stack and jumps to it straight through |
| 310 |
|
| * this C, which never gets control back. On a stack of its own there is no |
| 311 |
|
| * loop to find, so either is an error, caught by G_EVAL like a die; a walk |
| 312 |
|
| * reads it as "stop" or "skip", and anything else throws it again. |
| 313 |
|
| * |
| 314 |
|
| * hamt_xs_call_enter switches to the stack, with $@ localised so that calls |
| 315 |
|
| * that work leave the caller's $@ as it was; hamt_xs_call_leave switches |
| 316 |
|
| * back. A walk does that once around all its calls. */ |
| 317 |
|
| #define HAMT_XS_RETURNED 0 |
| 318 |
|
| #define HAMT_XS_LAST 1 |
| 319 |
|
| #define HAMT_XS_NEXT 2 |
| 320 |
|
| #define HAMT_XS_DIED 3 |
| 321 |
|
| |
| 322 |
2418 |
| static void hamt_xs_call_enter(pTHX) { |
| 323 |
2418 |
| dSP; |
| 324 |
2418 |
| ENTER; |
| 325 |
2418 |
| save_scalar(PL_errgv); |
| 326 |
2418 |
| PUSHSTACKi(PERLSI_UNKNOWN); |
| 327 |
|
| PERL_UNUSED_VAR(sp); |
| 328 |
2418 |
| } |
| 329 |
|
| |
| 330 |
2418 |
| static void hamt_xs_call_leave(pTHX) { |
| 331 |
2418 |
| dSP; |
| 332 |
2418 |
| POPSTACK; |
| 333 |
|
| PERL_UNUSED_VAR(sp); |
| 334 |
2418 |
| LEAVE; |
| 335 |
2418 |
| } |
| 336 |
|
| |
| 337 |
|
| /* A walk takes `last` and `next` from its sub, so it enters with a warning |
| 338 |
|
| * filter in $SIG{__WARN__}: perl warns "Exiting eval via last" about the |
| 339 |
|
| * G_EVAL frame, which is this library's and not the sub's. The filter drops |
| 340 |
|
| * that and hands everything else to the hook it replaced, kept in |
| 341 |
|
| * $HAMT::_warn_prev, or to perl. "Exiting subroutine via last" is perl's |
| 342 |
|
| * warning for any sub that does it, and is kept. */ |
| 343 |
|
| #ifndef PERL_WARNHOOK_FATAL |
| 344 |
|
| # define PERL_WARNHOOK_FATAL (&PL_sv_placeholder) |
| 345 |
|
| #endif |
| 346 |
|
| |
| 347 |
113 |
| static void hamt_xs_walk_enter(pTHX) { |
| 348 |
113 |
| CV *filter = get_cv("HAMT::_warn_filter", 0); |
| 349 |
113 |
| hamt_xs_call_enter(aTHX); |
| 350 |
113 |
▶ | if (filter && PL_warnhook != PERL_WARNHOOK_FATAL |
Branch: 2 branches
| Branch | True | False | Total |
| if (filter && PL_warnhook != PERL_WARNHOOK_FATAL |
113 |
0 |
113 |
| if (filter && PL_warnhook != PERL_WARNHOOK_FATAL |
113 |
0 |
113 |
|
| 351 |
113 |
▶ | && !(PL_warnhook && SvROK(PL_warnhook) && SvRV(PL_warnhook) == (SV *)filter)) { |
Branch: 3 branches
| Branch | True | False | Total |
| && !(PL_warnhook && SvROK(PL_warnhook) && SvRV(PL_warnhook) == (SV *)filter)) { |
26 |
87 |
113 |
| && !(PL_warnhook && SvROK(PL_warnhook) && SvRV(PL_warnhook) == (SV *)filter)) { |
26 |
0 |
26 |
| && !(PL_warnhook && SvROK(PL_warnhook) && SvRV(PL_warnhook) == (SV *)filter)) { |
23 |
3 |
26 |
|
| 352 |
110 |
| GV *gv = gv_fetchpvs("HAMT::_warn_prev", GV_ADD, SVt_PV); |
| 353 |
110 |
| save_scalar(gv); |
| 354 |
110 |
▶ | if (PL_warnhook) sv_setsv(GvSV(gv), PL_warnhook); |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_warnhook) sv_setsv(GvSV(gv), PL_warnhook); |
23 |
87 |
110 |
|
| 355 |
110 |
| SAVEGENERICSV(PL_warnhook); |
| 356 |
110 |
| PL_warnhook = newRV_inc((SV *)filter); |
| 357 |
|
| } |
| 358 |
113 |
| } |
| 359 |
|
| |
| 360 |
513 |
| static int hamt_xs_loop_error(pTHX_ SV *err, const char *what, STRLEN len) { |
| 361 |
|
| STRLEN elen; |
| 362 |
|
| const char *p; |
| 363 |
513 |
▶ | if (SvROK(err)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(err)) return 0; |
4 |
509 |
513 |
|
| 364 |
509 |
| p = SvPV(err, elen); |
| 365 |
509 |
▶ | return elen >= len && memEQ(p, what, len); |
Branch: 2 branches
| Branch | True | False | Total |
| return elen >= len && memEQ(p, what, len); |
381 |
128 |
509 |
| return elen >= len && memEQ(p, what, len); |
197 |
184 |
381 |
|
| 366 |
|
| } |
| 367 |
|
| |
| 368 |
|
| #define HAMT_XS_ERR_LAST "Can't \"last\" outside a loop block" |
| 369 |
|
| #define HAMT_XS_ERR_NEXT "Can't \"next\" outside a loop block" |
| 370 |
|
| |
| 371 |
|
| /* Calls the sub with the arguments, between hamt_xs_call_enter and _leave. |
| 372 |
|
| * When `res` is given the call is in scalar context and the result is put |
| 373 |
|
| * there, a temporary of the caller's frame. A die leaves its error in $@. */ |
| 374 |
36015 |
| static int hamt_xs_call(pTHX_ SV *code, SV **args, int nargs, SV **res) { |
| 375 |
36015 |
| dSP; |
| 376 |
|
| I32 n; |
| 377 |
|
| int i; |
| 378 |
|
| SV *err; |
| 379 |
|
| |
| 380 |
36015 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
36015 |
36015 |
|
| 381 |
36015 |
▶ | EXTEND(SP, nargs); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, nargs); |
36015 |
0 |
36015 |
| EXTEND(SP, nargs); |
0 |
36015 |
36015 |
|
| 382 |
142113 |
▶ | for (i = 0; i < nargs; i++) PUSHs(args[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nargs; i++) PUSHs(args[i]); |
106098 |
36015 |
142113 |
|
| 383 |
36015 |
| PUTBACK; |
| 384 |
36015 |
▶ | n = call_sv(code, (res ? G_SCALAR : G_DISCARD | G_VOID) | G_EVAL); |
Branch: 1 branch
| Branch | True | False | Total |
| n = call_sv(code, (res ? G_SCALAR : G_DISCARD | G_VOID) | G_EVAL); |
2305 |
33710 |
36015 |
|
| 385 |
36015 |
| SPAGAIN; |
| 386 |
36015 |
▶ | if (res) *res = n > 0 ? *SP : &PL_sv_undef; |
Branch: 2 branches
| Branch | True | False | Total |
| if (res) *res = n > 0 ? *SP : &PL_sv_undef; |
2305 |
33710 |
36015 |
| if (res) *res = n > 0 ? *SP : &PL_sv_undef; |
2305 |
0 |
2305 |
|
| 387 |
36015 |
| SP -= n; |
| 388 |
36015 |
| PUTBACK; |
| 389 |
36015 |
▶ | err = ERRSV; |
Branch: 1 branch
| Branch | True | False | Total |
| err = ERRSV; |
36015 |
0 |
36015 |
|
| 390 |
36015 |
▶ | if (!SvTRUE(err)) return HAMT_XS_RETURNED; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvTRUE(err)) return HAMT_XS_RETURNED; |
35748 |
267 |
36015 |
|
| 391 |
267 |
▶ | if (hamt_xs_loop_error(aTHX_ err, HAMT_XS_ERR_LAST, sizeof(HAMT_XS_ERR_LAST) - 1)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_xs_loop_error(aTHX_ err, HAMT_XS_ERR_LAST, sizeof(HAMT_XS_ERR_LAST) - 1)) |
21 |
246 |
267 |
|
| 392 |
21 |
| return HAMT_XS_LAST; |
| 393 |
246 |
▶ | if (hamt_xs_loop_error(aTHX_ err, HAMT_XS_ERR_NEXT, sizeof(HAMT_XS_ERR_NEXT) - 1)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_xs_loop_error(aTHX_ err, HAMT_XS_ERR_NEXT, sizeof(HAMT_XS_ERR_NEXT) - 1)) |
176 |
70 |
246 |
|
| 394 |
176 |
| return HAMT_XS_NEXT; |
| 395 |
70 |
| return HAMT_XS_DIED; |
| 396 |
|
| } |
| 397 |
|
| |
| 398 |
|
| /* A copy of $@ that outlives hamt_xs_call_leave, or NULL when the call |
| 399 |
|
| * returned or a walk takes `last` and `next` as stop and skip. */ |
| 400 |
36000 |
| static SV *hamt_xs_call_err(pTHX_ int how, int walk) { |
| 401 |
36000 |
▶ | if (how == HAMT_XS_RETURNED || (walk && how != HAMT_XS_DIED)) return NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| if (how == HAMT_XS_RETURNED || (walk && how != HAMT_XS_DIED)) return NULL; |
252 |
35748 |
36000 |
| if (how == HAMT_XS_RETURNED || (walk && how != HAMT_XS_DIED)) return NULL; |
209 |
43 |
252 |
| if (how == HAMT_XS_RETURNED || (walk && how != HAMT_XS_DIED)) return NULL; |
172 |
37 |
209 |
|
| 402 |
80 |
▶ | return newSVsv(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| return newSVsv(ERRSV); |
80 |
0 |
80 |
|
| 403 |
|
| } |
| 404 |
|
| |
| 405 |
|
| /* Throws a copy from hamt_xs_call_err, object or string, exactly as it was. */ |
| 406 |
73 |
| static void hamt_xs_rethrow(pTHX_ SV *err) { |
| 407 |
73 |
▶ | sv_setsv(ERRSV, err); |
Branch: 1 branch
| Branch | True | False | Total |
| sv_setsv(ERRSV, err); |
73 |
0 |
73 |
|
| 408 |
73 |
| SvREFCNT_dec(err); |
| 409 |
73 |
| croak(NULL); |
| 410 |
|
| } |
| 411 |
|
| |
| 412 |
|
| /* equals with a comparison sub: called with the two values, true or false. |
| 413 |
|
| * Nothing is allocated across the call, so an error is thrown again from |
| 414 |
|
| * here, through the comparison. */ |
| 415 |
350 |
| static int hamt_xs_eq_cb(void *ud, void *cx, void *va, void *vb) { |
| 416 |
|
| dTHXa(cx); |
| 417 |
|
| SV *args[2], *res, *err; |
| 418 |
|
| int truth; |
| 419 |
|
| |
| 420 |
|
| PERL_UNUSED_ARG(cx); |
| 421 |
350 |
| args[0] = (SV *)va; |
| 422 |
350 |
| args[1] = (SV *)vb; |
| 423 |
350 |
| ENTER; |
| 424 |
350 |
| SAVETMPS; |
| 425 |
350 |
| hamt_xs_call_enter(aTHX); |
| 426 |
350 |
| err = hamt_xs_call_err(aTHX_ hamt_xs_call(aTHX_ (SV *)ud, args, 2, &res), 0); |
| 427 |
350 |
▶ | truth = !err && SvTRUE(res) ? 1 : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| truth = !err && SvTRUE(res) ? 1 : 0; |
339 |
11 |
350 |
| truth = !err && SvTRUE(res) ? 1 : 0; |
338 |
1 |
339 |
|
| 428 |
350 |
| hamt_xs_call_leave(aTHX); |
| 429 |
350 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
350 |
0 |
350 |
|
| 430 |
350 |
| LEAVE; |
| 431 |
350 |
▶ | if (err) hamt_xs_rethrow(aTHX_ err); |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) hamt_xs_rethrow(aTHX_ err); |
11 |
339 |
350 |
|
| 432 |
339 |
| return truth; |
| 433 |
|
| } |
| 434 |
|
| |
| 435 |
|
| /* merge with a resolver: called with the key and the two values, and what |
| 436 |
|
| * it returns is the value the result holds. |
| 437 |
|
| * |
| 438 |
|
| * This one runs under G_EVAL. The merge is half way through building nodes |
| 439 |
|
| * when it calls, and a die that jumped out past it would leak every one of |
| 440 |
|
| * them; so the error is caught and kept, the merge is told to stop and |
| 441 |
|
| * unwinds what it built, and the caller dies again with the same error. |
| 442 |
|
| * diff keeps its error the same way. */ |
| 443 |
|
| typedef struct hamt_xs_sub { |
| 444 |
|
| SV *code; |
| 445 |
|
| SV *err; |
| 446 |
|
| } hamt_xs_sub; |
| 447 |
|
| |
| 448 |
1907 |
| static int hamt_xs_resolve_cb(void *ud, void *cx, hamt_entry *ea, hamt_entry *eb, |
| 449 |
|
| hamt_entry **out) { |
| 450 |
|
| dTHXa(cx); |
| 451 |
1907 |
| hamt_xs_sub *r = (hamt_xs_sub *)ud; |
| 452 |
|
| SV *args[3], *res; |
| 453 |
1907 |
| int rc = HAMT_OK; |
| 454 |
|
| |
| 455 |
1907 |
| ENTER; |
| 456 |
1907 |
| SAVETMPS; |
| 457 |
1907 |
| args[0] = sv_2mortal(hamt_key_to_sv(aTHX_ ea)); |
| 458 |
1907 |
| args[1] = hamt_xs_val_sv(aTHX_ ea); |
| 459 |
1907 |
| args[2] = hamt_xs_val_sv(aTHX_ eb); |
| 460 |
1907 |
| hamt_xs_call_enter(aTHX); |
| 461 |
1907 |
| r->err = hamt_xs_call_err(aTHX_ hamt_xs_call(aTHX_ r->code, args, 3, &res), 0); |
| 462 |
1907 |
| hamt_xs_call_leave(aTHX); |
| 463 |
|
| |
| 464 |
1907 |
▶ | if (r->err) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->err) { |
7 |
1900 |
1907 |
|
| 465 |
7 |
| rc = HAMT_ERR_ABORT; |
| 466 |
|
| } |
| 467 |
|
| /* What a sub returns is always a copy, so "it gave back the one it was |
| 468 |
|
| * handed" cannot be seen by address. It is seen by value, as `equals` |
| 469 |
|
| * compares values, and then the entry already there is kept: that is |
| 470 |
|
| * what lets a merge that changes nothing return the map it started from. */ |
| 471 |
1900 |
▶ | else if (ea->val && hamt_sv_eq_cx(cx, res, (SV *)ea->val)) *out = hamt_entry_retain(ea); |
Branch: 2 branches
| Branch | True | False | Total |
| else if (ea->val && hamt_sv_eq_cx(cx, res, (SV *)ea->val)) *out = hamt_entry_retain(ea); |
1900 |
0 |
1900 |
| else if (ea->val && hamt_sv_eq_cx(cx, res, (SV *)ea->val)) *out = hamt_entry_retain(ea); |
103 |
1797 |
1900 |
|
| 472 |
1797 |
▶ | else if (eb->val && hamt_sv_eq_cx(cx, res, (SV *)eb->val)) *out = hamt_entry_retain(eb); |
Branch: 2 branches
| Branch | True | False | Total |
| else if (eb->val && hamt_sv_eq_cx(cx, res, (SV *)eb->val)) *out = hamt_entry_retain(eb); |
1797 |
0 |
1797 |
| else if (eb->val && hamt_sv_eq_cx(cx, res, (SV *)eb->val)) *out = hamt_entry_retain(eb); |
501 |
1296 |
1797 |
|
| 473 |
|
| else { |
| 474 |
1296 |
| SV *copy = hamt_sv_copy_in(aTHX_ res); |
| 475 |
1296 |
| *out = hamt_entry_new(cx, ea->key, ea->klen, ea->utf8, ea->hash, (void *)copy); |
| 476 |
1296 |
| SvREFCNT_dec(copy); |
| 477 |
1296 |
▶ | if (!*out) rc = HAMT_ERR_OOM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*out) rc = HAMT_ERR_OOM; |
0 |
1296 |
1296 |
|
| 478 |
|
| } |
| 479 |
|
| |
| 480 |
1907 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
1907 |
0 |
1907 |
|
| 481 |
1907 |
| LEAVE; |
| 482 |
1907 |
| return rc; |
| 483 |
|
| } |
| 484 |
|
| |
| 485 |
|
| /* diff: called with the key, the value it had, the value it has, and one of |
| 486 |
|
| * 'add', 'remove', 'change'. A walk: `last` stops it, `next` goes on. The |
| 487 |
|
| * caller is between hamt_xs_call_enter and _leave; an error stops the walk |
| 488 |
|
| * and is kept for the caller to throw once it has left. */ |
| 489 |
16560 |
| static int hamt_xs_diff_cb(void *ud, void *cx, int kind, hamt_entry *ea, |
| 490 |
|
| hamt_entry *eb) { |
| 491 |
|
| dTHXa(cx); |
| 492 |
|
| static const char *const name[4] = { "", "add", "remove", "change" }; |
| 493 |
16560 |
| hamt_xs_sub *r = (hamt_xs_sub *)ud; |
| 494 |
|
| SV *args[4]; |
| 495 |
|
| int how; |
| 496 |
|
| |
| 497 |
|
| PERL_UNUSED_ARG(cx); |
| 498 |
16560 |
| ENTER; |
| 499 |
16560 |
| SAVETMPS; |
| 500 |
16560 |
▶ | args[0] = sv_2mortal(hamt_key_to_sv(aTHX_ ea ? ea : eb)); |
Branch: 1 branch
| Branch | True | False | Total |
| args[0] = sv_2mortal(hamt_key_to_sv(aTHX_ ea ? ea : eb)); |
12629 |
3931 |
16560 |
|
| 501 |
16560 |
| args[1] = hamt_xs_val_sv(aTHX_ ea); |
| 502 |
16560 |
| args[2] = hamt_xs_val_sv(aTHX_ eb); |
| 503 |
16560 |
| args[3] = sv_2mortal(newSVpv(name[kind], 0)); |
| 504 |
16560 |
| how = hamt_xs_call(aTHX_ r->code, args, 4, NULL); |
| 505 |
16560 |
| r->err = hamt_xs_call_err(aTHX_ how, 1); |
| 506 |
16560 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
16560 |
0 |
16560 |
|
| 507 |
16560 |
| LEAVE; |
| 508 |
16560 |
▶ | return how == HAMT_XS_LAST || r->err; |
Branch: 2 branches
| Branch | True | False | Total |
| return how == HAMT_XS_LAST || r->err; |
16559 |
1 |
16560 |
| return how == HAMT_XS_LAST || r->err; |
3 |
16556 |
16559 |
|
| 509 |
|
| } |
| 510 |
|
| |
| 511 |
|
| /* The struct behind an object, or NULL: for a method that takes "something |
| 512 |
|
| * that may be a map" and has an answer when it is not. */ |
| 513 |
2046 |
| static hamt_map *hamt_handle_try(pTHX_ SV *sv, const MGVTBL *vt) { |
| 514 |
2046 |
▶ | if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
2035 |
11 |
2046 |
| if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
2035 |
0 |
2035 |
|
| 515 |
|
| MAGIC *mg; |
| 516 |
2064 |
▶ | for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) |
Branch: 1 branch
| Branch | True | False | Total |
| for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) |
2032 |
32 |
2064 |
|
| 517 |
2032 |
▶ | if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == vt) |
Branch: 2 branches
| Branch | True | False | Total |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == vt) |
2032 |
0 |
2032 |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == vt) |
2003 |
29 |
2032 |
|
| 518 |
2003 |
| return (hamt_map *)mg->mg_ptr; |
| 519 |
|
| } |
| 520 |
43 |
| return NULL; |
| 521 |
|
| } |
| 522 |
|
| |
| 523 |
|
| /* ---- transients --------------------------------------------------------- |
| 524 |
|
| * |
| 525 |
|
| * A transient is a handle that IS updated in place: the same put and delete |
| 526 |
|
| * as everywhere else, on a handle that lives across calls. It starts out |
| 527 |
|
| * sharing its root with the map it was opened from, so its first updates |
| 528 |
|
| * copy and that map is untouched; `persistent` hands the root to a new map |
| 529 |
|
| * and closes the transient, so nothing can reach the root through it again. |
| 530 |
|
| * |
| 531 |
|
| * It remembers the class of the map it came from, to make its result one. */ |
| 532 |
|
| |
| 533 |
|
| typedef struct hamt_txn { |
| 534 |
|
| hamt_map m; /* first, so a transient can be updated as a map is */ |
| 535 |
|
| HV *origin; /* the class `persistent` blesses into */ |
| 536 |
|
| int open; |
| 537 |
|
| } hamt_txn; |
| 538 |
|
| |
| 539 |
250 |
| static int hamt_txn_free(pTHX_ SV *sv, MAGIC *mg) { |
| 540 |
250 |
| hamt_txn *t = (hamt_txn *)mg->mg_ptr; |
| 541 |
|
| PERL_UNUSED_ARG(sv); |
| 542 |
250 |
▶ | if (t) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (t) { |
250 |
0 |
250 |
|
| 543 |
250 |
| hamt_node *root = t->m.root; |
| 544 |
250 |
| HV *origin = t->origin; |
| 545 |
250 |
| mg->mg_ptr = NULL; |
| 546 |
250 |
| HAMT_FREE(HAMT_CX, t, sizeof(hamt_txn)); |
| 547 |
250 |
▶ | if (origin && !PL_dirty) SvREFCNT_dec((SV *)origin); |
Branch: 2 branches
| Branch | True | False | Total |
| if (origin && !PL_dirty) SvREFCNT_dec((SV *)origin); |
33 |
217 |
250 |
| if (origin && !PL_dirty) SvREFCNT_dec((SV *)origin); |
33 |
0 |
33 |
|
| 548 |
250 |
▶ | if (root) hamt_node_release(HAMT_CX, root); |
Branch: 1 branch
| Branch | True | False | Total |
| if (root) hamt_node_release(HAMT_CX, root); |
32 |
218 |
250 |
|
| 549 |
|
| } |
| 550 |
250 |
| return 0; |
| 551 |
|
| } |
| 552 |
|
| |
| 553 |
|
| static MGVTBL hamt_map_txn_vtbl = { |
| 554 |
|
| NULL, NULL, NULL, NULL, hamt_txn_free, NULL, hamt_handle_dup, NULL |
| 555 |
|
| }; |
| 556 |
|
| |
| 557 |
|
| static MGVTBL hamt_set_txn_vtbl = { |
| 558 |
|
| NULL, NULL, NULL, NULL, hamt_txn_free, NULL, hamt_handle_dup, NULL |
| 559 |
|
| }; |
| 560 |
|
| |
| 561 |
|
| /* The struct behind a transient that is still open, or a croak. */ |
| 562 |
168535 |
| static hamt_txn *hamt_txn_handle(pTHX_ SV *sv, const MGVTBL *vt, const char *what) { |
| 563 |
168535 |
▶ | if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
168533 |
2 |
168535 |
| if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
168533 |
0 |
168533 |
|
| 564 |
|
| MAGIC *mg; |
| 565 |
168535 |
▶ | for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { |
168530 |
5 |
168535 |
|
| 566 |
|
| hamt_txn *t; |
| 567 |
168530 |
▶ | if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != vt) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != vt) continue; |
168530 |
0 |
168530 |
| if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != vt) continue; |
2 |
168528 |
168530 |
|
| 568 |
168528 |
| t = (hamt_txn *)mg->mg_ptr; |
| 569 |
168528 |
▶ | if (!t) croak("%s: this transient belongs to another thread", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t) croak("%s: this transient belongs to another thread", what); |
0 |
168528 |
168528 |
|
| 570 |
168528 |
▶ | if (!t->open) croak("%s: this transient is closed", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t->open) croak("%s: this transient is closed", what); |
12 |
168516 |
168528 |
|
| 571 |
168516 |
| return t; |
| 572 |
|
| } |
| 573 |
|
| } |
| 574 |
7 |
| croak("%s: not a transient", what); |
| 575 |
|
| return NULL; |
| 576 |
|
| } |
| 577 |
|
| |
| 578 |
236 |
| static SV *hamt_txn_new(pTHX_ const MGVTBL *vt, const char *klass, HV *origin, |
| 579 |
|
| hamt_node *root, size_t count, const char *what) { |
| 580 |
236 |
| hamt_txn *t = (hamt_txn *)HAMT_ALLOC(HAMT_CX, sizeof(hamt_txn)); |
| 581 |
|
| SV *inner, *rv; |
| 582 |
|
| MAGIC *mg; |
| 583 |
|
| |
| 584 |
236 |
▶ | if (!t) croak("%s: out of memory", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t) croak("%s: out of memory", what); |
0 |
236 |
236 |
|
| 585 |
236 |
▶ | t->m.root = root ? hamt_node_retain(root) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| t->m.root = root ? hamt_node_retain(root) : NULL; |
230 |
6 |
236 |
|
| 586 |
236 |
| t->m.count = count; |
| 587 |
236 |
| t->origin = (HV *)SvREFCNT_inc_simple_NN((SV *)origin); |
| 588 |
236 |
| t->open = 1; |
| 589 |
|
| |
| 590 |
236 |
| inner = newSV(0); |
| 591 |
236 |
| mg = sv_magicext(inner, NULL, PERL_MAGIC_ext, (MGVTBL *)vt, (const char *)t, 0); |
| 592 |
236 |
| mg->mg_flags |= MGf_DUP; |
| 593 |
236 |
| rv = sv_2mortal(newRV_noinc(inner)); |
| 594 |
236 |
| sv_bless(rv, gv_stashpv(klass, GV_ADD)); |
| 595 |
236 |
| return rv; |
| 596 |
|
| } |
| 597 |
|
| |
| 598 |
|
| /* ---- the vector's handle ------------------------------------------------- |
| 599 |
|
| * |
| 600 |
|
| * The same arrangement as a map's, around a hamt_vec: a blessed reference |
| 601 |
|
| * whose scalar carries the struct in magic marked by a vtable of its own. */ |
| 602 |
|
| |
| 603 |
240655 |
| static int hamt_vec_handle_free(pTHX_ SV *sv, MAGIC *mg) { |
| 604 |
240655 |
| hamt_vec *v = (hamt_vec *)mg->mg_ptr; |
| 605 |
|
| PERL_UNUSED_ARG(sv); |
| 606 |
240655 |
▶ | if (v) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (v) { |
240655 |
0 |
240655 |
|
| 607 |
240655 |
| hamt_vec gone = *v; |
| 608 |
240655 |
| mg->mg_ptr = NULL; |
| 609 |
240655 |
| HAMT_FREE(HAMT_CX, v, sizeof(hamt_vec)); |
| 610 |
240655 |
| hamt_vec_clear(HAMT_CX, &gone); |
| 611 |
|
| } |
| 612 |
240655 |
| return 0; |
| 613 |
|
| } |
| 614 |
|
| |
| 615 |
|
| static MGVTBL hamt_vec_vtbl = { |
| 616 |
|
| NULL, NULL, NULL, NULL, hamt_vec_handle_free, NULL, hamt_handle_dup, NULL |
| 617 |
|
| }; |
| 618 |
|
| |
| 619 |
646652 |
| static hamt_vec *hamt_vec_handle(pTHX_ SV *sv, const char *what) { |
| 620 |
646652 |
▶ | if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
646629 |
23 |
646652 |
| if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
646629 |
0 |
646629 |
|
| 621 |
|
| MAGIC *mg; |
| 622 |
646642 |
▶ | for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { |
646591 |
51 |
646642 |
|
| 623 |
646591 |
▶ | if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &hamt_vec_vtbl) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &hamt_vec_vtbl) continue; |
646591 |
0 |
646591 |
| if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &hamt_vec_vtbl) continue; |
13 |
646578 |
646591 |
|
| 624 |
646578 |
▶ | if (!mg->mg_ptr) croak("%s: this vector belongs to another thread", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!mg->mg_ptr) croak("%s: this vector belongs to another thread", what); |
0 |
646578 |
646578 |
|
| 625 |
646578 |
| return (hamt_vec *)mg->mg_ptr; |
| 626 |
|
| } |
| 627 |
|
| } |
| 628 |
74 |
| croak("%s: not a vector", what); |
| 629 |
|
| return NULL; |
| 630 |
|
| } |
| 631 |
|
| |
| 632 |
65 |
| static hamt_vec *hamt_vec_handle_try(pTHX_ SV *sv) { |
| 633 |
65 |
▶ | if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
63 |
2 |
65 |
| if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
63 |
0 |
63 |
|
| 634 |
|
| MAGIC *mg; |
| 635 |
77 |
▶ | for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) |
Branch: 1 branch
| Branch | True | False | Total |
| for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) |
62 |
15 |
77 |
|
| 636 |
62 |
▶ | if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &hamt_vec_vtbl) |
Branch: 2 branches
| Branch | True | False | Total |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &hamt_vec_vtbl) |
62 |
0 |
62 |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &hamt_vec_vtbl) |
48 |
14 |
62 |
|
| 637 |
48 |
| return (hamt_vec *)mg->mg_ptr; |
| 638 |
|
| } |
| 639 |
17 |
| return NULL; |
| 640 |
|
| } |
| 641 |
|
| |
| 642 |
|
| /* A new object, mortal, onto the same vector as `src`, or onto an empty one. */ |
| 643 |
240641 |
| static SV *hamt_vec_handle_new(pTHX_ HV *stash, const hamt_vec *src, hamt_vec **out, |
| 644 |
|
| const char *what) { |
| 645 |
240641 |
| hamt_vec *v = (hamt_vec *)HAMT_ALLOC(HAMT_CX, sizeof(hamt_vec)); |
| 646 |
|
| SV *inner, *rv; |
| 647 |
|
| MAGIC *mg; |
| 648 |
|
| |
| 649 |
240641 |
▶ | if (!v) croak("%s: out of memory", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!v) croak("%s: out of memory", what); |
0 |
240641 |
240641 |
|
| 650 |
240641 |
▶ | if (src) hamt_vec_share(v, src); |
Branch: 1 branch
| Branch | True | False | Total |
| if (src) hamt_vec_share(v, src); |
158010 |
82631 |
240641 |
|
| 651 |
82631 |
| else hamt_vec_init(v); |
| 652 |
|
| |
| 653 |
240641 |
| inner = newSV(0); |
| 654 |
240641 |
| mg = sv_magicext(inner, NULL, PERL_MAGIC_ext, &hamt_vec_vtbl, (const char *)v, 0); |
| 655 |
240641 |
| mg->mg_flags |= MGf_DUP; |
| 656 |
240641 |
| rv = sv_2mortal(newRV_noinc(inner)); |
| 657 |
240641 |
| sv_bless(rv, stash); |
| 658 |
240641 |
| *out = v; |
| 659 |
240641 |
| return rv; |
| 660 |
|
| } |
| 661 |
|
| |
| 662 |
|
| /* A walk that calls Perl holds a struct of its own onto the vector, released |
| 663 |
|
| * by the save stack: what the sub does to the object, or to the last |
| 664 |
|
| * variable holding it, cannot reach the nodes being walked. Between an |
| 665 |
|
| * ENTER and its LEAVE. */ |
| 666 |
172 |
| static void hamt_xs_vec_unhold(pTHX_ void *p) { |
| 667 |
172 |
| hamt_vec *v = (hamt_vec *)p; |
| 668 |
172 |
| hamt_vec gone = *v; |
| 669 |
172 |
| HAMT_FREE(HAMT_CX, v, sizeof(hamt_vec)); |
| 670 |
172 |
| hamt_vec_clear(HAMT_CX, &gone); |
| 671 |
172 |
| } |
| 672 |
|
| |
| 673 |
88 |
| static hamt_vec *hamt_xs_hold_vec(pTHX_ const hamt_vec *src, const char *what) { |
| 674 |
88 |
| hamt_vec *v = (hamt_vec *)HAMT_ALLOC(HAMT_CX, sizeof(hamt_vec)); |
| 675 |
88 |
▶ | if (!v) croak("%s: out of memory", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!v) croak("%s: out of memory", what); |
0 |
88 |
88 |
|
| 676 |
88 |
| hamt_vec_share(v, src); |
| 677 |
88 |
| SAVEDESTRUCTOR_X(hamt_xs_vec_unhold, v); |
| 678 |
88 |
| return v; |
| 679 |
|
| } |
| 680 |
|
| |
| 681 |
|
| /* An index as an array takes one: negative counts from the end. 1 and the |
| 682 |
|
| * position when it names an element, 0 when it does not. */ |
| 683 |
74224 |
| static int hamt_xs_index(pTHX_ SV *sv, size_t count, size_t *out) { |
| 684 |
74224 |
| IV i = SvIV(sv); |
| 685 |
74224 |
▶ | if (i < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i < 0) { |
215 |
74009 |
74224 |
|
| 686 |
215 |
▶ | if ((UV)(-(i + 1)) >= (UV)count) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((UV)(-(i + 1)) >= (UV)count) return 0; |
10 |
205 |
215 |
|
| 687 |
205 |
| *out = count - (size_t)(-(i + 1)) - 1; |
| 688 |
205 |
| return 1; |
| 689 |
|
| } |
| 690 |
74009 |
▶ | if ((UV)i >= (UV)count) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((UV)i >= (UV)count) return 0; |
271 |
73738 |
74009 |
|
| 691 |
73738 |
| *out = (size_t)i; |
| 692 |
73738 |
| return 1; |
| 693 |
|
| } |
| 694 |
|
| |
| 695 |
|
| /* One value onto the end of a vector the caller owns outright. Copied and |
| 696 |
|
| * made read-only on the way in, as a map's values are. */ |
| 697 |
449919 |
| static void hamt_xs_vec_push(pTHX_ hamt_vec *v, SV *valsv, const char *what) { |
| 698 |
449919 |
| SV *copy = hamt_sv_copy_in(aTHX_ valsv); |
| 699 |
449917 |
| int rc = hamt_vec_push(HAMT_CX, v, (void *)copy); |
| 700 |
449917 |
| SvREFCNT_dec(copy); |
| 701 |
449917 |
▶ | if (rc) croak("%s: out of memory", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc) croak("%s: out of memory", what); |
0 |
449917 |
449917 |
|
| 702 |
449917 |
| } |
| 703 |
|
| |
| 704 |
|
| /* One value replaced, likewise. A value that IS the scalar already there |
| 705 |
|
| * changes nothing. The scalar it displaces is released last. */ |
| 706 |
40342 |
| static void hamt_xs_vec_set(pTHX_ hamt_vec *v, size_t i, SV *valsv, const char *what) { |
| 707 |
|
| SV *copy; |
| 708 |
|
| void *dead; |
| 709 |
|
| int rc; |
| 710 |
|
| |
| 711 |
40342 |
▶ | if (hamt_vec_get(v, i) == (void *)valsv) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_vec_get(v, i) == (void *)valsv) return; |
0 |
40342 |
40342 |
|
| 712 |
40342 |
| copy = hamt_sv_copy_in(aTHX_ valsv); |
| 713 |
40342 |
| rc = hamt_vec_set(HAMT_CX, v, i, (void *)copy, &dead); |
| 714 |
40342 |
| SvREFCNT_dec(copy); |
| 715 |
40342 |
▶ | if (rc) croak("%s: out of memory", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc) croak("%s: out of memory", what); |
0 |
40342 |
40342 |
|
| 716 |
40342 |
▶ | if (dead) HAMT_VAL_RELEASE(HAMT_CX, dead); |
Branch: 1 branch
| Branch | True | False | Total |
| if (dead) HAMT_VAL_RELEASE(HAMT_CX, dead); |
40342 |
0 |
40342 |
|
| 717 |
|
| } |
| 718 |
|
| |
| 719 |
103230 |
| static void hamt_xs_vec_pop(pTHX_ hamt_vec *v, const char *what) { |
| 720 |
|
| void *dead; |
| 721 |
103230 |
▶ | if (hamt_vec_pop(HAMT_CX, v, &dead)) croak("%s: out of memory", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_vec_pop(HAMT_CX, v, &dead)) croak("%s: out of memory", what); |
0 |
103230 |
103230 |
|
| 722 |
103230 |
▶ | if (dead) HAMT_VAL_RELEASE(HAMT_CX, dead); |
Branch: 1 branch
| Branch | True | False | Total |
| if (dead) HAMT_VAL_RELEASE(HAMT_CX, dead); |
103228 |
2 |
103230 |
|
| 723 |
103230 |
| } |
| 724 |
|
| |
| 725 |
|
| /* `out`, which must be empty, becomes `v` with the `drop` values from `at` |
| 726 |
|
| * taken out and everything in `mid` put in their place: the one operation |
| 727 |
|
| * behind insert and remove. `mid` may be NULL. It is two cuts and two |
| 728 |
|
| * joins, and no Perl runs in it, so the pieces are plain structs cleaned up |
| 729 |
|
| * here. */ |
| 730 |
7168 |
| static void hamt_xs_vec_splice(pTHX_ hamt_vec *out, const hamt_vec *v, size_t at, |
| 731 |
|
| size_t drop, const hamt_vec *mid, const char *what) { |
| 732 |
|
| hamt_vec left, right, joined, res; |
| 733 |
7168 |
| size_t after = at + drop; |
| 734 |
7168 |
| int rc = 0; |
| 735 |
|
| |
| 736 |
7168 |
| hamt_vec_init(&left); |
| 737 |
7168 |
| hamt_vec_init(&right); |
| 738 |
7168 |
| hamt_vec_init(&joined); |
| 739 |
7168 |
| hamt_vec_init(&res); |
| 740 |
|
| |
| 741 |
7168 |
▶ | if (at > 0) rc = hamt_vec_slice(HAMT_CX, &left, v, 0, at); |
Branch: 1 branch
| Branch | True | False | Total |
| if (at > 0) rc = hamt_vec_slice(HAMT_CX, &left, v, 0, at); |
6166 |
1002 |
7168 |
|
| 742 |
7168 |
▶ | if (!rc && after < v->count) rc = hamt_vec_slice(HAMT_CX, &right, v, after, v->count - after); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!rc && after < v->count) rc = hamt_vec_slice(HAMT_CX, &right, v, after, v->count - after); |
7168 |
0 |
7168 |
| if (!rc && after < v->count) rc = hamt_vec_slice(HAMT_CX, &right, v, after, v->count - after); |
5858 |
1310 |
7168 |
|
| 743 |
7168 |
▶ | if (!rc) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rc) { |
7168 |
0 |
7168 |
|
| 744 |
7168 |
▶ | if (mid) rc = hamt_vec_concat(HAMT_CX, &joined, &left, mid); |
Branch: 1 branch
| Branch | True | False | Total |
| if (mid) rc = hamt_vec_concat(HAMT_CX, &joined, &left, mid); |
4764 |
2404 |
7168 |
|
| 745 |
2404 |
| else hamt_vec_share(&joined, &left); |
| 746 |
|
| } |
| 747 |
7168 |
▶ | if (!rc) rc = hamt_vec_concat(HAMT_CX, &res, &joined, &right); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rc) rc = hamt_vec_concat(HAMT_CX, &res, &joined, &right); |
7168 |
0 |
7168 |
|
| 748 |
|
| |
| 749 |
7168 |
| hamt_vec_clear(HAMT_CX, &left); |
| 750 |
7168 |
| hamt_vec_clear(HAMT_CX, &right); |
| 751 |
7168 |
| hamt_vec_clear(HAMT_CX, &joined); |
| 752 |
7168 |
▶ | if (rc) croak("%s: out of memory", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc) croak("%s: out of memory", what); |
0 |
7168 |
7168 |
|
| 753 |
7168 |
| *out = res; |
| 754 |
7168 |
| } |
| 755 |
|
| |
| 756 |
|
| /* Where an insert goes: 0 up to the count itself, which is the end; a |
| 757 |
|
| * negative one counts from the end, so that -1 is before the last value, |
| 758 |
|
| * as splice has it. 1 and the position, or 0. */ |
| 759 |
4770 |
| static int hamt_xs_gap(pTHX_ SV *sv, size_t count, size_t *out) { |
| 760 |
4770 |
| IV i = SvIV(sv); |
| 761 |
4770 |
▶ | if (i < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i < 0) { |
3 |
4767 |
4770 |
|
| 762 |
3 |
▶ | if ((UV)(-(i + 1)) >= (UV)count) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((UV)(-(i + 1)) >= (UV)count) return 0; |
1 |
2 |
3 |
|
| 763 |
2 |
| *out = count - (size_t)(-(i + 1)) - 1; |
| 764 |
2 |
| return 1; |
| 765 |
|
| } |
| 766 |
4767 |
▶ | if ((UV)i > (UV)count) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((UV)i > (UV)count) return 0; |
3 |
4764 |
4767 |
|
| 767 |
4764 |
| *out = (size_t)i; |
| 768 |
4764 |
| return 1; |
| 769 |
|
| } |
| 770 |
|
| |
| 771 |
|
| /* The vector's transient: the struct, the class it closes into, and whether |
| 772 |
|
| * it is still open. */ |
| 773 |
|
| typedef struct hamt_vtxn { |
| 774 |
|
| hamt_vec v; |
| 775 |
|
| HV *origin; |
| 776 |
|
| int open; |
| 777 |
|
| } hamt_vtxn; |
| 778 |
|
| |
| 779 |
312 |
| static int hamt_vtxn_free(pTHX_ SV *sv, MAGIC *mg) { |
| 780 |
312 |
| hamt_vtxn *t = (hamt_vtxn *)mg->mg_ptr; |
| 781 |
|
| PERL_UNUSED_ARG(sv); |
| 782 |
312 |
▶ | if (t) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (t) { |
312 |
0 |
312 |
|
| 783 |
312 |
| hamt_vec gone = t->v; |
| 784 |
312 |
| HV *origin = t->origin; |
| 785 |
312 |
| mg->mg_ptr = NULL; |
| 786 |
312 |
| HAMT_FREE(HAMT_CX, t, sizeof(hamt_vtxn)); |
| 787 |
312 |
▶ | if (origin && !PL_dirty) SvREFCNT_dec((SV *)origin); |
Branch: 2 branches
| Branch | True | False | Total |
| if (origin && !PL_dirty) SvREFCNT_dec((SV *)origin); |
13 |
299 |
312 |
| if (origin && !PL_dirty) SvREFCNT_dec((SV *)origin); |
13 |
0 |
13 |
|
| 788 |
312 |
| hamt_vec_clear(HAMT_CX, &gone); |
| 789 |
|
| } |
| 790 |
312 |
| return 0; |
| 791 |
|
| } |
| 792 |
|
| |
| 793 |
|
| static MGVTBL hamt_vec_txn_vtbl = { |
| 794 |
|
| NULL, NULL, NULL, NULL, hamt_vtxn_free, NULL, hamt_handle_dup, NULL |
| 795 |
|
| }; |
| 796 |
|
| |
| 797 |
348634 |
| static hamt_vtxn *hamt_vtxn_handle(pTHX_ SV *sv, const char *what) { |
| 798 |
348634 |
▶ | if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
348634 |
0 |
348634 |
| if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
348634 |
0 |
348634 |
|
| 799 |
|
| MAGIC *mg; |
| 800 |
348635 |
▶ | for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { |
348634 |
1 |
348635 |
|
| 801 |
|
| hamt_vtxn *t; |
| 802 |
348634 |
▶ | if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &hamt_vec_txn_vtbl) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &hamt_vec_txn_vtbl) continue; |
348634 |
0 |
348634 |
| if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &hamt_vec_txn_vtbl) continue; |
1 |
348633 |
348634 |
|
| 803 |
348633 |
| t = (hamt_vtxn *)mg->mg_ptr; |
| 804 |
348633 |
▶ | if (!t) croak("%s: this transient belongs to another thread", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t) croak("%s: this transient belongs to another thread", what); |
0 |
348633 |
348633 |
|
| 805 |
348633 |
▶ | if (!t->open) croak("%s: this transient is closed", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t->open) croak("%s: this transient is closed", what); |
6 |
348627 |
348633 |
|
| 806 |
348627 |
| return t; |
| 807 |
|
| } |
| 808 |
|
| } |
| 809 |
1 |
| croak("%s: not a transient", what); |
| 810 |
|
| return NULL; |
| 811 |
|
| } |
| 812 |
|
| |
| 813 |
|
| #endif |