1 /* Storage allocation and gc for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 86, 88, 93, 94, 95, 97, 98, 1999, 2000, 2001, 2002
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)
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. */
25 /* Note that this declares bzero on OSF/1. How dumb. */
29 /* GC_MALLOC_CHECK defined means perform validity checks of malloc'd
30 memory. Can do this only if using gmalloc.c. */
32 #if defined SYSTEM_MALLOC || defined DOUG_LEA_MALLOC
33 #undef GC_MALLOC_CHECK
36 /* This file is part of the core Lisp implementation, and thus must
37 deal with the real data structures. If the Lisp implementation is
38 replaced, this file likely will not be used. */
40 #undef HIDE_LISP_IMPLEMENTATION
43 #include "intervals.h"
49 #include "blockinput.h"
51 #include "syssignal.h"
57 extern POINTER_TYPE
*sbrk ();
60 #ifdef DOUG_LEA_MALLOC
63 /* malloc.h #defines this as size_t, at least in glibc2. */
64 #ifndef __malloc_size_t
65 #define __malloc_size_t int
68 /* Specify maximum number of areas to mmap. It would be nice to use a
69 value that explicitly means "no limit". */
71 #define MMAP_MAX_AREAS 100000000
73 #else /* not DOUG_LEA_MALLOC */
75 /* The following come from gmalloc.c. */
77 #define __malloc_size_t size_t
78 extern __malloc_size_t _bytes_used
;
79 extern __malloc_size_t __malloc_extra_blocks
;
81 #endif /* not DOUG_LEA_MALLOC */
83 /* Macro to verify that storage intended for Lisp objects is not
84 out of range to fit in the space for a pointer.
85 ADDRESS is the start of the block, and SIZE
86 is the amount of space within which objects can start. */
88 #define VALIDATE_LISP_STORAGE(address, size) \
92 XSETCONS (val, (char *) address + size); \
93 if ((char *) XCONS (val) != (char *) address + size) \
100 /* Value of _bytes_used, when spare_memory was freed. */
102 static __malloc_size_t bytes_used_when_full
;
104 /* Mark, unmark, query mark bit of a Lisp string. S must be a pointer
105 to a struct Lisp_String. */
107 #define MARK_STRING(S) ((S)->size |= MARKBIT)
108 #define UNMARK_STRING(S) ((S)->size &= ~MARKBIT)
109 #define STRING_MARKED_P(S) ((S)->size & MARKBIT)
111 /* Value is the number of bytes/chars of S, a pointer to a struct
112 Lisp_String. This must be used instead of STRING_BYTES (S) or
113 S->size during GC, because S->size contains the mark bit for
116 #define GC_STRING_BYTES(S) (STRING_BYTES (S) & ~MARKBIT)
117 #define GC_STRING_CHARS(S) ((S)->size & ~MARKBIT)
119 /* Number of bytes of consing done since the last gc. */
121 int consing_since_gc
;
123 /* Count the amount of consing of various sorts of space. */
125 EMACS_INT cons_cells_consed
;
126 EMACS_INT floats_consed
;
127 EMACS_INT vector_cells_consed
;
128 EMACS_INT symbols_consed
;
129 EMACS_INT string_chars_consed
;
130 EMACS_INT misc_objects_consed
;
131 EMACS_INT intervals_consed
;
132 EMACS_INT strings_consed
;
134 /* Number of bytes of consing since GC before another GC should be done. */
136 EMACS_INT gc_cons_threshold
;
138 /* Nonzero during GC. */
142 /* Nonzero means display messages at beginning and end of GC. */
144 int garbage_collection_messages
;
146 #ifndef VIRT_ADDR_VARIES
148 #endif /* VIRT_ADDR_VARIES */
149 int malloc_sbrk_used
;
151 #ifndef VIRT_ADDR_VARIES
153 #endif /* VIRT_ADDR_VARIES */
154 int malloc_sbrk_unused
;
156 /* Two limits controlling how much undo information to keep. */
158 EMACS_INT undo_limit
;
159 EMACS_INT undo_strong_limit
;
161 /* Number of live and free conses etc. */
163 static int total_conses
, total_markers
, total_symbols
, total_vector_size
;
164 static int total_free_conses
, total_free_markers
, total_free_symbols
;
165 static int total_free_floats
, total_floats
;
167 /* Points to memory space allocated as "spare", to be freed if we run
170 static char *spare_memory
;
172 /* Amount of spare memory to keep in reserve. */
174 #define SPARE_MEMORY (1 << 14)
176 /* Number of extra blocks malloc should get when it needs more core. */
178 static int malloc_hysteresis
;
180 /* Non-nil means defun should do purecopy on the function definition. */
182 Lisp_Object Vpurify_flag
;
184 /* Non-nil means we are handling a memory-full error. */
186 Lisp_Object Vmemory_full
;
190 /* Force it into data space! */
192 EMACS_INT pure
[PURESIZE
/ sizeof (EMACS_INT
)] = {0,};
193 #define PUREBEG (char *) pure
197 #define pure PURE_SEG_BITS /* Use shared memory segment */
198 #define PUREBEG (char *)PURE_SEG_BITS
200 #endif /* HAVE_SHM */
202 /* Pointer to the pure area, and its size. */
204 static char *purebeg
;
205 static size_t pure_size
;
207 /* Number of bytes of pure storage used before pure storage overflowed.
208 If this is non-zero, this implies that an overflow occurred. */
210 static size_t pure_bytes_used_before_overflow
;
212 /* Value is non-zero if P points into pure space. */
214 #define PURE_POINTER_P(P) \
215 (((PNTR_COMPARISON_TYPE) (P) \
216 < (PNTR_COMPARISON_TYPE) ((char *) purebeg + pure_size)) \
217 && ((PNTR_COMPARISON_TYPE) (P) \
218 >= (PNTR_COMPARISON_TYPE) purebeg))
220 /* Index in pure at which next pure object will be allocated.. */
222 EMACS_INT pure_bytes_used
;
224 /* If nonzero, this is a warning delivered by malloc and not yet
227 char *pending_malloc_warning
;
229 /* Pre-computed signal argument for use when memory is exhausted. */
231 Lisp_Object Vmemory_signal_data
;
233 /* Maximum amount of C stack to save when a GC happens. */
235 #ifndef MAX_SAVE_STACK
236 #define MAX_SAVE_STACK 16000
239 /* Buffer in which we save a copy of the C stack at each GC. */
244 /* Non-zero means ignore malloc warnings. Set during initialization.
245 Currently not used. */
249 Lisp_Object Qgc_cons_threshold
, Qchar_table_extra_slots
;
251 /* Hook run after GC has finished. */
253 Lisp_Object Vpost_gc_hook
, Qpost_gc_hook
;
255 static void mark_buffer
P_ ((Lisp_Object
));
256 static void mark_kboards
P_ ((void));
257 static void gc_sweep
P_ ((void));
258 static void mark_glyph_matrix
P_ ((struct glyph_matrix
*));
259 static void mark_face_cache
P_ ((struct face_cache
*));
261 #ifdef HAVE_WINDOW_SYSTEM
262 static void mark_image
P_ ((struct image
*));
263 static void mark_image_cache
P_ ((struct frame
*));
264 #endif /* HAVE_WINDOW_SYSTEM */
266 static struct Lisp_String
*allocate_string
P_ ((void));
267 static void compact_small_strings
P_ ((void));
268 static void free_large_strings
P_ ((void));
269 static void sweep_strings
P_ ((void));
271 extern int message_enable_multibyte
;
273 /* When scanning the C stack for live Lisp objects, Emacs keeps track
274 of what memory allocated via lisp_malloc is intended for what
275 purpose. This enumeration specifies the type of memory. */
286 /* Keep the following vector-like types together, with
287 MEM_TYPE_WINDOW being the last, and MEM_TYPE_VECTOR the
288 first. Or change the code of live_vector_p, for instance. */
296 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
298 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
299 #include <stdio.h> /* For fprintf. */
302 /* A unique object in pure space used to make some Lisp objects
303 on free lists recognizable in O(1). */
307 #ifdef GC_MALLOC_CHECK
309 enum mem_type allocated_mem_type
;
310 int dont_register_blocks
;
312 #endif /* GC_MALLOC_CHECK */
314 /* A node in the red-black tree describing allocated memory containing
315 Lisp data. Each such block is recorded with its start and end
316 address when it is allocated, and removed from the tree when it
319 A red-black tree is a balanced binary tree with the following
322 1. Every node is either red or black.
323 2. Every leaf is black.
324 3. If a node is red, then both of its children are black.
325 4. Every simple path from a node to a descendant leaf contains
326 the same number of black nodes.
327 5. The root is always black.
329 When nodes are inserted into the tree, or deleted from the tree,
330 the tree is "fixed" so that these properties are always true.
332 A red-black tree with N internal nodes has height at most 2
333 log(N+1). Searches, insertions and deletions are done in O(log N).
334 Please see a text book about data structures for a detailed
335 description of red-black trees. Any book worth its salt should
340 struct mem_node
*left
, *right
, *parent
;
342 /* Start and end of allocated region. */
346 enum {MEM_BLACK
, MEM_RED
} color
;
352 /* Base address of stack. Set in main. */
354 Lisp_Object
*stack_base
;
356 /* Root of the tree describing allocated Lisp memory. */
358 static struct mem_node
*mem_root
;
360 /* Lowest and highest known address in the heap. */
362 static void *min_heap_address
, *max_heap_address
;
364 /* Sentinel node of the tree. */
366 static struct mem_node mem_z
;
367 #define MEM_NIL &mem_z
369 static POINTER_TYPE
*lisp_malloc
P_ ((size_t, enum mem_type
));
370 static struct Lisp_Vector
*allocate_vectorlike
P_ ((EMACS_INT
, enum mem_type
));
371 static void lisp_free
P_ ((POINTER_TYPE
*));
372 static void mark_stack
P_ ((void));
373 static int live_vector_p
P_ ((struct mem_node
*, void *));
374 static int live_buffer_p
P_ ((struct mem_node
*, void *));
375 static int live_string_p
P_ ((struct mem_node
*, void *));
376 static int live_cons_p
P_ ((struct mem_node
*, void *));
377 static int live_symbol_p
P_ ((struct mem_node
*, void *));
378 static int live_float_p
P_ ((struct mem_node
*, void *));
379 static int live_misc_p
P_ ((struct mem_node
*, void *));
380 static void mark_maybe_object
P_ ((Lisp_Object
));
381 static void mark_memory
P_ ((void *, void *));
382 static void mem_init
P_ ((void));
383 static struct mem_node
*mem_insert
P_ ((void *, void *, enum mem_type
));
384 static void mem_insert_fixup
P_ ((struct mem_node
*));
385 static void mem_rotate_left
P_ ((struct mem_node
*));
386 static void mem_rotate_right
P_ ((struct mem_node
*));
387 static void mem_delete
P_ ((struct mem_node
*));
388 static void mem_delete_fixup
P_ ((struct mem_node
*));
389 static INLINE
struct mem_node
*mem_find
P_ ((void *));
391 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
392 static void check_gcpros
P_ ((void));
395 #endif /* GC_MARK_STACK || GC_MALLOC_CHECK */
397 /* Recording what needs to be marked for gc. */
399 struct gcpro
*gcprolist
;
401 /* Addresses of staticpro'd variables. */
403 #define NSTATICS 1280
404 Lisp_Object
*staticvec
[NSTATICS
] = {0};
406 /* Index of next unused slot in staticvec. */
410 static POINTER_TYPE
*pure_alloc
P_ ((size_t, int));
413 /* Value is SZ rounded up to the next multiple of ALIGNMENT.
414 ALIGNMENT must be a power of 2. */
416 #define ALIGN(SZ, ALIGNMENT) \
417 (((SZ) + (ALIGNMENT) - 1) & ~((ALIGNMENT) - 1))
421 /************************************************************************
423 ************************************************************************/
425 /* Write STR to Vstandard_output plus some advice on how to free some
426 memory. Called when memory gets low. */
429 malloc_warning_1 (str
)
432 Fprinc (str
, Vstandard_output
);
433 write_string ("\nKilling some buffers may delay running out of memory.\n", -1);
434 write_string ("However, certainly by the time you receive the 95% warning,\n", -1);
435 write_string ("you should clean up, kill this Emacs, and start a new one.", -1);
440 /* Function malloc calls this if it finds we are near exhausting
447 pending_malloc_warning
= str
;
451 /* Display a malloc warning in buffer *Danger*. */
454 display_malloc_warning ()
456 register Lisp_Object val
;
458 val
= build_string (pending_malloc_warning
);
459 pending_malloc_warning
= 0;
460 internal_with_output_to_temp_buffer (" *Danger*", malloc_warning_1
, val
);
464 #ifdef DOUG_LEA_MALLOC
465 # define BYTES_USED (mallinfo ().arena)
467 # define BYTES_USED _bytes_used
471 /* Called if malloc returns zero. */
478 #ifndef SYSTEM_MALLOC
479 bytes_used_when_full
= BYTES_USED
;
482 /* The first time we get here, free the spare memory. */
489 /* This used to call error, but if we've run out of memory, we could
490 get infinite recursion trying to build the string. */
492 Fsignal (Qnil
, Vmemory_signal_data
);
496 /* Called if we can't allocate relocatable space for a buffer. */
499 buffer_memory_full ()
501 /* If buffers use the relocating allocator, no need to free
502 spare_memory, because we may have plenty of malloc space left
503 that we could get, and if we don't, the malloc that fails will
504 itself cause spare_memory to be freed. If buffers don't use the
505 relocating allocator, treat this like any other failing
514 /* This used to call error, but if we've run out of memory, we could
515 get infinite recursion trying to build the string. */
517 Fsignal (Qnil
, Vmemory_signal_data
);
521 /* Like malloc but check for no memory and block interrupt input.. */
527 register POINTER_TYPE
*val
;
530 val
= (POINTER_TYPE
*) malloc (size
);
539 /* Like realloc but check for no memory and block interrupt input.. */
542 xrealloc (block
, size
)
546 register POINTER_TYPE
*val
;
549 /* We must call malloc explicitly when BLOCK is 0, since some
550 reallocs don't do this. */
552 val
= (POINTER_TYPE
*) malloc (size
);
554 val
= (POINTER_TYPE
*) realloc (block
, size
);
557 if (!val
&& size
) memory_full ();
562 /* Like free but block interrupt input.. */
574 /* Like strdup, but uses xmalloc. */
580 size_t len
= strlen (s
) + 1;
581 char *p
= (char *) xmalloc (len
);
587 /* Like malloc but used for allocating Lisp data. NBYTES is the
588 number of bytes to allocate, TYPE describes the intended use of the
589 allcated memory block (for strings, for conses, ...). */
591 static POINTER_TYPE
*
592 lisp_malloc (nbytes
, type
)
600 #ifdef GC_MALLOC_CHECK
601 allocated_mem_type
= type
;
604 val
= (void *) malloc (nbytes
);
606 #if GC_MARK_STACK && !defined GC_MALLOC_CHECK
607 if (val
&& type
!= MEM_TYPE_NON_LISP
)
608 mem_insert (val
, (char *) val
+ nbytes
, type
);
618 /* Return a new buffer structure allocated from the heap with
619 a call to lisp_malloc. */
625 = (struct buffer
*) lisp_malloc (sizeof (struct buffer
),
627 VALIDATE_LISP_STORAGE (b
, sizeof *b
);
632 /* Free BLOCK. This must be called to free memory allocated with a
633 call to lisp_malloc. */
641 #if GC_MARK_STACK && !defined GC_MALLOC_CHECK
642 mem_delete (mem_find (block
));
648 /* Arranging to disable input signals while we're in malloc.
650 This only works with GNU malloc. To help out systems which can't
651 use GNU malloc, all the calls to malloc, realloc, and free
652 elsewhere in the code should be inside a BLOCK_INPUT/UNBLOCK_INPUT
653 pairs; unfortunately, we have no idea what C library functions
654 might call malloc, so we can't really protect them unless you're
655 using GNU malloc. Fortunately, most of the major operating can use
658 #ifndef SYSTEM_MALLOC
659 #ifndef DOUG_LEA_MALLOC
660 extern void * (*__malloc_hook
) P_ ((size_t));
661 extern void * (*__realloc_hook
) P_ ((void *, size_t));
662 extern void (*__free_hook
) P_ ((void *));
663 /* Else declared in malloc.h, perhaps with an extra arg. */
664 #endif /* DOUG_LEA_MALLOC */
665 static void * (*old_malloc_hook
) ();
666 static void * (*old_realloc_hook
) ();
667 static void (*old_free_hook
) ();
669 /* This function is used as the hook for free to call. */
672 emacs_blocked_free (ptr
)
677 #ifdef GC_MALLOC_CHECK
683 if (m
== MEM_NIL
|| m
->start
!= ptr
)
686 "Freeing `%p' which wasn't allocated with malloc\n", ptr
);
691 /* fprintf (stderr, "free %p...%p (%p)\n", m->start, m->end, ptr); */
695 #endif /* GC_MALLOC_CHECK */
697 __free_hook
= old_free_hook
;
700 /* If we released our reserve (due to running out of memory),
701 and we have a fair amount free once again,
702 try to set aside another reserve in case we run out once more. */
703 if (spare_memory
== 0
704 /* Verify there is enough space that even with the malloc
705 hysteresis this call won't run out again.
706 The code here is correct as long as SPARE_MEMORY
707 is substantially larger than the block size malloc uses. */
708 && (bytes_used_when_full
709 > BYTES_USED
+ max (malloc_hysteresis
, 4) * SPARE_MEMORY
))
710 spare_memory
= (char *) malloc ((size_t) SPARE_MEMORY
);
712 __free_hook
= emacs_blocked_free
;
717 /* If we released our reserve (due to running out of memory),
718 and we have a fair amount free once again,
719 try to set aside another reserve in case we run out once more.
721 This is called when a relocatable block is freed in ralloc.c. */
724 refill_memory_reserve ()
726 if (spare_memory
== 0)
727 spare_memory
= (char *) malloc ((size_t) SPARE_MEMORY
);
731 /* This function is the malloc hook that Emacs uses. */
734 emacs_blocked_malloc (size
)
740 __malloc_hook
= old_malloc_hook
;
741 #ifdef DOUG_LEA_MALLOC
742 mallopt (M_TOP_PAD
, malloc_hysteresis
* 4096);
744 __malloc_extra_blocks
= malloc_hysteresis
;
747 value
= (void *) malloc (size
);
749 #ifdef GC_MALLOC_CHECK
751 struct mem_node
*m
= mem_find (value
);
754 fprintf (stderr
, "Malloc returned %p which is already in use\n",
756 fprintf (stderr
, "Region in use is %p...%p, %u bytes, type %d\n",
757 m
->start
, m
->end
, (char *) m
->end
- (char *) m
->start
,
762 if (!dont_register_blocks
)
764 mem_insert (value
, (char *) value
+ max (1, size
), allocated_mem_type
);
765 allocated_mem_type
= MEM_TYPE_NON_LISP
;
768 #endif /* GC_MALLOC_CHECK */
770 __malloc_hook
= emacs_blocked_malloc
;
773 /* fprintf (stderr, "%p malloc\n", value); */
778 /* This function is the realloc hook that Emacs uses. */
781 emacs_blocked_realloc (ptr
, size
)
788 __realloc_hook
= old_realloc_hook
;
790 #ifdef GC_MALLOC_CHECK
793 struct mem_node
*m
= mem_find (ptr
);
794 if (m
== MEM_NIL
|| m
->start
!= ptr
)
797 "Realloc of %p which wasn't allocated with malloc\n",
805 /* fprintf (stderr, "%p -> realloc\n", ptr); */
807 /* Prevent malloc from registering blocks. */
808 dont_register_blocks
= 1;
809 #endif /* GC_MALLOC_CHECK */
811 value
= (void *) realloc (ptr
, size
);
813 #ifdef GC_MALLOC_CHECK
814 dont_register_blocks
= 0;
817 struct mem_node
*m
= mem_find (value
);
820 fprintf (stderr
, "Realloc returns memory that is already in use\n");
824 /* Can't handle zero size regions in the red-black tree. */
825 mem_insert (value
, (char *) value
+ max (size
, 1), MEM_TYPE_NON_LISP
);
828 /* fprintf (stderr, "%p <- realloc\n", value); */
829 #endif /* GC_MALLOC_CHECK */
831 __realloc_hook
= emacs_blocked_realloc
;
838 /* Called from main to set up malloc to use our hooks. */
841 uninterrupt_malloc ()
843 if (__free_hook
!= emacs_blocked_free
)
844 old_free_hook
= __free_hook
;
845 __free_hook
= emacs_blocked_free
;
847 if (__malloc_hook
!= emacs_blocked_malloc
)
848 old_malloc_hook
= __malloc_hook
;
849 __malloc_hook
= emacs_blocked_malloc
;
851 if (__realloc_hook
!= emacs_blocked_realloc
)
852 old_realloc_hook
= __realloc_hook
;
853 __realloc_hook
= emacs_blocked_realloc
;
856 #endif /* not SYSTEM_MALLOC */
860 /***********************************************************************
862 ***********************************************************************/
864 /* Number of intervals allocated in an interval_block structure.
865 The 1020 is 1024 minus malloc overhead. */
867 #define INTERVAL_BLOCK_SIZE \
868 ((1020 - sizeof (struct interval_block *)) / sizeof (struct interval))
870 /* Intervals are allocated in chunks in form of an interval_block
873 struct interval_block
875 struct interval_block
*next
;
876 struct interval intervals
[INTERVAL_BLOCK_SIZE
];
879 /* Current interval block. Its `next' pointer points to older
882 struct interval_block
*interval_block
;
884 /* Index in interval_block above of the next unused interval
887 static int interval_block_index
;
889 /* Number of free and live intervals. */
891 static int total_free_intervals
, total_intervals
;
893 /* List of free intervals. */
895 INTERVAL interval_free_list
;
897 /* Total number of interval blocks now in use. */
899 int n_interval_blocks
;
902 /* Initialize interval allocation. */
908 = (struct interval_block
*) lisp_malloc (sizeof *interval_block
,
910 interval_block
->next
= 0;
911 bzero ((char *) interval_block
->intervals
, sizeof interval_block
->intervals
);
912 interval_block_index
= 0;
913 interval_free_list
= 0;
914 n_interval_blocks
= 1;
918 /* Return a new interval. */
925 if (interval_free_list
)
927 val
= interval_free_list
;
928 interval_free_list
= INTERVAL_PARENT (interval_free_list
);
932 if (interval_block_index
== INTERVAL_BLOCK_SIZE
)
934 register struct interval_block
*newi
;
936 newi
= (struct interval_block
*) lisp_malloc (sizeof *newi
,
939 VALIDATE_LISP_STORAGE (newi
, sizeof *newi
);
940 newi
->next
= interval_block
;
941 interval_block
= newi
;
942 interval_block_index
= 0;
945 val
= &interval_block
->intervals
[interval_block_index
++];
947 consing_since_gc
+= sizeof (struct interval
);
949 RESET_INTERVAL (val
);
954 /* Mark Lisp objects in interval I. */
957 mark_interval (i
, dummy
)
961 if (XMARKBIT (i
->plist
))
963 mark_object (&i
->plist
);
968 /* Mark the interval tree rooted in TREE. Don't call this directly;
969 use the macro MARK_INTERVAL_TREE instead. */
972 mark_interval_tree (tree
)
973 register INTERVAL tree
;
975 /* No need to test if this tree has been marked already; this
976 function is always called through the MARK_INTERVAL_TREE macro,
977 which takes care of that. */
979 /* XMARK expands to an assignment; the LHS of an assignment can't be
981 XMARK (tree
->up
.obj
);
983 traverse_intervals_noorder (tree
, mark_interval
, Qnil
);
987 /* Mark the interval tree rooted in I. */
989 #define MARK_INTERVAL_TREE(i) \
991 if (!NULL_INTERVAL_P (i) \
992 && ! XMARKBIT (i->up.obj)) \
993 mark_interval_tree (i); \
997 /* The oddity in the call to XUNMARK is necessary because XUNMARK
998 expands to an assignment to its argument, and most C compilers
999 don't support casts on the left operand of `='. */
1001 #define UNMARK_BALANCE_INTERVALS(i) \
1003 if (! NULL_INTERVAL_P (i)) \
1005 XUNMARK ((i)->up.obj); \
1006 (i) = balance_intervals (i); \
1011 /* Number support. If NO_UNION_TYPE isn't in effect, we
1012 can't create number objects in macros. */
1020 obj
.s
.type
= Lisp_Int
;
1025 /***********************************************************************
1027 ***********************************************************************/
1029 /* Lisp_Strings are allocated in string_block structures. When a new
1030 string_block is allocated, all the Lisp_Strings it contains are
1031 added to a free-list string_free_list. When a new Lisp_String is
1032 needed, it is taken from that list. During the sweep phase of GC,
1033 string_blocks that are entirely free are freed, except two which
1036 String data is allocated from sblock structures. Strings larger
1037 than LARGE_STRING_BYTES, get their own sblock, data for smaller
1038 strings is sub-allocated out of sblocks of size SBLOCK_SIZE.
1040 Sblocks consist internally of sdata structures, one for each
1041 Lisp_String. The sdata structure points to the Lisp_String it
1042 belongs to. The Lisp_String points back to the `u.data' member of
1043 its sdata structure.
1045 When a Lisp_String is freed during GC, it is put back on
1046 string_free_list, and its `data' member and its sdata's `string'
1047 pointer is set to null. The size of the string is recorded in the
1048 `u.nbytes' member of the sdata. So, sdata structures that are no
1049 longer used, can be easily recognized, and it's easy to compact the
1050 sblocks of small strings which we do in compact_small_strings. */
1052 /* Size in bytes of an sblock structure used for small strings. This
1053 is 8192 minus malloc overhead. */
1055 #define SBLOCK_SIZE 8188
1057 /* Strings larger than this are considered large strings. String data
1058 for large strings is allocated from individual sblocks. */
1060 #define LARGE_STRING_BYTES 1024
1062 /* Structure describing string memory sub-allocated from an sblock.
1063 This is where the contents of Lisp strings are stored. */
1067 /* Back-pointer to the string this sdata belongs to. If null, this
1068 structure is free, and the NBYTES member of the union below
1069 contains the string's byte size (the same value that STRING_BYTES
1070 would return if STRING were non-null). If non-null, STRING_BYTES
1071 (STRING) is the size of the data, and DATA contains the string's
1073 struct Lisp_String
*string
;
1075 #ifdef GC_CHECK_STRING_BYTES
1078 unsigned char data
[1];
1080 #define SDATA_NBYTES(S) (S)->nbytes
1081 #define SDATA_DATA(S) (S)->data
1083 #else /* not GC_CHECK_STRING_BYTES */
1087 /* When STRING in non-null. */
1088 unsigned char data
[1];
1090 /* When STRING is null. */
1095 #define SDATA_NBYTES(S) (S)->u.nbytes
1096 #define SDATA_DATA(S) (S)->u.data
1098 #endif /* not GC_CHECK_STRING_BYTES */
1102 /* Structure describing a block of memory which is sub-allocated to
1103 obtain string data memory for strings. Blocks for small strings
1104 are of fixed size SBLOCK_SIZE. Blocks for large strings are made
1105 as large as needed. */
1110 struct sblock
*next
;
1112 /* Pointer to the next free sdata block. This points past the end
1113 of the sblock if there isn't any space left in this block. */
1114 struct sdata
*next_free
;
1116 /* Start of data. */
1117 struct sdata first_data
;
1120 /* Number of Lisp strings in a string_block structure. The 1020 is
1121 1024 minus malloc overhead. */
1123 #define STRINGS_IN_STRING_BLOCK \
1124 ((1020 - sizeof (struct string_block *)) / sizeof (struct Lisp_String))
1126 /* Structure describing a block from which Lisp_String structures
1131 struct string_block
*next
;
1132 struct Lisp_String strings
[STRINGS_IN_STRING_BLOCK
];
1135 /* Head and tail of the list of sblock structures holding Lisp string
1136 data. We always allocate from current_sblock. The NEXT pointers
1137 in the sblock structures go from oldest_sblock to current_sblock. */
1139 static struct sblock
*oldest_sblock
, *current_sblock
;
1141 /* List of sblocks for large strings. */
1143 static struct sblock
*large_sblocks
;
1145 /* List of string_block structures, and how many there are. */
1147 static struct string_block
*string_blocks
;
1148 static int n_string_blocks
;
1150 /* Free-list of Lisp_Strings. */
1152 static struct Lisp_String
*string_free_list
;
1154 /* Number of live and free Lisp_Strings. */
1156 static int total_strings
, total_free_strings
;
1158 /* Number of bytes used by live strings. */
1160 static int total_string_size
;
1162 /* Given a pointer to a Lisp_String S which is on the free-list
1163 string_free_list, return a pointer to its successor in the
1166 #define NEXT_FREE_LISP_STRING(S) (*(struct Lisp_String **) (S))
1168 /* Return a pointer to the sdata structure belonging to Lisp string S.
1169 S must be live, i.e. S->data must not be null. S->data is actually
1170 a pointer to the `u.data' member of its sdata structure; the
1171 structure starts at a constant offset in front of that. */
1173 #ifdef GC_CHECK_STRING_BYTES
1175 #define SDATA_OF_STRING(S) \
1176 ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *) \
1177 - sizeof (EMACS_INT)))
1179 #else /* not GC_CHECK_STRING_BYTES */
1181 #define SDATA_OF_STRING(S) \
1182 ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *)))
1184 #endif /* not GC_CHECK_STRING_BYTES */
1186 /* Value is the size of an sdata structure large enough to hold NBYTES
1187 bytes of string data. The value returned includes a terminating
1188 NUL byte, the size of the sdata structure, and padding. */
1190 #ifdef GC_CHECK_STRING_BYTES
1192 #define SDATA_SIZE(NBYTES) \
1193 ((sizeof (struct Lisp_String *) \
1195 + sizeof (EMACS_INT) \
1196 + sizeof (EMACS_INT) - 1) \
1197 & ~(sizeof (EMACS_INT) - 1))
1199 #else /* not GC_CHECK_STRING_BYTES */
1201 #define SDATA_SIZE(NBYTES) \
1202 ((sizeof (struct Lisp_String *) \
1204 + sizeof (EMACS_INT) - 1) \
1205 & ~(sizeof (EMACS_INT) - 1))
1207 #endif /* not GC_CHECK_STRING_BYTES */
1209 /* Initialize string allocation. Called from init_alloc_once. */
1214 total_strings
= total_free_strings
= total_string_size
= 0;
1215 oldest_sblock
= current_sblock
= large_sblocks
= NULL
;
1216 string_blocks
= NULL
;
1217 n_string_blocks
= 0;
1218 string_free_list
= NULL
;
1222 #ifdef GC_CHECK_STRING_BYTES
1224 static int check_string_bytes_count
;
1226 void check_string_bytes
P_ ((int));
1227 void check_sblock
P_ ((struct sblock
*));
1229 #define CHECK_STRING_BYTES(S) STRING_BYTES (S)
1232 /* Like GC_STRING_BYTES, but with debugging check. */
1236 struct Lisp_String
*s
;
1238 int nbytes
= (s
->size_byte
< 0 ? s
->size
: s
->size_byte
) & ~MARKBIT
;
1239 if (!PURE_POINTER_P (s
)
1241 && nbytes
!= SDATA_NBYTES (SDATA_OF_STRING (s
)))
1246 /* Check validity Lisp strings' string_bytes member in B. */
1252 struct sdata
*from
, *end
, *from_end
;
1256 for (from
= &b
->first_data
; from
< end
; from
= from_end
)
1258 /* Compute the next FROM here because copying below may
1259 overwrite data we need to compute it. */
1262 /* Check that the string size recorded in the string is the
1263 same as the one recorded in the sdata structure. */
1265 CHECK_STRING_BYTES (from
->string
);
1268 nbytes
= GC_STRING_BYTES (from
->string
);
1270 nbytes
= SDATA_NBYTES (from
);
1272 nbytes
= SDATA_SIZE (nbytes
);
1273 from_end
= (struct sdata
*) ((char *) from
+ nbytes
);
1278 /* Check validity of Lisp strings' string_bytes member. ALL_P
1279 non-zero means check all strings, otherwise check only most
1280 recently allocated strings. Used for hunting a bug. */
1283 check_string_bytes (all_p
)
1290 for (b
= large_sblocks
; b
; b
= b
->next
)
1292 struct Lisp_String
*s
= b
->first_data
.string
;
1294 CHECK_STRING_BYTES (s
);
1297 for (b
= oldest_sblock
; b
; b
= b
->next
)
1301 check_sblock (current_sblock
);
1304 #endif /* GC_CHECK_STRING_BYTES */
1307 /* Return a new Lisp_String. */
1309 static struct Lisp_String
*
1312 struct Lisp_String
*s
;
1314 /* If the free-list is empty, allocate a new string_block, and
1315 add all the Lisp_Strings in it to the free-list. */
1316 if (string_free_list
== NULL
)
1318 struct string_block
*b
;
1321 b
= (struct string_block
*) lisp_malloc (sizeof *b
, MEM_TYPE_STRING
);
1322 VALIDATE_LISP_STORAGE (b
, sizeof *b
);
1323 bzero (b
, sizeof *b
);
1324 b
->next
= string_blocks
;
1328 for (i
= STRINGS_IN_STRING_BLOCK
- 1; i
>= 0; --i
)
1331 NEXT_FREE_LISP_STRING (s
) = string_free_list
;
1332 string_free_list
= s
;
1335 total_free_strings
+= STRINGS_IN_STRING_BLOCK
;
1338 /* Pop a Lisp_String off the free-list. */
1339 s
= string_free_list
;
1340 string_free_list
= NEXT_FREE_LISP_STRING (s
);
1342 /* Probably not strictly necessary, but play it safe. */
1343 bzero (s
, sizeof *s
);
1345 --total_free_strings
;
1348 consing_since_gc
+= sizeof *s
;
1350 #ifdef GC_CHECK_STRING_BYTES
1357 if (++check_string_bytes_count
== 200)
1359 check_string_bytes_count
= 0;
1360 check_string_bytes (1);
1363 check_string_bytes (0);
1365 #endif /* GC_CHECK_STRING_BYTES */
1371 /* Set up Lisp_String S for holding NCHARS characters, NBYTES bytes,
1372 plus a NUL byte at the end. Allocate an sdata structure for S, and
1373 set S->data to its `u.data' member. Store a NUL byte at the end of
1374 S->data. Set S->size to NCHARS and S->size_byte to NBYTES. Free
1375 S->data if it was initially non-null. */
1378 allocate_string_data (s
, nchars
, nbytes
)
1379 struct Lisp_String
*s
;
1382 struct sdata
*data
, *old_data
;
1384 int needed
, old_nbytes
;
1386 /* Determine the number of bytes needed to store NBYTES bytes
1388 needed
= SDATA_SIZE (nbytes
);
1390 if (nbytes
> LARGE_STRING_BYTES
)
1392 size_t size
= sizeof *b
- sizeof (struct sdata
) + needed
;
1394 #ifdef DOUG_LEA_MALLOC
1395 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed
1396 because mapped region contents are not preserved in
1398 mallopt (M_MMAP_MAX
, 0);
1401 b
= (struct sblock
*) lisp_malloc (size
, MEM_TYPE_NON_LISP
);
1403 #ifdef DOUG_LEA_MALLOC
1404 /* Back to a reasonable maximum of mmap'ed areas. */
1405 mallopt (M_MMAP_MAX
, MMAP_MAX_AREAS
);
1408 b
->next_free
= &b
->first_data
;
1409 b
->first_data
.string
= NULL
;
1410 b
->next
= large_sblocks
;
1413 else if (current_sblock
== NULL
1414 || (((char *) current_sblock
+ SBLOCK_SIZE
1415 - (char *) current_sblock
->next_free
)
1418 /* Not enough room in the current sblock. */
1419 b
= (struct sblock
*) lisp_malloc (SBLOCK_SIZE
, MEM_TYPE_NON_LISP
);
1420 b
->next_free
= &b
->first_data
;
1421 b
->first_data
.string
= NULL
;
1425 current_sblock
->next
= b
;
1433 old_data
= s
->data
? SDATA_OF_STRING (s
) : NULL
;
1434 old_nbytes
= GC_STRING_BYTES (s
);
1436 data
= b
->next_free
;
1438 s
->data
= SDATA_DATA (data
);
1439 #ifdef GC_CHECK_STRING_BYTES
1440 SDATA_NBYTES (data
) = nbytes
;
1443 s
->size_byte
= nbytes
;
1444 s
->data
[nbytes
] = '\0';
1445 b
->next_free
= (struct sdata
*) ((char *) data
+ needed
);
1447 /* If S had already data assigned, mark that as free by setting its
1448 string back-pointer to null, and recording the size of the data
1452 SDATA_NBYTES (old_data
) = old_nbytes
;
1453 old_data
->string
= NULL
;
1456 consing_since_gc
+= needed
;
1460 /* Sweep and compact strings. */
1465 struct string_block
*b
, *next
;
1466 struct string_block
*live_blocks
= NULL
;
1468 string_free_list
= NULL
;
1469 total_strings
= total_free_strings
= 0;
1470 total_string_size
= 0;
1472 /* Scan strings_blocks, free Lisp_Strings that aren't marked. */
1473 for (b
= string_blocks
; b
; b
= next
)
1476 struct Lisp_String
*free_list_before
= string_free_list
;
1480 for (i
= 0; i
< STRINGS_IN_STRING_BLOCK
; ++i
)
1482 struct Lisp_String
*s
= b
->strings
+ i
;
1486 /* String was not on free-list before. */
1487 if (STRING_MARKED_P (s
))
1489 /* String is live; unmark it and its intervals. */
1492 if (!NULL_INTERVAL_P (s
->intervals
))
1493 UNMARK_BALANCE_INTERVALS (s
->intervals
);
1496 total_string_size
+= STRING_BYTES (s
);
1500 /* String is dead. Put it on the free-list. */
1501 struct sdata
*data
= SDATA_OF_STRING (s
);
1503 /* Save the size of S in its sdata so that we know
1504 how large that is. Reset the sdata's string
1505 back-pointer so that we know it's free. */
1506 #ifdef GC_CHECK_STRING_BYTES
1507 if (GC_STRING_BYTES (s
) != SDATA_NBYTES (data
))
1510 data
->u
.nbytes
= GC_STRING_BYTES (s
);
1512 data
->string
= NULL
;
1514 /* Reset the strings's `data' member so that we
1518 /* Put the string on the free-list. */
1519 NEXT_FREE_LISP_STRING (s
) = string_free_list
;
1520 string_free_list
= s
;
1526 /* S was on the free-list before. Put it there again. */
1527 NEXT_FREE_LISP_STRING (s
) = string_free_list
;
1528 string_free_list
= s
;
1533 /* Free blocks that contain free Lisp_Strings only, except
1534 the first two of them. */
1535 if (nfree
== STRINGS_IN_STRING_BLOCK
1536 && total_free_strings
> STRINGS_IN_STRING_BLOCK
)
1540 string_free_list
= free_list_before
;
1544 total_free_strings
+= nfree
;
1545 b
->next
= live_blocks
;
1550 string_blocks
= live_blocks
;
1551 free_large_strings ();
1552 compact_small_strings ();
1556 /* Free dead large strings. */
1559 free_large_strings ()
1561 struct sblock
*b
, *next
;
1562 struct sblock
*live_blocks
= NULL
;
1564 for (b
= large_sblocks
; b
; b
= next
)
1568 if (b
->first_data
.string
== NULL
)
1572 b
->next
= live_blocks
;
1577 large_sblocks
= live_blocks
;
1581 /* Compact data of small strings. Free sblocks that don't contain
1582 data of live strings after compaction. */
1585 compact_small_strings ()
1587 struct sblock
*b
, *tb
, *next
;
1588 struct sdata
*from
, *to
, *end
, *tb_end
;
1589 struct sdata
*to_end
, *from_end
;
1591 /* TB is the sblock we copy to, TO is the sdata within TB we copy
1592 to, and TB_END is the end of TB. */
1594 tb_end
= (struct sdata
*) ((char *) tb
+ SBLOCK_SIZE
);
1595 to
= &tb
->first_data
;
1597 /* Step through the blocks from the oldest to the youngest. We
1598 expect that old blocks will stabilize over time, so that less
1599 copying will happen this way. */
1600 for (b
= oldest_sblock
; b
; b
= b
->next
)
1603 xassert ((char *) end
<= (char *) b
+ SBLOCK_SIZE
);
1605 for (from
= &b
->first_data
; from
< end
; from
= from_end
)
1607 /* Compute the next FROM here because copying below may
1608 overwrite data we need to compute it. */
1611 #ifdef GC_CHECK_STRING_BYTES
1612 /* Check that the string size recorded in the string is the
1613 same as the one recorded in the sdata structure. */
1615 && GC_STRING_BYTES (from
->string
) != SDATA_NBYTES (from
))
1617 #endif /* GC_CHECK_STRING_BYTES */
1620 nbytes
= GC_STRING_BYTES (from
->string
);
1622 nbytes
= SDATA_NBYTES (from
);
1624 nbytes
= SDATA_SIZE (nbytes
);
1625 from_end
= (struct sdata
*) ((char *) from
+ nbytes
);
1627 /* FROM->string non-null means it's alive. Copy its data. */
1630 /* If TB is full, proceed with the next sblock. */
1631 to_end
= (struct sdata
*) ((char *) to
+ nbytes
);
1632 if (to_end
> tb_end
)
1636 tb_end
= (struct sdata
*) ((char *) tb
+ SBLOCK_SIZE
);
1637 to
= &tb
->first_data
;
1638 to_end
= (struct sdata
*) ((char *) to
+ nbytes
);
1641 /* Copy, and update the string's `data' pointer. */
1644 xassert (tb
!= b
|| to
<= from
);
1645 safe_bcopy ((char *) from
, (char *) to
, nbytes
);
1646 to
->string
->data
= SDATA_DATA (to
);
1649 /* Advance past the sdata we copied to. */
1655 /* The rest of the sblocks following TB don't contain live data, so
1656 we can free them. */
1657 for (b
= tb
->next
; b
; b
= next
)
1665 current_sblock
= tb
;
1669 DEFUN ("make-string", Fmake_string
, Smake_string
, 2, 2, 0,
1670 doc
: /* Return a newly created string of length LENGTH, with each element being INIT.
1671 Both LENGTH and INIT must be numbers. */)
1673 Lisp_Object length
, init
;
1675 register Lisp_Object val
;
1676 register unsigned char *p
, *end
;
1679 CHECK_NATNUM (length
);
1680 CHECK_NUMBER (init
);
1683 if (SINGLE_BYTE_CHAR_P (c
))
1685 nbytes
= XINT (length
);
1686 val
= make_uninit_string (nbytes
);
1688 end
= p
+ SCHARS (val
);
1694 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
1695 int len
= CHAR_STRING (c
, str
);
1697 nbytes
= len
* XINT (length
);
1698 val
= make_uninit_multibyte_string (XINT (length
), nbytes
);
1703 bcopy (str
, p
, len
);
1713 DEFUN ("make-bool-vector", Fmake_bool_vector
, Smake_bool_vector
, 2, 2, 0,
1714 doc
: /* Return a new bool-vector of length LENGTH, using INIT for as each element.
1715 LENGTH must be a number. INIT matters only in whether it is t or nil. */)
1717 Lisp_Object length
, init
;
1719 register Lisp_Object val
;
1720 struct Lisp_Bool_Vector
*p
;
1722 int length_in_chars
, length_in_elts
, bits_per_value
;
1724 CHECK_NATNUM (length
);
1726 bits_per_value
= sizeof (EMACS_INT
) * BITS_PER_CHAR
;
1728 length_in_elts
= (XFASTINT (length
) + bits_per_value
- 1) / bits_per_value
;
1729 length_in_chars
= ((XFASTINT (length
) + BITS_PER_CHAR
- 1) / BITS_PER_CHAR
);
1731 /* We must allocate one more elements than LENGTH_IN_ELTS for the
1732 slot `size' of the struct Lisp_Bool_Vector. */
1733 val
= Fmake_vector (make_number (length_in_elts
+ 1), Qnil
);
1734 p
= XBOOL_VECTOR (val
);
1736 /* Get rid of any bits that would cause confusion. */
1738 XSETBOOL_VECTOR (val
, p
);
1739 p
->size
= XFASTINT (length
);
1741 real_init
= (NILP (init
) ? 0 : -1);
1742 for (i
= 0; i
< length_in_chars
; i
++)
1743 p
->data
[i
] = real_init
;
1745 /* Clear the extraneous bits in the last byte. */
1746 if (XINT (length
) != length_in_chars
* BITS_PER_CHAR
)
1747 XBOOL_VECTOR (val
)->data
[length_in_chars
- 1]
1748 &= (1 << (XINT (length
) % BITS_PER_CHAR
)) - 1;
1754 /* Make a string from NBYTES bytes at CONTENTS, and compute the number
1755 of characters from the contents. This string may be unibyte or
1756 multibyte, depending on the contents. */
1759 make_string (contents
, nbytes
)
1760 const char *contents
;
1763 register Lisp_Object val
;
1764 int nchars
, multibyte_nbytes
;
1766 parse_str_as_multibyte (contents
, nbytes
, &nchars
, &multibyte_nbytes
);
1767 if (nbytes
== nchars
|| nbytes
!= multibyte_nbytes
)
1768 /* CONTENTS contains no multibyte sequences or contains an invalid
1769 multibyte sequence. We must make unibyte string. */
1770 val
= make_unibyte_string (contents
, nbytes
);
1772 val
= make_multibyte_string (contents
, nchars
, nbytes
);
1777 /* Make an unibyte string from LENGTH bytes at CONTENTS. */
1780 make_unibyte_string (contents
, length
)
1781 const char *contents
;
1784 register Lisp_Object val
;
1785 val
= make_uninit_string (length
);
1786 bcopy (contents
, SDATA (val
), length
);
1787 STRING_SET_UNIBYTE (val
);
1792 /* Make a multibyte string from NCHARS characters occupying NBYTES
1793 bytes at CONTENTS. */
1796 make_multibyte_string (contents
, nchars
, nbytes
)
1797 const char *contents
;
1800 register Lisp_Object val
;
1801 val
= make_uninit_multibyte_string (nchars
, nbytes
);
1802 bcopy (contents
, SDATA (val
), nbytes
);
1807 /* Make a string from NCHARS characters occupying NBYTES bytes at
1808 CONTENTS. It is a multibyte string if NBYTES != NCHARS. */
1811 make_string_from_bytes (contents
, nchars
, nbytes
)
1815 register Lisp_Object val
;
1816 val
= make_uninit_multibyte_string (nchars
, nbytes
);
1817 bcopy (contents
, SDATA (val
), nbytes
);
1818 if (SBYTES (val
) == SCHARS (val
))
1819 STRING_SET_UNIBYTE (val
);
1824 /* Make a string from NCHARS characters occupying NBYTES bytes at
1825 CONTENTS. The argument MULTIBYTE controls whether to label the
1826 string as multibyte. */
1829 make_specified_string (contents
, nchars
, nbytes
, multibyte
)
1834 register Lisp_Object val
;
1835 val
= make_uninit_multibyte_string (nchars
, nbytes
);
1836 bcopy (contents
, SDATA (val
), nbytes
);
1838 STRING_SET_UNIBYTE (val
);
1843 /* Make a string from the data at STR, treating it as multibyte if the
1850 return make_string (str
, strlen (str
));
1854 /* Return an unibyte Lisp_String set up to hold LENGTH characters
1855 occupying LENGTH bytes. */
1858 make_uninit_string (length
)
1862 val
= make_uninit_multibyte_string (length
, length
);
1863 STRING_SET_UNIBYTE (val
);
1868 /* Return a multibyte Lisp_String set up to hold NCHARS characters
1869 which occupy NBYTES bytes. */
1872 make_uninit_multibyte_string (nchars
, nbytes
)
1876 struct Lisp_String
*s
;
1881 s
= allocate_string ();
1882 allocate_string_data (s
, nchars
, nbytes
);
1883 XSETSTRING (string
, s
);
1884 string_chars_consed
+= nbytes
;
1890 /***********************************************************************
1892 ***********************************************************************/
1894 /* We store float cells inside of float_blocks, allocating a new
1895 float_block with malloc whenever necessary. Float cells reclaimed
1896 by GC are put on a free list to be reallocated before allocating
1897 any new float cells from the latest float_block.
1899 Each float_block is just under 1020 bytes long, since malloc really
1900 allocates in units of powers of two and uses 4 bytes for its own
1903 #define FLOAT_BLOCK_SIZE \
1904 ((1020 - sizeof (struct float_block *)) / sizeof (struct Lisp_Float))
1908 struct float_block
*next
;
1909 struct Lisp_Float floats
[FLOAT_BLOCK_SIZE
];
1912 /* Current float_block. */
1914 struct float_block
*float_block
;
1916 /* Index of first unused Lisp_Float in the current float_block. */
1918 int float_block_index
;
1920 /* Total number of float blocks now in use. */
1924 /* Free-list of Lisp_Floats. */
1926 struct Lisp_Float
*float_free_list
;
1929 /* Initialize float allocation. */
1934 float_block
= (struct float_block
*) lisp_malloc (sizeof *float_block
,
1936 float_block
->next
= 0;
1937 bzero ((char *) float_block
->floats
, sizeof float_block
->floats
);
1938 float_block_index
= 0;
1939 float_free_list
= 0;
1944 /* Explicitly free a float cell by putting it on the free-list. */
1948 struct Lisp_Float
*ptr
;
1950 *(struct Lisp_Float
**)&ptr
->data
= float_free_list
;
1954 float_free_list
= ptr
;
1958 /* Return a new float object with value FLOAT_VALUE. */
1961 make_float (float_value
)
1964 register Lisp_Object val
;
1966 if (float_free_list
)
1968 /* We use the data field for chaining the free list
1969 so that we won't use the same field that has the mark bit. */
1970 XSETFLOAT (val
, float_free_list
);
1971 float_free_list
= *(struct Lisp_Float
**)&float_free_list
->data
;
1975 if (float_block_index
== FLOAT_BLOCK_SIZE
)
1977 register struct float_block
*new;
1979 new = (struct float_block
*) lisp_malloc (sizeof *new,
1981 VALIDATE_LISP_STORAGE (new, sizeof *new);
1982 new->next
= float_block
;
1984 float_block_index
= 0;
1987 XSETFLOAT (val
, &float_block
->floats
[float_block_index
++]);
1990 XFLOAT_DATA (val
) = float_value
;
1991 XSETFASTINT (XFLOAT (val
)->type
, 0); /* bug chasing -wsr */
1992 consing_since_gc
+= sizeof (struct Lisp_Float
);
1999 /***********************************************************************
2001 ***********************************************************************/
2003 /* We store cons cells inside of cons_blocks, allocating a new
2004 cons_block with malloc whenever necessary. Cons cells reclaimed by
2005 GC are put on a free list to be reallocated before allocating
2006 any new cons cells from the latest cons_block.
2008 Each cons_block is just under 1020 bytes long,
2009 since malloc really allocates in units of powers of two
2010 and uses 4 bytes for its own overhead. */
2012 #define CONS_BLOCK_SIZE \
2013 ((1020 - sizeof (struct cons_block *)) / sizeof (struct Lisp_Cons))
2017 struct cons_block
*next
;
2018 struct Lisp_Cons conses
[CONS_BLOCK_SIZE
];
2021 /* Current cons_block. */
2023 struct cons_block
*cons_block
;
2025 /* Index of first unused Lisp_Cons in the current block. */
2027 int cons_block_index
;
2029 /* Free-list of Lisp_Cons structures. */
2031 struct Lisp_Cons
*cons_free_list
;
2033 /* Total number of cons blocks now in use. */
2038 /* Initialize cons allocation. */
2043 cons_block
= (struct cons_block
*) lisp_malloc (sizeof *cons_block
,
2045 cons_block
->next
= 0;
2046 bzero ((char *) cons_block
->conses
, sizeof cons_block
->conses
);
2047 cons_block_index
= 0;
2053 /* Explicitly free a cons cell by putting it on the free-list. */
2057 struct Lisp_Cons
*ptr
;
2059 *(struct Lisp_Cons
**)&ptr
->cdr
= cons_free_list
;
2063 cons_free_list
= ptr
;
2067 DEFUN ("cons", Fcons
, Scons
, 2, 2, 0,
2068 doc
: /* Create a new cons, give it CAR and CDR as components, and return it. */)
2070 Lisp_Object car
, cdr
;
2072 register Lisp_Object val
;
2076 /* We use the cdr for chaining the free list
2077 so that we won't use the same field that has the mark bit. */
2078 XSETCONS (val
, cons_free_list
);
2079 cons_free_list
= *(struct Lisp_Cons
**)&cons_free_list
->cdr
;
2083 if (cons_block_index
== CONS_BLOCK_SIZE
)
2085 register struct cons_block
*new;
2086 new = (struct cons_block
*) lisp_malloc (sizeof *new,
2088 VALIDATE_LISP_STORAGE (new, sizeof *new);
2089 new->next
= cons_block
;
2091 cons_block_index
= 0;
2094 XSETCONS (val
, &cons_block
->conses
[cons_block_index
++]);
2099 consing_since_gc
+= sizeof (struct Lisp_Cons
);
2100 cons_cells_consed
++;
2105 /* Make a list of 2, 3, 4 or 5 specified objects. */
2109 Lisp_Object arg1
, arg2
;
2111 return Fcons (arg1
, Fcons (arg2
, Qnil
));
2116 list3 (arg1
, arg2
, arg3
)
2117 Lisp_Object arg1
, arg2
, arg3
;
2119 return Fcons (arg1
, Fcons (arg2
, Fcons (arg3
, Qnil
)));
2124 list4 (arg1
, arg2
, arg3
, arg4
)
2125 Lisp_Object arg1
, arg2
, arg3
, arg4
;
2127 return Fcons (arg1
, Fcons (arg2
, Fcons (arg3
, Fcons (arg4
, Qnil
))));
2132 list5 (arg1
, arg2
, arg3
, arg4
, arg5
)
2133 Lisp_Object arg1
, arg2
, arg3
, arg4
, arg5
;
2135 return Fcons (arg1
, Fcons (arg2
, Fcons (arg3
, Fcons (arg4
,
2136 Fcons (arg5
, Qnil
)))));
2140 DEFUN ("list", Flist
, Slist
, 0, MANY
, 0,
2141 doc
: /* Return a newly created list with specified arguments as elements.
2142 Any number of arguments, even zero arguments, are allowed.
2143 usage: (list &rest OBJECTS) */)
2146 register Lisp_Object
*args
;
2148 register Lisp_Object val
;
2154 val
= Fcons (args
[nargs
], val
);
2160 DEFUN ("make-list", Fmake_list
, Smake_list
, 2, 2, 0,
2161 doc
: /* Return a newly created list of length LENGTH, with each element being INIT. */)
2163 register Lisp_Object length
, init
;
2165 register Lisp_Object val
;
2168 CHECK_NATNUM (length
);
2169 size
= XFASTINT (length
);
2174 val
= Fcons (init
, val
);
2179 val
= Fcons (init
, val
);
2184 val
= Fcons (init
, val
);
2189 val
= Fcons (init
, val
);
2194 val
= Fcons (init
, val
);
2209 /***********************************************************************
2211 ***********************************************************************/
2213 /* Singly-linked list of all vectors. */
2215 struct Lisp_Vector
*all_vectors
;
2217 /* Total number of vector-like objects now in use. */
2222 /* Value is a pointer to a newly allocated Lisp_Vector structure
2223 with room for LEN Lisp_Objects. */
2225 static struct Lisp_Vector
*
2226 allocate_vectorlike (len
, type
)
2230 struct Lisp_Vector
*p
;
2233 #ifdef DOUG_LEA_MALLOC
2234 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed
2235 because mapped region contents are not preserved in
2237 mallopt (M_MMAP_MAX
, 0);
2240 nbytes
= sizeof *p
+ (len
- 1) * sizeof p
->contents
[0];
2241 p
= (struct Lisp_Vector
*) lisp_malloc (nbytes
, type
);
2243 #ifdef DOUG_LEA_MALLOC
2244 /* Back to a reasonable maximum of mmap'ed areas. */
2245 mallopt (M_MMAP_MAX
, MMAP_MAX_AREAS
);
2248 VALIDATE_LISP_STORAGE (p
, 0);
2249 consing_since_gc
+= nbytes
;
2250 vector_cells_consed
+= len
;
2252 p
->next
= all_vectors
;
2259 /* Allocate a vector with NSLOTS slots. */
2261 struct Lisp_Vector
*
2262 allocate_vector (nslots
)
2265 struct Lisp_Vector
*v
= allocate_vectorlike (nslots
, MEM_TYPE_VECTOR
);
2271 /* Allocate other vector-like structures. */
2273 struct Lisp_Hash_Table
*
2274 allocate_hash_table ()
2276 EMACS_INT len
= VECSIZE (struct Lisp_Hash_Table
);
2277 struct Lisp_Vector
*v
= allocate_vectorlike (len
, MEM_TYPE_HASH_TABLE
);
2281 for (i
= 0; i
< len
; ++i
)
2282 v
->contents
[i
] = Qnil
;
2284 return (struct Lisp_Hash_Table
*) v
;
2291 EMACS_INT len
= VECSIZE (struct window
);
2292 struct Lisp_Vector
*v
= allocate_vectorlike (len
, MEM_TYPE_WINDOW
);
2295 for (i
= 0; i
< len
; ++i
)
2296 v
->contents
[i
] = Qnil
;
2299 return (struct window
*) v
;
2306 EMACS_INT len
= VECSIZE (struct frame
);
2307 struct Lisp_Vector
*v
= allocate_vectorlike (len
, MEM_TYPE_FRAME
);
2310 for (i
= 0; i
< len
; ++i
)
2311 v
->contents
[i
] = make_number (0);
2313 return (struct frame
*) v
;
2317 struct Lisp_Process
*
2320 EMACS_INT len
= VECSIZE (struct Lisp_Process
);
2321 struct Lisp_Vector
*v
= allocate_vectorlike (len
, MEM_TYPE_PROCESS
);
2324 for (i
= 0; i
< len
; ++i
)
2325 v
->contents
[i
] = Qnil
;
2328 return (struct Lisp_Process
*) v
;
2332 struct Lisp_Vector
*
2333 allocate_other_vector (len
)
2336 struct Lisp_Vector
*v
= allocate_vectorlike (len
, MEM_TYPE_VECTOR
);
2339 for (i
= 0; i
< len
; ++i
)
2340 v
->contents
[i
] = Qnil
;
2347 DEFUN ("make-vector", Fmake_vector
, Smake_vector
, 2, 2, 0,
2348 doc
: /* Return a newly created vector of length LENGTH, with each element being INIT.
2349 See also the function `vector'. */)
2351 register Lisp_Object length
, init
;
2354 register EMACS_INT sizei
;
2356 register struct Lisp_Vector
*p
;
2358 CHECK_NATNUM (length
);
2359 sizei
= XFASTINT (length
);
2361 p
= allocate_vector (sizei
);
2362 for (index
= 0; index
< sizei
; index
++)
2363 p
->contents
[index
] = init
;
2365 XSETVECTOR (vector
, p
);
2370 DEFUN ("make-char-table", Fmake_char_table
, Smake_char_table
, 1, 2, 0,
2371 doc
: /* Return a newly created char-table, with purpose PURPOSE.
2372 Each element is initialized to INIT, which defaults to nil.
2373 PURPOSE should be a symbol which has a `char-table-extra-slots' property.
2374 The property's value should be an integer between 0 and 10. */)
2376 register Lisp_Object purpose
, init
;
2380 CHECK_SYMBOL (purpose
);
2381 n
= Fget (purpose
, Qchar_table_extra_slots
);
2383 if (XINT (n
) < 0 || XINT (n
) > 10)
2384 args_out_of_range (n
, Qnil
);
2385 /* Add 2 to the size for the defalt and parent slots. */
2386 vector
= Fmake_vector (make_number (CHAR_TABLE_STANDARD_SLOTS
+ XINT (n
)),
2388 XCHAR_TABLE (vector
)->top
= Qt
;
2389 XCHAR_TABLE (vector
)->parent
= Qnil
;
2390 XCHAR_TABLE (vector
)->purpose
= purpose
;
2391 XSETCHAR_TABLE (vector
, XCHAR_TABLE (vector
));
2396 /* Return a newly created sub char table with default value DEFALT.
2397 Since a sub char table does not appear as a top level Emacs Lisp
2398 object, we don't need a Lisp interface to make it. */
2401 make_sub_char_table (defalt
)
2405 = Fmake_vector (make_number (SUB_CHAR_TABLE_STANDARD_SLOTS
), Qnil
);
2406 XCHAR_TABLE (vector
)->top
= Qnil
;
2407 XCHAR_TABLE (vector
)->defalt
= defalt
;
2408 XSETCHAR_TABLE (vector
, XCHAR_TABLE (vector
));
2413 DEFUN ("vector", Fvector
, Svector
, 0, MANY
, 0,
2414 doc
: /* Return a newly created vector with specified arguments as elements.
2415 Any number of arguments, even zero arguments, are allowed.
2416 usage: (vector &rest OBJECTS) */)
2421 register Lisp_Object len
, val
;
2423 register struct Lisp_Vector
*p
;
2425 XSETFASTINT (len
, nargs
);
2426 val
= Fmake_vector (len
, Qnil
);
2428 for (index
= 0; index
< nargs
; index
++)
2429 p
->contents
[index
] = args
[index
];
2434 DEFUN ("make-byte-code", Fmake_byte_code
, Smake_byte_code
, 4, MANY
, 0,
2435 doc
: /* Create a byte-code object with specified arguments as elements.
2436 The arguments should be the arglist, bytecode-string, constant vector,
2437 stack size, (optional) doc string, and (optional) interactive spec.
2438 The first four arguments are required; at most six have any
2440 usage: (make-byte-code &rest ELEMENTS) */)
2445 register Lisp_Object len
, val
;
2447 register struct Lisp_Vector
*p
;
2449 XSETFASTINT (len
, nargs
);
2450 if (!NILP (Vpurify_flag
))
2451 val
= make_pure_vector ((EMACS_INT
) nargs
);
2453 val
= Fmake_vector (len
, Qnil
);
2455 if (STRINGP (args
[1]) && STRING_MULTIBYTE (args
[1]))
2456 /* BYTECODE-STRING must have been produced by Emacs 20.2 or the
2457 earlier because they produced a raw 8-bit string for byte-code
2458 and now such a byte-code string is loaded as multibyte while
2459 raw 8-bit characters converted to multibyte form. Thus, now we
2460 must convert them back to the original unibyte form. */
2461 args
[1] = Fstring_as_unibyte (args
[1]);
2464 for (index
= 0; index
< nargs
; index
++)
2466 if (!NILP (Vpurify_flag
))
2467 args
[index
] = Fpurecopy (args
[index
]);
2468 p
->contents
[index
] = args
[index
];
2470 XSETCOMPILED (val
, p
);
2476 /***********************************************************************
2478 ***********************************************************************/
2480 /* Each symbol_block is just under 1020 bytes long, since malloc
2481 really allocates in units of powers of two and uses 4 bytes for its
2484 #define SYMBOL_BLOCK_SIZE \
2485 ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol))
2489 struct symbol_block
*next
;
2490 struct Lisp_Symbol symbols
[SYMBOL_BLOCK_SIZE
];
2493 /* Current symbol block and index of first unused Lisp_Symbol
2496 struct symbol_block
*symbol_block
;
2497 int symbol_block_index
;
2499 /* List of free symbols. */
2501 struct Lisp_Symbol
*symbol_free_list
;
2503 /* Total number of symbol blocks now in use. */
2505 int n_symbol_blocks
;
2508 /* Initialize symbol allocation. */
2513 symbol_block
= (struct symbol_block
*) lisp_malloc (sizeof *symbol_block
,
2515 symbol_block
->next
= 0;
2516 bzero ((char *) symbol_block
->symbols
, sizeof symbol_block
->symbols
);
2517 symbol_block_index
= 0;
2518 symbol_free_list
= 0;
2519 n_symbol_blocks
= 1;
2523 DEFUN ("make-symbol", Fmake_symbol
, Smake_symbol
, 1, 1, 0,
2524 doc
: /* Return a newly allocated uninterned symbol whose name is NAME.
2525 Its value and function definition are void, and its property list is nil. */)
2529 register Lisp_Object val
;
2530 register struct Lisp_Symbol
*p
;
2532 CHECK_STRING (name
);
2534 if (symbol_free_list
)
2536 XSETSYMBOL (val
, symbol_free_list
);
2537 symbol_free_list
= *(struct Lisp_Symbol
**)&symbol_free_list
->value
;
2541 if (symbol_block_index
== SYMBOL_BLOCK_SIZE
)
2543 struct symbol_block
*new;
2544 new = (struct symbol_block
*) lisp_malloc (sizeof *new,
2546 VALIDATE_LISP_STORAGE (new, sizeof *new);
2547 new->next
= symbol_block
;
2549 symbol_block_index
= 0;
2552 XSETSYMBOL (val
, &symbol_block
->symbols
[symbol_block_index
++]);
2558 p
->value
= Qunbound
;
2559 p
->function
= Qunbound
;
2561 p
->interned
= SYMBOL_UNINTERNED
;
2563 p
->indirect_variable
= 0;
2564 consing_since_gc
+= sizeof (struct Lisp_Symbol
);
2571 /***********************************************************************
2572 Marker (Misc) Allocation
2573 ***********************************************************************/
2575 /* Allocation of markers and other objects that share that structure.
2576 Works like allocation of conses. */
2578 #define MARKER_BLOCK_SIZE \
2579 ((1020 - sizeof (struct marker_block *)) / sizeof (union Lisp_Misc))
2583 struct marker_block
*next
;
2584 union Lisp_Misc markers
[MARKER_BLOCK_SIZE
];
2587 struct marker_block
*marker_block
;
2588 int marker_block_index
;
2590 union Lisp_Misc
*marker_free_list
;
2592 /* Total number of marker blocks now in use. */
2594 int n_marker_blocks
;
2599 marker_block
= (struct marker_block
*) lisp_malloc (sizeof *marker_block
,
2601 marker_block
->next
= 0;
2602 bzero ((char *) marker_block
->markers
, sizeof marker_block
->markers
);
2603 marker_block_index
= 0;
2604 marker_free_list
= 0;
2605 n_marker_blocks
= 1;
2608 /* Return a newly allocated Lisp_Misc object, with no substructure. */
2615 if (marker_free_list
)
2617 XSETMISC (val
, marker_free_list
);
2618 marker_free_list
= marker_free_list
->u_free
.chain
;
2622 if (marker_block_index
== MARKER_BLOCK_SIZE
)
2624 struct marker_block
*new;
2625 new = (struct marker_block
*) lisp_malloc (sizeof *new,
2627 VALIDATE_LISP_STORAGE (new, sizeof *new);
2628 new->next
= marker_block
;
2630 marker_block_index
= 0;
2633 XSETMISC (val
, &marker_block
->markers
[marker_block_index
++]);
2636 consing_since_gc
+= sizeof (union Lisp_Misc
);
2637 misc_objects_consed
++;
2641 DEFUN ("make-marker", Fmake_marker
, Smake_marker
, 0, 0, 0,
2642 doc
: /* Return a newly allocated marker which does not point at any place. */)
2645 register Lisp_Object val
;
2646 register struct Lisp_Marker
*p
;
2648 val
= allocate_misc ();
2649 XMISCTYPE (val
) = Lisp_Misc_Marker
;
2655 p
->insertion_type
= 0;
2659 /* Put MARKER back on the free list after using it temporarily. */
2662 free_marker (marker
)
2665 unchain_marker (marker
);
2667 XMISC (marker
)->u_marker
.type
= Lisp_Misc_Free
;
2668 XMISC (marker
)->u_free
.chain
= marker_free_list
;
2669 marker_free_list
= XMISC (marker
);
2671 total_free_markers
++;
2675 /* Return a newly created vector or string with specified arguments as
2676 elements. If all the arguments are characters that can fit
2677 in a string of events, make a string; otherwise, make a vector.
2679 Any number of arguments, even zero arguments, are allowed. */
2682 make_event_array (nargs
, args
)
2688 for (i
= 0; i
< nargs
; i
++)
2689 /* The things that fit in a string
2690 are characters that are in 0...127,
2691 after discarding the meta bit and all the bits above it. */
2692 if (!INTEGERP (args
[i
])
2693 || (XUINT (args
[i
]) & ~(-CHAR_META
)) >= 0200)
2694 return Fvector (nargs
, args
);
2696 /* Since the loop exited, we know that all the things in it are
2697 characters, so we can make a string. */
2701 result
= Fmake_string (make_number (nargs
), make_number (0));
2702 for (i
= 0; i
< nargs
; i
++)
2704 SSET (result
, i
, XINT (args
[i
]));
2705 /* Move the meta bit to the right place for a string char. */
2706 if (XINT (args
[i
]) & CHAR_META
)
2707 SSET (result
, i
, SREF (result
, i
) | 0x80);
2716 /************************************************************************
2718 ************************************************************************/
2720 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
2722 /* Conservative C stack marking requires a method to identify possibly
2723 live Lisp objects given a pointer value. We do this by keeping
2724 track of blocks of Lisp data that are allocated in a red-black tree
2725 (see also the comment of mem_node which is the type of nodes in
2726 that tree). Function lisp_malloc adds information for an allocated
2727 block to the red-black tree with calls to mem_insert, and function
2728 lisp_free removes it with mem_delete. Functions live_string_p etc
2729 call mem_find to lookup information about a given pointer in the
2730 tree, and use that to determine if the pointer points to a Lisp
2733 /* Initialize this part of alloc.c. */
2738 mem_z
.left
= mem_z
.right
= MEM_NIL
;
2739 mem_z
.parent
= NULL
;
2740 mem_z
.color
= MEM_BLACK
;
2741 mem_z
.start
= mem_z
.end
= NULL
;
2746 /* Value is a pointer to the mem_node containing START. Value is
2747 MEM_NIL if there is no node in the tree containing START. */
2749 static INLINE
struct mem_node
*
2755 if (start
< min_heap_address
|| start
> max_heap_address
)
2758 /* Make the search always successful to speed up the loop below. */
2759 mem_z
.start
= start
;
2760 mem_z
.end
= (char *) start
+ 1;
2763 while (start
< p
->start
|| start
>= p
->end
)
2764 p
= start
< p
->start
? p
->left
: p
->right
;
2769 /* Insert a new node into the tree for a block of memory with start
2770 address START, end address END, and type TYPE. Value is a
2771 pointer to the node that was inserted. */
2773 static struct mem_node
*
2774 mem_insert (start
, end
, type
)
2778 struct mem_node
*c
, *parent
, *x
;
2780 if (start
< min_heap_address
)
2781 min_heap_address
= start
;
2782 if (end
> max_heap_address
)
2783 max_heap_address
= end
;
2785 /* See where in the tree a node for START belongs. In this
2786 particular application, it shouldn't happen that a node is already
2787 present. For debugging purposes, let's check that. */
2791 #if GC_MARK_STACK != GC_MAKE_GCPROS_NOOPS
2793 while (c
!= MEM_NIL
)
2795 if (start
>= c
->start
&& start
< c
->end
)
2798 c
= start
< c
->start
? c
->left
: c
->right
;
2801 #else /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
2803 while (c
!= MEM_NIL
)
2806 c
= start
< c
->start
? c
->left
: c
->right
;
2809 #endif /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
2811 /* Create a new node. */
2812 #ifdef GC_MALLOC_CHECK
2813 x
= (struct mem_node
*) _malloc_internal (sizeof *x
);
2817 x
= (struct mem_node
*) xmalloc (sizeof *x
);
2823 x
->left
= x
->right
= MEM_NIL
;
2826 /* Insert it as child of PARENT or install it as root. */
2829 if (start
< parent
->start
)
2837 /* Re-establish red-black tree properties. */
2838 mem_insert_fixup (x
);
2844 /* Re-establish the red-black properties of the tree, and thereby
2845 balance the tree, after node X has been inserted; X is always red. */
2848 mem_insert_fixup (x
)
2851 while (x
!= mem_root
&& x
->parent
->color
== MEM_RED
)
2853 /* X is red and its parent is red. This is a violation of
2854 red-black tree property #3. */
2856 if (x
->parent
== x
->parent
->parent
->left
)
2858 /* We're on the left side of our grandparent, and Y is our
2860 struct mem_node
*y
= x
->parent
->parent
->right
;
2862 if (y
->color
== MEM_RED
)
2864 /* Uncle and parent are red but should be black because
2865 X is red. Change the colors accordingly and proceed
2866 with the grandparent. */
2867 x
->parent
->color
= MEM_BLACK
;
2868 y
->color
= MEM_BLACK
;
2869 x
->parent
->parent
->color
= MEM_RED
;
2870 x
= x
->parent
->parent
;
2874 /* Parent and uncle have different colors; parent is
2875 red, uncle is black. */
2876 if (x
== x
->parent
->right
)
2879 mem_rotate_left (x
);
2882 x
->parent
->color
= MEM_BLACK
;
2883 x
->parent
->parent
->color
= MEM_RED
;
2884 mem_rotate_right (x
->parent
->parent
);
2889 /* This is the symmetrical case of above. */
2890 struct mem_node
*y
= x
->parent
->parent
->left
;
2892 if (y
->color
== MEM_RED
)
2894 x
->parent
->color
= MEM_BLACK
;
2895 y
->color
= MEM_BLACK
;
2896 x
->parent
->parent
->color
= MEM_RED
;
2897 x
= x
->parent
->parent
;
2901 if (x
== x
->parent
->left
)
2904 mem_rotate_right (x
);
2907 x
->parent
->color
= MEM_BLACK
;
2908 x
->parent
->parent
->color
= MEM_RED
;
2909 mem_rotate_left (x
->parent
->parent
);
2914 /* The root may have been changed to red due to the algorithm. Set
2915 it to black so that property #5 is satisfied. */
2916 mem_root
->color
= MEM_BLACK
;
2932 /* Turn y's left sub-tree into x's right sub-tree. */
2935 if (y
->left
!= MEM_NIL
)
2936 y
->left
->parent
= x
;
2938 /* Y's parent was x's parent. */
2940 y
->parent
= x
->parent
;
2942 /* Get the parent to point to y instead of x. */
2945 if (x
== x
->parent
->left
)
2946 x
->parent
->left
= y
;
2948 x
->parent
->right
= y
;
2953 /* Put x on y's left. */
2967 mem_rotate_right (x
)
2970 struct mem_node
*y
= x
->left
;
2973 if (y
->right
!= MEM_NIL
)
2974 y
->right
->parent
= x
;
2977 y
->parent
= x
->parent
;
2980 if (x
== x
->parent
->right
)
2981 x
->parent
->right
= y
;
2983 x
->parent
->left
= y
;
2994 /* Delete node Z from the tree. If Z is null or MEM_NIL, do nothing. */
3000 struct mem_node
*x
, *y
;
3002 if (!z
|| z
== MEM_NIL
)
3005 if (z
->left
== MEM_NIL
|| z
->right
== MEM_NIL
)
3010 while (y
->left
!= MEM_NIL
)
3014 if (y
->left
!= MEM_NIL
)
3019 x
->parent
= y
->parent
;
3022 if (y
== y
->parent
->left
)
3023 y
->parent
->left
= x
;
3025 y
->parent
->right
= x
;
3032 z
->start
= y
->start
;
3037 if (y
->color
== MEM_BLACK
)
3038 mem_delete_fixup (x
);
3040 #ifdef GC_MALLOC_CHECK
3048 /* Re-establish the red-black properties of the tree, after a
3052 mem_delete_fixup (x
)
3055 while (x
!= mem_root
&& x
->color
== MEM_BLACK
)
3057 if (x
== x
->parent
->left
)
3059 struct mem_node
*w
= x
->parent
->right
;
3061 if (w
->color
== MEM_RED
)
3063 w
->color
= MEM_BLACK
;
3064 x
->parent
->color
= MEM_RED
;
3065 mem_rotate_left (x
->parent
);
3066 w
= x
->parent
->right
;
3069 if (w
->left
->color
== MEM_BLACK
&& w
->right
->color
== MEM_BLACK
)
3076 if (w
->right
->color
== MEM_BLACK
)
3078 w
->left
->color
= MEM_BLACK
;
3080 mem_rotate_right (w
);
3081 w
= x
->parent
->right
;
3083 w
->color
= x
->parent
->color
;
3084 x
->parent
->color
= MEM_BLACK
;
3085 w
->right
->color
= MEM_BLACK
;
3086 mem_rotate_left (x
->parent
);
3092 struct mem_node
*w
= x
->parent
->left
;
3094 if (w
->color
== MEM_RED
)
3096 w
->color
= MEM_BLACK
;
3097 x
->parent
->color
= MEM_RED
;
3098 mem_rotate_right (x
->parent
);
3099 w
= x
->parent
->left
;
3102 if (w
->right
->color
== MEM_BLACK
&& w
->left
->color
== MEM_BLACK
)
3109 if (w
->left
->color
== MEM_BLACK
)
3111 w
->right
->color
= MEM_BLACK
;
3113 mem_rotate_left (w
);
3114 w
= x
->parent
->left
;
3117 w
->color
= x
->parent
->color
;
3118 x
->parent
->color
= MEM_BLACK
;
3119 w
->left
->color
= MEM_BLACK
;
3120 mem_rotate_right (x
->parent
);
3126 x
->color
= MEM_BLACK
;
3130 /* Value is non-zero if P is a pointer to a live Lisp string on
3131 the heap. M is a pointer to the mem_block for P. */
3134 live_string_p (m
, p
)
3138 if (m
->type
== MEM_TYPE_STRING
)
3140 struct string_block
*b
= (struct string_block
*) m
->start
;
3141 int offset
= (char *) p
- (char *) &b
->strings
[0];
3143 /* P must point to the start of a Lisp_String structure, and it
3144 must not be on the free-list. */
3146 && offset
% sizeof b
->strings
[0] == 0
3147 && ((struct Lisp_String
*) p
)->data
!= NULL
);
3154 /* Value is non-zero if P is a pointer to a live Lisp cons on
3155 the heap. M is a pointer to the mem_block for P. */
3162 if (m
->type
== MEM_TYPE_CONS
)
3164 struct cons_block
*b
= (struct cons_block
*) m
->start
;
3165 int offset
= (char *) p
- (char *) &b
->conses
[0];
3167 /* P must point to the start of a Lisp_Cons, not be
3168 one of the unused cells in the current cons block,
3169 and not be on the free-list. */
3171 && offset
% sizeof b
->conses
[0] == 0
3173 || offset
/ sizeof b
->conses
[0] < cons_block_index
)
3174 && !EQ (((struct Lisp_Cons
*) p
)->car
, Vdead
));
3181 /* Value is non-zero if P is a pointer to a live Lisp symbol on
3182 the heap. M is a pointer to the mem_block for P. */
3185 live_symbol_p (m
, p
)
3189 if (m
->type
== MEM_TYPE_SYMBOL
)
3191 struct symbol_block
*b
= (struct symbol_block
*) m
->start
;
3192 int offset
= (char *) p
- (char *) &b
->symbols
[0];
3194 /* P must point to the start of a Lisp_Symbol, not be
3195 one of the unused cells in the current symbol block,
3196 and not be on the free-list. */
3198 && offset
% sizeof b
->symbols
[0] == 0
3199 && (b
!= symbol_block
3200 || offset
/ sizeof b
->symbols
[0] < symbol_block_index
)
3201 && !EQ (((struct Lisp_Symbol
*) p
)->function
, Vdead
));
3208 /* Value is non-zero if P is a pointer to a live Lisp float on
3209 the heap. M is a pointer to the mem_block for P. */
3216 if (m
->type
== MEM_TYPE_FLOAT
)
3218 struct float_block
*b
= (struct float_block
*) m
->start
;
3219 int offset
= (char *) p
- (char *) &b
->floats
[0];
3221 /* P must point to the start of a Lisp_Float, not be
3222 one of the unused cells in the current float block,
3223 and not be on the free-list. */
3225 && offset
% sizeof b
->floats
[0] == 0
3226 && (b
!= float_block
3227 || offset
/ sizeof b
->floats
[0] < float_block_index
)
3228 && !EQ (((struct Lisp_Float
*) p
)->type
, Vdead
));
3235 /* Value is non-zero if P is a pointer to a live Lisp Misc on
3236 the heap. M is a pointer to the mem_block for P. */
3243 if (m
->type
== MEM_TYPE_MISC
)
3245 struct marker_block
*b
= (struct marker_block
*) m
->start
;
3246 int offset
= (char *) p
- (char *) &b
->markers
[0];
3248 /* P must point to the start of a Lisp_Misc, not be
3249 one of the unused cells in the current misc block,
3250 and not be on the free-list. */
3252 && offset
% sizeof b
->markers
[0] == 0
3253 && (b
!= marker_block
3254 || offset
/ sizeof b
->markers
[0] < marker_block_index
)
3255 && ((union Lisp_Misc
*) p
)->u_marker
.type
!= Lisp_Misc_Free
);
3262 /* Value is non-zero if P is a pointer to a live vector-like object.
3263 M is a pointer to the mem_block for P. */
3266 live_vector_p (m
, p
)
3270 return (p
== m
->start
3271 && m
->type
>= MEM_TYPE_VECTOR
3272 && m
->type
<= MEM_TYPE_WINDOW
);
3276 /* Value is non-zero of P is a pointer to a live buffer. M is a
3277 pointer to the mem_block for P. */
3280 live_buffer_p (m
, p
)
3284 /* P must point to the start of the block, and the buffer
3285 must not have been killed. */
3286 return (m
->type
== MEM_TYPE_BUFFER
3288 && !NILP (((struct buffer
*) p
)->name
));
3291 #endif /* GC_MARK_STACK || defined GC_MALLOC_CHECK */
3295 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3297 /* Array of objects that are kept alive because the C stack contains
3298 a pattern that looks like a reference to them . */
3300 #define MAX_ZOMBIES 10
3301 static Lisp_Object zombies
[MAX_ZOMBIES
];
3303 /* Number of zombie objects. */
3305 static int nzombies
;
3307 /* Number of garbage collections. */
3311 /* Average percentage of zombies per collection. */
3313 static double avg_zombies
;
3315 /* Max. number of live and zombie objects. */
3317 static int max_live
, max_zombies
;
3319 /* Average number of live objects per GC. */
3321 static double avg_live
;
3323 DEFUN ("gc-status", Fgc_status
, Sgc_status
, 0, 0, "",
3324 doc
: /* Show information about live and zombie objects. */)
3327 Lisp_Object args
[7];
3328 args
[0] = build_string ("%d GCs, avg live/zombies = %.2f/%.2f (%f%%), max %d/%d");
3329 args
[1] = make_number (ngcs
);
3330 args
[2] = make_float (avg_live
);
3331 args
[3] = make_float (avg_zombies
);
3332 args
[4] = make_float (avg_zombies
/ avg_live
/ 100);
3333 args
[5] = make_number (max_live
);
3334 args
[6] = make_number (max_zombies
);
3335 return Fmessage (7, args
);
3338 #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
3341 /* Mark OBJ if we can prove it's a Lisp_Object. */
3344 mark_maybe_object (obj
)
3347 void *po
= (void *) XPNTR (obj
);
3348 struct mem_node
*m
= mem_find (po
);
3354 switch (XGCTYPE (obj
))
3357 mark_p
= (live_string_p (m
, po
)
3358 && !STRING_MARKED_P ((struct Lisp_String
*) po
));
3362 mark_p
= (live_cons_p (m
, po
)
3363 && !XMARKBIT (XCONS (obj
)->car
));
3367 mark_p
= (live_symbol_p (m
, po
)
3368 && !XMARKBIT (XSYMBOL (obj
)->plist
));
3372 mark_p
= (live_float_p (m
, po
)
3373 && !XMARKBIT (XFLOAT (obj
)->type
));
3376 case Lisp_Vectorlike
:
3377 /* Note: can't check GC_BUFFERP before we know it's a
3378 buffer because checking that dereferences the pointer
3379 PO which might point anywhere. */
3380 if (live_vector_p (m
, po
))
3381 mark_p
= (!GC_SUBRP (obj
)
3382 && !(XVECTOR (obj
)->size
& ARRAY_MARK_FLAG
));
3383 else if (live_buffer_p (m
, po
))
3384 mark_p
= GC_BUFFERP (obj
) && !XMARKBIT (XBUFFER (obj
)->name
);
3388 if (live_misc_p (m
, po
))
3390 switch (XMISCTYPE (obj
))
3392 case Lisp_Misc_Marker
:
3393 mark_p
= !XMARKBIT (XMARKER (obj
)->chain
);
3396 case Lisp_Misc_Buffer_Local_Value
:
3397 case Lisp_Misc_Some_Buffer_Local_Value
:
3398 mark_p
= !XMARKBIT (XBUFFER_LOCAL_VALUE (obj
)->realvalue
);
3401 case Lisp_Misc_Overlay
:
3402 mark_p
= !XMARKBIT (XOVERLAY (obj
)->plist
);
3409 case Lisp_Type_Limit
:
3415 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3416 if (nzombies
< MAX_ZOMBIES
)
3417 zombies
[nzombies
] = *p
;
3426 /* If P points to Lisp data, mark that as live if it isn't already
3430 mark_maybe_pointer (p
)
3435 /* Quickly rule out some values which can't point to Lisp data. We
3436 assume that Lisp data is aligned on even addresses. */
3437 if ((EMACS_INT
) p
& 1)
3443 Lisp_Object obj
= Qnil
;
3447 case MEM_TYPE_NON_LISP
:
3448 /* Nothing to do; not a pointer to Lisp memory. */
3451 case MEM_TYPE_BUFFER
:
3452 if (live_buffer_p (m
, p
)
3453 && !XMARKBIT (((struct buffer
*) p
)->name
))
3454 XSETVECTOR (obj
, p
);
3458 if (live_cons_p (m
, p
)
3459 && !XMARKBIT (((struct Lisp_Cons
*) p
)->car
))
3463 case MEM_TYPE_STRING
:
3464 if (live_string_p (m
, p
)
3465 && !STRING_MARKED_P ((struct Lisp_String
*) p
))
3466 XSETSTRING (obj
, p
);
3470 if (live_misc_p (m
, p
))
3475 switch (XMISCTYPE (tem
))
3477 case Lisp_Misc_Marker
:
3478 if (!XMARKBIT (XMARKER (tem
)->chain
))
3482 case Lisp_Misc_Buffer_Local_Value
:
3483 case Lisp_Misc_Some_Buffer_Local_Value
:
3484 if (!XMARKBIT (XBUFFER_LOCAL_VALUE (tem
)->realvalue
))
3488 case Lisp_Misc_Overlay
:
3489 if (!XMARKBIT (XOVERLAY (tem
)->plist
))
3496 case MEM_TYPE_SYMBOL
:
3497 if (live_symbol_p (m
, p
)
3498 && !XMARKBIT (((struct Lisp_Symbol
*) p
)->plist
))
3499 XSETSYMBOL (obj
, p
);
3502 case MEM_TYPE_FLOAT
:
3503 if (live_float_p (m
, p
)
3504 && !XMARKBIT (((struct Lisp_Float
*) p
)->type
))
3508 case MEM_TYPE_VECTOR
:
3509 case MEM_TYPE_PROCESS
:
3510 case MEM_TYPE_HASH_TABLE
:
3511 case MEM_TYPE_FRAME
:
3512 case MEM_TYPE_WINDOW
:
3513 if (live_vector_p (m
, p
))
3516 XSETVECTOR (tem
, p
);
3518 && !(XVECTOR (tem
)->size
& ARRAY_MARK_FLAG
))
3533 /* Mark Lisp objects referenced from the address range START..END. */
3536 mark_memory (start
, end
)
3542 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3546 /* Make START the pointer to the start of the memory region,
3547 if it isn't already. */
3555 /* Mark Lisp_Objects. */
3556 for (p
= (Lisp_Object
*) start
; (void *) p
< end
; ++p
)
3557 mark_maybe_object (*p
);
3559 /* Mark Lisp data pointed to. This is necessary because, in some
3560 situations, the C compiler optimizes Lisp objects away, so that
3561 only a pointer to them remains. Example:
3563 DEFUN ("testme", Ftestme, Stestme, 0, 0, 0, "")
3566 Lisp_Object obj = build_string ("test");
3567 struct Lisp_String *s = XSTRING (obj);
3568 Fgarbage_collect ();
3569 fprintf (stderr, "test `%s'\n", s->data);
3573 Here, `obj' isn't really used, and the compiler optimizes it
3574 away. The only reference to the life string is through the
3577 for (pp
= (void **) start
; (void *) pp
< end
; ++pp
)
3578 mark_maybe_pointer (*pp
);
3582 #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
3584 static int setjmp_tested_p
, longjmps_done
;
3586 #define SETJMP_WILL_LIKELY_WORK "\
3588 Emacs garbage collector has been changed to use conservative stack\n\
3589 marking. Emacs has determined that the method it uses to do the\n\
3590 marking will likely work on your system, but this isn't sure.\n\
3592 If you are a system-programmer, or can get the help of a local wizard\n\
3593 who is, please take a look at the function mark_stack in alloc.c, and\n\
3594 verify that the methods used are appropriate for your system.\n\
3596 Please mail the result to <emacs-devel@gnu.org>.\n\
3599 #define SETJMP_WILL_NOT_WORK "\
3601 Emacs garbage collector has been changed to use conservative stack\n\
3602 marking. Emacs has determined that the default method it uses to do the\n\
3603 marking will not work on your system. We will need a system-dependent\n\
3604 solution for your system.\n\
3606 Please take a look at the function mark_stack in alloc.c, and\n\
3607 try to find a way to make it work on your system.\n\
3608 Please mail the result to <emacs-devel@gnu.org>.\n\
3612 /* Perform a quick check if it looks like setjmp saves registers in a
3613 jmp_buf. Print a message to stderr saying so. When this test
3614 succeeds, this is _not_ a proof that setjmp is sufficient for
3615 conservative stack marking. Only the sources or a disassembly
3626 /* Arrange for X to be put in a register. */
3632 if (longjmps_done
== 1)
3634 /* Came here after the longjmp at the end of the function.
3636 If x == 1, the longjmp has restored the register to its
3637 value before the setjmp, and we can hope that setjmp
3638 saves all such registers in the jmp_buf, although that
3641 For other values of X, either something really strange is
3642 taking place, or the setjmp just didn't save the register. */
3645 fprintf (stderr
, SETJMP_WILL_LIKELY_WORK
);
3648 fprintf (stderr
, SETJMP_WILL_NOT_WORK
);
3655 if (longjmps_done
== 1)
3659 #endif /* not GC_SAVE_REGISTERS_ON_STACK && not GC_SETJMP_WORKS */
3662 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
3664 /* Abort if anything GCPRO'd doesn't survive the GC. */
3672 for (p
= gcprolist
; p
; p
= p
->next
)
3673 for (i
= 0; i
< p
->nvars
; ++i
)
3674 if (!survives_gc_p (p
->var
[i
]))
3678 #elif GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3685 fprintf (stderr
, "\nZombies kept alive = %d:\n", nzombies
);
3686 for (i
= 0; i
< min (MAX_ZOMBIES
, nzombies
); ++i
)
3688 fprintf (stderr
, " %d = ", i
);
3689 debug_print (zombies
[i
]);
3693 #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
3696 /* Mark live Lisp objects on the C stack.
3698 There are several system-dependent problems to consider when
3699 porting this to new architectures:
3703 We have to mark Lisp objects in CPU registers that can hold local
3704 variables or are used to pass parameters.
3706 If GC_SAVE_REGISTERS_ON_STACK is defined, it should expand to
3707 something that either saves relevant registers on the stack, or
3708 calls mark_maybe_object passing it each register's contents.
3710 If GC_SAVE_REGISTERS_ON_STACK is not defined, the current
3711 implementation assumes that calling setjmp saves registers we need
3712 to see in a jmp_buf which itself lies on the stack. This doesn't
3713 have to be true! It must be verified for each system, possibly
3714 by taking a look at the source code of setjmp.
3718 Architectures differ in the way their processor stack is organized.
3719 For example, the stack might look like this
3722 | Lisp_Object | size = 4
3724 | something else | size = 2
3726 | Lisp_Object | size = 4
3730 In such a case, not every Lisp_Object will be aligned equally. To
3731 find all Lisp_Object on the stack it won't be sufficient to walk
3732 the stack in steps of 4 bytes. Instead, two passes will be
3733 necessary, one starting at the start of the stack, and a second
3734 pass starting at the start of the stack + 2. Likewise, if the
3735 minimal alignment of Lisp_Objects on the stack is 1, four passes
3736 would be necessary, each one starting with one byte more offset
3737 from the stack start.
3739 The current code assumes by default that Lisp_Objects are aligned
3740 equally on the stack. */
3747 volatile int stack_grows_down_p
= (char *) &j
> (char *) stack_base
;
3750 /* This trick flushes the register windows so that all the state of
3751 the process is contained in the stack. */
3756 /* Save registers that we need to see on the stack. We need to see
3757 registers used to hold register variables and registers used to
3759 #ifdef GC_SAVE_REGISTERS_ON_STACK
3760 GC_SAVE_REGISTERS_ON_STACK (end
);
3761 #else /* not GC_SAVE_REGISTERS_ON_STACK */
3763 #ifndef GC_SETJMP_WORKS /* If it hasn't been checked yet that
3764 setjmp will definitely work, test it
3765 and print a message with the result
3767 if (!setjmp_tested_p
)
3769 setjmp_tested_p
= 1;
3772 #endif /* GC_SETJMP_WORKS */
3775 end
= stack_grows_down_p
? (char *) &j
+ sizeof j
: (char *) &j
;
3776 #endif /* not GC_SAVE_REGISTERS_ON_STACK */
3778 /* This assumes that the stack is a contiguous region in memory. If
3779 that's not the case, something has to be done here to iterate
3780 over the stack segments. */
3781 #ifndef GC_LISP_OBJECT_ALIGNMENT
3782 #define GC_LISP_OBJECT_ALIGNMENT sizeof (Lisp_Object)
3784 for (i
= 0; i
< sizeof (Lisp_Object
); i
+= GC_LISP_OBJECT_ALIGNMENT
)
3785 mark_memory ((char *) stack_base
+ i
, end
);
3787 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
3793 #endif /* GC_MARK_STACK != 0 */
3797 /***********************************************************************
3798 Pure Storage Management
3799 ***********************************************************************/
3801 /* Allocate room for SIZE bytes from pure Lisp storage and return a
3802 pointer to it. TYPE is the Lisp type for which the memory is
3803 allocated. TYPE < 0 means it's not used for a Lisp object.
3805 If store_pure_type_info is set and TYPE is >= 0, the type of
3806 the allocated object is recorded in pure_types. */
3808 static POINTER_TYPE
*
3809 pure_alloc (size
, type
)
3814 POINTER_TYPE
*result
;
3815 char *beg
= purebeg
;
3817 /* Give Lisp_Floats an extra alignment. */
3818 if (type
== Lisp_Float
)
3821 #if defined __GNUC__ && __GNUC__ >= 2
3822 alignment
= __alignof (struct Lisp_Float
);
3824 alignment
= sizeof (struct Lisp_Float
);
3826 pure_bytes_used
= ALIGN (pure_bytes_used
, alignment
);
3829 nbytes
= ALIGN (size
, sizeof (EMACS_INT
));
3831 if (pure_bytes_used
+ nbytes
> pure_size
)
3833 /* Don't allocate a large amount here,
3834 because it might get mmap'd and then its address
3835 might not be usable. */
3836 beg
= purebeg
= (char *) xmalloc (10000);
3838 pure_bytes_used_before_overflow
+= pure_bytes_used
;
3839 pure_bytes_used
= 0;
3842 result
= (POINTER_TYPE
*) (beg
+ pure_bytes_used
);
3843 pure_bytes_used
+= nbytes
;
3848 /* Print a warning if PURESIZE is too small. */
3853 if (pure_bytes_used_before_overflow
)
3854 message ("Pure Lisp storage overflow (approx. %d bytes needed)",
3855 (int) (pure_bytes_used
+ pure_bytes_used_before_overflow
));
3859 /* Return a string allocated in pure space. DATA is a buffer holding
3860 NCHARS characters, and NBYTES bytes of string data. MULTIBYTE
3861 non-zero means make the result string multibyte.
3863 Must get an error if pure storage is full, since if it cannot hold
3864 a large string it may be able to hold conses that point to that
3865 string; then the string is not protected from gc. */
3868 make_pure_string (data
, nchars
, nbytes
, multibyte
)
3874 struct Lisp_String
*s
;
3876 s
= (struct Lisp_String
*) pure_alloc (sizeof *s
, Lisp_String
);
3877 s
->data
= (unsigned char *) pure_alloc (nbytes
+ 1, -1);
3879 s
->size_byte
= multibyte
? nbytes
: -1;
3880 bcopy (data
, s
->data
, nbytes
);
3881 s
->data
[nbytes
] = '\0';
3882 s
->intervals
= NULL_INTERVAL
;
3883 XSETSTRING (string
, s
);
3888 /* Return a cons allocated from pure space. Give it pure copies
3889 of CAR as car and CDR as cdr. */
3892 pure_cons (car
, cdr
)
3893 Lisp_Object car
, cdr
;
3895 register Lisp_Object
new;
3896 struct Lisp_Cons
*p
;
3898 p
= (struct Lisp_Cons
*) pure_alloc (sizeof *p
, Lisp_Cons
);
3900 XSETCAR (new, Fpurecopy (car
));
3901 XSETCDR (new, Fpurecopy (cdr
));
3906 /* Value is a float object with value NUM allocated from pure space. */
3909 make_pure_float (num
)
3912 register Lisp_Object
new;
3913 struct Lisp_Float
*p
;
3915 p
= (struct Lisp_Float
*) pure_alloc (sizeof *p
, Lisp_Float
);
3917 XFLOAT_DATA (new) = num
;
3922 /* Return a vector with room for LEN Lisp_Objects allocated from
3926 make_pure_vector (len
)
3930 struct Lisp_Vector
*p
;
3931 size_t size
= sizeof *p
+ (len
- 1) * sizeof (Lisp_Object
);
3933 p
= (struct Lisp_Vector
*) pure_alloc (size
, Lisp_Vectorlike
);
3934 XSETVECTOR (new, p
);
3935 XVECTOR (new)->size
= len
;
3940 DEFUN ("purecopy", Fpurecopy
, Spurecopy
, 1, 1, 0,
3941 doc
: /* Make a copy of OBJECT in pure storage.
3942 Recursively copies contents of vectors and cons cells.
3943 Does not copy symbols. Copies strings without text properties. */)
3945 register Lisp_Object obj
;
3947 if (NILP (Vpurify_flag
))
3950 if (PURE_POINTER_P (XPNTR (obj
)))
3954 return pure_cons (XCAR (obj
), XCDR (obj
));
3955 else if (FLOATP (obj
))
3956 return make_pure_float (XFLOAT_DATA (obj
));
3957 else if (STRINGP (obj
))
3958 return make_pure_string (SDATA (obj
), SCHARS (obj
),
3960 STRING_MULTIBYTE (obj
));
3961 else if (COMPILEDP (obj
) || VECTORP (obj
))
3963 register struct Lisp_Vector
*vec
;
3964 register int i
, size
;
3966 size
= XVECTOR (obj
)->size
;
3967 if (size
& PSEUDOVECTOR_FLAG
)
3968 size
&= PSEUDOVECTOR_SIZE_MASK
;
3969 vec
= XVECTOR (make_pure_vector ((EMACS_INT
) size
));
3970 for (i
= 0; i
< size
; i
++)
3971 vec
->contents
[i
] = Fpurecopy (XVECTOR (obj
)->contents
[i
]);
3972 if (COMPILEDP (obj
))
3973 XSETCOMPILED (obj
, vec
);
3975 XSETVECTOR (obj
, vec
);
3978 else if (MARKERP (obj
))
3979 error ("Attempt to copy a marker to pure storage");
3986 /***********************************************************************
3988 ***********************************************************************/
3990 /* Put an entry in staticvec, pointing at the variable with address
3994 staticpro (varaddress
)
3995 Lisp_Object
*varaddress
;
3997 staticvec
[staticidx
++] = varaddress
;
3998 if (staticidx
>= NSTATICS
)
4006 struct catchtag
*next
;
4011 struct backtrace
*next
;
4012 Lisp_Object
*function
;
4013 Lisp_Object
*args
; /* Points to vector of args. */
4014 int nargs
; /* Length of vector. */
4015 /* If nargs is UNEVALLED, args points to slot holding list of
4022 /***********************************************************************
4024 ***********************************************************************/
4026 /* Temporarily prevent garbage collection. */
4029 inhibit_garbage_collection ()
4031 int count
= SPECPDL_INDEX ();
4032 int nbits
= min (VALBITS
, BITS_PER_INT
);
4034 specbind (Qgc_cons_threshold
, make_number (((EMACS_INT
) 1 << (nbits
- 1)) - 1));
4039 DEFUN ("garbage-collect", Fgarbage_collect
, Sgarbage_collect
, 0, 0, "",
4040 doc
: /* Reclaim storage for Lisp objects no longer needed.
4041 Returns info on amount of space in use:
4042 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)
4043 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS
4044 (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS)
4045 (USED-STRINGS . FREE-STRINGS))
4046 Garbage collection happens automatically if you cons more than
4047 `gc-cons-threshold' bytes of Lisp data since previous garbage collection. */)
4050 register struct gcpro
*tail
;
4051 register struct specbinding
*bind
;
4052 struct catchtag
*catch;
4053 struct handler
*handler
;
4054 register struct backtrace
*backlist
;
4055 char stack_top_variable
;
4058 Lisp_Object total
[8];
4059 int count
= SPECPDL_INDEX ();
4061 /* Can't GC if pure storage overflowed because we can't determine
4062 if something is a pure object or not. */
4063 if (pure_bytes_used_before_overflow
)
4066 /* In case user calls debug_print during GC,
4067 don't let that cause a recursive GC. */
4068 consing_since_gc
= 0;
4070 /* Save what's currently displayed in the echo area. */
4071 message_p
= push_message ();
4072 record_unwind_protect (push_message_unwind
, Qnil
);
4074 /* Save a copy of the contents of the stack, for debugging. */
4075 #if MAX_SAVE_STACK > 0
4076 if (NILP (Vpurify_flag
))
4078 i
= &stack_top_variable
- stack_bottom
;
4080 if (i
< MAX_SAVE_STACK
)
4082 if (stack_copy
== 0)
4083 stack_copy
= (char *) xmalloc (stack_copy_size
= i
);
4084 else if (stack_copy_size
< i
)
4085 stack_copy
= (char *) xrealloc (stack_copy
, (stack_copy_size
= i
));
4088 if ((EMACS_INT
) (&stack_top_variable
- stack_bottom
) > 0)
4089 bcopy (stack_bottom
, stack_copy
, i
);
4091 bcopy (&stack_top_variable
, stack_copy
, i
);
4095 #endif /* MAX_SAVE_STACK > 0 */
4097 if (garbage_collection_messages
)
4098 message1_nolog ("Garbage collecting...");
4102 shrink_regexp_cache ();
4104 /* Don't keep undo information around forever. */
4106 register struct buffer
*nextb
= all_buffers
;
4110 /* If a buffer's undo list is Qt, that means that undo is
4111 turned off in that buffer. Calling truncate_undo_list on
4112 Qt tends to return NULL, which effectively turns undo back on.
4113 So don't call truncate_undo_list if undo_list is Qt. */
4114 if (! EQ (nextb
->undo_list
, Qt
))
4116 = truncate_undo_list (nextb
->undo_list
, undo_limit
,
4119 /* Shrink buffer gaps, but skip indirect and dead buffers. */
4120 if (nextb
->base_buffer
== 0 && !NILP (nextb
->name
))
4122 /* If a buffer's gap size is more than 10% of the buffer
4123 size, or larger than 2000 bytes, then shrink it
4124 accordingly. Keep a minimum size of 20 bytes. */
4125 int size
= min (2000, max (20, (nextb
->text
->z_byte
/ 10)));
4127 if (nextb
->text
->gap_size
> size
)
4129 struct buffer
*save_current
= current_buffer
;
4130 current_buffer
= nextb
;
4131 make_gap (-(nextb
->text
->gap_size
- size
));
4132 current_buffer
= save_current
;
4136 nextb
= nextb
->next
;
4142 /* clear_marks (); */
4144 /* Mark all the special slots that serve as the roots of accessibility.
4146 Usually the special slots to mark are contained in particular structures.
4147 Then we know no slot is marked twice because the structures don't overlap.
4148 In some cases, the structures point to the slots to be marked.
4149 For these, we use MARKBIT to avoid double marking of the slot. */
4151 for (i
= 0; i
< staticidx
; i
++)
4152 mark_object (staticvec
[i
]);
4154 #if (GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS \
4155 || GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS)
4158 for (tail
= gcprolist
; tail
; tail
= tail
->next
)
4159 for (i
= 0; i
< tail
->nvars
; i
++)
4160 if (!XMARKBIT (tail
->var
[i
]))
4162 /* Explicit casting prevents compiler warning about
4163 discarding the `volatile' qualifier. */
4164 mark_object ((Lisp_Object
*)&tail
->var
[i
]);
4165 XMARK (tail
->var
[i
]);
4170 for (bind
= specpdl
; bind
!= specpdl_ptr
; bind
++)
4172 mark_object (&bind
->symbol
);
4173 mark_object (&bind
->old_value
);
4175 for (catch = catchlist
; catch; catch = catch->next
)
4177 mark_object (&catch->tag
);
4178 mark_object (&catch->val
);
4180 for (handler
= handlerlist
; handler
; handler
= handler
->next
)
4182 mark_object (&handler
->handler
);
4183 mark_object (&handler
->var
);
4185 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
4187 if (!XMARKBIT (*backlist
->function
))
4189 mark_object (backlist
->function
);
4190 XMARK (*backlist
->function
);
4192 if (backlist
->nargs
== UNEVALLED
|| backlist
->nargs
== MANY
)
4195 i
= backlist
->nargs
- 1;
4197 if (!XMARKBIT (backlist
->args
[i
]))
4199 mark_object (&backlist
->args
[i
]);
4200 XMARK (backlist
->args
[i
]);
4205 /* Look thru every buffer's undo list
4206 for elements that update markers that were not marked,
4209 register struct buffer
*nextb
= all_buffers
;
4213 /* If a buffer's undo list is Qt, that means that undo is
4214 turned off in that buffer. Calling truncate_undo_list on
4215 Qt tends to return NULL, which effectively turns undo back on.
4216 So don't call truncate_undo_list if undo_list is Qt. */
4217 if (! EQ (nextb
->undo_list
, Qt
))
4219 Lisp_Object tail
, prev
;
4220 tail
= nextb
->undo_list
;
4222 while (CONSP (tail
))
4224 if (GC_CONSP (XCAR (tail
))
4225 && GC_MARKERP (XCAR (XCAR (tail
)))
4226 && ! XMARKBIT (XMARKER (XCAR (XCAR (tail
)))->chain
))
4229 nextb
->undo_list
= tail
= XCDR (tail
);
4233 XSETCDR (prev
, tail
);
4244 nextb
= nextb
->next
;
4248 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4254 /* Clear the mark bits that we set in certain root slots. */
4256 #if (GC_MARK_STACK == GC_USE_GCPROS_AS_BEFORE \
4257 || GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES)
4258 for (tail
= gcprolist
; tail
; tail
= tail
->next
)
4259 for (i
= 0; i
< tail
->nvars
; i
++)
4260 XUNMARK (tail
->var
[i
]);
4263 unmark_byte_stack ();
4264 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
4266 XUNMARK (*backlist
->function
);
4267 if (backlist
->nargs
== UNEVALLED
|| backlist
->nargs
== MANY
)
4270 i
= backlist
->nargs
- 1;
4272 XUNMARK (backlist
->args
[i
]);
4274 XUNMARK (buffer_defaults
.name
);
4275 XUNMARK (buffer_local_symbols
.name
);
4277 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES && 0
4283 /* clear_marks (); */
4286 consing_since_gc
= 0;
4287 if (gc_cons_threshold
< 10000)
4288 gc_cons_threshold
= 10000;
4290 if (garbage_collection_messages
)
4292 if (message_p
|| minibuf_level
> 0)
4295 message1_nolog ("Garbage collecting...done");
4298 unbind_to (count
, Qnil
);
4300 total
[0] = Fcons (make_number (total_conses
),
4301 make_number (total_free_conses
));
4302 total
[1] = Fcons (make_number (total_symbols
),
4303 make_number (total_free_symbols
));
4304 total
[2] = Fcons (make_number (total_markers
),
4305 make_number (total_free_markers
));
4306 total
[3] = make_number (total_string_size
);
4307 total
[4] = make_number (total_vector_size
);
4308 total
[5] = Fcons (make_number (total_floats
),
4309 make_number (total_free_floats
));
4310 total
[6] = Fcons (make_number (total_intervals
),
4311 make_number (total_free_intervals
));
4312 total
[7] = Fcons (make_number (total_strings
),
4313 make_number (total_free_strings
));
4315 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4317 /* Compute average percentage of zombies. */
4320 for (i
= 0; i
< 7; ++i
)
4321 nlive
+= XFASTINT (XCAR (total
[i
]));
4323 avg_live
= (avg_live
* ngcs
+ nlive
) / (ngcs
+ 1);
4324 max_live
= max (nlive
, max_live
);
4325 avg_zombies
= (avg_zombies
* ngcs
+ nzombies
) / (ngcs
+ 1);
4326 max_zombies
= max (nzombies
, max_zombies
);
4331 if (!NILP (Vpost_gc_hook
))
4333 int count
= inhibit_garbage_collection ();
4334 safe_run_hooks (Qpost_gc_hook
);
4335 unbind_to (count
, Qnil
);
4338 return Flist (sizeof total
/ sizeof *total
, total
);
4342 /* Mark Lisp objects in glyph matrix MATRIX. Currently the
4343 only interesting objects referenced from glyphs are strings. */
4346 mark_glyph_matrix (matrix
)
4347 struct glyph_matrix
*matrix
;
4349 struct glyph_row
*row
= matrix
->rows
;
4350 struct glyph_row
*end
= row
+ matrix
->nrows
;
4352 for (; row
< end
; ++row
)
4356 for (area
= LEFT_MARGIN_AREA
; area
< LAST_AREA
; ++area
)
4358 struct glyph
*glyph
= row
->glyphs
[area
];
4359 struct glyph
*end_glyph
= glyph
+ row
->used
[area
];
4361 for (; glyph
< end_glyph
; ++glyph
)
4362 if (GC_STRINGP (glyph
->object
)
4363 && !STRING_MARKED_P (XSTRING (glyph
->object
)))
4364 mark_object (&glyph
->object
);
4370 /* Mark Lisp faces in the face cache C. */
4374 struct face_cache
*c
;
4379 for (i
= 0; i
< c
->used
; ++i
)
4381 struct face
*face
= FACE_FROM_ID (c
->f
, i
);
4385 for (j
= 0; j
< LFACE_VECTOR_SIZE
; ++j
)
4386 mark_object (&face
->lface
[j
]);
4393 #ifdef HAVE_WINDOW_SYSTEM
4395 /* Mark Lisp objects in image IMG. */
4401 mark_object (&img
->spec
);
4403 if (!NILP (img
->data
.lisp_val
))
4404 mark_object (&img
->data
.lisp_val
);
4408 /* Mark Lisp objects in image cache of frame F. It's done this way so
4409 that we don't have to include xterm.h here. */
4412 mark_image_cache (f
)
4415 forall_images_in_image_cache (f
, mark_image
);
4418 #endif /* HAVE_X_WINDOWS */
4422 /* Mark reference to a Lisp_Object.
4423 If the object referred to has not been seen yet, recursively mark
4424 all the references contained in it. */
4426 #define LAST_MARKED_SIZE 500
4427 Lisp_Object
*last_marked
[LAST_MARKED_SIZE
];
4428 int last_marked_index
;
4431 mark_object (argptr
)
4432 Lisp_Object
*argptr
;
4434 Lisp_Object
*objptr
= argptr
;
4435 register Lisp_Object obj
;
4436 #ifdef GC_CHECK_MARKED_OBJECTS
4446 if (PURE_POINTER_P (XPNTR (obj
)))
4449 last_marked
[last_marked_index
++] = objptr
;
4450 if (last_marked_index
== LAST_MARKED_SIZE
)
4451 last_marked_index
= 0;
4453 /* Perform some sanity checks on the objects marked here. Abort if
4454 we encounter an object we know is bogus. This increases GC time
4455 by ~80%, and requires compilation with GC_MARK_STACK != 0. */
4456 #ifdef GC_CHECK_MARKED_OBJECTS
4458 po
= (void *) XPNTR (obj
);
4460 /* Check that the object pointed to by PO is known to be a Lisp
4461 structure allocated from the heap. */
4462 #define CHECK_ALLOCATED() \
4464 m = mem_find (po); \
4469 /* Check that the object pointed to by PO is live, using predicate
4471 #define CHECK_LIVE(LIVEP) \
4473 if (!LIVEP (m, po)) \
4477 /* Check both of the above conditions. */
4478 #define CHECK_ALLOCATED_AND_LIVE(LIVEP) \
4480 CHECK_ALLOCATED (); \
4481 CHECK_LIVE (LIVEP); \
4484 #else /* not GC_CHECK_MARKED_OBJECTS */
4486 #define CHECK_ALLOCATED() (void) 0
4487 #define CHECK_LIVE(LIVEP) (void) 0
4488 #define CHECK_ALLOCATED_AND_LIVE(LIVEP) (void) 0
4490 #endif /* not GC_CHECK_MARKED_OBJECTS */
4492 switch (SWITCH_ENUM_CAST (XGCTYPE (obj
)))
4496 register struct Lisp_String
*ptr
= XSTRING (obj
);
4497 CHECK_ALLOCATED_AND_LIVE (live_string_p
);
4498 MARK_INTERVAL_TREE (ptr
->intervals
);
4500 #ifdef GC_CHECK_STRING_BYTES
4501 /* Check that the string size recorded in the string is the
4502 same as the one recorded in the sdata structure. */
4503 CHECK_STRING_BYTES (ptr
);
4504 #endif /* GC_CHECK_STRING_BYTES */
4508 case Lisp_Vectorlike
:
4509 #ifdef GC_CHECK_MARKED_OBJECTS
4511 if (m
== MEM_NIL
&& !GC_SUBRP (obj
)
4512 && po
!= &buffer_defaults
4513 && po
!= &buffer_local_symbols
)
4515 #endif /* GC_CHECK_MARKED_OBJECTS */
4517 if (GC_BUFFERP (obj
))
4519 if (!XMARKBIT (XBUFFER (obj
)->name
))
4521 #ifdef GC_CHECK_MARKED_OBJECTS
4522 if (po
!= &buffer_defaults
&& po
!= &buffer_local_symbols
)
4525 for (b
= all_buffers
; b
&& b
!= po
; b
= b
->next
)
4530 #endif /* GC_CHECK_MARKED_OBJECTS */
4534 else if (GC_SUBRP (obj
))
4536 else if (GC_COMPILEDP (obj
))
4537 /* We could treat this just like a vector, but it is better to
4538 save the COMPILED_CONSTANTS element for last and avoid
4541 register struct Lisp_Vector
*ptr
= XVECTOR (obj
);
4542 register EMACS_INT size
= ptr
->size
;
4545 if (size
& ARRAY_MARK_FLAG
)
4546 break; /* Already marked */
4548 CHECK_LIVE (live_vector_p
);
4549 ptr
->size
|= ARRAY_MARK_FLAG
; /* Else mark it */
4550 size
&= PSEUDOVECTOR_SIZE_MASK
;
4551 for (i
= 0; i
< size
; i
++) /* and then mark its elements */
4553 if (i
!= COMPILED_CONSTANTS
)
4554 mark_object (&ptr
->contents
[i
]);
4556 /* This cast should be unnecessary, but some Mips compiler complains
4557 (MIPS-ABI + SysVR4, DC/OSx, etc). */
4558 objptr
= (Lisp_Object
*) &ptr
->contents
[COMPILED_CONSTANTS
];
4561 else if (GC_FRAMEP (obj
))
4563 register struct frame
*ptr
= XFRAME (obj
);
4564 register EMACS_INT size
= ptr
->size
;
4566 if (size
& ARRAY_MARK_FLAG
) break; /* Already marked */
4567 ptr
->size
|= ARRAY_MARK_FLAG
; /* Else mark it */
4569 CHECK_LIVE (live_vector_p
);
4570 mark_object (&ptr
->name
);
4571 mark_object (&ptr
->icon_name
);
4572 mark_object (&ptr
->title
);
4573 mark_object (&ptr
->focus_frame
);
4574 mark_object (&ptr
->selected_window
);
4575 mark_object (&ptr
->minibuffer_window
);
4576 mark_object (&ptr
->param_alist
);
4577 mark_object (&ptr
->scroll_bars
);
4578 mark_object (&ptr
->condemned_scroll_bars
);
4579 mark_object (&ptr
->menu_bar_items
);
4580 mark_object (&ptr
->face_alist
);
4581 mark_object (&ptr
->menu_bar_vector
);
4582 mark_object (&ptr
->buffer_predicate
);
4583 mark_object (&ptr
->buffer_list
);
4584 mark_object (&ptr
->menu_bar_window
);
4585 mark_object (&ptr
->tool_bar_window
);
4586 mark_face_cache (ptr
->face_cache
);
4587 #ifdef HAVE_WINDOW_SYSTEM
4588 mark_image_cache (ptr
);
4589 mark_object (&ptr
->tool_bar_items
);
4590 mark_object (&ptr
->desired_tool_bar_string
);
4591 mark_object (&ptr
->current_tool_bar_string
);
4592 #endif /* HAVE_WINDOW_SYSTEM */
4594 else if (GC_BOOL_VECTOR_P (obj
))
4596 register struct Lisp_Vector
*ptr
= XVECTOR (obj
);
4598 if (ptr
->size
& ARRAY_MARK_FLAG
)
4599 break; /* Already marked */
4600 CHECK_LIVE (live_vector_p
);
4601 ptr
->size
|= ARRAY_MARK_FLAG
; /* Else mark it */
4603 else if (GC_WINDOWP (obj
))
4605 register struct Lisp_Vector
*ptr
= XVECTOR (obj
);
4606 struct window
*w
= XWINDOW (obj
);
4607 register EMACS_INT size
= ptr
->size
;
4610 /* Stop if already marked. */
4611 if (size
& ARRAY_MARK_FLAG
)
4615 CHECK_LIVE (live_vector_p
);
4616 ptr
->size
|= ARRAY_MARK_FLAG
;
4618 /* There is no Lisp data above The member CURRENT_MATRIX in
4619 struct WINDOW. Stop marking when that slot is reached. */
4621 (char *) &ptr
->contents
[i
] < (char *) &w
->current_matrix
;
4623 mark_object (&ptr
->contents
[i
]);
4625 /* Mark glyphs for leaf windows. Marking window matrices is
4626 sufficient because frame matrices use the same glyph
4628 if (NILP (w
->hchild
)
4630 && w
->current_matrix
)
4632 mark_glyph_matrix (w
->current_matrix
);
4633 mark_glyph_matrix (w
->desired_matrix
);
4636 else if (GC_HASH_TABLE_P (obj
))
4638 struct Lisp_Hash_Table
*h
= XHASH_TABLE (obj
);
4639 EMACS_INT size
= h
->size
;
4641 /* Stop if already marked. */
4642 if (size
& ARRAY_MARK_FLAG
)
4646 CHECK_LIVE (live_vector_p
);
4647 h
->size
|= ARRAY_MARK_FLAG
;
4649 /* Mark contents. */
4650 /* Do not mark next_free or next_weak.
4651 Being in the next_weak chain
4652 should not keep the hash table alive.
4653 No need to mark `count' since it is an integer. */
4654 mark_object (&h
->test
);
4655 mark_object (&h
->weak
);
4656 mark_object (&h
->rehash_size
);
4657 mark_object (&h
->rehash_threshold
);
4658 mark_object (&h
->hash
);
4659 mark_object (&h
->next
);
4660 mark_object (&h
->index
);
4661 mark_object (&h
->user_hash_function
);
4662 mark_object (&h
->user_cmp_function
);
4664 /* If hash table is not weak, mark all keys and values.
4665 For weak tables, mark only the vector. */
4666 if (GC_NILP (h
->weak
))
4667 mark_object (&h
->key_and_value
);
4669 XVECTOR (h
->key_and_value
)->size
|= ARRAY_MARK_FLAG
;
4674 register struct Lisp_Vector
*ptr
= XVECTOR (obj
);
4675 register EMACS_INT size
= ptr
->size
;
4678 if (size
& ARRAY_MARK_FLAG
) break; /* Already marked */
4679 CHECK_LIVE (live_vector_p
);
4680 ptr
->size
|= ARRAY_MARK_FLAG
; /* Else mark it */
4681 if (size
& PSEUDOVECTOR_FLAG
)
4682 size
&= PSEUDOVECTOR_SIZE_MASK
;
4684 for (i
= 0; i
< size
; i
++) /* and then mark its elements */
4685 mark_object (&ptr
->contents
[i
]);
4691 register struct Lisp_Symbol
*ptr
= XSYMBOL (obj
);
4692 struct Lisp_Symbol
*ptrx
;
4694 if (XMARKBIT (ptr
->plist
)) break;
4695 CHECK_ALLOCATED_AND_LIVE (live_symbol_p
);
4697 mark_object ((Lisp_Object
*) &ptr
->value
);
4698 mark_object (&ptr
->function
);
4699 mark_object (&ptr
->plist
);
4701 if (!PURE_POINTER_P (XSTRING (ptr
->xname
)))
4702 MARK_STRING (XSTRING (ptr
->xname
));
4703 MARK_INTERVAL_TREE (STRING_INTERVALS (ptr
->xname
));
4705 /* Note that we do not mark the obarray of the symbol.
4706 It is safe not to do so because nothing accesses that
4707 slot except to check whether it is nil. */
4711 /* For the benefit of the last_marked log. */
4712 objptr
= (Lisp_Object
*)&XSYMBOL (obj
)->next
;
4713 ptrx
= ptr
; /* Use of ptrx avoids compiler bug on Sun */
4714 XSETSYMBOL (obj
, ptrx
);
4715 /* We can't goto loop here because *objptr doesn't contain an
4716 actual Lisp_Object with valid datatype field. */
4723 CHECK_ALLOCATED_AND_LIVE (live_misc_p
);
4724 switch (XMISCTYPE (obj
))
4726 case Lisp_Misc_Marker
:
4727 XMARK (XMARKER (obj
)->chain
);
4728 /* DO NOT mark thru the marker's chain.
4729 The buffer's markers chain does not preserve markers from gc;
4730 instead, markers are removed from the chain when freed by gc. */
4733 case Lisp_Misc_Buffer_Local_Value
:
4734 case Lisp_Misc_Some_Buffer_Local_Value
:
4736 register struct Lisp_Buffer_Local_Value
*ptr
4737 = XBUFFER_LOCAL_VALUE (obj
);
4738 if (XMARKBIT (ptr
->realvalue
)) break;
4739 XMARK (ptr
->realvalue
);
4740 /* If the cdr is nil, avoid recursion for the car. */
4741 if (EQ (ptr
->cdr
, Qnil
))
4743 objptr
= &ptr
->realvalue
;
4746 mark_object (&ptr
->realvalue
);
4747 mark_object (&ptr
->buffer
);
4748 mark_object (&ptr
->frame
);
4753 case Lisp_Misc_Intfwd
:
4754 case Lisp_Misc_Boolfwd
:
4755 case Lisp_Misc_Objfwd
:
4756 case Lisp_Misc_Buffer_Objfwd
:
4757 case Lisp_Misc_Kboard_Objfwd
:
4758 /* Don't bother with Lisp_Buffer_Objfwd,
4759 since all markable slots in current buffer marked anyway. */
4760 /* Don't need to do Lisp_Objfwd, since the places they point
4761 are protected with staticpro. */
4764 case Lisp_Misc_Overlay
:
4766 struct Lisp_Overlay
*ptr
= XOVERLAY (obj
);
4767 if (!XMARKBIT (ptr
->plist
))
4770 mark_object (&ptr
->start
);
4771 mark_object (&ptr
->end
);
4772 objptr
= &ptr
->plist
;
4785 register struct Lisp_Cons
*ptr
= XCONS (obj
);
4786 if (XMARKBIT (ptr
->car
)) break;
4787 CHECK_ALLOCATED_AND_LIVE (live_cons_p
);
4789 /* If the cdr is nil, avoid recursion for the car. */
4790 if (EQ (ptr
->cdr
, Qnil
))
4795 mark_object (&ptr
->car
);
4801 CHECK_ALLOCATED_AND_LIVE (live_float_p
);
4802 XMARK (XFLOAT (obj
)->type
);
4813 #undef CHECK_ALLOCATED
4814 #undef CHECK_ALLOCATED_AND_LIVE
4817 /* Mark the pointers in a buffer structure. */
4823 register struct buffer
*buffer
= XBUFFER (buf
);
4824 register Lisp_Object
*ptr
;
4825 Lisp_Object base_buffer
;
4827 /* This is the buffer's markbit */
4828 mark_object (&buffer
->name
);
4829 XMARK (buffer
->name
);
4831 MARK_INTERVAL_TREE (BUF_INTERVALS (buffer
));
4833 if (CONSP (buffer
->undo_list
))
4836 tail
= buffer
->undo_list
;
4838 while (CONSP (tail
))
4840 register struct Lisp_Cons
*ptr
= XCONS (tail
);
4842 if (XMARKBIT (ptr
->car
))
4845 if (GC_CONSP (ptr
->car
)
4846 && ! XMARKBIT (XCAR (ptr
->car
))
4847 && GC_MARKERP (XCAR (ptr
->car
)))
4849 XMARK (XCAR_AS_LVALUE (ptr
->car
));
4850 mark_object (&XCDR_AS_LVALUE (ptr
->car
));
4853 mark_object (&ptr
->car
);
4855 if (CONSP (ptr
->cdr
))
4861 mark_object (&XCDR_AS_LVALUE (tail
));
4864 mark_object (&buffer
->undo_list
);
4866 for (ptr
= &buffer
->name
+ 1;
4867 (char *)ptr
< (char *)buffer
+ sizeof (struct buffer
);
4871 /* If this is an indirect buffer, mark its base buffer. */
4872 if (buffer
->base_buffer
&& !XMARKBIT (buffer
->base_buffer
->name
))
4874 XSETBUFFER (base_buffer
, buffer
->base_buffer
);
4875 mark_buffer (base_buffer
);
4880 /* Mark the pointers in the kboard objects. */
4887 for (kb
= all_kboards
; kb
; kb
= kb
->next_kboard
)
4889 if (kb
->kbd_macro_buffer
)
4890 for (p
= kb
->kbd_macro_buffer
; p
< kb
->kbd_macro_ptr
; p
++)
4892 mark_object (&kb
->Voverriding_terminal_local_map
);
4893 mark_object (&kb
->Vlast_command
);
4894 mark_object (&kb
->Vreal_last_command
);
4895 mark_object (&kb
->Vprefix_arg
);
4896 mark_object (&kb
->Vlast_prefix_arg
);
4897 mark_object (&kb
->kbd_queue
);
4898 mark_object (&kb
->defining_kbd_macro
);
4899 mark_object (&kb
->Vlast_kbd_macro
);
4900 mark_object (&kb
->Vsystem_key_alist
);
4901 mark_object (&kb
->system_key_syms
);
4902 mark_object (&kb
->Vdefault_minibuffer_frame
);
4903 mark_object (&kb
->echo_string
);
4908 /* Value is non-zero if OBJ will survive the current GC because it's
4909 either marked or does not need to be marked to survive. */
4917 switch (XGCTYPE (obj
))
4924 survives_p
= XMARKBIT (XSYMBOL (obj
)->plist
);
4928 switch (XMISCTYPE (obj
))
4930 case Lisp_Misc_Marker
:
4931 survives_p
= XMARKBIT (obj
);
4934 case Lisp_Misc_Buffer_Local_Value
:
4935 case Lisp_Misc_Some_Buffer_Local_Value
:
4936 survives_p
= XMARKBIT (XBUFFER_LOCAL_VALUE (obj
)->realvalue
);
4939 case Lisp_Misc_Intfwd
:
4940 case Lisp_Misc_Boolfwd
:
4941 case Lisp_Misc_Objfwd
:
4942 case Lisp_Misc_Buffer_Objfwd
:
4943 case Lisp_Misc_Kboard_Objfwd
:
4947 case Lisp_Misc_Overlay
:
4948 survives_p
= XMARKBIT (XOVERLAY (obj
)->plist
);
4958 struct Lisp_String
*s
= XSTRING (obj
);
4959 survives_p
= STRING_MARKED_P (s
);
4963 case Lisp_Vectorlike
:
4964 if (GC_BUFFERP (obj
))
4965 survives_p
= XMARKBIT (XBUFFER (obj
)->name
);
4966 else if (GC_SUBRP (obj
))
4969 survives_p
= XVECTOR (obj
)->size
& ARRAY_MARK_FLAG
;
4973 survives_p
= XMARKBIT (XCAR (obj
));
4977 survives_p
= XMARKBIT (XFLOAT (obj
)->type
);
4984 return survives_p
|| PURE_POINTER_P ((void *) XPNTR (obj
));
4989 /* Sweep: find all structures not marked, and free them. */
4994 /* Remove or mark entries in weak hash tables.
4995 This must be done before any object is unmarked. */
4996 sweep_weak_hash_tables ();
4999 #ifdef GC_CHECK_STRING_BYTES
5000 if (!noninteractive
)
5001 check_string_bytes (1);
5004 /* Put all unmarked conses on free list */
5006 register struct cons_block
*cblk
;
5007 struct cons_block
**cprev
= &cons_block
;
5008 register int lim
= cons_block_index
;
5009 register int num_free
= 0, num_used
= 0;
5013 for (cblk
= cons_block
; cblk
; cblk
= *cprev
)
5017 for (i
= 0; i
< lim
; i
++)
5018 if (!XMARKBIT (cblk
->conses
[i
].car
))
5021 *(struct Lisp_Cons
**)&cblk
->conses
[i
].cdr
= cons_free_list
;
5022 cons_free_list
= &cblk
->conses
[i
];
5024 cons_free_list
->car
= Vdead
;
5030 XUNMARK (cblk
->conses
[i
].car
);
5032 lim
= CONS_BLOCK_SIZE
;
5033 /* If this block contains only free conses and we have already
5034 seen more than two blocks worth of free conses then deallocate
5036 if (this_free
== CONS_BLOCK_SIZE
&& num_free
> CONS_BLOCK_SIZE
)
5038 *cprev
= cblk
->next
;
5039 /* Unhook from the free list. */
5040 cons_free_list
= *(struct Lisp_Cons
**) &cblk
->conses
[0].cdr
;
5046 num_free
+= this_free
;
5047 cprev
= &cblk
->next
;
5050 total_conses
= num_used
;
5051 total_free_conses
= num_free
;
5054 /* Put all unmarked floats on free list */
5056 register struct float_block
*fblk
;
5057 struct float_block
**fprev
= &float_block
;
5058 register int lim
= float_block_index
;
5059 register int num_free
= 0, num_used
= 0;
5061 float_free_list
= 0;
5063 for (fblk
= float_block
; fblk
; fblk
= *fprev
)
5067 for (i
= 0; i
< lim
; i
++)
5068 if (!XMARKBIT (fblk
->floats
[i
].type
))
5071 *(struct Lisp_Float
**)&fblk
->floats
[i
].data
= float_free_list
;
5072 float_free_list
= &fblk
->floats
[i
];
5074 float_free_list
->type
= Vdead
;
5080 XUNMARK (fblk
->floats
[i
].type
);
5082 lim
= FLOAT_BLOCK_SIZE
;
5083 /* If this block contains only free floats and we have already
5084 seen more than two blocks worth of free floats then deallocate
5086 if (this_free
== FLOAT_BLOCK_SIZE
&& num_free
> FLOAT_BLOCK_SIZE
)
5088 *fprev
= fblk
->next
;
5089 /* Unhook from the free list. */
5090 float_free_list
= *(struct Lisp_Float
**) &fblk
->floats
[0].data
;
5096 num_free
+= this_free
;
5097 fprev
= &fblk
->next
;
5100 total_floats
= num_used
;
5101 total_free_floats
= num_free
;
5104 /* Put all unmarked intervals on free list */
5106 register struct interval_block
*iblk
;
5107 struct interval_block
**iprev
= &interval_block
;
5108 register int lim
= interval_block_index
;
5109 register int num_free
= 0, num_used
= 0;
5111 interval_free_list
= 0;
5113 for (iblk
= interval_block
; iblk
; iblk
= *iprev
)
5118 for (i
= 0; i
< lim
; i
++)
5120 if (! XMARKBIT (iblk
->intervals
[i
].plist
))
5122 SET_INTERVAL_PARENT (&iblk
->intervals
[i
], interval_free_list
);
5123 interval_free_list
= &iblk
->intervals
[i
];
5129 XUNMARK (iblk
->intervals
[i
].plist
);
5132 lim
= INTERVAL_BLOCK_SIZE
;
5133 /* If this block contains only free intervals and we have already
5134 seen more than two blocks worth of free intervals then
5135 deallocate this block. */
5136 if (this_free
== INTERVAL_BLOCK_SIZE
&& num_free
> INTERVAL_BLOCK_SIZE
)
5138 *iprev
= iblk
->next
;
5139 /* Unhook from the free list. */
5140 interval_free_list
= INTERVAL_PARENT (&iblk
->intervals
[0]);
5142 n_interval_blocks
--;
5146 num_free
+= this_free
;
5147 iprev
= &iblk
->next
;
5150 total_intervals
= num_used
;
5151 total_free_intervals
= num_free
;
5154 /* Put all unmarked symbols on free list */
5156 register struct symbol_block
*sblk
;
5157 struct symbol_block
**sprev
= &symbol_block
;
5158 register int lim
= symbol_block_index
;
5159 register int num_free
= 0, num_used
= 0;
5161 symbol_free_list
= NULL
;
5163 for (sblk
= symbol_block
; sblk
; sblk
= *sprev
)
5166 struct Lisp_Symbol
*sym
= sblk
->symbols
;
5167 struct Lisp_Symbol
*end
= sym
+ lim
;
5169 for (; sym
< end
; ++sym
)
5171 /* Check if the symbol was created during loadup. In such a case
5172 it might be pointed to by pure bytecode which we don't trace,
5173 so we conservatively assume that it is live. */
5174 int pure_p
= PURE_POINTER_P (XSTRING (sym
->xname
));
5176 if (!XMARKBIT (sym
->plist
) && !pure_p
)
5178 *(struct Lisp_Symbol
**) &sym
->value
= symbol_free_list
;
5179 symbol_free_list
= sym
;
5181 symbol_free_list
->function
= Vdead
;
5189 UNMARK_STRING (XSTRING (sym
->xname
));
5190 XUNMARK (sym
->plist
);
5194 lim
= SYMBOL_BLOCK_SIZE
;
5195 /* If this block contains only free symbols and we have already
5196 seen more than two blocks worth of free symbols then deallocate
5198 if (this_free
== SYMBOL_BLOCK_SIZE
&& num_free
> SYMBOL_BLOCK_SIZE
)
5200 *sprev
= sblk
->next
;
5201 /* Unhook from the free list. */
5202 symbol_free_list
= *(struct Lisp_Symbol
**)&sblk
->symbols
[0].value
;
5208 num_free
+= this_free
;
5209 sprev
= &sblk
->next
;
5212 total_symbols
= num_used
;
5213 total_free_symbols
= num_free
;
5216 /* Put all unmarked misc's on free list.
5217 For a marker, first unchain it from the buffer it points into. */
5219 register struct marker_block
*mblk
;
5220 struct marker_block
**mprev
= &marker_block
;
5221 register int lim
= marker_block_index
;
5222 register int num_free
= 0, num_used
= 0;
5224 marker_free_list
= 0;
5226 for (mblk
= marker_block
; mblk
; mblk
= *mprev
)
5230 EMACS_INT already_free
= -1;
5232 for (i
= 0; i
< lim
; i
++)
5234 Lisp_Object
*markword
;
5235 switch (mblk
->markers
[i
].u_marker
.type
)
5237 case Lisp_Misc_Marker
:
5238 markword
= &mblk
->markers
[i
].u_marker
.chain
;
5240 case Lisp_Misc_Buffer_Local_Value
:
5241 case Lisp_Misc_Some_Buffer_Local_Value
:
5242 markword
= &mblk
->markers
[i
].u_buffer_local_value
.realvalue
;
5244 case Lisp_Misc_Overlay
:
5245 markword
= &mblk
->markers
[i
].u_overlay
.plist
;
5247 case Lisp_Misc_Free
:
5248 /* If the object was already free, keep it
5249 on the free list. */
5250 markword
= (Lisp_Object
*) &already_free
;
5256 if (markword
&& !XMARKBIT (*markword
))
5259 if (mblk
->markers
[i
].u_marker
.type
== Lisp_Misc_Marker
)
5261 /* tem1 avoids Sun compiler bug */
5262 struct Lisp_Marker
*tem1
= &mblk
->markers
[i
].u_marker
;
5263 XSETMARKER (tem
, tem1
);
5264 unchain_marker (tem
);
5266 /* Set the type of the freed object to Lisp_Misc_Free.
5267 We could leave the type alone, since nobody checks it,
5268 but this might catch bugs faster. */
5269 mblk
->markers
[i
].u_marker
.type
= Lisp_Misc_Free
;
5270 mblk
->markers
[i
].u_free
.chain
= marker_free_list
;
5271 marker_free_list
= &mblk
->markers
[i
];
5278 XUNMARK (*markword
);
5281 lim
= MARKER_BLOCK_SIZE
;
5282 /* If this block contains only free markers and we have already
5283 seen more than two blocks worth of free markers then deallocate
5285 if (this_free
== MARKER_BLOCK_SIZE
&& num_free
> MARKER_BLOCK_SIZE
)
5287 *mprev
= mblk
->next
;
5288 /* Unhook from the free list. */
5289 marker_free_list
= mblk
->markers
[0].u_free
.chain
;
5295 num_free
+= this_free
;
5296 mprev
= &mblk
->next
;
5300 total_markers
= num_used
;
5301 total_free_markers
= num_free
;
5304 /* Free all unmarked buffers */
5306 register struct buffer
*buffer
= all_buffers
, *prev
= 0, *next
;
5309 if (!XMARKBIT (buffer
->name
))
5312 prev
->next
= buffer
->next
;
5314 all_buffers
= buffer
->next
;
5315 next
= buffer
->next
;
5321 XUNMARK (buffer
->name
);
5322 UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer
));
5323 prev
= buffer
, buffer
= buffer
->next
;
5327 /* Free all unmarked vectors */
5329 register struct Lisp_Vector
*vector
= all_vectors
, *prev
= 0, *next
;
5330 total_vector_size
= 0;
5333 if (!(vector
->size
& ARRAY_MARK_FLAG
))
5336 prev
->next
= vector
->next
;
5338 all_vectors
= vector
->next
;
5339 next
= vector
->next
;
5347 vector
->size
&= ~ARRAY_MARK_FLAG
;
5348 if (vector
->size
& PSEUDOVECTOR_FLAG
)
5349 total_vector_size
+= (PSEUDOVECTOR_SIZE_MASK
& vector
->size
);
5351 total_vector_size
+= vector
->size
;
5352 prev
= vector
, vector
= vector
->next
;
5356 #ifdef GC_CHECK_STRING_BYTES
5357 if (!noninteractive
)
5358 check_string_bytes (1);
5365 /* Debugging aids. */
5367 DEFUN ("memory-limit", Fmemory_limit
, Smemory_limit
, 0, 0, 0,
5368 doc
: /* Return the address of the last byte Emacs has allocated, divided by 1024.
5369 This may be helpful in debugging Emacs's memory usage.
5370 We divide the value by 1024 to make sure it fits in a Lisp integer. */)
5375 XSETINT (end
, (EMACS_INT
) sbrk (0) / 1024);
5380 DEFUN ("memory-use-counts", Fmemory_use_counts
, Smemory_use_counts
, 0, 0, 0,
5381 doc
: /* Return a list of counters that measure how much consing there has been.
5382 Each of these counters increments for a certain kind of object.
5383 The counters wrap around from the largest positive integer to zero.
5384 Garbage collection does not decrease them.
5385 The elements of the value are as follows:
5386 (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS STRINGS)
5387 All are in units of 1 = one object consed
5388 except for VECTOR-CELLS and STRING-CHARS, which count the total length of
5390 MISCS include overlays, markers, and some internal types.
5391 Frames, windows, buffers, and subprocesses count as vectors
5392 (but the contents of a buffer's text do not count here). */)
5395 Lisp_Object consed
[8];
5397 consed
[0] = make_number (min (MOST_POSITIVE_FIXNUM
, cons_cells_consed
));
5398 consed
[1] = make_number (min (MOST_POSITIVE_FIXNUM
, floats_consed
));
5399 consed
[2] = make_number (min (MOST_POSITIVE_FIXNUM
, vector_cells_consed
));
5400 consed
[3] = make_number (min (MOST_POSITIVE_FIXNUM
, symbols_consed
));
5401 consed
[4] = make_number (min (MOST_POSITIVE_FIXNUM
, string_chars_consed
));
5402 consed
[5] = make_number (min (MOST_POSITIVE_FIXNUM
, misc_objects_consed
));
5403 consed
[6] = make_number (min (MOST_POSITIVE_FIXNUM
, intervals_consed
));
5404 consed
[7] = make_number (min (MOST_POSITIVE_FIXNUM
, strings_consed
));
5406 return Flist (8, consed
);
5409 int suppress_checking
;
5411 die (msg
, file
, line
)
5416 fprintf (stderr
, "\r\nEmacs fatal error: %s:%d: %s\r\n",
5421 /* Initialization */
5426 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
5428 pure_size
= PURESIZE
;
5429 pure_bytes_used
= 0;
5430 pure_bytes_used_before_overflow
= 0;
5432 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
5434 Vdead
= make_pure_string ("DEAD", 4, 4, 0);
5438 ignore_warnings
= 1;
5439 #ifdef DOUG_LEA_MALLOC
5440 mallopt (M_TRIM_THRESHOLD
, 128*1024); /* trim threshold */
5441 mallopt (M_MMAP_THRESHOLD
, 64*1024); /* mmap threshold */
5442 mallopt (M_MMAP_MAX
, MMAP_MAX_AREAS
); /* max. number of mmap'ed areas */
5452 malloc_hysteresis
= 32;
5454 malloc_hysteresis
= 0;
5457 spare_memory
= (char *) malloc (SPARE_MEMORY
);
5459 ignore_warnings
= 0;
5461 byte_stack_list
= 0;
5463 consing_since_gc
= 0;
5464 gc_cons_threshold
= 100000 * sizeof (Lisp_Object
);
5465 #ifdef VIRT_ADDR_VARIES
5466 malloc_sbrk_unused
= 1<<22; /* A large number */
5467 malloc_sbrk_used
= 100000; /* as reasonable as any number */
5468 #endif /* VIRT_ADDR_VARIES */
5475 byte_stack_list
= 0;
5477 #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
5478 setjmp_tested_p
= longjmps_done
= 0;
5486 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold
,
5487 doc
: /* *Number of bytes of consing between garbage collections.
5488 Garbage collection can happen automatically once this many bytes have been
5489 allocated since the last garbage collection. All data types count.
5491 Garbage collection happens automatically only when `eval' is called.
5493 By binding this temporarily to a large number, you can effectively
5494 prevent garbage collection during a part of the program. */);
5496 DEFVAR_INT ("pure-bytes-used", &pure_bytes_used
,
5497 doc
: /* Number of bytes of sharable Lisp data allocated so far. */);
5499 DEFVAR_INT ("cons-cells-consed", &cons_cells_consed
,
5500 doc
: /* Number of cons cells that have been consed so far. */);
5502 DEFVAR_INT ("floats-consed", &floats_consed
,
5503 doc
: /* Number of floats that have been consed so far. */);
5505 DEFVAR_INT ("vector-cells-consed", &vector_cells_consed
,
5506 doc
: /* Number of vector cells that have been consed so far. */);
5508 DEFVAR_INT ("symbols-consed", &symbols_consed
,
5509 doc
: /* Number of symbols that have been consed so far. */);
5511 DEFVAR_INT ("string-chars-consed", &string_chars_consed
,
5512 doc
: /* Number of string characters that have been consed so far. */);
5514 DEFVAR_INT ("misc-objects-consed", &misc_objects_consed
,
5515 doc
: /* Number of miscellaneous objects that have been consed so far. */);
5517 DEFVAR_INT ("intervals-consed", &intervals_consed
,
5518 doc
: /* Number of intervals that have been consed so far. */);
5520 DEFVAR_INT ("strings-consed", &strings_consed
,
5521 doc
: /* Number of strings that have been consed so far. */);
5523 DEFVAR_LISP ("purify-flag", &Vpurify_flag
,
5524 doc
: /* Non-nil means loading Lisp code in order to dump an executable.
5525 This means that certain objects should be allocated in shared (pure) space. */);
5527 DEFVAR_INT ("undo-limit", &undo_limit
,
5528 doc
: /* Keep no more undo information once it exceeds this size.
5529 This limit is applied when garbage collection happens.
5530 The size is counted as the number of bytes occupied,
5531 which includes both saved text and other data. */);
5534 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit
,
5535 doc
: /* Don't keep more than this much size of undo information.
5536 A command which pushes past this size is itself forgotten.
5537 This limit is applied when garbage collection happens.
5538 The size is counted as the number of bytes occupied,
5539 which includes both saved text and other data. */);
5540 undo_strong_limit
= 30000;
5542 DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages
,
5543 doc
: /* Non-nil means display messages at start and end of garbage collection. */);
5544 garbage_collection_messages
= 0;
5546 DEFVAR_LISP ("post-gc-hook", &Vpost_gc_hook
,
5547 doc
: /* Hook run after garbage collection has finished. */);
5548 Vpost_gc_hook
= Qnil
;
5549 Qpost_gc_hook
= intern ("post-gc-hook");
5550 staticpro (&Qpost_gc_hook
);
5552 DEFVAR_LISP ("memory-signal-data", &Vmemory_signal_data
,
5553 doc
: /* Precomputed `signal' argument for memory-full error. */);
5554 /* We build this in advance because if we wait until we need it, we might
5555 not be able to allocate the memory to hold it. */
5558 build_string ("Memory exhausted--use M-x save-some-buffers then exit and restart Emacs"));
5560 DEFVAR_LISP ("memory-full", &Vmemory_full
,
5561 doc
: /* Non-nil means we are handling a memory-full error. */);
5562 Vmemory_full
= Qnil
;
5564 staticpro (&Qgc_cons_threshold
);
5565 Qgc_cons_threshold
= intern ("gc-cons-threshold");
5567 staticpro (&Qchar_table_extra_slots
);
5568 Qchar_table_extra_slots
= intern ("char-table-extra-slots");
5573 defsubr (&Smake_byte_code
);
5574 defsubr (&Smake_list
);
5575 defsubr (&Smake_vector
);
5576 defsubr (&Smake_char_table
);
5577 defsubr (&Smake_string
);
5578 defsubr (&Smake_bool_vector
);
5579 defsubr (&Smake_symbol
);
5580 defsubr (&Smake_marker
);
5581 defsubr (&Spurecopy
);
5582 defsubr (&Sgarbage_collect
);
5583 defsubr (&Smemory_limit
);
5584 defsubr (&Smemory_use_counts
);
5586 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
5587 defsubr (&Sgc_status
);