(Fmake_string): Doc fix.
[bpt/emacs.git] / src / alloc.c
index 7f05cf7..865675d 100644 (file)
@@ -1,5 +1,5 @@
 /* Storage allocation and gc for GNU Emacs Lisp interpreter.
-   Copyright (C) 1985, 86, 88, 93, 94, 95, 97, 98, 1999, 2000, 2001, 2002, 2003
+   Copyright (C) 1985,86,88,93,94,95,97,98,1999,2000,01,02,03,2004
       Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
@@ -31,13 +31,6 @@ Boston, MA 02111-1307, USA.  */
 
 #include <signal.h>
 
-/* GC_MALLOC_CHECK defined means perform validity checks of malloc'd
-   memory.  Can do this only if using gmalloc.c.  */
-
-#if defined SYSTEM_MALLOC || defined DOUG_LEA_MALLOC
-#undef GC_MALLOC_CHECK
-#endif
-
 /* This file is part of the core Lisp implementation, and thus must
    deal with the real data structures.  If the Lisp implementation is
    replaced, this file likely will not be used.  */
@@ -56,6 +49,13 @@ Boston, MA 02111-1307, USA.  */
 #include "syssignal.h"
 #include <setjmp.h>
 
+/* GC_MALLOC_CHECK defined means perform validity checks of malloc'd
+   memory.  Can do this only if using gmalloc.c.  */
+
+#if defined SYSTEM_MALLOC || defined DOUG_LEA_MALLOC
+#undef GC_MALLOC_CHECK
+#endif
+
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #else
@@ -552,7 +552,7 @@ xrealloc (block, size)
 }
 
 
-/* Like free but block interrupt input..  */
+/* Like free but block interrupt input.  */
 
 void
 xfree (block)
@@ -598,6 +598,7 @@ lisp_malloc (nbytes, type)
 
   val = (void *) malloc (nbytes);
 
+#ifndef USE_LSB_TAG
   /* If the memory just allocated cannot be addressed thru a Lisp
      object's pointer, and it needs to be,
      that's equivalent to running out of memory.  */
@@ -612,6 +613,7 @@ lisp_malloc (nbytes, type)
          val = 0;
        }
     }
+#endif
 
 #if GC_MARK_STACK && !defined GC_MALLOC_CHECK
   if (val && type != MEM_TYPE_NON_LISP)
@@ -711,7 +713,7 @@ struct ablocks
 #define ABLOCKS_BASE(abase) (abase)
 #else
 #define ABLOCKS_BASE(abase) \
-  (1 & (int) ABLOCKS_BUSY (abase) ? abase : ((void**)abase)[-1])
+  (1 & (long) ABLOCKS_BUSY (abase) ? abase : ((void**)abase)[-1])
 #endif
 
 /* The list of free ablock.   */
@@ -738,7 +740,8 @@ lisp_align_malloc (nbytes, type)
 
   if (!free_ablock)
     {
-      int i, aligned;
+      int i;
+      EMACS_INT aligned; /* int gets warning casting to 64-bit pointer.  */
 
 #ifdef DOUG_LEA_MALLOC
       /* Prevent mmap'ing the chunk.  Lisp data may not be mmap'ed
@@ -755,6 +758,11 @@ lisp_align_malloc (nbytes, type)
 #else
       base = malloc (ABLOCKS_BYTES);
       abase = ALIGN (base, BLOCK_ALIGN);
+      if (base == 0)
+       {
+         UNBLOCK_INPUT;
+         memory_full ();
+       }
 #endif
 
       aligned = (base == abase);
@@ -766,6 +774,25 @@ lisp_align_malloc (nbytes, type)
       mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
 #endif
 
+#ifndef USE_LSB_TAG
+      /* If the memory just allocated cannot be addressed thru a Lisp
+        object's pointer, and it needs to be, that's equivalent to
+        running out of memory.  */
+      if (type != MEM_TYPE_NON_LISP)
+       {
+         Lisp_Object tem;
+         char *end = (char *) base + ABLOCKS_BYTES - 1;
+         XSETCONS (tem, end);
+         if ((char *) XCONS (tem) != end)
+           {
+             lisp_malloc_loser = base;
+             free (base);
+             UNBLOCK_INPUT;
+             memory_full ();
+           }
+       }
+#endif
+
       /* Initialize the blocks and put them on the free list.
         Is `base' was not properly aligned, we can't use the last block.  */
       for (i = 0; i < (aligned ? ABLOCKS_SIZE : ABLOCKS_SIZE - 1); i++)
@@ -774,35 +801,20 @@ lisp_align_malloc (nbytes, type)
          abase->blocks[i].x.next_free = free_ablock;
          free_ablock = &abase->blocks[i];
        }
