*** empty log message ***
[emacs.git] / src / alloc.c
blob480a30ace51e3a4a123f59fdb319569c22ae106b
1 /* Storage allocation and gc for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 86, 88, 93, 94, 95, 97, 98, 1999, 2000
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 /* Note that this declares bzero on OSF/1. How dumb. */
27 #include <signal.h>
29 /* This file is part of the core Lisp implementation, and thus must
30 deal with the real data structures. If the Lisp implementation is
31 replaced, this file likely will not be used. */
33 #undef HIDE_LISP_IMPLEMENTATION
34 #include "lisp.h"
35 #include "intervals.h"
36 #include "puresize.h"
37 #include "buffer.h"
38 #include "window.h"
39 #include "frame.h"
40 #include "blockinput.h"
41 #include "keyboard.h"
42 #include "charset.h"
43 #include "syssignal.h"
44 #include <setjmp.h>
46 extern char *sbrk ();
48 #ifdef DOUG_LEA_MALLOC
50 #include <malloc.h>
51 #define __malloc_size_t int
53 /* Specify maximum number of areas to mmap. It would be nice to use a
54 value that explicitly means "no limit". */
56 #define MMAP_MAX_AREAS 100000000
58 #else /* not DOUG_LEA_MALLOC */
60 /* The following come from gmalloc.c. */
62 #if defined (STDC_HEADERS)
63 #include <stddef.h>
64 #define __malloc_size_t size_t
65 #else
66 #define __malloc_size_t unsigned int
67 #endif
68 extern __malloc_size_t _bytes_used;
69 extern int __malloc_extra_blocks;
71 #endif /* not DOUG_LEA_MALLOC */
73 #define max(A,B) ((A) > (B) ? (A) : (B))
74 #define min(A,B) ((A) < (B) ? (A) : (B))
76 /* Macro to verify that storage intended for Lisp objects is not
77 out of range to fit in the space for a pointer.
78 ADDRESS is the start of the block, and SIZE
79 is the amount of space within which objects can start. */
81 #define VALIDATE_LISP_STORAGE(address, size) \
82 do \
83 { \
84 Lisp_Object val; \
85 XSETCONS (val, (char *) address + size); \
86 if ((char *) XCONS (val) != (char *) address + size) \
87 { \
88 xfree (address); \
89 memory_full (); \
90 } \
91 } while (0)
93 /* Value of _bytes_used, when spare_memory was freed. */
95 static __malloc_size_t bytes_used_when_full;
97 /* Mark, unmark, query mark bit of a Lisp string. S must be a pointer
98 to a struct Lisp_String. */
100 #define MARK_STRING(S) ((S)->size |= MARKBIT)
101 #define UNMARK_STRING(S) ((S)->size &= ~MARKBIT)
102 #define STRING_MARKED_P(S) ((S)->size & MARKBIT)
104 /* Value is the number of bytes/chars of S, a pointer to a struct
105 Lisp_String. This must be used instead of STRING_BYTES (S) or
106 S->size during GC, because S->size contains the mark bit for
107 strings. */
109 #define GC_STRING_BYTES(S) (STRING_BYTES (S) & ~MARKBIT)
110 #define GC_STRING_CHARS(S) ((S)->size & ~MARKBIT)
112 /* Number of bytes of consing done since the last gc. */
114 int consing_since_gc;
116 /* Count the amount of consing of various sorts of space. */
118 int cons_cells_consed;
119 int floats_consed;
120 int vector_cells_consed;
121 int symbols_consed;
122 int string_chars_consed;
123 int misc_objects_consed;
124 int intervals_consed;
125 int strings_consed;
127 /* Number of bytes of consing since GC before another GC should be done. */
129 int gc_cons_threshold;
131 /* Nonzero during GC. */
133 int gc_in_progress;
135 /* Nonzero means display messages at beginning and end of GC. */
137 int garbage_collection_messages;
139 #ifndef VIRT_ADDR_VARIES
140 extern
141 #endif /* VIRT_ADDR_VARIES */
142 int malloc_sbrk_used;
144 #ifndef VIRT_ADDR_VARIES
145 extern
146 #endif /* VIRT_ADDR_VARIES */
147 int malloc_sbrk_unused;
149 /* Two limits controlling how much undo information to keep. */
151 int undo_limit;
152 int undo_strong_limit;
154 /* Number of live and free conses etc. */
156 static int total_conses, total_markers, total_symbols, total_vector_size;
157 static int total_free_conses, total_free_markers, total_free_symbols;
158 static int total_free_floats, total_floats;
160 /* Points to memory space allocated as "spare", to be freed if we run
161 out of memory. */
163 static char *spare_memory;
165 /* Amount of spare memory to keep in reserve. */
167 #define SPARE_MEMORY (1 << 14)
169 /* Number of extra blocks malloc should get when it needs more core. */
171 static int malloc_hysteresis;
173 /* Non-nil means defun should do purecopy on the function definition. */
175 Lisp_Object Vpurify_flag;
177 #ifndef HAVE_SHM
179 /* Force it into data space! */
181 EMACS_INT pure[PURESIZE / sizeof (EMACS_INT)] = {0,};
182 #define PUREBEG (char *) pure
184 #else /* not HAVE_SHM */
186 #define pure PURE_SEG_BITS /* Use shared memory segment */
187 #define PUREBEG (char *)PURE_SEG_BITS
189 /* This variable is used only by the XPNTR macro when HAVE_SHM is
190 defined. If we used the PURESIZE macro directly there, that would
191 make most of Emacs dependent on puresize.h, which we don't want -
192 you should be able to change that without too much recompilation.
193 So map_in_data initializes pure_size, and the dependencies work
194 out. */
196 EMACS_INT pure_size;
198 #endif /* not HAVE_SHM */
200 /* Value is non-zero if P points into pure space. */
202 #define PURE_POINTER_P(P) \
203 (((PNTR_COMPARISON_TYPE) (P) \
204 < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)) \
205 && ((PNTR_COMPARISON_TYPE) (P) \
206 >= (PNTR_COMPARISON_TYPE) pure))
208 /* Index in pure at which next pure object will be allocated.. */
210 int pureptr;
212 /* If nonzero, this is a warning delivered by malloc and not yet
213 displayed. */
215 char *pending_malloc_warning;
217 /* Pre-computed signal argument for use when memory is exhausted. */
219 Lisp_Object memory_signal_data;
221 /* Maximum amount of C stack to save when a GC happens. */
223 #ifndef MAX_SAVE_STACK
224 #define MAX_SAVE_STACK 16000
225 #endif
227 /* Buffer in which we save a copy of the C stack at each GC. */
229 char *stack_copy;
230 int stack_copy_size;
232 /* Non-zero means ignore malloc warnings. Set during initialization.
233 Currently not used. */
235 int ignore_warnings;
237 Lisp_Object Qgc_cons_threshold, Qchar_table_extra_slots;
239 static void mark_buffer P_ ((Lisp_Object));
240 static void mark_kboards P_ ((void));
241 static void gc_sweep P_ ((void));
242 static void mark_glyph_matrix P_ ((struct glyph_matrix *));
243 static void mark_face_cache P_ ((struct face_cache *));
245 #ifdef HAVE_WINDOW_SYSTEM
246 static void mark_image P_ ((struct image *));
247 static void mark_image_cache P_ ((struct frame *));
248 #endif /* HAVE_WINDOW_SYSTEM */
250 static struct Lisp_String *allocate_string P_ ((void));
251 static void compact_small_strings P_ ((void));
252 static void free_large_strings P_ ((void));
253 static void sweep_strings P_ ((void));
255 extern int message_enable_multibyte;
257 /* When scanning the C stack for live Lisp objects, Emacs keeps track
258 of what memory allocated via lisp_malloc is intended for what
259 purpose. This enumeration specifies the type of memory. */
261 enum mem_type
263 MEM_TYPE_NON_LISP,
264 MEM_TYPE_BUFFER,
265 MEM_TYPE_CONS,
266 MEM_TYPE_STRING,
267 MEM_TYPE_MISC,
268 MEM_TYPE_SYMBOL,
269 MEM_TYPE_FLOAT,
270 MEM_TYPE_VECTOR
273 #if GC_MARK_STACK
275 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
276 #include <stdio.h> /* For fprintf. */
277 #endif
279 /* A unique object in pure space used to make some Lisp objects
280 on free lists recognizable in O(1). */
282 Lisp_Object Vdead;
284 struct mem_node;
285 static void *lisp_malloc P_ ((int, enum mem_type));
286 static void mark_stack P_ ((void));
287 static void init_stack P_ ((Lisp_Object *));
288 static int live_vector_p P_ ((struct mem_node *, void *));
289 static int live_buffer_p P_ ((struct mem_node *, void *));
290 static int live_string_p P_ ((struct mem_node *, void *));
291 static int live_cons_p P_ ((struct mem_node *, void *));
292 static int live_symbol_p P_ ((struct mem_node *, void *));
293 static int live_float_p P_ ((struct mem_node *, void *));
294 static int live_misc_p P_ ((struct mem_node *, void *));
295 static void mark_maybe_object P_ ((Lisp_Object));
296 static void mark_memory P_ ((void *, void *));
297 static void mem_init P_ ((void));
298 static struct mem_node *mem_insert P_ ((void *, void *, enum mem_type));
299 static void mem_insert_fixup P_ ((struct mem_node *));
300 static void mem_rotate_left P_ ((struct mem_node *));
301 static void mem_rotate_right P_ ((struct mem_node *));
302 static void mem_delete P_ ((struct mem_node *));
303 static void mem_delete_fixup P_ ((struct mem_node *));
304 static INLINE struct mem_node *mem_find P_ ((void *));
306 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
307 static void check_gcpros P_ ((void));
308 #endif
310 #endif /* GC_MARK_STACK != 0 */
313 /************************************************************************
314 Malloc
315 ************************************************************************/
317 /* Write STR to Vstandard_output plus some advice on how to free some
318 memory. Called when memory gets low. */
320 Lisp_Object
321 malloc_warning_1 (str)
322 Lisp_Object str;
324 Fprinc (str, Vstandard_output);
325 write_string ("\nKilling some buffers may delay running out of memory.\n", -1);
326 write_string ("However, certainly by the time you receive the 95% warning,\n", -1);
327 write_string ("you should clean up, kill this Emacs, and start a new one.", -1);
328 return Qnil;
332 /* Function malloc calls this if it finds we are near exhausting
333 storage. */
335 void
336 malloc_warning (str)
337 char *str;
339 pending_malloc_warning = str;
343 /* Display a malloc warning in buffer *Danger*. */
345 void
346 display_malloc_warning ()
348 register Lisp_Object val;
350 val = build_string (pending_malloc_warning);
351 pending_malloc_warning = 0;
352 internal_with_output_to_temp_buffer (" *Danger*", malloc_warning_1, val);
356 #ifdef DOUG_LEA_MALLOC
357 # define BYTES_USED (mallinfo ().arena)
358 #else
359 # define BYTES_USED _bytes_used
360 #endif
363 /* Called if malloc returns zero. */
365 void
366 memory_full ()
368 #ifndef SYSTEM_MALLOC
369 bytes_used_when_full = BYTES_USED;
370 #endif
372 /* The first time we get here, free the spare memory. */
373 if (spare_memory)
375 free (spare_memory);
376 spare_memory = 0;
379 /* This used to call error, but if we've run out of memory, we could
380 get infinite recursion trying to build the string. */
381 while (1)
382 Fsignal (Qnil, memory_signal_data);
386 /* Called if we can't allocate relocatable space for a buffer. */
388 void
389 buffer_memory_full ()
391 /* If buffers use the relocating allocator, no need to free
392 spare_memory, because we may have plenty of malloc space left
393 that we could get, and if we don't, the malloc that fails will
394 itself cause spare_memory to be freed. If buffers don't use the
395 relocating allocator, treat this like any other failing
396 malloc. */
398 #ifndef REL_ALLOC
399 memory_full ();
400 #endif
402 /* This used to call error, but if we've run out of memory, we could
403 get infinite recursion trying to build the string. */
404 while (1)
405 Fsignal (Qerror, memory_signal_data);
409 /* Like malloc but check for no memory and block interrupt input.. */
411 POINTER_TYPE *
412 xmalloc (size)
413 int size;
415 register POINTER_TYPE *val;
417 BLOCK_INPUT;
418 val = (POINTER_TYPE *) malloc (size);
419 UNBLOCK_INPUT;
421 if (!val && size)
422 memory_full ();
423 return val;
427 /* Like realloc but check for no memory and block interrupt input.. */
429 POINTER_TYPE *
430 xrealloc (block, size)
431 POINTER_TYPE *block;
432 int size;
434 register POINTER_TYPE *val;
436 BLOCK_INPUT;
437 /* We must call malloc explicitly when BLOCK is 0, since some
438 reallocs don't do this. */
439 if (! block)
440 val = (POINTER_TYPE *) malloc (size);
441 else
442 val = (POINTER_TYPE *) realloc (block, size);
443 UNBLOCK_INPUT;
445 if (!val && size) memory_full ();
446 return val;
450 /* Like free but block interrupt input.. */
452 void
453 xfree (block)
454 POINTER_TYPE *block;
456 BLOCK_INPUT;
457 free (block);
458 UNBLOCK_INPUT;
462 /* Like strdup, but uses xmalloc. */
464 char *
465 xstrdup (s)
466 char *s;
468 int len = strlen (s) + 1;
469 char *p = (char *) xmalloc (len);
470 bcopy (s, p, len);
471 return p;
475 /* Like malloc but used for allocating Lisp data. NBYTES is the
476 number of bytes to allocate, TYPE describes the intended use of the
477 allcated memory block (for strings, for conses, ...). */
479 static void *
480 lisp_malloc (nbytes, type)
481 int nbytes;
482 enum mem_type type;
484 register void *val;
486 BLOCK_INPUT;
487 val = (void *) malloc (nbytes);
489 #if GC_MARK_STACK
490 if (val && type != MEM_TYPE_NON_LISP)
491 mem_insert (val, (char *) val + nbytes, type);
492 #endif
494 UNBLOCK_INPUT;
495 if (!val && nbytes)
496 memory_full ();
497 return val;
501 /* Return a new buffer structure allocated from the heap with
502 a call to lisp_malloc. */
504 struct buffer *
505 allocate_buffer ()
507 return (struct buffer *) lisp_malloc (sizeof (struct buffer),
508 MEM_TYPE_BUFFER);
512 /* Free BLOCK. This must be called to free memory allocated with a
513 call to lisp_malloc. */
515 void
516 lisp_free (block)
517 long *block;
519 BLOCK_INPUT;
520 free (block);
521 #if GC_MARK_STACK
522 mem_delete (mem_find (block));
523 #endif
524 UNBLOCK_INPUT;
528 /* Arranging to disable input signals while we're in malloc.
530 This only works with GNU malloc. To help out systems which can't
531 use GNU malloc, all the calls to malloc, realloc, and free
532 elsewhere in the code should be inside a BLOCK_INPUT/UNBLOCK_INPUT
533 pairs; unfortunately, we have no idea what C library functions
534 might call malloc, so we can't really protect them unless you're
535 using GNU malloc. Fortunately, most of the major operating can use
536 GNU malloc. */
538 #ifndef SYSTEM_MALLOC
540 extern void * (*__malloc_hook) ();
541 static void * (*old_malloc_hook) ();
542 extern void * (*__realloc_hook) ();
543 static void * (*old_realloc_hook) ();
544 extern void (*__free_hook) ();
545 static void (*old_free_hook) ();
547 /* This function is used as the hook for free to call. */
549 static void
550 emacs_blocked_free (ptr)
551 void *ptr;
553 BLOCK_INPUT;
554 __free_hook = old_free_hook;
555 free (ptr);
556 /* If we released our reserve (due to running out of memory),
557 and we have a fair amount free once again,
558 try to set aside another reserve in case we run out once more. */
559 if (spare_memory == 0
560 /* Verify there is enough space that even with the malloc
561 hysteresis this call won't run out again.
562 The code here is correct as long as SPARE_MEMORY
563 is substantially larger than the block size malloc uses. */
564 && (bytes_used_when_full
565 > BYTES_USED + max (malloc_hysteresis, 4) * SPARE_MEMORY))
566 spare_memory = (char *) malloc (SPARE_MEMORY);
568 __free_hook = emacs_blocked_free;
569 UNBLOCK_INPUT;
573 /* If we released our reserve (due to running out of memory),
574 and we have a fair amount free once again,
575 try to set aside another reserve in case we run out once more.
577 This is called when a relocatable block is freed in ralloc.c. */
579 void
580 refill_memory_reserve ()
582 if (spare_memory == 0)
583 spare_memory = (char *) malloc (SPARE_MEMORY);
587 /* This function is the malloc hook that Emacs uses. */
589 static void *
590 emacs_blocked_malloc (size)
591 unsigned size;
593 void *value;
595 BLOCK_INPUT;
596 __malloc_hook = old_malloc_hook;
597 #ifdef DOUG_LEA_MALLOC
598 mallopt (M_TOP_PAD, malloc_hysteresis * 4096);
599 #else
600 __malloc_extra_blocks = malloc_hysteresis;
601 #endif
602 value = (void *) malloc (size);
603 __malloc_hook = emacs_blocked_malloc;
604 UNBLOCK_INPUT;
606 return value;
610 /* This function is the realloc hook that Emacs uses. */
612 static void *
613 emacs_blocked_realloc (ptr, size)
614 void *ptr;
615 unsigned size;
617 void *value;
619 BLOCK_INPUT;
620 __realloc_hook = old_realloc_hook;
621 value = (void *) realloc (ptr, size);
622 __realloc_hook = emacs_blocked_realloc;
623 UNBLOCK_INPUT;
625 return value;
629 /* Called from main to set up malloc to use our hooks. */
631 void
632 uninterrupt_malloc ()
634 if (__free_hook != emacs_blocked_free)
635 old_free_hook = __free_hook;
636 __free_hook = emacs_blocked_free;
638 if (__malloc_hook != emacs_blocked_malloc)
639 old_malloc_hook = __malloc_hook;
640 __malloc_hook = emacs_blocked_malloc;
642 if (__realloc_hook != emacs_blocked_realloc)
643 old_realloc_hook = __realloc_hook;
644 __realloc_hook = emacs_blocked_realloc;
647 #endif /* not SYSTEM_MALLOC */
651 /***********************************************************************
652 Interval Allocation
653 ***********************************************************************/
655 /* Number of intervals allocated in an interval_block structure.
656 The 1020 is 1024 minus malloc overhead. */
658 #define INTERVAL_BLOCK_SIZE \
659 ((1020 - sizeof (struct interval_block *)) / sizeof (struct interval))
661 /* Intervals are allocated in chunks in form of an interval_block
662 structure. */
664 struct interval_block
666 struct interval_block *next;
667 struct interval intervals[INTERVAL_BLOCK_SIZE];
670 /* Current interval block. Its `next' pointer points to older
671 blocks. */
673 struct interval_block *interval_block;
675 /* Index in interval_block above of the next unused interval
676 structure. */
678 static int interval_block_index;
680 /* Number of free and live intervals. */
682 static int total_free_intervals, total_intervals;
684 /* List of free intervals. */
686 INTERVAL interval_free_list;
688 /* Total number of interval blocks now in use. */
690 int n_interval_blocks;
693 /* Initialize interval allocation. */
695 static void
696 init_intervals ()
698 interval_block
699 = (struct interval_block *) lisp_malloc (sizeof *interval_block,
700 MEM_TYPE_NON_LISP);
701 interval_block->next = 0;
702 bzero ((char *) interval_block->intervals, sizeof interval_block->intervals);
703 interval_block_index = 0;
704 interval_free_list = 0;
705 n_interval_blocks = 1;
709 /* Return a new interval. */
711 INTERVAL
712 make_interval ()
714 INTERVAL val;
716 if (interval_free_list)
718 val = interval_free_list;
719 interval_free_list = INTERVAL_PARENT (interval_free_list);
721 else
723 if (interval_block_index == INTERVAL_BLOCK_SIZE)
725 register struct interval_block *newi;
727 newi = (struct interval_block *) lisp_malloc (sizeof *newi,
728 MEM_TYPE_NON_LISP);
730 VALIDATE_LISP_STORAGE (newi, sizeof *newi);
731 newi->next = interval_block;
732 interval_block = newi;
733 interval_block_index = 0;
734 n_interval_blocks++;
736 val = &interval_block->intervals[interval_block_index++];
738 consing_since_gc += sizeof (struct interval);
739 intervals_consed++;
740 RESET_INTERVAL (val);
741 return val;
745 /* Mark Lisp objects in interval I. */
747 static void
748 mark_interval (i, dummy)
749 register INTERVAL i;
750 Lisp_Object dummy;
752 if (XMARKBIT (i->plist))
753 abort ();
754 mark_object (&i->plist);
755 XMARK (i->plist);
759 /* Mark the interval tree rooted in TREE. Don't call this directly;
760 use the macro MARK_INTERVAL_TREE instead. */
762 static void
763 mark_interval_tree (tree)
764 register INTERVAL tree;
766 /* No need to test if this tree has been marked already; this
767 function is always called through the MARK_INTERVAL_TREE macro,
768 which takes care of that. */
770 /* XMARK expands to an assignment; the LHS of an assignment can't be
771 a cast. */
772 XMARK (tree->up.obj);
774 traverse_intervals (tree, 1, 0, mark_interval, Qnil);
778 /* Mark the interval tree rooted in I. */
780 #define MARK_INTERVAL_TREE(i) \
781 do { \
782 if (!NULL_INTERVAL_P (i) \
783 && ! XMARKBIT (i->up.obj)) \
784 mark_interval_tree (i); \
785 } while (0)
788 /* The oddity in the call to XUNMARK is necessary because XUNMARK
789 expands to an assignment to its argument, and most C compilers
790 don't support casts on the left operand of `='. */
792 #define UNMARK_BALANCE_INTERVALS(i) \
793 do { \
794 if (! NULL_INTERVAL_P (i)) \
796 XUNMARK ((i)->up.obj); \
797 (i) = balance_intervals (i); \
799 } while (0)
802 /* Number support. If NO_UNION_TYPE isn't in effect, we
803 can't create number objects in macros. */
804 #ifndef make_number
805 Lisp_Object
806 make_number (n)
807 int n;
809 Lisp_Object obj;
810 obj.s.val = n;
811 obj.s.type = Lisp_Int;
812 return obj;
814 #endif
816 /***********************************************************************
817 String Allocation
818 ***********************************************************************/
820 /* Lisp_Strings are allocated in string_block structures. When a new
821 string_block is allocated, all the Lisp_Strings it contains are
822 added to a free-list stiing_free_list. When a new Lisp_String is
823 needed, it is taken from that list. During the sweep phase of GC,
824 string_blocks that are entirely free are freed, except two which
825 we keep.
827 String data is allocated from sblock structures. Strings larger
828 than LARGE_STRING_BYTES, get their own sblock, data for smaller
829 strings is sub-allocated out of sblocks of size SBLOCK_SIZE.
831 Sblocks consist internally of sdata structures, one for each
832 Lisp_String. The sdata structure points to the Lisp_String it
833 belongs to. The Lisp_String points back to the `u.data' member of
834 its sdata structure.
836 When a Lisp_String is freed during GC, it is put back on
837 string_free_list, and its `data' member and its sdata's `string'
838 pointer is set to null. The size of the string is recorded in the
839 `u.nbytes' member of the sdata. So, sdata structures that are no
840 longer used, can be easily recognized, and it's easy to compact the
841 sblocks of small strings which we do in compact_small_strings. */
843 /* Size in bytes of an sblock structure used for small strings. This
844 is 8192 minus malloc overhead. */
846 #define SBLOCK_SIZE 8188
848 /* Strings larger than this are considered large strings. String data
849 for large strings is allocated from individual sblocks. */
851 #define LARGE_STRING_BYTES 1024
853 /* Structure describing string memory sub-allocated from an sblock.
854 This is where the contents of Lisp strings are stored. */
856 struct sdata
858 /* Back-pointer to the string this sdata belongs to. If null, this
859 structure is free, and the NBYTES member of the union below
860 contains the string's byte size (the same value that STRING_BYTES
861 would return if STRING were non-null). If non-null, STRING_BYTES
862 (STRING) is the size of the data, and DATA contains the string's
863 contents. */
864 struct Lisp_String *string;
866 union
868 /* When STRING in non-null. */
869 unsigned char data[1];
871 /* When STRING is null. */
872 EMACS_INT nbytes;
873 } u;
876 /* Structure describing a block of memory which is sub-allocated to
877 obtain string data memory for strings. Blocks for small strings
878 are of fixed size SBLOCK_SIZE. Blocks for large strings are made
879 as large as needed. */
881 struct sblock
883 /* Next in list. */
884 struct sblock *next;
886 /* Pointer to the next free sdata block. This points past the end
887 of the sblock if there isn't any space left in this block. */
888 struct sdata *next_free;
890 /* Start of data. */
891 struct sdata first_data;
894 /* Number of Lisp strings in a string_block structure. The 1020 is
895 1024 minus malloc overhead. */
897 #define STRINGS_IN_STRING_BLOCK \
898 ((1020 - sizeof (struct string_block *)) / sizeof (struct Lisp_String))
900 /* Structure describing a block from which Lisp_String structures
901 are allocated. */
903 struct string_block
905 struct string_block *next;
906 struct Lisp_String strings[STRINGS_IN_STRING_BLOCK];
909 /* Head and tail of the list of sblock structures holding Lisp string
910 data. We always allocate from current_sblock. The NEXT pointers
911 in the sblock structures go from oldest_sblock to current_sblock. */
913 static struct sblock *oldest_sblock, *current_sblock;
915 /* List of sblocks for large strings. */
917 static struct sblock *large_sblocks;
919 /* List of string_block structures, and how many there are. */
921 static struct string_block *string_blocks;
922 static int n_string_blocks;
924 /* Free-list of Lisp_Strings. */
926 static struct Lisp_String *string_free_list;
928 /* Number of live and free Lisp_Strings. */
930 static int total_strings, total_free_strings;
932 /* Number of bytes used by live strings. */
934 static int total_string_size;
936 /* Given a pointer to a Lisp_String S which is on the free-list
937 string_free_list, return a pointer to its successor in the
938 free-list. */
940 #define NEXT_FREE_LISP_STRING(S) (*(struct Lisp_String **) (S))
942 /* Return a pointer to the sdata structure belonging to Lisp string S.
943 S must be live, i.e. S->data must not be null. S->data is actually
944 a pointer to the `u.data' member of its sdata structure; the
945 structure starts at a constant offset in front of that. */
947 #define SDATA_OF_STRING(S) \
948 ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *)))
950 /* Value is the size of an sdata structure large enough to hold NBYTES
951 bytes of string data. The value returned includes a terminating
952 NUL byte, the size of the sdata structure, and padding. */
954 #define SDATA_SIZE(NBYTES) \
955 ((sizeof (struct Lisp_String *) \
956 + (NBYTES) + 1 \
957 + sizeof (EMACS_INT) - 1) \
958 & ~(sizeof (EMACS_INT) - 1))
961 /* Initialize string allocation. Called from init_alloc_once. */
963 void
964 init_strings ()
966 total_strings = total_free_strings = total_string_size = 0;
967 oldest_sblock = current_sblock = large_sblocks = NULL;
968 string_blocks = NULL;
969 n_string_blocks = 0;
970 string_free_list = NULL;
974 /* Return a new Lisp_String. */
976 static struct Lisp_String *
977 allocate_string ()
979 struct Lisp_String *s;
981 /* If the free-list is empty, allocate a new string_block, and
982 add all the Lisp_Strings in it to the free-list. */
983 if (string_free_list == NULL)
985 struct string_block *b;
986 int i;
988 b = (struct string_block *) lisp_malloc (sizeof *b, MEM_TYPE_STRING);
989 VALIDATE_LISP_STORAGE (b, sizeof *b);
990 bzero (b, sizeof *b);
991 b->next = string_blocks;
992 string_blocks = b;
993 ++n_string_blocks;
995 for (i = STRINGS_IN_STRING_BLOCK - 1; i >= 0; --i)
997 s = b->strings + i;
998 NEXT_FREE_LISP_STRING (s) = string_free_list;
999 string_free_list = s;
1002 total_free_strings += STRINGS_IN_STRING_BLOCK;
1005 /* Pop a Lisp_String off the free-list. */
1006 s = string_free_list;
1007 string_free_list = NEXT_FREE_LISP_STRING (s);
1009 /* Probably not strictly necessary, but play it safe. */
1010 bzero (s, sizeof *s);
1012 --total_free_strings;
1013 ++total_strings;
1014 ++strings_consed;
1015 consing_since_gc += sizeof *s;
1017 return s;
1021 /* Set up Lisp_String S for holding NCHARS characters, NBYTES bytes,
1022 plus a NUL byte at the end. Allocate an sdata structure for S, and
1023 set S->data to its `u.data' member. Store a NUL byte at the end of
1024 S->data. Set S->size to NCHARS and S->size_byte to NBYTES. Free
1025 S->data if it was initially non-null. */
1027 void
1028 allocate_string_data (s, nchars, nbytes)
1029 struct Lisp_String *s;
1030 int nchars, nbytes;
1032 struct sdata *data;
1033 struct sblock *b;
1034 int needed;
1036 /* Determine the number of bytes needed to store NBYTES bytes
1037 of string data. */
1038 needed = SDATA_SIZE (nbytes);
1040 if (nbytes > LARGE_STRING_BYTES)
1042 int size = sizeof *b - sizeof (struct sdata) + needed;
1044 #ifdef DOUG_LEA_MALLOC
1045 /* Prevent mmap'ing the chunk (which is potentially very large). */
1046 mallopt (M_MMAP_MAX, 0);
1047 #endif
1049 b = (struct sblock *) lisp_malloc (size, MEM_TYPE_NON_LISP);
1051 #ifdef DOUG_LEA_MALLOC
1052 /* Back to a reasonable maximum of mmap'ed areas. */
1053 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
1054 #endif
1056 b->next_free = &b->first_data;
1057 b->first_data.string = NULL;
1058 b->next = large_sblocks;
1059 large_sblocks = b;
1061 else if (current_sblock == NULL
1062 || (((char *) current_sblock + SBLOCK_SIZE
1063 - (char *) current_sblock->next_free)
1064 < needed))
1066 /* Not enough room in the current sblock. */
1067 b = (struct sblock *) lisp_malloc (SBLOCK_SIZE, MEM_TYPE_NON_LISP);
1068 b->next_free = &b->first_data;
1069 b->first_data.string = NULL;
1070 b->next = NULL;
1072 if (current_sblock)
1073 current_sblock->next = b;
1074 else
1075 oldest_sblock = b;
1076 current_sblock = b;
1078 else
1079 b = current_sblock;
1081 /* If S had already data assigned, mark that as free by setting
1082 its string back-pointer to null, and recording the size of
1083 the data in it.. */
1084 if (s->data)
1086 data = SDATA_OF_STRING (s);
1087 data->u.nbytes = GC_STRING_BYTES (s);
1088 data->string = NULL;
1091 data = b->next_free;
1092 data->string = s;
1093 s->data = data->u.data;
1094 s->size = nchars;
1095 s->size_byte = nbytes;
1096 s->data[nbytes] = '\0';
1097 b->next_free = (struct sdata *) ((char *) data + needed);
1099 consing_since_gc += needed;
1103 /* Sweep and compact strings. */
1105 static void
1106 sweep_strings ()
1108 struct string_block *b, *next;
1109 struct string_block *live_blocks = NULL;
1111 string_free_list = NULL;
1112 total_strings = total_free_strings = 0;
1113 total_string_size = 0;
1115 /* Scan strings_blocks, free Lisp_Strings that aren't marked. */
1116 for (b = string_blocks; b; b = next)
1118 int i, nfree = 0;
1119 struct Lisp_String *free_list_before = string_free_list;
1121 next = b->next;
1123 for (i = 0; i < STRINGS_IN_STRING_BLOCK; ++i)
1125 struct Lisp_String *s = b->strings + i;
1127 if (s->data)
1129 /* String was not on free-list before. */
1130 if (STRING_MARKED_P (s))
1132 /* String is live; unmark it and its intervals. */
1133 UNMARK_STRING (s);
1135 if (!NULL_INTERVAL_P (s->intervals))
1136 UNMARK_BALANCE_INTERVALS (s->intervals);
1138 ++total_strings;
1139 total_string_size += STRING_BYTES (s);
1141 else
1143 /* String is dead. Put it on the free-list. */
1144 struct sdata *data = SDATA_OF_STRING (s);
1146 /* Save the size of S in its sdata so that we know
1147 how large that is. Reset the sdata's string
1148 back-pointer so that we know it's free. */
1149 data->u.nbytes = GC_STRING_BYTES (s);
1150 data->string = NULL;
1152 /* Reset the strings's `data' member so that we
1153 know it's free. */
1154 s->data = NULL;
1156 /* Put the string on the free-list. */
1157 NEXT_FREE_LISP_STRING (s) = string_free_list;
1158 string_free_list = s;
1159 ++nfree;
1162 else
1164 /* S was on the free-list before. Put it there again. */
1165 NEXT_FREE_LISP_STRING (s) = string_free_list;
1166 string_free_list = s;
1167 ++nfree;
1171 /* Free blocks that contain free Lisp_Strings only, except
1172 the first two of them. */
1173 if (nfree == STRINGS_IN_STRING_BLOCK
1174 && total_free_strings > STRINGS_IN_STRING_BLOCK)
1176 lisp_free (b);
1177 --n_string_blocks;
1178 string_free_list = free_list_before;
1180 else
1182 total_free_strings += nfree;
1183 b->next = live_blocks;
1184 live_blocks = b;
1188 string_blocks = live_blocks;
1189 free_large_strings ();
1190 compact_small_strings ();
1194 /* Free dead large strings. */
1196 static void
1197 free_large_strings ()
1199 struct sblock *b, *next;
1200 struct sblock *live_blocks = NULL;
1202 for (b = large_sblocks; b; b = next)
1204 next = b->next;
1206 if (b->first_data.string == NULL)
1207 lisp_free (b);
1208 else
1210 b->next = live_blocks;
1211 live_blocks = b;
1215 large_sblocks = live_blocks;
1219 /* Compact data of small strings. Free sblocks that don't contain
1220 data of live strings after compaction. */
1222 static void
1223 compact_small_strings ()
1225 struct sblock *b, *tb, *next;
1226 struct sdata *from, *to, *end, *tb_end;
1227 struct sdata *to_end, *from_end;
1229 /* TB is the sblock we copy to, TO is the sdata within TB we copy
1230 to, and TB_END is the end of TB. */
1231 tb = oldest_sblock;
1232 tb_end = (struct sdata *) ((char *) tb + SBLOCK_SIZE);
1233 to = &tb->first_data;
1235 /* Step through the blocks from the oldest to the youngest. We
1236 expect that old blocks will stabilize over time, so that less
1237 copying will happen this way. */
1238 for (b = oldest_sblock; b; b = b->next)
1240 end = b->next_free;
1241 xassert ((char *) end <= (char *) b + SBLOCK_SIZE);
1243 for (from = &b->first_data; from < end; from = from_end)
1245 /* Compute the next FROM here because copying below may
1246 overwrite data we need to compute it. */
1247 int nbytes;
1249 if (from->string)
1250 nbytes = GC_STRING_BYTES (from->string);
1251 else
1252 nbytes = from->u.nbytes;
1254 nbytes = SDATA_SIZE (nbytes);
1255 from_end = (struct sdata *) ((char *) from + nbytes);
1257 /* FROM->string non-null means it's alive. Copy its data. */
1258 if (from->string)
1260 /* If TB is full, proceed with the next sblock. */
1261 to_end = (struct sdata *) ((char *) to + nbytes);
1262 if (to_end > tb_end)
1264 tb->next_free = to;
1265 tb = tb->next;
1266 tb_end = (struct sdata *) ((char *) tb + SBLOCK_SIZE);
1267 to = &tb->first_data;
1268 to_end = (struct sdata *) ((char *) to + nbytes);
1271 /* Copy, and update the string's `data' pointer. */
1272 if (from != to)
1274 bcopy (from, to, nbytes);
1275 to->string->data = to->u.data;
1278 /* Advance past the sdata we copied to. */
1279 to = to_end;
1284 /* The rest of the sblocks following TB don't contain live data, so
1285 we can free them. */
1286 for (b = tb->next; b; b = next)
1288 next = b->next;
1289 lisp_free (b);
1292 tb->next_free = to;
1293 tb->next = NULL;
1294 current_sblock = tb;
1298 DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0,
1299 "Return a newly created string of length LENGTH, with each element being INIT.\n\
1300 Both LENGTH and INIT must be numbers.")
1301 (length, init)
1302 Lisp_Object length, init;
1304 register Lisp_Object val;
1305 register unsigned char *p, *end;
1306 int c, nbytes;
1308 CHECK_NATNUM (length, 0);
1309 CHECK_NUMBER (init, 1);
1311 c = XINT (init);
1312 if (SINGLE_BYTE_CHAR_P (c))
1314 nbytes = XINT (length);
1315 val = make_uninit_string (nbytes);
1316 p = XSTRING (val)->data;
1317 end = p + XSTRING (val)->size;
1318 while (p != end)
1319 *p++ = c;
1321 else
1323 unsigned char str[4];
1324 int len = CHAR_STRING (c, str);
1326 nbytes = len * XINT (length);
1327 val = make_uninit_multibyte_string (XINT (length), nbytes);
1328 p = XSTRING (val)->data;
1329 end = p + nbytes;
1330 while (p != end)
1332 bcopy (str, p, len);
1333 p += len;
1337 *p = 0;
1338 return val;
1342 DEFUN ("make-bool-vector", Fmake_bool_vector, Smake_bool_vector, 2, 2, 0,
1343 "Return a new bool-vector of length LENGTH, using INIT for as each element.\n\
1344 LENGTH must be a number. INIT matters only in whether it is t or nil.")
1345 (length, init)
1346 Lisp_Object length, init;
1348 register Lisp_Object val;
1349 struct Lisp_Bool_Vector *p;
1350 int real_init, i;
1351 int length_in_chars, length_in_elts, bits_per_value;
1353 CHECK_NATNUM (length, 0);
1355 bits_per_value = sizeof (EMACS_INT) * BITS_PER_CHAR;
1357 length_in_elts = (XFASTINT (length) + bits_per_value - 1) / bits_per_value;
1358 length_in_chars = ((XFASTINT (length) + BITS_PER_CHAR - 1) / BITS_PER_CHAR);
1360 /* We must allocate one more elements than LENGTH_IN_ELTS for the
1361 slot `size' of the struct Lisp_Bool_Vector. */
1362 val = Fmake_vector (make_number (length_in_elts + 1), Qnil);
1363 p = XBOOL_VECTOR (val);
1365 /* Get rid of any bits that would cause confusion. */
1366 p->vector_size = 0;
1367 XSETBOOL_VECTOR (val, p);
1368 p->size = XFASTINT (length);
1370 real_init = (NILP (init) ? 0 : -1);
1371 for (i = 0; i < length_in_chars ; i++)
1372 p->data[i] = real_init;
1374 /* Clear the extraneous bits in the last byte. */
1375 if (XINT (length) != length_in_chars * BITS_PER_CHAR)
1376 XBOOL_VECTOR (val)->data[length_in_chars - 1]
1377 &= (1 << (XINT (length) % BITS_PER_CHAR)) - 1;
1379 return val;
1383 /* Make a string from NBYTES bytes at CONTENTS, and compute the number
1384 of characters from the contents. This string may be unibyte or
1385 multibyte, depending on the contents. */
1387 Lisp_Object
1388 make_string (contents, nbytes)
1389 char *contents;
1390 int nbytes;
1392 register Lisp_Object val;
1393 int nchars, multibyte_nbytes;
1395 parse_str_as_multibyte (contents, nbytes, &nchars, &multibyte_nbytes);
1396 val = make_uninit_multibyte_string (nchars, nbytes);
1397 bcopy (contents, XSTRING (val)->data, nbytes);
1398 if (nbytes == nchars || nbytes != multibyte_nbytes)
1399 /* CONTENTS contains no multibyte sequences or contains an invalid
1400 multibyte sequence. We must make unibyte string. */
1401 SET_STRING_BYTES (XSTRING (val), -1);
1402 return val;
1406 /* Make an unibyte string from LENGTH bytes at CONTENTS. */
1408 Lisp_Object
1409 make_unibyte_string (contents, length)
1410 char *contents;
1411 int length;
1413 register Lisp_Object val;
1414 val = make_uninit_string (length);
1415 bcopy (contents, XSTRING (val)->data, length);
1416 SET_STRING_BYTES (XSTRING (val), -1);
1417 return val;
1421 /* Make a multibyte string from NCHARS characters occupying NBYTES
1422 bytes at CONTENTS. */
1424 Lisp_Object
1425 make_multibyte_string (contents, nchars, nbytes)
1426 char *contents;
1427 int nchars, nbytes;
1429 register Lisp_Object val;
1430 val = make_uninit_multibyte_string (nchars, nbytes);
1431 bcopy (contents, XSTRING (val)->data, nbytes);
1432 return val;
1436 /* Make a string from NCHARS characters occupying NBYTES bytes at
1437 CONTENTS. It is a multibyte string if NBYTES != NCHARS. */
1439 Lisp_Object
1440 make_string_from_bytes (contents, nchars, nbytes)
1441 char *contents;
1442 int nchars, nbytes;
1444 register Lisp_Object val;
1445 val = make_uninit_multibyte_string (nchars, nbytes);
1446 bcopy (contents, XSTRING (val)->data, nbytes);
1447 if (STRING_BYTES (XSTRING (val)) == XSTRING (val)->size)
1448 SET_STRING_BYTES (XSTRING (val), -1);
1449 return val;
1453 /* Make a string from NCHARS characters occupying NBYTES bytes at
1454 CONTENTS. The argument MULTIBYTE controls whether to label the
1455 string as multibyte. */
1457 Lisp_Object
1458 make_specified_string (contents, nchars, nbytes, multibyte)
1459 char *contents;
1460 int nchars, nbytes;
1461 int multibyte;
1463 register Lisp_Object val;
1464 val = make_uninit_multibyte_string (nchars, nbytes);
1465 bcopy (contents, XSTRING (val)->data, nbytes);
1466 if (!multibyte)
1467 SET_STRING_BYTES (XSTRING (val), -1);
1468 return val;
1472 /* Make a string from the data at STR, treating it as multibyte if the
1473 data warrants. */
1475 Lisp_Object
1476 build_string (str)
1477 char *str;
1479 return make_string (str, strlen (str));
1483 /* Return an unibyte Lisp_String set up to hold LENGTH characters
1484 occupying LENGTH bytes. */
1486 Lisp_Object
1487 make_uninit_string (length)
1488 int length;
1490 Lisp_Object val;
1491 val = make_uninit_multibyte_string (length, length);
1492 SET_STRING_BYTES (XSTRING (val), -1);
1493 return val;
1497 /* Return a multibyte Lisp_String set up to hold NCHARS characters
1498 which occupy NBYTES bytes. */
1500 Lisp_Object
1501 make_uninit_multibyte_string (nchars, nbytes)
1502 int nchars, nbytes;
1504 Lisp_Object string;
1505 struct Lisp_String *s;
1507 if (nchars < 0)
1508 abort ();
1510 s = allocate_string ();
1511 allocate_string_data (s, nchars, nbytes);
1512 XSETSTRING (string, s);
1513 string_chars_consed += nbytes;
1514 return string;
1519 /***********************************************************************
1520 Float Allocation
1521 ***********************************************************************/
1523 /* We store float cells inside of float_blocks, allocating a new
1524 float_block with malloc whenever necessary. Float cells reclaimed
1525 by GC are put on a free list to be reallocated before allocating
1526 any new float cells from the latest float_block.
1528 Each float_block is just under 1020 bytes long, since malloc really
1529 allocates in units of powers of two and uses 4 bytes for its own
1530 overhead. */
1532 #define FLOAT_BLOCK_SIZE \
1533 ((1020 - sizeof (struct float_block *)) / sizeof (struct Lisp_Float))
1535 struct float_block
1537 struct float_block *next;
1538 struct Lisp_Float floats[FLOAT_BLOCK_SIZE];
1541 /* Current float_block. */
1543 struct float_block *float_block;
1545 /* Index of first unused Lisp_Float in the current float_block. */
1547 int float_block_index;
1549 /* Total number of float blocks now in use. */
1551 int n_float_blocks;
1553 /* Free-list of Lisp_Floats. */
1555 struct Lisp_Float *float_free_list;
1558 /* Initialze float allocation. */
1560 void
1561 init_float ()
1563 float_block = (struct float_block *) lisp_malloc (sizeof *float_block,
1564 MEM_TYPE_FLOAT);
1565 float_block->next = 0;
1566 bzero ((char *) float_block->floats, sizeof float_block->floats);
1567 float_block_index = 0;
1568 float_free_list = 0;
1569 n_float_blocks = 1;
1573 /* Explicitly free a float cell by putting it on the free-list. */
1575 void
1576 free_float (ptr)
1577 struct Lisp_Float *ptr;
1579 *(struct Lisp_Float **)&ptr->data = float_free_list;
1580 #if GC_MARK_STACK
1581 ptr->type = Vdead;
1582 #endif
1583 float_free_list = ptr;
1587 /* Return a new float object with value FLOAT_VALUE. */
1589 Lisp_Object
1590 make_float (float_value)
1591 double float_value;
1593 register Lisp_Object val;
1595 if (float_free_list)
1597 /* We use the data field for chaining the free list
1598 so that we won't use the same field that has the mark bit. */
1599 XSETFLOAT (val, float_free_list);
1600 float_free_list = *(struct Lisp_Float **)&float_free_list->data;
1602 else
1604 if (float_block_index == FLOAT_BLOCK_SIZE)
1606 register struct float_block *new;
1608 new = (struct float_block *) lisp_malloc (sizeof *new,
1609 MEM_TYPE_FLOAT);
1610 VALIDATE_LISP_STORAGE (new, sizeof *new);
1611 new->next = float_block;
1612 float_block = new;
1613 float_block_index = 0;
1614 n_float_blocks++;
1616 XSETFLOAT (val, &float_block->floats[float_block_index++]);
1619 XFLOAT_DATA (val) = float_value;
1620 XSETFASTINT (XFLOAT (val)->type, 0); /* bug chasing -wsr */
1621 consing_since_gc += sizeof (struct Lisp_Float);
1622 floats_consed++;
1623 return val;
1628 /***********************************************************************
1629 Cons Allocation
1630 ***********************************************************************/
1632 /* We store cons cells inside of cons_blocks, allocating a new
1633 cons_block with malloc whenever necessary. Cons cells reclaimed by
1634 GC are put on a free list to be reallocated before allocating
1635 any new cons cells from the latest cons_block.
1637 Each cons_block is just under 1020 bytes long,
1638 since malloc really allocates in units of powers of two
1639 and uses 4 bytes for its own overhead. */
1641 #define CONS_BLOCK_SIZE \
1642 ((1020 - sizeof (struct cons_block *)) / sizeof (struct Lisp_Cons))
1644 struct cons_block
1646 struct cons_block *next;
1647 struct Lisp_Cons conses[CONS_BLOCK_SIZE];
1650 /* Current cons_block. */
1652 struct cons_block *cons_block;
1654 /* Index of first unused Lisp_Cons in the current block. */
1656 int cons_block_index;
1658 /* Free-list of Lisp_Cons structures. */
1660 struct Lisp_Cons *cons_free_list;
1662 /* Total number of cons blocks now in use. */
1664 int n_cons_blocks;
1667 /* Initialize cons allocation. */
1669 void
1670 init_cons ()
1672 cons_block = (struct cons_block *) lisp_malloc (sizeof *cons_block,
1673 MEM_TYPE_CONS);
1674 cons_block->next = 0;
1675 bzero ((char *) cons_block->conses, sizeof cons_block->conses);
1676 cons_block_index = 0;
1677 cons_free_list = 0;
1678 n_cons_blocks = 1;
1682 /* Explicitly free a cons cell by putting it on the free-list. */
1684 void
1685 free_cons (ptr)
1686 struct Lisp_Cons *ptr;
1688 *(struct Lisp_Cons **)&ptr->cdr = cons_free_list;
1689 #if GC_MARK_STACK
1690 ptr->car = Vdead;
1691 #endif
1692 cons_free_list = ptr;
1696 DEFUN ("cons", Fcons, Scons, 2, 2, 0,
1697 "Create a new cons, give it CAR and CDR as components, and return it.")
1698 (car, cdr)
1699 Lisp_Object car, cdr;
1701 register Lisp_Object val;
1703 if (cons_free_list)
1705 /* We use the cdr for chaining the free list
1706 so that we won't use the same field that has the mark bit. */
1707 XSETCONS (val, cons_free_list);
1708 cons_free_list = *(struct Lisp_Cons **)&cons_free_list->cdr;
1710 else
1712 if (cons_block_index == CONS_BLOCK_SIZE)
1714 register struct cons_block *new;
1715 new = (struct cons_block *) lisp_malloc (sizeof *new,
1716 MEM_TYPE_CONS);
1717 VALIDATE_LISP_STORAGE (new, sizeof *new);
1718 new->next = cons_block;
1719 cons_block = new;
1720 cons_block_index = 0;
1721 n_cons_blocks++;
1723 XSETCONS (val, &cons_block->conses[cons_block_index++]);
1726 XCAR (val) = car;
1727 XCDR (val) = cdr;
1728 consing_since_gc += sizeof (struct Lisp_Cons);
1729 cons_cells_consed++;
1730 return val;
1734 /* Make a list of 2, 3, 4 or 5 specified objects. */
1736 Lisp_Object
1737 list2 (arg1, arg2)
1738 Lisp_Object arg1, arg2;
1740 return Fcons (arg1, Fcons (arg2, Qnil));
1744 Lisp_Object
1745 list3 (arg1, arg2, arg3)
1746 Lisp_Object arg1, arg2, arg3;
1748 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Qnil)));
1752 Lisp_Object
1753 list4 (arg1, arg2, arg3, arg4)
1754 Lisp_Object arg1, arg2, arg3, arg4;
1756 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4, Qnil))));
1760 Lisp_Object
1761 list5 (arg1, arg2, arg3, arg4, arg5)
1762 Lisp_Object arg1, arg2, arg3, arg4, arg5;
1764 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4,
1765 Fcons (arg5, Qnil)))));
1769 DEFUN ("list", Flist, Slist, 0, MANY, 0,
1770 "Return a newly created list with specified arguments as elements.\n\
1771 Any number of arguments, even zero arguments, are allowed.")
1772 (nargs, args)
1773 int nargs;
1774 register Lisp_Object *args;
1776 register Lisp_Object val;
1777 val = Qnil;
1779 while (nargs > 0)
1781 nargs--;
1782 val = Fcons (args[nargs], val);
1784 return val;
1788 DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0,
1789 "Return a newly created list of length LENGTH, with each element being INIT.")
1790 (length, init)
1791 register Lisp_Object length, init;
1793 register Lisp_Object val;
1794 register int size;
1796 CHECK_NATNUM (length, 0);
1797 size = XFASTINT (length);
1799 val = Qnil;
1800 while (size-- > 0)
1801 val = Fcons (init, val);
1802 return val;
1807 /***********************************************************************
1808 Vector Allocation
1809 ***********************************************************************/
1811 /* Singly-linked list of all vectors. */
1813 struct Lisp_Vector *all_vectors;
1815 /* Total number of vector-like objects now in use. */
1817 int n_vectors;
1820 /* Value is a pointer to a newly allocated Lisp_Vector structure
1821 with room for LEN Lisp_Objects. */
1823 struct Lisp_Vector *
1824 allocate_vectorlike (len)
1825 EMACS_INT len;
1827 struct Lisp_Vector *p;
1828 int nbytes;
1830 #ifdef DOUG_LEA_MALLOC
1831 /* Prevent mmap'ing the chunk (which is potentially very large).. */
1832 mallopt (M_MMAP_MAX, 0);
1833 #endif
1835 nbytes = sizeof *p + (len - 1) * sizeof p->contents[0];
1836 p = (struct Lisp_Vector *) lisp_malloc (nbytes, MEM_TYPE_VECTOR);
1838 #ifdef DOUG_LEA_MALLOC
1839 /* Back to a reasonable maximum of mmap'ed areas. */
1840 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
1841 #endif
1843 VALIDATE_LISP_STORAGE (p, 0);
1844 consing_since_gc += nbytes;
1845 vector_cells_consed += len;
1847 p->next = all_vectors;
1848 all_vectors = p;
1849 ++n_vectors;
1850 return p;
1854 DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0,
1855 "Return a newly created vector of length LENGTH, with each element being INIT.\n\
1856 See also the function `vector'.")
1857 (length, init)
1858 register Lisp_Object length, init;
1860 Lisp_Object vector;
1861 register EMACS_INT sizei;
1862 register int index;
1863 register struct Lisp_Vector *p;
1865 CHECK_NATNUM (length, 0);
1866 sizei = XFASTINT (length);
1868 p = allocate_vectorlike (sizei);
1869 p->size = sizei;
1870 for (index = 0; index < sizei; index++)
1871 p->contents[index] = init;
1873 XSETVECTOR (vector, p);
1874 return vector;
1878 DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0,
1879 "Return a newly created char-table, with purpose PURPOSE.\n\
1880 Each element is initialized to INIT, which defaults to nil.\n\
1881 PURPOSE should be a symbol which has a `char-table-extra-slots' property.\n\
1882 The property's value should be an integer between 0 and 10.")
1883 (purpose, init)
1884 register Lisp_Object purpose, init;
1886 Lisp_Object vector;
1887 Lisp_Object n;
1888 CHECK_SYMBOL (purpose, 1);
1889 n = Fget (purpose, Qchar_table_extra_slots);
1890 CHECK_NUMBER (n, 0);
1891 if (XINT (n) < 0 || XINT (n) > 10)
1892 args_out_of_range (n, Qnil);
1893 /* Add 2 to the size for the defalt and parent slots. */
1894 vector = Fmake_vector (make_number (CHAR_TABLE_STANDARD_SLOTS + XINT (n)),
1895 init);
1896 XCHAR_TABLE (vector)->top = Qt;
1897 XCHAR_TABLE (vector)->parent = Qnil;
1898 XCHAR_TABLE (vector)->purpose = purpose;
1899 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
1900 return vector;
1904 /* Return a newly created sub char table with default value DEFALT.
1905 Since a sub char table does not appear as a top level Emacs Lisp
1906 object, we don't need a Lisp interface to make it. */
1908 Lisp_Object
1909 make_sub_char_table (defalt)
1910 Lisp_Object defalt;
1912 Lisp_Object vector
1913 = Fmake_vector (make_number (SUB_CHAR_TABLE_STANDARD_SLOTS), Qnil);
1914 XCHAR_TABLE (vector)->top = Qnil;
1915 XCHAR_TABLE (vector)->defalt = defalt;
1916 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
1917 return vector;
1921 DEFUN ("vector", Fvector, Svector, 0, MANY, 0,
1922 "Return a newly created vector with specified arguments as elements.\n\
1923 Any number of arguments, even zero arguments, are allowed.")
1924 (nargs, args)
1925 register int nargs;
1926 Lisp_Object *args;
1928 register Lisp_Object len, val;
1929 register int index;
1930 register struct Lisp_Vector *p;
1932 XSETFASTINT (len, nargs);
1933 val = Fmake_vector (len, Qnil);
1934 p = XVECTOR (val);
1935 for (index = 0; index < nargs; index++)
1936 p->contents[index] = args[index];
1937 return val;
1941 DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0,
1942 "Create a byte-code object with specified arguments as elements.\n\
1943 The arguments should be the arglist, bytecode-string, constant vector,\n\
1944 stack size, (optional) doc string, and (optional) interactive spec.\n\
1945 The first four arguments are required; at most six have any\n\
1946 significance.")
1947 (nargs, args)
1948 register int nargs;
1949 Lisp_Object *args;
1951 register Lisp_Object len, val;
1952 register int index;
1953 register struct Lisp_Vector *p;
1955 XSETFASTINT (len, nargs);
1956 if (!NILP (Vpurify_flag))
1957 val = make_pure_vector ((EMACS_INT) nargs);
1958 else
1959 val = Fmake_vector (len, Qnil);
1961 if (STRINGP (args[1]) && STRING_MULTIBYTE (args[1]))
1962 /* BYTECODE-STRING must have been produced by Emacs 20.2 or the
1963 earlier because they produced a raw 8-bit string for byte-code
1964 and now such a byte-code string is loaded as multibyte while
1965 raw 8-bit characters converted to multibyte form. Thus, now we
1966 must convert them back to the original unibyte form. */
1967 args[1] = Fstring_as_unibyte (args[1]);
1969 p = XVECTOR (val);
1970 for (index = 0; index < nargs; index++)
1972 if (!NILP (Vpurify_flag))
1973 args[index] = Fpurecopy (args[index]);
1974 p->contents[index] = args[index];
1976 XSETCOMPILED (val, p);
1977 return val;
1982 /***********************************************************************
1983 Symbol Allocation
1984 ***********************************************************************/
1986 /* Each symbol_block is just under 1020 bytes long, since malloc
1987 really allocates in units of powers of two and uses 4 bytes for its
1988 own overhead. */
1990 #define SYMBOL_BLOCK_SIZE \
1991 ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol))
1993 struct symbol_block
1995 struct symbol_block *next;
1996 struct Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE];
1999 /* Current symbol block and index of first unused Lisp_Symbol
2000 structure in it. */
2002 struct symbol_block *symbol_block;
2003 int symbol_block_index;
2005 /* List of free symbols. */
2007 struct Lisp_Symbol *symbol_free_list;
2009 /* Total number of symbol blocks now in use. */
2011 int n_symbol_blocks;
2014 /* Initialize symbol allocation. */
2016 void
2017 init_symbol ()
2019 symbol_block = (struct symbol_block *) lisp_malloc (sizeof *symbol_block,
2020 MEM_TYPE_SYMBOL);
2021 symbol_block->next = 0;
2022 bzero ((char *) symbol_block->symbols, sizeof symbol_block->symbols);
2023 symbol_block_index = 0;
2024 symbol_free_list = 0;
2025 n_symbol_blocks = 1;
2029 DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0,
2030 "Return a newly allocated uninterned symbol whose name is NAME.\n\
2031 Its value and function definition are void, and its property list is nil.")
2032 (name)
2033 Lisp_Object name;
2035 register Lisp_Object val;
2036 register struct Lisp_Symbol *p;
2038 CHECK_STRING (name, 0);
2040 if (symbol_free_list)
2042 XSETSYMBOL (val, symbol_free_list);
2043 symbol_free_list = *(struct Lisp_Symbol **)&symbol_free_list->value;
2045 else
2047 if (symbol_block_index == SYMBOL_BLOCK_SIZE)
2049 struct symbol_block *new;
2050 new = (struct symbol_block *) lisp_malloc (sizeof *new,
2051 MEM_TYPE_SYMBOL);
2052 VALIDATE_LISP_STORAGE (new, sizeof *new);
2053 new->next = symbol_block;
2054 symbol_block = new;
2055 symbol_block_index = 0;
2056 n_symbol_blocks++;
2058 XSETSYMBOL (val, &symbol_block->symbols[symbol_block_index++]);
2061 p = XSYMBOL (val);
2062 p->name = XSTRING (name);
2063 p->obarray = Qnil;
2064 p->plist = Qnil;
2065 p->value = Qunbound;
2066 p->function = Qunbound;
2067 p->next = 0;
2068 consing_since_gc += sizeof (struct Lisp_Symbol);
2069 symbols_consed++;
2070 return val;
2075 /***********************************************************************
2076 Marker (Misc) Allocation
2077 ***********************************************************************/
2079 /* Allocation of markers and other objects that share that structure.
2080 Works like allocation of conses. */
2082 #define MARKER_BLOCK_SIZE \
2083 ((1020 - sizeof (struct marker_block *)) / sizeof (union Lisp_Misc))
2085 struct marker_block
2087 struct marker_block *next;
2088 union Lisp_Misc markers[MARKER_BLOCK_SIZE];
2091 struct marker_block *marker_block;
2092 int marker_block_index;
2094 union Lisp_Misc *marker_free_list;
2096 /* Total number of marker blocks now in use. */
2098 int n_marker_blocks;
2100 void
2101 init_marker ()
2103 marker_block = (struct marker_block *) lisp_malloc (sizeof *marker_block,
2104 MEM_TYPE_MISC);
2105 marker_block->next = 0;
2106 bzero ((char *) marker_block->markers, sizeof marker_block->markers);
2107 marker_block_index = 0;
2108 marker_free_list = 0;
2109 n_marker_blocks = 1;
2112 /* Return a newly allocated Lisp_Misc object, with no substructure. */
2114 Lisp_Object
2115 allocate_misc ()
2117 Lisp_Object val;
2119 if (marker_free_list)
2121 XSETMISC (val, marker_free_list);
2122 marker_free_list = marker_free_list->u_free.chain;
2124 else
2126 if (marker_block_index == MARKER_BLOCK_SIZE)
2128 struct marker_block *new;
2129 new = (struct marker_block *) lisp_malloc (sizeof *new,
2130 MEM_TYPE_MISC);
2131 VALIDATE_LISP_STORAGE (new, sizeof *new);
2132 new->next = marker_block;
2133 marker_block = new;
2134 marker_block_index = 0;
2135 n_marker_blocks++;
2137 XSETMISC (val, &marker_block->markers[marker_block_index++]);
2140 consing_since_gc += sizeof (union Lisp_Misc);
2141 misc_objects_consed++;
2142 return val;
2145 DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
2146 "Return a newly allocated marker which does not point at any place.")
2149 register Lisp_Object val;
2150 register struct Lisp_Marker *p;
2152 val = allocate_misc ();
2153 XMISCTYPE (val) = Lisp_Misc_Marker;
2154 p = XMARKER (val);
2155 p->buffer = 0;
2156 p->bytepos = 0;
2157 p->charpos = 0;
2158 p->chain = Qnil;
2159 p->insertion_type = 0;
2160 return val;
2163 /* Put MARKER back on the free list after using it temporarily. */
2165 void
2166 free_marker (marker)
2167 Lisp_Object marker;
2169 unchain_marker (marker);
2171 XMISC (marker)->u_marker.type = Lisp_Misc_Free;
2172 XMISC (marker)->u_free.chain = marker_free_list;
2173 marker_free_list = XMISC (marker);
2175 total_free_markers++;
2179 /* Return a newly created vector or string with specified arguments as
2180 elements. If all the arguments are characters that can fit
2181 in a string of events, make a string; otherwise, make a vector.
2183 Any number of arguments, even zero arguments, are allowed. */
2185 Lisp_Object
2186 make_event_array (nargs, args)
2187 register int nargs;
2188 Lisp_Object *args;
2190 int i;
2192 for (i = 0; i < nargs; i++)
2193 /* The things that fit in a string
2194 are characters that are in 0...127,
2195 after discarding the meta bit and all the bits above it. */
2196 if (!INTEGERP (args[i])
2197 || (XUINT (args[i]) & ~(-CHAR_META)) >= 0200)
2198 return Fvector (nargs, args);
2200 /* Since the loop exited, we know that all the things in it are
2201 characters, so we can make a string. */
2203 Lisp_Object result;
2205 result = Fmake_string (make_number (nargs), make_number (0));
2206 for (i = 0; i < nargs; i++)
2208 XSTRING (result)->data[i] = XINT (args[i]);
2209 /* Move the meta bit to the right place for a string char. */
2210 if (XINT (args[i]) & CHAR_META)
2211 XSTRING (result)->data[i] |= 0x80;
2214 return result;
2220 /************************************************************************
2221 C Stack Marking
2222 ************************************************************************/
2224 #if GC_MARK_STACK
2227 /* Base address of stack. Set in main. */
2229 Lisp_Object *stack_base;
2231 /* A node in the red-black tree describing allocated memory containing
2232 Lisp data. Each such block is recorded with its start and end
2233 address when it is allocated, and removed from the tree when it
2234 is freed.
2236 A red-black tree is a balanced binary tree with the following
2237 properties:
2239 1. Every node is either red or black.
2240 2. Every leaf is black.
2241 3. If a node is red, then both of its children are black.
2242 4. Every simple path from a node to a descendant leaf contains
2243 the same number of black nodes.
2244 5. The root is always black.
2246 When nodes are inserted into the tree, or deleted from the tree,
2247 the tree is "fixed" so that these properties are always true.
2249 A red-black tree with N internal nodes has height at most 2
2250 log(N+1). Searches, insertions and deletions are done in O(log N).
2251 Please see a text book about data structures for a detailed
2252 description of red-black trees. Any book worth its salt should
2253 describe them. */
2255 struct mem_node
2257 struct mem_node *left, *right, *parent;
2259 /* Start and end of allocated region. */
2260 void *start, *end;
2262 /* Node color. */
2263 enum {MEM_BLACK, MEM_RED} color;
2265 /* Memory type. */
2266 enum mem_type type;
2269 /* Root of the tree describing allocated Lisp memory. */
2271 static struct mem_node *mem_root;
2273 /* Sentinel node of the tree. */
2275 static struct mem_node mem_z;
2276 #define MEM_NIL &mem_z
2279 /* Initialize this part of alloc.c. */
2281 static void
2282 mem_init ()
2284 mem_z.left = mem_z.right = MEM_NIL;
2285 mem_z.parent = NULL;
2286 mem_z.color = MEM_BLACK;
2287 mem_z.start = mem_z.end = NULL;
2288 mem_root = MEM_NIL;
2292 /* Value is a pointer to the mem_node containing START. Value is
2293 MEM_NIL if there is no node in the tree containing START. */
2295 static INLINE struct mem_node *
2296 mem_find (start)
2297 void *start;
2299 struct mem_node *p;
2301 /* Make the search always successful to speed up the loop below. */
2302 mem_z.start = start;
2303 mem_z.end = (char *) start + 1;
2305 p = mem_root;
2306 while (start < p->start || start >= p->end)
2307 p = start < p->start ? p->left : p->right;
2308 return p;
2312 /* Insert a new node into the tree for a block of memory with start
2313 address START, end address END, and type TYPE. Value is a
2314 pointer to the node that was inserted. */
2316 static struct mem_node *
2317 mem_insert (start, end, type)
2318 void *start, *end;
2319 enum mem_type type;
2321 struct mem_node *c, *parent, *x;
2323 /* See where in the tree a node for START belongs. In this
2324 particular application, it shouldn't happen that a node is already
2325 present. For debugging purposes, let's check that. */
2326 c = mem_root;
2327 parent = NULL;
2329 #if GC_MARK_STACK != GC_MAKE_GCPROS_NOOPS
2331 while (c != MEM_NIL)
2333 if (start >= c->start && start < c->end)
2334 abort ();
2335 parent = c;
2336 c = start < c->start ? c->left : c->right;
2339 #else /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
2341 while (c != MEM_NIL)
2343 parent = c;
2344 c = start < c->start ? c->left : c->right;
2347 #endif /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
2349 /* Create a new node. */
2350 x = (struct mem_node *) xmalloc (sizeof *x);
2351 x->start = start;
2352 x->end = end;
2353 x->type = type;
2354 x->parent = parent;
2355 x->left = x->right = MEM_NIL;
2356 x->color = MEM_RED;
2358 /* Insert it as child of PARENT or install it as root. */
2359 if (parent)
2361 if (start < parent->start)
2362 parent->left = x;
2363 else
2364 parent->right = x;
2366 else
2367 mem_root = x;
2369 /* Re-establish red-black tree properties. */
2370 mem_insert_fixup (x);
2371 return x;
2375 /* Re-establish the red-black properties of the tree, and thereby
2376 balance the tree, after node X has been inserted; X is always red. */
2378 static void
2379 mem_insert_fixup (x)
2380 struct mem_node *x;
2382 while (x != mem_root && x->parent->color == MEM_RED)
2384 /* X is red and its parent is red. This is a violation of
2385 red-black tree property #3. */
2387 if (x->parent == x->parent->parent->left)
2389 /* We're on the left side of our grandparent, and Y is our
2390 "uncle". */
2391 struct mem_node *y = x->parent->parent->right;
2393 if (y->color == MEM_RED)
2395 /* Uncle and parent are red but should be black because
2396 X is red. Change the colors accordingly and proceed
2397 with the grandparent. */
2398 x->parent->color = MEM_BLACK;
2399 y->color = MEM_BLACK;
2400 x->parent->parent->color = MEM_RED;
2401 x = x->parent->parent;
2403 else
2405 /* Parent and uncle have different colors; parent is
2406 red, uncle is black. */
2407 if (x == x->parent->right)
2409 x = x->parent;
2410 mem_rotate_left (x);
2413 x->parent->color = MEM_BLACK;
2414 x->parent->parent->color = MEM_RED;
2415 mem_rotate_right (x->parent->parent);
2418 else
2420 /* This is the symmetrical case of above. */
2421 struct mem_node *y = x->parent->parent->left;
2423 if (y->color == MEM_RED)
2425 x->parent->color = MEM_BLACK;
2426 y->color = MEM_BLACK;
2427 x->parent->parent->color = MEM_RED;
2428 x = x->parent->parent;
2430 else
2432 if (x == x->parent->left)
2434 x = x->parent;
2435 mem_rotate_right (x);
2438 x->parent->color = MEM_BLACK;
2439 x->parent->parent->color = MEM_RED;
2440 mem_rotate_left (x->parent->parent);
2445 /* The root may have been changed to red due to the algorithm. Set
2446 it to black so that property #5 is satisfied. */
2447 mem_root->color = MEM_BLACK;
2451 /* (x) (y)
2452 / \ / \
2453 a (y) ===> (x) c
2454 / \ / \
2455 b c a b */
2457 static void
2458 mem_rotate_left (x)
2459 struct mem_node *x;
2461 struct mem_node *y;
2463 /* Turn y's left sub-tree into x's right sub-tree. */
2464 y = x->right;
2465 x->right = y->left;
2466 if (y->left != MEM_NIL)
2467 y->left->parent = x;
2469 /* Y's parent was x's parent. */
2470 if (y != MEM_NIL)
2471 y->parent = x->parent;
2473 /* Get the parent to point to y instead of x. */
2474 if (x->parent)
2476 if (x == x->parent->left)
2477 x->parent->left = y;
2478 else
2479 x->parent->right = y;
2481 else
2482 mem_root = y;
2484 /* Put x on y's left. */
2485 y->left = x;
2486 if (x != MEM_NIL)
2487 x->parent = y;
2491 /* (x) (Y)
2492 / \ / \
2493 (y) c ===> a (x)
2494 / \ / \
2495 a b b c */
2497 static void
2498 mem_rotate_right (x)
2499 struct mem_node *x;
2501 struct mem_node *y = x->left;
2503 x->left = y->right;
2504 if (y->right != MEM_NIL)
2505 y->right->parent = x;
2507 if (y != MEM_NIL)
2508 y->parent = x->parent;
2509 if (x->parent)
2511 if (x == x->parent->right)
2512 x->parent->right = y;
2513 else
2514 x->parent->left = y;
2516 else
2517 mem_root = y;
2519 y->right = x;
2520 if (x != MEM_NIL)
2521 x->parent = y;
2525 /* Delete node Z from the tree. If Z is null or MEM_NIL, do nothing. */
2527 static void
2528 mem_delete (z)
2529 struct mem_node *z;
2531 struct mem_node *x, *y;
2533 if (!z || z == MEM_NIL)
2534 return;
2536 if (z->left == MEM_NIL || z->right == MEM_NIL)
2537 y = z;
2538 else
2540 y = z->right;
2541 while (y->left != MEM_NIL)
2542 y = y->left;
2545 if (y->left != MEM_NIL)
2546 x = y->left;
2547 else
2548 x = y->right;
2550 x->parent = y->parent;
2551 if (y->parent)
2553 if (y == y->parent->left)
2554 y->parent->left = x;
2555 else
2556 y->parent->right = x;
2558 else
2559 mem_root = x;
2561 if (y != z)
2563 z->start = y->start;
2564 z->end = y->end;
2565 z->type = y->type;
2568 if (y->color == MEM_BLACK)
2569 mem_delete_fixup (x);
2570 xfree (y);
2574 /* Re-establish the red-black properties of the tree, after a
2575 deletion. */
2577 static void
2578 mem_delete_fixup (x)
2579 struct mem_node *x;
2581 while (x != mem_root && x->color == MEM_BLACK)
2583 if (x == x->parent->left)
2585 struct mem_node *w = x->parent->right;
2587 if (w->color == MEM_RED)
2589 w->color = MEM_BLACK;
2590 x->parent->color = MEM_RED;
2591 mem_rotate_left (x->parent);
2592 w = x->parent->right;
2595 if (w->left->color == MEM_BLACK && w->right->color == MEM_BLACK)
2597 w->color = MEM_RED;
2598 x = x->parent;
2600 else
2602 if (w->right->color == MEM_BLACK)
2604 w->left->color = MEM_BLACK;
2605 w->color = MEM_RED;
2606 mem_rotate_right (w);
2607 w = x->parent->right;
2609 w->color = x->parent->color;
2610 x->parent->color = MEM_BLACK;
2611 w->right->color = MEM_BLACK;
2612 mem_rotate_left (x->parent);
2613 x = mem_root;
2616 else
2618 struct mem_node *w = x->parent->left;
2620 if (w->color == MEM_RED)
2622 w->color = MEM_BLACK;
2623 x->parent->color = MEM_RED;
2624 mem_rotate_right (x->parent);
2625 w = x->parent->left;
2628 if (w->right->color == MEM_BLACK && w->left->color == MEM_BLACK)
2630 w->color = MEM_RED;
2631 x = x->parent;
2633 else
2635 if (w->left->color == MEM_BLACK)
2637 w->right->color = MEM_BLACK;
2638 w->color = MEM_RED;
2639 mem_rotate_left (w);
2640 w = x->parent->left;
2643 w->color = x->parent->color;
2644 x->parent->color = MEM_BLACK;
2645 w->left->color = MEM_BLACK;
2646 mem_rotate_right (x->parent);
2647 x = mem_root;
2652 x->color = MEM_BLACK;
2656 /* Value is non-zero if P is a pointer to a live Lisp string on
2657 the heap. M is a pointer to the mem_block for P. */
2659 static INLINE int
2660 live_string_p (m, p)
2661 struct mem_node *m;
2662 void *p;
2664 if (m->type == MEM_TYPE_STRING)
2666 struct string_block *b = (struct string_block *) m->start;
2667 int offset = (char *) p - (char *) &b->strings[0];
2669 /* P must point to the start of a Lisp_String structure, and it
2670 must not be on the free-list. */
2671 return (offset % sizeof b->strings[0] == 0
2672 && ((struct Lisp_String *) p)->data != NULL);
2674 else
2675 return 0;
2679 /* Value is non-zero if P is a pointer to a live Lisp cons on
2680 the heap. M is a pointer to the mem_block for P. */
2682 static INLINE int
2683 live_cons_p (m, p)
2684 struct mem_node *m;
2685 void *p;
2687 if (m->type == MEM_TYPE_CONS)
2689 struct cons_block *b = (struct cons_block *) m->start;
2690 int offset = (char *) p - (char *) &b->conses[0];
2692 /* P must point to the start of a Lisp_Cons, not be
2693 one of the unused cells in the current cons block,
2694 and not be on the free-list. */
2695 return (offset % sizeof b->conses[0] == 0
2696 && (b != cons_block
2697 || offset / sizeof b->conses[0] < cons_block_index)
2698 && !EQ (((struct Lisp_Cons *) p)->car, Vdead));
2700 else
2701 return 0;
2705 /* Value is non-zero if P is a pointer to a live Lisp symbol on
2706 the heap. M is a pointer to the mem_block for P. */
2708 static INLINE int
2709 live_symbol_p (m, p)
2710 struct mem_node *m;
2711 void *p;
2713 if (m->type == MEM_TYPE_SYMBOL)
2715 struct symbol_block *b = (struct symbol_block *) m->start;
2716 int offset = (char *) p - (char *) &b->symbols[0];
2718 /* P must point to the start of a Lisp_Symbol, not be
2719 one of the unused cells in the current symbol block,
2720 and not be on the free-list. */
2721 return (offset % sizeof b->symbols[0] == 0
2722 && (b != symbol_block
2723 || offset / sizeof b->symbols[0] < symbol_block_index)
2724 && !EQ (((struct Lisp_Symbol *) p)->function, Vdead));
2726 else
2727 return 0;
2731 /* Value is non-zero if P is a pointer to a live Lisp float on
2732 the heap. M is a pointer to the mem_block for P. */
2734 static INLINE int
2735 live_float_p (m, p)
2736 struct mem_node *m;
2737 void *p;
2739 if (m->type == MEM_TYPE_FLOAT)
2741 struct float_block *b = (struct float_block *) m->start;
2742 int offset = (char *) p - (char *) &b->floats[0];
2744 /* P must point to the start of a Lisp_Float, not be
2745 one of the unused cells in the current float block,
2746 and not be on the free-list. */
2747 return (offset % sizeof b->floats[0] == 0
2748 && (b != float_block
2749 || offset / sizeof b->floats[0] < float_block_index)
2750 && !EQ (((struct Lisp_Float *) p)->type, Vdead));
2752 else
2753 return 0;
2757 /* Value is non-zero if P is a pointer to a live Lisp Misc on
2758 the heap. M is a pointer to the mem_block for P. */
2760 static INLINE int
2761 live_misc_p (m, p)
2762 struct mem_node *m;
2763 void *p;
2765 if (m->type == MEM_TYPE_MISC)
2767 struct marker_block *b = (struct marker_block *) m->start;
2768 int offset = (char *) p - (char *) &b->markers[0];
2770 /* P must point to the start of a Lisp_Misc, not be
2771 one of the unused cells in the current misc block,
2772 and not be on the free-list. */
2773 return (offset % sizeof b->markers[0] == 0
2774 && (b != marker_block
2775 || offset / sizeof b->markers[0] < marker_block_index)
2776 && ((union Lisp_Misc *) p)->u_marker.type != Lisp_Misc_Free);
2778 else
2779 return 0;
2783 /* Value is non-zero if P is a pointer to a live vector-like object.
2784 M is a pointer to the mem_block for P. */
2786 static INLINE int
2787 live_vector_p (m, p)
2788 struct mem_node *m;
2789 void *p;
2791 return m->type == MEM_TYPE_VECTOR && p == m->start;
2795 /* Value is non-zero of P is a pointer to a live buffer. M is a
2796 pointer to the mem_block for P. */
2798 static INLINE int
2799 live_buffer_p (m, p)
2800 struct mem_node *m;
2801 void *p;
2803 /* P must point to the start of the block, and the buffer
2804 must not have been killed. */
2805 return (m->type == MEM_TYPE_BUFFER
2806 && p == m->start
2807 && !NILP (((struct buffer *) p)->name));
2811 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
2813 /* Array of objects that are kept alive because the C stack contains
2814 a pattern that looks like a reference to them . */
2816 #define MAX_ZOMBIES 10
2817 static Lisp_Object zombies[MAX_ZOMBIES];
2819 /* Number of zombie objects. */
2821 static int nzombies;
2823 /* Number of garbage collections. */
2825 static int ngcs;
2827 /* Average percentage of zombies per collection. */
2829 static double avg_zombies;
2831 /* Max. number of live and zombie objects. */
2833 static int max_live, max_zombies;
2835 /* Average number of live objects per GC. */
2837 static double avg_live;
2839 DEFUN ("gc-status", Fgc_status, Sgc_status, 0, 0, "",
2840 "Show information about live and zombie objects.")
2843 Lisp_Object args[7];
2844 args[0] = build_string ("%d GCs, avg live/zombies = %.2f/%.2f (%f%%), max %d/%d");
2845 args[1] = make_number (ngcs);
2846 args[2] = make_float (avg_live);
2847 args[3] = make_float (avg_zombies);
2848 args[4] = make_float (avg_zombies / avg_live / 100);
2849 args[5] = make_number (max_live);
2850 args[6] = make_number (max_zombies);
2851 return Fmessage (7, args);
2854 #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
2857 /* Mark OBJ if we can prove it's a Lisp_Object. */
2859 static INLINE void
2860 mark_maybe_object (obj)
2861 Lisp_Object obj;
2863 void *po = (void *) XPNTR (obj);
2864 struct mem_node *m = mem_find (po);
2866 if (m != MEM_NIL)
2868 int mark_p = 0;
2870 switch (XGCTYPE (obj))
2872 case Lisp_String:
2873 mark_p = (live_string_p (m, po)
2874 && !STRING_MARKED_P ((struct Lisp_String *) po));
2875 break;
2877 case Lisp_Cons:
2878 mark_p = (live_cons_p (m, po)
2879 && !XMARKBIT (XCONS (obj)->car));
2880 break;
2882 case Lisp_Symbol:
2883 mark_p = (live_symbol_p (m, po)
2884 && !XMARKBIT (XSYMBOL (obj)->plist));
2885 break;
2887 case Lisp_Float:
2888 mark_p = (live_float_p (m, po)
2889 && !XMARKBIT (XFLOAT (obj)->type));
2890 break;
2892 case Lisp_Vectorlike:
2893 /* Note: can't check GC_BUFFERP before we know it's a
2894 buffer because checking that dereferences the pointer
2895 PO which might point anywhere. */
2896 if (live_vector_p (m, po))
2897 mark_p = (!GC_SUBRP (obj)
2898 && !(XVECTOR (obj)->size & ARRAY_MARK_FLAG));
2899 else if (live_buffer_p (m, po))
2900 mark_p = GC_BUFFERP (obj) && !XMARKBIT (XBUFFER (obj)->name);
2901 break;
2903 case Lisp_Misc:
2904 if (live_misc_p (m, po))
2906 switch (XMISCTYPE (obj))
2908 case Lisp_Misc_Marker:
2909 mark_p = !XMARKBIT (XMARKER (obj)->chain);
2910 break;
2912 case Lisp_Misc_Buffer_Local_Value:
2913 case Lisp_Misc_Some_Buffer_Local_Value:
2914 mark_p = !XMARKBIT (XBUFFER_LOCAL_VALUE (obj)->realvalue);
2915 break;
2917 case Lisp_Misc_Overlay:
2918 mark_p = !XMARKBIT (XOVERLAY (obj)->plist);
2919 break;
2922 break;
2925 if (mark_p)
2927 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
2928 if (nzombies < MAX_ZOMBIES)
2929 zombies[nzombies] = *p;
2930 ++nzombies;
2931 #endif
2932 mark_object (&obj);
2937 /* Mark Lisp objects in the address range START..END. */
2939 static void
2940 mark_memory (start, end)
2941 void *start, *end;
2943 Lisp_Object *p;
2945 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
2946 nzombies = 0;
2947 #endif
2949 /* Make START the pointer to the start of the memory region,
2950 if it isn't already. */
2951 if (end < start)
2953 void *tem = start;
2954 start = end;
2955 end = tem;
2958 for (p = (Lisp_Object *) start; (void *) p < end; ++p)
2959 mark_maybe_object (*p);
2963 #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
2965 static int setjmp_tested_p, longjmps_done;
2967 #define SETJMP_WILL_LIKELY_WORK "\
2969 Emacs garbage collector has been changed to use conservative stack\n\
2970 marking. Emacs has determined that the method it uses to do the\n\
2971 marking will likely work on your system, but this isn't sure.\n\
2973 If you are a system-programmer, or can get the help of a local wizard\n\
2974 who is, please take a look at the function mark_stack in alloc.c, and\n\
2975 verify that the methods used are appropriate for your system.\n\
2977 Please mail the result to <gerd@gnu.org>.\n\
2980 #define SETJMP_WILL_NOT_WORK "\
2982 Emacs garbage collector has been changed to use conservative stack\n\
2983 marking. Emacs has determined that the default method it uses to do the\n\
2984 marking will not work on your system. We will need a system-dependent\n\
2985 solution for your system.\n\
2987 Please take a look at the function mark_stack in alloc.c, and\n\
2988 try to find a way to make it work on your system.\n\
2989 Please mail the result to <gerd@gnu.org>.\n\
2993 /* Perform a quick check if it looks like setjmp saves registers in a
2994 jmp_buf. Print a message to stderr saying so. When this test
2995 succeeds, this is _not_ a proof that setjmp is sufficient for
2996 conservative stack marking. Only the sources or a disassembly
2997 can prove that. */
2999 static void
3000 test_setjmp ()
3002 char buf[10];
3003 register int x;
3004 jmp_buf jbuf;
3005 int result = 0;
3007 /* Arrange for X to be put in a register. */
3008 sprintf (buf, "1");
3009 x = strlen (buf);
3010 x = 2 * x - 1;
3012 setjmp (jbuf);
3013 if (longjmps_done == 1)
3015 /* Came here after the longjmp at the end of the function.
3017 If x == 1, the longjmp has restored the register to its
3018 value before the setjmp, and we can hope that setjmp
3019 saves all such registers in the jmp_buf, although that
3020 isn't sure.
3022 For other values of X, either something really strange is
3023 taking place, or the setjmp just didn't save the register. */
3025 if (x == 1)
3026 fprintf (stderr, SETJMP_WILL_LIKELY_WORK);
3027 else
3029 fprintf (stderr, SETJMP_WILL_NOT_WORK);
3030 exit (1);
3034 ++longjmps_done;
3035 x = 2;
3036 if (longjmps_done == 1)
3037 longjmp (jbuf, 1);
3040 #endif /* not GC_SAVE_REGISTERS_ON_STACK && not GC_SETJMP_WORKS */
3043 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
3045 /* Abort if anything GCPRO'd doesn't survive the GC. */
3047 static void
3048 check_gcpros ()
3050 struct gcpro *p;
3051 int i;
3053 for (p = gcprolist; p; p = p->next)
3054 for (i = 0; i < p->nvars; ++i)
3055 if (!survives_gc_p (p->var[i]))
3056 abort ();
3059 #elif GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3061 static void
3062 dump_zombies ()
3064 int i;
3066 fprintf (stderr, "\nZombies kept alive = %d:\n", nzombies);
3067 for (i = 0; i < min (MAX_ZOMBIES, nzombies); ++i)
3069 fprintf (stderr, " %d = ", i);
3070 debug_print (zombies[i]);
3074 #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
3077 /* Mark live Lisp objects on the C stack.
3079 There are several system-dependent problems to consider when
3080 porting this to new architectures:
3082 Processor Registers
3084 We have to mark Lisp objects in CPU registers that can hold local
3085 variables or are used to pass parameters.
3087 If GC_SAVE_REGISTERS_ON_STACK is defined, it should expand to
3088 something that either saves relevant registers on the stack, or
3089 calls mark_maybe_object passing it each register's contents.
3091 If GC_SAVE_REGISTERS_ON_STACK is not defined, the current
3092 implementation assumes that calling setjmp saves registers we need
3093 to see in a jmp_buf which itself lies on the stack. This doesn't
3094 have to be true! It must be verified for each system, possibly
3095 by taking a look at the source code of setjmp.
3097 Stack Layout
3099 Architectures differ in the way their processor stack is organized.
3100 For example, the stack might look like this
3102 +----------------+
3103 | Lisp_Object | size = 4
3104 +----------------+
3105 | something else | size = 2
3106 +----------------+
3107 | Lisp_Object | size = 4
3108 +----------------+
3109 | ... |
3111 In such a case, not every Lisp_Object will be aligned equally. To
3112 find all Lisp_Object on the stack it won't be sufficient to walk
3113 the stack in steps of 4 bytes. Instead, two passes will be
3114 necessary, one starting at the start of the stack, and a second
3115 pass starting at the start of the stack + 2. Likewise, if the
3116 minimal alignment of Lisp_Objects on the stack is 1, four passes
3117 would be necessary, each one starting with one byte more offset
3118 from the stack start.
3120 The current code assumes by default that Lisp_Objects are aligned
3121 equally on the stack. */
3123 static void
3124 mark_stack ()
3126 jmp_buf j;
3127 int stack_grows_down_p = (char *) &j > (char *) stack_base;
3128 void *end;
3130 /* This trick flushes the register windows so that all the state of
3131 the process is contained in the stack. */
3132 #ifdef sparc
3133 asm ("ta 3");
3134 #endif
3136 /* Save registers that we need to see on the stack. We need to see
3137 registers used to hold register variables and registers used to
3138 pass parameters. */
3139 #ifdef GC_SAVE_REGISTERS_ON_STACK
3140 GC_SAVE_REGISTERS_ON_STACK (end);
3141 #else /* not GC_SAVE_REGISTERS_ON_STACK */
3143 #ifndef GC_SETJMP_WORKS /* If it hasn't been checked yet that
3144 setjmp will definitely work, test it
3145 and print a message with the result
3146 of the test. */
3147 if (!setjmp_tested_p)
3149 setjmp_tested_p = 1;
3150 test_setjmp ();
3152 #endif /* GC_SETJMP_WORKS */
3154 setjmp (j);
3155 end = stack_grows_down_p ? (char *) &j + sizeof j : (char *) &j;
3156 #endif /* not GC_SAVE_REGISTERS_ON_STACK */
3158 /* This assumes that the stack is a contiguous region in memory. If
3159 that's not the case, something has to be done here to iterate
3160 over the stack segments. */
3161 #if GC_LISP_OBJECT_ALIGNMENT == 1
3162 mark_memory (stack_base, end);
3163 mark_memory ((char *) stack_base + 1, end);
3164 mark_memory ((char *) stack_base + 2, end);
3165 mark_memory ((char *) stack_base + 3, end);
3166 #elif GC_LISP_OBJECT_ALIGNMENT == 2
3167 mark_memory (stack_base, end);
3168 mark_memory ((char *) stack_base + 2, end);
3169 #else
3170 mark_memory (stack_base, end);
3171 #endif
3173 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
3174 check_gcpros ();
3175 #endif
3179 #endif /* GC_MARK_STACK != 0 */
3183 /***********************************************************************
3184 Pure Storage Management
3185 ***********************************************************************/
3187 /* Return a string allocated in pure space. DATA is a buffer holding
3188 NCHARS characters, and NBYTES bytes of string data. MULTIBYTE
3189 non-zero means make the result string multibyte.
3191 Must get an error if pure storage is full, since if it cannot hold
3192 a large string it may be able to hold conses that point to that
3193 string; then the string is not protected from gc. */
3195 Lisp_Object
3196 make_pure_string (data, nchars, nbytes, multibyte)
3197 char *data;
3198 int nchars, nbytes;
3199 int multibyte;
3201 Lisp_Object string;
3202 struct Lisp_String *s;
3203 int string_size, data_size;
3205 #define PAD(SZ) (((SZ) + sizeof (EMACS_INT) - 1) & ~(sizeof (EMACS_INT) - 1))
3207 string_size = PAD (sizeof (struct Lisp_String));
3208 data_size = PAD (nbytes + 1);
3210 #undef PAD
3212 if (pureptr + string_size + data_size > PURESIZE)
3213 error ("Pure Lisp storage exhausted");
3215 s = (struct Lisp_String *) (PUREBEG + pureptr);
3216 pureptr += string_size;
3217 s->data = (unsigned char *) (PUREBEG + pureptr);
3218 pureptr += data_size;
3220 s->size = nchars;
3221 s->size_byte = multibyte ? nbytes : -1;
3222 bcopy (data, s->data, nbytes);
3223 s->data[nbytes] = '\0';
3224 s->intervals = NULL_INTERVAL;
3226 XSETSTRING (string, s);
3227 return string;
3231 /* Return a cons allocated from pure space. Give it pure copies
3232 of CAR as car and CDR as cdr. */
3234 Lisp_Object
3235 pure_cons (car, cdr)
3236 Lisp_Object car, cdr;
3238 register Lisp_Object new;
3240 if (pureptr + sizeof (struct Lisp_Cons) > PURESIZE)
3241 error ("Pure Lisp storage exhausted");
3242 XSETCONS (new, PUREBEG + pureptr);
3243 pureptr += sizeof (struct Lisp_Cons);
3244 XCAR (new) = Fpurecopy (car);
3245 XCDR (new) = Fpurecopy (cdr);
3246 return new;
3250 /* Value is a float object with value NUM allocated from pure space. */
3252 Lisp_Object
3253 make_pure_float (num)
3254 double num;
3256 register Lisp_Object new;
3258 /* Make sure that PUREBEG + pureptr is aligned on at least a sizeof
3259 (double) boundary. Some architectures (like the sparc) require
3260 this, and I suspect that floats are rare enough that it's no
3261 tragedy for those that do. */
3263 int alignment;
3264 char *p = PUREBEG + pureptr;
3266 #ifdef __GNUC__
3267 #if __GNUC__ >= 2
3268 alignment = __alignof (struct Lisp_Float);
3269 #else
3270 alignment = sizeof (struct Lisp_Float);
3271 #endif
3272 #else
3273 alignment = sizeof (struct Lisp_Float);
3274 #endif
3275 p = (char *) (((unsigned long) p + alignment - 1) & - alignment);
3276 pureptr = p - PUREBEG;
3279 if (pureptr + sizeof (struct Lisp_Float) > PURESIZE)
3280 error ("Pure Lisp storage exhausted");
3281 XSETFLOAT (new, PUREBEG + pureptr);
3282 pureptr += sizeof (struct Lisp_Float);
3283 XFLOAT_DATA (new) = num;
3284 XSETFASTINT (XFLOAT (new)->type, 0); /* bug chasing -wsr */
3285 return new;
3289 /* Return a vector with room for LEN Lisp_Objects allocated from
3290 pure space. */
3292 Lisp_Object
3293 make_pure_vector (len)
3294 EMACS_INT len;
3296 register Lisp_Object new;
3297 register EMACS_INT size = (sizeof (struct Lisp_Vector)
3298 + (len - 1) * sizeof (Lisp_Object));
3300 if (pureptr + size > PURESIZE)
3301 error ("Pure Lisp storage exhausted");
3303 XSETVECTOR (new, PUREBEG + pureptr);
3304 pureptr += size;
3305 XVECTOR (new)->size = len;
3306 return new;
3310 DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
3311 "Make a copy of OBJECT in pure storage.\n\
3312 Recursively copies contents of vectors and cons cells.\n\
3313 Does not copy symbols. Copies strings without text properties.")
3314 (obj)
3315 register Lisp_Object obj;
3317 if (NILP (Vpurify_flag))
3318 return obj;
3320 if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
3321 && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
3322 return obj;
3324 if (CONSP (obj))
3325 return pure_cons (XCAR (obj), XCDR (obj));
3326 else if (FLOATP (obj))
3327 return make_pure_float (XFLOAT_DATA (obj));
3328 else if (STRINGP (obj))
3329 return make_pure_string (XSTRING (obj)->data, XSTRING (obj)->size,
3330 STRING_BYTES (XSTRING (obj)),
3331 STRING_MULTIBYTE (obj));
3332 else if (COMPILEDP (obj) || VECTORP (obj))
3334 register struct Lisp_Vector *vec;
3335 register int i, size;
3337 size = XVECTOR (obj)->size;
3338 if (size & PSEUDOVECTOR_FLAG)
3339 size &= PSEUDOVECTOR_SIZE_MASK;
3340 vec = XVECTOR (make_pure_vector ((EMACS_INT) size));
3341 for (i = 0; i < size; i++)
3342 vec->contents[i] = Fpurecopy (XVECTOR (obj)->contents[i]);
3343 if (COMPILEDP (obj))
3344 XSETCOMPILED (obj, vec);
3345 else
3346 XSETVECTOR (obj, vec);
3347 return obj;
3349 else if (MARKERP (obj))
3350 error ("Attempt to copy a marker to pure storage");
3351 else
3352 return obj;
3357 /***********************************************************************
3358 Protection from GC
3359 ***********************************************************************/
3361 /* Recording what needs to be marked for gc. */
3363 struct gcpro *gcprolist;
3365 /* Addresses of staticpro'd variables. */
3367 #define NSTATICS 1024
3368 Lisp_Object *staticvec[NSTATICS] = {0};
3370 /* Index of next unused slot in staticvec. */
3372 int staticidx = 0;
3375 /* Put an entry in staticvec, pointing at the variable with address
3376 VARADDRESS. */
3378 void
3379 staticpro (varaddress)
3380 Lisp_Object *varaddress;
3382 staticvec[staticidx++] = varaddress;
3383 if (staticidx >= NSTATICS)
3384 abort ();
3387 struct catchtag
3389 Lisp_Object tag;
3390 Lisp_Object val;
3391 struct catchtag *next;
3394 struct backtrace
3396 struct backtrace *next;
3397 Lisp_Object *function;
3398 Lisp_Object *args; /* Points to vector of args. */
3399 int nargs; /* Length of vector. */
3400 /* If nargs is UNEVALLED, args points to slot holding list of
3401 unevalled args. */
3402 char evalargs;
3407 /***********************************************************************
3408 Protection from GC
3409 ***********************************************************************/
3411 /* Temporarily prevent garbage collection. */
3414 inhibit_garbage_collection ()
3416 int count = specpdl_ptr - specpdl;
3417 Lisp_Object number;
3418 int nbits = min (VALBITS, BITS_PER_INT);
3420 XSETINT (number, ((EMACS_INT) 1 << (nbits - 1)) - 1);
3422 specbind (Qgc_cons_threshold, number);
3424 return count;
3428 DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
3429 "Reclaim storage for Lisp objects no longer needed.\n\
3430 Returns info on amount of space in use:\n\
3431 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)\n\
3432 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS\n\
3433 (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS\n\
3434 (USED-STRINGS . FREE-STRINGS))\n\
3435 Garbage collection happens automatically if you cons more than\n\
3436 `gc-cons-threshold' bytes of Lisp data since previous garbage collection.")
3439 register struct gcpro *tail;
3440 register struct specbinding *bind;
3441 struct catchtag *catch;
3442 struct handler *handler;
3443 register struct backtrace *backlist;
3444 char stack_top_variable;
3445 register int i;
3446 int message_p;
3447 Lisp_Object total[7];
3449 /* In case user calls debug_print during GC,
3450 don't let that cause a recursive GC. */
3451 consing_since_gc = 0;
3453 /* Save what's currently displayed in the echo area. */
3454 message_p = push_message ();
3456 /* Save a copy of the contents of the stack, for debugging. */
3457 #if MAX_SAVE_STACK > 0
3458 if (NILP (Vpurify_flag))
3460 i = &stack_top_variable - stack_bottom;
3461 if (i < 0) i = -i;
3462 if (i < MAX_SAVE_STACK)
3464 if (stack_copy == 0)
3465 stack_copy = (char *) xmalloc (stack_copy_size = i);
3466 else if (stack_copy_size < i)
3467 stack_copy = (char *) xrealloc (stack_copy, (stack_copy_size = i));
3468 if (stack_copy)
3470 if ((EMACS_INT) (&stack_top_variable - stack_bottom) > 0)
3471 bcopy (stack_bottom, stack_copy, i);
3472 else
3473 bcopy (&stack_top_variable, stack_copy, i);
3477 #endif /* MAX_SAVE_STACK > 0 */
3479 if (garbage_collection_messages)
3480 message1_nolog ("Garbage collecting...");
3482 BLOCK_INPUT;
3484 shrink_regexp_cache ();
3486 /* Don't keep undo information around forever. */
3488 register struct buffer *nextb = all_buffers;
3490 while (nextb)
3492 /* If a buffer's undo list is Qt, that means that undo is
3493 turned off in that buffer. Calling truncate_undo_list on
3494 Qt tends to return NULL, which effectively turns undo back on.
3495 So don't call truncate_undo_list if undo_list is Qt. */
3496 if (! EQ (nextb->undo_list, Qt))
3497 nextb->undo_list
3498 = truncate_undo_list (nextb->undo_list, undo_limit,
3499 undo_strong_limit);
3500 nextb = nextb->next;
3504 gc_in_progress = 1;
3506 /* clear_marks (); */
3508 /* Mark all the special slots that serve as the roots of accessibility.
3510 Usually the special slots to mark are contained in particular structures.
3511 Then we know no slot is marked twice because the structures don't overlap.
3512 In some cases, the structures point to the slots to be marked.
3513 For these, we use MARKBIT to avoid double marking of the slot. */
3515 for (i = 0; i < staticidx; i++)
3516 mark_object (staticvec[i]);
3518 #if (GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS \
3519 || GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS)
3520 mark_stack ();
3521 #else
3522 for (tail = gcprolist; tail; tail = tail->next)
3523 for (i = 0; i < tail->nvars; i++)
3524 if (!XMARKBIT (tail->var[i]))
3526 mark_object (&tail->var[i]);
3527 XMARK (tail->var[i]);
3529 #endif
3531 mark_byte_stack ();
3532 for (bind = specpdl; bind != specpdl_ptr; bind++)
3534 mark_object (&bind->symbol);
3535 mark_object (&bind->old_value);
3537 for (catch = catchlist; catch; catch = catch->next)
3539 mark_object (&catch->tag);
3540 mark_object (&catch->val);
3542 for (handler = handlerlist; handler; handler = handler->next)
3544 mark_object (&handler->handler);
3545 mark_object (&handler->var);
3547 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3549 if (!XMARKBIT (*backlist->function))
3551 mark_object (backlist->function);
3552 XMARK (*backlist->function);
3554 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
3555 i = 0;
3556 else
3557 i = backlist->nargs - 1;
3558 for (; i >= 0; i--)
3559 if (!XMARKBIT (backlist->args[i]))
3561 mark_object (&backlist->args[i]);
3562 XMARK (backlist->args[i]);
3565 mark_kboards ();
3567 /* Look thru every buffer's undo list
3568 for elements that update markers that were not marked,
3569 and delete them. */
3571 register struct buffer *nextb = all_buffers;
3573 while (nextb)
3575 /* If a buffer's undo list is Qt, that means that undo is
3576 turned off in that buffer. Calling truncate_undo_list on
3577 Qt tends to return NULL, which effectively turns undo back on.
3578 So don't call truncate_undo_list if undo_list is Qt. */
3579 if (! EQ (nextb->undo_list, Qt))
3581 Lisp_Object tail, prev;
3582 tail = nextb->undo_list;
3583 prev = Qnil;
3584 while (CONSP (tail))
3586 if (GC_CONSP (XCAR (tail))
3587 && GC_MARKERP (XCAR (XCAR (tail)))
3588 && ! XMARKBIT (XMARKER (XCAR (XCAR (tail)))->chain))
3590 if (NILP (prev))
3591 nextb->undo_list = tail = XCDR (tail);
3592 else
3593 tail = XCDR (prev) = XCDR (tail);
3595 else
3597 prev = tail;
3598 tail = XCDR (tail);
3603 nextb = nextb->next;
3607 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3608 mark_stack ();
3609 #endif
3611 gc_sweep ();
3613 /* Clear the mark bits that we set in certain root slots. */
3615 #if (GC_MARK_STACK == GC_USE_GCPROS_AS_BEFORE \
3616 || GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES)
3617 for (tail = gcprolist; tail; tail = tail->next)
3618 for (i = 0; i < tail->nvars; i++)
3619 XUNMARK (tail->var[i]);
3620 #endif
3622 unmark_byte_stack ();
3623 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3625 XUNMARK (*backlist->function);
3626 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
3627 i = 0;
3628 else
3629 i = backlist->nargs - 1;
3630 for (; i >= 0; i--)
3631 XUNMARK (backlist->args[i]);
3633 XUNMARK (buffer_defaults.name);
3634 XUNMARK (buffer_local_symbols.name);
3636 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES && 0
3637 dump_zombies ();
3638 #endif
3640 UNBLOCK_INPUT;
3642 /* clear_marks (); */
3643 gc_in_progress = 0;
3645 consing_since_gc = 0;
3646 if (gc_cons_threshold < 10000)
3647 gc_cons_threshold = 10000;
3649 if (garbage_collection_messages)
3651 if (message_p || minibuf_level > 0)
3652 restore_message ();
3653 else
3654 message1_nolog ("Garbage collecting...done");
3657 pop_message ();
3659 total[0] = Fcons (make_number (total_conses),
3660 make_number (total_free_conses));
3661 total[1] = Fcons (make_number (total_symbols),
3662 make_number (total_free_symbols));
3663 total[2] = Fcons (make_number (total_markers),
3664 make_number (total_free_markers));
3665 total[3] = Fcons (make_number (total_string_size),
3666 make_number (total_vector_size));
3667 total[4] = Fcons (make_number (total_floats),
3668 make_number (total_free_floats));
3669 total[5] = Fcons (make_number (total_intervals),
3670 make_number (total_free_intervals));
3671 total[6] = Fcons (make_number (total_strings),
3672 make_number (total_free_strings));
3674 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3676 /* Compute average percentage of zombies. */
3677 double nlive = 0;
3679 for (i = 0; i < 7; ++i)
3680 nlive += XFASTINT (XCAR (total[i]));
3682 avg_live = (avg_live * ngcs + nlive) / (ngcs + 1);
3683 max_live = max (nlive, max_live);
3684 avg_zombies = (avg_zombies * ngcs + nzombies) / (ngcs + 1);
3685 max_zombies = max (nzombies, max_zombies);
3686 ++ngcs;
3688 #endif
3690 return Flist (7, total);
3694 /* Mark Lisp objects in glyph matrix MATRIX. Currently the
3695 only interesting objects referenced from glyphs are strings. */
3697 static void
3698 mark_glyph_matrix (matrix)
3699 struct glyph_matrix *matrix;
3701 struct glyph_row *row = matrix->rows;
3702 struct glyph_row *end = row + matrix->nrows;
3704 for (; row < end; ++row)
3705 if (row->enabled_p)
3707 int area;
3708 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
3710 struct glyph *glyph = row->glyphs[area];
3711 struct glyph *end_glyph = glyph + row->used[area];
3713 for (; glyph < end_glyph; ++glyph)
3714 if (GC_STRINGP (glyph->object)
3715 && !STRING_MARKED_P (XSTRING (glyph->object)))
3716 mark_object (&glyph->object);
3722 /* Mark Lisp faces in the face cache C. */
3724 static void
3725 mark_face_cache (c)
3726 struct face_cache *c;
3728 if (c)
3730 int i, j;
3731 for (i = 0; i < c->used; ++i)
3733 struct face *face = FACE_FROM_ID (c->f, i);
3735 if (face)
3737 for (j = 0; j < LFACE_VECTOR_SIZE; ++j)
3738 mark_object (&face->lface[j]);
3745 #ifdef HAVE_WINDOW_SYSTEM
3747 /* Mark Lisp objects in image IMG. */
3749 static void
3750 mark_image (img)
3751 struct image *img;
3753 mark_object (&img->spec);
3755 if (!NILP (img->data.lisp_val))
3756 mark_object (&img->data.lisp_val);
3760 /* Mark Lisp objects in image cache of frame F. It's done this way so
3761 that we don't have to include xterm.h here. */
3763 static void
3764 mark_image_cache (f)
3765 struct frame *f;
3767 forall_images_in_image_cache (f, mark_image);
3770 #endif /* HAVE_X_WINDOWS */
3774 /* Mark reference to a Lisp_Object.
3775 If the object referred to has not been seen yet, recursively mark
3776 all the references contained in it. */
3778 #define LAST_MARKED_SIZE 500
3779 Lisp_Object *last_marked[LAST_MARKED_SIZE];
3780 int last_marked_index;
3782 void
3783 mark_object (argptr)
3784 Lisp_Object *argptr;
3786 Lisp_Object *objptr = argptr;
3787 register Lisp_Object obj;
3788 #ifdef GC_CHECK_MARKED_OBJECTS
3789 void *po;
3790 struct mem_node *m;
3791 #endif
3793 loop:
3794 obj = *objptr;
3795 loop2:
3796 XUNMARK (obj);
3798 if (PURE_POINTER_P ((PNTR_COMPARISON_TYPE) XPNTR (obj)))
3799 return;
3801 last_marked[last_marked_index++] = objptr;
3802 if (last_marked_index == LAST_MARKED_SIZE)
3803 last_marked_index = 0;
3805 /* Perform some sanity checks on the objects marked here. Abort if
3806 we encounter an object we know is bogus. This increases GC time
3807 by ~80%, and requires compilation with GC_MARK_STACK != 0. */
3808 #ifdef GC_CHECK_MARKED_OBJECTS
3810 po = (void *) XPNTR (obj);
3812 /* Check that the object pointed to by PO is known to be a Lisp
3813 structure allocated from the heap. */
3814 #define CHECK_ALLOCATED() \
3815 do { \
3816 m = mem_find (po); \
3817 if (m == MEM_NIL) \
3818 abort (); \
3819 } while (0)
3821 /* Check that the object pointed to by PO is live, using predicate
3822 function LIVEP. */
3823 #define CHECK_LIVE(LIVEP) \
3824 do { \
3825 if (!LIVEP (m, po)) \
3826 abort (); \
3827 } while (0)
3829 /* Check both of the above conditions. */
3830 #define CHECK_ALLOCATED_AND_LIVE(LIVEP) \
3831 do { \
3832 CHECK_ALLOCATED (); \
3833 CHECK_LIVE (LIVEP); \
3834 } while (0) \
3836 #else /* not GC_CHECK_MARKED_OBJECTS */
3838 #define CHECK_ALLOCATED() (void) 0
3839 #define CHECK_LIVE(LIVEP) (void) 0
3840 #define CHECK_ALLOCATED_AND_LIVE(LIVEP) (void) 0
3842 #endif /* not GC_CHECK_MARKED_OBJECTS */
3844 switch (SWITCH_ENUM_CAST (XGCTYPE (obj)))
3846 case Lisp_String:
3848 register struct Lisp_String *ptr = XSTRING (obj);
3849 CHECK_ALLOCATED_AND_LIVE (live_string_p);
3850 MARK_INTERVAL_TREE (ptr->intervals);
3851 MARK_STRING (ptr);
3853 break;
3855 case Lisp_Vectorlike:
3856 #ifdef GC_CHECK_MARKED_OBJECTS
3857 m = mem_find (po);
3858 if (m == MEM_NIL && !GC_SUBRP (obj)
3859 && po != &buffer_defaults
3860 && po != &buffer_local_symbols)
3861 abort ();
3862 #endif /* GC_CHECK_MARKED_OBJECTS */
3864 if (GC_BUFFERP (obj))
3866 if (!XMARKBIT (XBUFFER (obj)->name))
3868 #ifdef GC_CHECK_MARKED_OBJECTS
3869 if (po != &buffer_defaults && po != &buffer_local_symbols)
3871 struct buffer *b;
3872 for (b = all_buffers; b && b != po; b = b->next)
3874 if (b == NULL)
3875 abort ();
3877 #endif /* GC_CHECK_MARKED_OBJECTS */
3878 mark_buffer (obj);
3881 else if (GC_SUBRP (obj))
3882 break;
3883 else if (GC_COMPILEDP (obj))
3884 /* We could treat this just like a vector, but it is better to
3885 save the COMPILED_CONSTANTS element for last and avoid
3886 recursion there. */
3888 register struct Lisp_Vector *ptr = XVECTOR (obj);
3889 register EMACS_INT size = ptr->size;
3890 /* See comment above under Lisp_Vector. */
3891 struct Lisp_Vector *volatile ptr1 = ptr;
3892 register int i;
3894 if (size & ARRAY_MARK_FLAG)
3895 break; /* Already marked */
3897 CHECK_LIVE (live_vector_p);
3898 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
3899 size &= PSEUDOVECTOR_SIZE_MASK;
3900 for (i = 0; i < size; i++) /* and then mark its elements */
3902 if (i != COMPILED_CONSTANTS)
3903 mark_object (&ptr1->contents[i]);
3905 /* This cast should be unnecessary, but some Mips compiler complains
3906 (MIPS-ABI + SysVR4, DC/OSx, etc). */
3907 objptr = (Lisp_Object *) &ptr1->contents[COMPILED_CONSTANTS];
3908 goto loop;
3910 else if (GC_FRAMEP (obj))
3912 /* See comment above under Lisp_Vector for why this is volatile. */
3913 register struct frame *volatile ptr = XFRAME (obj);
3914 register EMACS_INT size = ptr->size;
3916 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
3917 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
3919 CHECK_LIVE (live_vector_p);
3920 mark_object (&ptr->name);
3921 mark_object (&ptr->icon_name);
3922 mark_object (&ptr->title);
3923 mark_object (&ptr->focus_frame);
3924 mark_object (&ptr->selected_window);
3925 mark_object (&ptr->minibuffer_window);
3926 mark_object (&ptr->param_alist);
3927 mark_object (&ptr->scroll_bars);
3928 mark_object (&ptr->condemned_scroll_bars);
3929 mark_object (&ptr->menu_bar_items);
3930 mark_object (&ptr->face_alist);
3931 mark_object (&ptr->menu_bar_vector);
3932 mark_object (&ptr->buffer_predicate);
3933 mark_object (&ptr->buffer_list);
3934 mark_object (&ptr->menu_bar_window);
3935 mark_object (&ptr->tool_bar_window);
3936 mark_face_cache (ptr->face_cache);
3937 #ifdef HAVE_WINDOW_SYSTEM
3938 mark_image_cache (ptr);
3939 mark_object (&ptr->desired_tool_bar_items);
3940 mark_object (&ptr->current_tool_bar_items);
3941 mark_object (&ptr->desired_tool_bar_string);
3942 mark_object (&ptr->current_tool_bar_string);
3943 #endif /* HAVE_WINDOW_SYSTEM */
3945 else if (GC_BOOL_VECTOR_P (obj))
3947 register struct Lisp_Vector *ptr = XVECTOR (obj);
3949 if (ptr->size & ARRAY_MARK_FLAG)
3950 break; /* Already marked */
3951 CHECK_LIVE (live_vector_p);
3952 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
3954 else if (GC_WINDOWP (obj))
3956 register struct Lisp_Vector *ptr = XVECTOR (obj);
3957 struct window *w = XWINDOW (obj);
3958 register EMACS_INT size = ptr->size;
3959 /* The reason we use ptr1 is to avoid an apparent hardware bug
3960 that happens occasionally on the FSF's HP 300s.
3961 The bug is that a2 gets clobbered by recursive calls to mark_object.
3962 The clobberage seems to happen during function entry,
3963 perhaps in the moveml instruction.
3964 Yes, this is a crock, but we have to do it. */
3965 struct Lisp_Vector *volatile ptr1 = ptr;
3966 register int i;
3968 /* Stop if already marked. */
3969 if (size & ARRAY_MARK_FLAG)
3970 break;
3972 /* Mark it. */
3973 CHECK_LIVE (live_vector_p);
3974 ptr->size |= ARRAY_MARK_FLAG;
3976 /* There is no Lisp data above The member CURRENT_MATRIX in
3977 struct WINDOW. Stop marking when that slot is reached. */
3978 for (i = 0;
3979 (char *) &ptr1->contents[i] < (char *) &w->current_matrix;
3980 i++)
3981 mark_object (&ptr1->contents[i]);
3983 /* Mark glyphs for leaf windows. Marking window matrices is
3984 sufficient because frame matrices use the same glyph
3985 memory. */
3986 if (NILP (w->hchild)
3987 && NILP (w->vchild)
3988 && w->current_matrix)
3990 mark_glyph_matrix (w->current_matrix);
3991 mark_glyph_matrix (w->desired_matrix);
3994 else if (GC_HASH_TABLE_P (obj))
3996 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
3997 EMACS_INT size = h->size;
3999 /* Stop if already marked. */
4000 if (size & ARRAY_MARK_FLAG)
4001 break;
4003 /* Mark it. */
4004 CHECK_LIVE (live_vector_p);
4005 h->size |= ARRAY_MARK_FLAG;
4007 /* Mark contents. */
4008 mark_object (&h->test);
4009 mark_object (&h->weak);
4010 mark_object (&h->rehash_size);
4011 mark_object (&h->rehash_threshold);
4012 mark_object (&h->hash);
4013 mark_object (&h->next);
4014 mark_object (&h->index);
4015 mark_object (&h->user_hash_function);
4016 mark_object (&h->user_cmp_function);
4018 /* If hash table is not weak, mark all keys and values.
4019 For weak tables, mark only the vector. */
4020 if (GC_NILP (h->weak))
4021 mark_object (&h->key_and_value);
4022 else
4023 XVECTOR (h->key_and_value)->size |= ARRAY_MARK_FLAG;
4026 else
4028 register struct Lisp_Vector *ptr = XVECTOR (obj);
4029 register EMACS_INT size = ptr->size;
4030 /* The reason we use ptr1 is to avoid an apparent hardware bug
4031 that happens occasionally on the FSF's HP 300s.
4032 The bug is that a2 gets clobbered by recursive calls to mark_object.
4033 The clobberage seems to happen during function entry,
4034 perhaps in the moveml instruction.
4035 Yes, this is a crock, but we have to do it. */
4036 struct Lisp_Vector *volatile ptr1 = ptr;
4037 register int i;
4039 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
4040 CHECK_LIVE (live_vector_p);
4041 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
4042 if (size & PSEUDOVECTOR_FLAG)
4043 size &= PSEUDOVECTOR_SIZE_MASK;
4045 for (i = 0; i < size; i++) /* and then mark its elements */
4046 mark_object (&ptr1->contents[i]);
4048 break;
4050 case Lisp_Symbol:
4052 /* See comment above under Lisp_Vector for why this is volatile. */
4053 register struct Lisp_Symbol *volatile ptr = XSYMBOL (obj);
4054 struct Lisp_Symbol *ptrx;
4056 if (XMARKBIT (ptr->plist)) break;
4057 CHECK_ALLOCATED_AND_LIVE (live_symbol_p);
4058 XMARK (ptr->plist);
4059 mark_object ((Lisp_Object *) &ptr->value);
4060 mark_object (&ptr->function);
4061 mark_object (&ptr->plist);
4063 if (!PURE_POINTER_P (ptr->name))
4064 MARK_STRING (ptr->name);
4065 MARK_INTERVAL_TREE (ptr->name->intervals);
4067 /* Note that we do not mark the obarray of the symbol.
4068 It is safe not to do so because nothing accesses that
4069 slot except to check whether it is nil. */
4070 ptr = ptr->next;
4071 if (ptr)
4073 /* For the benefit of the last_marked log. */
4074 objptr = (Lisp_Object *)&XSYMBOL (obj)->next;
4075 ptrx = ptr; /* Use of ptrx avoids compiler bug on Sun */
4076 XSETSYMBOL (obj, ptrx);
4077 /* We can't goto loop here because *objptr doesn't contain an
4078 actual Lisp_Object with valid datatype field. */
4079 goto loop2;
4082 break;
4084 case Lisp_Misc:
4085 CHECK_ALLOCATED_AND_LIVE (live_misc_p);
4086 switch (XMISCTYPE (obj))
4088 case Lisp_Misc_Marker:
4089 XMARK (XMARKER (obj)->chain);
4090 /* DO NOT mark thru the marker's chain.
4091 The buffer's markers chain does not preserve markers from gc;
4092 instead, markers are removed from the chain when freed by gc. */
4093 break;
4095 case Lisp_Misc_Buffer_Local_Value:
4096 case Lisp_Misc_Some_Buffer_Local_Value:
4098 register struct Lisp_Buffer_Local_Value *ptr
4099 = XBUFFER_LOCAL_VALUE (obj);
4100 if (XMARKBIT (ptr->realvalue)) break;
4101 XMARK (ptr->realvalue);
4102 /* If the cdr is nil, avoid recursion for the car. */
4103 if (EQ (ptr->cdr, Qnil))
4105 objptr = &ptr->realvalue;
4106 goto loop;
4108 mark_object (&ptr->realvalue);
4109 mark_object (&ptr->buffer);
4110 mark_object (&ptr->frame);
4111 /* See comment above under Lisp_Vector for why not use ptr here. */
4112 objptr = &XBUFFER_LOCAL_VALUE (obj)->cdr;
4113 goto loop;
4116 case Lisp_Misc_Intfwd:
4117 case Lisp_Misc_Boolfwd:
4118 case Lisp_Misc_Objfwd:
4119 case Lisp_Misc_Buffer_Objfwd:
4120 case Lisp_Misc_Kboard_Objfwd:
4121 /* Don't bother with Lisp_Buffer_Objfwd,
4122 since all markable slots in current buffer marked anyway. */
4123 /* Don't need to do Lisp_Objfwd, since the places they point
4124 are protected with staticpro. */
4125 break;
4127 case Lisp_Misc_Overlay:
4129 struct Lisp_Overlay *ptr = XOVERLAY (obj);
4130 if (!XMARKBIT (ptr->plist))
4132 XMARK (ptr->plist);
4133 mark_object (&ptr->start);
4134 mark_object (&ptr->end);
4135 objptr = &ptr->plist;
4136 goto loop;
4139 break;
4141 default:
4142 abort ();
4144 break;
4146 case Lisp_Cons:
4148 register struct Lisp_Cons *ptr = XCONS (obj);
4149 if (XMARKBIT (ptr->car)) break;
4150 CHECK_ALLOCATED_AND_LIVE (live_cons_p);
4151 XMARK (ptr->car);
4152 /* If the cdr is nil, avoid recursion for the car. */
4153 if (EQ (ptr->cdr, Qnil))
4155 objptr = &ptr->car;
4156 goto loop;
4158 mark_object (&ptr->car);
4159 /* See comment above under Lisp_Vector for why not use ptr here. */
4160 objptr = &XCDR (obj);
4161 goto loop;
4164 case Lisp_Float:
4165 CHECK_ALLOCATED_AND_LIVE (live_float_p);
4166 XMARK (XFLOAT (obj)->type);
4167 break;
4169 case Lisp_Int:
4170 break;
4172 default:
4173 abort ();
4176 #undef CHECK_LIVE
4177 #undef CHECK_ALLOCATED
4178 #undef CHECK_ALLOCATED_AND_LIVE
4181 /* Mark the pointers in a buffer structure. */
4183 static void
4184 mark_buffer (buf)
4185 Lisp_Object buf;
4187 register struct buffer *buffer = XBUFFER (buf);
4188 register Lisp_Object *ptr;
4189 Lisp_Object base_buffer;
4191 /* This is the buffer's markbit */
4192 mark_object (&buffer->name);
4193 XMARK (buffer->name);
4195 MARK_INTERVAL_TREE (BUF_INTERVALS (buffer));
4197 if (CONSP (buffer->undo_list))
4199 Lisp_Object tail;
4200 tail = buffer->undo_list;
4202 while (CONSP (tail))
4204 register struct Lisp_Cons *ptr = XCONS (tail);
4206 if (XMARKBIT (ptr->car))
4207 break;
4208 XMARK (ptr->car);
4209 if (GC_CONSP (ptr->car)
4210 && ! XMARKBIT (XCAR (ptr->car))
4211 && GC_MARKERP (XCAR (ptr->car)))
4213 XMARK (XCAR (ptr->car));
4214 mark_object (&XCDR (ptr->car));
4216 else
4217 mark_object (&ptr->car);
4219 if (CONSP (ptr->cdr))
4220 tail = ptr->cdr;
4221 else
4222 break;
4225 mark_object (&XCDR (tail));
4227 else
4228 mark_object (&buffer->undo_list);
4230 for (ptr = &buffer->name + 1;
4231 (char *)ptr < (char *)buffer + sizeof (struct buffer);
4232 ptr++)
4233 mark_object (ptr);
4235 /* If this is an indirect buffer, mark its base buffer. */
4236 if (buffer->base_buffer && !XMARKBIT (buffer->base_buffer->name))
4238 XSETBUFFER (base_buffer, buffer->base_buffer);
4239 mark_buffer (base_buffer);
4244 /* Mark the pointers in the kboard objects. */
4246 static void
4247 mark_kboards ()
4249 KBOARD *kb;
4250 Lisp_Object *p;
4251 for (kb = all_kboards; kb; kb = kb->next_kboard)
4253 if (kb->kbd_macro_buffer)
4254 for (p = kb->kbd_macro_buffer; p < kb->kbd_macro_ptr; p++)
4255 mark_object (p);
4256 mark_object (&kb->Voverriding_terminal_local_map);
4257 mark_object (&kb->Vlast_command);
4258 mark_object (&kb->Vreal_last_command);
4259 mark_object (&kb->Vprefix_arg);
4260 mark_object (&kb->Vlast_prefix_arg);
4261 mark_object (&kb->kbd_queue);
4262 mark_object (&kb->defining_kbd_macro);
4263 mark_object (&kb->Vlast_kbd_macro);
4264 mark_object (&kb->Vsystem_key_alist);
4265 mark_object (&kb->system_key_syms);
4266 mark_object (&kb->Vdefault_minibuffer_frame);
4271 /* Value is non-zero if OBJ will survive the current GC because it's
4272 either marked or does not need to be marked to survive. */
4275 survives_gc_p (obj)
4276 Lisp_Object obj;
4278 int survives_p;
4280 switch (XGCTYPE (obj))
4282 case Lisp_Int:
4283 survives_p = 1;
4284 break;
4286 case Lisp_Symbol:
4287 survives_p = XMARKBIT (XSYMBOL (obj)->plist);
4288 break;
4290 case Lisp_Misc:
4291 switch (XMISCTYPE (obj))
4293 case Lisp_Misc_Marker:
4294 survives_p = XMARKBIT (obj);
4295 break;
4297 case Lisp_Misc_Buffer_Local_Value:
4298 case Lisp_Misc_Some_Buffer_Local_Value:
4299 survives_p = XMARKBIT (XBUFFER_LOCAL_VALUE (obj)->realvalue);
4300 break;
4302 case Lisp_Misc_Intfwd:
4303 case Lisp_Misc_Boolfwd:
4304 case Lisp_Misc_Objfwd:
4305 case Lisp_Misc_Buffer_Objfwd:
4306 case Lisp_Misc_Kboard_Objfwd:
4307 survives_p = 1;
4308 break;
4310 case Lisp_Misc_Overlay:
4311 survives_p = XMARKBIT (XOVERLAY (obj)->plist);
4312 break;
4314 default:
4315 abort ();
4317 break;
4319 case Lisp_String:
4321 struct Lisp_String *s = XSTRING (obj);
4322 survives_p = STRING_MARKED_P (s);
4324 break;
4326 case Lisp_Vectorlike:
4327 if (GC_BUFFERP (obj))
4328 survives_p = XMARKBIT (XBUFFER (obj)->name);
4329 else if (GC_SUBRP (obj))
4330 survives_p = 1;
4331 else
4332 survives_p = XVECTOR (obj)->size & ARRAY_MARK_FLAG;
4333 break;
4335 case Lisp_Cons:
4336 survives_p = XMARKBIT (XCAR (obj));
4337 break;
4339 case Lisp_Float:
4340 survives_p = XMARKBIT (XFLOAT (obj)->type);
4341 break;
4343 default:
4344 abort ();
4347 return survives_p || PURE_POINTER_P ((void *) XPNTR (obj));
4352 /* Sweep: find all structures not marked, and free them. */
4354 static void
4355 gc_sweep ()
4357 /* Remove or mark entries in weak hash tables.
4358 This must be done before any object is unmarked. */
4359 sweep_weak_hash_tables ();
4361 sweep_strings ();
4363 /* Put all unmarked conses on free list */
4365 register struct cons_block *cblk;
4366 struct cons_block **cprev = &cons_block;
4367 register int lim = cons_block_index;
4368 register int num_free = 0, num_used = 0;
4370 cons_free_list = 0;
4372 for (cblk = cons_block; cblk; cblk = *cprev)
4374 register int i;
4375 int this_free = 0;
4376 for (i = 0; i < lim; i++)
4377 if (!XMARKBIT (cblk->conses[i].car))
4379 this_free++;
4380 *(struct Lisp_Cons **)&cblk->conses[i].cdr = cons_free_list;
4381 cons_free_list = &cblk->conses[i];
4382 #if GC_MARK_STACK
4383 cons_free_list->car = Vdead;
4384 #endif
4386 else
4388 num_used++;
4389 XUNMARK (cblk->conses[i].car);
4391 lim = CONS_BLOCK_SIZE;
4392 /* If this block contains only free conses and we have already
4393 seen more than two blocks worth of free conses then deallocate
4394 this block. */
4395 if (this_free == CONS_BLOCK_SIZE && num_free > CONS_BLOCK_SIZE)
4397 *cprev = cblk->next;
4398 /* Unhook from the free list. */
4399 cons_free_list = *(struct Lisp_Cons **) &cblk->conses[0].cdr;
4400 lisp_free (cblk);
4401 n_cons_blocks--;
4403 else
4405 num_free += this_free;
4406 cprev = &cblk->next;
4409 total_conses = num_used;
4410 total_free_conses = num_free;
4413 /* Put all unmarked floats on free list */
4415 register struct float_block *fblk;
4416 struct float_block **fprev = &float_block;
4417 register int lim = float_block_index;
4418 register int num_free = 0, num_used = 0;
4420 float_free_list = 0;
4422 for (fblk = float_block; fblk; fblk = *fprev)
4424 register int i;
4425 int this_free = 0;
4426 for (i = 0; i < lim; i++)
4427 if (!XMARKBIT (fblk->floats[i].type))
4429 this_free++;
4430 *(struct Lisp_Float **)&fblk->floats[i].data = float_free_list;
4431 float_free_list = &fblk->floats[i];
4432 #if GC_MARK_STACK
4433 float_free_list->type = Vdead;
4434 #endif
4436 else
4438 num_used++;
4439 XUNMARK (fblk->floats[i].type);
4441 lim = FLOAT_BLOCK_SIZE;
4442 /* If this block contains only free floats and we have already
4443 seen more than two blocks worth of free floats then deallocate
4444 this block. */
4445 if (this_free == FLOAT_BLOCK_SIZE && num_free > FLOAT_BLOCK_SIZE)
4447 *fprev = fblk->next;
4448 /* Unhook from the free list. */
4449 float_free_list = *(struct Lisp_Float **) &fblk->floats[0].data;
4450 lisp_free (fblk);
4451 n_float_blocks--;
4453 else
4455 num_free += this_free;
4456 fprev = &fblk->next;
4459 total_floats = num_used;
4460 total_free_floats = num_free;
4463 /* Put all unmarked intervals on free list */
4465 register struct interval_block *iblk;
4466 struct interval_block **iprev = &interval_block;
4467 register int lim = interval_block_index;
4468 register int num_free = 0, num_used = 0;
4470 interval_free_list = 0;
4472 for (iblk = interval_block; iblk; iblk = *iprev)
4474 register int i;
4475 int this_free = 0;
4477 for (i = 0; i < lim; i++)
4479 if (! XMARKBIT (iblk->intervals[i].plist))
4481 SET_INTERVAL_PARENT (&iblk->intervals[i], interval_free_list);
4482 interval_free_list = &iblk->intervals[i];
4483 this_free++;
4485 else
4487 num_used++;
4488 XUNMARK (iblk->intervals[i].plist);
4491 lim = INTERVAL_BLOCK_SIZE;
4492 /* If this block contains only free intervals and we have already
4493 seen more than two blocks worth of free intervals then
4494 deallocate this block. */
4495 if (this_free == INTERVAL_BLOCK_SIZE && num_free > INTERVAL_BLOCK_SIZE)
4497 *iprev = iblk->next;
4498 /* Unhook from the free list. */
4499 interval_free_list = INTERVAL_PARENT (&iblk->intervals[0]);
4500 lisp_free (iblk);
4501 n_interval_blocks--;
4503 else
4505 num_free += this_free;
4506 iprev = &iblk->next;
4509 total_intervals = num_used;
4510 total_free_intervals = num_free;
4513 /* Put all unmarked symbols on free list */
4515 register struct symbol_block *sblk;
4516 struct symbol_block **sprev = &symbol_block;
4517 register int lim = symbol_block_index;
4518 register int num_free = 0, num_used = 0;
4520 symbol_free_list = 0;
4522 for (sblk = symbol_block; sblk; sblk = *sprev)
4524 register int i;
4525 int this_free = 0;
4526 for (i = 0; i < lim; i++)
4527 if (!XMARKBIT (sblk->symbols[i].plist))
4529 *(struct Lisp_Symbol **)&sblk->symbols[i].value = symbol_free_list;
4530 symbol_free_list = &sblk->symbols[i];
4531 #if GC_MARK_STACK
4532 symbol_free_list->function = Vdead;
4533 #endif
4534 this_free++;
4536 else
4538 num_used++;
4539 if (!PURE_POINTER_P (sblk->symbols[i].name))
4540 UNMARK_STRING (sblk->symbols[i].name);
4541 XUNMARK (sblk->symbols[i].plist);
4543 lim = SYMBOL_BLOCK_SIZE;
4544 /* If this block contains only free symbols and we have already
4545 seen more than two blocks worth of free symbols then deallocate
4546 this block. */
4547 if (this_free == SYMBOL_BLOCK_SIZE && num_free > SYMBOL_BLOCK_SIZE)
4549 *sprev = sblk->next;
4550 /* Unhook from the free list. */
4551 symbol_free_list = *(struct Lisp_Symbol **)&sblk->symbols[0].value;
4552 lisp_free (sblk);
4553 n_symbol_blocks--;
4555 else
4557 num_free += this_free;
4558 sprev = &sblk->next;
4561 total_symbols = num_used;
4562 total_free_symbols = num_free;
4565 /* Put all unmarked misc's on free list.
4566 For a marker, first unchain it from the buffer it points into. */
4568 register struct marker_block *mblk;
4569 struct marker_block **mprev = &marker_block;
4570 register int lim = marker_block_index;
4571 register int num_free = 0, num_used = 0;
4573 marker_free_list = 0;
4575 for (mblk = marker_block; mblk; mblk = *mprev)
4577 register int i;
4578 int this_free = 0;
4579 EMACS_INT already_free = -1;
4581 for (i = 0; i < lim; i++)
4583 Lisp_Object *markword;
4584 switch (mblk->markers[i].u_marker.type)
4586 case Lisp_Misc_Marker:
4587 markword = &mblk->markers[i].u_marker.chain;
4588 break;
4589 case Lisp_Misc_Buffer_Local_Value:
4590 case Lisp_Misc_Some_Buffer_Local_Value:
4591 markword = &mblk->markers[i].u_buffer_local_value.realvalue;
4592 break;
4593 case Lisp_Misc_Overlay:
4594 markword = &mblk->markers[i].u_overlay.plist;
4595 break;
4596 case Lisp_Misc_Free:
4597 /* If the object was already free, keep it
4598 on the free list. */
4599 markword = (Lisp_Object *) &already_free;
4600 break;
4601 default:
4602 markword = 0;
4603 break;
4605 if (markword && !XMARKBIT (*markword))
4607 Lisp_Object tem;
4608 if (mblk->markers[i].u_marker.type == Lisp_Misc_Marker)
4610 /* tem1 avoids Sun compiler bug */
4611 struct Lisp_Marker *tem1 = &mblk->markers[i].u_marker;
4612 XSETMARKER (tem, tem1);
4613 unchain_marker (tem);
4615 /* Set the type of the freed object to Lisp_Misc_Free.
4616 We could leave the type alone, since nobody checks it,
4617 but this might catch bugs faster. */
4618 mblk->markers[i].u_marker.type = Lisp_Misc_Free;
4619 mblk->markers[i].u_free.chain = marker_free_list;
4620 marker_free_list = &mblk->markers[i];
4621 this_free++;
4623 else
4625 num_used++;
4626 if (markword)
4627 XUNMARK (*markword);
4630 lim = MARKER_BLOCK_SIZE;
4631 /* If this block contains only free markers and we have already
4632 seen more than two blocks worth of free markers then deallocate
4633 this block. */
4634 if (this_free == MARKER_BLOCK_SIZE && num_free > MARKER_BLOCK_SIZE)
4636 *mprev = mblk->next;
4637 /* Unhook from the free list. */
4638 marker_free_list = mblk->markers[0].u_free.chain;
4639 lisp_free (mblk);
4640 n_marker_blocks--;
4642 else
4644 num_free += this_free;
4645 mprev = &mblk->next;
4649 total_markers = num_used;
4650 total_free_markers = num_free;
4653 /* Free all unmarked buffers */
4655 register struct buffer *buffer = all_buffers, *prev = 0, *next;
4657 while (buffer)
4658 if (!XMARKBIT (buffer->name))
4660 if (prev)
4661 prev->next = buffer->next;
4662 else
4663 all_buffers = buffer->next;
4664 next = buffer->next;
4665 lisp_free (buffer);
4666 buffer = next;
4668 else
4670 XUNMARK (buffer->name);
4671 UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer));
4672 prev = buffer, buffer = buffer->next;
4676 /* Free all unmarked vectors */
4678 register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next;
4679 total_vector_size = 0;
4681 while (vector)
4682 if (!(vector->size & ARRAY_MARK_FLAG))
4684 if (prev)
4685 prev->next = vector->next;
4686 else
4687 all_vectors = vector->next;
4688 next = vector->next;
4689 lisp_free (vector);
4690 n_vectors--;
4691 vector = next;
4694 else
4696 vector->size &= ~ARRAY_MARK_FLAG;
4697 if (vector->size & PSEUDOVECTOR_FLAG)
4698 total_vector_size += (PSEUDOVECTOR_SIZE_MASK & vector->size);
4699 else
4700 total_vector_size += vector->size;
4701 prev = vector, vector = vector->next;
4709 /* Debugging aids. */
4711 DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0,
4712 "Return the address of the last byte Emacs has allocated, divided by 1024.\n\
4713 This may be helpful in debugging Emacs's memory usage.\n\
4714 We divide the value by 1024 to make sure it fits in a Lisp integer.")
4717 Lisp_Object end;
4719 XSETINT (end, (EMACS_INT) sbrk (0) / 1024);
4721 return end;
4724 DEFUN ("memory-use-counts", Fmemory_use_counts, Smemory_use_counts, 0, 0, 0,
4725 "Return a list of counters that measure how much consing there has been.\n\
4726 Each of these counters increments for a certain kind of object.\n\
4727 The counters wrap around from the largest positive integer to zero.\n\
4728 Garbage collection does not decrease them.\n\
4729 The elements of the value are as follows:\n\
4730 (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS STRINGS)\n\
4731 All are in units of 1 = one object consed\n\
4732 except for VECTOR-CELLS and STRING-CHARS, which count the total length of\n\
4733 objects consed.\n\
4734 MISCS include overlays, markers, and some internal types.\n\
4735 Frames, windows, buffers, and subprocesses count as vectors\n\
4736 (but the contents of a buffer's text do not count here).")
4739 Lisp_Object consed[8];
4741 XSETINT (consed[0],
4742 cons_cells_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
4743 XSETINT (consed[1],
4744 floats_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
4745 XSETINT (consed[2],
4746 vector_cells_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
4747 XSETINT (consed[3],
4748 symbols_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
4749 XSETINT (consed[4],
4750 string_chars_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
4751 XSETINT (consed[5],
4752 misc_objects_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
4753 XSETINT (consed[6],
4754 intervals_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
4755 XSETINT (consed[7],
4756 strings_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
4758 return Flist (8, consed);
4761 int suppress_checking;
4762 void
4763 die (msg, file, line)
4764 const char *msg;
4765 const char *file;
4766 int line;
4768 fprintf (stderr, "\r\nEmacs fatal error: %s:%d: %s\r\n",
4769 file, line, msg);
4770 abort ();
4773 /* Initialization */
4775 void
4776 init_alloc_once ()
4778 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
4779 pureptr = 0;
4780 #if GC_MARK_STACK
4781 mem_init ();
4782 Vdead = make_pure_string ("DEAD", 4, 4, 0);
4783 #endif
4784 #ifdef HAVE_SHM
4785 pure_size = PURESIZE;
4786 #endif
4787 all_vectors = 0;
4788 ignore_warnings = 1;
4789 #ifdef DOUG_LEA_MALLOC
4790 mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */
4791 mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */
4792 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); /* max. number of mmap'ed areas */
4793 #endif
4794 init_strings ();
4795 init_cons ();
4796 init_symbol ();
4797 init_marker ();
4798 init_float ();
4799 init_intervals ();
4801 #ifdef REL_ALLOC
4802 malloc_hysteresis = 32;
4803 #else
4804 malloc_hysteresis = 0;
4805 #endif
4807 spare_memory = (char *) malloc (SPARE_MEMORY);
4809 ignore_warnings = 0;
4810 gcprolist = 0;
4811 byte_stack_list = 0;
4812 staticidx = 0;
4813 consing_since_gc = 0;
4814 gc_cons_threshold = 100000 * sizeof (Lisp_Object);
4815 #ifdef VIRT_ADDR_VARIES
4816 malloc_sbrk_unused = 1<<22; /* A large number */
4817 malloc_sbrk_used = 100000; /* as reasonable as any number */
4818 #endif /* VIRT_ADDR_VARIES */
4821 void
4822 init_alloc ()
4824 gcprolist = 0;
4825 byte_stack_list = 0;
4826 #if GC_MARK_STACK
4827 #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
4828 setjmp_tested_p = longjmps_done = 0;
4829 #endif
4830 #endif
4833 void
4834 syms_of_alloc ()
4836 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold,
4837 "*Number of bytes of consing between garbage collections.\n\
4838 Garbage collection can happen automatically once this many bytes have been\n\
4839 allocated since the last garbage collection. All data types count.\n\n\
4840 Garbage collection happens automatically only when `eval' is called.\n\n\
4841 By binding this temporarily to a large number, you can effectively\n\
4842 prevent garbage collection during a part of the program.");
4844 DEFVAR_INT ("pure-bytes-used", &pureptr,
4845 "Number of bytes of sharable Lisp data allocated so far.");
4847 DEFVAR_INT ("cons-cells-consed", &cons_cells_consed,
4848 "Number of cons cells that have been consed so far.");
4850 DEFVAR_INT ("floats-consed", &floats_consed,
4851 "Number of floats that have been consed so far.");
4853 DEFVAR_INT ("vector-cells-consed", &vector_cells_consed,
4854 "Number of vector cells that have been consed so far.");
4856 DEFVAR_INT ("symbols-consed", &symbols_consed,
4857 "Number of symbols that have been consed so far.");
4859 DEFVAR_INT ("string-chars-consed", &string_chars_consed,
4860 "Number of string characters that have been consed so far.");
4862 DEFVAR_INT ("misc-objects-consed", &misc_objects_consed,
4863 "Number of miscellaneous objects that have been consed so far.");
4865 DEFVAR_INT ("intervals-consed", &intervals_consed,
4866 "Number of intervals that have been consed so far.");
4868 DEFVAR_INT ("strings-consed", &strings_consed,
4869 "Number of strings that have been consed so far.");
4871 DEFVAR_LISP ("purify-flag", &Vpurify_flag,
4872 "Non-nil means loading Lisp code in order to dump an executable.\n\
4873 This means that certain objects should be allocated in shared (pure) space.");
4875 DEFVAR_INT ("undo-limit", &undo_limit,
4876 "Keep no more undo information once it exceeds this size.\n\
4877 This limit is applied when garbage collection happens.\n\
4878 The size is counted as the number of bytes occupied,\n\
4879 which includes both saved text and other data.");
4880 undo_limit = 20000;
4882 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit,
4883 "Don't keep more than this much size of undo information.\n\
4884 A command which pushes past this size is itself forgotten.\n\
4885 This limit is applied when garbage collection happens.\n\
4886 The size is counted as the number of bytes occupied,\n\
4887 which includes both saved text and other data.");
4888 undo_strong_limit = 30000;
4890 DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages,
4891 "Non-nil means display messages at start and end of garbage collection.");
4892 garbage_collection_messages = 0;
4894 /* We build this in advance because if we wait until we need it, we might
4895 not be able to allocate the memory to hold it. */
4896 memory_signal_data
4897 = Fcons (Qerror, Fcons (build_string ("Memory exhausted--use M-x save-some-buffers RET"), Qnil));
4898 staticpro (&memory_signal_data);
4900 staticpro (&Qgc_cons_threshold);
4901 Qgc_cons_threshold = intern ("gc-cons-threshold");
4903 staticpro (&Qchar_table_extra_slots);
4904 Qchar_table_extra_slots = intern ("char-table-extra-slots");
4906 defsubr (&Scons);
4907 defsubr (&Slist);
4908 defsubr (&Svector);
4909 defsubr (&Smake_byte_code);
4910 defsubr (&Smake_list);
4911 defsubr (&Smake_vector);
4912 defsubr (&Smake_char_table);
4913 defsubr (&Smake_string);
4914 defsubr (&Smake_bool_vector);
4915 defsubr (&Smake_symbol);
4916 defsubr (&Smake_marker);
4917 defsubr (&Spurecopy);
4918 defsubr (&Sgarbage_collect);
4919 defsubr (&Smemory_limit);
4920 defsubr (&Smemory_use_counts);
4922 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4923 defsubr (&Sgc_status);
4924 #endif