include/store/store_history.h

filter: Statementstmt 91.2% 83 / 91 Branchbran 86.3% 57 / 66 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.1% 140 / 157 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/store/store_hamt.h ↑ Summary include/store/store_checkpoint.h »
1 #ifndef STORE_HISTORY_H
2 #define STORE_HISTORY_H
3  
4 /* Undo and redo. The past is a vector of the values before the current one,
5 * oldest first; the future is a vector of the values undone, with the next
6 * one to redo at the end. Both are HAMT vectors, so keeping a step costs a
7 * slot in a tree that shares everything else, and taking one back costs
8 * the same.
9 *
10 * A move of `n` steps from one to the other is made on copies of both
11 * structs, and the store takes the copies only once every part of the move
12 * has worked: running out of memory part way croaks with the store as it
13 * was. A copy shares every node with what it was copied from, so making one
14 * costs a small struct; changing it copies only the nodes it changes. */
15  
16 /* `from` is the stack a step is taken off and `to` the one the current value
17 * goes onto: the past and the future for an undo, the other way about for a
18 * redo. How many steps were taken: `n`, or fewer if `from` held fewer. */
19 51959 static size_t store_move(pTHX_ store *s, int undo, size_t n, const char *what) {
20 51959 ▶ hamt_vec *from = undo ? s->past : s->future;
Branch: 1 branch
BranchTrueFalseTotal
hamt_vec *from = undo ? s->past : s->future; 26160 25799 51959
21 51959 ▶ hamt_vec *to = undo ? s->future : s->past;
Branch: 1 branch
BranchTrueFalseTotal
hamt_vec *to = undo ? s->future : s->past; 26160 25799 51959
22 hamt_vec *nfrom, *nto;
23 51959 size_t skip = s->past_skip;
24 51959 size_t phys = (STORE_HAMT->vec_count)(aTHX_ from), i;
25 51959 ▶ size_t have = undo ? store_past_len(aTHX_ s) : phys;
Branch: 1 branch
BranchTrueFalseTotal
size_t have = undo ? store_past_len(aTHX_ s) : phys; 26160 25799 51959
26 SV *arrive, *old;
27 51959 int status = HAMT_ABI_OK;
28  
29 51959 store_check_mutable(aTHX_ s, what);
30 51957 store_check_no_txn(aTHX_ s, what);
31 51955 ▶ if (n > have) n = have;
Branch: 1 branch
BranchTrueFalseTotal
if (n > have) n = have; 24791 27164 51955
32 51955 ▶ if (n == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) return 0; 20664 31291 51955
33  
34 /* The current value goes onto `to`, then the values passed over on the
35 * way, nearest first; the value arrived at is the n-th from the end of
36 * `from`, and `from` keeps what is before it. */
37 31291 nto = (STORE_HAMT->vec_copy)(aTHX_ to);
38 31291 ▶ if (!nto) croak("%s: out of memory", what);
Branch: 1 branch
BranchTrueFalseTotal
if (!nto) croak("%s: out of memory", what); 0 31291 31291
39 76289 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 44998 31291 76289
40 44998 ▶ SV *v = i == 0 ? s->value : (STORE_HAMT->vec_fetch)(aTHX_ from, phys - i);
Branch: 1 branch
BranchTrueFalseTotal
SV *v = i == 0 ? s->value : (STORE_HAMT->vec_fetch)(aTHX_ from, phys - i); 31291 13707 44998
41 44998 ▶ if ((STORE_HAMT->vec_push)(aTHX_ nto, v) != HAMT_ABI_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if ((STORE_HAMT->vec_push)(aTHX_ nto, v) != HAMT_ABI_OK) { 0 44998 44998
42 0 (STORE_HAMT->vec_release)(aTHX_ nto);
43 0 croak("%s: out of memory", what);
44 }
45 }
46 /* A redo adds to the past, which is cut as a change cuts it. */
47 31291 ▶ if (!undo && s->limited) {
Branch: 2 branches
BranchTrueFalseTotal
if (!undo && s->limited) { 9030 22261 31291
if (!undo && s->limited) { 2422 6608 9030
48 2422 int nomem = 0;
49 2422 hamt_vec *cut = (STORE_HAMT->vec_count)(aTHX_ nto) >= s->limit + STORE_TRIM_SLACK
50 2422 ▶ ? store_trimmed(aTHX_ nto, s->limit, &nomem) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? store_trimmed(aTHX_ nto, s->limit, &nomem) : NULL; 2 2420 2422
51 2422 ▶ if (nomem) {
Branch: 1 branch
BranchTrueFalseTotal
if (nomem) { 0 2422 2422
52 0 (STORE_HAMT->vec_release)(aTHX_ nto);
53 0 croak("%s: out of memory", what);
54 }
55 2422 ▶ if (cut) { (STORE_HAMT->vec_release)(aTHX_ nto); nto = cut; skip = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (cut) { (STORE_HAMT->vec_release)(aTHX_ nto); nto = cut; skip = 0; } 2 2420 2422
56 2422 skip = store_skip_after(s, (STORE_HAMT->vec_count)(aTHX_ nto), skip);
57 }
58 31291 nfrom = (STORE_HAMT->vec_slice)(aTHX_ from, 0, phys - n, &status);
59 31291 ▶ if (!nfrom) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nfrom) { 0 31291 31291
60 0 (STORE_HAMT->vec_release)(aTHX_ nto);
61 0 croak("%s: out of memory", what);
62 }
63  
64 31291 arrive = SvREFCNT_inc_simple_NN((STORE_HAMT->vec_fetch)(aTHX_ from, phys - n));
65 31291 old = s->value;
66 31291 s->value = arrive;
67 31291 ▶ if (undo) { s->past = nfrom; s->future = nto; }
Branch: 1 branch
BranchTrueFalseTotal
if (undo) { s->past = nfrom; s->future = nto; } 22261 9030 31291
68 9030 else { s->future = nfrom; s->past = nto; }
69 31291 s->past_skip = skip;
70  
71 31291 store_enqueue(aTHX_ s, old, arrive);
72 31291 store_sv_release(aTHX_ old);
73 31291 (STORE_HAMT->vec_release)(aTHX_ from);
74 31291 (STORE_HAMT->vec_release)(aTHX_ to);
75 31291 return n;
76 }
77  
78 /* Both stacks emptied; the value stays. */
79 2572 static void store_clear_history(pTHX_ store *s, const char *what) {
80 2572 hamt_vec *past, *future, *dead_past = s->past, *dead_future = s->future;
81  
82 2572 store_check_mutable(aTHX_ s, what);
83 2571 store_check_no_txn(aTHX_ s, what);
84 2570 past = (STORE_HAMT->vec_new)(aTHX);
85 2570 ▶ future = past ? (STORE_HAMT->vec_new)(aTHX) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
future = past ? (STORE_HAMT->vec_new)(aTHX) : NULL; 2570 0 2570
86 2570 ▶ if (!future) {
Branch: 1 branch
BranchTrueFalseTotal
if (!future) { 0 2570 2570
87 0 ▶ if (past) (STORE_HAMT->vec_release)(aTHX_ past);
Branch: 1 branch
BranchTrueFalseTotal
if (past) (STORE_HAMT->vec_release)(aTHX_ past); 0 0 0
88 0 croak("%s: out of memory", what);
89 }
90 2570 s->past = past;
91 2570 s->future = future;
92 2570 s->past_skip = 0;
93 2570 (STORE_HAMT->vec_release)(aTHX_ dead_past);
94 2570 (STORE_HAMT->vec_release)(aTHX_ dead_future);
95 2570 }
96  
97 /* A history limit from Perl: undef is none, and anything else must be a
98 * whole number that is not negative. */
99 54 static void store_limit_arg(pTHX_ SV *sv, size_t *limit, int *limited, const char *what) {
100 NV nv;
101 54 sv = store_read_once(aTHX_ sv);
102 54 ▶ if (!SvOK(sv)) { *limited = 0; *limit = 0; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) { *limited = 0; *limit = 0; return; } 3 51 54
103 51 ▶ if (!looks_like_number(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!looks_like_number(sv)) 6 45 51
104 6 croak("%s: the history limit is not a number", what);
105 45 nv = SvNV(sv);
106 45 ▶ if (nv < 0 || nv != (NV)(UV)nv)
Branch: 2 branches
BranchTrueFalseTotal
if (nv < 0 || nv != (NV)(UV)nv) 42 3 45
if (nv < 0 || nv != (NV)(UV)nv) 2 40 42
107 5 croak("%s: the history limit must be a whole number, not negative, or undef", what);
108 40 *limited = 1;
109 40 *limit = (size_t)(UV)nv;
110 }
111  
112 /* A new limit, applied at once: the past is cut to it, oldest first. The
113 * values ahead are left alone; a redo cuts the past again as it goes. */
114 62 static void store_set_limit(pTHX_ store *s, size_t limit, int limited, const char *what) {
115 62 hamt_vec *cut = NULL, *dead = NULL;
116 62 int nomem = 0;
117  
118 62 store_check_mutable(aTHX_ s, what);
119 61 store_check_no_txn(aTHX_ s, what);
120 /* Cut to what can be undone, or less, so that no step beyond the old
121 * limit comes back under a new one. */
122 {
123 60 size_t keep = store_past_len(aTHX_ s);
124 60 ▶ if (limited && limit < keep) keep = limit;
Branch: 2 branches
BranchTrueFalseTotal
if (limited && limit < keep) keep = limit; 58 2 60
if (limited && limit < keep) keep = limit; 2 56 58
125 60 cut = store_trimmed(aTHX_ s->past, keep, &nomem);
126 60 ▶ if (nomem) croak("%s: out of memory", what);
Branch: 1 branch
BranchTrueFalseTotal
if (nomem) croak("%s: out of memory", what); 0 60 60
127 }
128 60 s->limited = limited;
129 60 s->limit = limit;
130 60 ▶ if (cut) { dead = s->past; s->past = cut; }
Branch: 1 branch
BranchTrueFalseTotal
if (cut) { dead = s->past; s->past = cut; } 3 57 60
131 60 s->past_skip = 0;
132 60 ▶ if (dead) (STORE_HAMT->vec_release)(aTHX_ dead);
Branch: 1 branch
BranchTrueFalseTotal
if (dead) (STORE_HAMT->vec_release)(aTHX_ dead); 3 57 60
133 60 }
134  
135 /* A count of steps from Perl: absent is one, and anything else must be a
136 * whole number that is not negative. */
137 51863 static size_t store_steps(pTHX_ SV *sv, const char *what) {
138 NV nv;
139 51863 ▶ if (!sv) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!sv) return 1; 11258 40605 51863
140 /* Read once, into a plain copy: SvNV_nomg is not in 5.10 or 5.12. */
141 40605 sv = store_read_once(aTHX_ sv);
142 40605 ▶ if (!SvOK(sv) || !looks_like_number(sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(sv) || !looks_like_number(sv)) 40604 1 40605
if (!SvOK(sv) || !looks_like_number(sv)) 3 40601 40604
143 4 croak("%s: the number of steps is not a number", what);
144 40601 nv = SvNV(sv);
145 40601 ▶ if (nv < 0 || nv != (NV)(UV)nv)
Branch: 2 branches
BranchTrueFalseTotal
if (nv < 0 || nv != (NV)(UV)nv) 40600 1 40601
if (nv < 0 || nv != (NV)(UV)nv) 1 40599 40600
146 2 croak("%s: the number of steps must be a whole number, not negative", what);
147 40599 return (size_t)(UV)nv;
148 }
149  
150 #endif
« include/store/store_hamt.h ↑ Summary include/store/store_checkpoint.h »