-      ABLOCKS_BUSY (abase) = (struct ablocks *) aligned;
+      ABLOCKS_BUSY (abase) = (struct ablocks *) (long) aligned;
 
       eassert (0 == ((EMACS_UINT)abase) % BLOCK_ALIGN);
       eassert (ABLOCK_ABASE (&abase->blocks[3]) == abase); /* 3 is arbitrary */
       eassert (ABLOCK_ABASE (&abase->blocks[0]) == abase);
       eassert (ABLOCKS_BASE (abase) == base);
-      eassert (aligned == (int)ABLOCKS_BUSY (abase));
+      eassert (aligned == (long) ABLOCKS_BUSY (abase));
     }
 
   abase = ABLOCK_ABASE (free_ablock);
-  ABLOCKS_BUSY (abase) = (struct ablocks *) (2 + (int) ABLOCKS_BUSY (abase));
+  ABLOCKS_BUSY (abase) = (struct ablocks *) (2 + (long) ABLOCKS_BUSY (abase));
   val = free_ablock;
   free_ablock = free_ablock->x.next_free;
 
-  /* If the memory just allocated cannot be addressed thru a Lisp
-     object's pointer, and it needs to be,
-     that's equivalent to running out of memory.  */
-  if (val && type != MEM_TYPE_NON_LISP)
-    {
-      Lisp_Object tem;
-      XSETCONS (tem, (char *) val + nbytes - 1);
-      if ((char *) XCONS (tem) != (char *) val + nbytes - 1)
-       {
-         lisp_malloc_loser = val;
-         free (val);
-         val = 0;
-       }
-    }
-
 #if GC_MARK_STACK && !defined GC_MALLOC_CHECK
   if (val && type != MEM_TYPE_NON_LISP)
     mem_insert (val, (char *) val + nbytes, type);
@@ -831,11 +843,11 @@ lisp_align_free (block)
   ablock->x.next_free = free_ablock;
   free_ablock = ablock;
   /* Update busy count.  */
-  ABLOCKS_BUSY (abase) = (struct ablocks *) (-2 + (int) ABLOCKS_BUSY (abase));
-  
-  if (2 > (int) ABLOCKS_BUSY (abase))
+  ABLOCKS_BUSY (abase) = (struct ablocks *) (-2 + (long) ABLOCKS_BUSY (abase));
+
+  if (2 > (long) ABLOCKS_BUSY (abase))
     { /* All the blocks are free.  */
-      int i = 0, aligned = (int) ABLOCKS_BUSY (abase);
+      int i = 0, aligned = (long) ABLOCKS_BUSY (abase);
       struct ablock **tem = &free_ablock;
       struct ablock *atop = &abase->blocks[aligned ? ABLOCKS_SIZE : ABLOCKS_SIZE - 1];
 
@@ -1096,8 +1108,9 @@ uninterrupt_malloc ()
 
 struct interval_block
 {
-  struct interval_block *next;
+  /* Place `intervals' first, to preserve alignment.  */
   struct interval intervals[INTERVAL_BLOCK_SIZE];
+  struct interval_block *next;
 };
 
 /* Current interval block.  Its `next' pointer points to older
@@ -1128,14 +1141,10 @@ int n_interval_blocks;
 static void
 init_intervals ()
 {
-  interval_block
-    = (struct interval_block *) lisp_malloc (sizeof *interval_block,
-                                            MEM_TYPE_NON_LISP);
-  interval_block->next = 0;
-  bzero ((char *) interval_block->intervals, sizeof interval_block->intervals);
-  interval_block_index = 0;
+  interval_block = NULL;
+  interval_block_index = INTERVAL_BLOCK_SIZE;
   interval_free_list = 0;
-  n_interval_blocks = 1;
+  n_interval_blocks = 0;
 }
 
 
@@ -1339,8 +1348,9 @@ struct sblock
 
 struct string_block
 {
-  struct string_block *next;
+  /* Place `strings' first, to preserve alignment.  */
   struct Lisp_String strings[STRING_BLOCK_SIZE];
+  struct string_block *next;
 };
 
 /* Head and tail of the list of sblock structures holding Lisp string
@@ -1883,8 +1893,9 @@ compact_small_strings ()
 
 
 DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0,
-       doc: /* Return a newly created string of length LENGTH, with each element being INIT.
-Both LENGTH and INIT must be numbers.  */)
+       doc: /* Return a newly created string of length LENGTH, with INIT in each element.
+LENGTH must be an integer.
+INIT must be an integer that represents a character.  */)
      (length, init)
      Lisp_Object length, init;
 {
@@ -1939,10 +1950,11 @@ LENGTH must be a number.  INIT matters only in whether it is t or nil.  */)
 
   CHECK_NATNUM (length);
 
