XEmacs 21.2.27 "Hera".
[chise/xemacs-chise.git.1] / src / alloc.c
index 8765c62..ca864f2 100644 (file)
@@ -36,6 +36,7 @@ Boston, MA 02111-1307, USA.  */
        Added lcrecord lists for 19.14.
    slb: Lots of work on the purification and dump time code.
         Synched Doug Lea malloc support from Emacs 20.2.
+   og:  Killed the purespace.  Portable dumper.
 */
 
 #include <config.h>
@@ -51,18 +52,42 @@ Boston, MA 02111-1307, USA.  */
 #include "extents.h"
 #include "frame.h"
 #include "glyphs.h"
+#include "opaque.h"
 #include "redisplay.h"
 #include "specifier.h"
 #include "sysfile.h"
 #include "window.h"
+#include "console-stream.h"
 
 #ifdef DOUG_LEA_MALLOC
 #include <malloc.h>
 #endif
 
+#ifdef HAVE_MMAP
+#include <unistd.h>
+#include <sys/mman.h>
+#endif
+
+#ifdef PDUMP
+typedef struct
+{
+  const struct lrecord_description *desc;
+  int count;
+} pdump_reloc_table;
+
+static char *pdump_rt_list = 0;
+#endif
+
 EXFUN (Fgarbage_collect, 0);
 
-/* #define GDB_SUCKS */
+/* Return the true size of a struct with a variable-length array field.  */
+#define STRETCHY_STRUCT_SIZEOF(stretchy_struct_type,           \
+                              stretchy_array_field,            \
+                              stretchy_array_length)           \
+  (offsetof (stretchy_struct_type, stretchy_array_field) +     \
+   (offsetof (stretchy_struct_type, stretchy_array_field[1]) - \
+    offsetof (stretchy_struct_type, stretchy_array_field[0])) *        \
+   (stretchy_array_length))
 
 #if 0 /* this is _way_ too slow to be part of the standard debug options */
 #if defined(DEBUG_XEMACS) && defined(MULE)
@@ -70,14 +95,6 @@ EXFUN (Fgarbage_collect, 0);
 #endif
 #endif
 
-/* Define this to see where all that space is going... */
-/* But the length of the printout is obnoxious, so limit it to testers */
-/* If somebody wants to see this they can ask for it.
-#ifdef DEBUG_XEMACS
-#define PURESTAT
-#endif
-*/
-
 /* Define this to use malloc/free with no freelist for all datatypes,
    the hope being that some debugging tools may help detect
    freed memory references */
@@ -86,27 +103,14 @@ EXFUN (Fgarbage_collect, 0);
 #define ALLOC_NO_POOLS
 #endif
 
-#include "puresize.h"
-
 #ifdef DEBUG_XEMACS
-int debug_allocation;
-
-int debug_allocation_backtrace_length;
+static int debug_allocation;
+static int debug_allocation_backtrace_length;
 #endif
 
 /* Number of bytes of consing done since the last gc */
 EMACS_INT consing_since_gc;
-#ifdef EMACS_BTL
-extern void cadillac_record_backtrace ();
-#define INCREMENT_CONS_COUNTER_1(size)         \
-  do {                                         \
-    EMACS_INT __sz__ = ((EMACS_INT) (size));   \
-    consing_since_gc += __sz__;                        \
-    cadillac_record_backtrace (2, __sz__);     \
-  } while (0)
-#else
 #define INCREMENT_CONS_COUNTER_1(size) (consing_since_gc += (size))
-#endif /* EMACS_BTL */
 
 #define debug_allocation_backtrace()                           \
 do {                                                           \
@@ -139,14 +143,11 @@ do {                                                              \
   INCREMENT_CONS_COUNTER_1 (size)
 #endif
 
-#define DECREMENT_CONS_COUNTER(size)           \
-  do {                                         \
-    EMACS_INT __sz__ = ((EMACS_INT) (size));   \
-    if (consing_since_gc >= __sz__)            \
-      consing_since_gc -= __sz__;              \
-    else                                       \
-      consing_since_gc = 0;                    \
-  } while (0)
+#define DECREMENT_CONS_COUNTER(size) do {      \
+  consing_since_gc -= (size);                  \
+  if (consing_since_gc < 0)                    \
+    consing_since_gc = 0;                      \
+} while (0)
 
 /* Number of bytes of consing since gc before another gc should be done. */
 EMACS_INT gc_cons_threshold;
@@ -186,34 +187,9 @@ extern
 #endif /* VIRT_ADDR_VARIES */
  EMACS_INT malloc_sbrk_unused;
 
-/* Non-zero means defun should do purecopy on the function definition */
+/* Non-zero means we're in the process of doing the dump */
 int purify_flag;
 
-#ifdef HEAP_IN_DATA
-extern void sheap_adjust_h();
-#endif
-
-#define PUREBEG ((char *) pure)
-
-#if 0 /* This is breathing_space in XEmacs */
-/* Points to memory space allocated as "spare",
-   to be freed if we run out of memory.  */
-static char *spare_memory;
-
-/* Amount of spare memory to keep in reserve.  */
-#define SPARE_MEMORY (1 << 14)
-#endif
-
-/* Index in pure at which next pure object will be allocated. */
-static size_t pure_bytes_used;
-
-#define PURIFIED(ptr)                          \
-((char *) (ptr) >= PUREBEG &&                  \
- (char *) (ptr) <  PUREBEG + get_PURESIZE())
-
-/* Non-zero if pure_bytes_used > get_PURESIZE(); accounts for excess purespace needs. */
-static size_t pure_lossage;
-
 #ifdef ERROR_CHECK_TYPECHECK
 
 Error_behavior ERROR_ME, ERROR_ME_NOT, ERROR_ME_WARN;
@@ -221,108 +197,22 @@ Error_behavior ERROR_ME, ERROR_ME_NOT, ERROR_ME_WARN;
 #endif
 
 int
-purified (Lisp_Object obj)
-{
-  return POINTER_TYPE_P (XGCTYPE (obj)) && PURIFIED (XPNTR (obj));
-}
-
-size_t
-purespace_usage (void)
-{
-  return pure_bytes_used;
-}
-
-static int
-check_purespace (size_t size)
-{
-  if (pure_lossage)
-    {
-      pure_lossage += size;
-      return 0;
-    }
-  else if (pure_bytes_used + size > get_PURESIZE())
-    {
-      /* This can cause recursive bad behavior, we'll yell at the end */
-      /* when we're done. */
-      /* message ("\nERROR:  Pure Lisp storage exhausted!\n"); */
-      pure_lossage = size;
-      return 0;
-    }
-  else
-    return 1;
-}
-
-
-\f
-#ifndef PURESTAT
-
-#define bump_purestat(p,b) DO_NOTHING
-
-#else /* PURESTAT */
-
-static int purecopying_for_bytecode;
-
-static size_t pure_sizeof (Lisp_Object /*, int recurse */);
-
-/* Keep statistics on how much of what is in purespace */
-static struct purestat
+c_readonly (Lisp_Object obj)
 {
-  int nobjects;
-  int nbytes;
-  CONST char *name;
+  return POINTER_TYPE_P (XTYPE (obj)) && C_READONLY (obj);
 }
-  purestat_cons = {0, 0, "cons cells"},
-  purestat_float = {0, 0, "float objects"},
-  purestat_string_pname = {0, 0, "symbol-name strings"},
-  purestat_bytecode = {0, 0, "compiled-function objects"},
-  purestat_string_bytecodes = {0, 0, "byte-code strings"},
-  purestat_vector_bytecode_constants = {0, 0, "byte-constant vectors"},
-  purestat_string_interactive = {0, 0, "interactive strings"},
-#ifdef I18N3
-  purestat_string_domain = {0, 0, "domain strings"},
-#endif
-  purestat_string_documentation = {0, 0, "documentation strings"},
-  purestat_string_other_function = {0, 0, "other function strings"},
-  purestat_vector_other = {0, 0, "other vectors"},
-  purestat_string_other = {0, 0, "other strings"},
-  purestat_string_all = {0, 0, "all strings"},
-  purestat_vector_all = {0, 0, "all vectors"};
-
-static struct purestat *purestats[] =
-{
-  &purestat_cons,
-  &purestat_float,
-  &purestat_string_pname,
-  &purestat_bytecode,
-  &purestat_string_bytecodes,
-  &purestat_vector_bytecode_constants,
-  &purestat_string_interactive,
-#ifdef I18N3
-  &purestat_string_domain,
-#endif
-  &purestat_string_documentation,
-  &purestat_string_other_function,
-  &purestat_vector_other,
-  &purestat_string_other,
-  0,
-  &purestat_string_all,
-  &purestat_vector_all
-};
 
-static void
-bump_purestat (struct purestat *purestat, size_t nbytes)
+int
+lisp_readonly (Lisp_Object obj)
 {
-  if (pure_lossage) return;
-  purestat->nobjects += 1;
-  purestat->nbytes += nbytes;
+  return POINTER_TYPE_P (XTYPE (obj)) && LISP_READONLY (obj);
 }
-#endif /* PURESTAT */
 
 \f
 /* Maximum amount of C stack to save when a GC happens.  */
 
 #ifndef MAX_SAVE_STACK
-#define MAX_SAVE_STACK 16000
+#define MAX_SAVE_STACK 0 /* 16000 */
 #endif
 
 /* Non-zero means ignore malloc warnings.  Set during initialization.  */
@@ -387,18 +277,29 @@ memory_full (void)
 void *
 xmalloc (size_t size)
 {
-  void *val = (void *) malloc (size);
+  void *val = malloc (size);
 
   if (!val && (size != 0)) memory_full ();
   return val;
 }
 
+#ifdef xcalloc
+#undef xcalloc
+#endif
+
+static void *
+xcalloc (size_t nelem, size_t elsize)
+{
+  void *val = calloc (nelem, elsize);
+
+  if (!val && (nelem != 0)) memory_full ();
+  return val;
+}
+
 void *
 xmalloc_and_zero (size_t size)
 {
-  void *val = xmalloc (size);
-  memset (val, 0, size);
-  return val;
+  return xcalloc (size, sizeof (char));
 }
 
 #ifdef xrealloc
@@ -410,7 +311,7 @@ xrealloc (void *block, size_t size)
 {
   /* We must call malloc explicitly when BLOCK is 0, since some
      reallocs don't do this.  */
-  void *val = (void *) (block ? realloc (block, size) : malloc (size));
+  void *val = block ? realloc (block, size) : malloc (size);
 
   if (!val && (size != 0)) memory_full ();
   return val;
@@ -474,8 +375,7 @@ xstrdup (CONST char *str)
 
   void *val = xmalloc (len);
   if (val == 0) return 0;
-  memcpy (val, str, len);
-  return (char *) val;
+  return (char *) memcpy (val, str, len);
 }
 
 #ifdef NEED_STRDUP
@@ -490,19 +390,7 @@ strdup (CONST char *s)
 static void *
 allocate_lisp_storage (size_t size)
 {
-  void *p = xmalloc (size);
-#ifndef USE_MINIMAL_TAGBITS
-  char *lim = ((char *) p) + size;
-  Lisp_Object val;
-
-  XSETOBJ (val, Lisp_Type_Record, lim);
-  if ((char *) XPNTR (val) != lim)
-    {
-      xfree (p);
-      memory_full ();
-    }
-#endif /* ! USE_MINIMAL_TAGBITS */
-  return p;
+  return xmalloc (size);
 }
 
 
@@ -517,17 +405,15 @@ alloc_lcrecord (size_t size, CONST struct lrecord_implementation *implementation
 {
   struct lcrecord_header *lcheader;
 
-  if (size <= 0) abort ();
+#ifdef ERROR_CHECK_GC
   if (implementation->static_size == 0)
-    {
-      if (!implementation->size_in_bytes_method)
-       abort ();
-    }
-  else if (implementation->static_size != size)
-    abort ();
+    assert (implementation->size_in_bytes_method);
+  else
+    assert (implementation->static_size == size);
+#endif
 
   lcheader = (struct lcrecord_header *) allocate_lisp_storage (size);
-  set_lheader_implementation(&(lcheader->lheader), implementation);
+  set_lheader_implementation (&(lcheader->lheader), implementation);
   lcheader->next = all_lcrecords;
 #if 1                           /* mly prefers to see small ID numbers */
   lcheader->uid = lrecord_uid_counter++;
@@ -572,7 +458,7 @@ free_lcrecord (struct lcrecord_header *lcrecord)
        }
     }
   if (lrecord->implementation->finalizer)
-    ((lrecord->implementation->finalizer) (lrecord, 0));
+    lrecord->implementation->finalizer (lrecord, 0);
   xfree (lrecord);
   return;
 }
@@ -593,124 +479,78 @@ disksave_object_finalization_1 (void)
     }
 }
 
-
-/* This must not be called -- it just serves as for EQ test
- *  If lheader->implementation->finalizer is this_marks_a_marked_record,
- *  then lrecord has been marked by the GC sweeper
- * header->implementation is put back to its correct value by
- *  sweep_records */
-void
-this_marks_a_marked_record (void *dummy0, int dummy1)
-{
-  abort ();
-}
-
 /* Semi-kludge -- lrecord_symbol_value_forward objects get stuck
    in CONST space and you get SEGV's if you attempt to mark them.
    This sits in lheader->implementation->marker. */
 
 Lisp_Object
-this_one_is_unmarkable (Lisp_Object obj, void (*markobj) (Lisp_Object))
+this_one_is_unmarkable (Lisp_Object obj)
 {
   abort ();
   return Qnil;
 }
 
-/* XGCTYPE for records */
-int
-gc_record_type_p (Lisp_Object frob, CONST struct lrecord_implementation *type)
-{
-  CONST struct lrecord_implementation *imp;
+\f
+/************************************************************************/
+/*                       Debugger support                              */
+/************************************************************************/
+/* Give gdb/dbx enough information to decode Lisp Objects.  We make
+   sure certain symbols are always defined, so gdb doesn't complain
+   about expressions in src/.gdbinit.  See src/.gdbinit or src/.dbxrc
+   to see how this is used.  */
 
-  if (XGCTYPE (frob) != Lisp_Type_Record)
-    return 0;
+EMACS_UINT dbg_valmask = ((1UL << VALBITS) - 1) << GCBITS;
+EMACS_UINT dbg_typemask = (1UL << GCTYPEBITS) - 1;
 
-  imp = XRECORD_LHEADER_IMPLEMENTATION (frob);
-#ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
-  return imp == type;
+#ifdef USE_UNION_TYPE
+unsigned char dbg_USE_UNION_TYPE = 1;
 #else
-  return imp == type || imp == type + 1;
+unsigned char dbg_USE_UNION_TYPE = 0;
 #endif
-}
 
-\f
-/**********************************************************************/
-/*                       Debugger support                            */
-/**********************************************************************/
-/* Give gdb/dbx enough information to decode Lisp Objects.
-   We make sure certain symbols are defined, so gdb doesn't complain
-   about expressions in src/gdbinit.  Values are randomly chosen.
-   See src/gdbinit or src/dbxrc to see how this is used.  */
-
-enum dbg_constants
-{
-#ifdef USE_MINIMAL_TAGBITS
-  dbg_valmask = (EMACS_INT) (((1UL << VALBITS) - 1) << GCBITS),
-  dbg_typemask = (EMACS_INT) ((1UL << GCTYPEBITS) - 1),
-  dbg_USE_MINIMAL_TAGBITS = 1,
-  dbg_Lisp_Type_Int = 100,
-#else /* ! USE_MIMIMAL_TAGBITS */
-  dbg_valmask = (EMACS_INT) ((1UL << VALBITS) - 1),
-  dbg_typemask = (EMACS_INT) (((1UL << GCTYPEBITS) - 1) << (VALBITS + GCMARKBITS)),
-  dbg_USE_MINIMAL_TAGBITS = 0,
-  dbg_Lisp_Type_Int = Lisp_Type_Int,
-#endif /* ! USE_MIMIMAL_TAGBITS */
-#ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
-  dbg_USE_INDEXED_LRECORD_IMPLEMENTATION = 1,
-#else
-  dbg_USE_INDEXED_LRECORD_IMPLEMENTATION = 0,
-#endif
-  dbg_Lisp_Type_Char = Lisp_Type_Char,
-  dbg_Lisp_Type_Record = Lisp_Type_Record,
-#ifdef LRECORD_CONS
-  dbg_Lisp_Type_Cons = 101,
-#else
-  dbg_Lisp_Type_Cons = Lisp_Type_Cons,
-  lrecord_cons = 201,
-#endif
-#ifdef LRECORD_STRING
-  dbg_Lisp_Type_String = 102,
-#else
-  dbg_Lisp_Type_String = Lisp_Type_String,
-  lrecord_string = 202,
-#endif
-#ifdef LRECORD_VECTOR
-  dbg_Lisp_Type_Vector = 103,
-#else
-  dbg_Lisp_Type_Vector = Lisp_Type_Vector,
-  lrecord_vector = 203,
-#endif
-#ifdef LRECORD_SYMBOL
-  dbg_Lisp_Type_Symbol = 104,
-#else
-  dbg_Lisp_Type_Symbol = Lisp_Type_Symbol,
-  lrecord_symbol = 204,
-#endif
+unsigned char Lisp_Type_Int = 100;
+unsigned char Lisp_Type_Cons = 101;
+unsigned char Lisp_Type_String = 102;
+unsigned char Lisp_Type_Vector = 103;
+unsigned char Lisp_Type_Symbol = 104;
+
 #ifndef MULE
-  lrecord_char_table_entry = 205,
-  lrecord_charset          = 206,
-  lrecord_coding_system    = 207,
+unsigned char lrecord_char_table_entry;
+unsigned char lrecord_charset;
+#ifndef FILE_CODING
+unsigned char lrecord_coding_system;
+#endif
 #endif
+
 #ifndef HAVE_TOOLBARS
-  lrecord_toolbar_button   = 208,
+unsigned char lrecord_toolbar_button;
 #endif
-#ifndef HAVE_TOOLTALK
-  lrecord_tooltalk_message = 210,
-  lrecord_tooltalk_pattern = 211,
+
+#ifndef TOOLTALK
+unsigned char lrecord_tooltalk_message;
+unsigned char lrecord_tooltalk_pattern;
 #endif
+
 #ifndef HAVE_DATABASE
-  lrecord_database = 212,
+unsigned char lrecord_database;
 #endif
-  dbg_valbits = VALBITS,
-  dbg_gctypebits = GCTYPEBITS
-  /* If we don't have an actual object of this enum, pgcc (and perhaps
-     other compilers) might optimize away the entire type declaration :-( */
-} dbg_dummy;
+
+unsigned char dbg_valbits = VALBITS;
+unsigned char dbg_gctypebits = GCTYPEBITS;
+
+/* Macros turned into functions for ease of debugging.
+   Debuggers don't know about macros! */
+int dbg_eq (Lisp_Object obj1, Lisp_Object obj2);
+int
+dbg_eq (Lisp_Object obj1, Lisp_Object obj2)
+{
+  return EQ (obj1, obj2);
+}
 
 \f
-/**********************************************************************/
-/*                        Fixed-size type macros                      */
-/**********************************************************************/
+/************************************************************************/
+/*                       Fixed-size type macros                        */
+/************************************************************************/
 
 /* For fixed-size types that are commonly used, we malloc() large blocks
    of memory at a time and subdivide them into chunks of the correct
@@ -892,45 +732,46 @@ refill_memory_reserve ()
      / sizeof (structtype))
 #endif /* ALLOC_NO_POOLS */
 
