XEmacs 21.2.42 "Poseidon".
[chise/xemacs-chise.git.1] / src / alloc.c
1 /* Storage allocation and gc for XEmacs Lisp interpreter.
2    Copyright (C) 1985-1998 Free Software Foundation, Inc.
3    Copyright (C) 1995 Sun Microsystems, Inc.
4    Copyright (C) 1995, 1996 Ben Wing.
5
6 This file is part of XEmacs.
7
8 XEmacs is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with XEmacs; see the file COPYING.  If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 /* Synched up with: FSF 19.28, Mule 2.0.  Substantially different from
24    FSF. */
25
26 /* Authorship:
27
28    FSF: Original version; a long time ago.
29    Mly: Significantly rewritten to use new 3-bit tags and
30         nicely abstracted object definitions, for 19.8.
31    JWZ: Improved code to keep track of purespace usage and
32         issue nice purespace and GC stats.
33    Ben Wing: Cleaned up frob-block lrecord code, added error-checking
34         and various changes for Mule, for 19.12.
35         Added bit vectors for 19.13.
36         Added lcrecord lists for 19.14.
37    slb: Lots of work on the purification and dump time code.
38         Synched Doug Lea malloc support from Emacs 20.2.
39    og:  Killed the purespace.  Portable dumper (moved to dumper.c)
40 */
41
42 #include <config.h>
43 #include "lisp.h"
44
45 #include "backtrace.h"
46 #include "buffer.h"
47 #include "bytecode.h"
48 #include "chartab.h"
49 #include "device.h"
50 #include "elhash.h"
51 #include "events.h"
52 #include "extents.h"
53 #include "frame.h"
54 #include "glyphs.h"
55 #include "opaque.h"
56 #include "redisplay.h"
57 #include "specifier.h"
58 #include "sysfile.h"
59 #include "sysdep.h"
60 #include "window.h"
61 #include "console-stream.h"
62
63 #ifdef DOUG_LEA_MALLOC
64 #include <malloc.h>
65 #endif
66
67 #ifdef PDUMP
68 #include "dumper.h"
69 #endif
70
71 EXFUN (Fgarbage_collect, 0);
72
73 #if 0 /* this is _way_ too slow to be part of the standard debug options */
74 #if defined(DEBUG_XEMACS) && defined(MULE)
75 #define VERIFY_STRING_CHARS_INTEGRITY
76 #endif
77 #endif
78
79 /* Define this to use malloc/free with no freelist for all datatypes,
80    the hope being that some debugging tools may help detect
81    freed memory references */
82 #ifdef USE_DEBUG_MALLOC /* Taking the above comment at face value -slb */
83 #include <dmalloc.h>
84 #define ALLOC_NO_POOLS
85 #endif
86
87 #ifdef DEBUG_XEMACS
88 static int debug_allocation;
89 static int debug_allocation_backtrace_length;
90 #endif
91
92 /* Number of bytes of consing done since the last gc */
93 EMACS_INT consing_since_gc;
94 #define INCREMENT_CONS_COUNTER_1(size) (consing_since_gc += (size))
95
96 #define debug_allocation_backtrace()                            \
97 do {                                                            \
98   if (debug_allocation_backtrace_length > 0)                    \
99     debug_short_backtrace (debug_allocation_backtrace_length);  \
100 } while (0)
101
102 #ifdef DEBUG_XEMACS
103 #define INCREMENT_CONS_COUNTER(foosize, type)                   \
104   do {                                                          \
105     if (debug_allocation)                                       \
106       {                                                         \
107         stderr_out ("allocating %s (size %ld)\n", type, (long)foosize); \
108         debug_allocation_backtrace ();                          \
109       }                                                         \
110     INCREMENT_CONS_COUNTER_1 (foosize);                         \
111   } while (0)
112 #define NOSEEUM_INCREMENT_CONS_COUNTER(foosize, type)           \
113   do {                                                          \
114     if (debug_allocation > 1)                                   \
115       {                                                         \
116         stderr_out ("allocating noseeum %s (size %ld)\n", type, (long)foosize); \
117         debug_allocation_backtrace ();                          \
118       }                                                         \
119     INCREMENT_CONS_COUNTER_1 (foosize);                         \
120   } while (0)
121 #else
122 #define INCREMENT_CONS_COUNTER(size, type) INCREMENT_CONS_COUNTER_1 (size)
123 #define NOSEEUM_INCREMENT_CONS_COUNTER(size, type) \
124   INCREMENT_CONS_COUNTER_1 (size)
125 #endif
126
127 #define DECREMENT_CONS_COUNTER(size) do {       \
128   consing_since_gc -= (size);                   \
129   if (consing_since_gc < 0)                     \
130     consing_since_gc = 0;                       \
131 } while (0)
132
133 /* Number of bytes of consing since gc before another gc should be done. */
134 EMACS_INT gc_cons_threshold;
135
136 /* Nonzero during gc */
137 int gc_in_progress;
138
139 /* Number of times GC has happened at this level or below.
140  * Level 0 is most volatile, contrary to usual convention.
141  *  (Of course, there's only one level at present) */
142 EMACS_INT gc_generation_number[1];
143
144 /* This is just for use by the printer, to allow things to print uniquely */
145 static int lrecord_uid_counter;
146
147 /* Nonzero when calling certain hooks or doing other things where
148    a GC would be bad */
149 int gc_currently_forbidden;
150
151 /* Hooks. */
152 Lisp_Object Vpre_gc_hook, Qpre_gc_hook;
153 Lisp_Object Vpost_gc_hook, Qpost_gc_hook;
154
155 /* "Garbage collecting" */
156 Lisp_Object Vgc_message;
157 Lisp_Object Vgc_pointer_glyph;
158 static const char gc_default_message[] = "Garbage collecting";
159 Lisp_Object Qgarbage_collecting;
160
161 /* Non-zero means we're in the process of doing the dump */
162 int purify_flag;
163
164 #ifdef ERROR_CHECK_TYPECHECK
165
166 Error_behavior ERROR_ME, ERROR_ME_NOT, ERROR_ME_WARN;
167
168 #endif
169
170 int
171 c_readonly (Lisp_Object obj)
172 {
173   return POINTER_TYPE_P (XTYPE (obj)) && C_READONLY (obj);
174 }
175
176 int
177 lisp_readonly (Lisp_Object obj)
178 {
179   return POINTER_TYPE_P (XTYPE (obj)) && LISP_READONLY (obj);
180 }
181
182 \f
183 /* Maximum amount of C stack to save when a GC happens.  */
184
185 #ifndef MAX_SAVE_STACK
186 #define MAX_SAVE_STACK 0 /* 16000 */
187 #endif
188
189 /* Non-zero means ignore malloc warnings.  Set during initialization.  */
190 int ignore_malloc_warnings;
191
192 \f
193 static void *breathing_space;
194
195 void
196 release_breathing_space (void)
197 {
198   if (breathing_space)
199     {
200       void *tmp = breathing_space;
201       breathing_space = 0;
202       xfree (tmp);
203     }
204 }
205
206 /* malloc calls this if it finds we are near exhausting storage */
207 void
208 malloc_warning (const char *str)
209 {
210   if (ignore_malloc_warnings)
211     return;
212
213   warn_when_safe
214     (Qmemory, Qcritical,
215      "%s\n"
216      "Killing some buffers may delay running out of memory.\n"
217      "However, certainly by the time you receive the 95%% warning,\n"
218      "you should clean up, kill this Emacs, and start a new one.",
219      str);
220 }
221
222 /* Called if malloc returns zero */
223 DOESNT_RETURN
224 memory_full (void)
225 {
226   /* Force a GC next time eval is called.
227      It's better to loop garbage-collecting (we might reclaim enough
228      to win) than to loop beeping and barfing "Memory exhausted"
229    */
230   consing_since_gc = gc_cons_threshold + 1;
231   release_breathing_space ();
232
233   /* Flush some histories which might conceivably contain garbalogical
234      inhibitors.  */
235   if (!NILP (Fboundp (Qvalues)))
236     Fset (Qvalues, Qnil);
237   Vcommand_history = Qnil;
238
239   error ("Memory exhausted");
240 }
241
242 /* like malloc and realloc but check for no memory left, and block input. */
243
244 #undef xmalloc
245 void *
246 xmalloc (size_t size)
247 {
248   void *val = malloc (size);
249
250   if (!val && (size != 0)) memory_full ();
251   return val;
252 }
253
254 #undef xcalloc
255 static void *
256 xcalloc (size_t nelem, size_t elsize)
257 {
258   void *val = calloc (nelem, elsize);
259
260   if (!val && (nelem != 0)) memory_full ();
261   return val;
262 }
263
264 void *
265 xmalloc_and_zero (size_t size)
266 {
267   return xcalloc (size, sizeof (char));
268 }
269
270 #undef xrealloc
271 void *
272 xrealloc (void *block, size_t size)
273 {
274   /* We must call malloc explicitly when BLOCK is 0, since some
275      reallocs don't do this.  */
276   void *val = block ? realloc (block, size) : malloc (size);
277
278   if (!val && (size != 0)) memory_full ();
279   return val;
280 }
281
282 void
283 #ifdef ERROR_CHECK_MALLOC
284 xfree_1 (void *block)
285 #else
286 xfree (void *block)
287 #endif
288 {
289 #ifdef ERROR_CHECK_MALLOC
290   /* Unbelievably, calling free() on 0xDEADBEEF doesn't cause an
291      error until much later on for many system mallocs, such as
292      the one that comes with Solaris 2.3.  FMH!! */
293   assert (block != (void *) 0xDEADBEEF);
294   assert (block);
295 #endif /* ERROR_CHECK_MALLOC */
296   free (block);
297 }
298
299 #ifdef ERROR_CHECK_GC
300
301 #if SIZEOF_INT == 4
302 typedef unsigned int four_byte_t;
303 #elif SIZEOF_LONG == 4
304 typedef unsigned long four_byte_t;
305 #elif SIZEOF_SHORT == 4
306 typedef unsigned short four_byte_t;
307 #else
308 What kind of strange-ass system are we running on?
309 #endif
310
311 static void
312 deadbeef_memory (void *ptr, size_t size)
313 {
314   four_byte_t *ptr4 = (four_byte_t *) ptr;
315   size_t beefs = size >> 2;
316
317   /* In practice, size will always be a multiple of four.  */
318   while (beefs--)
319     (*ptr4++) = 0xDEADBEEF;
320 }
321
322 #else /* !ERROR_CHECK_GC */
323
324
325 #define deadbeef_memory(ptr, size)
326
327 #endif /* !ERROR_CHECK_GC */
328
329 #undef xstrdup
330 char *
331 xstrdup (const char *str)
332 {
333   int len = strlen (str) + 1;   /* for stupid terminating 0 */
334
335   void *val = xmalloc (len);
336   if (val == 0) return 0;
337   return (char *) memcpy (val, str, len);
338 }
339
340 #ifdef NEED_STRDUP
341 char *
342 strdup (const char *s)
343 {
344   return xstrdup (s);
345 }
346 #endif /* NEED_STRDUP */
347
348 \f
349 static void *
350 allocate_lisp_storage (size_t size)
351 {
352   return xmalloc (size);
353 }
354
355
356 /* lcrecords are chained together through their "next" field.
357    After doing the mark phase, GC will walk this linked list
358    and free any lcrecord which hasn't been marked. */
359 static struct lcrecord_header *all_lcrecords;
360
361 void *
362 alloc_lcrecord (size_t size, const struct lrecord_implementation *implementation)
363 {
364   struct lcrecord_header *lcheader;
365
366   type_checking_assert
367     ((implementation->static_size == 0 ?
368       implementation->size_in_bytes_method != NULL :
369       implementation->static_size == size)
370      &&
371      (! implementation->basic_p)
372      &&
373      (! (implementation->hash == NULL && implementation->equal != NULL)));
374
375   lcheader = (struct lcrecord_header *) allocate_lisp_storage (size);
376   set_lheader_implementation (&lcheader->lheader, implementation);
377   lcheader->next = all_lcrecords;
378 #if 1                           /* mly prefers to see small ID numbers */
379   lcheader->uid = lrecord_uid_counter++;
380 #else                           /* jwz prefers to see real addrs */
381   lcheader->uid = (int) &lcheader;
382 #endif
383   lcheader->free = 0;
384   all_lcrecords = lcheader;
385   INCREMENT_CONS_COUNTER (size, implementation->name);
386   return lcheader;
387 }
388
389 #if 0 /* Presently unused */
390 /* Very, very poor man's EGC?
391  * This may be slow and thrash pages all over the place.
392  *  Only call it if you really feel you must (and if the
393  *  lrecord was fairly recently allocated).
394  * Otherwise, just let the GC do its job -- that's what it's there for
395  */
396 void
397 free_lcrecord (struct lcrecord_header *lcrecord)
398 {
399   if (all_lcrecords == lcrecord)
400     {
401       all_lcrecords = lcrecord->next;
402     }
403   else
404     {
405       struct lrecord_header *header = all_lcrecords;
406       for (;;)
407         {
408           struct lrecord_header *next = header->next;
409           if (next == lcrecord)
410             {
411               header->next = lrecord->next;
412               break;
413             }
414           else if (next == 0)
415             abort ();
416           else
417             header = next;
418         }
419     }
420   if (lrecord->implementation->finalizer)
421     lrecord->implementation->finalizer (lrecord, 0);
422   xfree (lrecord);
423   return;
424 }
425 #endif /* Unused */
426
427
428 static void
429 disksave_object_finalization_1 (void)
430 {
431   struct lcrecord_header *header;
432
433   for (header = all_lcrecords; header; header = header->next)
434     {
435       if (LHEADER_IMPLEMENTATION (&header->lheader)->finalizer &&
436           !header->free)
437         LHEADER_IMPLEMENTATION (&header->lheader)->finalizer (header, 1);
438     }
439 }
440
441 \f
442 /************************************************************************/
443 /*                        Debugger support                              */
444 /************************************************************************/
445 /* Give gdb/dbx enough information to decode Lisp Objects.  We make
446    sure certain symbols are always defined, so gdb doesn't complain
447    about expressions in src/.gdbinit.  See src/.gdbinit or src/.dbxrc
448    to see how this is used.  */
449
450 const EMACS_UINT dbg_valmask = ((1UL << VALBITS) - 1) << GCBITS;
451 const EMACS_UINT dbg_typemask = (1UL << GCTYPEBITS) - 1;
452
453 #ifdef USE_UNION_TYPE
454 const unsigned char dbg_USE_UNION_TYPE = 1;
455 #else
456 const unsigned char dbg_USE_UNION_TYPE = 0;
457 #endif
458
459 const unsigned char dbg_valbits = VALBITS;
460 const unsigned char dbg_gctypebits = GCTYPEBITS;
461
462 /* Macros turned into functions for ease of debugging.
463    Debuggers don't know about macros! */
464 int dbg_eq (Lisp_Object obj1, Lisp_Object obj2);
465 int
466 dbg_eq (Lisp_Object obj1, Lisp_Object obj2)
467 {
468   return EQ (obj1, obj2);
469 }
470
471 \f
472 /************************************************************************/
473 /*                        Fixed-size type macros                        */
474 /************************************************************************/
475
476 /* For fixed-size types that are commonly used, we malloc() large blocks
477    of memory at a time and subdivide them into chunks of the correct
478    size for an object of that type.  This is more efficient than
479    malloc()ing each object separately because we save on malloc() time
480    and overhead due to the fewer number of malloc()ed blocks, and
481    also because we don't need any extra pointers within each object
482    to keep them threaded together for GC purposes.  For less common
483    (and frequently large-size) types, we use lcrecords, which are
484    malloc()ed individually and chained together through a pointer
485    in the lcrecord header.  lcrecords do not need to be fixed-size
486    (i.e. two objects of the same type need not have the same size;
487    however, the size of a particular object cannot vary dynamically).
488    It is also much easier to create a new lcrecord type because no
489    additional code needs to be added to alloc.c.  Finally, lcrecords
490    may be more efficient when there are only a small number of them.
491
492    The types that are stored in these large blocks (or "frob blocks")
493    are cons, float, compiled-function, symbol, marker, extent, event,
494    and string.
495
496    Note that strings are special in that they are actually stored in
497    two parts: a structure containing information about the string, and
498    the actual data associated with the string.  The former structure
499    (a struct Lisp_String) is a fixed-size structure and is managed the
500    same way as all the other such types.  This structure contains a
501    pointer to the actual string data, which is stored in structures of
502    type struct string_chars_block.  Each string_chars_block consists
503    of a pointer to a struct Lisp_String, followed by the data for that
504    string, followed by another pointer to a Lisp_String, followed by
505    the data for that string, etc.  At GC time, the data in these
506    blocks is compacted by searching sequentially through all the
507    blocks and compressing out any holes created by unmarked strings.
508    Strings that are more than a certain size (bigger than the size of
509    a string_chars_block, although something like half as big might
510    make more sense) are malloc()ed separately and not stored in
511    string_chars_blocks.  Furthermore, no one string stretches across
512    two string_chars_blocks.
513
514    Vectors are each malloc()ed separately, similar to lcrecords.
515
516    In the following discussion, we use conses, but it applies equally
517    well to the other fixed-size types.
518
519    We store cons cells inside of cons_blocks, allocating a new
520    cons_block with malloc() whenever necessary.  Cons cells reclaimed
521    by GC are put on a free list to be reallocated before allocating
522    any new cons cells from the latest cons_block.  Each cons_block is
523    just under 2^n - MALLOC_OVERHEAD bytes long, since malloc (at least
524    the versions in malloc.c and gmalloc.c) really allocates in units
525    of powers of two and uses 4 bytes for its own overhead.
526
527    What GC actually does is to search through all the cons_blocks,
528    from the most recently allocated to the oldest, and put all
529    cons cells that are not marked (whether or not they're already
530    free) on a cons_free_list.  The cons_free_list is a stack, and
531    so the cons cells in the oldest-allocated cons_block end up
532    at the head of the stack and are the first to be reallocated.
533    If any cons_block is entirely free, it is freed with free()
534    and its cons cells removed from the cons_free_list.  Because
535    the cons_free_list ends up basically in memory order, we have
536    a high locality of reference (assuming a reasonable turnover
537    of allocating and freeing) and have a reasonable probability
538    of entirely freeing up cons_blocks that have been more recently
539    allocated.  This stage is called the "sweep stage" of GC, and
540    is executed after the "mark stage", which involves starting
541    from all places that are known to point to in-use Lisp objects
542    (e.g. the obarray, where are all symbols are stored; the
543    current catches and condition-cases; the backtrace list of
544    currently executing functions; the gcpro list; etc.) and
545    recursively marking all objects that are accessible.
546
547    At the beginning of the sweep stage, the conses in the cons blocks
548    are in one of three states: in use and marked, in use but not
549    marked, and not in use (already freed).  Any conses that are marked
550    have been marked in the mark stage just executed, because as part
551    of the sweep stage we unmark any marked objects.  The way we tell
552    whether or not a cons cell is in use is through the LRECORD_FREE_P
553    macro.  This uses a special lrecord type `lrecord_type_free',
554    which is never associated with any valid object.
555
556    Conses on the free_cons_list are threaded through a pointer stored
557    in the conses themselves.  Because the cons is still in a
558    cons_block and needs to remain marked as not in use for the next
559    time that GC happens, we need room to store both the "free"
560    indicator and the chaining pointer.  So this pointer is stored
561    after the lrecord header (actually where C places a pointer after
562    the lrecord header; they are not necessarily contiguous).  This
563    implies that all fixed-size types must be big enough to contain at
564    least one pointer.  This is true for all current fixed-size types,
565    with the possible exception of Lisp_Floats, for which we define the
566    meat of the struct using a union of a pointer and a double to
567    ensure adequate space for the free list chain pointer.
568
569    Some types of objects need additional "finalization" done
570    when an object is converted from in use to not in use;
571    this is the purpose of the ADDITIONAL_FREE_type macro.
572    For example, markers need to be removed from the chain
573    of markers that is kept in each buffer.  This is because
574    markers in a buffer automatically disappear if the marker
575    is no longer referenced anywhere (the same does not
576    apply to extents, however).
577
578    WARNING: Things are in an extremely bizarre state when
579    the ADDITIONAL_FREE_type macros are called, so beware!
580
581    When ERROR_CHECK_GC is defined, we do things differently so as to
582    maximize our chances of catching places where there is insufficient
583    GCPROing.  The thing we want to avoid is having an object that
584    we're using but didn't GCPRO get freed by GC and then reallocated
585    while we're in the process of using it -- this will result in
586    something seemingly unrelated getting trashed, and is extremely
587    difficult to track down.  If the object gets freed but not
588    reallocated, we can usually catch this because we set most of the
589    bytes of a freed object to 0xDEADBEEF. (The lisp object type is set
590    to the invalid type `lrecord_type_free', however, and a pointer
591    used to chain freed objects together is stored after the lrecord
592    header; we play some tricks with this pointer to make it more
593    bogus, so crashes are more likely to occur right away.)
594
595    We want freed objects to stay free as long as possible,
596    so instead of doing what we do above, we maintain the
597    free objects in a first-in first-out queue.  We also
598    don't recompute the free list each GC, unlike above;
599    this ensures that the queue ordering is preserved.
600    [This means that we are likely to have worse locality
601    of reference, and that we can never free a frob block
602    once it's allocated. (Even if we know that all cells
603    in it are free, there's no easy way to remove all those
604    cells from the free list because the objects on the
605    free list are unlikely to be in memory order.)]
606    Furthermore, we never take objects off the free list
607    unless there's a large number (usually 1000, but
608    varies depending on type) of them already on the list.
609    This way, we ensure that an object that gets freed will
610    remain free for the next 1000 (or whatever) times that
611    an object of that type is allocated.  */
612
613 #ifndef MALLOC_OVERHEAD
614 #ifdef GNU_MALLOC
615 #define MALLOC_OVERHEAD 0
616 #elif defined (rcheck)
617 #define MALLOC_OVERHEAD 20
618 #else
619 #define MALLOC_OVERHEAD 8
620 #endif
621 #endif /* MALLOC_OVERHEAD */
622
623 #if !defined(HAVE_MMAP) || defined(DOUG_LEA_MALLOC)
624 /* If we released our reserve (due to running out of memory),
625    and we have a fair amount free once again,
626    try to set aside another reserve in case we run out once more.
627
628    This is called when a relocatable block is freed in ralloc.c.  */
629 void refill_memory_reserve (void);
630 void
631 refill_memory_reserve (void)
632 {
633   if (breathing_space == 0)
634     breathing_space = (char *) malloc (4096 - MALLOC_OVERHEAD);
635 }
636 #endif
637
638 #ifdef ALLOC_NO_POOLS
639 # define TYPE_ALLOC_SIZE(type, structtype) 1
640 #else
641 # define TYPE_ALLOC_SIZE(type, structtype)                      \
642     ((2048 - MALLOC_OVERHEAD - sizeof (struct type##_block *))  \
643      / sizeof (structtype))
644 #endif /* ALLOC_NO_POOLS */
645
646 #define DECLARE_FIXED_TYPE_ALLOC(type, structtype)      \
647                                                         \
648 struct type##_block                                     \
649 {                                                       \
650   struct type##_block *prev;                            \
651   structtype block[TYPE_ALLOC_SIZE (type, structtype)]; \
652 };                                                      \
653                                                         \
654 static struct type##_block *current_##type##_block;     \
655 static int current_##type##_block_index;                \
656                                                         \
657 static Lisp_Free *type##_free_list;                     \
658 static Lisp_Free *type##_free_list_tail;                \
659                                                         \
660 static void                                             \
661 init_##type##_alloc (void)                              \
662 {                                                       \
663   current_##type##_block = 0;                           \
664   current_##type##_block_index =                        \
665     countof (current_##type##_block->block);            \
666   type##_free_list = 0;                                 \
667   type##_free_list_tail = 0;                            \
668 }                                                       \
669                                                         \
670 static int gc_count_num_##type##_in_use;                \
671 static int gc_count_num_##type##_freelist
672
673 #define ALLOCATE_FIXED_TYPE_FROM_BLOCK(type, result) do {               \
674   if (current_##type##_block_index                                      \
675       == countof (current_##type##_block->block))                       \
676     {                                                                   \
677       struct type##_block *AFTFB_new = (struct type##_block *)          \
678         allocate_lisp_storage (sizeof (struct type##_block));           \
679       AFTFB_new->prev = current_##type##_block;                         \
680       current_##type##_block = AFTFB_new;                               \
681       current_##type##_block_index = 0;                                 \
682     }                                                                   \
683   (result) =                                                            \
684     &(current_##type##_block->block[current_##type##_block_index++]);   \
685 } while (0)
686
687 /* Allocate an instance of a type that is stored in blocks.
688    TYPE is the "name" of the type, STRUCTTYPE is the corresponding
689    structure type. */
690
691 #ifdef ERROR_CHECK_GC
692
693 /* Note: if you get crashes in this function, suspect incorrect calls
694    to free_cons() and friends.  This happened once because the cons
695    cell was not GC-protected and was getting collected before
696    free_cons() was called. */
697
698 #define ALLOCATE_FIXED_TYPE_1(type, structtype, result) do {    \
699   if (gc_count_num_##type##_freelist >                          \
700       MINIMUM_ALLOWED_FIXED_TYPE_CELLS_##type)                  \
701     {                                                           \
702       result = (structtype *) type##_free_list;                 \
703       /* Before actually using the chain pointer,               \
704          we complement all its bits; see FREE_FIXED_TYPE(). */  \
705       type##_free_list = (Lisp_Free *)                          \
706         (~ (EMACS_UINT) (type##_free_list->chain));             \
707       gc_count_num_##type##_freelist--;                         \
708     }                                                           \
709   else                                                          \
710     ALLOCATE_FIXED_TYPE_FROM_BLOCK (type, result);              \
711   MARK_LRECORD_AS_NOT_FREE (result);                            \
712 } while (0)
713
714 #else /* !ERROR_CHECK_GC */
715
716 #define ALLOCATE_FIXED_TYPE_1(type, structtype, result) do {    \
717   if (type##_free_list)                                         \
718     {                                                           \
719       result = (structtype *) type##_free_list;                 \
720       type##_free_list = type##_free_list->chain;               \
721     }                                                           \
722   else                                                          \
723     ALLOCATE_FIXED_TYPE_FROM_BLOCK (type, result);              \
724   MARK_LRECORD_AS_NOT_FREE (result);                            \
725 } while (0)
726
727 #endif /* !ERROR_CHECK_GC */
728
729
730 #define ALLOCATE_FIXED_TYPE(type, structtype, result)   \
731 do                                                      \
732 {                                                       \
733   ALLOCATE_FIXED_TYPE_1 (type, structtype, result);     \
734   INCREMENT_CONS_COUNTER (sizeof (structtype), #type);  \
735 } while (0)
736
737 #define NOSEEUM_ALLOCATE_FIXED_TYPE(type, structtype, result)   \
738 do                                                              \
739 {                                                               \
740   ALLOCATE_FIXED_TYPE_1 (type, structtype, result);             \
741   NOSEEUM_INCREMENT_CONS_COUNTER (sizeof (structtype), #type);  \
742 } while (0)
743
744
745 /* Lisp_Free is the type to represent a free list member inside a frob
746    block of any lisp object type.  */
747 typedef struct Lisp_Free
748 {
749   struct lrecord_header lheader;
750   struct Lisp_Free *chain;
751 } Lisp_Free;
752
753 #define LRECORD_FREE_P(ptr) \
754 ((ptr)->lheader.type == lrecord_type_free)
755
756 #define MARK_LRECORD_AS_FREE(ptr) \
757 ((void) ((ptr)->lheader.type = lrecord_type_free))
758
759 #ifdef ERROR_CHECK_GC
760 #define MARK_LRECORD_AS_NOT_FREE(ptr) \
761 ((void) ((ptr)->lheader.type = lrecord_type_undefined))
762 #else
763 #define MARK_LRECORD_AS_NOT_FREE(ptr) DO_NOTHING
764 #endif
765
766 #ifdef ERROR_CHECK_GC
767
768 #define PUT_FIXED_TYPE_ON_FREE_LIST(type, structtype, ptr) do { \
769   if (type##_free_list_tail)                                    \
770     {                                                           \
771       /* When we store the chain pointer, we complement all     \
772          its bits; this should significantly increase its       \
773          bogosity in case someone tries to use the value, and   \
774          should make us crash faster if someone overwrites the  \
775          pointer because when it gets un-complemented in        \
776          ALLOCATED_FIXED_TYPE(), the resulting pointer will be  \
777          extremely bogus. */                                    \
778       type##_free_list_tail->chain =                            \
779         (Lisp_Free *) ~ (EMACS_UINT) (ptr);                     \
780     }                                                           \
781   else                                                          \
782     type##_free_list = (Lisp_Free *) (ptr);                     \
783   type##_free_list_tail = (Lisp_Free *) (ptr);                  \
784 } while (0)
785
786 #else /* !ERROR_CHECK_GC */
787
788 #define PUT_FIXED_TYPE_ON_FREE_LIST(type, structtype, ptr) do { \
789   ((Lisp_Free *) (ptr))->chain = type##_free_list;              \
790   type##_free_list = (Lisp_Free *) (ptr);                       \
791 } while (0)                                                     \
792
793 #endif /* !ERROR_CHECK_GC */
794
795 /* TYPE and STRUCTTYPE are the same as in ALLOCATE_FIXED_TYPE(). */
796
797 #define FREE_FIXED_TYPE(type, structtype, ptr) do {             \
798   structtype *FFT_ptr = (ptr);                                  \
799   ADDITIONAL_FREE_##type (FFT_ptr);                             \
800   deadbeef_memory (FFT_ptr, sizeof (structtype));               \
801   PUT_FIXED_TYPE_ON_FREE_LIST (type, structtype, FFT_ptr);      \
802   MARK_LRECORD_AS_FREE (FFT_ptr);                               \
803 } while (0)
804
805 /* Like FREE_FIXED_TYPE() but used when we are explicitly
806    freeing a structure through free_cons(), free_marker(), etc.
807    rather than through the normal process of sweeping.
808    We attempt to undo the changes made to the allocation counters
809    as a result of this structure being allocated.  This is not
810    completely necessary but helps keep things saner: e.g. this way,
811    repeatedly allocating and freeing a cons will not result in
812    the consing-since-gc counter advancing, which would cause a GC
813    and somewhat defeat the purpose of explicitly freeing. */
814
815 #define FREE_FIXED_TYPE_WHEN_NOT_IN_GC(type, structtype, ptr)   \
816 do { FREE_FIXED_TYPE (type, structtype, ptr);                   \
817      DECREMENT_CONS_COUNTER (sizeof (structtype));              \
818      gc_count_num_##type##_freelist++;                          \
819    } while (0)
820
821
822 \f
823 /************************************************************************/
824 /*                         Cons allocation                              */
825 /************************************************************************/
826
827 DECLARE_FIXED_TYPE_ALLOC (cons, Lisp_Cons);
828 /* conses are used and freed so often that we set this really high */
829 /* #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_cons 20000 */
830 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_cons 2000
831
832 static Lisp_Object
833 mark_cons (Lisp_Object obj)
834 {
835   if (NILP (XCDR (obj)))
836     return XCAR (obj);
837
838   mark_object (XCAR (obj));
839   return XCDR (obj);
840 }
841
842 static int
843 cons_equal (Lisp_Object ob1, Lisp_Object ob2, int depth)
844 {
845   depth++;
846   while (internal_equal (XCAR (ob1), XCAR (ob2), depth))
847     {
848       ob1 = XCDR (ob1);
849       ob2 = XCDR (ob2);
850       if (! CONSP (ob1) || ! CONSP (ob2))
851         return internal_equal (ob1, ob2, depth);
852     }
853   return 0;
854 }
855
856 static const struct lrecord_description cons_description[] = {
857   { XD_LISP_OBJECT, offsetof (Lisp_Cons, car) },
858   { XD_LISP_OBJECT, offsetof (Lisp_Cons, cdr) },
859   { XD_END }
860 };
861
862 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("cons", cons,
863                                      mark_cons, print_cons, 0,
864                                      cons_equal,
865                                      /*
866                                       * No `hash' method needed.
867                                       * internal_hash knows how to
868                                       * handle conses.
869                                       */
870                                      0,
871                                      cons_description,
872                                      Lisp_Cons);
873
874 DEFUN ("cons", Fcons, 2, 2, 0, /*
875 Create a new cons, give it CAR and CDR as components, and return it.
876 */
877        (car, cdr))
878 {
879   /* This cannot GC. */
880   Lisp_Object val;
881   Lisp_Cons *c;
882
883   ALLOCATE_FIXED_TYPE (cons, Lisp_Cons, c);
884   set_lheader_implementation (&c->lheader, &lrecord_cons);
885   XSETCONS (val, c);
886   c->car = car;
887   c->cdr = cdr;
888   return val;
889 }
890
891 /* This is identical to Fcons() but it used for conses that we're
892    going to free later, and is useful when trying to track down
893    "real" consing. */
894 Lisp_Object
895 noseeum_cons (Lisp_Object car, Lisp_Object cdr)
896 {
897   Lisp_Object val;
898   Lisp_Cons *c;
899
900   NOSEEUM_ALLOCATE_FIXED_TYPE (cons, Lisp_Cons, c);
901   set_lheader_implementation (&c->lheader, &lrecord_cons);
902   XSETCONS (val, c);
903   XCAR (val) = car;
904   XCDR (val) = cdr;
905   return val;
906 }
907
908 DEFUN ("list", Flist, 0, MANY, 0, /*
909 Return a newly created list with specified arguments as elements.
910 Any number of arguments, even zero arguments, are allowed.
911 */
912        (int nargs, Lisp_Object *args))
913 {
914   Lisp_Object val = Qnil;
915   Lisp_Object *argp = args + nargs;
916
917   while (argp > args)
918     val = Fcons (*--argp, val);
919   return val;
920 }
921
922 Lisp_Object
923 list1 (Lisp_Object obj0)
924 {
925   /* This cannot GC. */
926   return Fcons (obj0, Qnil);
927 }
928
929 Lisp_Object
930 list2 (Lisp_Object obj0, Lisp_Object obj1)
931 {
932   /* This cannot GC. */
933   return Fcons (obj0, Fcons (obj1, Qnil));
934 }
935
936 Lisp_Object
937 list3 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2)
938 {
939   /* This cannot GC. */
940   return Fcons (obj0, Fcons (obj1, Fcons (obj2, Qnil)));
941 }
942
943 Lisp_Object
944 cons3 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2)
945 {
946   /* This cannot GC. */
947   return Fcons (obj0, Fcons (obj1, obj2));
948 }
949
950 Lisp_Object
951 acons (Lisp_Object key, Lisp_Object value, Lisp_Object alist)
952 {
953   return Fcons (Fcons (key, value), alist);
954 }
955
956 Lisp_Object
957 list4 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, Lisp_Object obj3)
958 {
959   /* This cannot GC. */
960   return Fcons (obj0, Fcons (obj1, Fcons (obj2, Fcons (obj3, Qnil))));
961 }
962
963 Lisp_Object
964 list5 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, Lisp_Object obj3,
965        Lisp_Object obj4)
966 {
967   /* This cannot GC. */
968   return Fcons (obj0, Fcons (obj1, Fcons (obj2, Fcons (obj3, Fcons (obj4, Qnil)))));
969 }
970
971 Lisp_Object
972 list6 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, Lisp_Object obj3,
973        Lisp_Object obj4, Lisp_Object obj5)
974 {
975   /* This cannot GC. */
976   return Fcons (obj0, Fcons (obj1, Fcons (obj2, Fcons (obj3, Fcons (obj4, Fcons (obj5, Qnil))))));
977 }
978
979 DEFUN ("make-list", Fmake_list, 2, 2, 0, /*
980 Return a new list of length LENGTH, with each element being OBJECT.
981 */
982        (length, object))
983 {
984   CHECK_NATNUM (length);
985
986   {
987     Lisp_Object val = Qnil;
988     size_t size = XINT (length);
989
990     while (size--)
991       val = Fcons (object, val);
992     return val;
993   }
994 }
995
996 \f
997 /************************************************************************/
998 /*                        Float allocation                              */
999 /************************************************************************/
1000
1001 #ifdef LISP_FLOAT_TYPE
1002
1003 DECLARE_FIXED_TYPE_ALLOC (float, Lisp_Float);
1004 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_float 1000
1005
1006 Lisp_Object
1007 make_float (double float_value)
1008 {
1009   Lisp_Object val;
1010   Lisp_Float *f;
1011
1012   ALLOCATE_FIXED_TYPE (float, Lisp_Float, f);
1013
1014   /* Avoid dump-time `uninitialized memory read' purify warnings. */
1015   if (sizeof (struct lrecord_header) + sizeof (double) != sizeof (*f))
1016     xzero (*f);
1017
1018   set_lheader_implementation (&f->lheader, &lrecord_float);
1019   float_data (f) = float_value;
1020   XSETFLOAT (val, f);
1021   return val;
1022 }
1023
1024 #endif /* LISP_FLOAT_TYPE */
1025
1026 \f
1027 /************************************************************************/
1028 /*                         Vector allocation                            */
1029 /************************************************************************/
1030
1031 static Lisp_Object
1032 mark_vector (Lisp_Object obj)
1033 {
1034   Lisp_Vector *ptr = XVECTOR (obj);
1035   int len = vector_length (ptr);
1036   int i;
1037
1038   for (i = 0; i < len - 1; i++)
1039     mark_object (ptr->contents[i]);
1040   return (len > 0) ? ptr->contents[len - 1] : Qnil;
1041 }
1042
1043 static size_t
1044 size_vector (const void *lheader)
1045 {
1046   return FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Vector, contents,
1047                                        ((Lisp_Vector *) lheader)->size);
1048 }
1049
1050 static int
1051 vector_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
1052 {
1053   int len = XVECTOR_LENGTH (obj1);
1054   if (len != XVECTOR_LENGTH (obj2))
1055     return 0;
1056
1057   {
1058     Lisp_Object *ptr1 = XVECTOR_DATA (obj1);
1059     Lisp_Object *ptr2 = XVECTOR_DATA (obj2);
1060     while (len--)
1061       if (!internal_equal (*ptr1++, *ptr2++, depth + 1))
1062         return 0;
1063   }
1064   return 1;
1065 }
1066
1067 static hashcode_t
1068 vector_hash (Lisp_Object obj, int depth)
1069 {
1070   return HASH2 (XVECTOR_LENGTH (obj),
1071                 internal_array_hash (XVECTOR_DATA (obj),
1072                                      XVECTOR_LENGTH (obj),
1073                                      depth + 1));
1074 }
1075
1076 static const struct lrecord_description vector_description[] = {
1077   { XD_LONG,              offsetof (Lisp_Vector, size) },
1078   { XD_LISP_OBJECT_ARRAY, offsetof (Lisp_Vector, contents), XD_INDIRECT(0, 0) },
1079   { XD_END }
1080 };
1081
1082 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION("vector", vector,
1083                                        mark_vector, print_vector, 0,
1084                                        vector_equal,
1085                                        vector_hash,
1086                                        vector_description,
1087                                        size_vector, Lisp_Vector);
1088
1089 /* #### should allocate `small' vectors from a frob-block */
1090 static Lisp_Vector *
1091 make_vector_internal (size_t sizei)
1092 {
1093   /* no vector_next */
1094   size_t sizem = FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Vector, contents, sizei);
1095   Lisp_Vector *p = (Lisp_Vector *) alloc_lcrecord (sizem, &lrecord_vector);
1096
1097   p->size = sizei;
1098   return p;
1099 }
1100
1101 Lisp_Object
1102 make_vector (size_t length, Lisp_Object object)
1103 {
1104   Lisp_Vector *vecp = make_vector_internal (length);
1105   Lisp_Object *p = vector_data (vecp);
1106
1107   while (length--)
1108     *p++ = object;
1109
1110   {
1111     Lisp_Object vector;
1112     XSETVECTOR (vector, vecp);
1113     return vector;
1114   }
1115 }
1116
1117 DEFUN ("make-vector", Fmake_vector, 2, 2, 0, /*
1118 Return a new vector of length LENGTH, with each element being OBJECT.
1119 See also the function `vector'.
1120 */
1121        (length, object))
1122 {
1123   CONCHECK_NATNUM (length);
1124   return make_vector (XINT (length), object);
1125 }
1126
1127 DEFUN ("vector", Fvector, 0, MANY, 0, /*
1128 Return a newly created vector with specified arguments as elements.
1129 Any number of arguments, even zero arguments, are allowed.
1130 */
1131        (int nargs, Lisp_Object *args))
1132 {
1133   Lisp_Vector *vecp = make_vector_internal (nargs);
1134   Lisp_Object *p = vector_data (vecp);
1135
1136   while (nargs--)
1137     *p++ = *args++;
1138
1139   {
1140     Lisp_Object vector;
1141     XSETVECTOR (vector, vecp);
1142     return vector;
1143   }
1144 }
1145
1146 Lisp_Object
1147 vector1 (Lisp_Object obj0)
1148 {
1149   return Fvector (1, &obj0);
1150 }
1151
1152 Lisp_Object
1153 vector2 (Lisp_Object obj0, Lisp_Object obj1)
1154 {
1155   Lisp_Object args[2];
1156   args[0] = obj0;
1157   args[1] = obj1;
1158   return Fvector (2, args);
1159 }
1160
1161 Lisp_Object
1162 vector3 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2)
1163 {
1164   Lisp_Object args[3];
1165   args[0] = obj0;
1166   args[1] = obj1;
1167   args[2] = obj2;
1168   return Fvector (3, args);
1169 }
1170
1171 #if 0 /* currently unused */
1172
1173 Lisp_Object
1174 vector4 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2,
1175          Lisp_Object obj3)
1176 {
1177   Lisp_Object args[4];
1178   args[0] = obj0;
1179   args[1] = obj1;
1180   args[2] = obj2;
1181   args[3] = obj3;
1182   return Fvector (4, args);
1183 }
1184
1185 Lisp_Object
1186 vector5 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2,
1187          Lisp_Object obj3, Lisp_Object obj4)
1188 {
1189   Lisp_Object args[5];
1190   args[0] = obj0;
1191   args[1] = obj1;
1192   args[2] = obj2;
1193   args[3] = obj3;
1194   args[4] = obj4;
1195   return Fvector (5, args);
1196 }
1197
1198 Lisp_Object
1199 vector6 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2,
1200          Lisp_Object obj3, Lisp_Object obj4, Lisp_Object obj5)
1201 {
1202   Lisp_Object args[6];
1203   args[0] = obj0;
1204   args[1] = obj1;
1205   args[2] = obj2;
1206   args[3] = obj3;
1207   args[4] = obj4;
1208   args[5] = obj5;
1209   return Fvector (6, args);
1210 }
1211
1212 Lisp_Object
1213 vector7 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2,
1214          Lisp_Object obj3, Lisp_Object obj4, Lisp_Object obj5,
1215          Lisp_Object obj6)
1216 {
1217   Lisp_Object args[7];
1218   args[0] = obj0;
1219   args[1] = obj1;
1220   args[2] = obj2;
1221   args[3] = obj3;
1222   args[4] = obj4;
1223   args[5] = obj5;
1224   args[6] = obj6;
1225   return Fvector (7, args);
1226 }
1227
1228 Lisp_Object
1229 vector8 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2,
1230          Lisp_Object obj3, Lisp_Object obj4, Lisp_Object obj5,
1231          Lisp_Object obj6, Lisp_Object obj7)
1232 {
1233   Lisp_Object args[8];
1234   args[0] = obj0;
1235   args[1] = obj1;
1236   args[2] = obj2;
1237   args[3] = obj3;
1238   args[4] = obj4;
1239   args[5] = obj5;
1240   args[6] = obj6;
1241   args[7] = obj7;
1242   return Fvector (8, args);
1243 }
1244 #endif /* unused */
1245
1246 /************************************************************************/
1247 /*                       Bit Vector allocation                          */
1248 /************************************************************************/
1249
1250 static Lisp_Object all_bit_vectors;
1251
1252 /* #### should allocate `small' bit vectors from a frob-block */
1253 static Lisp_Bit_Vector *
1254 make_bit_vector_internal (size_t sizei)
1255 {
1256   size_t num_longs = BIT_VECTOR_LONG_STORAGE (sizei);
1257   size_t sizem = FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Bit_Vector, bits, num_longs);
1258   Lisp_Bit_Vector *p = (Lisp_Bit_Vector *) allocate_lisp_storage (sizem);
1259   set_lheader_implementation (&p->lheader, &lrecord_bit_vector);
1260
1261   INCREMENT_CONS_COUNTER (sizem, "bit-vector");
1262
1263   bit_vector_length (p) = sizei;
1264   bit_vector_next   (p) = all_bit_vectors;
1265   /* make sure the extra bits in the last long are 0; the calling
1266      functions might not set them. */
1267   p->bits[num_longs - 1] = 0;
1268   XSETBIT_VECTOR (all_bit_vectors, p);
1269   return p;
1270 }
1271
1272 Lisp_Object
1273 make_bit_vector (size_t length, Lisp_Object bit)
1274 {
1275   Lisp_Bit_Vector *p = make_bit_vector_internal (length);
1276   size_t num_longs = BIT_VECTOR_LONG_STORAGE (length);
1277
1278   CHECK_BIT (bit);
1279
1280   if (ZEROP (bit))
1281     memset (p->bits, 0, num_longs * sizeof (long));
1282   else
1283     {
1284       size_t bits_in_last = length & (LONGBITS_POWER_OF_2 - 1);
1285       memset (p->bits, ~0, num_longs * sizeof (long));
1286       /* But we have to make sure that the unused bits in the
1287          last long are 0, so that equal/hash is easy. */
1288       if (bits_in_last)
1289         p->bits[num_longs - 1] &= (1 << bits_in_last) - 1;
1290     }
1291
1292   {
1293     Lisp_Object bit_vector;
1294     XSETBIT_VECTOR (bit_vector, p);
1295     return bit_vector;
1296   }
1297 }
1298
1299 Lisp_Object
1300 make_bit_vector_from_byte_vector (unsigned char *bytevec, size_t length)
1301 {
1302   int i;
1303   Lisp_Bit_Vector *p = make_bit_vector_internal (length);
1304
1305   for (i = 0; i < length; i++)
1306     set_bit_vector_bit (p, i, bytevec[i]);
1307
1308   {
1309     Lisp_Object bit_vector;
1310     XSETBIT_VECTOR (bit_vector, p);
1311     return bit_vector;
1312   }
1313 }
1314
1315 DEFUN ("make-bit-vector", Fmake_bit_vector, 2, 2, 0, /*
1316 Return a new bit vector of length LENGTH. with each bit set to BIT.
1317 BIT must be one of the integers 0 or 1.  See also the function `bit-vector'.
1318 */
1319        (length, bit))
1320 {
1321   CONCHECK_NATNUM (length);
1322
1323   return make_bit_vector (XINT (length), bit);
1324 }
1325
1326 DEFUN ("bit-vector", Fbit_vector, 0, MANY, 0, /*
1327 Return a newly created bit vector with specified arguments as elements.
1328 Any number of arguments, even zero arguments, are allowed.
1329 Each argument must be one of the integers 0 or 1.
1330 */
1331        (int nargs, Lisp_Object *args))
1332 {
1333   int i;
1334   Lisp_Bit_Vector *p = make_bit_vector_internal (nargs);
1335
1336   for (i = 0; i < nargs; i++)
1337     {
1338       CHECK_BIT (args[i]);
1339       set_bit_vector_bit (p, i, !ZEROP (args[i]));
1340     }
1341
1342   {
1343     Lisp_Object bit_vector;
1344     XSETBIT_VECTOR (bit_vector, p);
1345     return bit_vector;
1346   }
1347 }
1348
1349 \f
1350 /************************************************************************/
1351 /*                   Compiled-function allocation                       */
1352 /************************************************************************/
1353
1354 DECLARE_FIXED_TYPE_ALLOC (compiled_function, Lisp_Compiled_Function);
1355 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_compiled_function 1000
1356
1357 static Lisp_Object
1358 make_compiled_function (void)
1359 {
1360   Lisp_Compiled_Function *f;
1361   Lisp_Object fun;
1362
1363   ALLOCATE_FIXED_TYPE (compiled_function, Lisp_Compiled_Function, f);
1364   set_lheader_implementation (&f->lheader, &lrecord_compiled_function);
1365
1366   f->stack_depth = 0;
1367   f->specpdl_depth = 0;
1368   f->flags.documentationp = 0;
1369   f->flags.interactivep = 0;
1370   f->flags.domainp = 0; /* I18N3 */
1371   f->instructions = Qzero;
1372   f->constants = Qzero;
1373   f->arglist = Qnil;
1374   f->doc_and_interactive = Qnil;
1375 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
1376   f->annotated = Qnil;
1377 #endif
1378   XSETCOMPILED_FUNCTION (fun, f);
1379   return fun;
1380 }
1381
1382 DEFUN ("make-byte-code", Fmake_byte_code, 4, MANY, 0, /*
1383 Return a new compiled-function object.
1384 Usage: (arglist instructions constants stack-depth
1385         &optional doc-string interactive)
1386 Note that, unlike all other emacs-lisp functions, calling this with five
1387 arguments is NOT the same as calling it with six arguments, the last of
1388 which is nil.  If the INTERACTIVE arg is specified as nil, then that means
1389 that this function was defined with `(interactive)'.  If the arg is not
1390 specified, then that means the function is not interactive.
1391 This is terrible behavior which is retained for compatibility with old
1392 `.elc' files which expect these semantics.
1393 */
1394        (int nargs, Lisp_Object *args))
1395 {
1396 /* In a non-insane world this function would have this arglist...
1397    (arglist instructions constants stack_depth &optional doc_string interactive)
1398  */
1399   Lisp_Object fun = make_compiled_function ();
1400   Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (fun);
1401
1402   Lisp_Object arglist      = args[0];
1403   Lisp_Object instructions = args[1];
1404   Lisp_Object constants    = args[2];
1405   Lisp_Object stack_depth  = args[3];
1406   Lisp_Object doc_string   = (nargs > 4) ? args[4] : Qnil;
1407   Lisp_Object interactive  = (nargs > 5) ? args[5] : Qunbound;
1408
1409   if (nargs < 4 || nargs > 6)
1410     return Fsignal (Qwrong_number_of_arguments,
1411                     list2 (intern ("make-byte-code"), make_int (nargs)));
1412
1413   /* Check for valid formal parameter list now, to allow us to use
1414      SPECBIND_FAST_UNSAFE() later in funcall_compiled_function(). */
1415   {
1416     EXTERNAL_LIST_LOOP_3 (symbol, arglist, tail)
1417       {
1418         CHECK_SYMBOL (symbol);
1419         if (EQ (symbol, Qt)   ||
1420             EQ (symbol, Qnil) ||
1421             SYMBOL_IS_KEYWORD (symbol))
1422           signal_simple_error_2
1423             ("Invalid constant symbol in formal parameter list",
1424              symbol, arglist);
1425       }
1426   }
1427   f->arglist = arglist;
1428
1429   /* `instructions' is a string or a cons (string . int) for a
1430      lazy-loaded function. */
1431   if (CONSP (instructions))
1432     {
1433       CHECK_STRING (XCAR (instructions));
1434       CHECK_INT (XCDR (instructions));
1435     }
1436   else
1437     {
1438       CHECK_STRING (instructions);
1439     }
1440   f->instructions = instructions;
1441
1442   if (!NILP (constants))
1443     CHECK_VECTOR (constants);
1444   f->constants = constants;
1445
1446   CHECK_NATNUM (stack_depth);
1447   f->stack_depth = (unsigned short) XINT (stack_depth);
1448
1449 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
1450   if (!NILP (Vcurrent_compiled_function_annotation))
1451     f->annotated = Fcopy (Vcurrent_compiled_function_annotation);
1452   else if (!NILP (Vload_file_name_internal_the_purecopy))
1453     f->annotated = Vload_file_name_internal_the_purecopy;
1454   else if (!NILP (Vload_file_name_internal))
1455     {
1456       struct gcpro gcpro1;
1457       GCPRO1 (fun);             /* don't let fun get reaped */
1458       Vload_file_name_internal_the_purecopy =
1459         Ffile_name_nondirectory (Vload_file_name_internal);
1460       f->annotated = Vload_file_name_internal_the_purecopy;
1461       UNGCPRO;
1462     }
1463 #endif /* COMPILED_FUNCTION_ANNOTATION_HACK */
1464
1465   /* doc_string may be nil, string, int, or a cons (string . int).
1466      interactive may be list or string (or unbound). */
1467   f->doc_and_interactive = Qunbound;
1468 #ifdef I18N3
1469   if ((f->flags.domainp = !NILP (Vfile_domain)) != 0)
1470     f->doc_and_interactive = Vfile_domain;
1471 #endif
1472   if ((f->flags.interactivep = !UNBOUNDP (interactive)) != 0)
1473     {
1474       f->doc_and_interactive
1475         = (UNBOUNDP (f->doc_and_interactive) ? interactive :
1476            Fcons (interactive, f->doc_and_interactive));
1477     }
1478   if ((f->flags.documentationp = !NILP (doc_string)) != 0)
1479     {
1480       f->doc_and_interactive
1481         = (UNBOUNDP (f->doc_and_interactive) ? doc_string :
1482            Fcons (doc_string, f->doc_and_interactive));
1483     }
1484   if (UNBOUNDP (f->doc_and_interactive))
1485     f->doc_and_interactive = Qnil;
1486
1487   return fun;
1488 }
1489
1490 \f
1491 /************************************************************************/
1492 /*                          Symbol allocation                           */
1493 /************************************************************************/
1494
1495 DECLARE_FIXED_TYPE_ALLOC (symbol, Lisp_Symbol);
1496 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_symbol 1000
1497
1498 DEFUN ("make-symbol", Fmake_symbol, 1, 1, 0, /*
1499 Return a newly allocated uninterned symbol whose name is NAME.
1500 Its value and function definition are void, and its property list is nil.
1501 */
1502        (name))
1503 {
1504   Lisp_Object val;
1505   Lisp_Symbol *p;
1506
1507   CHECK_STRING (name);
1508
1509   ALLOCATE_FIXED_TYPE (symbol, Lisp_Symbol, p);
1510   set_lheader_implementation (&p->lheader, &lrecord_symbol);
1511   p->name     = XSTRING (name);
1512   p->plist    = Qnil;
1513   p->value    = Qunbound;
1514   p->function = Qunbound;
1515   symbol_next (p) = 0;
1516   XSETSYMBOL (val, p);
1517   return val;
1518 }
1519
1520 \f
1521 /************************************************************************/
1522 /*                         Extent allocation                            */
1523 /************************************************************************/
1524
1525 DECLARE_FIXED_TYPE_ALLOC (extent, struct extent);
1526 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_extent 1000
1527
1528 struct extent *
1529 allocate_extent (void)
1530 {
1531   struct extent *e;
1532
1533   ALLOCATE_FIXED_TYPE (extent, struct extent, e);
1534   set_lheader_implementation (&e->lheader, &lrecord_extent);
1535   extent_object (e) = Qnil;
1536   set_extent_start (e, -1);
1537   set_extent_end (e, -1);
1538   e->plist = Qnil;
1539
1540   xzero (e->flags);
1541
1542   extent_face (e) = Qnil;
1543   e->flags.end_open = 1;  /* default is for endpoints to behave like markers */
1544   e->flags.detachable = 1;
1545
1546   return e;
1547 }
1548
1549 \f
1550 /************************************************************************/
1551 /*                         Event allocation                             */
1552 /************************************************************************/
1553
1554 DECLARE_FIXED_TYPE_ALLOC (event, Lisp_Event);
1555 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_event 1000
1556
1557 Lisp_Object
1558 allocate_event (void)
1559 {
1560   Lisp_Object val;
1561   Lisp_Event *e;
1562
1563   ALLOCATE_FIXED_TYPE (event, Lisp_Event, e);
1564   set_lheader_implementation (&e->lheader, &lrecord_event);
1565
1566   XSETEVENT (val, e);
1567   return val;
1568 }
1569
1570 \f
1571 /************************************************************************/
1572 /*                       Marker allocation                              */
1573 /************************************************************************/
1574
1575 DECLARE_FIXED_TYPE_ALLOC (marker, Lisp_Marker);
1576 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_marker 1000
1577
1578 DEFUN ("make-marker", Fmake_marker, 0, 0, 0, /*
1579 Return a new marker which does not point at any place.
1580 */
1581        ())
1582 {
1583   Lisp_Object val;
1584   Lisp_Marker *p;
1585
1586   ALLOCATE_FIXED_TYPE (marker, Lisp_Marker, p);
1587   set_lheader_implementation (&p->lheader, &lrecord_marker);
1588   p->buffer = 0;
1589   p->memind = 0;
1590   marker_next (p) = 0;
1591   marker_prev (p) = 0;
1592   p->insertion_type = 0;
1593   XSETMARKER (val, p);
1594   return val;
1595 }
1596
1597 Lisp_Object
1598 noseeum_make_marker (void)
1599 {
1600   Lisp_Object val;
1601   Lisp_Marker *p;
1602
1603   NOSEEUM_ALLOCATE_FIXED_TYPE (marker, Lisp_Marker, p);
1604   set_lheader_implementation (&p->lheader, &lrecord_marker);
1605   p->buffer = 0;
1606   p->memind = 0;
1607   marker_next (p) = 0;
1608   marker_prev (p) = 0;
1609   p->insertion_type = 0;
1610   XSETMARKER (val, p);
1611   return val;
1612 }
1613
1614 \f
1615 /************************************************************************/
1616 /*                        String allocation                             */
1617 /************************************************************************/
1618
1619 /* The data for "short" strings generally resides inside of structs of type
1620    string_chars_block. The Lisp_String structure is allocated just like any
1621    other Lisp object (except for vectors), and these are freelisted when
1622    they get garbage collected. The data for short strings get compacted,
1623    but the data for large strings do not.
1624
1625    Previously Lisp_String structures were relocated, but this caused a lot
1626    of bus-errors because the C code didn't include enough GCPRO's for
1627    strings (since EVERY REFERENCE to a short string needed to be GCPRO'd so
1628    that the reference would get relocated).
1629
1630    This new method makes things somewhat bigger, but it is MUCH safer.  */
1631
1632 DECLARE_FIXED_TYPE_ALLOC (string, Lisp_String);
1633 /* strings are used and freed quite often */
1634 /* #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_string 10000 */
1635 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_string 1000
1636
1637 static Lisp_Object
1638 mark_string (Lisp_Object obj)
1639 {
1640   Lisp_String *ptr = XSTRING (obj);
1641
1642   if (CONSP (ptr->plist) && EXTENT_INFOP (XCAR (ptr->plist)))
1643     flush_cached_extent_info (XCAR (ptr->plist));
1644   return ptr->plist;
1645 }
1646
1647 static int
1648 string_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
1649 {
1650   Bytecount len;
1651   return (((len = XSTRING_LENGTH (obj1)) == XSTRING_LENGTH (obj2)) &&
1652           !memcmp (XSTRING_DATA (obj1), XSTRING_DATA (obj2), len));
1653 }
1654
1655 static const struct lrecord_description string_description[] = {
1656   { XD_BYTECOUNT,       offsetof (Lisp_String, size) },
1657   { XD_OPAQUE_DATA_PTR, offsetof (Lisp_String, data), XD_INDIRECT(0, 1) },
1658   { XD_LISP_OBJECT,     offsetof (Lisp_String, plist) },
1659   { XD_END }
1660 };
1661
1662 /* We store the string's extent info as the first element of the string's
1663    property list; and the string's MODIFF as the first or second element
1664    of the string's property list (depending on whether the extent info
1665    is present), but only if the string has been modified.  This is ugly
1666    but it reduces the memory allocated for the string in the vast
1667    majority of cases, where the string is never modified and has no
1668    extent info.
1669
1670    #### This means you can't use an int as a key in a string's plist. */
1671
1672 static Lisp_Object *
1673 string_plist_ptr (Lisp_Object string)
1674 {
1675   Lisp_Object *ptr = &XSTRING (string)->plist;
1676
1677   if (CONSP (*ptr) && EXTENT_INFOP (XCAR (*ptr)))
1678     ptr = &XCDR (*ptr);
1679   if (CONSP (*ptr) && INTP (XCAR (*ptr)))
1680     ptr = &XCDR (*ptr);
1681   return ptr;
1682 }
1683
1684 static Lisp_Object
1685 string_getprop (Lisp_Object string, Lisp_Object property)
1686 {
1687   return external_plist_get (string_plist_ptr (string), property, 0, ERROR_ME);
1688 }
1689
1690 static int
1691 string_putprop (Lisp_Object string, Lisp_Object property, Lisp_Object value)
1692 {
1693   external_plist_put (string_plist_ptr (string), property, value, 0, ERROR_ME);
1694   return 1;
1695 }
1696
1697 static int
1698 string_remprop (Lisp_Object string, Lisp_Object property)
1699 {
1700   return external_remprop (string_plist_ptr (string), property, 0, ERROR_ME);
1701 }
1702
1703 static Lisp_Object
1704 string_plist (Lisp_Object string)
1705 {
1706   return *string_plist_ptr (string);
1707 }
1708
1709 /* No `finalize', or `hash' methods.
1710    internal_hash() already knows how to hash strings and finalization
1711    is done with the ADDITIONAL_FREE_string macro, which is the
1712    standard way to do finalization when using
1713    SWEEP_FIXED_TYPE_BLOCK(). */
1714 DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS ("string", string,
1715                                                 mark_string, print_string,
1716                                                 0, string_equal, 0,
1717                                                 string_description,
1718                                                 string_getprop,
1719                                                 string_putprop,
1720                                                 string_remprop,
1721                                                 string_plist,
1722                                                 Lisp_String);
1723
1724 /* String blocks contain this many useful bytes. */
1725 #define STRING_CHARS_BLOCK_SIZE                                 \
1726 ((Bytecount) (8192 - MALLOC_OVERHEAD -                          \
1727               ((2 * sizeof (struct string_chars_block *))       \
1728                + sizeof (EMACS_INT))))
1729 /* Block header for small strings. */
1730 struct string_chars_block
1731 {
1732   EMACS_INT pos;
1733   struct string_chars_block *next;
1734   struct string_chars_block *prev;
1735   /* Contents of string_chars_block->string_chars are interleaved
1736      string_chars structures (see below) and the actual string data */
1737   unsigned char string_chars[STRING_CHARS_BLOCK_SIZE];
1738 };
1739
1740 static struct string_chars_block *first_string_chars_block;
1741 static struct string_chars_block *current_string_chars_block;
1742
1743 /* If SIZE is the length of a string, this returns how many bytes
1744  *  the string occupies in string_chars_block->string_chars
1745  *  (including alignment padding).
1746  */
1747 #define STRING_FULLSIZE(size) \
1748    ALIGN_SIZE (((size) + 1 + sizeof (Lisp_String *)),\
1749                ALIGNOF (Lisp_String *))
1750
1751 #define BIG_STRING_FULLSIZE_P(fullsize) ((fullsize) >= STRING_CHARS_BLOCK_SIZE)
1752 #define BIG_STRING_SIZE_P(size) (BIG_STRING_FULLSIZE_P (STRING_FULLSIZE(size)))
1753
1754 #define STRING_CHARS_FREE_P(ptr) ((ptr)->string == NULL)
1755 #define MARK_STRING_CHARS_AS_FREE(ptr) ((void) ((ptr)->string = NULL))
1756
1757 struct string_chars
1758 {
1759   Lisp_String *string;
1760   unsigned char chars[1];
1761 };
1762
1763 struct unused_string_chars
1764 {
1765   Lisp_String *string;
1766   EMACS_INT fullsize;
1767 };
1768
1769 static void
1770 init_string_chars_alloc (void)
1771 {
1772   first_string_chars_block = xnew (struct string_chars_block);
1773   first_string_chars_block->prev = 0;
1774   first_string_chars_block->next = 0;
1775   first_string_chars_block->pos = 0;
1776   current_string_chars_block = first_string_chars_block;
1777 }
1778
1779 static struct string_chars *
1780 allocate_string_chars_struct (Lisp_String *string_it_goes_with,
1781                               EMACS_INT fullsize)
1782 {
1783   struct string_chars *s_chars;
1784
1785   if (fullsize <=
1786       (countof (current_string_chars_block->string_chars)
1787        - current_string_chars_block->pos))
1788     {
1789       /* This string can fit in the current string chars block */
1790       s_chars = (struct string_chars *)
1791         (current_string_chars_block->string_chars
1792          + current_string_chars_block->pos);
1793       current_string_chars_block->pos += fullsize;
1794     }
1795   else
1796     {
1797       /* Make a new current string chars block */
1798       struct string_chars_block *new_scb = xnew (struct string_chars_block);
1799
1800       current_string_chars_block->next = new_scb;
1801       new_scb->prev = current_string_chars_block;
1802       new_scb->next = 0;
1803       current_string_chars_block = new_scb;
1804       new_scb->pos = fullsize;
1805       s_chars = (struct string_chars *)
1806         current_string_chars_block->string_chars;
1807     }
1808
1809   s_chars->string = string_it_goes_with;
1810
1811   INCREMENT_CONS_COUNTER (fullsize, "string chars");
1812
1813   return s_chars;
1814 }
1815
1816 Lisp_Object
1817 make_uninit_string (Bytecount length)
1818 {
1819   Lisp_String *s;
1820   EMACS_INT fullsize = STRING_FULLSIZE (length);
1821   Lisp_Object val;
1822
1823   assert (length >= 0 && fullsize > 0);
1824
1825   /* Allocate the string header */
1826   ALLOCATE_FIXED_TYPE (string, Lisp_String, s);
1827   set_lheader_implementation (&s->lheader, &lrecord_string);
1828
1829   set_string_data (s, BIG_STRING_FULLSIZE_P (fullsize)
1830                    ? xnew_array (Bufbyte, length + 1)
1831                    : allocate_string_chars_struct (s, fullsize)->chars);
1832
1833   set_string_length (s, length);
1834   s->plist = Qnil;
1835
1836   set_string_byte (s, length, 0);
1837
1838   XSETSTRING (val, s);
1839   return val;
1840 }
1841
1842 #ifdef VERIFY_STRING_CHARS_INTEGRITY
1843 static void verify_string_chars_integrity (void);
1844 #endif
1845
1846 /* Resize the string S so that DELTA bytes can be inserted starting
1847    at POS.  If DELTA < 0, it means deletion starting at POS.  If
1848    POS < 0, resize the string but don't copy any characters.  Use
1849    this if you're planning on completely overwriting the string.
1850 */
1851
1852 void
1853 resize_string (Lisp_String *s, Bytecount pos, Bytecount delta)
1854 {
1855   Bytecount oldfullsize, newfullsize;
1856 #ifdef VERIFY_STRING_CHARS_INTEGRITY
1857   verify_string_chars_integrity ();
1858 #endif
1859
1860 #ifdef ERROR_CHECK_BUFPOS
1861   if (pos >= 0)
1862     {
1863       assert (pos <= string_length (s));
1864       if (delta < 0)
1865         assert (pos + (-delta) <= string_length (s));
1866     }
1867   else
1868     {
1869       if (delta < 0)
1870         assert ((-delta) <= string_length (s));
1871     }
1872 #endif /* ERROR_CHECK_BUFPOS */
1873
1874   if (delta == 0)
1875     /* simplest case: no size change. */
1876     return;
1877
1878   if (pos >= 0 && delta < 0)
1879     /* If DELTA < 0, the functions below will delete the characters
1880        before POS.  We want to delete characters *after* POS, however,
1881        so convert this to the appropriate form. */
1882     pos += -delta;
1883
1884   oldfullsize = STRING_FULLSIZE (string_length (s));
1885   newfullsize = STRING_FULLSIZE (string_length (s) + delta);
1886
1887   if (BIG_STRING_FULLSIZE_P (oldfullsize))
1888     {
1889       if (BIG_STRING_FULLSIZE_P (newfullsize))
1890         {
1891           /* Both strings are big.  We can just realloc().
1892              But careful!  If the string is shrinking, we have to
1893              memmove() _before_ realloc(), and if growing, we have to
1894              memmove() _after_ realloc() - otherwise the access is
1895              illegal, and we might crash. */
1896           Bytecount len = string_length (s) + 1 - pos;
1897
1898           if (delta < 0 && pos >= 0)
1899             memmove (string_data (s) + pos + delta, string_data (s) + pos, len);
1900           set_string_data (s, (Bufbyte *) xrealloc (string_data (s),
1901                                                     string_length (s) + delta + 1));
1902           if (delta > 0 && pos >= 0)
1903             memmove (string_data (s) + pos + delta, string_data (s) + pos, len);
1904         }
1905       else /* String has been demoted from BIG_STRING. */
1906         {
1907           Bufbyte *new_data =
1908             allocate_string_chars_struct (s, newfullsize)->chars;
1909           Bufbyte *old_data = string_data (s);
1910
1911           if (pos >= 0)
1912             {
1913               memcpy (new_data, old_data, pos);
1914               memcpy (new_data + pos + delta, old_data + pos,
1915                       string_length (s) + 1 - pos);
1916             }
1917           set_string_data (s, new_data);
1918           xfree (old_data);
1919         }
1920     }
1921   else /* old string is small */
1922     {
1923       if (oldfullsize == newfullsize)
1924         {
1925           /* special case; size change but the necessary
1926              allocation size won't change (up or down; code
1927              somewhere depends on there not being any unused
1928              allocation space, modulo any alignment
1929              constraints). */
1930           if (pos >= 0)
1931             {
1932               Bufbyte *addroff = pos + string_data (s);
1933
1934               memmove (addroff + delta, addroff,
1935                        /* +1 due to zero-termination. */
1936                        string_length (s) + 1 - pos);
1937             }
1938         }
1939       else
1940         {
1941           Bufbyte *old_data = string_data (s);
1942           Bufbyte *new_data =
1943             BIG_STRING_FULLSIZE_P (newfullsize)
1944             ? xnew_array (Bufbyte, string_length (s) + delta + 1)
1945             : allocate_string_chars_struct (s, newfullsize)->chars;
1946
1947           if (pos >= 0)
1948             {
1949               memcpy (new_data, old_data, pos);
1950               memcpy (new_data + pos + delta, old_data + pos,
1951                       string_length (s) + 1 - pos);
1952             }
1953           set_string_data (s, new_data);
1954
1955           {
1956             /* We need to mark this chunk of the string_chars_block
1957                as unused so that compact_string_chars() doesn't
1958                freak. */
1959             struct string_chars *old_s_chars = (struct string_chars *)
1960               ((char *) old_data - offsetof (struct string_chars, chars));
1961             /* Sanity check to make sure we aren't hosed by strange
1962                alignment/padding. */
1963             assert (old_s_chars->string == s);
1964             MARK_STRING_CHARS_AS_FREE (old_s_chars);
1965             ((struct unused_string_chars *) old_s_chars)->fullsize =
1966               oldfullsize;
1967           }
1968         }
1969     }
1970
1971   set_string_length (s, string_length (s) + delta);
1972   /* If pos < 0, the string won't be zero-terminated.
1973      Terminate now just to make sure. */
1974   string_data (s)[string_length (s)] = '\0';
1975
1976   if (pos >= 0)
1977     {
1978       Lisp_Object string;
1979
1980       XSETSTRING (string, s);
1981       /* We also have to adjust all of the extent indices after the
1982          place we did the change.  We say "pos - 1" because
1983          adjust_extents() is exclusive of the starting position
1984          passed to it. */
1985       adjust_extents (string, pos - 1, string_length (s),
1986                       delta);
1987     }
1988
1989 #ifdef VERIFY_STRING_CHARS_INTEGRITY
1990   verify_string_chars_integrity ();
1991 #endif
1992 }
1993
1994 #ifdef MULE
1995
1996 void
1997 set_string_char (Lisp_String *s, Charcount i, Emchar c)
1998 {
1999   Bufbyte newstr[MAX_EMCHAR_LEN];
2000   Bytecount bytoff = charcount_to_bytecount (string_data (s), i);
2001   Bytecount oldlen = charcount_to_bytecount (string_data (s) + bytoff, 1);
2002   Bytecount newlen = set_charptr_emchar (newstr, c);
2003
2004   if (oldlen != newlen)
2005     resize_string (s, bytoff, newlen - oldlen);
2006   /* Remember, string_data (s) might have changed so we can't cache it. */
2007   memcpy (string_data (s) + bytoff, newstr, newlen);
2008 }
2009
2010 #endif /* MULE */
2011
2012 DEFUN ("make-string", Fmake_string, 2, 2, 0, /*
2013 Return a new string consisting of LENGTH copies of CHARACTER.
2014 LENGTH must be a non-negative integer.
2015 */
2016        (length, character))
2017 {
2018   CHECK_NATNUM (length);
2019   CHECK_CHAR_COERCE_INT (character);
2020   {
2021     Bufbyte init_str[MAX_EMCHAR_LEN];
2022     int len = set_charptr_emchar (init_str, XCHAR (character));
2023     Lisp_Object val = make_uninit_string (len * XINT (length));
2024
2025     if (len == 1)
2026       /* Optimize the single-byte case */
2027       memset (XSTRING_DATA (val), XCHAR (character), XSTRING_LENGTH (val));
2028     else
2029       {
2030         size_t i;
2031         Bufbyte *ptr = XSTRING_DATA (val);
2032
2033         for (i = XINT (length); i; i--)
2034           {
2035             Bufbyte *init_ptr = init_str;
2036             switch (len)
2037               {
2038               case 4: *ptr++ = *init_ptr++;
2039               case 3: *ptr++ = *init_ptr++;
2040               case 2: *ptr++ = *init_ptr++;
2041               case 1: *ptr++ = *init_ptr++;
2042               }
2043           }
2044       }
2045     return val;
2046   }
2047 }
2048
2049 DEFUN ("string", Fstring, 0, MANY, 0, /*
2050 Concatenate all the argument characters and make the result a string.
2051 */
2052        (int nargs, Lisp_Object *args))
2053 {
2054   Bufbyte *storage = alloca_array (Bufbyte, nargs * MAX_EMCHAR_LEN);
2055   Bufbyte *p = storage;
2056
2057   for (; nargs; nargs--, args++)
2058     {
2059       Lisp_Object lisp_char = *args;
2060       CHECK_CHAR_COERCE_INT (lisp_char);
2061       p += set_charptr_emchar (p, XCHAR (lisp_char));
2062     }
2063   return make_string (storage, p - storage);
2064 }
2065
2066
2067 /* Take some raw memory, which MUST already be in internal format,
2068    and package it up into a Lisp string. */
2069 Lisp_Object
2070 make_string (const Bufbyte *contents, Bytecount length)
2071 {
2072   Lisp_Object val;
2073
2074   /* Make sure we find out about bad make_string's when they happen */
2075 #if defined (ERROR_CHECK_BUFPOS) && defined (MULE)
2076   bytecount_to_charcount (contents, length); /* Just for the assertions */
2077 #endif
2078
2079   val = make_uninit_string (length);
2080   memcpy (XSTRING_DATA (val), contents, length);
2081   return val;
2082 }
2083
2084 /* Take some raw memory, encoded in some external data format,
2085    and convert it into a Lisp string. */
2086 Lisp_Object
2087 make_ext_string (const Extbyte *contents, EMACS_INT length,
2088                  Lisp_Object coding_system)
2089 {
2090   Lisp_Object string;
2091   TO_INTERNAL_FORMAT (DATA, (contents, length),
2092                       LISP_STRING, string,
2093                       coding_system);
2094   return string;
2095 }
2096
2097 Lisp_Object
2098 build_string (const char *str)
2099 {
2100   /* Some strlen's crash and burn if passed null. */
2101   return make_string ((const Bufbyte *) str, (str ? strlen(str) : 0));
2102 }
2103
2104 Lisp_Object
2105 build_ext_string (const char *str, Lisp_Object coding_system)
2106 {
2107   /* Some strlen's crash and burn if passed null. */
2108   return make_ext_string ((const Extbyte *) str, (str ? strlen(str) : 0),
2109                           coding_system);
2110 }
2111
2112 Lisp_Object
2113 build_translated_string (const char *str)
2114 {
2115   return build_string (GETTEXT (str));
2116 }
2117
2118 Lisp_Object
2119 make_string_nocopy (const Bufbyte *contents, Bytecount length)
2120 {
2121   Lisp_String *s;
2122   Lisp_Object val;
2123
2124   /* Make sure we find out about bad make_string_nocopy's when they happen */
2125 #if defined (ERROR_CHECK_BUFPOS) && defined (MULE)
2126   bytecount_to_charcount (contents, length); /* Just for the assertions */
2127 #endif
2128
2129   /* Allocate the string header */
2130   ALLOCATE_FIXED_TYPE (string, Lisp_String, s);
2131   set_lheader_implementation (&s->lheader, &lrecord_string);
2132   SET_C_READONLY_RECORD_HEADER (&s->lheader);
2133   s->plist = Qnil;
2134   set_string_data (s, (Bufbyte *)contents);
2135   set_string_length (s, length);
2136
2137   XSETSTRING (val, s);
2138   return val;
2139 }
2140
2141 \f
2142 /************************************************************************/
2143 /*                           lcrecord lists                             */
2144 /************************************************************************/
2145
2146 /* Lcrecord lists are used to manage the allocation of particular
2147    sorts of lcrecords, to avoid calling alloc_lcrecord() (and thus
2148    malloc() and garbage-collection junk) as much as possible.
2149    It is similar to the Blocktype class.
2150
2151    It works like this:
2152
2153    1) Create an lcrecord-list object using make_lcrecord_list().
2154       This is often done at initialization.  Remember to staticpro_nodump
2155       this object!  The arguments to make_lcrecord_list() are the
2156       same as would be passed to alloc_lcrecord().
2157    2) Instead of calling alloc_lcrecord(), call allocate_managed_lcrecord()
2158       and pass the lcrecord-list earlier created.
2159    3) When done with the lcrecord, call free_managed_lcrecord().
2160       The standard freeing caveats apply: ** make sure there are no
2161       pointers to the object anywhere! **
2162    4) Calling free_managed_lcrecord() is just like kissing the
2163       lcrecord goodbye as if it were garbage-collected.  This means:
2164       -- the contents of the freed lcrecord are undefined, and the
2165          contents of something produced by allocate_managed_lcrecord()
2166          are undefined, just like for alloc_lcrecord().
2167       -- the mark method for the lcrecord's type will *NEVER* be called
2168          on freed lcrecords.
2169       -- the finalize method for the lcrecord's type will be called
2170          at the time that free_managed_lcrecord() is called.
2171
2172    */
2173
2174 static Lisp_Object
2175 mark_lcrecord_list (Lisp_Object obj)
2176 {
2177   struct lcrecord_list *list = XLCRECORD_LIST (obj);
2178   Lisp_Object chain = list->free;
2179
2180   while (!NILP (chain))
2181     {
2182       struct lrecord_header *lheader = XRECORD_LHEADER (chain);
2183       struct free_lcrecord_header *free_header =
2184         (struct free_lcrecord_header *) lheader;
2185
2186       gc_checking_assert
2187         (/* There should be no other pointers to the free list. */
2188          ! MARKED_RECORD_HEADER_P (lheader)
2189          &&
2190          /* Only lcrecords should be here. */
2191          ! LHEADER_IMPLEMENTATION (lheader)->basic_p
2192          &&
2193          /* Only free lcrecords should be here. */
2194          free_header->lcheader.free
2195          &&
2196          /* The type of the lcrecord must be right. */
2197          LHEADER_IMPLEMENTATION (lheader) == list->implementation
2198          &&
2199          /* So must the size. */
2200          (LHEADER_IMPLEMENTATION (lheader)->static_size == 0 ||
2201           LHEADER_IMPLEMENTATION (lheader)->static_size == list->size)
2202          );
2203
2204       MARK_RECORD_HEADER (lheader);
2205       chain = free_header->chain;
2206     }
2207
2208   return Qnil;
2209 }
2210
2211 DEFINE_LRECORD_IMPLEMENTATION ("lcrecord-list", lcrecord_list,
2212                                mark_lcrecord_list, internal_object_printer,
2213                                0, 0, 0, 0, struct lcrecord_list);
2214 Lisp_Object
2215 make_lcrecord_list (size_t size,
2216                     const struct lrecord_implementation *implementation)
2217 {
2218   struct lcrecord_list *p = alloc_lcrecord_type (struct lcrecord_list,
2219                                                  &lrecord_lcrecord_list);
2220   Lisp_Object val;
2221
2222   p->implementation = implementation;
2223   p->size = size;
2224   p->free = Qnil;
2225   XSETLCRECORD_LIST (val, p);
2226   return val;
2227 }
2228
2229 Lisp_Object
2230 allocate_managed_lcrecord (Lisp_Object lcrecord_list)
2231 {
2232   struct lcrecord_list *list = XLCRECORD_LIST (lcrecord_list);
2233   if (!NILP (list->free))
2234     {
2235       Lisp_Object val = list->free;
2236       struct free_lcrecord_header *free_header =
2237         (struct free_lcrecord_header *) XPNTR (val);
2238
2239 #ifdef ERROR_CHECK_GC
2240       struct lrecord_header *lheader = &free_header->lcheader.lheader;
2241
2242       /* There should be no other pointers to the free list. */
2243       assert (! MARKED_RECORD_HEADER_P (lheader));
2244       /* Only lcrecords should be here. */
2245       assert (! LHEADER_IMPLEMENTATION (lheader)->basic_p);
2246       /* Only free lcrecords should be here. */
2247       assert (free_header->lcheader.free);
2248       /* The type of the lcrecord must be right. */
2249       assert (LHEADER_IMPLEMENTATION (lheader) == list->implementation);
2250       /* So must the size. */
2251       assert (LHEADER_IMPLEMENTATION (lheader)->static_size == 0 ||
2252               LHEADER_IMPLEMENTATION (lheader)->static_size == list->size);
2253 #endif /* ERROR_CHECK_GC */
2254
2255       list->free = free_header->chain;
2256       free_header->lcheader.free = 0;
2257       return val;
2258     }
2259   else
2260     {
2261       Lisp_Object val;
2262
2263       XSETOBJ (val, alloc_lcrecord (list->size, list->implementation));
2264       return val;
2265     }
2266 }
2267
2268 void
2269 free_managed_lcrecord (Lisp_Object lcrecord_list, Lisp_Object lcrecord)
2270 {
2271   struct lcrecord_list *list = XLCRECORD_LIST (lcrecord_list);
2272   struct free_lcrecord_header *free_header =
2273     (struct free_lcrecord_header *) XPNTR (lcrecord);
2274   struct lrecord_header *lheader = &free_header->lcheader.lheader;
2275   const struct lrecord_implementation *implementation
2276     = LHEADER_IMPLEMENTATION (lheader);
2277
2278   /* Make sure the size is correct.  This will catch, for example,
2279      putting a window configuration on the wrong free list. */
2280   gc_checking_assert ((implementation->size_in_bytes_method ?
2281                        implementation->size_in_bytes_method (lheader) :
2282                        implementation->static_size)
2283                       == list->size);
2284
2285   if (implementation->finalizer)
2286     implementation->finalizer (lheader, 0);
2287   free_header->chain = list->free;
2288   free_header->lcheader.free = 1;
2289   list->free = lcrecord;
2290 }
2291
2292 \f
2293
2294 \f
2295 DEFUN ("purecopy", Fpurecopy, 1, 1, 0, /*
2296 Kept for compatibility, returns its argument.
2297 Old:
2298 Make a copy of OBJECT in pure storage.
2299 Recursively copies contents of vectors and cons cells.
2300 Does not copy symbols.
2301 */
2302        (object))
2303 {
2304   return object;
2305 }
2306
2307 \f
2308 /************************************************************************/
2309 /*                         Garbage Collection                           */
2310 /************************************************************************/
2311
2312 /* All the built-in lisp object types are enumerated in `enum lrecord_type'.
2313    Additional ones may be defined by a module (none yet).  We leave some
2314    room in `lrecord_implementations_table' for such new lisp object types. */
2315 const struct lrecord_implementation *lrecord_implementations_table[(unsigned int)lrecord_type_last_built_in_type + MODULE_DEFINABLE_TYPE_COUNT];
2316 unsigned int lrecord_type_count = (unsigned int)lrecord_type_last_built_in_type;
2317 /* Object marker functions are in the lrecord_implementation structure.
2318    But copying them to a parallel array is much more cache-friendly.
2319    This hack speeds up (garbage-collect) by about 5%. */
2320 Lisp_Object (*lrecord_markers[countof (lrecord_implementations_table)]) (Lisp_Object);
2321
2322 struct gcpro *gcprolist;
2323
2324 /* We want the staticpros relocated, but not the pointers found therein.
2325    Hence we use a trivial description, as for pointerless objects. */
2326 static const struct lrecord_description staticpro_description_1[] = {
2327   { XD_END }
2328 };
2329
2330 static const struct struct_description staticpro_description = {
2331   sizeof (Lisp_Object *),
2332   staticpro_description_1
2333 };
2334
2335 static const struct lrecord_description staticpros_description_1[] = {
2336   XD_DYNARR_DESC (Lisp_Object_ptr_dynarr, &staticpro_description),
2337   { XD_END }
2338 };
2339
2340 static const struct struct_description staticpros_description = {
2341   sizeof (Lisp_Object_ptr_dynarr),
2342   staticpros_description_1
2343 };
2344
2345 Lisp_Object_ptr_dynarr *staticpros;
2346
2347 /* Mark the Lisp_Object at non-heap VARADDRESS as a root object for
2348    garbage collection, and for dumping. */
2349 void
2350 staticpro (Lisp_Object *varaddress)
2351 {
2352   Dynarr_add (staticpros, varaddress);
2353   dump_add_root_object (varaddress);
2354 }
2355
2356
2357 Lisp_Object_ptr_dynarr *staticpros_nodump;
2358
2359 /* Mark the Lisp_Object at non-heap VARADDRESS as a root object for
2360    garbage collection, but not for dumping. */
2361 void
2362 staticpro_nodump (Lisp_Object *varaddress)
2363 {
2364   Dynarr_add (staticpros_nodump, varaddress);
2365 }
2366
2367 #ifdef ERROR_CHECK_GC
2368 #define GC_CHECK_LHEADER_INVARIANTS(lheader) do {               \
2369   struct lrecord_header * GCLI_lh = (lheader);                  \
2370   assert (GCLI_lh != 0);                                        \
2371   assert (GCLI_lh->type < lrecord_type_count);                  \
2372   assert (! C_READONLY_RECORD_HEADER_P (GCLI_lh) ||             \
2373           (MARKED_RECORD_HEADER_P (GCLI_lh) &&                  \
2374            LISP_READONLY_RECORD_HEADER_P (GCLI_lh)));           \
2375 } while (0)
2376 #else
2377 #define GC_CHECK_LHEADER_INVARIANTS(lheader)
2378 #endif
2379
2380 \f
2381 /* Mark reference to a Lisp_Object.  If the object referred to has not been
2382    seen yet, recursively mark all the references contained in it. */
2383
2384 void
2385 mark_object (Lisp_Object obj)
2386 {
2387  tail_recurse:
2388
2389   /* Checks we used to perform */
2390   /* if (EQ (obj, Qnull_pointer)) return; */
2391   /* if (!POINTER_TYPE_P (XGCTYPE (obj))) return; */
2392   /* if (PURIFIED (XPNTR (obj))) return; */
2393
2394   if (XTYPE (obj) == Lisp_Type_Record)
2395     {
2396       struct lrecord_header *lheader = XRECORD_LHEADER (obj);
2397
2398       GC_CHECK_LHEADER_INVARIANTS (lheader);
2399
2400       gc_checking_assert (LHEADER_IMPLEMENTATION (lheader)->basic_p ||
2401                           ! ((struct lcrecord_header *) lheader)->free);
2402
2403       /* All c_readonly objects have their mark bit set,
2404          so that we only need to check the mark bit here. */
2405       if (! MARKED_RECORD_HEADER_P (lheader))
2406         {
2407           MARK_RECORD_HEADER (lheader);
2408
2409           if (RECORD_MARKER (lheader))
2410             {
2411               obj = RECORD_MARKER (lheader) (obj);
2412               if (!NILP (obj)) goto tail_recurse;
2413             }
2414         }
2415     }
2416 }
2417
2418 /* mark all of the conses in a list and mark the final cdr; but
2419    DO NOT mark the cars.
2420
2421    Use only for internal lists!  There should never be other pointers
2422    to the cons cells, because if so, the cars will remain unmarked
2423    even when they maybe should be marked. */
2424 void
2425 mark_conses_in_list (Lisp_Object obj)
2426 {
2427   Lisp_Object rest;
2428
2429   for (rest = obj; CONSP (rest); rest = XCDR (rest))
2430     {
2431       if (CONS_MARKED_P (XCONS (rest)))
2432         return;
2433       MARK_CONS (XCONS (rest));
2434     }
2435
2436   mark_object (rest);
2437 }
2438
2439 \f
2440 /* Find all structures not marked, and free them. */
2441
2442 static int gc_count_num_bit_vector_used, gc_count_bit_vector_total_size;
2443 static int gc_count_bit_vector_storage;
2444 static int gc_count_num_short_string_in_use;
2445 static int gc_count_string_total_size;
2446 static int gc_count_short_string_total_size;
2447
2448 /* static int gc_count_total_records_used, gc_count_records_total_size; */
2449
2450 \f
2451 /* stats on lcrecords in use - kinda kludgy */
2452
2453 static struct
2454 {
2455   int instances_in_use;
2456   int bytes_in_use;
2457   int instances_freed;
2458   int bytes_freed;
2459   int instances_on_free_list;
2460 } lcrecord_stats [countof (lrecord_implementations_table)];
2461
2462 static void
2463 tick_lcrecord_stats (const struct lrecord_header *h, int free_p)
2464 {
2465   unsigned int type_index = h->type;
2466
2467   if (((struct lcrecord_header *) h)->free)
2468     {
2469       gc_checking_assert (!free_p);
2470       lcrecord_stats[type_index].instances_on_free_list++;
2471     }
2472   else
2473     {
2474       const struct lrecord_implementation *implementation =
2475         LHEADER_IMPLEMENTATION (h);
2476
2477       size_t sz = (implementation->size_in_bytes_method ?
2478                    implementation->size_in_bytes_method (h) :
2479                    implementation->static_size);
2480       if (free_p)
2481         {
2482           lcrecord_stats[type_index].instances_freed++;
2483           lcrecord_stats[type_index].bytes_freed += sz;
2484         }
2485       else
2486         {
2487           lcrecord_stats[type_index].instances_in_use++;
2488           lcrecord_stats[type_index].bytes_in_use += sz;
2489         }
2490     }
2491 }
2492
2493 \f
2494 /* Free all unmarked records */
2495 static void
2496 sweep_lcrecords_1 (struct lcrecord_header **prev, int *used)
2497 {
2498   struct lcrecord_header *header;
2499   int num_used = 0;
2500   /* int total_size = 0; */
2501
2502   xzero (lcrecord_stats); /* Reset all statistics to 0. */
2503
2504   /* First go through and call all the finalize methods.
2505      Then go through and free the objects.  There used to
2506      be only one loop here, with the call to the finalizer
2507      occurring directly before the xfree() below.  That
2508      is marginally faster but much less safe -- if the
2509      finalize method for an object needs to reference any
2510      other objects contained within it (and many do),
2511      we could easily be screwed by having already freed that
2512      other object. */
2513
2514   for (header = *prev; header; header = header->next)
2515     {
2516       struct lrecord_header *h = &(header->lheader);
2517
2518       GC_CHECK_LHEADER_INVARIANTS (h);
2519
2520       if (! MARKED_RECORD_HEADER_P (h) && ! header->free)
2521         {
2522           if (LHEADER_IMPLEMENTATION (h)->finalizer)
2523             LHEADER_IMPLEMENTATION (h)->finalizer (h, 0);
2524         }
2525     }
2526
2527   for (header = *prev; header; )
2528     {
2529       struct lrecord_header *h = &(header->lheader);
2530       if (MARKED_RECORD_HEADER_P (h))
2531         {
2532           if (! C_READONLY_RECORD_HEADER_P (h))
2533             UNMARK_RECORD_HEADER (h);
2534           num_used++;
2535           /* total_size += n->implementation->size_in_bytes (h);*/
2536           /* #### May modify header->next on a C_READONLY lcrecord */
2537           prev = &(header->next);
2538           header = *prev;
2539           tick_lcrecord_stats (h, 0);
2540         }
2541       else
2542         {
2543           struct lcrecord_header *next = header->next;
2544           *prev = next;
2545           tick_lcrecord_stats (h, 1);
2546           /* used to call finalizer right here. */
2547           xfree (header);
2548           header = next;
2549         }
2550     }
2551   *used = num_used;
2552   /* *total = total_size; */
2553 }
2554
2555
2556 static void
2557 sweep_bit_vectors_1 (Lisp_Object *prev,
2558                      int *used, int *total, int *storage)
2559 {
2560   Lisp_Object bit_vector;
2561   int num_used = 0;
2562   int total_size = 0;
2563   int total_storage = 0;
2564
2565   /* BIT_VECTORP fails because the objects are marked, which changes
2566      their implementation */
2567   for (bit_vector = *prev; !EQ (bit_vector, Qzero); )
2568     {
2569       Lisp_Bit_Vector *v = XBIT_VECTOR (bit_vector);
2570       int len = v->size;
2571       if (MARKED_RECORD_P (bit_vector))
2572         {
2573           if (! C_READONLY_RECORD_HEADER_P(&(v->lheader)))
2574             UNMARK_RECORD_HEADER (&(v->lheader));
2575           total_size += len;
2576           total_storage +=
2577             MALLOC_OVERHEAD +
2578             FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Bit_Vector, bits,
2579                                           BIT_VECTOR_LONG_STORAGE (len));
2580           num_used++;
2581           /* #### May modify next on a C_READONLY bitvector */
2582           prev = &(bit_vector_next (v));
2583           bit_vector = *prev;
2584         }
2585       else
2586         {
2587           Lisp_Object next = bit_vector_next (v);
2588           *prev = next;
2589           xfree (v);
2590           bit_vector = next;
2591         }
2592     }
2593   *used = num_used;
2594   *total = total_size;
2595   *storage = total_storage;
2596 }
2597
2598 /* And the Lord said: Thou shalt use the `c-backslash-region' command
2599    to make macros prettier. */
2600
2601 #ifdef ERROR_CHECK_GC
2602
2603 #define SWEEP_FIXED_TYPE_BLOCK(typename, obj_type)                      \
2604 do {                                                                    \
2605   struct typename##_block *SFTB_current;                                \
2606   int SFTB_limit;                                                       \
2607   int num_free = 0, num_used = 0;                                       \
2608                                                                         \
2609   for (SFTB_current = current_##typename##_block,                       \
2610        SFTB_limit = current_##typename##_block_index;                   \
2611        SFTB_current;                                                    \
2612        )                                                                \
2613     {                                                                   \
2614       int SFTB_iii;                                                     \
2615                                                                         \
2616       for (SFTB_iii = 0; SFTB_iii < SFTB_limit; SFTB_iii++)             \
2617         {                                                               \
2618           obj_type *SFTB_victim = &(SFTB_current->block[SFTB_iii]);     \
2619                                                                         \
2620           if (LRECORD_FREE_P (SFTB_victim))                             \
2621             {                                                           \
2622               num_free++;                                               \
2623             }                                                           \
2624           else if (C_READONLY_RECORD_HEADER_P (&SFTB_victim->lheader))  \
2625             {                                                           \
2626               num_used++;                                               \
2627             }                                                           \
2628           else if (! MARKED_RECORD_HEADER_P (&SFTB_victim->lheader))    \
2629             {                                                           \
2630               num_free++;                                               \
2631               FREE_FIXED_TYPE (typename, obj_type, SFTB_victim);        \
2632             }                                                           \
2633           else                                                          \
2634             {                                                           \
2635               num_used++;                                               \
2636               UNMARK_##typename (SFTB_victim);                          \
2637             }                                                           \
2638         }                                                               \
2639       SFTB_current = SFTB_current->prev;                                \
2640       SFTB_limit = countof (current_##typename##_block->block);         \
2641     }                                                                   \
2642                                                                         \
2643   gc_count_num_##typename##_in_use = num_used;                          \
2644   gc_count_num_##typename##_freelist = num_free;                        \
2645 } while (0)
2646
2647 #else /* !ERROR_CHECK_GC */
2648
2649 #define SWEEP_FIXED_TYPE_BLOCK(typename, obj_type)                              \
2650 do {                                                                            \
2651   struct typename##_block *SFTB_current;                                        \
2652   struct typename##_block **SFTB_prev;                                          \
2653   int SFTB_limit;                                                               \
2654   int num_free = 0, num_used = 0;                                               \
2655                                                                                 \
2656   typename##_free_list = 0;                                                     \
2657                                                                                 \
2658   for (SFTB_prev = &current_##typename##_block,                                 \
2659        SFTB_current = current_##typename##_block,                               \
2660        SFTB_limit = current_##typename##_block_index;                           \
2661        SFTB_current;                                                            \
2662        )                                                                        \
2663     {                                                                           \
2664       int SFTB_iii;                                                             \
2665       int SFTB_empty = 1;                                                       \
2666       Lisp_Free *SFTB_old_free_list = typename##_free_list;                     \
2667                                                                                 \
2668       for (SFTB_iii = 0; SFTB_iii < SFTB_limit; SFTB_iii++)                     \
2669         {                                                                       \
2670           obj_type *SFTB_victim = &(SFTB_current->block[SFTB_iii]);             \
2671                                                                                 \
2672           if (LRECORD_FREE_P (SFTB_victim))                                     \
2673             {                                                                   \
2674               num_free++;                                                       \
2675               PUT_FIXED_TYPE_ON_FREE_LIST (typename, obj_type, SFTB_victim);    \
2676             }                                                                   \
2677           else if (C_READONLY_RECORD_HEADER_P (&SFTB_victim->lheader))          \
2678             {                                                                   \
2679               SFTB_empty = 0;                                                   \
2680               num_used++;                                                       \
2681             }                                                                   \
2682           else if (! MARKED_RECORD_HEADER_P (&SFTB_victim->lheader))            \
2683             {                                                                   \
2684               num_free++;                                                       \
2685               FREE_FIXED_TYPE (typename, obj_type, SFTB_victim);                \
2686             }                                                                   \
2687           else                                                                  \
2688             {                                                                   \
2689               SFTB_empty = 0;                                                   \
2690               num_used++;                                                       \
2691               UNMARK_##typename (SFTB_victim);                                  \
2692             }                                                                   \
2693         }                                                                       \
2694       if (!SFTB_empty)                                                          \
2695         {                                                                       \
2696           SFTB_prev = &(SFTB_current->prev);                                    \
2697           SFTB_current = SFTB_current->prev;                                    \
2698         }                                                                       \
2699       else if (SFTB_current == current_##typename##_block                       \
2700                && !SFTB_current->prev)                                          \
2701         {                                                                       \
2702           /* No real point in freeing sole allocation block */                  \
2703           break;                                                                \
2704         }                                                                       \
2705       else                                                                      \
2706         {                                                                       \
2707           struct typename##_block *SFTB_victim_block = SFTB_current;            \
2708           if (SFTB_victim_block == current_##typename##_block)                  \
2709             current_##typename##_block_index                                    \
2710               = countof (current_##typename##_block->block);                    \
2711           SFTB_current = SFTB_current->prev;                                    \
2712           {                                                                     \
2713             *SFTB_prev = SFTB_current;                                          \
2714             xfree (SFTB_victim_block);                                          \
2715             /* Restore free list to what it was before victim was swept */      \
2716             typename##_free_list = SFTB_old_free_list;                          \
2717             num_free -= SFTB_limit;                                             \
2718           }                                                                     \
2719         }                                                                       \
2720       SFTB_limit = countof (current_##typename##_block->block);                 \
2721     }                                                                           \
2722                                                                                 \
2723   gc_count_num_##typename##_in_use = num_used;                                  \
2724   gc_count_num_##typename##_freelist = num_free;                                \
2725 } while (0)
2726
2727 #endif /* !ERROR_CHECK_GC */
2728
2729 \f
2730
2731
2732 static void
2733 sweep_conses (void)
2734 {
2735 #define UNMARK_cons(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
2736 #define ADDITIONAL_FREE_cons(ptr)
2737
2738   SWEEP_FIXED_TYPE_BLOCK (cons, Lisp_Cons);
2739 }
2740
2741 /* Explicitly free a cons cell.  */
2742 void
2743 free_cons (Lisp_Cons *ptr)
2744 {
2745 #ifdef ERROR_CHECK_GC
2746   /* If the CAR is not an int, then it will be a pointer, which will
2747      always be four-byte aligned.  If this cons cell has already been
2748      placed on the free list, however, its car will probably contain
2749      a chain pointer to the next cons on the list, which has cleverly
2750      had all its 0's and 1's inverted.  This allows for a quick
2751      check to make sure we're not freeing something already freed. */
2752   if (POINTER_TYPE_P (XTYPE (ptr->car)))
2753     ASSERT_VALID_POINTER (XPNTR (ptr->car));
2754 #endif /* ERROR_CHECK_GC */
2755
2756 #ifndef ALLOC_NO_POOLS
2757   FREE_FIXED_TYPE_WHEN_NOT_IN_GC (cons, Lisp_Cons, ptr);
2758 #endif /* ALLOC_NO_POOLS */
2759 }
2760
2761 /* explicitly free a list.  You **must make sure** that you have
2762    created all the cons cells that make up this list and that there
2763    are no pointers to any of these cons cells anywhere else.  If there
2764    are, you will lose. */
2765
2766 void
2767 free_list (Lisp_Object list)
2768 {
2769   Lisp_Object rest, next;
2770
2771   for (rest = list; !NILP (rest); rest = next)
2772     {
2773       next = XCDR (rest);
2774       free_cons (XCONS (rest));
2775     }
2776 }
2777
2778 /* explicitly free an alist.  You **must make sure** that you have
2779    created all the cons cells that make up this alist and that there
2780    are no pointers to any of these cons cells anywhere else.  If there
2781    are, you will lose. */
2782
2783 void
2784 free_alist (Lisp_Object alist)
2785 {
2786   Lisp_Object rest, next;
2787
2788   for (rest = alist; !NILP (rest); rest = next)
2789     {
2790       next = XCDR (rest);
2791       free_cons (XCONS (XCAR (rest)));
2792       free_cons (XCONS (rest));
2793     }
2794 }
2795
2796 static void
2797 sweep_compiled_functions (void)
2798 {
2799 #define UNMARK_compiled_function(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
2800 #define ADDITIONAL_FREE_compiled_function(ptr)
2801
2802   SWEEP_FIXED_TYPE_BLOCK (compiled_function, Lisp_Compiled_Function);
2803 }
2804
2805
2806 #ifdef LISP_FLOAT_TYPE
2807 static void
2808 sweep_floats (void)
2809 {
2810 #define UNMARK_float(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
2811 #define ADDITIONAL_FREE_float(ptr)
2812
2813   SWEEP_FIXED_TYPE_BLOCK (float, Lisp_Float);
2814 }
2815 #endif /* LISP_FLOAT_TYPE */
2816
2817 static void
2818 sweep_symbols (void)
2819 {
2820 #define UNMARK_symbol(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
2821 #define ADDITIONAL_FREE_symbol(ptr)
2822
2823   SWEEP_FIXED_TYPE_BLOCK (symbol, Lisp_Symbol);
2824 }
2825
2826 static void
2827 sweep_extents (void)
2828 {
2829 #define UNMARK_extent(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
2830 #define ADDITIONAL_FREE_extent(ptr)
2831
2832   SWEEP_FIXED_TYPE_BLOCK (extent, struct extent);
2833 }
2834
2835 static void
2836 sweep_events (void)
2837 {
2838 #define UNMARK_event(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
2839 #define ADDITIONAL_FREE_event(ptr)
2840
2841   SWEEP_FIXED_TYPE_BLOCK (event, Lisp_Event);
2842 }
2843
2844 static void
2845 sweep_markers (void)
2846 {
2847 #define UNMARK_marker(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
2848 #define ADDITIONAL_FREE_marker(ptr)                                     \
2849   do { Lisp_Object tem;                                                 \
2850        XSETMARKER (tem, ptr);                                           \
2851        unchain_marker (tem);                                            \
2852      } while (0)
2853
2854   SWEEP_FIXED_TYPE_BLOCK (marker, Lisp_Marker);
2855 }
2856
2857 /* Explicitly free a marker.  */
2858 void
2859 free_marker (Lisp_Marker *ptr)
2860 {
2861   /* Perhaps this will catch freeing an already-freed marker. */
2862   gc_checking_assert (ptr->lheader.type == lrecord_type_marker);
2863
2864 #ifndef ALLOC_NO_POOLS
2865   FREE_FIXED_TYPE_WHEN_NOT_IN_GC (marker, Lisp_Marker, ptr);
2866 #endif /* ALLOC_NO_POOLS */
2867 }
2868 \f
2869
2870 #if defined (MULE) && defined (VERIFY_STRING_CHARS_INTEGRITY)
2871
2872 static void
2873 verify_string_chars_integrity (void)
2874 {
2875   struct string_chars_block *sb;
2876
2877   /* Scan each existing string block sequentially, string by string.  */
2878   for (sb = first_string_chars_block; sb; sb = sb->next)
2879     {
2880       int pos = 0;
2881       /* POS is the index of the next string in the block.  */
2882       while (pos < sb->pos)
2883         {
2884           struct string_chars *s_chars =
2885             (struct string_chars *) &(sb->string_chars[pos]);
2886           Lisp_String *string;
2887           int size;
2888           int fullsize;
2889
2890           /* If the string_chars struct is marked as free (i.e. the
2891              STRING pointer is NULL) then this is an unused chunk of
2892              string storage. (See below.) */
2893
2894           if (STRING_CHARS_FREE_P (s_chars))
2895             {
2896               fullsize = ((struct unused_string_chars *) s_chars)->fullsize;
2897               pos += fullsize;
2898               continue;
2899             }
2900
2901           string = s_chars->string;
2902           /* Must be 32-bit aligned. */
2903           assert ((((int) string) & 3) == 0);
2904
2905           size = string_length (string);
2906           fullsize = STRING_FULLSIZE (size);
2907
2908           assert (!BIG_STRING_FULLSIZE_P (fullsize));
2909           assert (string_data (string) == s_chars->chars);
2910           pos += fullsize;
2911         }
2912       assert (pos == sb->pos);
2913     }
2914 }
2915
2916 #endif /* MULE && ERROR_CHECK_GC */
2917
2918 /* Compactify string chars, relocating the reference to each --
2919    free any empty string_chars_block we see. */
2920 static void
2921 compact_string_chars (void)
2922 {
2923   struct string_chars_block *to_sb = first_string_chars_block;
2924   int to_pos = 0;
2925   struct string_chars_block *from_sb;
2926
2927   /* Scan each existing string block sequentially, string by string.  */
2928   for (from_sb = first_string_chars_block; from_sb; from_sb = from_sb->next)
2929     {
2930       int from_pos = 0;
2931       /* FROM_POS is the index of the next string in the block.  */
2932       while (from_pos < from_sb->pos)
2933         {
2934           struct string_chars *from_s_chars =
2935             (struct string_chars *) &(from_sb->string_chars[from_pos]);
2936           struct string_chars *to_s_chars;
2937           Lisp_String *string;
2938           int size;
2939           int fullsize;
2940
2941           /* If the string_chars struct is marked as free (i.e. the
2942              STRING pointer is NULL) then this is an unused chunk of
2943              string storage.  This happens under Mule when a string's
2944              size changes in such a way that its fullsize changes.
2945              (Strings can change size because a different-length
2946              character can be substituted for another character.)
2947              In this case, after the bogus string pointer is the
2948              "fullsize" of this entry, i.e. how many bytes to skip. */
2949
2950           if (STRING_CHARS_FREE_P (from_s_chars))
2951             {
2952               fullsize = ((struct unused_string_chars *) from_s_chars)->fullsize;
2953               from_pos += fullsize;
2954               continue;
2955             }
2956
2957           string = from_s_chars->string;
2958           assert (!(LRECORD_FREE_P (string)));
2959
2960           size = string_length (string);
2961           fullsize = STRING_FULLSIZE (size);
2962
2963           gc_checking_assert (! BIG_STRING_FULLSIZE_P (fullsize));
2964
2965           /* Just skip it if it isn't marked.  */
2966           if (! MARKED_RECORD_HEADER_P (&(string->lheader)))
2967             {
2968               from_pos += fullsize;
2969               continue;
2970             }
2971
2972           /* If it won't fit in what's left of TO_SB, close TO_SB out
2973              and go on to the next string_chars_block.  We know that TO_SB
2974              cannot advance past FROM_SB here since FROM_SB is large enough
2975              to currently contain this string. */
2976           if ((to_pos + fullsize) > countof (to_sb->string_chars))
2977             {
2978               to_sb->pos = to_pos;
2979               to_sb = to_sb->next;
2980               to_pos = 0;
2981             }
2982
2983           /* Compute new address of this string
2984              and update TO_POS for the space being used.  */
2985           to_s_chars = (struct string_chars *) &(to_sb->string_chars[to_pos]);
2986
2987           /* Copy the string_chars to the new place.  */
2988           if (from_s_chars != to_s_chars)
2989             memmove (to_s_chars, from_s_chars, fullsize);
2990
2991           /* Relocate FROM_S_CHARS's reference */
2992           set_string_data (string, &(to_s_chars->chars[0]));
2993
2994           from_pos += fullsize;
2995           to_pos += fullsize;
2996         }
2997     }
2998
2999   /* Set current to the last string chars block still used and
3000      free any that follow. */
3001   {
3002     struct string_chars_block *victim;
3003
3004     for (victim = to_sb->next; victim; )
3005       {
3006         struct string_chars_block *next = victim->next;
3007         xfree (victim);
3008         victim = next;
3009       }
3010
3011     current_string_chars_block = to_sb;
3012     current_string_chars_block->pos = to_pos;
3013     current_string_chars_block->next = 0;
3014   }
3015 }
3016
3017 #if 1 /* Hack to debug missing purecopy's */
3018 static int debug_string_purity;
3019
3020 static void
3021 debug_string_purity_print (Lisp_String *p)
3022 {
3023   Charcount i;
3024   Charcount s = string_char_length (p);
3025   stderr_out ("\"");
3026   for (i = 0; i < s; i++)
3027   {
3028     Emchar ch = string_char (p, i);
3029     if (ch < 32 || ch >= 126)
3030       stderr_out ("\\%03o", ch);
3031     else if (ch == '\\' || ch == '\"')
3032       stderr_out ("\\%c", ch);
3033     else
3034       stderr_out ("%c", ch);
3035   }
3036   stderr_out ("\"\n");
3037 }
3038 #endif /* 1 */
3039
3040
3041 static void
3042 sweep_strings (void)
3043 {
3044   int num_small_used = 0, num_small_bytes = 0, num_bytes = 0;
3045   int debug = debug_string_purity;
3046
3047 #define UNMARK_string(ptr) do {                 \
3048     Lisp_String *p = (ptr);                     \
3049     size_t size = string_length (p);            \
3050     UNMARK_RECORD_HEADER (&(p->lheader));       \
3051     num_bytes += size;                          \
3052     if (!BIG_STRING_SIZE_P (size))              \
3053       {                                         \
3054         num_small_bytes += size;                \
3055         num_small_used++;                       \
3056       }                                         \
3057     if (debug)                                  \
3058       debug_string_purity_print (p);            \
3059   } while (0)
3060 #define ADDITIONAL_FREE_string(ptr) do {        \
3061     size_t size = string_length (ptr);          \
3062     if (BIG_STRING_SIZE_P (size))               \
3063       xfree (ptr->data);                        \
3064   } while (0)
3065
3066   SWEEP_FIXED_TYPE_BLOCK (string, Lisp_String);
3067
3068   gc_count_num_short_string_in_use = num_small_used;
3069   gc_count_string_total_size = num_bytes;
3070   gc_count_short_string_total_size = num_small_bytes;
3071 }
3072
3073
3074 /* I hate duplicating all this crap! */
3075 int
3076 marked_p (Lisp_Object obj)
3077 {
3078   /* Checks we used to perform. */
3079   /* if (EQ (obj, Qnull_pointer)) return 1; */
3080   /* if (!POINTER_TYPE_P (XGCTYPE (obj))) return 1; */
3081   /* if (PURIFIED (XPNTR (obj))) return 1; */
3082
3083   if (XTYPE (obj) == Lisp_Type_Record)
3084     {
3085       struct lrecord_header *lheader = XRECORD_LHEADER (obj);
3086
3087       GC_CHECK_LHEADER_INVARIANTS (lheader);
3088
3089       return MARKED_RECORD_HEADER_P (lheader);
3090     }
3091   return 1;
3092 }
3093
3094 static void
3095 gc_sweep (void)
3096 {
3097   /* Free all unmarked records.  Do this at the very beginning,
3098      before anything else, so that the finalize methods can safely
3099      examine items in the objects.  sweep_lcrecords_1() makes
3100      sure to call all the finalize methods *before* freeing anything,
3101      to complete the safety. */
3102   {
3103     int ignored;
3104     sweep_lcrecords_1 (&all_lcrecords, &ignored);
3105   }
3106
3107   compact_string_chars ();
3108
3109   /* Finalize methods below (called through the ADDITIONAL_FREE_foo
3110      macros) must be *extremely* careful to make sure they're not
3111      referencing freed objects.  The only two existing finalize
3112      methods (for strings and markers) pass muster -- the string
3113      finalizer doesn't look at anything but its own specially-
3114      created block, and the marker finalizer only looks at live
3115      buffers (which will never be freed) and at the markers before
3116      and after it in the chain (which, by induction, will never be
3117      freed because if so, they would have already removed themselves
3118      from the chain). */
3119
3120   /* Put all unmarked strings on free list, free'ing the string chars
3121      of large unmarked strings */
3122   sweep_strings ();
3123
3124   /* Put all unmarked conses on free list */
3125   sweep_conses ();
3126
3127   /* Free all unmarked bit vectors */
3128   sweep_bit_vectors_1 (&all_bit_vectors,
3129                        &gc_count_num_bit_vector_used,
3130                        &gc_count_bit_vector_total_size,
3131                        &gc_count_bit_vector_storage);
3132
3133   /* Free all unmarked compiled-function objects */
3134   sweep_compiled_functions ();
3135
3136 #ifdef LISP_FLOAT_TYPE
3137   /* Put all unmarked floats on free list */
3138   sweep_floats ();
3139 #endif
3140
3141   /* Put all unmarked symbols on free list */
3142   sweep_symbols ();
3143
3144   /* Put all unmarked extents on free list */
3145   sweep_extents ();
3146
3147   /* Put all unmarked markers on free list.
3148      Dechain each one first from the buffer into which it points. */
3149   sweep_markers ();
3150
3151   sweep_events ();
3152
3153 #ifdef PDUMP
3154   pdump_objects_unmark ();
3155 #endif
3156 }
3157 \f
3158 /* Clearing for disksave. */
3159
3160 void
3161 disksave_object_finalization (void)
3162 {
3163   /* It's important that certain information from the environment not get
3164      dumped with the executable (pathnames, environment variables, etc.).
3165      To make it easier to tell when this has happened with strings(1) we
3166      clear some known-to-be-garbage blocks of memory, so that leftover
3167      results of old evaluation don't look like potential problems.
3168      But first we set some notable variables to nil and do one more GC,
3169      to turn those strings into garbage.
3170   */
3171
3172   /* Yeah, this list is pretty ad-hoc... */
3173   Vprocess_environment = Qnil;
3174   Vexec_directory = Qnil;
3175   Vdata_directory = Qnil;
3176   Vsite_directory = Qnil;
3177   Vdoc_directory = Qnil;
3178   Vconfigure_info_directory = Qnil;
3179   Vexec_path = Qnil;
3180   Vload_path = Qnil;
3181   /* Vdump_load_path = Qnil; */
3182   /* Release hash tables for locate_file */
3183   Flocate_file_clear_hashing (Qt);
3184   uncache_home_directory();
3185
3186 #if defined(LOADHIST) && !(defined(LOADHIST_DUMPED) || \
3187                            defined(LOADHIST_BUILTIN))
3188   Vload_history = Qnil;
3189 #endif
3190   Vshell_file_name = Qnil;
3191
3192   garbage_collect_1 ();
3193
3194   /* Run the disksave finalization methods of all live objects. */
3195   disksave_object_finalization_1 ();
3196
3197   /* Zero out the uninitialized (really, unused) part of the containers
3198      for the live strings. */
3199   {
3200     struct string_chars_block *scb;
3201     for (scb = first_string_chars_block; scb; scb = scb->next)
3202       {
3203         int count = sizeof (scb->string_chars) - scb->pos;
3204
3205         assert (count >= 0 && count < STRING_CHARS_BLOCK_SIZE);
3206         if (count != 0)
3207           {
3208             /* from the block's fill ptr to the end */
3209             memset ((scb->string_chars + scb->pos), 0, count);
3210           }
3211       }
3212   }
3213
3214   /* There, that ought to be enough... */
3215
3216 }
3217
3218 \f
3219 Lisp_Object
3220 restore_gc_inhibit (Lisp_Object val)
3221 {
3222   gc_currently_forbidden = XINT (val);
3223   return val;
3224 }
3225
3226 /* Maybe we want to use this when doing a "panic" gc after memory_full()? */
3227 static int gc_hooks_inhibited;
3228
3229 \f
3230 void
3231 garbage_collect_1 (void)
3232 {
3233 #if MAX_SAVE_STACK > 0
3234   char stack_top_variable;
3235   extern char *stack_bottom;
3236 #endif
3237   struct frame *f;
3238   int speccount;
3239   int cursor_changed;
3240   Lisp_Object pre_gc_cursor;
3241   struct gcpro gcpro1;
3242
3243   if (gc_in_progress
3244       || gc_currently_forbidden
3245       || in_display
3246       || preparing_for_armageddon)
3247     return;
3248
3249   /* We used to call selected_frame() here.
3250
3251      The following functions cannot be called inside GC
3252      so we move to after the above tests. */
3253   {
3254     Lisp_Object frame;
3255     Lisp_Object device = Fselected_device (Qnil);
3256     if (NILP (device)) /* Could happen during startup, eg. if always_gc */
3257       return;
3258     frame = DEVICE_SELECTED_FRAME (XDEVICE (device));
3259     if (NILP (frame))
3260       signal_simple_error ("No frames exist on device", device);
3261     f = XFRAME (frame);
3262   }
3263
3264   pre_gc_cursor = Qnil;
3265   cursor_changed = 0;
3266
3267   GCPRO1 (pre_gc_cursor);
3268
3269   /* Very important to prevent GC during any of the following
3270      stuff that might run Lisp code; otherwise, we'll likely
3271      have infinite GC recursion. */
3272   speccount = specpdl_depth ();
3273   record_unwind_protect (restore_gc_inhibit,
3274                          make_int (gc_currently_forbidden));
3275   gc_currently_forbidden = 1;
3276
3277   if (!gc_hooks_inhibited)
3278     run_hook_trapping_errors ("Error in pre-gc-hook", Qpre_gc_hook);
3279
3280   /* Now show the GC cursor/message. */
3281   if (!noninteractive)
3282     {
3283       if (FRAME_WIN_P (f))
3284         {
3285           Lisp_Object frame = make_frame (f);
3286           Lisp_Object cursor = glyph_image_instance (Vgc_pointer_glyph,
3287                                                      FRAME_SELECTED_WINDOW (f),
3288                                                      ERROR_ME_NOT, 1);
3289           pre_gc_cursor = f->pointer;
3290           if (POINTER_IMAGE_INSTANCEP (cursor)
3291               /* don't change if we don't know how to change back. */
3292               && POINTER_IMAGE_INSTANCEP (pre_gc_cursor))
3293             {
3294               cursor_changed = 1;
3295               Fset_frame_pointer (frame, cursor);
3296             }
3297         }
3298
3299       /* Don't print messages to the stream device. */
3300       if (!cursor_changed && !FRAME_STREAM_P (f))
3301         {
3302           char *msg = (STRINGP (Vgc_message)
3303                        ? GETTEXT ((char *) XSTRING_DATA (Vgc_message))
3304                        : 0);
3305           Lisp_Object args[2], whole_msg;
3306           args[0] = build_string (msg ? msg :
3307                                   GETTEXT ((const char *) gc_default_message));
3308           args[1] = build_string ("...");
3309           whole_msg = Fconcat (2, args);
3310           echo_area_message (f, (Bufbyte *) 0, whole_msg, 0, -1,
3311                              Qgarbage_collecting);
3312         }
3313     }
3314
3315   /***** Now we actually start the garbage collection. */
3316
3317   gc_in_progress = 1;
3318
3319   gc_generation_number[0]++;
3320
3321 #if MAX_SAVE_STACK > 0
3322
3323   /* Save a copy of the contents of the stack, for debugging.  */
3324   if (!purify_flag)
3325     {
3326       /* Static buffer in which we save a copy of the C stack at each GC.  */
3327       static char *stack_copy;
3328       static size_t stack_copy_size;
3329
3330       ptrdiff_t stack_diff = &stack_top_variable - stack_bottom;
3331       size_t stack_size = (stack_diff > 0 ? stack_diff : -stack_diff);
3332       if (stack_size < MAX_SAVE_STACK)
3333         {
3334           if (stack_copy_size < stack_size)
3335             {
3336               stack_copy = (char *) xrealloc (stack_copy, stack_size);
3337               stack_copy_size = stack_size;
3338             }
3339
3340           memcpy (stack_copy,
3341                   stack_diff > 0 ? stack_bottom : &stack_top_variable,
3342                   stack_size);
3343         }
3344     }
3345 #endif /* MAX_SAVE_STACK > 0 */
3346
3347   /* Do some totally ad-hoc resource clearing. */
3348   /* #### generalize this? */
3349   clear_event_resource ();
3350   cleanup_specifiers ();
3351
3352   /* Mark all the special slots that serve as the roots of accessibility. */
3353
3354   { /* staticpro() */
3355     Lisp_Object **p = Dynarr_begin (staticpros);
3356     size_t count;
3357     for (count = Dynarr_length (staticpros); count; count--)
3358       mark_object (**p++);
3359   }
3360
3361   { /* staticpro_nodump() */
3362     Lisp_Object **p = Dynarr_begin (staticpros_nodump);
3363     size_t count;
3364     for (count = Dynarr_length (staticpros_nodump); count; count--)
3365       mark_object (**p++);
3366   }
3367
3368   { /* GCPRO() */
3369     struct gcpro *tail;
3370     int i;
3371     for (tail = gcprolist; tail; tail = tail->next)
3372       for (i = 0; i < tail->nvars; i++)
3373         mark_object (tail->var[i]);
3374   }
3375
3376   { /* specbind() */
3377     struct specbinding *bind;
3378     for (bind = specpdl; bind != specpdl_ptr; bind++)
3379       {
3380         mark_object (bind->symbol);
3381         mark_object (bind->old_value);
3382       }
3383   }
3384
3385   {
3386     struct catchtag *catch;
3387     for (catch = catchlist; catch; catch = catch->next)
3388       {
3389         mark_object (catch->tag);
3390         mark_object (catch->val);
3391       }
3392   }
3393
3394   {
3395     struct backtrace *backlist;
3396     for (backlist = backtrace_list; backlist; backlist = backlist->next)
3397       {
3398         int nargs = backlist->nargs;
3399         int i;
3400
3401         mark_object (*backlist->function);
3402         if (nargs < 0 /* nargs == UNEVALLED || nargs == MANY */)
3403           mark_object (backlist->args[0]);
3404         else
3405           for (i = 0; i < nargs; i++)
3406             mark_object (backlist->args[i]);
3407       }
3408   }
3409
3410   mark_redisplay ();
3411   mark_profiling_info ();
3412
3413   /* OK, now do the after-mark stuff.  This is for things that
3414      are only marked when something else is marked (e.g. weak hash tables).
3415      There may be complex dependencies between such objects -- e.g.
3416      a weak hash table might be unmarked, but after processing a later
3417      weak hash table, the former one might get marked.  So we have to
3418      iterate until nothing more gets marked. */
3419
3420   while (finish_marking_weak_hash_tables () > 0 ||
3421          finish_marking_weak_lists       () > 0)
3422     ;
3423
3424   /* And prune (this needs to be called after everything else has been
3425      marked and before we do any sweeping). */
3426   /* #### this is somewhat ad-hoc and should probably be an object
3427      method */
3428   prune_weak_hash_tables ();
3429   prune_weak_lists ();
3430   prune_specifiers ();
3431   prune_syntax_tables ();
3432
3433   gc_sweep ();
3434
3435   consing_since_gc = 0;
3436 #ifndef DEBUG_XEMACS
3437   /* Allow you to set it really fucking low if you really want ... */
3438   if (gc_cons_threshold < 10000)
3439     gc_cons_threshold = 10000;
3440 #endif
3441
3442   gc_in_progress = 0;
3443
3444   /******* End of garbage collection ********/
3445
3446   run_hook_trapping_errors ("Error in post-gc-hook", Qpost_gc_hook);
3447
3448   /* Now remove the GC cursor/message */
3449   if (!noninteractive)
3450     {
3451       if (cursor_changed)
3452         Fset_frame_pointer (make_frame (f), pre_gc_cursor);
3453       else if (!FRAME_STREAM_P (f))
3454         {
3455           char *msg = (STRINGP (Vgc_message)
3456                        ? GETTEXT ((char *) XSTRING_DATA (Vgc_message))
3457                        : 0);
3458
3459           /* Show "...done" only if the echo area would otherwise be empty. */
3460           if (NILP (clear_echo_area (selected_frame (),
3461                                      Qgarbage_collecting, 0)))
3462             {
3463               Lisp_Object args[2], whole_msg;
3464               args[0] = build_string (msg ? msg :
3465                                       GETTEXT ((const char *)
3466                                                gc_default_message));
3467               args[1] = build_string ("... done");
3468               whole_msg = Fconcat (2, args);
3469               echo_area_message (selected_frame (), (Bufbyte *) 0,
3470                                  whole_msg, 0, -1,
3471                                  Qgarbage_collecting);
3472             }
3473         }
3474     }
3475
3476   /* now stop inhibiting GC */
3477   unbind_to (speccount, Qnil);
3478
3479   if (!breathing_space)
3480     {
3481       breathing_space = malloc (4096 - MALLOC_OVERHEAD);
3482     }
3483
3484   UNGCPRO;
3485   return;
3486 }
3487
3488 /* Debugging aids.  */
3489
3490 static Lisp_Object
3491 gc_plist_hack (const char *name, int value, Lisp_Object tail)
3492 {
3493   /* C doesn't have local functions (or closures, or GC, or readable syntax,
3494      or portable numeric datatypes, or bit-vectors, or characters, or
3495      arrays, or exceptions, or ...) */
3496   return cons3 (intern (name), make_int (value), tail);
3497 }
3498
3499 #define HACK_O_MATIC(type, name, pl) do {                               \
3500   int s = 0;                                                            \
3501   struct type##_block *x = current_##type##_block;                      \
3502   while (x) { s += sizeof (*x) + MALLOC_OVERHEAD; x = x->prev; }        \
3503   (pl) = gc_plist_hack ((name), s, (pl));                               \
3504 } while (0)
3505
3506 DEFUN ("garbage-collect", Fgarbage_collect, 0, 0, "", /*
3507 Reclaim storage for Lisp objects no longer needed.
3508 Return info on amount of space in use:
3509  ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)
3510   (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS
3511   PLIST)
3512   where `PLIST' is a list of alternating keyword/value pairs providing
3513   more detailed information.
3514 Garbage collection happens automatically if you cons more than
3515 `gc-cons-threshold' bytes of Lisp data since previous garbage collection.
3516 */
3517        ())
3518 {
3519   Lisp_Object pl = Qnil;
3520   int i;
3521   int gc_count_vector_total_size = 0;
3522
3523   garbage_collect_1 ();
3524
3525   for (i = 0; i < lrecord_type_count; i++)
3526     {
3527       if (lcrecord_stats[i].bytes_in_use != 0
3528           || lcrecord_stats[i].bytes_freed != 0
3529           || lcrecord_stats[i].instances_on_free_list != 0)
3530         {
3531           char buf [255];
3532           const char *name = lrecord_implementations_table[i]->name;
3533           int len = strlen (name);
3534           /* save this for the FSFmacs-compatible part of the summary */
3535           if (i == lrecord_vector.lrecord_type_index)
3536             gc_count_vector_total_size =
3537               lcrecord_stats[i].bytes_in_use + lcrecord_stats[i].bytes_freed;
3538
3539           sprintf (buf, "%s-storage", name);
3540           pl = gc_plist_hack (buf, lcrecord_stats[i].bytes_in_use, pl);
3541           /* Okay, simple pluralization check for `symbol-value-varalias' */
3542           if (name[len-1] == 's')
3543             sprintf (buf, "%ses-freed", name);
3544           else
3545             sprintf (buf, "%ss-freed", name);
3546           if (lcrecord_stats[i].instances_freed != 0)
3547             pl = gc_plist_hack (buf, lcrecord_stats[i].instances_freed, pl);
3548           if (name[len-1] == 's')
3549             sprintf (buf, "%ses-on-free-list", name);
3550           else
3551             sprintf (buf, "%ss-on-free-list", name);
3552           if (lcrecord_stats[i].instances_on_free_list != 0)
3553             pl = gc_plist_hack (buf, lcrecord_stats[i].instances_on_free_list,
3554                                 pl);
3555           if (name[len-1] == 's')
3556             sprintf (buf, "%ses-used", name);
3557           else
3558             sprintf (buf, "%ss-used", name);
3559           pl = gc_plist_hack (buf, lcrecord_stats[i].instances_in_use, pl);
3560         }
3561     }
3562
3563   HACK_O_MATIC (extent, "extent-storage", pl);
3564   pl = gc_plist_hack ("extents-free", gc_count_num_extent_freelist, pl);
3565   pl = gc_plist_hack ("extents-used", gc_count_num_extent_in_use, pl);
3566   HACK_O_MATIC (event, "event-storage", pl);
3567   pl = gc_plist_hack ("events-free", gc_count_num_event_freelist, pl);
3568   pl = gc_plist_hack ("events-used", gc_count_num_event_in_use, pl);
3569   HACK_O_MATIC (marker, "marker-storage", pl);
3570   pl = gc_plist_hack ("markers-free", gc_count_num_marker_freelist, pl);
3571   pl = gc_plist_hack ("markers-used", gc_count_num_marker_in_use, pl);
3572 #ifdef LISP_FLOAT_TYPE
3573   HACK_O_MATIC (float, "float-storage", pl);
3574   pl = gc_plist_hack ("floats-free", gc_count_num_float_freelist, pl);
3575   pl = gc_plist_hack ("floats-used", gc_count_num_float_in_use, pl);
3576 #endif /* LISP_FLOAT_TYPE */
3577   HACK_O_MATIC (string, "string-header-storage", pl);
3578   pl = gc_plist_hack ("long-strings-total-length",
3579                       gc_count_string_total_size
3580                       - gc_count_short_string_total_size, pl);
3581   HACK_O_MATIC (string_chars, "short-string-storage", pl);
3582   pl = gc_plist_hack ("short-strings-total-length",
3583                       gc_count_short_string_total_size, pl);
3584   pl = gc_plist_hack ("strings-free", gc_count_num_string_freelist, pl);
3585   pl = gc_plist_hack ("long-strings-used",
3586                       gc_count_num_string_in_use
3587                       - gc_count_num_short_string_in_use, pl);
3588   pl = gc_plist_hack ("short-strings-used",
3589                       gc_count_num_short_string_in_use, pl);
3590
3591   HACK_O_MATIC (compiled_function, "compiled-function-storage", pl);
3592   pl = gc_plist_hack ("compiled-functions-free",
3593                       gc_count_num_compiled_function_freelist, pl);
3594   pl = gc_plist_hack ("compiled-functions-used",
3595                       gc_count_num_compiled_function_in_use, pl);
3596
3597   pl = gc_plist_hack ("bit-vector-storage", gc_count_bit_vector_storage, pl);
3598   pl = gc_plist_hack ("bit-vectors-total-length",
3599                       gc_count_bit_vector_total_size, pl);
3600   pl = gc_plist_hack ("bit-vectors-used", gc_count_num_bit_vector_used, pl);
3601
3602   HACK_O_MATIC (symbol, "symbol-storage", pl);
3603   pl = gc_plist_hack ("symbols-free", gc_count_num_symbol_freelist, pl);
3604   pl = gc_plist_hack ("symbols-used", gc_count_num_symbol_in_use, pl);
3605
3606   HACK_O_MATIC (cons, "cons-storage", pl);
3607   pl = gc_plist_hack ("conses-free", gc_count_num_cons_freelist, pl);
3608   pl = gc_plist_hack ("conses-used", gc_count_num_cons_in_use, pl);
3609
3610   /* The things we do for backwards-compatibility */
3611   return
3612     list6 (Fcons (make_int (gc_count_num_cons_in_use),
3613                   make_int (gc_count_num_cons_freelist)),
3614            Fcons (make_int (gc_count_num_symbol_in_use),
3615                   make_int (gc_count_num_symbol_freelist)),
3616            Fcons (make_int (gc_count_num_marker_in_use),
3617                   make_int (gc_count_num_marker_freelist)),
3618            make_int (gc_count_string_total_size),
3619            make_int (gc_count_vector_total_size),
3620            pl);
3621 }
3622 #undef HACK_O_MATIC
3623
3624 DEFUN ("consing-since-gc", Fconsing_since_gc, 0, 0, "", /*
3625 Return the number of bytes consed since the last garbage collection.
3626 \"Consed\" is a misnomer in that this actually counts allocation
3627 of all different kinds of objects, not just conses.
3628
3629 If this value exceeds `gc-cons-threshold', a garbage collection happens.
3630 */
3631        ())
3632 {
3633   return make_int (consing_since_gc);
3634 }
3635
3636 #if 0
3637 DEFUN ("memory-limit", Fmemory_limit, 0, 0, 0, /*
3638 Return the address of the last byte Emacs has allocated, divided by 1024.
3639 This may be helpful in debugging Emacs's memory usage.
3640 The value is divided by 1024 to make sure it will fit in a lisp integer.
3641 */
3642        ())
3643 {
3644   return make_int ((EMACS_INT) sbrk (0) / 1024);
3645 }
3646 #endif
3647
3648 \f
3649 int
3650 object_dead_p (Lisp_Object obj)
3651 {
3652   return ((BUFFERP  (obj) && !BUFFER_LIVE_P  (XBUFFER  (obj))) ||
3653           (FRAMEP   (obj) && !FRAME_LIVE_P   (XFRAME   (obj))) ||
3654           (WINDOWP  (obj) && !WINDOW_LIVE_P  (XWINDOW  (obj))) ||
3655           (DEVICEP  (obj) && !DEVICE_LIVE_P  (XDEVICE  (obj))) ||
3656           (CONSOLEP (obj) && !CONSOLE_LIVE_P (XCONSOLE (obj))) ||
3657           (EVENTP   (obj) && !EVENT_LIVE_P   (XEVENT   (obj))) ||
3658           (EXTENTP  (obj) && !EXTENT_LIVE_P  (XEXTENT  (obj))));
3659 }
3660
3661 #ifdef MEMORY_USAGE_STATS
3662
3663 /* Attempt to determine the actual amount of space that is used for
3664    the block allocated starting at PTR, supposedly of size "CLAIMED_SIZE".
3665
3666    It seems that the following holds:
3667
3668    1. When using the old allocator (malloc.c):
3669
3670       -- blocks are always allocated in chunks of powers of two.  For
3671          each block, there is an overhead of 8 bytes if rcheck is not
3672          defined, 20 bytes if it is defined.  In other words, a
3673          one-byte allocation needs 8 bytes of overhead for a total of
3674          9 bytes, and needs to have 16 bytes of memory chunked out for
3675          it.
3676
3677    2. When using the new allocator (gmalloc.c):
3678
3679       -- blocks are always allocated in chunks of powers of two up
3680          to 4096 bytes.  Larger blocks are allocated in chunks of
3681          an integral multiple of 4096 bytes.  The minimum block
3682          size is 2*sizeof (void *), or 16 bytes if SUNOS_LOCALTIME_BUG
3683          is defined.  There is no per-block overhead, but there
3684          is an overhead of 3*sizeof (size_t) for each 4096 bytes
3685          allocated.
3686
3687     3. When using the system malloc, anything goes, but they are
3688        generally slower and more space-efficient than the GNU
3689        allocators.  One possibly reasonable assumption to make
3690        for want of better data is that sizeof (void *), or maybe
3691        2 * sizeof (void *), is required as overhead and that
3692        blocks are allocated in the minimum required size except
3693        that some minimum block size is imposed (e.g. 16 bytes). */
3694
3695 size_t
3696 malloced_storage_size (void *ptr, size_t claimed_size,
3697                        struct overhead_stats *stats)
3698 {
3699   size_t orig_claimed_size = claimed_size;
3700
3701 #ifdef GNU_MALLOC
3702
3703   if (claimed_size < 2 * sizeof (void *))
3704     claimed_size = 2 * sizeof (void *);
3705 # ifdef SUNOS_LOCALTIME_BUG
3706   if (claimed_size < 16)
3707     claimed_size = 16;
3708 # endif
3709   if (claimed_size < 4096)
3710     {
3711       int log = 1;
3712
3713       /* compute the log base two, more or less, then use it to compute
3714          the block size needed. */
3715       claimed_size--;
3716       /* It's big, it's heavy, it's wood! */
3717       while ((claimed_size /= 2) != 0)
3718         ++log;
3719       claimed_size = 1;
3720       /* It's better than bad, it's good! */
3721       while (log > 0)
3722         {
3723           claimed_size *= 2;
3724           log--;
3725         }
3726       /* We have to come up with some average about the amount of
3727          blocks used. */
3728       if ((size_t) (rand () & 4095) < claimed_size)
3729         claimed_size += 3 * sizeof (void *);
3730     }
3731   else
3732     {
3733       claimed_size += 4095;
3734       claimed_size &= ~4095;
3735       claimed_size += (claimed_size / 4096) * 3 * sizeof (size_t);
3736     }
3737
3738 #elif defined (SYSTEM_MALLOC)
3739
3740   if (claimed_size < 16)
3741     claimed_size = 16;
3742   claimed_size += 2 * sizeof (void *);
3743
3744 #else /* old GNU allocator */
3745
3746 # ifdef rcheck /* #### may not be defined here */
3747   claimed_size += 20;
3748 # else
3749   claimed_size += 8;
3750 # endif
3751   {
3752     int log = 1;
3753
3754     /* compute the log base two, more or less, then use it to compute
3755        the block size needed. */
3756     claimed_size--;
3757     /* It's big, it's heavy, it's wood! */
3758     while ((claimed_size /= 2) != 0)
3759       ++log;
3760     claimed_size = 1;
3761     /* It's better than bad, it's good! */
3762     while (log > 0)
3763       {
3764         claimed_size *= 2;
3765         log--;
3766       }
3767   }
3768
3769 #endif /* old GNU allocator */
3770
3771   if (stats)
3772     {
3773       stats->was_requested += orig_claimed_size;
3774       stats->malloc_overhead += claimed_size - orig_claimed_size;
3775     }
3776   return claimed_size;
3777 }
3778
3779 size_t
3780 fixed_type_block_overhead (size_t size)
3781 {
3782   size_t per_block = TYPE_ALLOC_SIZE (cons, unsigned char);
3783   size_t overhead = 0;
3784   size_t storage_size = malloced_storage_size (0, per_block, 0);
3785   while (size >= per_block)
3786     {
3787       size -= per_block;
3788       overhead += sizeof (void *) + per_block - storage_size;
3789     }
3790   if (rand () % per_block < size)
3791     overhead += sizeof (void *) + per_block - storage_size;
3792   return overhead;
3793 }
3794
3795 #endif /* MEMORY_USAGE_STATS */
3796
3797 \f
3798 /* Initialization */
3799 void
3800 reinit_alloc_once_early (void)
3801 {
3802   gc_generation_number[0] = 0;
3803   breathing_space = 0;
3804   XSETINT (all_bit_vectors, 0); /* Qzero may not be set yet. */
3805   XSETINT (Vgc_message, 0);
3806   all_lcrecords = 0;
3807   ignore_malloc_warnings = 1;
3808 #ifdef DOUG_LEA_MALLOC
3809   mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */
3810   mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */
3811 #if 0 /* Moved to emacs.c */
3812   mallopt (M_MMAP_MAX, 64); /* max. number of mmap'ed areas */
3813 #endif
3814 #endif
3815   init_string_alloc ();
3816   init_string_chars_alloc ();
3817   init_cons_alloc ();
3818   init_symbol_alloc ();
3819   init_compiled_function_alloc ();
3820 #ifdef LISP_FLOAT_TYPE
3821   init_float_alloc ();
3822 #endif /* LISP_FLOAT_TYPE */
3823   init_marker_alloc ();
3824   init_extent_alloc ();
3825   init_event_alloc ();
3826
3827   ignore_malloc_warnings = 0;
3828
3829   if (staticpros_nodump)
3830     Dynarr_free (staticpros_nodump);
3831   staticpros_nodump = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *);
3832   Dynarr_resize (staticpros_nodump, 100); /* merely a small optimization */
3833
3834   consing_since_gc = 0;
3835 #if 1
3836   gc_cons_threshold = 500000; /* XEmacs change */
3837 #else
3838   gc_cons_threshold = 15000; /* debugging */
3839 #endif
3840   lrecord_uid_counter = 259;
3841   debug_string_purity = 0;
3842   gcprolist = 0;
3843
3844   gc_currently_forbidden = 0;
3845   gc_hooks_inhibited = 0;
3846
3847 #ifdef ERROR_CHECK_TYPECHECK
3848   ERROR_ME.really_unlikely_name_to_have_accidentally_in_a_non_errb_structure =
3849     666;
3850   ERROR_ME_NOT.
3851     really_unlikely_name_to_have_accidentally_in_a_non_errb_structure = 42;
3852   ERROR_ME_WARN.
3853     really_unlikely_name_to_have_accidentally_in_a_non_errb_structure =
3854       3333632;
3855 #endif /* ERROR_CHECK_TYPECHECK */
3856 }
3857
3858 void
3859 init_alloc_once_early (void)
3860 {
3861   reinit_alloc_once_early ();
3862
3863   {
3864     int i;
3865     for (i = 0; i < countof (lrecord_implementations_table); i++)
3866       lrecord_implementations_table[i] = 0;
3867   }
3868
3869   INIT_LRECORD_IMPLEMENTATION (cons);
3870   INIT_LRECORD_IMPLEMENTATION (vector);
3871   INIT_LRECORD_IMPLEMENTATION (string);
3872   INIT_LRECORD_IMPLEMENTATION (lcrecord_list);
3873
3874   staticpros = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *);
3875   Dynarr_resize (staticpros, 1410); /* merely a small optimization */
3876   dump_add_root_struct_ptr (&staticpros, &staticpros_description);
3877 }
3878
3879 void
3880 reinit_alloc (void)
3881 {
3882   gcprolist = 0;
3883 }
3884
3885 void
3886 syms_of_alloc (void)
3887 {
3888   DEFSYMBOL (Qpre_gc_hook);
3889   DEFSYMBOL (Qpost_gc_hook);
3890   DEFSYMBOL (Qgarbage_collecting);
3891
3892   DEFSUBR (Fcons);
3893   DEFSUBR (Flist);
3894   DEFSUBR (Fvector);
3895   DEFSUBR (Fbit_vector);
3896   DEFSUBR (Fmake_byte_code);
3897   DEFSUBR (Fmake_list);
3898   DEFSUBR (Fmake_vector);
3899   DEFSUBR (Fmake_bit_vector);
3900   DEFSUBR (Fmake_string);
3901   DEFSUBR (Fstring);
3902   DEFSUBR (Fmake_symbol);
3903   DEFSUBR (Fmake_marker);
3904   DEFSUBR (Fpurecopy);
3905   DEFSUBR (Fgarbage_collect);
3906 #if 0
3907   DEFSUBR (Fmemory_limit);
3908 #endif
3909   DEFSUBR (Fconsing_since_gc);
3910 }
3911
3912 void
3913 vars_of_alloc (void)
3914 {
3915   DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold /*
3916 *Number of bytes of consing between garbage collections.
3917 \"Consing\" is a misnomer in that this actually counts allocation
3918 of all different kinds of objects, not just conses.
3919 Garbage collection can happen automatically once this many bytes have been
3920 allocated since the last garbage collection.  All data types count.
3921
3922 Garbage collection happens automatically when `eval' or `funcall' are
3923 called.  (Note that `funcall' is called implicitly as part of evaluation.)
3924 By binding this temporarily to a large number, you can effectively
3925 prevent garbage collection during a part of the program.
3926
3927 See also `consing-since-gc'.
3928 */ );
3929
3930 #ifdef DEBUG_XEMACS
3931   DEFVAR_INT ("debug-allocation", &debug_allocation /*
3932 If non-zero, print out information to stderr about all objects allocated.
3933 See also `debug-allocation-backtrace-length'.
3934 */ );
3935   debug_allocation = 0;
3936
3937   DEFVAR_INT ("debug-allocation-backtrace-length",
3938               &debug_allocation_backtrace_length /*
3939 Length (in stack frames) of short backtrace printed out by `debug-allocation'.
3940 */ );
3941   debug_allocation_backtrace_length = 2;
3942 #endif
3943
3944   DEFVAR_BOOL ("purify-flag", &purify_flag /*
3945 Non-nil means loading Lisp code in order to dump an executable.
3946 This means that certain objects should be allocated in readonly space.
3947 */ );
3948
3949   DEFVAR_LISP ("pre-gc-hook", &Vpre_gc_hook /*
3950 Function or functions to be run just before each garbage collection.
3951 Interrupts, garbage collection, and errors are inhibited while this hook
3952 runs, so be extremely careful in what you add here.  In particular, avoid
3953 consing, and do not interact with the user.
3954 */ );
3955   Vpre_gc_hook = Qnil;
3956
3957   DEFVAR_LISP ("post-gc-hook", &Vpost_gc_hook /*
3958 Function or functions to be run just after each garbage collection.
3959 Interrupts, garbage collection, and errors are inhibited while this hook
3960 runs, so be extremely careful in what you add here.  In particular, avoid
3961 consing, and do not interact with the user.
3962 */ );
3963   Vpost_gc_hook = Qnil;
3964
3965   DEFVAR_LISP ("gc-message", &Vgc_message /*
3966 String to print to indicate that a garbage collection is in progress.
3967 This is printed in the echo area.  If the selected frame is on a
3968 window system and `gc-pointer-glyph' specifies a value (i.e. a pointer
3969 image instance) in the domain of the selected frame, the mouse pointer
3970 will change instead of this message being printed.
3971 */ );
3972   Vgc_message = build_string (gc_default_message);
3973
3974   DEFVAR_LISP ("gc-pointer-glyph", &Vgc_pointer_glyph /*
3975 Pointer glyph used to indicate that a garbage collection is in progress.
3976 If the selected window is on a window system and this glyph specifies a
3977 value (i.e. a pointer image instance) in the domain of the selected
3978 window, the pointer will be changed as specified during garbage collection.
3979 Otherwise, a message will be printed in the echo area, as controlled
3980 by `gc-message'.
3981 */ );
3982 }
3983
3984 void
3985 complex_vars_of_alloc (void)
3986 {
3987   Vgc_pointer_glyph = Fmake_glyph_internal (Qpointer);
3988 }