Commit | Line | Data |
---|---|---|
b9c5136f | 1 | /* Caching facts about regions of the buffer, for optimization. |
95df8112 | 2 | |
ba318903 | 3 | Copyright (C) 1985-1989, 1993, 1995, 2001-2014 Free Software Foundation, |
ab422c4d | 4 | Inc. |
b9c5136f KH |
5 | |
6 | This file is part of GNU Emacs. | |
7 | ||
9ec0b715 | 8 | GNU Emacs is free software: you can redistribute it and/or modify |
b9c5136f | 9 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
10 | the Free Software Foundation, either version 3 of the License, or |
11 | (at your option) any later version. | |
b9c5136f KH |
12 | |
13 | GNU Emacs is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 19 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
b9c5136f KH |
20 | |
21 | ||
22 | #include <config.h> | |
5890e9f7 JD |
23 | #include <stdio.h> |
24 | ||
b9c5136f | 25 | #include "lisp.h" |
e5560ff7 | 26 | #include "character.h" |
b9c5136f KH |
27 | #include "buffer.h" |
28 | #include "region-cache.h" | |
29 | ||
b9c5136f KH |
30 | \f |
31 | /* Data structures. */ | |
32 | ||
33 | /* The region cache. | |
34 | ||
35 | We want something that maps character positions in a buffer onto | |
36 | values. The representation should deal well with long runs of | |
37 | characters with the same value. | |
38 | ||
39 | The tricky part: the representation should be very cheap to | |
40 | maintain in the presence of many insertions and deletions. If the | |
41 | overhead of maintaining the cache is too high, the speedups it | |
42 | offers will be worthless. | |
43 | ||
44 | ||
45 | We represent the region cache as a sorted array of struct | |
46 | boundary's, each of which contains a buffer position and a value; | |
47 | the value applies to all the characters after the buffer position, | |
48 | until the position of the next boundary, or the end of the buffer. | |
49 | ||
50 | The cache always has a boundary whose position is BUF_BEG, so | |
51 | there's always a value associated with every character in the | |
52 | buffer. Since the cache is sorted, this is always the first | |
53 | element of the cache. | |
54 | ||
55 | To facilitate the insertion and deletion of boundaries in the | |
56 | cache, the cache has a gap, just like Emacs's text buffers do. | |
57 | ||
58 | To help boundary positions float along with insertions and | |
59 | deletions, all boundary positions before the cache gap are stored | |
60 | relative to BUF_BEG (buf) (thus they're >= 0), and all boundary | |
61 | positions after the gap are stored relative to BUF_Z (buf) (thus | |
62 | they're <= 0). Look at BOUNDARY_POS to see this in action. See | |
63 | revalidate_region_cache to see how this helps. */ | |
64 | ||
65 | struct boundary { | |
0065d054 | 66 | ptrdiff_t pos; |
b9c5136f KH |
67 | int value; |
68 | }; | |
69 | ||
70 | struct region_cache { | |
71 | /* A sorted array of locations where the known-ness of the buffer | |
72 | changes. */ | |
73 | struct boundary *boundaries; | |
74 | ||
75 | /* boundaries[gap_start ... gap_start + gap_len - 1] is the gap. */ | |
0065d054 | 76 | ptrdiff_t gap_start, gap_len; |
b9c5136f KH |
77 | |
78 | /* The number of elements allocated to boundaries, not including the | |
79 | gap. */ | |
0065d054 | 80 | ptrdiff_t cache_len; |
b9c5136f KH |
81 | |
82 | /* The areas that haven't changed since the last time we cleaned out | |
83 | invalid entries from the cache. These overlap when the buffer is | |
84 | entirely unchanged. */ | |
0065d054 | 85 | ptrdiff_t beg_unchanged, end_unchanged; |
b9c5136f KH |
86 | |
87 | /* The first and last positions in the buffer. Because boundaries | |
88 | store their positions relative to the start (BEG) and end (Z) of | |
89 | the buffer, knowing these positions allows us to accurately | |
90 | interpret positions without having to pass the buffer structure | |
91 | or its endpoints around all the time. | |
92 | ||
93 | Yes, buffer_beg is always 1. It's there for symmetry with | |
94 | buffer_end and the BEG and BUF_BEG macros. */ | |
0065d054 | 95 | ptrdiff_t buffer_beg, buffer_end; |
b9c5136f KH |
96 | }; |
97 | ||
98 | /* Return the position of boundary i in cache c. */ | |
99 | #define BOUNDARY_POS(c, i) \ | |
100 | ((i) < (c)->gap_start \ | |
101 | ? (c)->buffer_beg + (c)->boundaries[(i)].pos \ | |
102 | : (c)->buffer_end + (c)->boundaries[(c)->gap_len + (i)].pos) | |
103 | ||
104 | /* Return the value for text after boundary i in cache c. */ | |
105 | #define BOUNDARY_VALUE(c, i) \ | |
106 | ((i) < (c)->gap_start \ | |
107 | ? (c)->boundaries[(i)].value \ | |
108 | : (c)->boundaries[(c)->gap_len + (i)].value) | |
109 | ||
110 | /* Set the value for text after boundary i in cache c to v. */ | |
111 | #define SET_BOUNDARY_VALUE(c, i, v) \ | |
112 | ((i) < (c)->gap_start \ | |
113 | ? ((c)->boundaries[(i)].value = (v))\ | |
114 | : ((c)->boundaries[(c)->gap_len + (i)].value = (v))) | |
115 | ||
116 | ||
117 | /* How many elements to add to the gap when we resize the buffer. */ | |
118 | #define NEW_CACHE_GAP (40) | |
119 | ||
120 | /* See invalidate_region_cache; if an invalidation would throw away | |
121 | information about this many characters, call | |
122 | revalidate_region_cache before doing the new invalidation, to | |
123 | preserve that information, instead of throwing it away. */ | |
124 | #define PRESERVE_THRESHOLD (500) | |
125 | ||
971de7fb | 126 | static void revalidate_region_cache (struct buffer *buf, struct region_cache *c); |
b9c5136f KH |
127 | |
128 | \f | |
129 | /* Interface: Allocating, initializing, and disposing of region caches. */ | |
130 | ||
131 | struct region_cache * | |
971de7fb | 132 | new_region_cache (void) |
b9c5136f | 133 | { |
38182d90 | 134 | struct region_cache *c = xmalloc (sizeof *c); |
b9c5136f KH |
135 | |
136 | c->gap_start = 0; | |
137 | c->gap_len = NEW_CACHE_GAP; | |
138 | c->cache_len = 0; | |
23f86fce DA |
139 | c->boundaries = xmalloc ((c->gap_len + c->cache_len) |
140 | * sizeof (*c->boundaries)); | |
b9c5136f KH |
141 | |
142 | c->beg_unchanged = 0; | |
143 | c->end_unchanged = 0; | |
b0ead4a8 SM |
144 | c->buffer_beg = BEG; |
145 | c->buffer_end = BEG; | |
b9c5136f KH |
146 | |
147 | /* Insert the boundary for the buffer start. */ | |
148 | c->cache_len++; | |
149 | c->gap_len--; | |
150 | c->gap_start++; | |
151 | c->boundaries[0].pos = 0; /* from buffer_beg */ | |
152 | c->boundaries[0].value = 0; | |
153 | ||
154 | return c; | |
155 | } | |
156 | ||
157 | void | |
971de7fb | 158 | free_region_cache (struct region_cache *c) |
b9c5136f KH |
159 | { |
160 | xfree (c->boundaries); | |
161 | xfree (c); | |
162 | } | |
163 | ||
164 | \f | |
165 | /* Finding positions in the cache. */ | |
166 | ||
167 | /* Return the index of the last boundary in cache C at or before POS. | |
168 | In other words, return the boundary that specifies the value for | |
169 | the region POS..(POS + 1). | |
170 | ||
171 | This operation should be logarithmic in the number of cache | |
172 | entries. It would be nice if it took advantage of locality of | |
173 | reference, too, by searching entries near the last entry found. */ | |
0065d054 PE |
174 | static ptrdiff_t |
175 | find_cache_boundary (struct region_cache *c, ptrdiff_t pos) | |
b9c5136f | 176 | { |
0065d054 | 177 | ptrdiff_t low = 0, high = c->cache_len; |
b9c5136f KH |
178 | |
179 | while (low + 1 < high) | |
180 | { | |
181 | /* mid is always a valid index, because low < high and ">> 1" | |
182 | rounds down. */ | |
0065d054 PE |
183 | ptrdiff_t mid = (low >> 1) + (high >> 1) + (low & high & 1); |
184 | ptrdiff_t boundary = BOUNDARY_POS (c, mid); | |
b9c5136f KH |
185 | |
186 | if (pos < boundary) | |
187 | high = mid; | |
188 | else | |
189 | low = mid; | |
190 | } | |
191 | ||
192 | /* Some testing. */ | |
f258b4be DA |
193 | eassert (!(BOUNDARY_POS (c, low) > pos |
194 | || (low + 1 < c->cache_len | |
195 | && BOUNDARY_POS (c, low + 1) <= pos))); | |
b9c5136f KH |
196 | |
197 | return low; | |
198 | } | |
199 | ||
200 | ||
201 | \f | |
202 | /* Moving the cache gap around, inserting, and deleting. */ | |
203 | ||
204 | ||
205 | /* Move the gap of cache C to index POS, and make sure it has space | |
206 | for at least MIN_SIZE boundaries. */ | |
207 | static void | |
0065d054 | 208 | move_cache_gap (struct region_cache *c, ptrdiff_t pos, ptrdiff_t min_size) |
b9c5136f KH |
209 | { |
210 | /* Copy these out of the cache and into registers. */ | |
0065d054 PE |
211 | ptrdiff_t gap_start = c->gap_start; |
212 | ptrdiff_t gap_len = c->gap_len; | |
213 | ptrdiff_t buffer_beg = c->buffer_beg; | |
214 | ptrdiff_t buffer_end = c->buffer_end; | |
b9c5136f | 215 | |
b9c5136f KH |
216 | /* We mustn't ever try to put the gap before the dummy start |
217 | boundary. That must always be start-relative. */ | |
f258b4be | 218 | eassert (0 < pos && pos <= c->cache_len); |
b9c5136f KH |
219 | |
220 | /* Need we move the gap right? */ | |
221 | while (gap_start < pos) | |
222 | { | |
223 | /* Copy one boundary from after to before the gap, and | |
224 | convert its position to start-relative. */ | |
225 | c->boundaries[gap_start].pos | |
226 | = (buffer_end | |
227 | + c->boundaries[gap_start + gap_len].pos | |
228 | - buffer_beg); | |
229 | c->boundaries[gap_start].value | |
230 | = c->boundaries[gap_start + gap_len].value; | |
231 | gap_start++; | |
232 | } | |
233 | ||
234 | /* To enlarge the gap, we need to re-allocate the boundary array, and | |
235 | then shift the area after the gap to the new end. Since the cost | |
236 | is proportional to the amount of stuff after the gap, we do the | |
237 | enlargement here, after a right shift but before a left shift, | |
238 | when the portion after the gap is smallest. */ | |
239 | if (gap_len < min_size) | |
240 | { | |
eb424fe3 | 241 | ptrdiff_t i, nboundaries = c->cache_len; |
b9c5136f KH |
242 | |
243 | c->boundaries = | |
eb424fe3 | 244 | xpalloc (c->boundaries, &nboundaries, min_size - gap_len, -1, |
0065d054 | 245 | sizeof *c->boundaries); |
b9c5136f KH |
246 | |
247 | /* Some systems don't provide a version of the copy routine that | |
248 | can be trusted to shift memory upward into an overlapping | |
249 | region. memmove isn't widely available. */ | |
eb424fe3 | 250 | min_size = nboundaries - c->cache_len - gap_len; |
b9c5136f KH |
251 | for (i = c->cache_len - 1; i >= gap_start; i--) |
252 | { | |
253 | c->boundaries[i + min_size].pos = c->boundaries[i + gap_len].pos; | |
254 | c->boundaries[i + min_size].value = c->boundaries[i + gap_len].value; | |
255 | } | |
256 | ||
257 | gap_len = min_size; | |
258 | } | |
259 | ||
260 | /* Need we move the gap left? */ | |
261 | while (pos < gap_start) | |
262 | { | |
263 | gap_start--; | |
264 | ||
265 | /* Copy one region from before to after the gap, and | |
266 | convert its position to end-relative. */ | |
267 | c->boundaries[gap_start + gap_len].pos | |
268 | = c->boundaries[gap_start].pos + buffer_beg - buffer_end; | |
269 | c->boundaries[gap_start + gap_len].value | |
270 | = c->boundaries[gap_start].value; | |
271 | } | |
272 | ||
273 | /* Assign these back into the cache. */ | |
274 | c->gap_start = gap_start; | |
275 | c->gap_len = gap_len; | |
276 | } | |
277 | ||
278 | ||
c4fc4e30 | 279 | /* Insert a new boundary in cache C; it will have cache index I, |
b9c5136f KH |
280 | and have the specified POS and VALUE. */ |
281 | static void | |
0065d054 | 282 | insert_cache_boundary (struct region_cache *c, ptrdiff_t i, ptrdiff_t pos, |
c098fdb8 | 283 | int value) |
b9c5136f | 284 | { |
f258b4be DA |
285 | /* I must be a valid cache index, and we must never want |
286 | to insert something before the dummy first boundary. */ | |
287 | eassert (0 < i && i <= c->cache_len); | |
b9c5136f KH |
288 | |
289 | /* We must only be inserting things in order. */ | |
f258b4be DA |
290 | eassert ((BOUNDARY_POS (c, i - 1) < pos |
291 | && (i == c->cache_len | |
292 | || pos < BOUNDARY_POS (c, i)))); | |
b9c5136f KH |
293 | |
294 | /* The value must be different from the ones around it. However, we | |
295 | temporarily create boundaries that establish the same value as | |
296 | the subsequent boundary, so we're not going to flag that case. */ | |
f258b4be | 297 | eassert (BOUNDARY_VALUE (c, i - 1) != value); |
b9c5136f | 298 | |
c4fc4e30 | 299 | move_cache_gap (c, i, 1); |
b9c5136f | 300 | |
c4fc4e30 PE |
301 | c->boundaries[i].pos = pos - c->buffer_beg; |
302 | c->boundaries[i].value = value; | |
b9c5136f KH |
303 | c->gap_start++; |
304 | c->gap_len--; | |
305 | c->cache_len++; | |
306 | } | |
307 | ||
308 | ||
309 | /* Delete the i'th entry from cache C if START <= i < END. */ | |
310 | ||
311 | static void | |
c098fdb8 | 312 | delete_cache_boundaries (struct region_cache *c, |
0065d054 | 313 | ptrdiff_t start, ptrdiff_t end) |
b9c5136f | 314 | { |
0065d054 | 315 | ptrdiff_t len = end - start; |
b9c5136f KH |
316 | |
317 | /* Gotta be in range. */ | |
f258b4be | 318 | eassert (0 <= start && end <= c->cache_len); |
b9c5136f KH |
319 | |
320 | /* Gotta be in order. */ | |
f258b4be | 321 | eassert (start <= end); |
b9c5136f KH |
322 | |
323 | /* Can't delete the dummy entry. */ | |
f258b4be | 324 | eassert (!(start == 0 && end >= 1)); |
b9c5136f KH |
325 | |
326 | /* Minimize gap motion. If we're deleting nothing, do nothing. */ | |
327 | if (len == 0) | |
328 | ; | |
329 | /* If the gap is before the region to delete, delete from the start | |
330 | forward. */ | |
331 | else if (c->gap_start <= start) | |
332 | { | |
333 | move_cache_gap (c, start, 0); | |
334 | c->gap_len += len; | |
335 | } | |
336 | /* If the gap is after the region to delete, delete from the end | |
337 | backward. */ | |
338 | else if (end <= c->gap_start) | |
339 | { | |
340 | move_cache_gap (c, end, 0); | |
341 | c->gap_start -= len; | |
342 | c->gap_len += len; | |
343 | } | |
344 | /* If the gap is in the region to delete, just expand it. */ | |
345 | else | |
346 | { | |
347 | c->gap_start = start; | |
348 | c->gap_len += len; | |
349 | } | |
350 | ||
351 | c->cache_len -= len; | |
352 | } | |
177c0ea7 | 353 | |
b9c5136f KH |
354 | |
355 | \f | |
356 | /* Set the value for a region. */ | |
357 | ||
358 | /* Set the value in cache C for the region START..END to VALUE. */ | |
359 | static void | |
c098fdb8 | 360 | set_cache_region (struct region_cache *c, |
0065d054 | 361 | ptrdiff_t start, ptrdiff_t end, int value) |
b9c5136f | 362 | { |
f258b4be DA |
363 | eassert (start <= end); |
364 | eassert (c->buffer_beg <= start && end <= c->buffer_end); | |
b9c5136f KH |
365 | |
366 | /* Eliminate this case; then we can assume that start and end-1 are | |
367 | both the locations of real characters in the buffer. */ | |
368 | if (start == end) | |
369 | return; | |
177c0ea7 | 370 | |
b9c5136f KH |
371 | { |
372 | /* We need to make sure that there are no boundaries in the area | |
373 | between start to end; the whole area will have the same value, | |
374 | so those boundaries will not be necessary. | |
177c0ea7 | 375 | |
b9c5136f KH |
376 | Let start_ix be the cache index of the boundary governing the |
377 | first character of start..end, and let end_ix be the cache | |
378 | index of the earliest boundary after the last character in | |
379 | start..end. (This tortured terminology is intended to answer | |
380 | all the "< or <=?" sort of questions.) */ | |
0065d054 PE |
381 | ptrdiff_t start_ix = find_cache_boundary (c, start); |
382 | ptrdiff_t end_ix = find_cache_boundary (c, end - 1) + 1; | |
b9c5136f KH |
383 | |
384 | /* We must remember the value established by the last boundary | |
385 | before end; if that boundary's domain stretches beyond end, | |
386 | we'll need to create a new boundary at end, and that boundary | |
387 | must have that remembered value. */ | |
388 | int value_at_end = BOUNDARY_VALUE (c, end_ix - 1); | |
389 | ||
390 | /* Delete all boundaries strictly within start..end; this means | |
391 | those whose indices are between start_ix (exclusive) and end_ix | |
392 | (exclusive). */ | |
393 | delete_cache_boundaries (c, start_ix + 1, end_ix); | |
394 | ||
395 | /* Make sure we have the right value established going in to | |
396 | start..end from the left, and no unnecessary boundaries. */ | |
397 | if (BOUNDARY_POS (c, start_ix) == start) | |
398 | { | |
399 | /* Is this boundary necessary? If no, remove it; if yes, set | |
400 | its value. */ | |
401 | if (start_ix > 0 | |
402 | && BOUNDARY_VALUE (c, start_ix - 1) == value) | |
403 | { | |
404 | delete_cache_boundaries (c, start_ix, start_ix + 1); | |
405 | start_ix--; | |
406 | } | |
407 | else | |
408 | SET_BOUNDARY_VALUE (c, start_ix, value); | |
409 | } | |
410 | else | |
411 | { | |
412 | /* Do we need to add a new boundary here? */ | |
413 | if (BOUNDARY_VALUE (c, start_ix) != value) | |
414 | { | |
415 | insert_cache_boundary (c, start_ix + 1, start, value); | |
416 | start_ix++; | |
417 | } | |
418 | } | |
177c0ea7 | 419 | |
b9c5136f KH |
420 | /* This is equivalent to letting end_ix float (like a buffer |
421 | marker does) with the insertions and deletions we may have | |
422 | done. */ | |
423 | end_ix = start_ix + 1; | |
424 | ||
425 | /* Make sure we have the correct value established as we leave | |
426 | start..end to the right. */ | |
427 | if (end == c->buffer_end) | |
428 | /* There is no text after start..end; nothing to do. */ | |
429 | ; | |
430 | else if (end_ix >= c->cache_len | |
431 | || end < BOUNDARY_POS (c, end_ix)) | |
432 | { | |
433 | /* There is no boundary at end, but we may need one. */ | |
434 | if (value_at_end != value) | |
435 | insert_cache_boundary (c, end_ix, end, value_at_end); | |
436 | } | |
437 | else | |
438 | { | |
439 | /* There is a boundary at end; should it be there? */ | |
440 | if (value == BOUNDARY_VALUE (c, end_ix)) | |
441 | delete_cache_boundaries (c, end_ix, end_ix + 1); | |
442 | } | |
443 | } | |
444 | } | |
445 | ||
446 | ||
447 | \f | |
448 | /* Interface: Invalidating the cache. Private: Re-validating the cache. */ | |
449 | ||
450 | /* Indicate that a section of BUF has changed, to invalidate CACHE. | |
451 | HEAD is the number of chars unchanged at the beginning of the buffer. | |
452 | TAIL is the number of chars unchanged at the end of the buffer. | |
453 | NOTE: this is *not* the same as the ending position of modified | |
454 | region. | |
455 | (This way of specifying regions makes more sense than absolute | |
456 | buffer positions in the presence of insertions and deletions; the | |
457 | args to pass are the same before and after such an operation.) */ | |
458 | void | |
c098fdb8 | 459 | invalidate_region_cache (struct buffer *buf, struct region_cache *c, |
0065d054 | 460 | ptrdiff_t head, ptrdiff_t tail) |
b9c5136f KH |
461 | { |
462 | /* Let chead = c->beg_unchanged, and | |
463 | ctail = c->end_unchanged. | |
464 | If z-tail < beg+chead by a large amount, or | |
465 | z-ctail < beg+head by a large amount, | |
466 | ||
467 | then cutting back chead and ctail to head and tail would lose a | |
468 | lot of information that we could preserve by revalidating the | |
469 | cache before processing this invalidation. Losing that | |
470 | information may be more costly than revalidating the cache now. | |
471 | So go ahead and call revalidate_region_cache if it seems that it | |
472 | might be worthwhile. */ | |
473 | if (((BUF_BEG (buf) + c->beg_unchanged) - (BUF_Z (buf) - tail) | |
474 | > PRESERVE_THRESHOLD) | |
475 | || ((BUF_BEG (buf) + head) - (BUF_Z (buf) - c->end_unchanged) | |
476 | > PRESERVE_THRESHOLD)) | |
477 | revalidate_region_cache (buf, c); | |
478 | ||
479 | ||
480 | if (head < c->beg_unchanged) | |
481 | c->beg_unchanged = head; | |
482 | if (tail < c->end_unchanged) | |
483 | c->end_unchanged = tail; | |
484 | ||
485 | /* We now know nothing about the region between the unchanged head | |
486 | and the unchanged tail (call it the "modified region"), not even | |
487 | its length. | |
488 | ||
489 | If the modified region has shrunk in size (deletions do this), | |
490 | then the cache may now contain boundaries originally located in | |
491 | text that doesn't exist any more. | |
492 | ||
493 | If the modified region has increased in size (insertions do | |
494 | this), then there may now be boundaries in the modified region | |
495 | whose positions are wrong. | |
496 | ||
497 | Even calling BOUNDARY_POS on boundaries still in the unchanged | |
498 | head or tail may well give incorrect answers now, since | |
499 | c->buffer_beg and c->buffer_end may well be wrong now. (Well, | |
500 | okay, c->buffer_beg never changes, so boundaries in the unchanged | |
501 | head will still be okay. But it's the principle of the thing.) | |
502 | ||
503 | So things are generally a mess. | |
504 | ||
505 | But we don't clean up this mess here; that would be expensive, | |
506 | and this function gets called every time any buffer modification | |
507 | occurs. Rather, we can clean up everything in one swell foop, | |
508 | accounting for all the modifications at once, by calling | |
509 | revalidate_region_cache before we try to consult the cache the | |
510 | next time. */ | |
511 | } | |
512 | ||
513 | ||
177c0ea7 | 514 | /* Clean out any cache entries applying to the modified region, and |
b9c5136f KH |
515 | make the positions of the remaining entries accurate again. |
516 | ||
517 | After calling this function, the mess described in the comment in | |
518 | invalidate_region_cache is cleaned up. | |
519 | ||
520 | This function operates by simply throwing away everything it knows | |
521 | about the modified region. It doesn't care exactly which | |
522 | insertions and deletions took place; it just tosses it all. | |
523 | ||
524 | For example, if you insert a single character at the beginning of | |
525 | the buffer, and a single character at the end of the buffer (for | |
526 | example), without calling this function in between the two | |
527 | insertions, then the entire cache will be freed of useful | |
528 | information. On the other hand, if you do manage to call this | |
529 | function in between the two insertions, then the modified regions | |
530 | will be small in both cases, no information will be tossed, and the | |
531 | cache will know that it doesn't have knowledge of the first and | |
532 | last characters any more. | |
533 | ||
534 | Calling this function may be expensive; it does binary searches in | |
535 | the cache, and causes cache gap motion. */ | |
536 | ||
537 | static void | |
971de7fb | 538 | revalidate_region_cache (struct buffer *buf, struct region_cache *c) |
b9c5136f KH |
539 | { |
540 | /* The boundaries now in the cache are expressed relative to the | |
541 | buffer_beg and buffer_end values stored in the cache. Now, | |
542 | buffer_beg and buffer_end may not be the same as BUF_BEG (buf) | |
543 | and BUF_Z (buf), so we have two different "bases" to deal with | |
544 | --- the cache's, and the buffer's. */ | |
545 | ||
546 | /* If the entire buffer is still valid, don't waste time. Yes, this | |
547 | should be a >, not a >=; think about what beg_unchanged and | |
548 | end_unchanged get set to when the only change has been an | |
549 | insertion. */ | |
550 | if (c->buffer_beg + c->beg_unchanged | |
551 | > c->buffer_end - c->end_unchanged) | |
552 | return; | |
553 | ||
554 | /* If all the text we knew about as of the last cache revalidation | |
555 | is still there, then all of the information in the cache is still | |
556 | valid. Because c->buffer_beg and c->buffer_end are out-of-date, | |
557 | the modified region appears from the cache's point of view to be | |
558 | a null region located someplace in the buffer. | |
559 | ||
560 | Now, invalidating that empty string will have no actual affect on | |
561 | the cache; instead, we need to update the cache's basis first | |
562 | (which will give the modified region the same size in the cache | |
563 | as it has in the buffer), and then invalidate the modified | |
564 | region. */ | |
177c0ea7 | 565 | if (c->buffer_beg + c->beg_unchanged |
b9c5136f KH |
566 | == c->buffer_end - c->end_unchanged) |
567 | { | |
568 | /* Move the gap so that all the boundaries in the unchanged head | |
569 | are expressed beg-relative, and all the boundaries in the | |
570 | unchanged tail are expressed end-relative. That done, we can | |
571 | plug in the new buffer beg and end, and all the positions | |
572 | will be accurate. | |
573 | ||
574 | The boundary which has jurisdiction over the modified region | |
575 | should be left before the gap. */ | |
576 | move_cache_gap (c, | |
577 | (find_cache_boundary (c, (c->buffer_beg | |
578 | + c->beg_unchanged)) | |
579 | + 1), | |
580 | 0); | |
581 | ||
582 | c->buffer_beg = BUF_BEG (buf); | |
583 | c->buffer_end = BUF_Z (buf); | |
584 | ||
585 | /* Now that the cache's basis has been changed, the modified | |
586 | region actually takes up some space in the cache, so we can | |
587 | invalidate it. */ | |
588 | set_cache_region (c, | |
589 | c->buffer_beg + c->beg_unchanged, | |
590 | c->buffer_end - c->end_unchanged, | |
591 | 0); | |
592 | } | |
593 | ||
594 | /* Otherwise, there is a non-empty region in the cache which | |
595 | corresponds to the modified region of the buffer. */ | |
596 | else | |
597 | { | |
0065d054 | 598 | ptrdiff_t modified_ix; |
b9c5136f KH |
599 | |
600 | /* These positions are correct, relative to both the cache basis | |
601 | and the buffer basis. */ | |
602 | set_cache_region (c, | |
603 | c->buffer_beg + c->beg_unchanged, | |
604 | c->buffer_end - c->end_unchanged, | |
605 | 0); | |
606 | ||
607 | /* Now the cache contains only boundaries that are in the | |
608 | unchanged head and tail; we've disposed of any boundaries | |
609 | whose positions we can't be sure of given the information | |
610 | we've saved. | |
611 | ||
612 | If we put the cache gap between the unchanged head and the | |
613 | unchanged tail, we can adjust all the boundary positions at | |
614 | once, simply by setting buffer_beg and buffer_end. | |
615 | ||
616 | The boundary which has jurisdiction over the modified region | |
617 | should be left before the gap. */ | |
618 | modified_ix = | |
619 | find_cache_boundary (c, (c->buffer_beg + c->beg_unchanged)) + 1; | |
620 | move_cache_gap (c, modified_ix, 0); | |
621 | ||
622 | c->buffer_beg = BUF_BEG (buf); | |
623 | c->buffer_end = BUF_Z (buf); | |
624 | ||
625 | /* Now, we may have shrunk the buffer when we changed the basis, | |
626 | and brought the boundaries we created for the start and end | |
627 | of the modified region together, giving them the same | |
628 | position. If that's the case, we should collapse them into | |
629 | one boundary. Or we may even delete them both, if the values | |
630 | before and after them are the same. */ | |
631 | if (modified_ix < c->cache_len | |
632 | && (BOUNDARY_POS (c, modified_ix - 1) | |
633 | == BOUNDARY_POS (c, modified_ix))) | |
634 | { | |
635 | int value_after = BOUNDARY_VALUE (c, modified_ix); | |
636 | ||
637 | /* Should we remove both of the boundaries? Yes, if the | |
638 | latter boundary is now establishing the same value that | |
639 | the former boundary's predecessor does. */ | |
640 | if (modified_ix - 1 > 0 | |
641 | && value_after == BOUNDARY_VALUE (c, modified_ix - 2)) | |
642 | delete_cache_boundaries (c, modified_ix - 1, modified_ix + 1); | |
643 | else | |
644 | { | |
645 | /* We do need a boundary here; collapse the two | |
646 | boundaries into one. */ | |
647 | SET_BOUNDARY_VALUE (c, modified_ix - 1, value_after); | |
648 | delete_cache_boundaries (c, modified_ix, modified_ix + 1); | |
649 | } | |
650 | } | |
651 | } | |
652 | ||
653 | /* Now the entire cache is valid. */ | |
654 | c->beg_unchanged | |
655 | = c->end_unchanged | |
656 | = c->buffer_end - c->buffer_beg; | |
657 | } | |
658 | ||
659 | \f | |
660 | /* Interface: Adding information to the cache. */ | |
661 | ||
662 | /* Assert that the region of BUF between START and END (absolute | |
663 | buffer positions) is "known," for the purposes of CACHE (e.g. "has | |
664 | no newlines", in the case of the line cache). */ | |
665 | void | |
c098fdb8 | 666 | know_region_cache (struct buffer *buf, struct region_cache *c, |
0065d054 | 667 | ptrdiff_t start, ptrdiff_t end) |
b9c5136f KH |
668 | { |
669 | revalidate_region_cache (buf, c); | |
670 | ||
671 | set_cache_region (c, start, end, 1); | |
672 | } | |
673 | ||
674 | \f | |
675 | /* Interface: using the cache. */ | |
676 | ||
457882c2 PE |
677 | /* Return the value for the text immediately after POS in BUF if the value |
678 | is known, for the purposes of CACHE, and return zero otherwise. | |
679 | If NEXT is non-zero, set *NEXT to the nearest | |
333f9019 | 680 | position after POS where the knowledge changes. */ |
b9c5136f | 681 | int |
c098fdb8 | 682 | region_cache_forward (struct buffer *buf, struct region_cache *c, |
0065d054 | 683 | ptrdiff_t pos, ptrdiff_t *next) |
b9c5136f KH |
684 | { |
685 | revalidate_region_cache (buf, c); | |
686 | ||
687 | { | |
0065d054 | 688 | ptrdiff_t i = find_cache_boundary (c, pos); |
b9c5136f | 689 | int i_value = BOUNDARY_VALUE (c, i); |
0065d054 | 690 | ptrdiff_t j; |
b9c5136f KH |
691 | |
692 | /* Beyond the end of the buffer is unknown, by definition. */ | |
693 | if (pos >= BUF_Z (buf)) | |
694 | { | |
695 | if (next) *next = BUF_Z (buf); | |
696 | i_value = 0; | |
697 | } | |
698 | else if (next) | |
699 | { | |
700 | /* Scan forward from i to find the next differing position. */ | |
701 | for (j = i + 1; j < c->cache_len; j++) | |
702 | if (BOUNDARY_VALUE (c, j) != i_value) | |
703 | break; | |
704 | ||
705 | if (j < c->cache_len) | |
706 | *next = BOUNDARY_POS (c, j); | |
707 | else | |
708 | *next = BUF_Z (buf); | |
709 | } | |
710 | ||
711 | return i_value; | |
712 | } | |
713 | } | |
714 | ||
457882c2 PE |
715 | /* Return the value for the text immediately before POS in BUF if the |
716 | value is known, for the purposes of CACHE, and return zero | |
717 | otherwise. If NEXT is non-zero, set *NEXT to the nearest | |
333f9019 | 718 | position before POS where the knowledge changes. */ |
96c06863 PE |
719 | int |
720 | region_cache_backward (struct buffer *buf, struct region_cache *c, | |
721 | ptrdiff_t pos, ptrdiff_t *next) | |
b9c5136f KH |
722 | { |
723 | revalidate_region_cache (buf, c); | |
724 | ||
725 | /* Before the beginning of the buffer is unknown, by | |
726 | definition. */ | |
727 | if (pos <= BUF_BEG (buf)) | |
728 | { | |
729 | if (next) *next = BUF_BEG (buf); | |
730 | return 0; | |
731 | } | |
732 | ||
733 | { | |
0065d054 | 734 | ptrdiff_t i = find_cache_boundary (c, pos - 1); |
b9c5136f | 735 | int i_value = BOUNDARY_VALUE (c, i); |
0065d054 | 736 | ptrdiff_t j; |
b9c5136f KH |
737 | |
738 | if (next) | |
739 | { | |
740 | /* Scan backward from i to find the next differing position. */ | |
741 | for (j = i - 1; j >= 0; j--) | |
742 | if (BOUNDARY_VALUE (c, j) != i_value) | |
743 | break; | |
744 | ||
745 | if (j >= 0) | |
746 | *next = BOUNDARY_POS (c, j + 1); | |
747 | else | |
748 | *next = BUF_BEG (buf); | |
749 | } | |
750 | ||
751 | return i_value; | |
752 | } | |
753 | } | |
754 | ||
f258b4be DA |
755 | #ifdef ENABLE_CHECKING |
756 | ||
b9c5136f KH |
757 | /* Debugging: pretty-print a cache to the standard error output. */ |
758 | ||
e3b27b31 | 759 | void pp_cache (struct region_cache *) EXTERNALLY_VISIBLE; |
b9c5136f | 760 | void |
971de7fb | 761 | pp_cache (struct region_cache *c) |
b9c5136f | 762 | { |
0065d054 PE |
763 | ptrdiff_t i; |
764 | ptrdiff_t beg_u = c->buffer_beg + c->beg_unchanged; | |
765 | ptrdiff_t end_u = c->buffer_end - c->end_unchanged; | |
b9c5136f KH |
766 | |
767 | fprintf (stderr, | |
0065d054 | 768 | "basis: %"pD"d..%"pD"d modified: %"pD"d..%"pD"d\n", |
c2982e87 PE |
769 | c->buffer_beg, c->buffer_end, |
770 | beg_u, end_u); | |
b9c5136f KH |
771 | |
772 | for (i = 0; i < c->cache_len; i++) | |
773 | { | |
0065d054 | 774 | ptrdiff_t pos = BOUNDARY_POS (c, i); |
b9c5136f KH |
775 | |
776 | putc (((pos < beg_u) ? 'v' | |
777 | : (pos == beg_u) ? '-' | |
778 | : ' '), | |
779 | stderr); | |
780 | putc (((pos > end_u) ? '^' | |
781 | : (pos == end_u) ? '-' | |
782 | : ' '), | |
783 | stderr); | |
0065d054 | 784 | fprintf (stderr, "%"pD"d : %d\n", pos, BOUNDARY_VALUE (c, i)); |
b9c5136f KH |
785 | } |
786 | } | |
f258b4be DA |
787 | |
788 | #endif /* ENABLE_CHECKING */ |