-  bits_per_value = sizeof (EMACS_INT) * BITS_PER_CHAR;
+  bits_per_value = sizeof (EMACS_INT) * BOOL_VECTOR_BITS_PER_CHAR;
 
   length_in_elts = (XFASTINT (length) + bits_per_value - 1) / bits_per_value;
-  length_in_chars = ((XFASTINT (length) + BITS_PER_CHAR - 1) / BITS_PER_CHAR);
+  length_in_chars = ((XFASTINT (length) + BOOL_VECTOR_BITS_PER_CHAR - 1)
+                    / BOOL_VECTOR_BITS_PER_CHAR);
 
   /* We must allocate one more elements than LENGTH_IN_ELTS for the
      slot `size' of the struct Lisp_Bool_Vector.  */
@@ -1959,9 +1971,9 @@ LENGTH must be a number.  INIT matters only in whether it is t or nil.  */)
     p->data[i] = real_init;
 
   /* Clear the extraneous bits in the last byte.  */
-  if (XINT (length) != length_in_chars * BITS_PER_CHAR)
+  if (XINT (length) != length_in_chars * BOOL_VECTOR_BITS_PER_CHAR)
     XBOOL_VECTOR (val)->data[length_in_chars - 1]
-      &= (1 << (XINT (length) % BITS_PER_CHAR)) - 1;
+      &= (1 << (XINT (length) % BOOL_VECTOR_BITS_PER_CHAR)) - 1;
 
   return val;
 }
@@ -2121,8 +2133,10 @@ make_uninit_multibyte_string (nchars, nbytes)
    by GC are put on a free list to be reallocated before allocating
    any new float cells from the latest float_block.  */
 
-#define FLOAT_BLOCK_SIZE \
-  (((BLOCK_BYTES - sizeof (struct float_block *)) * CHAR_BIT) \
+#define FLOAT_BLOCK_SIZE                                       \
+  (((BLOCK_BYTES - sizeof (struct float_block *)               \
+     /* The compiler might add padding at the end.  */         \
+     - (sizeof (struct Lisp_Float) - sizeof (int))) * CHAR_BIT) \
    / (sizeof (struct Lisp_Float) * CHAR_BIT + 1))
 
 #define GETMARKBIT(block,n)                            \
@@ -2225,15 +2239,17 @@ make_float (float_value)
          new = (struct float_block *) lisp_align_malloc (sizeof *new,
                                                          MEM_TYPE_FLOAT);
          new->next = float_block;
+         bzero ((char *) new->gcmarkbits, sizeof new->gcmarkbits);
          float_block = new;
          float_block_index = 0;
          n_float_blocks++;
        }
-      XSETFLOAT (val, &float_block->floats[float_block_index++]);
+      XSETFLOAT (val, &float_block->floats[float_block_index]);
+      float_block_index++;
     }
 
   XFLOAT_DATA (val) = float_value;
-  FLOAT_UNMARK (XFLOAT (val));
+  eassert (!FLOAT_MARKED_P (XFLOAT (val)));
   consing_since_gc += sizeof (struct Lisp_Float);
   floats_consed++;
   return val;
