| 1 |
|
| #ifndef STORE_CORE_H |
| 2 |
|
| #define STORE_CORE_H |
| 3 |
|
| |
| 4 |
|
| /* The cell: one value, and the one place it changes. |
| 5 |
|
| * |
| 6 |
|
| * ---- the handle -------------------------------------------------------- |
| 7 |
|
| * |
| 8 |
|
| * A store is a blessed reference to a scalar that carries the C struct in |
| 9 |
|
| * ext magic, marked by the address of a vtable in this file: HAMT's handle, |
| 10 |
|
| * for HAMT's reasons. Magic cannot be forged from Perl or carried through a |
| 11 |
|
| * serialiser, so an object without it is simply not a store and every method |
| 12 |
|
| * says so; the struct is released when the scalar is freed, so there is no |
| 13 |
|
| * DESTROY to call twice or to shadow in a subclass; and a new thread does |
| 14 |
|
| * not get the handle, by CLONE_SKIP and, failing that, by a dup hook that |
| 15 |
|
| * empties the clone's magic. */ |
| 16 |
|
| |
| 17 |
|
| /* Not during global destruction: by then perl is freeing every scalar in an |
| 18 |
|
| * order of its own, and one this store still holds may already be gone. */ |
| 19 |
264878 |
| static void store_sv_release(pTHX_ SV *sv) { |
| 20 |
264878 |
▶ | if (sv && !PL_dirty) SvREFCNT_dec(sv); |
Branch: 2 branches
| Branch | True | False | Total |
| if (sv && !PL_dirty) SvREFCNT_dec(sv); |
262629 |
2249 |
264878 |
| if (sv && !PL_dirty) SvREFCNT_dec(sv); |
262629 |
0 |
262629 |
|
| 21 |
264878 |
| } |
| 22 |
|
| |
| 23 |
|
| typedef struct store { |
| 24 |
|
| SV *value; /* the current value: owned, read-only, never NULL */ |
| 25 |
|
| hamt_vec *past; /* the values before it, oldest first: never NULL */ |
| 26 |
|
| hamt_vec *future; /* the values undone, the next one to redo last */ |
| 27 |
|
| size_t limit; /* the most steps `past` keeps, when `limited` */ |
| 28 |
|
| size_t past_skip; /* its oldest steps, beyond the limit, not yet cut */ |
| 29 |
|
| int limited; |
| 30 |
|
| |
| 31 |
|
| /* Phase 05: the watchers, in the order they were added, and the changes |
| 32 |
|
| * waiting to be told to them. A watcher removed during a round is left |
| 33 |
|
| * in place with no code and swept out when the round is over. */ |
| 34 |
|
| struct store_watcher *watchers; |
| 35 |
|
| size_t nwatchers, cwatchers, live_watchers; |
| 36 |
|
| UV next_id; |
| 37 |
|
| struct store_change *queue; |
| 38 |
|
| size_t qhead, qlen, qcap; |
| 39 |
|
| int delivering; |
| 40 |
|
| |
| 41 |
|
| /* Phase 06: the sub a new value must satisfy, or NULL; and whether it |
| 42 |
|
| * is running, when nothing may change the store. */ |
| 43 |
|
| SV *validator; |
| 44 |
|
| state_store_validate_fn cvalidator; /* or one from the C ABI */ |
| 45 |
|
| void *cvalidator_ud; |
| 46 |
|
| int validating; |
| 47 |
|
| |
| 48 |
|
| /* Phase 07: how many transactions are open. Inside one, a change records |
| 49 |
|
| * no step and is told to nobody: the transaction does both, once, when |
| 50 |
|
| * the outermost one ends. */ |
| 51 |
|
| int txn_depth; |
| 52 |
|
| |
| 53 |
|
| /* Phase 08: named values, or NULL until the first is named. */ |
| 54 |
|
| hamt_map *checkpoints; |
| 55 |
|
| } store; |
| 56 |
|
| |
| 57 |
|
| /* A croak for what cannot be done inside a transaction. */ |
| 58 |
54607 |
| static void store_check_no_txn(pTHX_ const store *s, const char *what) { |
| 59 |
54607 |
▶ | if (s->txn_depth) croak("%s: not inside a transaction", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->txn_depth) croak("%s: not inside a transaction", what); |
5 |
54602 |
54607 |
|
| 60 |
54602 |
| } |
| 61 |
|
| |
| 62 |
|
| /* A validator from Perl: a code reference, or undef for none. The CV, not |
| 63 |
|
| * the reference to it. */ |
| 64 |
48 |
| static SV *store_validator_arg(pTHX_ SV *sv, const char *what) { |
| 65 |
48 |
| SvGETMAGIC(sv); |
| 66 |
48 |
▶ | if (!SvOK(sv)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(sv)) return NULL; |
6 |
42 |
48 |
|
| 67 |
42 |
▶ | if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVCV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVCV) |
38 |
4 |
42 |
| if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVCV) |
4 |
34 |
38 |
|
| 68 |
8 |
| croak("%s: the validator is not a code reference", what); |
| 69 |
34 |
| return SvRV(sv); |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
|
| /* A croak for anything that would change the store while its validator is |
| 73 |
|
| * deciding whether a change may be made. */ |
| 74 |
377965 |
| static void store_check_mutable(pTHX_ const store *s, const char *what) { |
| 75 |
377965 |
▶ | if (s->validating) croak("%s: the validator may not change the store", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->validating) croak("%s: the validator may not change the store", what); |
12 |
377953 |
377965 |
|
| 76 |
377953 |
| } |
| 77 |
|
| |
| 78 |
|
| /* Ask the validator - a Perl sub, or a C function and its data - whether |
| 79 |
|
| * `candidate` may replace `old` in the store `storerv` refers to, or in no |
| 80 |
|
| * store yet when it is undef. 1 yes, 0 no; a Perl validator that dies goes |
| 81 |
|
| * on up as it is. `flag`, when given, is set for as long as the validator |
| 82 |
|
| * runs and put back however it ends. */ |
| 83 |
357 |
| static int store_validate(pTHX_ SV *validator, state_store_validate_fn cfn, void *cud, |
| 84 |
|
| SV *candidate, SV *old, SV *storerv, int *flag) { |
| 85 |
357 |
| SV *args[3], *result, *err = NULL; |
| 86 |
|
| int ok; |
| 87 |
|
| |
| 88 |
357 |
| args[0] = candidate; |
| 89 |
357 |
| args[1] = old; |
| 90 |
357 |
| args[2] = storerv; |
| 91 |
357 |
| ENTER; |
| 92 |
357 |
| SAVETMPS; |
| 93 |
357 |
▶ | if (flag) { SAVEINT(*flag); *flag = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (flag) { SAVEINT(*flag); *flag = 1; } |
347 |
10 |
357 |
|
| 94 |
357 |
▶ | if (cfn) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cfn) { |
306 |
51 |
357 |
|
| 95 |
306 |
| ok = cfn(aTHX_ cud, candidate, old, storerv) ? 1 : 0; |
| 96 |
|
| } |
| 97 |
|
| else { |
| 98 |
51 |
| err = store_call(aTHX_ validator, args, 3, G_SCALAR, &result, NULL); |
| 99 |
51 |
▶ | ok = !err && SvTRUE(result); |
Branch: 2 branches
| Branch | True | False | Total |
| ok = !err && SvTRUE(result); |
45 |
6 |
51 |
| ok = !err && SvTRUE(result); |
38 |
7 |
45 |
|
| 100 |
51 |
▶ | if (err) SvREFCNT_inc_simple_void_NN(err); |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) SvREFCNT_inc_simple_void_NN(err); |
6 |
45 |
51 |
|
| 101 |
|
| } |
| 102 |
357 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
51 |
306 |
357 |
|
| 103 |
357 |
| LEAVE; |
| 104 |
357 |
▶ | if (err) store_rethrow(aTHX_ sv_2mortal(err)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) store_rethrow(aTHX_ sv_2mortal(err)); |
6 |
351 |
357 |
|
| 105 |
351 |
| return ok; |
| 106 |
|
| } |
| 107 |
|
| |
| 108 |
|
| /* A watcher is a Perl sub or, from the C ABI, a function and its data. |
| 109 |
|
| * One removed has neither. */ |
| 110 |
|
| typedef struct store_watcher { |
| 111 |
|
| UV id; |
| 112 |
|
| SV *code; |
| 113 |
|
| state_store_watch_fn cfn; |
| 114 |
|
| void *ud; |
| 115 |
|
| } store_watcher; |
| 116 |
|
| typedef struct store_change { SV *was; SV *now; } store_change; |
| 117 |
|
| |
| 118 |
|
| /* A change to be told to the watchers once the store has settled: both |
| 119 |
|
| * values held by the queue until it has been. Nothing is queued when nobody |
| 120 |
|
| * is watching. */ |
| 121 |
238209 |
| static void store_enqueue(pTHX_ store *s, SV *was, SV *now) { |
| 122 |
238209 |
▶ | if (!s->live_watchers || s->txn_depth) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!s->live_watchers || s->txn_depth) return; |
33677 |
204532 |
238209 |
| if (!s->live_watchers || s->txn_depth) return; |
171 |
33506 |
33677 |
|
| 123 |
33506 |
▶ | if (s->qhead + s->qlen == s->qcap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->qhead + s->qlen == s->qcap) { |
187 |
33319 |
33506 |
|
| 124 |
187 |
▶ | if (s->qhead) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->qhead) { |
90 |
97 |
187 |
|
| 125 |
90 |
▶ | Move(s->queue + s->qhead, s->queue, s->qlen, store_change); |
Branch: 1 branch
| Branch | True | False | Total |
| Move(s->queue + s->qhead, s->queue, s->qlen, store_change); |
0 |
90 |
90 |
|
| 126 |
90 |
| s->qhead = 0; |
| 127 |
|
| } |
| 128 |
|
| else { |
| 129 |
97 |
▶ | s->qcap = s->qcap ? s->qcap * 2 : 4; |
Branch: 1 branch
| Branch | True | False | Total |
| s->qcap = s->qcap ? s->qcap * 2 : 4; |
12 |
85 |
97 |
|
| 130 |
97 |
▶ | Renew(s->queue, s->qcap, store_change); |
Branch: 1 branch
| Branch | True | False | Total |
| Renew(s->queue, s->qcap, store_change); |
0 |
97 |
97 |
|
| 131 |
|
| } |
| 132 |
|
| } |
| 133 |
33506 |
| s->queue[s->qhead + s->qlen].was = SvREFCNT_inc_simple_NN(was); |
| 134 |
33506 |
| s->queue[s->qhead + s->qlen].now = SvREFCNT_inc_simple_NN(now); |
| 135 |
33506 |
| s->qlen++; |
| 136 |
|
| } |
| 137 |
|
| |
| 138 |
|
| /* Everything the watchers held, given back: for a store being freed, and |
| 139 |
|
| * for a round abandoned. */ |
| 140 |
2 |
| static void store_queue_clear(pTHX_ store *s) { |
| 141 |
10002 |
▶ | while (s->qlen) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (s->qlen) { |
10000 |
2 |
10002 |
|
| 142 |
10000 |
| store_change c = s->queue[s->qhead++]; |
| 143 |
10000 |
| s->qlen--; |
| 144 |
10000 |
| store_sv_release(aTHX_ c.was); |
| 145 |
10000 |
| store_sv_release(aTHX_ c.now); |
| 146 |
|
| } |
| 147 |
2 |
| s->qhead = 0; |
| 148 |
2 |
| } |
| 149 |
|
| |
| 150 |
|
| /* `v` with only its last `keep` values, as a new vector; or NULL when it |
| 151 |
|
| * already has no more than that, or, with *nomem set, when there was no |
| 152 |
|
| * memory to make one. The values dropped are the oldest. */ |
| 153 |
3297 |
| static hamt_vec *store_trimmed(pTHX_ const hamt_vec *v, size_t keep, int *nomem) { |
| 154 |
3297 |
| size_t have = (STORE_HAMT->vec_count)(aTHX_ v); |
| 155 |
|
| hamt_vec *t; |
| 156 |
3297 |
| int status = HAMT_ABI_OK; |
| 157 |
|
| |
| 158 |
3297 |
| *nomem = 0; |
| 159 |
3297 |
▶ | if (have <= keep) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (have <= keep) return NULL; |
57 |
3240 |
3297 |
|
| 160 |
3240 |
| t = (STORE_HAMT->vec_slice)(aTHX_ v, have - keep, keep, &status); |
| 161 |
3240 |
▶ | if (!t) *nomem = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t) *nomem = 1; |
0 |
3240 |
3240 |
|
| 162 |
3240 |
| return t; |
| 163 |
|
| } |
| 164 |
|
| |
| 165 |
|
| /* Under a limit the past is not cut on every change: it is let grow to the |
| 166 |
|
| * limit and this many steps more, then cut back to the limit in one go. A |
| 167 |
|
| * cut is a slice of the vector, and one change in 32 paying for it costs a |
| 168 |
|
| * small fraction of every change paying for it (bench/limit.pl). The steps |
| 169 |
|
| * beyond the limit are never offered to undo, so what can be undone is |
| 170 |
|
| * exact; they only stay alive a little longer. */ |
| 171 |
|
| #define STORE_TRIM_SLACK 32 |
| 172 |
|
| |
| 173 |
|
| /* How many steps can be undone: the past, less its oldest steps beyond the |
| 174 |
|
| * limit that have not been cut yet. */ |
| 175 |
27768 |
| static size_t store_past_len(pTHX_ const store *s) { |
| 176 |
27768 |
| return (STORE_HAMT->vec_count)(aTHX_ s->past) - s->past_skip; |
| 177 |
|
| } |
| 178 |
|
| |
| 179 |
|
| /* How many of the oldest steps of `past` are beyond the limit, once steps |
| 180 |
|
| * have been pushed onto it: those it already had, and any more the pushes |
| 181 |
|
| * took it over by. */ |
| 182 |
183883 |
| static size_t store_skip_after(const store *s, size_t count, size_t skip) { |
| 183 |
183883 |
▶ | if (s->limited && count - skip > s->limit) skip = count - s->limit; |
Branch: 2 branches
| Branch | True | False | Total |
| if (s->limited && count - skip > s->limit) skip = count - s->limit; |
128444 |
55439 |
183883 |
| if (s->limited && count - skip > s->limit) skip = count - s->limit; |
109580 |
18864 |
128444 |
|
| 184 |
183883 |
| return skip; |
| 185 |
|
| } |
| 186 |
|
| |
| 187 |
|
| /* A past about to have a step pushed onto it, cut back first if it has |
| 188 |
|
| * outgrown the limit by the slack: a new vector of the newest `limit - 1`, |
| 189 |
|
| * or NULL when no cut is due. *nomem as store_trimmed. */ |
| 190 |
126022 |
| static hamt_vec *store_room(pTHX_ const store *s, const hamt_vec *past, int *nomem) { |
| 191 |
126022 |
| *nomem = 0; |
| 192 |
126022 |
▶ | if ((STORE_HAMT->vec_count)(aTHX_ past) < s->limit + STORE_TRIM_SLACK) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((STORE_HAMT->vec_count)(aTHX_ past) < s->limit + STORE_TRIM_SLACK) return NULL; |
122787 |
3235 |
126022 |
|
| 193 |
3235 |
| return store_trimmed(aTHX_ past, s->limit - 1, nomem); |
| 194 |
|
| } |
| 195 |
|
| |
| 196 |
2274 |
| static int store_magic_free(pTHX_ SV *sv, MAGIC *mg) { |
| 197 |
2274 |
| store *s = (store *)mg->mg_ptr; |
| 198 |
|
| PERL_UNUSED_ARG(sv); |
| 199 |
2274 |
▶ | if (s) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s) { |
2274 |
0 |
2274 |
|
| 200 |
2274 |
| SV *value = s->value; |
| 201 |
2274 |
| hamt_vec *past = s->past, *future = s->future; |
| 202 |
2274 |
| store_watcher *w = s->watchers; |
| 203 |
2274 |
| SV *s_validator = s->validator; |
| 204 |
2274 |
| hamt_map *s_checkpoints = s->checkpoints; |
| 205 |
2274 |
| store_change *q = s->queue; |
| 206 |
2274 |
| size_t nw = s->nwatchers, qhead = s->qhead, qlen = s->qlen, i; |
| 207 |
2274 |
| mg->mg_ptr = NULL; |
| 208 |
|
| /* The struct goes before the values: releasing one can run its |
| 209 |
|
| * DESTROY, and by then there must be nothing half-gone. */ |
| 210 |
2274 |
| store_free(aTHX_ s); |
| 211 |
2274 |
| store_sv_release(aTHX_ value); |
| 212 |
2274 |
▶ | for (i = 0; i < qlen; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < qlen; i++) { |
0 |
2274 |
2274 |
|
| 213 |
0 |
| store_sv_release(aTHX_ q[qhead + i].was); |
| 214 |
0 |
| store_sv_release(aTHX_ q[qhead + i].now); |
| 215 |
|
| } |
| 216 |
2320 |
▶ | for (i = 0; i < nw; i++) store_sv_release(aTHX_ w[i].code); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nw; i++) store_sv_release(aTHX_ w[i].code); |
46 |
2274 |
2320 |
|
| 217 |
2274 |
| Safefree(q); |
| 218 |
2274 |
| Safefree(w); |
| 219 |
2274 |
| store_sv_release(aTHX_ s_validator); |
| 220 |
2274 |
▶ | if (s_checkpoints) (STORE_HAMT->map_release)(aTHX_ s_checkpoints); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s_checkpoints) (STORE_HAMT->map_release)(aTHX_ s_checkpoints); |
65 |
2209 |
2274 |
|
| 221 |
2274 |
▶ | if (past) (STORE_HAMT->vec_release)(aTHX_ past); |
Branch: 1 branch
| Branch | True | False | Total |
| if (past) (STORE_HAMT->vec_release)(aTHX_ past); |
2274 |
0 |
2274 |
|
| 222 |
2274 |
▶ | if (future) (STORE_HAMT->vec_release)(aTHX_ future); |
Branch: 1 branch
| Branch | True | False | Total |
| if (future) (STORE_HAMT->vec_release)(aTHX_ future); |
2274 |
0 |
2274 |
|
| 223 |
|
| } |
| 224 |
2274 |
| return 0; |
| 225 |
|
| } |
| 226 |
|
| |
| 227 |
0 |
| static int store_magic_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) { |
| 228 |
|
| PERL_UNUSED_CONTEXT; |
| 229 |
|
| PERL_UNUSED_ARG(param); |
| 230 |
0 |
| mg->mg_ptr = NULL; |
| 231 |
0 |
| return 0; |
| 232 |
|
| } |
| 233 |
|
| |
| 234 |
|
| static MGVTBL store_vtbl = { |
| 235 |
|
| NULL, NULL, NULL, NULL, store_magic_free, NULL, store_magic_dup, NULL |
| 236 |
|
| }; |
| 237 |
|
| |
| 238 |
|
| /* The struct behind an object, or a croak that names the method. */ |
| 239 |
362999 |
| static store *store_handle(pTHX_ SV *sv, const char *what) { |
| 240 |
362999 |
▶ | if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
362990 |
9 |
362999 |
| if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { |
362990 |
0 |
362990 |
|
| 241 |
|
| MAGIC *mg; |
| 242 |
362996 |
▶ | 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) { |
362979 |
17 |
362996 |
|
| 243 |
362979 |
▶ | if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &store_vtbl) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &store_vtbl) continue; |
362976 |
3 |
362979 |
| if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &store_vtbl) continue; |
3 |
362973 |
362976 |
|
| 244 |
362973 |
▶ | if (!mg->mg_ptr) croak("%s: this store belongs to another thread", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!mg->mg_ptr) croak("%s: this store belongs to another thread", what); |
0 |
362973 |
362973 |
|
| 245 |
362973 |
| return (store *)mg->mg_ptr; |
| 246 |
|
| } |
| 247 |
|
| } |
| 248 |
26 |
| croak("%s: not a store", what); |
| 249 |
|
| return NULL; |
| 250 |
|
| } |
| 251 |
|
| |
| 252 |
|
| /* The class to bless into: the one named, or the one an object is in. */ |
| 253 |
2223 |
| static HV *store_class_stash(pTHX_ SV *klass) { |
| 254 |
2223 |
▶ | 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 |
2220 |
2223 |
| if (SvROK(klass) && SvOBJECT(SvRV(klass))) return SvSTASH(SvRV(klass)); |
3 |
0 |
3 |
|
| 255 |
2220 |
| return gv_stashsv(klass, GV_ADD); |
| 256 |
|
| } |
| 257 |
|
| |
| 258 |
|
| /* A value as it is kept: a copy, read-only, so that every holder of it sees |
| 259 |
|
| * what was stored and nothing written through an alias since. A reference |
| 260 |
|
| * copied this way is a second reference to the same thing, which is what |
| 261 |
|
| * makes a HAMT container in a store still that container. */ |
| 262 |
209402 |
| static SV *store_copy_in(pTHX_ SV *in) { |
| 263 |
209402 |
| SV *c = newSVsv(in); |
| 264 |
209402 |
| SvREADONLY_on(c); |
| 265 |
209402 |
| return c; |
| 266 |
|
| } |
| 267 |
|
| |
| 268 |
|
| /* A new object of the class behind `stash`, mortal, holding `initial`. */ |
| 269 |
2274 |
| static SV *store_new(pTHX_ HV *stash, SV *initial, store **out) { |
| 270 |
|
| store *s; |
| 271 |
|
| hamt_vec *past, *future; |
| 272 |
|
| SV *inner, *rv; |
| 273 |
|
| MAGIC *mg; |
| 274 |
|
| |
| 275 |
2274 |
| past = (STORE_HAMT->vec_new)(aTHX); |
| 276 |
2274 |
▶ | future = past ? (STORE_HAMT->vec_new)(aTHX) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| future = past ? (STORE_HAMT->vec_new)(aTHX) : NULL; |
2274 |
0 |
2274 |
|
| 277 |
2274 |
▶ | if (!future) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!future) { |
0 |
2274 |
2274 |
|
| 278 |
0 |
▶ | if (past) (STORE_HAMT->vec_release)(aTHX_ past); |
Branch: 1 branch
| Branch | True | False | Total |
| if (past) (STORE_HAMT->vec_release)(aTHX_ past); |
0 |
0 |
0 |
|
| 279 |
0 |
| croak("State::Store::new: out of memory"); |
| 280 |
|
| } |
| 281 |
2274 |
| s = (store *)store_alloc(aTHX_ sizeof(store)); |
| 282 |
2274 |
| s->past = past; |
| 283 |
2274 |
| s->future = future; |
| 284 |
2274 |
| s->value = store_copy_in(aTHX_ initial); |
| 285 |
2274 |
| inner = newSV(0); |
| 286 |
2274 |
| mg = sv_magicext(inner, NULL, PERL_MAGIC_ext, &store_vtbl, (const char *)s, 0); |
| 287 |
2274 |
| mg->mg_flags |= MGf_DUP; |
| 288 |
2274 |
| rv = sv_2mortal(newRV_noinc(inner)); |
| 289 |
2274 |
| sv_bless(rv, stash); |
| 290 |
2274 |
| *out = s; |
| 291 |
2274 |
| return rv; |
| 292 |
|
| } |
| 293 |
|
| |
| 294 |
|
| /* The stored scalar itself, mortal, kept alive past the store by a |
| 295 |
|
| * reference of its own. */ |
| 296 |
92459 |
| static SV *store_get(pTHX_ const store *s) { |
| 297 |
92459 |
| return sv_2mortal(SvREFCNT_inc_simple_NN(s->value)); |
| 298 |
|
| } |
| 299 |
|
| |
| 300 |
|
| /* The one place a new value arrives. 1 if it did, 0 if `in` is the value |
| 301 |
|
| * already held, as HAMT's value_equal has it: the same scalar, two |
| 302 |
|
| * references to one thing, or two non-references that are the same string. |
| 303 |
|
| * A change that is no change records nothing and leaves the redo stack. |
| 304 |
|
| * The validator is asked when `validate` is set: by set and swap, not by a |
| 305 |
|
| * restore, whose value was accepted when it was stored. -1 if it refused, |
| 306 |
|
| * with nothing changed. |
| 307 |
|
| * |
| 308 |
|
| * A tied or otherwise magical `in` is read once, here, and the rest works |
| 309 |
|
| * on what was read. |
| 310 |
|
| * |
| 311 |
|
| * The value displaced goes onto the past, and the future is emptied. All |
| 312 |
|
| * that can fail is allocated before anything changes, so running out of |
| 313 |
|
| * memory croaks with the store as it was. What is let go is let go last, |
| 314 |
|
| * when the store already holds the new value: releasing it can run a |
| 315 |
|
| * DESTROY, and that may use this store. */ |
| 316 |
208345 |
| static int store_put(pTHX_ store *s, SV *in, SV *storerv, int validate, const char *what) { |
| 317 |
|
| SV *old; |
| 318 |
208345 |
| hamt_vec *fresh = NULL, *dead = NULL, *room = NULL, *dead_past = NULL; |
| 319 |
208345 |
▶ | int record = !s->txn_depth && (!s->limited || s->limit > 0), nomem = 0; |
Branch: 3 branches
| Branch | True | False | Total |
| int record = !s->txn_depth && (!s->limited || s->limit > 0), nomem = 0; |
204004 |
4341 |
208345 |
| int record = !s->txn_depth && (!s->limited || s->limit > 0), nomem = 0; |
147138 |
56866 |
204004 |
| int record = !s->txn_depth && (!s->limited || s->limit > 0), nomem = 0; |
126024 |
21114 |
147138 |
|
| 320 |
|
| |
| 321 |
|
| SV *candidate; |
| 322 |
|
| |
| 323 |
208345 |
| store_check_mutable(aTHX_ s, what); |
| 324 |
208344 |
▶ | if (SvGMAGICAL(in)) in = store_read_once(aTHX_ in); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvGMAGICAL(in)) in = store_read_once(aTHX_ in); |
2 |
208342 |
208344 |
|
| 325 |
208344 |
▶ | if ((STORE_HAMT->value_equal)(aTHX_ s->value, in)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((STORE_HAMT->value_equal)(aTHX_ s->value, in)) return 0; |
3440 |
204904 |
208344 |
|
| 326 |
|
| |
| 327 |
|
| /* The validator is shown the very scalar that will be stored, and |
| 328 |
|
| * decides before anything is allocated or changed. */ |
| 329 |
204904 |
| candidate = sv_2mortal(store_copy_in(aTHX_ in)); |
| 330 |
204904 |
▶ | if (validate && (s->validator || s->cvalidator) |
Branch: 3 branches
| Branch | True | False | Total |
| if (validate && (s->validator || s->cvalidator) |
204840 |
64 |
204904 |
| if (validate && (s->validator || s->cvalidator) |
204799 |
41 |
204840 |
| if (validate && (s->validator || s->cvalidator) |
306 |
204493 |
204799 |
|
| 331 |
347 |
▶ | && !store_validate(aTHX_ s->validator, s->cvalidator, s->cvalidator_ud, |
Branch: 1 branch
| Branch | True | False | Total |
| && !store_validate(aTHX_ s->validator, s->cvalidator, s->cvalidator_ud, |
57 |
284 |
341 |
|
| 332 |
|
| candidate, s->value, storerv, &s->validating)) |
| 333 |
57 |
| return -1; |
| 334 |
|
| |
| 335 |
204841 |
▶ | if ((STORE_HAMT->vec_count)(aTHX_ s->future)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((STORE_HAMT->vec_count)(aTHX_ s->future)) { |
10142 |
194699 |
204841 |
|
| 336 |
10142 |
| fresh = (STORE_HAMT->vec_new)(aTHX); |
| 337 |
10142 |
▶ | if (!fresh) croak("%s: out of memory", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!fresh) croak("%s: out of memory", what); |
0 |
10142 |
10142 |
|
| 338 |
|
| } |
| 339 |
|
| /* When a cut is due, the past is cut to leave room for one more before |
| 340 |
|
| * anything is pushed, so that a failure leaves it as it was. */ |
| 341 |
204841 |
▶ | if (record && s->limited) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (record && s->limited) { |
179387 |
25454 |
204841 |
| if (record && s->limited) { |
125968 |
53419 |
179387 |
|
| 342 |
125968 |
| room = store_room(aTHX_ s, s->past, &nomem); |
| 343 |
125968 |
▶ | if (nomem) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nomem) { |
0 |
125968 |
125968 |
|
| 344 |
0 |
▶ | if (fresh) (STORE_HAMT->vec_release)(aTHX_ fresh); |
Branch: 1 branch
| Branch | True | False | Total |
| if (fresh) (STORE_HAMT->vec_release)(aTHX_ fresh); |
0 |
0 |
0 |
|
| 345 |
0 |
| croak("%s: out of memory", what); |
| 346 |
|
| } |
| 347 |
|
| } |
| 348 |
204841 |
▶ | if (record && (STORE_HAMT->vec_push)(aTHX_ room ? room : s->past, s->value) != HAMT_ABI_OK) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (record && (STORE_HAMT->vec_push)(aTHX_ room ? room : s->past, s->value) != HAMT_ABI_OK) { |
179387 |
25454 |
204841 |
| if (record && (STORE_HAMT->vec_push)(aTHX_ room ? room : s->past, s->value) != HAMT_ABI_OK) { |
176152 |
3235 |
179387 |
| if (record && (STORE_HAMT->vec_push)(aTHX_ room ? room : s->past, s->value) != HAMT_ABI_OK) { |
0 |
179387 |
179387 |
|
| 349 |
0 |
▶ | if (room) (STORE_HAMT->vec_release)(aTHX_ room); |
Branch: 1 branch
| Branch | True | False | Total |
| if (room) (STORE_HAMT->vec_release)(aTHX_ room); |
0 |
0 |
0 |
|
| 350 |
0 |
▶ | if (fresh) (STORE_HAMT->vec_release)(aTHX_ fresh); |
Branch: 1 branch
| Branch | True | False | Total |
| if (fresh) (STORE_HAMT->vec_release)(aTHX_ fresh); |
0 |
0 |
0 |
|
| 351 |
0 |
| croak("%s: out of memory", what); |
| 352 |
|
| } |
| 353 |
|
| |
| 354 |
204841 |
| old = s->value; |
| 355 |
204841 |
| s->value = SvREFCNT_inc_simple_NN(candidate); |
| 356 |
204841 |
▶ | if (fresh) { dead = s->future; s->future = fresh; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (fresh) { dead = s->future; s->future = fresh; } |
10142 |
194699 |
204841 |
|
| 357 |
204841 |
▶ | if (room) { dead_past = s->past; s->past = room; s->past_skip = 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (room) { dead_past = s->past; s->past = room; s->past_skip = 0; } |
3235 |
201606 |
204841 |
|
| 358 |
204841 |
▶ | if (record) s->past_skip = store_skip_after(s, (STORE_HAMT->vec_count)(aTHX_ s->past), s->past_skip); |
Branch: 1 branch
| Branch | True | False | Total |
| if (record) s->past_skip = store_skip_after(s, (STORE_HAMT->vec_count)(aTHX_ s->past), s->past_skip); |
179387 |
25454 |
204841 |
|
| 359 |
|
| |
| 360 |
204841 |
| store_enqueue(aTHX_ s, old, s->value); |
| 361 |
204841 |
| store_sv_release(aTHX_ old); |
| 362 |
204841 |
▶ | if (dead) (STORE_HAMT->vec_release)(aTHX_ dead); |
Branch: 1 branch
| Branch | True | False | Total |
| if (dead) (STORE_HAMT->vec_release)(aTHX_ dead); |
10142 |
194699 |
204841 |
|
| 363 |
204841 |
▶ | if (dead_past) (STORE_HAMT->vec_release)(aTHX_ dead_past); |
Branch: 1 branch
| Branch | True | False | Total |
| if (dead_past) (STORE_HAMT->vec_release)(aTHX_ dead_past); |
3235 |
201606 |
204841 |
|
| 364 |
204841 |
| return 1; |
| 365 |
|
| } |
| 366 |
|
| |
| 367 |
|
| #endif |