-#define DECLARE_FIXED_TYPE_ALLOC(type, structtype)                       \
-                                                                         \
-struct type##_block                                                      \
-{                                                                        \
-  struct type##_block *prev;                                             \
-  structtype block[TYPE_ALLOC_SIZE (type, structtype)];                          \
-};                                                                       \
-                                                                         \
-static struct type##_block *current_##type##_block;                      \
-static int current_##type##_block_index;                                 \
-                                                                         \
-static structtype *type##_free_list;                                     \
-static structtype *type##_free_list_tail;                                \
-                                                                         \
-static void                                                              \
-init_##type##_alloc (void)                                               \
-{                                                                        \
-  current_##type##_block = 0;                                            \
-  current_##type##_block_index = countof (current_##type##_block->block); \
-  type##_free_list = 0;                                                          \
-  type##_free_list_tail = 0;                                             \
-}                                                                        \
-                                                                         \
-static int gc_count_num_##type##_in_use, gc_count_num_##type##_freelist
-
-#define ALLOCATE_FIXED_TYPE_FROM_BLOCK(type, result)                   \
-  do {                                                                 \
-    if (current_##type##_block_index                                   \
-       == countof (current_##type##_block->block))                     \
+#define DECLARE_FIXED_TYPE_ALLOC(type, structtype)     \
+                                                       \
+struct type##_block                                    \
+{                                                      \
+  struct type##_block *prev;                           \
+  structtype block[TYPE_ALLOC_SIZE (type, structtype)];        \
+};                                                     \
+                                                       \
+static struct type##_block *current_##type##_block;    \
+static int current_##type##_block_index;               \
+                                                       \
+static structtype *type##_free_list;                   \
+static structtype *type##_free_list_tail;              \
+                                                       \
+static void                                            \
+init_##type##_alloc (void)                             \
+{                                                      \
+  current_##type##_block = 0;                          \
+  current_##type##_block_index =                       \
+    countof (current_##type##_block->block);           \
+  type##_free_list = 0;                                        \
+  type##_free_list_tail = 0;                           \
+}                                                      \
+                                                       \
+static int gc_count_num_##type##_in_use;               \
+static int gc_count_num_##type##_freelist
+
+#define ALLOCATE_FIXED_TYPE_FROM_BLOCK(type, result) do {              \
+  if (current_##type##_block_index                                     \
+      == countof (current_##type##_block->block))                      \
     {                                                                  \
-      struct type##_block *__new__ = (struct type##_block *)           \
-         allocate_lisp_storage (sizeof (struct type##_block));         \
-      __new__->prev = current_##type##_block;                          \
-      current_##type##_block = __new__;                                        \
+      struct type##_block *AFTFB_new = (struct type##_block *)         \
+       allocate_lisp_storage (sizeof (struct type##_block));           \
+      AFTFB_new->prev = current_##type##_block;                                \
+      current_##type##_block = AFTFB_new;                              \
       current_##type##_block_index = 0;                                        \
     }                                                                  \
-    (result) =                                                         \
-      &(current_##type##_block->block[current_##type##_block_index++]);        \
-  } while (0)
+  (result) =                                                           \
+    &(current_##type##_block->block[current_##type##_block_index++]);  \
+} while (0)
 
 /* Allocate an instance of a type that is stored in blocks.
    TYPE is the "name" of the type, STRUCTTYPE is the corresponding
@@ -1046,22 +887,22 @@ do { if (type##_free_list_tail)                                          \
 #else /* !ERROR_CHECK_GC */
 
 #define PUT_FIXED_TYPE_ON_FREE_LIST(type, structtype, ptr)     \
-do { * (structtype **) ((char *) ptr + sizeof (void *)) =      \
+do { * (structtype **) ((char *) (ptr) + sizeof (void *)) =    \
        type##_free_list;                                       \
-     type##_free_list = ptr;                                   \
+     type##_free_list = (ptr);                                 \
    } while (0)
 
 #endif /* !ERROR_CHECK_GC */
 
 /* TYPE and STRUCTTYPE are the same as in ALLOCATE_FIXED_TYPE(). */
 
-#define FREE_FIXED_TYPE(type, structtype, ptr)                 \
-do { structtype *_weird_ = (ptr);                              \
-     ADDITIONAL_FREE_##type (_weird_);                         \
-     deadbeef_memory (ptr, sizeof (structtype));               \
-     PUT_FIXED_TYPE_ON_FREE_LIST (type, structtype, ptr);      \
-     MARK_STRUCT_AS_FREE (_weird_);                            \
-   } while (0)
+#define FREE_FIXED_TYPE(type, structtype, ptr) do {            \
+  structtype *FFT_ptr = (ptr);                                 \
+  ADDITIONAL_FREE_##type (FFT_ptr);                            \
+  deadbeef_memory (FFT_ptr, sizeof (structtype));              \
+  PUT_FIXED_TYPE_ON_FREE_LIST (type, structtype, FFT_ptr);     \
+  MARK_STRUCT_AS_FREE (FFT_ptr);                               \
+} while (0)
 
 /* Like FREE_FIXED_TYPE() but used when we are explicitly
    freeing a structure through free_cons(), free_marker(), etc.
@@ -1081,23 +922,22 @@ do { FREE_FIXED_TYPE (type, structtype, ptr);                    \
 
 
 \f
-/**********************************************************************/
-/*                         Cons allocation                            */
-/**********************************************************************/
+/************************************************************************/
+/*                        Cons allocation                              */
+/************************************************************************/
 
 DECLARE_FIXED_TYPE_ALLOC (cons, struct Lisp_Cons);
 /* conses are used and freed so often that we set this really high */
 /* #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_cons 20000 */
 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_cons 2000
 
-#ifdef LRECORD_CONS
 static Lisp_Object
-mark_cons (Lisp_Object obj, void (*markobj) (Lisp_Object))
+mark_cons (Lisp_Object obj)
 {
   if (NILP (XCDR (obj)))
     return XCAR (obj);
 
-  (markobj) (XCAR (obj));
+  mark_object (XCAR (obj));
   return XCDR (obj);
 }
 
@@ -1114,6 +954,11 @@ cons_equal (Lisp_Object ob1, Lisp_Object ob2, int depth)
   return 0;
 }
 
+static const struct lrecord_description cons_description[] = {
+  { XD_LISP_OBJECT, offsetof(struct Lisp_Cons, car), 2 },
+  { XD_END }
+};
+
 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("cons", cons,
                                     mark_cons, print_cons, 0,
                                     cons_equal,
@@ -1123,8 +968,8 @@ DEFINE_BASIC_LRECORD_IMPLEMENTATION ("cons", cons,
                                      * handle conses.
                                      */
                                     0,
+                                    cons_description,
                                     struct Lisp_Cons);
-#endif /* LRECORD_CONS */
 
 DEFUN ("cons", Fcons, 2, 2, 0, /*
 Create a new cons, give it CAR and CDR as components, and return it.
@@ -1136,9 +981,7 @@ Create a new cons, give it CAR and CDR as components, and return it.
   struct Lisp_Cons *c;
 
   ALLOCATE_FIXED_TYPE (cons, struct Lisp_Cons, c);
-#ifdef LRECORD_CONS
-  set_lheader_implementation (&(c->lheader), lrecord_cons);
-#endif
+  set_lheader_implementation (&(c->lheader), &lrecord_cons);
   XSETCONS (val, c);
   c->car = car;
   c->cdr = cdr;
@@ -1155,9 +998,7 @@ noseeum_cons (Lisp_Object car, Lisp_Object cdr)
   struct Lisp_Cons *c;
 
   NOSEEUM_ALLOCATE_FIXED_TYPE (cons, struct Lisp_Cons, c);
-#ifdef LRECORD_CONS
-  set_lheader_implementation (&(c->lheader), lrecord_cons);
-#endif
+  set_lheader_implementation (&(c->lheader), &lrecord_cons);
   XSETCONS (val, c);
   XCAR (val) = car;
   XCDR (val) = cdr;
@@ -1173,7 +1014,7 @@ Any number of arguments, even zero arguments, are allowed.
   Lisp_Object val = Qnil;
   Lisp_Object *argp = args + nargs;
 
-  while (nargs-- > 0)
+  while (argp > args)
     val = Fcons (*--argp, val);
   return val;
 }
@@ -1244,18 +1085,18 @@ Return a new list of length LENGTH, with each element being INIT.
 
   {
     Lisp_Object val = Qnil;
-    int size = XINT (length);
+    size_t size = XINT (length);
 
-    while (size-- > 0)
+    while (size--)
       val = Fcons (init, val);
     return val;
   }
 }
 
 \f
-/**********************************************************************/
-/*                        Float allocation                            */
-/**********************************************************************/
+/************************************************************************/
+/*                       Float allocation                              */
+/************************************************************************/
 
 #ifdef LISP_FLOAT_TYPE
 
@@ -1269,7 +1110,7 @@ make_float (double float_value)
   struct Lisp_Float *f;
 
   ALLOCATE_FIXED_TYPE (float, struct Lisp_Float, f);
-  set_lheader_implementation (&(f->lheader), lrecord_float);
+  set_lheader_implementation (&(f->lheader), &lrecord_float);
   float_data (f) = float_value;
   XSETFLOAT (val, f);
   return val;
@@ -1278,48 +1119,52 @@ make_float (double float_value)
 #endif /* LISP_FLOAT_TYPE */
 
 \f
-/**********************************************************************/
-/*                         Vector allocation                          */
-/**********************************************************************/
+/************************************************************************/
+/*                        Vector allocation                            */
+/************************************************************************/
 
-#ifdef LRECORD_VECTOR
 static Lisp_Object
-mark_vector (Lisp_Object obj, void (*markobj) (Lisp_Object))
+mark_vector (Lisp_Object obj)
 {
-  struct Lisp_Vector *ptr = XVECTOR (obj);
+  Lisp_Vector *ptr = XVECTOR (obj);
   int len = vector_length (ptr);
   int i;
 
   for (i = 0; i < len - 1; i++)
-    (markobj) (ptr->contents[i]);
+    mark_object (ptr->contents[i]);
   return (len > 0) ? ptr->contents[len - 1] : Qnil;
 }
 
 static size_t
 size_vector (CONST void *lheader)
 {
-  /* * -1 because struct Lisp_Vector includes 1 slot */
-  return sizeof (struct Lisp_Vector) +
-    ((((struct Lisp_Vector *) lheader)->size - 1) * sizeof (Lisp_Object));
+  return STRETCHY_STRUCT_SIZEOF (Lisp_Vector, contents,
+                                ((Lisp_Vector *) lheader)->size);
 }
 
 static int
-vector_equal (Lisp_Object o1, Lisp_Object o2, int depth)
+vector_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
 {
-  int indice;
-  int len = XVECTOR_LENGTH (o1);
-  if (len != XVECTOR_LENGTH (o2))
+  int len = XVECTOR_LENGTH (obj1);
+  if (len != XVECTOR_LENGTH (obj2))
     return 0;
-  for (indice = 0; indice < len; indice++)
-    {
-      if (!internal_equal (XVECTOR_DATA (o1) [indice],
-                          XVECTOR_DATA (o2) [indice],
-                          depth + 1))
+
+  {
+    Lisp_Object *ptr1 = XVECTOR_DATA (obj1);
+    Lisp_Object *ptr2 = XVECTOR_DATA (obj2);
+    while (len--)
+      if (!internal_equal (*ptr1++, *ptr2++, depth + 1))
        return 0;
-    }
+  }
   return 1;
 }
 
+static const struct lrecord_description vector_description[] = {
+  { XD_LONG,        offsetof(struct Lisp_Vector, size) },
+  { XD_LISP_OBJECT, offsetof(struct Lisp_Vector, contents), XD_INDIRECT(0, 0) },
+  { XD_END }
+};
+
 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION("vector", vector,
                                       mark_vector, print_vector, 0,
                                       vector_equal,
@@ -1329,77 +1174,35 @@ DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION("vector", vector,
                                        * knows how to handle vectors.
                                        */
                                       0,
-                                      size_vector, struct Lisp_Vector);
-
-/* #### should allocate `small' vectors from a frob-block */
-static struct Lisp_Vector *
-make_vector_internal (size_t sizei)
-{
-  size_t sizem = (sizeof (struct Lisp_Vector)
-                 /* -1 because struct Lisp_Vector includes 1 slot */
-                 + (sizei - 1) * sizeof (Lisp_Object));
-  struct Lisp_Vector *p =
-    (struct Lisp_Vector *) alloc_lcrecord (sizem, lrecord_vector);
-
-  p->size = sizei;
-  return p;
-}
-
-#else /* ! LRECORD_VECTOR */
-
-static Lisp_Object all_vectors;
+                                      vector_description,
+                                      size_vector, Lisp_Vector);
 
 /* #### should allocate `small' vectors from a frob-block */
-static struct Lisp_Vector *
+static Lisp_Vector *
 make_vector_internal (size_t sizei)
 {
-  size_t sizem = (sizeof (struct Lisp_Vector)
-                 /* -1 because struct Lisp_Vector includes 1 slot,
-                  * +1 to account for vector_next */
-                 + (sizei - 1 + 1) * sizeof (Lisp_Object));
-  struct Lisp_Vector *p = (struct Lisp_Vector *) allocate_lisp_storage (sizem);
-
-  INCREMENT_CONS_COUNTER (sizem, "vector");
+  /* no vector_next */
+  size_t sizem = STRETCHY_STRUCT_SIZEOF (Lisp_Vector, contents, sizei);
+  Lisp_Vector *p = (Lisp_Vector *) alloc_lcrecord (sizem, &lrecord_vector);
 
   p->size = sizei;
-  vector_next (p) = all_vectors;
-  XSETVECTOR (all_vectors, p);
   return p;
 }
 
-#endif /* ! LRECORD_VECTOR */
-
 Lisp_Object
-make_vector (EMACS_INT length, Lisp_Object init)
+make_vector (size_t length, Lisp_Object init)
 {
-  int elt;
-  Lisp_Object vector;
-  struct Lisp_Vector *p;
-
-  if (length < 0)
-    length = XINT (wrong_type_argument (Qnatnump, make_int (length)));
+  Lisp_Vector *vecp = make_vector_internal (length);
+  Lisp_Object *p = vector_data (vecp);
 
-  p = make_vector_internal (length);
-  XSETVECTOR (vector, p);
+  while (length--)
+    *p++ = init;
 
-#if 0
-  /* Initialize big arrays full of 0's quickly, for what that's worth */
   {
-    char *travesty = (char *) &init;
-    for (i = 1; i < sizeof (Lisp_Object); i++)
-    {
-      if (travesty[i] != travesty[0])
-        goto fill;
-    }
-    memset (vector_data (p), travesty[0], length * sizeof (Lisp_Object));
+    Lisp_Object vector;
+    XSETVECTOR (vector, vecp);
     return vector;
   }
- fill:
-#endif
-  for (elt = 0; elt < length; elt++)
-    vector_data(p)[elt] = init;
-
-  return vector;
 }
 
 DEFUN ("make-vector", Fmake_vector, 2, 2, 0, /*
@@ -1408,7 +1211,7 @@ See also the function `vector'.
 */
        (length, init))
 {
-  CHECK_NATNUM (length);
+  CONCHECK_NATNUM (length);
   return make_vector (XINT (length), init);
 }
 
@@ -1418,15 +1221,17 @@ Any number of arguments, even zero arguments, are allowed.
 */
        (int nargs, Lisp_Object *args))
 {
-  Lisp_Object vector;
-  int elt;
-  struct Lisp_Vector *p = make_vector_internal (nargs);
+  Lisp_Vector *vecp = make_vector_internal (nargs);
+  Lisp_Object *p = vector_data (vecp);
 
-  for (elt = 0; elt < nargs; elt++)
-    vector_data(p)[elt] = args[elt];
+  while (nargs--)
+    *p++ = *args++;
 
-  XSETVECTOR (vector, p);
-  return vector;
+  {
+    Lisp_Object vector;
+    XSETVECTOR (vector, vecp);
+    return vector;
+  }
 }
 
 Lisp_Object
@@ -1529,9 +1334,9 @@ vector8 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2,
 }
 #endif /* unused */
 
-/**********************************************************************/
-/*                       Bit Vector allocation                        */
-/**********************************************************************/
+/************************************************************************/
+/*                      Bit Vector allocation                          */
+/************************************************************************/
 
 static Lisp_Object all_bit_vectors;
 
@@ -1539,69 +1344,63 @@ static Lisp_Object all_bit_vectors;
 static struct Lisp_Bit_Vector *
 make_bit_vector_internal (size_t sizei)
 {
-  size_t sizem = sizeof (struct Lisp_Bit_Vector) +
-    /* -1 because struct Lisp_Bit_Vector includes 1 slot */
-    sizeof (long) * (BIT_VECTOR_LONG_STORAGE (sizei) - 1);
-  struct Lisp_Bit_Vector *p =
-    (struct Lisp_Bit_Vector *) allocate_lisp_storage (sizem);
-  set_lheader_implementation (&(p->lheader), lrecord_bit_vector);
+  size_t num_longs = BIT_VECTOR_LONG_STORAGE (sizei);
+  size_t sizem = STRETCHY_STRUCT_SIZEOF (Lisp_Bit_Vector, bits, num_longs);
+  Lisp_Bit_Vector *p = (Lisp_Bit_Vector *) allocate_lisp_storage (sizem);
+  set_lheader_implementation (&(p->lheader), &lrecord_bit_vector);
 
   INCREMENT_CONS_COUNTER (sizem, "bit-vector");
 
   bit_vector_length (p) = sizei;
-  bit_vector_next (p) = all_bit_vectors;
+  bit_vector_next   (p) = all_bit_vectors;
   /* make sure the extra bits in the last long are 0; the calling
      functions might not set them. */
-  p->bits[BIT_VECTOR_LONG_STORAGE (sizei) - 1] = 0;
+  p->bits[num_longs - 1] = 0;
   XSETBIT_VECTOR (all_bit_vectors, p);
   return p;
 }
 
 Lisp_Object
-make_bit_vector (EMACS_INT length, Lisp_Object init)
+make_bit_vector (size_t length, Lisp_Object init)
 {
-  Lisp_Object bit_vector;
-  struct Lisp_Bit_Vector *p;
-  EMACS_INT num_longs;
+  struct Lisp_Bit_Vector *p = make_bit_vector_internal (length);
+  size_t num_longs = BIT_VECTOR_LONG_STORAGE (length);
 
   CHECK_BIT (init);
 
-  num_longs = BIT_VECTOR_LONG_STORAGE (length);
-  p = make_bit_vector_internal (length);
-  XSETBIT_VECTOR (bit_vector, p);
-
   if (ZEROP (init))
     memset (p->bits, 0, num_longs * sizeof (long));
   else
     {
-      EMACS_INT bits_in_last = length & (LONGBITS_POWER_OF_2 - 1);
+      size_t bits_in_last = length & (LONGBITS_POWER_OF_2 - 1);
       memset (p->bits, ~0, num_longs * sizeof (long));
       /* But we have to make sure that the unused bits in the
-        last integer are 0, so that equal/hash is easy. */
+        last long are 0, so that equal/hash is easy. */
       if (bits_in_last)
        p->bits[num_longs - 1] &= (1 << bits_in_last) - 1;
     }
 
-  return bit_vector;
+  {
+    Lisp_Object bit_vector;
+    XSETBIT_VECTOR (bit_vector, p);
+    return bit_vector;
+  }
 }
 
 Lisp_Object
-make_bit_vector_from_byte_vector (unsigned char *bytevec, EMACS_INT length)
+make_bit_vector_from_byte_vector (unsigned char *bytevec, size_t length)
 {
-  Lisp_Object bit_vector;
-  struct Lisp_Bit_Vector *p;
   int i;
-
-  if (length < 0)
-    length = XINT (wrong_type_argument (Qnatnump, make_int (length)));
-
-  p = make_bit_vector_internal (length);
-  XSETBIT_VECTOR (bit_vector, p);
+  Lisp_Bit_Vector *p = make_bit_vector_internal (length);
 
   for (i = 0; i < length; i++)
     set_bit_vector_bit (p, i, bytevec[i]);
 
-  return bit_vector;
+  {
+    Lisp_Object bit_vector;
+    XSETBIT_VECTOR (bit_vector, p);
+    return bit_vector;
+  }
 }
 
 DEFUN ("make-bit-vector", Fmake_bit_vector, 2, 2, 0, /*
@@ -1621,240 +1420,168 @@ Any number of arguments, even zero arguments, are allowed.
 */
        (int nargs, Lisp_Object *args))
 {
-  Lisp_Object bit_vector;
-  int elt;
-  struct Lisp_Bit_Vector *p;
-
-  for (elt = 0; elt < nargs; elt++)
-    CHECK_BIT (args[elt]);
-
-  p = make_bit_vector_internal (nargs);
+  int i;
+  Lisp_Bit_Vector *p = make_bit_vector_internal (nargs);
 
-  for (elt = 0; elt < nargs; elt++)
-    set_bit_vector_bit (p, elt, !ZEROP (args[elt]));
+  for (i = 0; i < nargs; i++)
+    {
+      CHECK_BIT (args[i]);
+      set_bit_vector_bit (p, i, !ZEROP (args[i]));
+    }
 
-  XSETBIT_VECTOR (bit_vector, p);
-  return bit_vector;
+  {
+    Lisp_Object bit_vector;
+    XSETBIT_VECTOR (bit_vector, p);
+    return bit_vector;
+  }
 }
 
 \f
-/**********************************************************************/
-/*                   Compiled-function allocation                     */
-/**********************************************************************/
+/************************************************************************/
+/*                  Compiled-function allocation                       */
+/************************************************************************/
 
-DECLARE_FIXED_TYPE_ALLOC (compiled_function, struct Lisp_Compiled_Function);
+DECLARE_FIXED_TYPE_ALLOC (compiled_function, Lisp_Compiled_Function);
 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_compiled_function 1000
 
 static Lisp_Object
-make_compiled_function (int make_pure)
-{
-  struct Lisp_Compiled_Function *b;
-  Lisp_Object new;
-  size_t size = sizeof (struct Lisp_Compiled_Function);
-
-  if (make_pure && check_purespace (size))
-    {
-      b = (struct Lisp_Compiled_Function *) (PUREBEG + pure_bytes_used);
-      set_lheader_implementation (&(b->lheader), lrecord_compiled_function);
-#ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
-      b->lheader.pure = 1;
-#endif
-      pure_bytes_used += size;
-      bump_purestat (&purestat_bytecode, size);
-    }
-  else
-    {
-      ALLOCATE_FIXED_TYPE (compiled_function, struct Lisp_Compiled_Function,
-                          b);
-      set_lheader_implementation (&(b->lheader), lrecord_compiled_function);
-    }
-  b->maxdepth = 0;
-  b->flags.documentationp = 0;
-  b->flags.interactivep = 0;
-  b->flags.domainp = 0; /* I18N3 */
-  b->bytecodes = Qzero;
-  b->constants = Qzero;
-  b->arglist = Qnil;
-  b->doc_and_interactive = Qnil;
+make_compiled_function (void)
+{
+  Lisp_Compiled_Function *f;
+  Lisp_Object fun;
+
+  ALLOCATE_FIXED_TYPE (compiled_function, Lisp_Compiled_Function, f);
+  set_lheader_implementation (&(f->lheader), &lrecord_compiled_function);
+
+  f->stack_depth = 0;
+  f->specpdl_depth = 0;
+  f->flags.documentationp = 0;
+  f->flags.interactivep = 0;
+  f->flags.domainp = 0; /* I18N3 */
+  f->instructions = Qzero;
+  f->constants = Qzero;
+  f->arglist = Qnil;
+  f->doc_and_interactive = Qnil;
 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
-  b->annotated = Qnil;
+  f->annotated = Qnil;
 #endif
-  XSETCOMPILED_FUNCTION (new, b);
-  return new;
+  XSETCOMPILED_FUNCTION (fun, f);
+  return fun;
 }
 
 DEFUN ("make-byte-code", Fmake_byte_code, 4, MANY, 0, /*
 Return a new compiled-function object.
-Usage: (arglist instructions constants stack-size
-       &optional doc-string interactive-spec)
+Usage: (arglist instructions constants stack-depth
+       &optional doc-string interactive)
 Note that, unlike all other emacs-lisp functions, calling this with five
 arguments is NOT the same as calling it with six arguments, the last of
 which is nil.  If the INTERACTIVE arg is specified as nil, then that means
 that this function was defined with `(interactive)'.  If the arg is not
 specified, then that means the function is not interactive.
 This is terrible behavior which is retained for compatibility with old
-`.elc' files which expected these semantics.
+`.elc' files which expect these semantics.
 */
        (int nargs, Lisp_Object *args))
 {
-/*   In a non-insane world this function would have this arglist...
-     (arglist, instructions, constants, stack_size, doc_string, interactive)
-     Lisp_Object arglist, instructions, constants, stack_size, doc_string,
-       interactive;
+/* In a non-insane world this function would have this arglist...
+   (arglist instructions constants stack_depth &optional doc_string interactive)
  */
+  Lisp_Object fun = make_compiled_function ();
+  Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (fun);
+
   Lisp_Object arglist      = args[0];
   Lisp_Object instructions = args[1];
   Lisp_Object constants    = args[2];
-  Lisp_Object stack_size   = args[3];
+  Lisp_Object stack_depth  = args[3];
   Lisp_Object doc_string   = (nargs > 4) ? args[4] : Qnil;
   Lisp_Object interactive  = (nargs > 5) ? args[5] : Qunbound;
-  /* Don't purecopy the doc references in instructions because it's
-     wasteful; they will get fixed up later.
-
-     #### If something goes wrong and they don't get fixed up,
-     we're screwed, because pure stuff isn't marked and thus the
-     cons references won't be marked and will get reused.
-
-     Note: there will be a window after the byte code is created and
-     before the doc references are fixed up in which there will be
-     impure objects inside a pure object, which apparently won't
-     get marked, leading the trouble.  But during that entire window,
-     the objects are sitting on Vload_force_doc_string_list, which
-     is staticpro'd, so we're OK. */
-  int purecopy_instructions = 1;
-
-  if (nargs > 6)
+
+  if (nargs < 4 || nargs > 6)
     return Fsignal (Qwrong_number_of_arguments,
                    list2 (intern ("make-byte-code"), make_int (nargs)));
 
-  CHECK_LIST (arglist);
-  /* instructions is a string or a cons (string . int) for a
+  /* Check for valid formal parameter list now, to allow us to use
+     SPECBIND_FAST_UNSAFE() later in funcall_compiled_function(). */
+  {
+    Lisp_Object symbol, tail;
+    EXTERNAL_LIST_LOOP_3 (symbol, arglist, tail)
+      {
+       CHECK_SYMBOL (symbol);
+       if (EQ (symbol, Qt)   ||
+           EQ (symbol, Qnil) ||
+           SYMBOL_IS_KEYWORD (symbol))
+         signal_simple_error_2
+           ("Invalid constant symbol in formal parameter list",
+            symbol, arglist);
+      }
+  }
+  f->arglist = arglist;
+
+  /* `instructions' is a string or a cons (string . int) for a
      lazy-loaded function. */
   if (CONSP (instructions))
     {
       CHECK_STRING (XCAR (instructions));
       CHECK_INT (XCDR (instructions));
-      if (!NILP (constants))
-       CHECK_VECTOR (constants);
-      purecopy_instructions = 0;
     }
   else
     {
       CHECK_STRING (instructions);
-      CHECK_VECTOR (constants);
     }
-  CHECK_NATNUM (stack_size);
-  /* doc_string may be nil, string, int, or a cons (string . int). */
+  f->instructions = instructions;
 
-  /* interactive may be list or string (or unbound). */
+  if (!NILP (constants))
+    CHECK_VECTOR (constants);
+  f->constants = constants;
 
-  if (purify_flag)
-    {
-      if (!purified (arglist))
-       arglist = Fpurecopy (arglist);
-      if (purecopy_instructions && !purified (instructions))
-       instructions = Fpurecopy (instructions);
-      if (!purified (doc_string))
-       doc_string = Fpurecopy (doc_string);
-      if (!purified (interactive) && !UNBOUNDP (interactive))
-       interactive = Fpurecopy (interactive);
-
-      /* Statistics are kept differently for the constants */
-      if (!purified (constants))
-#ifdef PURESTAT
-       {
-         int old = purecopying_for_bytecode;
-         purecopying_for_bytecode = 1;
-         constants = Fpurecopy (constants);
-         purecopying_for_bytecode = old;
-       }
-#else
-        constants = Fpurecopy (constants);
-#endif /* PURESTAT */
-
-#ifdef PURESTAT
-      if (STRINGP (instructions))
-       bump_purestat (&purestat_string_bytecodes, pure_sizeof (instructions));
-      if (VECTORP (constants))
-       bump_purestat (&purestat_vector_bytecode_constants,
-                      pure_sizeof (constants));
-      if (STRINGP (doc_string))
-       /* These should be have been snagged by make-docfile... */
-       bump_purestat (&purestat_string_documentation,
-                      pure_sizeof (doc_string));
-      if (STRINGP (interactive))
-       bump_purestat (&purestat_string_interactive,
-                      pure_sizeof (interactive));
-#endif /* PURESTAT */
-    }
+  CHECK_NATNUM (stack_depth);
+  f->stack_depth = XINT (stack_depth);
 
-  {
-    int docp = !NILP (doc_string);
-    int intp = !UNBOUNDP (interactive);
-#ifdef I18N3
-    int domp = !NILP (Vfile_domain);
-#endif
-    Lisp_Object val = make_compiled_function (purify_flag);
-    struct Lisp_Compiled_Function *b = XCOMPILED_FUNCTION (val);
-    b->flags.documentationp = docp;
-    b->flags.interactivep   = intp;
-#ifdef I18N3
-    b->flags.domainp        = domp;
-#endif
-    b->maxdepth  = XINT (stack_size);
-    b->bytecodes = instructions;
-    b->constants = constants;
-    b->arglist   = arglist;
 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
-    if (!NILP (Vcurrent_compiled_function_annotation))
-      b->annotated = Fpurecopy (Vcurrent_compiled_function_annotation);
-    else if (!NILP (Vload_file_name_internal_the_purecopy))
-      b->annotated = Vload_file_name_internal_the_purecopy;
-    else if (!NILP (Vload_file_name_internal))
-      {
-       struct gcpro gcpro1;
-       GCPRO1(val);            /* don't let val or b get reaped */
-       Vload_file_name_internal_the_purecopy =
-         Fpurecopy (Ffile_name_nondirectory (Vload_file_name_internal));
-       b->annotated = Vload_file_name_internal_the_purecopy;
-       UNGCPRO;
-      }
+  if (!NILP (Vcurrent_compiled_function_annotation))
+    f->annotated = Fcopy (Vcurrent_compiled_function_annotation);
+  else if (!NILP (Vload_file_name_internal_the_purecopy))
+    f->annotated = Vload_file_name_internal_the_purecopy;
+  else if (!NILP (Vload_file_name_internal))
+    {
+      struct gcpro gcpro1;
+      GCPRO1 (fun);            /* don't let fun get reaped */
+      Vload_file_name_internal_the_purecopy =
+       Ffile_name_nondirectory (Vload_file_name_internal);
+      f->annotated = Vload_file_name_internal_the_purecopy;
+      UNGCPRO;
+    }
 #endif /* COMPILED_FUNCTION_ANNOTATION_HACK */
 
+  /* doc_string may be nil, string, int, or a cons (string . int).
+     interactive may be list or string (or unbound). */
+  f->doc_and_interactive = Qunbound;
 #ifdef I18N3
-    if (docp && intp && domp)
-      b->doc_and_interactive = (((purify_flag) ? pure_cons : Fcons)
-                               (doc_string,
-                                (((purify_flag) ? pure_cons : Fcons)
-                                 (interactive, Vfile_domain))));
-    else if (docp && domp)
-      b->doc_and_interactive = (((purify_flag) ? pure_cons : Fcons)
-                               (doc_string, Vfile_domain));
-    else if (intp && domp)
-      b->doc_and_interactive = (((purify_flag) ? pure_cons : Fcons)
-                               (interactive, Vfile_domain));
-    else
-#endif
-    if (docp && intp)
-      b->doc_and_interactive = (((purify_flag) ? pure_cons : Fcons)
-                               (doc_string, interactive));
-    else if (intp)
-      b->doc_and_interactive = interactive;
-#ifdef I18N3
-    else if (domp)
-      b->doc_and_interactive = Vfile_domain;
+  if ((f->flags.domainp = !NILP (Vfile_domain)) != 0)
+    f->doc_and_interactive = Vfile_domain;
 #endif
-    else
-      b->doc_and_interactive = doc_string;
+  if ((f->flags.interactivep = !UNBOUNDP (interactive)) != 0)
+    {
+      f->doc_and_interactive
+       = (UNBOUNDP (f->doc_and_interactive) ? interactive :
+          Fcons (interactive, f->doc_and_interactive));
+    }
+  if ((f->flags.documentationp = !NILP (doc_string)) != 0)
+    {
+      f->doc_and_interactive
+       = (UNBOUNDP (f->doc_and_interactive) ? doc_string :
+          Fcons (doc_string, f->doc_and_interactive));
+    }
+  if (UNBOUNDP (f->doc_and_interactive))
+    f->doc_and_interactive = Qnil;
 
-    return val;
-  }
+  return fun;
 }
 
 \f
-/**********************************************************************/
-/*                          Symbol allocation                         */
-/**********************************************************************/
+/************************************************************************/
+/*                         Symbol allocation                           */
+/************************************************************************/
 
 DECLARE_FIXED_TYPE_ALLOC (symbol, struct Lisp_Symbol);
 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_symbol 1000
@@ -1863,31 +1590,28 @@ DEFUN ("make-symbol", Fmake_symbol, 1, 1, 0, /*
 Return a newly allocated uninterned symbol whose name is NAME.
 Its value and function definition are void, and its property list is nil.
 */
-       (str))
+       (name))
 {
   Lisp_Object val;
   struct Lisp_Symbol *p;
 
-  CHECK_STRING (str);
+  CHECK_STRING (name);
 
   ALLOCATE_FIXED_TYPE (symbol, struct Lisp_Symbol, p);
-#ifdef LRECORD_SYMBOL
-  set_lheader_implementation (&(p->lheader), lrecord_symbol);
-#endif
-  p->name = XSTRING (str);
-  p->plist = Qnil;
-  p->value = Qunbound;
+  set_lheader_implementation (&(p->lheader), &lrecord_symbol);
+  p->name     = XSTRING (name);
+  p->plist    = Qnil;
+  p->value    = Qunbound;
   p->function = Qunbound;
-  p->obarray = Qnil;
   symbol_next (p) = 0;
   XSETSYMBOL (val, p);
   return val;
 }
 
 \f
-/**********************************************************************/
-/*                         Extent allocation                          */
-/**********************************************************************/
+/************************************************************************/
+/*                        Extent allocation                            */
+/************************************************************************/
 
 DECLARE_FIXED_TYPE_ALLOC (extent, struct extent);
 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_extent 1000
@@ -1898,8 +1622,7 @@ allocate_extent (void)
   struct extent *e;
 
   ALLOCATE_FIXED_TYPE (extent, struct extent, e);
-  /* xzero (*e); */
-  set_lheader_implementation (&(e->lheader), lrecord_extent);
+  set_lheader_implementation (&(e->lheader), &lrecord_extent);
   extent_object (e) = Qnil;
   set_extent_start (e, -1);
   set_extent_end (e, -1);
@@ -1915,9 +1638,9 @@ allocate_extent (void)
 }
 
 \f
-/**********************************************************************/
-/*                         Event allocation                           */
-/**********************************************************************/
+/************************************************************************/
+/*                        Event allocation                             */
+/************************************************************************/
 
 DECLARE_FIXED_TYPE_ALLOC (event, struct Lisp_Event);
 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_event 1000
@@ -1929,16 +1652,16 @@ allocate_event (void)
   struct Lisp_Event *e;
 
   ALLOCATE_FIXED_TYPE (event, struct Lisp_Event, e);
-  set_lheader_implementation (&(e->lheader), lrecord_event);
+  set_lheader_implementation (&(e->lheader), &lrecord_event);
 
   XSETEVENT (val, e);
   return val;
 }
 
 \f
-/**********************************************************************/
-/*                       Marker allocation                            */
-/**********************************************************************/
+/************************************************************************/
+/*                      Marker allocation                              */
+/************************************************************************/
 
 DECLARE_FIXED_TYPE_ALLOC (marker, struct Lisp_Marker);
 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_marker 1000
@@ -1952,7 +1675,7 @@ Return a new marker which does not point at any place.
   struct Lisp_Marker *p;
 
   ALLOCATE_FIXED_TYPE (marker, struct Lisp_Marker, p);
-  set_lheader_implementation (&(p->lheader), lrecord_marker);
+  set_lheader_implementation (&(p->lheader), &lrecord_marker);
   p->buffer = 0;
   p->memind = 0;
   marker_next (p) = 0;
@@ -1969,7 +1692,7 @@ noseeum_make_marker (void)
   struct Lisp_Marker *p;
 
   NOSEEUM_ALLOCATE_FIXED_TYPE (marker, struct Lisp_Marker, p);
-  set_lheader_implementation (&(p->lheader), lrecord_marker);
+  set_lheader_implementation (&(p->lheader), &lrecord_marker);
   p->buffer = 0;
   p->memind = 0;
   marker_next (p) = 0;
@@ -1980,9 +1703,9 @@ noseeum_make_marker (void)
 }
 
 \f
-/**********************************************************************/
-/*                        String allocation                           */
-/**********************************************************************/
+/************************************************************************/
+/*                       String allocation                             */
+/************************************************************************/
 
 /* The data for "short" strings generally resides inside of structs of type
    string_chars_block. The Lisp_String structure is allocated just like any
@@ -1997,30 +1720,36 @@ noseeum_make_marker (void)
 
    This new method makes things somewhat bigger, but it is MUCH safer.  */
 
-DECLARE_FIXED_TYPE_ALLOC (string, struct Lisp_String);
+DECLARE_FIXED_TYPE_ALLOC (string, Lisp_String);
 /* strings are used and freed quite often */
 /* #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_string 10000 */
 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_string 1000
 
-#ifdef LRECORD_STRING
 static Lisp_Object
-mark_string (Lisp_Object obj, void (*markobj) (Lisp_Object))
+mark_string (Lisp_Object obj)
 {
-  struct Lisp_String *ptr = XSTRING (obj);
+  Lisp_String *ptr = XSTRING (obj);
 
-  if (GC_CONSP (ptr->plist) && GC_EXTENT_INFOP (XCAR (ptr->plist)))
+  if (CONSP (ptr->plist) && EXTENT_INFOP (XCAR (ptr->plist)))
     flush_cached_extent_info (XCAR (ptr->plist));
   return ptr->plist;
 }
 
 static int
-string_equal (Lisp_Object o1, Lisp_Object o2, int depth)
+string_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
 {
   Bytecount len;
-  return (((len = XSTRING_LENGTH (o1)) == XSTRING_LENGTH (o2)) &&
-         !memcmp (XSTRING_DATA (o1), XSTRING_DATA (o2), len));
+  return (((len = XSTRING_LENGTH (obj1)) == XSTRING_LENGTH (obj2)) &&
+         !memcmp (XSTRING_DATA (obj1), XSTRING_DATA (obj2), len));
 }
 
+static const struct lrecord_description string_description[] = {
+  { XD_BYTECOUNT,       offsetof(Lisp_String, size) },
+  { XD_OPAQUE_DATA_PTR, offsetof(Lisp_String, data), XD_INDIRECT(0, 1) },
+  { XD_LISP_OBJECT,     offsetof(Lisp_String, plist), 1 },
+  { XD_END }
+};
+
 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("string", string,
                                     mark_string, print_string,
                                     /*
@@ -2034,8 +1763,8 @@ DEFINE_BASIC_LRECORD_IMPLEMENTATION ("string", string,
                                      * SWEEP_FIXED_TYPE_BLOCK().
                                      */
                                     0, string_equal, 0,
-                                    struct Lisp_String);
-#endif /* LRECORD_STRING */
+                                    string_description,
+                                    Lisp_String);
 
 /* String blocks contain this many useful bytes. */
 #define STRING_CHARS_BLOCK_SIZE                                        \
@@ -2053,34 +1782,29 @@ struct string_chars_block
   unsigned char string_chars[STRING_CHARS_BLOCK_SIZE];
 };
 
-struct string_chars_block *first_string_chars_block;
-struct string_chars_block *current_string_chars_block;
+static struct string_chars_block *first_string_chars_block;
+static struct string_chars_block *current_string_chars_block;
 
 /* If SIZE is the length of a string, this returns how many bytes
  *  the string occupies in string_chars_block->string_chars
  *  (including alignment padding).
  */
-#define STRING_FULLSIZE(s) \
-   ALIGN_SIZE (((s) + 1 + sizeof (struct Lisp_String *)),\
-               ALIGNOF (struct Lisp_String *))
+#define STRING_FULLSIZE(size) \
+   ALIGN_SIZE (((size) + 1 + sizeof (Lisp_String *)),\
+               ALIGNOF (Lisp_String *))
 
 #define BIG_STRING_FULLSIZE_P(fullsize) ((fullsize) >= STRING_CHARS_BLOCK_SIZE)
 #define BIG_STRING_SIZE_P(size) (BIG_STRING_FULLSIZE_P (STRING_FULLSIZE(size)))
 
-#define CHARS_TO_STRING_CHAR(x) \
-  ((struct string_chars *) \
-   (((char *) (x)) - (slot_offset (struct string_chars, chars[0]))))
-
-
 struct string_chars
 {
-  struct Lisp_String *string;
+  Lisp_String *string;
   unsigned char chars[1];
 };
 
 struct unused_string_chars
 {
-  struct Lisp_String *string;
+  Lisp_String *string;
   EMACS_INT fullsize;
 };
 
@@ -2095,19 +1819,14 @@ init_string_chars_alloc (void)
 }
 
 static struct string_chars *
-allocate_string_chars_struct (struct Lisp_String *string_it_goes_with,
+allocate_string_chars_struct (Lisp_String *string_it_goes_with,
                              EMACS_INT fullsize)
 {
   struct string_chars *s_chars;
 
-  /* Allocate the string's actual data */
-  if (BIG_STRING_FULLSIZE_P (fullsize))
-    {
-      s_chars = (struct string_chars *) xmalloc (fullsize);
-    }
-  else if (fullsize <=
-           (countof (current_string_chars_block->string_chars)
-            - current_string_chars_block->pos))
+  if (fullsize <=
+      (countof (current_string_chars_block->string_chars)
+       - current_string_chars_block->pos))
     {
       /* This string can fit in the current string chars block */
       s_chars = (struct string_chars *)
@@ -2118,13 +1837,13 @@ allocate_string_chars_struct (struct Lisp_String *string_it_goes_with,
   else
     {
       /* Make a new current string chars block */
-      struct string_chars_block *new = xnew (struct string_chars_block);
+      struct string_chars_block *new_scb = xnew (struct string_chars_block);
 
-      current_string_chars_block->next = new;
-      new->prev = current_string_chars_block;
-      new->next = 0;
-      current_string_chars_block = new;
-      new->pos = fullsize;
+      current_string_chars_block->next = new_scb;
+      new_scb->prev = current_string_chars_block;
+      new_scb->next = 0;
+      current_string_chars_block = new_scb;
+      new_scb->pos = fullsize;
       s_chars = (struct string_chars *)
        current_string_chars_block->string_chars;
     }
@@ -2139,23 +1858,20 @@ allocate_string_chars_struct (struct Lisp_String *string_it_goes_with,
 Lisp_Object
 make_uninit_string (Bytecount length)
 {
-  struct Lisp_String *s;
-  struct string_chars *s_chars;
+  Lisp_String *s;
   EMACS_INT fullsize = STRING_FULLSIZE (length);
   Lisp_Object val;
 
-  if ((length < 0) || (fullsize <= 0))
-    abort ();
+  assert (length >= 0 && fullsize > 0);
 
   /* Allocate the string header */
-  ALLOCATE_FIXED_TYPE (string, struct Lisp_String, s);
-#ifdef LRECORD_STRING
-  set_lheader_implementation (&(s->lheader), lrecord_string);
-#endif
+  ALLOCATE_FIXED_TYPE (string, Lisp_String, s);
+  set_lheader_implementation (&(s->lheader), &lrecord_string);
 
-  s_chars = allocate_string_chars_struct (s, fullsize);
+  set_string_data (s, BIG_STRING_FULLSIZE_P (fullsize)
+                  ? xnew_array (Bufbyte, length + 1)
+                  : allocate_string_chars_struct (s, fullsize)->chars);
 
-  set_string_data (s, &(s_chars->chars[0]));
   set_string_length (s, length);
   s->plist = Qnil;
 
@@ -2176,8 +1892,9 @@ static void verify_string_chars_integrity (void);
 */
 
 void
-resize_string (struct Lisp_String *s, Bytecount pos, Bytecount delta)
+resize_string (Lisp_String *s, Bytecount pos, Bytecount delta)
 {
+  Bytecount oldfullsize, newfullsize;
 #ifdef VERIFY_STRING_CHARS_INTEGRITY
   verify_string_chars_integrity ();
 #endif
@@ -2196,51 +1913,63 @@ resize_string (struct Lisp_String *s, Bytecount pos, Bytecount delta)
     }
 #endif /* ERROR_CHECK_BUFPOS */
 
-  if (pos >= 0 && delta < 0)
-  /* If DELTA < 0, the functions below will delete the characters
-     before POS.  We want to delete characters *after* POS, however,
-     so convert this to the appropriate form. */
-    pos += -delta;
-
   if (delta == 0)
     /* simplest case: no size change. */
     return;
-  else
-    {
-      Bytecount oldfullsize = STRING_FULLSIZE (string_length (s));
-      Bytecount newfullsize = STRING_FULLSIZE (string_length (s) + delta);
 
-      if (oldfullsize == newfullsize)
+  if (pos >= 0 && delta < 0)
+    /* If DELTA < 0, the functions below will delete the characters
+       before POS.  We want to delete characters *after* POS, however,
+       so convert this to the appropriate form. */
+    pos += -delta;
+
+  oldfullsize = STRING_FULLSIZE (string_length (s));
+  newfullsize = STRING_FULLSIZE (string_length (s) + delta);
+
+  if (BIG_STRING_FULLSIZE_P (oldfullsize))
+    {
+      if (BIG_STRING_FULLSIZE_P (newfullsize))
        {
-         /* next simplest case; size change but the necessary
-            allocation size won't change (up or down; code somewhere
-            depends on there not being any unused allocation space,
-            modulo any alignment constraints). */
+         /* Both strings are big.  We can just realloc(). */
+         set_string_data (s, (Bufbyte *) xrealloc (string_data (s),
+                                                   string_length (s) + delta + 1));
          if (pos >= 0)
            {
              Bufbyte *addroff = pos + string_data (s);
 
              memmove (addroff + delta, addroff,
-                      /* +1 due to zero-termination. */
                       string_length (s) + 1 - pos);
            }
        }
-      else if (BIG_STRING_FULLSIZE_P (oldfullsize) &&
-              BIG_STRING_FULLSIZE_P (newfullsize))
+      else /* String has been demoted from BIG_STRING. */
        {
-         /* next simplest case; the string is big enough to be malloc()ed
-            itself, so we just realloc.
+         Bufbyte *new_data =
+           allocate_string_chars_struct (s, newfullsize)->chars;
+         Bufbyte *old_data = string_data (s);
 
-            It's important not to let the string get below the threshold
-            for making big strings and still remain malloc()ed; if that
-            were the case, repeated calls to this function on the same
-            string could result in memory leakage. */
-         set_string_data (s, (Bufbyte *) xrealloc (string_data (s),
-                                                   newfullsize));
          if (pos >= 0)
            {
-             Bufbyte *addroff = pos + string_data (s);
-
+             memcpy (new_data, old_data, pos);
+             memcpy (new_data + pos + delta, old_data + pos,
+                     string_length (s) + 1 - pos);
+           }
+         set_string_data (s, new_data);
+         xfree (old_data);
+       }
+    }
+  else /* old string is small */
+    {
+      if (oldfullsize == newfullsize)
+       {
+         /* special case; size change but the necessary
+            allocation size won't change (up or down; code
+            somewhere depends on there not being any unused
+            allocation space, modulo any alignment
+            constraints). */
+         if (pos >= 0)
+           {
+             Bufbyte *addroff = pos + string_data (s);
+
              memmove (addroff + delta, addroff,
                       /* +1 due to zero-termination. */
                       string_length (s) + 1 - pos);
@@ -2248,58 +1977,52 @@ resize_string (struct Lisp_String *s, Bytecount pos, Bytecount delta)
        }
       else
        {
-         /* worst case.  We make a new string_chars struct and copy
-            the string's data into it, inserting/deleting the delta
-            in the process.  The old string data will either get
-            freed by us (if it was malloc()ed) or will be reclaimed
-            in the normal course of garbage collection. */
-         struct string_chars *s_chars =
-           allocate_string_chars_struct (s, newfullsize);
-         Bufbyte *new_addr = &(s_chars->chars[0]);
-         Bufbyte *old_addr = string_data (s);
+         Bufbyte *old_data = string_data (s);
+         Bufbyte *new_data =
+           BIG_STRING_FULLSIZE_P (newfullsize)
+           ? xnew_array (Bufbyte, string_length (s) + delta + 1)
+           : allocate_string_chars_struct (s, newfullsize)->chars;
+
          if (pos >= 0)
            {
-             memcpy (new_addr, old_addr, pos);
-             memcpy (new_addr + pos + delta, old_addr + pos,
+             memcpy (new_data, old_data, pos);
+             memcpy (new_data + pos + delta, old_data + pos,
                      string_length (s) + 1 - pos);
            }
-         set_string_data (s, new_addr);
-         if (BIG_STRING_FULLSIZE_P (oldfullsize))
-           xfree (old_addr);
-         else
-           {
-             /* We need to mark this chunk of the string_chars_block
-                as unused so that compact_string_chars() doesn't
-                freak. */
-             struct string_chars *old_s_chars =
-               (struct string_chars *) ((char *) old_addr -
-                                        sizeof (struct Lisp_String *));
-             /* Sanity check to make sure we aren't hosed by strange
-                alignment/padding. */
-             assert (old_s_chars->string == s);
-             MARK_STRUCT_AS_FREE (old_s_chars);
-             ((struct unused_string_chars *) old_s_chars)->fullsize =
-               oldfullsize;
-           }
+         set_string_data (s, new_data);
+
+         {
+           /* We need to mark this chunk of the string_chars_block
+              as unused so that compact_string_chars() doesn't
+              freak. */
+           struct string_chars *old_s_chars = (struct string_chars *)
+             ((char *) old_data - offsetof (struct string_chars, chars));
+           /* Sanity check to make sure we aren't hosed by strange
+              alignment/padding. */
+           assert (old_s_chars->string == s);
+           MARK_STRUCT_AS_FREE (old_s_chars);
+           ((struct unused_string_chars *) old_s_chars)->fullsize =
+             oldfullsize;
+         }
        }
+    }
 
-      set_string_length (s, string_length (s) + delta);
-      /* If pos < 0, the string won't be zero-terminated.
-        Terminate now just to make sure. */
-      string_data (s)[string_length (s)] = '\0';
+  set_string_length (s, string_length (s) + delta);
+  /* If pos < 0, the string won't be zero-terminated.
+     Terminate now just to make sure. */
+  string_data (s)[string_length (s)] = '\0';
 
-      if (pos >= 0)
-       {
-         Lisp_Object string;
-
-         XSETSTRING (string, s);
-         /* We also have to adjust all of the extent indices after the
-            place we did the change.  We say "pos - 1" because
-            adjust_extents() is exclusive of the starting position
-            passed to it. */
-         adjust_extents (string, pos - 1, string_length (s),
-                         delta);
-       }
+  if (pos >= 0)
+    {
+      Lisp_Object string;
+
+      XSETSTRING (string, s);
+      /* We also have to adjust all of the extent indices after the
+        place we did the change.  We say "pos - 1" because
+        adjust_extents() is exclusive of the starting position
+        passed to it. */
+      adjust_extents (string, pos - 1, string_length (s),
+                     delta);
     }
 
 #ifdef VERIFY_STRING_CHARS_INTEGRITY
@@ -2310,14 +2033,12 @@ resize_string (struct Lisp_String *s, Bytecount pos, Bytecount delta)
 #ifdef MULE
 
 void
-set_string_char (struct Lisp_String *s, Charcount i, Emchar c)
+set_string_char (Lisp_String *s, Charcount i, Emchar c)
 {
-  Bytecount oldlen, newlen;
   Bufbyte newstr[MAX_EMCHAR_LEN];
   Bytecount bytoff = charcount_to_bytecount (string_data (s), i);
-
-  oldlen = charcount_to_bytecount (string_data (s) + bytoff, 1);
-  newlen = set_charptr_emchar (newstr, c);
+  Bytecount oldlen = charcount_to_bytecount (string_data (s) + bytoff, 1);
+  Bytecount newlen = set_charptr_emchar (newstr, c);
 
   if (oldlen != newlen)
     resize_string (s, bytoff, newlen - oldlen);
@@ -2333,30 +2054,35 @@ LENGTH must be an integer and INIT must be a character.
 */
        (length, init))
 {
-  Lisp_Object val;
-
   CHECK_NATNUM (length);
   CHECK_CHAR_COERCE_INT (init);
   {
-    Bufbyte str[MAX_EMCHAR_LEN];
-    int len = set_charptr_emchar (str, XCHAR (init));
+    Bufbyte init_str[MAX_EMCHAR_LEN];
+    int len = set_charptr_emchar (init_str, XCHAR (init));
+    Lisp_Object val = make_uninit_string (len * XINT (length));
 
-    val = make_uninit_string (len * XINT (length));
     if (len == 1)
       /* Optimize the single-byte case */
       memset (XSTRING_DATA (val), XCHAR (init), XSTRING_LENGTH (val));
     else
       {
-       int i, j, k;
+       size_t i;
        Bufbyte *ptr = XSTRING_DATA (val);
 
-       k = 0;
-       for (i = 0; i < XINT (length); i++)
-         for (j = 0; j < len; j++)
-           ptr[k++] = str[j];
+       for (i = XINT (length); i; i--)
+         {
+           Bufbyte *init_ptr = init_str;
+           switch (len)
+             {
+             case 4: *ptr++ = *init_ptr++;
+             case 3: *ptr++ = *init_ptr++;
+             case 2: *ptr++ = *init_ptr++;
+             case 1: *ptr++ = *init_ptr++;
+             }
+         }
       }
+    return val;
   }
-  return val;
 }
 
 DEFUN ("string", Fstring, 0, MANY, 0, /*
@@ -2376,6 +2102,7 @@ Concatenate all the argument characters and make the result a string.
   return make_string (storage, p - storage);
 }
 
+
 /* Take some raw memory, which MUST already be in internal format,
    and package it up into a Lisp string. */
 Lisp_Object
@@ -2426,6 +2153,29 @@ build_translated_string (CONST char *str)
   return build_string (GETTEXT (str));
 }
 
+Lisp_Object
+make_string_nocopy (CONST Bufbyte *contents, Bytecount length)
+{
+  Lisp_String *s;
+  Lisp_Object val;
+
+  /* Make sure we find out about bad make_string_nocopy's when they happen */
+#if defined (ERROR_CHECK_BUFPOS) && defined (MULE)
+  bytecount_to_charcount (contents, length); /* Just for the assertions */
+#endif
+
+  /* Allocate the string header */
+  ALLOCATE_FIXED_TYPE (string, Lisp_String, s);
+  set_lheader_implementation (&(s->lheader), &lrecord_string);
+  SET_C_READONLY_RECORD_HEADER (&s->lheader);
+  s->plist = Qnil;
+  set_string_data (s, (Bufbyte *)contents);
+  set_string_length (s, length);
+
+  XSETSTRING (val, s);
+  return val;
+}
+
 \f
 /************************************************************************/
 /*                           lcrecord lists                             */
@@ -2439,7 +2189,7 @@ build_translated_string (CONST char *str)
    It works like this:
 
    1) Create an lcrecord-list object using make_lcrecord_list().
-      This is often done at initialization.  Remember to staticpro
+      This is often done at initialization.  Remember to staticpro_nodump
       this object!  The arguments to make_lcrecord_list() are the
       same as would be passed to alloc_lcrecord().
    2) Instead of calling alloc_lcrecord(), call allocate_managed_lcrecord()
@@ -2460,7 +2210,7 @@ build_translated_string (CONST char *str)
    */
 
 static Lisp_Object
-mark_lcrecord_list (Lisp_Object obj, void (*markobj) (Lisp_Object))
+mark_lcrecord_list (Lisp_Object obj)
 {
   struct lcrecord_list *list = XLCRECORD_LIST (obj);
   Lisp_Object chain = list->free;
@@ -2497,13 +2247,13 @@ mark_lcrecord_list (Lisp_Object obj, void (*markobj) (Lisp_Object))
 
 DEFINE_LRECORD_IMPLEMENTATION ("lcrecord-list", lcrecord_list,
                               mark_lcrecord_list, internal_object_printer,
-                              0, 0, 0, struct lcrecord_list);
+                              0, 0, 0, 0, struct lcrecord_list);
 Lisp_Object
 make_lcrecord_list (size_t size,
                    CONST struct lrecord_implementation *implementation)
 {
   struct lcrecord_list *p = alloc_lcrecord_type (struct lcrecord_list,
-                                                lrecord_lcrecord_list);
+                                                &lrecord_lcrecord_list);
   Lisp_Object val;
 
   p->implementation = implementation;
@@ -2570,889 +2320,216 @@ free_managed_lcrecord (Lisp_Object lcrecord_list, Lisp_Object lcrecord)
   /* Make sure the size is correct.  This will catch, for example,
      putting a window configuration on the wrong free list. */
   if (implementation->size_in_bytes_method)
-    assert (((implementation->size_in_bytes_method) (lheader))
-           == list->size);
+    assert (implementation->size_in_bytes_method (lheader) == list->size);
   else
     assert (implementation->static_size == list->size);
 #endif /* ERROR_CHECK_GC */
 
   if (implementation->finalizer)
-    ((implementation->finalizer) (lheader, 0));
+    implementation->finalizer (lheader, 0);
   free_header->chain = list->free;
   free_header->lcheader.free = 1;
   list->free = lcrecord;
 }
 
 \f
-/**********************************************************************/
-/*                 Purity of essence, peace on earth                  */
-/**********************************************************************/
-
-static int symbols_initialized;
 
-Lisp_Object
-make_pure_string (CONST Bufbyte *data, Bytecount length,
-                 Lisp_Object plist, int no_need_to_copy_data)
+\f
+DEFUN ("purecopy", Fpurecopy, 1, 1, 0, /*
+Kept for compatibility, returns its argument.
+Old:
+Make a copy of OBJECT in pure storage.
+Recursively copies contents of vectors and cons cells.
+Does not copy symbols.
+*/
+       (obj))
 {
-  Lisp_Object new;
-  struct Lisp_String *s;
-  size_t size = sizeof (struct Lisp_String) +
-    (no_need_to_copy_data ? 0 : (length + 1)); /* + 1 for terminating 0 */
-  size = ALIGN_SIZE (size, ALIGNOF (Lisp_Object));
-
-  if (symbols_initialized && !pure_lossage)
-    {
-      /* Try to share some names.  Saves a few kbytes. */
-      Lisp_Object tem = oblookup (Vobarray, data, length);
-      if (SYMBOLP (tem))
-       {
-         s = XSYMBOL (tem)->name;
-         if (!PURIFIED (s)) abort ();
-         XSETSTRING (new, s);
-         return new;
-       }
-    }
-
-  if (!check_purespace (size))
-    return make_string (data, length);
-
-  s = (struct Lisp_String *) (PUREBEG + pure_bytes_used);
-#ifdef LRECORD_STRING
-  set_lheader_implementation (&(s->lheader), lrecord_string);
-#ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
-  s->lheader.pure = 1;
-#endif
-#endif
-  set_string_length (s, length);
-  if (no_need_to_copy_data)
-    {
-      set_string_data (s, (Bufbyte *) data);
-    }
-  else
-    {
-      set_string_data (s, (Bufbyte *) s + sizeof (struct Lisp_String));
-      memcpy (string_data (s), data, length);
-      set_string_byte (s, length, 0);
-    }
-  s->plist = Qnil;
-  pure_bytes_used += size;
-
-#ifdef PURESTAT
-  bump_purestat (&purestat_string_all, size);
-  if (purecopying_for_bytecode)
-    bump_purestat (&purestat_string_other_function, size);
-#endif /* PURESTAT */
-
-  /* Do this after the official "completion" of the purecopying. */
-  s->plist = Fpurecopy (plist);
-
-  XSETSTRING (new, s);
-  return new;
+  return obj;
 }
 
 
-Lisp_Object
-make_pure_pname (CONST Bufbyte *data, Bytecount length,
-                int no_need_to_copy_data)
-{
-  Lisp_Object name = make_pure_string (data, length, Qnil,
-                                      no_need_to_copy_data);
-  bump_purestat (&purestat_string_pname, pure_sizeof (name));
-
-  /* We've made (at least) Qnil now, and Vobarray will soon be set up. */
-  symbols_initialized = 1;
-
-  return name;
-}
+\f
+/************************************************************************/
+/*                        Garbage Collection                           */
+/************************************************************************/
 
+/* This will be used more extensively In The Future */
+static int last_lrecord_type_index_assigned;
 
-Lisp_Object
-pure_cons (Lisp_Object car, Lisp_Object cdr)
-{
-  Lisp_Object new;
-  struct Lisp_Cons *c;
+CONST struct lrecord_implementation *lrecord_implementations_table[128];
+#define max_lrecord_type (countof (lrecord_implementations_table) - 1)
 
-  if (!check_purespace (sizeof (struct Lisp_Cons)))
-    return Fcons (Fpurecopy (car), Fpurecopy (cdr));
+struct gcpro *gcprolist;
 
-  c = (struct Lisp_Cons *) (PUREBEG + pure_bytes_used);
-#ifdef LRECORD_CONS
-  set_lheader_implementation (&(c->lheader), lrecord_cons);
-#ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
-  c->lheader.pure = 1;
-#endif
+/* 415 used Mly 29-Jun-93 */
+/* 1327 used slb 28-Feb-98 */
+/* 1328 used og  03-Oct-99 (moving slowly, heh?) */
+#ifdef HAVE_SHLIB
+#define NSTATICS 4000
+#else
+#define NSTATICS 2000
 #endif
-  pure_bytes_used += sizeof (struct Lisp_Cons);
-  bump_purestat (&purestat_cons, sizeof (struct Lisp_Cons));
-
-  c->car = Fpurecopy (car);
-  c->cdr = Fpurecopy (cdr);
-  XSETCONS (new, c);
-  return new;
-}
+/* Not "static" because of linker lossage on some systems */
+Lisp_Object *staticvec[NSTATICS]
+     /* Force it into data space! */
+     = {0};
+static int staticidx;
 
-Lisp_Object
-pure_list (int nargs, Lisp_Object *args)
+/* Put an entry in staticvec, pointing at the variable whose address is given
+ */
+void
+staticpro (Lisp_Object *varaddress)
 {
-  Lisp_Object val = Qnil;
-
-  for (--nargs; nargs >= 0; nargs--)
-    val = pure_cons (args[nargs], val);
-
-  return val;
+  if (staticidx >= countof (staticvec))
+    /* #### This is now a dubious abort() since this routine may be called */
+    /* by Lisp attempting to load a DLL. */
+    abort ();
+  staticvec[staticidx++] = varaddress;
 }
 
-#ifdef LISP_FLOAT_TYPE
+/* Not "static" because of linker lossage on some systems */
+Lisp_Object *staticvec_nodump[200]
+     /* Force it into data space! */
+     = {0};
+static int staticidx_nodump;
 
-static Lisp_Object
-make_pure_float (double num)
+/* Put an entry in staticvec_nodump, pointing at the variable whose address is given
+ */
+void
+staticpro_nodump (Lisp_Object *varaddress)
 {
-  struct Lisp_Float *f;
-  Lisp_Object val;
-
-  /* Make sure that PUREBEG + pure_bytes_used is aligned on at least a sizeof
-     (double) boundary.  Some architectures (like the sparc) require
-     this, and I suspect that floats are rare enough that it's no
-     tragedy for those that don't. */
-  {
-#if defined (__GNUC__) && (__GNUC__ >= 2)
-    /* In gcc, we can directly ask what the alignment constraints of a
-       structure are, but in general, that's not possible...  Arrgh!!
-     */
-    int alignment = __alignof (struct Lisp_Float);
-#else /* !GNUC */
-    /* Best guess is to make the `double' slot be aligned to the size
-       of double (which is probably 8 bytes).  This assumes that it's
-       ok to align the beginning of the structure to the same boundary
-       that the `double' slot in it is supposed to be aligned to; this
-       should be ok because presumably there is padding in the layout
-       of the struct to account for this.
-     */
-    int alignment = sizeof (float_data (f));
-#endif /* !GNUC */
-    char *p = ((char *) PUREBEG + pure_bytes_used);
-
-    p = (char *) (((EMACS_UINT) p + alignment - 1) & - alignment);
-    pure_bytes_used = p - (char *) PUREBEG;
-  }
+  if (staticidx_nodump >= countof (staticvec_nodump))
+    /* #### This is now a dubious abort() since this routine may be called */
+    /* by Lisp attempting to load a DLL. */
+    abort ();
+  staticvec_nodump[staticidx_nodump++] = varaddress;
+}
 
-  if (!check_purespace (sizeof (struct Lisp_Float)))
-    return make_float (num);
+/* Not "static" because of linker lossage on some systems */
+struct {
+  void *data;
+  const struct struct_description *desc;
+} dumpstructvec[200];
 
-  f = (struct Lisp_Float *) (PUREBEG + pure_bytes_used);
-  set_lheader_implementation (&(f->lheader), lrecord_float);
-#ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
-  f->lheader.pure = 1;
-#endif
-  pure_bytes_used += sizeof (struct Lisp_Float);
-  bump_purestat (&purestat_float, sizeof (struct Lisp_Float));
+static int dumpstructidx;
 
-  float_data (f) = num;
-  XSETFLOAT (val, f);
-  return val;
+/* Put an entry in dumpstructvec, pointing at the variable whose address is given
+ */
+void
+dumpstruct (void *varaddress, const struct struct_description *desc)
+{
+  if (dumpstructidx >= countof (dumpstructvec))
+    abort ();
+  dumpstructvec[dumpstructidx].data = varaddress;
+  dumpstructvec[dumpstructidx].desc = desc;
+  dumpstructidx++;
 }
 
-#endif /* LISP_FLOAT_TYPE */
+Lisp_Object *pdump_wirevec[50];
+static int pdump_wireidx;
 
-Lisp_Object
-make_pure_vector (size_t len, Lisp_Object init)
+/* Put an entry in pdump_wirevec, pointing at the variable whose address is given
+ */
+void
+pdump_wire (Lisp_Object *varaddress)
 {
-  Lisp_Object new;
-  struct Lisp_Vector *v;
-  size_t size = (sizeof (struct Lisp_Vector)
-                + (len - 1) * sizeof (Lisp_Object));
-
-  init = Fpurecopy (init);
-
-  if (!check_purespace (size))
-    return make_vector (len, init);
-
-  v = (struct Lisp_Vector *) (PUREBEG + pure_bytes_used);
-#ifdef LRECORD_VECTOR
-  set_lheader_implementation (&(v->header.lheader), lrecord_vector);
-#ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
-  v->header.lheader.pure = 1;
-#endif
-#endif
-  pure_bytes_used += size;
-  bump_purestat (&purestat_vector_all, size);
-
-  v->size = len;
-
-  for (size = 0; size < len; size++)
-    v->contents[size] = init;
-
-  XSETVECTOR (new, v);
-  return new;
+  if (pdump_wireidx >= countof (pdump_wirevec))
+    abort ();
+  pdump_wirevec[pdump_wireidx++] = varaddress;
 }
 
-#if 0
-/* Presently unused */
-void *
-alloc_pure_lrecord (int size, struct lrecord_implementation *implementation)
-{
-  struct lrecord_header *header = (void *) (PUREBEG + pure_bytes_used);
 
-  if (pure_bytes_used + size > get_PURESIZE())
-    pure_storage_exhausted ();
+Lisp_Object *pdump_wirevec_list[50];
+static int pdump_wireidx_list;
 
-  set_lheader_implementation (header, implementation);
-  header->next = 0;
-  return header;
+/* Put an entry in pdump_wirevec_list, pointing at the variable whose address is given
+ */
+void
+pdump_wire_list (Lisp_Object *varaddress)
+{
+  if (pdump_wireidx_list >= countof (pdump_wirevec_list))
+    abort ();
+  pdump_wirevec_list[pdump_wireidx_list++] = varaddress;
 }
-#endif /* unused */
-
 
 \f
-DEFUN ("purecopy", Fpurecopy, 1, 1, 0, /*
-Make a copy of OBJECT in pure storage.
-Recursively copies contents of vectors and cons cells.
-Does not copy symbols.
-*/
-       (obj))
+/* Mark reference to a Lisp_Object.  If the object referred to has not been
+   seen yet, recursively mark all the references contained in it. */
+
+void
+mark_object (Lisp_Object obj)
 {
-  int i;
-  if (!purify_flag)
-    return obj;
+ tail_recurse:
 
-  if (!POINTER_TYPE_P (XTYPE (obj))
-      || PURIFIED (XPNTR (obj))
-      /* happens when bootstrapping Qnil */
-      || EQ (obj, Qnull_pointer))
-    return obj;
+#ifdef ERROR_CHECK_GC
+  assert (! (EQ (obj, Qnull_pointer)));
+#endif
+  /* Checks we used to perform */
+  /* if (EQ (obj, Qnull_pointer)) return; */
+  /* if (!POINTER_TYPE_P (XGCTYPE (obj))) return; */
+  /* if (PURIFIED (XPNTR (obj))) return; */
 
-  switch (XTYPE (obj))
+  if (XTYPE (obj) == Lisp_Type_Record)
     {
-#ifndef LRECORD_CONS
-    case Lisp_Type_Cons:
-      return pure_cons (XCAR (obj), XCDR (obj));
+      struct lrecord_header *lheader = XRECORD_LHEADER (obj);
+#if defined (ERROR_CHECK_GC)
+      assert (lheader->type <= last_lrecord_type_index_assigned);
 #endif
+      if (C_READONLY_RECORD_HEADER_P (lheader))
+       return;
 
-#ifndef LRECORD_STRING
-    case Lisp_Type_String:
-      return make_pure_string (XSTRING_DATA (obj),
-                              XSTRING_LENGTH (obj),
-                              XSTRING (obj)->plist,
-                               0);
-#endif /* ! LRECORD_STRING */
-
-#ifndef LRECORD_VECTOR
-    case Lisp_Type_Vector:
-      {
-        struct Lisp_Vector *o = XVECTOR (obj);
-        Lisp_Object new = make_pure_vector (vector_length (o), Qnil);
-        for (i = 0; i < vector_length (o); i++)
-         XVECTOR_DATA (new)[i] = Fpurecopy (o->contents[i]);
-        return new;
-      }
-#endif /* !LRECORD_VECTOR */
-
-    default:
-      {
-        if (COMPILED_FUNCTIONP (obj))
-          {
-            struct Lisp_Compiled_Function *o = XCOMPILED_FUNCTION (obj);
-            Lisp_Object new = make_compiled_function (1);
-           /* How on earth could this code have worked before?  -sb */
-            struct Lisp_Compiled_Function *n = XCOMPILED_FUNCTION (new);
-            n->flags = o->flags;
-            n->bytecodes = Fpurecopy (o->bytecodes);
-            n->constants = Fpurecopy (o->constants);
-            n->arglist = Fpurecopy (o->arglist);
-            n->doc_and_interactive = Fpurecopy (o->doc_and_interactive);
-           n->maxdepth = o->maxdepth;
-            return new;
-          }
-#ifdef LRECORD_CONS
-      else if (CONSP (obj))
-       return pure_cons (XCAR (obj), XCDR (obj));
-#endif /* LRECORD_CONS */
-#ifdef LRECORD_VECTOR
-      else if (VECTORP (obj))
-       {
-         struct Lisp_Vector *o = XVECTOR (obj);
-         Lisp_Object new = make_pure_vector (vector_length (o), Qnil);
-         for (i = 0; i < vector_length (o); i++)
-           XVECTOR_DATA (new)[i] = Fpurecopy (o->contents[i]);
-         return new;
-       }
-#endif /* LRECORD_VECTOR */
-#ifdef LRECORD_STRING
-      else if (STRINGP (obj))
+      if (! MARKED_RECORD_HEADER_P (lheader) &&
+         ! UNMARKABLE_RECORD_HEADER_P (lheader))
        {
-         return make_pure_string (XSTRING_DATA (obj),
-                                  XSTRING_LENGTH (obj),
-                                  XSTRING (obj)->plist,
-                                  0);
+         CONST struct lrecord_implementation *implementation =
+           LHEADER_IMPLEMENTATION (lheader);
+         MARK_RECORD_HEADER (lheader);
+#ifdef ERROR_CHECK_GC
+         if (!implementation->basic_p)
+           assert (! ((struct lcrecord_header *) lheader)->free);
+#endif
+         if (implementation->marker)
+           {
+             obj = implementation->marker (obj);
+             if (!NILP (obj)) goto tail_recurse;
+           }
        }
-#endif /* LRECORD_STRING */
-#ifdef LISP_FLOAT_TYPE
-        else if (FLOATP (obj))
-          return make_pure_float (float_data (XFLOAT (obj)));
-#endif /* LISP_FLOAT_TYPE */
-       else if (SYMBOLP (obj))
-         {
-           /*
-            * Symbols can't be made pure (and thus read-only),
-            * because assigning to their function, value or plist
-            * slots would produced a SEGV in the dumped XEmacs.  So
-            * we previously would just return the symbol unchanged.
-            *
-            * But purified aggregate objects like lists and vectors
-            * can contain uninterned symbols.  If there are no
-            * other non-pure references to the symbol, then the
-            * symbol is not protected from garbage collection
-            * because the collector does not mark the contents of
-            * purified objects.  So to protect the symbols, an impure
-            * reference has to be kept for each uninterned symbol
-            * that is referenced by a pure object.  All such
-            * symbols are stored in the hashtable pointed to by
-            * Vpure_uninterned_symbol_table, which is itself
-            * staticpro'd.
-            */
-           if (!NILP (XSYMBOL (obj)->obarray))
-             return obj;
-           Fputhash (obj, Qnil, Vpure_uninterned_symbol_table);
-           return obj;
-         }
-       else
-          signal_simple_error ("Can't purecopy %S", obj);
-      }
     }
-  return obj;
 }
 
+/* mark all of the conses in a list and mark the final cdr; but
+   DO NOT mark the cars.
 
-\f
-static void
-puresize_adjust_h (size_t puresize)
-{
-  FILE *stream = fopen ("puresize-adjust.h", "w");
-
-  if (stream == NULL)
-    report_file_error ("Opening puresize adjustment file",
-                      Fcons (build_string ("puresize-adjust.h"), Qnil));
-
-  fprintf (stream,
-          "/*\tDo not edit this file!\n"
-          "\tAutomatically generated by XEmacs */\n"
-          "# define PURESIZE_ADJUSTMENT (%ld)\n",
-          (long) (puresize - RAW_PURESIZE));
-  fclose (stream);
-}
-
+   Use only for internal lists!  There should never be other pointers
+   to the cons cells, because if so, the cars will remain unmarked
+   even when they maybe should be marked. */
 void
-report_pure_usage (int report_impurities,
-                   int die_if_pure_storage_exceeded)
+mark_conses_in_list (Lisp_Object obj)
 {
-  int rc = 0;
+  Lisp_Object rest;
 
-  if (pure_lossage)
-    {
-      message ("\n****\tPure Lisp storage exhausted!\n"
-              "\tPurespace usage: %ld of %ld\n"
-              "****",
-               (long) get_PURESIZE() + pure_lossage,
-              (long) get_PURESIZE());
-      if (die_if_pure_storage_exceeded)
-       {
-         puresize_adjust_h (get_PURESIZE() + pure_lossage);
-#ifdef HEAP_IN_DATA
-         sheap_adjust_h();
-#endif
-         rc = -1;
-       }
-    }
-  else
+  for (rest = obj; CONSP (rest); rest = XCDR (rest))
     {
-      size_t lost = (get_PURESIZE() - pure_bytes_used) / 1024;
-      char buf[200];
-      /* extern Lisp_Object Vemacs_beta_version; */
-      /* This used to be NILP(Vemacs_beta_version) ? 512 : 4; */
-#ifndef PURESIZE_SLOP
-#define PURESIZE_SLOP 0
-#endif
-      size_t slop = PURESIZE_SLOP;
-
-      sprintf (buf, "Purespace usage: %ld of %ld (%d%%",
-               (long) pure_bytes_used,
-              (long) get_PURESIZE(),
-               (int) (pure_bytes_used / (get_PURESIZE() / 100.0) + 0.5));
-      if (lost > ((slop ? slop : 1) / 1024)) {
-        sprintf (buf + strlen (buf), " -- %ldk wasted", (long)lost);
-       if (die_if_pure_storage_exceeded) {
-         puresize_adjust_h (pure_bytes_used + slop);
-#ifdef HEAP_IN_DATA
-         sheap_adjust_h();
-#endif
-         rc = -1;
-       }
-      }
-
-      strcat (buf, ").");
-      message ("%s", buf);
+      if (CONS_MARKED_P (XCONS (rest)))
+       return;
+      MARK_CONS (XCONS (rest));
     }
 
-#ifdef PURESTAT
-
-  purestat_vector_other.nbytes =
-    purestat_vector_all.nbytes -
-    purestat_vector_bytecode_constants.nbytes;
-  purestat_vector_other.nobjects =
-    purestat_vector_all.nobjects -
-    purestat_vector_bytecode_constants.nobjects;
-
-  purestat_string_other.nbytes =
-    purestat_string_all.nbytes -
-    (purestat_string_pname.nbytes +
-     purestat_string_bytecodes.nbytes +
-     purestat_string_interactive.nbytes +
-     purestat_string_documentation.nbytes +
-#ifdef I18N3
-     purestat_string_domain.nbytes +
-#endif
-     purestat_string_other_function.nbytes);
-
-  purestat_string_other.nobjects =
-    purestat_string_all.nobjects -
-    (purestat_string_pname.nobjects +
-     purestat_string_bytecodes.nobjects +
-     purestat_string_interactive.nobjects +
-     purestat_string_documentation.nobjects +
-#ifdef I18N3
-     purestat_string_domain.nobjects +
-#endif
-     purestat_string_other_function.nobjects);
-
-  message ("   %-26s Total    Bytes", "");
+  mark_object (rest);
+}
 
-  {
-    int j;
+\f
+/* Find all structures not marked, and free them. */
 
-    for (j = 0; j < countof (purestats); j++)
-      if (!purestats[j])
-        clear_message ();
-      else
-       {
-         char buf [100];
-         sprintf(buf, "%s:", purestats[j]->name);
-         message ("   %-26s %5d  %7d  %2d%%",
-                  buf,
-                  purestats[j]->nobjects,
-                  purestats[j]->nbytes,
-                  (int) (purestats[j]->nbytes / (pure_bytes_used / 100.0) + 0.5));
-       }
-  }
-#endif /* PURESTAT */
+static int gc_count_num_bit_vector_used, gc_count_bit_vector_total_size;
+static int gc_count_bit_vector_storage;
+static int gc_count_num_short_string_in_use;
+static int gc_count_string_total_size;
+static int gc_count_short_string_total_size;
 
-
-  if (report_impurities)
-    {
-      Lisp_Object tem = Felt (Fgarbage_collect (), make_int (5));
-      struct gcpro gcpro1;
-      GCPRO1 (tem);
-      message ("\nImpurities:");
-      while (!NILP (tem))
-       {
-         if (CONSP (tem) && SYMBOLP (Fcar (tem)) && CONSP (Fcdr (tem)))
-           {
-             int total = XINT (Fcar (Fcdr (tem)));
-             if (total > 0)
-               {
-                 char buf [100];
-                 char *s = buf;
-                 memcpy (buf, string_data (XSYMBOL (Fcar (tem))->name),
-                         string_length (XSYMBOL (Fcar (tem))->name) + 1);
-                 while (*s++) if (*s == '-') *s = ' ';
-                 s--; *s++ = ':'; *s = 0;
-                 message ("   %-33s %6d", buf, total);
-               }
-             tem = Fcdr (Fcdr (tem));
-           }
-         else                  /* WTF?! */
-           {
-             Fprin1 (tem, Qexternal_debugging_output);
-             tem = Qnil;
-           }
-       }
-      UNGCPRO;
-      garbage_collect_1 ();         /* GC garbage_collect's garbage */
-    }
-  clear_message ();
-
-  if (rc < 0) {
-    unlink("SATISFIED");
-    fatal ("Pure size adjusted, Don't Panic!  I will restart the `make'");
-  } else if (pure_lossage && die_if_pure_storage_exceeded) {
-    fatal ("Pure storage exhausted");
-  }
-}
-
-\f
-/**********************************************************************/
-/*                            staticpro                               */
-/**********************************************************************/
-
-struct gcpro *gcprolist;
-
-/* 415 used Mly 29-Jun-93 */
-/* 1327 used slb 28-Feb-98 */
-#ifdef HAVE_SHLIB
-#define NSTATICS 4000
-#else
-#define NSTATICS 2000
-#endif
-/* Not "static" because of linker lossage on some systems */
-Lisp_Object *staticvec[NSTATICS]
-     /* Force it into data space! */
-     = {0};
-static int staticidx;
-
-/* Put an entry in staticvec, pointing at the variable whose address is given
- */
-void
-staticpro (Lisp_Object *varaddress)
-{
-  if (staticidx >= countof (staticvec))
-    /* #### This is now a dubious abort() since this routine may be called */
-    /* by Lisp attempting to load a DLL. */
-    abort ();
-  staticvec[staticidx++] = varaddress;
-}
-
-\f
-/* Mark reference to a Lisp_Object.  If the object referred to has not been
-   seen yet, recursively mark all the references contained in it. */
-
-static void
-mark_object (Lisp_Object obj)
-{
- tail_recurse:
-
-  if (EQ (obj, Qnull_pointer))
-    return;
-  if (!POINTER_TYPE_P (XGCTYPE (obj)))
-    return;
-  if (PURIFIED (XPNTR (obj)))
-    return;
-  switch (XGCTYPE (obj))
-    {
-#ifndef LRECORD_CONS
-    case Lisp_Type_Cons:
-      {
-       struct Lisp_Cons *ptr = XCONS (obj);
-       if (CONS_MARKED_P (ptr))
-         break;
-       MARK_CONS (ptr);
-       /* If the cdr is nil, tail-recurse on the car.  */
-       if (NILP (ptr->cdr))
-         {
-           obj = ptr->car;
-         }
-       else
-         {
-           mark_object (ptr->car);
-           obj = ptr->cdr;
-         }
-       goto tail_recurse;
-      }
-#endif
-
-    case Lisp_Type_Record:
-    /* case Lisp_Symbol_Value_Magic: */
-      {
-       struct lrecord_header *lheader = XRECORD_LHEADER (obj);
-       CONST struct lrecord_implementation *implementation
-         = LHEADER_IMPLEMENTATION (lheader);
-
-       if (! MARKED_RECORD_HEADER_P (lheader) &&
-           ! UNMARKABLE_RECORD_HEADER_P (lheader))
-         {
-           MARK_RECORD_HEADER (lheader);
-#ifdef ERROR_CHECK_GC
-           if (!implementation->basic_p)
-             assert (! ((struct lcrecord_header *) lheader)->free);
-#endif
-           if (implementation->marker != 0)
-             {
-               obj = ((implementation->marker) (obj, mark_object));
-               if (!NILP (obj)) goto tail_recurse;
-             }
-         }
-      }
-      break;
-
-#ifndef LRECORD_STRING
-    case Lisp_Type_String:
-      {
-       struct Lisp_String *ptr = XSTRING (obj);
-
-       if (!XMARKBIT (ptr->plist))
-         {
-           if (CONSP (ptr->plist) &&
-               EXTENT_INFOP (XCAR (ptr->plist)))
-             flush_cached_extent_info (XCAR (ptr->plist));
-           XMARK (ptr->plist);
-           obj = ptr->plist;
-           goto tail_recurse;
-         }
-      }
-      break;
-#endif /* ! LRECORD_STRING */
-
-#ifndef LRECORD_VECTOR
-    case Lisp_Type_Vector:
-      {
-       struct Lisp_Vector *ptr = XVECTOR (obj);
-       int len = vector_length (ptr);
-       int i;
-
-       if (len < 0)
-         break;                /* Already marked */
-       ptr->size = -1 - len;   /* Else mark it */
-       for (i = 0; i < len - 1; i++) /* and then mark its elements */
-         mark_object (ptr->contents[i]);
-        if (len > 0)
-        {
-          obj = ptr->contents[len - 1];
-          goto tail_recurse;
-        }
-      }
-      break;
-#endif /* !LRECORD_VECTOR */
-
-#ifndef LRECORD_SYMBOL
-    case Lisp_Type_Symbol:
-      {
-       struct Lisp_Symbol *sym = XSYMBOL (obj);
-
-       while (!XMARKBIT (sym->plist))
-         {
-           XMARK (sym->plist);
-           mark_object (sym->value);
-           mark_object (sym->function);
-           {
-             /*
-              * symbol->name is a struct Lisp_String *, not a
-              * Lisp_Object.  Fix it up and pass to mark_object.
-              */
-             Lisp_Object symname;
-             XSETSTRING(symname, sym->name);
-             mark_object(symname);
-           }
-           if (!symbol_next (sym))
-             {
-               obj = sym->plist;
-               goto tail_recurse;
-             }
-           mark_object (sym->plist);
-           /* Mark the rest of the symbols in the hash-chain */
-           sym = symbol_next (sym);
-         }
-      }
-      break;
-#endif /* !LRECORD_SYMBOL */
-
-    default:
-      abort ();
-    }
-}
-
-/* mark all of the conses in a list and mark the final cdr; but
-   DO NOT mark the cars.
-
-   Use only for internal lists!  There should never be other pointers
-   to the cons cells, because if so, the cars will remain unmarked
-   even when they maybe should be marked. */
-void
-mark_conses_in_list (Lisp_Object obj)
-{
-  Lisp_Object rest;
-
-  for (rest = obj; CONSP (rest); rest = XCDR (rest))
-    {
-      if (CONS_MARKED_P (XCONS (rest)))
-       return;
-      MARK_CONS (XCONS (rest));
-    }
-
-  mark_object (rest);
-}
-
-\f
-#ifdef PURESTAT
-/* Simpler than mark-object, because pure structure can't
-   have any circularities */
-
-#if 0 /* unused */
-static int idiot_c_doesnt_have_closures;
-static void
-idiot_c (Lisp_Object obj)
-{
-  idiot_c_doesnt_have_closures += pure_sizeof (obj, 1);
-}
-#endif /* unused */
-
-static size_t
-pure_string_sizeof (Lisp_Object obj)
-{
-  struct Lisp_String *ptr = XSTRING (obj);
-
-  if (string_data (ptr) != (Bufbyte *) ptr + sizeof (*ptr))
-    {
-      /* string-data not allocated contiguously.
-        Probably (better be!!) a pointer constant "C" data. */
-      return sizeof (*ptr);
-    }
-  else
-    {
-      size_t size = sizeof (*ptr) + string_length (ptr) + 1;
-      size = ALIGN_SIZE (size, sizeof (Lisp_Object));
-      return size;
-    }
-}
-
-/* recurse arg isn't actually used */
-static size_t
-pure_sizeof (Lisp_Object obj /*, int recurse */)
-{
-  size_t total = 0;
-
-  /*tail_recurse: */
-  if (!POINTER_TYPE_P (XTYPE (obj))
-      || !PURIFIED (XPNTR (obj)))
-    return total;
-
-  /* symbol's sizes are accounted for separately */
-  if (SYMBOLP (obj))
-    return total;
-
-  switch (XTYPE (obj))
-    {
-
-#ifndef LRECORD_STRING
-    case Lisp_Type_String:
-      total += pure_string_sizeof (obj);
-      break;
-#endif /* ! LRECORD_STRING */
-
-#ifndef LRECORD_VECTOR
-    case Lisp_Type_Vector:
-      {
-        struct Lisp_Vector *ptr = XVECTOR (obj);
-        int len = vector_length (ptr);
-
-        total += (sizeof (struct Lisp_Vector)
-                  + (len - 1) * sizeof (Lisp_Object));
-#if 0 /* unused */
-        if (!recurse)
-          break;
-        {
-          int i;
-         for (i = 0; i < len - 1; i++)
-           total += pure_sizeof (ptr->contents[i], 1);
-       }
-        if (len > 0)
-         {
-           obj = ptr->contents[len - 1];
-           goto tail_recurse;
-         }
-#endif /* unused */
-      }
-      break;
-#endif /* !LRECORD_VECTOR */
-
-    case Lisp_Type_Record:
-      {
-       struct lrecord_header *lheader = XRECORD_LHEADER (obj);
-       CONST struct lrecord_implementation *implementation
-         = LHEADER_IMPLEMENTATION (lheader);
-
-#ifdef LRECORD_STRING
-       if (STRINGP (obj))
-         total += pure_string_sizeof (obj);
-       else
-#endif
-        if (implementation->size_in_bytes_method)
-          total += ((implementation->size_in_bytes_method) (lheader));
-       else
-          total += implementation->static_size;
-
-#if 0 /* unused */
-        if (!recurse)
-          break;
-
-       if (implementation->marker != 0)
-         {
-           int old = idiot_c_doesnt_have_closures;
-
-           idiot_c_doesnt_have_closures = 0;
-           obj = ((implementation->marker) (obj, idiot_c));
-           total += idiot_c_doesnt_have_closures;
-           idiot_c_doesnt_have_closures = old;
-
-           if (!NILP (obj)) goto tail_recurse;
-         }
-#endif /* unused */
-      }
-      break;
-
-#ifndef LRECORD_CONS
-    case Lisp_Type_Cons:
-      {
-        struct Lisp_Cons *ptr = XCONS (obj);
-        total += sizeof (*ptr);
-#if 0 /* unused */
-        if (!recurse)
-          break;
-       /* If the cdr is nil, tail-recurse on the car.  */
-       if (NILP (ptr->cdr))
-         {
-           obj = ptr->car;
-         }
-       else
-         {
-           total += pure_sizeof (ptr->car, 1);
-           obj = ptr->cdr;
-         }
-       goto tail_recurse;
-#endif /* unused */
-      }
-      break;
-#endif
-
-      /* Others can't be purified */
-    default:
-      abort ();
-    }
-  return total;
-}
-#endif /* PURESTAT */
-
-
-
-\f
-/* Find all structures not marked, and free them. */
-
-#ifndef LRECORD_VECTOR
-static int gc_count_num_vector_used, gc_count_vector_total_size;
-static int gc_count_vector_storage;
-#endif
-static int gc_count_num_bit_vector_used, gc_count_bit_vector_total_size;
-static int gc_count_bit_vector_storage;
-static int gc_count_num_short_string_in_use;
-static int gc_count_string_total_size;
-static int gc_count_short_string_total_size;
-
-/* static int gc_count_total_records_used, gc_count_records_total_size; */
+/* static int gc_count_total_records_used, gc_count_records_total_size; */
 
 \f
-/* This will be used more extensively In The Future */
-static int last_lrecord_type_index_assigned;
-
-CONST struct lrecord_implementation *lrecord_implementations_table[128];
-#define max_lrecord_type (countof (lrecord_implementations_table) - 1)
-
 int
 lrecord_type_index (CONST struct lrecord_implementation *implementation)
 {
@@ -3463,8 +2540,7 @@ lrecord_type_index (CONST struct lrecord_implementation *implementation)
   if (type_index < 0 || type_index > max_lrecord_type
       || lrecord_implementations_table[type_index] != implementation)
     {
-      if (last_lrecord_type_index_assigned == max_lrecord_type)
-        abort ();
+      assert (last_lrecord_type_index_assigned < max_lrecord_type);
       type_index = ++last_lrecord_type_index_assigned;
       lrecord_implementations_table[type_index] = implementation;
       *(implementation->lrecord_type_index) = type_index;
@@ -3483,21 +2559,6 @@ static struct
   int instances_on_free_list;
 } lcrecord_stats [countof (lrecord_implementations_table)];
 
-
-static void
-reset_lcrecord_stats (void)
-{
-  int i;
-  for (i = 0; i < countof (lcrecord_stats); i++)
-    {
-      lcrecord_stats[i].instances_in_use = 0;
-      lcrecord_stats[i].bytes_in_use = 0;
-      lcrecord_stats[i].instances_freed = 0;
-      lcrecord_stats[i].bytes_freed = 0;
-      lcrecord_stats[i].instances_on_free_list = 0;
-    }
-}
-
 static void
 tick_lcrecord_stats (CONST struct lrecord_header *h, int free_p)
 {
@@ -3513,7 +2574,7 @@ tick_lcrecord_stats (CONST struct lrecord_header *h, int free_p)
   else
     {
       size_t sz = (implementation->size_in_bytes_method
-                  ? ((implementation->size_in_bytes_method) (h))
+                  ? implementation->size_in_bytes_method (h)
                   : implementation->static_size);
 
       if (free_p)
@@ -3537,7 +2598,8 @@ sweep_lcrecords_1 (struct lcrecord_header **prev, int *used)
   struct lcrecord_header *header;
   int num_used = 0;
   /* int total_size = 0; */
-  reset_lcrecord_stats ();
+
+  xzero (lcrecord_stats); /* Reset all statistics to 0. */
 
   /* First go through and call all the finalize methods.
      Then go through and free the objects.  There used to
@@ -3552,21 +2614,25 @@ sweep_lcrecords_1 (struct lcrecord_header **prev, int *used)
   for (header = *prev; header; header = header->next)
     {
       struct lrecord_header *h = &(header->lheader);
-      if (!MARKED_RECORD_HEADER_P (h) && ! (header->free))
+      if (!C_READONLY_RECORD_HEADER_P(h)
+         && !MARKED_RECORD_HEADER_P (h)
+         && ! (header->free))
        {
          if (LHEADER_IMPLEMENTATION (h)->finalizer)
-           ((LHEADER_IMPLEMENTATION (h)->finalizer) (h, 0));
+           LHEADER_IMPLEMENTATION (h)->finalizer (h, 0);
        }
     }
 
   for (header = *prev; header; )
     {
       struct lrecord_header *h = &(header->lheader);
-      if (MARKED_RECORD_HEADER_P (h))
+      if (C_READONLY_RECORD_HEADER_P(h) || MARKED_RECORD_HEADER_P (h))
        {
-         UNMARK_RECORD_HEADER (h);
+         if (MARKED_RECORD_HEADER_P (h))
+           UNMARK_RECORD_HEADER (h);
          num_used++;
-         /* total_size += ((n->implementation->size_in_bytes) (h));*/
+         /* total_size += n->implementation->size_in_bytes (h);*/
+         /* ### May modify header->next on a C_READONLY lcrecord */
          prev = &(header->next);
          header = *prev;
          tick_lcrecord_stats (h, 0);
@@ -3585,47 +2651,6 @@ sweep_lcrecords_1 (struct lcrecord_header **prev, int *used)
   /* *total = total_size; */
 }
 
-#ifndef LRECORD_VECTOR
-
-static void
-sweep_vectors_1 (Lisp_Object *prev,
-                 int *used, int *total, int *storage)
-{
-  Lisp_Object vector;
-  int num_used = 0;
-  int total_size = 0;
-  int total_storage = 0;
-
-  for (vector = *prev; VECTORP (vector); )
-    {
-      struct Lisp_Vector *v = XVECTOR (vector);
-      int len = v->size;
-      if (len < 0)     /* marked */
-       {
-          len = - (len + 1);
-         v->size = len;
-         total_size += len;
-          total_storage += (MALLOC_OVERHEAD
-                            + sizeof (struct Lisp_Vector)
-                            + (len - 1 + 1) * sizeof (Lisp_Object));
-         num_used++;
-         prev = &(vector_next (v));
-         vector = *prev;
-       }
-      else
-       {
-          Lisp_Object next = vector_next (v);
-          *prev = next;
-         xfree (v);
-         vector = next;
-       }
-    }
-  *used = num_used;
-  *total = total_size;
-  *storage = total_storage;
-}
-
-#endif /* ! LRECORD_VECTOR */
 
 static void
 sweep_bit_vectors_1 (Lisp_Object *prev,
@@ -3640,17 +2665,19 @@ sweep_bit_vectors_1 (Lisp_Object *prev,
      their implementation */
   for (bit_vector = *prev; !EQ (bit_vector, Qzero); )
     {
-      struct Lisp_Bit_Vector *v = XBIT_VECTOR (bit_vector);
+      Lisp_Bit_Vector *v = XBIT_VECTOR (bit_vector);
       int len = v->size;
-      if (MARKED_RECORD_P (bit_vector))
+      if (C_READONLY_RECORD_HEADER_P(&(v->lheader)) || MARKED_RECORD_P (bit_vector))
        {
-         UNMARK_RECORD_HEADER (&(v->lheader));
+         if (MARKED_RECORD_P (bit_vector))
+           UNMARK_RECORD_HEADER (&(v->lheader));
          total_size += len;
-          total_storage += (MALLOC_OVERHEAD
-                            + sizeof (struct Lisp_Bit_Vector)
-                            + (BIT_VECTOR_LONG_STORAGE (len) - 1)
-                           * sizeof (long));
+          total_storage +=
+           MALLOC_OVERHEAD +
+           STRETCHY_STRUCT_SIZEOF (Lisp_Bit_Vector, bits,
+                                   BIT_VECTOR_LONG_STORAGE (len));
          num_used++;
+         /* ### May modify next on a C_READONLY bitvector */
          prev = &(bit_vector_next (v));
          bit_vector = *prev;
        }
@@ -3674,41 +2701,45 @@ sweep_bit_vectors_1 (Lisp_Object *prev,
 
 #define SWEEP_FIXED_TYPE_BLOCK(typename, obj_type)                     \
 do {                                                                   \
-  struct typename##_block *_frob_current;                              \
-  struct typename##_block **_frob_prev;                                        \
-  int _frob_limit;                                                     \
+  struct typename##_block *SFTB_current;                               \
+  struct typename##_block **SFTB_prev;                                 \
+  int SFTB_limit;                                                      \
   int num_free = 0, num_used = 0;                                      \
                                                                        \
-  for (_frob_prev = &current_##typename##_block,                       \
-       _frob_current = current_##typename##_block,                     \
-       _frob_limit = current_##typename##_block_index;                 \
-       _frob_current;                                                  \
+  for (SFTB_prev = &current_##typename##_block,                                \
+       SFTB_current = current_##typename##_block,                      \
+       SFTB_limit = current_##typename##_block_index;                  \
+       SFTB_current;                                                   \
        )                                                               \
     {                                                                  \
-      int _frob_iii;                                                   \
+      int SFTB_iii;                                                    \
                                                                        \
-      for (_frob_iii = 0; _frob_iii < _frob_limit; _frob_iii++)                \
+      for (SFTB_iii = 0; SFTB_iii < SFTB_limit; SFTB_iii++)            \
        {                                                               \
-         obj_type *_frob_victim = &(_frob_current->block[_frob_iii]);  \
+         obj_type *SFTB_victim = &(SFTB_current->block[SFTB_iii]);     \
                                                                        \
-         if (FREE_STRUCT_P (_frob_victim))                             \
+         if (FREE_STRUCT_P (SFTB_victim))                              \
            {                                                           \
              num_free++;                                               \
            }                                                           \
-         else if (!MARKED_##typename##_P (_frob_victim))               \
+         else if (C_READONLY_RECORD_HEADER_P (&SFTB_victim->lheader))  \
+           {                                                           \
+             num_used++;                                               \
+           }                                                           \
+         else if (!MARKED_RECORD_HEADER_P (&SFTB_victim->lheader))     \
            {                                                           \
              num_free++;                                               \
-             FREE_FIXED_TYPE (typename, obj_type, _frob_victim);       \
+             FREE_FIXED_TYPE (typename, obj_type, SFTB_victim);        \
            }                                                           \
          else                                                          \
            {                                                           \
              num_used++;                                               \
-             UNMARK_##typename (_frob_victim);                         \
+             UNMARK_##typename (SFTB_victim);                          \
            }                                                           \
        }                                                               \
-      _frob_prev = &(_frob_current->prev);                             \
-      _frob_current = _frob_current->prev;                             \
-      _frob_limit = countof (current_##typename##_block->block);       \
+      SFTB_prev = &(SFTB_current->prev);                               \
+      SFTB_current = SFTB_current->prev;                               \
+      SFTB_limit = countof (current_##typename##_block->block);                \
     }                                                                  \
                                                                        \
   gc_count_num_##typename##_in_use = num_used;                         \
@@ -3717,77 +2748,82 @@ do {                                                                    \
 
 #else /* !ERROR_CHECK_GC */
 
-#define SWEEP_FIXED_TYPE_BLOCK(typename, obj_type)                           \
-do {                                                                         \
-  struct typename##_block *_frob_current;                                    \
-  struct typename##_block **_frob_prev;                                              \
-  int _frob_limit;                                                           \
-  int num_free = 0, num_used = 0;                                            \
-                                                                             \
-  typename##_free_list = 0;                                                  \
-                                                                             \
-  for (_frob_prev = &current_##typename##_block,                             \
-       _frob_current = current_##typename##_block,                           \
-       _frob_limit = current_##typename##_block_index;                       \
-       _frob_current;                                                        \
-       )                                                                     \
-    {                                                                        \
-      int _frob_iii;                                                         \
-      int _frob_empty = 1;                                                   \
-      obj_type *_frob_old_free_list = typename##_free_list;                  \
-                                                                             \
-      for (_frob_iii = 0; _frob_iii < _frob_limit; _frob_iii++)                      \
-       {                                                                     \
-         obj_type *_frob_victim = &(_frob_current->block[_frob_iii]);        \
-                                                                             \
-         if (FREE_STRUCT_P (_frob_victim))                                   \
-           {                                                                 \
-             num_free++;                                                     \
-             PUT_FIXED_TYPE_ON_FREE_LIST (typename, obj_type, _frob_victim); \
-           }                                                                 \
-         else if (!MARKED_##typename##_P (_frob_victim))                     \
-           {                                                                 \
-             num_free++;                                                     \
-             FREE_FIXED_TYPE (typename, obj_type, _frob_victim);             \
-           }                                                                 \
-         else                                                                \
-           {                                                                 \
-             _frob_empty = 0;                                                \
-             num_used++;                                                     \
-             UNMARK_##typename (_frob_victim);                               \
-           }                                                                 \
-       }                                                                     \
-      if (!_frob_empty)                                                              \
-       {                                                                     \
-         _frob_prev = &(_frob_current->prev);                                \
-         _frob_current = _frob_current->prev;                                \
-       }                                                                     \
-      else if (_frob_current == current_##typename##_block                   \
-              && !_frob_current->prev)                                       \
-       {                                                                     \
-         /* No real point in freeing sole allocation block */                \
-         break;                                                              \
-       }                                                                     \
-      else                                                                   \
-       {                                                                     \
-         struct typename##_block *_frob_victim_block = _frob_current;        \
-         if (_frob_victim_block == current_##typename##_block)               \
-           current_##typename##_block_index                                  \
-             = countof (current_##typename##_block->block);                  \
-         _frob_current = _frob_current->prev;                                \
-         {                                                                   \
-           *_frob_prev = _frob_current;                                      \
-           xfree (_frob_victim_block);                                       \
-           /* Restore free list to what it was before victim was swept */    \
-           typename##_free_list = _frob_old_free_list;                       \
-           num_free -= _frob_limit;                                          \
-         }                                                                   \
-       }                                                                     \
-      _frob_limit = countof (current_##typename##_block->block);             \
-    }                                                                        \
-                                                                             \
-  gc_count_num_##typename##_in_use = num_used;                               \
-  gc_count_num_##typename##_freelist = num_free;                             \
+#define SWEEP_FIXED_TYPE_BLOCK(typename, obj_type)                             \
+do {                                                                           \
+  struct typename##_block *SFTB_current;                                       \
+  struct typename##_block **SFTB_prev;                                         \
+  int SFTB_limit;                                                              \
+  int num_free = 0, num_used = 0;                                              \
+                                                                               \
+  typename##_free_list = 0;                                                    \
+                                                                               \
+  for (SFTB_prev = &current_##typename##_block,                                        \
+       SFTB_current = current_##typename##_block,                              \
+       SFTB_limit = current_##typename##_block_index;                          \
+       SFTB_current;                                                           \
+       )                                                                       \
+    {                                                                          \
+      int SFTB_iii;                                                            \
+      int SFTB_empty = 1;                                                      \
+      obj_type *SFTB_old_free_list = typename##_free_list;                     \
+                                                                               \
+      for (SFTB_iii = 0; SFTB_iii < SFTB_limit; SFTB_iii++)                    \
+       {                                                                       \
+         obj_type *SFTB_victim = &(SFTB_current->block[SFTB_iii]);             \
+                                                                               \
+         if (FREE_STRUCT_P (SFTB_victim))                                      \
+           {                                                                   \
+             num_free++;                                                       \
+             PUT_FIXED_TYPE_ON_FREE_LIST (typename, obj_type, SFTB_victim);    \
+           }                                                                   \
+         else if (C_READONLY_RECORD_HEADER_P (&SFTB_victim->lheader))          \
+           {                                                                   \
+             SFTB_empty = 0;                                                   \
+             num_used++;                                                       \
+           }                                                                   \
+         else if (!MARKED_RECORD_HEADER_P (&SFTB_victim->lheader))             \
+           {                                                                   \
+             num_free++;                                                       \
+             FREE_FIXED_TYPE (typename, obj_type, SFTB_victim);                \
+           }                                                                   \
+         else                                                                  \
+           {                                                                   \
+             SFTB_empty = 0;                                                   \
+             num_used++;                                                       \
+             UNMARK_##typename (SFTB_victim);                                  \
+           }                                                                   \
+       }                                                                       \
+      if (!SFTB_empty)                                                         \
+       {                                                                       \
+         SFTB_prev = &(SFTB_current->prev);                                    \
+         SFTB_current = SFTB_current->prev;                                    \
+       }                                                                       \
+      else if (SFTB_current == current_##typename##_block                      \
+              && !SFTB_current->prev)                                          \
+       {                                                                       \
+         /* No real point in freeing sole allocation block */                  \
+         break;                                                                \
+       }                                                                       \
+      else                                                                     \
+       {                                                                       \
+         struct typename##_block *SFTB_victim_block = SFTB_current;            \
+         if (SFTB_victim_block == current_##typename##_block)                  \
+           current_##typename##_block_index                                    \
+             = countof (current_##typename##_block->block);                    \
+         SFTB_current = SFTB_current->prev;                                    \
+         {                                                                     \
+           *SFTB_prev = SFTB_current;                                          \
+           xfree (SFTB_victim_block);                                          \
+           /* Restore free list to what it was before victim was swept */      \
+           typename##_free_list = SFTB_old_free_list;                          \
+           num_free -= SFTB_limit;                                             \
+         }                                                                     \
+       }                                                                       \
+      SFTB_limit = countof (current_##typename##_block->block);                        \
+    }                                                                          \
+                                                                               \
+  gc_count_num_##typename##_in_use = num_used;                                 \
+  gc_count_num_##typename##_freelist = num_free;                               \
 } while (0)
 
 #endif /* !ERROR_CHECK_GC */
@@ -3798,13 +2834,7 @@ do {                                                                           \
 static void
 sweep_conses (void)
 {
-#ifndef LRECORD_CONS
-# define MARKED_cons_P(ptr) XMARKBIT ((ptr)->car)
-# define UNMARK_cons(ptr) do { XUNMARK ((ptr)->car); } while (0)
-#else /* LRECORD_CONS */
-# define MARKED_cons_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
-# define UNMARK_cons(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
-#endif /* LRECORD_CONS */
+#define UNMARK_cons(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
 #define ADDITIONAL_FREE_cons(ptr)
 
   SWEEP_FIXED_TYPE_BLOCK (cons, struct Lisp_Cons);
@@ -3868,12 +2898,10 @@ free_alist (Lisp_Object alist)
 static void
 sweep_compiled_functions (void)
 {
-#define MARKED_compiled_function_P(ptr) \
-  MARKED_RECORD_HEADER_P (&((ptr)->lheader))
 #define UNMARK_compiled_function(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
 #define ADDITIONAL_FREE_compiled_function(ptr)
 
-  SWEEP_FIXED_TYPE_BLOCK (compiled_function, struct Lisp_Compiled_Function);
+  SWEEP_FIXED_TYPE_BLOCK (compiled_function, Lisp_Compiled_Function);
 }
 
 
@@ -3881,7 +2909,6 @@ sweep_compiled_functions (void)
 static void
 sweep_floats (void)
 {
-#define MARKED_float_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
 #define UNMARK_float(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
 #define ADDITIONAL_FREE_float(ptr)
 
@@ -3892,13 +2919,7 @@ sweep_floats (void)
 static void
 sweep_symbols (void)
 {
-#ifndef LRECORD_SYMBOL
-# define MARKED_symbol_P(ptr) XMARKBIT ((ptr)->plist)
-# define UNMARK_symbol(ptr) do { XUNMARK ((ptr)->plist); } while (0)
-#else
-# define MARKED_symbol_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
-# define UNMARK_symbol(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
-#endif /* !LRECORD_SYMBOL */
+#define UNMARK_symbol(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
 #define ADDITIONAL_FREE_symbol(ptr)
 
   SWEEP_FIXED_TYPE_BLOCK (symbol, struct Lisp_Symbol);
@@ -3907,7 +2928,6 @@ sweep_symbols (void)
 static void
 sweep_extents (void)
 {
-#define MARKED_extent_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
 #define UNMARK_extent(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
 #define ADDITIONAL_FREE_extent(ptr)
 
@@ -3917,7 +2937,6 @@ sweep_extents (void)
 static void
 sweep_events (void)
 {
-#define MARKED_event_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
 #define UNMARK_event(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
 #define ADDITIONAL_FREE_event(ptr)
 
@@ -3927,7 +2946,6 @@ sweep_events (void)
 static void
 sweep_markers (void)
 {
-#define MARKED_marker_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
 #define UNMARK_marker(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
 #define ADDITIONAL_FREE_marker(ptr)                                    \
   do { Lisp_Object tem;                                                        \
@@ -3946,7 +2964,7 @@ free_marker (struct Lisp_Marker *ptr)
   /* Perhaps this will catch freeing an already-freed marker. */
   Lisp_Object temmy;
   XSETMARKER (temmy, ptr);
-  assert (GC_MARKERP (temmy));
+  assert (MARKERP (temmy));
 #endif /* ERROR_CHECK_GC */
 
 #ifndef ALLOC_NO_POOLS
@@ -3971,7 +2989,7 @@ verify_string_chars_integrity (void)
         {
           struct string_chars *s_chars =
             (struct string_chars *) &(sb->string_chars[pos]);
-          struct Lisp_String *string;
+          Lisp_String *string;
          int size;
          int fullsize;
 
@@ -4022,7 +3040,7 @@ compact_string_chars (void)
           struct string_chars *from_s_chars =
             (struct string_chars *) &(from_sb->string_chars[from_pos]);
           struct string_chars *to_s_chars;
-          struct Lisp_String *string;
+          Lisp_String *string;
          int size;
          int fullsize;
 
@@ -4052,11 +3070,7 @@ compact_string_chars (void)
             abort ();
 
           /* Just skip it if it isn't marked.  */
-#ifdef LRECORD_STRING
          if (! MARKED_RECORD_HEADER_P (&(string->lheader)))
-#else
-          if (!XMARKBIT (string->plist))
-#endif
             {
               from_pos += fullsize;
               continue;
@@ -4111,7 +3125,7 @@ compact_string_chars (void)
 static int debug_string_purity;
 
 static void
-debug_string_purity_print (struct Lisp_String *p)
+debug_string_purity_print (Lisp_String *p)
 {
   Charcount i;
   Charcount s = string_char_length (p);
@@ -4137,49 +3151,25 @@ sweep_strings (void)
   int num_small_used = 0, num_small_bytes = 0, num_bytes = 0;
   int debug = debug_string_purity;
 
-#ifdef LRECORD_STRING
-
-# define MARKED_string_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
-# define UNMARK_string(ptr)                            \
-  do { struct Lisp_String *p = (ptr);                  \
-       int size = string_length (p);                   \
-       UNMARK_RECORD_HEADER (&(p->lheader));           \
-       num_bytes += size;                              \
-       if (!BIG_STRING_SIZE_P (size))                  \
-        { num_small_bytes += size;                     \
-          num_small_used++;                            \
-        }                                              \
-       if (debug) debug_string_purity_print (p);       \
-     } while (0)
-# define ADDITIONAL_FREE_string(p)                             \
-  do { int size = string_length (p);                           \
-       if (BIG_STRING_SIZE_P (size))                           \
-        xfree_1 (CHARS_TO_STRING_CHAR (string_data (p)));      \
-     } while (0)
-
-#else
-
-# define MARKED_string_P(ptr) XMARKBIT ((ptr)->plist)
-# define UNMARK_string(ptr)                            \
-  do { struct Lisp_String *p = (ptr);                  \
-       int size = string_length (p);                   \
-       XUNMARK (p->plist);                             \
-       num_bytes += size;                              \
-       if (!BIG_STRING_SIZE_P (size))                  \
-        { num_small_bytes += size;                     \
-          num_small_used++;                            \
-        }                                              \
-       if (debug) debug_string_purity_print (p);       \
-     } while (0)
-# define ADDITIONAL_FREE_string(p)                             \
-  do { int size = string_length (p);                           \
-       if (BIG_STRING_SIZE_P (size))                           \
-        xfree_1 (CHARS_TO_STRING_CHAR (string_data (p)));      \
-     } while (0)
-
-#endif /* ! LRECORD_STRING */
+#define UNMARK_string(ptr) do {                        \
+    Lisp_String *p = (ptr);                    \
+    size_t size = string_length (p);           \
+    UNMARK_RECORD_HEADER (&(p->lheader));      \
+    num_bytes += size;                         \
+    if (!BIG_STRING_SIZE_P (size))             \
+      { num_small_bytes += size;               \
+      num_small_used++;                                \
+      }                                                \
+    if (debug)                                 \
+      debug_string_purity_print (p);           \
+  } while (0)
+#define ADDITIONAL_FREE_string(ptr) do {       \
+    size_t size = string_length (ptr);         \
+    if (BIG_STRING_SIZE_P (size))              \
+      xfree (ptr->data);                       \
+  } while (0)
 
-  SWEEP_FIXED_TYPE_BLOCK (string, struct Lisp_String);
+  SWEEP_FIXED_TYPE_BLOCK (string, Lisp_String);
 
   gc_count_num_short_string_in_use = num_small_used;
   gc_count_string_total_size = num_bytes;
@@ -4188,36 +3178,26 @@ sweep_strings (void)
 
 
 /* I hate duplicating all this crap! */
-static int
+int
 marked_p (Lisp_Object obj)
 {
-  if (EQ (obj, Qnull_pointer)) return 1;
-  if (!POINTER_TYPE_P (XGCTYPE (obj))) return 1;
-  if (PURIFIED (XPNTR (obj))) return 1;
-  switch (XGCTYPE (obj))
-    {
-#ifndef LRECORD_CONS
-    case Lisp_Type_Cons:
-      return XMARKBIT (XCAR (obj));
+#ifdef ERROR_CHECK_GC
+  assert (! (EQ (obj, Qnull_pointer)));
 #endif
-    case Lisp_Type_Record:
-      return MARKED_RECORD_HEADER_P (XRECORD_LHEADER (obj));
-#ifndef LRECORD_STRING
-    case Lisp_Type_String:
-      return XMARKBIT (XSTRING (obj)->plist);
-#endif /* ! LRECORD_STRING */
-#ifndef LRECORD_VECTOR
-    case Lisp_Type_Vector:
-      return XVECTOR_LENGTH (obj) < 0;
-#endif /* !LRECORD_VECTOR */
-#ifndef LRECORD_SYMBOL
-    case Lisp_Type_Symbol:
-      return XMARKBIT (XSYMBOL (obj)->plist);
+  /* Checks we used to perform. */
+  /* if (EQ (obj, Qnull_pointer)) return 1; */
+  /* if (!POINTER_TYPE_P (XGCTYPE (obj))) return 1; */
+  /* if (PURIFIED (XPNTR (obj))) return 1; */
+
+  if (XTYPE (obj) == Lisp_Type_Record)
+    {
+      struct lrecord_header *lheader = XRECORD_LHEADER (obj);
+#if defined (ERROR_CHECK_GC)
+      assert (lheader->type <= last_lrecord_type_index_assigned);
 #endif
-    default:
-      abort ();
+      return C_READONLY_RECORD_HEADER_P (lheader) || MARKED_RECORD_HEADER_P (lheader);
     }
-  return 0;    /* suppress compiler warning */
+  return 1;
 }
 
 static void
@@ -4253,13 +3233,6 @@ gc_sweep (void)
   /* Put all unmarked conses on free list */
   sweep_conses ();
 
-#ifndef LRECORD_VECTOR
-  /* Free all unmarked vectors */
-  sweep_vectors_1 (&all_vectors,
-                   &gc_count_num_vector_used, &gc_count_vector_total_size,
-                   &gc_count_vector_storage);
-#endif
-
   /* Free all unmarked bit vectors */
   sweep_bit_vectors_1 (&all_bit_vectors,
                       &gc_count_num_bit_vector_used,
@@ -4286,6 +3259,27 @@ gc_sweep (void)
 
   sweep_events ();
 
+#ifdef PDUMP
+  /* Unmark all dumped objects */
+  {
+    int i;
+    char *p = pdump_rt_list;
+    if(p)
+      for(;;)
+       {
+         pdump_reloc_table *rt = (pdump_reloc_table *)p;
+         p += sizeof (pdump_reloc_table);
+         if (rt->desc) {
+           for (i=0; i<rt->count; i++)
+             {
+               UNMARK_RECORD_HEADER ((struct lrecord_header *)(*(EMACS_INT *)p));
+               p += sizeof (EMACS_INT);
+             }
+         } else
+           break;
+       }
+  }
+#endif
 }
 \f
 /* Clearing for disksave. */
@@ -4295,7 +3289,7 @@ disksave_object_finalization (void)
 {
   /* It's important that certain information from the environment not get
      dumped with the executable (pathnames, environment variables, etc.).
-     To make it easier to tell when this has happend with strings(1) we
+     To make it easier to tell when this has happened with strings(1) we
      clear some known-to-be-garbage blocks of memory, so that leftover
      results of old evaluation don't look like potential problems.
      But first we set some notable variables to nil and do one more GC,
@@ -4312,6 +3306,8 @@ disksave_object_finalization (void)
   Vexec_path = Qnil;
   Vload_path = Qnil;
   /* Vdump_load_path = Qnil; */
+  /* Release hash tables for locate_file */
+  Flocate_file_clear_hashing (Qt);
   uncache_home_directory();
 
 #if defined(LOADHIST) && !(defined(LOADHIST_DUMPED) || \
@@ -4325,14 +3321,6 @@ disksave_object_finalization (void)
   /* Run the disksave finalization methods of all live objects. */
   disksave_object_finalization_1 ();
 
-#if 0 /* I don't see any point in this.  The purespace starts out all 0's */
-  /* Zero out the unused portion of purespace */
-  if (!pure_lossage)
-    memset (  (char *) (PUREBEG + pure_bytes_used), 0,
-           (((char *) (PUREBEG + get_PURESIZE())) -
-            ((char *) (PUREBEG + pure_bytes_used))));
-#endif
-
   /* Zero out the uninitialized (really, unused) part of the containers
      for the live strings. */
   {
@@ -4368,9 +3356,10 @@ static int gc_hooks_inhibited;
 void
 garbage_collect_1 (void)
 {
+#if MAX_SAVE_STACK > 0
   char stack_top_variable;
   extern char *stack_bottom;
-  int i;
+#endif
   struct frame *f;
   int speccount;
   int cursor_changed;
@@ -4383,13 +3372,24 @@ garbage_collect_1 (void)
       || preparing_for_armageddon)
     return;
 
+  /* We used to call selected_frame() here.
+
+     The following functions cannot be called inside GC
+     so we move to after the above tests. */
+  {
+    Lisp_Object frame;
+    Lisp_Object device = Fselected_device (Qnil);
+    if (NILP (device)) /* Could happen during startup, eg. if always_gc */
+      return;
+    frame = DEVICE_SELECTED_FRAME (XDEVICE (device));
+    if (NILP (frame))
+      signal_simple_error ("No frames exist on device", device);
+    f = XFRAME (frame);
+  }
+
   pre_gc_cursor = Qnil;
   cursor_changed = 0;
 
-  /* This function cannot be called inside GC so we move to after the */
-  /* above tests */
-  f = selected_frame ();
-
   GCPRO1 (pre_gc_cursor);
 
   /* Very important to prevent GC during any of the following
@@ -4476,42 +3476,47 @@ garbage_collect_1 (void)
   cleanup_specifiers ();
 
   /* Mark all the special slots that serve as the roots of accessibility. */
-  {
-    struct gcpro *tail;
-    struct catchtag *catch;
-    struct backtrace *backlist;
-    struct specbinding *bind;
 
+  { /* staticpro() */
+    int i;
     for (i = 0; i < staticidx; i++)
-      {
-#ifdef GDB_SUCKS
-       printf ("%d\n", i);
-       debug_print (*staticvec[i]);
-#endif
-        mark_object (*(staticvec[i]));
-      }
+      mark_object (*(staticvec[i]));
+    for (i = 0; i < staticidx_nodump; i++)
+      mark_object (*(staticvec_nodump[i]));
+  }
 
+  { /* GCPRO() */
+    struct gcpro *tail;
+    int i;
     for (tail = gcprolist; tail; tail = tail->next)
-      {
-       for (i = 0; i < tail->nvars; i++)
-         mark_object (tail->var[i]);
-      }
+      for (i = 0; i < tail->nvars; i++)
+       mark_object (tail->var[i]);
+  }
 
+  { /* specbind() */
+    struct specbinding *bind;
     for (bind = specpdl; bind != specpdl_ptr; bind++)
       {
        mark_object (bind->symbol);
        mark_object (bind->old_value);
       }
+  }
 
+  {
+    struct catchtag *catch;
     for (catch = catchlist; catch; catch = catch->next)
       {
        mark_object (catch->tag);
        mark_object (catch->val);
       }
+  }
 
+  {
+    struct backtrace *backlist;
     for (backlist = backtrace_list; backlist; backlist = backlist->next)
       {
        int nargs = backlist->nargs;
+       int i;
 
        mark_object (*backlist->function);
        if (nargs == UNEVALLED || nargs == MANY)
@@ -4520,38 +3525,30 @@ garbage_collect_1 (void)
          for (i = 0; i < nargs; i++)
            mark_object (backlist->args[i]);
       }
-
-    mark_redisplay (mark_object);
-    mark_profiling_info (mark_object);
   }
 
+  mark_redisplay ();
+  mark_profiling_info ();
+
   /* OK, now do the after-mark stuff.  This is for things that
-     are only marked when something else is marked (e.g. weak hashtables).
+     are only marked when something else is marked (e.g. weak hash tables).
      There may be complex dependencies between such objects -- e.g.
-     a weak hashtable might be unmarked, but after processing a later
-     weak hashtable, the former one might get marked.  So we have to
+     a weak hash table might be unmarked, but after processing a later
+     weak hash table, the former one might get marked.  So we have to
      iterate until nothing more gets marked. */
-  {
-    int did_mark;
-    /* Need to iterate until there's nothing more to mark, in case
-       of chains of mark dependencies. */
-    do
-      {
-        did_mark = 0;
-       did_mark += !!finish_marking_weak_hashtables (marked_p, mark_object);
-       did_mark += !!finish_marking_weak_lists (marked_p, mark_object);
-      }
-    while (did_mark);
-  }
+
+  while (finish_marking_weak_hash_tables () > 0 ||
+        finish_marking_weak_lists       () > 0)
+    ;
 
   /* And prune (this needs to be called after everything else has been
      marked and before we do any sweeping). */
   /* #### this is somewhat ad-hoc and should probably be an object
      method */
-  prune_weak_hashtables (marked_p);
-  prune_weak_lists (marked_p);
-  prune_specifiers (marked_p);
-  prune_syntax_tables (marked_p);
+  prune_weak_hash_tables ();
+  prune_weak_lists ();
+  prune_specifiers ();
+  prune_syntax_tables ();
 
   gc_sweep ();
 
@@ -4608,15 +3605,6 @@ garbage_collect_1 (void)
   return;
 }
 
-#ifdef EMACS_BTL
- /* This isn't actually called.  BTL recognizes the stack frame of the top
-    of the garbage collector by noting that PC is between &garbage_collect_1
-    and &BTL_after_garbage_collect_1_stub.  So this fn must be right here.
-    There's not any other way to know the address of the end of a function.
-  */
-void BTL_after_garbage_collect_1_stub () { abort (); }
-#endif /* EMACS_BTL */
-
 /* Debugging aids.  */
 
 static Lisp_Object
@@ -4628,13 +3616,12 @@ gc_plist_hack (CONST char *name, int value, Lisp_Object tail)
   return cons3 (intern (name), make_int (value), tail);
 }
 
-#define HACK_O_MATIC(type, name, pl)                                   \
-  {                                                                    \
-    int s = 0;                                                         \
-    struct type##_block *x = current_##type##_block;                   \
-    while (x) { s += sizeof (*x) + MALLOC_OVERHEAD; x = x->prev; }     \
-    (pl) = gc_plist_hack ((name), s, (pl));                            \
-  }
+#define HACK_O_MATIC(type, name, pl) do {                              \
+  int s = 0;                                                           \
+  struct type##_block *x = current_##type##_block;                     \
+  while (x) { s += sizeof (*x) + MALLOC_OVERHEAD; x = x->prev; }       \
+  (pl) = gc_plist_hack ((name), s, (pl));                              \
+} while (0)
 
 DEFUN ("garbage-collect", Fgarbage_collect, 0, 0, "", /*
 Reclaim storage for Lisp objects no longer needed.
@@ -4651,16 +3638,11 @@ Garbage collection happens automatically if you cons more than
 {
   Lisp_Object pl = Qnil;
   int i;
-#ifdef LRECORD_VECTOR
   int gc_count_vector_total_size = 0;
-#endif
-
-  if (purify_flag && pure_lossage)
-    return Qnil;
 
   garbage_collect_1 ();
 
-  for (i = 0; i < last_lrecord_type_index_assigned; i++)
+  for (i = 0; i <= last_lrecord_type_index_assigned; i++)
     {
       if (lcrecord_stats[i].bytes_in_use != 0
           || lcrecord_stats[i].bytes_freed != 0
@@ -4669,12 +3651,11 @@ Garbage collection happens automatically if you cons more than
           char buf [255];
           CONST char *name = lrecord_implementations_table[i]->name;
          int len = strlen (name);
-#ifdef LRECORD_VECTOR
          /* save this for the FSFmacs-compatible part of the summary */
-         if (i == *lrecord_vector[0].lrecord_type_index)
+         if (i == *lrecord_vector.lrecord_type_index)
            gc_count_vector_total_size =
              lcrecord_stats[i].bytes_in_use + lcrecord_stats[i].bytes_freed;
-#endif
+
           sprintf (buf, "%s-storage", name);
           pl = gc_plist_hack (buf, lcrecord_stats[i].bytes_in_use, pl);
          /* Okay, simple pluralization check for `symbol-value-varalias' */
@@ -4733,13 +3714,6 @@ Garbage collection happens automatically if you cons more than
   pl = gc_plist_hack ("compiled-functions-used",
                      gc_count_num_compiled_function_in_use, pl);
 
-#ifndef LRECORD_VECTOR
-  pl = gc_plist_hack ("vector-storage", gc_count_vector_storage, pl);
-  pl = gc_plist_hack ("vectors-total-length",
-                      gc_count_vector_total_size, pl);
-  pl = gc_plist_hack ("vectors-used", gc_count_num_vector_used, pl);
-#endif
-
   pl = gc_plist_hack ("bit-vector-storage", gc_count_bit_vector_storage, pl);
   pl = gc_plist_hack ("bit-vectors-total-length",
                       gc_count_bit_vector_total_size, pl);
@@ -4942,56 +3916,10 @@ fixed_type_block_overhead (size_t size)
 \f
 /* Initialization */
 void
-init_alloc_once_early (void)
+reinit_alloc_once_early (void)
 {
-  int iii;
-
-#ifdef PURESTAT
-  for (iii = 0; iii < countof (purestats); iii++)
-    {
-      if (! purestats[iii]) continue;
-      purestats[iii]->nobjects = 0;
-      purestats[iii]->nbytes = 0;
-    }
-  purecopying_for_bytecode = 0;
-#endif /* PURESTAT */
-
-  last_lrecord_type_index_assigned = -1;
-  for (iii = 0; iii < countof (lrecord_implementations_table); iii++)
-    {
-      lrecord_implementations_table[iii] = 0;
-    }
-
-#ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
-  /*
-   * If USE_INDEXED_LRECORD_IMPLEMENTATION is defined, all the staticly
-   * defined subr lrecords were initialized with lheader->type == 0.
-   * See subr_lheader_initializer in lisp.h.  Force type index 0 to be
-   * assigned to lrecord_subr so that those predefined indexes match
-   * reality.
-   */
-  lrecord_type_index (lrecord_subr);
-  assert (*(lrecord_subr[0].lrecord_type_index) == 0);
-  /*
-   * The same is true for symbol_value_forward objects, except the
-   * type is 1.
-   */
-  lrecord_type_index (lrecord_symbol_value_forward);
-  assert (*(lrecord_symbol_value_forward[0].lrecord_type_index) == 1);
-#endif /* USE_INDEXED_LRECORD_IMPLEMENTATION */
-
-  symbols_initialized = 0;
-
   gc_generation_number[0] = 0;
-  /* purify_flag 1 is correct even if CANNOT_DUMP.
-   * loadup.el will set to nil at end. */
-  purify_flag = 1;
-  pure_bytes_used = 0;
-  pure_lossage = 0;
   breathing_space = 0;
-#ifndef LRECORD_VECTOR
-  XSETINT (all_vectors, 0); /* Qzero may not be set yet. */
-#endif
   XSETINT (all_bit_vectors, 0); /* Qzero may not be set yet. */
   XSETINT (Vgc_message, 0);
   all_lcrecords = 0;
@@ -5016,7 +3944,11 @@ init_alloc_once_early (void)
   init_event_alloc ();
 
   ignore_malloc_warnings = 0;
-  staticidx = 0;
+
+  staticidx_nodump = 0;
+  dumpstructidx = 0;
+  pdump_wireidx = 0;
+
   consing_since_gc = 0;
 #if 1
   gc_cons_threshold = 500000; /* XEmacs change */
@@ -5046,6 +3978,40 @@ init_alloc_once_early (void)
 }
 
 void
+init_alloc_once_early (void)
+{
+  int iii;
+
+  reinit_alloc_once_early ();
+
+  last_lrecord_type_index_assigned = -1;
+  for (iii = 0; iii < countof (lrecord_implementations_table); iii++)
+    {
+      lrecord_implementations_table[iii] = 0;
+    }
+
+  /*
+   * All the staticly
+   * defined subr lrecords were initialized with lheader->type == 0.
+   * See subr_lheader_initializer in lisp.h.  Force type index 0 to be
+   * assigned to lrecord_subr so that those predefined indexes match
+   * reality.
+   */
+  lrecord_type_index (&lrecord_subr);
+  assert (*(lrecord_subr.lrecord_type_index) == 0);
+  /*
+   * The same is true for symbol_value_forward objects, except the
+   * type is 1.
+   */
+  lrecord_type_index (&lrecord_symbol_value_forward);
+  assert (*(lrecord_symbol_value_forward.lrecord_type_index) == 1);
+
+  staticidx = 0;
+}
+
+int pure_bytes_used = 0;
+
+void
 reinit_alloc (void)
 {
   gcprolist = 0;
@@ -5124,7 +4090,7 @@ Length (in stack frames) of short backtrace printed out by `debug-allocation'.
 
   DEFVAR_BOOL ("purify-flag", &purify_flag /*
 Non-nil means loading Lisp code in order to dump an executable.
-This means that certain objects should be allocated in shared (pure) space.
+This means that certain objects should be allocated in readonly space.
 */ );
 
   DEFVAR_LISP ("pre-gc-hook", &Vpre_gc_hook /*
@@ -5150,9 +4116,7 @@ window system and `gc-pointer-glyph' specifies a value (i.e. a pointer
 image instance) in the domain of the selected frame, the mouse pointer
 will change instead of this message being printed.
 */ );
-  Vgc_message = make_pure_string ((CONST Bufbyte *) gc_default_message,
-                                 countof (gc_default_message) - 1,
-                                 Qnil, 1);
+  Vgc_message = build_string (gc_default_message);
 
   DEFVAR_LISP ("gc-pointer-glyph", &Vgc_pointer_glyph /*
 Pointer glyph used to indicate that a garbage collection is in progress.
@@ -5169,3 +4133,982 @@ complex_vars_of_alloc (void)
 {
   Vgc_pointer_glyph = Fmake_glyph_internal (Qpointer);
 }
+
+
+#ifdef PDUMP
+
+/* The structure of the file
+ *
+ * 0                   - header
+ * 256                 - dumped objects
+ * stab_offset         - nb_staticpro*(Lisp_Object *) from staticvec
+ *                     - nb_staticpro*(relocated Lisp_Object) pointed to by staticpro
+ *                     - nb_structdmp*pair(void *, adr) for pointers to structures
+ *                     - lrecord_implementations_table[]
+ *                     - relocation table
+ *                      - wired variable address/value couples with the count preceding the list
+ */
+typedef struct
+{
+  char signature[8];
+  EMACS_UINT stab_offset;
+  EMACS_UINT reloc_address;
+  int nb_staticpro;
+  int nb_structdmp;
+  int last_type;
+} dump_header;
+
+char *pdump_start, *pdump_end;
+
+static const unsigned char align_table[256] =
+{
+  8, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
+};
+
+typedef struct pdump_entry_list_elmt
+{
+  struct pdump_entry_list_elmt *next;
+  const void *obj;
+  size_t size;
+  int count;
+  int is_lrecord;
+  EMACS_INT save_offset;
+} pdump_entry_list_elmt;
+
+typedef struct
+{
+  pdump_entry_list_elmt *first;
+  int align;
+  int count;
+} pdump_entry_list;
+
+typedef struct pdump_struct_list_elmt
+{
+  pdump_entry_list list;
+  const struct struct_description *sdesc;
+} pdump_struct_list_elmt;
+
+typedef struct
+{
+  pdump_struct_list_elmt *list;
+  int count;
+  int size;
+} pdump_struct_list;
+
+static pdump_entry_list pdump_object_table[256];
+static pdump_entry_list pdump_opaque_data_list;
+static pdump_struct_list pdump_struct_table;
+static pdump_entry_list_elmt *pdump_qnil;
+
+static int pdump_alert_undump_object[256];
+
+static unsigned long cur_offset;
+static size_t max_size;
+static int pdump_fd;
+static void *pdump_buf;
+
+#define PDUMP_HASHSIZE 200001
+
+static pdump_entry_list_elmt **pdump_hash;
+
+/* Since most pointers are eight bytes aligned, the >>3 allows for a better hash */
+static int
+pdump_make_hash (const void *obj)
+{
+  return ((unsigned long)(obj)>>3) % PDUMP_HASHSIZE;
+}
+
+static pdump_entry_list_elmt *
+pdump_get_entry (const void *obj)
+{
+  int pos = pdump_make_hash(obj);
+  pdump_entry_list_elmt *e;
+  while ((e = pdump_hash[pos]) != 0)
+    {
+      if (e->obj == obj)
+       return e;
+
+      pos++;
+      if (pos == PDUMP_HASHSIZE)
+       pos = 0;
+    }
+  return 0;
+}
+
+static void
+pdump_add_entry (pdump_entry_list *list, const void *obj, size_t size, int count, int is_lrecord)
+{
+  pdump_entry_list_elmt *e;
+  int align;
+  int pos = pdump_make_hash (obj);
+
+  while ((e = pdump_hash[pos]) != 0)
+    {
+      if (e->obj == obj)
+       return;
+
+      pos++;
+      if (pos == PDUMP_HASHSIZE)
+       pos = 0;
+    }
+
+  e = malloc (sizeof (pdump_entry_list_elmt));
+
+  e->next = list->first;
+  e->obj = obj;
+  e->size = size;
+  e->count = count;
+  e->is_lrecord = is_lrecord;
+  list->first = e;
+
+  list->count += count;
+  pdump_hash[pos] = e;
+
+  align = align_table[size & 255];
+  if (align<2 && is_lrecord)
+    align = 2;
+
+  if(align < list->align)
+    list->align = align;
+}
+
+static pdump_entry_list *
+pdump_get_entry_list(const struct struct_description *sdesc)
+{
+  int i;
+  for(i=0; i<pdump_struct_table.count; i++)
+    if (pdump_struct_table.list[i].sdesc == sdesc)
+      return &pdump_struct_table.list[i].list;
+
+  if (pdump_struct_table.size <= pdump_struct_table.count)
+    {
+      if (pdump_struct_table.size == -1)
+       pdump_struct_table.size = 10;
+      else
+       pdump_struct_table.size = pdump_struct_table.size * 2;
+      pdump_struct_table.list = xrealloc (pdump_struct_table.list,
+                                         pdump_struct_table.size*sizeof (pdump_struct_list_elmt));
+    }
+  pdump_struct_table.list[pdump_struct_table.count].list.first = 0;
+  pdump_struct_table.list[pdump_struct_table.count].list.align = 8;
+  pdump_struct_table.list[pdump_struct_table.count].list.count = 0;
+  pdump_struct_table.list[pdump_struct_table.count].sdesc = sdesc;
+
+  return &pdump_struct_table.list[pdump_struct_table.count++].list;
+}
+
+static struct {
+  Lisp_Object obj;
+  int position;
+  int offset;
+} backtrace[65536];
+
+static int depth;
+
+static void pdump_backtrace (void)
+{
+  int i;
+  fprintf (stderr, "pdump backtrace :\n");
+  for (i=0;i<depth;i++)
+    {
+      if (!backtrace[i].obj)
+       fprintf (stderr, "  - ind. (%d, %d)\n", backtrace[i].position, backtrace[i].offset);
+      else
+       {
+         fprintf (stderr, "  - %s (%d, %d)\n",
+                  XRECORD_LHEADER_IMPLEMENTATION (backtrace[i].obj)->name,
+                  backtrace[i].position,
+                  backtrace[i].offset);
+       }
+    }
+}
+
+static void pdump_register_object (Lisp_Object obj);
+static void pdump_register_struct (const void *data, const struct struct_description *sdesc, int count);
+
+static EMACS_INT
+pdump_get_indirect_count (EMACS_INT code, const struct lrecord_description *idesc, const void *idata)
+{
+  EMACS_INT count;
+  const void *irdata;
+
+  int line = XD_INDIRECT_VAL (code);
+  int delta = XD_INDIRECT_DELTA (code);
+
+  irdata = ((char *)idata) + idesc[line].offset;
+  switch (idesc[line].type) {
+  case XD_SIZE_T:
+    count = *(size_t *)irdata;
+    break;
+  case XD_INT:
+    count = *(int *)irdata;
+    break;
+  case XD_LONG:
+    count = *(long *)irdata;
+    break;
+  case XD_BYTECOUNT:
+    count = *(Bytecount *)irdata;
+    break;
+  default:
+    fprintf (stderr, "Unsupported count type : %d (line = %d, code=%ld)\n", idesc[line].type, line, (long)code);
+    pdump_backtrace ();
+    abort ();
+  }
+  count += delta;
+  return count;
+}
+
+static void
+pdump_register_sub (const void *data, const struct lrecord_description *desc, int me)
+{
+  int pos;
+  const void *rdata;
+
+ restart:
+  for (pos = 0; desc[pos].type != XD_END; pos++)
+    {
+      backtrace[me].position = pos;
+      backtrace[me].offset = desc[pos].offset;
+
+      rdata = ((const char *)data) + desc[pos].offset;
+      switch(desc[pos].type)
+       {
+       case XD_SPECIFIER_END:
+         pos = 0;
+         desc = ((const struct Lisp_Specifier *)data)->methods->extra_description;
+         goto restart;
+       case XD_SIZE_T:
+       case XD_INT:
+       case XD_LONG:
+       case XD_BYTECOUNT:
+       case XD_LO_RESET_NIL:
+       case XD_INT_RESET:
+       case XD_LO_LINK:
+         break;
+       case XD_OPAQUE_DATA_PTR:
+         {
+           EMACS_INT count = desc[pos].data1;
+           if (XD_IS_INDIRECT(count))
+             count = pdump_get_indirect_count (count, desc, data);
+
+           pdump_add_entry (&pdump_opaque_data_list,
+                            *(void **)rdata,
+                            count,
+                            1,
+                            0);
+           break;
+         }
+       case XD_C_STRING:
+         {
+           const char *str = *(const char **)rdata;
+           if (str)
+             pdump_add_entry (&pdump_opaque_data_list, str, strlen (str)+1, 1, 0);
+           break;
+         }
+       case XD_DOC_STRING:
+         {
+           const char *str = *(const char **)rdata;
+           if ((EMACS_INT)str > 0)
+             pdump_add_entry (&pdump_opaque_data_list, str, strlen (str)+1, 1, 0);
+           break;
+         }
+       case XD_LISP_OBJECT:
+         {
+           EMACS_INT count = desc[pos].data1;
+           int i;
+           if (XD_IS_INDIRECT (count))
+             count = pdump_get_indirect_count (count, desc, data);
+
+           for(i=0;i<count;i++) {
+             const Lisp_Object *pobj = ((const Lisp_Object *)rdata) + i;
+             Lisp_Object dobj = *pobj;
+
+             backtrace[me].offset = (const char *)pobj - (const char *)data;
+             pdump_register_object (dobj);
+           }
+           break;
+         }
+       case XD_STRUCT_PTR:
+         {
+           EMACS_INT count = desc[pos].data1;
+           const struct struct_description *sdesc = desc[pos].data2;
+           const char *dobj = *(const char **)rdata;
+           if (dobj) {
+             if (XD_IS_INDIRECT (count))
+               count = pdump_get_indirect_count (count, desc, data);
+
+             pdump_register_struct (dobj, sdesc, count);
+           }
+           break;
+         }
+       default:
+         fprintf (stderr, "Unsupported dump type : %d\n", desc[pos].type);
+         pdump_backtrace ();
+         abort ();
+       };
+    }
+}
+
+static void
+pdump_register_object (Lisp_Object obj)
+{
+  if (!obj ||
+      !POINTER_TYPE_P (XTYPE (obj)) ||
+      pdump_get_entry (XRECORD_LHEADER (obj)))
+    return;
+
+  if (XRECORD_LHEADER_IMPLEMENTATION (obj)->description)
+    {
+      int me = depth++;
+      if (me>65536)
+       {
+         fprintf (stderr, "Backtrace overflow, loop ?\n");
+         abort ();
+       }
+      backtrace[me].obj = obj;
+      backtrace[me].position = 0;
+      backtrace[me].offset = 0;
+
+      pdump_add_entry (pdump_object_table + XRECORD_LHEADER (obj)->type,
+                      XRECORD_LHEADER (obj),
+                      XRECORD_LHEADER_IMPLEMENTATION (obj)->static_size ?
+                      XRECORD_LHEADER_IMPLEMENTATION (obj)->static_size :
+                      XRECORD_LHEADER_IMPLEMENTATION (obj)->size_in_bytes_method (XRECORD_LHEADER (obj)),
+                      1,
+                      1);
+      pdump_register_sub (XRECORD_LHEADER (obj),
+                         XRECORD_LHEADER_IMPLEMENTATION (obj)->description,
+                         me);
+      --depth;
+    }
+  else
+    {
+      pdump_alert_undump_object[XRECORD_LHEADER (obj)->type]++;
+      fprintf (stderr, "Undumpable object type : %s\n", XRECORD_LHEADER_IMPLEMENTATION (obj)->name);
+      pdump_backtrace ();
+    }
+}
+
+static void
+pdump_register_struct (const void *data, const struct struct_description *sdesc, int count)
+{
+  if (data && !pdump_get_entry (data))
+    {
+      int me = depth++;
+      int i;
+      if (me>65536)
+       {
+         fprintf (stderr, "Backtrace overflow, loop ?\n");
+         abort ();
+       }
+      backtrace[me].obj = 0;
+      backtrace[me].position = 0;
+      backtrace[me].offset = 0;
+
+      pdump_add_entry (pdump_get_entry_list (sdesc),
+                      data,
+                      sdesc->size,
+                      count,
+                      0);
+      for (i=0; i<count; i++)
+       {
+         pdump_register_sub (((char *)data) + sdesc->size*i,
+                             sdesc->description,
+                             me);
+       }
+      --depth;
+    }
+}
+
+static void
+pdump_dump_data (pdump_entry_list_elmt *elmt, const struct lrecord_description *desc)
+{
+  size_t size = elmt->size;
+  int count = elmt->count;
+  if (desc)
+    {
+      int pos, i;
+      void *rdata;
+      memcpy (pdump_buf, elmt->obj, size*count);
+
+      for (i=0; i<count; i++)
+       {
+         char *cur = ((char *)pdump_buf) + i*size;
+       restart:
+         for (pos = 0; desc[pos].type != XD_END; pos++)
+           {
+             rdata = cur + desc[pos].offset;
+             switch (desc[pos].type)
+               {
+               case XD_SPECIFIER_END:
+                 pos = 0;
+                 desc = ((const struct Lisp_Specifier *)(elmt->obj))->methods->extra_description;
+                 goto restart;
+               case XD_SIZE_T:
+               case XD_INT:
+               case XD_LONG:
+               case XD_BYTECOUNT:
+                 break;
+               case XD_LO_RESET_NIL:
+                 {
+                   EMACS_INT count = desc[pos].data1;
+                   int i;
+                   if (XD_IS_INDIRECT (count))
+                     count = pdump_get_indirect_count (count, desc, elmt->obj);
+                   for (i=0; i<count; i++)
+                     ((EMACS_INT *)rdata)[i] = pdump_qnil->save_offset;
+                   break;
+                 }
+               case XD_INT_RESET:
+                 {
+                   EMACS_INT val = desc[pos].data1;
+                   if (XD_IS_INDIRECT (val))
+                     val = pdump_get_indirect_count (val, desc, elmt->obj);
+                   *(int *)rdata = val;
+                   break;
+                 }
+               case XD_OPAQUE_DATA_PTR:
+               case XD_C_STRING:
+               case XD_STRUCT_PTR:
+                 {
+                   void *ptr = *(void **)rdata;
+                   if (ptr)
+                     *(EMACS_INT *)rdata = pdump_get_entry (ptr)->save_offset;
+                   break;
+                 }
+               case XD_LO_LINK:
+                 {
+                   Lisp_Object obj = *(Lisp_Object *)rdata;
+                   pdump_entry_list_elmt *elmt1;
+                   for(;;)
+                     {
+                       elmt1 = pdump_get_entry (XRECORD_LHEADER(obj));
+                       if (elmt1)
+                         break;
+                       obj = *(Lisp_Object *)(desc[pos].offset + (char *)(XRECORD_LHEADER (obj)));
+                     }
+                   *(EMACS_INT *)rdata = elmt1->save_offset;
+                   break;
+                 }
+               case XD_LISP_OBJECT:
+                 {
+                   EMACS_INT count = desc[pos].data1;
+                   int i;
+                   if (XD_IS_INDIRECT (count))
+                     count = pdump_get_indirect_count (count, desc, elmt->obj);
+
+                   for(i=0; i<count; i++)
+                     {
+                       Lisp_Object *pobj = ((Lisp_Object *)rdata) + i;
+                       Lisp_Object dobj = *pobj;
+                       if (dobj && POINTER_TYPE_P (XTYPE (dobj)))
+                         *pobj = pdump_get_entry (XRECORD_LHEADER (dobj))->save_offset;
+                     }
+                   break;
+                 }
+               case XD_DOC_STRING:
+                 {
+                   EMACS_INT str = *(EMACS_INT *)rdata;
+                   if (str > 0)
+                     *(EMACS_INT *)rdata = pdump_get_entry ((void *)str)->save_offset;
+                   break;
+                 }
+               default:
+                 fprintf (stderr, "Unsupported dump type : %d\n", desc[pos].type);
+                 abort ();
+               };
+           }
+       }
+    }
+  write (pdump_fd, desc ? pdump_buf : elmt->obj, size*count);
+  if (elmt->is_lrecord && ((size*count) & 3))
+    write (pdump_fd, "\0\0\0", 4-((size*count) & 3));
+}
+
+static void
+pdump_reloc_one (void *data, EMACS_INT delta, const struct lrecord_description *desc)
+{
+  int pos;
+  void *rdata;
+
+  restart:
+  for (pos = 0; desc[pos].type != XD_END; pos++)
+    {
+      rdata = ((char *)data) + desc[pos].offset;
+      switch (desc[pos].type) {
+      case XD_SPECIFIER_END:
+       pos = 0;
+       desc = ((const struct Lisp_Specifier *)data)->methods->extra_description;
+       goto restart;
+      case XD_SIZE_T:
+      case XD_INT:
+      case XD_LONG:
+      case XD_BYTECOUNT:
+      case XD_INT_RESET:
+       break;
+      case XD_OPAQUE_DATA_PTR:
+      case XD_C_STRING:
+      case XD_STRUCT_PTR:
+      case XD_LO_LINK:
+       {
+         EMACS_INT ptr = *(EMACS_INT *)rdata;
+         if (ptr)
+           *(EMACS_INT *)rdata = ptr+delta;
+         break;
+       }
+      case XD_LISP_OBJECT:
+      case XD_LO_RESET_NIL:
+       {
+         EMACS_INT count = desc[pos].data1;
+         int i;
+         if (XD_IS_INDIRECT (count))
+           count = pdump_get_indirect_count (count, desc, data);
+
+         for (i=0; i<count; i++)
+           {
+             Lisp_Object *pobj = ((Lisp_Object *)rdata) + i;
+             Lisp_Object dobj = *pobj;
+             if (dobj && POINTER_TYPE_P (XTYPE (dobj)))
+               *pobj = dobj + delta;
+           }
+         break;
+       }
+      case XD_DOC_STRING:
+       {
+         EMACS_INT str = *(EMACS_INT *)rdata;
+         if (str > 0)
+           *(EMACS_INT *)rdata = str + delta;
+         break;
+       }
+      default:
+       fprintf (stderr, "Unsupported dump type : %d\n", desc[pos].type);
+       abort ();
+      };
+    }
+}
+
+static void
+pdump_allocate_offset (pdump_entry_list_elmt *elmt, const struct lrecord_description *desc)
+{
+  size_t size = (elmt->is_lrecord ? (elmt->size + 3) & ~3 : elmt->size)*elmt->count;
+  elmt->save_offset = cur_offset;
+  if (size>max_size)
+    max_size = size;
+  cur_offset += size;
+}
+
+static void
+pdump_scan_by_alignement (void (*f)(pdump_entry_list_elmt *, const struct lrecord_description *))
+{
+  int align, i;
+  const struct lrecord_description *idesc;
+  pdump_entry_list_elmt *elmt;
+  for (align=8; align>=0; align--)
+    {
+      for (i=0; i<=last_lrecord_type_index_assigned; i++)
+       if (pdump_object_table[i].align == align)
+         {
+           elmt = pdump_object_table[i].first;
+           if (!elmt)
+             continue;
+           idesc = lrecord_implementations_table[i]->description;
+           while (elmt)
+             {
+               f (elmt, idesc);
+               elmt = elmt->next;
+             }
+         }
+
+      for (i=0; i<pdump_struct_table.count; i++)
+       if (pdump_struct_table.list[i].list.align == align) {
+         elmt = pdump_struct_table.list[i].list.first;
+         idesc = pdump_struct_table.list[i].sdesc->description;
+         while (elmt)
+           {
+             f (elmt, idesc);
+             elmt = elmt->next;
+           }
+       }
+
+      elmt = pdump_opaque_data_list.first;
+      while (elmt)
+       {
+         if (align_table[elmt->size & 255] == align)
+           f (elmt, 0);
+         elmt = elmt->next;
+       }
+    }
+}
+
+static void
+pdump_dump_staticvec (void)
+{
+  Lisp_Object *reloc = malloc (staticidx*sizeof (Lisp_Object));
+  int i;
+  write (pdump_fd, staticvec, staticidx*sizeof (Lisp_Object *));
+
+  for(i=0; i<staticidx; i++)
+    {
+      Lisp_Object obj = *staticvec[i];
+      if (obj && POINTER_TYPE_P (XTYPE (obj)))
+       reloc[i] = pdump_get_entry (XRECORD_LHEADER (obj))->save_offset;
+      else
+       reloc[i] = obj;
+    }
+  write (pdump_fd, reloc, staticidx*sizeof (Lisp_Object));
+  free (reloc);
+}
+
+static void
+pdump_dump_structvec (void)
+{
+  int i;
+  for (i=0; i<dumpstructidx; i++)
+    {
+      EMACS_INT adr;
+      write (pdump_fd, &(dumpstructvec[i].data), sizeof (void *));
+      adr = pdump_get_entry (*(void **)(dumpstructvec[i].data))->save_offset;
+      write (pdump_fd, &adr, sizeof (adr));
+  }
+}
+
+static void
+pdump_dump_itable (void)
+{
+  write (pdump_fd, lrecord_implementations_table, sizeof (lrecord_implementations_table));
+}
+
+static void
+pdump_dump_rtables (void)
+{
+  int i, j;
+  pdump_entry_list_elmt *elmt;
+  pdump_reloc_table rt;
+
+  for (i=0; i<=last_lrecord_type_index_assigned; i++)
+    {
+      elmt = pdump_object_table[i].first;
+      if(!elmt)
+       continue;
+      rt.desc = lrecord_implementations_table[i]->description;
+      rt.count = pdump_object_table[i].count;
+      write (pdump_fd, &rt, sizeof (rt));
+      while (elmt)
+       {
+         EMACS_INT rdata = pdump_get_entry (XRECORD_LHEADER (elmt->obj))->save_offset;
+         write (pdump_fd, &rdata, sizeof (rdata));
+         elmt = elmt->next;
+       }
+  }
+
+  rt.desc = 0;
+  rt.count = 0;
+  write (pdump_fd, &rt, sizeof (rt));
+
+  for (i=0; i<pdump_struct_table.count; i++)
+    {
+      elmt = pdump_struct_table.list[i].list.first;
+      rt.desc = pdump_struct_table.list[i].sdesc->description;
+      rt.count = pdump_struct_table.list[i].list.count;
+      write (pdump_fd, &rt, sizeof (rt));
+      while (elmt)
+       {
+         EMACS_INT rdata = pdump_get_entry (XRECORD_LHEADER (elmt->obj))->save_offset;
+         for (j=0; j<elmt->count; j++) {
+           write (pdump_fd, &rdata, sizeof (rdata));
+           rdata += elmt->size;
+         }
+         elmt = elmt->next;
+       }
+    }
+  rt.desc = 0;
+  rt.count = 0;
+  write (pdump_fd, &rt, sizeof (rt));
+}
+
+static void
+pdump_dump_wired (void)
+{
+  EMACS_INT count = pdump_wireidx + pdump_wireidx_list;
+  int i;
+
+  write (pdump_fd, &count, sizeof (count));
+
+  for (i=0; i<pdump_wireidx; i++)
+    {
+      Lisp_Object obj = pdump_get_entry (XRECORD_LHEADER (*(pdump_wirevec[i])))->save_offset;
+      write (pdump_fd, &pdump_wirevec[i], sizeof (pdump_wirevec[i]));
+      write (pdump_fd, &obj, sizeof (obj));
+    }
+
+  for (i=0; i<pdump_wireidx_list; i++)
+    {
+      Lisp_Object obj = *(pdump_wirevec_list[i]);
+      pdump_entry_list_elmt *elmt;
+      EMACS_INT res;
+
+      for(;;)
+       {
+         const struct lrecord_description *desc;
+         int pos;
+         elmt = pdump_get_entry (XRECORD_LHEADER (obj));
+         if (elmt)
+           break;
+         desc = XRECORD_LHEADER_IMPLEMENTATION (obj)->description;
+         for (pos = 0; desc[pos].type != XD_LO_LINK; pos++)
+           if (desc[pos].type == XD_END)
+             abort ();
+
+         obj = *(Lisp_Object *)(desc[pos].offset + (char *)(XRECORD_LHEADER (obj)));
+       }
+      res = elmt->save_offset;
+
+      write (pdump_fd, &pdump_wirevec_list[i], sizeof (pdump_wirevec_list[i]));
+      write (pdump_fd, &res, sizeof (res));
+    }
+}
+
+void
+pdump (void)
+{
+  int i;
+  Lisp_Object t_console, t_device, t_frame;
+  int none;
+  dump_header hd;
+
+  /* These appear in a DEFVAR_LISP, which does a staticpro() */
+  t_console = Vterminal_console;
+  t_frame   = Vterminal_frame;
+  t_device  = Vterminal_device;
+
+  Vterminal_console = Qnil;
+  Vterminal_frame   = Qnil;
+  Vterminal_device  = Qnil;
+
+  pdump_hash = malloc (PDUMP_HASHSIZE*sizeof (pdump_entry_list_elmt *));
+  memset (pdump_hash, 0, PDUMP_HASHSIZE*sizeof (pdump_entry_list_elmt *));
+
+  for (i=0; i<=last_lrecord_type_index_assigned; i++)
+    {
+      pdump_object_table[i].first = 0;
+      pdump_object_table[i].align = 8;
+      pdump_object_table[i].count = 0;
+      pdump_alert_undump_object[i] = 0;
+    }
+  pdump_struct_table.count = 0;
+  pdump_struct_table.size = -1;
+
+  pdump_opaque_data_list.first = 0;
+  pdump_opaque_data_list.align = 8;
+  pdump_opaque_data_list.count = 0;
+  depth = 0;
+
+  for (i=0; i<staticidx; i++)
+    pdump_register_object (*staticvec[i]);
+  for (i=0; i<pdump_wireidx; i++)
+    pdump_register_object (*pdump_wirevec[i]);
+
+  none = 1;
+  for(i=0;i<=last_lrecord_type_index_assigned;i++)
+    if (pdump_alert_undump_object[i])
+      {
+       if (none)
+         printf ("Undumpable types list :\n");
+       none = 0;
+       printf ("  - %s (%d)\n", lrecord_implementations_table[i]->name, pdump_alert_undump_object[i]);
+      }
+  if (!none)
+    return;
+
+  for (i=0; i<dumpstructidx; i++)
+    pdump_register_struct (*(void **)(dumpstructvec[i].data), dumpstructvec[i].desc, 1);
+
+  memcpy (hd.signature, "XEmacsDP", 8);
+  hd.reloc_address = 0;
+  hd.nb_staticpro = staticidx;
+  hd.nb_structdmp = dumpstructidx;
+  hd.last_type    = last_lrecord_type_index_assigned;
+
+  cur_offset = 256;
+  max_size = 0;
+
+  pdump_scan_by_alignement (pdump_allocate_offset);
+  pdump_qnil = pdump_get_entry (XRECORD_LHEADER (Qnil));
+
+  pdump_buf = malloc (max_size);
+  pdump_fd = open ("xemacs.dmp", O_WRONLY|O_CREAT|O_TRUNC, 0666);
+  hd.stab_offset = (cur_offset + 3) & ~3;
+
+  write (pdump_fd, &hd, sizeof (hd));
+  lseek (pdump_fd, 256, SEEK_SET);
+
+  pdump_scan_by_alignement (pdump_dump_data);
+
+  lseek (pdump_fd, hd.stab_offset, SEEK_SET);
+
+  pdump_dump_staticvec ();
+  pdump_dump_structvec ();
+  pdump_dump_itable ();
+  pdump_dump_rtables ();
+  pdump_dump_wired ();
+
+  close (pdump_fd);
+  free (pdump_buf);
+
+  free (pdump_hash);
+
+  Vterminal_console = t_console;
+  Vterminal_frame   = t_frame;
+  Vterminal_device  = t_device;
+}
+
+int
+pdump_load (void)
+{
+  size_t length;
+  int i;
+  char *p;
+  EMACS_INT delta;
+  EMACS_INT count;
+
+  pdump_start = pdump_end = 0;
+
+  pdump_fd = open ("xemacs.dmp", O_RDONLY);
+  if (pdump_fd<0)
+    return 0;
+
+  length = lseek (pdump_fd, 0, SEEK_END);
+  lseek (pdump_fd, 0, SEEK_SET);
+
+#ifdef HAVE_MMAP
+  pdump_start = mmap (0, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, pdump_fd, 0);
+  if (pdump_start == MAP_FAILED)
+    pdump_start = 0;
+#endif
+
+  if (!pdump_start)
+    {
+      pdump_start = (void *)((((unsigned long)(malloc(length+255))) + 255) & ~255);
+      read(pdump_fd, pdump_start, length);
+    }
+
+  close (pdump_fd);
+
+  pdump_end = pdump_start + length;
+
+  staticidx = ((dump_header *)(pdump_start))->nb_staticpro;
+  last_lrecord_type_index_assigned = ((dump_header *)(pdump_start))->last_type;
+  delta = ((EMACS_INT)pdump_start) - ((dump_header *)pdump_start)->reloc_address;
+  p = pdump_start + ((dump_header *)pdump_start)->stab_offset;
+
+  /* Put back the staticvec in place */
+  memcpy (staticvec, p, staticidx*sizeof (Lisp_Object *));
+  p += staticidx*sizeof (Lisp_Object *);
+  for (i=0; i<staticidx; i++)
+    {
+      Lisp_Object obj = *(Lisp_Object *)p;
+      p += sizeof (Lisp_Object);
+      if (obj && POINTER_TYPE_P (XTYPE (obj)))
+       obj += delta;
+      *staticvec[i] = obj;
+    }
+
+  /* Put back the dumpstructs */
+  for (i=0; i<((dump_header *)pdump_start)->nb_structdmp; i++)
+    {
+      void **adr = *(void **)p;
+      p += sizeof (void *);
+      *adr = (void *)((*(EMACS_INT *)p) + delta);
+      p += sizeof (EMACS_INT);
+    }
+
+  /* Put back the lrecord_implementations_table */
+  memcpy (lrecord_implementations_table, p, sizeof (lrecord_implementations_table));
+  p += sizeof (lrecord_implementations_table);
+
+  /* Give back their numbers to the lrecord implementations */
+  for (i=0; i<sizeof(lrecord_implementations_table)/sizeof(lrecord_implementations_table[0]); i++)
+    if (lrecord_implementations_table[i])
+      {
+       *(lrecord_implementations_table[i]->lrecord_type_index) = i;
+       last_lrecord_type_index_assigned = i;
+      }
+
+  /* Do the relocations */
+  pdump_rt_list = p;
+  count = 2;
+  for(;;)
+    {
+      pdump_reloc_table *rt = (pdump_reloc_table *)p;
+      p += sizeof (pdump_reloc_table);
+      if (rt->desc) {
+       for (i=0; i<rt->count; i++)
+         {
+           EMACS_INT adr = delta + *(EMACS_INT *)p;
+           *(EMACS_INT *)p = adr;
+           pdump_reloc_one ((void *)adr, delta, rt->desc);
+           p += sizeof (EMACS_INT);
+         }
+      } else
+       if(!(--count))
+         break;
+    }
+
+  /* Put the pdump_wire variables in place */
+  count = *(EMACS_INT *)p;
+  p += sizeof(EMACS_INT);
+
+  for (i=0; i<count; i++)
+    {
+      Lisp_Object *var, obj;
+      var = *(Lisp_Object **)p;
+      p += sizeof (Lisp_Object *);
+
+      obj = *(Lisp_Object *)p;
+      p += sizeof (Lisp_Object);
+
+      if (obj && POINTER_TYPE_P (XTYPE (obj)))
+       obj += delta;
+      *var = obj;
+    }
+
+  /* Final cleanups */
+  /*   reorganize hash tables */
+  p = pdump_rt_list;
+  for(;;)
+    {
+      pdump_reloc_table *rt = (pdump_reloc_table *)p;
+      p += sizeof (pdump_reloc_table);
+      if (!rt->desc)
+       break;
+      if (rt->desc == hash_table_description)
+       {
+         for (i=0; i<rt->count; i++)
+           {
+             struct Lisp_Hash_Table *ht = XHASH_TABLE (*(EMACS_INT *)p);
+             reorganize_hash_table (ht);
+             p += sizeof (EMACS_INT);
+           }
+         break;
+       } else
+         p += sizeof (EMACS_INT)*rt->count;
+    }
+  return 1;
+}
+
+#endif