@@ -2319,7 +2335,6 @@ free_cons (ptr)
   cons_free_list = ptr;
 }
 
-
 DEFUN ("cons", Fcons, Scons, 2, 2, 0,
        doc: /* Create a new cons, give it CAR and CDR as components, and return it.  */)
      (car, cdr)
@@ -2341,17 +2356,19 @@ DEFUN ("cons", Fcons, Scons, 2, 2, 0,
          register struct cons_block *new;
          new = (struct cons_block *) lisp_align_malloc (sizeof *new,
                                                         MEM_TYPE_CONS);
+         bzero ((char *) new->gcmarkbits, sizeof new->gcmarkbits);
          new->next = cons_block;
          cons_block = new;
          cons_block_index = 0;
          n_cons_blocks++;
        }
-      XSETCONS (val, &cons_block->conses[cons_block_index++]);
+      XSETCONS (val, &cons_block->conses[cons_block_index]);
+      cons_block_index++;
     }
 
   XSETCAR (val, car);
   XSETCDR (val, cdr);
-  CONS_UNMARK (XCONS (val));
+  eassert (!CONS_MARKED_P (XCONS (val)));
   consing_since_gc += sizeof (struct Lisp_Cons);
   cons_cells_consed++;
   return val;
@@ -2490,7 +2507,9 @@ allocate_vectorlike (len, type)
   /* Prevent mmap'ing the chunk.  Lisp data may not be mmap'ed
      because mapped region contents are not preserved in
      a dumped Emacs.  */
+  BLOCK_INPUT;
   mallopt (M_MMAP_MAX, 0);
+  UNBLOCK_INPUT;
 #endif
 
   nbytes = sizeof *p + (len - 1) * sizeof p->contents[0];
@@ -2498,7 +2517,9 @@ allocate_vectorlike (len, type)
 
 #ifdef DOUG_LEA_MALLOC
   /* Back to a reasonable maximum of mmap'ed areas.  */
+  BLOCK_INPUT;
   mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
+  UNBLOCK_INPUT;
 #endif
 
   consing_since_gc += nbytes;
@@ -2741,8 +2762,9 @@ usage: (make-byte-code ARGLIST BYTE-CODE CONSTANTS DEPTH &optional DOCSTRING INT
 
 struct symbol_block
 {
-  struct symbol_block *next;
+  /* Place `symbols' first, to preserve alignment.  */
   struct Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE];
+  struct symbol_block *next;
 };
 
 /* Current symbol block and index of first unused Lisp_Symbol
@@ -2765,13 +2787,10 @@ int n_symbol_blocks;
 void
 init_symbol ()
 {
-  symbol_block = (struct symbol_block *) lisp_malloc (sizeof *symbol_block,
-                                                     MEM_TYPE_SYMBOL);
-  symbol_block->next = 0;
-  bzero ((char *) symbol_block->symbols, sizeof symbol_block->symbols);
-  symbol_block_index = 0;
+  symbol_block = NULL;
+  symbol_block_index = SYMBOL_BLOCK_SIZE;
   symbol_free_list = 0;
-  n_symbol_blocks = 1;
+  n_symbol_blocks = 0;
 }
 
 
@@ -2803,7 +2822,8 @@ Its value and function definition are void, and its property list is nil.  */)
          symbol_block_index = 0;
          n_symbol_blocks++;
        }
-      XSETSYMBOL (val, &symbol_block->symbols[symbol_block_index++]);
+      XSETSYMBOL (val, &symbol_block->symbols[symbol_block_index]);
+      symbol_block_index++;
     }
 
   p = XSYMBOL (val);
@@ -2835,8 +2855,9 @@ Its value and function definition are void, and its property list is nil.  */)
 
 struct marker_block
 {
-  struct marker_block *next;
+  /* Place `markers' first, to preserve alignment.  */
   union Lisp_Misc markers[MARKER_BLOCK_SIZE];
+  struct marker_block *next;
 };
 
 struct marker_block *marker_block;
