Mouse-wheel scrolling for DocView Continuous mode. (Bug#4896)
[bpt/emacs.git] / src / ralloc.c
index 9d31320..6faee2c 100644 (file)
@@ -1,12 +1,13 @@
-/* Block-relocating memory allocator. 
-   Copyright (C) 1993, 1995, 2000 Free Software Foundation, Inc.
+/* Block-relocating memory allocator.
+   Copyright (C) 1993, 1995, 2000, 2001, 2002, 2003, 2004,
+                 2005, 2006, 2007, 2008, 2009  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
@@ -14,9 +15,7 @@ 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., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 /* NOTES:
 
@@ -27,7 +26,9 @@ Boston, MA 02111-1307, USA.  */
 #ifdef emacs
 
 #include <config.h>
+#include <setjmp.h>
 #include "lisp.h"              /* Needed for VALBITS.  */
+#include "blockinput.h"
 
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
@@ -42,7 +43,7 @@ typedef size_t SIZE;
 extern void safe_bcopy ();
 
 #ifdef DOUG_LEA_MALLOC
-#define M_TOP_PAD           -2 
+#define M_TOP_PAD           -2
 extern int mallopt ();
 #else /* not DOUG_LEA_MALLOC */
 #ifndef SYSTEM_MALLOC
@@ -98,7 +99,7 @@ static POINTER break_value;
 /* This is the size of a page.  We round memory requests to this boundary.  */
 static int page_size;
 
-/* Whenever we get memory from the system, get this many extra bytes.  This 
+/* Whenever we get memory from the system, get this many extra bytes.  This
    must be a multiple of page_size.  */
 static int extra_bytes;
 
@@ -141,7 +142,7 @@ extern POINTER (*__morecore) ();
    We try to make just one heap and make it larger as necessary.
    But sometimes we can't do that, because we can't get contiguous
    space to add onto the heap.  When that happens, we start a new heap.  */
-   
+
 typedef struct heap
 {
   struct heap *next;
@@ -174,12 +175,12 @@ static heap_ptr first_heap, last_heap;
 /* These structures are allocated in the malloc arena.
    The linked list is kept in order of increasing '.data' members.
    The data blocks abut each other; if b->next is non-nil, then
-   b->data + b->size == b->next->data.  
+   b->data + b->size == b->next->data.
 
    An element with variable==NIL denotes a freed block, which has not yet
-   been collected.  They may only appear while r_alloc_freeze > 0, and will be
-   freed when the arena is thawed.  Currently, these blocs are not reusable,
-   while the arena is frozen.  Very inefficient.  */
+   been collected.  They may only appear while r_alloc_freeze_level > 0,
+   and will be freed when the arena is thawed.  Currently, these blocs are
+   not reusable, while the arena is frozen.  Very inefficient.  */
 
 typedef struct bp
 {
@@ -328,7 +329,7 @@ static void
 relinquish ()
 {
   register heap_ptr h;
-  int excess = 0;
+  long excess = 0;
 
   /* Add the amount of space beyond break_value
      in all heaps which have extend beyond break_value at all.  */
@@ -402,6 +403,11 @@ find_bloc (ptr)
 
   while (p != NIL_BLOC)
     {
+      /* Consistency check. Don't return inconsistent blocs.
+        Don't abort here, as callers might be expecting this,  but
+        callers that always expect a bloc to be returned should abort
+        if one isn't to avoid a memory corruption bug that is
+        difficult to track down.  */
       if (p->variable == ptr && p->data == *ptr)
        return p;
 
@@ -425,8 +431,7 @@ get_bloc (size)
   if (! (new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE))
       || ! (new_bloc->data = obtain (break_value, size)))
     {
-      if (new_bloc)
-       free (new_bloc);
+      free (new_bloc);
 
       return 0;
     }
@@ -467,8 +472,8 @@ get_bloc (size)
 /* Calculate new locations of blocs in the list beginning with BLOC,
    relocating it to start at ADDRESS, in heap HEAP.  If enough space is
    not presently available in our reserve, call obtain for
-   more space. 
-   
+   more space.
+
    Store the new location of each bloc in its new_data field.
    Do not touch the contents of blocs or break_value.  */
 
@@ -481,7 +486,7 @@ relocate_blocs (bloc, heap, address)
   register bloc_ptr b = bloc;
 
   /* No need to ever call this if arena is frozen, bug somewhere!  */
-  if (r_alloc_freeze_level) 
+  if (r_alloc_freeze_level)
     abort();
 
   while (b)
@@ -506,7 +511,7 @@ relocate_blocs (bloc, heap, address)
          /* Add up the size of all the following blocs.  */
          while (tb != NIL_BLOC)
            {
-             if (tb->variable) 
+             if (tb->variable)
                s += tb->size;
 
              tb = tb->next;
@@ -523,7 +528,7 @@ relocate_blocs (bloc, heap, address)
       /* Record the new address of this bloc
         and update where the next bloc can start.  */
       b->new_data = address;
-      if (b->variable) 
+      if (b->variable)
        address = (char *) address + b->size;
       b = b->next;
     }
@@ -639,7 +644,7 @@ resize_bloc (bloc, size)
   SIZE old_size;
 
   /* No need to ever call this if arena is frozen, bug somewhere!  */
-  if (r_alloc_freeze_level) 
+  if (r_alloc_freeze_level)
     abort();
 
   if (bloc == NIL_BLOC || size == bloc->size)
@@ -681,8 +686,8 @@ resize_bloc (bloc, size)
            {
              b->size = 0;
              b->data = b->new_data;
-            } 
-         else 
+            }
+         else
            {
              safe_bcopy (b->data, b->new_data, b->size);
              *b->variable = b->data = b->new_data;
@@ -708,8 +713,8 @@ resize_bloc (bloc, size)
            {
              b->size = 0;
              b->data = b->new_data;
-            } 
-         else 
+            }
+         else
            {
              safe_bcopy (b->data, b->new_data, b->size);
              *b->variable = b->data = b->new_data;
@@ -738,7 +743,7 @@ free_bloc (bloc)
       bloc->variable = (POINTER *) NIL;
       return;
     }
-  
+
   resize_bloc (bloc, 0);
 
   if (bloc == first_bloc && bloc == last_bloc)
@@ -794,7 +799,7 @@ free_bloc (bloc)
    __morecore hook values - in particular, __default_morecore in the
    GNU malloc package.  */
 
-POINTER 
+POINTER
 r_alloc_sbrk (size)
      long size;
 {
@@ -850,7 +855,7 @@ r_alloc_sbrk (size)
       if (first_heap->bloc_start < new_bloc_start)
        {
          /* This is no clean solution - no idea how to do it better.  */
-         if (r_alloc_freeze_level) 
+         if (r_alloc_freeze_level)
            return NIL;
 
          /* There is a bug here: if the above obtain call succeeded, but the
@@ -940,8 +945,8 @@ r_alloc_sbrk (size)
    which will use the data area.
 
    The allocation of 0 bytes is valid.
-   In case r_alloc_freeze is set, a best fit of unused blocs could be done
-   before allocating a new area.  Not yet done.
+   In case r_alloc_freeze_level is set, a best fit of unused blocs could be
+   done before allocating a new area.  Not yet done.
 
    If we can't allocate the necessary memory, set *PTR to zero, and
    return zero.  */
@@ -982,7 +987,7 @@ r_alloc_free (ptr)
 
   dead_bloc = find_bloc (ptr);
   if (dead_bloc == NIL_BLOC)
-    abort ();
+    abort (); /* Double free? PTR not originally used to allocate?  */
 
   free_bloc (dead_bloc);
   *ptr = 0;
@@ -997,7 +1002,7 @@ r_alloc_free (ptr)
    SIZE is less than or equal to the current bloc size, in which case
    do nothing.
 
-   In case r_alloc_freeze is set, a new bloc is allocated, and the
+   In case r_alloc_freeze_level is set, a new bloc is allocated, and the
    memory copied to it.  Not very efficient.  We could traverse the
    bloc_list for a best fit of free blocs first.
 
@@ -1018,7 +1023,7 @@ r_re_alloc (ptr, size)
 
   if (!*ptr)
     return r_alloc (ptr, size);
-  if (!size) 
+  if (!size)
     {
       r_alloc_free (ptr);
       return r_alloc (ptr, 0);
@@ -1026,14 +1031,14 @@ r_re_alloc (ptr, size)
 
   bloc = find_bloc (ptr);
   if (bloc == NIL_BLOC)
-    abort ();
+    abort (); /* Already freed? PTR not originally used to allocate?  */
 
-  if (size < bloc->size) 
+  if (size < bloc->size)
     {
       /* Wouldn't it be useful to actually resize the bloc here?  */
       /* I think so too, but not if it's too expensive...  */
-      if ((bloc->size - MEM_ROUNDUP (size) >= page_size) 
-          && r_alloc_freeze_level == 0) 
+      if ((bloc->size - MEM_ROUNDUP (size) >= page_size)
+          && r_alloc_freeze_level == 0)
        {
          resize_bloc (bloc, MEM_ROUNDUP (size));
          /* Never mind if this fails, just do nothing...  */
@@ -1055,7 +1060,7 @@ r_re_alloc (ptr, size)
           else
            return NIL;
        }
-      else 
+      else
        {
          if (! resize_bloc (bloc, MEM_ROUNDUP (size)))
            return NIL;
@@ -1091,22 +1096,22 @@ void
 r_alloc_thaw ()
 {
 
-  if (! r_alloc_initialized) 
+  if (! r_alloc_initialized)
     r_alloc_init ();
 
   if (--r_alloc_freeze_level < 0)
     abort ();
 
-  /* This frees all unused blocs.  It is not too inefficient, as the resize 
-     and bcopy is done only once.  Afterwards, all unreferenced blocs are 
+  /* This frees all unused blocs.  It is not too inefficient, as the resize
+     and bcopy is done only once.  Afterwards, all unreferenced blocs are
      already shrunk to zero size.  */
-  if (!r_alloc_freeze_level) 
+  if (!r_alloc_freeze_level)
     {
       bloc_ptr *b = &first_bloc;
-      while (*b) 
-       if (!(*b)->variable) 
-         free_bloc (*b); 
-       else 
+      while (*b)
+       if (!(*b)->variable)
+         free_bloc (*b);
+       else
          b = &(*b)->next;
     }
 }
@@ -1224,6 +1229,34 @@ r_alloc_check ()
 
 #endif /* DEBUG */
 
+/* Update the internal record of which variable points to some data to NEW.
+   Used by buffer-swap-text in Emacs to restore consistency after it
+   swaps the buffer text between two buffer objects.  The OLD pointer
+   is checked to ensure that memory corruption does not occur due to
+   misuse.  */
+void
+r_alloc_reset_variable (old, new)
+     POINTER *old, *new;
+{
+  bloc_ptr bloc = first_bloc;
+
+  /* Find the bloc that corresponds to the data pointed to by pointer.
+     find_bloc cannot be used, as it has internal consistency checks
+     which fail when the variable needs reseting.  */
+  while (bloc != NIL_BLOC)
+    {
+      if (bloc->data == *new)
+       break;
+
+      bloc = bloc->next;
+    }
+
+  if (bloc == NIL_BLOC || bloc->variable != old)
+    abort (); /* Already freed? OLD not originally used to allocate?  */
+
+  /* Update variable to point to the new location.  */
+  bloc->variable = new;
+}
 
 \f
 /***********************************************************************
@@ -1238,7 +1271,7 @@ r_alloc_init ()
   if (r_alloc_initialized)
     return;
   r_alloc_initialized = 1;
-  
+
   page_size = PAGE;
 #ifndef SYSTEM_MALLOC
   real_morecore = __morecore;
@@ -1255,7 +1288,9 @@ r_alloc_init ()
 #endif
 
 #ifdef DOUG_LEA_MALLOC
-    mallopt (M_TOP_PAD, 64 * 4096);
+  BLOCK_INPUT;
+  mallopt (M_TOP_PAD, 64 * 4096);
+  UNBLOCK_INPUT;
 #else
 #ifndef SYSTEM_MALLOC
   /* Give GNU malloc's morecore some hysteresis
@@ -1283,6 +1318,9 @@ r_alloc_init ()
         (char *) first_heap->end - (char *) first_heap->start);
   virtual_break_value = break_value = first_heap->bloc_start = first_heap->end;
 #endif
-  
+
   use_relocatable_buffers = 1;
 }
+
+/* arch-tag: 6a524a15-faff-44c8-95d4-a5da6f55110f
+   (do not change this comment) */