aboutsummaryrefslogtreecommitdiffstats
path: root/src/region-cache.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/region-cache.c')
-rw-r--r--src/region-cache.c90
1 files changed, 42 insertions, 48 deletions
diff --git a/src/region-cache.c b/src/region-cache.c
index d701f4d71b0..9f4e5921877 100644
--- a/src/region-cache.c
+++ b/src/region-cache.c
@@ -1,6 +1,6 @@
1/* Caching facts about regions of the buffer, for optimization. 1/* Caching facts about regions of the buffer, for optimization.
2 2
3Copyright (C) 1985-1989, 1993, 1995, 2001-2011 3Copyright (C) 1985-1989, 1993, 1995, 2001-2012
4 Free Software Foundation, Inc. 4 Free Software Foundation, Inc.
5 5
6This file is part of GNU Emacs. 6This file is part of GNU Emacs.
@@ -63,7 +63,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
63 revalidate_region_cache to see how this helps. */ 63 revalidate_region_cache to see how this helps. */
64 64
65struct boundary { 65struct boundary {
66 EMACS_INT pos; 66 ptrdiff_t pos;
67 int value; 67 int value;
68}; 68};
69 69
@@ -73,16 +73,16 @@ struct region_cache {
73 struct boundary *boundaries; 73 struct boundary *boundaries;
74 74
75 /* boundaries[gap_start ... gap_start + gap_len - 1] is the gap. */ 75 /* boundaries[gap_start ... gap_start + gap_len - 1] is the gap. */
76 EMACS_INT gap_start, gap_len; 76 ptrdiff_t gap_start, gap_len;
77 77
78 /* The number of elements allocated to boundaries, not including the 78 /* The number of elements allocated to boundaries, not including the
79 gap. */ 79 gap. */
80 EMACS_INT cache_len; 80 ptrdiff_t cache_len;
81 81
82 /* The areas that haven't changed since the last time we cleaned out 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 83 invalid entries from the cache. These overlap when the buffer is
84 entirely unchanged. */ 84 entirely unchanged. */
85 EMACS_INT beg_unchanged, end_unchanged; 85 ptrdiff_t beg_unchanged, end_unchanged;
86 86
87 /* The first and last positions in the buffer. Because boundaries 87 /* The first and last positions in the buffer. Because boundaries
88 store their positions relative to the start (BEG) and end (Z) of 88 store their positions relative to the start (BEG) and end (Z) of
@@ -92,7 +92,7 @@ struct region_cache {
92 92
93 Yes, buffer_beg is always 1. It's there for symmetry with 93 Yes, buffer_beg is always 1. It's there for symmetry with
94 buffer_end and the BEG and BUF_BEG macros. */ 94 buffer_end and the BEG and BUF_BEG macros. */
95 EMACS_INT buffer_beg, buffer_end; 95 ptrdiff_t buffer_beg, buffer_end;
96}; 96};
97 97
98/* Return the position of boundary i in cache c. */ 98/* Return the position of boundary i in cache c. */
@@ -173,17 +173,17 @@ free_region_cache (struct region_cache *c)
173 This operation should be logarithmic in the number of cache 173 This operation should be logarithmic in the number of cache
174 entries. It would be nice if it took advantage of locality of 174 entries. It would be nice if it took advantage of locality of
175 reference, too, by searching entries near the last entry found. */ 175 reference, too, by searching entries near the last entry found. */
176static EMACS_INT 176static ptrdiff_t
177find_cache_boundary (struct region_cache *c, EMACS_INT pos) 177find_cache_boundary (struct region_cache *c, ptrdiff_t pos)
178{ 178{
179 EMACS_INT low = 0, high = c->cache_len; 179 ptrdiff_t low = 0, high = c->cache_len;
180 180
181 while (low + 1 < high) 181 while (low + 1 < high)
182 { 182 {
183 /* mid is always a valid index, because low < high and ">> 1" 183 /* mid is always a valid index, because low < high and ">> 1"
184 rounds down. */ 184 rounds down. */
185 EMACS_INT mid = (low + high) >> 1; 185 ptrdiff_t mid = (low >> 1) + (high >> 1) + (low & high & 1);
186 EMACS_INT boundary = BOUNDARY_POS (c, mid); 186 ptrdiff_t boundary = BOUNDARY_POS (c, mid);
187 187
188 if (pos < boundary) 188 if (pos < boundary)
189 high = mid; 189 high = mid;
@@ -208,13 +208,13 @@ find_cache_boundary (struct region_cache *c, EMACS_INT pos)
208/* Move the gap of cache C to index POS, and make sure it has space 208/* Move the gap of cache C to index POS, and make sure it has space
209 for at least MIN_SIZE boundaries. */ 209 for at least MIN_SIZE boundaries. */
210static void 210static void
211move_cache_gap (struct region_cache *c, EMACS_INT pos, EMACS_INT min_size) 211move_cache_gap (struct region_cache *c, ptrdiff_t pos, ptrdiff_t min_size)
212{ 212{
213 /* Copy these out of the cache and into registers. */ 213 /* Copy these out of the cache and into registers. */
214 EMACS_INT gap_start = c->gap_start; 214 ptrdiff_t gap_start = c->gap_start;
215 EMACS_INT gap_len = c->gap_len; 215 ptrdiff_t gap_len = c->gap_len;
216 EMACS_INT buffer_beg = c->buffer_beg; 216 ptrdiff_t buffer_beg = c->buffer_beg;
217 EMACS_INT buffer_end = c->buffer_end; 217 ptrdiff_t buffer_end = c->buffer_end;
218 218
219 if (pos < 0 219 if (pos < 0
220 || pos > c->cache_len) 220 || pos > c->cache_len)
@@ -246,17 +246,11 @@ move_cache_gap (struct region_cache *c, EMACS_INT pos, EMACS_INT min_size)
246 when the portion after the gap is smallest. */ 246 when the portion after the gap is smallest. */
247 if (gap_len < min_size) 247 if (gap_len < min_size)
248 { 248 {
249 EMACS_INT i; 249 ptrdiff_t i;
250
251 /* Always make at least NEW_CACHE_GAP elements, as long as we're
252 expanding anyway. */
253 if (min_size < NEW_CACHE_GAP)
254 min_size = NEW_CACHE_GAP;
255 250
256 c->boundaries = 251 c->boundaries =
257 (struct boundary *) xrealloc (c->boundaries, 252 xpalloc (c->boundaries, &c->cache_len, min_size, -1,
258 ((min_size + c->cache_len) 253 sizeof *c->boundaries);
259 * sizeof (*c->boundaries)));
260 254
261 /* Some systems don't provide a version of the copy routine that 255 /* Some systems don't provide a version of the copy routine that
262 can be trusted to shift memory upward into an overlapping 256 can be trusted to shift memory upward into an overlapping
@@ -293,7 +287,7 @@ move_cache_gap (struct region_cache *c, EMACS_INT pos, EMACS_INT min_size)
293/* Insert a new boundary in cache C; it will have cache index I, 287/* Insert a new boundary in cache C; it will have cache index I,
294 and have the specified POS and VALUE. */ 288 and have the specified POS and VALUE. */
295static void 289static void
296insert_cache_boundary (struct region_cache *c, EMACS_INT i, EMACS_INT pos, 290insert_cache_boundary (struct region_cache *c, ptrdiff_t i, ptrdiff_t pos,
297 int value) 291 int value)
298{ 292{
299 /* i must be a valid cache index. */ 293 /* i must be a valid cache index. */
@@ -331,9 +325,9 @@ insert_cache_boundary (struct region_cache *c, EMACS_INT i, EMACS_INT pos,
331 325
332static void 326static void
333delete_cache_boundaries (struct region_cache *c, 327delete_cache_boundaries (struct region_cache *c,
334 EMACS_INT start, EMACS_INT end) 328 ptrdiff_t start, ptrdiff_t end)
335{ 329{
336 EMACS_INT len = end - start; 330 ptrdiff_t len = end - start;
337 331
338 /* Gotta be in range. */ 332 /* Gotta be in range. */
339 if (start < 0 333 if (start < 0
@@ -384,7 +378,7 @@ delete_cache_boundaries (struct region_cache *c,
384/* Set the value in cache C for the region START..END to VALUE. */ 378/* Set the value in cache C for the region START..END to VALUE. */
385static void 379static void
386set_cache_region (struct region_cache *c, 380set_cache_region (struct region_cache *c,
387 EMACS_INT start, EMACS_INT end, int value) 381 ptrdiff_t start, ptrdiff_t end, int value)
388{ 382{
389 if (start > end) 383 if (start > end)
390 abort (); 384 abort ();
@@ -407,8 +401,8 @@ set_cache_region (struct region_cache *c,
407 index of the earliest boundary after the last character in 401 index of the earliest boundary after the last character in
408 start..end. (This tortured terminology is intended to answer 402 start..end. (This tortured terminology is intended to answer
409 all the "< or <=?" sort of questions.) */ 403 all the "< or <=?" sort of questions.) */
410 EMACS_INT start_ix = find_cache_boundary (c, start); 404 ptrdiff_t start_ix = find_cache_boundary (c, start);
411 EMACS_INT end_ix = find_cache_boundary (c, end - 1) + 1; 405 ptrdiff_t end_ix = find_cache_boundary (c, end - 1) + 1;
412 406
413 /* We must remember the value established by the last boundary 407 /* We must remember the value established by the last boundary
414 before end; if that boundary's domain stretches beyond end, 408 before end; if that boundary's domain stretches beyond end,
@@ -486,7 +480,7 @@ set_cache_region (struct region_cache *c,
486 args to pass are the same before and after such an operation.) */ 480 args to pass are the same before and after such an operation.) */
487void 481void
488invalidate_region_cache (struct buffer *buf, struct region_cache *c, 482invalidate_region_cache (struct buffer *buf, struct region_cache *c,
489 EMACS_INT head, EMACS_INT tail) 483 ptrdiff_t head, ptrdiff_t tail)
490{ 484{
491 /* Let chead = c->beg_unchanged, and 485 /* Let chead = c->beg_unchanged, and
492 ctail = c->end_unchanged. 486 ctail = c->end_unchanged.
@@ -624,7 +618,7 @@ revalidate_region_cache (struct buffer *buf, struct region_cache *c)
624 corresponds to the modified region of the buffer. */ 618 corresponds to the modified region of the buffer. */
625 else 619 else
626 { 620 {
627 EMACS_INT modified_ix; 621 ptrdiff_t modified_ix;
628 622
629 /* These positions are correct, relative to both the cache basis 623 /* These positions are correct, relative to both the cache basis
630 and the buffer basis. */ 624 and the buffer basis. */
@@ -693,7 +687,7 @@ revalidate_region_cache (struct buffer *buf, struct region_cache *c)
693 no newlines", in the case of the line cache). */ 687 no newlines", in the case of the line cache). */
694void 688void
695know_region_cache (struct buffer *buf, struct region_cache *c, 689know_region_cache (struct buffer *buf, struct region_cache *c,
696 EMACS_INT start, EMACS_INT end) 690 ptrdiff_t start, ptrdiff_t end)
697{ 691{
698 revalidate_region_cache (buf, c); 692 revalidate_region_cache (buf, c);
699 693
@@ -705,17 +699,17 @@ know_region_cache (struct buffer *buf, struct region_cache *c,
705 699
706/* Return true if the text immediately after POS in BUF is known, for 700/* Return true if the text immediately after POS in BUF is known, for
707 the purposes of CACHE. If NEXT is non-zero, set *NEXT to the nearest 701 the purposes of CACHE. If NEXT is non-zero, set *NEXT to the nearest
708 position after POS where the knownness changes. */ 702 position after POS where the knowledge changes. */
709int 703int
710region_cache_forward (struct buffer *buf, struct region_cache *c, 704region_cache_forward (struct buffer *buf, struct region_cache *c,
711 EMACS_INT pos, EMACS_INT *next) 705 ptrdiff_t pos, ptrdiff_t *next)
712{ 706{
713 revalidate_region_cache (buf, c); 707 revalidate_region_cache (buf, c);
714 708
715 { 709 {
716 EMACS_INT i = find_cache_boundary (c, pos); 710 ptrdiff_t i = find_cache_boundary (c, pos);
717 int i_value = BOUNDARY_VALUE (c, i); 711 int i_value = BOUNDARY_VALUE (c, i);
718 EMACS_INT j; 712 ptrdiff_t j;
719 713
720 /* Beyond the end of the buffer is unknown, by definition. */ 714 /* Beyond the end of the buffer is unknown, by definition. */
721 if (pos >= BUF_Z (buf)) 715 if (pos >= BUF_Z (buf))
@@ -742,9 +736,9 @@ region_cache_forward (struct buffer *buf, struct region_cache *c,
742 736
743/* Return true if the text immediately before POS in BUF is known, for 737/* Return true if the text immediately before POS in BUF is known, for
744 the purposes of CACHE. If NEXT is non-zero, set *NEXT to the nearest 738 the purposes of CACHE. If NEXT is non-zero, set *NEXT to the nearest
745 position before POS where the knownness changes. */ 739 position before POS where the knowledge changes. */
746int region_cache_backward (struct buffer *buf, struct region_cache *c, 740int region_cache_backward (struct buffer *buf, struct region_cache *c,
747 EMACS_INT pos, EMACS_INT *next) 741 ptrdiff_t pos, ptrdiff_t *next)
748{ 742{
749 revalidate_region_cache (buf, c); 743 revalidate_region_cache (buf, c);
750 744
@@ -757,9 +751,9 @@ int region_cache_backward (struct buffer *buf, struct region_cache *c,
757 } 751 }
758 752
759 { 753 {
760 EMACS_INT i = find_cache_boundary (c, pos - 1); 754 ptrdiff_t i = find_cache_boundary (c, pos - 1);
761 int i_value = BOUNDARY_VALUE (c, i); 755 int i_value = BOUNDARY_VALUE (c, i);
762 EMACS_INT j; 756 ptrdiff_t j;
763 757
764 if (next) 758 if (next)
765 { 759 {
@@ -785,18 +779,18 @@ void pp_cache (struct region_cache *) EXTERNALLY_VISIBLE;
785void 779void
786pp_cache (struct region_cache *c) 780pp_cache (struct region_cache *c)
787{ 781{
788 int i; 782 ptrdiff_t i;
789 EMACS_INT beg_u = c->buffer_beg + c->beg_unchanged; 783 ptrdiff_t beg_u = c->buffer_beg + c->beg_unchanged;
790 EMACS_INT end_u = c->buffer_end - c->end_unchanged; 784 ptrdiff_t end_u = c->buffer_end - c->end_unchanged;
791 785
792 fprintf (stderr, 786 fprintf (stderr,
793 "basis: %"pI"d..%"pI"d modified: %"pI"d..%"pI"d\n", 787 "basis: %"pD"d..%"pD"d modified: %"pD"d..%"pD"d\n",
794 c->buffer_beg, c->buffer_end, 788 c->buffer_beg, c->buffer_end,
795 beg_u, end_u); 789 beg_u, end_u);
796 790
797 for (i = 0; i < c->cache_len; i++) 791 for (i = 0; i < c->cache_len; i++)
798 { 792 {
799 EMACS_INT pos = BOUNDARY_POS (c, i); 793 ptrdiff_t pos = BOUNDARY_POS (c, i);
800 794
801 putc (((pos < beg_u) ? 'v' 795 putc (((pos < beg_u) ? 'v'
802 : (pos == beg_u) ? '-' 796 : (pos == beg_u) ? '-'
@@ -806,6 +800,6 @@ pp_cache (struct region_cache *c)
806 : (pos == end_u) ? '-' 800 : (pos == end_u) ? '-'
807 : ' '), 801 : ' '),
808 stderr); 802 stderr);
809 fprintf (stderr, "%"pI"d : %d\n", pos, BOUNDARY_VALUE (c, i)); 803 fprintf (stderr, "%"pD"d : %d\n", pos, BOUNDARY_VALUE (c, i));
810 } 804 }
811} 805}