Merge r21-4-11-chise-0_20-=ucs.
[chise/xemacs-chise.git.1] / src / alloc.c
index aa39388..1b2b806 100644 (file)
@@ -42,7 +42,6 @@ Boston, MA 02111-1307, USA.  */
 #include <config.h>
 #include "lisp.h"
 
-#include "alloc.h"
 #include "backtrace.h"
 #include "buffer.h"
 #include "bytecode.h"
@@ -86,8 +85,8 @@ EXFUN (Fgarbage_collect, 0);
 #endif
 
 #ifdef DEBUG_XEMACS
-static int debug_allocation;
-static int debug_allocation_backtrace_length;
+static Fixnum debug_allocation;
+static Fixnum debug_allocation_backtrace_length;
 #endif
 
 /* Number of bytes of consing done since the last gc */
@@ -159,16 +158,6 @@ Lisp_Object Vgc_pointer_glyph;
 static const char gc_default_message[] = "Garbage collecting";
 Lisp_Object Qgarbage_collecting;
 
-#ifndef VIRT_ADDR_VARIES
-extern
-#endif /* VIRT_ADDR_VARIES */
- EMACS_INT malloc_sbrk_used;
-
-#ifndef VIRT_ADDR_VARIES
-extern
-#endif /* VIRT_ADDR_VARIES */
- EMACS_INT malloc_sbrk_unused;
-
 /* Non-zero means we're in the process of doing the dump */
 int purify_flag;
 
@@ -470,6 +459,20 @@ unsigned char dbg_USE_UNION_TYPE = 0;
 unsigned char dbg_valbits = VALBITS;
 unsigned char dbg_gctypebits = GCTYPEBITS;
 
+/* On some systems, the above definitions will be optimized away by
+   the compiler or linker unless they are referenced in some function. */
+long dbg_inhibit_dbg_symbol_deletion (void);
+long
+dbg_inhibit_dbg_symbol_deletion (void)
+{
+  return
+    (dbg_valmask +
+     dbg_typemask +
+     dbg_USE_UNION_TYPE +
+     dbg_valbits +
+     dbg_gctypebits);
+}
+
 /* Macros turned into functions for ease of debugging.
    Debuggers don't know about macros! */
 int dbg_eq (Lisp_Object obj1, Lisp_Object obj2);
@@ -555,35 +558,27 @@ dbg_eq (Lisp_Object obj1, Lisp_Object obj2)
    currently executing functions; the gcpro list; etc.) and
    recursively marking all objects that are accessible.
 
-   At the beginning of the sweep stage, the conses in the cons
-   blocks are in one of three states: in use and marked, in use
-   but not marked, and not in use (already freed).  Any conses
-   that are marked have been marked in the mark stage just
-   executed, because as part of the sweep stage we unmark any
-   marked objects.  The way we tell whether or not a cons cell
-   is in use is through the FREE_STRUCT_P macro.  This basically
-   looks at the first 4 bytes (or however many bytes a pointer
-   fits in) to see if all the bits in those bytes are 1.  The
-   resulting value (0xFFFFFFFF) is not a valid pointer and is
-   not a valid Lisp_Object.  All current fixed-size types have
-   a pointer or Lisp_Object as their first element with the
-   exception of strings; they have a size value, which can
-   never be less than zero, and so 0xFFFFFFFF is invalid for
-   strings as well.  Now assuming that a cons cell is in use,
-   the way we tell whether or not it is marked is to look at
-   the mark bit of its car (each Lisp_Object has one bit
-   reserved as a mark bit, in case it's needed).  Note that
-   different types of objects use different fields to indicate
-   whether the object is marked, but the principle is the same.
-
-   Conses on the free_cons_list are threaded through a pointer
-   stored in the bytes directly after the bytes that are set
-   to 0xFFFFFFFF (we cannot overwrite these because the cons
-   is still in a cons_block and needs to remain marked as
-   not in use for the next time that GC happens).  This
-   implies that all fixed-size types must be at least big
-   enough to store two pointers, which is indeed the case
-   for all current fixed-size types.
+   At the beginning of the sweep stage, the conses in the cons blocks
+   are in one of three states: in use and marked, in use but not
+   marked, and not in use (already freed).  Any conses that are marked
+   have been marked in the mark stage just executed, because as part
+   of the sweep stage we unmark any marked objects.  The way we tell
+   whether or not a cons cell is in use is through the LRECORD_FREE_P
+   macro.  This uses a special lrecord type `lrecord_type_free',
+   which is never associated with any valid object.
+
+   Conses on the free_cons_list are threaded through a pointer stored
+   in the conses themselves.  Because the cons is still in a
+   cons_block and needs to remain marked as not in use for the next
+   time that GC happens, we need room to store both the "free"
+   indicator and the chaining pointer.  So this pointer is stored
+   after the lrecord header (actually where C places a pointer after
+   the lrecord header; they are not necessarily contiguous).  This
+   implies that all fixed-size types must be big enough to contain at
+   least one pointer.  This is true for all current fixed-size types,
+   with the possible exception of Lisp_Floats, for which we define the
+   meat of the struct using a union of a pointer and a double to
+   ensure adequate space for the free list chain pointer.
 
    Some types of objects need additional "finalization" done
    when an object is converted from in use to not in use;
@@ -597,19 +592,18 @@ dbg_eq (Lisp_Object obj1, Lisp_Object obj2)
    WARNING: Things are in an extremely bizarre state when
    the ADDITIONAL_FREE_type macros are called, so beware!
 
-   When ERROR_CHECK_GC is defined, we do things differently
-   so as to maximize our chances of catching places where
-   there is insufficient GCPROing.  The thing we want to
-   avoid is having an object that we're using but didn't
-   GCPRO get freed by GC and then reallocated while we're
-   in the process of using it -- this will result in something
-   seemingly unrelated getting trashed, and is extremely
-   difficult to track down.  If the object gets freed but
-   not reallocated, we can usually catch this because we
-   set all bytes of a freed object to 0xDEADBEEF. (The
-   first four bytes, however, are 0xFFFFFFFF, and the next
-   four are a pointer used to chain freed objects together;
-   we play some tricks with this pointer to make it more
+   When ERROR_CHECK_GC is defined, we do things differently so as to
+   maximize our chances of catching places where there is insufficient
+   GCPROing.  The thing we want to avoid is having an object that
+   we're using but didn't GCPRO get freed by GC and then reallocated
+   while we're in the process of using it -- this will result in
+   something seemingly unrelated getting trashed, and is extremely
+   difficult to track down.  If the object gets freed but not
+   reallocated, we can usually catch this because we set most of the
+   bytes of a freed object to 0xDEADBEEF. (The lisp object type is set
+   to the invalid type `lrecord_type_free', however, and a pointer
+   used to chain freed objects together is stored after the lrecord
+   header; we play some tricks with this pointer to make it more
    bogus, so crashes are more likely to occur right away.)
 
    We want freed objects to stay free as long as possible,