@@ -2851,13 +2872,10 @@ int n_marker_blocks;
 void
 init_marker ()
 {
-  marker_block = (struct marker_block *) lisp_malloc (sizeof *marker_block,
-                                                     MEM_TYPE_MISC);
-  marker_block->next = 0;
-  bzero ((char *) marker_block->markers, sizeof marker_block->markers);
-  marker_block_index = 0;
+  marker_block = NULL;
+  marker_block_index = MARKER_BLOCK_SIZE;
   marker_free_list = 0;
-  n_marker_blocks = 1;
+  n_marker_blocks = 0;
 }
 
 /* Return a newly allocated Lisp_Misc object, with no substructure.  */
@@ -2884,7 +2902,8 @@ allocate_misc ()
          marker_block_index = 0;
          n_marker_blocks++;
        }
-      XSETMISC (val, &marker_block->markers[marker_block_index++]);
+      XSETMISC (val, &marker_block->markers[marker_block_index]);
+      marker_block_index++;
     }
 
   consing_since_gc += sizeof (union Lisp_Misc);
@@ -3419,6 +3438,7 @@ live_string_p (m, p)
         must not be on the free-list.  */
       return (offset >= 0
              && offset % sizeof b->strings[0] == 0
+             && offset < (STRING_BLOCK_SIZE * sizeof b->strings[0])
              && ((struct Lisp_String *) p)->data != NULL);
     }
   else
@@ -3443,8 +3463,8 @@ live_cons_p (m, p)
         one of the unused cells in the current cons block,
         and not be on the free-list.  */
       return (offset >= 0
-             && offset < (CONS_BLOCK_SIZE * sizeof b->conses[0])
              && offset % sizeof b->conses[0] == 0
+             && offset < (CONS_BLOCK_SIZE * sizeof b->conses[0])
              && (b != cons_block
                  || offset / sizeof b->conses[0] < cons_block_index)
              && !EQ (((struct Lisp_Cons *) p)->car, Vdead));
@@ -3472,6 +3492,7 @@ live_symbol_p (m, p)
         and not be on the free-list.  */
       return (offset >= 0
              && offset % sizeof b->symbols[0] == 0
+             && offset < (SYMBOL_BLOCK_SIZE * sizeof b->symbols[0])
              && (b != symbol_block
                  || offset / sizeof b->symbols[0] < symbol_block_index)
              && !EQ (((struct Lisp_Symbol *) p)->function, Vdead));
@@ -3497,8 +3518,8 @@ live_float_p (m, p)
       /* P must point to the start of a Lisp_Float and not be
         one of the unused cells in the current float block.  */
       return (offset >= 0
-             && offset < (FLOAT_BLOCK_SIZE * sizeof b->floats[0])
              && offset % sizeof b->floats[0] == 0
+             && offset < (FLOAT_BLOCK_SIZE * sizeof b->floats[0])
              && (b != float_block
                  || offset / sizeof b->floats[0] < float_block_index));
     }
@@ -3525,6 +3546,7 @@ live_misc_p (m, p)
         and not be on the free-list.  */
       return (offset >= 0
              && offset % sizeof b->markers[0] == 0
+             && offset < (MARKER_BLOCK_SIZE * sizeof b->markers[0])
              && (b != marker_block
                  || offset / sizeof b->markers[0] < marker_block_index)
              && ((union Lisp_Misc *) p)->u_marker.type != Lisp_Misc_Free);
@@ -4059,6 +4081,9 @@ pure_alloc (size, type)
      int type;
 {
   POINTER_TYPE *result;
+#ifdef USE_LSB_TAG
+  size_t alignment = (1 << GCTYPEBITS);
+#else
   size_t alignment = sizeof (EMACS_INT);
 
   /* Give Lisp_Floats an extra alignment.  */
@@ -4070,6 +4095,7 @@ pure_alloc (size, type)
       alignment = sizeof (struct Lisp_Float);
 #endif
     }
+#endif
 
  again:
   result = ALIGN (purebeg + pure_bytes_used, alignment);
