include/store/store_history.h

filter: Statementstmt 89.5% 120 / 134 Branchbran 83.0% 88 / 106 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.6% 208 / 240 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/store/store_call.h ↑ Summary include/store/store_core.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 62179 static size_t store_move(pTHX_ store *s, int undo, size_t n, const char *what) {
20 62179 ▶ hamt_vec *from = undo ? s->past : s->future;
Branch: 1 branch
BranchTrueFalseTotal
hamt_vec *from = undo ? s->past : s->future; 31405 30774 62179
21 62179 ▶ hamt_vec *to = undo ? s->future : s->past;
Branch: 1 branch
BranchTrueFalseTotal
hamt_vec *to = undo ? s->future : s->past; 31405 30774 62179
22 hamt_vec *nfrom, *nto;
23 62179 size_t skip = s->past_skip;
24 62179 size_t phys = (STORE_HAMT->vec_count)(aTHX_ from), i;
25 62179 ▶ size_t have = undo ? store_past_len(aTHX_ s) : phys;
Branch: 1 branch
BranchTrueFalseTotal
size_t have = undo ? store_past_len(aTHX_ s) : phys; 31405 30774 62179
26 SV *arrive, *old;
27 62179 int status = HAMT_ABI_OK;
28  
29 62179 store_check_mutable(aTHX_ s, what);
30 62177 store_check_no_txn(aTHX_ s, what);
31 62175 ▶ if (n > have) n = have;
Branch: 1 branch
BranchTrueFalseTotal
if (n > have) n = have; 28363 33812 62175
32 62175 ▶ if (n == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) return 0; 26165 36010 62175
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 36010 nto = (STORE_HAMT->vec_copy)(aTHX_ to);
38 36010 ▶ if (!nto) croak("%s: out of memory", what);
Branch: 1 branch
BranchTrueFalseTotal
if (!nto) croak("%s: out of memory", what); 0 36010 36010
39 90284 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 54274 36010 90284
40 54274 ▶ 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); 36010 18264 54274
41 54274 ▶ 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 54274 54274
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 36010 ▶ if (!undo && s->limited) {
Branch: 2 branches
BranchTrueFalseTotal
if (!undo && s->limited) { 10027 25983 36010
if (!undo && s->limited) { 2425 7602 10027
48 2425 int nomem = 0;
49 2425 hamt_vec *cut = (STORE_HAMT->vec_count)(aTHX_ nto) >= s->limit + STORE_TRIM_SLACK
50 2425 ▶ ? store_trimmed(aTHX_ nto, s->limit, &nomem) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? store_trimmed(aTHX_ nto, s->limit, &nomem) : NULL; 2 2423 2425
51 2425 ▶ if (nomem) {
Branch: 1 branch
BranchTrueFalseTotal
if (nomem) { 0 2425 2425
52 0 (STORE_HAMT->vec_release)(aTHX_ nto);
53 0 croak("%s: out of memory", what);
54 }
55 2425 ▶ 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 2423 2425
56 2425 skip = store_skip_after(s, (STORE_HAMT->vec_count)(aTHX_ nto), skip);
57 }
58 36010 nfrom = (STORE_HAMT->vec_slice)(aTHX_ from, 0, phys - n, &status);
59 36010 ▶ if (!nfrom) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nfrom) { 0 36010 36010
60 0 (STORE_HAMT->vec_release)(aTHX_ nto);
61 0 croak("%s: out of memory", what);
62 }
63  
64 36010 arrive = SvREFCNT_inc_simple_NN((STORE_HAMT->vec_fetch)(aTHX_ from, phys - n));
65 36010 old = s->value;
66 36010 s->value = arrive;
67 36010 ▶ if (undo) { s->past = nfrom; s->future = nto; }
Branch: 1 branch
BranchTrueFalseTotal
if (undo) { s->past = nfrom; s->future = nto; } 25983 10027 36010
68 10027 else { s->future = nfrom; s->past = nto; }
69 36010 s->past_skip = skip;
70  
71 36010 store_enqueue(aTHX_ s, old, arrive);
72 36010 store_sv_release(aTHX_ old);
73 36010 (STORE_HAMT->vec_release)(aTHX_ from);
74 36010 (STORE_HAMT->vec_release)(aTHX_ to);
75 36010 return n;
76 }
77  
78 /* Both stacks emptied; the value stays. */
79 2781 static void store_clear_history(pTHX_ store *s, const char *what) {
80 2781 hamt_vec *past, *future, *dead_past = s->past, *dead_future = s->future;
81  
82 2781 store_check_mutable(aTHX_ s, what);
83 2780 store_check_no_txn(aTHX_ s, what);
84 2779 past = (STORE_HAMT->vec_new)(aTHX);
85 2779 ▶ future = past ? (STORE_HAMT->vec_new)(aTHX) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
future = past ? (STORE_HAMT->vec_new)(aTHX) : NULL; 2779 0 2779
86 2779 ▶ if (!future) {
Branch: 1 branch
BranchTrueFalseTotal
if (!future) { 0 2779 2779
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 2779 s->past = past;
91 2779 s->future = future;
92 2779 s->past_skip = 0;
93 2779 (STORE_HAMT->vec_release)(aTHX_ dead_past);
94 2779 (STORE_HAMT->vec_release)(aTHX_ dead_future);
95 2779 }
96  
97 /* A history limit from Perl: undef is none, and anything else must be a
98 * whole number that is not negative. */
99 2152 static void store_limit_arg(pTHX_ SV *sv, size_t *limit, int *limited, const char *what) {
100 NV nv;
101 2152 sv = store_read_once(aTHX_ sv);
102 2152 ▶ if (!SvOK(sv)) { *limited = 0; *limit = 0; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) { *limited = 0; *limit = 0; return; } 1695 457 2152
103 457 ▶ if (!looks_like_number(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!looks_like_number(sv)) 6 451 457
104 6 croak("%s: the history limit is not a number", what);
105 451 nv = SvNV(sv);
106 451 ▶ if (nv < 0 || nv != (NV)(UV)nv)
Branch: 2 branches
BranchTrueFalseTotal
if (nv < 0 || nv != (NV)(UV)nv) 448 3 451
if (nv < 0 || nv != (NV)(UV)nv) 2 446 448
107 5 croak("%s: the history limit must be a whole number, not negative, or undef", what);
108 446 *limited = 1;
109 446 *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 /* ---- the history as values ---------------------------------------------
136 *
137 * What undo and redo would reach, read without moving: for a program that
138 * keeps a store's history somewhere of its own, a file say, and builds the
139 * store again from it later. Nothing here changes a store that anyone has
140 * seen, so none of it is refused inside a transaction or the validator. */
141  
142 /* The value `index` steps into what undo can reach, 0 the oldest; NULL past
143 * the end. Steps beyond the limit that have not been cut yet are not there,
144 * as they are not there for undo. Borrowed. */
145 305638 static SV *store_past_at(pTHX_ const store *s, size_t index) {
146 305638 ▶ if (index >= store_past_len(aTHX_ s)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (index >= store_past_len(aTHX_ s)) return NULL; 51 305587 305638
147 305587 return (STORE_HAMT->vec_fetch)(aTHX_ s->past, s->past_skip + index);
148 }
149  
150 /* The value `index` redos ahead, 0 the next one; NULL past the end. The
151 * future is kept with the next one last, so this counts from the end. */
152 21328 static SV *store_future_at(pTHX_ const store *s, size_t index) {
153 21328 size_t n = (STORE_HAMT->vec_count)(aTHX_ s->future);
154 21328 ▶ if (index >= n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (index >= n) return NULL; 51 21277 21328
155 21277 return (STORE_HAMT->vec_fetch)(aTHX_ s->future, n - 1 - index);
156 }
157  
158 /* A new store's history, which is empty, replaced by `past`, oldest first,
159 * and `future`, the next to redo first: a copy of each value, read-only, a
160 * NULL among them being undef. Under a limit only the newest `limit` of the
161 * past are kept, as setting that limit would keep them. Both vectors are
162 * filled before either is taken, so running out of memory croaks with the
163 * store as it was. For a store nobody else has yet: no watcher is told and
164 * no validator asked. */
165 2210 static void store_fill_history(pTHX_ store *s, SV *const *past, size_t npast,
166 SV *const *future, size_t nfuture, const char *what) {
167 2210 hamt_vec *p, *f, *dead_past = s->past, *dead_future = s->future;
168 2210 size_t i, from = 0;
169  
170 2210 ▶ if (s->limited && npast > s->limit) from = npast - s->limit;
Branch: 2 branches
BranchTrueFalseTotal
if (s->limited && npast > s->limit) from = npast - s->limit; 404 1806 2210
if (s->limited && npast > s->limit) from = npast - s->limit; 203 201 404
171 2210 p = (STORE_HAMT->vec_new)(aTHX);
172 2210 ▶ f = p ? (STORE_HAMT->vec_new)(aTHX) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
f = p ? (STORE_HAMT->vec_new)(aTHX) : NULL; 2210 0 2210
173 2210 ▶ if (!f) goto nomem;
Branch: 1 branch
BranchTrueFalseTotal
if (!f) goto nomem; 0 2210 2210
174 29198 ▶ for (i = from; i < npast; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = from; i < npast; i++) 26988 2210 29198
175 26988 ▶ if ((STORE_HAMT->vec_push)(aTHX_ p, past[i] ? past[i] : &PL_sv_undef) != HAMT_ABI_OK)
Branch: 2 branches
BranchTrueFalseTotal
if ((STORE_HAMT->vec_push)(aTHX_ p, past[i] ? past[i] : &PL_sv_undef) != HAMT_ABI_OK) 26937 51 26988
if ((STORE_HAMT->vec_push)(aTHX_ p, past[i] ? past[i] : &PL_sv_undef) != HAMT_ABI_OK) 0 26988 26988
176 0 goto nomem;
177 11361 ▶ for (i = nfuture; i-- > 0; )
Branch: 1 branch
BranchTrueFalseTotal
for (i = nfuture; i-- > 0; ) 9151 2210 11361
178 9151 ▶ if ((STORE_HAMT->vec_push)(aTHX_ f, future[i] ? future[i] : &PL_sv_undef) != HAMT_ABI_OK)
Branch: 2 branches
BranchTrueFalseTotal
if ((STORE_HAMT->vec_push)(aTHX_ f, future[i] ? future[i] : &PL_sv_undef) != HAMT_ABI_OK) 9151 0 9151
if ((STORE_HAMT->vec_push)(aTHX_ f, future[i] ? future[i] : &PL_sv_undef) != HAMT_ABI_OK) 0 9151 9151
179 0 goto nomem;
180  
181 2210 s->past = p;
182 2210 s->future = f;
183 2210 s->past_skip = 0;
184 2210 (STORE_HAMT->vec_release)(aTHX_ dead_past);
185 2210 (STORE_HAMT->vec_release)(aTHX_ dead_future);
186 2210 return;
187  
188 0 nomem:
189 0 ▶ if (p) (STORE_HAMT->vec_release)(aTHX_ p);
Branch: 1 branch
BranchTrueFalseTotal
if (p) (STORE_HAMT->vec_release)(aTHX_ p); 0 0 0
190 0 ▶ if (f) (STORE_HAMT->vec_release)(aTHX_ f);
Branch: 1 branch
BranchTrueFalseTotal
if (f) (STORE_HAMT->vec_release)(aTHX_ f); 0 0 0
191 0 croak("%s: out of memory", what);
192 }
193  
194 /* A past or a future from Perl: undef for none, or a reference to an array,
195 * whose values are copied as they would be stored - read once each, so a
196 * tied array or element is read once - into a new mortal array. What the
197 * caller's array does afterwards, or a validator does to it, changes
198 * nothing here. */
199 4621 static AV *store_history_arg(pTHX_ SV *sv, const char *name, const char *what) {
200 AV *from, *into;
201 SSize_t i, top;
202  
203 4621 SvGETMAGIC(sv);
204 4621 ▶ if (!SvOK(sv)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) return NULL; 2 4619 4621
205 4619 ▶ if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVAV) 4416 203 4619
if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVAV) 3 4413 4416
206 206 croak("%s: %s is not an array reference", what, name);
207 4413 from = (AV *)SvRV(sv);
208 4413 into = (AV *)sv_2mortal((SV *)newAV());
209 4413 top = av_len(from);
210 4413 ▶ if (top >= 0) av_extend(into, top);
Branch: 1 branch
BranchTrueFalseTotal
if (top >= 0) av_extend(into, top); 3050 1363 4413
211 43328 ▶ for (i = 0; i <= top; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= top; i++) { 38915 4413 43328
212 38915 SV **e = av_fetch(from, i, 0);
213 38915 ▶ av_push(into, store_copy_in(aTHX_ e ? *e : &PL_sv_undef));
Branch: 1 branch
BranchTrueFalseTotal
av_push(into, store_copy_in(aTHX_ e ? *e : &PL_sv_undef)); 38913 2 38915
214 }
215 4413 return into;
216 }
217  
218 /* How many values such an array holds, and where they start. */
219 #define STORE_AV_COUNT(av) ((av) ? (size_t)(av_len(av) + 1) : 0)
220 #define STORE_AV_VALUES(av) ((av) ? AvARRAY(av) : NULL)
221  
222 /* A count of steps from Perl: absent is one, and anything else must be a
223 * whole number that is not negative. */
224 62032 static size_t store_steps(pTHX_ SV *sv, const char *what) {
225 NV nv;
226 62032 ▶ if (!sv) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!sv) return 1; 11267 50765 62032
227 /* Read once, into a plain copy: SvNV_nomg is not in 5.10 or 5.12. */
228 50765 sv = store_read_once(aTHX_ sv);
229 50765 ▶ if (!SvOK(sv) || !looks_like_number(sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(sv) || !looks_like_number(sv)) 50764 1 50765
if (!SvOK(sv) || !looks_like_number(sv)) 3 50761 50764
230 4 croak("%s: the number of steps is not a number", what);
231 50761 nv = SvNV(sv);
232 50761 ▶ if (nv < 0 || nv != (NV)(UV)nv)
Branch: 2 branches
BranchTrueFalseTotal
if (nv < 0 || nv != (NV)(UV)nv) 50760 1 50761
if (nv < 0 || nv != (NV)(UV)nv) 1 50759 50760
233 2 croak("%s: the number of steps must be a whole number, not negative", what);
234 50759 return (size_t)(UV)nv;
235 }
236  
237 #endif
« include/store/store_call.h ↑ Summary include/store/store_core.h »