@@ -674,8 +668,8 @@ struct type##_block                                 \
 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 Lisp_Free *type##_free_list;                    \
+static Lisp_Free *type##_free_list_tail;               \
                                                        \
 static void                                            \
 init_##type##_alloc (void)                             \
@@ -715,43 +709,38 @@ static int gc_count_num_##type##_freelist
    cell was not GC-protected and was getting collected before
    free_cons() was called. */
 
-#define ALLOCATE_FIXED_TYPE_1(type, structtype, result)                         \
-do                                                                      \
-{                                                                       \
-  if (gc_count_num_##type##_freelist >                                  \
-      MINIMUM_ALLOWED_FIXED_TYPE_CELLS_##type)                                  \
-    {                                                                   \
-      result = type##_free_list;                                        \
-      /* Before actually using the chain pointer, we complement all its         \
-         bits; see FREE_FIXED_TYPE(). */                                \
-      type##_free_list =                                                \
-        (structtype *) ~(unsigned long)                                         \
-          (* (structtype **) ((char *) result + sizeof (void *)));      \
-      gc_count_num_##type##_freelist--;                                         \
-    }                                                                   \
-  else                                                                  \
-    ALLOCATE_FIXED_TYPE_FROM_BLOCK (type, result);                      \
-  MARK_STRUCT_AS_NOT_FREE (result);                                     \
+#define ALLOCATE_FIXED_TYPE_1(type, structtype, result) do {   \
+  if (gc_count_num_##type##_freelist >                         \
+      MINIMUM_ALLOWED_FIXED_TYPE_CELLS_##type)                 \
+    {                                                          \
+      result = (structtype *) type##_free_list;                        \
+      /* Before actually using the chain pointer,              \
+        we complement all its bits; see FREE_FIXED_TYPE(). */  \
+      type##_free_list = (Lisp_Free *)                         \
+       (~ (EMACS_UINT) (type##_free_list->chain));             \
+      gc_count_num_##type##_freelist--;                                \
+    }                                                          \
+  else                                                         \
+    ALLOCATE_FIXED_TYPE_FROM_BLOCK (type, result);             \
+  MARK_LRECORD_AS_NOT_FREE (result);                           \
 } while (0)
 
 #else /* !ERROR_CHECK_GC */
 
-#define ALLOCATE_FIXED_TYPE_1(type, structtype, result)                \
-do                                                             \
-{                                                              \
+#define ALLOCATE_FIXED_TYPE_1(type, structtype, result) do {   \
   if (type##_free_list)                                                \
     {                                                          \
-      result = type##_free_list;                               \
-      type##_free_list =                                       \
-        * (structtype **) ((char *) result + sizeof (void *)); \
+      result = (structtype *) type##_free_list;                        \
+      type##_free_list = type##_free_list->chain;              \
     }                                                          \
   else                                                         \
     ALLOCATE_FIXED_TYPE_FROM_BLOCK (type, result);             \
-  MARK_STRUCT_AS_NOT_FREE (result);                            \
+  MARK_LRECORD_AS_NOT_FREE (result);                           \
 } while (0)
 
 #endif /* !ERROR_CHECK_GC */
 
+
 #define ALLOCATE_FIXED_TYPE(type, structtype, result)  \
 do                                                     \
 {                                                      \
@@ -766,68 +755,54 @@ do                                                                \
   NOSEEUM_INCREMENT_CONS_COUNTER (sizeof (structtype), #type); \
 } while (0)
 
-/* INVALID_POINTER_VALUE should be a value that is invalid as a pointer
-   to a Lisp object and invalid as an actual Lisp_Object value.  We have
-   to make sure that this value cannot be an integer in Lisp_Object form.
-   0xFFFFFFFF could be so on a 64-bit system, so we extend it to 64 bits.
-   On a 32-bit system, the type bits will be non-zero, making the value
-   be a pointer, and the pointer will be misaligned.
-
-   Even if Emacs is run on some weirdo system that allows and allocates
-   byte-aligned pointers, this pointer is at the very top of the address
-   space and so it's almost inconceivable that it could ever be valid. */
-
-#if INTBITS == 32
-# define INVALID_POINTER_VALUE 0xFFFFFFFF
-#elif INTBITS == 48
-# define INVALID_POINTER_VALUE 0xFFFFFFFFFFFF
-#elif INTBITS == 64
-# define INVALID_POINTER_VALUE 0xFFFFFFFFFFFFFFFF
+
+/* Lisp_Free is the type to represent a free list member inside a frob
+   block of any lisp object type.  */
+typedef struct Lisp_Free
+{
+  struct lrecord_header lheader;
+  struct Lisp_Free *chain;
+} Lisp_Free;
+
+#define LRECORD_FREE_P(ptr) \
+((ptr)->lheader.type == lrecord_type_free)
+
+#define MARK_LRECORD_AS_FREE(ptr) \
+((void) ((ptr)->lheader.type = lrecord_type_free))
+
+#ifdef ERROR_CHECK_GC
+#define MARK_LRECORD_AS_NOT_FREE(ptr) \
+((void) ((ptr)->lheader.type = lrecord_type_undefined))
 #else
-You have some weird system and need to supply a reasonable value here.
+#define MARK_LRECORD_AS_NOT_FREE(ptr) DO_NOTHING
 #endif
 
-/* The construct (* (void **) (ptr)) would cause aliasing problems
-   with modern optimizing compilers like `gcc -O3 -fstrict-aliasing'.
-   But `char *' can legally alias any pointer.  Hence this union trick. */
-typedef union { char c; void *p; } *aliasing_voidpp;
-#define ALIASING_VOIDPP_DEREFERENCE(ptr) \
-  (((aliasing_voidpp) (ptr))->p)
-#define FREE_STRUCT_P(ptr) \
-  (ALIASING_VOIDPP_DEREFERENCE (ptr) == (void *) INVALID_POINTER_VALUE)
-#define MARK_STRUCT_AS_FREE(ptr) \
-  (ALIASING_VOIDPP_DEREFERENCE (ptr) = (void *) INVALID_POINTER_VALUE)
-#define MARK_STRUCT_AS_NOT_FREE(ptr) \
-  (ALIASING_VOIDPP_DEREFERENCE (ptr) = 0)
-
 #ifdef ERROR_CHECK_GC
 
-#define PUT_FIXED_TYPE_ON_FREE_LIST(type, structtype, ptr)             \
-do { if (type##_free_list_tail)                                                \
-       {                                                               \
-        /* When we store the chain pointer, we complement all          \
-           its bits; this should significantly increase its            \
-           bogosity in case someone tries to use the value, and        \
-           should make us dump faster if someone stores something      \
-           over the pointer because when it gets un-complemented in    \
-           ALLOCATED_FIXED_TYPE(), the resulting pointer will be       \
-           extremely bogus. */                                         \
-        * (structtype **)                                              \
-          ((char *) type##_free_list_tail + sizeof (void *)) =         \
-            (structtype *) ~(unsigned long) ptr;                       \
-       }                                                               \
-     else                                                              \
-       type##_free_list = ptr;                                         \
-     type##_free_list_tail = ptr;                                      \
-   } while (0)
+#define PUT_FIXED_TYPE_ON_FREE_LIST(type, structtype, ptr) do {        \
+  if (type##_free_list_tail)                                   \
+    {                                                          \
+      /* When we store the chain pointer, we complement all    \
+        its bits; this should significantly increase its       \
+        bogosity in case someone tries to use the value, and   \
+        should make us crash faster if someone overwrites the  \
+        pointer because when it gets un-complemented in        \
+        ALLOCATED_FIXED_TYPE(), the resulting pointer will be  \
+        extremely bogus. */                                    \
+      type##_free_list_tail->chain =                           \
+       (Lisp_Free *) ~ (EMACS_UINT) (ptr);                     \
+    }                                                          \
+  else                                                         \
+    type##_free_list = (Lisp_Free *) (ptr);                    \
+  type##_free_list_tail = (Lisp_Free *) (ptr);                 \
+} while (0)
 
 #else /* !ERROR_CHECK_GC */
 
-#define PUT_FIXED_TYPE_ON_FREE_LIST(type, structtype, ptr)     \
-do { * (structtype **) ((char *) (ptr) + sizeof (void *)) =    \
-       type##_free_list;                                       \
-     type##_free_list = (ptr);                                 \
-   } while (0)
+#define PUT_FIXED_TYPE_ON_FREE_LIST(type, structtype, ptr) do {        \
+  ((Lisp_Free *) (ptr))->chain = type##_free_list;             \
+  type##_free_list = (Lisp_Free *) (ptr);                      \
+} while (0)                                                    \
 
 #endif /* !ERROR_CHECK_GC */
 
@@ -838,7 +813,7 @@ do { * (structtype **) ((char *) (ptr) + sizeof (void *)) = \
   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);                               \
+  MARK_LRECORD_AS_FREE (FFT_ptr);                              \
 } while (0)
 
 /* Like FREE_FIXED_TYPE() but used when we are explicitly
@@ -1016,9 +991,9 @@ list6 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, Lisp_Object obj3,
 }
 
 DEFUN ("make-list", Fmake_list, 2, 2, 0, /*
-Return a new list of length LENGTH, with each element being INIT.
+Return a new list of length LENGTH, with each element being OBJECT.
 */
-       (length, init))
+       (length, object))
 {
   CHECK_NATNUM (length);
 
@@ -1027,7 +1002,7 @@ Return a new list of length LENGTH, with each element being INIT.
     size_t size = XINT (length);
 
     while (size--)
-      val = Fcons (init, val);
+      val = Fcons (object, val);
     return val;
   }
 }
@@ -1082,7 +1057,7 @@ mark_vector (Lisp_Object obj)
 static size_t
 size_vector (const void *lheader)
 {
-  return FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Vector, contents,
+  return FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Vector, Lisp_Object, contents,
                                       ((Lisp_Vector *) lheader)->size);
 }
 
@@ -1130,7 +1105,8 @@ static Lisp_Vector *
 make_vector_internal (size_t sizei)
 {
   /* no vector_next */
-  size_t sizem = FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Vector, contents, sizei);
+  size_t sizem = FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Vector, Lisp_Object,
+                                              contents, sizei);
   Lisp_Vector *p = (Lisp_Vector *) alloc_lcrecord (sizem, &lrecord_vector);
 
   p->size = sizei;
@@ -1138,13 +1114,13 @@ make_vector_internal (size_t sizei)
 }
 
 Lisp_Object
