Refill some long/short copyright headers.
[bpt/emacs.git] / src / region-cache.c
index cedb001..1f9b62d 100644 (file)
@@ -1,13 +1,14 @@
 /* Caching facts about regions of the buffer, for optimization.
-   Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1995, 2002, 2003, 2004,
-                 2005, 2006 Free Software Foundation, Inc.
+
+Copyright (C) 1985-1989, 1993, 1995, 2001-2011
+  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
-GNU Emacs is free software; you can redistribute it and/or modify
+GNU Emacs is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
-any later version.
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -15,13 +16,12 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-Boston, MA 02110-1301, USA.  */
+along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 
 #include <config.h>
 #include <stdio.h>
+#include <setjmp.h>
 
 #include "lisp.h"
 #include "buffer.h"
@@ -63,7 +63,7 @@ Boston, MA 02110-1301, USA.  */
    revalidate_region_cache to see how this helps.  */
 
 struct boundary {
-  int pos;
+  EMACS_INT pos;
   int value;
 };
 
@@ -73,16 +73,16 @@ struct region_cache {
   struct boundary *boundaries;
 
   /* boundaries[gap_start ... gap_start + gap_len - 1] is the gap.  */
-  int gap_start, gap_len;
+  EMACS_INT gap_start, gap_len;
 
   /* The number of elements allocated to boundaries, not including the
      gap.  */
-  int cache_len;
+  EMACS_INT cache_len;
 
   /* The areas that haven't changed since the last time we cleaned out
      invalid entries from the cache.  These overlap when the buffer is
      entirely unchanged.  */
-  int beg_unchanged, end_unchanged;
+  EMACS_INT beg_unchanged, end_unchanged;
 
   /* The first and last positions in the buffer.  Because boundaries
      store their positions relative to the start (BEG) and end (Z) of
@@ -92,7 +92,7 @@ struct region_cache {
 
      Yes, buffer_beg is always 1.  It's there for symmetry with
      buffer_end and the BEG and BUF_BEG macros.  */
-  int buffer_beg, buffer_end;
+  EMACS_INT buffer_beg, buffer_end;
 };
 
 /* Return the position of boundary i in cache c.  */
