home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / alloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-29  |  103.3 KB  |  3,610 lines

  1. /* Storage allocation and gc for XEmacs Lisp interpreter.
  2.    Copyright (C) 1985, 1986, 1988, 1992, 1993, 1994
  3.    Free Software Foundation, Inc.
  4.  
  5. This file is part of XEmacs.
  6.  
  7. XEmacs is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with XEmacs; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* Synched up with: FSF 19.28, Mule 2.0.  Substantially different from
  22.    FSF. */
  23.  
  24. #include <config.h>
  25. #include "lisp.h"
  26.  
  27. #ifndef standalone
  28. #ifdef HAVE_X_WINDOWS
  29. #include "device-x.h"
  30. #endif    /* HAVE_X_WINDOWS */
  31. #ifdef HAVE_NEXTSTEP
  32. #include "device-ns.h"
  33. #endif  /* HAVE_NEXTSTEP */
  34. #include "backtrace.h"
  35. #include "buffer.h"
  36. #include "bytecode.h"
  37. #include "elhash.h"
  38. #include "events.h"
  39. #include "extents.h"
  40. #include "frame.h"
  41. #include "redisplay.h"
  42. #include "specifier.h"
  43. #include "window.h"
  44. #endif
  45.  
  46. /* Define this to see where all that space is going... */
  47. #define PURESTAT
  48.  
  49. /* Define this to use malloc/free with no freelist for all datatypes,
  50.    the hope being that some debugging tools may help detect
  51.    freed memory references */
  52. /* #define ALLOC_NO_POOLS */
  53.  
  54. #include "puresize.h"
  55.  
  56. #define ALIGN_SIZE(len,unit) \
  57.   ((((len) + (unit) - 1) / (unit)) * (unit))
  58.  
  59. /* Number of bytes of consing done since the last gc */
  60. int consing_since_gc;
  61. #ifdef EMACS_BTL
  62. extern void cadillac_record_backtrace ();
  63. #define INCREMENT_CONS_COUNTER(size)        \
  64.   do {                        \
  65.     int __sz__ = ((int) (size));        \
  66.     consing_since_gc += __sz__;            \
  67.     cadillac_record_backtrace (2, __sz__);    \
  68.   } while (0)
  69. #else
  70. #define INCREMENT_CONS_COUNTER(size) (consing_since_gc += (size))
  71. #endif
  72.  
  73. /* Number of bytes of consing since gc before another gc should be done. */
  74. int gc_cons_threshold;
  75.  
  76. /* Nonzero during gc */
  77. int gc_in_progress;
  78.  
  79. /* Number of times GC has happened at this level or below.
  80.  * Level 0 is most volatile, contrary to usual convention.
  81.  *  (Of course, there's only one level at present) */
  82. int gc_generation_number[1];
  83.  
  84. /* This is just for use by the printer, to allow things to print uniquely */
  85. static int lrecord_uid_counter;
  86.  
  87. /* Nonzero when calling the hooks in Energize-beta */
  88. int gc_currently_forbidden;
  89.  
  90. /* Hooks. */
  91. Lisp_Object Vpre_gc_hook, Qpre_gc_hook;
  92. Lisp_Object Vpost_gc_hook, Qpost_gc_hook;
  93.  
  94. /* "Garbage collecting" */
  95. Lisp_Object Vgc_message;
  96. static CONST char gc_default_message[] = "Garbage collecting";
  97. Lisp_Object Qgarbage_collecting;
  98.  
  99. #ifndef VIRT_ADDR_VARIES
  100. extern
  101. #endif /* VIRT_ADDR_VARIES */
  102.  int malloc_sbrk_used;
  103.  
  104. #ifndef VIRT_ADDR_VARIES
  105. extern
  106. #endif /* VIRT_ADDR_VARIES */
  107.  int malloc_sbrk_unused;
  108.  
  109. /* Non-zero means defun should do purecopy on the function definition */
  110. int purify_flag;
  111.  
  112. extern Lisp_Object pure[];/* moved to pure.c to speed incremental linking */
  113.  
  114. #define PUREBEG ((unsigned char *) pure)
  115.  
  116. /* Index in pure at which next pure object will be allocated. */
  117. static long pureptr;
  118.  
  119. #define PURIFIED(ptr)                            \
  120.    ((PNTR_COMPARISON_TYPE) (ptr) <                    \
  121.     (PNTR_COMPARISON_TYPE) (PUREBEG + PURESIZE) &&            \
  122.     (PNTR_COMPARISON_TYPE) (ptr) >=                    \
  123.     (PNTR_COMPARISON_TYPE) PUREBEG)
  124.  
  125. /* Non-zero if pureptr > PURESIZE; accounts for excess purespace needs. */
  126. static long pure_lossage;
  127.  
  128. int
  129. purified (Lisp_Object obj)
  130. {
  131.   if (!POINTER_TYPE_P (XGCTYPE (obj)))
  132.     return (0);
  133.   return (PURIFIED (XPNTR (obj)));
  134. }
  135.  
  136. static int
  137. check_purespace (int size)
  138. {
  139.   if (pure_lossage)
  140.     {
  141.       pure_lossage += size;
  142.       return (0);
  143.     }
  144.   else if (pureptr + size > PURESIZE)
  145.     {
  146.       message ("\nERROR:  Pure Lisp storage exhausted!\n");
  147.       pure_lossage = size;
  148.       return (0);
  149.     }
  150.   else
  151.     return (1);
  152. }
  153.  
  154.  
  155.  
  156. #ifndef PURESTAT
  157.  
  158. #define bump_purestat(p,b) do {} while (0) /* Do nothing */
  159.  
  160. #else /* PURESTAT */
  161.  
  162. static int purecopying_for_bytecode;
  163.  
  164. static int pure_sizeof (Lisp_Object /*, int recurse */);
  165.  
  166. /* Keep statistics on how much of what is in purespace */
  167. struct purestat
  168. {
  169.   int nobjects;
  170.   int nbytes;
  171.   CONST char *name;
  172. };
  173.  
  174. #define FMH(s,n) static struct purestat s = { 0, 0, n }
  175. FMH (purestat_cons, "cons cells:");
  176. FMH (purestat_float, "float objects:");
  177. FMH (purestat_string_pname, "symbol-name strings:");
  178. FMH (purestat_bytecode, "function objects:");
  179. FMH (purestat_string_bytecodes, "byte-code strings:");
  180. FMH (purestat_vector_bytecode_constants, "byte-constant vectors:");
  181. FMH (purestat_string_interactive, "interactive strings:");
  182. #ifdef I18N3
  183. FMH (purestat_string_domain, "domain strings:");
  184. #endif
  185. FMH (purestat_string_documentation, "documentation strings:");
  186. FMH (purestat_string_other_function, "other function strings:");
  187. FMH (purestat_vector_other, "other vectors:");
  188. FMH (purestat_string_other, "other strings:");
  189. FMH (purestat_string_all, "all strings:");
  190. FMH (purestat_vector_all, "all vectors:");
  191.  
  192. static struct purestat *purestats[] =
  193. {
  194.   &purestat_cons,
  195.   &purestat_float,
  196.   &purestat_string_pname,
  197.   &purestat_bytecode,
  198.   &purestat_string_bytecodes,
  199.   &purestat_vector_bytecode_constants,
  200.   &purestat_string_interactive,
  201. #ifdef I18N3
  202.   &purestat_string_domain,
  203. #endif
  204.   &purestat_string_documentation,
  205.   &purestat_string_other_function,
  206.   &purestat_vector_other,
  207.   &purestat_string_other,
  208.   0,
  209.   &purestat_string_all,
  210.   &purestat_vector_all
  211. };
  212. #undef FMH
  213.  
  214. static void
  215. bump_purestat (struct purestat *purestat, int nbytes)
  216. {
  217.   if (pure_lossage) return;
  218.   purestat->nobjects += 1;
  219.   purestat->nbytes += nbytes;
  220. }
  221. #endif /* PURESTAT */
  222.  
  223.  
  224. /* Maximum amount of C stack to save when a GC happens.  */
  225.  
  226. #ifndef MAX_SAVE_STACK
  227. #define MAX_SAVE_STACK 16000
  228. #endif
  229.  
  230. /* Buffer in which we save a copy of the C stack at each GC.  */
  231.  
  232. static char *stack_copy;
  233. static int stack_copy_size;
  234.  
  235. /* Non-zero means ignore malloc warnings.  Set during initialization.  */
  236. int ignore_malloc_warnings;
  237.  
  238. static void mark_object (Lisp_Object obj);
  239.  
  240. static void *breathing_space;
  241.  
  242. void
  243. release_breathing_space (void)
  244. {
  245.   if (breathing_space) 
  246.     {
  247.       void *tmp = breathing_space;
  248.       breathing_space = 0;
  249.       xfree (tmp);
  250.     }
  251. }
  252.  
  253. /* malloc calls this if it finds we are near exhausting storage */
  254. void
  255. malloc_warning (CONST char *str)
  256. {
  257.   if (ignore_malloc_warnings)
  258.     return;
  259.  
  260.   warn_when_safe
  261.     (Qmemory, Qcritical,
  262.      "%s\n"
  263.      "Killing some buffers may delay running out of memory.\n"
  264.      "However, certainly by the time you receive the 95%% warning,\n"
  265.      "you should clean up, kill this Emacs, and start a new one.",
  266.      str);
  267. }
  268.  
  269. /* Called if malloc returns zero */
  270. DOESNT_RETURN
  271. memory_full (void)
  272. {
  273.   /* Force a GC next time eval is called.
  274.      It's better to loop garbage-collecting (we might reclaim enough
  275.      to win) than to loop beeping and barfing "Memory exhausted"
  276.    */
  277.   consing_since_gc = gc_cons_threshold + 1;
  278.   release_breathing_space ();
  279.  
  280. #ifndef standalone
  281.   /* Flush some histories which might conceivably contain
  282.    *  garbalogical inhibitors */
  283.   if (!NILP (Fboundp (Qvalues)))
  284.     Fset (Qvalues, Qnil);
  285.   Vcommand_history = Qnil;
  286. #endif
  287.  
  288.   error ("Memory exhausted");
  289. }
  290.  
  291. /* like malloc and realloc but check for no memory left, and block input. */
  292.  
  293. void *
  294. xmalloc (int size)
  295. {
  296.   void *val;
  297.  
  298.   val = (void *) malloc (size);
  299.  
  300.   if (!val && (size != 0)) memory_full ();
  301.   return val;
  302. }
  303.  
  304. void *
  305. xmalloc_and_zero (int size)
  306. {
  307.   void *val = xmalloc (size);
  308.   memset (val, 0, size);
  309.   return val;
  310. }
  311.  
  312. void *
  313. xrealloc (void *block, int size)
  314. {
  315.   void *val;
  316.  
  317.   /* We must call malloc explicitly when BLOCK is 0, since some
  318.      reallocs don't do this.  */
  319.   if (! block)
  320.     val = (void *) malloc (size);
  321.   else
  322.     val = (void *) realloc (block, size);
  323.  
  324.   if (!val && (size != 0)) memory_full ();
  325.   return val;
  326. }
  327.  
  328. void
  329. #ifdef ERROR_CHECK_MALLOC
  330. xfree_1 (void *block)
  331. #else
  332. xfree (void *block)
  333. #endif
  334. {
  335. #ifdef ERROR_CHECK_MALLOC
  336.   /* Unbelievably, calling free() on 0xDEADBEEF doesn't cause an
  337.      error until much later on for many system mallocs, such as
  338.      the one that comes with Solaris 2.3.  FMH!! */
  339.   assert (block != (void *) 0xDEADBEEF);
  340. #endif
  341.   free (block);
  342. }
  343.  
  344. #if INTBITS == 32
  345. # define FOUR_BYTE_TYPE unsigned int
  346. #elif LONGBITS == 32
  347. # define FOUR_BYTE_TYPE unsigned long
  348. #elif SHORTBITS == 32
  349. # define FOUR_BYTE_TYPE unsigned short
  350. #else
  351. What kind of strange-ass system are we running on?
  352. #endif
  353.  
  354. #ifdef ERROR_CHECK_GC
  355.  
  356. #ifdef WORDS_BIGENDIAN
  357. static unsigned char deadbeef_as_char[] = {0xDE, 0xAD, 0xBE, 0xEF};
  358. #else
  359. static unsigned char deadbeef_as_char[] = {0xEF, 0xBE, 0xAD, 0xDE};
  360. #endif
  361.  
  362. static void
  363. deadbeef_memory (void *ptr, unsigned long size)
  364. {
  365.   unsigned long long_length = size / sizeof (FOUR_BYTE_TYPE);
  366.   unsigned long i;
  367.   unsigned long bytes_left_over = size - sizeof (FOUR_BYTE_TYPE) * long_length;
  368.   
  369.   for (i = 0; i < long_length; i++)
  370.     ((FOUR_BYTE_TYPE *) ptr)[i] = 0xdeadbeef;
  371.   for (i = i; i < bytes_left_over; i++)
  372.     ((unsigned char *) ptr + long_length)[i] = deadbeef_as_char[i];
  373. }
  374.  
  375. #else
  376.  
  377. #define deadbeef_memory(ptr, size)
  378.  
  379. #endif
  380.  
  381. char *
  382. xstrdup (CONST char *str)
  383. {
  384.   char *val;
  385.   int len = strlen (str) + 1;   /* for stupid terminating 0 */
  386.  
  387.   val = xmalloc (len);
  388.   if (val == 0) return 0;
  389.   memcpy (val, str, len);
  390.   return (val);
  391. }
  392.  
  393. #ifdef NEED_STRDUP
  394. char *
  395. strdup (CONST char *s)
  396. {
  397.   return xstrdup (s);
  398. }
  399. #endif /* NEED_STRDUP */
  400.  
  401.  
  402. static void *
  403. allocate_lisp_storage (int size)
  404. {
  405.   void *p = xmalloc (size);
  406.   char *lim = ((char *) p) + size;
  407.   Lisp_Object val = Qnil;
  408.  
  409.   XSETCONS (val, lim);
  410.   if ((char *) XCONS (val) != lim)
  411.     {
  412.       xfree (p);
  413.       memory_full ();
  414.     }
  415.   return (p);
  416. }
  417.  
  418.  
  419. #define MARKED_RECORD_HEADER_P(lheader) \
  420.   (((lheader)->implementation->finalizer) == this_marks_a_marked_record)
  421. #define UNMARKABLE_RECORD_HEADER_P(lheader) \
  422.   (((lheader)->implementation->marker) == this_one_is_unmarkable)
  423. #define MARK_RECORD_HEADER(lheader) \
  424.   do { (((lheader)->implementation)++); } while (0)
  425. #define UNMARK_RECORD_HEADER(lheader) \
  426.   do { (((lheader)->implementation)--); } while (0)
  427.  
  428.  
  429. /* lrecords are chained together through their "next.v" field.
  430.  * After doing the mark phase, the GC will walk this linked
  431.  *  list and free any record which hasn't been marked 
  432.  */
  433. static struct lcrecord_header *all_lcrecords;
  434.  
  435. void *
  436. alloc_lcrecord (int size, CONST struct lrecord_implementation *implementation)
  437. {
  438.   struct lcrecord_header *lcheader;
  439.  
  440.   if (size <= 0) abort ();
  441.   if (implementation->static_size == 0)
  442.     {
  443.       if (!implementation->size_in_bytes_method)
  444.     abort ();
  445.     }
  446.   else if (implementation->static_size != size)
  447.     abort ();
  448.  
  449.   lcheader = allocate_lisp_storage (size);
  450.   lcheader->lheader.implementation = implementation;
  451.   lcheader->next = all_lcrecords;
  452. #if 1                           /* mly prefers to see small ID numbers */
  453.   lcheader->uid = lrecord_uid_counter++;
  454. #else                /* jwz prefers to see real addrs */
  455.   lcheader->uid = (int) &lcheader;
  456. #endif
  457.   all_lcrecords = lcheader;
  458.   INCREMENT_CONS_COUNTER (size);
  459.   return (lcheader);
  460. }
  461.  
  462. #if 0 /* Presently unused */
  463. /* Very, very poor man's EGC?
  464.  * This may be slow and thrash pages all over the place.
  465.  *  Only call it if you really feel you must (and if the
  466.  *  lrecord was fairly recently allocated).
  467.  * Otherwise, just let the GC do its job -- that's what it's there for
  468.  */
  469. void
  470. free_lcrecord (struct lcrecord_header *lcrecord)
  471. {
  472.   if (all_lcrecords == lcrecord)
  473.     {
  474.       all_lcrecords = lcrecord->next;
  475.     }
  476.   else
  477.     {
  478.       struct lrecord_header *header = all_lcrecords;
  479.       for (;;)
  480.     {
  481.       struct lrecord_header *next = header->next;
  482.       if (next == lcrecord)
  483.         {
  484.           header->next = lrecord->next;
  485.           break;
  486.         }
  487.       else if (next == 0)
  488.         abort ();
  489.       else
  490.         header = next;
  491.     }
  492.     }
  493.   if (lrecord->implementation->finalizer)
  494.     ((lrecord->implementation->finalizer) (lrecord, 0));
  495.   xfree (lrecord);
  496.   return;
  497. }
  498. #endif /* Unused */
  499.  
  500.  
  501. static void
  502. disksave_object_finalization_1 (void)
  503. {
  504.   struct lcrecord_header *header;
  505.  
  506.   for (header = all_lcrecords; header; header = header->next)
  507.     {
  508.       if (header->lheader.implementation->finalizer)
  509.     ((header->lheader.implementation->finalizer) (header, 1));
  510.     }
  511. }
  512.   
  513.  
  514. /* This must not be called -- it just serves as for EQ test
  515.  *  If lheader->implementation->finalizer is this_marks_a_marked_record,
  516.  *  then lrecord has been marked by the GC sweeper
  517.  * header->implementation is put back to its correct value by
  518.  *  sweep_records */
  519. void
  520. this_marks_a_marked_record (void *dummy0, int dummy1)
  521. {
  522.   abort ();
  523. }
  524.  
  525. /* Semi-kludge -- lrecord_symbol_value_forward objects get stuck
  526.    in CONST space and you get SEGV's if you attempt to mark them.
  527.    This sits in lheader->implementation->marker. */
  528.  
  529. Lisp_Object
  530. this_one_is_unmarkable (Lisp_Object obj, void (*markobj) (Lisp_Object))
  531. {
  532.   abort ();
  533.   return Qnil;
  534. }
  535.  
  536. /* XGCTYPE for records */
  537. int
  538. gc_record_type_p (Lisp_Object frob, CONST struct lrecord_implementation *type)
  539. {
  540.   return (XGCTYPE (frob) == Lisp_Record
  541.           && (XRECORD_LHEADER (frob)->implementation == type
  542.               || XRECORD_LHEADER (frob)->implementation == type + 1));
  543. }
  544.  
  545.  
  546. /**********************************************************************/
  547. /*                        Fixed-size type macros                      */
  548. /**********************************************************************/
  549.  
  550. /* For fixed-size types that are commonly used, we malloc() large blocks
  551.    of memory at a time and subdivide them into chunks of the correct
  552.    size for an object of that type.  This is more efficient than
  553.    malloc()ing each object separately because we save on malloc() time
  554.    and overhead due to the fewer number of malloc()ed blocks, and
  555.    also because we don't need any extra pointers within each object
  556.    to keep them threaded together for GC purposes.  For less common
  557.    (and frequently large-size) types, we use lcrecords, which are
  558.    malloc()ed individually and chained together through a pointer
  559.    in the lcrecord header.  lcrecords do not need to be fixed-size
  560.    (i.e. two objects of the same type need not have the same size;
  561.    however, the size of a particular object cannot vary dynamically).
  562.    It is also much easier to create a new lcrecord type because no
  563.    additional code needs to be added to alloc.c.  Finally, lcrecords
  564.    may be more efficient when there are only a small number of them.
  565.  
  566.    The types that are stored in these large blocks (or "frob blocks")
  567.    are cons, float, bytecode, symbol, marker, extent, extent replica,
  568.    event, and string.
  569.  
  570.    Note that strings are special in that they are actually stored in
  571.    two parts: a structure containing information about the string, and
  572.    the actual data associated with the string.  The former structure
  573.    (a struct Lisp_String) is a fixed-size structure and is managed the
  574.    same way as all the other such types.  This structure contains a
  575.    pointer to the actual string data, which is stored in structures of
  576.    type struct string_chars_block.  Each string_chars_block consists
  577.    of a pointer to a struct Lisp_String, followed by the data for that
  578.    string, followed by another pointer to a struct Lisp_String,
  579.    followed by the data for that string, etc.  At GC time, the data in
  580.    these blocks is compacted by searching sequentially through all the
  581.    blocks and compressing out any holes created by unmarked strings.
  582.    Strings that are more than a certain size (bigger than the size of
  583.    a string_chars_block, although something like half as big might
  584.    make more sense) are malloc()ed separately and not stored in
  585.    string_chars_blocks.  Furthermore, no one string stretches across
  586.    two string_chars_blocks.
  587.  
  588.    Vectors are each malloc()ed separately, similar to lcrecords.
  589.    
  590.    In the following discussion, we use conses, but it applies equally
  591.    well to the other fixed-size types.
  592.  
  593.    We store cons cells inside of cons_blocks, allocating a new
  594.    cons_block with malloc() whenever necessary.  Cons cells reclaimed
  595.    by GC are put on a free list to be reallocated before allocating
  596.    any new cons cells from the latest cons_block.  Each cons_block is
  597.    just under 2^n - MALLOC_OVERHEAD bytes long, since malloc (at least
  598.    the versions in malloc.c and gmalloc.c) really allocates in units
  599.    of powers of two and uses 4 bytes for its own overhead.
  600.  
  601.    What GC actually does is to search through all the cons_blocks,
  602.    from the most recently allocated to the oldest, and put all
  603.    cons cells that are not marked (whether or not they're already
  604.    free) on a cons_free_list.  The cons_free_list is a stack, and
  605.    so the cons cells in the oldest-allocated cons_block end up
  606.    at the head of the stack and are the first to be reallocated.
  607.    If any cons_block is entirely free, it is freed with free()
  608.    and its cons cells removed from the cons_free_list.  Because
  609.    the cons_free_list ends up basically in memory order, we have
  610.    a high locality of reference (assuming a reasonable turnover
  611.    of allocating and freeing) and have a reasonable probability
  612.    of entirely freeing up cons_blocks that have been more recently
  613.    allocated.  This stage is called the "sweep stage" of GC, and
  614.    is executed after the "mark stage", which involves starting
  615.    from all places that are known to point to in-use Lisp objects
  616.    (e.g. the obarray, where are all symbols are stored; the
  617.    current catches and condition-cases; the backtrace list of
  618.    currently executing functions; the gcpro list; etc.) and
  619.    recursively marking all objects that are accessible.
  620.  
  621.    At the beginning of the sweep stage, the conses in the cons
  622.    blocks are in one of three states: in use and marked, in use
  623.    but not marked, and not in use (already freed).  Any conses
  624.    that are marked have been marked in the mark stage just
  625.    executed, because as part of the sweep stage we unmark any
  626.    marked objects.  The way we tell whether or not a cons cell
  627.    is in use is through the FREE_STRUCT_P macro.  This basically
  628.    looks at the first 4 bytes (or however many bytes a pointer
  629.    fits in) to see if all the bits in those bytes are 1.  The
  630.    resulting value (0xFFFFFFFF) is not a valid pointer and is
  631.    not a valid Lisp_Object.  All current fixed-size types have
  632.    a pointer or Lisp_Object as their first element with the
  633.    exception of strings; they have a size value, which can
  634.    never be less than zero, and so 0xFFFFFFFF is invalid for
  635.    strings as well.  Now assuming that a cons cell is in use,
  636.    the way we tell whether or not it is marked is to look at
  637.    the mark bit of its car (each Lisp_Object has one bit
  638.    reserved as a mark bit, in case it's needed).  Note that
  639.    different types of objects use different fields to indicate
  640.    whether the object is marked, but the principle is the same.
  641.  
  642.    Conses on the free_cons_list are threaded through a pointer
  643.    stored in the bytes directly after the bytes that are set
  644.    to 0xFFFFFFFF (we cannot overwrite these because the cons
  645.    is still in a cons_block and needs to remain marked as
  646.    not in use for the next time that GC happens).  This
  647.    implies that all fixed-size types must be at least big
  648.    enough to store two pointers, which is indeed the case
  649.    for all current fixed-size types.
  650.  
  651.    Some types of objects need additional "finalization" done
  652.    when an object is converted from in use to not in use;
  653.    this is the purpose of the ADDITIONAL_FREE_type macro.
  654.    For example, markers need to be removed from the chain
  655.    of markers that is kept in each buffer.  This is because
  656.    markers in a buffer automatically disappear if the marker
  657.    is no longer referenced anywhere (the same does not
  658.    apply to extents, however).
  659.  
  660.    When ERROR_CHECK_GC is defined, we do things differently
  661.    so as to maximize our chances of catching places where
  662.    there is insufficient GCPROing.  The thing we want to
  663.    avoid is having an object that we're using but didn't
  664.    GCPRO get freed by GC and then reallocated while we're
  665.    in the process of using it -- this will result in something
  666.    seemingly unrelated getting trashed, and is extremely
  667.    difficult to track down.  If the object gets freed but
  668.    not reallocated, we can usually catch this because we
  669.    set all bytes of a freed object to 0xDEADBEEF. (The
  670.    first four bytes, however, are 0xFFFFFFFF, and the next
  671.    four are a pointer used to chain freed objects together;
  672.    we play some tricks with this pointer to make it more
  673.    bogus, so crashes are more likely to occur right away.)
  674.  
  675.    We want freed objects to stay free as long as possible,
  676.    so instead of doing what we do above, we maintain the
  677.    free objects in a first-in first-out queue.  We also
  678.    don't recompute the free list each GC, unlike above;
  679.    this ensures that the queue ordering is preserved.
  680.    [This means that we are likely to have worse locality
  681.    of reference, and that we can never free a frob block
  682.    once it's allocated. (Even if we know that all cells
  683.    in it are free, there's no easy way to remove all those
  684.    cells from the free list because the objects on the
  685.    free list are unlikely to be in memory order.)]
  686.    Furthermore, we never take objects off the free list
  687.    unless there's a large number (usually 1000, but
  688.    varies depending on type) of them already on the list.
  689.    This way, we ensure that an object that gets freed will
  690.    remain free for the next 1000 (or whatever) times that
  691.    an object of that type is allocated.
  692. */
  693.  
  694. #ifndef MALLOC_OVERHEAD
  695. #define MALLOC_OVERHEAD 4
  696. #endif
  697.  
  698. #ifdef ALLOC_NO_POOLS
  699. # define TYPE_ALLOC_SIZE(type, structtype) 1
  700. #else
  701. # define TYPE_ALLOC_SIZE(type, structtype)            \
  702.     ((2048 - MALLOC_OVERHEAD - sizeof (struct type##_block *))    \
  703.      / sizeof (structtype))
  704. #endif
  705.  
  706. #define DECLARE_FIXED_TYPE_ALLOC(type, structtype)              \
  707.                                       \
  708. struct type##_block                              \
  709. {                                      \
  710.   struct type##_block *prev;                          \
  711.   structtype block[TYPE_ALLOC_SIZE (type, structtype)];              \
  712. };                                      \
  713.                                       \
  714. static struct type##_block *current_##type##_block;              \
  715. static int current_##type##_block_index;                  \
  716.                                       \
  717. static structtype *type##_free_list;                      \
  718. static structtype *type##_free_list_tail;                  \
  719.                                       \
  720. static void                                  \
  721. init_##type##_alloc (void)                          \
  722. {                                      \
  723.   current_##type##_block = 0;                          \
  724.   current_##type##_block_index = countof (current_##type##_block->block); \
  725.   type##_free_list = 0;                              \
  726.   type##_free_list_tail = 0;                          \
  727. }                                      \
  728.                                       \
  729. static int gc_count_num_##type##_in_use, gc_count_num_##type##_freelist
  730.  
  731. #define ALLOCATE_FIXED_TYPE_FROM_BLOCK(type, result)            \
  732.   do {                                    \
  733.     if (current_##type##_block_index                    \
  734.     == countof (current_##type##_block->block))            \
  735.     {                                    \
  736.       struct type##_block *__new__                    \
  737.          = allocate_lisp_storage (sizeof (struct type##_block));    \
  738.       __new__->prev = current_##type##_block;                \
  739.       current_##type##_block = __new__;                    \
  740.       current_##type##_block_index = 0;                    \
  741.     }                                    \
  742.     (result) =                                \
  743.       &(current_##type##_block->block[current_##type##_block_index++]);    \
  744.   } while (0)
  745.  
  746. /* Allocate an instance of a type that is stored in blocks.
  747.    TYPE is the "name" of the type, STRUCTTYPE is the corresponding
  748.    structure type. */
  749.  
  750. #ifdef ERROR_CHECK_GC
  751.  
  752. /* Note: if you get crashes in this function, suspect incorrect calls
  753.    to free_cons() and friends.  This happened once because the cons
  754.    cell was not GC-protected and was getting collected before
  755.    free_cons() was called. */
  756.  
  757. #define ALLOCATE_FIXED_TYPE(type, structtype, result)             \
  758. do                                     \
  759. {                                     \
  760.   if (gc_count_num_##type##_freelist >                     \
  761.       MINIMUM_ALLOWED_FIXED_TYPE_CELLS_##type)                  \
  762.     {                                     \
  763.       result = type##_free_list;                     \
  764.       /* Before actually using the chain pointer, we complement all its     \
  765.          bits; see FREE_FIXED_TYPE(). */                 \
  766.       type##_free_list =                         \
  767.         (structtype *) ~(unsigned long)                     \
  768.           (* (structtype **) ((char *) result + sizeof (void *)));     \
  769.       gc_count_num_##type##_freelist--;                     \
  770.     }                                     \
  771.   else                                     \
  772.     ALLOCATE_FIXED_TYPE_FROM_BLOCK (type, result);             \
  773.   MARK_STRUCT_AS_NOT_FREE (result);                     \
  774.   INCREMENT_CONS_COUNTER (sizeof (structtype));                 \
  775. } while (0)
  776.  
  777. #else
  778.  
  779. #define ALLOCATE_FIXED_TYPE(type, structtype, result)        \
  780. do                                \
  781. {                                \
  782.   if (type##_free_list)                        \
  783.     {                                \
  784.       result = type##_free_list;                \
  785.       type##_free_list =                    \
  786.         * (structtype **) ((char *) result + sizeof (void *));    \
  787.     }                                \
  788.   else                                \
  789.     ALLOCATE_FIXED_TYPE_FROM_BLOCK (type, result);        \
  790.   MARK_STRUCT_AS_NOT_FREE (result);                \
  791.   INCREMENT_CONS_COUNTER (sizeof (structtype));            \
  792. } while (0)
  793.  
  794. #endif
  795.  
  796. /* INVALID_POINTER_VALUE should be a value that is invalid as a pointer
  797.    to a Lisp object and invalid as an actual Lisp_Object value.  We have
  798.    to make sure that this value cannot be an integer in Lisp_Object form.
  799.    0xFFFFFFFF could be so on a 64-bit system, so we extend it to 64 bits.
  800.    On a 32-bit system, the type bits will be non-zero, making the value
  801.    be a pointer, and the pointer will be misaligned.
  802.  
  803.    Even if Emacs is run on some weirdo system that allows and allocates
  804.    byte-aligned pointers, this pointer is at the very top of the address
  805.    space and so it's almost inconceivable that it could ever be valid. */
  806.    
  807. #if INTBITS == 32
  808. # define INVALID_POINTER_VALUE 0xFFFFFFFF
  809. #elif INTBITS == 48
  810. # define INVALID_POINTER_VALUE 0xFFFFFFFFFFFF
  811. #elif INTBITS == 64
  812. # define INVALID_POINTER_VALUE 0xFFFFFFFFFFFFFFFF
  813. #else
  814. You have some weird system and need to supply a reasonable value here.
  815. #endif
  816.  
  817. #define FREE_STRUCT_P(ptr) \
  818.   (* (void **) ptr == (void *) INVALID_POINTER_VALUE)
  819. #define MARK_STRUCT_AS_FREE(ptr) \
  820.   (* (void **) ptr = (void *) INVALID_POINTER_VALUE)
  821. #define MARK_STRUCT_AS_NOT_FREE(ptr) \
  822.   (* (void **) ptr = 0)
  823.  
  824. #ifdef ERROR_CHECK_GC
  825.  
  826. #define PUT_FIXED_TYPE_ON_FREE_LIST(type, structtype, ptr)        \
  827. do { if (type##_free_list_tail)                        \
  828.        {                                \
  829.      /* When we store the chain pointer, we complement all        \
  830.         its bits; this should significantly increase its        \
  831.         bogosity in case someone tries to use the value, and    \
  832.         should make us dump faster if someone stores something    \
  833.         over the pointer because when it gets un-complemented in    \
  834.         ALLOCATED_FIXED_TYPE(), the resulting pointer will be    \
  835.         extremely bogus. */                        \
  836.      * (structtype **)                        \
  837.        ((char *) type##_free_list_tail + sizeof (void *)) =        \
  838.          (structtype *) ~(unsigned long) ptr;            \
  839.        }                                \
  840.      else                                \
  841.        type##_free_list = ptr;                        \
  842.      type##_free_list_tail = ptr;                    \
  843.    } while (0)
  844.  
  845. #else
  846.  
  847. #define PUT_FIXED_TYPE_ON_FREE_LIST(type, structtype, ptr)    \
  848. do { * (structtype **) ((char *) ptr + sizeof (void *)) =    \
  849.        type##_free_list;                    \
  850.      type##_free_list = ptr;                    \
  851.    } while (0)
  852.  
  853. #endif
  854.  
  855. /* TYPE and STRUCTTYPE are the same as in ALLOCATE_FIXED_TYPE(). */
  856.  
  857. #define FREE_FIXED_TYPE(type, structtype, ptr)            \
  858. do { structtype *_weird_ = (ptr);                \
  859.      ADDITIONAL_FREE_##type (_weird_);                \
  860.      deadbeef_memory (ptr, sizeof (structtype));        \
  861.      PUT_FIXED_TYPE_ON_FREE_LIST (type, structtype, ptr);    \
  862.      MARK_STRUCT_AS_FREE (_weird_);                \
  863.    } while (0)
  864.  
  865.  
  866. /**********************************************************************/
  867. /*                         Cons allocation                            */
  868. /**********************************************************************/
  869.  
  870. DECLARE_FIXED_TYPE_ALLOC (cons, struct Lisp_Cons);
  871. /* conses are used and freed so often that we set this really high */
  872. /* #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_cons 20000 */
  873. #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_cons 2000
  874.  
  875. DEFUN ("cons", Fcons, Scons, 2, 2, 0,
  876.   "Create a new cons, give it CAR and CDR as components, and return it.")
  877.   (car, cdr)
  878.      Lisp_Object car, cdr;
  879. {
  880.   Lisp_Object val = Qnil;
  881.   struct Lisp_Cons *c;
  882.  
  883.   ALLOCATE_FIXED_TYPE (cons, struct Lisp_Cons, c);
  884.   XSETCONS (val, c);
  885.   XCAR (val) = car;
  886.   XCDR (val) = cdr;
  887.   return val;
  888. }
  889.  
  890. DEFUN ("list", Flist, Slist, 0, MANY, 0,
  891.   "Return a newly created list with specified arguments as elements.\n\
  892. Any number of arguments, even zero arguments, are allowed.")
  893.   (nargs, args)
  894.      int nargs;
  895.      Lisp_Object *args;
  896. {
  897.   Lisp_Object len, val, val_tail;
  898.  
  899.   len = make_number (nargs);
  900.   val = Fmake_list (len, Qnil);
  901.   val_tail = val;
  902.   while (!NILP (val_tail))
  903.     {
  904.       XCAR (val_tail) = *args++;
  905.       val_tail = XCDR (val_tail);
  906.     }
  907.   return val;
  908. }
  909.  
  910. Lisp_Object
  911. list1 (Lisp_Object obj0)
  912. {
  913.   return (Fcons (obj0, Qnil));
  914. }
  915.  
  916. Lisp_Object
  917. list2 (Lisp_Object obj0, Lisp_Object obj1)
  918. {
  919.   return Fcons (obj0, list1 (obj1));
  920. }
  921.  
  922. Lisp_Object
  923. list3 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2)
  924. {
  925.   return Fcons (obj0, list2 (obj1, obj2));
  926. }
  927.  
  928. static Lisp_Object
  929. cons3 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2)
  930. {
  931.   return Fcons (obj0, Fcons (obj1, obj2));
  932. }
  933.  
  934. Lisp_Object
  935. list4 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, Lisp_Object obj3)
  936. {
  937.   return Fcons (obj0, list3 (obj1, obj2, obj3));
  938. }
  939.  
  940. Lisp_Object
  941. list5 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, Lisp_Object obj3,
  942.        Lisp_Object obj4)
  943. {
  944.   return Fcons (obj0, list4 (obj1, obj2, obj3, obj4));
  945. }
  946.  
  947. Lisp_Object
  948. list6 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, Lisp_Object obj3,
  949.        Lisp_Object obj4, Lisp_Object obj5)
  950. {
  951.   return Fcons (obj0, list5 (obj1, obj2, obj3, obj4, obj5));
  952. }
  953.  
  954. DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0,
  955.   "Return a newly created list of length LENGTH, with each element being INIT.")
  956.      (length, init)
  957.      Lisp_Object length, init;
  958. {
  959.   Lisp_Object val;
  960.   int size;
  961.  
  962.   if (!INTP (length) || XINT (length) < 0)
  963.     length = wrong_type_argument (Qnatnump, length);
  964.   size = XINT (length);
  965.  
  966.   val = Qnil;
  967.   while (size-- > 0)
  968.     val = Fcons (init, val);
  969.   return val;
  970. }
  971.  
  972.  
  973. /**********************************************************************/
  974. /*                        Float allocation                            */
  975. /**********************************************************************/
  976.  
  977. #ifdef LISP_FLOAT_TYPE
  978.  
  979. DECLARE_FIXED_TYPE_ALLOC (float, struct Lisp_Float);
  980. #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_float 1000
  981.  
  982. Lisp_Object
  983. make_float (double float_value)
  984. {
  985.   Lisp_Object val;
  986.   struct Lisp_Float *f;
  987.  
  988.   ALLOCATE_FIXED_TYPE (float, struct Lisp_Float, f);
  989.   f->lheader.implementation = lrecord_float;
  990.   float_next (f) = ((struct Lisp_Float *) -1);
  991.   float_data (f) = float_value;
  992.   XSETFLOAT (val, f);
  993.   return (val);
  994. }
  995.  
  996. #endif /* LISP_FLOAT_TYPE */
  997.  
  998.  
  999. /**********************************************************************/
  1000. /*                         Vector allocation                          */
  1001. /**********************************************************************/
  1002.  
  1003. static Lisp_Object all_vectors;
  1004.  
  1005. /* #### should allocate `small' vectors from a frob-block */
  1006. static struct Lisp_Vector *
  1007. make_vector_internal (int sizei)
  1008. {
  1009.   int sizem = (sizeof (struct Lisp_Vector)
  1010.                /* -1 because struct Lisp_Vector includes 1 slot,
  1011.                 * +1 to account for vector_next */
  1012.                + (sizei - 1 + 1) * sizeof (Lisp_Object)
  1013.                );
  1014.   struct Lisp_Vector *p = allocate_lisp_storage (sizem);
  1015. #ifdef LRECORD_VECTOR
  1016.   set_lheader_implementation (&(p->lheader), lrecord_vector);
  1017. #endif
  1018.  
  1019.   INCREMENT_CONS_COUNTER (sizem);
  1020.  
  1021.   p->size = sizei;
  1022.   vector_next (p) = all_vectors;
  1023.   XSETVECTOR (all_vectors, p);
  1024.   return (p);
  1025. }
  1026.  
  1027. Lisp_Object
  1028. make_vector (int length, Lisp_Object init)
  1029. {
  1030.   int elt;
  1031.   Lisp_Object vector = Qnil;
  1032.   struct Lisp_Vector *p;
  1033.  
  1034.   if (length < 0)
  1035.     length = XINT (wrong_type_argument (Qnatnump, make_number (length)));
  1036.  
  1037.   p = make_vector_internal (length);
  1038.   XSETVECTOR (vector, p);
  1039.  
  1040. #if 0
  1041.   /* Initialize big arrays full of 0's quickly, for what that's worth */
  1042.   {
  1043.     char *travesty = (char *) &init;
  1044.     for (i = 1; i < sizeof (Lisp_Object); i++)
  1045.     {
  1046.       if (travesty[i] != travesty[0])
  1047.         goto fill;
  1048.     }
  1049.     memset (vector_data (p), travesty[0], length * sizeof (Lisp_Object));
  1050.     return (vector);
  1051.   }
  1052.  fill:
  1053. #endif
  1054.   for (elt = 0; elt < length; elt++)
  1055.     vector_data(p)[elt] = init;
  1056.  
  1057.   return (vector);
  1058. }
  1059.  
  1060.  
  1061. DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0,
  1062.   "Return a newly created vector of length LENGTH, with each element being INIT.\n\
  1063. See also the function `vector'.")
  1064.      (length, init)
  1065.      Lisp_Object length, init;
  1066. {
  1067.   if (!INTP (length) || XINT (length) < 0)
  1068.     length = wrong_type_argument (Qnatnump, length);
  1069.  
  1070.   return (make_vector (XINT (length), init));
  1071. }
  1072.  
  1073. DEFUN ("vector", Fvector, Svector, 0, MANY, 0,
  1074.   "Return a newly created vector with specified arguments as elements.\n\
  1075. Any number of arguments, even zero arguments, are allowed.")
  1076.      (nargs, args)
  1077.      int nargs;
  1078.      Lisp_Object *args;
  1079. {
  1080.   Lisp_Object vector = Qnil;
  1081.   int elt;
  1082.   struct Lisp_Vector *p;
  1083.  
  1084.   p = make_vector_internal (nargs);
  1085.   XSETVECTOR (vector, p);
  1086.  
  1087.   for (elt = 0; elt < nargs; elt++)
  1088.     vector_data(p)[elt] = args[elt];
  1089.  
  1090.   return (vector);
  1091. }
  1092.  
  1093. Lisp_Object
  1094. vector1 (Lisp_Object obj0)
  1095. {
  1096.   return Fvector (1, &obj0);
  1097. }
  1098.  
  1099. Lisp_Object
  1100. vector2 (Lisp_Object obj0, Lisp_Object obj1)
  1101. {
  1102.   Lisp_Object args[2];
  1103.   args[0] = obj0;
  1104.   args[1] = obj1;
  1105.   return Fvector (2, args);
  1106. }
  1107.  
  1108. Lisp_Object
  1109. vector3 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2)
  1110. {
  1111.   Lisp_Object args[3];
  1112.   args[0] = obj0;
  1113.   args[1] = obj1;
  1114.   args[2] = obj2;
  1115.   return Fvector (3, args);
  1116. }
  1117.  
  1118. Lisp_Object
  1119. vector4 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2,
  1120.      Lisp_Object obj3)
  1121. {
  1122.   Lisp_Object args[4];
  1123.   args[0] = obj0;
  1124.   args[1] = obj1;
  1125.   args[2] = obj2;
  1126.   args[3] = obj3;
  1127.   return Fvector (4, args);
  1128. }
  1129.  
  1130. Lisp_Object
  1131. vector5 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2,
  1132.      Lisp_Object obj3, Lisp_Object obj4)
  1133. {
  1134.   Lisp_Object args[5];
  1135.   args[0] = obj0;
  1136.   args[1] = obj1;
  1137.   args[2] = obj2;
  1138.   args[3] = obj3;
  1139.   args[4] = obj4;
  1140.   return Fvector (5, args);
  1141. }
  1142.  
  1143. Lisp_Object
  1144. vector6 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2,
  1145.      Lisp_Object obj3, Lisp_Object obj4, Lisp_Object obj5)
  1146. {
  1147.   Lisp_Object args[6];
  1148.   args[0] = obj0;
  1149.   args[1] = obj1;
  1150.   args[2] = obj2;
  1151.   args[3] = obj3;
  1152.   args[4] = obj4;
  1153.   args[5] = obj5;
  1154.   return Fvector (6, args);
  1155. }
  1156.  
  1157. Lisp_Object
  1158. vector7 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2,
  1159.      Lisp_Object obj3, Lisp_Object obj4, Lisp_Object obj5,
  1160.      Lisp_Object obj6)
  1161. {
  1162.   Lisp_Object args[7];
  1163.   args[0] = obj0;
  1164.   args[1] = obj1;
  1165.   args[2] = obj2;
  1166.   args[3] = obj3;
  1167.   args[4] = obj4;
  1168.   args[5] = obj5;
  1169.   args[6] = obj6;
  1170.   return Fvector (7, args);
  1171. }
  1172.  
  1173. Lisp_Object
  1174. vector8 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2,
  1175.      Lisp_Object obj3, Lisp_Object obj4, Lisp_Object obj5,
  1176.      Lisp_Object obj6, Lisp_Object obj7)
  1177. {
  1178.   Lisp_Object args[8];
  1179.   args[0] = obj0;
  1180.   args[1] = obj1;
  1181.   args[2] = obj2;
  1182.   args[3] = obj3;
  1183.   args[4] = obj4;
  1184.   args[5] = obj5;
  1185.   args[6] = obj6;
  1186.   args[7] = obj7;
  1187.   return Fvector (8, args);
  1188. }
  1189.  
  1190.  
  1191. /**********************************************************************/
  1192. /*           Bytecode (aka "compiled-function") allocation            */
  1193. /**********************************************************************/
  1194.  
  1195. DECLARE_FIXED_TYPE_ALLOC (bytecode, struct Lisp_Bytecode);
  1196. #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_bytecode 1000
  1197.  
  1198. static Lisp_Object
  1199. make_bytecode (int make_pure)
  1200. {
  1201.   struct Lisp_Bytecode *b;
  1202.   Lisp_Object new;
  1203.   int size = sizeof (struct Lisp_Bytecode);
  1204.  
  1205.   if (make_pure && check_purespace (size))
  1206.     {
  1207.       b = (struct Lisp_Bytecode *) (PUREBEG + pureptr);
  1208.       set_lheader_implementation (&(b->lheader), lrecord_bytecode);
  1209.       pureptr += size;
  1210.       bump_purestat (&purestat_bytecode, size);
  1211.     }
  1212.   else
  1213.     {
  1214.       ALLOCATE_FIXED_TYPE (bytecode, struct Lisp_Bytecode, b);
  1215.       set_lheader_implementation (&(b->lheader), lrecord_bytecode);
  1216.     }
  1217.   b->maxdepth = 0;
  1218.   b->flags.documentationp = 0;
  1219.   b->flags.interactivep = 0;
  1220.   b->flags.domainp = 0; /* I18N3 */
  1221.   b->bytecodes = Qzero;
  1222.   b->constants = Qzero;
  1223.   b->arglist = Qnil;
  1224.   b->doc_and_interactive = Qnil;
  1225.   XSETBYTECODE (new, b);
  1226.   return (new);
  1227. }
  1228.  
  1229. DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0,
  1230.   "Create a compiled-function object.\n\
  1231. Usage: (arglist instructions constants stack-size\n\
  1232.     &optional doc-string interactive-spec)\n\
  1233. Note that, unlike all other emacs-lisp functions, calling this with five\n\
  1234. arguments is NOT the same as calling it with six arguments, the last of\n\
  1235. which is nil.  If the INTERACTIVE arg is specified as nil, then that means\n\
  1236. that this function was defined with `(interactive)'.  If the arg is not\n\
  1237. specified, then that means the function is not interactive.\n\
  1238. This is terrible behavior which is retained for compatibility with old\n\
  1239. `.elc' files which expected these semantics.")
  1240.      (nargs, args)
  1241.      int nargs;
  1242.      Lisp_Object *args;
  1243. {
  1244. /*   In a non-insane world this function would have this arglist...
  1245.      (arglist, instructions, constants, stack_size, doc_string, interactive)
  1246.      Lisp_Object arglist, instructions, constants, stack_size, doc_string,
  1247.     interactive;
  1248.  */
  1249.   Lisp_Object arglist      = args[0];
  1250.   Lisp_Object instructions = args[1];
  1251.   Lisp_Object constants    = args[2];
  1252.   Lisp_Object stack_size   = args[3];
  1253.   Lisp_Object doc_string   = ((nargs > 4) ? args[4] : Qnil);
  1254.   Lisp_Object interactive  = ((nargs > 5) ? args[5] : Qunbound);
  1255.  
  1256.   if (nargs > 6)
  1257.     return Fsignal (Qwrong_number_of_arguments, 
  1258.             list2 (intern ("make-byte-code"), make_number (nargs)));
  1259.  
  1260.   CHECK_LIST (arglist, 0);
  1261.   CHECK_STRING (instructions, 0);
  1262.   CHECK_VECTOR (constants, 0);
  1263.   CHECK_NATNUM (stack_size, 0);
  1264.   /* doc_string may be nil, string, or int. */
  1265.   /* interactive may be list or string (or unbound). */
  1266.  
  1267.   if (purify_flag)
  1268.     {
  1269.       if (!purified (arglist))
  1270.     arglist = Fpurecopy (arglist);
  1271.       if (!purified (instructions))
  1272.     instructions = Fpurecopy (instructions);
  1273.       if (!purified (doc_string))
  1274.     doc_string = Fpurecopy (doc_string);
  1275.       if (!purified (interactive) && !EQ (interactive, Qunbound))
  1276.     interactive = Fpurecopy (interactive);
  1277.  
  1278.       /* Statistics are kept differently for the constants */
  1279.       if (!purified (constants))
  1280. #ifdef PURESTAT
  1281.     {
  1282.       int old = purecopying_for_bytecode;
  1283.       purecopying_for_bytecode = 1;
  1284.       constants = Fpurecopy (constants);
  1285.       purecopying_for_bytecode = old;
  1286.     }
  1287. #else
  1288.         constants = Fpurecopy (constants);
  1289. #endif /* PURESTAT */
  1290.  
  1291. #ifdef PURESTAT
  1292.       if (STRINGP (instructions))
  1293.     bump_purestat (&purestat_string_bytecodes, pure_sizeof (instructions));
  1294.       if (VECTORP (constants))
  1295.     bump_purestat (&purestat_vector_bytecode_constants,
  1296.                pure_sizeof (constants));
  1297.       if (STRINGP (doc_string))
  1298.     /* These should be have been snagged by make-docfile... */
  1299.     bump_purestat (&purestat_string_documentation,
  1300.                pure_sizeof (doc_string));
  1301.       if (STRINGP (interactive))
  1302.     bump_purestat (&purestat_string_interactive,
  1303.                pure_sizeof (interactive));
  1304. #endif /* PURESTAT */
  1305.     }
  1306.   
  1307.   {
  1308.     int docp = !NILP (doc_string);
  1309.     int intp = !EQ (interactive, Qunbound);
  1310. #ifdef I18N3
  1311.     int domp = !NILP (Vfile_domain);
  1312. #endif
  1313.     Lisp_Object val = make_bytecode (purify_flag);
  1314.     struct Lisp_Bytecode *b = XBYTECODE (val);
  1315.     b->flags.documentationp = docp;
  1316.     b->flags.interactivep   = intp;
  1317. #ifdef I18N3
  1318.     b->flags.domainp        = domp;
  1319. #endif
  1320.     b->maxdepth  = XINT (stack_size);
  1321.     b->bytecodes = instructions;
  1322.     b->constants = constants;
  1323.     b->arglist   = arglist;
  1324.  
  1325. #ifdef I18N3
  1326.     if (docp && intp && domp)
  1327.       b->doc_and_interactive = (((purify_flag) ? pure_cons : Fcons)
  1328.                 (doc_string,
  1329.                  (((purify_flag) ? pure_cons : Fcons)
  1330.                   (interactive, Vfile_domain))));
  1331.     else if (docp && domp)
  1332.       b->doc_and_interactive = (((purify_flag) ? pure_cons : Fcons)
  1333.                 (doc_string, Vfile_domain));
  1334.     else if (intp && domp)
  1335.       b->doc_and_interactive = (((purify_flag) ? pure_cons : Fcons)
  1336.                 (interactive, Vfile_domain));
  1337.     else
  1338. #endif
  1339.     if (docp && intp)
  1340.       b->doc_and_interactive = (((purify_flag) ? pure_cons : Fcons)
  1341.                 (doc_string, interactive));
  1342.     else if (intp)
  1343.       b->doc_and_interactive = interactive;
  1344. #ifdef I18N3
  1345.     else if (domp)
  1346.       b->doc_and_interactive = Vfile_domain;
  1347. #endif
  1348.     else
  1349.       b->doc_and_interactive = doc_string;
  1350.  
  1351.     return (val);
  1352.   }
  1353. }
  1354.  
  1355.  
  1356. /**********************************************************************/
  1357. /*                          Symbol allocation                         */
  1358. /**********************************************************************/
  1359.  
  1360. DECLARE_FIXED_TYPE_ALLOC (symbol, struct Lisp_Symbol);
  1361. #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_symbol 1000
  1362.  
  1363. DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0,
  1364.   "Return a newly allocated uninterned symbol whose name is NAME.\n\
  1365. Its value and function definition are void, and its property list is nil.")
  1366.   (str)
  1367.      Lisp_Object str;
  1368. {
  1369.   Lisp_Object val;
  1370.   struct Lisp_Symbol *p;
  1371.  
  1372.   CHECK_STRING (str, 0);
  1373.  
  1374.   ALLOCATE_FIXED_TYPE (symbol, struct Lisp_Symbol, p);
  1375. #ifdef LRECORD_SYMBOL
  1376.   set_lheader_implementation (&(p->lheader), lrecord_symbol);
  1377. #endif
  1378.   p->name = XSTRING (str);
  1379.   p->plist = Qnil;
  1380.   p->value = Qunbound;
  1381.   p->function = Qunbound;
  1382.   symbol_next (p) = 0;
  1383.   XSETSYMBOL (val, p);
  1384.   return val;
  1385. }
  1386.  
  1387.  
  1388. /**********************************************************************/
  1389. /*                         Extent allocation                          */
  1390. /**********************************************************************/
  1391.  
  1392. DECLARE_FIXED_TYPE_ALLOC (extent, struct extent);
  1393. #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_extent 1000
  1394.  
  1395. struct extent *
  1396. make_extent (void)
  1397. {
  1398.   struct extent *e;
  1399.  
  1400.   ALLOCATE_FIXED_TYPE (extent, struct extent, e);
  1401.   /* memset (e, 0, sizeof (struct extent)); */
  1402.   set_lheader_implementation (&(e->lheader), lrecord_extent);
  1403.   extent_object (e) = Qnil;
  1404.   set_extent_start (e, 0);
  1405.   set_extent_end (e, 0);
  1406.   e->plist = Qnil;
  1407.  
  1408.   memset (&e->flags, 0, sizeof (e->flags));
  1409.  
  1410.   extent_face (e) = Qnil;
  1411.   e->flags.end_open = 1;  /* default is for endpoints to behave like markers */
  1412.   e->flags.detachable = 1;
  1413.  
  1414.   return (e);
  1415. }
  1416.  
  1417.  
  1418. /**********************************************************************/
  1419. /*                     Extent replica allocation                      */
  1420. /**********************************************************************/
  1421.  
  1422. DECLARE_FIXED_TYPE_ALLOC (extent_replica, struct extent_replica);
  1423. #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_extent_replica 1000
  1424.  
  1425. struct extent_replica *
  1426. make_extent_replica (Lisp_Object extent, Bufpos start, Bufpos end)
  1427. {
  1428.   struct extent_replica *r;
  1429.  
  1430.   ALLOCATE_FIXED_TYPE (extent_replica, struct extent_replica, r);
  1431.   /* memset (r, 0, sizeof (struct extent_replica)); */
  1432.   set_lheader_implementation (&(r->lheader), lrecord_extent_replica);
  1433.   extent_replica_extent (r) = extent;
  1434.   set_extent_replica_start (r, (start >= 0) ? start : 0);
  1435.   set_extent_replica_end (r, (end >= start) ? end : start);
  1436.  
  1437.   return (r);
  1438. }
  1439.  
  1440.  
  1441. /**********************************************************************/
  1442. /*                         Event allocation                           */
  1443. /**********************************************************************/
  1444.  
  1445. DECLARE_FIXED_TYPE_ALLOC (event, struct Lisp_Event);
  1446. #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_event 1000
  1447.  
  1448. Lisp_Object
  1449. make_event (void)
  1450. {
  1451.   Lisp_Object val;
  1452.   struct Lisp_Event *e;
  1453.  
  1454.   ALLOCATE_FIXED_TYPE (event, struct Lisp_Event, e);
  1455.   set_lheader_implementation (&(e->lheader), lrecord_event);
  1456.  
  1457.   XSETEVENT (val, e);
  1458.   return val;
  1459. }
  1460.   
  1461.  
  1462. /**********************************************************************/
  1463. /*                       Marker allocation                            */
  1464. /**********************************************************************/
  1465.  
  1466. DECLARE_FIXED_TYPE_ALLOC (marker, struct Lisp_Marker);
  1467. #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_marker 1000
  1468.  
  1469. DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
  1470.   "Return a newly allocated marker which does not point at any place.")
  1471.   ()
  1472. {
  1473.   Lisp_Object val;
  1474.   struct Lisp_Marker *p;
  1475.  
  1476.   ALLOCATE_FIXED_TYPE (marker, struct Lisp_Marker, p);
  1477.   set_lheader_implementation (&(p->lheader), lrecord_marker);
  1478.   p->buffer = 0;
  1479.   p->memind = 0;
  1480.   marker_next (p) = 0;
  1481.   XSETMARKER (val, p);
  1482.   return val;
  1483. }
  1484.  
  1485.  
  1486. /**********************************************************************/
  1487. /*                        String allocation                           */
  1488. /**********************************************************************/
  1489.  
  1490. /* The data for "short" strings generally resides inside of structs of type 
  1491.    string_chars_block. The Lisp_String structure is allocated just like any 
  1492.    other Lisp object (except for vectors), and these are freelisted when
  1493.    they get garbage collected. The data for short strings get compacted,
  1494.    but the data for large strings do not. 
  1495.  
  1496.    Previously Lisp_String structures were relocated, but this caused a lot
  1497.    of bus-errors because the C code didn't include enough GCPRO's for
  1498.    strings (since EVERY REFERENCE to a short string needed to be GCPRO'd so
  1499.    that the reference would get relocated).
  1500.  
  1501.    This new method makes things somewhat bigger, but it is MUCH safer.  */
  1502.  
  1503. DECLARE_FIXED_TYPE_ALLOC (string, struct Lisp_String);
  1504. /* strings are used and freed quite often */
  1505. /* #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_string 10000 */
  1506. #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_string 1000
  1507.  
  1508. /* String blocks contain this many useful bytes. */
  1509. #define STRING_CHARS_BLOCK_SIZE \
  1510.   (8192 - MALLOC_OVERHEAD - ((2 * sizeof (struct string_chars_block *)) \
  1511.                              + sizeof (LISP_WORD_TYPE)))
  1512. /* Block header for small strings. */
  1513. struct string_chars_block
  1514. {
  1515.   LISP_WORD_TYPE pos;
  1516.   struct string_chars_block *next;
  1517.   struct string_chars_block *prev;
  1518.   /* Contents of string_chars_block->string_chars are interleaved
  1519.      string_chars structures (see below) and the actual string data */
  1520.   unsigned char string_chars[STRING_CHARS_BLOCK_SIZE];
  1521. };
  1522.  
  1523. struct string_chars_block *first_string_chars_block;
  1524. struct string_chars_block *current_string_chars_block;
  1525.  
  1526. /* If SIZE is the length of a string, this returns how many bytes
  1527.  *  the string occupies in string_chars_block->string_chars
  1528.  *  (including alignment padding).
  1529.  */
  1530. #define STRING_FULLSIZE(s) \
  1531.    ALIGN_SIZE (((s) + 1 + sizeof (struct Lisp_String *)),\
  1532.                ALIGNOF (struct Lisp_String *))
  1533.  
  1534. #define BIG_STRING_FULLSIZE_P(fullsize) ((fullsize) >= STRING_CHARS_BLOCK_SIZE)
  1535. #define BIG_STRING_SIZE_P(size) (BIG_STRING_FULLSIZE_P (STRING_FULLSIZE(size)))
  1536.  
  1537. #define CHARS_TO_STRING_CHAR(x) \
  1538.   ((struct string_chars *) \
  1539.    (((char *) (x)) - (slot_offset (struct string_chars, chars))))
  1540.  
  1541.  
  1542. struct string_chars
  1543. {
  1544.   struct Lisp_String *string;
  1545.   unsigned char chars[1];
  1546. };
  1547.  
  1548.  
  1549. static void
  1550. init_string_chars_alloc (void)
  1551. {
  1552.   first_string_chars_block = 
  1553.     (struct string_chars_block *) xmalloc (sizeof (struct string_chars_block));
  1554.   first_string_chars_block->prev = 0;
  1555.   first_string_chars_block->next = 0;
  1556.   first_string_chars_block->pos = 0;
  1557.   current_string_chars_block = first_string_chars_block;
  1558. }
  1559.  
  1560.  
  1561. Lisp_Object
  1562. make_uninit_string (Bytecount length)
  1563. {
  1564.   struct Lisp_String *s;
  1565.   struct string_chars *s_chars;
  1566.   int fullsize = STRING_FULLSIZE (length);
  1567.   Lisp_Object val;
  1568.  
  1569.   if ((length < 0) || (fullsize <= 0))
  1570.     abort ();
  1571.  
  1572.   /* Allocate the string header */
  1573.   ALLOCATE_FIXED_TYPE (string, struct Lisp_String, s);
  1574.  
  1575.   /* Allocate the string's actual data */
  1576.   if (BIG_STRING_FULLSIZE_P (fullsize))
  1577.     {
  1578.       s_chars = (struct string_chars *) xmalloc (fullsize);
  1579.     }
  1580.   else if (fullsize <=
  1581.            (countof (current_string_chars_block->string_chars)
  1582.             - current_string_chars_block->pos))
  1583.     {
  1584.       /* This string can fit in the current string chars block */
  1585.       s_chars = (struct string_chars *)
  1586.     (current_string_chars_block->string_chars
  1587.      + current_string_chars_block->pos);
  1588.       current_string_chars_block->pos += fullsize;
  1589.     }
  1590.   else
  1591.     {
  1592.       /* Make a new current string chars block */
  1593.       struct string_chars_block *new 
  1594.     = (struct string_chars_block *)
  1595.       xmalloc (sizeof (struct string_chars_block));
  1596.       
  1597.       current_string_chars_block->next = new;
  1598.       new->prev = current_string_chars_block;
  1599.       new->next = 0;
  1600.       current_string_chars_block = new;
  1601.       new->pos = fullsize;
  1602.       s_chars = (struct string_chars *)
  1603.     current_string_chars_block->string_chars;
  1604.     }
  1605.  
  1606.   s_chars->string = s;
  1607.   set_string_data (s, &(s_chars->chars[0]));
  1608.   set_string_length (s, length);
  1609.   s->plist = Qnil;
  1610.   
  1611.   set_string_byte (s, length, 0);
  1612.  
  1613.   INCREMENT_CONS_COUNTER (fullsize);
  1614.  
  1615.   XSETSTRING (val, s);
  1616.   return (val);
  1617. }
  1618.  
  1619. DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0,
  1620.   "Return a newly created string of length LENGTH, with each element being INIT.\n\
  1621. LENGTH must be an integer and INIT must be a character.")
  1622.   (length, init)
  1623.      Lisp_Object length, init;
  1624. {
  1625.   Lisp_Object val;
  1626.  
  1627.   CHECK_NATNUM (length, 0);
  1628.   CHECK_COERCE_CHAR (init, 1);
  1629. #ifdef MULE
  1630.   {
  1631.     Bufbyte str[MAX_EMCHAR_LEN];
  1632.     int len = emchar_to_charptr (XINT (init), str);
  1633.  
  1634.     val = make_uninit_string (len * XINT (length));
  1635.     if (len == 1)
  1636.       /* Optimize the single-byte case */
  1637.       memset (string_data (XSTRING (val)), XINT (init),
  1638.           string_length (XSTRING (val)));
  1639.     else
  1640.       {
  1641.     int i, j, k;
  1642.     Bufbyte *ptr = string_data (XSTRING (val));
  1643.  
  1644.     k = 0;
  1645.     for (i = 0; i < XINT (length); i++)
  1646.       for (j = 0; j < len; j++)
  1647.         ptr[k++] = str[j];
  1648.       }
  1649.   }
  1650. #else
  1651.   /* !!#### remove this after 19.13 */
  1652.   val = make_uninit_string (XINT (length));
  1653.   memset (string_data (XSTRING (val)), XINT (init),
  1654.       string_length (XSTRING (val)));
  1655. #endif
  1656.   return (val);
  1657. }
  1658.  
  1659. Lisp_Object
  1660. make_string (CONST Bufbyte *contents, Bytecount length)
  1661. {
  1662.   Lisp_Object val;
  1663.   
  1664.   val = make_uninit_string (length);
  1665.   memcpy (string_data (XSTRING (val)), contents, length);
  1666.   return (val);
  1667. }
  1668.  
  1669. Lisp_Object
  1670. make_ext_string (CONST char *contents, int length)
  1671. {
  1672.   Bufbyte *intstr;
  1673.   Bytecount intlen;
  1674.  
  1675.   intstr = charptr_from_external (contents, length, &intlen);
  1676.   return make_string (intstr, intlen);
  1677. }
  1678.  
  1679. Lisp_Object
  1680. build_string (CONST char *str)
  1681. {
  1682.   return make_string ((Bufbyte *) str, strlen (str));
  1683. }
  1684.  
  1685. Lisp_Object
  1686. build_ext_string (CONST char *str)
  1687. {
  1688.   return build_string (c_charptr_from_external (str));
  1689. }
  1690.  
  1691. Lisp_Object
  1692. build_translated_string (CONST char *str)
  1693. {
  1694.   return build_string (GETTEXT (str));
  1695. }
  1696.  
  1697.  
  1698. /**********************************************************************/
  1699. /* Purity of essence, peace on earth                                  */
  1700. /**********************************************************************/
  1701.  
  1702. static int symbols_initialized;
  1703.  
  1704. Lisp_Object
  1705. make_pure_string (CONST Bufbyte *data, Bytecount length,
  1706.                   int no_need_to_copy_data)
  1707. {
  1708.   Lisp_Object new;
  1709.   struct Lisp_String *s;
  1710.   int size = (sizeof (struct Lisp_String) + ((no_need_to_copy_data) 
  1711.                                              ? 0 
  1712.                                              /* + 1 for terminating 0 */
  1713.                                              : (length + 1)));
  1714.   size = ALIGN_SIZE (size, ALIGNOF (Lisp_Object));
  1715.  
  1716.   if (symbols_initialized && !pure_lossage)
  1717.     {
  1718.       /* Try to share some names.  Saves a few kbytes. */
  1719.       Lisp_Object tem = oblookup (Vobarray, data, length);
  1720.       if (SYMBOLP (tem))
  1721.     {
  1722.       s = XSYMBOL (tem)->name;
  1723.       if (!PURIFIED (s)) abort ();
  1724.       XSETSTRING (new, s);
  1725.       return (new);
  1726.     }
  1727.     }
  1728.  
  1729.   if (!check_purespace (size))
  1730.     return (make_string (data, length));
  1731.  
  1732.   s = (struct Lisp_String *) (PUREBEG + pureptr);
  1733.   set_string_length (s, length);
  1734.   if (no_need_to_copy_data)
  1735.     {
  1736.       set_string_data (s, (Bufbyte *) data);
  1737.     }
  1738.   else
  1739.     {
  1740.       set_string_data (s, (Bufbyte *) s + sizeof (struct Lisp_String));
  1741.       memcpy (string_data (s), data, length);
  1742.       set_string_byte (s, length, 0);
  1743.     }
  1744.   /* A waste: always Qnil in pure strings. */
  1745.   s->plist = Qnil;
  1746.   pureptr += size;
  1747.  
  1748. #ifdef PURESTAT
  1749.   bump_purestat (&purestat_string_all, size);
  1750.   if (purecopying_for_bytecode)
  1751.     bump_purestat (&purestat_string_other_function, size);
  1752. #endif
  1753.  
  1754.   XSETSTRING (new, s);
  1755.   return (new);
  1756. }
  1757.  
  1758.  
  1759. Lisp_Object
  1760. make_pure_pname (CONST Bufbyte *data, Bytecount length,
  1761.          int no_need_to_copy_data)
  1762. {
  1763.   Lisp_Object name = make_pure_string (data, length, no_need_to_copy_data);
  1764.   bump_purestat (&purestat_string_pname, pure_sizeof (name));
  1765.  
  1766.   /* We've made (at least) Qnil now, and Vobarray will soon be set up. */
  1767.   symbols_initialized = 1;
  1768.  
  1769.   return (name);
  1770. }
  1771.  
  1772.  
  1773. Lisp_Object
  1774. pure_cons (Lisp_Object car, Lisp_Object cdr)
  1775. {
  1776.   Lisp_Object new;
  1777.  
  1778.   if (!check_purespace (sizeof (struct Lisp_Cons)))
  1779.     return (Fcons (Fpurecopy (car), Fpurecopy (cdr)));
  1780.  
  1781.   XSETCONS (new, PUREBEG + pureptr);
  1782.   pureptr += sizeof (struct Lisp_Cons);
  1783.   bump_purestat (&purestat_cons, sizeof (struct Lisp_Cons));
  1784.  
  1785.   XCAR (new) = Fpurecopy (car);
  1786.   XCDR (new) = Fpurecopy (cdr);
  1787.   return (new);
  1788. }
  1789.  
  1790. #ifdef LISP_FLOAT_TYPE
  1791.  
  1792. Lisp_Object
  1793. make_pure_float (double num)
  1794. {
  1795.   struct Lisp_Float *f;
  1796.   Lisp_Object val;
  1797.  
  1798.   /* Make sure that PUREBEG + pureptr is aligned on at least a sizeof
  1799.      (double) boundary.  Some architectures (like the sparc) require
  1800.      this, and I suspect that floats are rare enough that it's no
  1801.      tragedy for those that don't. */
  1802.   {
  1803. #if defined (__GNUC__) && (__GNUC__ >= 2)
  1804.     /* In gcc, we can directly ask what the alignment constraints of a
  1805.        structure are, but in general, that's not possible...  Arrgh!!
  1806.      */
  1807.     int alignment = __alignof (struct Lisp_Float);
  1808. #else /* !GNUC */
  1809.     /* Best guess is to make the `double' slot be aligned to the size
  1810.        of double (which is probably 8 bytes).  This assumes that it's
  1811.        ok to align the beginning of the structure to the same boundary
  1812.        that the `double' slot in it is supposed to be aligned to; this
  1813.        should be ok because presumably there is padding in the layout
  1814.        of the struct to account for this.
  1815.      */
  1816.     int alignment = sizeof (float_data (f));
  1817. #endif
  1818.     char *p = ((char *) PUREBEG + pureptr);
  1819.  
  1820.     p = (char *) (((unsigned LISP_WORD_TYPE) p + alignment - 1) & - alignment);
  1821.     pureptr = p - (char *) PUREBEG;
  1822.   }
  1823.  
  1824.   if (!check_purespace (sizeof (struct Lisp_Float)))
  1825.     return (make_float (num));
  1826.  
  1827.   f = (struct Lisp_Float *) (PUREBEG + pureptr);
  1828.   set_lheader_implementation (&(f->lheader), lrecord_float);
  1829.   pureptr += sizeof (struct Lisp_Float);
  1830.   bump_purestat (&purestat_float, sizeof (struct Lisp_Float));
  1831.  
  1832.   float_next (f) = ((struct Lisp_Float *) -1);
  1833.   float_data (f) = num;
  1834.   XSETFLOAT (val, f);
  1835.   return (val);
  1836. }
  1837.  
  1838. #endif /* LISP_FLOAT_TYPE */
  1839.  
  1840. Lisp_Object
  1841. make_pure_vector (int len, Lisp_Object init)
  1842. {
  1843.   Lisp_Object new;
  1844.   int size = (sizeof (struct Lisp_Vector)
  1845.               + (len - 1) * sizeof (Lisp_Object));
  1846.  
  1847.   init = Fpurecopy (init);
  1848.  
  1849.   if (!check_purespace (size))
  1850.     return (make_vector (len, init));
  1851.  
  1852.   XSETVECTOR (new, PUREBEG + pureptr);
  1853.   pureptr += size;
  1854.   bump_purestat (&purestat_vector_all, size);
  1855.  
  1856.   XVECTOR (new)->size = len;
  1857.  
  1858.   for (size = 0; size < len; size++)
  1859.     vector_data (XVECTOR (new))[size] = init;
  1860.  
  1861.   return (new);
  1862. }
  1863.  
  1864. #if 0
  1865. /* Presently unused */
  1866. void *
  1867. alloc_pure_lrecord (int size, struct lrecord_implementation *implementation)
  1868. {
  1869.   struct lrecord_header *header = (void *) (PUREBEG + pureptr);
  1870.  
  1871.   if (pureptr + size > PURESIZE)
  1872.     pure_storage_exhausted ();
  1873.  
  1874.   set_lheader_implementation (header, implementation);
  1875.   header->next = 0;
  1876.   return (header);
  1877. }
  1878. #endif
  1879.  
  1880.  
  1881.  
  1882. DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
  1883.   "Make a copy of OBJECT in pure storage.\n\
  1884. Recursively copies contents of vectors and cons cells.\n\
  1885. Does not copy symbols.")
  1886.      (obj)
  1887.      Lisp_Object obj;
  1888. {
  1889.   int i;
  1890.   if (!purify_flag)
  1891.     return (obj);
  1892.  
  1893.   if (!POINTER_TYPE_P (XTYPE (obj))
  1894.       || PURIFIED (XPNTR (obj)))
  1895.     return (obj);
  1896.  
  1897.   switch (XTYPE (obj))
  1898.     {
  1899.     case Lisp_Cons:
  1900.       return pure_cons (XCAR (obj), XCDR (obj));
  1901.  
  1902.     case Lisp_String:
  1903.       /* #### also needs to copy the string properties, esp.
  1904.      so that I18N3 translation will work properly. */
  1905.       return make_pure_string (string_data (XSTRING (obj)),
  1906.                    string_length (XSTRING (obj)), 
  1907.                                0);
  1908.  
  1909.     case Lisp_Vector:
  1910.       {
  1911.         struct Lisp_Vector *o = XVECTOR (obj);
  1912.         Lisp_Object new = make_pure_vector (vector_length (o), Qnil);
  1913.         for (i = 0; i < vector_length (o); i++)
  1914.       vector_data (XVECTOR (new))[i] = Fpurecopy (o->contents[i]);
  1915.         return (new);
  1916.       }
  1917.  
  1918.     default:
  1919.       {
  1920.         if (BYTECODEP (obj))
  1921.           {
  1922.             struct Lisp_Bytecode *o = XBYTECODE (obj);
  1923.             Lisp_Object new = make_bytecode (1);
  1924.             struct Lisp_Bytecode *n = XBYTECODE (obj);
  1925.             n->flags = o->flags;
  1926.             n->bytecodes = Fpurecopy (o->bytecodes);
  1927.             n->constants = Fpurecopy (o->constants);
  1928.             n->arglist = Fpurecopy (o->arglist);
  1929.             n->doc_and_interactive = Fpurecopy (o->doc_and_interactive);
  1930.             return (new);
  1931.           }
  1932. #ifdef LISP_FLOAT_TYPE
  1933.         else if (FLOATP (obj))
  1934.           return make_pure_float (float_data (XFLOAT (obj)));
  1935. #endif /* LISP_FLOAT_TYPE */
  1936.     else if (!SYMBOLP (obj))
  1937.           signal_simple_error ("Can't purecopy %S", obj);
  1938.       }
  1939.     }
  1940.   return (obj);
  1941. }
  1942.  
  1943.  
  1944.  
  1945. void
  1946. report_pure_usage (int report_impurities,
  1947.                    int die_if_pure_storage_exceeded)
  1948. {
  1949.   if (pure_lossage)
  1950.     {
  1951.       CONST long report_round = 5000;
  1952.  
  1953.       message ("\n****\tPure Lisp storage exhausted!\n"
  1954.  "\tCheck whether you are loading .el files when .elc files were intended.\n"
  1955.  "\tOtherwise, increase PURESIZE in puresize.h and relink.\n\n"
  1956.  "\tPURESIZE is presently %ld.\n"
  1957.  "\tAn additional %ld bytes will guarantee enough pure space;\n"
  1958.  "\ta smaller increment may work (due to structure-sharing).\n"
  1959.  "****",
  1960.            (long) PURESIZE,
  1961.                (((pure_lossage + report_round - 1)
  1962.                  / report_round) * report_round));
  1963.     }
  1964.   else
  1965.     {
  1966.       int lost = (PURESIZE - pureptr) / 1024;
  1967.       char buf[200];
  1968.  
  1969.       sprintf (buf, "Purespace usage: %ld of %ld (%d%%",
  1970.                pureptr, (long) PURESIZE,
  1971.                (int) (pureptr / (PURESIZE / 100.0) + 0.5));
  1972.       if (lost > 2)
  1973.         sprintf (buf + strlen (buf), " -- %dk wasted", lost);
  1974.       strcat (buf, ").");
  1975.       message ("%s", buf);
  1976.     }
  1977.  
  1978. #ifdef PURESTAT
  1979.   {
  1980.     int iii;
  1981.  
  1982.     purestat_vector_other.nbytes =
  1983.       purestat_vector_all.nbytes - purestat_vector_bytecode_constants.nbytes;
  1984.     purestat_vector_other.nobjects =
  1985.       purestat_vector_all.nobjects - purestat_vector_bytecode_constants.nobjects;
  1986.  
  1987.     purestat_string_other.nbytes =
  1988.       purestat_string_all.nbytes - (purestat_string_pname.nbytes +
  1989.                                     purestat_string_bytecodes.nbytes +
  1990.                                     purestat_string_interactive.nbytes +
  1991.                                     purestat_string_documentation.nbytes +
  1992. #ifdef I18N3
  1993.                                     purestat_string_domain.nbytes +
  1994. #endif
  1995.                                     purestat_string_other_function.nbytes);
  1996.     purestat_string_other.nobjects =
  1997.       purestat_string_all.nobjects - (purestat_string_pname.nobjects +
  1998.                                       purestat_string_bytecodes.nobjects +
  1999.                                       purestat_string_interactive.nobjects +
  2000.                                       purestat_string_documentation.nobjects +
  2001. #ifdef I18N3
  2002.                                       purestat_string_domain.nobjects +
  2003. #endif
  2004.                                       purestat_string_other_function.nobjects);
  2005.  
  2006.     message ("   %-24stotal:   bytes:", "");
  2007.  
  2008.     for (iii = 0; iii < countof (purestats); iii++)
  2009.       if (!purestats[iii])
  2010.         message ("");
  2011.       else
  2012.         message ("   %-24s%5d  %7d  %2d%%",
  2013.                  purestats[iii]->name,
  2014.                  purestats[iii]->nobjects,
  2015.                  purestats[iii]->nbytes,
  2016.                  (int) (purestats[iii]->nbytes / (pureptr / 100.0) + 0.5));
  2017.   }
  2018. #endif /* PURESTAT */
  2019.  
  2020.  
  2021.   if (report_impurities)
  2022.     {
  2023.       Lisp_Object tem = Felt (Fgarbage_collect (), make_number (5));
  2024.       struct gcpro gcpro1;
  2025.       GCPRO1 (tem);
  2026.       message ("\nImpurities:");
  2027.       while (!NILP (tem))
  2028.     {
  2029.       if (CONSP (tem) && SYMBOLP (Fcar (tem)) && CONSP (Fcdr (tem)))
  2030.         {
  2031.           int total = XINT (Fcar (Fcdr (tem)));
  2032.           if (total > 0)
  2033.         {
  2034.           char buf [100];
  2035.           char *s = buf;
  2036.           memcpy (buf, string_data (XSYMBOL (Fcar (tem))->name),
  2037.               string_length (XSYMBOL (Fcar (tem))->name) + 1);
  2038.           while (*s++) if (*s == '-') *s = ' ';
  2039.           s--; *s++ = ':'; *s = 0;
  2040.           message ("   %-32s%6d", buf, total);
  2041.         }
  2042.           tem = Fcdr (Fcdr (tem));
  2043.         }
  2044.       else            /* WTF?! */
  2045.         {
  2046.           Fprin1 (tem, Qexternal_debugging_output);
  2047.           tem = Qnil;
  2048.         }
  2049.     }
  2050.       UNGCPRO;
  2051.       garbage_collect_1 ();         /* GC garbage_collect's garbage */
  2052.     }
  2053.   message ("");
  2054.  
  2055.   if (pure_lossage && die_if_pure_storage_exceeded)
  2056.     fatal ("Pure storage exhausted");
  2057. }
  2058.  
  2059.  
  2060. /**********************************************************************/
  2061. /* staticpro                                                          */
  2062. /**********************************************************************/
  2063.  
  2064. struct gcpro *gcprolist;
  2065.  
  2066. /* 415 used Mly 29-Jun-93 */
  2067. #define NSTATICS 1500
  2068. /* Not "static" because of linker lossage on some systems */
  2069. Lisp_Object *staticvec[NSTATICS]
  2070.      /* Force it into data space! */
  2071.      = {0};
  2072. static int staticidx;
  2073.  
  2074. /* Put an entry in staticvec, pointing at the variable whose address is given
  2075.  */
  2076. void
  2077. staticpro (Lisp_Object *varaddress)
  2078. {
  2079.   if (staticidx >= countof (staticvec))
  2080.     abort ();
  2081.   staticvec[staticidx++] = varaddress;
  2082. }
  2083.  
  2084.  
  2085. /* Mark reference to a Lisp_Object.  If the object referred to has not been
  2086.    seen yet, recursively mark all the references contained in it. */
  2087.    
  2088. static void
  2089. mark_object (Lisp_Object obj)
  2090. {
  2091.  tail_recurse:
  2092.  
  2093.   if (!POINTER_TYPE_P (XGCTYPE (obj)))
  2094.     return;
  2095.   if (PURIFIED (XPNTR (obj)))
  2096.     return;
  2097.   switch (XGCTYPE (obj))
  2098.     {
  2099.     case Lisp_Cons:
  2100.       {
  2101.     struct Lisp_Cons *ptr = XCONS (obj);
  2102.     if (XMARKBIT (ptr->car))
  2103.       break;
  2104.     XMARK (ptr->car);
  2105.     /* If the cdr is nil, tail-recurse on the car.  */
  2106.     if (EQ (ptr->cdr, Qnil))
  2107.       {
  2108.         obj = ptr->car;
  2109.       }
  2110.     else
  2111.       {
  2112.         mark_object (ptr->car);
  2113.         obj = ptr->cdr;
  2114.       }
  2115.     goto tail_recurse;
  2116.       }
  2117.  
  2118.     case Lisp_Record:
  2119.     /* case Lisp_Symbol_Value_Magic: */
  2120.       {
  2121.     struct lrecord_header *lheader = XRECORD_LHEADER (obj);
  2122.     CONST struct lrecord_implementation *implementation
  2123.       = lheader->implementation;
  2124.  
  2125.     if (! MARKED_RECORD_HEADER_P (lheader) &&
  2126.         ! UNMARKABLE_RECORD_HEADER_P (lheader))
  2127.       {
  2128.         MARK_RECORD_HEADER (lheader);
  2129.         if (implementation->marker != 0)
  2130.           {
  2131.         obj = ((implementation->marker) (obj, mark_object));
  2132.         if (!NILP (obj)) goto tail_recurse;
  2133.           }
  2134.       }
  2135.       }
  2136.       break;
  2137.  
  2138.     case Lisp_String:
  2139.       {
  2140.     struct Lisp_String *ptr = XSTRING (obj);
  2141.  
  2142.     if (!XMARKBIT (ptr->plist))
  2143.       {
  2144.         XMARK (ptr->plist);
  2145.         obj = ptr->plist;
  2146.         goto tail_recurse;
  2147.       }
  2148.       }
  2149.       break;
  2150.  
  2151.     case Lisp_Vector:
  2152.       {
  2153.     struct Lisp_Vector *ptr = XVECTOR (obj);
  2154.     int len = vector_length (ptr);
  2155.     int i;
  2156.  
  2157.     if (len < 0)
  2158.       break;        /* Already marked */
  2159.     ptr->size = -1 - len;    /* Else mark it */
  2160.     for (i = 0; i < len - 1; i++) /* and then mark its elements */
  2161.       mark_object (ptr->contents[i]);
  2162.         if (len > 0)
  2163.         {
  2164.           obj = ptr->contents[len - 1];
  2165.           goto tail_recurse;
  2166.         }
  2167.       }
  2168.       break;
  2169.  
  2170. #ifndef LRECORD_SYMBOL
  2171.     case Lisp_Symbol:
  2172.       {
  2173.     struct Lisp_Symbol *sym = XSYMBOL (obj);
  2174.  
  2175.     while (!XMARKBIT (sym->plist))
  2176.       {
  2177.         XMARK (sym->plist);
  2178.         mark_object (sym->value);
  2179.         mark_object (sym->function);
  2180.         {
  2181.           /*  Open-code mark_string */
  2182.           /*  symbol->name is a struct Lisp_String *, not a Lisp_Object */
  2183.           struct Lisp_String *pname = sym->name;
  2184.           if (!PURIFIED (pname)
  2185.           && !XMARKBIT (pname->plist))
  2186.         {
  2187.           XMARK (pname->plist);
  2188.           mark_object (pname->plist);
  2189.         }
  2190.         }
  2191.         if (!symbol_next (sym))
  2192.           {
  2193.         obj = sym->plist;
  2194.         goto tail_recurse;
  2195.           }
  2196.         mark_object (sym->plist);
  2197.         /* Mark the rest of the symbols in the hash-chain */
  2198.         sym = symbol_next (sym);
  2199.       }
  2200.       }
  2201.       break;
  2202. #endif /* !LRECORD_SYMBOL */
  2203.  
  2204.     default:
  2205.       abort ();
  2206.     }
  2207. }
  2208.  
  2209. #ifdef PURESTAT
  2210. /* Simpler than mark-object, because pure structure can't 
  2211.    have any circularities
  2212.  */
  2213.  
  2214. #if 0 /* unused */
  2215. static int idiot_c_doesnt_have_closures;
  2216. static void
  2217. idiot_c (Lisp_Object obj)
  2218. {
  2219.   idiot_c_doesnt_have_closures += pure_sizeof (obj, 1);
  2220. }
  2221. #endif /* unused */
  2222.  
  2223. /* recurse arg isn't actually used */
  2224. static int
  2225. pure_sizeof (Lisp_Object obj /*, int recurse */)
  2226. {
  2227.   int total = 0;
  2228.  
  2229.   /*tail_recurse: */
  2230.   if (!POINTER_TYPE_P (XTYPE (obj))
  2231.       || !PURIFIED (XPNTR (obj)))
  2232.     return (total);
  2233.  
  2234.   /* symbol's sizes are accounted for separately */
  2235.   if (SYMBOLP (obj))
  2236.     return (total);
  2237.  
  2238.   switch (XTYPE (obj))
  2239.     {
  2240.     case Lisp_String:
  2241.       {
  2242.     struct Lisp_String *ptr = XSTRING (obj);
  2243.         int size = string_length (ptr);
  2244.  
  2245.         if (string_data (ptr) !=
  2246.         (unsigned char *) ptr + sizeof (struct Lisp_String))
  2247.       {
  2248.         /* string-data not allocated contiguously.  
  2249.            Probably (better be!!) a pointer constant "C" data. */
  2250.         size = sizeof (struct Lisp_String);
  2251.       }
  2252.         else
  2253.       {
  2254.         size = sizeof (struct Lisp_String) + size + 1;
  2255.         size = ALIGN_SIZE (size, sizeof (Lisp_Object));
  2256.       }
  2257.         total += size;
  2258.       }
  2259.       break;
  2260.  
  2261.     case Lisp_Vector:
  2262.       {
  2263.         struct Lisp_Vector *ptr = XVECTOR (obj);
  2264.         int len = vector_length (ptr);
  2265.  
  2266.         total += (sizeof (struct Lisp_Vector)
  2267.                   + (len - 1) * sizeof (Lisp_Object));
  2268. #if 0 /* unused */
  2269.         if (!recurse)
  2270.           break;
  2271.         { 
  2272.           int i;
  2273.       for (i = 0; i < len - 1; i++)
  2274.         total += pure_sizeof (ptr->contents[i], 1);
  2275.     }
  2276.         if (len > 0)
  2277.       {
  2278.         obj = ptr->contents[len - 1];
  2279.         goto tail_recurse;
  2280.       }
  2281. #endif /* unused */
  2282.       }
  2283.       break;
  2284.  
  2285.     case Lisp_Record:
  2286.       {
  2287.     struct lrecord_header *lheader = XRECORD_LHEADER (obj);
  2288.     CONST struct lrecord_implementation *implementation
  2289.       = lheader->implementation;
  2290.  
  2291.         if (implementation->size_in_bytes_method)
  2292.           total += ((implementation->size_in_bytes_method) (lheader));
  2293.     else
  2294.           total += implementation->static_size;
  2295.  
  2296. #if 0 /* unused */
  2297.         if (!recurse)
  2298.           break;
  2299.  
  2300.     if (implementation->marker != 0)
  2301.       {
  2302.         int old = idiot_c_doesnt_have_closures;
  2303.  
  2304.         idiot_c_doesnt_have_closures = 0;
  2305.         obj = ((implementation->marker) (obj, idiot_c));
  2306.         total += idiot_c_doesnt_have_closures;
  2307.         idiot_c_doesnt_have_closures = old;
  2308.             
  2309.         if (!NILP (obj)) goto tail_recurse;
  2310.       }
  2311. #endif /* unused */
  2312.       }
  2313.       break;
  2314.  
  2315.     case Lisp_Cons:
  2316.       {
  2317.         struct Lisp_Cons *ptr = XCONS (obj);
  2318.         total += sizeof (*ptr);
  2319. #if 0 /* unused */
  2320.         if (!recurse)
  2321.           break;
  2322.     /* If the cdr is nil, tail-recurse on the car.  */
  2323.     if (EQ (ptr->cdr, Qnil))
  2324.       {
  2325.         obj = ptr->car;
  2326.       }
  2327.     else
  2328.       {
  2329.         total += pure_sizeof (ptr->car, 1);
  2330.         obj = ptr->cdr;
  2331.       }
  2332.     goto tail_recurse;
  2333. #endif /* unused */
  2334.       }
  2335.       break;
  2336.  
  2337.       /* Others can't be purified */
  2338.     default:
  2339.       abort ();
  2340.     }
  2341.   return (total);
  2342. }
  2343. #endif /* PURESTAT */
  2344.  
  2345.  
  2346.  
  2347.  
  2348. /* Find all structures not marked, and free them. */
  2349.  
  2350. static int gc_count_num_vector_used, gc_count_vector_total_size;
  2351. static int gc_count_vector_storage;
  2352. static int gc_count_num_short_string_in_use;
  2353. static int gc_count_string_total_size;
  2354. static int gc_count_short_string_total_size;
  2355.  
  2356. /* static int gc_count_total_records_used, gc_count_records_total_size; */
  2357.  
  2358.  
  2359. /* This will be used more extensively In The Future */
  2360. static int last_lrecord_type_index_assigned;
  2361.  
  2362. static CONST struct lrecord_implementation *lrecord_implementations_table[128];
  2363. #define max_lrecord_type (countof (lrecord_implementations_table) - 1)
  2364.  
  2365. static int
  2366. lrecord_type_index (CONST struct lrecord_implementation *implementation)
  2367. {
  2368.   int type_index = *(implementation->lrecord_type_index);
  2369.   /* Have to do this circuitous and validation test because of problems
  2370.      dumping out initialized variables (ie can't set xxx_type_index to -1
  2371.      because that would make xxx_type_index read-only in a dumped emacs. */
  2372.   if (type_index < 0 || type_index > max_lrecord_type
  2373.       || lrecord_implementations_table[type_index] != implementation)
  2374.     {
  2375.       if (last_lrecord_type_index_assigned == max_lrecord_type)
  2376.         abort ();
  2377.       type_index = ++last_lrecord_type_index_assigned;
  2378.       lrecord_implementations_table[type_index] = implementation;
  2379.       *(implementation->lrecord_type_index) = type_index;
  2380.     }
  2381.   return (type_index);
  2382. }
  2383.  
  2384. /* stats on lcrecords in use - kinda kludgy */
  2385.  
  2386. static struct
  2387. {
  2388.   int instances_in_use;
  2389.   int bytes_in_use;
  2390.   int instances_freed;
  2391.   int bytes_freed;
  2392. } lcrecord_stats [countof (lrecord_implementations_table)];
  2393.  
  2394.  
  2395. static void
  2396. reset_lcrecord_stats (void)
  2397. {
  2398.   int i;
  2399.   for (i = 0; i < countof (lcrecord_stats); i++)
  2400.     {
  2401.       lcrecord_stats[i].instances_in_use = 0;
  2402.       lcrecord_stats[i].bytes_in_use = 0;
  2403.       lcrecord_stats[i].instances_freed = 0;
  2404.       lcrecord_stats[i].bytes_freed = 0;
  2405.     }
  2406. }
  2407.  
  2408. static void
  2409. tick_lcrecord_stats (CONST struct lrecord_header *h, int free_p)
  2410. {
  2411.   CONST struct lrecord_implementation *implementation = h->implementation;
  2412.   int type_index = lrecord_type_index (implementation);
  2413.   unsigned int sz = (implementation->size_in_bytes_method
  2414.                      ? ((implementation->size_in_bytes_method) (h))
  2415.                      : implementation->static_size);
  2416.  
  2417.   if (free_p)
  2418.     {
  2419.       lcrecord_stats[type_index].instances_freed++;
  2420.       lcrecord_stats[type_index].bytes_freed += sz;
  2421.     }
  2422.   else
  2423.     {
  2424.       lcrecord_stats[type_index].instances_in_use++;
  2425.       lcrecord_stats[type_index].bytes_in_use += sz;
  2426.     }
  2427. }
  2428.  
  2429.  
  2430. /* Free all unmarked records */
  2431. static void
  2432. sweep_lcrecords_1 (struct lcrecord_header **prev, int *used)
  2433. {
  2434.   struct lcrecord_header *header;
  2435.   int num_used = 0;
  2436.   /* int total_size = 0; */
  2437.   reset_lcrecord_stats ();
  2438.   for (header = *prev; header; )
  2439.     {
  2440.       struct lrecord_header *h = &(header->lheader);
  2441.       if (MARKED_RECORD_HEADER_P (h))
  2442.     {
  2443.       UNMARK_RECORD_HEADER (h);
  2444.       num_used++;
  2445.       /* total_size += ((n->implementation->size_in_bytes) (h));*/
  2446.       prev = &(header->next);
  2447.       header = *prev;
  2448.       tick_lcrecord_stats (h, 0);
  2449.     }
  2450.       else
  2451.     {
  2452.       struct lcrecord_header *next = header->next;
  2453.           *prev = next;
  2454.       tick_lcrecord_stats (h, 1);
  2455.       if (h->implementation->finalizer)
  2456.         ((h->implementation->finalizer) (h, 0));
  2457.       xfree (header);
  2458.       header = next;
  2459.     }
  2460.     }
  2461.   *used = num_used;
  2462.   /* *total = total_size; */
  2463. }
  2464.  
  2465. static void
  2466. sweep_vectors_1 (Lisp_Object *prev, 
  2467.                  int *used, int *total, int *storage)
  2468. {
  2469.   Lisp_Object vector;
  2470.   int num_used = 0;
  2471.   int total_size = 0;
  2472.   int total_storage = 0;
  2473.  
  2474.   for (vector = *prev; VECTORP (vector); )
  2475.     {
  2476.       struct Lisp_Vector *v = XVECTOR (vector);
  2477.       int len = v->size;
  2478.       if (len < 0)     /* marked */
  2479.     {
  2480.           len = - (len + 1);
  2481.       v->size = len;
  2482.       total_size += len;
  2483.           total_storage += (MALLOC_OVERHEAD
  2484.                             + sizeof (struct Lisp_Vector)
  2485.                             + (len - 1 + 1) * sizeof (Lisp_Object));
  2486.       num_used++;
  2487.       prev = &(vector_next (v));
  2488.       vector = *prev;
  2489.     }
  2490.       else
  2491.     {
  2492.           Lisp_Object next = vector_next (v);
  2493.           *prev = next;
  2494.       xfree (v);
  2495.       vector = next;
  2496.     }
  2497.     }
  2498.   *used = num_used;
  2499.   *total = total_size;
  2500.   *storage = total_storage;
  2501. }
  2502.  
  2503. /* And the Lord said: Thou shalt use the `c-backslash-region' command
  2504.    to make macros prettier. */
  2505.  
  2506. #ifdef ERROR_CHECK_GC
  2507.  
  2508. #define SWEEP_FIXED_TYPE_BLOCK(typename, obj_type)            \
  2509. do {                                    \
  2510.   struct typename##_block *_frob_current;                \
  2511.   struct typename##_block **_frob_prev;                    \
  2512.   int _frob_limit;                            \
  2513.   int num_free = 0, num_used = 0;                    \
  2514.                                     \
  2515.   for (_frob_prev = ¤t_##typename##_block,            \
  2516.        _frob_current = current_##typename##_block,            \
  2517.        _frob_limit = current_##typename##_block_index;            \
  2518.        _frob_current;                            \
  2519.        )                                \
  2520.     {                                    \
  2521.       int _frob_iii;                            \
  2522.                                     \
  2523.       for (_frob_iii = 0; _frob_iii < _frob_limit; _frob_iii++)        \
  2524.     {                                \
  2525.       obj_type *_frob_victim = &(_frob_current->block[_frob_iii]);    \
  2526.                                     \
  2527.       if (FREE_STRUCT_P (_frob_victim))                \
  2528.         {                                \
  2529.           num_free++;                        \
  2530.         }                                \
  2531.       else if (!MARKED_##typename##_P (_frob_victim))        \
  2532.         {                                \
  2533.           num_free++;                        \
  2534.           FREE_FIXED_TYPE (typename, obj_type, _frob_victim);    \
  2535.         }                                \
  2536.       else                                \
  2537.         {                                \
  2538.           num_used++;                        \
  2539.           UNMARK_##typename (_frob_victim);                \
  2540.         }                                \
  2541.     }                                \
  2542.       _frob_prev = &(_frob_current->prev);                \
  2543.       _frob_current = _frob_current->prev;                \
  2544.       _frob_limit = countof (current_##typename##_block->block);    \
  2545.     }                                    \
  2546.                                     \
  2547.   gc_count_num_##typename##_in_use = num_used;                \
  2548.   gc_count_num_##typename##_freelist = num_free;            \
  2549. } while (0)
  2550.  
  2551. #else
  2552.  
  2553. #define SWEEP_FIXED_TYPE_BLOCK(typename, obj_type)                  \
  2554. do {                                          \
  2555.   struct typename##_block *_frob_current;                      \
  2556.   struct typename##_block **_frob_prev;                          \
  2557.   int _frob_limit;                                  \
  2558.   int num_free = 0, num_used = 0;                          \
  2559.                                           \
  2560.   typename##_free_list = 0;                              \
  2561.                                           \
  2562.   for (_frob_prev = ¤t_##typename##_block,                  \
  2563.        _frob_current = current_##typename##_block,                  \
  2564.        _frob_limit = current_##typename##_block_index;                  \
  2565.        _frob_current;                                  \
  2566.        )                                      \
  2567.     {                                          \
  2568.       int _frob_iii;                                  \
  2569.       int _frob_empty = 1;                              \
  2570.       obj_type *_frob_old_free_list = typename##_free_list;              \
  2571.                                           \
  2572.       for (_frob_iii = 0; _frob_iii < _frob_limit; _frob_iii++)              \
  2573.     {                                      \
  2574.       obj_type *_frob_victim = &(_frob_current->block[_frob_iii]);          \
  2575.                                           \
  2576.       if (FREE_STRUCT_P (_frob_victim))                      \
  2577.         {                                      \
  2578.           num_free++;                              \
  2579.           PUT_FIXED_TYPE_ON_FREE_LIST (typename, obj_type, _frob_victim); \
  2580.         }                                      \
  2581.       else if (!MARKED_##typename##_P (_frob_victim))              \
  2582.         {                                      \
  2583.           num_free++;                              \
  2584.           FREE_FIXED_TYPE (typename, obj_type, _frob_victim);          \
  2585.         }                                      \
  2586.       else                                      \
  2587.         {                                      \
  2588.           _frob_empty = 0;                              \
  2589.           num_used++;                              \
  2590.           UNMARK_##typename (_frob_victim);                      \
  2591.         }                                      \
  2592.     }                                      \
  2593.       if (!_frob_empty)                                  \
  2594.     {                                      \
  2595.       _frob_prev = &(_frob_current->prev);                      \
  2596.       _frob_current = _frob_current->prev;                      \
  2597.     }                                      \
  2598.       else if (_frob_current == current_##typename##_block              \
  2599.            && !_frob_current->prev)                          \
  2600.     {                                      \
  2601.       /* No real point in freeing sole allocation block */              \
  2602.       break;                                  \
  2603.     }                                      \
  2604.       else                                      \
  2605.     {                                      \
  2606.       struct typename##_block *_frob_victim_block = _frob_current;          \
  2607.       if (_frob_victim_block == current_##typename##_block)              \
  2608.         current_##typename##_block_index                      \
  2609.           = countof (current_##typename##_block->block);              \
  2610.       _frob_current = _frob_current->prev;                      \
  2611.       {                                      \
  2612.         *_frob_prev = _frob_current;                      \
  2613.         xfree (_frob_victim_block);                          \
  2614.         /* Restore free list to what it was before victim was swept */    \
  2615.         typename##_free_list = _frob_old_free_list;                  \
  2616.         num_free -= _frob_limit;                          \
  2617.       }                                      \
  2618.     }                                      \
  2619.       _frob_limit = countof (current_##typename##_block->block);          \
  2620.     }                                          \
  2621.                                           \
  2622.   gc_count_num_##typename##_in_use = num_used;                      \
  2623.   gc_count_num_##typename##_freelist = num_free;                  \
  2624. } while (0)
  2625.  
  2626. #endif
  2627.  
  2628.  
  2629.  
  2630.  
  2631. static void
  2632. sweep_conses (void)
  2633. {
  2634. #define MARKED_cons_P(ptr) XMARKBIT ((ptr)->car)
  2635. #define UNMARK_cons(ptr) do { XUNMARK ((ptr)->car); } while (0)
  2636. #define ADDITIONAL_FREE_cons(ptr)
  2637.  
  2638.   SWEEP_FIXED_TYPE_BLOCK (cons, struct Lisp_Cons);
  2639. }
  2640.  
  2641. /* Explicitly free a cons cell.  */
  2642. void
  2643. free_cons (struct Lisp_Cons *ptr)
  2644. {
  2645. #ifdef ERROR_CHECK_GC
  2646.   /* If the CAR is not an int, then it will be a pointer, which will
  2647.      always be four-byte aligned.  If this cons cell has already been
  2648.      placed on the free list, however, its car will probably contain
  2649.      a chain pointer to the next cons on the list, which has cleverly
  2650.      had all its 0's and 1's inverted.  This allows for a quick
  2651.      check to make sure we're not freeing something already freed. */
  2652.   if (!INTP (ptr->car))
  2653.     assert ((((int) XPNTR (ptr->car)) & 3) == 0);
  2654. #endif
  2655. #ifndef ALLOC_NO_POOLS
  2656.   FREE_FIXED_TYPE (cons, struct Lisp_Cons, ptr);
  2657.   /* This isn't completely necessary because the counts are recalculated
  2658.      each GC, but it helps the ERROR_CHECK_GC case be more accurate */
  2659.   gc_count_num_cons_freelist++;
  2660. #endif /* ALLOC_NO_POOLS */
  2661. }
  2662.  
  2663. /* explicitly free a list.  You **must make sure** that you have
  2664.    created all the cons cells that make up this list and that there
  2665.    are no pointers to any of these cons cells anywhere else.  If there
  2666.    are, you will lose. */
  2667.  
  2668. void
  2669. free_list (Lisp_Object list)
  2670. {
  2671.   Lisp_Object rest, next;
  2672.  
  2673.   for (rest = list; !NILP (rest); rest = next)
  2674.     {
  2675.       next = XCDR (rest);
  2676.       free_cons (XCONS (rest));
  2677.     }
  2678. }
  2679.  
  2680. /* explicitly free an alist.  You **must make sure** that you have
  2681.    created all the cons cells that make up this alist and that there
  2682.    are no pointers to any of these cons cells anywhere else.  If there
  2683.    are, you will lose. */
  2684.  
  2685. void
  2686. free_alist (Lisp_Object alist)
  2687. {
  2688.   Lisp_Object rest, next;
  2689.  
  2690.   for (rest = alist; !NILP (rest); rest = next)
  2691.     {
  2692.       next = XCDR (rest);
  2693.       free_cons (XCONS (XCAR (rest)));
  2694.       free_cons (XCONS (rest));
  2695.     }
  2696. }
  2697.  
  2698. static void
  2699. sweep_bytecodes (void)
  2700. {
  2701. #define MARKED_bytecode_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
  2702. #define UNMARK_bytecode(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
  2703. #define ADDITIONAL_FREE_bytecode(ptr)
  2704.  
  2705.   SWEEP_FIXED_TYPE_BLOCK (bytecode, struct Lisp_Bytecode);
  2706. }
  2707.  
  2708.  
  2709. #ifdef LISP_FLOAT_TYPE
  2710. static void
  2711. sweep_floats (void)
  2712. {
  2713. #define MARKED_float_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
  2714. #define UNMARK_float(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
  2715. #define ADDITIONAL_FREE_float(ptr)
  2716.  
  2717.   SWEEP_FIXED_TYPE_BLOCK (float, struct Lisp_Float);
  2718. }
  2719. #endif /* LISP_FLOAT_TYPE */
  2720.  
  2721. static void
  2722. sweep_symbols (void)
  2723. {
  2724. #ifndef LRECORD_SYMBOL
  2725. # define MARKED_symbol_P(ptr) XMARKBIT ((ptr)->plist)
  2726. # define UNMARK_symbol(ptr) do { XUNMARK ((ptr)->plist); } while (0)
  2727. #else
  2728. # define MARKED_symbol_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
  2729. # define UNMARK_symbol(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
  2730. #endif /* !LRECORD_SYMBOL */
  2731. #define ADDITIONAL_FREE_symbol(ptr)
  2732.  
  2733.   SWEEP_FIXED_TYPE_BLOCK (symbol, struct Lisp_Symbol);
  2734. }
  2735.  
  2736.  
  2737. #ifndef standalone
  2738.  
  2739. #ifdef ENERGIZE
  2740. extern void energize_extent_finalization (struct extent *);
  2741. #else
  2742. #define energize_extent_finalization(extent) /* Empty */
  2743. #endif
  2744.  
  2745. static void
  2746. sweep_extents (void)
  2747. {
  2748. #define MARKED_extent_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
  2749. #define UNMARK_extent(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
  2750. #define ADDITIONAL_FREE_extent(ptr) energize_extent_finalization (ptr)
  2751.  
  2752.   SWEEP_FIXED_TYPE_BLOCK (extent, struct extent);
  2753. }
  2754.  
  2755. static void
  2756. sweep_extent_replicas (void)
  2757. {
  2758. #define MARKED_extent_replica_P(ptr) MARKED_RECORD_HEADER_P(&((ptr)->lheader))
  2759. #define UNMARK_extent_replica(ptr) UNMARK_RECORD_HEADER(&((ptr)->lheader))
  2760. #define ADDITIONAL_FREE_extent_replica(ptr)
  2761.  
  2762.   SWEEP_FIXED_TYPE_BLOCK (extent_replica, struct extent_replica);
  2763. }
  2764.  
  2765. static void
  2766. sweep_events (void)
  2767. {
  2768. #define MARKED_event_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
  2769. #define UNMARK_event(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
  2770. #define ADDITIONAL_FREE_event(ptr)
  2771.  
  2772.   SWEEP_FIXED_TYPE_BLOCK (event, struct Lisp_Event);
  2773. }
  2774.  
  2775. static void
  2776. sweep_markers (void)
  2777. {
  2778. #define MARKED_marker_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
  2779. #define UNMARK_marker(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
  2780. #define ADDITIONAL_FREE_marker(ptr)                    \
  2781.   do { Lisp_Object tem;                            \
  2782.        XSETMARKER (tem, ptr);                        \
  2783.        unchain_marker (tem);                        \
  2784.      } while (0)
  2785.  
  2786.   SWEEP_FIXED_TYPE_BLOCK (marker, struct Lisp_Marker);
  2787. }
  2788. #endif /* not standalone */
  2789.  
  2790.  
  2791. /* Compactify string chars, relocating the reference to each --
  2792.    free any empty string_chars_block we see. */
  2793. static void
  2794. compact_string_chars (void)
  2795. {
  2796.   struct string_chars_block *to_sb = first_string_chars_block;
  2797.   int to_pos = 0;
  2798.   struct string_chars_block *from_sb;
  2799.  
  2800.   /* Scan each existing string block sequentially, string by string.  */
  2801.   for (from_sb = first_string_chars_block; from_sb; from_sb = from_sb->next)
  2802.     {
  2803.       int from_pos = 0;
  2804.       /* FROM_POS is the index of the next string in the block.  */
  2805.       while (from_pos < from_sb->pos)
  2806.         {
  2807.           struct string_chars *from_s_chars = 
  2808.             (struct string_chars *) &(from_sb->string_chars[from_pos]);
  2809.           struct string_chars *to_s_chars;
  2810.           struct Lisp_String *string = from_s_chars->string;
  2811.       int size;
  2812.       int fullsize;
  2813.  
  2814.       /* skip completely if this cell is empty. */
  2815.       if (FREE_STRUCT_P (string))
  2816.         continue;
  2817.  
  2818.           size = string_length (string);
  2819.           fullsize = STRING_FULLSIZE (size);
  2820.  
  2821.           if (BIG_STRING_FULLSIZE_P (fullsize))
  2822.             abort ();
  2823.  
  2824.           /* Just skip it if it isn't marked.  */
  2825.           if (!XMARKBIT (string->plist))
  2826.             {
  2827.               from_pos += fullsize;
  2828.               continue;
  2829.             }
  2830.  
  2831.           /* If it won't fit in what's left of TO_SB, close TO_SB out
  2832.              and go on to the next string_chars_block.  We know that TO_SB
  2833.              cannot advance past FROM_SB here since FROM_SB is large enough
  2834.              to currently contain this string. */
  2835.           if ((to_pos + fullsize) > countof (to_sb->string_chars))
  2836.             {
  2837.               to_sb->pos = to_pos;
  2838.               to_sb = to_sb->next;
  2839.               to_pos = 0;
  2840.             }
  2841.              
  2842.           /* Compute new address of this string
  2843.              and update TO_POS for the space being used.  */
  2844.           to_s_chars = (struct string_chars *) &(to_sb->string_chars[to_pos]);
  2845.  
  2846.           /* Copy the string_chars to the new place.  */
  2847.           if (from_s_chars != to_s_chars)
  2848.             memcpy (to_s_chars, from_s_chars, fullsize);
  2849.  
  2850.           /* Relocate FROM_S_CHARS's reference */
  2851.           set_string_data (string, &(to_s_chars->chars[0]));
  2852.              
  2853.           from_pos += fullsize;
  2854.           to_pos += fullsize;
  2855.         }
  2856.     }
  2857.  
  2858.   /* Set current to the last string chars block still used and 
  2859.      free any that follow. */
  2860.   {
  2861.     struct string_chars_block *victim;
  2862.  
  2863.     for (victim = to_sb->next; victim; )
  2864.       {
  2865.     struct string_chars_block *next = victim->next;
  2866.     xfree (victim);
  2867.     victim = next;
  2868.       }
  2869.  
  2870.     current_string_chars_block = to_sb;
  2871.     current_string_chars_block->pos = to_pos;
  2872.     current_string_chars_block->next = 0;
  2873.   }
  2874. }
  2875.  
  2876. #if 1 /* Hack to debug missing purecopy's */
  2877. static int debug_string_purity;
  2878.  
  2879. static void
  2880. debug_string_purity_print (struct Lisp_String *p)
  2881. {
  2882.   Charcount i;
  2883.   Charcount s = string_char_length (p);
  2884.   putc ('\"', stderr);
  2885.   for (i = 0; i < s; i++)
  2886.   {
  2887.     Emchar ch = string_char (p, i);
  2888.     if (ch < 32 || ch >= 126)
  2889.       stderr_out ("\\%03o", ch);
  2890.     else if (ch == '\\' || ch == '\"')
  2891.       stderr_out ("\\%c", ch);
  2892.     else
  2893.       stderr_out ("%c", ch);
  2894.   }
  2895.   stderr_out ("\"\n");
  2896. }
  2897. #endif
  2898.  
  2899.  
  2900. static void
  2901. sweep_strings (void)
  2902. {
  2903.   int num_small_used = 0, num_small_bytes = 0, num_bytes = 0;
  2904.   int debug = debug_string_purity;
  2905.  
  2906. #define MARKED_string_P(ptr) XMARKBIT ((ptr)->plist)
  2907. #define UNMARK_string(ptr)                \
  2908.   do { struct Lisp_String *p = (ptr);            \
  2909.        int size = string_length (p);            \
  2910.        XUNMARK (p->plist);                \
  2911.        num_bytes += size;                \
  2912.        if (!BIG_STRING_SIZE_P (size))            \
  2913.      { num_small_bytes += size;            \
  2914.        num_small_used++;                \
  2915.      }                        \
  2916.        if (debug) debug_string_purity_print (p);    \
  2917.      } while (0)
  2918. #define ADDITIONAL_FREE_string(p)                \
  2919.   do { int size = string_length (p);                \
  2920.        if (BIG_STRING_SIZE_P (size))                \
  2921.      xfree_1 (CHARS_TO_STRING_CHAR (string_data (p)));    \
  2922.      } while (0)
  2923.  
  2924.   SWEEP_FIXED_TYPE_BLOCK (string, struct Lisp_String);
  2925.  
  2926.   gc_count_num_short_string_in_use = num_small_used;
  2927.   gc_count_string_total_size = num_bytes;
  2928.   gc_count_short_string_total_size = num_small_bytes;
  2929. }
  2930.  
  2931.  
  2932. /* I hate duplicating all this crap! */
  2933. static int
  2934. marked_p (Lisp_Object obj)
  2935. {
  2936.   if (!POINTER_TYPE_P (XGCTYPE (obj))) return 1;
  2937.   if (PURIFIED (XPNTR (obj))) return 1;
  2938.   switch (XGCTYPE (obj))
  2939.     {
  2940.     case Lisp_Cons:
  2941.       return XMARKBIT (XCAR (obj));
  2942.     case Lisp_Record:
  2943.       return MARKED_RECORD_HEADER_P (XRECORD_LHEADER (obj));
  2944.     case Lisp_String:
  2945.       return XMARKBIT (XSTRING (obj)->plist);
  2946.     case Lisp_Vector:
  2947.       return (vector_length (XVECTOR (obj)) < 0);
  2948. #ifndef LRECORD_SYMBOL
  2949.     case Lisp_Symbol:
  2950.       return XMARKBIT (XSYMBOL (obj)->plist);
  2951. #endif
  2952.     default:
  2953.       abort ();
  2954.     }
  2955.   return 0;    /* suppress compiler warning */
  2956. }
  2957.  
  2958. static void
  2959. gc_sweep (void)
  2960. {
  2961.   compact_string_chars ();
  2962.  
  2963.   /* Put all unmarked strings on free list, free'ing the string chars
  2964.      of large unmarked strings */
  2965.   sweep_strings ();
  2966.  
  2967.   /* Put all unmarked conses on free list */
  2968.   sweep_conses ();
  2969.  
  2970.   /* Free all unmarked records */
  2971.   {
  2972.     int ignored;
  2973.     sweep_lcrecords_1 (&all_lcrecords, &ignored);
  2974.   }
  2975.  
  2976.   /* Free all unmarked vectors */
  2977.   sweep_vectors_1 (&all_vectors, 
  2978.                    &gc_count_num_vector_used, &gc_count_vector_total_size,
  2979.                    &gc_count_vector_storage);
  2980.  
  2981.   /* Free all unmarked bytecode objects */
  2982.   sweep_bytecodes ();
  2983.  
  2984. #ifdef LISP_FLOAT_TYPE
  2985.   /* Put all unmarked floats on free list */
  2986.   sweep_floats ();
  2987. #endif
  2988.  
  2989.   /* Put all unmarked symbols on free list */
  2990.   sweep_symbols ();
  2991.  
  2992. #ifndef standalone
  2993.   /* Put all unmarked extents on free list */
  2994.   sweep_extents ();
  2995.  
  2996.   /* put all extent replicas on a free_list */
  2997.   sweep_extent_replicas ();
  2998.  
  2999.   /* Put all unmarked markers on free list.
  3000.      Dechain each one first from the buffer into which it points. */
  3001.   sweep_markers ();
  3002.  
  3003.   sweep_events ();
  3004. #endif
  3005.  
  3006. }
  3007.  
  3008. /* Clearing for disksave. */
  3009.  
  3010. extern Lisp_Object Vprocess_environment;
  3011. extern Lisp_Object Vdoc_directory;
  3012. extern Lisp_Object Vconfigure_info_directory;
  3013. extern Lisp_Object Vload_path;
  3014. extern Lisp_Object Vload_history;
  3015. extern Lisp_Object Vshell_file_name;
  3016.  
  3017. void
  3018. disksave_object_finalization (void)
  3019. {
  3020.   /* It's important that certain information from the environment not get
  3021.      dumped with the executable (pathnames, environment variables, etc.).
  3022.      To make it easier to tell when this has happend with strings(1) we
  3023.      clear some known-to-be-garbage blocks of memory, so that leftover
  3024.      results of old evaluation don't look like potential problems.
  3025.      But first we set some notable variables to nil and do one more GC,
  3026.      to turn those strings into garbage.
  3027.    */
  3028.  
  3029.   /* Yeah, this list is pretty ad-hoc... */
  3030.   Vprocess_environment = Qnil;
  3031.   Vexec_directory = Qnil;
  3032.   Vdata_directory = Qnil;
  3033.   Vdoc_directory = Qnil;
  3034.   Vconfigure_info_directory = Qnil;
  3035.   Vexec_path = Qnil;
  3036.   Vload_path = Qnil;
  3037.   /* Vdump_load_path = Qnil; */
  3038.   Vload_history = Qnil;
  3039.   Vshell_file_name = Qnil;
  3040.  
  3041.   garbage_collect_1 ();
  3042.  
  3043.   /* Run the disksave finalization methods of all live objects. */
  3044.   disksave_object_finalization_1 ();
  3045.  
  3046.   /* Zero out the unused portion of purespace */
  3047.   if (!pure_lossage)
  3048.     memset (  (char *) (PUREBEG + pureptr), 0,
  3049.         (((char *) (PUREBEG + PURESIZE)) -
  3050.          ((char *) (PUREBEG + pureptr))));
  3051.  
  3052.   /* Zero out the uninitialized (really, unused) part of the containers
  3053.      for the live strings. */
  3054.   {
  3055.     struct string_chars_block *scb;
  3056.     for (scb = first_string_chars_block; scb; scb = scb->next)
  3057.       /* from the block's fill ptr to the end */
  3058.       memset ((scb->string_chars + scb->pos), 0,
  3059.               sizeof (scb->string_chars) - scb->pos);
  3060.   }
  3061.  
  3062.   /* There, that ought to be enough... */
  3063.  
  3064. }
  3065.  
  3066.  
  3067. Lisp_Object
  3068. restore_gc_inhibit (Lisp_Object val)
  3069. {
  3070.   gc_currently_forbidden = XINT (val);
  3071.   return val;
  3072. }
  3073.  
  3074. /* Maybe we want to use this when doing a "panic" gc after memory_full()? */
  3075. static int gc_hooks_inhibited;
  3076.  
  3077.  
  3078. void
  3079. garbage_collect_1 (void)
  3080. {
  3081.   char stack_top_variable;
  3082.   extern char *stack_bottom;
  3083.   int i;
  3084. #ifdef HAVE_WINDOW_SYSTEM
  3085.   struct frame *f = selected_frame ();
  3086. #endif /* HAVE_WINDOW_SYSTEM */
  3087.   int speccount = specpdl_depth ();
  3088.  
  3089. #ifdef HAVE_WINDOW_SYSTEM
  3090.   int cursor_changed = 0;
  3091. #endif /* HAVE_WINDOW_SYSTEM */
  3092.  
  3093.   if (gc_in_progress != 0)
  3094.     return;
  3095.  
  3096.   if (gc_currently_forbidden || in_display)
  3097.     return;
  3098.  
  3099.   /* Very important to prevent GC during any of the following
  3100.      stuff that might run Lisp code; otherwise, we'll likely
  3101.      have infinite GC recursion. */
  3102.   record_unwind_protect (restore_gc_inhibit,
  3103.                          make_number (gc_currently_forbidden));
  3104.   gc_currently_forbidden = 1;
  3105.  
  3106.   if (!gc_hooks_inhibited)
  3107.     run_hook_trapping_errors ("Error in pre-gc-hook", Qpre_gc_hook);
  3108.  
  3109.   if (!noninteractive)
  3110.     {
  3111. #ifdef HAVE_WINDOW_SYSTEM
  3112.       if (!NILP (Vgc_message) &&
  3113.       !STRINGP (Vgc_message))
  3114.         {
  3115.       /* #### show_gc_cursor should be a device method. */
  3116. #ifdef HAVE_X_WINDOWS
  3117.         if (DEVICE_IS_X (XDEVICE (Vselected_device)))        
  3118.       cursor_changed = x_show_gc_cursor (f, Vgc_message);
  3119. #endif /* HAVE_X_WINDOWS */
  3120. #ifdef HAVE_NEXTSTEP
  3121.         if (DEVICE_IS_NS (XDEVICE (Vselected_device)))        
  3122.       cursor_changed = ns_show_gc_cursor (f, Vgc_message);
  3123. #endif /* HAVE_NEXTSTEP */
  3124.         }
  3125.  
  3126.       /* Don't print messages to the stream device unless we are
  3127.          actually running in batch mode. */
  3128.       if (!cursor_changed
  3129.       && (!DEVICE_IS_STREAM (XDEVICE (Fselected_device ()))
  3130.           || noninteractive))
  3131. #endif /* HAVE_WINDOW_SYSTEM */
  3132.     {
  3133.       char *msg = (STRINGP (Vgc_message)
  3134.                ? GETTEXT ((char *) string_data (XSTRING (Vgc_message)))
  3135.                : GETTEXT ((char *) gc_default_message));
  3136.       Lisp_Object args[2], whole_msg;
  3137.       args[0] = build_string (msg);
  3138.       args[1] = build_string ("...");
  3139.       whole_msg = Fconcat (2, args);
  3140.       echo_area_message (selected_frame (), (Bufbyte *) 0,
  3141.                  whole_msg, 0, string_length (XSTRING (whole_msg)),
  3142.                  Qgarbage_collecting);
  3143.     }
  3144.     }
  3145.  
  3146.   /***** Now we actually start the garbage collection. */
  3147.  
  3148.   gc_in_progress = 1;
  3149.  
  3150.   gc_generation_number[0]++;
  3151.  
  3152. #if MAX_SAVE_STACK > 0
  3153.  
  3154.   /* Save a copy of the contents of the stack, for debugging.  */
  3155.   if (!purify_flag)
  3156.     {
  3157.       i = &stack_top_variable - stack_bottom;
  3158.       if (i < 0) i = -i;
  3159.       if (i < MAX_SAVE_STACK)
  3160.     {
  3161.       if (stack_copy == 0)
  3162.         stack_copy = (char *) malloc (stack_copy_size = i);
  3163.       else if (stack_copy_size < i)
  3164.         stack_copy = (char *) realloc (stack_copy, (stack_copy_size = i));
  3165.       if (stack_copy)
  3166.         {
  3167.           if ((int) (&stack_top_variable - stack_bottom) > 0)
  3168.         memcpy (stack_copy, stack_bottom, i);
  3169.           else
  3170.         memcpy (stack_copy, &stack_top_variable, i);
  3171.         }
  3172.     }
  3173.     }
  3174. #endif /* MAX_SAVE_STACK > 0 */
  3175.  
  3176.   /* Do some totally ad-hoc resource clearing. */
  3177.   /* #### generalize this? */
  3178.   clear_event_resource ();
  3179.   cleanup_specifiers ();
  3180.  
  3181.   /* Mark all the special slots that serve as the roots of accessibility. */
  3182.   {
  3183.     struct gcpro *tail;
  3184.     struct catchtag *catch;
  3185.     struct backtrace *backlist;
  3186.     struct specbinding *bind;
  3187.  
  3188.     for (i = 0; i < staticidx; i++)
  3189.       mark_object (*(staticvec[i]));
  3190.  
  3191.     for (tail = gcprolist; tail; tail = tail->next)
  3192.       {
  3193.     for (i = 0; i < tail->nvars; i++)
  3194.       mark_object (tail->var[i]);
  3195.       }
  3196.  
  3197.     for (bind = specpdl; bind != specpdl_ptr; bind++)
  3198.       {
  3199.     mark_object (bind->symbol);
  3200.     mark_object (bind->old_value);
  3201.       }
  3202.  
  3203.     for (catch = catchlist; catch; catch = catch->next)
  3204.       {
  3205.     mark_object (catch->tag);
  3206.     mark_object (catch->val);
  3207.       }
  3208.  
  3209.     for (backlist = backtrace_list; backlist; backlist = backlist->next)
  3210.       {
  3211.     int nargs = backlist->nargs;
  3212.  
  3213.     mark_object (*backlist->function);
  3214.     if (nargs == UNEVALLED || nargs == MANY)
  3215.       mark_object (backlist->args[0]);
  3216.     else
  3217.       for (i = 0; i < nargs; i++)
  3218.         mark_object (backlist->args[i]);
  3219.       }
  3220.  
  3221.     mark_redisplay (mark_object);
  3222.   }
  3223.  
  3224.   /* OK, now do the after-mark stuff.  This is for things that
  3225.      are only marked when something else is marked (e.g. weak hashtables). */
  3226.   {
  3227.     int did_mark;
  3228.     /* Need to iterate until there's nothing more to mark, in case
  3229.        of chains of mark dependencies. */
  3230.     do
  3231.       {
  3232.         did_mark = 0;
  3233.     did_mark += !!finish_marking_weak_hashtables (marked_p, mark_object);
  3234.     did_mark += !!finish_marking_specifiers (marked_p, mark_object);
  3235.       }
  3236.     while (did_mark);
  3237.   }
  3238.  
  3239.   /* And prune (this needs to be called after everything else has been
  3240.      marked and before we do any sweeping). */
  3241.   prune_weak_hashtables (marked_p);
  3242.   prune_specifiers (marked_p);
  3243.  
  3244.   gc_sweep ();
  3245.  
  3246.   consing_since_gc = 0;
  3247. #ifndef DEBUG_XEMACS
  3248.   /* Allow you to set it really fucking low if you really want ... */
  3249.   if (gc_cons_threshold < 10000)
  3250.     gc_cons_threshold = 10000;
  3251. #endif
  3252.  
  3253.   gc_in_progress = 0;
  3254.  
  3255.   /******* End of garbage collection ********/
  3256.  
  3257.   run_hook_trapping_errors ("Error in post-gc-hook", Qpost_gc_hook);
  3258.  
  3259.   if (!noninteractive)
  3260.     {
  3261. #ifdef HAVE_WINDOW_SYSTEM
  3262.       if (cursor_changed)
  3263.         {
  3264.       /* #### show_gc_cursor should be a device method. */
  3265. #ifdef HAVE_X_WINDOWS
  3266.         if (DEVICE_IS_X (XDEVICE (Vselected_device)))        
  3267.       x_show_gc_cursor (f, Qnil);
  3268. #endif /* HAVE_X_WINDOWS */
  3269. #ifdef HAVE_NEXTSTEP
  3270.         if (DEVICE_IS_NS (XDEVICE (Vselected_device)))        
  3271.       ns_show_gc_cursor (f, Qnil);
  3272. #endif /* HAVE_NEXTSTEP */
  3273.         }
  3274.  
  3275.       if (!cursor_changed
  3276.       && (!DEVICE_IS_STREAM (XDEVICE (Fselected_device ()))
  3277.           || noninteractive))
  3278. #endif /* HAVE_WINDOW_SYSTEM */
  3279.     {
  3280.       char *msg = (STRINGP (Vgc_message)
  3281.                ? GETTEXT ((char *) string_data (XSTRING (Vgc_message)))
  3282.                : GETTEXT ((char *) gc_default_message));
  3283.  
  3284.       /* Show "...done" only if the echo area would otherwise be empty. */
  3285.       if (NILP (clear_echo_area (selected_frame (), 
  3286.                      Qgarbage_collecting, 0)))
  3287.         {
  3288.           Lisp_Object args[2], whole_msg;
  3289.           args[0] = build_string (msg);
  3290.           args[1] = build_string ("... done");
  3291.           whole_msg = Fconcat (2, args);
  3292.           echo_area_message (selected_frame (), (Bufbyte *) 0,
  3293.                  whole_msg, 0, 
  3294.                  string_length (XSTRING (whole_msg)),
  3295.                  Qgarbage_collecting);
  3296.         }
  3297.     }
  3298.     }
  3299.  
  3300.   /* now stop inhibiting GC */
  3301.   unbind_to (speccount, Qnil);
  3302.  
  3303.   if (!breathing_space)
  3304.     {
  3305.       breathing_space = (void *) malloc (4096 - MALLOC_OVERHEAD);
  3306.     }
  3307.  
  3308.   return;
  3309. }
  3310.  
  3311. #ifdef EMACS_BTL
  3312.  /* This isn't actually called.  BTL recognises the stack frame of the top
  3313.     of the garbage collector by noting that PC is between &garbage_collect_1
  3314.     and &BTL_after_garbage_collect_1_stub.  So this fn must be right here.
  3315.     There's not any other way to know the address of the end of a function.
  3316.   */
  3317. void BTL_after_garbage_collect_1_stub () { abort (); }
  3318. #endif
  3319.  
  3320. /* Debugging aids.  */
  3321.  
  3322. static Lisp_Object
  3323. gc_plist_hack (CONST char *name, int value, Lisp_Object tail)
  3324. {
  3325.   /* C doesn't have local functions (or closures, or GC, or readable syntax,
  3326.      or portable numeric datatypes, or bit-vectors, or characters, or
  3327.      arrays, or exceptions, or ...) */
  3328.   return (cons3 (intern (name), make_number (value), tail));
  3329. }
  3330.  
  3331. #define HACK_O_MATIC(type, name, pl)                    \
  3332.   {                                    \
  3333.     int s = 0;                                \
  3334.     struct type##_block *x = current_##type##_block;            \
  3335.     while (x) { s += sizeof (*x) + MALLOC_OVERHEAD; x = x->prev; }    \
  3336.     (pl) = gc_plist_hack ((name), s, (pl));                \
  3337.   }
  3338.  
  3339. DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
  3340.   "Reclaim storage for Lisp objects no longer needed.\n\
  3341. Returns info on amount of space in use:\n\
  3342.  ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)\n\
  3343.   (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS\n\
  3344.   PLIST) \n\
  3345.   where `PLIST' is a list of alternating keyword/value pairs providing\n\
  3346.   more detailed information.\n\
  3347. Garbage collection happens automatically if you cons more than\n\
  3348. `gc-cons-threshold' bytes of Lisp data since previous garbage collection.")
  3349.   ()
  3350. {
  3351.   Lisp_Object pl = Qnil;
  3352.   Lisp_Object ret[6];
  3353.   int i;
  3354.  
  3355.   garbage_collect_1 ();
  3356.  
  3357.   for (i = 0; i < last_lrecord_type_index_assigned; i++)
  3358.     {
  3359.       if (lcrecord_stats[i].bytes_in_use != 0 
  3360.           || lcrecord_stats[i].bytes_freed != 0)
  3361.         {
  3362.           char buf [255];
  3363.           CONST char *name = lrecord_implementations_table[i]->name;
  3364.       int len = strlen (name);
  3365.           sprintf (buf, "%s-storage", name);
  3366.           pl = gc_plist_hack (buf, lcrecord_stats[i].bytes_in_use, pl);
  3367.       /* Okay, simple pluralization check for `symbol-value-varalias' */
  3368.       if (name[len-1] == 's')
  3369.         sprintf (buf, "%ses-freed", name);
  3370.       else
  3371.         sprintf (buf, "%ss-freed", name);
  3372.           if (lcrecord_stats[i].instances_freed != 0)
  3373.             pl = gc_plist_hack (buf, lcrecord_stats[i].instances_freed, pl);
  3374.       if (name[len-1] == 's')
  3375.         sprintf (buf, "%ses-used", name);
  3376.       else
  3377.         sprintf (buf, "%ss-used", name);
  3378.           pl = gc_plist_hack (buf, lcrecord_stats[i].instances_in_use, pl);
  3379.         }
  3380.     }
  3381.  
  3382.   HACK_O_MATIC (extent_replica, "extent-duplicate-storage", pl);
  3383.   pl = gc_plist_hack ("extent-duplicates-free", 
  3384.                       gc_count_num_extent_replica_freelist, pl);
  3385.   pl = gc_plist_hack ("extent-duplicates-used", 
  3386.                       gc_count_num_extent_replica_in_use, pl);
  3387.   HACK_O_MATIC (extent, "extent-storage", pl);
  3388.   pl = gc_plist_hack ("extents-free", gc_count_num_extent_freelist, pl);
  3389.   pl = gc_plist_hack ("extents-used", gc_count_num_extent_in_use, pl);
  3390.   HACK_O_MATIC (event, "event-storage", pl);
  3391.   pl = gc_plist_hack ("events-free", gc_count_num_event_freelist, pl);
  3392.   pl = gc_plist_hack ("events-used", gc_count_num_event_in_use, pl);
  3393.   HACK_O_MATIC (marker, "marker-storage", pl);
  3394.   pl = gc_plist_hack ("markers-free", gc_count_num_marker_freelist, pl);
  3395.   pl = gc_plist_hack ("markers-used", gc_count_num_marker_in_use, pl);
  3396. #ifdef LISP_FLOAT_TYPE
  3397.   HACK_O_MATIC (float, "float-storage", pl);
  3398.   pl = gc_plist_hack ("floats-free", gc_count_num_float_freelist, pl);
  3399.   pl = gc_plist_hack ("floats-used", gc_count_num_float_in_use, pl);
  3400. #endif /* LISP_FLOAT_TYPE */
  3401.   HACK_O_MATIC (string, "string-header-storage", pl);
  3402.   pl = gc_plist_hack ("long-strings-total-length", 
  3403.                       gc_count_string_total_size
  3404.               - gc_count_short_string_total_size, pl);
  3405.   HACK_O_MATIC (string_chars, "short-string-storage", pl);
  3406.   pl = gc_plist_hack ("short-strings-total-length",
  3407.                       gc_count_short_string_total_size, pl);
  3408.   pl = gc_plist_hack ("strings-free", gc_count_num_string_freelist, pl);
  3409.   pl = gc_plist_hack ("long-strings-used", 
  3410.                       gc_count_num_string_in_use
  3411.               - gc_count_num_short_string_in_use, pl);
  3412.   pl = gc_plist_hack ("short-strings-used", 
  3413.                       gc_count_num_short_string_in_use, pl);
  3414.  
  3415.   HACK_O_MATIC (bytecode, "bytecode-storage", pl);
  3416.   pl = gc_plist_hack ("bytecodes-free", gc_count_num_bytecode_freelist, pl);
  3417.   pl = gc_plist_hack ("bytecodes-used", gc_count_num_bytecode_in_use, pl);
  3418.  
  3419.   pl = gc_plist_hack ("vector-storage", gc_count_vector_storage, pl);
  3420.   pl = gc_plist_hack ("vectors-total-length", 
  3421.                       gc_count_vector_total_size, pl);
  3422.   pl = gc_plist_hack ("vectors-used", gc_count_num_vector_used, pl);
  3423.  
  3424.   HACK_O_MATIC (symbol, "symbol-storage", pl);
  3425.   pl = gc_plist_hack ("symbols-free", gc_count_num_symbol_freelist, pl);
  3426.   pl = gc_plist_hack ("symbols-used", gc_count_num_symbol_in_use, pl);
  3427.  
  3428.   HACK_O_MATIC (cons, "cons-storage", pl);
  3429.   pl = gc_plist_hack ("conses-free", gc_count_num_cons_freelist, pl);
  3430.   pl = gc_plist_hack ("conses-used", gc_count_num_cons_in_use, pl);
  3431.  
  3432.   /* The things we do for backwards-compatibility */
  3433.   ret[0] = Fcons (make_number (gc_count_num_cons_in_use),
  3434.                   make_number (gc_count_num_cons_freelist));
  3435.   ret[1] = Fcons (make_number (gc_count_num_symbol_in_use),
  3436.                   make_number (gc_count_num_symbol_freelist));
  3437.   ret[2] = Fcons (make_number (gc_count_num_marker_in_use),
  3438.                   make_number (gc_count_num_marker_freelist));
  3439.   ret[3] = make_number (gc_count_string_total_size);
  3440.   ret[4] = make_number (gc_count_vector_total_size);
  3441.   ret[5] = pl;
  3442.   return (Flist (6, ret));
  3443. }
  3444. #undef HACK_O_MATIC
  3445.  
  3446. DEFUN ("consing-since-gc", Fconsing_since_gc, Sconsing_since_gc, 0, 0, "",
  3447.   "Return the number of bytes consed since the last garbage collection.")
  3448.   ()
  3449. {
  3450.   return (make_number (consing_since_gc));
  3451. }
  3452.   
  3453. DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, "",
  3454.   "Return the address of the last byte Emacs has allocated, divided by 1024.\n\
  3455. This may be helpful in debugging Emacs's memory usage.\n\
  3456. The value is divided by 1024 to make sure it will fit in a lisp integer.")
  3457.   ()
  3458. {
  3459.   return (make_number ((LISP_WORD_TYPE) sbrk (0) / 1024));
  3460. }
  3461.  
  3462.  
  3463. /* Initialization */
  3464. void
  3465. init_alloc_once_early (void)
  3466. {
  3467.   int iii;
  3468.  
  3469. #ifdef PURESTAT
  3470.   for (iii = 0; iii < countof (purestats); iii++)
  3471.     {
  3472.       if (! purestats[iii]) continue;
  3473.       purestats[iii]->nobjects = 0;
  3474.       purestats[iii]->nbytes = 0;
  3475.     }
  3476.   purecopying_for_bytecode = 0;
  3477. #endif
  3478.   
  3479.   last_lrecord_type_index_assigned = -1;
  3480.   for (iii = 0; iii < countof (lrecord_implementations_table); iii++)
  3481.     {
  3482.       lrecord_implementations_table[iii] = 0;
  3483.     }
  3484.   
  3485.   symbols_initialized = 0;
  3486.   
  3487.   gc_generation_number[0] = 0;
  3488.   /* purify_flag 1 is correct even if CANNOT_DUMP.
  3489.    * loadup.el will set to nil at end. */
  3490.   purify_flag = 1;
  3491.   pureptr = 0;
  3492.   pure_lossage = 0;
  3493.   breathing_space = 0;
  3494.   XSETINT (all_vectors, 0); /* Qzero may not be set yet. */
  3495.   XSETINT (Vgc_message, 0);
  3496.   all_lcrecords = 0;
  3497.   ignore_malloc_warnings = 1;
  3498.   init_string_alloc ();
  3499.   init_string_chars_alloc ();
  3500.   init_cons_alloc ();
  3501.   init_symbol_alloc ();
  3502.   init_bytecode_alloc ();
  3503. #ifdef LISP_FLOAT_TYPE
  3504.   init_float_alloc ();
  3505. #endif /* LISP_FLOAT_TYPE */
  3506. #ifndef standalone
  3507.   init_marker_alloc ();
  3508.   init_extent_alloc ();
  3509.   init_extent_replica_alloc ();
  3510.   init_event_alloc ();
  3511. #endif
  3512.   ignore_malloc_warnings = 0;
  3513.   staticidx = 0;
  3514.   consing_since_gc = 0;
  3515. #if 1
  3516.   gc_cons_threshold = 500000; /* XEmacs change */
  3517. #else
  3518.   gc_cons_threshold = 5000; /* debugging */
  3519. #endif
  3520. #ifdef VIRT_ADDR_VARIES
  3521.   malloc_sbrk_unused = 1<<22;    /* A large number */
  3522.   malloc_sbrk_used = 100000;    /* as reasonable as any number */
  3523. #endif /* VIRT_ADDR_VARIES */
  3524.   lrecord_uid_counter = 259;
  3525.   debug_string_purity = 0;
  3526.   gcprolist = 0;
  3527.  
  3528.   gc_currently_forbidden = 0;
  3529.   gc_hooks_inhibited = 0;
  3530. }
  3531.  
  3532. void
  3533. reinit_alloc (void)
  3534. {
  3535.   gcprolist = 0;
  3536. }
  3537.  
  3538. void
  3539. syms_of_alloc (void)
  3540. {
  3541.   defsymbol (&Qpre_gc_hook, "pre-gc-hook");
  3542.   defsymbol (&Qpost_gc_hook, "post-gc-hook");
  3543.   defsymbol (&Qgarbage_collecting, "garbage-collecting");
  3544.  
  3545.   defsubr (&Scons);
  3546.   defsubr (&Slist);
  3547.   defsubr (&Svector);
  3548.   defsubr (&Smake_byte_code);
  3549.   defsubr (&Smake_list);
  3550.   defsubr (&Smake_vector);
  3551.   defsubr (&Smake_string);
  3552.   defsubr (&Smake_symbol);
  3553.   defsubr (&Smake_marker);
  3554.   defsubr (&Spurecopy);
  3555.   defsubr (&Sgarbage_collect);
  3556.   defsubr (&Smemory_limit);
  3557.   defsubr (&Sconsing_since_gc);
  3558. }
  3559.  
  3560. void
  3561. vars_of_alloc (void)
  3562. {
  3563.   DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold,
  3564.     "*Number of bytes of consing between garbage collections.\n\
  3565. Garbage collection can happen automatically once this many bytes have been\n\
  3566. allocated since the last garbage collection.  All data types count.\n\n\
  3567. Garbage collection happens automatically when `eval' or `funcall' are\n\
  3568. called.  (Note that `funcall' is called implicitly as part of evaluation.)\n\
  3569. By binding this temporarily to a large number, you can effectively\n\
  3570. prevent garbage collection during a part of the program.");
  3571.  
  3572.   DEFVAR_INT ("pure-bytes-used", &pureptr,
  3573.     "Number of bytes of sharable Lisp data allocated so far.");
  3574.  
  3575. #if 0
  3576.   DEFVAR_INT ("data-bytes-used", &malloc_sbrk_used,
  3577.     "Number of bytes of unshared memory allocated in this session.");
  3578.  
  3579.   DEFVAR_INT ("data-bytes-free", &malloc_sbrk_unused,
  3580.     "Number of bytes of unshared memory remaining available in this session.");
  3581. #endif
  3582.  
  3583.   DEFVAR_BOOL ("purify-flag", &purify_flag,
  3584.     "Non-nil means loading Lisp code in order to dump an executable.\n\
  3585. This means that certain objects should be allocated in shared (pure) space.");
  3586.  
  3587.   DEFVAR_LISP ("pre-gc-hook", &Vpre_gc_hook,
  3588.        "Function or functions to be run just before each garbage collection.\n\
  3589. Interrupts, garbage collection, and errors are inhibited while this hook\n\
  3590. runs, so be extremely careful in what you add here.  In particular, avoid\n\
  3591. consing, and do not interact with the user.");
  3592.   Vpre_gc_hook = Qnil;
  3593.  
  3594.   DEFVAR_LISP ("post-gc-hook", &Vpost_gc_hook,
  3595.        "Function or functions to be run just after each garbage collection.\n\
  3596. Interrupts, garbage collection, and errors are inhibited while this hook\n\
  3597. runs, so be extremely careful in what you add here.  In particular, avoid\n\
  3598. consing, and do not interact with the user.");
  3599.   Vpost_gc_hook = Qnil;
  3600.  
  3601.   DEFVAR_LISP ("gc-message", &Vgc_message,
  3602.     "What to display to indicate that a garbage collection is in progress.\n\
  3603. If this is a string, it is printed in the echo area.\n\
  3604. If this is a cursor object, the mouse pointer of the selected frame is\n\
  3605. changed to that cursor for the duration of the garbage collection.\n\
  3606. See the variable `x-gc-pointer-shape', which is used to control this.");
  3607.   Vgc_message = make_pure_string ((Bufbyte *) gc_default_message,
  3608.                   countof (gc_default_message) - 1, 1);
  3609. }
  3610.