-make_vector (size_t length, Lisp_Object init)
+make_vector (size_t length, Lisp_Object object)
 {
   Lisp_Vector *vecp = make_vector_internal (length);
   Lisp_Object *p = vector_data (vecp);
 
   while (length--)
-    *p++ = init;
+    *p++ = object;
 
   {
     Lisp_Object vector;
@@ -1154,13 +1130,13 @@ make_vector (size_t length, Lisp_Object init)
 }
 
 DEFUN ("make-vector", Fmake_vector, 2, 2, 0, /*
-Return a new vector of length LENGTH, with each element being INIT.
+Return a new vector of length LENGTH, with each element being OBJECT.
 See also the function `vector'.
 */
-       (length, init))
+       (length, object))
 {
   CONCHECK_NATNUM (length);
-  return make_vector (XINT (length), init);
+  return make_vector (XINT (length), object);
 }
 
 DEFUN ("vector", Fvector, 0, MANY, 0, /*
@@ -1293,7 +1269,8 @@ static Lisp_Bit_Vector *
 make_bit_vector_internal (size_t sizei)
 {
   size_t num_longs = BIT_VECTOR_LONG_STORAGE (sizei);
-  size_t sizem = FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Bit_Vector, bits, num_longs);
+  size_t sizem = FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Bit_Vector, unsigned long,
+                                              bits, num_longs);
   Lisp_Bit_Vector *p = (Lisp_Bit_Vector *) allocate_lisp_storage (sizem);
   set_lheader_implementation (&p->lheader, &lrecord_bit_vector);
 
@@ -1309,14 +1286,14 @@ make_bit_vector_internal (size_t sizei)
 }
 
 Lisp_Object
-make_bit_vector (size_t length, Lisp_Object init)
+make_bit_vector (size_t length, Lisp_Object bit)
 {
   Lisp_Bit_Vector *p = make_bit_vector_internal (length);
   size_t num_longs = BIT_VECTOR_LONG_STORAGE (length);
 
-  CHECK_BIT (init);
+  CHECK_BIT (bit);
 
-  if (ZEROP (init))
+  if (ZEROP (bit))
     memset (p->bits, 0, num_longs * sizeof (long));
   else
     {
@@ -1338,7 +1315,7 @@ make_bit_vector (size_t length, Lisp_Object init)
 Lisp_Object
 make_bit_vector_from_byte_vector (unsigned char *bytevec, size_t length)
 {
-  int i;
+  size_t i;
   Lisp_Bit_Vector *p = make_bit_vector_internal (length);
 
   for (i = 0; i < length; i++)
@@ -1352,19 +1329,20 @@ make_bit_vector_from_byte_vector (unsigned char *bytevec, size_t length)
 }
 
 DEFUN ("make-bit-vector", Fmake_bit_vector, 2, 2, 0, /*
-Return a new bit vector of length LENGTH. with each bit being INIT.
-Each element is set to INIT.  See also the function `bit-vector'.
+Return a new bit vector of length LENGTH. with each bit set to BIT.
+BIT must be one of the integers 0 or 1.  See also the function `bit-vector'.
 */
-       (length, init))
+       (length, bit))
 {
   CONCHECK_NATNUM (length);
 
-  return make_bit_vector (XINT (length), init);
+  return make_bit_vector (XINT (length), bit);
 }
 
 DEFUN ("bit-vector", Fbit_vector, 0, MANY, 0, /*
 Return a newly created bit vector with specified arguments as elements.
 Any number of arguments, even zero arguments, are allowed.
+Each argument must be one of the integers 0 or 1.
 */
        (int nargs, Lisp_Object *args))
 {
@@ -1451,7 +1429,6 @@ This is terrible behavior which is retained for compatibility with old
   /* 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);
@@ -1790,6 +1767,9 @@ static struct string_chars_block *current_string_chars_block;
 #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 STRING_CHARS_FREE_P(ptr) ((ptr)->string == NULL)
+#define MARK_STRING_CHARS_AS_FREE(ptr) ((void) ((ptr)->string = NULL))
+
 struct string_chars
 {
   Lisp_String *string;
@@ -1997,7 +1977,7 @@ resize_string (Lisp_String *s, Bytecount pos, Bytecount delta)
            /* 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);
+           MARK_STRING_CHARS_AS_FREE (old_s_chars);
            ((struct unused_string_chars *) old_s_chars)->fullsize =
              oldfullsize;
          }
@@ -2046,21 +2026,21 @@ set_string_char (Lisp_String *s, Charcount i, Emchar c)
 #endif /* MULE */
 
 DEFUN ("make-string", Fmake_string, 2, 2, 0, /*
-Return a new string of length LENGTH, with each character being INIT.
-LENGTH must be an integer and INIT must be a character.
+Return a new string consisting of LENGTH copies of CHARACTER.
+LENGTH must be a non-negative integer.
 */
-       (length, init))
+       (length, character))
 {
   CHECK_NATNUM (length);
-  CHECK_CHAR_COERCE_INT (init);
+  CHECK_CHAR_COERCE_INT (character);
   {
     Bufbyte init_str[MAX_EMCHAR_LEN];
-    int len = set_charptr_emchar (init_str, XCHAR (init));
+    int len = set_charptr_emchar (init_str, XCHAR (character));
     Lisp_Object val = make_uninit_string (len * XINT (length));
 
     if (len == 1)
       /* Optimize the single-byte case */
-      memset (XSTRING_DATA (val), XCHAR (init), XSTRING_LENGTH (val));
+      memset (XSTRING_DATA (val), XCHAR (character), XSTRING_LENGTH (val));
     else
       {
        size_t i;
@@ -2071,6 +2051,10 @@ LENGTH must be an integer and INIT must be a character.
            Bufbyte *init_ptr = init_str;
            switch (len)
              {
+#ifdef UTF2000
+             case 6: *ptr++ = *init_ptr++;
+             case 5: *ptr++ = *init_ptr++;
+#endif
              case 4: *ptr++ = *init_ptr++;
              case 3: *ptr++ = *init_ptr++;
              case 2: *ptr++ = *init_ptr++;
@@ -2296,8 +2280,7 @@ allocate_managed_lcrecord (Lisp_Object lcrecord_list)
     {
       Lisp_Object val;
 
-      XSETOBJ (val, Lisp_Type_Record,
-              alloc_lcrecord (list->size, list->implementation));
+      XSETOBJ (val, alloc_lcrecord (list->size, list->implementation));
       return val;
     }
 }
@@ -2336,9 +2319,9 @@ Make a copy of OBJECT in pure storage.
 Recursively copies contents of vectors and cons cells.
 Does not copy symbols.
 */
-       (obj))
+       (object))
 {
-  return obj;
+  return object;
 }
 
 \f
@@ -2349,9 +2332,8 @@ Does not copy symbols.
 /* All the built-in lisp object types are enumerated in `enum lrecord_type'.
    Additional ones may be defined by a module (none yet).  We leave some
    room in `lrecord_implementations_table' for such new lisp object types. */
-#define MODULE_DEFINABLE_TYPE_COUNT 32
-const struct lrecord_implementation *lrecord_implementations_table[lrecord_type_count + MODULE_DEFINABLE_TYPE_COUNT];
-
+const struct lrecord_implementation *lrecord_implementations_table[(unsigned int)lrecord_type_last_built_in_type + MODULE_DEFINABLE_TYPE_COUNT];
+unsigned int lrecord_type_count = (unsigned int)lrecord_type_last_built_in_type;
 /* Object marker functions are in the lrecord_implementation structure.
    But copying them to a parallel array is much more cache-friendly.
    This hack speeds up (garbage-collect) by about 5%. */
@@ -2359,98 +2341,47 @@ Lisp_Object (*lrecord_markers[countof (lrecord_implementations_table)]) (Lisp_Ob
 
 struct gcpro *gcprolist;
 
-/* 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
-
-/* Not "static" because used by dumper.c */
-Lisp_Object *staticvec[NSTATICS];
-int staticidx;
-
-/* Put an entry in staticvec, pointing at the variable whose address is given
- */
-void
-staticpro (Lisp_Object *varaddress)
-{
-  /* #### This is now a dubious assert() since this routine may be called */
-  /* by Lisp attempting to load a DLL. */
-  assert (staticidx < countof (staticvec));
-  staticvec[staticidx++] = varaddress;
-}
-
-
-Lisp_Object *staticvec_nodump[200];
-int staticidx_nodump;
-
-/* Put an entry in staticvec_nodump, pointing at the variable whose address is given
- */
-void
-staticpro_nodump (Lisp_Object *varaddress)
-{
-  /* #### This is now a dubious assert() since this routine may be called */
-  /* by Lisp attempting to load a DLL. */
-  assert (staticidx_nodump < countof (staticvec_nodump));
-  staticvec_nodump[staticidx_nodump++] = varaddress;
-}
-
-
-struct pdump_dumpstructinfo dumpstructvec[200];
-int dumpstructidx;
-
-/* Put an entry in dumpstructvec, pointing at the variable whose address is given
- */
-void
-dumpstruct (void *varaddress, const struct struct_description *desc)
-{
-  assert (dumpstructidx < countof (dumpstructvec));
-  dumpstructvec[dumpstructidx].data = varaddress;
-  dumpstructvec[dumpstructidx].desc = desc;
-  dumpstructidx++;
-}
+/* We want the staticpros relocated, but not the pointers found therein.
+   Hence we use a trivial description, as for pointerless objects. */
+static const struct lrecord_description staticpro_description_1[] = {
+  { XD_END }
+};
 
-struct pdump_dumpopaqueinfo dumpopaquevec[250];
-int dumpopaqueidx;
+static const struct struct_description staticpro_description = {
+  sizeof (Lisp_Object *),
+  staticpro_description_1
+};
 
-/* Put an entry in dumpopaquevec, pointing at the variable whose address is given
- */
-void
-dumpopaque (void *varaddress, size_t size)
-{
-  assert (dumpopaqueidx < countof (dumpopaquevec));
+static const struct lrecord_description staticpros_description_1[] = {
+  XD_DYNARR_DESC (Lisp_Object_ptr_dynarr, &staticpro_description),
+  { XD_END }
+};
 
-  dumpopaquevec[dumpopaqueidx].data = varaddress;
-  dumpopaquevec[dumpopaqueidx].size = size;
-  dumpopaqueidx++;
-}
+static const struct struct_description staticpros_description = {
+  sizeof (Lisp_Object_ptr_dynarr),
+  staticpros_description_1
+};
 
-Lisp_Object *pdump_wirevec[50];
-int pdump_wireidx;
+Lisp_Object_ptr_dynarr *staticpros;
 
-/* Put an entry in pdump_wirevec, pointing at the variable whose address is given
- */
+/* Mark the Lisp_Object at non-heap VARADDRESS as a root object for
+   garbage collection, and for dumping. */
 void
-pdump_wire (Lisp_Object *varaddress)
+staticpro (Lisp_Object *varaddress)
 {
-  assert (pdump_wireidx < countof (pdump_wirevec));
-  pdump_wirevec[pdump_wireidx++] = varaddress;
+  Dynarr_add (staticpros, varaddress);
+  dump_add_root_object (varaddress);
 }
 
 
-Lisp_Object *pdump_wirevec_list[50];
-int pdump_wireidx_list;
+Lisp_Object_ptr_dynarr *staticpros_nodump;
 
-/* Put an entry in pdump_wirevec_list, pointing at the variable whose address is given
- */
+/* Mark the Lisp_Object at non-heap VARADDRESS as a root object for
+   garbage collection, but not for dumping. */
 void
-pdump_wire_list (Lisp_Object *varaddress)
+staticpro_nodump (Lisp_Object *varaddress)
 {
-  assert (pdump_wireidx_list < countof (pdump_wirevec_list));
-  pdump_wirevec_list[pdump_wireidx_list++] = varaddress;
+  Dynarr_add (staticpros_nodump, varaddress);
 }
 
 #ifdef ERROR_CHECK_GC
@@ -2546,7 +2477,8 @@ static struct
   int instances_freed;
   int bytes_freed;
   int instances_on_free_list;
-} lcrecord_stats [countof (lrecord_implementations_table)];
+} lcrecord_stats [countof (lrecord_implementations_table)
+                 + MODULE_DEFINABLE_TYPE_COUNT];
 
 static void
 tick_lcrecord_stats (const struct lrecord_header *h, int free_p)