@@ -123,13 +123,13 @@ struct region_cache {
    preserve that information, instead of throwing it away.  */
 #define PRESERVE_THRESHOLD (500)
 
-static void revalidate_region_cache ();
+static void revalidate_region_cache (struct buffer *buf, struct region_cache *c);
 
 \f
 /* Interface: Allocating, initializing, and disposing of region caches.  */
 
 struct region_cache *
-new_region_cache ()
+new_region_cache (void)
 {
   struct region_cache *c
     = (struct region_cache *) xmalloc (sizeof (struct region_cache));
@@ -157,8 +157,7 @@ new_region_cache ()
 }
 
 void
-free_region_cache (c)
-     struct region_cache *c;
+free_region_cache (struct region_cache *c)
 {
   xfree (c->boundaries);
   xfree (c);
@@ -174,19 +173,17 @@ free_region_cache (c)
    This operation should be logarithmic in the number of cache
    entries.  It would be nice if it took advantage of locality of
    reference, too, by searching entries near the last entry found.  */
-static int
-find_cache_boundary (c, pos)
-     struct region_cache *c;
-     int pos;
+static EMACS_INT
+find_cache_boundary (struct region_cache *c, EMACS_INT pos)
 {
-  int low = 0, high = c->cache_len;
+  EMACS_INT low = 0, high = c->cache_len;
 
   while (low + 1 < high)
     {
       /* mid is always a valid index, because low < high and ">> 1"
          rounds down.  */
-      int mid = (low + high) >> 1;
-      int boundary = BOUNDARY_POS (c, mid);
+      EMACS_INT mid = (low + high) >> 1;
+      EMACS_INT boundary = BOUNDARY_POS (c, mid);
 
       if (pos < boundary)
         high = mid;
@@ -211,16 +208,13 @@ find_cache_boundary (c, pos)
 /* Move the gap of cache C to index POS, and make sure it has space
    for at least MIN_SIZE boundaries.  */
 static void
-move_cache_gap (c, pos, min_size)
-     struct region_cache *c;
-     int pos;
-     int min_size;
+move_cache_gap (struct region_cache *c, EMACS_INT pos, EMACS_INT min_size)
 {
   /* Copy these out of the cache and into registers.  */
-  int gap_start = c->gap_start;
-  int gap_len = c->gap_len;
-  int buffer_beg = c->buffer_beg;
-  int buffer_end = c->buffer_end;
+  EMACS_INT gap_start = c->gap_start;
+  EMACS_INT gap_len = c->gap_len;
+  EMACS_INT buffer_beg = c->buffer_beg;
+  EMACS_INT buffer_end = c->buffer_end;
 
   if (pos < 0
       || pos > c->cache_len)
@@ -252,7 +246,7 @@ move_cache_gap (c, pos, min_size)
      when the portion after the gap is smallest.  */
   if (gap_len < min_size)
     {
-      int i;
+      EMACS_INT i;
 
       /* Always make at least NEW_CACHE_GAP elements, as long as we're
          expanding anyway.  */
@@ -299,10 +293,8 @@ move_cache_gap (c, pos, min_size)
 /* Insert a new boundary in cache C; it will have cache index INDEX,
    and have the specified POS and VALUE.  */
 static void
-insert_cache_boundary (c, index, pos, value)
-     struct region_cache *c;
-     int index;
-     int pos, value;
+insert_cache_boundary (struct region_cache *c, EMACS_INT index, EMACS_INT pos,
+                      int value)
 {
   /* index must be a valid cache index.  */
   if (index < 0 || index > c->cache_len)
@@ -338,11 +330,10 @@ insert_cache_boundary (c, index, pos, value)
 /* Delete the i'th entry from cache C if START <= i < END.  */
 
 static void
-delete_cache_boundaries (c, start, end)
-     struct region_cache *c;
-     int start, end;
+delete_cache_boundaries (struct region_cache *c,
+                        EMACS_INT start, EMACS_INT end)
 {
-  int len = end - start;
+  EMACS_INT len = end - start;
 
   /* Gotta be in range.  */
   if (start < 0
@@ -392,10 +383,8 @@ delete_cache_boundaries (c, start, end)
 
 /* Set the value in cache C for the region START..END to VALUE.  */
 static void
-set_cache_region (c, start, end, value)
-     struct region_cache *c;
-     int start, end;
-     int value;
+set_cache_region (struct region_cache *c,
+                 EMACS_INT start, EMACS_INT end, int value)
 {
   if (start > end)
     abort ();
@@ -418,8 +407,8 @@ set_cache_region (c, start, end, value)
        index of the earliest boundary after the last character in
        start..end.  (This tortured terminology is intended to answer
        all the "< or <=?" sort of questions.)  */
-    int start_ix = find_cache_boundary (c, start);
-    int end_ix   = find_cache_boundary (c, end - 1) + 1;
+    EMACS_INT start_ix = find_cache_boundary (c, start);
+    EMACS_INT end_ix   = find_cache_boundary (c, end - 1) + 1;
 
     /* We must remember the value established by the last boundary
        before end; if that boundary's domain stretches beyond end,
@@ -496,10 +485,8 @@ set_cache_region (c, start, end, value)
    buffer positions in the presence of insertions and deletions; the
    args to pass are the same before and after such an operation.)  */
 void
-invalidate_region_cache (buf, c, head, tail)
-     struct buffer *buf;
-     struct region_cache *c;
-     int head, tail;
+invalidate_region_cache (struct buffer *buf, struct region_cache *c,
+                        EMACS_INT head, EMACS_INT tail)
 {
   /* Let chead = c->beg_unchanged, and
          ctail = c->end_unchanged.
@@ -577,9 +564,7 @@ invalidate_region_cache (buf, c, head, tail)
    the cache, and causes cache gap motion.  */
 
 static void
-revalidate_region_cache (buf, c)
-     struct buffer *buf;
-     struct region_cache *c;
+revalidate_region_cache (struct buffer *buf, struct region_cache *c)
 {
   /* The boundaries now in the cache are expressed relative to the
      buffer_beg and buffer_end values stored in the cache.  Now,
@@ -639,7 +624,7 @@ revalidate_region_cache (buf, c)
      corresponds to the modified region of the buffer.  */
   else
     {
-      int modified_ix;
+      EMACS_INT modified_ix;
 
       /* These positions are correct, relative to both the cache basis
          and the buffer basis.  */
@@ -707,10 +692,8 @@ revalidate_region_cache (buf, c)
    buffer positions) is "known," for the purposes of CACHE (e.g. "has
    no newlines", in the case of the line cache).  */
 void
-know_region_cache (buf, c, start, end)
-     struct buffer *buf;
-     struct region_cache *c;
-     int start, end;
+know_region_cache (struct buffer *buf, struct region_cache *c,
+                  EMACS_INT start, EMACS_INT end)
 {
   revalidate_region_cache (buf, c);
 
@@ -724,18 +707,15 @@ know_region_cache (buf, c, start, end)
    the purposes of CACHE.  If NEXT is non-zero, set *NEXT to the nearest
    position after POS where the knownness changes.  */
 int
-region_cache_forward (buf, c, pos, next)
-     struct buffer *buf;
-     struct region_cache *c;
-     int pos;
-     int *next;
+region_cache_forward (struct buffer *buf, struct region_cache *c,
+                     EMACS_INT pos, EMACS_INT *next)
 {
   revalidate_region_cache (buf, c);
 
   {
-    int i = find_cache_boundary (c, pos);
+    EMACS_INT i = find_cache_boundary (c, pos);
     int i_value = BOUNDARY_VALUE (c, i);
-    int j;
+    EMACS_INT j;
 
     /* Beyond the end of the buffer is unknown, by definition.  */
     if (pos >= BUF_Z (buf))
@@ -763,11 +743,8 @@ region_cache_forward (buf, c, pos, next)
 /* Return true if the text immediately before POS in BUF is known, for
    the purposes of CACHE.  If NEXT is non-zero, set *NEXT to the nearest
    position before POS where the knownness changes.  */
-int region_cache_backward (buf, c, pos, next)
-     struct buffer *buf;
-     struct region_cache *c;
-     int pos;
-     int *next;
+int region_cache_backward (struct buffer *buf, struct region_cache *c,
+                          EMACS_INT pos, EMACS_INT *next)
 {
   revalidate_region_cache (buf, c);
 
@@ -780,9 +757,9 @@ int region_cache_backward (buf, c, pos, next)
     }
 
   {
-    int i = find_cache_boundary (c, pos - 1);
+    EMACS_INT i = find_cache_boundary (c, pos - 1);
     int i_value = BOUNDARY_VALUE (c, i);
-    int j;
+    EMACS_INT j;
 
     if (next)
       {
@@ -805,21 +782,20 @@ int region_cache_backward (buf, c, pos, next)
 /* Debugging: pretty-print a cache to the standard error output.  */
 
 void
-pp_cache (c)
-     struct region_cache *c;
+pp_cache (struct region_cache *c)
 {
   int i;
-  int beg_u = c->buffer_beg + c->beg_unchanged;
-  int end_u = c->buffer_end - c->end_unchanged;
+  EMACS_INT beg_u = c->buffer_beg + c->beg_unchanged;
+  EMACS_INT end_u = c->buffer_end - c->end_unchanged;
 
   fprintf (stderr,
-           "basis: %d..%d    modified: %d..%d\n",
-           c->buffer_beg, c->buffer_end,
-           beg_u, end_u);
+           "basis: %ld..%ld    modified: %ld..%ld\n",
+           (long)c->buffer_beg, (long)c->buffer_end,
+           (long)beg_u, (long)end_u);
 
   for (i = 0; i < c->cache_len; i++)
     {
-      int pos = BOUNDARY_POS (c, i);
+      EMACS_INT pos = BOUNDARY_POS (c, i);
 
       putc (((pos < beg_u) ? 'v'
              : (pos == beg_u) ? '-'
@@ -829,9 +805,7 @@ pp_cache (c)
              : (pos == end_u) ? '-'
              : ' '),
             stderr);
-      fprintf (stderr, "%d : %d\n", pos, BOUNDARY_VALUE (c, i));
+      fprintf (stderr, "%ld : %d\n", (long)pos, BOUNDARY_VALUE (c, i));
     }
 }
 
-/* arch-tag: 98c29f3f-2ca2-4e3a-92f0-f2249200a17d
-   (do not change this comment) */