root/src/region-cache.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. new_region_cache
  2. free_region_cache
  3. find_cache_boundary
  4. move_cache_gap
  5. insert_cache_boundary
  6. delete_cache_boundaries
  7. set_cache_region
  8. invalidate_region_cache
  9. revalidate_region_cache
  10. know_region_cache
  11. region_cache_forward
  12. region_cache_backward
  13. pp_cache

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

/* [<][>][^][v][top][bottom][index][help] */