@@ -4205,12 +4231,13 @@ Does not copy symbols.  Copies strings without text properties.  */)
   else if (COMPILEDP (obj) || VECTORP (obj))
     {
       register struct Lisp_Vector *vec;
-      register int i, size;
+      register int i;
+      EMACS_INT size;
 
       size = XVECTOR (obj)->size;
       if (size & PSEUDOVECTOR_FLAG)
        size &= PSEUDOVECTOR_SIZE_MASK;
-      vec = XVECTOR (make_pure_vector ((EMACS_INT) size));
+      vec = XVECTOR (make_pure_vector (size));
       for (i = 0; i < size; i++)
        vec->contents[i] = Fpurecopy (XVECTOR (obj)->contents[i]);
       if (COMPILEDP (obj))
@@ -4259,6 +4286,8 @@ struct backtrace
   /* If nargs is UNEVALLED, args points to slot holding list of
      unevalled args.  */
   char evalargs;
+  /* Nonzero means call value of debugger when done with this operation. */
+  char debug_on_exit;
 };
 
 
@@ -4392,12 +4421,7 @@ returns nil, because real GC can't be done.  */)
 
   /* clear_marks (); */
 
-  /* Mark all the special slots that serve as the roots of accessibility.
-
-     Usually the special slots to mark are contained in particular structures.
-     Then we know no slot is marked twice because the structures don't overlap.
-     In some cases, the structures point to the slots to be marked.
-     For these, we use MARKBIT to avoid double marking of the slot.  */
+  /* Mark all the special slots that serve as the roots of accessibility.  */
 
   for (i = 0; i < staticidx; i++)
     mark_object (*staticvec[i]);
@@ -4410,11 +4434,7 @@ returns nil, because real GC can't be done.  */)
     register struct gcpro *tail;
     for (tail = gcprolist; tail; tail = tail->next)
       for (i = 0; i < tail->nvars; i++)
-       if (!XMARKBIT (tail->var[i]))
-         {
-           mark_object (tail->var[i]);
-           XMARK (tail->var[i]);
-         }
+       mark_object (tail->var[i]);
   }
 #endif
 
@@ -4436,52 +4456,64 @@ returns nil, because real GC can't be done.  */)
     }
   for (backlist = backtrace_list; backlist; backlist = backlist->next)
     {
-      if (!XMARKBIT (*backlist->function))
-       {
-         mark_object (*backlist->function);
-         XMARK (*backlist->function);
-       }
+      mark_object (*backlist->function);
+
       if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
        i = 0;
       else
        i = backlist->nargs - 1;
       for (; i >= 0; i--)
-       if (!XMARKBIT (backlist->args[i]))
-         {
-           mark_object (backlist->args[i]);
-           XMARK (backlist->args[i]);
-         }
+       mark_object (backlist->args[i]);
     }
   mark_kboards ();
 