@@ -2664,8 +2596,8 @@ sweep_bit_vectors_1 (Lisp_Object *prev,
          total_size += len;
           total_storage +=
            MALLOC_OVERHEAD +
-           FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Bit_Vector, bits,
-                                         BIT_VECTOR_LONG_STORAGE (len));
+           FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Bit_Vector, unsigned long,
+                                         bits, BIT_VECTOR_LONG_STORAGE (len));
          num_used++;
          /* #### May modify next on a C_READONLY bitvector */
          prev = &(bit_vector_next (v));
@@ -2692,12 +2624,10 @@ sweep_bit_vectors_1 (Lisp_Object *prev,
 #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;                                      \
                                                                        \
-  for (SFTB_prev = &current_##typename##_block,                                \
-       SFTB_current = current_##typename##_block,                      \
+  for (SFTB_current = current_##typename##_block,                      \
        SFTB_limit = current_##typename##_block_index;                  \
        SFTB_current;                                                   \
        )                                                               \
@@ -2708,7 +2638,7 @@ do {                                                                      \
        {                                                               \
          obj_type *SFTB_victim = &(SFTB_current->block[SFTB_iii]);     \
                                                                        \
-         if (FREE_STRUCT_P (SFTB_victim))                              \
+         if (LRECORD_FREE_P (SFTB_victim))                             \
            {                                                           \
              num_free++;                                               \
            }                                                           \
@@ -2727,7 +2657,6 @@ do {                                                                      \
              UNMARK_##typename (SFTB_victim);                          \
            }                                                           \
        }                                                               \
-      SFTB_prev = &(SFTB_current->prev);                               \
       SFTB_current = SFTB_current->prev;                               \
       SFTB_limit = countof (current_##typename##_block->block);                \
     }                                                                  \
@@ -2755,13 +2684,13 @@ do {                                                                            \
     {                                                                          \
       int SFTB_iii;                                                            \
       int SFTB_empty = 1;                                                      \
-      obj_type *SFTB_old_free_list = typename##_free_list;                     \
+      Lisp_Free *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))                                      \
+         if (LRECORD_FREE_P (SFTB_victim))                                     \
            {                                                                   \
              num_free++;                                                       \
              PUT_FIXED_TYPE_ON_FREE_LIST (typename, obj_type, SFTB_victim);    \
@@ -2951,7 +2880,7 @@ void
 free_marker (Lisp_Marker *ptr)
 {
   /* Perhaps this will catch freeing an already-freed marker. */
-  gc_checking_assert (ptr->lheader.type = lrecord_type_marker);
+  gc_checking_assert (ptr->lheader.type == lrecord_type_marker);
 
 #ifndef ALLOC_NO_POOLS
   FREE_FIXED_TYPE_WHEN_NOT_IN_GC (marker, Lisp_Marker, ptr);
@@ -2979,11 +2908,11 @@ verify_string_chars_integrity (void)
          int size;
          int fullsize;
 
-         /* If the string_chars struct is marked as free (i.e. the STRING
-            pointer is 0xFFFFFFFF) then this is an unused chunk of string
-             storage. (See below.) */
+         /* If the string_chars struct is marked as free (i.e. the
+            STRING pointer is NULL) then this is an unused chunk of
+            string storage. (See below.) */
 
-         if (FREE_STRUCT_P (s_chars))
+         if (STRING_CHARS_FREE_P (s_chars))
            {
              fullsize = ((struct unused_string_chars *) s_chars)->fullsize;
              pos += fullsize;
@@ -3030,16 +2959,16 @@ compact_string_chars (void)
          int size;
          int fullsize;
 
-         /* If the string_chars struct is marked as free (i.e. the STRING
-            pointer is 0xFFFFFFFF) then this is an unused chunk of string
-             storage.  This happens under Mule when a string's size changes
-            in such a way that its fullsize changes. (Strings can change
-            size because a different-length character can be substituted
-            for another character.) In this case, after the bogus string
-            pointer is the "fullsize" of this entry, i.e. how many bytes
-            to skip. */
+         /* If the string_chars struct is marked as free (i.e. the
+            STRING pointer is NULL) then this is an unused chunk of
+            string storage.  This happens under Mule when a string's
+            size changes in such a way that its fullsize changes.
+            (Strings can change size because a different-length
+            character can be substituted for another character.)
+            In this case, after the bogus string pointer is the
+            "fullsize" of this entry, i.e. how many bytes to skip. */
 
-         if (FREE_STRUCT_P (from_s_chars))
+         if (STRING_CHARS_FREE_P (from_s_chars))
            {
              fullsize = ((struct unused_string_chars *) from_s_chars)->fullsize;
              from_pos += fullsize;
@@ -3047,7 +2976,7 @@ compact_string_chars (void)
             }
 
           string = from_s_chars->string;
-         assert (!(FREE_STRUCT_P (string)));
+         assert (!(LRECORD_FREE_P (string)));
 
           size = string_length (string);
           fullsize = STRING_FULLSIZE (size);
@@ -3318,6 +3247,61 @@ restore_gc_inhibit (Lisp_Object val)
 /* Maybe we want to use this when doing a "panic" gc after memory_full()? */
 static int gc_hooks_inhibited;
 
+struct post_gc_action
+{
+  void (*fun) (void *);
+  void *arg;
+};
+
+typedef struct post_gc_action post_gc_action;
+
+typedef struct
+{
+  Dynarr_declare (post_gc_action);
+} post_gc_action_dynarr;
+
+static post_gc_action_dynarr *post_gc_actions;
+
+/* Register an action to be called at the end of GC.
+   gc_in_progress is 0 when this is called.
+   This is used when it is discovered that an action needs to be taken,
+   but it's during GC, so it's not safe. (e.g. in a finalize method.)
+
+   As a general rule, do not use Lisp objects here.
+   And NEVER signal an error.
+*/
+
+void
+register_post_gc_action (void (*fun) (void *), void *arg)
+{
+  post_gc_action action;
+
+  if (!post_gc_actions)
+    post_gc_actions = Dynarr_new (post_gc_action);
+
+  action.fun = fun;
+  action.arg = arg;
+
+  Dynarr_add (post_gc_actions, action);
+}
+
+static void
+run_post_gc_actions (void)
+{
+  int i;
+
+  if (post_gc_actions)
+    {
+      for (i = 0; i < Dynarr_length (post_gc_actions); i++)
+       {
+         post_gc_action action = Dynarr_at (post_gc_actions, i);
+         (action.fun) (action.arg);
+       }
+
+      Dynarr_reset (post_gc_actions);
+    }
+}
+
 \f
 void
 garbage_collect_1 (void)
@@ -3407,6 +3391,7 @@ garbage_collect_1 (void)
   /***** Now we actually start the garbage collection. */
 
   gc_in_progress = 1;
+  inhibit_non_essential_printing_operations = 1;
 
   gc_generation_number[0]++;
 
@@ -3444,11 +3429,17 @@ garbage_collect_1 (void)
   /* Mark all the special slots that serve as the roots of accessibility. */
 
   { /* staticpro() */
-    int i;
-    for (i = 0; i < staticidx; i++)
-      mark_object (*(staticvec[i]));
-    for (i = 0; i < staticidx_nodump; i++)
-      mark_object (*(staticvec_nodump[i]));
+    Lisp_Object **p = Dynarr_begin (staticpros);
+    size_t count;
+    for (count = Dynarr_length (staticpros); count; count--)
+      mark_object (**p++);
+  }
+
+  { /* staticpro_nodump() */
+    Lisp_Object **p = Dynarr_begin (staticpros_nodump);
+    size_t count;
+    for (count = Dynarr_length (staticpros_nodump); count; count--)
+      mark_object (**p++);
   }
 
   { /* GCPRO() */
@@ -3485,7 +3476,7 @@ garbage_collect_1 (void)
        int i;
 
        mark_object (*backlist->function);
-       if (nargs == UNEVALLED || nargs == MANY)
+       if (nargs < 0 /* nargs == UNEVALLED || nargs == MANY */)
          mark_object (backlist->args[0]);
        else
          for (i = 0; i < nargs; i++)
@@ -3525,8 +3516,11 @@ garbage_collect_1 (void)
     gc_cons_threshold = 10000;
 #endif
 
+  inhibit_non_essential_printing_operations = 0;
   gc_in_progress = 0;
 
+  run_post_gc_actions ();
+
   /******* End of garbage collection ********/
 
   run_hook_trapping_errors ("Error in post-gc-hook", Qpost_gc_hook);
@@ -3603,7 +3597,7 @@ Garbage collection happens automatically if you cons more than
        ())
 {
   Lisp_Object pl = Qnil;
-  int i;
+  unsigned int i;
   int gc_count_vector_total_size = 0;
 
   garbage_collect_1 ();
@@ -3618,7 +3612,7 @@ Garbage collection happens automatically if you cons more than
           const char *name = lrecord_implementations_table[i]->name;
          int len = strlen (name);
          /* save this for the FSFmacs-compatible part of the summary */
-         if (i == lrecord_vector.lrecord_type_index)
+         if (i == lrecord_type_vector)
            gc_count_vector_total_size =
              lcrecord_stats[i].bytes_in_use + lcrecord_stats[i].bytes_freed;
 
@@ -3720,7 +3714,7 @@ If this value exceeds `gc-cons-threshold', a garbage collection happens.
 }
 
 #if 0
-DEFUN ("memory-limit", Fmemory_limit, 0, 0, "", /*
+DEFUN ("memory-limit", Fmemory_limit, 0, 0, 0, /*
 Return the address of the last byte Emacs has allocated, divided by 1024.
 This may be helpful in debugging Emacs's memory usage.
 The value is divided by 1024 to make sure it will fit in a lisp integer.
@@ -3894,8 +3888,8 @@ reinit_alloc_once_early (void)
 #ifdef DOUG_LEA_MALLOC
   mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */
   mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */
-#if 0 /* Moved to emacs.c */
-  mallopt (M_MMAP_MAX, 64); /* max. number of mmap'ed areas */
+#if 1 /* Moved to emacs.c */
+  mallopt (M_MMAP_MAX, 0); /* max. number of mmap'ed areas */
 #endif
 #endif
   init_string_alloc ();
@@ -3912,9 +3906,10 @@ reinit_alloc_once_early (void)
 
   ignore_malloc_warnings = 0;
 
-  staticidx_nodump = 0;
-  dumpstructidx = 0;
-  pdump_wireidx = 0;
+  if (staticpros_nodump)
+    Dynarr_free (staticpros_nodump);
+  staticpros_nodump = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *);
+  Dynarr_resize (staticpros_nodump, 100); /* merely a small optimization */
 
   consing_since_gc = 0;
 #if 1
@@ -3922,10 +3917,6 @@ reinit_alloc_once_early (void)
 #else
   gc_cons_threshold = 15000; /* debugging */
 #endif
-#ifdef VIRT_ADDR_VARIES
-  malloc_sbrk_unused = 1<<22;  /* A large number */
-  malloc_sbrk_used = 100000;   /* as reasonable as any number */
-#endif /* VIRT_ADDR_VARIES */
   lrecord_uid_counter = 259;
   debug_string_purity = 0;
   gcprolist = 0;
@@ -3960,11 +3951,11 @@ init_alloc_once_early (void)
   INIT_LRECORD_IMPLEMENTATION (string);
   INIT_LRECORD_IMPLEMENTATION (lcrecord_list);
 
-  staticidx = 0;
+  staticpros = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *);
+  Dynarr_resize (staticpros, 1410); /* merely a small optimization */
+  dump_add_root_struct_ptr (&staticpros, &staticpros_description);
 }
 
-int pure_bytes_used = 0;
-
 void
 reinit_alloc (void)
 {
@@ -3974,9 +3965,9 @@ reinit_alloc (void)
 void
 syms_of_alloc (void)
 {
-  defsymbol (&Qpre_gc_hook, "pre-gc-hook");
-  defsymbol (&Qpost_gc_hook, "post-gc-hook");
-  defsymbol (&Qgarbage_collecting, "garbage-collecting");
+  DEFSYMBOL (Qpre_gc_hook);
+  DEFSYMBOL (Qpost_gc_hook);
+  DEFSYMBOL (Qgarbage_collecting);
 
   DEFSUBR (Fcons);
   DEFSUBR (Flist);
@@ -4016,20 +4007,6 @@ prevent garbage collection during a part of the program.
 See also `consing-since-gc'.
 */ );
 
-  DEFVAR_INT ("pure-bytes-used", &pure_bytes_used /*
-Number of bytes of sharable Lisp data allocated so far.
-*/ );
-
-#if 0
-  DEFVAR_INT ("data-bytes-used", &malloc_sbrk_used /*
-Number of bytes of unshared memory allocated in this session.
-*/ );
-
-  DEFVAR_INT ("data-bytes-free", &malloc_sbrk_unused /*
-Number of bytes of unshared memory remaining available in this session.
-*/ );
-#endif
-
 #ifdef DEBUG_XEMACS
   DEFVAR_INT ("debug-allocation", &debug_allocation /*
 If non-zero, print out information to stderr about all objects allocated.