(long_to_cons): Fix type of top.
[emacs.git] / src / alloc.c
blobd703d3d99b267b5cad51b5cb0f5e9b5981561a8c
1 /* Storage allocation and gc for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 86, 88, 93, 94, 95, 97, 98, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include <stdio.h>
25 #ifdef ALLOC_DEBUG
26 #undef INLINE
27 #endif
29 /* Note that this declares bzero on OSF/1. How dumb. */
31 #include <signal.h>
33 /* GC_MALLOC_CHECK defined means perform validity checks of malloc'd
34 memory. Can do this only if using gmalloc.c. */
36 #if defined SYSTEM_MALLOC || defined DOUG_LEA_MALLOC
37 #undef GC_MALLOC_CHECK
38 #endif
40 /* This file is part of the core Lisp implementation, and thus must
41 deal with the real data structures. If the Lisp implementation is
42 replaced, this file likely will not be used. */
44 #undef HIDE_LISP_IMPLEMENTATION
45 #include "lisp.h"
46 #include "process.h"
47 #include "intervals.h"
48 #include "puresize.h"
49 #include "buffer.h"
50 #include "window.h"
51 #include "keyboard.h"
52 #include "frame.h"
53 #include "blockinput.h"
54 #include "charset.h"
55 #include "syssignal.h"
56 #include <setjmp.h>
58 #ifdef HAVE_UNISTD_H
59 #include <unistd.h>
60 #else
61 extern POINTER_TYPE *sbrk ();
62 #endif
64 #ifdef DOUG_LEA_MALLOC
66 #include <malloc.h>
67 /* malloc.h #defines this as size_t, at least in glibc2. */
68 #ifndef __malloc_size_t
69 #define __malloc_size_t int
70 #endif
72 /* Specify maximum number of areas to mmap. It would be nice to use a
73 value that explicitly means "no limit". */
75 #define MMAP_MAX_AREAS 100000000
77 #else /* not DOUG_LEA_MALLOC */
79 /* The following come from gmalloc.c. */
81 #define __malloc_size_t size_t
82 extern __malloc_size_t _bytes_used;
83 extern __malloc_size_t __malloc_extra_blocks;
85 #endif /* not DOUG_LEA_MALLOC */
87 /* Macro to verify that storage intended for Lisp objects is not
88 out of range to fit in the space for a pointer.
89 ADDRESS is the start of the block, and SIZE
90 is the amount of space within which objects can start. */
92 #define VALIDATE_LISP_STORAGE(address, size) \
93 do \
94 { \
95 Lisp_Object val; \
96 XSETCONS (val, (char *) address + size); \
97 if ((char *) XCONS (val) != (char *) address + size) \
98 { \
99 xfree (address); \
100 memory_full (); \
102 } while (0)
104 /* Value of _bytes_used, when spare_memory was freed. */
106 static __malloc_size_t bytes_used_when_full;
108 /* Mark, unmark, query mark bit of a Lisp string. S must be a pointer
109 to a struct Lisp_String. */
111 #define MARK_STRING(S) ((S)->size |= MARKBIT)
112 #define UNMARK_STRING(S) ((S)->size &= ~MARKBIT)
113 #define STRING_MARKED_P(S) ((S)->size & MARKBIT)
115 /* Value is the number of bytes/chars of S, a pointer to a struct
116 Lisp_String. This must be used instead of STRING_BYTES (S) or
117 S->size during GC, because S->size contains the mark bit for
118 strings. */
120 #define GC_STRING_BYTES(S) (STRING_BYTES (S) & ~MARKBIT)
121 #define GC_STRING_CHARS(S) ((S)->size & ~MARKBIT)
123 /* Number of bytes of consing done since the last gc. */
125 int consing_since_gc;
127 /* Count the amount of consing of various sorts of space. */
129 EMACS_INT cons_cells_consed;
130 EMACS_INT floats_consed;
131 EMACS_INT vector_cells_consed;
132 EMACS_INT symbols_consed;
133 EMACS_INT string_chars_consed;
134 EMACS_INT misc_objects_consed;
135 EMACS_INT intervals_consed;
136 EMACS_INT strings_consed;
138 /* Number of bytes of consing since GC before another GC should be done. */
140 EMACS_INT gc_cons_threshold;
142 /* Nonzero during GC. */
144 int gc_in_progress;
146 /* Nonzero means display messages at beginning and end of GC. */
148 int garbage_collection_messages;
150 #ifndef VIRT_ADDR_VARIES
151 extern
152 #endif /* VIRT_ADDR_VARIES */
153 int malloc_sbrk_used;
155 #ifndef VIRT_ADDR_VARIES
156 extern
157 #endif /* VIRT_ADDR_VARIES */
158 int malloc_sbrk_unused;
160 /* Two limits controlling how much undo information to keep. */
162 EMACS_INT undo_limit;
163 EMACS_INT undo_strong_limit;
165 /* Number of live and free conses etc. */
167 static int total_conses, total_markers, total_symbols, total_vector_size;
168 static int total_free_conses, total_free_markers, total_free_symbols;
169 static int total_free_floats, total_floats;
171 /* Points to memory space allocated as "spare", to be freed if we run
172 out of memory. */
174 static char *spare_memory;
176 /* Amount of spare memory to keep in reserve. */
178 #define SPARE_MEMORY (1 << 14)
180 /* Number of extra blocks malloc should get when it needs more core. */
182 static int malloc_hysteresis;
184 /* Non-nil means defun should do purecopy on the function definition. */
186 Lisp_Object Vpurify_flag;
188 /* Non-nil means we are handling a memory-full error. */
190 Lisp_Object Vmemory_full;
192 #ifndef HAVE_SHM
194 /* Force it into data space! */
196 EMACS_INT pure[PURESIZE / sizeof (EMACS_INT)] = {0,};
197 #define PUREBEG (char *) pure
199 #else /* HAVE_SHM */
201 #define pure PURE_SEG_BITS /* Use shared memory segment */
202 #define PUREBEG (char *)PURE_SEG_BITS
204 #endif /* HAVE_SHM */
206 /* Pointer to the pure area, and its size. */
208 static char *purebeg;
209 static size_t pure_size;
211 /* Number of bytes of pure storage used before pure storage overflowed.
212 If this is non-zero, this implies that an overflow occurred. */
214 static size_t pure_bytes_used_before_overflow;
216 /* Value is non-zero if P points into pure space. */
218 #define PURE_POINTER_P(P) \
219 (((PNTR_COMPARISON_TYPE) (P) \
220 < (PNTR_COMPARISON_TYPE) ((char *) purebeg + pure_size)) \
221 && ((PNTR_COMPARISON_TYPE) (P) \
222 >= (PNTR_COMPARISON_TYPE) purebeg))
224 /* Index in pure at which next pure object will be allocated.. */
226 EMACS_INT pure_bytes_used;
228 /* If nonzero, this is a warning delivered by malloc and not yet
229 displayed. */
231 char *pending_malloc_warning;
233 /* Pre-computed signal argument for use when memory is exhausted. */
235 Lisp_Object Vmemory_signal_data;
237 /* Maximum amount of C stack to save when a GC happens. */
239 #ifndef MAX_SAVE_STACK
240 #define MAX_SAVE_STACK 16000
241 #endif
243 /* Buffer in which we save a copy of the C stack at each GC. */
245 char *stack_copy;
246 int stack_copy_size;
248 /* Non-zero means ignore malloc warnings. Set during initialization.
249 Currently not used. */
251 int ignore_warnings;
253 Lisp_Object Qgc_cons_threshold, Qchar_table_extra_slots;
255 /* Hook run after GC has finished. */
257 Lisp_Object Vpost_gc_hook, Qpost_gc_hook;
259 Lisp_Object Vgc_elapsed; /* accumulated elapsed time in GC */
260 EMACS_INT gcs_done; /* accumulated GCs */
262 static void mark_buffer P_ ((Lisp_Object));
263 static void mark_kboards P_ ((void));
264 static void gc_sweep P_ ((void));
265 static void mark_glyph_matrix P_ ((struct glyph_matrix *));
266 static void mark_face_cache P_ ((struct face_cache *));
268 #ifdef HAVE_WINDOW_SYSTEM
269 static void mark_image P_ ((struct image *));
270 static void mark_image_cache P_ ((struct frame *));
271 #endif /* HAVE_WINDOW_SYSTEM */
273 static struct Lisp_String *allocate_string P_ ((void));
274 static void compact_small_strings P_ ((void));
275 static void free_large_strings P_ ((void));
276 static void sweep_strings P_ ((void));
278 extern int message_enable_multibyte;
280 /* When scanning the C stack for live Lisp objects, Emacs keeps track
281 of what memory allocated via lisp_malloc is intended for what
282 purpose. This enumeration specifies the type of memory. */
284 enum mem_type
286 MEM_TYPE_NON_LISP,
287 MEM_TYPE_BUFFER,
288 MEM_TYPE_CONS,
289 MEM_TYPE_STRING,
290 MEM_TYPE_MISC,
291 MEM_TYPE_SYMBOL,
292 MEM_TYPE_FLOAT,
293 /* Keep the following vector-like types together, with
294 MEM_TYPE_WINDOW being the last, and MEM_TYPE_VECTOR the
295 first. Or change the code of live_vector_p, for instance. */
296 MEM_TYPE_VECTOR,
297 MEM_TYPE_PROCESS,
298 MEM_TYPE_HASH_TABLE,
299 MEM_TYPE_FRAME,
300 MEM_TYPE_WINDOW
303 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
305 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
306 #include <stdio.h> /* For fprintf. */
307 #endif
309 /* A unique object in pure space used to make some Lisp objects
310 on free lists recognizable in O(1). */
312 Lisp_Object Vdead;
314 #ifdef GC_MALLOC_CHECK
316 enum mem_type allocated_mem_type;
317 int dont_register_blocks;
319 #endif /* GC_MALLOC_CHECK */
321 /* A node in the red-black tree describing allocated memory containing
322 Lisp data. Each such block is recorded with its start and end
323 address when it is allocated, and removed from the tree when it
324 is freed.
326 A red-black tree is a balanced binary tree with the following
327 properties:
329 1. Every node is either red or black.
330 2. Every leaf is black.
331 3. If a node is red, then both of its children are black.
332 4. Every simple path from a node to a descendant leaf contains
333 the same number of black nodes.
334 5. The root is always black.
336 When nodes are inserted into the tree, or deleted from the tree,
337 the tree is "fixed" so that these properties are always true.
339 A red-black tree with N internal nodes has height at most 2
340 log(N+1). Searches, insertions and deletions are done in O(log N).
341 Please see a text book about data structures for a detailed
342 description of red-black trees. Any book worth its salt should
343 describe them. */
345 struct mem_node
347 /* Children of this node. These pointers are never NULL. When there
348 is no child, the value is MEM_NIL, which points to a dummy node. */
349 struct mem_node *left, *right;
351 /* The parent of this node. In the root node, this is NULL. */
352 struct mem_node *parent;
354 /* Start and end of allocated region. */
355 void *start, *end;
357 /* Node color. */
358 enum {MEM_BLACK, MEM_RED} color;
360 /* Memory type. */
361 enum mem_type type;
364 /* Base address of stack. Set in main. */
366 Lisp_Object *stack_base;
368 /* Root of the tree describing allocated Lisp memory. */
370 static struct mem_node *mem_root;
372 /* Lowest and highest known address in the heap. */
374 static void *min_heap_address, *max_heap_address;
376 /* Sentinel node of the tree. */
378 static struct mem_node mem_z;
379 #define MEM_NIL &mem_z
381 static POINTER_TYPE *lisp_malloc P_ ((size_t, enum mem_type));
382 static struct Lisp_Vector *allocate_vectorlike P_ ((EMACS_INT, enum mem_type));
383 static void lisp_free P_ ((POINTER_TYPE *));
384 static void mark_stack P_ ((void));
385 static int live_vector_p P_ ((struct mem_node *, void *));
386 static int live_buffer_p P_ ((struct mem_node *, void *));
387 static int live_string_p P_ ((struct mem_node *, void *));
388 static int live_cons_p P_ ((struct mem_node *, void *));
389 static int live_symbol_p P_ ((struct mem_node *, void *));
390 static int live_float_p P_ ((struct mem_node *, void *));
391 static int live_misc_p P_ ((struct mem_node *, void *));
392 static void mark_maybe_object P_ ((Lisp_Object));
393 static void mark_memory P_ ((void *, void *));
394 static void mem_init P_ ((void));
395 static struct mem_node *mem_insert P_ ((void *, void *, enum mem_type));
396 static void mem_insert_fixup P_ ((struct mem_node *));
397 static void mem_rotate_left P_ ((struct mem_node *));
398 static void mem_rotate_right P_ ((struct mem_node *));
399 static void mem_delete P_ ((struct mem_node *));
400 static void mem_delete_fixup P_ ((struct mem_node *));
401 static INLINE struct mem_node *mem_find P_ ((void *));
403 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
404 static void check_gcpros P_ ((void));
405 #endif
407 #endif /* GC_MARK_STACK || GC_MALLOC_CHECK */
409 /* Recording what needs to be marked for gc. */
411 struct gcpro *gcprolist;
413 /* Addresses of staticpro'd variables. */
415 #define NSTATICS 1280
416 Lisp_Object *staticvec[NSTATICS] = {0};
418 /* Index of next unused slot in staticvec. */
420 int staticidx = 0;
422 static POINTER_TYPE *pure_alloc P_ ((size_t, int));
425 /* Value is SZ rounded up to the next multiple of ALIGNMENT.
426 ALIGNMENT must be a power of 2. */
428 #define ALIGN(SZ, ALIGNMENT) \
429 (((SZ) + (ALIGNMENT) - 1) & ~((ALIGNMENT) - 1))
433 /************************************************************************
434 Malloc
435 ************************************************************************/
437 /* Function malloc calls this if it finds we are near exhausting storage. */
439 void
440 malloc_warning (str)
441 char *str;
443 pending_malloc_warning = str;
447 /* Display an already-pending malloc warning. */
449 void
450 display_malloc_warning ()
452 call3 (intern ("display-warning"),
453 intern ("alloc"),
454 build_string (pending_malloc_warning),
455 intern ("emergency"));
456 pending_malloc_warning = 0;
460 #ifdef DOUG_LEA_MALLOC
461 # define BYTES_USED (mallinfo ().arena)
462 #else
463 # define BYTES_USED _bytes_used
464 #endif
467 /* Called if malloc returns zero. */
469 void
470 memory_full ()
472 Vmemory_full = Qt;
474 #ifndef SYSTEM_MALLOC
475 bytes_used_when_full = BYTES_USED;
476 #endif
478 /* The first time we get here, free the spare memory. */
479 if (spare_memory)
481 free (spare_memory);
482 spare_memory = 0;
485 /* This used to call error, but if we've run out of memory, we could
486 get infinite recursion trying to build the string. */
487 while (1)
488 Fsignal (Qnil, Vmemory_signal_data);
492 /* Called if we can't allocate relocatable space for a buffer. */
494 void
495 buffer_memory_full ()
497 /* If buffers use the relocating allocator, no need to free
498 spare_memory, because we may have plenty of malloc space left
499 that we could get, and if we don't, the malloc that fails will
500 itself cause spare_memory to be freed. If buffers don't use the
501 relocating allocator, treat this like any other failing
502 malloc. */
504 #ifndef REL_ALLOC
505 memory_full ();
506 #endif
508 Vmemory_full = Qt;
510 /* This used to call error, but if we've run out of memory, we could
511 get infinite recursion trying to build the string. */
512 while (1)
513 Fsignal (Qnil, Vmemory_signal_data);
517 /* Like malloc but check for no memory and block interrupt input.. */
519 POINTER_TYPE *
520 xmalloc (size)
521 size_t size;
523 register POINTER_TYPE *val;
525 BLOCK_INPUT;
526 val = (POINTER_TYPE *) malloc (size);
527 UNBLOCK_INPUT;
529 if (!val && size)
530 memory_full ();
531 return val;
535 /* Like realloc but check for no memory and block interrupt input.. */
537 POINTER_TYPE *
538 xrealloc (block, size)
539 POINTER_TYPE *block;
540 size_t size;
542 register POINTER_TYPE *val;
544 BLOCK_INPUT;
545 /* We must call malloc explicitly when BLOCK is 0, since some
546 reallocs don't do this. */
547 if (! block)
548 val = (POINTER_TYPE *) malloc (size);
549 else
550 val = (POINTER_TYPE *) realloc (block, size);
551 UNBLOCK_INPUT;
553 if (!val && size) memory_full ();
554 return val;
558 /* Like free but block interrupt input.. */
560 void
561 xfree (block)
562 POINTER_TYPE *block;
564 BLOCK_INPUT;
565 free (block);
566 UNBLOCK_INPUT;
570 /* Like strdup, but uses xmalloc. */
572 char *
573 xstrdup (s)
574 const char *s;
576 size_t len = strlen (s) + 1;
577 char *p = (char *) xmalloc (len);
578 bcopy (s, p, len);
579 return p;
583 /* Like malloc but used for allocating Lisp data. NBYTES is the
584 number of bytes to allocate, TYPE describes the intended use of the
585 allcated memory block (for strings, for conses, ...). */
587 static POINTER_TYPE *
588 lisp_malloc (nbytes, type)
589 size_t nbytes;
590 enum mem_type type;
592 register void *val;
594 BLOCK_INPUT;
596 #ifdef GC_MALLOC_CHECK
597 allocated_mem_type = type;
598 #endif
600 val = (void *) malloc (nbytes);
602 #if GC_MARK_STACK && !defined GC_MALLOC_CHECK
603 if (val && type != MEM_TYPE_NON_LISP)
604 mem_insert (val, (char *) val + nbytes, type);
605 #endif
607 UNBLOCK_INPUT;
608 if (!val && nbytes)
609 memory_full ();
610 return val;
614 /* Return a new buffer structure allocated from the heap with
615 a call to lisp_malloc. */
617 struct buffer *
618 allocate_buffer ()
620 struct buffer *b
621 = (struct buffer *) lisp_malloc (sizeof (struct buffer),
622 MEM_TYPE_BUFFER);
623 VALIDATE_LISP_STORAGE (b, sizeof *b);
624 return b;
628 /* Free BLOCK. This must be called to free memory allocated with a
629 call to lisp_malloc. */
631 static void
632 lisp_free (block)
633 POINTER_TYPE *block;
635 BLOCK_INPUT;
636 free (block);
637 #if GC_MARK_STACK && !defined GC_MALLOC_CHECK
638 mem_delete (mem_find (block));
639 #endif
640 UNBLOCK_INPUT;
644 /* Arranging to disable input signals while we're in malloc.
646 This only works with GNU malloc. To help out systems which can't
647 use GNU malloc, all the calls to malloc, realloc, and free
648 elsewhere in the code should be inside a BLOCK_INPUT/UNBLOCK_INPUT
649 pairs; unfortunately, we have no idea what C library functions
650 might call malloc, so we can't really protect them unless you're
651 using GNU malloc. Fortunately, most of the major operating systems
652 can use GNU malloc. */
654 #ifndef SYSTEM_MALLOC
655 #ifndef DOUG_LEA_MALLOC
656 extern void * (*__malloc_hook) P_ ((size_t));
657 extern void * (*__realloc_hook) P_ ((void *, size_t));
658 extern void (*__free_hook) P_ ((void *));
659 /* Else declared in malloc.h, perhaps with an extra arg. */
660 #endif /* DOUG_LEA_MALLOC */
661 static void * (*old_malloc_hook) ();
662 static void * (*old_realloc_hook) ();
663 static void (*old_free_hook) ();
665 /* This function is used as the hook for free to call. */
667 static void
668 emacs_blocked_free (ptr)
669 void *ptr;
671 BLOCK_INPUT;
673 #ifdef GC_MALLOC_CHECK
674 if (ptr)
676 struct mem_node *m;
678 m = mem_find (ptr);
679 if (m == MEM_NIL || m->start != ptr)
681 fprintf (stderr,
682 "Freeing `%p' which wasn't allocated with malloc\n", ptr);
683 abort ();
685 else
687 /* fprintf (stderr, "free %p...%p (%p)\n", m->start, m->end, ptr); */
688 mem_delete (m);
691 #endif /* GC_MALLOC_CHECK */
693 __free_hook = old_free_hook;
694 free (ptr);
696 /* If we released our reserve (due to running out of memory),
697 and we have a fair amount free once again,
698 try to set aside another reserve in case we run out once more. */
699 if (spare_memory == 0
700 /* Verify there is enough space that even with the malloc
701 hysteresis this call won't run out again.
702 The code here is correct as long as SPARE_MEMORY
703 is substantially larger than the block size malloc uses. */
704 && (bytes_used_when_full
705 > BYTES_USED + max (malloc_hysteresis, 4) * SPARE_MEMORY))
706 spare_memory = (char *) malloc ((size_t) SPARE_MEMORY);
708 __free_hook = emacs_blocked_free;
709 UNBLOCK_INPUT;
713 /* If we released our reserve (due to running out of memory),
714 and we have a fair amount free once again,
715 try to set aside another reserve in case we run out once more.
717 This is called when a relocatable block is freed in ralloc.c. */
719 void
720 refill_memory_reserve ()
722 if (spare_memory == 0)
723 spare_memory = (char *) malloc ((size_t) SPARE_MEMORY);
727 /* This function is the malloc hook that Emacs uses. */
729 static void *
730 emacs_blocked_malloc (size)
731 size_t size;
733 void *value;
735 BLOCK_INPUT;
736 __malloc_hook = old_malloc_hook;
737 #ifdef DOUG_LEA_MALLOC
738 mallopt (M_TOP_PAD, malloc_hysteresis * 4096);
739 #else
740 __malloc_extra_blocks = malloc_hysteresis;
741 #endif
743 value = (void *) malloc (size);
745 #ifdef GC_MALLOC_CHECK
747 struct mem_node *m = mem_find (value);
748 if (m != MEM_NIL)
750 fprintf (stderr, "Malloc returned %p which is already in use\n",
751 value);
752 fprintf (stderr, "Region in use is %p...%p, %u bytes, type %d\n",
753 m->start, m->end, (char *) m->end - (char *) m->start,
754 m->type);
755 abort ();
758 if (!dont_register_blocks)
760 mem_insert (value, (char *) value + max (1, size), allocated_mem_type);
761 allocated_mem_type = MEM_TYPE_NON_LISP;
764 #endif /* GC_MALLOC_CHECK */
766 __malloc_hook = emacs_blocked_malloc;
767 UNBLOCK_INPUT;
769 /* fprintf (stderr, "%p malloc\n", value); */
770 return value;
774 /* This function is the realloc hook that Emacs uses. */
776 static void *
777 emacs_blocked_realloc (ptr, size)
778 void *ptr;
779 size_t size;
781 void *value;
783 BLOCK_INPUT;
784 __realloc_hook = old_realloc_hook;
786 #ifdef GC_MALLOC_CHECK
787 if (ptr)
789 struct mem_node *m = mem_find (ptr);
790 if (m == MEM_NIL || m->start != ptr)
792 fprintf (stderr,
793 "Realloc of %p which wasn't allocated with malloc\n",
794 ptr);
795 abort ();
798 mem_delete (m);
801 /* fprintf (stderr, "%p -> realloc\n", ptr); */
803 /* Prevent malloc from registering blocks. */
804 dont_register_blocks = 1;
805 #endif /* GC_MALLOC_CHECK */
807 value = (void *) realloc (ptr, size);
809 #ifdef GC_MALLOC_CHECK
810 dont_register_blocks = 0;
813 struct mem_node *m = mem_find (value);
814 if (m != MEM_NIL)
816 fprintf (stderr, "Realloc returns memory that is already in use\n");
817 abort ();
820 /* Can't handle zero size regions in the red-black tree. */
821 mem_insert (value, (char *) value + max (size, 1), MEM_TYPE_NON_LISP);
824 /* fprintf (stderr, "%p <- realloc\n", value); */
825 #endif /* GC_MALLOC_CHECK */
827 __realloc_hook = emacs_blocked_realloc;
828 UNBLOCK_INPUT;
830 return value;
834 /* Called from main to set up malloc to use our hooks. */
836 void
837 uninterrupt_malloc ()
839 if (__free_hook != emacs_blocked_free)
840 old_free_hook = __free_hook;
841 __free_hook = emacs_blocked_free;
843 if (__malloc_hook != emacs_blocked_malloc)
844 old_malloc_hook = __malloc_hook;
845 __malloc_hook = emacs_blocked_malloc;
847 if (__realloc_hook != emacs_blocked_realloc)
848 old_realloc_hook = __realloc_hook;
849 __realloc_hook = emacs_blocked_realloc;
852 #endif /* not SYSTEM_MALLOC */
856 /***********************************************************************
857 Interval Allocation
858 ***********************************************************************/
860 /* Number of intervals allocated in an interval_block structure.
861 The 1020 is 1024 minus malloc overhead. */
863 #define INTERVAL_BLOCK_SIZE \
864 ((1020 - sizeof (struct interval_block *)) / sizeof (struct interval))
866 /* Intervals are allocated in chunks in form of an interval_block
867 structure. */
869 struct interval_block
871 struct interval_block *next;
872 struct interval intervals[INTERVAL_BLOCK_SIZE];
875 /* Current interval block. Its `next' pointer points to older
876 blocks. */
878 struct interval_block *interval_block;
880 /* Index in interval_block above of the next unused interval
881 structure. */
883 static int interval_block_index;
885 /* Number of free and live intervals. */
887 static int total_free_intervals, total_intervals;
889 /* List of free intervals. */
891 INTERVAL interval_free_list;
893 /* Total number of interval blocks now in use. */
895 int n_interval_blocks;
898 /* Initialize interval allocation. */
900 static void
901 init_intervals ()
903 interval_block
904 = (struct interval_block *) lisp_malloc (sizeof *interval_block,
905 MEM_TYPE_NON_LISP);
906 interval_block->next = 0;
907 bzero ((char *) interval_block->intervals, sizeof interval_block->intervals);
908 interval_block_index = 0;
909 interval_free_list = 0;
910 n_interval_blocks = 1;
914 /* Return a new interval. */
916 INTERVAL
917 make_interval ()
919 INTERVAL val;
921 if (interval_free_list)
923 val = interval_free_list;
924 interval_free_list = INTERVAL_PARENT (interval_free_list);
926 else
928 if (interval_block_index == INTERVAL_BLOCK_SIZE)
930 register struct interval_block *newi;
932 newi = (struct interval_block *) lisp_malloc (sizeof *newi,
933 MEM_TYPE_NON_LISP);
935 VALIDATE_LISP_STORAGE (newi, sizeof *newi);
936 newi->next = interval_block;
937 interval_block = newi;
938 interval_block_index = 0;
939 n_interval_blocks++;
941 val = &interval_block->intervals[interval_block_index++];
943 consing_since_gc += sizeof (struct interval);
944 intervals_consed++;
945 RESET_INTERVAL (val);
946 return val;
950 /* Mark Lisp objects in interval I. */
952 static void
953 mark_interval (i, dummy)
954 register INTERVAL i;
955 Lisp_Object dummy;
957 if (XMARKBIT (i->plist))
958 abort ();
959 mark_object (&i->plist);
960 XMARK (i->plist);
964 /* Mark the interval tree rooted in TREE. Don't call this directly;
965 use the macro MARK_INTERVAL_TREE instead. */
967 static void
968 mark_interval_tree (tree)
969 register INTERVAL tree;
971 /* No need to test if this tree has been marked already; this
972 function is always called through the MARK_INTERVAL_TREE macro,
973 which takes care of that. */
975 /* XMARK expands to an assignment; the LHS of an assignment can't be
976 a cast. */
977 XMARK (tree->up.obj);
979 traverse_intervals_noorder (tree, mark_interval, Qnil);
983 /* Mark the interval tree rooted in I. */
985 #define MARK_INTERVAL_TREE(i) \
986 do { \
987 if (!NULL_INTERVAL_P (i) \
988 && ! XMARKBIT (i->up.obj)) \
989 mark_interval_tree (i); \
990 } while (0)
993 /* The oddity in the call to XUNMARK is necessary because XUNMARK
994 expands to an assignment to its argument, and most C compilers
995 don't support casts on the left operand of `='. */
997 #define UNMARK_BALANCE_INTERVALS(i) \
998 do { \
999 if (! NULL_INTERVAL_P (i)) \
1001 XUNMARK ((i)->up.obj); \
1002 (i) = balance_intervals (i); \
1004 } while (0)
1007 /* Number support. If NO_UNION_TYPE isn't in effect, we
1008 can't create number objects in macros. */
1009 #ifndef make_number
1010 Lisp_Object
1011 make_number (n)
1012 int n;
1014 Lisp_Object obj;
1015 obj.s.val = n;
1016 obj.s.type = Lisp_Int;
1017 return obj;
1019 #endif
1021 /***********************************************************************
1022 String Allocation
1023 ***********************************************************************/
1025 /* Lisp_Strings are allocated in string_block structures. When a new
1026 string_block is allocated, all the Lisp_Strings it contains are
1027 added to a free-list string_free_list. When a new Lisp_String is
1028 needed, it is taken from that list. During the sweep phase of GC,
1029 string_blocks that are entirely free are freed, except two which
1030 we keep.
1032 String data is allocated from sblock structures. Strings larger
1033 than LARGE_STRING_BYTES, get their own sblock, data for smaller
1034 strings is sub-allocated out of sblocks of size SBLOCK_SIZE.
1036 Sblocks consist internally of sdata structures, one for each
1037 Lisp_String. The sdata structure points to the Lisp_String it
1038 belongs to. The Lisp_String points back to the `u.data' member of
1039 its sdata structure.
1041 When a Lisp_String is freed during GC, it is put back on
1042 string_free_list, and its `data' member and its sdata's `string'
1043 pointer is set to null. The size of the string is recorded in the
1044 `u.nbytes' member of the sdata. So, sdata structures that are no
1045 longer used, can be easily recognized, and it's easy to compact the
1046 sblocks of small strings which we do in compact_small_strings. */
1048 /* Size in bytes of an sblock structure used for small strings. This
1049 is 8192 minus malloc overhead. */
1051 #define SBLOCK_SIZE 8188
1053 /* Strings larger than this are considered large strings. String data
1054 for large strings is allocated from individual sblocks. */
1056 #define LARGE_STRING_BYTES 1024
1058 /* Structure describing string memory sub-allocated from an sblock.
1059 This is where the contents of Lisp strings are stored. */
1061 struct sdata
1063 /* Back-pointer to the string this sdata belongs to. If null, this
1064 structure is free, and the NBYTES member of the union below
1065 contains the string's byte size (the same value that STRING_BYTES
1066 would return if STRING were non-null). If non-null, STRING_BYTES
1067 (STRING) is the size of the data, and DATA contains the string's
1068 contents. */
1069 struct Lisp_String *string;
1071 #ifdef GC_CHECK_STRING_BYTES
1073 EMACS_INT nbytes;
1074 unsigned char data[1];
1076 #define SDATA_NBYTES(S) (S)->nbytes
1077 #define SDATA_DATA(S) (S)->data
1079 #else /* not GC_CHECK_STRING_BYTES */
1081 union
1083 /* When STRING in non-null. */
1084 unsigned char data[1];
1086 /* When STRING is null. */
1087 EMACS_INT nbytes;
1088 } u;
1091 #define SDATA_NBYTES(S) (S)->u.nbytes
1092 #define SDATA_DATA(S) (S)->u.data
1094 #endif /* not GC_CHECK_STRING_BYTES */
1098 /* Structure describing a block of memory which is sub-allocated to
1099 obtain string data memory for strings. Blocks for small strings
1100 are of fixed size SBLOCK_SIZE. Blocks for large strings are made
1101 as large as needed. */
1103 struct sblock
1105 /* Next in list. */
1106 struct sblock *next;
1108 /* Pointer to the next free sdata block. This points past the end
1109 of the sblock if there isn't any space left in this block. */
1110 struct sdata *next_free;
1112 /* Start of data. */
1113 struct sdata first_data;
1116 /* Number of Lisp strings in a string_block structure. The 1020 is
1117 1024 minus malloc overhead. */
1119 #define STRINGS_IN_STRING_BLOCK \
1120 ((1020 - sizeof (struct string_block *)) / sizeof (struct Lisp_String))
1122 /* Structure describing a block from which Lisp_String structures
1123 are allocated. */
1125 struct string_block
1127 struct string_block *next;
1128 struct Lisp_String strings[STRINGS_IN_STRING_BLOCK];
1131 /* Head and tail of the list of sblock structures holding Lisp string
1132 data. We always allocate from current_sblock. The NEXT pointers
1133 in the sblock structures go from oldest_sblock to current_sblock. */
1135 static struct sblock *oldest_sblock, *current_sblock;
1137 /* List of sblocks for large strings. */
1139 static struct sblock *large_sblocks;
1141 /* List of string_block structures, and how many there are. */
1143 static struct string_block *string_blocks;
1144 static int n_string_blocks;
1146 /* Free-list of Lisp_Strings. */
1148 static struct Lisp_String *string_free_list;
1150 /* Number of live and free Lisp_Strings. */
1152 static int total_strings, total_free_strings;
1154 /* Number of bytes used by live strings. */
1156 static int total_string_size;
1158 /* Given a pointer to a Lisp_String S which is on the free-list
1159 string_free_list, return a pointer to its successor in the
1160 free-list. */
1162 #define NEXT_FREE_LISP_STRING(S) (*(struct Lisp_String **) (S))
1164 /* Return a pointer to the sdata structure belonging to Lisp string S.
1165 S must be live, i.e. S->data must not be null. S->data is actually
1166 a pointer to the `u.data' member of its sdata structure; the
1167 structure starts at a constant offset in front of that. */
1169 #ifdef GC_CHECK_STRING_BYTES
1171 #define SDATA_OF_STRING(S) \
1172 ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *) \
1173 - sizeof (EMACS_INT)))
1175 #else /* not GC_CHECK_STRING_BYTES */
1177 #define SDATA_OF_STRING(S) \
1178 ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *)))
1180 #endif /* not GC_CHECK_STRING_BYTES */
1182 /* Value is the size of an sdata structure large enough to hold NBYTES
1183 bytes of string data. The value returned includes a terminating
1184 NUL byte, the size of the sdata structure, and padding. */
1186 #ifdef GC_CHECK_STRING_BYTES
1188 #define SDATA_SIZE(NBYTES) \
1189 ((sizeof (struct Lisp_String *) \
1190 + (NBYTES) + 1 \
1191 + sizeof (EMACS_INT) \
1192 + sizeof (EMACS_INT) - 1) \
1193 & ~(sizeof (EMACS_INT) - 1))
1195 #else /* not GC_CHECK_STRING_BYTES */
1197 #define SDATA_SIZE(NBYTES) \
1198 ((sizeof (struct Lisp_String *) \
1199 + (NBYTES) + 1 \
1200 + sizeof (EMACS_INT) - 1) \
1201 & ~(sizeof (EMACS_INT) - 1))
1203 #endif /* not GC_CHECK_STRING_BYTES */
1205 /* Initialize string allocation. Called from init_alloc_once. */
1207 void
1208 init_strings ()
1210 total_strings = total_free_strings = total_string_size = 0;
1211 oldest_sblock = current_sblock = large_sblocks = NULL;
1212 string_blocks = NULL;
1213 n_string_blocks = 0;
1214 string_free_list = NULL;
1218 #ifdef GC_CHECK_STRING_BYTES
1220 static int check_string_bytes_count;
1222 void check_string_bytes P_ ((int));
1223 void check_sblock P_ ((struct sblock *));
1225 #define CHECK_STRING_BYTES(S) STRING_BYTES (S)
1228 /* Like GC_STRING_BYTES, but with debugging check. */
1231 string_bytes (s)
1232 struct Lisp_String *s;
1234 int nbytes = (s->size_byte < 0 ? s->size : s->size_byte) & ~MARKBIT;
1235 if (!PURE_POINTER_P (s)
1236 && s->data
1237 && nbytes != SDATA_NBYTES (SDATA_OF_STRING (s)))
1238 abort ();
1239 return nbytes;
1242 /* Check validity of Lisp strings' string_bytes member in B. */
1244 void
1245 check_sblock (b)
1246 struct sblock *b;
1248 struct sdata *from, *end, *from_end;
1250 end = b->next_free;
1252 for (from = &b->first_data; from < end; from = from_end)
1254 /* Compute the next FROM here because copying below may
1255 overwrite data we need to compute it. */
1256 int nbytes;
1258 /* Check that the string size recorded in the string is the
1259 same as the one recorded in the sdata structure. */
1260 if (from->string)
1261 CHECK_STRING_BYTES (from->string);
1263 if (from->string)
1264 nbytes = GC_STRING_BYTES (from->string);
1265 else
1266 nbytes = SDATA_NBYTES (from);
1268 nbytes = SDATA_SIZE (nbytes);
1269 from_end = (struct sdata *) ((char *) from + nbytes);
1274 /* Check validity of Lisp strings' string_bytes member. ALL_P
1275 non-zero means check all strings, otherwise check only most
1276 recently allocated strings. Used for hunting a bug. */
1278 void
1279 check_string_bytes (all_p)
1280 int all_p;
1282 if (all_p)
1284 struct sblock *b;
1286 for (b = large_sblocks; b; b = b->next)
1288 struct Lisp_String *s = b->first_data.string;
1289 if (s)
1290 CHECK_STRING_BYTES (s);
1293 for (b = oldest_sblock; b; b = b->next)
1294 check_sblock (b);
1296 else
1297 check_sblock (current_sblock);
1300 #endif /* GC_CHECK_STRING_BYTES */
1303 /* Return a new Lisp_String. */
1305 static struct Lisp_String *
1306 allocate_string ()
1308 struct Lisp_String *s;
1310 /* If the free-list is empty, allocate a new string_block, and
1311 add all the Lisp_Strings in it to the free-list. */
1312 if (string_free_list == NULL)
1314 struct string_block *b;
1315 int i;
1317 b = (struct string_block *) lisp_malloc (sizeof *b, MEM_TYPE_STRING);
1318 VALIDATE_LISP_STORAGE (b, sizeof *b);
1319 bzero (b, sizeof *b);
1320 b->next = string_blocks;
1321 string_blocks = b;
1322 ++n_string_blocks;
1324 for (i = STRINGS_IN_STRING_BLOCK - 1; i >= 0; --i)
1326 s = b->strings + i;
1327 NEXT_FREE_LISP_STRING (s) = string_free_list;
1328 string_free_list = s;
1331 total_free_strings += STRINGS_IN_STRING_BLOCK;
1334 /* Pop a Lisp_String off the free-list. */
1335 s = string_free_list;
1336 string_free_list = NEXT_FREE_LISP_STRING (s);
1338 /* Probably not strictly necessary, but play it safe. */
1339 bzero (s, sizeof *s);
1341 --total_free_strings;
1342 ++total_strings;
1343 ++strings_consed;
1344 consing_since_gc += sizeof *s;
1346 #ifdef GC_CHECK_STRING_BYTES
1347 if (!noninteractive
1348 #ifdef MAC_OS8
1349 && current_sblock
1350 #endif
1353 if (++check_string_bytes_count == 200)
1355 check_string_bytes_count = 0;
1356 check_string_bytes (1);
1358 else
1359 check_string_bytes (0);
1361 #endif /* GC_CHECK_STRING_BYTES */
1363 return s;
1367 /* Set up Lisp_String S for holding NCHARS characters, NBYTES bytes,
1368 plus a NUL byte at the end. Allocate an sdata structure for S, and
1369 set S->data to its `u.data' member. Store a NUL byte at the end of
1370 S->data. Set S->size to NCHARS and S->size_byte to NBYTES. Free
1371 S->data if it was initially non-null. */
1373 void
1374 allocate_string_data (s, nchars, nbytes)
1375 struct Lisp_String *s;
1376 int nchars, nbytes;
1378 struct sdata *data, *old_data;
1379 struct sblock *b;
1380 int needed, old_nbytes;
1382 /* Determine the number of bytes needed to store NBYTES bytes
1383 of string data. */
1384 needed = SDATA_SIZE (nbytes);
1386 if (nbytes > LARGE_STRING_BYTES)
1388 size_t size = sizeof *b - sizeof (struct sdata) + needed;
1390 #ifdef DOUG_LEA_MALLOC
1391 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed
1392 because mapped region contents are not preserved in
1393 a dumped Emacs. */
1394 mallopt (M_MMAP_MAX, 0);
1395 #endif
1397 b = (struct sblock *) lisp_malloc (size, MEM_TYPE_NON_LISP);
1399 #ifdef DOUG_LEA_MALLOC
1400 /* Back to a reasonable maximum of mmap'ed areas. */
1401 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
1402 #endif
1404 b->next_free = &b->first_data;
1405 b->first_data.string = NULL;
1406 b->next = large_sblocks;
1407 large_sblocks = b;
1409 else if (current_sblock == NULL
1410 || (((char *) current_sblock + SBLOCK_SIZE
1411 - (char *) current_sblock->next_free)
1412 < needed))
1414 /* Not enough room in the current sblock. */
1415 b = (struct sblock *) lisp_malloc (SBLOCK_SIZE, MEM_TYPE_NON_LISP);
1416 b->next_free = &b->first_data;
1417 b->first_data.string = NULL;
1418 b->next = NULL;
1420 if (current_sblock)
1421 current_sblock->next = b;
1422 else
1423 oldest_sblock = b;
1424 current_sblock = b;
1426 else
1427 b = current_sblock;
1429 old_data = s->data ? SDATA_OF_STRING (s) : NULL;
1430 old_nbytes = GC_STRING_BYTES (s);
1432 data = b->next_free;
1433 data->string = s;
1434 s->data = SDATA_DATA (data);
1435 #ifdef GC_CHECK_STRING_BYTES
1436 SDATA_NBYTES (data) = nbytes;
1437 #endif
1438 s->size = nchars;
1439 s->size_byte = nbytes;
1440 s->data[nbytes] = '\0';
1441 b->next_free = (struct sdata *) ((char *) data + needed);
1443 /* If S had already data assigned, mark that as free by setting its
1444 string back-pointer to null, and recording the size of the data
1445 in it. */
1446 if (old_data)
1448 SDATA_NBYTES (old_data) = old_nbytes;
1449 old_data->string = NULL;
1452 consing_since_gc += needed;
1456 /* Sweep and compact strings. */
1458 static void
1459 sweep_strings ()
1461 struct string_block *b, *next;
1462 struct string_block *live_blocks = NULL;
1464 string_free_list = NULL;
1465 total_strings = total_free_strings = 0;
1466 total_string_size = 0;
1468 /* Scan strings_blocks, free Lisp_Strings that aren't marked. */
1469 for (b = string_blocks; b; b = next)
1471 int i, nfree = 0;
1472 struct Lisp_String *free_list_before = string_free_list;
1474 next = b->next;
1476 for (i = 0; i < STRINGS_IN_STRING_BLOCK; ++i)
1478 struct Lisp_String *s = b->strings + i;
1480 if (s->data)
1482 /* String was not on free-list before. */
1483 if (STRING_MARKED_P (s))
1485 /* String is live; unmark it and its intervals. */
1486 UNMARK_STRING (s);
1488 if (!NULL_INTERVAL_P (s->intervals))
1489 UNMARK_BALANCE_INTERVALS (s->intervals);
1491 ++total_strings;
1492 total_string_size += STRING_BYTES (s);
1494 else
1496 /* String is dead. Put it on the free-list. */
1497 struct sdata *data = SDATA_OF_STRING (s);
1499 /* Save the size of S in its sdata so that we know
1500 how large that is. Reset the sdata's string
1501 back-pointer so that we know it's free. */
1502 #ifdef GC_CHECK_STRING_BYTES
1503 if (GC_STRING_BYTES (s) != SDATA_NBYTES (data))
1504 abort ();
1505 #else
1506 data->u.nbytes = GC_STRING_BYTES (s);
1507 #endif
1508 data->string = NULL;
1510 /* Reset the strings's `data' member so that we
1511 know it's free. */
1512 s->data = NULL;
1514 /* Put the string on the free-list. */
1515 NEXT_FREE_LISP_STRING (s) = string_free_list;
1516 string_free_list = s;
1517 ++nfree;
1520 else
1522 /* S was on the free-list before. Put it there again. */
1523 NEXT_FREE_LISP_STRING (s) = string_free_list;
1524 string_free_list = s;
1525 ++nfree;
1529 /* Free blocks that contain free Lisp_Strings only, except
1530 the first two of them. */
1531 if (nfree == STRINGS_IN_STRING_BLOCK
1532 && total_free_strings > STRINGS_IN_STRING_BLOCK)
1534 lisp_free (b);
1535 --n_string_blocks;
1536 string_free_list = free_list_before;
1538 else
1540 total_free_strings += nfree;
1541 b->next = live_blocks;
1542 live_blocks = b;
1546 string_blocks = live_blocks;
1547 free_large_strings ();
1548 compact_small_strings ();
1552 /* Free dead large strings. */
1554 static void
1555 free_large_strings ()
1557 struct sblock *b, *next;
1558 struct sblock *live_blocks = NULL;
1560 for (b = large_sblocks; b; b = next)
1562 next = b->next;
1564 if (b->first_data.string == NULL)
1565 lisp_free (b);
1566 else
1568 b->next = live_blocks;
1569 live_blocks = b;
1573 large_sblocks = live_blocks;
1577 /* Compact data of small strings. Free sblocks that don't contain
1578 data of live strings after compaction. */
1580 static void
1581 compact_small_strings ()
1583 struct sblock *b, *tb, *next;
1584 struct sdata *from, *to, *end, *tb_end;
1585 struct sdata *to_end, *from_end;
1587 /* TB is the sblock we copy to, TO is the sdata within TB we copy
1588 to, and TB_END is the end of TB. */
1589 tb = oldest_sblock;
1590 tb_end = (struct sdata *) ((char *) tb + SBLOCK_SIZE);
1591 to = &tb->first_data;
1593 /* Step through the blocks from the oldest to the youngest. We
1594 expect that old blocks will stabilize over time, so that less
1595 copying will happen this way. */
1596 for (b = oldest_sblock; b; b = b->next)
1598 end = b->next_free;
1599 xassert ((char *) end <= (char *) b + SBLOCK_SIZE);
1601 for (from = &b->first_data; from < end; from = from_end)
1603 /* Compute the next FROM here because copying below may
1604 overwrite data we need to compute it. */
1605 int nbytes;
1607 #ifdef GC_CHECK_STRING_BYTES
1608 /* Check that the string size recorded in the string is the
1609 same as the one recorded in the sdata structure. */
1610 if (from->string
1611 && GC_STRING_BYTES (from->string) != SDATA_NBYTES (from))
1612 abort ();
1613 #endif /* GC_CHECK_STRING_BYTES */
1615 if (from->string)
1616 nbytes = GC_STRING_BYTES (from->string);
1617 else
1618 nbytes = SDATA_NBYTES (from);
1620 nbytes = SDATA_SIZE (nbytes);
1621 from_end = (struct sdata *) ((char *) from + nbytes);
1623 /* FROM->string non-null means it's alive. Copy its data. */
1624 if (from->string)
1626 /* If TB is full, proceed with the next sblock. */
1627 to_end = (struct sdata *) ((char *) to + nbytes);
1628 if (to_end > tb_end)
1630 tb->next_free = to;
1631 tb = tb->next;
1632 tb_end = (struct sdata *) ((char *) tb + SBLOCK_SIZE);
1633 to = &tb->first_data;
1634 to_end = (struct sdata *) ((char *) to + nbytes);
1637 /* Copy, and update the string's `data' pointer. */
1638 if (from != to)
1640 xassert (tb != b || to <= from);
1641 safe_bcopy ((char *) from, (char *) to, nbytes);
1642 to->string->data = SDATA_DATA (to);
1645 /* Advance past the sdata we copied to. */
1646 to = to_end;
1651 /* The rest of the sblocks following TB don't contain live data, so
1652 we can free them. */
1653 for (b = tb->next; b; b = next)
1655 next = b->next;
1656 lisp_free (b);
1659 tb->next_free = to;
1660 tb->next = NULL;
1661 current_sblock = tb;
1665 DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0,
1666 doc: /* Return a newly created string of length LENGTH, with each element being INIT.
1667 Both LENGTH and INIT must be numbers. */)
1668 (length, init)
1669 Lisp_Object length, init;
1671 register Lisp_Object val;
1672 register unsigned char *p, *end;
1673 int c, nbytes;
1675 CHECK_NATNUM (length);
1676 CHECK_NUMBER (init);
1678 c = XINT (init);
1679 if (SINGLE_BYTE_CHAR_P (c))
1681 nbytes = XINT (length);
1682 val = make_uninit_string (nbytes);
1683 p = SDATA (val);
1684 end = p + SCHARS (val);
1685 while (p != end)
1686 *p++ = c;
1688 else
1690 unsigned char str[MAX_MULTIBYTE_LENGTH];
1691 int len = CHAR_STRING (c, str);
1693 nbytes = len * XINT (length);
1694 val = make_uninit_multibyte_string (XINT (length), nbytes);
1695 p = SDATA (val);
1696 end = p + nbytes;
1697 while (p != end)
1699 bcopy (str, p, len);
1700 p += len;
1704 *p = 0;
1705 return val;
1709 DEFUN ("make-bool-vector", Fmake_bool_vector, Smake_bool_vector, 2, 2, 0,
1710 doc: /* Return a new bool-vector of length LENGTH, using INIT for as each element.
1711 LENGTH must be a number. INIT matters only in whether it is t or nil. */)
1712 (length, init)
1713 Lisp_Object length, init;
1715 register Lisp_Object val;
1716 struct Lisp_Bool_Vector *p;
1717 int real_init, i;
1718 int length_in_chars, length_in_elts, bits_per_value;
1720 CHECK_NATNUM (length);
1722 bits_per_value = sizeof (EMACS_INT) * BITS_PER_CHAR;
1724 length_in_elts = (XFASTINT (length) + bits_per_value - 1) / bits_per_value;
1725 length_in_chars = ((XFASTINT (length) + BITS_PER_CHAR - 1) / BITS_PER_CHAR);
1727 /* We must allocate one more elements than LENGTH_IN_ELTS for the
1728 slot `size' of the struct Lisp_Bool_Vector. */
1729 val = Fmake_vector (make_number (length_in_elts + 1), Qnil);
1730 p = XBOOL_VECTOR (val);
1732 /* Get rid of any bits that would cause confusion. */
1733 p->vector_size = 0;
1734 XSETBOOL_VECTOR (val, p);
1735 p->size = XFASTINT (length);
1737 real_init = (NILP (init) ? 0 : -1);
1738 for (i = 0; i < length_in_chars ; i++)
1739 p->data[i] = real_init;
1741 /* Clear the extraneous bits in the last byte. */
1742 if (XINT (length) != length_in_chars * BITS_PER_CHAR)
1743 XBOOL_VECTOR (val)->data[length_in_chars - 1]
1744 &= (1 << (XINT (length) % BITS_PER_CHAR)) - 1;
1746 return val;
1750 /* Make a string from NBYTES bytes at CONTENTS, and compute the number
1751 of characters from the contents. This string may be unibyte or
1752 multibyte, depending on the contents. */
1754 Lisp_Object
1755 make_string (contents, nbytes)
1756 const char *contents;
1757 int nbytes;
1759 register Lisp_Object val;
1760 int nchars, multibyte_nbytes;
1762 parse_str_as_multibyte (contents, nbytes, &nchars, &multibyte_nbytes);
1763 if (nbytes == nchars || nbytes != multibyte_nbytes)
1764 /* CONTENTS contains no multibyte sequences or contains an invalid
1765 multibyte sequence. We must make unibyte string. */
1766 val = make_unibyte_string (contents, nbytes);
1767 else
1768 val = make_multibyte_string (contents, nchars, nbytes);
1769 return val;
1773 /* Make an unibyte string from LENGTH bytes at CONTENTS. */
1775 Lisp_Object
1776 make_unibyte_string (contents, length)
1777 const char *contents;
1778 int length;
1780 register Lisp_Object val;
1781 val = make_uninit_string (length);
1782 bcopy (contents, SDATA (val), length);
1783 STRING_SET_UNIBYTE (val);
1784 return val;
1788 /* Make a multibyte string from NCHARS characters occupying NBYTES
1789 bytes at CONTENTS. */
1791 Lisp_Object
1792 make_multibyte_string (contents, nchars, nbytes)
1793 const char *contents;
1794 int nchars, nbytes;
1796 register Lisp_Object val;
1797 val = make_uninit_multibyte_string (nchars, nbytes);
1798 bcopy (contents, SDATA (val), nbytes);
1799 return val;
1803 /* Make a string from NCHARS characters occupying NBYTES bytes at
1804 CONTENTS. It is a multibyte string if NBYTES != NCHARS. */
1806 Lisp_Object
1807 make_string_from_bytes (contents, nchars, nbytes)
1808 char *contents;
1809 int nchars, nbytes;
1811 register Lisp_Object val;
1812 val = make_uninit_multibyte_string (nchars, nbytes);
1813 bcopy (contents, SDATA (val), nbytes);
1814 if (SBYTES (val) == SCHARS (val))
1815 STRING_SET_UNIBYTE (val);
1816 return val;
1820 /* Make a string from NCHARS characters occupying NBYTES bytes at
1821 CONTENTS. The argument MULTIBYTE controls whether to label the
1822 string as multibyte. */
1824 Lisp_Object
1825 make_specified_string (contents, nchars, nbytes, multibyte)
1826 char *contents;
1827 int nchars, nbytes;
1828 int multibyte;
1830 register Lisp_Object val;
1831 val = make_uninit_multibyte_string (nchars, nbytes);
1832 bcopy (contents, SDATA (val), nbytes);
1833 if (!multibyte)
1834 STRING_SET_UNIBYTE (val);
1835 return val;
1839 /* Make a string from the data at STR, treating it as multibyte if the
1840 data warrants. */
1842 Lisp_Object
1843 build_string (str)
1844 const char *str;
1846 return make_string (str, strlen (str));
1850 /* Return an unibyte Lisp_String set up to hold LENGTH characters
1851 occupying LENGTH bytes. */
1853 Lisp_Object
1854 make_uninit_string (length)
1855 int length;
1857 Lisp_Object val;
1858 val = make_uninit_multibyte_string (length, length);
1859 STRING_SET_UNIBYTE (val);
1860 return val;
1864 /* Return a multibyte Lisp_String set up to hold NCHARS characters
1865 which occupy NBYTES bytes. */
1867 Lisp_Object
1868 make_uninit_multibyte_string (nchars, nbytes)
1869 int nchars, nbytes;
1871 Lisp_Object string;
1872 struct Lisp_String *s;
1874 if (nchars < 0)
1875 abort ();
1877 s = allocate_string ();
1878 allocate_string_data (s, nchars, nbytes);
1879 XSETSTRING (string, s);
1880 string_chars_consed += nbytes;
1881 return string;
1886 /***********************************************************************
1887 Float Allocation
1888 ***********************************************************************/
1890 /* We store float cells inside of float_blocks, allocating a new
1891 float_block with malloc whenever necessary. Float cells reclaimed
1892 by GC are put on a free list to be reallocated before allocating
1893 any new float cells from the latest float_block.
1895 Each float_block is just under 1020 bytes long, since malloc really
1896 allocates in units of powers of two and uses 4 bytes for its own
1897 overhead. */
1899 #define FLOAT_BLOCK_SIZE \
1900 ((1020 - sizeof (struct float_block *)) / sizeof (struct Lisp_Float))
1902 struct float_block
1904 struct float_block *next;
1905 struct Lisp_Float floats[FLOAT_BLOCK_SIZE];
1908 /* Current float_block. */
1910 struct float_block *float_block;
1912 /* Index of first unused Lisp_Float in the current float_block. */
1914 int float_block_index;
1916 /* Total number of float blocks now in use. */
1918 int n_float_blocks;
1920 /* Free-list of Lisp_Floats. */
1922 struct Lisp_Float *float_free_list;
1925 /* Initialize float allocation. */
1927 void
1928 init_float ()
1930 float_block = (struct float_block *) lisp_malloc (sizeof *float_block,
1931 MEM_TYPE_FLOAT);
1932 float_block->next = 0;
1933 bzero ((char *) float_block->floats, sizeof float_block->floats);
1934 float_block_index = 0;
1935 float_free_list = 0;
1936 n_float_blocks = 1;
1940 /* Explicitly free a float cell by putting it on the free-list. */
1942 void
1943 free_float (ptr)
1944 struct Lisp_Float *ptr;
1946 *(struct Lisp_Float **)&ptr->data = float_free_list;
1947 #if GC_MARK_STACK
1948 ptr->type = Vdead;
1949 #endif
1950 float_free_list = ptr;
1954 /* Return a new float object with value FLOAT_VALUE. */
1956 Lisp_Object
1957 make_float (float_value)
1958 double float_value;
1960 register Lisp_Object val;
1962 if (float_free_list)
1964 /* We use the data field for chaining the free list
1965 so that we won't use the same field that has the mark bit. */
1966 XSETFLOAT (val, float_free_list);
1967 float_free_list = *(struct Lisp_Float **)&float_free_list->data;
1969 else
1971 if (float_block_index == FLOAT_BLOCK_SIZE)
1973 register struct float_block *new;
1975 new = (struct float_block *) lisp_malloc (sizeof *new,
1976 MEM_TYPE_FLOAT);
1977 VALIDATE_LISP_STORAGE (new, sizeof *new);
1978 new->next = float_block;
1979 float_block = new;
1980 float_block_index = 0;
1981 n_float_blocks++;
1983 XSETFLOAT (val, &float_block->floats[float_block_index++]);
1986 XFLOAT_DATA (val) = float_value;
1987 XSETFASTINT (XFLOAT (val)->type, 0); /* bug chasing -wsr */
1988 consing_since_gc += sizeof (struct Lisp_Float);
1989 floats_consed++;
1990 return val;
1995 /***********************************************************************
1996 Cons Allocation
1997 ***********************************************************************/
1999 /* We store cons cells inside of cons_blocks, allocating a new
2000 cons_block with malloc whenever necessary. Cons cells reclaimed by
2001 GC are put on a free list to be reallocated before allocating
2002 any new cons cells from the latest cons_block.
2004 Each cons_block is just under 1020 bytes long,
2005 since malloc really allocates in units of powers of two
2006 and uses 4 bytes for its own overhead. */
2008 #define CONS_BLOCK_SIZE \
2009 ((1020 - sizeof (struct cons_block *)) / sizeof (struct Lisp_Cons))
2011 struct cons_block
2013 struct cons_block *next;
2014 struct Lisp_Cons conses[CONS_BLOCK_SIZE];
2017 /* Current cons_block. */
2019 struct cons_block *cons_block;
2021 /* Index of first unused Lisp_Cons in the current block. */
2023 int cons_block_index;
2025 /* Free-list of Lisp_Cons structures. */
2027 struct Lisp_Cons *cons_free_list;
2029 /* Total number of cons blocks now in use. */
2031 int n_cons_blocks;
2034 /* Initialize cons allocation. */
2036 void
2037 init_cons ()
2039 cons_block = (struct cons_block *) lisp_malloc (sizeof *cons_block,
2040 MEM_TYPE_CONS);
2041 cons_block->next = 0;
2042 bzero ((char *) cons_block->conses, sizeof cons_block->conses);
2043 cons_block_index = 0;
2044 cons_free_list = 0;
2045 n_cons_blocks = 1;
2049 /* Explicitly free a cons cell by putting it on the free-list. */
2051 void
2052 free_cons (ptr)
2053 struct Lisp_Cons *ptr;
2055 *(struct Lisp_Cons **)&ptr->cdr = cons_free_list;
2056 #if GC_MARK_STACK
2057 ptr->car = Vdead;
2058 #endif
2059 cons_free_list = ptr;
2063 DEFUN ("cons", Fcons, Scons, 2, 2, 0,
2064 doc: /* Create a new cons, give it CAR and CDR as components, and return it. */)
2065 (car, cdr)
2066 Lisp_Object car, cdr;
2068 register Lisp_Object val;
2070 if (cons_free_list)
2072 /* We use the cdr for chaining the free list
2073 so that we won't use the same field that has the mark bit. */
2074 XSETCONS (val, cons_free_list);
2075 cons_free_list = *(struct Lisp_Cons **)&cons_free_list->cdr;
2077 else
2079 if (cons_block_index == CONS_BLOCK_SIZE)
2081 register struct cons_block *new;
2082 new = (struct cons_block *) lisp_malloc (sizeof *new,
2083 MEM_TYPE_CONS);
2084 VALIDATE_LISP_STORAGE (new, sizeof *new);
2085 new->next = cons_block;
2086 cons_block = new;
2087 cons_block_index = 0;
2088 n_cons_blocks++;
2090 XSETCONS (val, &cons_block->conses[cons_block_index++]);
2093 XSETCAR (val, car);
2094 XSETCDR (val, cdr);
2095 consing_since_gc += sizeof (struct Lisp_Cons);
2096 cons_cells_consed++;
2097 return val;
2101 /* Make a list of 2, 3, 4 or 5 specified objects. */
2103 Lisp_Object
2104 list2 (arg1, arg2)
2105 Lisp_Object arg1, arg2;
2107 return Fcons (arg1, Fcons (arg2, Qnil));
2111 Lisp_Object
2112 list3 (arg1, arg2, arg3)
2113 Lisp_Object arg1, arg2, arg3;
2115 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Qnil)));
2119 Lisp_Object
2120 list4 (arg1, arg2, arg3, arg4)
2121 Lisp_Object arg1, arg2, arg3, arg4;
2123 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4, Qnil))));
2127 Lisp_Object
2128 list5 (arg1, arg2, arg3, arg4, arg5)
2129 Lisp_Object arg1, arg2, arg3, arg4, arg5;
2131 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4,
2132 Fcons (arg5, Qnil)))));
2136 DEFUN ("list", Flist, Slist, 0, MANY, 0,
2137 doc: /* Return a newly created list with specified arguments as elements.
2138 Any number of arguments, even zero arguments, are allowed.
2139 usage: (list &rest OBJECTS) */)
2140 (nargs, args)
2141 int nargs;
2142 register Lisp_Object *args;
2144 register Lisp_Object val;
2145 val = Qnil;
2147 while (nargs > 0)
2149 nargs--;
2150 val = Fcons (args[nargs], val);
2152 return val;
2156 DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0,
2157 doc: /* Return a newly created list of length LENGTH, with each element being INIT. */)
2158 (length, init)
2159 register Lisp_Object length, init;
2161 register Lisp_Object val;
2162 register int size;
2164 CHECK_NATNUM (length);
2165 size = XFASTINT (length);
2167 val = Qnil;
2168 while (size > 0)
2170 val = Fcons (init, val);
2171 --size;
2173 if (size > 0)
2175 val = Fcons (init, val);
2176 --size;
2178 if (size > 0)
2180 val = Fcons (init, val);
2181 --size;
2183 if (size > 0)
2185 val = Fcons (init, val);
2186 --size;
2188 if (size > 0)
2190 val = Fcons (init, val);
2191 --size;
2197 QUIT;
2200 return val;
2205 /***********************************************************************
2206 Vector Allocation
2207 ***********************************************************************/
2209 /* Singly-linked list of all vectors. */
2211 struct Lisp_Vector *all_vectors;
2213 /* Total number of vector-like objects now in use. */
2215 int n_vectors;
2218 /* Value is a pointer to a newly allocated Lisp_Vector structure
2219 with room for LEN Lisp_Objects. */
2221 static struct Lisp_Vector *
2222 allocate_vectorlike (len, type)
2223 EMACS_INT len;
2224 enum mem_type type;
2226 struct Lisp_Vector *p;
2227 size_t nbytes;
2229 #ifdef DOUG_LEA_MALLOC
2230 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed
2231 because mapped region contents are not preserved in
2232 a dumped Emacs. */
2233 mallopt (M_MMAP_MAX, 0);
2234 #endif
2236 nbytes = sizeof *p + (len - 1) * sizeof p->contents[0];
2237 p = (struct Lisp_Vector *) lisp_malloc (nbytes, type);
2239 #ifdef DOUG_LEA_MALLOC
2240 /* Back to a reasonable maximum of mmap'ed areas. */
2241 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
2242 #endif
2244 VALIDATE_LISP_STORAGE (p, 0);
2245 consing_since_gc += nbytes;
2246 vector_cells_consed += len;
2248 p->next = all_vectors;
2249 all_vectors = p;
2250 ++n_vectors;
2251 return p;
2255 /* Allocate a vector with NSLOTS slots. */
2257 struct Lisp_Vector *
2258 allocate_vector (nslots)
2259 EMACS_INT nslots;
2261 struct Lisp_Vector *v = allocate_vectorlike (nslots, MEM_TYPE_VECTOR);
2262 v->size = nslots;
2263 return v;
2267 /* Allocate other vector-like structures. */
2269 struct Lisp_Hash_Table *
2270 allocate_hash_table ()
2272 EMACS_INT len = VECSIZE (struct Lisp_Hash_Table);
2273 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_HASH_TABLE);
2274 EMACS_INT i;
2276 v->size = len;
2277 for (i = 0; i < len; ++i)
2278 v->contents[i] = Qnil;
2280 return (struct Lisp_Hash_Table *) v;
2284 struct window *
2285 allocate_window ()
2287 EMACS_INT len = VECSIZE (struct window);
2288 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_WINDOW);
2289 EMACS_INT i;
2291 for (i = 0; i < len; ++i)
2292 v->contents[i] = Qnil;
2293 v->size = len;
2295 return (struct window *) v;
2299 struct frame *
2300 allocate_frame ()
2302 EMACS_INT len = VECSIZE (struct frame);
2303 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_FRAME);
2304 EMACS_INT i;
2306 for (i = 0; i < len; ++i)
2307 v->contents[i] = make_number (0);
2308 v->size = len;
2309 return (struct frame *) v;
2313 struct Lisp_Process *
2314 allocate_process ()
2316 EMACS_INT len = VECSIZE (struct Lisp_Process);
2317 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_PROCESS);
2318 EMACS_INT i;
2320 for (i = 0; i < len; ++i)
2321 v->contents[i] = Qnil;
2322 v->size = len;
2324 return (struct Lisp_Process *) v;
2328 struct Lisp_Vector *
2329 allocate_other_vector (len)
2330 EMACS_INT len;
2332 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_VECTOR);
2333 EMACS_INT i;
2335 for (i = 0; i < len; ++i)
2336 v->contents[i] = Qnil;
2337 v->size = len;
2339 return v;
2343 DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0,
2344 doc: /* Return a newly created vector of length LENGTH, with each element being INIT.
2345 See also the function `vector'. */)
2346 (length, init)
2347 register Lisp_Object length, init;
2349 Lisp_Object vector;
2350 register EMACS_INT sizei;
2351 register int index;
2352 register struct Lisp_Vector *p;
2354 CHECK_NATNUM (length);
2355 sizei = XFASTINT (length);
2357 p = allocate_vector (sizei);
2358 for (index = 0; index < sizei; index++)
2359 p->contents[index] = init;
2361 XSETVECTOR (vector, p);
2362 return vector;
2366 DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0,
2367 doc: /* Return a newly created char-table, with purpose PURPOSE.
2368 Each element is initialized to INIT, which defaults to nil.
2369 PURPOSE should be a symbol which has a `char-table-extra-slots' property.
2370 The property's value should be an integer between 0 and 10. */)
2371 (purpose, init)
2372 register Lisp_Object purpose, init;
2374 Lisp_Object vector;
2375 Lisp_Object n;
2376 CHECK_SYMBOL (purpose);
2377 n = Fget (purpose, Qchar_table_extra_slots);
2378 CHECK_NUMBER (n);
2379 if (XINT (n) < 0 || XINT (n) > 10)
2380 args_out_of_range (n, Qnil);
2381 /* Add 2 to the size for the defalt and parent slots. */
2382 vector = Fmake_vector (make_number (CHAR_TABLE_STANDARD_SLOTS + XINT (n)),
2383 init);
2384 XCHAR_TABLE (vector)->top = Qt;
2385 XCHAR_TABLE (vector)->parent = Qnil;
2386 XCHAR_TABLE (vector)->purpose = purpose;
2387 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
2388 return vector;
2392 /* Return a newly created sub char table with default value DEFALT.
2393 Since a sub char table does not appear as a top level Emacs Lisp
2394 object, we don't need a Lisp interface to make it. */
2396 Lisp_Object
2397 make_sub_char_table (defalt)
2398 Lisp_Object defalt;
2400 Lisp_Object vector
2401 = Fmake_vector (make_number (SUB_CHAR_TABLE_STANDARD_SLOTS), Qnil);
2402 XCHAR_TABLE (vector)->top = Qnil;
2403 XCHAR_TABLE (vector)->defalt = defalt;
2404 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
2405 return vector;
2409 DEFUN ("vector", Fvector, Svector, 0, MANY, 0,
2410 doc: /* Return a newly created vector with specified arguments as elements.
2411 Any number of arguments, even zero arguments, are allowed.
2412 usage: (vector &rest OBJECTS) */)
2413 (nargs, args)
2414 register int nargs;
2415 Lisp_Object *args;
2417 register Lisp_Object len, val;
2418 register int index;
2419 register struct Lisp_Vector *p;
2421 XSETFASTINT (len, nargs);
2422 val = Fmake_vector (len, Qnil);
2423 p = XVECTOR (val);
2424 for (index = 0; index < nargs; index++)
2425 p->contents[index] = args[index];
2426 return val;
2430 DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0,
2431 doc: /* Create a byte-code object with specified arguments as elements.
2432 The arguments should be the arglist, bytecode-string, constant vector,
2433 stack size, (optional) doc string, and (optional) interactive spec.
2434 The first four arguments are required; at most six have any
2435 significance.
2436 usage: (make-byte-code &rest ELEMENTS) */)
2437 (nargs, args)
2438 register int nargs;
2439 Lisp_Object *args;
2441 register Lisp_Object len, val;
2442 register int index;
2443 register struct Lisp_Vector *p;
2445 XSETFASTINT (len, nargs);
2446 if (!NILP (Vpurify_flag))
2447 val = make_pure_vector ((EMACS_INT) nargs);
2448 else
2449 val = Fmake_vector (len, Qnil);
2451 if (STRINGP (args[1]) && STRING_MULTIBYTE (args[1]))
2452 /* BYTECODE-STRING must have been produced by Emacs 20.2 or the
2453 earlier because they produced a raw 8-bit string for byte-code
2454 and now such a byte-code string is loaded as multibyte while
2455 raw 8-bit characters converted to multibyte form. Thus, now we
2456 must convert them back to the original unibyte form. */
2457 args[1] = Fstring_as_unibyte (args[1]);
2459 p = XVECTOR (val);
2460 for (index = 0; index < nargs; index++)
2462 if (!NILP (Vpurify_flag))
2463 args[index] = Fpurecopy (args[index]);
2464 p->contents[index] = args[index];
2466 XSETCOMPILED (val, p);
2467 return val;
2472 /***********************************************************************
2473 Symbol Allocation
2474 ***********************************************************************/
2476 /* Each symbol_block is just under 1020 bytes long, since malloc
2477 really allocates in units of powers of two and uses 4 bytes for its
2478 own overhead. */
2480 #define SYMBOL_BLOCK_SIZE \
2481 ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol))
2483 struct symbol_block
2485 struct symbol_block *next;
2486 struct Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE];
2489 /* Current symbol block and index of first unused Lisp_Symbol
2490 structure in it. */
2492 struct symbol_block *symbol_block;
2493 int symbol_block_index;
2495 /* List of free symbols. */
2497 struct Lisp_Symbol *symbol_free_list;
2499 /* Total number of symbol blocks now in use. */
2501 int n_symbol_blocks;
2504 /* Initialize symbol allocation. */
2506 void
2507 init_symbol ()
2509 symbol_block = (struct symbol_block *) lisp_malloc (sizeof *symbol_block,
2510 MEM_TYPE_SYMBOL);
2511 symbol_block->next = 0;
2512 bzero ((char *) symbol_block->symbols, sizeof symbol_block->symbols);
2513 symbol_block_index = 0;
2514 symbol_free_list = 0;
2515 n_symbol_blocks = 1;
2519 DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0,
2520 doc: /* Return a newly allocated uninterned symbol whose name is NAME.
2521 Its value and function definition are void, and its property list is nil. */)
2522 (name)
2523 Lisp_Object name;
2525 register Lisp_Object val;
2526 register struct Lisp_Symbol *p;
2528 CHECK_STRING (name);
2530 if (symbol_free_list)
2532 XSETSYMBOL (val, symbol_free_list);
2533 symbol_free_list = *(struct Lisp_Symbol **)&symbol_free_list->value;
2535 else
2537 if (symbol_block_index == SYMBOL_BLOCK_SIZE)
2539 struct symbol_block *new;
2540 new = (struct symbol_block *) lisp_malloc (sizeof *new,
2541 MEM_TYPE_SYMBOL);
2542 VALIDATE_LISP_STORAGE (new, sizeof *new);
2543 new->next = symbol_block;
2544 symbol_block = new;
2545 symbol_block_index = 0;
2546 n_symbol_blocks++;
2548 XSETSYMBOL (val, &symbol_block->symbols[symbol_block_index++]);
2551 p = XSYMBOL (val);
2552 p->xname = name;
2553 p->plist = Qnil;
2554 p->value = Qunbound;
2555 p->function = Qunbound;
2556 p->next = NULL;
2557 p->interned = SYMBOL_UNINTERNED;
2558 p->constant = 0;
2559 p->indirect_variable = 0;
2560 consing_since_gc += sizeof (struct Lisp_Symbol);
2561 symbols_consed++;
2562 return val;
2567 /***********************************************************************
2568 Marker (Misc) Allocation
2569 ***********************************************************************/
2571 /* Allocation of markers and other objects that share that structure.
2572 Works like allocation of conses. */
2574 #define MARKER_BLOCK_SIZE \
2575 ((1020 - sizeof (struct marker_block *)) / sizeof (union Lisp_Misc))
2577 struct marker_block
2579 struct marker_block *next;
2580 union Lisp_Misc markers[MARKER_BLOCK_SIZE];
2583 struct marker_block *marker_block;
2584 int marker_block_index;
2586 union Lisp_Misc *marker_free_list;
2588 /* Total number of marker blocks now in use. */
2590 int n_marker_blocks;
2592 void
2593 init_marker ()
2595 marker_block = (struct marker_block *) lisp_malloc (sizeof *marker_block,
2596 MEM_TYPE_MISC);
2597 marker_block->next = 0;
2598 bzero ((char *) marker_block->markers, sizeof marker_block->markers);
2599 marker_block_index = 0;
2600 marker_free_list = 0;
2601 n_marker_blocks = 1;
2604 /* Return a newly allocated Lisp_Misc object, with no substructure. */
2606 Lisp_Object
2607 allocate_misc ()
2609 Lisp_Object val;
2611 if (marker_free_list)
2613 XSETMISC (val, marker_free_list);
2614 marker_free_list = marker_free_list->u_free.chain;
2616 else
2618 if (marker_block_index == MARKER_BLOCK_SIZE)
2620 struct marker_block *new;
2621 new = (struct marker_block *) lisp_malloc (sizeof *new,
2622 MEM_TYPE_MISC);
2623 VALIDATE_LISP_STORAGE (new, sizeof *new);
2624 new->next = marker_block;
2625 marker_block = new;
2626 marker_block_index = 0;
2627 n_marker_blocks++;
2629 XSETMISC (val, &marker_block->markers[marker_block_index++]);
2632 consing_since_gc += sizeof (union Lisp_Misc);
2633 misc_objects_consed++;
2634 return val;
2637 /* Return a Lisp_Misc_Save_Value object containing POINTER and
2638 INTEGER. This is used to package C values to call record_unwind_protect.
2639 The unwind function can get the C values back using XSAVE_VALUE. */
2641 Lisp_Object
2642 make_save_value (pointer, integer)
2643 void *pointer;
2644 int integer;
2646 register Lisp_Object val;
2647 register struct Lisp_Save_Value *p;
2649 val = allocate_misc ();
2650 XMISCTYPE (val) = Lisp_Misc_Save_Value;
2651 p = XSAVE_VALUE (val);
2652 p->pointer = pointer;
2653 p->integer = integer;
2654 return val;
2657 DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
2658 doc: /* Return a newly allocated marker which does not point at any place. */)
2661 register Lisp_Object val;
2662 register struct Lisp_Marker *p;
2664 val = allocate_misc ();
2665 XMISCTYPE (val) = Lisp_Misc_Marker;
2666 p = XMARKER (val);
2667 p->buffer = 0;
2668 p->bytepos = 0;
2669 p->charpos = 0;
2670 p->chain = Qnil;
2671 p->insertion_type = 0;
2672 return val;
2675 /* Put MARKER back on the free list after using it temporarily. */
2677 void
2678 free_marker (marker)
2679 Lisp_Object marker;
2681 unchain_marker (marker);
2683 XMISC (marker)->u_marker.type = Lisp_Misc_Free;
2684 XMISC (marker)->u_free.chain = marker_free_list;
2685 marker_free_list = XMISC (marker);
2687 total_free_markers++;
2691 /* Return a newly created vector or string with specified arguments as
2692 elements. If all the arguments are characters that can fit
2693 in a string of events, make a string; otherwise, make a vector.
2695 Any number of arguments, even zero arguments, are allowed. */
2697 Lisp_Object
2698 make_event_array (nargs, args)
2699 register int nargs;
2700 Lisp_Object *args;
2702 int i;
2704 for (i = 0; i < nargs; i++)
2705 /* The things that fit in a string
2706 are characters that are in 0...127,
2707 after discarding the meta bit and all the bits above it. */
2708 if (!INTEGERP (args[i])
2709 || (XUINT (args[i]) & ~(-CHAR_META)) >= 0200)
2710 return Fvector (nargs, args);
2712 /* Since the loop exited, we know that all the things in it are
2713 characters, so we can make a string. */
2715 Lisp_Object result;
2717 result = Fmake_string (make_number (nargs), make_number (0));
2718 for (i = 0; i < nargs; i++)
2720 SSET (result, i, XINT (args[i]));
2721 /* Move the meta bit to the right place for a string char. */
2722 if (XINT (args[i]) & CHAR_META)
2723 SSET (result, i, SREF (result, i) | 0x80);
2726 return result;
2732 /************************************************************************
2733 C Stack Marking
2734 ************************************************************************/
2736 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
2738 /* Conservative C stack marking requires a method to identify possibly
2739 live Lisp objects given a pointer value. We do this by keeping
2740 track of blocks of Lisp data that are allocated in a red-black tree
2741 (see also the comment of mem_node which is the type of nodes in
2742 that tree). Function lisp_malloc adds information for an allocated
2743 block to the red-black tree with calls to mem_insert, and function
2744 lisp_free removes it with mem_delete. Functions live_string_p etc
2745 call mem_find to lookup information about a given pointer in the
2746 tree, and use that to determine if the pointer points to a Lisp
2747 object or not. */
2749 /* Initialize this part of alloc.c. */
2751 static void
2752 mem_init ()
2754 mem_z.left = mem_z.right = MEM_NIL;
2755 mem_z.parent = NULL;
2756 mem_z.color = MEM_BLACK;
2757 mem_z.start = mem_z.end = NULL;
2758 mem_root = MEM_NIL;
2762 /* Value is a pointer to the mem_node containing START. Value is
2763 MEM_NIL if there is no node in the tree containing START. */
2765 static INLINE struct mem_node *
2766 mem_find (start)
2767 void *start;
2769 struct mem_node *p;
2771 if (start < min_heap_address || start > max_heap_address)
2772 return MEM_NIL;
2774 /* Make the search always successful to speed up the loop below. */
2775 mem_z.start = start;
2776 mem_z.end = (char *) start + 1;
2778 p = mem_root;
2779 while (start < p->start || start >= p->end)
2780 p = start < p->start ? p->left : p->right;
2781 return p;
2785 /* Insert a new node into the tree for a block of memory with start
2786 address START, end address END, and type TYPE. Value is a
2787 pointer to the node that was inserted. */
2789 static struct mem_node *
2790 mem_insert (start, end, type)
2791 void *start, *end;
2792 enum mem_type type;
2794 struct mem_node *c, *parent, *x;
2796 if (start < min_heap_address)
2797 min_heap_address = start;
2798 if (end > max_heap_address)
2799 max_heap_address = end;
2801 /* See where in the tree a node for START belongs. In this
2802 particular application, it shouldn't happen that a node is already
2803 present. For debugging purposes, let's check that. */
2804 c = mem_root;
2805 parent = NULL;
2807 #if GC_MARK_STACK != GC_MAKE_GCPROS_NOOPS
2809 while (c != MEM_NIL)
2811 if (start >= c->start && start < c->end)
2812 abort ();
2813 parent = c;
2814 c = start < c->start ? c->left : c->right;
2817 #else /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
2819 while (c != MEM_NIL)
2821 parent = c;
2822 c = start < c->start ? c->left : c->right;
2825 #endif /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
2827 /* Create a new node. */
2828 #ifdef GC_MALLOC_CHECK
2829 x = (struct mem_node *) _malloc_internal (sizeof *x);
2830 if (x == NULL)
2831 abort ();
2832 #else
2833 x = (struct mem_node *) xmalloc (sizeof *x);
2834 #endif
2835 x->start = start;
2836 x->end = end;
2837 x->type = type;
2838 x->parent = parent;
2839 x->left = x->right = MEM_NIL;
2840 x->color = MEM_RED;
2842 /* Insert it as child of PARENT or install it as root. */
2843 if (parent)
2845 if (start < parent->start)
2846 parent->left = x;
2847 else
2848 parent->right = x;
2850 else
2851 mem_root = x;
2853 /* Re-establish red-black tree properties. */
2854 mem_insert_fixup (x);
2856 return x;
2860 /* Re-establish the red-black properties of the tree, and thereby
2861 balance the tree, after node X has been inserted; X is always red. */
2863 static void
2864 mem_insert_fixup (x)
2865 struct mem_node *x;
2867 while (x != mem_root && x->parent->color == MEM_RED)
2869 /* X is red and its parent is red. This is a violation of
2870 red-black tree property #3. */
2872 if (x->parent == x->parent->parent->left)
2874 /* We're on the left side of our grandparent, and Y is our
2875 "uncle". */
2876 struct mem_node *y = x->parent->parent->right;
2878 if (y->color == MEM_RED)
2880 /* Uncle and parent are red but should be black because
2881 X is red. Change the colors accordingly and proceed
2882 with the grandparent. */
2883 x->parent->color = MEM_BLACK;
2884 y->color = MEM_BLACK;
2885 x->parent->parent->color = MEM_RED;
2886 x = x->parent->parent;
2888 else
2890 /* Parent and uncle have different colors; parent is
2891 red, uncle is black. */
2892 if (x == x->parent->right)
2894 x = x->parent;
2895 mem_rotate_left (x);
2898 x->parent->color = MEM_BLACK;
2899 x->parent->parent->color = MEM_RED;
2900 mem_rotate_right (x->parent->parent);
2903 else
2905 /* This is the symmetrical case of above. */
2906 struct mem_node *y = x->parent->parent->left;
2908 if (y->color == MEM_RED)
2910 x->parent->color = MEM_BLACK;
2911 y->color = MEM_BLACK;
2912 x->parent->parent->color = MEM_RED;
2913 x = x->parent->parent;
2915 else
2917 if (x == x->parent->left)
2919 x = x->parent;
2920 mem_rotate_right (x);
2923 x->parent->color = MEM_BLACK;
2924 x->parent->parent->color = MEM_RED;
2925 mem_rotate_left (x->parent->parent);
2930 /* The root may have been changed to red due to the algorithm. Set
2931 it to black so that property #5 is satisfied. */
2932 mem_root->color = MEM_BLACK;
2936 /* (x) (y)
2937 / \ / \
2938 a (y) ===> (x) c
2939 / \ / \
2940 b c a b */
2942 static void
2943 mem_rotate_left (x)
2944 struct mem_node *x;
2946 struct mem_node *y;
2948 /* Turn y's left sub-tree into x's right sub-tree. */
2949 y = x->right;
2950 x->right = y->left;
2951 if (y->left != MEM_NIL)
2952 y->left->parent = x;
2954 /* Y's parent was x's parent. */
2955 if (y != MEM_NIL)
2956 y->parent = x->parent;
2958 /* Get the parent to point to y instead of x. */
2959 if (x->parent)
2961 if (x == x->parent->left)
2962 x->parent->left = y;
2963 else
2964 x->parent->right = y;
2966 else
2967 mem_root = y;
2969 /* Put x on y's left. */
2970 y->left = x;
2971 if (x != MEM_NIL)
2972 x->parent = y;
2976 /* (x) (Y)
2977 / \ / \
2978 (y) c ===> a (x)
2979 / \ / \
2980 a b b c */
2982 static void
2983 mem_rotate_right (x)
2984 struct mem_node *x;
2986 struct mem_node *y = x->left;
2988 x->left = y->right;
2989 if (y->right != MEM_NIL)
2990 y->right->parent = x;
2992 if (y != MEM_NIL)
2993 y->parent = x->parent;
2994 if (x->parent)
2996 if (x == x->parent->right)
2997 x->parent->right = y;
2998 else
2999 x->parent->left = y;
3001 else
3002 mem_root = y;
3004 y->right = x;
3005 if (x != MEM_NIL)
3006 x->parent = y;
3010 /* Delete node Z from the tree. If Z is null or MEM_NIL, do nothing. */
3012 static void
3013 mem_delete (z)
3014 struct mem_node *z;
3016 struct mem_node *x, *y;
3018 if (!z || z == MEM_NIL)
3019 return;
3021 if (z->left == MEM_NIL || z->right == MEM_NIL)
3022 y = z;
3023 else
3025 y = z->right;
3026 while (y->left != MEM_NIL)
3027 y = y->left;
3030 if (y->left != MEM_NIL)
3031 x = y->left;
3032 else
3033 x = y->right;
3035 x->parent = y->parent;
3036 if (y->parent)
3038 if (y == y->parent->left)
3039 y->parent->left = x;
3040 else
3041 y->parent->right = x;
3043 else
3044 mem_root = x;
3046 if (y != z)
3048 z->start = y->start;
3049 z->end = y->end;
3050 z->type = y->type;
3053 if (y->color == MEM_BLACK)
3054 mem_delete_fixup (x);
3056 #ifdef GC_MALLOC_CHECK
3057 _free_internal (y);
3058 #else
3059 xfree (y);
3060 #endif
3064 /* Re-establish the red-black properties of the tree, after a
3065 deletion. */
3067 static void
3068 mem_delete_fixup (x)
3069 struct mem_node *x;
3071 while (x != mem_root && x->color == MEM_BLACK)
3073 if (x == x->parent->left)
3075 struct mem_node *w = x->parent->right;
3077 if (w->color == MEM_RED)
3079 w->color = MEM_BLACK;
3080 x->parent->color = MEM_RED;
3081 mem_rotate_left (x->parent);
3082 w = x->parent->right;
3085 if (w->left->color == MEM_BLACK && w->right->color == MEM_BLACK)
3087 w->color = MEM_RED;
3088 x = x->parent;
3090 else
3092 if (w->right->color == MEM_BLACK)
3094 w->left->color = MEM_BLACK;
3095 w->color = MEM_RED;
3096 mem_rotate_right (w);
3097 w = x->parent->right;
3099 w->color = x->parent->color;
3100 x->parent->color = MEM_BLACK;
3101 w->right->color = MEM_BLACK;
3102 mem_rotate_left (x->parent);
3103 x = mem_root;
3106 else
3108 struct mem_node *w = x->parent->left;
3110 if (w->color == MEM_RED)
3112 w->color = MEM_BLACK;
3113 x->parent->color = MEM_RED;
3114 mem_rotate_right (x->parent);
3115 w = x->parent->left;
3118 if (w->right->color == MEM_BLACK && w->left->color == MEM_BLACK)
3120 w->color = MEM_RED;
3121 x = x->parent;
3123 else
3125 if (w->left->color == MEM_BLACK)
3127 w->right->color = MEM_BLACK;
3128 w->color = MEM_RED;
3129 mem_rotate_left (w);
3130 w = x->parent->left;
3133 w->color = x->parent->color;
3134 x->parent->color = MEM_BLACK;
3135 w->left->color = MEM_BLACK;
3136 mem_rotate_right (x->parent);
3137 x = mem_root;
3142 x->color = MEM_BLACK;
3146 /* Value is non-zero if P is a pointer to a live Lisp string on
3147 the heap. M is a pointer to the mem_block for P. */
3149 static INLINE int
3150 live_string_p (m, p)
3151 struct mem_node *m;
3152 void *p;
3154 if (m->type == MEM_TYPE_STRING)
3156 struct string_block *b = (struct string_block *) m->start;
3157 int offset = (char *) p - (char *) &b->strings[0];
3159 /* P must point to the start of a Lisp_String structure, and it
3160 must not be on the free-list. */
3161 return (offset >= 0
3162 && offset % sizeof b->strings[0] == 0
3163 && ((struct Lisp_String *) p)->data != NULL);
3165 else
3166 return 0;
3170 /* Value is non-zero if P is a pointer to a live Lisp cons on
3171 the heap. M is a pointer to the mem_block for P. */
3173 static INLINE int
3174 live_cons_p (m, p)
3175 struct mem_node *m;
3176 void *p;
3178 if (m->type == MEM_TYPE_CONS)
3180 struct cons_block *b = (struct cons_block *) m->start;
3181 int offset = (char *) p - (char *) &b->conses[0];
3183 /* P must point to the start of a Lisp_Cons, not be
3184 one of the unused cells in the current cons block,
3185 and not be on the free-list. */
3186 return (offset >= 0
3187 && offset % sizeof b->conses[0] == 0
3188 && (b != cons_block
3189 || offset / sizeof b->conses[0] < cons_block_index)
3190 && !EQ (((struct Lisp_Cons *) p)->car, Vdead));
3192 else
3193 return 0;
3197 /* Value is non-zero if P is a pointer to a live Lisp symbol on
3198 the heap. M is a pointer to the mem_block for P. */
3200 static INLINE int
3201 live_symbol_p (m, p)
3202 struct mem_node *m;
3203 void *p;
3205 if (m->type == MEM_TYPE_SYMBOL)
3207 struct symbol_block *b = (struct symbol_block *) m->start;
3208 int offset = (char *) p - (char *) &b->symbols[0];
3210 /* P must point to the start of a Lisp_Symbol, not be
3211 one of the unused cells in the current symbol block,
3212 and not be on the free-list. */
3213 return (offset >= 0
3214 && offset % sizeof b->symbols[0] == 0
3215 && (b != symbol_block
3216 || offset / sizeof b->symbols[0] < symbol_block_index)
3217 && !EQ (((struct Lisp_Symbol *) p)->function, Vdead));
3219 else
3220 return 0;
3224 /* Value is non-zero if P is a pointer to a live Lisp float on
3225 the heap. M is a pointer to the mem_block for P. */
3227 static INLINE int
3228 live_float_p (m, p)
3229 struct mem_node *m;
3230 void *p;
3232 if (m->type == MEM_TYPE_FLOAT)
3234 struct float_block *b = (struct float_block *) m->start;
3235 int offset = (char *) p - (char *) &b->floats[0];
3237 /* P must point to the start of a Lisp_Float, not be
3238 one of the unused cells in the current float block,
3239 and not be on the free-list. */
3240 return (offset >= 0
3241 && offset % sizeof b->floats[0] == 0
3242 && (b != float_block
3243 || offset / sizeof b->floats[0] < float_block_index)
3244 && !EQ (((struct Lisp_Float *) p)->type, Vdead));
3246 else
3247 return 0;
3251 /* Value is non-zero if P is a pointer to a live Lisp Misc on
3252 the heap. M is a pointer to the mem_block for P. */
3254 static INLINE int
3255 live_misc_p (m, p)
3256 struct mem_node *m;
3257 void *p;
3259 if (m->type == MEM_TYPE_MISC)
3261 struct marker_block *b = (struct marker_block *) m->start;
3262 int offset = (char *) p - (char *) &b->markers[0];
3264 /* P must point to the start of a Lisp_Misc, not be
3265 one of the unused cells in the current misc block,
3266 and not be on the free-list. */
3267 return (offset >= 0
3268 && offset % sizeof b->markers[0] == 0
3269 && (b != marker_block
3270 || offset / sizeof b->markers[0] < marker_block_index)
3271 && ((union Lisp_Misc *) p)->u_marker.type != Lisp_Misc_Free);
3273 else
3274 return 0;
3278 /* Value is non-zero if P is a pointer to a live vector-like object.
3279 M is a pointer to the mem_block for P. */
3281 static INLINE int
3282 live_vector_p (m, p)
3283 struct mem_node *m;
3284 void *p;
3286 return (p == m->start
3287 && m->type >= MEM_TYPE_VECTOR
3288 && m->type <= MEM_TYPE_WINDOW);
3292 /* Value is non-zero of P is a pointer to a live buffer. M is a
3293 pointer to the mem_block for P. */
3295 static INLINE int
3296 live_buffer_p (m, p)
3297 struct mem_node *m;
3298 void *p;
3300 /* P must point to the start of the block, and the buffer
3301 must not have been killed. */
3302 return (m->type == MEM_TYPE_BUFFER
3303 && p == m->start
3304 && !NILP (((struct buffer *) p)->name));
3307 #endif /* GC_MARK_STACK || defined GC_MALLOC_CHECK */
3309 #if GC_MARK_STACK
3311 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3313 /* Array of objects that are kept alive because the C stack contains
3314 a pattern that looks like a reference to them . */
3316 #define MAX_ZOMBIES 10
3317 static Lisp_Object zombies[MAX_ZOMBIES];
3319 /* Number of zombie objects. */
3321 static int nzombies;
3323 /* Number of garbage collections. */
3325 static int ngcs;
3327 /* Average percentage of zombies per collection. */
3329 static double avg_zombies;
3331 /* Max. number of live and zombie objects. */
3333 static int max_live, max_zombies;
3335 /* Average number of live objects per GC. */
3337 static double avg_live;
3339 DEFUN ("gc-status", Fgc_status, Sgc_status, 0, 0, "",
3340 doc: /* Show information about live and zombie objects. */)
3343 Lisp_Object args[8], zombie_list = Qnil;
3344 int i;
3345 for (i = 0; i < nzombies; i++)
3346 zombie_list = Fcons (zombies[i], zombie_list);
3347 args[0] = build_string ("%d GCs, avg live/zombies = %.2f/%.2f (%f%%), max %d/%d\nzombies: %S");
3348 args[1] = make_number (ngcs);
3349 args[2] = make_float (avg_live);
3350 args[3] = make_float (avg_zombies);
3351 args[4] = make_float (avg_zombies / avg_live / 100);
3352 args[5] = make_number (max_live);
3353 args[6] = make_number (max_zombies);
3354 args[7] = zombie_list;
3355 return Fmessage (8, args);
3358 #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
3361 /* Mark OBJ if we can prove it's a Lisp_Object. */
3363 static INLINE void
3364 mark_maybe_object (obj)
3365 Lisp_Object obj;
3367 void *po = (void *) XPNTR (obj);
3368 struct mem_node *m = mem_find (po);
3370 if (m != MEM_NIL)
3372 int mark_p = 0;
3374 switch (XGCTYPE (obj))
3376 case Lisp_String:
3377 mark_p = (live_string_p (m, po)
3378 && !STRING_MARKED_P ((struct Lisp_String *) po));
3379 break;
3381 case Lisp_Cons:
3382 mark_p = (live_cons_p (m, po)
3383 && !XMARKBIT (XCONS (obj)->car));
3384 break;
3386 case Lisp_Symbol:
3387 mark_p = (live_symbol_p (m, po)
3388 && !XMARKBIT (XSYMBOL (obj)->plist));
3389 break;
3391 case Lisp_Float:
3392 mark_p = (live_float_p (m, po)
3393 && !XMARKBIT (XFLOAT (obj)->type));
3394 break;
3396 case Lisp_Vectorlike:
3397 /* Note: can't check GC_BUFFERP before we know it's a
3398 buffer because checking that dereferences the pointer
3399 PO which might point anywhere. */
3400 if (live_vector_p (m, po))
3401 mark_p = (!GC_SUBRP (obj)
3402 && !(XVECTOR (obj)->size & ARRAY_MARK_FLAG));
3403 else if (live_buffer_p (m, po))
3404 mark_p = GC_BUFFERP (obj) && !XMARKBIT (XBUFFER (obj)->name);
3405 break;
3407 case Lisp_Misc:
3408 if (live_misc_p (m, po))
3410 switch (XMISCTYPE (obj))
3412 case Lisp_Misc_Marker:
3413 mark_p = !XMARKBIT (XMARKER (obj)->chain);
3414 break;
3416 case Lisp_Misc_Buffer_Local_Value:
3417 case Lisp_Misc_Some_Buffer_Local_Value:
3418 mark_p = !XMARKBIT (XBUFFER_LOCAL_VALUE (obj)->realvalue);
3419 break;
3421 case Lisp_Misc_Overlay:
3422 mark_p = !XMARKBIT (XOVERLAY (obj)->plist);
3423 break;
3426 break;
3428 case Lisp_Int:
3429 case Lisp_Type_Limit:
3430 break;
3433 if (mark_p)
3435 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3436 if (nzombies < MAX_ZOMBIES)
3437 zombies[nzombies] = obj;
3438 ++nzombies;
3439 #endif
3440 mark_object (&obj);
3446 /* If P points to Lisp data, mark that as live if it isn't already
3447 marked. */
3449 static INLINE void
3450 mark_maybe_pointer (p)
3451 void *p;
3453 struct mem_node *m;
3455 /* Quickly rule out some values which can't point to Lisp data. We
3456 assume that Lisp data is aligned on even addresses. */
3457 if ((EMACS_INT) p & 1)
3458 return;
3460 m = mem_find (p);
3461 if (m != MEM_NIL)
3463 Lisp_Object obj = Qnil;
3465 switch (m->type)
3467 case MEM_TYPE_NON_LISP:
3468 /* Nothing to do; not a pointer to Lisp memory. */
3469 break;
3471 case MEM_TYPE_BUFFER:
3472 if (live_buffer_p (m, p)
3473 && !XMARKBIT (((struct buffer *) p)->name))
3474 XSETVECTOR (obj, p);
3475 break;
3477 case MEM_TYPE_CONS:
3478 if (live_cons_p (m, p)
3479 && !XMARKBIT (((struct Lisp_Cons *) p)->car))
3480 XSETCONS (obj, p);
3481 break;
3483 case MEM_TYPE_STRING:
3484 if (live_string_p (m, p)
3485 && !STRING_MARKED_P ((struct Lisp_String *) p))
3486 XSETSTRING (obj, p);
3487 break;
3489 case MEM_TYPE_MISC:
3490 if (live_misc_p (m, p))
3492 Lisp_Object tem;
3493 XSETMISC (tem, p);
3495 switch (XMISCTYPE (tem))
3497 case Lisp_Misc_Marker:
3498 if (!XMARKBIT (XMARKER (tem)->chain))
3499 obj = tem;
3500 break;
3502 case Lisp_Misc_Buffer_Local_Value:
3503 case Lisp_Misc_Some_Buffer_Local_Value:
3504 if (!XMARKBIT (XBUFFER_LOCAL_VALUE (tem)->realvalue))
3505 obj = tem;
3506 break;
3508 case Lisp_Misc_Overlay:
3509 if (!XMARKBIT (XOVERLAY (tem)->plist))
3510 obj = tem;
3511 break;
3514 break;
3516 case MEM_TYPE_SYMBOL:
3517 if (live_symbol_p (m, p)
3518 && !XMARKBIT (((struct Lisp_Symbol *) p)->plist))
3519 XSETSYMBOL (obj, p);
3520 break;
3522 case MEM_TYPE_FLOAT:
3523 if (live_float_p (m, p)
3524 && !XMARKBIT (((struct Lisp_Float *) p)->type))
3525 XSETFLOAT (obj, p);
3526 break;
3528 case MEM_TYPE_VECTOR:
3529 case MEM_TYPE_PROCESS:
3530 case MEM_TYPE_HASH_TABLE:
3531 case MEM_TYPE_FRAME:
3532 case MEM_TYPE_WINDOW:
3533 if (live_vector_p (m, p))
3535 Lisp_Object tem;
3536 XSETVECTOR (tem, p);
3537 if (!GC_SUBRP (tem)
3538 && !(XVECTOR (tem)->size & ARRAY_MARK_FLAG))
3539 obj = tem;
3541 break;
3543 default:
3544 abort ();
3547 if (!GC_NILP (obj))
3548 mark_object (&obj);
3553 /* Mark Lisp objects referenced from the address range START..END. */
3555 static void
3556 mark_memory (start, end)
3557 void *start, *end;
3559 Lisp_Object *p;
3560 void **pp;
3562 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3563 nzombies = 0;
3564 #endif
3566 /* Make START the pointer to the start of the memory region,
3567 if it isn't already. */
3568 if (end < start)
3570 void *tem = start;
3571 start = end;
3572 end = tem;
3575 /* Mark Lisp_Objects. */
3576 for (p = (Lisp_Object *) start; (void *) p < end; ++p)
3577 mark_maybe_object (*p);
3579 /* Mark Lisp data pointed to. This is necessary because, in some
3580 situations, the C compiler optimizes Lisp objects away, so that
3581 only a pointer to them remains. Example:
3583 DEFUN ("testme", Ftestme, Stestme, 0, 0, 0, "")
3586 Lisp_Object obj = build_string ("test");
3587 struct Lisp_String *s = XSTRING (obj);
3588 Fgarbage_collect ();
3589 fprintf (stderr, "test `%s'\n", s->data);
3590 return Qnil;
3593 Here, `obj' isn't really used, and the compiler optimizes it
3594 away. The only reference to the life string is through the
3595 pointer `s'. */
3597 for (pp = (void **) start; (void *) pp < end; ++pp)
3598 mark_maybe_pointer (*pp);
3601 /* setjmp will work with GCC unless NON_SAVING_SETJMP is defined in
3602 the GCC system configuration. In gcc 3.2, the only systems for
3603 which this is so are i386-sco5 non-ELF, i386-sysv3 (maybe included
3604 by others?) and ns32k-pc532-min. */
3606 #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
3608 static int setjmp_tested_p, longjmps_done;
3610 #define SETJMP_WILL_LIKELY_WORK "\
3612 Emacs garbage collector has been changed to use conservative stack\n\
3613 marking. Emacs has determined that the method it uses to do the\n\
3614 marking will likely work on your system, but this isn't sure.\n\
3616 If you are a system-programmer, or can get the help of a local wizard\n\
3617 who is, please take a look at the function mark_stack in alloc.c, and\n\
3618 verify that the methods used are appropriate for your system.\n\
3620 Please mail the result to <emacs-devel@gnu.org>.\n\
3623 #define SETJMP_WILL_NOT_WORK "\
3625 Emacs garbage collector has been changed to use conservative stack\n\
3626 marking. Emacs has determined that the default method it uses to do the\n\
3627 marking will not work on your system. We will need a system-dependent\n\
3628 solution for your system.\n\
3630 Please take a look at the function mark_stack in alloc.c, and\n\
3631 try to find a way to make it work on your system.\n\
3633 Note that you may get false negatives, depending on the compiler.\n\
3634 In particular, you need to use -O with GCC for this test.\n\
3636 Please mail the result to <emacs-devel@gnu.org>.\n\
3640 /* Perform a quick check if it looks like setjmp saves registers in a
3641 jmp_buf. Print a message to stderr saying so. When this test
3642 succeeds, this is _not_ a proof that setjmp is sufficient for
3643 conservative stack marking. Only the sources or a disassembly
3644 can prove that. */
3646 static void
3647 test_setjmp ()
3649 char buf[10];
3650 register int x;
3651 jmp_buf jbuf;
3652 int result = 0;
3654 /* Arrange for X to be put in a register. */
3655 sprintf (buf, "1");
3656 x = strlen (buf);
3657 x = 2 * x - 1;
3659 setjmp (jbuf);
3660 if (longjmps_done == 1)
3662 /* Came here after the longjmp at the end of the function.
3664 If x == 1, the longjmp has restored the register to its
3665 value before the setjmp, and we can hope that setjmp
3666 saves all such registers in the jmp_buf, although that
3667 isn't sure.
3669 For other values of X, either something really strange is
3670 taking place, or the setjmp just didn't save the register. */
3672 if (x == 1)
3673 fprintf (stderr, SETJMP_WILL_LIKELY_WORK);
3674 else
3676 fprintf (stderr, SETJMP_WILL_NOT_WORK);
3677 exit (1);
3681 ++longjmps_done;
3682 x = 2;
3683 if (longjmps_done == 1)
3684 longjmp (jbuf, 1);
3687 #endif /* not GC_SAVE_REGISTERS_ON_STACK && not GC_SETJMP_WORKS */
3690 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
3692 /* Abort if anything GCPRO'd doesn't survive the GC. */
3694 static void
3695 check_gcpros ()
3697 struct gcpro *p;
3698 int i;
3700 for (p = gcprolist; p; p = p->next)
3701 for (i = 0; i < p->nvars; ++i)
3702 if (!survives_gc_p (p->var[i]))
3703 abort ();
3706 #elif GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3708 static void
3709 dump_zombies ()
3711 int i;
3713 fprintf (stderr, "\nZombies kept alive = %d:\n", nzombies);
3714 for (i = 0; i < min (MAX_ZOMBIES, nzombies); ++i)
3716 fprintf (stderr, " %d = ", i);
3717 debug_print (zombies[i]);
3721 #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
3724 /* Mark live Lisp objects on the C stack.
3726 There are several system-dependent problems to consider when
3727 porting this to new architectures:
3729 Processor Registers
3731 We have to mark Lisp objects in CPU registers that can hold local
3732 variables or are used to pass parameters.
3734 If GC_SAVE_REGISTERS_ON_STACK is defined, it should expand to
3735 something that either saves relevant registers on the stack, or
3736 calls mark_maybe_object passing it each register's contents.
3738 If GC_SAVE_REGISTERS_ON_STACK is not defined, the current
3739 implementation assumes that calling setjmp saves registers we need
3740 to see in a jmp_buf which itself lies on the stack. This doesn't
3741 have to be true! It must be verified for each system, possibly
3742 by taking a look at the source code of setjmp.
3744 Stack Layout
3746 Architectures differ in the way their processor stack is organized.
3747 For example, the stack might look like this
3749 +----------------+
3750 | Lisp_Object | size = 4
3751 +----------------+
3752 | something else | size = 2
3753 +----------------+
3754 | Lisp_Object | size = 4
3755 +----------------+
3756 | ... |
3758 In such a case, not every Lisp_Object will be aligned equally. To
3759 find all Lisp_Object on the stack it won't be sufficient to walk
3760 the stack in steps of 4 bytes. Instead, two passes will be
3761 necessary, one starting at the start of the stack, and a second
3762 pass starting at the start of the stack + 2. Likewise, if the
3763 minimal alignment of Lisp_Objects on the stack is 1, four passes
3764 would be necessary, each one starting with one byte more offset
3765 from the stack start.
3767 The current code assumes by default that Lisp_Objects are aligned
3768 equally on the stack. */
3770 static void
3771 mark_stack ()
3773 int i;
3774 jmp_buf j;
3775 volatile int stack_grows_down_p = (char *) &j > (char *) stack_base;
3776 void *end;
3778 /* This trick flushes the register windows so that all the state of
3779 the process is contained in the stack. */
3780 /* Fixme: Code in the Boehm GC sugests flushing (with `flushrs') is
3781 needed on ia64 too. See mach_dep.c, where it also says inline
3782 assembler doesn't work with relevant proprietary compilers. */
3783 #ifdef sparc
3784 asm ("ta 3");
3785 #endif
3787 /* Save registers that we need to see on the stack. We need to see
3788 registers used to hold register variables and registers used to
3789 pass parameters. */
3790 #ifdef GC_SAVE_REGISTERS_ON_STACK
3791 GC_SAVE_REGISTERS_ON_STACK (end);
3792 #else /* not GC_SAVE_REGISTERS_ON_STACK */
3794 #ifndef GC_SETJMP_WORKS /* If it hasn't been checked yet that
3795 setjmp will definitely work, test it
3796 and print a message with the result
3797 of the test. */
3798 if (!setjmp_tested_p)
3800 setjmp_tested_p = 1;
3801 test_setjmp ();
3803 #endif /* GC_SETJMP_WORKS */
3805 setjmp (j);
3806 end = stack_grows_down_p ? (char *) &j + sizeof j : (char *) &j;
3807 #endif /* not GC_SAVE_REGISTERS_ON_STACK */
3809 /* This assumes that the stack is a contiguous region in memory. If
3810 that's not the case, something has to be done here to iterate
3811 over the stack segments. */
3812 #ifndef GC_LISP_OBJECT_ALIGNMENT
3813 #ifdef __GNUC__
3814 #define GC_LISP_OBJECT_ALIGNMENT __alignof__ (Lisp_Object)
3815 #else
3816 #define GC_LISP_OBJECT_ALIGNMENT sizeof (Lisp_Object)
3817 #endif
3818 #endif
3819 for (i = 0; i < sizeof (Lisp_Object); i += GC_LISP_OBJECT_ALIGNMENT)
3820 mark_memory ((char *) stack_base + i, end);
3822 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
3823 check_gcpros ();
3824 #endif
3828 #endif /* GC_MARK_STACK != 0 */
3832 /***********************************************************************
3833 Pure Storage Management
3834 ***********************************************************************/
3836 /* Allocate room for SIZE bytes from pure Lisp storage and return a
3837 pointer to it. TYPE is the Lisp type for which the memory is
3838 allocated. TYPE < 0 means it's not used for a Lisp object.
3840 If store_pure_type_info is set and TYPE is >= 0, the type of
3841 the allocated object is recorded in pure_types. */
3843 static POINTER_TYPE *
3844 pure_alloc (size, type)
3845 size_t size;
3846 int type;
3848 POINTER_TYPE *result;
3849 size_t alignment = sizeof (EMACS_INT);
3851 /* Give Lisp_Floats an extra alignment. */
3852 if (type == Lisp_Float)
3854 #if defined __GNUC__ && __GNUC__ >= 2
3855 alignment = __alignof (struct Lisp_Float);
3856 #else
3857 alignment = sizeof (struct Lisp_Float);
3858 #endif
3861 again:
3862 result = (POINTER_TYPE *) ALIGN ((EMACS_UINT)purebeg + pure_bytes_used, alignment);
3863 pure_bytes_used = ((char *)result - (char *)purebeg) + size;
3865 if (pure_bytes_used <= pure_size)
3866 return result;
3868 /* Don't allocate a large amount here,
3869 because it might get mmap'd and then its address
3870 might not be usable. */
3871 purebeg = (char *) xmalloc (10000);
3872 pure_size = 10000;
3873 pure_bytes_used_before_overflow += pure_bytes_used - size;
3874 pure_bytes_used = 0;
3875 goto again;
3879 /* Print a warning if PURESIZE is too small. */
3881 void
3882 check_pure_size ()
3884 if (pure_bytes_used_before_overflow)
3885 message ("Pure Lisp storage overflow (approx. %d bytes needed)",
3886 (int) (pure_bytes_used + pure_bytes_used_before_overflow));
3890 /* Return a string allocated in pure space. DATA is a buffer holding
3891 NCHARS characters, and NBYTES bytes of string data. MULTIBYTE
3892 non-zero means make the result string multibyte.
3894 Must get an error if pure storage is full, since if it cannot hold
3895 a large string it may be able to hold conses that point to that
3896 string; then the string is not protected from gc. */
3898 Lisp_Object
3899 make_pure_string (data, nchars, nbytes, multibyte)
3900 char *data;
3901 int nchars, nbytes;
3902 int multibyte;
3904 Lisp_Object string;
3905 struct Lisp_String *s;
3907 s = (struct Lisp_String *) pure_alloc (sizeof *s, Lisp_String);
3908 s->data = (unsigned char *) pure_alloc (nbytes + 1, -1);
3909 s->size = nchars;
3910 s->size_byte = multibyte ? nbytes : -1;
3911 bcopy (data, s->data, nbytes);
3912 s->data[nbytes] = '\0';
3913 s->intervals = NULL_INTERVAL;
3914 XSETSTRING (string, s);
3915 return string;
3919 /* Return a cons allocated from pure space. Give it pure copies
3920 of CAR as car and CDR as cdr. */
3922 Lisp_Object
3923 pure_cons (car, cdr)
3924 Lisp_Object car, cdr;
3926 register Lisp_Object new;
3927 struct Lisp_Cons *p;
3929 p = (struct Lisp_Cons *) pure_alloc (sizeof *p, Lisp_Cons);
3930 XSETCONS (new, p);
3931 XSETCAR (new, Fpurecopy (car));
3932 XSETCDR (new, Fpurecopy (cdr));
3933 return new;
3937 /* Value is a float object with value NUM allocated from pure space. */
3939 Lisp_Object
3940 make_pure_float (num)
3941 double num;
3943 register Lisp_Object new;
3944 struct Lisp_Float *p;
3946 p = (struct Lisp_Float *) pure_alloc (sizeof *p, Lisp_Float);
3947 XSETFLOAT (new, p);
3948 XFLOAT_DATA (new) = num;
3949 return new;
3953 /* Return a vector with room for LEN Lisp_Objects allocated from
3954 pure space. */
3956 Lisp_Object
3957 make_pure_vector (len)
3958 EMACS_INT len;
3960 Lisp_Object new;
3961 struct Lisp_Vector *p;
3962 size_t size = sizeof *p + (len - 1) * sizeof (Lisp_Object);
3964 p = (struct Lisp_Vector *) pure_alloc (size, Lisp_Vectorlike);
3965 XSETVECTOR (new, p);
3966 XVECTOR (new)->size = len;
3967 return new;
3971 DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
3972 doc: /* Make a copy of OBJECT in pure storage.
3973 Recursively copies contents of vectors and cons cells.
3974 Does not copy symbols. Copies strings without text properties. */)
3975 (obj)
3976 register Lisp_Object obj;
3978 if (NILP (Vpurify_flag))
3979 return obj;
3981 if (PURE_POINTER_P (XPNTR (obj)))
3982 return obj;
3984 if (CONSP (obj))
3985 return pure_cons (XCAR (obj), XCDR (obj));
3986 else if (FLOATP (obj))
3987 return make_pure_float (XFLOAT_DATA (obj));
3988 else if (STRINGP (obj))
3989 return make_pure_string (SDATA (obj), SCHARS (obj),
3990 SBYTES (obj),
3991 STRING_MULTIBYTE (obj));
3992 else if (COMPILEDP (obj) || VECTORP (obj))
3994 register struct Lisp_Vector *vec;
3995 register int i, size;
3997 size = XVECTOR (obj)->size;
3998 if (size & PSEUDOVECTOR_FLAG)
3999 size &= PSEUDOVECTOR_SIZE_MASK;
4000 vec = XVECTOR (make_pure_vector ((EMACS_INT) size));
4001 for (i = 0; i < size; i++)
4002 vec->contents[i] = Fpurecopy (XVECTOR (obj)->contents[i]);
4003 if (COMPILEDP (obj))
4004 XSETCOMPILED (obj, vec);
4005 else
4006 XSETVECTOR (obj, vec);
4007 return obj;
4009 else if (MARKERP (obj))
4010 error ("Attempt to copy a marker to pure storage");
4012 return obj;
4017 /***********************************************************************
4018 Protection from GC
4019 ***********************************************************************/
4021 /* Put an entry in staticvec, pointing at the variable with address
4022 VARADDRESS. */
4024 void
4025 staticpro (varaddress)
4026 Lisp_Object *varaddress;
4028 staticvec[staticidx++] = varaddress;
4029 if (staticidx >= NSTATICS)
4030 abort ();
4033 struct catchtag
4035 Lisp_Object tag;
4036 Lisp_Object val;
4037 struct catchtag *next;
4040 struct backtrace
4042 struct backtrace *next;
4043 Lisp_Object *function;
4044 Lisp_Object *args; /* Points to vector of args. */
4045 int nargs; /* Length of vector. */
4046 /* If nargs is UNEVALLED, args points to slot holding list of
4047 unevalled args. */
4048 char evalargs;
4053 /***********************************************************************
4054 Protection from GC
4055 ***********************************************************************/
4057 /* Temporarily prevent garbage collection. */
4060 inhibit_garbage_collection ()
4062 int count = SPECPDL_INDEX ();
4063 int nbits = min (VALBITS, BITS_PER_INT);
4065 specbind (Qgc_cons_threshold, make_number (((EMACS_INT) 1 << (nbits - 1)) - 1));
4066 return count;
4070 DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
4071 doc: /* Reclaim storage for Lisp objects no longer needed.
4072 Returns info on amount of space in use:
4073 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)
4074 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS
4075 (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS)
4076 (USED-STRINGS . FREE-STRINGS))
4077 Garbage collection happens automatically if you cons more than
4078 `gc-cons-threshold' bytes of Lisp data since previous garbage collection. */)
4081 register struct gcpro *tail;
4082 register struct specbinding *bind;
4083 struct catchtag *catch;
4084 struct handler *handler;
4085 register struct backtrace *backlist;
4086 char stack_top_variable;
4087 register int i;
4088 int message_p;
4089 Lisp_Object total[8];
4090 int count = SPECPDL_INDEX ();
4091 EMACS_TIME t1, t2, t3;
4093 EMACS_GET_TIME (t1);
4095 /* Can't GC if pure storage overflowed because we can't determine
4096 if something is a pure object or not. */
4097 if (pure_bytes_used_before_overflow)
4098 return Qnil;
4100 /* In case user calls debug_print during GC,
4101 don't let that cause a recursive GC. */
4102 consing_since_gc = 0;
4104 /* Save what's currently displayed in the echo area. */
4105 message_p = push_message ();
4106 record_unwind_protect (pop_message_unwind, Qnil);
4108 /* Save a copy of the contents of the stack, for debugging. */
4109 #if MAX_SAVE_STACK > 0
4110 if (NILP (Vpurify_flag))
4112 i = &stack_top_variable - stack_bottom;
4113 if (i < 0) i = -i;
4114 if (i < MAX_SAVE_STACK)
4116 if (stack_copy == 0)
4117 stack_copy = (char *) xmalloc (stack_copy_size = i);
4118 else if (stack_copy_size < i)
4119 stack_copy = (char *) xrealloc (stack_copy, (stack_copy_size = i));
4120 if (stack_copy)
4122 if ((EMACS_INT) (&stack_top_variable - stack_bottom) > 0)
4123 bcopy (stack_bottom, stack_copy, i);
4124 else
4125 bcopy (&stack_top_variable, stack_copy, i);
4129 #endif /* MAX_SAVE_STACK > 0 */
4131 if (garbage_collection_messages)
4132 message1_nolog ("Garbage collecting...");
4134 BLOCK_INPUT;
4136 shrink_regexp_cache ();
4138 /* Don't keep undo information around forever. */
4140 register struct buffer *nextb = all_buffers;
4142 while (nextb)
4144 /* If a buffer's undo list is Qt, that means that undo is
4145 turned off in that buffer. Calling truncate_undo_list on
4146 Qt tends to return NULL, which effectively turns undo back on.
4147 So don't call truncate_undo_list if undo_list is Qt. */
4148 if (! EQ (nextb->undo_list, Qt))
4149 nextb->undo_list
4150 = truncate_undo_list (nextb->undo_list, undo_limit,
4151 undo_strong_limit);
4153 /* Shrink buffer gaps, but skip indirect and dead buffers. */
4154 if (nextb->base_buffer == 0 && !NILP (nextb->name))
4156 /* If a buffer's gap size is more than 10% of the buffer
4157 size, or larger than 2000 bytes, then shrink it
4158 accordingly. Keep a minimum size of 20 bytes. */
4159 int size = min (2000, max (20, (nextb->text->z_byte / 10)));
4161 if (nextb->text->gap_size > size)
4163 struct buffer *save_current = current_buffer;
4164 current_buffer = nextb;
4165 make_gap (-(nextb->text->gap_size - size));
4166 current_buffer = save_current;
4170 nextb = nextb->next;
4174 gc_in_progress = 1;
4176 /* clear_marks (); */
4178 /* Mark all the special slots that serve as the roots of accessibility.
4180 Usually the special slots to mark are contained in particular structures.
4181 Then we know no slot is marked twice because the structures don't overlap.
4182 In some cases, the structures point to the slots to be marked.
4183 For these, we use MARKBIT to avoid double marking of the slot. */
4185 for (i = 0; i < staticidx; i++)
4186 mark_object (staticvec[i]);
4188 #if (GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS \
4189 || GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS)
4190 mark_stack ();
4191 #else
4192 for (tail = gcprolist; tail; tail = tail->next)
4193 for (i = 0; i < tail->nvars; i++)
4194 if (!XMARKBIT (tail->var[i]))
4196 /* Explicit casting prevents compiler warning about
4197 discarding the `volatile' qualifier. */
4198 mark_object ((Lisp_Object *)&tail->var[i]);
4199 XMARK (tail->var[i]);
4201 #endif
4203 mark_byte_stack ();
4204 for (bind = specpdl; bind != specpdl_ptr; bind++)
4206 mark_object (&bind->symbol);
4207 mark_object (&bind->old_value);
4209 for (catch = catchlist; catch; catch = catch->next)
4211 mark_object (&catch->tag);
4212 mark_object (&catch->val);
4214 for (handler = handlerlist; handler; handler = handler->next)
4216 mark_object (&handler->handler);
4217 mark_object (&handler->var);
4219 for (backlist = backtrace_list; backlist; backlist = backlist->next)
4221 if (!XMARKBIT (*backlist->function))
4223 mark_object (backlist->function);
4224 XMARK (*backlist->function);
4226 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
4227 i = 0;
4228 else
4229 i = backlist->nargs - 1;
4230 for (; i >= 0; i--)
4231 if (!XMARKBIT (backlist->args[i]))
4233 mark_object (&backlist->args[i]);
4234 XMARK (backlist->args[i]);
4237 mark_kboards ();
4239 /* Look thru every buffer's undo list
4240 for elements that update markers that were not marked,
4241 and delete them. */
4243 register struct buffer *nextb = all_buffers;
4245 while (nextb)
4247 /* If a buffer's undo list is Qt, that means that undo is
4248 turned off in that buffer. Calling truncate_undo_list on
4249 Qt tends to return NULL, which effectively turns undo back on.
4250 So don't call truncate_undo_list if undo_list is Qt. */
4251 if (! EQ (nextb->undo_list, Qt))
4253 Lisp_Object tail, prev;
4254 tail = nextb->undo_list;
4255 prev = Qnil;
4256 while (CONSP (tail))
4258 if (GC_CONSP (XCAR (tail))
4259 && GC_MARKERP (XCAR (XCAR (tail)))
4260 && ! XMARKBIT (XMARKER (XCAR (XCAR (tail)))->chain))
4262 if (NILP (prev))
4263 nextb->undo_list = tail = XCDR (tail);
4264 else
4266 tail = XCDR (tail);
4267 XSETCDR (prev, tail);
4270 else
4272 prev = tail;
4273 tail = XCDR (tail);
4278 nextb = nextb->next;
4282 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4283 mark_stack ();
4284 #endif
4286 #ifdef USE_GTK
4288 extern void xg_mark_data ();
4289 xg_mark_data ();
4291 #endif
4293 gc_sweep ();
4295 /* Clear the mark bits that we set in certain root slots. */
4297 #if (GC_MARK_STACK == GC_USE_GCPROS_AS_BEFORE \
4298 || GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES)
4299 for (tail = gcprolist; tail; tail = tail->next)
4300 for (i = 0; i < tail->nvars; i++)
4301 XUNMARK (tail->var[i]);
4302 #endif
4304 unmark_byte_stack ();
4305 for (backlist = backtrace_list; backlist; backlist = backlist->next)
4307 XUNMARK (*backlist->function);
4308 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
4309 i = 0;
4310 else
4311 i = backlist->nargs - 1;
4312 for (; i >= 0; i--)
4313 XUNMARK (backlist->args[i]);
4315 XUNMARK (buffer_defaults.name);
4316 XUNMARK (buffer_local_symbols.name);
4318 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES && 0
4319 dump_zombies ();
4320 #endif
4322 UNBLOCK_INPUT;
4324 /* clear_marks (); */
4325 gc_in_progress = 0;
4327 consing_since_gc = 0;
4328 if (gc_cons_threshold < 10000)
4329 gc_cons_threshold = 10000;
4331 if (garbage_collection_messages)
4333 if (message_p || minibuf_level > 0)
4334 restore_message ();
4335 else
4336 message1_nolog ("Garbage collecting...done");
4339 unbind_to (count, Qnil);
4341 total[0] = Fcons (make_number (total_conses),
4342 make_number (total_free_conses));
4343 total[1] = Fcons (make_number (total_symbols),
4344 make_number (total_free_symbols));
4345 total[2] = Fcons (make_number (total_markers),
4346 make_number (total_free_markers));
4347 total[3] = make_number (total_string_size);
4348 total[4] = make_number (total_vector_size);
4349 total[5] = Fcons (make_number (total_floats),
4350 make_number (total_free_floats));
4351 total[6] = Fcons (make_number (total_intervals),
4352 make_number (total_free_intervals));
4353 total[7] = Fcons (make_number (total_strings),
4354 make_number (total_free_strings));
4356 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4358 /* Compute average percentage of zombies. */
4359 double nlive = 0;
4361 for (i = 0; i < 7; ++i)
4362 if (CONSP (total[i]))
4363 nlive += XFASTINT (XCAR (total[i]));
4365 avg_live = (avg_live * ngcs + nlive) / (ngcs + 1);
4366 max_live = max (nlive, max_live);
4367 avg_zombies = (avg_zombies * ngcs + nzombies) / (ngcs + 1);
4368 max_zombies = max (nzombies, max_zombies);
4369 ++ngcs;
4371 #endif
4373 if (!NILP (Vpost_gc_hook))
4375 int count = inhibit_garbage_collection ();
4376 safe_run_hooks (Qpost_gc_hook);
4377 unbind_to (count, Qnil);
4380 /* Accumulate statistics. */
4381 EMACS_GET_TIME (t2);
4382 EMACS_SUB_TIME (t3, t2, t1);
4383 if (FLOATP (Vgc_elapsed))
4384 Vgc_elapsed = make_float (XFLOAT_DATA (Vgc_elapsed) +
4385 EMACS_SECS (t3) +
4386 EMACS_USECS (t3) * 1.0e-6);
4387 gcs_done++;
4389 return Flist (sizeof total / sizeof *total, total);
4393 /* Mark Lisp objects in glyph matrix MATRIX. Currently the
4394 only interesting objects referenced from glyphs are strings. */
4396 static void
4397 mark_glyph_matrix (matrix)
4398 struct glyph_matrix *matrix;
4400 struct glyph_row *row = matrix->rows;
4401 struct glyph_row *end = row + matrix->nrows;
4403 for (; row < end; ++row)
4404 if (row->enabled_p)
4406 int area;
4407 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
4409 struct glyph *glyph = row->glyphs[area];
4410 struct glyph *end_glyph = glyph + row->used[area];
4412 for (; glyph < end_glyph; ++glyph)
4413 if (GC_STRINGP (glyph->object)
4414 && !STRING_MARKED_P (XSTRING (glyph->object)))
4415 mark_object (&glyph->object);
4421 /* Mark Lisp faces in the face cache C. */
4423 static void
4424 mark_face_cache (c)
4425 struct face_cache *c;
4427 if (c)
4429 int i, j;
4430 for (i = 0; i < c->used; ++i)
4432 struct face *face = FACE_FROM_ID (c->f, i);
4434 if (face)
4436 for (j = 0; j < LFACE_VECTOR_SIZE; ++j)
4437 mark_object (&face->lface[j]);
4444 #ifdef HAVE_WINDOW_SYSTEM
4446 /* Mark Lisp objects in image IMG. */
4448 static void
4449 mark_image (img)
4450 struct image *img;
4452 mark_object (&img->spec);
4454 if (!NILP (img->data.lisp_val))
4455 mark_object (&img->data.lisp_val);
4459 /* Mark Lisp objects in image cache of frame F. It's done this way so
4460 that we don't have to include xterm.h here. */
4462 static void
4463 mark_image_cache (f)
4464 struct frame *f;
4466 forall_images_in_image_cache (f, mark_image);
4469 #endif /* HAVE_X_WINDOWS */
4473 /* Mark reference to a Lisp_Object.
4474 If the object referred to has not been seen yet, recursively mark
4475 all the references contained in it. */
4477 #define LAST_MARKED_SIZE 500
4478 Lisp_Object *last_marked[LAST_MARKED_SIZE];
4479 int last_marked_index;
4481 /* For debugging--call abort when we cdr down this many
4482 links of a list, in mark_object. In debugging,
4483 the call to abort will hit a breakpoint.
4484 Normally this is zero and the check never goes off. */
4485 int mark_object_loop_halt;
4487 void
4488 mark_object (argptr)
4489 Lisp_Object *argptr;
4491 Lisp_Object *objptr = argptr;
4492 register Lisp_Object obj;
4493 #ifdef GC_CHECK_MARKED_OBJECTS
4494 void *po;
4495 struct mem_node *m;
4496 #endif
4497 int cdr_count = 0;
4499 loop:
4500 obj = *objptr;
4501 loop2:
4502 XUNMARK (obj);
4504 if (PURE_POINTER_P (XPNTR (obj)))
4505 return;
4507 last_marked[last_marked_index++] = objptr;
4508 if (last_marked_index == LAST_MARKED_SIZE)
4509 last_marked_index = 0;
4511 /* Perform some sanity checks on the objects marked here. Abort if
4512 we encounter an object we know is bogus. This increases GC time
4513 by ~80%, and requires compilation with GC_MARK_STACK != 0. */
4514 #ifdef GC_CHECK_MARKED_OBJECTS
4516 po = (void *) XPNTR (obj);
4518 /* Check that the object pointed to by PO is known to be a Lisp
4519 structure allocated from the heap. */
4520 #define CHECK_ALLOCATED() \
4521 do { \
4522 m = mem_find (po); \
4523 if (m == MEM_NIL) \
4524 abort (); \
4525 } while (0)
4527 /* Check that the object pointed to by PO is live, using predicate
4528 function LIVEP. */
4529 #define CHECK_LIVE(LIVEP) \
4530 do { \
4531 if (!LIVEP (m, po)) \
4532 abort (); \
4533 } while (0)
4535 /* Check both of the above conditions. */
4536 #define CHECK_ALLOCATED_AND_LIVE(LIVEP) \
4537 do { \
4538 CHECK_ALLOCATED (); \
4539 CHECK_LIVE (LIVEP); \
4540 } while (0) \
4542 #else /* not GC_CHECK_MARKED_OBJECTS */
4544 #define CHECK_ALLOCATED() (void) 0
4545 #define CHECK_LIVE(LIVEP) (void) 0
4546 #define CHECK_ALLOCATED_AND_LIVE(LIVEP) (void) 0
4548 #endif /* not GC_CHECK_MARKED_OBJECTS */
4550 switch (SWITCH_ENUM_CAST (XGCTYPE (obj)))
4552 case Lisp_String:
4554 register struct Lisp_String *ptr = XSTRING (obj);
4555 CHECK_ALLOCATED_AND_LIVE (live_string_p);
4556 MARK_INTERVAL_TREE (ptr->intervals);
4557 MARK_STRING (ptr);
4558 #ifdef GC_CHECK_STRING_BYTES
4559 /* Check that the string size recorded in the string is the
4560 same as the one recorded in the sdata structure. */
4561 CHECK_STRING_BYTES (ptr);
4562 #endif /* GC_CHECK_STRING_BYTES */
4564 break;
4566 case Lisp_Vectorlike:
4567 #ifdef GC_CHECK_MARKED_OBJECTS
4568 m = mem_find (po);
4569 if (m == MEM_NIL && !GC_SUBRP (obj)
4570 && po != &buffer_defaults
4571 && po != &buffer_local_symbols)
4572 abort ();
4573 #endif /* GC_CHECK_MARKED_OBJECTS */
4575 if (GC_BUFFERP (obj))
4577 if (!XMARKBIT (XBUFFER (obj)->name))
4579 #ifdef GC_CHECK_MARKED_OBJECTS
4580 if (po != &buffer_defaults && po != &buffer_local_symbols)
4582 struct buffer *b;
4583 for (b = all_buffers; b && b != po; b = b->next)
4585 if (b == NULL)
4586 abort ();
4588 #endif /* GC_CHECK_MARKED_OBJECTS */
4589 mark_buffer (obj);
4592 else if (GC_SUBRP (obj))
4593 break;
4594 else if (GC_COMPILEDP (obj))
4595 /* We could treat this just like a vector, but it is better to
4596 save the COMPILED_CONSTANTS element for last and avoid
4597 recursion there. */
4599 register struct Lisp_Vector *ptr = XVECTOR (obj);
4600 register EMACS_INT size = ptr->size;
4601 register int i;
4603 if (size & ARRAY_MARK_FLAG)
4604 break; /* Already marked */
4606 CHECK_LIVE (live_vector_p);
4607 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
4608 size &= PSEUDOVECTOR_SIZE_MASK;
4609 for (i = 0; i < size; i++) /* and then mark its elements */
4611 if (i != COMPILED_CONSTANTS)
4612 mark_object (&ptr->contents[i]);
4614 /* This cast should be unnecessary, but some Mips compiler complains
4615 (MIPS-ABI + SysVR4, DC/OSx, etc). */
4616 objptr = (Lisp_Object *) &ptr->contents[COMPILED_CONSTANTS];
4617 goto loop;
4619 else if (GC_FRAMEP (obj))
4621 register struct frame *ptr = XFRAME (obj);
4622 register EMACS_INT size = ptr->size;
4624 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
4625 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
4627 CHECK_LIVE (live_vector_p);
4628 mark_object (&ptr->name);
4629 mark_object (&ptr->icon_name);
4630 mark_object (&ptr->title);
4631 mark_object (&ptr->focus_frame);
4632 mark_object (&ptr->selected_window);
4633 mark_object (&ptr->minibuffer_window);
4634 mark_object (&ptr->param_alist);
4635 mark_object (&ptr->scroll_bars);
4636 mark_object (&ptr->condemned_scroll_bars);
4637 mark_object (&ptr->menu_bar_items);
4638 mark_object (&ptr->face_alist);
4639 mark_object (&ptr->menu_bar_vector);
4640 mark_object (&ptr->buffer_predicate);
4641 mark_object (&ptr->buffer_list);
4642 mark_object (&ptr->menu_bar_window);
4643 mark_object (&ptr->tool_bar_window);
4644 mark_face_cache (ptr->face_cache);
4645 #ifdef HAVE_WINDOW_SYSTEM
4646 mark_image_cache (ptr);
4647 mark_object (&ptr->tool_bar_items);
4648 mark_object (&ptr->desired_tool_bar_string);
4649 mark_object (&ptr->current_tool_bar_string);
4650 #endif /* HAVE_WINDOW_SYSTEM */
4652 else if (GC_BOOL_VECTOR_P (obj))
4654 register struct Lisp_Vector *ptr = XVECTOR (obj);
4656 if (ptr->size & ARRAY_MARK_FLAG)
4657 break; /* Already marked */
4658 CHECK_LIVE (live_vector_p);
4659 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
4661 else if (GC_WINDOWP (obj))
4663 register struct Lisp_Vector *ptr = XVECTOR (obj);
4664 struct window *w = XWINDOW (obj);
4665 register EMACS_INT size = ptr->size;
4666 register int i;
4668 /* Stop if already marked. */
4669 if (size & ARRAY_MARK_FLAG)
4670 break;
4672 /* Mark it. */
4673 CHECK_LIVE (live_vector_p);
4674 ptr->size |= ARRAY_MARK_FLAG;
4676 /* There is no Lisp data above The member CURRENT_MATRIX in
4677 struct WINDOW. Stop marking when that slot is reached. */
4678 for (i = 0;
4679 (char *) &ptr->contents[i] < (char *) &w->current_matrix;
4680 i++)
4681 mark_object (&ptr->contents[i]);
4683 /* Mark glyphs for leaf windows. Marking window matrices is
4684 sufficient because frame matrices use the same glyph
4685 memory. */
4686 if (NILP (w->hchild)
4687 && NILP (w->vchild)
4688 && w->current_matrix)
4690 mark_glyph_matrix (w->current_matrix);
4691 mark_glyph_matrix (w->desired_matrix);
4694 else if (GC_HASH_TABLE_P (obj))
4696 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
4697 EMACS_INT size = h->size;
4699 /* Stop if already marked. */
4700 if (size & ARRAY_MARK_FLAG)
4701 break;
4703 /* Mark it. */
4704 CHECK_LIVE (live_vector_p);
4705 h->size |= ARRAY_MARK_FLAG;
4707 /* Mark contents. */
4708 /* Do not mark next_free or next_weak.
4709 Being in the next_weak chain
4710 should not keep the hash table alive.
4711 No need to mark `count' since it is an integer. */
4712 mark_object (&h->test);
4713 mark_object (&h->weak);
4714 mark_object (&h->rehash_size);
4715 mark_object (&h->rehash_threshold);
4716 mark_object (&h->hash);
4717 mark_object (&h->next);
4718 mark_object (&h->index);
4719 mark_object (&h->user_hash_function);
4720 mark_object (&h->user_cmp_function);
4722 /* If hash table is not weak, mark all keys and values.
4723 For weak tables, mark only the vector. */
4724 if (GC_NILP (h->weak))
4725 mark_object (&h->key_and_value);
4726 else
4727 XVECTOR (h->key_and_value)->size |= ARRAY_MARK_FLAG;
4730 else
4732 register struct Lisp_Vector *ptr = XVECTOR (obj);
4733 register EMACS_INT size = ptr->size;
4734 register int i;
4736 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
4737 CHECK_LIVE (live_vector_p);
4738 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
4739 if (size & PSEUDOVECTOR_FLAG)
4740 size &= PSEUDOVECTOR_SIZE_MASK;
4742 for (i = 0; i < size; i++) /* and then mark its elements */
4743 mark_object (&ptr->contents[i]);
4745 break;
4747 case Lisp_Symbol:
4749 register struct Lisp_Symbol *ptr = XSYMBOL (obj);
4750 struct Lisp_Symbol *ptrx;
4752 if (XMARKBIT (ptr->plist)) break;
4753 CHECK_ALLOCATED_AND_LIVE (live_symbol_p);
4754 XMARK (ptr->plist);
4755 mark_object ((Lisp_Object *) &ptr->value);
4756 mark_object (&ptr->function);
4757 mark_object (&ptr->plist);
4759 if (!PURE_POINTER_P (XSTRING (ptr->xname)))
4760 MARK_STRING (XSTRING (ptr->xname));
4761 MARK_INTERVAL_TREE (STRING_INTERVALS (ptr->xname));
4763 /* Note that we do not mark the obarray of the symbol.
4764 It is safe not to do so because nothing accesses that
4765 slot except to check whether it is nil. */
4766 ptr = ptr->next;
4767 if (ptr)
4769 /* For the benefit of the last_marked log. */
4770 objptr = (Lisp_Object *)&XSYMBOL (obj)->next;
4771 ptrx = ptr; /* Use of ptrx avoids compiler bug on Sun */
4772 XSETSYMBOL (obj, ptrx);
4773 /* We can't goto loop here because *objptr doesn't contain an
4774 actual Lisp_Object with valid datatype field. */
4775 goto loop2;
4778 break;
4780 case Lisp_Misc:
4781 CHECK_ALLOCATED_AND_LIVE (live_misc_p);
4782 switch (XMISCTYPE (obj))
4784 case Lisp_Misc_Marker:
4785 XMARK (XMARKER (obj)->chain);
4786 /* DO NOT mark thru the marker's chain.
4787 The buffer's markers chain does not preserve markers from gc;
4788 instead, markers are removed from the chain when freed by gc. */
4789 break;
4791 case Lisp_Misc_Buffer_Local_Value:
4792 case Lisp_Misc_Some_Buffer_Local_Value:
4794 register struct Lisp_Buffer_Local_Value *ptr
4795 = XBUFFER_LOCAL_VALUE (obj);
4796 if (XMARKBIT (ptr->realvalue)) break;
4797 XMARK (ptr->realvalue);
4798 /* If the cdr is nil, avoid recursion for the car. */
4799 if (EQ (ptr->cdr, Qnil))
4801 objptr = &ptr->realvalue;
4802 goto loop;
4804 mark_object (&ptr->realvalue);
4805 mark_object (&ptr->buffer);
4806 mark_object (&ptr->frame);
4807 objptr = &ptr->cdr;
4808 goto loop;
4811 case Lisp_Misc_Intfwd:
4812 case Lisp_Misc_Boolfwd:
4813 case Lisp_Misc_Objfwd:
4814 case Lisp_Misc_Buffer_Objfwd:
4815 case Lisp_Misc_Kboard_Objfwd:
4816 /* Don't bother with Lisp_Buffer_Objfwd,
4817 since all markable slots in current buffer marked anyway. */
4818 /* Don't need to do Lisp_Objfwd, since the places they point
4819 are protected with staticpro. */
4820 break;
4822 case Lisp_Misc_Overlay:
4824 struct Lisp_Overlay *ptr = XOVERLAY (obj);
4825 if (!XMARKBIT (ptr->plist))
4827 XMARK (ptr->plist);
4828 mark_object (&ptr->start);
4829 mark_object (&ptr->end);
4830 objptr = &ptr->plist;
4831 goto loop;
4834 break;
4836 default:
4837 abort ();
4839 break;
4841 case Lisp_Cons:
4843 register struct Lisp_Cons *ptr = XCONS (obj);
4844 if (XMARKBIT (ptr->car)) break;
4845 CHECK_ALLOCATED_AND_LIVE (live_cons_p);
4846 XMARK (ptr->car);
4847 /* If the cdr is nil, avoid recursion for the car. */
4848 if (EQ (ptr->cdr, Qnil))
4850 objptr = &ptr->car;
4851 cdr_count = 0;
4852 goto loop;
4854 mark_object (&ptr->car);
4855 objptr = &ptr->cdr;
4856 cdr_count++;
4857 if (cdr_count == mark_object_loop_halt)
4858 abort ();
4859 goto loop;
4862 case Lisp_Float:
4863 CHECK_ALLOCATED_AND_LIVE (live_float_p);
4864 XMARK (XFLOAT (obj)->type);
4865 break;
4867 case Lisp_Int:
4868 break;
4870 default:
4871 abort ();
4874 #undef CHECK_LIVE
4875 #undef CHECK_ALLOCATED
4876 #undef CHECK_ALLOCATED_AND_LIVE
4879 /* Mark the pointers in a buffer structure. */
4881 static void
4882 mark_buffer (buf)
4883 Lisp_Object buf;
4885 register struct buffer *buffer = XBUFFER (buf);
4886 register Lisp_Object *ptr;
4887 Lisp_Object base_buffer;
4889 /* This is the buffer's markbit */
4890 mark_object (&buffer->name);
4891 XMARK (buffer->name);
4893 MARK_INTERVAL_TREE (BUF_INTERVALS (buffer));
4895 if (CONSP (buffer->undo_list))
4897 Lisp_Object tail;
4898 tail = buffer->undo_list;
4900 while (CONSP (tail))
4902 register struct Lisp_Cons *ptr = XCONS (tail);
4904 if (XMARKBIT (ptr->car))
4905 break;
4906 XMARK (ptr->car);
4907 if (GC_CONSP (ptr->car)
4908 && ! XMARKBIT (XCAR (ptr->car))
4909 && GC_MARKERP (XCAR (ptr->car)))
4911 XMARK (XCAR_AS_LVALUE (ptr->car));
4912 mark_object (&XCDR_AS_LVALUE (ptr->car));
4914 else
4915 mark_object (&ptr->car);
4917 if (CONSP (ptr->cdr))
4918 tail = ptr->cdr;
4919 else
4920 break;
4923 mark_object (&XCDR_AS_LVALUE (tail));
4925 else
4926 mark_object (&buffer->undo_list);
4928 for (ptr = &buffer->name + 1;
4929 (char *)ptr < (char *)buffer + sizeof (struct buffer);
4930 ptr++)
4931 mark_object (ptr);
4933 /* If this is an indirect buffer, mark its base buffer. */
4934 if (buffer->base_buffer && !XMARKBIT (buffer->base_buffer->name))
4936 XSETBUFFER (base_buffer, buffer->base_buffer);
4937 mark_buffer (base_buffer);
4942 /* Mark the pointers in the kboard objects. */
4944 static void
4945 mark_kboards ()
4947 KBOARD *kb;
4948 Lisp_Object *p;
4949 for (kb = all_kboards; kb; kb = kb->next_kboard)
4951 if (kb->kbd_macro_buffer)
4952 for (p = kb->kbd_macro_buffer; p < kb->kbd_macro_ptr; p++)
4953 mark_object (p);
4954 mark_object (&kb->Voverriding_terminal_local_map);
4955 mark_object (&kb->Vlast_command);
4956 mark_object (&kb->Vreal_last_command);
4957 mark_object (&kb->Vprefix_arg);
4958 mark_object (&kb->Vlast_prefix_arg);
4959 mark_object (&kb->kbd_queue);
4960 mark_object (&kb->defining_kbd_macro);
4961 mark_object (&kb->Vlast_kbd_macro);
4962 mark_object (&kb->Vsystem_key_alist);
4963 mark_object (&kb->system_key_syms);
4964 mark_object (&kb->Vdefault_minibuffer_frame);
4965 mark_object (&kb->echo_string);
4970 /* Value is non-zero if OBJ will survive the current GC because it's
4971 either marked or does not need to be marked to survive. */
4974 survives_gc_p (obj)
4975 Lisp_Object obj;
4977 int survives_p;
4979 switch (XGCTYPE (obj))
4981 case Lisp_Int:
4982 survives_p = 1;
4983 break;
4985 case Lisp_Symbol:
4986 survives_p = XMARKBIT (XSYMBOL (obj)->plist);
4987 break;
4989 case Lisp_Misc:
4990 switch (XMISCTYPE (obj))
4992 case Lisp_Misc_Marker:
4993 survives_p = XMARKBIT (obj);
4994 break;
4996 case Lisp_Misc_Buffer_Local_Value:
4997 case Lisp_Misc_Some_Buffer_Local_Value:
4998 survives_p = XMARKBIT (XBUFFER_LOCAL_VALUE (obj)->realvalue);
4999 break;
5001 case Lisp_Misc_Intfwd:
5002 case Lisp_Misc_Boolfwd:
5003 case Lisp_Misc_Objfwd:
5004 case Lisp_Misc_Buffer_Objfwd:
5005 case Lisp_Misc_Kboard_Objfwd:
5006 survives_p = 1;
5007 break;
5009 case Lisp_Misc_Overlay:
5010 survives_p = XMARKBIT (XOVERLAY (obj)->plist);
5011 break;
5013 default:
5014 abort ();
5016 break;
5018 case Lisp_String:
5020 struct Lisp_String *s = XSTRING (obj);
5021 survives_p = STRING_MARKED_P (s);
5023 break;
5025 case Lisp_Vectorlike:
5026 if (GC_BUFFERP (obj))
5027 survives_p = XMARKBIT (XBUFFER (obj)->name);
5028 else if (GC_SUBRP (obj))
5029 survives_p = 1;
5030 else
5031 survives_p = XVECTOR (obj)->size & ARRAY_MARK_FLAG;
5032 break;
5034 case Lisp_Cons:
5035 survives_p = XMARKBIT (XCAR (obj));
5036 break;
5038 case Lisp_Float:
5039 survives_p = XMARKBIT (XFLOAT (obj)->type);
5040 break;
5042 default:
5043 abort ();
5046 return survives_p || PURE_POINTER_P ((void *) XPNTR (obj));
5051 /* Sweep: find all structures not marked, and free them. */
5053 static void
5054 gc_sweep ()
5056 /* Remove or mark entries in weak hash tables.
5057 This must be done before any object is unmarked. */
5058 sweep_weak_hash_tables ();
5060 sweep_strings ();
5061 #ifdef GC_CHECK_STRING_BYTES
5062 if (!noninteractive)
5063 check_string_bytes (1);
5064 #endif
5066 /* Put all unmarked conses on free list */
5068 register struct cons_block *cblk;
5069 struct cons_block **cprev = &cons_block;
5070 register int lim = cons_block_index;
5071 register int num_free = 0, num_used = 0;
5073 cons_free_list = 0;
5075 for (cblk = cons_block; cblk; cblk = *cprev)
5077 register int i;
5078 int this_free = 0;
5079 for (i = 0; i < lim; i++)
5080 if (!XMARKBIT (cblk->conses[i].car))
5082 this_free++;
5083 *(struct Lisp_Cons **)&cblk->conses[i].cdr = cons_free_list;
5084 cons_free_list = &cblk->conses[i];
5085 #if GC_MARK_STACK
5086 cons_free_list->car = Vdead;
5087 #endif
5089 else
5091 num_used++;
5092 XUNMARK (cblk->conses[i].car);
5094 lim = CONS_BLOCK_SIZE;
5095 /* If this block contains only free conses and we have already
5096 seen more than two blocks worth of free conses then deallocate
5097 this block. */
5098 if (this_free == CONS_BLOCK_SIZE && num_free > CONS_BLOCK_SIZE)
5100 *cprev = cblk->next;
5101 /* Unhook from the free list. */
5102 cons_free_list = *(struct Lisp_Cons **) &cblk->conses[0].cdr;
5103 lisp_free (cblk);
5104 n_cons_blocks--;
5106 else
5108 num_free += this_free;
5109 cprev = &cblk->next;
5112 total_conses = num_used;
5113 total_free_conses = num_free;
5116 /* Put all unmarked floats on free list */
5118 register struct float_block *fblk;
5119 struct float_block **fprev = &float_block;
5120 register int lim = float_block_index;
5121 register int num_free = 0, num_used = 0;
5123 float_free_list = 0;
5125 for (fblk = float_block; fblk; fblk = *fprev)
5127 register int i;
5128 int this_free = 0;
5129 for (i = 0; i < lim; i++)
5130 if (!XMARKBIT (fblk->floats[i].type))
5132 this_free++;
5133 *(struct Lisp_Float **)&fblk->floats[i].data = float_free_list;
5134 float_free_list = &fblk->floats[i];
5135 #if GC_MARK_STACK
5136 float_free_list->type = Vdead;
5137 #endif
5139 else
5141 num_used++;
5142 XUNMARK (fblk->floats[i].type);
5144 lim = FLOAT_BLOCK_SIZE;
5145 /* If this block contains only free floats and we have already
5146 seen more than two blocks worth of free floats then deallocate
5147 this block. */
5148 if (this_free == FLOAT_BLOCK_SIZE && num_free > FLOAT_BLOCK_SIZE)
5150 *fprev = fblk->next;
5151 /* Unhook from the free list. */
5152 float_free_list = *(struct Lisp_Float **) &fblk->floats[0].data;
5153 lisp_free (fblk);
5154 n_float_blocks--;
5156 else
5158 num_free += this_free;
5159 fprev = &fblk->next;
5162 total_floats = num_used;
5163 total_free_floats = num_free;
5166 /* Put all unmarked intervals on free list */
5168 register struct interval_block *iblk;
5169 struct interval_block **iprev = &interval_block;
5170 register int lim = interval_block_index;
5171 register int num_free = 0, num_used = 0;
5173 interval_free_list = 0;
5175 for (iblk = interval_block; iblk; iblk = *iprev)
5177 register int i;
5178 int this_free = 0;
5180 for (i = 0; i < lim; i++)
5182 if (! XMARKBIT (iblk->intervals[i].plist))
5184 SET_INTERVAL_PARENT (&iblk->intervals[i], interval_free_list);
5185 interval_free_list = &iblk->intervals[i];
5186 this_free++;
5188 else
5190 num_used++;
5191 XUNMARK (iblk->intervals[i].plist);
5194 lim = INTERVAL_BLOCK_SIZE;
5195 /* If this block contains only free intervals and we have already
5196 seen more than two blocks worth of free intervals then
5197 deallocate this block. */
5198 if (this_free == INTERVAL_BLOCK_SIZE && num_free > INTERVAL_BLOCK_SIZE)
5200 *iprev = iblk->next;
5201 /* Unhook from the free list. */
5202 interval_free_list = INTERVAL_PARENT (&iblk->intervals[0]);
5203 lisp_free (iblk);
5204 n_interval_blocks--;
5206 else
5208 num_free += this_free;
5209 iprev = &iblk->next;
5212 total_intervals = num_used;
5213 total_free_intervals = num_free;
5216 /* Put all unmarked symbols on free list */
5218 register struct symbol_block *sblk;
5219 struct symbol_block **sprev = &symbol_block;
5220 register int lim = symbol_block_index;
5221 register int num_free = 0, num_used = 0;
5223 symbol_free_list = NULL;
5225 for (sblk = symbol_block; sblk; sblk = *sprev)
5227 int this_free = 0;
5228 struct Lisp_Symbol *sym = sblk->symbols;
5229 struct Lisp_Symbol *end = sym + lim;
5231 for (; sym < end; ++sym)
5233 /* Check if the symbol was created during loadup. In such a case
5234 it might be pointed to by pure bytecode which we don't trace,
5235 so we conservatively assume that it is live. */
5236 int pure_p = PURE_POINTER_P (XSTRING (sym->xname));
5238 if (!XMARKBIT (sym->plist) && !pure_p)
5240 *(struct Lisp_Symbol **) &sym->value = symbol_free_list;
5241 symbol_free_list = sym;
5242 #if GC_MARK_STACK
5243 symbol_free_list->function = Vdead;
5244 #endif
5245 ++this_free;
5247 else
5249 ++num_used;
5250 if (!pure_p)
5251 UNMARK_STRING (XSTRING (sym->xname));
5252 XUNMARK (sym->plist);
5256 lim = SYMBOL_BLOCK_SIZE;
5257 /* If this block contains only free symbols and we have already
5258 seen more than two blocks worth of free symbols then deallocate
5259 this block. */
5260 if (this_free == SYMBOL_BLOCK_SIZE && num_free > SYMBOL_BLOCK_SIZE)
5262 *sprev = sblk->next;
5263 /* Unhook from the free list. */
5264 symbol_free_list = *(struct Lisp_Symbol **)&sblk->symbols[0].value;
5265 lisp_free (sblk);
5266 n_symbol_blocks--;
5268 else
5270 num_free += this_free;
5271 sprev = &sblk->next;
5274 total_symbols = num_used;
5275 total_free_symbols = num_free;
5278 /* Put all unmarked misc's on free list.
5279 For a marker, first unchain it from the buffer it points into. */
5281 register struct marker_block *mblk;
5282 struct marker_block **mprev = &marker_block;
5283 register int lim = marker_block_index;
5284 register int num_free = 0, num_used = 0;
5286 marker_free_list = 0;
5288 for (mblk = marker_block; mblk; mblk = *mprev)
5290 register int i;
5291 int this_free = 0;
5292 EMACS_INT already_free = -1;
5294 for (i = 0; i < lim; i++)
5296 Lisp_Object *markword;
5297 switch (mblk->markers[i].u_marker.type)
5299 case Lisp_Misc_Marker:
5300 markword = &mblk->markers[i].u_marker.chain;
5301 break;
5302 case Lisp_Misc_Buffer_Local_Value:
5303 case Lisp_Misc_Some_Buffer_Local_Value:
5304 markword = &mblk->markers[i].u_buffer_local_value.realvalue;
5305 break;
5306 case Lisp_Misc_Overlay:
5307 markword = &mblk->markers[i].u_overlay.plist;
5308 break;
5309 case Lisp_Misc_Free:
5310 /* If the object was already free, keep it
5311 on the free list. */
5312 markword = (Lisp_Object *) &already_free;
5313 break;
5314 default:
5315 markword = 0;
5316 break;
5318 if (markword && !XMARKBIT (*markword))
5320 Lisp_Object tem;
5321 if (mblk->markers[i].u_marker.type == Lisp_Misc_Marker)
5323 /* tem1 avoids Sun compiler bug */
5324 struct Lisp_Marker *tem1 = &mblk->markers[i].u_marker;
5325 XSETMARKER (tem, tem1);
5326 unchain_marker (tem);
5328 /* Set the type of the freed object to Lisp_Misc_Free.
5329 We could leave the type alone, since nobody checks it,
5330 but this might catch bugs faster. */
5331 mblk->markers[i].u_marker.type = Lisp_Misc_Free;
5332 mblk->markers[i].u_free.chain = marker_free_list;
5333 marker_free_list = &mblk->markers[i];
5334 this_free++;
5336 else
5338 num_used++;
5339 if (markword)
5340 XUNMARK (*markword);
5343 lim = MARKER_BLOCK_SIZE;
5344 /* If this block contains only free markers and we have already
5345 seen more than two blocks worth of free markers then deallocate
5346 this block. */
5347 if (this_free == MARKER_BLOCK_SIZE && num_free > MARKER_BLOCK_SIZE)
5349 *mprev = mblk->next;
5350 /* Unhook from the free list. */
5351 marker_free_list = mblk->markers[0].u_free.chain;
5352 lisp_free (mblk);
5353 n_marker_blocks--;
5355 else
5357 num_free += this_free;
5358 mprev = &mblk->next;
5362 total_markers = num_used;
5363 total_free_markers = num_free;
5366 /* Free all unmarked buffers */
5368 register struct buffer *buffer = all_buffers, *prev = 0, *next;
5370 while (buffer)
5371 if (!XMARKBIT (buffer->name))
5373 if (prev)
5374 prev->next = buffer->next;
5375 else
5376 all_buffers = buffer->next;
5377 next = buffer->next;
5378 lisp_free (buffer);
5379 buffer = next;
5381 else
5383 XUNMARK (buffer->name);
5384 UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer));
5385 prev = buffer, buffer = buffer->next;
5389 /* Free all unmarked vectors */
5391 register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next;
5392 total_vector_size = 0;
5394 while (vector)
5395 if (!(vector->size & ARRAY_MARK_FLAG))
5397 if (prev)
5398 prev->next = vector->next;
5399 else
5400 all_vectors = vector->next;
5401 next = vector->next;
5402 lisp_free (vector);
5403 n_vectors--;
5404 vector = next;
5407 else
5409 vector->size &= ~ARRAY_MARK_FLAG;
5410 if (vector->size & PSEUDOVECTOR_FLAG)
5411 total_vector_size += (PSEUDOVECTOR_SIZE_MASK & vector->size);
5412 else
5413 total_vector_size += vector->size;
5414 prev = vector, vector = vector->next;
5418 #ifdef GC_CHECK_STRING_BYTES
5419 if (!noninteractive)
5420 check_string_bytes (1);
5421 #endif
5427 /* Debugging aids. */
5429 DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0,
5430 doc: /* Return the address of the last byte Emacs has allocated, divided by 1024.
5431 This may be helpful in debugging Emacs's memory usage.
5432 We divide the value by 1024 to make sure it fits in a Lisp integer. */)
5435 Lisp_Object end;
5437 XSETINT (end, (EMACS_INT) sbrk (0) / 1024);
5439 return end;
5442 DEFUN ("memory-use-counts", Fmemory_use_counts, Smemory_use_counts, 0, 0, 0,
5443 doc: /* Return a list of counters that measure how much consing there has been.
5444 Each of these counters increments for a certain kind of object.
5445 The counters wrap around from the largest positive integer to zero.
5446 Garbage collection does not decrease them.
5447 The elements of the value are as follows:
5448 (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS STRINGS)
5449 All are in units of 1 = one object consed
5450 except for VECTOR-CELLS and STRING-CHARS, which count the total length of
5451 objects consed.
5452 MISCS include overlays, markers, and some internal types.
5453 Frames, windows, buffers, and subprocesses count as vectors
5454 (but the contents of a buffer's text do not count here). */)
5457 Lisp_Object consed[8];
5459 consed[0] = make_number (min (MOST_POSITIVE_FIXNUM, cons_cells_consed));
5460 consed[1] = make_number (min (MOST_POSITIVE_FIXNUM, floats_consed));
5461 consed[2] = make_number (min (MOST_POSITIVE_FIXNUM, vector_cells_consed));
5462 consed[3] = make_number (min (MOST_POSITIVE_FIXNUM, symbols_consed));
5463 consed[4] = make_number (min (MOST_POSITIVE_FIXNUM, string_chars_consed));
5464 consed[5] = make_number (min (MOST_POSITIVE_FIXNUM, misc_objects_consed));
5465 consed[6] = make_number (min (MOST_POSITIVE_FIXNUM, intervals_consed));
5466 consed[7] = make_number (min (MOST_POSITIVE_FIXNUM, strings_consed));
5468 return Flist (8, consed);
5471 int suppress_checking;
5472 void
5473 die (msg, file, line)
5474 const char *msg;
5475 const char *file;
5476 int line;
5478 fprintf (stderr, "\r\nEmacs fatal error: %s:%d: %s\r\n",
5479 file, line, msg);
5480 abort ();
5483 /* Initialization */
5485 void
5486 init_alloc_once ()
5488 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
5489 purebeg = PUREBEG;
5490 pure_size = PURESIZE;
5491 pure_bytes_used = 0;
5492 pure_bytes_used_before_overflow = 0;
5494 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
5495 mem_init ();
5496 Vdead = make_pure_string ("DEAD", 4, 4, 0);
5497 #endif
5499 all_vectors = 0;
5500 ignore_warnings = 1;
5501 #ifdef DOUG_LEA_MALLOC
5502 mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */
5503 mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */
5504 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); /* max. number of mmap'ed areas */
5505 #endif
5506 init_strings ();
5507 init_cons ();
5508 init_symbol ();
5509 init_marker ();
5510 init_float ();
5511 init_intervals ();
5513 #ifdef REL_ALLOC
5514 malloc_hysteresis = 32;
5515 #else
5516 malloc_hysteresis = 0;
5517 #endif
5519 spare_memory = (char *) malloc (SPARE_MEMORY);
5521 ignore_warnings = 0;
5522 gcprolist = 0;
5523 byte_stack_list = 0;
5524 staticidx = 0;
5525 consing_since_gc = 0;
5526 gc_cons_threshold = 100000 * sizeof (Lisp_Object);
5527 #ifdef VIRT_ADDR_VARIES
5528 malloc_sbrk_unused = 1<<22; /* A large number */
5529 malloc_sbrk_used = 100000; /* as reasonable as any number */
5530 #endif /* VIRT_ADDR_VARIES */
5533 void
5534 init_alloc ()
5536 gcprolist = 0;
5537 byte_stack_list = 0;
5538 #if GC_MARK_STACK
5539 #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
5540 setjmp_tested_p = longjmps_done = 0;
5541 #endif
5542 #endif
5543 Vgc_elapsed = make_float (0.0);
5544 gcs_done = 0;
5547 void
5548 syms_of_alloc ()
5550 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold,
5551 doc: /* *Number of bytes of consing between garbage collections.
5552 Garbage collection can happen automatically once this many bytes have been
5553 allocated since the last garbage collection. All data types count.
5555 Garbage collection happens automatically only when `eval' is called.
5557 By binding this temporarily to a large number, you can effectively
5558 prevent garbage collection during a part of the program. */);
5560 DEFVAR_INT ("pure-bytes-used", &pure_bytes_used,
5561 doc: /* Number of bytes of sharable Lisp data allocated so far. */);
5563 DEFVAR_INT ("cons-cells-consed", &cons_cells_consed,
5564 doc: /* Number of cons cells that have been consed so far. */);
5566 DEFVAR_INT ("floats-consed", &floats_consed,
5567 doc: /* Number of floats that have been consed so far. */);
5569 DEFVAR_INT ("vector-cells-consed", &vector_cells_consed,
5570 doc: /* Number of vector cells that have been consed so far. */);
5572 DEFVAR_INT ("symbols-consed", &symbols_consed,
5573 doc: /* Number of symbols that have been consed so far. */);
5575 DEFVAR_INT ("string-chars-consed", &string_chars_consed,
5576 doc: /* Number of string characters that have been consed so far. */);
5578 DEFVAR_INT ("misc-objects-consed", &misc_objects_consed,
5579 doc: /* Number of miscellaneous objects that have been consed so far. */);
5581 DEFVAR_INT ("intervals-consed", &intervals_consed,
5582 doc: /* Number of intervals that have been consed so far. */);
5584 DEFVAR_INT ("strings-consed", &strings_consed,
5585 doc: /* Number of strings that have been consed so far. */);
5587 DEFVAR_LISP ("purify-flag", &Vpurify_flag,
5588 doc: /* Non-nil means loading Lisp code in order to dump an executable.
5589 This means that certain objects should be allocated in shared (pure) space. */);
5591 DEFVAR_INT ("undo-limit", &undo_limit,
5592 doc: /* Keep no more undo information once it exceeds this size.
5593 This limit is applied when garbage collection happens.
5594 The size is counted as the number of bytes occupied,
5595 which includes both saved text and other data. */);
5596 undo_limit = 20000;
5598 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit,
5599 doc: /* Don't keep more than this much size of undo information.
5600 A command which pushes past this size is itself forgotten.
5601 This limit is applied when garbage collection happens.
5602 The size is counted as the number of bytes occupied,
5603 which includes both saved text and other data. */);
5604 undo_strong_limit = 30000;
5606 DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages,
5607 doc: /* Non-nil means display messages at start and end of garbage collection. */);
5608 garbage_collection_messages = 0;
5610 DEFVAR_LISP ("post-gc-hook", &Vpost_gc_hook,
5611 doc: /* Hook run after garbage collection has finished. */);
5612 Vpost_gc_hook = Qnil;
5613 Qpost_gc_hook = intern ("post-gc-hook");
5614 staticpro (&Qpost_gc_hook);
5616 DEFVAR_LISP ("memory-signal-data", &Vmemory_signal_data,
5617 doc: /* Precomputed `signal' argument for memory-full error. */);
5618 /* We build this in advance because if we wait until we need it, we might
5619 not be able to allocate the memory to hold it. */
5620 Vmemory_signal_data
5621 = list2 (Qerror,
5622 build_string ("Memory exhausted--use M-x save-some-buffers then exit and restart Emacs"));
5624 DEFVAR_LISP ("memory-full", &Vmemory_full,
5625 doc: /* Non-nil means we are handling a memory-full error. */);
5626 Vmemory_full = Qnil;
5628 staticpro (&Qgc_cons_threshold);
5629 Qgc_cons_threshold = intern ("gc-cons-threshold");
5631 staticpro (&Qchar_table_extra_slots);
5632 Qchar_table_extra_slots = intern ("char-table-extra-slots");
5634 DEFVAR_LISP ("gc-elapsed", &Vgc_elapsed,
5635 doc: /* Accumulated time elapsed in garbage collections.
5636 The time is in seconds as a floating point value.
5637 Programs may reset this to get statistics in a specific period. */);
5638 DEFVAR_INT ("gcs-done", &gcs_done,
5639 doc: /* Accumulated number of garbage collections done.
5640 Programs may reset this to get statistics in a specific period. */);
5642 defsubr (&Scons);
5643 defsubr (&Slist);
5644 defsubr (&Svector);
5645 defsubr (&Smake_byte_code);
5646 defsubr (&Smake_list);
5647 defsubr (&Smake_vector);
5648 defsubr (&Smake_char_table);
5649 defsubr (&Smake_string);
5650 defsubr (&Smake_bool_vector);
5651 defsubr (&Smake_symbol);
5652 defsubr (&Smake_marker);
5653 defsubr (&Spurecopy);
5654 defsubr (&Sgarbage_collect);
5655 defsubr (&Smemory_limit);
5656 defsubr (&Smemory_use_counts);
5658 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
5659 defsubr (&Sgc_status);
5660 #endif