-  /* Look thru every buffer's undo list
-     for elements that update markers that were not marked,
-     and delete them.  */
+#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
+  mark_stack ();
+#endif
+
+#ifdef USE_GTK
+  {
+    extern void xg_mark_data ();
+    xg_mark_data ();
+  }
+#endif
+
+  gc_sweep ();
+
+  /* Look thru every buffer's undo list for elements that used to
+     contain update markers that were changed to Lisp_Misc_Free
+     objects and delete them.  This may leave a few cons cells
+     unchained, but we will get those on the next sweep.  */
   {
     register struct buffer *nextb = all_buffers;
 
     while (nextb)
       {
        /* If a buffer's undo list is Qt, that means that undo is
-          turned off in that buffer.  Calling truncate_undo_list on
-          Qt tends to return NULL, which effectively turns undo back on.
-          So don't call truncate_undo_list if undo_list is Qt.  */
+          turned off in that buffer.  */
        if (! EQ (nextb->undo_list, Qt))
          {
-           Lisp_Object tail, prev;
+           Lisp_Object tail, prev, elt, car;
            tail = nextb->undo_list;
            prev = Qnil;
            while (CONSP (tail))
              {
-               if (GC_CONSP (XCAR (tail))
-                   && GC_MARKERP (XCAR (XCAR (tail)))
-                   && !XMARKER (XCAR (XCAR (tail)))->gcmarkbit)
+               if ((elt = XCAR (tail), GC_CONSP (elt))
+                   && (car = XCAR (elt), GC_MISCP (car))
+                   && XMISCTYPE (car) == Lisp_Misc_Free)
                  {
+                   Lisp_Object cdr = XCDR (tail);
+                   /* Do not use free_cons here, as we don't know if
+                      anybody else has a pointer to these conses.  */
+                   XSETCAR (elt, Qnil);
+                   XSETCDR (elt, Qnil);
+                   XSETCAR (tail, Qnil);
+                   XSETCDR (tail, Qnil);
                    if (NILP (prev))
-                     nextb->undo_list = tail = XCDR (tail);
+                     nextb->undo_list = tail = cdr;
                    else
                      {
-                       tail = XCDR (tail);
+                       tail = cdr;
                        XSETCDR (prev, tail);
                      }
                  }
@@ -4497,43 +4529,9 @@ returns nil, because real GC can't be done.  */)
       }
   }
 
-#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
-  mark_stack ();
-#endif
-
-#ifdef USE_GTK
-  {
-    extern void xg_mark_data ();
-    xg_mark_data ();
-  }
-#endif
-
-  gc_sweep ();
-
   /* Clear the mark bits that we set in certain root slots.  */
 
-#if (GC_MARK_STACK == GC_USE_GCPROS_AS_BEFORE \
-     || GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES)
-  {
-    register struct gcpro *tail;
-
-    for (tail = gcprolist; tail; tail = tail->next)
-      for (i = 0; i < tail->nvars; i++)
-       XUNMARK (tail->var[i]);
-  }
-#endif
-
   unmark_byte_stack ();
-  for (backlist = backtrace_list; backlist; backlist = backlist->next)
-    {
-      XUNMARK (*backlist->function);
-      if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
-       i = 0;
-      else
-       i = backlist->nargs - 1;
-      for (; i >= 0; i--)
-       XUNMARK (backlist->args[i]);
-    }
   VECTOR_UNMARK (&buffer_defaults);
   VECTOR_UNMARK (&buffer_local_symbols);
 
@@ -4718,7 +4716,6 @@ mark_object (arg)
   int cdr_count = 0;
 
  loop:
-  XUNMARK (obj);
 
   if (PURE_POINTER_P (XPNTR (obj)))
     return;
@@ -5212,16 +5209,6 @@ survives_gc_p (obj)
 static void
 gc_sweep ()
 {
-  /* Remove or mark entries in weak hash tables.
-     This must be done before any object is unmarked.  */
-  sweep_weak_hash_tables ();
-
-  sweep_strings ();
-#ifdef GC_CHECK_STRING_BYTES
-  if (!noninteractive)
-    check_string_bytes (1);
-#endif
-
   /* Put all unmarked conses on free list */
   {
     register struct cons_block *cblk;
@@ -5272,6 +5259,16 @@ gc_sweep ()
     total_free_conses = num_free;
   }
 
+  /* Remove or mark entries in weak hash tables.
+     This must be done before any object is unmarked.  */
+  sweep_weak_hash_tables ();
+
+  sweep_strings ();
+#ifdef GC_CHECK_STRING_BYTES
+  if (!noninteractive)
+    check_string_bytes (1);
+#endif
+
   /* Put all unmarked floats on free list */
   {
     register struct float_block *fblk;
@@ -5470,6 +5467,9 @@ gc_sweep ()
        /* If this block contains only free markers and we have already
           seen more than two blocks worth of free markers then deallocate
           this block.  */
+#if 0
+       /* There may still be pointers to these markers from a buffer's
+          undo list, so don't free them.  KFS 2004-05-21  /
        if (this_free == MARKER_BLOCK_SIZE && num_free > MARKER_BLOCK_SIZE)
          {
            *mprev = mblk->next;
@@ -5479,6 +5479,7 @@ gc_sweep ()
            n_marker_blocks--;
          }
        else
+#endif
          {
            num_free += this_free;
            mprev = &mblk->next;
@@ -5786,3 +5787,6 @@ The time is in seconds as a floating point value.  */);
   defsubr (&Sgc_status);
 #endif
 }
+
+/* arch-tag: 6695ca10-e3c5-4c2c-8bc3-ed26a7dda857
+   (do not change this comment) */