(cvs-menu): Don't move point. Use popup-menu.
[emacs.git] / src / alloc.c
blobb7c61b4bd070fa6cc94482b0afbdd131fcc21df1
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_ ((size_t, 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 size_t 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 size_t 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 size_t 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 POINTER_TYPE *
480 lisp_malloc (nbytes, type)
481 size_t 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 POINTER_TYPE *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) ();
546 static void *emacs_blocked_malloc P_ ((size_t));
547 static void *emacs_blocked_realloc P_ ((void *, size_t));
549 /* This function is used as the hook for free to call. */
551 static void
552 emacs_blocked_free (ptr)
553 void *ptr;
555 BLOCK_INPUT;
556 __free_hook = old_free_hook;
557 free (ptr);
558 /* If we released our reserve (due to running out of memory),
559 and we have a fair amount free once again,
560 try to set aside another reserve in case we run out once more. */
561 if (spare_memory == 0
562 /* Verify there is enough space that even with the malloc
563 hysteresis this call won't run out again.
564 The code here is correct as long as SPARE_MEMORY
565 is substantially larger than the block size malloc uses. */
566 && (bytes_used_when_full
567 > BYTES_USED + max (malloc_hysteresis, 4) * SPARE_MEMORY))
568 spare_memory = (char *) malloc ((size_t) SPARE_MEMORY);
570 __free_hook = emacs_blocked_free;
571 UNBLOCK_INPUT;
575 /* If we released our reserve (due to running out of memory),
576 and we have a fair amount free once again,
577 try to set aside another reserve in case we run out once more.
579 This is called when a relocatable block is freed in ralloc.c. */
581 void
582 refill_memory_reserve ()
584 if (spare_memory == 0)
585 spare_memory = (char *) malloc ((size_t) SPARE_MEMORY);
589 /* This function is the malloc hook that Emacs uses. */
591 static void *
592 emacs_blocked_malloc (size)
593 size_t size;
595 void *value;
597 BLOCK_INPUT;
598 __malloc_hook = old_malloc_hook;
599 #ifdef DOUG_LEA_MALLOC
600 mallopt (M_TOP_PAD, malloc_hysteresis * 4096);
601 #else
602 __malloc_extra_blocks = malloc_hysteresis;
603 #endif
604 value = (void *) malloc (size);
605 __malloc_hook = emacs_blocked_malloc;
606 UNBLOCK_INPUT;
608 return value;
612 /* This function is the realloc hook that Emacs uses. */
614 static void *
615 emacs_blocked_realloc (ptr, size)
616 void *ptr;
617 size_t size;
619 void *value;
621 BLOCK_INPUT;
622 __realloc_hook = old_realloc_hook;
623 value = (void *) realloc (ptr, size);
624 __realloc_hook = emacs_blocked_realloc;
625 UNBLOCK_INPUT;
627 return value;
631 /* Called from main to set up malloc to use our hooks. */
633 void
634 uninterrupt_malloc ()
636 if (__free_hook != emacs_blocked_free)
637 old_free_hook = __free_hook;
638 __free_hook = emacs_blocked_free;
640 if (__malloc_hook != emacs_blocked_malloc)
641 old_malloc_hook = __malloc_hook;
642 __malloc_hook = emacs_blocked_malloc;
644 if (__realloc_hook != emacs_blocked_realloc)
645 old_realloc_hook = __realloc_hook;
646 __realloc_hook = emacs_blocked_realloc;
649 #endif /* not SYSTEM_MALLOC */
653 /***********************************************************************
654 Interval Allocation
655 ***********************************************************************/
657 /* Number of intervals allocated in an interval_block structure.
658 The 1020 is 1024 minus malloc overhead. */
660 #define INTERVAL_BLOCK_SIZE \
661 ((1020 - sizeof (struct interval_block *)) / sizeof (struct interval))
663 /* Intervals are allocated in chunks in form of an interval_block
664 structure. */
666 struct interval_block
668 struct interval_block *next;
669 struct interval intervals[INTERVAL_BLOCK_SIZE];
672 /* Current interval block. Its `next' pointer points to older
673 blocks. */
675 struct interval_block *interval_block;
677 /* Index in interval_block above of the next unused interval
678 structure. */
680 static int interval_block_index;
682 /* Number of free and live intervals. */
684 static int total_free_intervals, total_intervals;
686 /* List of free intervals. */
688 INTERVAL interval_free_list;
690 /* Total number of interval blocks now in use. */
692 int n_interval_blocks;
695 /* Initialize interval allocation. */
697 static void
698 init_intervals ()
700 interval_block
701 = (struct interval_block *) lisp_malloc (sizeof *interval_block,
702 MEM_TYPE_NON_LISP);
703 interval_block->next = 0;
704 bzero ((char *) interval_block->intervals, sizeof interval_block->intervals);
705 interval_block_index = 0;
706 interval_free_list = 0;
707 n_interval_blocks = 1;
711 /* Return a new interval. */
713 INTERVAL
714 make_interval ()
716 INTERVAL val;
718 if (interval_free_list)
720 val = interval_free_list;
721 interval_free_list = INTERVAL_PARENT (interval_free_list);
723 else
725 if (interval_block_index == INTERVAL_BLOCK_SIZE)
727 register struct interval_block *newi;
729 newi = (struct interval_block *) lisp_malloc (sizeof *newi,
730 MEM_TYPE_NON_LISP);
732 VALIDATE_LISP_STORAGE (newi, sizeof *newi);
733 newi->next = interval_block;
734 interval_block = newi;
735 interval_block_index = 0;
736 n_interval_blocks++;
738 val = &interval_block->intervals[interval_block_index++];
740 consing_since_gc += sizeof (struct interval);
741 intervals_consed++;
742 RESET_INTERVAL (val);
743 return val;
747 /* Mark Lisp objects in interval I. */
749 static void
750 mark_interval (i, dummy)
751 register INTERVAL i;
752 Lisp_Object dummy;
754 if (XMARKBIT (i->plist))
755 abort ();
756 mark_object (&i->plist);
757 XMARK (i->plist);
761 /* Mark the interval tree rooted in TREE. Don't call this directly;
762 use the macro MARK_INTERVAL_TREE instead. */
764 static void
765 mark_interval_tree (tree)
766 register INTERVAL tree;
768 /* No need to test if this tree has been marked already; this
769 function is always called through the MARK_INTERVAL_TREE macro,
770 which takes care of that. */
772 /* XMARK expands to an assignment; the LHS of an assignment can't be
773 a cast. */
774 XMARK (tree->up.obj);
776 traverse_intervals (tree, 1, 0, mark_interval, Qnil);
780 /* Mark the interval tree rooted in I. */
782 #define MARK_INTERVAL_TREE(i) \
783 do { \
784 if (!NULL_INTERVAL_P (i) \
785 && ! XMARKBIT (i->up.obj)) \
786 mark_interval_tree (i); \
787 } while (0)
790 /* The oddity in the call to XUNMARK is necessary because XUNMARK
791 expands to an assignment to its argument, and most C compilers
792 don't support casts on the left operand of `='. */
794 #define UNMARK_BALANCE_INTERVALS(i) \
795 do { \
796 if (! NULL_INTERVAL_P (i)) \
798 XUNMARK ((i)->up.obj); \
799 (i) = balance_intervals (i); \
801 } while (0)
804 /* Number support. If NO_UNION_TYPE isn't in effect, we
805 can't create number objects in macros. */
806 #ifndef make_number
807 Lisp_Object
808 make_number (n)
809 int n;
811 Lisp_Object obj;
812 obj.s.val = n;
813 obj.s.type = Lisp_Int;
814 return obj;
816 #endif
818 /***********************************************************************
819 String Allocation
820 ***********************************************************************/
822 /* Lisp_Strings are allocated in string_block structures. When a new
823 string_block is allocated, all the Lisp_Strings it contains are
824 added to a free-list stiing_free_list. When a new Lisp_String is
825 needed, it is taken from that list. During the sweep phase of GC,
826 string_blocks that are entirely free are freed, except two which
827 we keep.
829 String data is allocated from sblock structures. Strings larger
830 than LARGE_STRING_BYTES, get their own sblock, data for smaller
831 strings is sub-allocated out of sblocks of size SBLOCK_SIZE.
833 Sblocks consist internally of sdata structures, one for each
834 Lisp_String. The sdata structure points to the Lisp_String it
835 belongs to. The Lisp_String points back to the `u.data' member of
836 its sdata structure.
838 When a Lisp_String is freed during GC, it is put back on
839 string_free_list, and its `data' member and its sdata's `string'
840 pointer is set to null. The size of the string is recorded in the
841 `u.nbytes' member of the sdata. So, sdata structures that are no
842 longer used, can be easily recognized, and it's easy to compact the
843 sblocks of small strings which we do in compact_small_strings. */
845 /* Size in bytes of an sblock structure used for small strings. This
846 is 8192 minus malloc overhead. */
848 #define SBLOCK_SIZE 8188
850 /* Strings larger than this are considered large strings. String data
851 for large strings is allocated from individual sblocks. */
853 #define LARGE_STRING_BYTES 1024
855 /* Structure describing string memory sub-allocated from an sblock.
856 This is where the contents of Lisp strings are stored. */
858 struct sdata
860 /* Back-pointer to the string this sdata belongs to. If null, this
861 structure is free, and the NBYTES member of the union below
862 contains the string's byte size (the same value that STRING_BYTES
863 would return if STRING were non-null). If non-null, STRING_BYTES
864 (STRING) is the size of the data, and DATA contains the string's
865 contents. */
866 struct Lisp_String *string;
868 union
870 /* When STRING in non-null. */
871 unsigned char data[1];
873 /* When STRING is null. */
874 EMACS_INT nbytes;
875 } u;
878 /* Structure describing a block of memory which is sub-allocated to
879 obtain string data memory for strings. Blocks for small strings
880 are of fixed size SBLOCK_SIZE. Blocks for large strings are made
881 as large as needed. */
883 struct sblock
885 /* Next in list. */
886 struct sblock *next;
888 /* Pointer to the next free sdata block. This points past the end
889 of the sblock if there isn't any space left in this block. */
890 struct sdata *next_free;
892 /* Start of data. */
893 struct sdata first_data;
896 /* Number of Lisp strings in a string_block structure. The 1020 is
897 1024 minus malloc overhead. */
899 #define STRINGS_IN_STRING_BLOCK \
900 ((1020 - sizeof (struct string_block *)) / sizeof (struct Lisp_String))
902 /* Structure describing a block from which Lisp_String structures
903 are allocated. */
905 struct string_block
907 struct string_block *next;
908 struct Lisp_String strings[STRINGS_IN_STRING_BLOCK];
911 /* Head and tail of the list of sblock structures holding Lisp string
912 data. We always allocate from current_sblock. The NEXT pointers
913 in the sblock structures go from oldest_sblock to current_sblock. */
915 static struct sblock *oldest_sblock, *current_sblock;
917 /* List of sblocks for large strings. */
919 static struct sblock *large_sblocks;
921 /* List of string_block structures, and how many there are. */
923 static struct string_block *string_blocks;
924 static int n_string_blocks;
926 /* Free-list of Lisp_Strings. */
928 static struct Lisp_String *string_free_list;
930 /* Number of live and free Lisp_Strings. */
932 static int total_strings, total_free_strings;
934 /* Number of bytes used by live strings. */
936 static int total_string_size;
938 /* Given a pointer to a Lisp_String S which is on the free-list
939 string_free_list, return a pointer to its successor in the
940 free-list. */
942 #define NEXT_FREE_LISP_STRING(S) (*(struct Lisp_String **) (S))
944 /* Return a pointer to the sdata structure belonging to Lisp string S.
945 S must be live, i.e. S->data must not be null. S->data is actually
946 a pointer to the `u.data' member of its sdata structure; the
947 structure starts at a constant offset in front of that. */
949 #define SDATA_OF_STRING(S) \
950 ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *)))
952 /* Value is the size of an sdata structure large enough to hold NBYTES
953 bytes of string data. The value returned includes a terminating
954 NUL byte, the size of the sdata structure, and padding. */
956 #define SDATA_SIZE(NBYTES) \
957 ((sizeof (struct Lisp_String *) \
958 + (NBYTES) + 1 \
959 + sizeof (EMACS_INT) - 1) \
960 & ~(sizeof (EMACS_INT) - 1))
963 /* Initialize string allocation. Called from init_alloc_once. */
965 void
966 init_strings ()
968 total_strings = total_free_strings = total_string_size = 0;
969 oldest_sblock = current_sblock = large_sblocks = NULL;
970 string_blocks = NULL;
971 n_string_blocks = 0;
972 string_free_list = NULL;
976 /* Return a new Lisp_String. */
978 static struct Lisp_String *
979 allocate_string ()
981 struct Lisp_String *s;
983 /* If the free-list is empty, allocate a new string_block, and
984 add all the Lisp_Strings in it to the free-list. */
985 if (string_free_list == NULL)
987 struct string_block *b;
988 int i;
990 b = (struct string_block *) lisp_malloc (sizeof *b, MEM_TYPE_STRING);
991 VALIDATE_LISP_STORAGE (b, sizeof *b);
992 bzero (b, sizeof *b);
993 b->next = string_blocks;
994 string_blocks = b;
995 ++n_string_blocks;
997 for (i = STRINGS_IN_STRING_BLOCK - 1; i >= 0; --i)
999 s = b->strings + i;
1000 NEXT_FREE_LISP_STRING (s) = string_free_list;
1001 string_free_list = s;
1004 total_free_strings += STRINGS_IN_STRING_BLOCK;
1007 /* Pop a Lisp_String off the free-list. */
1008 s = string_free_list;
1009 string_free_list = NEXT_FREE_LISP_STRING (s);
1011 /* Probably not strictly necessary, but play it safe. */
1012 bzero (s, sizeof *s);
1014 --total_free_strings;
1015 ++total_strings;
1016 ++strings_consed;
1017 consing_since_gc += sizeof *s;
1019 return s;
1023 /* Set up Lisp_String S for holding NCHARS characters, NBYTES bytes,
1024 plus a NUL byte at the end. Allocate an sdata structure for S, and
1025 set S->data to its `u.data' member. Store a NUL byte at the end of
1026 S->data. Set S->size to NCHARS and S->size_byte to NBYTES. Free
1027 S->data if it was initially non-null. */
1029 void
1030 allocate_string_data (s, nchars, nbytes)
1031 struct Lisp_String *s;
1032 int nchars, nbytes;
1034 struct sdata *data, *old_data;
1035 struct sblock *b;
1036 int needed, old_nbytes;
1038 /* Determine the number of bytes needed to store NBYTES bytes
1039 of string data. */
1040 needed = SDATA_SIZE (nbytes);
1042 if (nbytes > LARGE_STRING_BYTES)
1044 size_t size = sizeof *b - sizeof (struct sdata) + needed;
1046 #ifdef DOUG_LEA_MALLOC
1047 /* Prevent mmap'ing the chunk (which is potentially very large). */
1048 mallopt (M_MMAP_MAX, 0);
1049 #endif
1051 b = (struct sblock *) lisp_malloc (size, MEM_TYPE_NON_LISP);
1053 #ifdef DOUG_LEA_MALLOC
1054 /* Back to a reasonable maximum of mmap'ed areas. */
1055 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
1056 #endif
1058 b->next_free = &b->first_data;
1059 b->first_data.string = NULL;
1060 b->next = large_sblocks;
1061 large_sblocks = b;
1063 else if (current_sblock == NULL
1064 || (((char *) current_sblock + SBLOCK_SIZE
1065 - (char *) current_sblock->next_free)
1066 < needed))
1068 /* Not enough room in the current sblock. */
1069 b = (struct sblock *) lisp_malloc (SBLOCK_SIZE, MEM_TYPE_NON_LISP);
1070 b->next_free = &b->first_data;
1071 b->first_data.string = NULL;
1072 b->next = NULL;
1074 if (current_sblock)
1075 current_sblock->next = b;
1076 else
1077 oldest_sblock = b;
1078 current_sblock = b;
1080 else
1081 b = current_sblock;
1083 old_data = s->data ? SDATA_OF_STRING (s) : NULL;
1084 old_nbytes = GC_STRING_BYTES (s);
1086 data = b->next_free;
1087 data->string = s;
1088 s->data = data->u.data;
1089 s->size = nchars;
1090 s->size_byte = nbytes;
1091 s->data[nbytes] = '\0';
1092 b->next_free = (struct sdata *) ((char *) data + needed);
1094 /* If S had already data assigned, mark that as free by setting its
1095 string back-pointer to null, and recording the size of the data
1096 in it. */
1097 if (old_data)
1099 old_data->u.nbytes = old_nbytes;
1100 old_data->string = NULL;
1103 consing_since_gc += needed;
1107 /* Sweep and compact strings. */
1109 static void
1110 sweep_strings ()
1112 struct string_block *b, *next;
1113 struct string_block *live_blocks = NULL;
1115 string_free_list = NULL;
1116 total_strings = total_free_strings = 0;
1117 total_string_size = 0;
1119 /* Scan strings_blocks, free Lisp_Strings that aren't marked. */
1120 for (b = string_blocks; b; b = next)
1122 int i, nfree = 0;
1123 struct Lisp_String *free_list_before = string_free_list;
1125 next = b->next;
1127 for (i = 0; i < STRINGS_IN_STRING_BLOCK; ++i)
1129 struct Lisp_String *s = b->strings + i;
1131 if (s->data)
1133 /* String was not on free-list before. */
1134 if (STRING_MARKED_P (s))
1136 /* String is live; unmark it and its intervals. */
1137 UNMARK_STRING (s);
1139 if (!NULL_INTERVAL_P (s->intervals))
1140 UNMARK_BALANCE_INTERVALS (s->intervals);
1142 ++total_strings;
1143 total_string_size += STRING_BYTES (s);
1145 else
1147 /* String is dead. Put it on the free-list. */
1148 struct sdata *data = SDATA_OF_STRING (s);
1150 /* Save the size of S in its sdata so that we know
1151 how large that is. Reset the sdata's string
1152 back-pointer so that we know it's free. */
1153 data->u.nbytes = GC_STRING_BYTES (s);
1154 data->string = NULL;
1156 /* Reset the strings's `data' member so that we
1157 know it's free. */
1158 s->data = NULL;
1160 /* Put the string on the free-list. */
1161 NEXT_FREE_LISP_STRING (s) = string_free_list;
1162 string_free_list = s;
1163 ++nfree;
1166 else
1168 /* S was on the free-list before. Put it there again. */
1169 NEXT_FREE_LISP_STRING (s) = string_free_list;
1170 string_free_list = s;
1171 ++nfree;
1175 /* Free blocks that contain free Lisp_Strings only, except
1176 the first two of them. */
1177 if (nfree == STRINGS_IN_STRING_BLOCK
1178 && total_free_strings > STRINGS_IN_STRING_BLOCK)
1180 lisp_free (b);
1181 --n_string_blocks;
1182 string_free_list = free_list_before;
1184 else
1186 total_free_strings += nfree;
1187 b->next = live_blocks;
1188 live_blocks = b;
1192 string_blocks = live_blocks;
1193 free_large_strings ();
1194 compact_small_strings ();
1198 /* Free dead large strings. */
1200 static void
1201 free_large_strings ()
1203 struct sblock *b, *next;
1204 struct sblock *live_blocks = NULL;
1206 for (b = large_sblocks; b; b = next)
1208 next = b->next;
1210 if (b->first_data.string == NULL)
1211 lisp_free (b);
1212 else
1214 b->next = live_blocks;
1215 live_blocks = b;
1219 large_sblocks = live_blocks;
1223 /* Compact data of small strings. Free sblocks that don't contain
1224 data of live strings after compaction. */
1226 static void
1227 compact_small_strings ()
1229 struct sblock *b, *tb, *next;
1230 struct sdata *from, *to, *end, *tb_end;
1231 struct sdata *to_end, *from_end;
1233 /* TB is the sblock we copy to, TO is the sdata within TB we copy
1234 to, and TB_END is the end of TB. */
1235 tb = oldest_sblock;
1236 tb_end = (struct sdata *) ((char *) tb + SBLOCK_SIZE);
1237 to = &tb->first_data;
1239 /* Step through the blocks from the oldest to the youngest. We
1240 expect that old blocks will stabilize over time, so that less
1241 copying will happen this way. */
1242 for (b = oldest_sblock; b; b = b->next)
1244 end = b->next_free;
1245 xassert ((char *) end <= (char *) b + SBLOCK_SIZE);
1247 for (from = &b->first_data; from < end; from = from_end)
1249 /* Compute the next FROM here because copying below may
1250 overwrite data we need to compute it. */
1251 int nbytes;
1253 if (from->string)
1254 nbytes = GC_STRING_BYTES (from->string);
1255 else
1256 nbytes = from->u.nbytes;
1258 nbytes = SDATA_SIZE (nbytes);
1259 from_end = (struct sdata *) ((char *) from + nbytes);
1261 /* FROM->string non-null means it's alive. Copy its data. */
1262 if (from->string)
1264 /* If TB is full, proceed with the next sblock. */
1265 to_end = (struct sdata *) ((char *) to + nbytes);
1266 if (to_end > tb_end)
1268 tb->next_free = to;
1269 tb = tb->next;
1270 tb_end = (struct sdata *) ((char *) tb + SBLOCK_SIZE);
1271 to = &tb->first_data;
1272 to_end = (struct sdata *) ((char *) to + nbytes);
1275 /* Copy, and update the string's `data' pointer. */
1276 if (from != to)
1278 bcopy (from, to, nbytes);
1279 to->string->data = to->u.data;
1282 /* Advance past the sdata we copied to. */
1283 to = to_end;
1288 /* The rest of the sblocks following TB don't contain live data, so
1289 we can free them. */
1290 for (b = tb->next; b; b = next)
1292 next = b->next;
1293 lisp_free (b);
1296 tb->next_free = to;
1297 tb->next = NULL;
1298 current_sblock = tb;
1302 DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0,
1303 "Return a newly created string of length LENGTH, with each element being INIT.\n\
1304 Both LENGTH and INIT must be numbers.")
1305 (length, init)
1306 Lisp_Object length, init;
1308 register Lisp_Object val;
1309 register unsigned char *p, *end;
1310 int c, nbytes;
1312 CHECK_NATNUM (length, 0);
1313 CHECK_NUMBER (init, 1);
1315 c = XINT (init);
1316 if (SINGLE_BYTE_CHAR_P (c))
1318 nbytes = XINT (length);
1319 val = make_uninit_string (nbytes);
1320 p = XSTRING (val)->data;
1321 end = p + XSTRING (val)->size;
1322 while (p != end)
1323 *p++ = c;
1325 else
1327 unsigned char str[4];
1328 int len = CHAR_STRING (c, str);
1330 nbytes = len * XINT (length);
1331 val = make_uninit_multibyte_string (XINT (length), nbytes);
1332 p = XSTRING (val)->data;
1333 end = p + nbytes;
1334 while (p != end)
1336 bcopy (str, p, len);
1337 p += len;
1341 *p = 0;
1342 return val;
1346 DEFUN ("make-bool-vector", Fmake_bool_vector, Smake_bool_vector, 2, 2, 0,
1347 "Return a new bool-vector of length LENGTH, using INIT for as each element.\n\
1348 LENGTH must be a number. INIT matters only in whether it is t or nil.")
1349 (length, init)
1350 Lisp_Object length, init;
1352 register Lisp_Object val;
1353 struct Lisp_Bool_Vector *p;
1354 int real_init, i;
1355 int length_in_chars, length_in_elts, bits_per_value;
1357 CHECK_NATNUM (length, 0);
1359 bits_per_value = sizeof (EMACS_INT) * BITS_PER_CHAR;
1361 length_in_elts = (XFASTINT (length) + bits_per_value - 1) / bits_per_value;
1362 length_in_chars = ((XFASTINT (length) + BITS_PER_CHAR - 1) / BITS_PER_CHAR);
1364 /* We must allocate one more elements than LENGTH_IN_ELTS for the
1365 slot `size' of the struct Lisp_Bool_Vector. */
1366 val = Fmake_vector (make_number (length_in_elts + 1), Qnil);
1367 p = XBOOL_VECTOR (val);
1369 /* Get rid of any bits that would cause confusion. */
1370 p->vector_size = 0;
1371 XSETBOOL_VECTOR (val, p);
1372 p->size = XFASTINT (length);
1374 real_init = (NILP (init) ? 0 : -1);
1375 for (i = 0; i < length_in_chars ; i++)
1376 p->data[i] = real_init;
1378 /* Clear the extraneous bits in the last byte. */
1379 if (XINT (length) != length_in_chars * BITS_PER_CHAR)
1380 XBOOL_VECTOR (val)->data[length_in_chars - 1]
1381 &= (1 << (XINT (length) % BITS_PER_CHAR)) - 1;
1383 return val;
1387 /* Make a string from NBYTES bytes at CONTENTS, and compute the number
1388 of characters from the contents. This string may be unibyte or
1389 multibyte, depending on the contents. */
1391 Lisp_Object
1392 make_string (contents, nbytes)
1393 char *contents;
1394 int nbytes;
1396 register Lisp_Object val;
1397 int nchars, multibyte_nbytes;
1399 parse_str_as_multibyte (contents, nbytes, &nchars, &multibyte_nbytes);
1400 val = make_uninit_multibyte_string (nchars, nbytes);
1401 bcopy (contents, XSTRING (val)->data, nbytes);
1402 if (nbytes == nchars || nbytes != multibyte_nbytes)
1403 /* CONTENTS contains no multibyte sequences or contains an invalid
1404 multibyte sequence. We must make unibyte string. */
1405 SET_STRING_BYTES (XSTRING (val), -1);
1406 return val;
1410 /* Make an unibyte string from LENGTH bytes at CONTENTS. */
1412 Lisp_Object
1413 make_unibyte_string (contents, length)
1414 char *contents;
1415 int length;
1417 register Lisp_Object val;
1418 val = make_uninit_string (length);
1419 bcopy (contents, XSTRING (val)->data, length);
1420 SET_STRING_BYTES (XSTRING (val), -1);
1421 return val;
1425 /* Make a multibyte string from NCHARS characters occupying NBYTES
1426 bytes at CONTENTS. */
1428 Lisp_Object
1429 make_multibyte_string (contents, nchars, nbytes)
1430 char *contents;
1431 int nchars, nbytes;
1433 register Lisp_Object val;
1434 val = make_uninit_multibyte_string (nchars, nbytes);
1435 bcopy (contents, XSTRING (val)->data, nbytes);
1436 return val;
1440 /* Make a string from NCHARS characters occupying NBYTES bytes at
1441 CONTENTS. It is a multibyte string if NBYTES != NCHARS. */
1443 Lisp_Object
1444 make_string_from_bytes (contents, nchars, nbytes)
1445 char *contents;
1446 int nchars, nbytes;
1448 register Lisp_Object val;
1449 val = make_uninit_multibyte_string (nchars, nbytes);
1450 bcopy (contents, XSTRING (val)->data, nbytes);
1451 if (STRING_BYTES (XSTRING (val)) == XSTRING (val)->size)
1452 SET_STRING_BYTES (XSTRING (val), -1);
1453 return val;
1457 /* Make a string from NCHARS characters occupying NBYTES bytes at
1458 CONTENTS. The argument MULTIBYTE controls whether to label the
1459 string as multibyte. */
1461 Lisp_Object
1462 make_specified_string (contents, nchars, nbytes, multibyte)
1463 char *contents;
1464 int nchars, nbytes;
1465 int multibyte;
1467 register Lisp_Object val;
1468 val = make_uninit_multibyte_string (nchars, nbytes);
1469 bcopy (contents, XSTRING (val)->data, nbytes);
1470 if (!multibyte)
1471 SET_STRING_BYTES (XSTRING (val), -1);
1472 return val;
1476 /* Make a string from the data at STR, treating it as multibyte if the
1477 data warrants. */
1479 Lisp_Object
1480 build_string (str)
1481 char *str;
1483 return make_string (str, strlen (str));
1487 /* Return an unibyte Lisp_String set up to hold LENGTH characters
1488 occupying LENGTH bytes. */
1490 Lisp_Object
1491 make_uninit_string (length)
1492 int length;
1494 Lisp_Object val;
1495 val = make_uninit_multibyte_string (length, length);
1496 SET_STRING_BYTES (XSTRING (val), -1);
1497 return val;
1501 /* Return a multibyte Lisp_String set up to hold NCHARS characters
1502 which occupy NBYTES bytes. */
1504 Lisp_Object
1505 make_uninit_multibyte_string (nchars, nbytes)
1506 int nchars, nbytes;
1508 Lisp_Object string;
1509 struct Lisp_String *s;
1511 if (nchars < 0)
1512 abort ();
1514 s = allocate_string ();
1515 allocate_string_data (s, nchars, nbytes);
1516 XSETSTRING (string, s);
1517 string_chars_consed += nbytes;
1518 return string;
1523 /***********************************************************************
1524 Float Allocation
1525 ***********************************************************************/
1527 /* We store float cells inside of float_blocks, allocating a new
1528 float_block with malloc whenever necessary. Float cells reclaimed
1529 by GC are put on a free list to be reallocated before allocating
1530 any new float cells from the latest float_block.
1532 Each float_block is just under 1020 bytes long, since malloc really
1533 allocates in units of powers of two and uses 4 bytes for its own
1534 overhead. */
1536 #define FLOAT_BLOCK_SIZE \
1537 ((1020 - sizeof (struct float_block *)) / sizeof (struct Lisp_Float))
1539 struct float_block
1541 struct float_block *next;
1542 struct Lisp_Float floats[FLOAT_BLOCK_SIZE];
1545 /* Current float_block. */
1547 struct float_block *float_block;
1549 /* Index of first unused Lisp_Float in the current float_block. */
1551 int float_block_index;
1553 /* Total number of float blocks now in use. */
1555 int n_float_blocks;
1557 /* Free-list of Lisp_Floats. */
1559 struct Lisp_Float *float_free_list;
1562 /* Initialze float allocation. */
1564 void
1565 init_float ()
1567 float_block = (struct float_block *) lisp_malloc (sizeof *float_block,
1568 MEM_TYPE_FLOAT);
1569 float_block->next = 0;
1570 bzero ((char *) float_block->floats, sizeof float_block->floats);
1571 float_block_index = 0;
1572 float_free_list = 0;
1573 n_float_blocks = 1;
1577 /* Explicitly free a float cell by putting it on the free-list. */
1579 void
1580 free_float (ptr)
1581 struct Lisp_Float *ptr;
1583 *(struct Lisp_Float **)&ptr->data = float_free_list;
1584 #if GC_MARK_STACK
1585 ptr->type = Vdead;
1586 #endif
1587 float_free_list = ptr;
1591 /* Return a new float object with value FLOAT_VALUE. */
1593 Lisp_Object
1594 make_float (float_value)
1595 double float_value;
1597 register Lisp_Object val;
1599 if (float_free_list)
1601 /* We use the data field for chaining the free list
1602 so that we won't use the same field that has the mark bit. */
1603 XSETFLOAT (val, float_free_list);
1604 float_free_list = *(struct Lisp_Float **)&float_free_list->data;
1606 else
1608 if (float_block_index == FLOAT_BLOCK_SIZE)
1610 register struct float_block *new;
1612 new = (struct float_block *) lisp_malloc (sizeof *new,
1613 MEM_TYPE_FLOAT);
1614 VALIDATE_LISP_STORAGE (new, sizeof *new);
1615 new->next = float_block;
1616 float_block = new;
1617 float_block_index = 0;
1618 n_float_blocks++;
1620 XSETFLOAT (val, &float_block->floats[float_block_index++]);
1623 XFLOAT_DATA (val) = float_value;
1624 XSETFASTINT (XFLOAT (val)->type, 0); /* bug chasing -wsr */
1625 consing_since_gc += sizeof (struct Lisp_Float);
1626 floats_consed++;
1627 return val;
1632 /***********************************************************************
1633 Cons Allocation
1634 ***********************************************************************/
1636 /* We store cons cells inside of cons_blocks, allocating a new
1637 cons_block with malloc whenever necessary. Cons cells reclaimed by
1638 GC are put on a free list to be reallocated before allocating
1639 any new cons cells from the latest cons_block.
1641 Each cons_block is just under 1020 bytes long,
1642 since malloc really allocates in units of powers of two
1643 and uses 4 bytes for its own overhead. */
1645 #define CONS_BLOCK_SIZE \
1646 ((1020 - sizeof (struct cons_block *)) / sizeof (struct Lisp_Cons))
1648 struct cons_block
1650 struct cons_block *next;
1651 struct Lisp_Cons conses[CONS_BLOCK_SIZE];
1654 /* Current cons_block. */
1656 struct cons_block *cons_block;
1658 /* Index of first unused Lisp_Cons in the current block. */
1660 int cons_block_index;
1662 /* Free-list of Lisp_Cons structures. */
1664 struct Lisp_Cons *cons_free_list;
1666 /* Total number of cons blocks now in use. */
1668 int n_cons_blocks;
1671 /* Initialize cons allocation. */
1673 void
1674 init_cons ()
1676 cons_block = (struct cons_block *) lisp_malloc (sizeof *cons_block,
1677 MEM_TYPE_CONS);
1678 cons_block->next = 0;
1679 bzero ((char *) cons_block->conses, sizeof cons_block->conses);
1680 cons_block_index = 0;
1681 cons_free_list = 0;
1682 n_cons_blocks = 1;
1686 /* Explicitly free a cons cell by putting it on the free-list. */
1688 void
1689 free_cons (ptr)
1690 struct Lisp_Cons *ptr;
1692 *(struct Lisp_Cons **)&ptr->cdr = cons_free_list;
1693 #if GC_MARK_STACK
1694 ptr->car = Vdead;
1695 #endif
1696 cons_free_list = ptr;
1700 DEFUN ("cons", Fcons, Scons, 2, 2, 0,
1701 "Create a new cons, give it CAR and CDR as components, and return it.")
1702 (car, cdr)
1703 Lisp_Object car, cdr;
1705 register Lisp_Object val;
1707 if (cons_free_list)
1709 /* We use the cdr for chaining the free list
1710 so that we won't use the same field that has the mark bit. */
1711 XSETCONS (val, cons_free_list);
1712 cons_free_list = *(struct Lisp_Cons **)&cons_free_list->cdr;
1714 else
1716 if (cons_block_index == CONS_BLOCK_SIZE)
1718 register struct cons_block *new;
1719 new = (struct cons_block *) lisp_malloc (sizeof *new,
1720 MEM_TYPE_CONS);
1721 VALIDATE_LISP_STORAGE (new, sizeof *new);
1722 new->next = cons_block;
1723 cons_block = new;
1724 cons_block_index = 0;
1725 n_cons_blocks++;
1727 XSETCONS (val, &cons_block->conses[cons_block_index++]);
1730 XCAR (val) = car;
1731 XCDR (val) = cdr;
1732 consing_since_gc += sizeof (struct Lisp_Cons);
1733 cons_cells_consed++;
1734 return val;
1738 /* Make a list of 2, 3, 4 or 5 specified objects. */
1740 Lisp_Object
1741 list2 (arg1, arg2)
1742 Lisp_Object arg1, arg2;
1744 return Fcons (arg1, Fcons (arg2, Qnil));
1748 Lisp_Object
1749 list3 (arg1, arg2, arg3)
1750 Lisp_Object arg1, arg2, arg3;
1752 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Qnil)));
1756 Lisp_Object
1757 list4 (arg1, arg2, arg3, arg4)
1758 Lisp_Object arg1, arg2, arg3, arg4;
1760 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4, Qnil))));
1764 Lisp_Object
1765 list5 (arg1, arg2, arg3, arg4, arg5)
1766 Lisp_Object arg1, arg2, arg3, arg4, arg5;
1768 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4,
1769 Fcons (arg5, Qnil)))));
1773 DEFUN ("list", Flist, Slist, 0, MANY, 0,
1774 "Return a newly created list with specified arguments as elements.\n\
1775 Any number of arguments, even zero arguments, are allowed.")
1776 (nargs, args)
1777 int nargs;
1778 register Lisp_Object *args;
1780 register Lisp_Object val;
1781 val = Qnil;
1783 while (nargs > 0)
1785 nargs--;
1786 val = Fcons (args[nargs], val);
1788 return val;
1792 DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0,
1793 "Return a newly created list of length LENGTH, with each element being INIT.")
1794 (length, init)
1795 register Lisp_Object length, init;
1797 register Lisp_Object val;
1798 register int size;
1800 CHECK_NATNUM (length, 0);
1801 size = XFASTINT (length);
1803 val = Qnil;
1804 while (size-- > 0)
1805 val = Fcons (init, val);
1806 return val;
1811 /***********************************************************************
1812 Vector Allocation
1813 ***********************************************************************/
1815 /* Singly-linked list of all vectors. */
1817 struct Lisp_Vector *all_vectors;
1819 /* Total number of vector-like objects now in use. */
1821 int n_vectors;
1824 /* Value is a pointer to a newly allocated Lisp_Vector structure
1825 with room for LEN Lisp_Objects. */
1827 struct Lisp_Vector *
1828 allocate_vectorlike (len)
1829 EMACS_INT len;
1831 struct Lisp_Vector *p;
1832 size_t nbytes;
1834 #ifdef DOUG_LEA_MALLOC
1835 /* Prevent mmap'ing the chunk (which is potentially very large).. */
1836 mallopt (M_MMAP_MAX, 0);
1837 #endif
1839 nbytes = sizeof *p + (len - 1) * sizeof p->contents[0];
1840 p = (struct Lisp_Vector *) lisp_malloc (nbytes, MEM_TYPE_VECTOR);
1842 #ifdef DOUG_LEA_MALLOC
1843 /* Back to a reasonable maximum of mmap'ed areas. */
1844 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
1845 #endif
1847 VALIDATE_LISP_STORAGE (p, 0);
1848 consing_since_gc += nbytes;
1849 vector_cells_consed += len;
1851 p->next = all_vectors;
1852 all_vectors = p;
1853 ++n_vectors;
1854 return p;
1858 DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0,
1859 "Return a newly created vector of length LENGTH, with each element being INIT.\n\
1860 See also the function `vector'.")
1861 (length, init)
1862 register Lisp_Object length, init;
1864 Lisp_Object vector;
1865 register EMACS_INT sizei;
1866 register int index;
1867 register struct Lisp_Vector *p;
1869 CHECK_NATNUM (length, 0);
1870 sizei = XFASTINT (length);
1872 p = allocate_vectorlike (sizei);
1873 p->size = sizei;
1874 for (index = 0; index < sizei; index++)
1875 p->contents[index] = init;
1877 XSETVECTOR (vector, p);
1878 return vector;
1882 DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0,
1883 "Return a newly created char-table, with purpose PURPOSE.\n\
1884 Each element is initialized to INIT, which defaults to nil.\n\
1885 PURPOSE should be a symbol which has a `char-table-extra-slots' property.\n\
1886 The property's value should be an integer between 0 and 10.")
1887 (purpose, init)
1888 register Lisp_Object purpose, init;
1890 Lisp_Object vector;
1891 Lisp_Object n;
1892 CHECK_SYMBOL (purpose, 1);
1893 n = Fget (purpose, Qchar_table_extra_slots);
1894 CHECK_NUMBER (n, 0);
1895 if (XINT (n) < 0 || XINT (n) > 10)
1896 args_out_of_range (n, Qnil);
1897 /* Add 2 to the size for the defalt and parent slots. */
1898 vector = Fmake_vector (make_number (CHAR_TABLE_STANDARD_SLOTS + XINT (n)),
1899 init);
1900 XCHAR_TABLE (vector)->top = Qt;
1901 XCHAR_TABLE (vector)->parent = Qnil;
1902 XCHAR_TABLE (vector)->purpose = purpose;
1903 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
1904 return vector;
1908 /* Return a newly created sub char table with default value DEFALT.
1909 Since a sub char table does not appear as a top level Emacs Lisp
1910 object, we don't need a Lisp interface to make it. */
1912 Lisp_Object
1913 make_sub_char_table (defalt)
1914 Lisp_Object defalt;
1916 Lisp_Object vector
1917 = Fmake_vector (make_number (SUB_CHAR_TABLE_STANDARD_SLOTS), Qnil);
1918 XCHAR_TABLE (vector)->top = Qnil;
1919 XCHAR_TABLE (vector)->defalt = defalt;
1920 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
1921 return vector;
1925 DEFUN ("vector", Fvector, Svector, 0, MANY, 0,
1926 "Return a newly created vector with specified arguments as elements.\n\
1927 Any number of arguments, even zero arguments, are allowed.")
1928 (nargs, args)
1929 register int nargs;
1930 Lisp_Object *args;
1932 register Lisp_Object len, val;
1933 register int index;
1934 register struct Lisp_Vector *p;
1936 XSETFASTINT (len, nargs);
1937 val = Fmake_vector (len, Qnil);
1938 p = XVECTOR (val);
1939 for (index = 0; index < nargs; index++)
1940 p->contents[index] = args[index];
1941 return val;
1945 DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0,
1946 "Create a byte-code object with specified arguments as elements.\n\
1947 The arguments should be the arglist, bytecode-string, constant vector,\n\
1948 stack size, (optional) doc string, and (optional) interactive spec.\n\
1949 The first four arguments are required; at most six have any\n\
1950 significance.")
1951 (nargs, args)
1952 register int nargs;
1953 Lisp_Object *args;
1955 register Lisp_Object len, val;
1956 register int index;
1957 register struct Lisp_Vector *p;
1959 XSETFASTINT (len, nargs);
1960 if (!NILP (Vpurify_flag))
1961 val = make_pure_vector ((EMACS_INT) nargs);
1962 else
1963 val = Fmake_vector (len, Qnil);
1965 if (STRINGP (args[1]) && STRING_MULTIBYTE (args[1]))
1966 /* BYTECODE-STRING must have been produced by Emacs 20.2 or the
1967 earlier because they produced a raw 8-bit string for byte-code
1968 and now such a byte-code string is loaded as multibyte while
1969 raw 8-bit characters converted to multibyte form. Thus, now we
1970 must convert them back to the original unibyte form. */
1971 args[1] = Fstring_as_unibyte (args[1]);
1973 p = XVECTOR (val);
1974 for (index = 0; index < nargs; index++)
1976 if (!NILP (Vpurify_flag))
1977 args[index] = Fpurecopy (args[index]);
1978 p->contents[index] = args[index];
1980 XSETCOMPILED (val, p);
1981 return val;
1986 /***********************************************************************
1987 Symbol Allocation
1988 ***********************************************************************/
1990 /* Each symbol_block is just under 1020 bytes long, since malloc
1991 really allocates in units of powers of two and uses 4 bytes for its
1992 own overhead. */
1994 #define SYMBOL_BLOCK_SIZE \
1995 ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol))
1997 struct symbol_block
1999 struct symbol_block *next;
2000 struct Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE];
2003 /* Current symbol block and index of first unused Lisp_Symbol
2004 structure in it. */
2006 struct symbol_block *symbol_block;
2007 int symbol_block_index;
2009 /* List of free symbols. */
2011 struct Lisp_Symbol *symbol_free_list;
2013 /* Total number of symbol blocks now in use. */
2015 int n_symbol_blocks;
2018 /* Initialize symbol allocation. */
2020 void
2021 init_symbol ()
2023 symbol_block = (struct symbol_block *) lisp_malloc (sizeof *symbol_block,
2024 MEM_TYPE_SYMBOL);
2025 symbol_block->next = 0;
2026 bzero ((char *) symbol_block->symbols, sizeof symbol_block->symbols);
2027 symbol_block_index = 0;
2028 symbol_free_list = 0;
2029 n_symbol_blocks = 1;
2033 DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0,
2034 "Return a newly allocated uninterned symbol whose name is NAME.\n\
2035 Its value and function definition are void, and its property list is nil.")
2036 (name)
2037 Lisp_Object name;
2039 register Lisp_Object val;
2040 register struct Lisp_Symbol *p;
2042 CHECK_STRING (name, 0);
2044 if (symbol_free_list)
2046 XSETSYMBOL (val, symbol_free_list);
2047 symbol_free_list = *(struct Lisp_Symbol **)&symbol_free_list->value;
2049 else
2051 if (symbol_block_index == SYMBOL_BLOCK_SIZE)
2053 struct symbol_block *new;
2054 new = (struct symbol_block *) lisp_malloc (sizeof *new,
2055 MEM_TYPE_SYMBOL);
2056 VALIDATE_LISP_STORAGE (new, sizeof *new);
2057 new->next = symbol_block;
2058 symbol_block = new;
2059 symbol_block_index = 0;
2060 n_symbol_blocks++;
2062 XSETSYMBOL (val, &symbol_block->symbols[symbol_block_index++]);
2065 p = XSYMBOL (val);
2066 p->name = XSTRING (name);
2067 p->obarray = Qnil;
2068 p->plist = Qnil;
2069 p->value = Qunbound;
2070 p->function = Qunbound;
2071 p->next = 0;
2072 consing_since_gc += sizeof (struct Lisp_Symbol);
2073 symbols_consed++;
2074 return val;
2079 /***********************************************************************
2080 Marker (Misc) Allocation
2081 ***********************************************************************/
2083 /* Allocation of markers and other objects that share that structure.
2084 Works like allocation of conses. */
2086 #define MARKER_BLOCK_SIZE \
2087 ((1020 - sizeof (struct marker_block *)) / sizeof (union Lisp_Misc))
2089 struct marker_block
2091 struct marker_block *next;
2092 union Lisp_Misc markers[MARKER_BLOCK_SIZE];
2095 struct marker_block *marker_block;
2096 int marker_block_index;
2098 union Lisp_Misc *marker_free_list;
2100 /* Total number of marker blocks now in use. */
2102 int n_marker_blocks;
2104 void
2105 init_marker ()
2107 marker_block = (struct marker_block *) lisp_malloc (sizeof *marker_block,
2108 MEM_TYPE_MISC);
2109 marker_block->next = 0;
2110 bzero ((char *) marker_block->markers, sizeof marker_block->markers);
2111 marker_block_index = 0;
2112 marker_free_list = 0;
2113 n_marker_blocks = 1;
2116 /* Return a newly allocated Lisp_Misc object, with no substructure. */
2118 Lisp_Object
2119 allocate_misc ()
2121 Lisp_Object val;
2123 if (marker_free_list)
2125 XSETMISC (val, marker_free_list);
2126 marker_free_list = marker_free_list->u_free.chain;
2128 else
2130 if (marker_block_index == MARKER_BLOCK_SIZE)
2132 struct marker_block *new;
2133 new = (struct marker_block *) lisp_malloc (sizeof *new,
2134 MEM_TYPE_MISC);
2135 VALIDATE_LISP_STORAGE (new, sizeof *new);
2136 new->next = marker_block;
2137 marker_block = new;
2138 marker_block_index = 0;
2139 n_marker_blocks++;
2141 XSETMISC (val, &marker_block->markers[marker_block_index++]);
2144 consing_since_gc += sizeof (union Lisp_Misc);
2145 misc_objects_consed++;
2146 return val;
2149 DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
2150 "Return a newly allocated marker which does not point at any place.")
2153 register Lisp_Object val;
2154 register struct Lisp_Marker *p;
2156 val = allocate_misc ();
2157 XMISCTYPE (val) = Lisp_Misc_Marker;
2158 p = XMARKER (val);
2159 p->buffer = 0;
2160 p->bytepos = 0;
2161 p->charpos = 0;
2162 p->chain = Qnil;
2163 p->insertion_type = 0;
2164 return val;
2167 /* Put MARKER back on the free list after using it temporarily. */
2169 void
2170 free_marker (marker)
2171 Lisp_Object marker;
2173 unchain_marker (marker);
2175 XMISC (marker)->u_marker.type = Lisp_Misc_Free;
2176 XMISC (marker)->u_free.chain = marker_free_list;
2177 marker_free_list = XMISC (marker);
2179 total_free_markers++;
2183 /* Return a newly created vector or string with specified arguments as
2184 elements. If all the arguments are characters that can fit
2185 in a string of events, make a string; otherwise, make a vector.
2187 Any number of arguments, even zero arguments, are allowed. */
2189 Lisp_Object
2190 make_event_array (nargs, args)
2191 register int nargs;
2192 Lisp_Object *args;
2194 int i;
2196 for (i = 0; i < nargs; i++)
2197 /* The things that fit in a string
2198 are characters that are in 0...127,
2199 after discarding the meta bit and all the bits above it. */
2200 if (!INTEGERP (args[i])
2201 || (XUINT (args[i]) & ~(-CHAR_META)) >= 0200)
2202 return Fvector (nargs, args);
2204 /* Since the loop exited, we know that all the things in it are
2205 characters, so we can make a string. */
2207 Lisp_Object result;
2209 result = Fmake_string (make_number (nargs), make_number (0));
2210 for (i = 0; i < nargs; i++)
2212 XSTRING (result)->data[i] = XINT (args[i]);
2213 /* Move the meta bit to the right place for a string char. */
2214 if (XINT (args[i]) & CHAR_META)
2215 XSTRING (result)->data[i] |= 0x80;
2218 return result;
2224 /************************************************************************
2225 C Stack Marking
2226 ************************************************************************/
2228 #if GC_MARK_STACK
2231 /* Base address of stack. Set in main. */
2233 Lisp_Object *stack_base;
2235 /* A node in the red-black tree describing allocated memory containing
2236 Lisp data. Each such block is recorded with its start and end
2237 address when it is allocated, and removed from the tree when it
2238 is freed.
2240 A red-black tree is a balanced binary tree with the following
2241 properties:
2243 1. Every node is either red or black.
2244 2. Every leaf is black.
2245 3. If a node is red, then both of its children are black.
2246 4. Every simple path from a node to a descendant leaf contains
2247 the same number of black nodes.
2248 5. The root is always black.
2250 When nodes are inserted into the tree, or deleted from the tree,
2251 the tree is "fixed" so that these properties are always true.
2253 A red-black tree with N internal nodes has height at most 2
2254 log(N+1). Searches, insertions and deletions are done in O(log N).
2255 Please see a text book about data structures for a detailed
2256 description of red-black trees. Any book worth its salt should
2257 describe them. */
2259 struct mem_node
2261 struct mem_node *left, *right, *parent;
2263 /* Start and end of allocated region. */
2264 void *start, *end;
2266 /* Node color. */
2267 enum {MEM_BLACK, MEM_RED} color;
2269 /* Memory type. */
2270 enum mem_type type;
2273 /* Root of the tree describing allocated Lisp memory. */
2275 static struct mem_node *mem_root;
2277 /* Sentinel node of the tree. */
2279 static struct mem_node mem_z;
2280 #define MEM_NIL &mem_z
2283 /* Initialize this part of alloc.c. */
2285 static void
2286 mem_init ()
2288 mem_z.left = mem_z.right = MEM_NIL;
2289 mem_z.parent = NULL;
2290 mem_z.color = MEM_BLACK;
2291 mem_z.start = mem_z.end = NULL;
2292 mem_root = MEM_NIL;
2296 /* Value is a pointer to the mem_node containing START. Value is
2297 MEM_NIL if there is no node in the tree containing START. */
2299 static INLINE struct mem_node *
2300 mem_find (start)
2301 void *start;
2303 struct mem_node *p;
2305 /* Make the search always successful to speed up the loop below. */
2306 mem_z.start = start;
2307 mem_z.end = (char *) start + 1;
2309 p = mem_root;
2310 while (start < p->start || start >= p->end)
2311 p = start < p->start ? p->left : p->right;
2312 return p;
2316 /* Insert a new node into the tree for a block of memory with start
2317 address START, end address END, and type TYPE. Value is a
2318 pointer to the node that was inserted. */
2320 static struct mem_node *
2321 mem_insert (start, end, type)
2322 void *start, *end;
2323 enum mem_type type;
2325 struct mem_node *c, *parent, *x;
2327 /* See where in the tree a node for START belongs. In this
2328 particular application, it shouldn't happen that a node is already
2329 present. For debugging purposes, let's check that. */
2330 c = mem_root;
2331 parent = NULL;
2333 #if GC_MARK_STACK != GC_MAKE_GCPROS_NOOPS
2335 while (c != MEM_NIL)
2337 if (start >= c->start && start < c->end)
2338 abort ();
2339 parent = c;
2340 c = start < c->start ? c->left : c->right;
2343 #else /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
2345 while (c != MEM_NIL)
2347 parent = c;
2348 c = start < c->start ? c->left : c->right;
2351 #endif /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
2353 /* Create a new node. */
2354 x = (struct mem_node *) xmalloc (sizeof *x);
2355 x->start = start;
2356 x->end = end;
2357 x->type = type;
2358 x->parent = parent;
2359 x->left = x->right = MEM_NIL;
2360 x->color = MEM_RED;
2362 /* Insert it as child of PARENT or install it as root. */
2363 if (parent)
2365 if (start < parent->start)
2366 parent->left = x;
2367 else
2368 parent->right = x;
2370 else
2371 mem_root = x;
2373 /* Re-establish red-black tree properties. */
2374 mem_insert_fixup (x);
2375 return x;
2379 /* Re-establish the red-black properties of the tree, and thereby
2380 balance the tree, after node X has been inserted; X is always red. */
2382 static void
2383 mem_insert_fixup (x)
2384 struct mem_node *x;
2386 while (x != mem_root && x->parent->color == MEM_RED)
2388 /* X is red and its parent is red. This is a violation of
2389 red-black tree property #3. */
2391 if (x->parent == x->parent->parent->left)
2393 /* We're on the left side of our grandparent, and Y is our
2394 "uncle". */
2395 struct mem_node *y = x->parent->parent->right;
2397 if (y->color == MEM_RED)
2399 /* Uncle and parent are red but should be black because
2400 X is red. Change the colors accordingly and proceed
2401 with the grandparent. */
2402 x->parent->color = MEM_BLACK;
2403 y->color = MEM_BLACK;
2404 x->parent->parent->color = MEM_RED;
2405 x = x->parent->parent;
2407 else
2409 /* Parent and uncle have different colors; parent is
2410 red, uncle is black. */
2411 if (x == x->parent->right)
2413 x = x->parent;
2414 mem_rotate_left (x);
2417 x->parent->color = MEM_BLACK;
2418 x->parent->parent->color = MEM_RED;
2419 mem_rotate_right (x->parent->parent);
2422 else
2424 /* This is the symmetrical case of above. */
2425 struct mem_node *y = x->parent->parent->left;
2427 if (y->color == MEM_RED)
2429 x->parent->color = MEM_BLACK;
2430 y->color = MEM_BLACK;
2431 x->parent->parent->color = MEM_RED;
2432 x = x->parent->parent;
2434 else
2436 if (x == x->parent->left)
2438 x = x->parent;
2439 mem_rotate_right (x);
2442 x->parent->color = MEM_BLACK;
2443 x->parent->parent->color = MEM_RED;
2444 mem_rotate_left (x->parent->parent);
2449 /* The root may have been changed to red due to the algorithm. Set
2450 it to black so that property #5 is satisfied. */
2451 mem_root->color = MEM_BLACK;
2455 /* (x) (y)
2456 / \ / \
2457 a (y) ===> (x) c
2458 / \ / \
2459 b c a b */
2461 static void
2462 mem_rotate_left (x)
2463 struct mem_node *x;
2465 struct mem_node *y;
2467 /* Turn y's left sub-tree into x's right sub-tree. */
2468 y = x->right;
2469 x->right = y->left;
2470 if (y->left != MEM_NIL)
2471 y->left->parent = x;
2473 /* Y's parent was x's parent. */
2474 if (y != MEM_NIL)
2475 y->parent = x->parent;
2477 /* Get the parent to point to y instead of x. */
2478 if (x->parent)
2480 if (x == x->parent->left)
2481 x->parent->left = y;
2482 else
2483 x->parent->right = y;
2485 else
2486 mem_root = y;
2488 /* Put x on y's left. */
2489 y->left = x;
2490 if (x != MEM_NIL)
2491 x->parent = y;
2495 /* (x) (Y)
2496 / \ / \
2497 (y) c ===> a (x)
2498 / \ / \
2499 a b b c */
2501 static void
2502 mem_rotate_right (x)
2503 struct mem_node *x;
2505 struct mem_node *y = x->left;
2507 x->left = y->right;
2508 if (y->right != MEM_NIL)
2509 y->right->parent = x;
2511 if (y != MEM_NIL)
2512 y->parent = x->parent;
2513 if (x->parent)
2515 if (x == x->parent->right)
2516 x->parent->right = y;
2517 else
2518 x->parent->left = y;
2520 else
2521 mem_root = y;
2523 y->right = x;
2524 if (x != MEM_NIL)
2525 x->parent = y;
2529 /* Delete node Z from the tree. If Z is null or MEM_NIL, do nothing. */
2531 static void
2532 mem_delete (z)
2533 struct mem_node *z;
2535 struct mem_node *x, *y;
2537 if (!z || z == MEM_NIL)
2538 return;
2540 if (z->left == MEM_NIL || z->right == MEM_NIL)
2541 y = z;
2542 else
2544 y = z->right;
2545 while (y->left != MEM_NIL)
2546 y = y->left;
2549 if (y->left != MEM_NIL)
2550 x = y->left;
2551 else
2552 x = y->right;
2554 x->parent = y->parent;
2555 if (y->parent)
2557 if (y == y->parent->left)
2558 y->parent->left = x;
2559 else
2560 y->parent->right = x;
2562 else
2563 mem_root = x;
2565 if (y != z)
2567 z->start = y->start;
2568 z->end = y->end;
2569 z->type = y->type;
2572 if (y->color == MEM_BLACK)
2573 mem_delete_fixup (x);
2574 xfree (y);
2578 /* Re-establish the red-black properties of the tree, after a
2579 deletion. */
2581 static void
2582 mem_delete_fixup (x)
2583 struct mem_node *x;
2585 while (x != mem_root && x->color == MEM_BLACK)
2587 if (x == x->parent->left)
2589 struct mem_node *w = x->parent->right;
2591 if (w->color == MEM_RED)
2593 w->color = MEM_BLACK;
2594 x->parent->color = MEM_RED;
2595 mem_rotate_left (x->parent);
2596 w = x->parent->right;
2599 if (w->left->color == MEM_BLACK && w->right->color == MEM_BLACK)
2601 w->color = MEM_RED;
2602 x = x->parent;
2604 else
2606 if (w->right->color == MEM_BLACK)
2608 w->left->color = MEM_BLACK;
2609 w->color = MEM_RED;
2610 mem_rotate_right (w);
2611 w = x->parent->right;
2613 w->color = x->parent->color;
2614 x->parent->color = MEM_BLACK;
2615 w->right->color = MEM_BLACK;
2616 mem_rotate_left (x->parent);
2617 x = mem_root;
2620 else
2622 struct mem_node *w = x->parent->left;
2624 if (w->color == MEM_RED)
2626 w->color = MEM_BLACK;
2627 x->parent->color = MEM_RED;
2628 mem_rotate_right (x->parent);
2629 w = x->parent->left;
2632 if (w->right->color == MEM_BLACK && w->left->color == MEM_BLACK)
2634 w->color = MEM_RED;
2635 x = x->parent;
2637 else
2639 if (w->left->color == MEM_BLACK)
2641 w->right->color = MEM_BLACK;
2642 w->color = MEM_RED;
2643 mem_rotate_left (w);
2644 w = x->parent->left;
2647 w->color = x->parent->color;
2648 x->parent->color = MEM_BLACK;
2649 w->left->color = MEM_BLACK;
2650 mem_rotate_right (x->parent);
2651 x = mem_root;
2656 x->color = MEM_BLACK;
2660 /* Value is non-zero if P is a pointer to a live Lisp string on
2661 the heap. M is a pointer to the mem_block for P. */
2663 static INLINE int
2664 live_string_p (m, p)
2665 struct mem_node *m;
2666 void *p;
2668 if (m->type == MEM_TYPE_STRING)
2670 struct string_block *b = (struct string_block *) m->start;
2671 int offset = (char *) p - (char *) &b->strings[0];
2673 /* P must point to the start of a Lisp_String structure, and it
2674 must not be on the free-list. */
2675 return (offset % sizeof b->strings[0] == 0
2676 && ((struct Lisp_String *) p)->data != NULL);
2678 else
2679 return 0;
2683 /* Value is non-zero if P is a pointer to a live Lisp cons on
2684 the heap. M is a pointer to the mem_block for P. */
2686 static INLINE int
2687 live_cons_p (m, p)
2688 struct mem_node *m;
2689 void *p;
2691 if (m->type == MEM_TYPE_CONS)
2693 struct cons_block *b = (struct cons_block *) m->start;
2694 int offset = (char *) p - (char *) &b->conses[0];
2696 /* P must point to the start of a Lisp_Cons, not be
2697 one of the unused cells in the current cons block,
2698 and not be on the free-list. */
2699 return (offset % sizeof b->conses[0] == 0
2700 && (b != cons_block
2701 || offset / sizeof b->conses[0] < cons_block_index)
2702 && !EQ (((struct Lisp_Cons *) p)->car, Vdead));
2704 else
2705 return 0;
2709 /* Value is non-zero if P is a pointer to a live Lisp symbol on
2710 the heap. M is a pointer to the mem_block for P. */
2712 static INLINE int
2713 live_symbol_p (m, p)
2714 struct mem_node *m;
2715 void *p;
2717 if (m->type == MEM_TYPE_SYMBOL)
2719 struct symbol_block *b = (struct symbol_block *) m->start;
2720 int offset = (char *) p - (char *) &b->symbols[0];
2722 /* P must point to the start of a Lisp_Symbol, not be
2723 one of the unused cells in the current symbol block,
2724 and not be on the free-list. */
2725 return (offset % sizeof b->symbols[0] == 0
2726 && (b != symbol_block
2727 || offset / sizeof b->symbols[0] < symbol_block_index)
2728 && !EQ (((struct Lisp_Symbol *) p)->function, Vdead));
2730 else
2731 return 0;
2735 /* Value is non-zero if P is a pointer to a live Lisp float on
2736 the heap. M is a pointer to the mem_block for P. */
2738 static INLINE int
2739 live_float_p (m, p)
2740 struct mem_node *m;
2741 void *p;
2743 if (m->type == MEM_TYPE_FLOAT)
2745 struct float_block *b = (struct float_block *) m->start;
2746 int offset = (char *) p - (char *) &b->floats[0];
2748 /* P must point to the start of a Lisp_Float, not be
2749 one of the unused cells in the current float block,
2750 and not be on the free-list. */
2751 return (offset % sizeof b->floats[0] == 0
2752 && (b != float_block
2753 || offset / sizeof b->floats[0] < float_block_index)
2754 && !EQ (((struct Lisp_Float *) p)->type, Vdead));
2756 else
2757 return 0;
2761 /* Value is non-zero if P is a pointer to a live Lisp Misc on
2762 the heap. M is a pointer to the mem_block for P. */
2764 static INLINE int
2765 live_misc_p (m, p)
2766 struct mem_node *m;
2767 void *p;
2769 if (m->type == MEM_TYPE_MISC)
2771 struct marker_block *b = (struct marker_block *) m->start;
2772 int offset = (char *) p - (char *) &b->markers[0];
2774 /* P must point to the start of a Lisp_Misc, not be
2775 one of the unused cells in the current misc block,
2776 and not be on the free-list. */
2777 return (offset % sizeof b->markers[0] == 0
2778 && (b != marker_block
2779 || offset / sizeof b->markers[0] < marker_block_index)
2780 && ((union Lisp_Misc *) p)->u_marker.type != Lisp_Misc_Free);
2782 else
2783 return 0;
2787 /* Value is non-zero if P is a pointer to a live vector-like object.
2788 M is a pointer to the mem_block for P. */
2790 static INLINE int
2791 live_vector_p (m, p)
2792 struct mem_node *m;
2793 void *p;
2795 return m->type == MEM_TYPE_VECTOR && p == m->start;
2799 /* Value is non-zero of P is a pointer to a live buffer. M is a
2800 pointer to the mem_block for P. */
2802 static INLINE int
2803 live_buffer_p (m, p)
2804 struct mem_node *m;
2805 void *p;
2807 /* P must point to the start of the block, and the buffer
2808 must not have been killed. */
2809 return (m->type == MEM_TYPE_BUFFER
2810 && p == m->start
2811 && !NILP (((struct buffer *) p)->name));
2815 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
2817 /* Array of objects that are kept alive because the C stack contains
2818 a pattern that looks like a reference to them . */
2820 #define MAX_ZOMBIES 10
2821 static Lisp_Object zombies[MAX_ZOMBIES];
2823 /* Number of zombie objects. */
2825 static int nzombies;
2827 /* Number of garbage collections. */
2829 static int ngcs;
2831 /* Average percentage of zombies per collection. */
2833 static double avg_zombies;
2835 /* Max. number of live and zombie objects. */
2837 static int max_live, max_zombies;
2839 /* Average number of live objects per GC. */
2841 static double avg_live;
2843 DEFUN ("gc-status", Fgc_status, Sgc_status, 0, 0, "",
2844 "Show information about live and zombie objects.")
2847 Lisp_Object args[7];
2848 args[0] = build_string ("%d GCs, avg live/zombies = %.2f/%.2f (%f%%), max %d/%d");
2849 args[1] = make_number (ngcs);
2850 args[2] = make_float (avg_live);
2851 args[3] = make_float (avg_zombies);
2852 args[4] = make_float (avg_zombies / avg_live / 100);
2853 args[5] = make_number (max_live);
2854 args[6] = make_number (max_zombies);
2855 return Fmessage (7, args);
2858 #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
2861 /* Mark OBJ if we can prove it's a Lisp_Object. */
2863 static INLINE void
2864 mark_maybe_object (obj)
2865 Lisp_Object obj;
2867 void *po = (void *) XPNTR (obj);
2868 struct mem_node *m = mem_find (po);
2870 if (m != MEM_NIL)
2872 int mark_p = 0;
2874 switch (XGCTYPE (obj))
2876 case Lisp_String:
2877 mark_p = (live_string_p (m, po)
2878 && !STRING_MARKED_P ((struct Lisp_String *) po));
2879 break;
2881 case Lisp_Cons:
2882 mark_p = (live_cons_p (m, po)
2883 && !XMARKBIT (XCONS (obj)->car));
2884 break;
2886 case Lisp_Symbol:
2887 mark_p = (live_symbol_p (m, po)
2888 && !XMARKBIT (XSYMBOL (obj)->plist));
2889 break;
2891 case Lisp_Float:
2892 mark_p = (live_float_p (m, po)
2893 && !XMARKBIT (XFLOAT (obj)->type));
2894 break;
2896 case Lisp_Vectorlike:
2897 /* Note: can't check GC_BUFFERP before we know it's a
2898 buffer because checking that dereferences the pointer
2899 PO which might point anywhere. */
2900 if (live_vector_p (m, po))
2901 mark_p = (!GC_SUBRP (obj)
2902 && !(XVECTOR (obj)->size & ARRAY_MARK_FLAG));
2903 else if (live_buffer_p (m, po))
2904 mark_p = GC_BUFFERP (obj) && !XMARKBIT (XBUFFER (obj)->name);
2905 break;
2907 case Lisp_Misc:
2908 if (live_misc_p (m, po))
2910 switch (XMISCTYPE (obj))
2912 case Lisp_Misc_Marker:
2913 mark_p = !XMARKBIT (XMARKER (obj)->chain);
2914 break;
2916 case Lisp_Misc_Buffer_Local_Value:
2917 case Lisp_Misc_Some_Buffer_Local_Value:
2918 mark_p = !XMARKBIT (XBUFFER_LOCAL_VALUE (obj)->realvalue);
2919 break;
2921 case Lisp_Misc_Overlay:
2922 mark_p = !XMARKBIT (XOVERLAY (obj)->plist);
2923 break;
2926 break;
2929 if (mark_p)
2931 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
2932 if (nzombies < MAX_ZOMBIES)
2933 zombies[nzombies] = *p;
2934 ++nzombies;
2935 #endif
2936 mark_object (&obj);
2941 /* Mark Lisp objects in the address range START..END. */
2943 static void
2944 mark_memory (start, end)
2945 void *start, *end;
2947 Lisp_Object *p;
2949 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
2950 nzombies = 0;
2951 #endif
2953 /* Make START the pointer to the start of the memory region,
2954 if it isn't already. */
2955 if (end < start)
2957 void *tem = start;
2958 start = end;
2959 end = tem;
2962 for (p = (Lisp_Object *) start; (void *) p < end; ++p)
2963 mark_maybe_object (*p);
2967 #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
2969 static int setjmp_tested_p, longjmps_done;
2971 #define SETJMP_WILL_LIKELY_WORK "\
2973 Emacs garbage collector has been changed to use conservative stack\n\
2974 marking. Emacs has determined that the method it uses to do the\n\
2975 marking will likely work on your system, but this isn't sure.\n\
2977 If you are a system-programmer, or can get the help of a local wizard\n\
2978 who is, please take a look at the function mark_stack in alloc.c, and\n\
2979 verify that the methods used are appropriate for your system.\n\
2981 Please mail the result to <gerd@gnu.org>.\n\
2984 #define SETJMP_WILL_NOT_WORK "\
2986 Emacs garbage collector has been changed to use conservative stack\n\
2987 marking. Emacs has determined that the default method it uses to do the\n\
2988 marking will not work on your system. We will need a system-dependent\n\
2989 solution for your system.\n\
2991 Please take a look at the function mark_stack in alloc.c, and\n\
2992 try to find a way to make it work on your system.\n\
2993 Please mail the result to <gerd@gnu.org>.\n\
2997 /* Perform a quick check if it looks like setjmp saves registers in a
2998 jmp_buf. Print a message to stderr saying so. When this test
2999 succeeds, this is _not_ a proof that setjmp is sufficient for
3000 conservative stack marking. Only the sources or a disassembly
3001 can prove that. */
3003 static void
3004 test_setjmp ()
3006 char buf[10];
3007 register int x;
3008 jmp_buf jbuf;
3009 int result = 0;
3011 /* Arrange for X to be put in a register. */
3012 sprintf (buf, "1");
3013 x = strlen (buf);
3014 x = 2 * x - 1;
3016 setjmp (jbuf);
3017 if (longjmps_done == 1)
3019 /* Came here after the longjmp at the end of the function.
3021 If x == 1, the longjmp has restored the register to its
3022 value before the setjmp, and we can hope that setjmp
3023 saves all such registers in the jmp_buf, although that
3024 isn't sure.
3026 For other values of X, either something really strange is
3027 taking place, or the setjmp just didn't save the register. */
3029 if (x == 1)
3030 fprintf (stderr, SETJMP_WILL_LIKELY_WORK);
3031 else
3033 fprintf (stderr, SETJMP_WILL_NOT_WORK);
3034 exit (1);
3038 ++longjmps_done;
3039 x = 2;
3040 if (longjmps_done == 1)
3041 longjmp (jbuf, 1);
3044 #endif /* not GC_SAVE_REGISTERS_ON_STACK && not GC_SETJMP_WORKS */
3047 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
3049 /* Abort if anything GCPRO'd doesn't survive the GC. */
3051 static void
3052 check_gcpros ()
3054 struct gcpro *p;
3055 int i;
3057 for (p = gcprolist; p; p = p->next)
3058 for (i = 0; i < p->nvars; ++i)
3059 if (!survives_gc_p (p->var[i]))
3060 abort ();
3063 #elif GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3065 static void
3066 dump_zombies ()
3068 int i;
3070 fprintf (stderr, "\nZombies kept alive = %d:\n", nzombies);
3071 for (i = 0; i < min (MAX_ZOMBIES, nzombies); ++i)
3073 fprintf (stderr, " %d = ", i);
3074 debug_print (zombies[i]);
3078 #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
3081 /* Mark live Lisp objects on the C stack.
3083 There are several system-dependent problems to consider when
3084 porting this to new architectures:
3086 Processor Registers
3088 We have to mark Lisp objects in CPU registers that can hold local
3089 variables or are used to pass parameters.
3091 If GC_SAVE_REGISTERS_ON_STACK is defined, it should expand to
3092 something that either saves relevant registers on the stack, or
3093 calls mark_maybe_object passing it each register's contents.
3095 If GC_SAVE_REGISTERS_ON_STACK is not defined, the current
3096 implementation assumes that calling setjmp saves registers we need
3097 to see in a jmp_buf which itself lies on the stack. This doesn't
3098 have to be true! It must be verified for each system, possibly
3099 by taking a look at the source code of setjmp.
3101 Stack Layout
3103 Architectures differ in the way their processor stack is organized.
3104 For example, the stack might look like this
3106 +----------------+
3107 | Lisp_Object | size = 4
3108 +----------------+
3109 | something else | size = 2
3110 +----------------+
3111 | Lisp_Object | size = 4
3112 +----------------+
3113 | ... |
3115 In such a case, not every Lisp_Object will be aligned equally. To
3116 find all Lisp_Object on the stack it won't be sufficient to walk
3117 the stack in steps of 4 bytes. Instead, two passes will be
3118 necessary, one starting at the start of the stack, and a second
3119 pass starting at the start of the stack + 2. Likewise, if the
3120 minimal alignment of Lisp_Objects on the stack is 1, four passes
3121 would be necessary, each one starting with one byte more offset
3122 from the stack start.
3124 The current code assumes by default that Lisp_Objects are aligned
3125 equally on the stack. */
3127 static void
3128 mark_stack ()
3130 jmp_buf j;
3131 int stack_grows_down_p = (char *) &j > (char *) stack_base;
3132 void *end;
3134 /* This trick flushes the register windows so that all the state of
3135 the process is contained in the stack. */
3136 #ifdef sparc
3137 asm ("ta 3");
3138 #endif
3140 /* Save registers that we need to see on the stack. We need to see
3141 registers used to hold register variables and registers used to
3142 pass parameters. */
3143 #ifdef GC_SAVE_REGISTERS_ON_STACK
3144 GC_SAVE_REGISTERS_ON_STACK (end);
3145 #else /* not GC_SAVE_REGISTERS_ON_STACK */
3147 #ifndef GC_SETJMP_WORKS /* If it hasn't been checked yet that
3148 setjmp will definitely work, test it
3149 and print a message with the result
3150 of the test. */
3151 if (!setjmp_tested_p)
3153 setjmp_tested_p = 1;
3154 test_setjmp ();
3156 #endif /* GC_SETJMP_WORKS */
3158 setjmp (j);
3159 end = stack_grows_down_p ? (char *) &j + sizeof j : (char *) &j;
3160 #endif /* not GC_SAVE_REGISTERS_ON_STACK */
3162 /* This assumes that the stack is a contiguous region in memory. If
3163 that's not the case, something has to be done here to iterate
3164 over the stack segments. */
3165 #if GC_LISP_OBJECT_ALIGNMENT == 1
3166 mark_memory (stack_base, end);
3167 mark_memory ((char *) stack_base + 1, end);
3168 mark_memory ((char *) stack_base + 2, end);
3169 mark_memory ((char *) stack_base + 3, end);
3170 #elif GC_LISP_OBJECT_ALIGNMENT == 2
3171 mark_memory (stack_base, end);
3172 mark_memory ((char *) stack_base + 2, end);
3173 #else
3174 mark_memory (stack_base, end);
3175 #endif
3177 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
3178 check_gcpros ();
3179 #endif
3183 #endif /* GC_MARK_STACK != 0 */
3187 /***********************************************************************
3188 Pure Storage Management
3189 ***********************************************************************/
3191 /* Return a string allocated in pure space. DATA is a buffer holding
3192 NCHARS characters, and NBYTES bytes of string data. MULTIBYTE
3193 non-zero means make the result string multibyte.
3195 Must get an error if pure storage is full, since if it cannot hold
3196 a large string it may be able to hold conses that point to that
3197 string; then the string is not protected from gc. */
3199 Lisp_Object
3200 make_pure_string (data, nchars, nbytes, multibyte)
3201 char *data;
3202 int nchars, nbytes;
3203 int multibyte;
3205 Lisp_Object string;
3206 struct Lisp_String *s;
3207 int string_size, data_size;
3209 #define PAD(SZ) (((SZ) + sizeof (EMACS_INT) - 1) & ~(sizeof (EMACS_INT) - 1))
3211 string_size = PAD (sizeof (struct Lisp_String));
3212 data_size = PAD (nbytes + 1);
3214 #undef PAD
3216 if (pureptr + string_size + data_size > PURESIZE)
3217 error ("Pure Lisp storage exhausted");
3219 s = (struct Lisp_String *) (PUREBEG + pureptr);
3220 pureptr += string_size;
3221 s->data = (unsigned char *) (PUREBEG + pureptr);
3222 pureptr += data_size;
3224 s->size = nchars;
3225 s->size_byte = multibyte ? nbytes : -1;
3226 bcopy (data, s->data, nbytes);
3227 s->data[nbytes] = '\0';
3228 s->intervals = NULL_INTERVAL;
3230 XSETSTRING (string, s);
3231 return string;
3235 /* Return a cons allocated from pure space. Give it pure copies
3236 of CAR as car and CDR as cdr. */
3238 Lisp_Object
3239 pure_cons (car, cdr)
3240 Lisp_Object car, cdr;
3242 register Lisp_Object new;
3244 if (pureptr + sizeof (struct Lisp_Cons) > PURESIZE)
3245 error ("Pure Lisp storage exhausted");
3246 XSETCONS (new, PUREBEG + pureptr);
3247 pureptr += sizeof (struct Lisp_Cons);
3248 XCAR (new) = Fpurecopy (car);
3249 XCDR (new) = Fpurecopy (cdr);
3250 return new;
3254 /* Value is a float object with value NUM allocated from pure space. */
3256 Lisp_Object
3257 make_pure_float (num)
3258 double num;
3260 register Lisp_Object new;
3262 /* Make sure that PUREBEG + pureptr is aligned on at least a sizeof
3263 (double) boundary. Some architectures (like the sparc) require
3264 this, and I suspect that floats are rare enough that it's no
3265 tragedy for those that do. */
3267 size_t alignment;
3268 char *p = PUREBEG + pureptr;
3270 #ifdef __GNUC__
3271 #if __GNUC__ >= 2
3272 alignment = __alignof (struct Lisp_Float);
3273 #else
3274 alignment = sizeof (struct Lisp_Float);
3275 #endif
3276 #else
3277 alignment = sizeof (struct Lisp_Float);
3278 #endif
3279 p = (char *) (((unsigned long) p + alignment - 1) & - alignment);
3280 pureptr = p - PUREBEG;
3283 if (pureptr + sizeof (struct Lisp_Float) > PURESIZE)
3284 error ("Pure Lisp storage exhausted");
3285 XSETFLOAT (new, PUREBEG + pureptr);
3286 pureptr += sizeof (struct Lisp_Float);
3287 XFLOAT_DATA (new) = num;
3288 XSETFASTINT (XFLOAT (new)->type, 0); /* bug chasing -wsr */
3289 return new;
3293 /* Return a vector with room for LEN Lisp_Objects allocated from
3294 pure space. */
3296 Lisp_Object
3297 make_pure_vector (len)
3298 EMACS_INT len;
3300 register Lisp_Object new;
3301 register EMACS_INT size = (sizeof (struct Lisp_Vector)
3302 + (len - 1) * sizeof (Lisp_Object));
3304 if (pureptr + size > PURESIZE)
3305 error ("Pure Lisp storage exhausted");
3307 XSETVECTOR (new, PUREBEG + pureptr);
3308 pureptr += size;
3309 XVECTOR (new)->size = len;
3310 return new;
3314 DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
3315 "Make a copy of OBJECT in pure storage.\n\
3316 Recursively copies contents of vectors and cons cells.\n\
3317 Does not copy symbols. Copies strings without text properties.")
3318 (obj)
3319 register Lisp_Object obj;
3321 if (NILP (Vpurify_flag))
3322 return obj;
3324 if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
3325 && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
3326 return obj;
3328 if (CONSP (obj))
3329 return pure_cons (XCAR (obj), XCDR (obj));
3330 else if (FLOATP (obj))
3331 return make_pure_float (XFLOAT_DATA (obj));
3332 else if (STRINGP (obj))
3333 return make_pure_string (XSTRING (obj)->data, XSTRING (obj)->size,
3334 STRING_BYTES (XSTRING (obj)),
3335 STRING_MULTIBYTE (obj));
3336 else if (COMPILEDP (obj) || VECTORP (obj))
3338 register struct Lisp_Vector *vec;
3339 register int i, size;
3341 size = XVECTOR (obj)->size;
3342 if (size & PSEUDOVECTOR_FLAG)
3343 size &= PSEUDOVECTOR_SIZE_MASK;
3344 vec = XVECTOR (make_pure_vector ((EMACS_INT) size));
3345 for (i = 0; i < size; i++)
3346 vec->contents[i] = Fpurecopy (XVECTOR (obj)->contents[i]);
3347 if (COMPILEDP (obj))
3348 XSETCOMPILED (obj, vec);
3349 else
3350 XSETVECTOR (obj, vec);
3351 return obj;
3353 else if (MARKERP (obj))
3354 error ("Attempt to copy a marker to pure storage");
3355 else
3356 return obj;
3361 /***********************************************************************
3362 Protection from GC
3363 ***********************************************************************/
3365 /* Recording what needs to be marked for gc. */
3367 struct gcpro *gcprolist;
3369 /* Addresses of staticpro'd variables. */
3371 #define NSTATICS 1024
3372 Lisp_Object *staticvec[NSTATICS] = {0};
3374 /* Index of next unused slot in staticvec. */
3376 int staticidx = 0;
3379 /* Put an entry in staticvec, pointing at the variable with address
3380 VARADDRESS. */
3382 void
3383 staticpro (varaddress)
3384 Lisp_Object *varaddress;
3386 staticvec[staticidx++] = varaddress;
3387 if (staticidx >= NSTATICS)
3388 abort ();
3391 struct catchtag
3393 Lisp_Object tag;
3394 Lisp_Object val;
3395 struct catchtag *next;
3398 struct backtrace
3400 struct backtrace *next;
3401 Lisp_Object *function;
3402 Lisp_Object *args; /* Points to vector of args. */
3403 int nargs; /* Length of vector. */
3404 /* If nargs is UNEVALLED, args points to slot holding list of
3405 unevalled args. */
3406 char evalargs;
3411 /***********************************************************************
3412 Protection from GC
3413 ***********************************************************************/
3415 /* Temporarily prevent garbage collection. */
3418 inhibit_garbage_collection ()
3420 int count = specpdl_ptr - specpdl;
3421 Lisp_Object number;
3422 int nbits = min (VALBITS, BITS_PER_INT);
3424 XSETINT (number, ((EMACS_INT) 1 << (nbits - 1)) - 1);
3426 specbind (Qgc_cons_threshold, number);
3428 return count;
3432 DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
3433 "Reclaim storage for Lisp objects no longer needed.\n\
3434 Returns info on amount of space in use:\n\
3435 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)\n\
3436 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS\n\
3437 (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS\n\
3438 (USED-STRINGS . FREE-STRINGS))\n\
3439 Garbage collection happens automatically if you cons more than\n\
3440 `gc-cons-threshold' bytes of Lisp data since previous garbage collection.")
3443 register struct gcpro *tail;
3444 register struct specbinding *bind;
3445 struct catchtag *catch;
3446 struct handler *handler;
3447 register struct backtrace *backlist;
3448 char stack_top_variable;
3449 register int i;
3450 int message_p;
3451 Lisp_Object total[7];
3453 /* In case user calls debug_print during GC,
3454 don't let that cause a recursive GC. */
3455 consing_since_gc = 0;
3457 /* Save what's currently displayed in the echo area. */
3458 message_p = push_message ();
3460 /* Save a copy of the contents of the stack, for debugging. */
3461 #if MAX_SAVE_STACK > 0
3462 if (NILP (Vpurify_flag))
3464 i = &stack_top_variable - stack_bottom;
3465 if (i < 0) i = -i;
3466 if (i < MAX_SAVE_STACK)
3468 if (stack_copy == 0)
3469 stack_copy = (char *) xmalloc (stack_copy_size = i);
3470 else if (stack_copy_size < i)
3471 stack_copy = (char *) xrealloc (stack_copy, (stack_copy_size = i));
3472 if (stack_copy)
3474 if ((EMACS_INT) (&stack_top_variable - stack_bottom) > 0)
3475 bcopy (stack_bottom, stack_copy, i);
3476 else
3477 bcopy (&stack_top_variable, stack_copy, i);
3481 #endif /* MAX_SAVE_STACK > 0 */
3483 if (garbage_collection_messages)
3484 message1_nolog ("Garbage collecting...");
3486 BLOCK_INPUT;
3488 shrink_regexp_cache ();
3490 /* Don't keep undo information around forever. */
3492 register struct buffer *nextb = all_buffers;
3494 while (nextb)
3496 /* If a buffer's undo list is Qt, that means that undo is
3497 turned off in that buffer. Calling truncate_undo_list on
3498 Qt tends to return NULL, which effectively turns undo back on.
3499 So don't call truncate_undo_list if undo_list is Qt. */
3500 if (! EQ (nextb->undo_list, Qt))
3501 nextb->undo_list
3502 = truncate_undo_list (nextb->undo_list, undo_limit,
3503 undo_strong_limit);
3504 nextb = nextb->next;
3508 gc_in_progress = 1;
3510 /* clear_marks (); */
3512 /* Mark all the special slots that serve as the roots of accessibility.
3514 Usually the special slots to mark are contained in particular structures.
3515 Then we know no slot is marked twice because the structures don't overlap.
3516 In some cases, the structures point to the slots to be marked.
3517 For these, we use MARKBIT to avoid double marking of the slot. */
3519 for (i = 0; i < staticidx; i++)
3520 mark_object (staticvec[i]);
3522 #if (GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS \
3523 || GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS)
3524 mark_stack ();
3525 #else
3526 for (tail = gcprolist; tail; tail = tail->next)
3527 for (i = 0; i < tail->nvars; i++)
3528 if (!XMARKBIT (tail->var[i]))
3530 mark_object (&tail->var[i]);
3531 XMARK (tail->var[i]);
3533 #endif
3535 mark_byte_stack ();
3536 for (bind = specpdl; bind != specpdl_ptr; bind++)
3538 mark_object (&bind->symbol);
3539 mark_object (&bind->old_value);
3541 for (catch = catchlist; catch; catch = catch->next)
3543 mark_object (&catch->tag);
3544 mark_object (&catch->val);
3546 for (handler = handlerlist; handler; handler = handler->next)
3548 mark_object (&handler->handler);
3549 mark_object (&handler->var);
3551 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3553 if (!XMARKBIT (*backlist->function))
3555 mark_object (backlist->function);
3556 XMARK (*backlist->function);
3558 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
3559 i = 0;
3560 else
3561 i = backlist->nargs - 1;
3562 for (; i >= 0; i--)
3563 if (!XMARKBIT (backlist->args[i]))
3565 mark_object (&backlist->args[i]);
3566 XMARK (backlist->args[i]);
3569 mark_kboards ();
3571 /* Look thru every buffer's undo list
3572 for elements that update markers that were not marked,
3573 and delete them. */
3575 register struct buffer *nextb = all_buffers;
3577 while (nextb)
3579 /* If a buffer's undo list is Qt, that means that undo is
3580 turned off in that buffer. Calling truncate_undo_list on
3581 Qt tends to return NULL, which effectively turns undo back on.
3582 So don't call truncate_undo_list if undo_list is Qt. */
3583 if (! EQ (nextb->undo_list, Qt))
3585 Lisp_Object tail, prev;
3586 tail = nextb->undo_list;
3587 prev = Qnil;
3588 while (CONSP (tail))
3590 if (GC_CONSP (XCAR (tail))
3591 && GC_MARKERP (XCAR (XCAR (tail)))
3592 && ! XMARKBIT (XMARKER (XCAR (XCAR (tail)))->chain))
3594 if (NILP (prev))
3595 nextb->undo_list = tail = XCDR (tail);
3596 else
3597 tail = XCDR (prev) = XCDR (tail);
3599 else
3601 prev = tail;
3602 tail = XCDR (tail);
3607 nextb = nextb->next;
3611 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3612 mark_stack ();
3613 #endif
3615 gc_sweep ();
3617 /* Clear the mark bits that we set in certain root slots. */
3619 #if (GC_MARK_STACK == GC_USE_GCPROS_AS_BEFORE \
3620 || GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES)
3621 for (tail = gcprolist; tail; tail = tail->next)
3622 for (i = 0; i < tail->nvars; i++)
3623 XUNMARK (tail->var[i]);
3624 #endif
3626 unmark_byte_stack ();
3627 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3629 XUNMARK (*backlist->function);
3630 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
3631 i = 0;
3632 else
3633 i = backlist->nargs - 1;
3634 for (; i >= 0; i--)
3635 XUNMARK (backlist->args[i]);
3637 XUNMARK (buffer_defaults.name);
3638 XUNMARK (buffer_local_symbols.name);
3640 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES && 0
3641 dump_zombies ();
3642 #endif
3644 UNBLOCK_INPUT;
3646 /* clear_marks (); */
3647 gc_in_progress = 0;
3649 consing_since_gc = 0;
3650 if (gc_cons_threshold < 10000)
3651 gc_cons_threshold = 10000;
3653 if (garbage_collection_messages)
3655 if (message_p || minibuf_level > 0)
3656 restore_message ();
3657 else
3658 message1_nolog ("Garbage collecting...done");
3661 pop_message ();
3663 total[0] = Fcons (make_number (total_conses),
3664 make_number (total_free_conses));
3665 total[1] = Fcons (make_number (total_symbols),
3666 make_number (total_free_symbols));
3667 total[2] = Fcons (make_number (total_markers),
3668 make_number (total_free_markers));
3669 total[3] = Fcons (make_number (total_string_size),
3670 make_number (total_vector_size));
3671 total[4] = Fcons (make_number (total_floats),
3672 make_number (total_free_floats));
3673 total[5] = Fcons (make_number (total_intervals),
3674 make_number (total_free_intervals));
3675 total[6] = Fcons (make_number (total_strings),
3676 make_number (total_free_strings));
3678 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3680 /* Compute average percentage of zombies. */
3681 double nlive = 0;
3683 for (i = 0; i < 7; ++i)
3684 nlive += XFASTINT (XCAR (total[i]));
3686 avg_live = (avg_live * ngcs + nlive) / (ngcs + 1);
3687 max_live = max (nlive, max_live);
3688 avg_zombies = (avg_zombies * ngcs + nzombies) / (ngcs + 1);
3689 max_zombies = max (nzombies, max_zombies);
3690 ++ngcs;
3692 #endif
3694 return Flist (7, total);
3698 /* Mark Lisp objects in glyph matrix MATRIX. Currently the
3699 only interesting objects referenced from glyphs are strings. */
3701 static void
3702 mark_glyph_matrix (matrix)
3703 struct glyph_matrix *matrix;
3705 struct glyph_row *row = matrix->rows;
3706 struct glyph_row *end = row + matrix->nrows;
3708 for (; row < end; ++row)
3709 if (row->enabled_p)
3711 int area;
3712 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
3714 struct glyph *glyph = row->glyphs[area];
3715 struct glyph *end_glyph = glyph + row->used[area];
3717 for (; glyph < end_glyph; ++glyph)
3718 if (GC_STRINGP (glyph->object)
3719 && !STRING_MARKED_P (XSTRING (glyph->object)))
3720 mark_object (&glyph->object);
3726 /* Mark Lisp faces in the face cache C. */
3728 static void
3729 mark_face_cache (c)
3730 struct face_cache *c;
3732 if (c)
3734 int i, j;
3735 for (i = 0; i < c->used; ++i)
3737 struct face *face = FACE_FROM_ID (c->f, i);
3739 if (face)
3741 for (j = 0; j < LFACE_VECTOR_SIZE; ++j)
3742 mark_object (&face->lface[j]);
3749 #ifdef HAVE_WINDOW_SYSTEM
3751 /* Mark Lisp objects in image IMG. */
3753 static void
3754 mark_image (img)
3755 struct image *img;
3757 mark_object (&img->spec);
3759 if (!NILP (img->data.lisp_val))
3760 mark_object (&img->data.lisp_val);
3764 /* Mark Lisp objects in image cache of frame F. It's done this way so
3765 that we don't have to include xterm.h here. */
3767 static void
3768 mark_image_cache (f)
3769 struct frame *f;
3771 forall_images_in_image_cache (f, mark_image);
3774 #endif /* HAVE_X_WINDOWS */
3778 /* Mark reference to a Lisp_Object.
3779 If the object referred to has not been seen yet, recursively mark
3780 all the references contained in it. */
3782 #define LAST_MARKED_SIZE 500
3783 Lisp_Object *last_marked[LAST_MARKED_SIZE];
3784 int last_marked_index;
3786 void
3787 mark_object (argptr)
3788 Lisp_Object *argptr;
3790 Lisp_Object *objptr = argptr;
3791 register Lisp_Object obj;
3792 #ifdef GC_CHECK_MARKED_OBJECTS
3793 void *po;
3794 struct mem_node *m;
3795 #endif
3797 loop:
3798 obj = *objptr;
3799 loop2:
3800 XUNMARK (obj);
3802 if (PURE_POINTER_P ((PNTR_COMPARISON_TYPE) XPNTR (obj)))
3803 return;
3805 last_marked[last_marked_index++] = objptr;
3806 if (last_marked_index == LAST_MARKED_SIZE)
3807 last_marked_index = 0;
3809 /* Perform some sanity checks on the objects marked here. Abort if
3810 we encounter an object we know is bogus. This increases GC time
3811 by ~80%, and requires compilation with GC_MARK_STACK != 0. */
3812 #ifdef GC_CHECK_MARKED_OBJECTS
3814 po = (void *) XPNTR (obj);
3816 /* Check that the object pointed to by PO is known to be a Lisp
3817 structure allocated from the heap. */
3818 #define CHECK_ALLOCATED() \
3819 do { \
3820 m = mem_find (po); \
3821 if (m == MEM_NIL) \
3822 abort (); \
3823 } while (0)
3825 /* Check that the object pointed to by PO is live, using predicate
3826 function LIVEP. */
3827 #define CHECK_LIVE(LIVEP) \
3828 do { \
3829 if (!LIVEP (m, po)) \
3830 abort (); \
3831 } while (0)
3833 /* Check both of the above conditions. */
3834 #define CHECK_ALLOCATED_AND_LIVE(LIVEP) \
3835 do { \
3836 CHECK_ALLOCATED (); \
3837 CHECK_LIVE (LIVEP); \
3838 } while (0) \
3840 #else /* not GC_CHECK_MARKED_OBJECTS */
3842 #define CHECK_ALLOCATED() (void) 0
3843 #define CHECK_LIVE(LIVEP) (void) 0
3844 #define CHECK_ALLOCATED_AND_LIVE(LIVEP) (void) 0
3846 #endif /* not GC_CHECK_MARKED_OBJECTS */
3848 switch (SWITCH_ENUM_CAST (XGCTYPE (obj)))
3850 case Lisp_String:
3852 register struct Lisp_String *ptr = XSTRING (obj);
3853 CHECK_ALLOCATED_AND_LIVE (live_string_p);
3854 MARK_INTERVAL_TREE (ptr->intervals);
3855 MARK_STRING (ptr);
3857 break;
3859 case Lisp_Vectorlike:
3860 #ifdef GC_CHECK_MARKED_OBJECTS
3861 m = mem_find (po);
3862 if (m == MEM_NIL && !GC_SUBRP (obj)
3863 && po != &buffer_defaults
3864 && po != &buffer_local_symbols)
3865 abort ();
3866 #endif /* GC_CHECK_MARKED_OBJECTS */
3868 if (GC_BUFFERP (obj))
3870 if (!XMARKBIT (XBUFFER (obj)->name))
3872 #ifdef GC_CHECK_MARKED_OBJECTS
3873 if (po != &buffer_defaults && po != &buffer_local_symbols)
3875 struct buffer *b;
3876 for (b = all_buffers; b && b != po; b = b->next)
3878 if (b == NULL)
3879 abort ();
3881 #endif /* GC_CHECK_MARKED_OBJECTS */
3882 mark_buffer (obj);
3885 else if (GC_SUBRP (obj))
3886 break;
3887 else if (GC_COMPILEDP (obj))
3888 /* We could treat this just like a vector, but it is better to
3889 save the COMPILED_CONSTANTS element for last and avoid
3890 recursion there. */
3892 register struct Lisp_Vector *ptr = XVECTOR (obj);
3893 register EMACS_INT size = ptr->size;
3894 /* See comment above under Lisp_Vector. */
3895 struct Lisp_Vector *volatile ptr1 = ptr;
3896 register int i;
3898 if (size & ARRAY_MARK_FLAG)
3899 break; /* Already marked */
3901 CHECK_LIVE (live_vector_p);
3902 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
3903 size &= PSEUDOVECTOR_SIZE_MASK;
3904 for (i = 0; i < size; i++) /* and then mark its elements */
3906 if (i != COMPILED_CONSTANTS)
3907 mark_object (&ptr1->contents[i]);
3909 /* This cast should be unnecessary, but some Mips compiler complains
3910 (MIPS-ABI + SysVR4, DC/OSx, etc). */
3911 objptr = (Lisp_Object *) &ptr1->contents[COMPILED_CONSTANTS];
3912 goto loop;
3914 else if (GC_FRAMEP (obj))
3916 /* See comment above under Lisp_Vector for why this is volatile. */
3917 register struct frame *volatile ptr = XFRAME (obj);
3918 register EMACS_INT size = ptr->size;
3920 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
3921 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
3923 CHECK_LIVE (live_vector_p);
3924 mark_object (&ptr->name);
3925 mark_object (&ptr->icon_name);
3926 mark_object (&ptr->title);
3927 mark_object (&ptr->focus_frame);
3928 mark_object (&ptr->selected_window);
3929 mark_object (&ptr->minibuffer_window);
3930 mark_object (&ptr->param_alist);
3931 mark_object (&ptr->scroll_bars);
3932 mark_object (&ptr->condemned_scroll_bars);
3933 mark_object (&ptr->menu_bar_items);
3934 mark_object (&ptr->face_alist);
3935 mark_object (&ptr->menu_bar_vector);
3936 mark_object (&ptr->buffer_predicate);
3937 mark_object (&ptr->buffer_list);
3938 mark_object (&ptr->menu_bar_window);
3939 mark_object (&ptr->tool_bar_window);
3940 mark_face_cache (ptr->face_cache);
3941 #ifdef HAVE_WINDOW_SYSTEM
3942 mark_image_cache (ptr);
3943 mark_object (&ptr->desired_tool_bar_items);
3944 mark_object (&ptr->current_tool_bar_items);
3945 mark_object (&ptr->desired_tool_bar_string);
3946 mark_object (&ptr->current_tool_bar_string);
3947 #endif /* HAVE_WINDOW_SYSTEM */
3949 else if (GC_BOOL_VECTOR_P (obj))
3951 register struct Lisp_Vector *ptr = XVECTOR (obj);
3953 if (ptr->size & ARRAY_MARK_FLAG)
3954 break; /* Already marked */
3955 CHECK_LIVE (live_vector_p);
3956 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
3958 else if (GC_WINDOWP (obj))
3960 register struct Lisp_Vector *ptr = XVECTOR (obj);
3961 struct window *w = XWINDOW (obj);
3962 register EMACS_INT size = ptr->size;
3963 /* The reason we use ptr1 is to avoid an apparent hardware bug
3964 that happens occasionally on the FSF's HP 300s.
3965 The bug is that a2 gets clobbered by recursive calls to mark_object.
3966 The clobberage seems to happen during function entry,
3967 perhaps in the moveml instruction.
3968 Yes, this is a crock, but we have to do it. */
3969 struct Lisp_Vector *volatile ptr1 = ptr;
3970 register int i;
3972 /* Stop if already marked. */
3973 if (size & ARRAY_MARK_FLAG)
3974 break;
3976 /* Mark it. */
3977 CHECK_LIVE (live_vector_p);
3978 ptr->size |= ARRAY_MARK_FLAG;
3980 /* There is no Lisp data above The member CURRENT_MATRIX in
3981 struct WINDOW. Stop marking when that slot is reached. */
3982 for (i = 0;
3983 (char *) &ptr1->contents[i] < (char *) &w->current_matrix;
3984 i++)
3985 mark_object (&ptr1->contents[i]);
3987 /* Mark glyphs for leaf windows. Marking window matrices is
3988 sufficient because frame matrices use the same glyph
3989 memory. */
3990 if (NILP (w->hchild)
3991 && NILP (w->vchild)
3992 && w->current_matrix)
3994 mark_glyph_matrix (w->current_matrix);
3995 mark_glyph_matrix (w->desired_matrix);
3998 else if (GC_HASH_TABLE_P (obj))
4000 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
4001 EMACS_INT size = h->size;
4003 /* Stop if already marked. */
4004 if (size & ARRAY_MARK_FLAG)
4005 break;
4007 /* Mark it. */
4008 CHECK_LIVE (live_vector_p);
4009 h->size |= ARRAY_MARK_FLAG;
4011 /* Mark contents. */
4012 mark_object (&h->test);
4013 mark_object (&h->weak);
4014 mark_object (&h->rehash_size);
4015 mark_object (&h->rehash_threshold);
4016 mark_object (&h->hash);
4017 mark_object (&h->next);
4018 mark_object (&h->index);
4019 mark_object (&h->user_hash_function);
4020 mark_object (&h->user_cmp_function);
4022 /* If hash table is not weak, mark all keys and values.
4023 For weak tables, mark only the vector. */
4024 if (GC_NILP (h->weak))
4025 mark_object (&h->key_and_value);
4026 else
4027 XVECTOR (h->key_and_value)->size |= ARRAY_MARK_FLAG;
4030 else
4032 register struct Lisp_Vector *ptr = XVECTOR (obj);
4033 register EMACS_INT size = ptr->size;
4034 /* The reason we use ptr1 is to avoid an apparent hardware bug
4035 that happens occasionally on the FSF's HP 300s.
4036 The bug is that a2 gets clobbered by recursive calls to mark_object.
4037 The clobberage seems to happen during function entry,
4038 perhaps in the moveml instruction.
4039 Yes, this is a crock, but we have to do it. */
4040 struct Lisp_Vector *volatile ptr1 = ptr;
4041 register int i;
4043 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
4044 CHECK_LIVE (live_vector_p);
4045 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
4046 if (size & PSEUDOVECTOR_FLAG)
4047 size &= PSEUDOVECTOR_SIZE_MASK;
4049 for (i = 0; i < size; i++) /* and then mark its elements */
4050 mark_object (&ptr1->contents[i]);
4052 break;
4054 case Lisp_Symbol:
4056 /* See comment above under Lisp_Vector for why this is volatile. */
4057 register struct Lisp_Symbol *volatile ptr = XSYMBOL (obj);
4058 struct Lisp_Symbol *ptrx;
4060 if (XMARKBIT (ptr->plist)) break;
4061 CHECK_ALLOCATED_AND_LIVE (live_symbol_p);
4062 XMARK (ptr->plist);
4063 mark_object ((Lisp_Object *) &ptr->value);
4064 mark_object (&ptr->function);
4065 mark_object (&ptr->plist);
4067 if (!PURE_POINTER_P (ptr->name))
4068 MARK_STRING (ptr->name);
4069 MARK_INTERVAL_TREE (ptr->name->intervals);
4071 /* Note that we do not mark the obarray of the symbol.
4072 It is safe not to do so because nothing accesses that
4073 slot except to check whether it is nil. */
4074 ptr = ptr->next;
4075 if (ptr)
4077 /* For the benefit of the last_marked log. */
4078 objptr = (Lisp_Object *)&XSYMBOL (obj)->next;
4079 ptrx = ptr; /* Use of ptrx avoids compiler bug on Sun */
4080 XSETSYMBOL (obj, ptrx);
4081 /* We can't goto loop here because *objptr doesn't contain an
4082 actual Lisp_Object with valid datatype field. */
4083 goto loop2;
4086 break;
4088 case Lisp_Misc:
4089 CHECK_ALLOCATED_AND_LIVE (live_misc_p);
4090 switch (XMISCTYPE (obj))
4092 case Lisp_Misc_Marker:
4093 XMARK (XMARKER (obj)->chain);
4094 /* DO NOT mark thru the marker's chain.
4095 The buffer's markers chain does not preserve markers from gc;
4096 instead, markers are removed from the chain when freed by gc. */
4097 break;
4099 case Lisp_Misc_Buffer_Local_Value:
4100 case Lisp_Misc_Some_Buffer_Local_Value:
4102 register struct Lisp_Buffer_Local_Value *ptr
4103 = XBUFFER_LOCAL_VALUE (obj);
4104 if (XMARKBIT (ptr->realvalue)) break;
4105 XMARK (ptr->realvalue);
4106 /* If the cdr is nil, avoid recursion for the car. */
4107 if (EQ (ptr->cdr, Qnil))
4109 objptr = &ptr->realvalue;
4110 goto loop;
4112 mark_object (&ptr->realvalue);
4113 mark_object (&ptr->buffer);
4114 mark_object (&ptr->frame);
4115 /* See comment above under Lisp_Vector for why not use ptr here. */
4116 objptr = &XBUFFER_LOCAL_VALUE (obj)->cdr;
4117 goto loop;
4120 case Lisp_Misc_Intfwd:
4121 case Lisp_Misc_Boolfwd:
4122 case Lisp_Misc_Objfwd:
4123 case Lisp_Misc_Buffer_Objfwd:
4124 case Lisp_Misc_Kboard_Objfwd:
4125 /* Don't bother with Lisp_Buffer_Objfwd,
4126 since all markable slots in current buffer marked anyway. */
4127 /* Don't need to do Lisp_Objfwd, since the places they point
4128 are protected with staticpro. */
4129 break;
4131 case Lisp_Misc_Overlay:
4133 struct Lisp_Overlay *ptr = XOVERLAY (obj);
4134 if (!XMARKBIT (ptr->plist))
4136 XMARK (ptr->plist);
4137 mark_object (&ptr->start);
4138 mark_object (&ptr->end);
4139 objptr = &ptr->plist;
4140 goto loop;
4143 break;
4145 default:
4146 abort ();
4148 break;
4150 case Lisp_Cons:
4152 register struct Lisp_Cons *ptr = XCONS (obj);
4153 if (XMARKBIT (ptr->car)) break;
4154 CHECK_ALLOCATED_AND_LIVE (live_cons_p);
4155 XMARK (ptr->car);
4156 /* If the cdr is nil, avoid recursion for the car. */
4157 if (EQ (ptr->cdr, Qnil))
4159 objptr = &ptr->car;
4160 goto loop;
4162 mark_object (&ptr->car);
4163 /* See comment above under Lisp_Vector for why not use ptr here. */
4164 objptr = &XCDR (obj);
4165 goto loop;
4168 case Lisp_Float:
4169 CHECK_ALLOCATED_AND_LIVE (live_float_p);
4170 XMARK (XFLOAT (obj)->type);
4171 break;
4173 case Lisp_Int:
4174 break;
4176 default:
4177 abort ();
4180 #undef CHECK_LIVE
4181 #undef CHECK_ALLOCATED
4182 #undef CHECK_ALLOCATED_AND_LIVE
4185 /* Mark the pointers in a buffer structure. */
4187 static void
4188 mark_buffer (buf)
4189 Lisp_Object buf;
4191 register struct buffer *buffer = XBUFFER (buf);
4192 register Lisp_Object *ptr;
4193 Lisp_Object base_buffer;
4195 /* This is the buffer's markbit */
4196 mark_object (&buffer->name);
4197 XMARK (buffer->name);
4199 MARK_INTERVAL_TREE (BUF_INTERVALS (buffer));
4201 if (CONSP (buffer->undo_list))
4203 Lisp_Object tail;
4204 tail = buffer->undo_list;
4206 while (CONSP (tail))
4208 register struct Lisp_Cons *ptr = XCONS (tail);
4210 if (XMARKBIT (ptr->car))
4211 break;
4212 XMARK (ptr->car);
4213 if (GC_CONSP (ptr->car)
4214 && ! XMARKBIT (XCAR (ptr->car))
4215 && GC_MARKERP (XCAR (ptr->car)))
4217 XMARK (XCAR (ptr->car));
4218 mark_object (&XCDR (ptr->car));
4220 else
4221 mark_object (&ptr->car);
4223 if (CONSP (ptr->cdr))
4224 tail = ptr->cdr;
4225 else
4226 break;
4229 mark_object (&XCDR (tail));
4231 else
4232 mark_object (&buffer->undo_list);
4234 for (ptr = &buffer->name + 1;
4235 (char *)ptr < (char *)buffer + sizeof (struct buffer);
4236 ptr++)
4237 mark_object (ptr);
4239 /* If this is an indirect buffer, mark its base buffer. */
4240 if (buffer->base_buffer && !XMARKBIT (buffer->base_buffer->name))
4242 XSETBUFFER (base_buffer, buffer->base_buffer);
4243 mark_buffer (base_buffer);
4248 /* Mark the pointers in the kboard objects. */
4250 static void
4251 mark_kboards ()
4253 KBOARD *kb;
4254 Lisp_Object *p;
4255 for (kb = all_kboards; kb; kb = kb->next_kboard)
4257 if (kb->kbd_macro_buffer)
4258 for (p = kb->kbd_macro_buffer; p < kb->kbd_macro_ptr; p++)
4259 mark_object (p);
4260 mark_object (&kb->Voverriding_terminal_local_map);
4261 mark_object (&kb->Vlast_command);
4262 mark_object (&kb->Vreal_last_command);
4263 mark_object (&kb->Vprefix_arg);
4264 mark_object (&kb->Vlast_prefix_arg);
4265 mark_object (&kb->kbd_queue);
4266 mark_object (&kb->defining_kbd_macro);
4267 mark_object (&kb->Vlast_kbd_macro);
4268 mark_object (&kb->Vsystem_key_alist);
4269 mark_object (&kb->system_key_syms);
4270 mark_object (&kb->Vdefault_minibuffer_frame);
4275 /* Value is non-zero if OBJ will survive the current GC because it's
4276 either marked or does not need to be marked to survive. */
4279 survives_gc_p (obj)
4280 Lisp_Object obj;
4282 int survives_p;
4284 switch (XGCTYPE (obj))
4286 case Lisp_Int:
4287 survives_p = 1;
4288 break;
4290 case Lisp_Symbol:
4291 survives_p = XMARKBIT (XSYMBOL (obj)->plist);
4292 break;
4294 case Lisp_Misc:
4295 switch (XMISCTYPE (obj))
4297 case Lisp_Misc_Marker:
4298 survives_p = XMARKBIT (obj);
4299 break;
4301 case Lisp_Misc_Buffer_Local_Value:
4302 case Lisp_Misc_Some_Buffer_Local_Value:
4303 survives_p = XMARKBIT (XBUFFER_LOCAL_VALUE (obj)->realvalue);
4304 break;
4306 case Lisp_Misc_Intfwd:
4307 case Lisp_Misc_Boolfwd:
4308 case Lisp_Misc_Objfwd:
4309 case Lisp_Misc_Buffer_Objfwd:
4310 case Lisp_Misc_Kboard_Objfwd:
4311 survives_p = 1;
4312 break;
4314 case Lisp_Misc_Overlay:
4315 survives_p = XMARKBIT (XOVERLAY (obj)->plist);
4316 break;
4318 default:
4319 abort ();
4321 break;
4323 case Lisp_String:
4325 struct Lisp_String *s = XSTRING (obj);
4326 survives_p = STRING_MARKED_P (s);
4328 break;
4330 case Lisp_Vectorlike:
4331 if (GC_BUFFERP (obj))
4332 survives_p = XMARKBIT (XBUFFER (obj)->name);
4333 else if (GC_SUBRP (obj))
4334 survives_p = 1;
4335 else
4336 survives_p = XVECTOR (obj)->size & ARRAY_MARK_FLAG;
4337 break;
4339 case Lisp_Cons:
4340 survives_p = XMARKBIT (XCAR (obj));
4341 break;
4343 case Lisp_Float:
4344 survives_p = XMARKBIT (XFLOAT (obj)->type);
4345 break;
4347 default:
4348 abort ();
4351 return survives_p || PURE_POINTER_P ((void *) XPNTR (obj));
4356 /* Sweep: find all structures not marked, and free them. */
4358 static void
4359 gc_sweep ()
4361 /* Remove or mark entries in weak hash tables.
4362 This must be done before any object is unmarked. */
4363 sweep_weak_hash_tables ();
4365 sweep_strings ();
4367 /* Put all unmarked conses on free list */
4369 register struct cons_block *cblk;
4370 struct cons_block **cprev = &cons_block;
4371 register int lim = cons_block_index;
4372 register int num_free = 0, num_used = 0;
4374 cons_free_list = 0;
4376 for (cblk = cons_block; cblk; cblk = *cprev)
4378 register int i;
4379 int this_free = 0;
4380 for (i = 0; i < lim; i++)
4381 if (!XMARKBIT (cblk->conses[i].car))
4383 this_free++;
4384 *(struct Lisp_Cons **)&cblk->conses[i].cdr = cons_free_list;
4385 cons_free_list = &cblk->conses[i];
4386 #if GC_MARK_STACK
4387 cons_free_list->car = Vdead;
4388 #endif
4390 else
4392 num_used++;
4393 XUNMARK (cblk->conses[i].car);
4395 lim = CONS_BLOCK_SIZE;
4396 /* If this block contains only free conses and we have already
4397 seen more than two blocks worth of free conses then deallocate
4398 this block. */
4399 if (this_free == CONS_BLOCK_SIZE && num_free > CONS_BLOCK_SIZE)
4401 *cprev = cblk->next;
4402 /* Unhook from the free list. */
4403 cons_free_list = *(struct Lisp_Cons **) &cblk->conses[0].cdr;
4404 lisp_free (cblk);
4405 n_cons_blocks--;
4407 else
4409 num_free += this_free;
4410 cprev = &cblk->next;
4413 total_conses = num_used;
4414 total_free_conses = num_free;
4417 /* Put all unmarked floats on free list */
4419 register struct float_block *fblk;
4420 struct float_block **fprev = &float_block;
4421 register int lim = float_block_index;
4422 register int num_free = 0, num_used = 0;
4424 float_free_list = 0;
4426 for (fblk = float_block; fblk; fblk = *fprev)
4428 register int i;
4429 int this_free = 0;
4430 for (i = 0; i < lim; i++)
4431 if (!XMARKBIT (fblk->floats[i].type))
4433 this_free++;
4434 *(struct Lisp_Float **)&fblk->floats[i].data = float_free_list;
4435 float_free_list = &fblk->floats[i];
4436 #if GC_MARK_STACK
4437 float_free_list->type = Vdead;
4438 #endif
4440 else
4442 num_used++;
4443 XUNMARK (fblk->floats[i].type);
4445 lim = FLOAT_BLOCK_SIZE;
4446 /* If this block contains only free floats and we have already
4447 seen more than two blocks worth of free floats then deallocate
4448 this block. */
4449 if (this_free == FLOAT_BLOCK_SIZE && num_free > FLOAT_BLOCK_SIZE)
4451 *fprev = fblk->next;
4452 /* Unhook from the free list. */
4453 float_free_list = *(struct Lisp_Float **) &fblk->floats[0].data;
4454 lisp_free (fblk);
4455 n_float_blocks--;
4457 else
4459 num_free += this_free;
4460 fprev = &fblk->next;
4463 total_floats = num_used;
4464 total_free_floats = num_free;
4467 /* Put all unmarked intervals on free list */
4469 register struct interval_block *iblk;
4470 struct interval_block **iprev = &interval_block;
4471 register int lim = interval_block_index;
4472 register int num_free = 0, num_used = 0;
4474 interval_free_list = 0;
4476 for (iblk = interval_block; iblk; iblk = *iprev)
4478 register int i;
4479 int this_free = 0;
4481 for (i = 0; i < lim; i++)
4483 if (! XMARKBIT (iblk->intervals[i].plist))
4485 SET_INTERVAL_PARENT (&iblk->intervals[i], interval_free_list);
4486 interval_free_list = &iblk->intervals[i];
4487 this_free++;
4489 else
4491 num_used++;
4492 XUNMARK (iblk->intervals[i].plist);
4495 lim = INTERVAL_BLOCK_SIZE;
4496 /* If this block contains only free intervals and we have already
4497 seen more than two blocks worth of free intervals then
4498 deallocate this block. */
4499 if (this_free == INTERVAL_BLOCK_SIZE && num_free > INTERVAL_BLOCK_SIZE)
4501 *iprev = iblk->next;
4502 /* Unhook from the free list. */
4503 interval_free_list = INTERVAL_PARENT (&iblk->intervals[0]);
4504 lisp_free (iblk);
4505 n_interval_blocks--;
4507 else
4509 num_free += this_free;
4510 iprev = &iblk->next;
4513 total_intervals = num_used;
4514 total_free_intervals = num_free;
4517 /* Put all unmarked symbols on free list */
4519 register struct symbol_block *sblk;
4520 struct symbol_block **sprev = &symbol_block;
4521 register int lim = symbol_block_index;
4522 register int num_free = 0, num_used = 0;
4524 symbol_free_list = 0;
4526 for (sblk = symbol_block; sblk; sblk = *sprev)
4528 register int i;
4529 int this_free = 0;
4530 for (i = 0; i < lim; i++)
4531 if (!XMARKBIT (sblk->symbols[i].plist))
4533 *(struct Lisp_Symbol **)&sblk->symbols[i].value = symbol_free_list;
4534 symbol_free_list = &sblk->symbols[i];
4535 #if GC_MARK_STACK
4536 symbol_free_list->function = Vdead;
4537 #endif
4538 this_free++;
4540 else
4542 num_used++;
4543 if (!PURE_POINTER_P (sblk->symbols[i].name))
4544 UNMARK_STRING (sblk->symbols[i].name);
4545 XUNMARK (sblk->symbols[i].plist);
4547 lim = SYMBOL_BLOCK_SIZE;
4548 /* If this block contains only free symbols and we have already
4549 seen more than two blocks worth of free symbols then deallocate
4550 this block. */
4551 if (this_free == SYMBOL_BLOCK_SIZE && num_free > SYMBOL_BLOCK_SIZE)
4553 *sprev = sblk->next;
4554 /* Unhook from the free list. */
4555 symbol_free_list = *(struct Lisp_Symbol **)&sblk->symbols[0].value;
4556 lisp_free (sblk);
4557 n_symbol_blocks--;
4559 else
4561 num_free += this_free;
4562 sprev = &sblk->next;
4565 total_symbols = num_used;
4566 total_free_symbols = num_free;
4569 /* Put all unmarked misc's on free list.
4570 For a marker, first unchain it from the buffer it points into. */
4572 register struct marker_block *mblk;
4573 struct marker_block **mprev = &marker_block;
4574 register int lim = marker_block_index;
4575 register int num_free = 0, num_used = 0;
4577 marker_free_list = 0;
4579 for (mblk = marker_block; mblk; mblk = *mprev)
4581 register int i;
4582 int this_free = 0;
4583 EMACS_INT already_free = -1;
4585 for (i = 0; i < lim; i++)
4587 Lisp_Object *markword;
4588 switch (mblk->markers[i].u_marker.type)
4590 case Lisp_Misc_Marker:
4591 markword = &mblk->markers[i].u_marker.chain;
4592 break;
4593 case Lisp_Misc_Buffer_Local_Value:
4594 case Lisp_Misc_Some_Buffer_Local_Value:
4595 markword = &mblk->markers[i].u_buffer_local_value.realvalue;
4596 break;
4597 case Lisp_Misc_Overlay:
4598 markword = &mblk->markers[i].u_overlay.plist;
4599 break;
4600 case Lisp_Misc_Free:
4601 /* If the object was already free, keep it
4602 on the free list. */
4603 markword = (Lisp_Object *) &already_free;
4604 break;
4605 default:
4606 markword = 0;
4607 break;
4609 if (markword && !XMARKBIT (*markword))
4611 Lisp_Object tem;
4612 if (mblk->markers[i].u_marker.type == Lisp_Misc_Marker)
4614 /* tem1 avoids Sun compiler bug */
4615 struct Lisp_Marker *tem1 = &mblk->markers[i].u_marker;
4616 XSETMARKER (tem, tem1);
4617 unchain_marker (tem);
4619 /* Set the type of the freed object to Lisp_Misc_Free.
4620 We could leave the type alone, since nobody checks it,
4621 but this might catch bugs faster. */
4622 mblk->markers[i].u_marker.type = Lisp_Misc_Free;
4623 mblk->markers[i].u_free.chain = marker_free_list;
4624 marker_free_list = &mblk->markers[i];
4625 this_free++;
4627 else
4629 num_used++;
4630 if (markword)
4631 XUNMARK (*markword);
4634 lim = MARKER_BLOCK_SIZE;
4635 /* If this block contains only free markers and we have already
4636 seen more than two blocks worth of free markers then deallocate
4637 this block. */
4638 if (this_free == MARKER_BLOCK_SIZE && num_free > MARKER_BLOCK_SIZE)
4640 *mprev = mblk->next;
4641 /* Unhook from the free list. */
4642 marker_free_list = mblk->markers[0].u_free.chain;
4643 lisp_free (mblk);
4644 n_marker_blocks--;
4646 else
4648 num_free += this_free;
4649 mprev = &mblk->next;
4653 total_markers = num_used;
4654 total_free_markers = num_free;
4657 /* Free all unmarked buffers */
4659 register struct buffer *buffer = all_buffers, *prev = 0, *next;
4661 while (buffer)
4662 if (!XMARKBIT (buffer->name))
4664 if (prev)
4665 prev->next = buffer->next;
4666 else
4667 all_buffers = buffer->next;
4668 next = buffer->next;
4669 lisp_free (buffer);
4670 buffer = next;
4672 else
4674 XUNMARK (buffer->name);
4675 UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer));
4676 prev = buffer, buffer = buffer->next;
4680 /* Free all unmarked vectors */
4682 register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next;
4683 total_vector_size = 0;
4685 while (vector)
4686 if (!(vector->size & ARRAY_MARK_FLAG))
4688 if (prev)
4689 prev->next = vector->next;
4690 else
4691 all_vectors = vector->next;
4692 next = vector->next;
4693 lisp_free (vector);
4694 n_vectors--;
4695 vector = next;
4698 else
4700 vector->size &= ~ARRAY_MARK_FLAG;
4701 if (vector->size & PSEUDOVECTOR_FLAG)
4702 total_vector_size += (PSEUDOVECTOR_SIZE_MASK & vector->size);
4703 else
4704 total_vector_size += vector->size;
4705 prev = vector, vector = vector->next;
4713 /* Debugging aids. */
4715 DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0,
4716 "Return the address of the last byte Emacs has allocated, divided by 1024.\n\
4717 This may be helpful in debugging Emacs's memory usage.\n\
4718 We divide the value by 1024 to make sure it fits in a Lisp integer.")
4721 Lisp_Object end;
4723 XSETINT (end, (EMACS_INT) sbrk (0) / 1024);
4725 return end;
4728 DEFUN ("memory-use-counts", Fmemory_use_counts, Smemory_use_counts, 0, 0, 0,
4729 "Return a list of counters that measure how much consing there has been.\n\
4730 Each of these counters increments for a certain kind of object.\n\
4731 The counters wrap around from the largest positive integer to zero.\n\
4732 Garbage collection does not decrease them.\n\
4733 The elements of the value are as follows:\n\
4734 (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS STRINGS)\n\
4735 All are in units of 1 = one object consed\n\
4736 except for VECTOR-CELLS and STRING-CHARS, which count the total length of\n\
4737 objects consed.\n\
4738 MISCS include overlays, markers, and some internal types.\n\
4739 Frames, windows, buffers, and subprocesses count as vectors\n\
4740 (but the contents of a buffer's text do not count here).")
4743 Lisp_Object consed[8];
4745 XSETINT (consed[0],
4746 cons_cells_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
4747 XSETINT (consed[1],
4748 floats_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
4749 XSETINT (consed[2],
4750 vector_cells_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
4751 XSETINT (consed[3],
4752 symbols_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
4753 XSETINT (consed[4],
4754 string_chars_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
4755 XSETINT (consed[5],
4756 misc_objects_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
4757 XSETINT (consed[6],
4758 intervals_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
4759 XSETINT (consed[7],
4760 strings_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
4762 return Flist (8, consed);
4765 int suppress_checking;
4766 void
4767 die (msg, file, line)
4768 const char *msg;
4769 const char *file;
4770 int line;
4772 fprintf (stderr, "\r\nEmacs fatal error: %s:%d: %s\r\n",
4773 file, line, msg);
4774 abort ();
4777 /* Initialization */
4779 void
4780 init_alloc_once ()
4782 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
4783 pureptr = 0;
4784 #if GC_MARK_STACK
4785 mem_init ();
4786 Vdead = make_pure_string ("DEAD", 4, 4, 0);
4787 #endif
4788 #ifdef HAVE_SHM
4789 pure_size = PURESIZE;
4790 #endif
4791 all_vectors = 0;
4792 ignore_warnings = 1;
4793 #ifdef DOUG_LEA_MALLOC
4794 mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */
4795 mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */
4796 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); /* max. number of mmap'ed areas */
4797 #endif
4798 init_strings ();
4799 init_cons ();
4800 init_symbol ();
4801 init_marker ();
4802 init_float ();
4803 init_intervals ();
4805 #ifdef REL_ALLOC
4806 malloc_hysteresis = 32;
4807 #else
4808 malloc_hysteresis = 0;
4809 #endif
4811 spare_memory = (char *) malloc (SPARE_MEMORY);
4813 ignore_warnings = 0;
4814 gcprolist = 0;
4815 byte_stack_list = 0;
4816 staticidx = 0;
4817 consing_since_gc = 0;
4818 gc_cons_threshold = 100000 * sizeof (Lisp_Object);
4819 #ifdef VIRT_ADDR_VARIES
4820 malloc_sbrk_unused = 1<<22; /* A large number */
4821 malloc_sbrk_used = 100000; /* as reasonable as any number */
4822 #endif /* VIRT_ADDR_VARIES */
4825 void
4826 init_alloc ()
4828 gcprolist = 0;
4829 byte_stack_list = 0;
4830 #if GC_MARK_STACK
4831 #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
4832 setjmp_tested_p = longjmps_done = 0;
4833 #endif
4834 #endif
4837 void
4838 syms_of_alloc ()
4840 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold,
4841 "*Number of bytes of consing between garbage collections.\n\
4842 Garbage collection can happen automatically once this many bytes have been\n\
4843 allocated since the last garbage collection. All data types count.\n\n\
4844 Garbage collection happens automatically only when `eval' is called.\n\n\
4845 By binding this temporarily to a large number, you can effectively\n\
4846 prevent garbage collection during a part of the program.");
4848 DEFVAR_INT ("pure-bytes-used", &pureptr,
4849 "Number of bytes of sharable Lisp data allocated so far.");
4851 DEFVAR_INT ("cons-cells-consed", &cons_cells_consed,
4852 "Number of cons cells that have been consed so far.");
4854 DEFVAR_INT ("floats-consed", &floats_consed,
4855 "Number of floats that have been consed so far.");
4857 DEFVAR_INT ("vector-cells-consed", &vector_cells_consed,
4858 "Number of vector cells that have been consed so far.");
4860 DEFVAR_INT ("symbols-consed", &symbols_consed,
4861 "Number of symbols that have been consed so far.");
4863 DEFVAR_INT ("string-chars-consed", &string_chars_consed,
4864 "Number of string characters that have been consed so far.");
4866 DEFVAR_INT ("misc-objects-consed", &misc_objects_consed,
4867 "Number of miscellaneous objects that have been consed so far.");
4869 DEFVAR_INT ("intervals-consed", &intervals_consed,
4870 "Number of intervals that have been consed so far.");
4872 DEFVAR_INT ("strings-consed", &strings_consed,
4873 "Number of strings that have been consed so far.");
4875 DEFVAR_LISP ("purify-flag", &Vpurify_flag,
4876 "Non-nil means loading Lisp code in order to dump an executable.\n\
4877 This means that certain objects should be allocated in shared (pure) space.");
4879 DEFVAR_INT ("undo-limit", &undo_limit,
4880 "Keep no more undo information once it exceeds this size.\n\
4881 This limit is applied when garbage collection happens.\n\
4882 The size is counted as the number of bytes occupied,\n\
4883 which includes both saved text and other data.");
4884 undo_limit = 20000;
4886 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit,
4887 "Don't keep more than this much size of undo information.\n\
4888 A command which pushes past this size is itself forgotten.\n\
4889 This limit is applied when garbage collection happens.\n\
4890 The size is counted as the number of bytes occupied,\n\
4891 which includes both saved text and other data.");
4892 undo_strong_limit = 30000;
4894 DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages,
4895 "Non-nil means display messages at start and end of garbage collection.");
4896 garbage_collection_messages = 0;
4898 /* We build this in advance because if we wait until we need it, we might
4899 not be able to allocate the memory to hold it. */
4900 memory_signal_data
4901 = Fcons (Qerror, Fcons (build_string ("Memory exhausted--use M-x save-some-buffers RET"), Qnil));
4902 staticpro (&memory_signal_data);
4904 staticpro (&Qgc_cons_threshold);
4905 Qgc_cons_threshold = intern ("gc-cons-threshold");
4907 staticpro (&Qchar_table_extra_slots);
4908 Qchar_table_extra_slots = intern ("char-table-extra-slots");
4910 defsubr (&Scons);
4911 defsubr (&Slist);
4912 defsubr (&Svector);
4913 defsubr (&Smake_byte_code);
4914 defsubr (&Smake_list);
4915 defsubr (&Smake_vector);
4916 defsubr (&Smake_char_table);
4917 defsubr (&Smake_string);
4918 defsubr (&Smake_bool_vector);
4919 defsubr (&Smake_symbol);
4920 defsubr (&Smake_marker);
4921 defsubr (&Spurecopy);
4922 defsubr (&Sgarbage_collect);
4923 defsubr (&Smemory_limit);
4924 defsubr (&Smemory_use_counts);
4926 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4927 defsubr (&Sgc_status);
4928 #endif