1 /* Storage allocation and gc for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 86, 88, 93, 94, 95, 97, 98, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
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. */
29 /* Note that this declares bzero on OSF/1. How dumb. */
33 /* GC_MALLOC_CHECK defined means perform validity checks of malloc'd
34 memory. Can do this only if using gmalloc.c. */
36 #if defined SYSTEM_MALLOC || defined DOUG_LEA_MALLOC
37 #undef GC_MALLOC_CHECK
40 /* This file is part of the core Lisp implementation, and thus must
41 deal with the real data structures. If the Lisp implementation is
42 replaced, this file likely will not be used. */
44 #undef HIDE_LISP_IMPLEMENTATION
47 #include "intervals.h"
53 #include "blockinput.h"
55 #include "syssignal.h"
61 extern POINTER_TYPE
*sbrk ();
64 #ifdef DOUG_LEA_MALLOC
67 /* malloc.h #defines this as size_t, at least in glibc2. */
68 #ifndef __malloc_size_t
69 #define __malloc_size_t int
72 /* Specify maximum number of areas to mmap. It would be nice to use a
73 value that explicitly means "no limit". */
75 #define MMAP_MAX_AREAS 100000000
77 #else /* not DOUG_LEA_MALLOC */
79 /* The following come from gmalloc.c. */
81 #define __malloc_size_t size_t
82 extern __malloc_size_t _bytes_used
;
83 extern __malloc_size_t __malloc_extra_blocks
;
85 #endif /* not DOUG_LEA_MALLOC */
87 /* Macro to verify that storage intended for Lisp objects is not
88 out of range to fit in the space for a pointer.
89 ADDRESS is the start of the block, and SIZE
90 is the amount of space within which objects can start. */
92 #define VALIDATE_LISP_STORAGE(address, size) \
96 XSETCONS (val, (char *) address + size); \
97 if ((char *) XCONS (val) != (char *) address + size) \
104 /* Value of _bytes_used, when spare_memory was freed. */
106 static __malloc_size_t bytes_used_when_full
;
108 /* Mark, unmark, query mark bit of a Lisp string. S must be a pointer
109 to a struct Lisp_String. */
111 #define MARK_STRING(S) ((S)->size |= MARKBIT)
112 #define UNMARK_STRING(S) ((S)->size &= ~MARKBIT)
113 #define STRING_MARKED_P(S) ((S)->size & MARKBIT)
115 /* Value is the number of bytes/chars of S, a pointer to a struct
116 Lisp_String. This must be used instead of STRING_BYTES (S) or
117 S->size during GC, because S->size contains the mark bit for
120 #define GC_STRING_BYTES(S) (STRING_BYTES (S) & ~MARKBIT)
121 #define GC_STRING_CHARS(S) ((S)->size & ~MARKBIT)
123 /* Number of bytes of consing done since the last gc. */
125 int consing_since_gc
;
127 /* Count the amount of consing of various sorts of space. */
129 EMACS_INT cons_cells_consed
;
130 EMACS_INT floats_consed
;
131 EMACS_INT vector_cells_consed
;
132 EMACS_INT symbols_consed
;
133 EMACS_INT string_chars_consed
;
134 EMACS_INT misc_objects_consed
;
135 EMACS_INT intervals_consed
;
136 EMACS_INT strings_consed
;
138 /* Number of bytes of consing since GC before another GC should be done. */
140 EMACS_INT gc_cons_threshold
;
142 /* Nonzero during GC. */
146 /* Nonzero means display messages at beginning and end of GC. */
148 int garbage_collection_messages
;
150 #ifndef VIRT_ADDR_VARIES
152 #endif /* VIRT_ADDR_VARIES */
153 int malloc_sbrk_used
;
155 #ifndef VIRT_ADDR_VARIES
157 #endif /* VIRT_ADDR_VARIES */
158 int malloc_sbrk_unused
;
160 /* Two limits controlling how much undo information to keep. */
162 EMACS_INT undo_limit
;
163 EMACS_INT undo_strong_limit
;
165 /* Number of live and free conses etc. */
167 static int total_conses
, total_markers
, total_symbols
, total_vector_size
;
168 static int total_free_conses
, total_free_markers
, total_free_symbols
;
169 static int total_free_floats
, total_floats
;
171 /* Points to memory space allocated as "spare", to be freed if we run
174 static char *spare_memory
;
176 /* Amount of spare memory to keep in reserve. */
178 #define SPARE_MEMORY (1 << 14)
180 /* Number of extra blocks malloc should get when it needs more core. */
182 static int malloc_hysteresis
;
184 /* Non-nil means defun should do purecopy on the function definition. */
186 Lisp_Object Vpurify_flag
;
188 /* Non-nil means we are handling a memory-full error. */
190 Lisp_Object Vmemory_full
;
194 /* Force it into data space! */
196 EMACS_INT pure
[PURESIZE
/ sizeof (EMACS_INT
)] = {0,};
197 #define PUREBEG (char *) pure
201 #define pure PURE_SEG_BITS /* Use shared memory segment */
202 #define PUREBEG (char *)PURE_SEG_BITS
204 #endif /* HAVE_SHM */
206 /* Pointer to the pure area, and its size. */
208 static char *purebeg
;
209 static size_t pure_size
;
211 /* Number of bytes of pure storage used before pure storage overflowed.
212 If this is non-zero, this implies that an overflow occurred. */
214 static size_t pure_bytes_used_before_overflow
;
216 /* Value is non-zero if P points into pure space. */
218 #define PURE_POINTER_P(P) \
219 (((PNTR_COMPARISON_TYPE) (P) \
220 < (PNTR_COMPARISON_TYPE) ((char *) purebeg + pure_size)) \
221 && ((PNTR_COMPARISON_TYPE) (P) \
222 >= (PNTR_COMPARISON_TYPE) purebeg))
224 /* Index in pure at which next pure object will be allocated.. */
226 EMACS_INT pure_bytes_used
;
228 /* If nonzero, this is a warning delivered by malloc and not yet
231 char *pending_malloc_warning
;
233 /* Pre-computed signal argument for use when memory is exhausted. */
235 Lisp_Object Vmemory_signal_data
;
237 /* Maximum amount of C stack to save when a GC happens. */
239 #ifndef MAX_SAVE_STACK
240 #define MAX_SAVE_STACK 16000
243 /* Buffer in which we save a copy of the C stack at each GC. */
248 /* Non-zero means ignore malloc warnings. Set during initialization.
249 Currently not used. */
253 Lisp_Object Qgc_cons_threshold
, Qchar_table_extra_slots
;
255 /* Hook run after GC has finished. */
257 Lisp_Object Vpost_gc_hook
, Qpost_gc_hook
;
259 static void mark_buffer
P_ ((Lisp_Object
));
260 static void mark_kboards
P_ ((void));
261 static void gc_sweep
P_ ((void));
262 static void mark_glyph_matrix
P_ ((struct glyph_matrix
*));
263 static void mark_face_cache
P_ ((struct face_cache
*));
265 #ifdef HAVE_WINDOW_SYSTEM
266 static void mark_image
P_ ((struct image
*));
267 static void mark_image_cache
P_ ((struct frame
*));
268 #endif /* HAVE_WINDOW_SYSTEM */
270 static struct Lisp_String
*allocate_string
P_ ((void));
271 static void compact_small_strings
P_ ((void));
272 static void free_large_strings
P_ ((void));
273 static void sweep_strings
P_ ((void));
275 extern int message_enable_multibyte
;
277 /* When scanning the C stack for live Lisp objects, Emacs keeps track
278 of what memory allocated via lisp_malloc is intended for what
279 purpose. This enumeration specifies the type of memory. */
290 /* Keep the following vector-like types together, with
291 MEM_TYPE_WINDOW being the last, and MEM_TYPE_VECTOR the
292 first. Or change the code of live_vector_p, for instance. */
300 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
302 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
303 #include <stdio.h> /* For fprintf. */
306 /* A unique object in pure space used to make some Lisp objects
307 on free lists recognizable in O(1). */
311 #ifdef GC_MALLOC_CHECK
313 enum mem_type allocated_mem_type
;
314 int dont_register_blocks
;
316 #endif /* GC_MALLOC_CHECK */
318 /* A node in the red-black tree describing allocated memory containing
319 Lisp data. Each such block is recorded with its start and end
320 address when it is allocated, and removed from the tree when it
323 A red-black tree is a balanced binary tree with the following
326 1. Every node is either red or black.
327 2. Every leaf is black.
328 3. If a node is red, then both of its children are black.
329 4. Every simple path from a node to a descendant leaf contains
330 the same number of black nodes.
331 5. The root is always black.
333 When nodes are inserted into the tree, or deleted from the tree,
334 the tree is "fixed" so that these properties are always true.
336 A red-black tree with N internal nodes has height at most 2
337 log(N+1). Searches, insertions and deletions are done in O(log N).
338 Please see a text book about data structures for a detailed
339 description of red-black trees. Any book worth its salt should
344 /* Children of this node. These pointers are never NULL. When there
345 is no child, the value is MEM_NIL, which points to a dummy node. */
346 struct mem_node
*left
, *right
;
348 /* The parent of this node. In the root node, this is NULL. */
349 struct mem_node
*parent
;
351 /* Start and end of allocated region. */
355 enum {MEM_BLACK
, MEM_RED
} color
;
361 /* Base address of stack. Set in main. */
363 Lisp_Object
*stack_base
;
365 /* Root of the tree describing allocated Lisp memory. */
367 static struct mem_node
*mem_root
;
369 /* Lowest and highest known address in the heap. */
371 static void *min_heap_address
, *max_heap_address
;
373 /* Sentinel node of the tree. */
375 static struct mem_node mem_z
;
376 #define MEM_NIL &mem_z
378 static POINTER_TYPE
*lisp_malloc
P_ ((size_t, enum mem_type
));
379 static struct Lisp_Vector
*allocate_vectorlike
P_ ((EMACS_INT
, enum mem_type
));
380 static void lisp_free
P_ ((POINTER_TYPE
*));
381 static void mark_stack
P_ ((void));
382 static int live_vector_p
P_ ((struct mem_node
*, void *));
383 static int live_buffer_p
P_ ((struct mem_node
*, void *));
384 static int live_string_p
P_ ((struct mem_node
*, void *));
385 static int live_cons_p
P_ ((struct mem_node
*, void *));
386 static int live_symbol_p
P_ ((struct mem_node
*, void *));
387 static int live_float_p
P_ ((struct mem_node
*, void *));
388 static int live_misc_p
P_ ((struct mem_node
*, void *));
389 static void mark_maybe_object
P_ ((Lisp_Object
));
390 static void mark_memory
P_ ((void *, void *));
391 static void mem_init
P_ ((void));
392 static struct mem_node
*mem_insert
P_ ((void *, void *, enum mem_type
));
393 static void mem_insert_fixup
P_ ((struct mem_node
*));
394 static void mem_rotate_left
P_ ((struct mem_node
*));
395 static void mem_rotate_right
P_ ((struct mem_node
*));
396 static void mem_delete
P_ ((struct mem_node
*));
397 static void mem_delete_fixup
P_ ((struct mem_node
*));
398 static INLINE
struct mem_node
*mem_find
P_ ((void *));
400 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
401 static void check_gcpros
P_ ((void));
404 #endif /* GC_MARK_STACK || GC_MALLOC_CHECK */
406 /* Recording what needs to be marked for gc. */
408 struct gcpro
*gcprolist
;
410 /* Addresses of staticpro'd variables. */
412 #define NSTATICS 1280
413 Lisp_Object
*staticvec
[NSTATICS
] = {0};
415 /* Index of next unused slot in staticvec. */
419 static POINTER_TYPE
*pure_alloc
P_ ((size_t, int));
422 /* Value is SZ rounded up to the next multiple of ALIGNMENT.
423 ALIGNMENT must be a power of 2. */
425 #define ALIGN(SZ, ALIGNMENT) \
426 (((SZ) + (ALIGNMENT) - 1) & ~((ALIGNMENT) - 1))
430 /************************************************************************
432 ************************************************************************/
434 /* Function malloc calls this if it finds we are near exhausting storage. */
440 pending_malloc_warning
= str
;
444 /* Display an already-pending malloc warning. */
447 display_malloc_warning ()
449 call3 (intern ("display-warning"),
451 build_string (pending_malloc_warning
),
452 intern ("emergency"));
453 pending_malloc_warning
= 0;
457 #ifdef DOUG_LEA_MALLOC
458 # define BYTES_USED (mallinfo ().arena)
460 # define BYTES_USED _bytes_used
464 /* Called if malloc returns zero. */
471 #ifndef SYSTEM_MALLOC
472 bytes_used_when_full
= BYTES_USED
;
475 /* The first time we get here, free the spare memory. */
482 /* This used to call error, but if we've run out of memory, we could
483 get infinite recursion trying to build the string. */
485 Fsignal (Qnil
, Vmemory_signal_data
);
489 /* Called if we can't allocate relocatable space for a buffer. */
492 buffer_memory_full ()
494 /* If buffers use the relocating allocator, no need to free
495 spare_memory, because we may have plenty of malloc space left
496 that we could get, and if we don't, the malloc that fails will
497 itself cause spare_memory to be freed. If buffers don't use the
498 relocating allocator, treat this like any other failing
507 /* This used to call error, but if we've run out of memory, we could
508 get infinite recursion trying to build the string. */
510 Fsignal (Qnil
, Vmemory_signal_data
);
514 /* Like malloc but check for no memory and block interrupt input.. */
520 register POINTER_TYPE
*val
;
523 val
= (POINTER_TYPE
*) malloc (size
);
532 /* Like realloc but check for no memory and block interrupt input.. */
535 xrealloc (block
, size
)
539 register POINTER_TYPE
*val
;
542 /* We must call malloc explicitly when BLOCK is 0, since some
543 reallocs don't do this. */
545 val
= (POINTER_TYPE
*) malloc (size
);
547 val
= (POINTER_TYPE
*) realloc (block
, size
);
550 if (!val
&& size
) memory_full ();
555 /* Like free but block interrupt input.. */
567 /* Like strdup, but uses xmalloc. */
573 size_t len
= strlen (s
) + 1;
574 char *p
= (char *) xmalloc (len
);
580 /* Like malloc but used for allocating Lisp data. NBYTES is the
581 number of bytes to allocate, TYPE describes the intended use of the
582 allcated memory block (for strings, for conses, ...). */
584 static POINTER_TYPE
*
585 lisp_malloc (nbytes
, type
)
593 #ifdef GC_MALLOC_CHECK
594 allocated_mem_type
= type
;
597 val
= (void *) malloc (nbytes
);
599 #if GC_MARK_STACK && !defined GC_MALLOC_CHECK
600 if (val
&& type
!= MEM_TYPE_NON_LISP
)
601 mem_insert (val
, (char *) val
+ nbytes
, type
);
611 /* Return a new buffer structure allocated from the heap with
612 a call to lisp_malloc. */
618 = (struct buffer
*) lisp_malloc (sizeof (struct buffer
),
620 VALIDATE_LISP_STORAGE (b
, sizeof *b
);
625 /* Free BLOCK. This must be called to free memory allocated with a
626 call to lisp_malloc. */
634 #if GC_MARK_STACK && !defined GC_MALLOC_CHECK
635 mem_delete (mem_find (block
));
641 /* Arranging to disable input signals while we're in malloc.
643 This only works with GNU malloc. To help out systems which can't
644 use GNU malloc, all the calls to malloc, realloc, and free
645 elsewhere in the code should be inside a BLOCK_INPUT/UNBLOCK_INPUT
646 pairs; unfortunately, we have no idea what C library functions
647 might call malloc, so we can't really protect them unless you're
648 using GNU malloc. Fortunately, most of the major operating can use
651 #ifndef SYSTEM_MALLOC
652 #ifndef DOUG_LEA_MALLOC
653 extern void * (*__malloc_hook
) P_ ((size_t));
654 extern void * (*__realloc_hook
) P_ ((void *, size_t));
655 extern void (*__free_hook
) P_ ((void *));
656 /* Else declared in malloc.h, perhaps with an extra arg. */
657 #endif /* DOUG_LEA_MALLOC */
658 static void * (*old_malloc_hook
) ();
659 static void * (*old_realloc_hook
) ();
660 static void (*old_free_hook
) ();
662 /* This function is used as the hook for free to call. */
665 emacs_blocked_free (ptr
)
670 #ifdef GC_MALLOC_CHECK
676 if (m
== MEM_NIL
|| m
->start
!= ptr
)
679 "Freeing `%p' which wasn't allocated with malloc\n", ptr
);
684 /* fprintf (stderr, "free %p...%p (%p)\n", m->start, m->end, ptr); */
688 #endif /* GC_MALLOC_CHECK */
690 __free_hook
= old_free_hook
;
693 /* If we released our reserve (due to running out of memory),
694 and we have a fair amount free once again,
695 try to set aside another reserve in case we run out once more. */
696 if (spare_memory
== 0
697 /* Verify there is enough space that even with the malloc
698 hysteresis this call won't run out again.
699 The code here is correct as long as SPARE_MEMORY
700 is substantially larger than the block size malloc uses. */
701 && (bytes_used_when_full
702 > BYTES_USED
+ max (malloc_hysteresis
, 4) * SPARE_MEMORY
))
703 spare_memory
= (char *) malloc ((size_t) SPARE_MEMORY
);
705 __free_hook
= emacs_blocked_free
;
710 /* If we released our reserve (due to running out of memory),
711 and we have a fair amount free once again,
712 try to set aside another reserve in case we run out once more.
714 This is called when a relocatable block is freed in ralloc.c. */
717 refill_memory_reserve ()
719 if (spare_memory
== 0)
720 spare_memory
= (char *) malloc ((size_t) SPARE_MEMORY
);
724 /* This function is the malloc hook that Emacs uses. */
727 emacs_blocked_malloc (size
)
733 __malloc_hook
= old_malloc_hook
;
734 #ifdef DOUG_LEA_MALLOC
735 mallopt (M_TOP_PAD
, malloc_hysteresis
* 4096);
737 __malloc_extra_blocks
= malloc_hysteresis
;
740 value
= (void *) malloc (size
);
742 #ifdef GC_MALLOC_CHECK
744 struct mem_node
*m
= mem_find (value
);
747 fprintf (stderr
, "Malloc returned %p which is already in use\n",
749 fprintf (stderr
, "Region in use is %p...%p, %u bytes, type %d\n",
750 m
->start
, m
->end
, (char *) m
->end
- (char *) m
->start
,
755 if (!dont_register_blocks
)
757 mem_insert (value
, (char *) value
+ max (1, size
), allocated_mem_type
);
758 allocated_mem_type
= MEM_TYPE_NON_LISP
;
761 #endif /* GC_MALLOC_CHECK */
763 __malloc_hook
= emacs_blocked_malloc
;
766 /* fprintf (stderr, "%p malloc\n", value); */
771 /* This function is the realloc hook that Emacs uses. */
774 emacs_blocked_realloc (ptr
, size
)
781 __realloc_hook
= old_realloc_hook
;
783 #ifdef GC_MALLOC_CHECK
786 struct mem_node
*m
= mem_find (ptr
);
787 if (m
== MEM_NIL
|| m
->start
!= ptr
)
790 "Realloc of %p which wasn't allocated with malloc\n",
798 /* fprintf (stderr, "%p -> realloc\n", ptr); */
800 /* Prevent malloc from registering blocks. */
801 dont_register_blocks
= 1;
802 #endif /* GC_MALLOC_CHECK */
804 value
= (void *) realloc (ptr
, size
);
806 #ifdef GC_MALLOC_CHECK
807 dont_register_blocks
= 0;
810 struct mem_node
*m
= mem_find (value
);
813 fprintf (stderr
, "Realloc returns memory that is already in use\n");
817 /* Can't handle zero size regions in the red-black tree. */
818 mem_insert (value
, (char *) value
+ max (size
, 1), MEM_TYPE_NON_LISP
);
821 /* fprintf (stderr, "%p <- realloc\n", value); */
822 #endif /* GC_MALLOC_CHECK */
824 __realloc_hook
= emacs_blocked_realloc
;
831 /* Called from main to set up malloc to use our hooks. */
834 uninterrupt_malloc ()
836 if (__free_hook
!= emacs_blocked_free
)
837 old_free_hook
= __free_hook
;
838 __free_hook
= emacs_blocked_free
;
840 if (__malloc_hook
!= emacs_blocked_malloc
)
841 old_malloc_hook
= __malloc_hook
;
842 __malloc_hook
= emacs_blocked_malloc
;
844 if (__realloc_hook
!= emacs_blocked_realloc
)
845 old_realloc_hook
= __realloc_hook
;
846 __realloc_hook
= emacs_blocked_realloc
;
849 #endif /* not SYSTEM_MALLOC */
853 /***********************************************************************
855 ***********************************************************************/
857 /* Number of intervals allocated in an interval_block structure.
858 The 1020 is 1024 minus malloc overhead. */
860 #define INTERVAL_BLOCK_SIZE \
861 ((1020 - sizeof (struct interval_block *)) / sizeof (struct interval))
863 /* Intervals are allocated in chunks in form of an interval_block
866 struct interval_block
868 struct interval_block
*next
;
869 struct interval intervals
[INTERVAL_BLOCK_SIZE
];
872 /* Current interval block. Its `next' pointer points to older
875 struct interval_block
*interval_block
;
877 /* Index in interval_block above of the next unused interval
880 static int interval_block_index
;
882 /* Number of free and live intervals. */
884 static int total_free_intervals
, total_intervals
;
886 /* List of free intervals. */
888 INTERVAL interval_free_list
;
890 /* Total number of interval blocks now in use. */
892 int n_interval_blocks
;
895 /* Initialize interval allocation. */
901 = (struct interval_block
*) lisp_malloc (sizeof *interval_block
,
903 interval_block
->next
= 0;
904 bzero ((char *) interval_block
->intervals
, sizeof interval_block
->intervals
);
905 interval_block_index
= 0;
906 interval_free_list
= 0;
907 n_interval_blocks
= 1;
911 /* Return a new interval. */
918 if (interval_free_list
)
920 val
= interval_free_list
;
921 interval_free_list
= INTERVAL_PARENT (interval_free_list
);
925 if (interval_block_index
== INTERVAL_BLOCK_SIZE
)
927 register struct interval_block
*newi
;
929 newi
= (struct interval_block
*) lisp_malloc (sizeof *newi
,
932 VALIDATE_LISP_STORAGE (newi
, sizeof *newi
);
933 newi
->next
= interval_block
;
934 interval_block
= newi
;
935 interval_block_index
= 0;
938 val
= &interval_block
->intervals
[interval_block_index
++];
940 consing_since_gc
+= sizeof (struct interval
);
942 RESET_INTERVAL (val
);
947 /* Mark Lisp objects in interval I. */
950 mark_interval (i
, dummy
)
954 if (XMARKBIT (i
->plist
))
956 mark_object (&i
->plist
);
961 /* Mark the interval tree rooted in TREE. Don't call this directly;
962 use the macro MARK_INTERVAL_TREE instead. */
965 mark_interval_tree (tree
)
966 register INTERVAL tree
;
968 /* No need to test if this tree has been marked already; this
969 function is always called through the MARK_INTERVAL_TREE macro,
970 which takes care of that. */
972 /* XMARK expands to an assignment; the LHS of an assignment can't be
974 XMARK (tree
->up
.obj
);
976 traverse_intervals_noorder (tree
, mark_interval
, Qnil
);
980 /* Mark the interval tree rooted in I. */
982 #define MARK_INTERVAL_TREE(i) \
984 if (!NULL_INTERVAL_P (i) \
985 && ! XMARKBIT (i->up.obj)) \
986 mark_interval_tree (i); \
990 /* The oddity in the call to XUNMARK is necessary because XUNMARK
991 expands to an assignment to its argument, and most C compilers
992 don't support casts on the left operand of `='. */
994 #define UNMARK_BALANCE_INTERVALS(i) \
996 if (! NULL_INTERVAL_P (i)) \
998 XUNMARK ((i)->up.obj); \
999 (i) = balance_intervals (i); \
1004 /* Number support. If NO_UNION_TYPE isn't in effect, we
1005 can't create number objects in macros. */
1013 obj
.s
.type
= Lisp_Int
;
1018 /***********************************************************************
1020 ***********************************************************************/
1022 /* Lisp_Strings are allocated in string_block structures. When a new
1023 string_block is allocated, all the Lisp_Strings it contains are
1024 added to a free-list string_free_list. When a new Lisp_String is
1025 needed, it is taken from that list. During the sweep phase of GC,
1026 string_blocks that are entirely free are freed, except two which
1029 String data is allocated from sblock structures. Strings larger
1030 than LARGE_STRING_BYTES, get their own sblock, data for smaller
1031 strings is sub-allocated out of sblocks of size SBLOCK_SIZE.
1033 Sblocks consist internally of sdata structures, one for each
1034 Lisp_String. The sdata structure points to the Lisp_String it
1035 belongs to. The Lisp_String points back to the `u.data' member of
1036 its sdata structure.
1038 When a Lisp_String is freed during GC, it is put back on
1039 string_free_list, and its `data' member and its sdata's `string'
1040 pointer is set to null. The size of the string is recorded in the
1041 `u.nbytes' member of the sdata. So, sdata structures that are no
1042 longer used, can be easily recognized, and it's easy to compact the
1043 sblocks of small strings which we do in compact_small_strings. */
1045 /* Size in bytes of an sblock structure used for small strings. This
1046 is 8192 minus malloc overhead. */
1048 #define SBLOCK_SIZE 8188
1050 /* Strings larger than this are considered large strings. String data
1051 for large strings is allocated from individual sblocks. */
1053 #define LARGE_STRING_BYTES 1024
1055 /* Structure describing string memory sub-allocated from an sblock.
1056 This is where the contents of Lisp strings are stored. */
1060 /* Back-pointer to the string this sdata belongs to. If null, this
1061 structure is free, and the NBYTES member of the union below
1062 contains the string's byte size (the same value that STRING_BYTES
1063 would return if STRING were non-null). If non-null, STRING_BYTES
1064 (STRING) is the size of the data, and DATA contains the string's
1066 struct Lisp_String
*string
;
1068 #ifdef GC_CHECK_STRING_BYTES
1071 unsigned char data
[1];
1073 #define SDATA_NBYTES(S) (S)->nbytes
1074 #define SDATA_DATA(S) (S)->data
1076 #else /* not GC_CHECK_STRING_BYTES */
1080 /* When STRING in non-null. */
1081 unsigned char data
[1];
1083 /* When STRING is null. */
1088 #define SDATA_NBYTES(S) (S)->u.nbytes
1089 #define SDATA_DATA(S) (S)->u.data
1091 #endif /* not GC_CHECK_STRING_BYTES */
1095 /* Structure describing a block of memory which is sub-allocated to
1096 obtain string data memory for strings. Blocks for small strings
1097 are of fixed size SBLOCK_SIZE. Blocks for large strings are made
1098 as large as needed. */
1103 struct sblock
*next
;
1105 /* Pointer to the next free sdata block. This points past the end
1106 of the sblock if there isn't any space left in this block. */
1107 struct sdata
*next_free
;
1109 /* Start of data. */
1110 struct sdata first_data
;
1113 /* Number of Lisp strings in a string_block structure. The 1020 is
1114 1024 minus malloc overhead. */
1116 #define STRINGS_IN_STRING_BLOCK \
1117 ((1020 - sizeof (struct string_block *)) / sizeof (struct Lisp_String))
1119 /* Structure describing a block from which Lisp_String structures
1124 struct string_block
*next
;
1125 struct Lisp_String strings
[STRINGS_IN_STRING_BLOCK
];
1128 /* Head and tail of the list of sblock structures holding Lisp string
1129 data. We always allocate from current_sblock. The NEXT pointers
1130 in the sblock structures go from oldest_sblock to current_sblock. */
1132 static struct sblock
*oldest_sblock
, *current_sblock
;
1134 /* List of sblocks for large strings. */
1136 static struct sblock
*large_sblocks
;
1138 /* List of string_block structures, and how many there are. */
1140 static struct string_block
*string_blocks
;
1141 static int n_string_blocks
;
1143 /* Free-list of Lisp_Strings. */
1145 static struct Lisp_String
*string_free_list
;
1147 /* Number of live and free Lisp_Strings. */
1149 static int total_strings
, total_free_strings
;
1151 /* Number of bytes used by live strings. */
1153 static int total_string_size
;
1155 /* Given a pointer to a Lisp_String S which is on the free-list
1156 string_free_list, return a pointer to its successor in the
1159 #define NEXT_FREE_LISP_STRING(S) (*(struct Lisp_String **) (S))
1161 /* Return a pointer to the sdata structure belonging to Lisp string S.
1162 S must be live, i.e. S->data must not be null. S->data is actually
1163 a pointer to the `u.data' member of its sdata structure; the
1164 structure starts at a constant offset in front of that. */
1166 #ifdef GC_CHECK_STRING_BYTES
1168 #define SDATA_OF_STRING(S) \
1169 ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *) \
1170 - sizeof (EMACS_INT)))
1172 #else /* not GC_CHECK_STRING_BYTES */
1174 #define SDATA_OF_STRING(S) \
1175 ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *)))
1177 #endif /* not GC_CHECK_STRING_BYTES */
1179 /* Value is the size of an sdata structure large enough to hold NBYTES
1180 bytes of string data. The value returned includes a terminating
1181 NUL byte, the size of the sdata structure, and padding. */
1183 #ifdef GC_CHECK_STRING_BYTES
1185 #define SDATA_SIZE(NBYTES) \
1186 ((sizeof (struct Lisp_String *) \
1188 + sizeof (EMACS_INT) \
1189 + sizeof (EMACS_INT) - 1) \
1190 & ~(sizeof (EMACS_INT) - 1))
1192 #else /* not GC_CHECK_STRING_BYTES */
1194 #define SDATA_SIZE(NBYTES) \
1195 ((sizeof (struct Lisp_String *) \
1197 + sizeof (EMACS_INT) - 1) \
1198 & ~(sizeof (EMACS_INT) - 1))
1200 #endif /* not GC_CHECK_STRING_BYTES */
1202 /* Initialize string allocation. Called from init_alloc_once. */
1207 total_strings
= total_free_strings
= total_string_size
= 0;
1208 oldest_sblock
= current_sblock
= large_sblocks
= NULL
;
1209 string_blocks
= NULL
;
1210 n_string_blocks
= 0;
1211 string_free_list
= NULL
;
1215 #ifdef GC_CHECK_STRING_BYTES
1217 static int check_string_bytes_count
;
1219 void check_string_bytes
P_ ((int));
1220 void check_sblock
P_ ((struct sblock
*));
1222 #define CHECK_STRING_BYTES(S) STRING_BYTES (S)
1225 /* Like GC_STRING_BYTES, but with debugging check. */
1229 struct Lisp_String
*s
;
1231 int nbytes
= (s
->size_byte
< 0 ? s
->size
: s
->size_byte
) & ~MARKBIT
;
1232 if (!PURE_POINTER_P (s
)
1234 && nbytes
!= SDATA_NBYTES (SDATA_OF_STRING (s
)))
1239 /* Check validity Lisp strings' string_bytes member in B. */
1245 struct sdata
*from
, *end
, *from_end
;
1249 for (from
= &b
->first_data
; from
< end
; from
= from_end
)
1251 /* Compute the next FROM here because copying below may
1252 overwrite data we need to compute it. */
1255 /* Check that the string size recorded in the string is the
1256 same as the one recorded in the sdata structure. */
1258 CHECK_STRING_BYTES (from
->string
);
1261 nbytes
= GC_STRING_BYTES (from
->string
);
1263 nbytes
= SDATA_NBYTES (from
);
1265 nbytes
= SDATA_SIZE (nbytes
);
1266 from_end
= (struct sdata
*) ((char *) from
+ nbytes
);
1271 /* Check validity of Lisp strings' string_bytes member. ALL_P
1272 non-zero means check all strings, otherwise check only most
1273 recently allocated strings. Used for hunting a bug. */
1276 check_string_bytes (all_p
)
1283 for (b
= large_sblocks
; b
; b
= b
->next
)
1285 struct Lisp_String
*s
= b
->first_data
.string
;
1287 CHECK_STRING_BYTES (s
);
1290 for (b
= oldest_sblock
; b
; b
= b
->next
)
1294 check_sblock (current_sblock
);
1297 #endif /* GC_CHECK_STRING_BYTES */
1300 /* Return a new Lisp_String. */
1302 static struct Lisp_String
*
1305 struct Lisp_String
*s
;
1307 /* If the free-list is empty, allocate a new string_block, and
1308 add all the Lisp_Strings in it to the free-list. */
1309 if (string_free_list
== NULL
)
1311 struct string_block
*b
;
1314 b
= (struct string_block
*) lisp_malloc (sizeof *b
, MEM_TYPE_STRING
);
1315 VALIDATE_LISP_STORAGE (b
, sizeof *b
);
1316 bzero (b
, sizeof *b
);
1317 b
->next
= string_blocks
;
1321 for (i
= STRINGS_IN_STRING_BLOCK
- 1; i
>= 0; --i
)
1324 NEXT_FREE_LISP_STRING (s
) = string_free_list
;
1325 string_free_list
= s
;
1328 total_free_strings
+= STRINGS_IN_STRING_BLOCK
;
1331 /* Pop a Lisp_String off the free-list. */
1332 s
= string_free_list
;
1333 string_free_list
= NEXT_FREE_LISP_STRING (s
);
1335 /* Probably not strictly necessary, but play it safe. */
1336 bzero (s
, sizeof *s
);
1338 --total_free_strings
;
1341 consing_since_gc
+= sizeof *s
;
1343 #ifdef GC_CHECK_STRING_BYTES
1350 if (++check_string_bytes_count
== 200)
1352 check_string_bytes_count
= 0;
1353 check_string_bytes (1);
1356 check_string_bytes (0);
1358 #endif /* GC_CHECK_STRING_BYTES */
1364 /* Set up Lisp_String S for holding NCHARS characters, NBYTES bytes,
1365 plus a NUL byte at the end. Allocate an sdata structure for S, and
1366 set S->data to its `u.data' member. Store a NUL byte at the end of
1367 S->data. Set S->size to NCHARS and S->size_byte to NBYTES. Free
1368 S->data if it was initially non-null. */
1371 allocate_string_data (s
, nchars
, nbytes
)
1372 struct Lisp_String
*s
;
1375 struct sdata
*data
, *old_data
;
1377 int needed
, old_nbytes
;
1379 /* Determine the number of bytes needed to store NBYTES bytes
1381 needed
= SDATA_SIZE (nbytes
);
1383 if (nbytes
> LARGE_STRING_BYTES
)
1385 size_t size
= sizeof *b
- sizeof (struct sdata
) + needed
;
1387 #ifdef DOUG_LEA_MALLOC
1388 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed
1389 because mapped region contents are not preserved in
1391 mallopt (M_MMAP_MAX
, 0);
1394 b
= (struct sblock
*) lisp_malloc (size
, MEM_TYPE_NON_LISP
);
1396 #ifdef DOUG_LEA_MALLOC
1397 /* Back to a reasonable maximum of mmap'ed areas. */
1398 mallopt (M_MMAP_MAX
, MMAP_MAX_AREAS
);
1401 b
->next_free
= &b
->first_data
;
1402 b
->first_data
.string
= NULL
;
1403 b
->next
= large_sblocks
;
1406 else if (current_sblock
== NULL
1407 || (((char *) current_sblock
+ SBLOCK_SIZE
1408 - (char *) current_sblock
->next_free
)
1411 /* Not enough room in the current sblock. */
1412 b
= (struct sblock
*) lisp_malloc (SBLOCK_SIZE
, MEM_TYPE_NON_LISP
);
1413 b
->next_free
= &b
->first_data
;
1414 b
->first_data
.string
= NULL
;
1418 current_sblock
->next
= b
;
1426 old_data
= s
->data
? SDATA_OF_STRING (s
) : NULL
;
1427 old_nbytes
= GC_STRING_BYTES (s
);
1429 data
= b
->next_free
;
1431 s
->data
= SDATA_DATA (data
);
1432 #ifdef GC_CHECK_STRING_BYTES
1433 SDATA_NBYTES (data
) = nbytes
;
1436 s
->size_byte
= nbytes
;
1437 s
->data
[nbytes
] = '\0';
1438 b
->next_free
= (struct sdata
*) ((char *) data
+ needed
);
1440 /* If S had already data assigned, mark that as free by setting its
1441 string back-pointer to null, and recording the size of the data
1445 SDATA_NBYTES (old_data
) = old_nbytes
;
1446 old_data
->string
= NULL
;
1449 consing_since_gc
+= needed
;
1453 /* Sweep and compact strings. */
1458 struct string_block
*b
, *next
;
1459 struct string_block
*live_blocks
= NULL
;
1461 string_free_list
= NULL
;
1462 total_strings
= total_free_strings
= 0;
1463 total_string_size
= 0;
1465 /* Scan strings_blocks, free Lisp_Strings that aren't marked. */
1466 for (b
= string_blocks
; b
; b
= next
)
1469 struct Lisp_String
*free_list_before
= string_free_list
;
1473 for (i
= 0; i
< STRINGS_IN_STRING_BLOCK
; ++i
)
1475 struct Lisp_String
*s
= b
->strings
+ i
;
1479 /* String was not on free-list before. */
1480 if (STRING_MARKED_P (s
))
1482 /* String is live; unmark it and its intervals. */
1485 if (!NULL_INTERVAL_P (s
->intervals
))
1486 UNMARK_BALANCE_INTERVALS (s
->intervals
);
1489 total_string_size
+= STRING_BYTES (s
);
1493 /* String is dead. Put it on the free-list. */
1494 struct sdata
*data
= SDATA_OF_STRING (s
);
1496 /* Save the size of S in its sdata so that we know
1497 how large that is. Reset the sdata's string
1498 back-pointer so that we know it's free. */
1499 #ifdef GC_CHECK_STRING_BYTES
1500 if (GC_STRING_BYTES (s
) != SDATA_NBYTES (data
))
1503 data
->u
.nbytes
= GC_STRING_BYTES (s
);
1505 data
->string
= NULL
;
1507 /* Reset the strings's `data' member so that we
1511 /* Put the string on the free-list. */
1512 NEXT_FREE_LISP_STRING (s
) = string_free_list
;
1513 string_free_list
= s
;
1519 /* S was on the free-list before. Put it there again. */
1520 NEXT_FREE_LISP_STRING (s
) = string_free_list
;
1521 string_free_list
= s
;
1526 /* Free blocks that contain free Lisp_Strings only, except
1527 the first two of them. */
1528 if (nfree
== STRINGS_IN_STRING_BLOCK
1529 && total_free_strings
> STRINGS_IN_STRING_BLOCK
)
1533 string_free_list
= free_list_before
;
1537 total_free_strings
+= nfree
;
1538 b
->next
= live_blocks
;
1543 string_blocks
= live_blocks
;
1544 free_large_strings ();
1545 compact_small_strings ();
1549 /* Free dead large strings. */
1552 free_large_strings ()
1554 struct sblock
*b
, *next
;
1555 struct sblock
*live_blocks
= NULL
;
1557 for (b
= large_sblocks
; b
; b
= next
)
1561 if (b
->first_data
.string
== NULL
)
1565 b
->next
= live_blocks
;
1570 large_sblocks
= live_blocks
;
1574 /* Compact data of small strings. Free sblocks that don't contain
1575 data of live strings after compaction. */
1578 compact_small_strings ()
1580 struct sblock
*b
, *tb
, *next
;
1581 struct sdata
*from
, *to
, *end
, *tb_end
;
1582 struct sdata
*to_end
, *from_end
;
1584 /* TB is the sblock we copy to, TO is the sdata within TB we copy
1585 to, and TB_END is the end of TB. */
1587 tb_end
= (struct sdata
*) ((char *) tb
+ SBLOCK_SIZE
);
1588 to
= &tb
->first_data
;
1590 /* Step through the blocks from the oldest to the youngest. We
1591 expect that old blocks will stabilize over time, so that less
1592 copying will happen this way. */
1593 for (b
= oldest_sblock
; b
; b
= b
->next
)
1596 xassert ((char *) end
<= (char *) b
+ SBLOCK_SIZE
);
1598 for (from
= &b
->first_data
; from
< end
; from
= from_end
)
1600 /* Compute the next FROM here because copying below may
1601 overwrite data we need to compute it. */
1604 #ifdef GC_CHECK_STRING_BYTES
1605 /* Check that the string size recorded in the string is the
1606 same as the one recorded in the sdata structure. */
1608 && GC_STRING_BYTES (from
->string
) != SDATA_NBYTES (from
))
1610 #endif /* GC_CHECK_STRING_BYTES */
1613 nbytes
= GC_STRING_BYTES (from
->string
);
1615 nbytes
= SDATA_NBYTES (from
);
1617 nbytes
= SDATA_SIZE (nbytes
);
1618 from_end
= (struct sdata
*) ((char *) from
+ nbytes
);
1620 /* FROM->string non-null means it's alive. Copy its data. */
1623 /* If TB is full, proceed with the next sblock. */
1624 to_end
= (struct sdata
*) ((char *) to
+ nbytes
);
1625 if (to_end
> tb_end
)
1629 tb_end
= (struct sdata
*) ((char *) tb
+ SBLOCK_SIZE
);
1630 to
= &tb
->first_data
;
1631 to_end
= (struct sdata
*) ((char *) to
+ nbytes
);
1634 /* Copy, and update the string's `data' pointer. */
1637 xassert (tb
!= b
|| to
<= from
);
1638 safe_bcopy ((char *) from
, (char *) to
, nbytes
);
1639 to
->string
->data
= SDATA_DATA (to
);
1642 /* Advance past the sdata we copied to. */
1648 /* The rest of the sblocks following TB don't contain live data, so
1649 we can free them. */
1650 for (b
= tb
->next
; b
; b
= next
)
1658 current_sblock
= tb
;
1662 DEFUN ("make-string", Fmake_string
, Smake_string
, 2, 2, 0,
1663 doc
: /* Return a newly created string of length LENGTH, with each element being INIT.
1664 Both LENGTH and INIT must be numbers. */)
1666 Lisp_Object length
, init
;
1668 register Lisp_Object val
;
1669 register unsigned char *p
, *end
;
1672 CHECK_NATNUM (length
);
1673 CHECK_NUMBER (init
);
1676 if (SINGLE_BYTE_CHAR_P (c
))
1678 nbytes
= XINT (length
);
1679 val
= make_uninit_string (nbytes
);
1681 end
= p
+ SCHARS (val
);
1687 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
1688 int len
= CHAR_STRING (c
, str
);
1690 nbytes
= len
* XINT (length
);
1691 val
= make_uninit_multibyte_string (XINT (length
), nbytes
);
1696 bcopy (str
, p
, len
);
1706 DEFUN ("make-bool-vector", Fmake_bool_vector
, Smake_bool_vector
, 2, 2, 0,
1707 doc
: /* Return a new bool-vector of length LENGTH, using INIT for as each element.
1708 LENGTH must be a number. INIT matters only in whether it is t or nil. */)
1710 Lisp_Object length
, init
;
1712 register Lisp_Object val
;
1713 struct Lisp_Bool_Vector
*p
;
1715 int length_in_chars
, length_in_elts
, bits_per_value
;
1717 CHECK_NATNUM (length
);
1719 bits_per_value
= sizeof (EMACS_INT
) * BITS_PER_CHAR
;
1721 length_in_elts
= (XFASTINT (length
) + bits_per_value
- 1) / bits_per_value
;
1722 length_in_chars
= ((XFASTINT (length
) + BITS_PER_CHAR
- 1) / BITS_PER_CHAR
);
1724 /* We must allocate one more elements than LENGTH_IN_ELTS for the
1725 slot `size' of the struct Lisp_Bool_Vector. */
1726 val
= Fmake_vector (make_number (length_in_elts
+ 1), Qnil
);
1727 p
= XBOOL_VECTOR (val
);
1729 /* Get rid of any bits that would cause confusion. */
1731 XSETBOOL_VECTOR (val
, p
);
1732 p
->size
= XFASTINT (length
);
1734 real_init
= (NILP (init
) ? 0 : -1);
1735 for (i
= 0; i
< length_in_chars
; i
++)
1736 p
->data
[i
] = real_init
;
1738 /* Clear the extraneous bits in the last byte. */
1739 if (XINT (length
) != length_in_chars
* BITS_PER_CHAR
)
1740 XBOOL_VECTOR (val
)->data
[length_in_chars
- 1]
1741 &= (1 << (XINT (length
) % BITS_PER_CHAR
)) - 1;
1747 /* Make a string from NBYTES bytes at CONTENTS, and compute the number
1748 of characters from the contents. This string may be unibyte or
1749 multibyte, depending on the contents. */
1752 make_string (contents
, nbytes
)
1753 const char *contents
;
1756 register Lisp_Object val
;
1757 int nchars
, multibyte_nbytes
;
1759 parse_str_as_multibyte (contents
, nbytes
, &nchars
, &multibyte_nbytes
);
1760 if (nbytes
== nchars
|| nbytes
!= multibyte_nbytes
)
1761 /* CONTENTS contains no multibyte sequences or contains an invalid
1762 multibyte sequence. We must make unibyte string. */
1763 val
= make_unibyte_string (contents
, nbytes
);
1765 val
= make_multibyte_string (contents
, nchars
, nbytes
);
1770 /* Make an unibyte string from LENGTH bytes at CONTENTS. */
1773 make_unibyte_string (contents
, length
)
1774 const char *contents
;
1777 register Lisp_Object val
;
1778 val
= make_uninit_string (length
);
1779 bcopy (contents
, SDATA (val
), length
);
1780 STRING_SET_UNIBYTE (val
);
1785 /* Make a multibyte string from NCHARS characters occupying NBYTES
1786 bytes at CONTENTS. */
1789 make_multibyte_string (contents
, nchars
, nbytes
)
1790 const char *contents
;
1793 register Lisp_Object val
;
1794 val
= make_uninit_multibyte_string (nchars
, nbytes
);
1795 bcopy (contents
, SDATA (val
), nbytes
);
1800 /* Make a string from NCHARS characters occupying NBYTES bytes at
1801 CONTENTS. It is a multibyte string if NBYTES != NCHARS. */
1804 make_string_from_bytes (contents
, nchars
, nbytes
)
1808 register Lisp_Object val
;
1809 val
= make_uninit_multibyte_string (nchars
, nbytes
);
1810 bcopy (contents
, SDATA (val
), nbytes
);
1811 if (SBYTES (val
) == SCHARS (val
))
1812 STRING_SET_UNIBYTE (val
);
1817 /* Make a string from NCHARS characters occupying NBYTES bytes at
1818 CONTENTS. The argument MULTIBYTE controls whether to label the
1819 string as multibyte. */
1822 make_specified_string (contents
, nchars
, nbytes
, multibyte
)
1827 register Lisp_Object val
;
1828 val
= make_uninit_multibyte_string (nchars
, nbytes
);
1829 bcopy (contents
, SDATA (val
), nbytes
);
1831 STRING_SET_UNIBYTE (val
);
1836 /* Make a string from the data at STR, treating it as multibyte if the
1843 return make_string (str
, strlen (str
));
1847 /* Return an unibyte Lisp_String set up to hold LENGTH characters
1848 occupying LENGTH bytes. */
1851 make_uninit_string (length
)
1855 val
= make_uninit_multibyte_string (length
, length
);
1856 STRING_SET_UNIBYTE (val
);
1861 /* Return a multibyte Lisp_String set up to hold NCHARS characters
1862 which occupy NBYTES bytes. */
1865 make_uninit_multibyte_string (nchars
, nbytes
)
1869 struct Lisp_String
*s
;
1874 s
= allocate_string ();
1875 allocate_string_data (s
, nchars
, nbytes
);
1876 XSETSTRING (string
, s
);
1877 string_chars_consed
+= nbytes
;
1883 /***********************************************************************
1885 ***********************************************************************/
1887 /* We store float cells inside of float_blocks, allocating a new
1888 float_block with malloc whenever necessary. Float cells reclaimed
1889 by GC are put on a free list to be reallocated before allocating
1890 any new float cells from the latest float_block.
1892 Each float_block is just under 1020 bytes long, since malloc really
1893 allocates in units of powers of two and uses 4 bytes for its own
1896 #define FLOAT_BLOCK_SIZE \
1897 ((1020 - sizeof (struct float_block *)) / sizeof (struct Lisp_Float))
1901 struct float_block
*next
;
1902 struct Lisp_Float floats
[FLOAT_BLOCK_SIZE
];
1905 /* Current float_block. */
1907 struct float_block
*float_block
;
1909 /* Index of first unused Lisp_Float in the current float_block. */
1911 int float_block_index
;
1913 /* Total number of float blocks now in use. */
1917 /* Free-list of Lisp_Floats. */
1919 struct Lisp_Float
*float_free_list
;
1922 /* Initialize float allocation. */
1927 float_block
= (struct float_block
*) lisp_malloc (sizeof *float_block
,
1929 float_block
->next
= 0;
1930 bzero ((char *) float_block
->floats
, sizeof float_block
->floats
);
1931 float_block_index
= 0;
1932 float_free_list
= 0;
1937 /* Explicitly free a float cell by putting it on the free-list. */
1941 struct Lisp_Float
*ptr
;
1943 *(struct Lisp_Float
**)&ptr
->data
= float_free_list
;
1947 float_free_list
= ptr
;
1951 /* Return a new float object with value FLOAT_VALUE. */
1954 make_float (float_value
)
1957 register Lisp_Object val
;
1959 if (float_free_list
)
1961 /* We use the data field for chaining the free list
1962 so that we won't use the same field that has the mark bit. */
1963 XSETFLOAT (val
, float_free_list
);
1964 float_free_list
= *(struct Lisp_Float
**)&float_free_list
->data
;
1968 if (float_block_index
== FLOAT_BLOCK_SIZE
)
1970 register struct float_block
*new;
1972 new = (struct float_block
*) lisp_malloc (sizeof *new,
1974 VALIDATE_LISP_STORAGE (new, sizeof *new);
1975 new->next
= float_block
;
1977 float_block_index
= 0;
1980 XSETFLOAT (val
, &float_block
->floats
[float_block_index
++]);
1983 XFLOAT_DATA (val
) = float_value
;
1984 XSETFASTINT (XFLOAT (val
)->type
, 0); /* bug chasing -wsr */
1985 consing_since_gc
+= sizeof (struct Lisp_Float
);
1992 /***********************************************************************
1994 ***********************************************************************/
1996 /* We store cons cells inside of cons_blocks, allocating a new
1997 cons_block with malloc whenever necessary. Cons cells reclaimed by
1998 GC are put on a free list to be reallocated before allocating
1999 any new cons cells from the latest cons_block.
2001 Each cons_block is just under 1020 bytes long,
2002 since malloc really allocates in units of powers of two
2003 and uses 4 bytes for its own overhead. */
2005 #define CONS_BLOCK_SIZE \
2006 ((1020 - sizeof (struct cons_block *)) / sizeof (struct Lisp_Cons))
2010 struct cons_block
*next
;
2011 struct Lisp_Cons conses
[CONS_BLOCK_SIZE
];
2014 /* Current cons_block. */
2016 struct cons_block
*cons_block
;
2018 /* Index of first unused Lisp_Cons in the current block. */
2020 int cons_block_index
;
2022 /* Free-list of Lisp_Cons structures. */
2024 struct Lisp_Cons
*cons_free_list
;
2026 /* Total number of cons blocks now in use. */
2031 /* Initialize cons allocation. */
2036 cons_block
= (struct cons_block
*) lisp_malloc (sizeof *cons_block
,
2038 cons_block
->next
= 0;
2039 bzero ((char *) cons_block
->conses
, sizeof cons_block
->conses
);
2040 cons_block_index
= 0;
2046 /* Explicitly free a cons cell by putting it on the free-list. */
2050 struct Lisp_Cons
*ptr
;
2052 *(struct Lisp_Cons
**)&ptr
->cdr
= cons_free_list
;
2056 cons_free_list
= ptr
;
2060 DEFUN ("cons", Fcons
, Scons
, 2, 2, 0,
2061 doc
: /* Create a new cons, give it CAR and CDR as components, and return it. */)
2063 Lisp_Object car
, cdr
;
2065 register Lisp_Object val
;
2069 /* We use the cdr for chaining the free list
2070 so that we won't use the same field that has the mark bit. */
2071 XSETCONS (val
, cons_free_list
);
2072 cons_free_list
= *(struct Lisp_Cons
**)&cons_free_list
->cdr
;
2076 if (cons_block_index
== CONS_BLOCK_SIZE
)
2078 register struct cons_block
*new;
2079 new = (struct cons_block
*) lisp_malloc (sizeof *new,
2081 VALIDATE_LISP_STORAGE (new, sizeof *new);
2082 new->next
= cons_block
;
2084 cons_block_index
= 0;
2087 XSETCONS (val
, &cons_block
->conses
[cons_block_index
++]);
2092 consing_since_gc
+= sizeof (struct Lisp_Cons
);
2093 cons_cells_consed
++;
2098 /* Make a list of 2, 3, 4 or 5 specified objects. */
2102 Lisp_Object arg1
, arg2
;
2104 return Fcons (arg1
, Fcons (arg2
, Qnil
));
2109 list3 (arg1
, arg2
, arg3
)
2110 Lisp_Object arg1
, arg2
, arg3
;
2112 return Fcons (arg1
, Fcons (arg2
, Fcons (arg3
, Qnil
)));
2117 list4 (arg1
, arg2
, arg3
, arg4
)
2118 Lisp_Object arg1
, arg2
, arg3
, arg4
;
2120 return Fcons (arg1
, Fcons (arg2
, Fcons (arg3
, Fcons (arg4
, Qnil
))));
2125 list5 (arg1
, arg2
, arg3
, arg4
, arg5
)
2126 Lisp_Object arg1
, arg2
, arg3
, arg4
, arg5
;
2128 return Fcons (arg1
, Fcons (arg2
, Fcons (arg3
, Fcons (arg4
,
2129 Fcons (arg5
, Qnil
)))));
2133 DEFUN ("list", Flist
, Slist
, 0, MANY
, 0,
2134 doc
: /* Return a newly created list with specified arguments as elements.
2135 Any number of arguments, even zero arguments, are allowed.
2136 usage: (list &rest OBJECTS) */)
2139 register Lisp_Object
*args
;
2141 register Lisp_Object val
;
2147 val
= Fcons (args
[nargs
], val
);
2153 DEFUN ("make-list", Fmake_list
, Smake_list
, 2, 2, 0,
2154 doc
: /* Return a newly created list of length LENGTH, with each element being INIT. */)
2156 register Lisp_Object length
, init
;
2158 register Lisp_Object val
;
2161 CHECK_NATNUM (length
);
2162 size
= XFASTINT (length
);
2167 val
= Fcons (init
, val
);
2172 val
= Fcons (init
, val
);
2177 val
= Fcons (init
, val
);
2182 val
= Fcons (init
, val
);
2187 val
= Fcons (init
, val
);
2202 /***********************************************************************
2204 ***********************************************************************/
2206 /* Singly-linked list of all vectors. */
2208 struct Lisp_Vector
*all_vectors
;
2210 /* Total number of vector-like objects now in use. */
2215 /* Value is a pointer to a newly allocated Lisp_Vector structure
2216 with room for LEN Lisp_Objects. */
2218 static struct Lisp_Vector
*
2219 allocate_vectorlike (len
, type
)
2223 struct Lisp_Vector
*p
;
2226 #ifdef DOUG_LEA_MALLOC
2227 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed
2228 because mapped region contents are not preserved in
2230 mallopt (M_MMAP_MAX
, 0);
2233 nbytes
= sizeof *p
+ (len
- 1) * sizeof p
->contents
[0];
2234 p
= (struct Lisp_Vector
*) lisp_malloc (nbytes
, type
);
2236 #ifdef DOUG_LEA_MALLOC
2237 /* Back to a reasonable maximum of mmap'ed areas. */
2238 mallopt (M_MMAP_MAX
, MMAP_MAX_AREAS
);
2241 VALIDATE_LISP_STORAGE (p
, 0);
2242 consing_since_gc
+= nbytes
;
2243 vector_cells_consed
+= len
;
2245 p
->next
= all_vectors
;
2252 /* Allocate a vector with NSLOTS slots. */
2254 struct Lisp_Vector
*
2255 allocate_vector (nslots
)
2258 struct Lisp_Vector
*v
= allocate_vectorlike (nslots
, MEM_TYPE_VECTOR
);
2264 /* Allocate other vector-like structures. */
2266 struct Lisp_Hash_Table
*
2267 allocate_hash_table ()
2269 EMACS_INT len
= VECSIZE (struct Lisp_Hash_Table
);
2270 struct Lisp_Vector
*v
= allocate_vectorlike (len
, MEM_TYPE_HASH_TABLE
);
2274 for (i
= 0; i
< len
; ++i
)
2275 v
->contents
[i
] = Qnil
;
2277 return (struct Lisp_Hash_Table
*) v
;
2284 EMACS_INT len
= VECSIZE (struct window
);
2285 struct Lisp_Vector
*v
= allocate_vectorlike (len
, MEM_TYPE_WINDOW
);
2288 for (i
= 0; i
< len
; ++i
)
2289 v
->contents
[i
] = Qnil
;
2292 return (struct window
*) v
;
2299 EMACS_INT len
= VECSIZE (struct frame
);
2300 struct Lisp_Vector
*v
= allocate_vectorlike (len
, MEM_TYPE_FRAME
);
2303 for (i
= 0; i
< len
; ++i
)
2304 v
->contents
[i
] = make_number (0);
2306 return (struct frame
*) v
;
2310 struct Lisp_Process
*
2313 EMACS_INT len
= VECSIZE (struct Lisp_Process
);
2314 struct Lisp_Vector
*v
= allocate_vectorlike (len
, MEM_TYPE_PROCESS
);
2317 for (i
= 0; i
< len
; ++i
)
2318 v
->contents
[i
] = Qnil
;
2321 return (struct Lisp_Process
*) v
;
2325 struct Lisp_Vector
*
2326 allocate_other_vector (len
)
2329 struct Lisp_Vector
*v
= allocate_vectorlike (len
, MEM_TYPE_VECTOR
);
2332 for (i
= 0; i
< len
; ++i
)
2333 v
->contents
[i
] = Qnil
;
2340 DEFUN ("make-vector", Fmake_vector
, Smake_vector
, 2, 2, 0,
2341 doc
: /* Return a newly created vector of length LENGTH, with each element being INIT.
2342 See also the function `vector'. */)
2344 register Lisp_Object length
, init
;
2347 register EMACS_INT sizei
;
2349 register struct Lisp_Vector
*p
;
2351 CHECK_NATNUM (length
);
2352 sizei
= XFASTINT (length
);
2354 p
= allocate_vector (sizei
);
2355 for (index
= 0; index
< sizei
; index
++)
2356 p
->contents
[index
] = init
;
2358 XSETVECTOR (vector
, p
);
2363 DEFUN ("make-char-table", Fmake_char_table
, Smake_char_table
, 1, 2, 0,
2364 doc
: /* Return a newly created char-table, with purpose PURPOSE.
2365 Each element is initialized to INIT, which defaults to nil.
2366 PURPOSE should be a symbol which has a `char-table-extra-slots' property.
2367 The property's value should be an integer between 0 and 10. */)
2369 register Lisp_Object purpose
, init
;
2373 CHECK_SYMBOL (purpose
);
2374 n
= Fget (purpose
, Qchar_table_extra_slots
);
2376 if (XINT (n
) < 0 || XINT (n
) > 10)
2377 args_out_of_range (n
, Qnil
);
2378 /* Add 2 to the size for the defalt and parent slots. */
2379 vector
= Fmake_vector (make_number (CHAR_TABLE_STANDARD_SLOTS
+ XINT (n
)),
2381 XCHAR_TABLE (vector
)->top
= Qt
;
2382 XCHAR_TABLE (vector
)->parent
= Qnil
;
2383 XCHAR_TABLE (vector
)->purpose
= purpose
;
2384 XSETCHAR_TABLE (vector
, XCHAR_TABLE (vector
));
2389 /* Return a newly created sub char table with default value DEFALT.
2390 Since a sub char table does not appear as a top level Emacs Lisp
2391 object, we don't need a Lisp interface to make it. */
2394 make_sub_char_table (defalt
)
2398 = Fmake_vector (make_number (SUB_CHAR_TABLE_STANDARD_SLOTS
), Qnil
);
2399 XCHAR_TABLE (vector
)->top
= Qnil
;
2400 XCHAR_TABLE (vector
)->defalt
= defalt
;
2401 XSETCHAR_TABLE (vector
, XCHAR_TABLE (vector
));
2406 DEFUN ("vector", Fvector
, Svector
, 0, MANY
, 0,
2407 doc
: /* Return a newly created vector with specified arguments as elements.
2408 Any number of arguments, even zero arguments, are allowed.
2409 usage: (vector &rest OBJECTS) */)
2414 register Lisp_Object len
, val
;
2416 register struct Lisp_Vector
*p
;
2418 XSETFASTINT (len
, nargs
);
2419 val
= Fmake_vector (len
, Qnil
);
2421 for (index
= 0; index
< nargs
; index
++)
2422 p
->contents
[index
] = args
[index
];
2427 DEFUN ("make-byte-code", Fmake_byte_code
, Smake_byte_code
, 4, MANY
, 0,
2428 doc
: /* Create a byte-code object with specified arguments as elements.
2429 The arguments should be the arglist, bytecode-string, constant vector,
2430 stack size, (optional) doc string, and (optional) interactive spec.
2431 The first four arguments are required; at most six have any
2433 usage: (make-byte-code &rest ELEMENTS) */)
2438 register Lisp_Object len
, val
;
2440 register struct Lisp_Vector
*p
;
2442 XSETFASTINT (len
, nargs
);
2443 if (!NILP (Vpurify_flag
))
2444 val
= make_pure_vector ((EMACS_INT
) nargs
);
2446 val
= Fmake_vector (len
, Qnil
);
2448 if (STRINGP (args
[1]) && STRING_MULTIBYTE (args
[1]))
2449 /* BYTECODE-STRING must have been produced by Emacs 20.2 or the
2450 earlier because they produced a raw 8-bit string for byte-code
2451 and now such a byte-code string is loaded as multibyte while
2452 raw 8-bit characters converted to multibyte form. Thus, now we
2453 must convert them back to the original unibyte form. */
2454 args
[1] = Fstring_as_unibyte (args
[1]);
2457 for (index
= 0; index
< nargs
; index
++)
2459 if (!NILP (Vpurify_flag
))
2460 args
[index
] = Fpurecopy (args
[index
]);
2461 p
->contents
[index
] = args
[index
];
2463 XSETCOMPILED (val
, p
);
2469 /***********************************************************************
2471 ***********************************************************************/
2473 /* Each symbol_block is just under 1020 bytes long, since malloc
2474 really allocates in units of powers of two and uses 4 bytes for its
2477 #define SYMBOL_BLOCK_SIZE \
2478 ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol))
2482 struct symbol_block
*next
;
2483 struct Lisp_Symbol symbols
[SYMBOL_BLOCK_SIZE
];
2486 /* Current symbol block and index of first unused Lisp_Symbol
2489 struct symbol_block
*symbol_block
;
2490 int symbol_block_index
;
2492 /* List of free symbols. */
2494 struct Lisp_Symbol
*symbol_free_list
;
2496 /* Total number of symbol blocks now in use. */
2498 int n_symbol_blocks
;
2501 /* Initialize symbol allocation. */
2506 symbol_block
= (struct symbol_block
*) lisp_malloc (sizeof *symbol_block
,
2508 symbol_block
->next
= 0;
2509 bzero ((char *) symbol_block
->symbols
, sizeof symbol_block
->symbols
);
2510 symbol_block_index
= 0;
2511 symbol_free_list
= 0;
2512 n_symbol_blocks
= 1;
2516 DEFUN ("make-symbol", Fmake_symbol
, Smake_symbol
, 1, 1, 0,
2517 doc
: /* Return a newly allocated uninterned symbol whose name is NAME.
2518 Its value and function definition are void, and its property list is nil. */)
2522 register Lisp_Object val
;
2523 register struct Lisp_Symbol
*p
;
2525 CHECK_STRING (name
);
2527 if (symbol_free_list
)
2529 XSETSYMBOL (val
, symbol_free_list
);
2530 symbol_free_list
= *(struct Lisp_Symbol
**)&symbol_free_list
->value
;
2534 if (symbol_block_index
== SYMBOL_BLOCK_SIZE
)
2536 struct symbol_block
*new;
2537 new = (struct symbol_block
*) lisp_malloc (sizeof *new,
2539 VALIDATE_LISP_STORAGE (new, sizeof *new);
2540 new->next
= symbol_block
;
2542 symbol_block_index
= 0;
2545 XSETSYMBOL (val
, &symbol_block
->symbols
[symbol_block_index
++]);
2551 p
->value
= Qunbound
;
2552 p
->function
= Qunbound
;
2554 p
->interned
= SYMBOL_UNINTERNED
;
2556 p
->indirect_variable
= 0;
2557 consing_since_gc
+= sizeof (struct Lisp_Symbol
);
2564 /***********************************************************************
2565 Marker (Misc) Allocation
2566 ***********************************************************************/
2568 /* Allocation of markers and other objects that share that structure.
2569 Works like allocation of conses. */
2571 #define MARKER_BLOCK_SIZE \
2572 ((1020 - sizeof (struct marker_block *)) / sizeof (union Lisp_Misc))
2576 struct marker_block
*next
;
2577 union Lisp_Misc markers
[MARKER_BLOCK_SIZE
];
2580 struct marker_block
*marker_block
;
2581 int marker_block_index
;
2583 union Lisp_Misc
*marker_free_list
;
2585 /* Total number of marker blocks now in use. */
2587 int n_marker_blocks
;
2592 marker_block
= (struct marker_block
*) lisp_malloc (sizeof *marker_block
,
2594 marker_block
->next
= 0;
2595 bzero ((char *) marker_block
->markers
, sizeof marker_block
->markers
);
2596 marker_block_index
= 0;
2597 marker_free_list
= 0;
2598 n_marker_blocks
= 1;
2601 /* Return a newly allocated Lisp_Misc object, with no substructure. */
2608 if (marker_free_list
)
2610 XSETMISC (val
, marker_free_list
);
2611 marker_free_list
= marker_free_list
->u_free
.chain
;
2615 if (marker_block_index
== MARKER_BLOCK_SIZE
)
2617 struct marker_block
*new;
2618 new = (struct marker_block
*) lisp_malloc (sizeof *new,
2620 VALIDATE_LISP_STORAGE (new, sizeof *new);
2621 new->next
= marker_block
;
2623 marker_block_index
= 0;
2626 XSETMISC (val
, &marker_block
->markers
[marker_block_index
++]);
2629 consing_since_gc
+= sizeof (union Lisp_Misc
);
2630 misc_objects_consed
++;
2634 /* Return a Lisp_Misc_Save_Value object containing POINTER and
2635 INTEGER. This is used to package C values to call record_unwind_protect.
2636 The unwind function can get the C values back using XSAVE_VALUE. */
2639 make_save_value (pointer
, integer
)
2643 register Lisp_Object val
;
2644 register struct Lisp_Save_Value
*p
;
2646 val
= allocate_misc ();
2647 XMISCTYPE (val
) = Lisp_Misc_Save_Value
;
2648 p
= XSAVE_VALUE (val
);
2649 p
->pointer
= pointer
;
2650 p
->integer
= integer
;
2654 DEFUN ("make-marker", Fmake_marker
, Smake_marker
, 0, 0, 0,
2655 doc
: /* Return a newly allocated marker which does not point at any place. */)
2658 register Lisp_Object val
;
2659 register struct Lisp_Marker
*p
;
2661 val
= allocate_misc ();
2662 XMISCTYPE (val
) = Lisp_Misc_Marker
;
2668 p
->insertion_type
= 0;
2672 /* Put MARKER back on the free list after using it temporarily. */
2675 free_marker (marker
)
2678 unchain_marker (marker
);
2680 XMISC (marker
)->u_marker
.type
= Lisp_Misc_Free
;
2681 XMISC (marker
)->u_free
.chain
= marker_free_list
;
2682 marker_free_list
= XMISC (marker
);
2684 total_free_markers
++;
2688 /* Return a newly created vector or string with specified arguments as
2689 elements. If all the arguments are characters that can fit
2690 in a string of events, make a string; otherwise, make a vector.
2692 Any number of arguments, even zero arguments, are allowed. */
2695 make_event_array (nargs
, args
)
2701 for (i
= 0; i
< nargs
; i
++)
2702 /* The things that fit in a string
2703 are characters that are in 0...127,
2704 after discarding the meta bit and all the bits above it. */
2705 if (!INTEGERP (args
[i
])
2706 || (XUINT (args
[i
]) & ~(-CHAR_META
)) >= 0200)
2707 return Fvector (nargs
, args
);
2709 /* Since the loop exited, we know that all the things in it are
2710 characters, so we can make a string. */
2714 result
= Fmake_string (make_number (nargs
), make_number (0));
2715 for (i
= 0; i
< nargs
; i
++)
2717 SSET (result
, i
, XINT (args
[i
]));
2718 /* Move the meta bit to the right place for a string char. */
2719 if (XINT (args
[i
]) & CHAR_META
)
2720 SSET (result
, i
, SREF (result
, i
) | 0x80);
2729 /************************************************************************
2731 ************************************************************************/
2733 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
2735 /* Conservative C stack marking requires a method to identify possibly
2736 live Lisp objects given a pointer value. We do this by keeping
2737 track of blocks of Lisp data that are allocated in a red-black tree
2738 (see also the comment of mem_node which is the type of nodes in
2739 that tree). Function lisp_malloc adds information for an allocated
2740 block to the red-black tree with calls to mem_insert, and function
2741 lisp_free removes it with mem_delete. Functions live_string_p etc
2742 call mem_find to lookup information about a given pointer in the
2743 tree, and use that to determine if the pointer points to a Lisp
2746 /* Initialize this part of alloc.c. */
2751 mem_z
.left
= mem_z
.right
= MEM_NIL
;
2752 mem_z
.parent
= NULL
;
2753 mem_z
.color
= MEM_BLACK
;
2754 mem_z
.start
= mem_z
.end
= NULL
;
2759 /* Value is a pointer to the mem_node containing START. Value is
2760 MEM_NIL if there is no node in the tree containing START. */
2762 static INLINE
struct mem_node
*
2768 if (start
< min_heap_address
|| start
> max_heap_address
)
2771 /* Make the search always successful to speed up the loop below. */
2772 mem_z
.start
= start
;
2773 mem_z
.end
= (char *) start
+ 1;
2776 while (start
< p
->start
|| start
>= p
->end
)
2777 p
= start
< p
->start
? p
->left
: p
->right
;
2782 /* Insert a new node into the tree for a block of memory with start
2783 address START, end address END, and type TYPE. Value is a
2784 pointer to the node that was inserted. */
2786 static struct mem_node
*
2787 mem_insert (start
, end
, type
)
2791 struct mem_node
*c
, *parent
, *x
;
2793 if (start
< min_heap_address
)
2794 min_heap_address
= start
;
2795 if (end
> max_heap_address
)
2796 max_heap_address
= end
;
2798 /* See where in the tree a node for START belongs. In this
2799 particular application, it shouldn't happen that a node is already
2800 present. For debugging purposes, let's check that. */
2804 #if GC_MARK_STACK != GC_MAKE_GCPROS_NOOPS
2806 while (c
!= MEM_NIL
)
2808 if (start
>= c
->start
&& start
< c
->end
)
2811 c
= start
< c
->start
? c
->left
: c
->right
;
2814 #else /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
2816 while (c
!= MEM_NIL
)
2819 c
= start
< c
->start
? c
->left
: c
->right
;
2822 #endif /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
2824 /* Create a new node. */
2825 #ifdef GC_MALLOC_CHECK
2826 x
= (struct mem_node
*) _malloc_internal (sizeof *x
);
2830 x
= (struct mem_node
*) xmalloc (sizeof *x
);
2836 x
->left
= x
->right
= MEM_NIL
;
2839 /* Insert it as child of PARENT or install it as root. */
2842 if (start
< parent
->start
)
2850 /* Re-establish red-black tree properties. */
2851 mem_insert_fixup (x
);
2857 /* Re-establish the red-black properties of the tree, and thereby
2858 balance the tree, after node X has been inserted; X is always red. */
2861 mem_insert_fixup (x
)
2864 while (x
!= mem_root
&& x
->parent
->color
== MEM_RED
)
2866 /* X is red and its parent is red. This is a violation of
2867 red-black tree property #3. */
2869 if (x
->parent
== x
->parent
->parent
->left
)
2871 /* We're on the left side of our grandparent, and Y is our
2873 struct mem_node
*y
= x
->parent
->parent
->right
;
2875 if (y
->color
== MEM_RED
)
2877 /* Uncle and parent are red but should be black because
2878 X is red. Change the colors accordingly and proceed
2879 with the grandparent. */
2880 x
->parent
->color
= MEM_BLACK
;
2881 y
->color
= MEM_BLACK
;
2882 x
->parent
->parent
->color
= MEM_RED
;
2883 x
= x
->parent
->parent
;
2887 /* Parent and uncle have different colors; parent is
2888 red, uncle is black. */
2889 if (x
== x
->parent
->right
)
2892 mem_rotate_left (x
);
2895 x
->parent
->color
= MEM_BLACK
;
2896 x
->parent
->parent
->color
= MEM_RED
;
2897 mem_rotate_right (x
->parent
->parent
);
2902 /* This is the symmetrical case of above. */
2903 struct mem_node
*y
= x
->parent
->parent
->left
;
2905 if (y
->color
== MEM_RED
)
2907 x
->parent
->color
= MEM_BLACK
;
2908 y
->color
= MEM_BLACK
;
2909 x
->parent
->parent
->color
= MEM_RED
;
2910 x
= x
->parent
->parent
;
2914 if (x
== x
->parent
->left
)
2917 mem_rotate_right (x
);
2920 x
->parent
->color
= MEM_BLACK
;
2921 x
->parent
->parent
->color
= MEM_RED
;
2922 mem_rotate_left (x
->parent
->parent
);
2927 /* The root may have been changed to red due to the algorithm. Set
2928 it to black so that property #5 is satisfied. */
2929 mem_root
->color
= MEM_BLACK
;
2945 /* Turn y's left sub-tree into x's right sub-tree. */
2948 if (y
->left
!= MEM_NIL
)
2949 y
->left
->parent
= x
;
2951 /* Y's parent was x's parent. */
2953 y
->parent
= x
->parent
;
2955 /* Get the parent to point to y instead of x. */
2958 if (x
== x
->parent
->left
)
2959 x
->parent
->left
= y
;
2961 x
->parent
->right
= y
;
2966 /* Put x on y's left. */
2980 mem_rotate_right (x
)
2983 struct mem_node
*y
= x
->left
;
2986 if (y
->right
!= MEM_NIL
)
2987 y
->right
->parent
= x
;
2990 y
->parent
= x
->parent
;
2993 if (x
== x
->parent
->right
)
2994 x
->parent
->right
= y
;
2996 x
->parent
->left
= y
;
3007 /* Delete node Z from the tree. If Z is null or MEM_NIL, do nothing. */
3013 struct mem_node
*x
, *y
;
3015 if (!z
|| z
== MEM_NIL
)
3018 if (z
->left
== MEM_NIL
|| z
->right
== MEM_NIL
)
3023 while (y
->left
!= MEM_NIL
)
3027 if (y
->left
!= MEM_NIL
)
3032 x
->parent
= y
->parent
;
3035 if (y
== y
->parent
->left
)
3036 y
->parent
->left
= x
;
3038 y
->parent
->right
= x
;
3045 z
->start
= y
->start
;
3050 if (y
->color
== MEM_BLACK
)
3051 mem_delete_fixup (x
);
3053 #ifdef GC_MALLOC_CHECK
3061 /* Re-establish the red-black properties of the tree, after a
3065 mem_delete_fixup (x
)
3068 while (x
!= mem_root
&& x
->color
== MEM_BLACK
)
3070 if (x
== x
->parent
->left
)
3072 struct mem_node
*w
= x
->parent
->right
;
3074 if (w
->color
== MEM_RED
)
3076 w
->color
= MEM_BLACK
;
3077 x
->parent
->color
= MEM_RED
;
3078 mem_rotate_left (x
->parent
);
3079 w
= x
->parent
->right
;
3082 if (w
->left
->color
== MEM_BLACK
&& w
->right
->color
== MEM_BLACK
)
3089 if (w
->right
->color
== MEM_BLACK
)
3091 w
->left
->color
= MEM_BLACK
;
3093 mem_rotate_right (w
);
3094 w
= x
->parent
->right
;
3096 w
->color
= x
->parent
->color
;
3097 x
->parent
->color
= MEM_BLACK
;
3098 w
->right
->color
= MEM_BLACK
;
3099 mem_rotate_left (x
->parent
);
3105 struct mem_node
*w
= x
->parent
->left
;
3107 if (w
->color
== MEM_RED
)
3109 w
->color
= MEM_BLACK
;
3110 x
->parent
->color
= MEM_RED
;
3111 mem_rotate_right (x
->parent
);
3112 w
= x
->parent
->left
;
3115 if (w
->right
->color
== MEM_BLACK
&& w
->left
->color
== MEM_BLACK
)
3122 if (w
->left
->color
== MEM_BLACK
)
3124 w
->right
->color
= MEM_BLACK
;
3126 mem_rotate_left (w
);
3127 w
= x
->parent
->left
;
3130 w
->color
= x
->parent
->color
;
3131 x
->parent
->color
= MEM_BLACK
;
3132 w
->left
->color
= MEM_BLACK
;
3133 mem_rotate_right (x
->parent
);
3139 x
->color
= MEM_BLACK
;
3143 /* Value is non-zero if P is a pointer to a live Lisp string on
3144 the heap. M is a pointer to the mem_block for P. */
3147 live_string_p (m
, p
)
3151 if (m
->type
== MEM_TYPE_STRING
)
3153 struct string_block
*b
= (struct string_block
*) m
->start
;
3154 int offset
= (char *) p
- (char *) &b
->strings
[0];
3156 /* P must point to the start of a Lisp_String structure, and it
3157 must not be on the free-list. */
3159 && offset
% sizeof b
->strings
[0] == 0
3160 && ((struct Lisp_String
*) p
)->data
!= NULL
);
3167 /* Value is non-zero if P is a pointer to a live Lisp cons on
3168 the heap. M is a pointer to the mem_block for P. */
3175 if (m
->type
== MEM_TYPE_CONS
)
3177 struct cons_block
*b
= (struct cons_block
*) m
->start
;
3178 int offset
= (char *) p
- (char *) &b
->conses
[0];
3180 /* P must point to the start of a Lisp_Cons, not be
3181 one of the unused cells in the current cons block,
3182 and not be on the free-list. */
3184 && offset
% sizeof b
->conses
[0] == 0
3186 || offset
/ sizeof b
->conses
[0] < cons_block_index
)
3187 && !EQ (((struct Lisp_Cons
*) p
)->car
, Vdead
));
3194 /* Value is non-zero if P is a pointer to a live Lisp symbol on
3195 the heap. M is a pointer to the mem_block for P. */
3198 live_symbol_p (m
, p
)
3202 if (m
->type
== MEM_TYPE_SYMBOL
)
3204 struct symbol_block
*b
= (struct symbol_block
*) m
->start
;
3205 int offset
= (char *) p
- (char *) &b
->symbols
[0];
3207 /* P must point to the start of a Lisp_Symbol, not be
3208 one of the unused cells in the current symbol block,
3209 and not be on the free-list. */
3211 && offset
% sizeof b
->symbols
[0] == 0
3212 && (b
!= symbol_block
3213 || offset
/ sizeof b
->symbols
[0] < symbol_block_index
)
3214 && !EQ (((struct Lisp_Symbol
*) p
)->function
, Vdead
));
3221 /* Value is non-zero if P is a pointer to a live Lisp float on
3222 the heap. M is a pointer to the mem_block for P. */
3229 if (m
->type
== MEM_TYPE_FLOAT
)
3231 struct float_block
*b
= (struct float_block
*) m
->start
;
3232 int offset
= (char *) p
- (char *) &b
->floats
[0];
3234 /* P must point to the start of a Lisp_Float, not be
3235 one of the unused cells in the current float block,
3236 and not be on the free-list. */
3238 && offset
% sizeof b
->floats
[0] == 0
3239 && (b
!= float_block
3240 || offset
/ sizeof b
->floats
[0] < float_block_index
)
3241 && !EQ (((struct Lisp_Float
*) p
)->type
, Vdead
));
3248 /* Value is non-zero if P is a pointer to a live Lisp Misc on
3249 the heap. M is a pointer to the mem_block for P. */
3256 if (m
->type
== MEM_TYPE_MISC
)
3258 struct marker_block
*b
= (struct marker_block
*) m
->start
;
3259 int offset
= (char *) p
- (char *) &b
->markers
[0];
3261 /* P must point to the start of a Lisp_Misc, not be
3262 one of the unused cells in the current misc block,
3263 and not be on the free-list. */
3265 && offset
% sizeof b
->markers
[0] == 0
3266 && (b
!= marker_block
3267 || offset
/ sizeof b
->markers
[0] < marker_block_index
)
3268 && ((union Lisp_Misc
*) p
)->u_marker
.type
!= Lisp_Misc_Free
);
3275 /* Value is non-zero if P is a pointer to a live vector-like object.
3276 M is a pointer to the mem_block for P. */
3279 live_vector_p (m
, p
)
3283 return (p
== m
->start
3284 && m
->type
>= MEM_TYPE_VECTOR
3285 && m
->type
<= MEM_TYPE_WINDOW
);
3289 /* Value is non-zero of P is a pointer to a live buffer. M is a
3290 pointer to the mem_block for P. */
3293 live_buffer_p (m
, p
)
3297 /* P must point to the start of the block, and the buffer
3298 must not have been killed. */
3299 return (m
->type
== MEM_TYPE_BUFFER
3301 && !NILP (((struct buffer
*) p
)->name
));
3304 #endif /* GC_MARK_STACK || defined GC_MALLOC_CHECK */
3308 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3310 /* Array of objects that are kept alive because the C stack contains
3311 a pattern that looks like a reference to them . */
3313 #define MAX_ZOMBIES 10
3314 static Lisp_Object zombies
[MAX_ZOMBIES
];
3316 /* Number of zombie objects. */
3318 static int nzombies
;
3320 /* Number of garbage collections. */
3324 /* Average percentage of zombies per collection. */
3326 static double avg_zombies
;
3328 /* Max. number of live and zombie objects. */
3330 static int max_live
, max_zombies
;
3332 /* Average number of live objects per GC. */
3334 static double avg_live
;
3336 DEFUN ("gc-status", Fgc_status
, Sgc_status
, 0, 0, "",
3337 doc
: /* Show information about live and zombie objects. */)
3340 Lisp_Object args
[7];
3341 args
[0] = build_string ("%d GCs, avg live/zombies = %.2f/%.2f (%f%%), max %d/%d");
3342 args
[1] = make_number (ngcs
);
3343 args
[2] = make_float (avg_live
);
3344 args
[3] = make_float (avg_zombies
);
3345 args
[4] = make_float (avg_zombies
/ avg_live
/ 100);
3346 args
[5] = make_number (max_live
);
3347 args
[6] = make_number (max_zombies
);
3348 return Fmessage (7, args
);
3351 #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
3354 /* Mark OBJ if we can prove it's a Lisp_Object. */
3357 mark_maybe_object (obj
)
3360 void *po
= (void *) XPNTR (obj
);
3361 struct mem_node
*m
= mem_find (po
);
3367 switch (XGCTYPE (obj
))
3370 mark_p
= (live_string_p (m
, po
)
3371 && !STRING_MARKED_P ((struct Lisp_String
*) po
));
3375 mark_p
= (live_cons_p (m
, po
)
3376 && !XMARKBIT (XCONS (obj
)->car
));
3380 mark_p
= (live_symbol_p (m
, po
)
3381 && !XMARKBIT (XSYMBOL (obj
)->plist
));
3385 mark_p
= (live_float_p (m
, po
)
3386 && !XMARKBIT (XFLOAT (obj
)->type
));
3389 case Lisp_Vectorlike
:
3390 /* Note: can't check GC_BUFFERP before we know it's a
3391 buffer because checking that dereferences the pointer
3392 PO which might point anywhere. */
3393 if (live_vector_p (m
, po
))
3394 mark_p
= (!GC_SUBRP (obj
)
3395 && !(XVECTOR (obj
)->size
& ARRAY_MARK_FLAG
));
3396 else if (live_buffer_p (m
, po
))
3397 mark_p
= GC_BUFFERP (obj
) && !XMARKBIT (XBUFFER (obj
)->name
);
3401 if (live_misc_p (m
, po
))
3403 switch (XMISCTYPE (obj
))
3405 case Lisp_Misc_Marker
:
3406 mark_p
= !XMARKBIT (XMARKER (obj
)->chain
);
3409 case Lisp_Misc_Buffer_Local_Value
:
3410 case Lisp_Misc_Some_Buffer_Local_Value
:
3411 mark_p
= !XMARKBIT (XBUFFER_LOCAL_VALUE (obj
)->realvalue
);
3414 case Lisp_Misc_Overlay
:
3415 mark_p
= !XMARKBIT (XOVERLAY (obj
)->plist
);
3422 case Lisp_Type_Limit
:
3428 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3429 if (nzombies
< MAX_ZOMBIES
)
3430 zombies
[nzombies
] = *p
;
3439 /* If P points to Lisp data, mark that as live if it isn't already
3443 mark_maybe_pointer (p
)
3448 /* Quickly rule out some values which can't point to Lisp data. We
3449 assume that Lisp data is aligned on even addresses. */
3450 if ((EMACS_INT
) p
& 1)
3456 Lisp_Object obj
= Qnil
;
3460 case MEM_TYPE_NON_LISP
:
3461 /* Nothing to do; not a pointer to Lisp memory. */
3464 case MEM_TYPE_BUFFER
:
3465 if (live_buffer_p (m
, p
)
3466 && !XMARKBIT (((struct buffer
*) p
)->name
))
3467 XSETVECTOR (obj
, p
);
3471 if (live_cons_p (m
, p
)
3472 && !XMARKBIT (((struct Lisp_Cons
*) p
)->car
))
3476 case MEM_TYPE_STRING
:
3477 if (live_string_p (m
, p
)
3478 && !STRING_MARKED_P ((struct Lisp_String
*) p
))
3479 XSETSTRING (obj
, p
);
3483 if (live_misc_p (m
, p
))
3488 switch (XMISCTYPE (tem
))
3490 case Lisp_Misc_Marker
:
3491 if (!XMARKBIT (XMARKER (tem
)->chain
))
3495 case Lisp_Misc_Buffer_Local_Value
:
3496 case Lisp_Misc_Some_Buffer_Local_Value
:
3497 if (!XMARKBIT (XBUFFER_LOCAL_VALUE (tem
)->realvalue
))
3501 case Lisp_Misc_Overlay
:
3502 if (!XMARKBIT (XOVERLAY (tem
)->plist
))
3509 case MEM_TYPE_SYMBOL
:
3510 if (live_symbol_p (m
, p
)
3511 && !XMARKBIT (((struct Lisp_Symbol
*) p
)->plist
))
3512 XSETSYMBOL (obj
, p
);
3515 case MEM_TYPE_FLOAT
:
3516 if (live_float_p (m
, p
)
3517 && !XMARKBIT (((struct Lisp_Float
*) p
)->type
))
3521 case MEM_TYPE_VECTOR
:
3522 case MEM_TYPE_PROCESS
:
3523 case MEM_TYPE_HASH_TABLE
:
3524 case MEM_TYPE_FRAME
:
3525 case MEM_TYPE_WINDOW
:
3526 if (live_vector_p (m
, p
))
3529 XSETVECTOR (tem
, p
);
3531 && !(XVECTOR (tem
)->size
& ARRAY_MARK_FLAG
))
3546 /* Mark Lisp objects referenced from the address range START..END. */
3549 mark_memory (start
, end
)
3555 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3559 /* Make START the pointer to the start of the memory region,
3560 if it isn't already. */
3568 /* Mark Lisp_Objects. */
3569 for (p
= (Lisp_Object
*) start
; (void *) p
< end
; ++p
)
3570 mark_maybe_object (*p
);
3572 /* Mark Lisp data pointed to. This is necessary because, in some
3573 situations, the C compiler optimizes Lisp objects away, so that
3574 only a pointer to them remains. Example:
3576 DEFUN ("testme", Ftestme, Stestme, 0, 0, 0, "")
3579 Lisp_Object obj = build_string ("test");
3580 struct Lisp_String *s = XSTRING (obj);
3581 Fgarbage_collect ();
3582 fprintf (stderr, "test `%s'\n", s->data);
3586 Here, `obj' isn't really used, and the compiler optimizes it
3587 away. The only reference to the life string is through the
3590 for (pp
= (void **) start
; (void *) pp
< end
; ++pp
)
3591 mark_maybe_pointer (*pp
);
3594 /* setjmp will work with GCC unless NON_SAVING_SETJMP is defined in
3595 the GCC system configuration. In gcc 3.2, the only systems for
3596 which this is so are i386-sco5 non-ELF, i386-sysv3 (maybe included
3597 by others?) and ns32k-pc532-min. */
3599 #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
3601 static int setjmp_tested_p
, longjmps_done
;
3603 #define SETJMP_WILL_LIKELY_WORK "\
3605 Emacs garbage collector has been changed to use conservative stack\n\
3606 marking. Emacs has determined that the method it uses to do the\n\
3607 marking will likely work on your system, but this isn't sure.\n\
3609 If you are a system-programmer, or can get the help of a local wizard\n\
3610 who is, please take a look at the function mark_stack in alloc.c, and\n\
3611 verify that the methods used are appropriate for your system.\n\
3613 Please mail the result to <emacs-devel@gnu.org>.\n\
3616 #define SETJMP_WILL_NOT_WORK "\
3618 Emacs garbage collector has been changed to use conservative stack\n\
3619 marking. Emacs has determined that the default method it uses to do the\n\
3620 marking will not work on your system. We will need a system-dependent\n\
3621 solution for your system.\n\
3623 Please take a look at the function mark_stack in alloc.c, and\n\
3624 try to find a way to make it work on your system.\n\
3626 Note that you may get false negatives, depending on the compiler.\n\
3627 In particular, you need to use -O with GCC for this test.\n\
3629 Please mail the result to <emacs-devel@gnu.org>.\n\
3633 /* Perform a quick check if it looks like setjmp saves registers in a
3634 jmp_buf. Print a message to stderr saying so. When this test
3635 succeeds, this is _not_ a proof that setjmp is sufficient for
3636 conservative stack marking. Only the sources or a disassembly
3647 /* Arrange for X to be put in a register. */
3653 if (longjmps_done
== 1)
3655 /* Came here after the longjmp at the end of the function.
3657 If x == 1, the longjmp has restored the register to its
3658 value before the setjmp, and we can hope that setjmp
3659 saves all such registers in the jmp_buf, although that
3662 For other values of X, either something really strange is
3663 taking place, or the setjmp just didn't save the register. */
3666 fprintf (stderr
, SETJMP_WILL_LIKELY_WORK
);
3669 fprintf (stderr
, SETJMP_WILL_NOT_WORK
);
3676 if (longjmps_done
== 1)
3680 #endif /* not GC_SAVE_REGISTERS_ON_STACK && not GC_SETJMP_WORKS */
3683 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
3685 /* Abort if anything GCPRO'd doesn't survive the GC. */
3693 for (p
= gcprolist
; p
; p
= p
->next
)
3694 for (i
= 0; i
< p
->nvars
; ++i
)
3695 if (!survives_gc_p (p
->var
[i
]))
3699 #elif GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3706 fprintf (stderr
, "\nZombies kept alive = %d:\n", nzombies
);
3707 for (i
= 0; i
< min (MAX_ZOMBIES
, nzombies
); ++i
)
3709 fprintf (stderr
, " %d = ", i
);
3710 debug_print (zombies
[i
]);
3714 #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
3717 /* Mark live Lisp objects on the C stack.
3719 There are several system-dependent problems to consider when
3720 porting this to new architectures:
3724 We have to mark Lisp objects in CPU registers that can hold local
3725 variables or are used to pass parameters.
3727 If GC_SAVE_REGISTERS_ON_STACK is defined, it should expand to
3728 something that either saves relevant registers on the stack, or
3729 calls mark_maybe_object passing it each register's contents.
3731 If GC_SAVE_REGISTERS_ON_STACK is not defined, the current
3732 implementation assumes that calling setjmp saves registers we need
3733 to see in a jmp_buf which itself lies on the stack. This doesn't
3734 have to be true! It must be verified for each system, possibly
3735 by taking a look at the source code of setjmp.
3739 Architectures differ in the way their processor stack is organized.
3740 For example, the stack might look like this
3743 | Lisp_Object | size = 4
3745 | something else | size = 2
3747 | Lisp_Object | size = 4
3751 In such a case, not every Lisp_Object will be aligned equally. To
3752 find all Lisp_Object on the stack it won't be sufficient to walk
3753 the stack in steps of 4 bytes. Instead, two passes will be
3754 necessary, one starting at the start of the stack, and a second
3755 pass starting at the start of the stack + 2. Likewise, if the
3756 minimal alignment of Lisp_Objects on the stack is 1, four passes
3757 would be necessary, each one starting with one byte more offset
3758 from the stack start.
3760 The current code assumes by default that Lisp_Objects are aligned
3761 equally on the stack. */
3768 volatile int stack_grows_down_p
= (char *) &j
> (char *) stack_base
;
3771 /* This trick flushes the register windows so that all the state of
3772 the process is contained in the stack. */
3777 /* Save registers that we need to see on the stack. We need to see
3778 registers used to hold register variables and registers used to
3780 #ifdef GC_SAVE_REGISTERS_ON_STACK
3781 GC_SAVE_REGISTERS_ON_STACK (end
);
3782 #else /* not GC_SAVE_REGISTERS_ON_STACK */
3784 #ifndef GC_SETJMP_WORKS /* If it hasn't been checked yet that
3785 setjmp will definitely work, test it
3786 and print a message with the result
3788 if (!setjmp_tested_p
)
3790 setjmp_tested_p
= 1;
3793 #endif /* GC_SETJMP_WORKS */
3796 end
= stack_grows_down_p
? (char *) &j
+ sizeof j
: (char *) &j
;
3797 #endif /* not GC_SAVE_REGISTERS_ON_STACK */
3799 /* This assumes that the stack is a contiguous region in memory. If
3800 that's not the case, something has to be done here to iterate
3801 over the stack segments. */
3802 #ifndef GC_LISP_OBJECT_ALIGNMENT
3803 #define GC_LISP_OBJECT_ALIGNMENT sizeof (Lisp_Object)
3805 for (i
= 0; i
< sizeof (Lisp_Object
); i
+= GC_LISP_OBJECT_ALIGNMENT
)
3806 mark_memory ((char *) stack_base
+ i
, end
);
3808 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
3814 #endif /* GC_MARK_STACK != 0 */
3818 /***********************************************************************
3819 Pure Storage Management
3820 ***********************************************************************/
3822 /* Allocate room for SIZE bytes from pure Lisp storage and return a
3823 pointer to it. TYPE is the Lisp type for which the memory is
3824 allocated. TYPE < 0 means it's not used for a Lisp object.
3826 If store_pure_type_info is set and TYPE is >= 0, the type of
3827 the allocated object is recorded in pure_types. */
3829 static POINTER_TYPE
*
3830 pure_alloc (size
, type
)
3834 POINTER_TYPE
*result
;
3835 size_t alignment
= sizeof (EMACS_INT
);
3837 /* Give Lisp_Floats an extra alignment. */
3838 if (type
== Lisp_Float
)
3840 #if defined __GNUC__ && __GNUC__ >= 2
3841 alignment
= __alignof (struct Lisp_Float
);
3843 alignment
= sizeof (struct Lisp_Float
);
3848 result
= (POINTER_TYPE
*) ALIGN ((EMACS_UINT
)purebeg
+ pure_bytes_used
, alignment
);
3849 pure_bytes_used
= ((char *)result
- (char *)purebeg
) + size
;
3851 if (pure_bytes_used
<= pure_size
)
3854 /* Don't allocate a large amount here,
3855 because it might get mmap'd and then its address
3856 might not be usable. */
3857 purebeg
= (char *) xmalloc (10000);
3859 pure_bytes_used_before_overflow
+= pure_bytes_used
- size
;
3860 pure_bytes_used
= 0;
3865 /* Print a warning if PURESIZE is too small. */
3870 if (pure_bytes_used_before_overflow
)
3871 message ("Pure Lisp storage overflow (approx. %d bytes needed)",
3872 (int) (pure_bytes_used
+ pure_bytes_used_before_overflow
));
3876 /* Return a string allocated in pure space. DATA is a buffer holding
3877 NCHARS characters, and NBYTES bytes of string data. MULTIBYTE
3878 non-zero means make the result string multibyte.
3880 Must get an error if pure storage is full, since if it cannot hold
3881 a large string it may be able to hold conses that point to that
3882 string; then the string is not protected from gc. */
3885 make_pure_string (data
, nchars
, nbytes
, multibyte
)
3891 struct Lisp_String
*s
;
3893 s
= (struct Lisp_String
*) pure_alloc (sizeof *s
, Lisp_String
);
3894 s
->data
= (unsigned char *) pure_alloc (nbytes
+ 1, -1);
3896 s
->size_byte
= multibyte
? nbytes
: -1;
3897 bcopy (data
, s
->data
, nbytes
);
3898 s
->data
[nbytes
] = '\0';
3899 s
->intervals
= NULL_INTERVAL
;
3900 XSETSTRING (string
, s
);
3905 /* Return a cons allocated from pure space. Give it pure copies
3906 of CAR as car and CDR as cdr. */
3909 pure_cons (car
, cdr
)
3910 Lisp_Object car
, cdr
;
3912 register Lisp_Object
new;
3913 struct Lisp_Cons
*p
;
3915 p
= (struct Lisp_Cons
*) pure_alloc (sizeof *p
, Lisp_Cons
);
3917 XSETCAR (new, Fpurecopy (car
));
3918 XSETCDR (new, Fpurecopy (cdr
));
3923 /* Value is a float object with value NUM allocated from pure space. */
3926 make_pure_float (num
)
3929 register Lisp_Object
new;
3930 struct Lisp_Float
*p
;
3932 p
= (struct Lisp_Float
*) pure_alloc (sizeof *p
, Lisp_Float
);
3934 XFLOAT_DATA (new) = num
;
3939 /* Return a vector with room for LEN Lisp_Objects allocated from
3943 make_pure_vector (len
)
3947 struct Lisp_Vector
*p
;
3948 size_t size
= sizeof *p
+ (len
- 1) * sizeof (Lisp_Object
);
3950 p
= (struct Lisp_Vector
*) pure_alloc (size
, Lisp_Vectorlike
);
3951 XSETVECTOR (new, p
);
3952 XVECTOR (new)->size
= len
;
3957 DEFUN ("purecopy", Fpurecopy
, Spurecopy
, 1, 1, 0,
3958 doc
: /* Make a copy of OBJECT in pure storage.
3959 Recursively copies contents of vectors and cons cells.
3960 Does not copy symbols. Copies strings without text properties. */)
3962 register Lisp_Object obj
;
3964 if (NILP (Vpurify_flag
))
3967 if (PURE_POINTER_P (XPNTR (obj
)))
3971 return pure_cons (XCAR (obj
), XCDR (obj
));
3972 else if (FLOATP (obj
))
3973 return make_pure_float (XFLOAT_DATA (obj
));
3974 else if (STRINGP (obj
))
3975 return make_pure_string (SDATA (obj
), SCHARS (obj
),
3977 STRING_MULTIBYTE (obj
));
3978 else if (COMPILEDP (obj
) || VECTORP (obj
))
3980 register struct Lisp_Vector
*vec
;
3981 register int i
, size
;
3983 size
= XVECTOR (obj
)->size
;
3984 if (size
& PSEUDOVECTOR_FLAG
)
3985 size
&= PSEUDOVECTOR_SIZE_MASK
;
3986 vec
= XVECTOR (make_pure_vector ((EMACS_INT
) size
));
3987 for (i
= 0; i
< size
; i
++)
3988 vec
->contents
[i
] = Fpurecopy (XVECTOR (obj
)->contents
[i
]);
3989 if (COMPILEDP (obj
))
3990 XSETCOMPILED (obj
, vec
);
3992 XSETVECTOR (obj
, vec
);
3995 else if (MARKERP (obj
))
3996 error ("Attempt to copy a marker to pure storage");
4003 /***********************************************************************
4005 ***********************************************************************/
4007 /* Put an entry in staticvec, pointing at the variable with address
4011 staticpro (varaddress
)
4012 Lisp_Object
*varaddress
;
4014 staticvec
[staticidx
++] = varaddress
;
4015 if (staticidx
>= NSTATICS
)
4023 struct catchtag
*next
;
4028 struct backtrace
*next
;
4029 Lisp_Object
*function
;
4030 Lisp_Object
*args
; /* Points to vector of args. */
4031 int nargs
; /* Length of vector. */
4032 /* If nargs is UNEVALLED, args points to slot holding list of
4039 /***********************************************************************
4041 ***********************************************************************/
4043 /* Temporarily prevent garbage collection. */
4046 inhibit_garbage_collection ()
4048 int count
= SPECPDL_INDEX ();
4049 int nbits
= min (VALBITS
, BITS_PER_INT
);
4051 specbind (Qgc_cons_threshold
, make_number (((EMACS_INT
) 1 << (nbits
- 1)) - 1));
4056 DEFUN ("garbage-collect", Fgarbage_collect
, Sgarbage_collect
, 0, 0, "",
4057 doc
: /* Reclaim storage for Lisp objects no longer needed.
4058 Returns info on amount of space in use:
4059 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)
4060 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS
4061 (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS)
4062 (USED-STRINGS . FREE-STRINGS))
4063 Garbage collection happens automatically if you cons more than
4064 `gc-cons-threshold' bytes of Lisp data since previous garbage collection. */)
4067 register struct gcpro
*tail
;
4068 register struct specbinding
*bind
;
4069 struct catchtag
*catch;
4070 struct handler
*handler
;
4071 register struct backtrace
*backlist
;
4072 char stack_top_variable
;
4075 Lisp_Object total
[8];
4076 int count
= SPECPDL_INDEX ();
4078 /* Can't GC if pure storage overflowed because we can't determine
4079 if something is a pure object or not. */
4080 if (pure_bytes_used_before_overflow
)
4083 /* In case user calls debug_print during GC,
4084 don't let that cause a recursive GC. */
4085 consing_since_gc
= 0;
4087 /* Save what's currently displayed in the echo area. */
4088 message_p
= push_message ();
4089 record_unwind_protect (pop_message_unwind
, Qnil
);
4091 /* Save a copy of the contents of the stack, for debugging. */
4092 #if MAX_SAVE_STACK > 0
4093 if (NILP (Vpurify_flag
))
4095 i
= &stack_top_variable
- stack_bottom
;
4097 if (i
< MAX_SAVE_STACK
)
4099 if (stack_copy
== 0)
4100 stack_copy
= (char *) xmalloc (stack_copy_size
= i
);
4101 else if (stack_copy_size
< i
)
4102 stack_copy
= (char *) xrealloc (stack_copy
, (stack_copy_size
= i
));
4105 if ((EMACS_INT
) (&stack_top_variable
- stack_bottom
) > 0)
4106 bcopy (stack_bottom
, stack_copy
, i
);
4108 bcopy (&stack_top_variable
, stack_copy
, i
);
4112 #endif /* MAX_SAVE_STACK > 0 */
4114 if (garbage_collection_messages
)
4115 message1_nolog ("Garbage collecting...");
4119 shrink_regexp_cache ();
4121 /* Don't keep undo information around forever. */
4123 register struct buffer
*nextb
= all_buffers
;
4127 /* If a buffer's undo list is Qt, that means that undo is
4128 turned off in that buffer. Calling truncate_undo_list on
4129 Qt tends to return NULL, which effectively turns undo back on.
4130 So don't call truncate_undo_list if undo_list is Qt. */
4131 if (! EQ (nextb
->undo_list
, Qt
))
4133 = truncate_undo_list (nextb
->undo_list
, undo_limit
,
4136 /* Shrink buffer gaps, but skip indirect and dead buffers. */
4137 if (nextb
->base_buffer
== 0 && !NILP (nextb
->name
))
4139 /* If a buffer's gap size is more than 10% of the buffer
4140 size, or larger than 2000 bytes, then shrink it
4141 accordingly. Keep a minimum size of 20 bytes. */
4142 int size
= min (2000, max (20, (nextb
->text
->z_byte
/ 10)));
4144 if (nextb
->text
->gap_size
> size
)
4146 struct buffer
*save_current
= current_buffer
;
4147 current_buffer
= nextb
;
4148 make_gap (-(nextb
->text
->gap_size
- size
));
4149 current_buffer
= save_current
;
4153 nextb
= nextb
->next
;
4159 /* clear_marks (); */
4161 /* Mark all the special slots that serve as the roots of accessibility.
4163 Usually the special slots to mark are contained in particular structures.
4164 Then we know no slot is marked twice because the structures don't overlap.
4165 In some cases, the structures point to the slots to be marked.
4166 For these, we use MARKBIT to avoid double marking of the slot. */
4168 for (i
= 0; i
< staticidx
; i
++)
4169 mark_object (staticvec
[i
]);
4171 #if (GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS \
4172 || GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS)
4175 for (tail
= gcprolist
; tail
; tail
= tail
->next
)
4176 for (i
= 0; i
< tail
->nvars
; i
++)
4177 if (!XMARKBIT (tail
->var
[i
]))
4179 /* Explicit casting prevents compiler warning about
4180 discarding the `volatile' qualifier. */
4181 mark_object ((Lisp_Object
*)&tail
->var
[i
]);
4182 XMARK (tail
->var
[i
]);
4187 for (bind
= specpdl
; bind
!= specpdl_ptr
; bind
++)
4189 mark_object (&bind
->symbol
);
4190 mark_object (&bind
->old_value
);
4192 for (catch = catchlist
; catch; catch = catch->next
)
4194 mark_object (&catch->tag
);
4195 mark_object (&catch->val
);
4197 for (handler
= handlerlist
; handler
; handler
= handler
->next
)
4199 mark_object (&handler
->handler
);
4200 mark_object (&handler
->var
);
4202 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
4204 if (!XMARKBIT (*backlist
->function
))
4206 mark_object (backlist
->function
);
4207 XMARK (*backlist
->function
);
4209 if (backlist
->nargs
== UNEVALLED
|| backlist
->nargs
== MANY
)
4212 i
= backlist
->nargs
- 1;
4214 if (!XMARKBIT (backlist
->args
[i
]))
4216 mark_object (&backlist
->args
[i
]);
4217 XMARK (backlist
->args
[i
]);
4222 /* Look thru every buffer's undo list
4223 for elements that update markers that were not marked,
4226 register struct buffer
*nextb
= all_buffers
;
4230 /* If a buffer's undo list is Qt, that means that undo is
4231 turned off in that buffer. Calling truncate_undo_list on
4232 Qt tends to return NULL, which effectively turns undo back on.
4233 So don't call truncate_undo_list if undo_list is Qt. */
4234 if (! EQ (nextb
->undo_list
, Qt
))
4236 Lisp_Object tail
, prev
;
4237 tail
= nextb
->undo_list
;
4239 while (CONSP (tail
))
4241 if (GC_CONSP (XCAR (tail
))
4242 && GC_MARKERP (XCAR (XCAR (tail
)))
4243 && ! XMARKBIT (XMARKER (XCAR (XCAR (tail
)))->chain
))
4246 nextb
->undo_list
= tail
= XCDR (tail
);
4250 XSETCDR (prev
, tail
);
4261 nextb
= nextb
->next
;
4265 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4271 extern void xg_mark_data ();
4278 /* Clear the mark bits that we set in certain root slots. */
4280 #if (GC_MARK_STACK == GC_USE_GCPROS_AS_BEFORE \
4281 || GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES)
4282 for (tail
= gcprolist
; tail
; tail
= tail
->next
)
4283 for (i
= 0; i
< tail
->nvars
; i
++)
4284 XUNMARK (tail
->var
[i
]);
4287 unmark_byte_stack ();
4288 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
4290 XUNMARK (*backlist
->function
);
4291 if (backlist
->nargs
== UNEVALLED
|| backlist
->nargs
== MANY
)
4294 i
= backlist
->nargs
- 1;
4296 XUNMARK (backlist
->args
[i
]);
4298 XUNMARK (buffer_defaults
.name
);
4299 XUNMARK (buffer_local_symbols
.name
);
4301 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES && 0
4307 /* clear_marks (); */
4310 consing_since_gc
= 0;
4311 if (gc_cons_threshold
< 10000)
4312 gc_cons_threshold
= 10000;
4314 if (garbage_collection_messages
)
4316 if (message_p
|| minibuf_level
> 0)
4319 message1_nolog ("Garbage collecting...done");
4322 unbind_to (count
, Qnil
);
4324 total
[0] = Fcons (make_number (total_conses
),
4325 make_number (total_free_conses
));
4326 total
[1] = Fcons (make_number (total_symbols
),
4327 make_number (total_free_symbols
));
4328 total
[2] = Fcons (make_number (total_markers
),
4329 make_number (total_free_markers
));
4330 total
[3] = make_number (total_string_size
);
4331 total
[4] = make_number (total_vector_size
);
4332 total
[5] = Fcons (make_number (total_floats
),
4333 make_number (total_free_floats
));
4334 total
[6] = Fcons (make_number (total_intervals
),
4335 make_number (total_free_intervals
));
4336 total
[7] = Fcons (make_number (total_strings
),
4337 make_number (total_free_strings
));
4339 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4341 /* Compute average percentage of zombies. */
4344 for (i
= 0; i
< 7; ++i
)
4345 nlive
+= XFASTINT (XCAR (total
[i
]));
4347 avg_live
= (avg_live
* ngcs
+ nlive
) / (ngcs
+ 1);
4348 max_live
= max (nlive
, max_live
);
4349 avg_zombies
= (avg_zombies
* ngcs
+ nzombies
) / (ngcs
+ 1);
4350 max_zombies
= max (nzombies
, max_zombies
);
4355 if (!NILP (Vpost_gc_hook
))
4357 int count
= inhibit_garbage_collection ();
4358 safe_run_hooks (Qpost_gc_hook
);
4359 unbind_to (count
, Qnil
);
4362 return Flist (sizeof total
/ sizeof *total
, total
);
4366 /* Mark Lisp objects in glyph matrix MATRIX. Currently the
4367 only interesting objects referenced from glyphs are strings. */
4370 mark_glyph_matrix (matrix
)
4371 struct glyph_matrix
*matrix
;
4373 struct glyph_row
*row
= matrix
->rows
;
4374 struct glyph_row
*end
= row
+ matrix
->nrows
;
4376 for (; row
< end
; ++row
)
4380 for (area
= LEFT_MARGIN_AREA
; area
< LAST_AREA
; ++area
)
4382 struct glyph
*glyph
= row
->glyphs
[area
];
4383 struct glyph
*end_glyph
= glyph
+ row
->used
[area
];
4385 for (; glyph
< end_glyph
; ++glyph
)
4386 if (GC_STRINGP (glyph
->object
)
4387 && !STRING_MARKED_P (XSTRING (glyph
->object
)))
4388 mark_object (&glyph
->object
);
4394 /* Mark Lisp faces in the face cache C. */
4398 struct face_cache
*c
;
4403 for (i
= 0; i
< c
->used
; ++i
)
4405 struct face
*face
= FACE_FROM_ID (c
->f
, i
);
4409 for (j
= 0; j
< LFACE_VECTOR_SIZE
; ++j
)
4410 mark_object (&face
->lface
[j
]);
4417 #ifdef HAVE_WINDOW_SYSTEM
4419 /* Mark Lisp objects in image IMG. */
4425 mark_object (&img
->spec
);
4427 if (!NILP (img
->data
.lisp_val
))
4428 mark_object (&img
->data
.lisp_val
);
4432 /* Mark Lisp objects in image cache of frame F. It's done this way so
4433 that we don't have to include xterm.h here. */
4436 mark_image_cache (f
)
4439 forall_images_in_image_cache (f
, mark_image
);
4442 #endif /* HAVE_X_WINDOWS */
4446 /* Mark reference to a Lisp_Object.
4447 If the object referred to has not been seen yet, recursively mark
4448 all the references contained in it. */
4450 #define LAST_MARKED_SIZE 500
4451 Lisp_Object
*last_marked
[LAST_MARKED_SIZE
];
4452 int last_marked_index
;
4454 /* For debugging--call abort when we cdr down this many
4455 links of a list, in mark_object. In debugging,
4456 the call to abort will hit a breakpoint.
4457 Normally this is zero and the check never goes off. */
4458 int mark_object_loop_halt
;
4461 mark_object (argptr
)
4462 Lisp_Object
*argptr
;
4464 Lisp_Object
*objptr
= argptr
;
4465 register Lisp_Object obj
;
4466 #ifdef GC_CHECK_MARKED_OBJECTS
4477 if (PURE_POINTER_P (XPNTR (obj
)))
4480 last_marked
[last_marked_index
++] = objptr
;
4481 if (last_marked_index
== LAST_MARKED_SIZE
)
4482 last_marked_index
= 0;
4484 /* Perform some sanity checks on the objects marked here. Abort if
4485 we encounter an object we know is bogus. This increases GC time
4486 by ~80%, and requires compilation with GC_MARK_STACK != 0. */
4487 #ifdef GC_CHECK_MARKED_OBJECTS
4489 po
= (void *) XPNTR (obj
);
4491 /* Check that the object pointed to by PO is known to be a Lisp
4492 structure allocated from the heap. */
4493 #define CHECK_ALLOCATED() \
4495 m = mem_find (po); \
4500 /* Check that the object pointed to by PO is live, using predicate
4502 #define CHECK_LIVE(LIVEP) \
4504 if (!LIVEP (m, po)) \
4508 /* Check both of the above conditions. */
4509 #define CHECK_ALLOCATED_AND_LIVE(LIVEP) \
4511 CHECK_ALLOCATED (); \
4512 CHECK_LIVE (LIVEP); \
4515 #else /* not GC_CHECK_MARKED_OBJECTS */
4517 #define CHECK_ALLOCATED() (void) 0
4518 #define CHECK_LIVE(LIVEP) (void) 0
4519 #define CHECK_ALLOCATED_AND_LIVE(LIVEP) (void) 0
4521 #endif /* not GC_CHECK_MARKED_OBJECTS */
4523 switch (SWITCH_ENUM_CAST (XGCTYPE (obj
)))
4527 register struct Lisp_String
*ptr
= XSTRING (obj
);
4528 CHECK_ALLOCATED_AND_LIVE (live_string_p
);
4529 MARK_INTERVAL_TREE (ptr
->intervals
);
4531 #ifdef GC_CHECK_STRING_BYTES
4532 /* Check that the string size recorded in the string is the
4533 same as the one recorded in the sdata structure. */
4534 CHECK_STRING_BYTES (ptr
);
4535 #endif /* GC_CHECK_STRING_BYTES */
4539 case Lisp_Vectorlike
:
4540 #ifdef GC_CHECK_MARKED_OBJECTS
4542 if (m
== MEM_NIL
&& !GC_SUBRP (obj
)
4543 && po
!= &buffer_defaults
4544 && po
!= &buffer_local_symbols
)
4546 #endif /* GC_CHECK_MARKED_OBJECTS */
4548 if (GC_BUFFERP (obj
))
4550 if (!XMARKBIT (XBUFFER (obj
)->name
))
4552 #ifdef GC_CHECK_MARKED_OBJECTS
4553 if (po
!= &buffer_defaults
&& po
!= &buffer_local_symbols
)
4556 for (b
= all_buffers
; b
&& b
!= po
; b
= b
->next
)
4561 #endif /* GC_CHECK_MARKED_OBJECTS */
4565 else if (GC_SUBRP (obj
))
4567 else if (GC_COMPILEDP (obj
))
4568 /* We could treat this just like a vector, but it is better to
4569 save the COMPILED_CONSTANTS element for last and avoid
4572 register struct Lisp_Vector
*ptr
= XVECTOR (obj
);
4573 register EMACS_INT size
= ptr
->size
;
4576 if (size
& ARRAY_MARK_FLAG
)
4577 break; /* Already marked */
4579 CHECK_LIVE (live_vector_p
);
4580 ptr
->size
|= ARRAY_MARK_FLAG
; /* Else mark it */
4581 size
&= PSEUDOVECTOR_SIZE_MASK
;
4582 for (i
= 0; i
< size
; i
++) /* and then mark its elements */
4584 if (i
!= COMPILED_CONSTANTS
)
4585 mark_object (&ptr
->contents
[i
]);
4587 /* This cast should be unnecessary, but some Mips compiler complains
4588 (MIPS-ABI + SysVR4, DC/OSx, etc). */
4589 objptr
= (Lisp_Object
*) &ptr
->contents
[COMPILED_CONSTANTS
];
4592 else if (GC_FRAMEP (obj
))
4594 register struct frame
*ptr
= XFRAME (obj
);
4595 register EMACS_INT size
= ptr
->size
;
4597 if (size
& ARRAY_MARK_FLAG
) break; /* Already marked */
4598 ptr
->size
|= ARRAY_MARK_FLAG
; /* Else mark it */
4600 CHECK_LIVE (live_vector_p
);
4601 mark_object (&ptr
->name
);
4602 mark_object (&ptr
->icon_name
);
4603 mark_object (&ptr
->title
);
4604 mark_object (&ptr
->focus_frame
);
4605 mark_object (&ptr
->selected_window
);
4606 mark_object (&ptr
->minibuffer_window
);
4607 mark_object (&ptr
->param_alist
);
4608 mark_object (&ptr
->scroll_bars
);
4609 mark_object (&ptr
->condemned_scroll_bars
);
4610 mark_object (&ptr
->menu_bar_items
);
4611 mark_object (&ptr
->face_alist
);
4612 mark_object (&ptr
->menu_bar_vector
);
4613 mark_object (&ptr
->buffer_predicate
);
4614 mark_object (&ptr
->buffer_list
);
4615 mark_object (&ptr
->menu_bar_window
);
4616 mark_object (&ptr
->tool_bar_window
);
4617 mark_face_cache (ptr
->face_cache
);
4618 #ifdef HAVE_WINDOW_SYSTEM
4619 mark_image_cache (ptr
);
4620 mark_object (&ptr
->tool_bar_items
);
4621 mark_object (&ptr
->desired_tool_bar_string
);
4622 mark_object (&ptr
->current_tool_bar_string
);
4623 #endif /* HAVE_WINDOW_SYSTEM */
4625 else if (GC_BOOL_VECTOR_P (obj
))
4627 register struct Lisp_Vector
*ptr
= XVECTOR (obj
);
4629 if (ptr
->size
& ARRAY_MARK_FLAG
)
4630 break; /* Already marked */
4631 CHECK_LIVE (live_vector_p
);
4632 ptr
->size
|= ARRAY_MARK_FLAG
; /* Else mark it */
4634 else if (GC_WINDOWP (obj
))
4636 register struct Lisp_Vector
*ptr
= XVECTOR (obj
);
4637 struct window
*w
= XWINDOW (obj
);
4638 register EMACS_INT size
= ptr
->size
;
4641 /* Stop if already marked. */
4642 if (size
& ARRAY_MARK_FLAG
)
4646 CHECK_LIVE (live_vector_p
);
4647 ptr
->size
|= ARRAY_MARK_FLAG
;
4649 /* There is no Lisp data above The member CURRENT_MATRIX in
4650 struct WINDOW. Stop marking when that slot is reached. */
4652 (char *) &ptr
->contents
[i
] < (char *) &w
->current_matrix
;
4654 mark_object (&ptr
->contents
[i
]);
4656 /* Mark glyphs for leaf windows. Marking window matrices is
4657 sufficient because frame matrices use the same glyph
4659 if (NILP (w
->hchild
)
4661 && w
->current_matrix
)
4663 mark_glyph_matrix (w
->current_matrix
);
4664 mark_glyph_matrix (w
->desired_matrix
);
4667 else if (GC_HASH_TABLE_P (obj
))
4669 struct Lisp_Hash_Table
*h
= XHASH_TABLE (obj
);
4670 EMACS_INT size
= h
->size
;
4672 /* Stop if already marked. */
4673 if (size
& ARRAY_MARK_FLAG
)
4677 CHECK_LIVE (live_vector_p
);
4678 h
->size
|= ARRAY_MARK_FLAG
;
4680 /* Mark contents. */
4681 /* Do not mark next_free or next_weak.
4682 Being in the next_weak chain
4683 should not keep the hash table alive.
4684 No need to mark `count' since it is an integer. */
4685 mark_object (&h
->test
);
4686 mark_object (&h
->weak
);
4687 mark_object (&h
->rehash_size
);
4688 mark_object (&h
->rehash_threshold
);
4689 mark_object (&h
->hash
);
4690 mark_object (&h
->next
);
4691 mark_object (&h
->index
);
4692 mark_object (&h
->user_hash_function
);
4693 mark_object (&h
->user_cmp_function
);
4695 /* If hash table is not weak, mark all keys and values.
4696 For weak tables, mark only the vector. */
4697 if (GC_NILP (h
->weak
))
4698 mark_object (&h
->key_and_value
);
4700 XVECTOR (h
->key_and_value
)->size
|= ARRAY_MARK_FLAG
;
4705 register struct Lisp_Vector
*ptr
= XVECTOR (obj
);
4706 register EMACS_INT size
= ptr
->size
;
4709 if (size
& ARRAY_MARK_FLAG
) break; /* Already marked */
4710 CHECK_LIVE (live_vector_p
);
4711 ptr
->size
|= ARRAY_MARK_FLAG
; /* Else mark it */
4712 if (size
& PSEUDOVECTOR_FLAG
)
4713 size
&= PSEUDOVECTOR_SIZE_MASK
;
4715 for (i
= 0; i
< size
; i
++) /* and then mark its elements */
4716 mark_object (&ptr
->contents
[i
]);
4722 register struct Lisp_Symbol
*ptr
= XSYMBOL (obj
);
4723 struct Lisp_Symbol
*ptrx
;
4725 if (XMARKBIT (ptr
->plist
)) break;
4726 CHECK_ALLOCATED_AND_LIVE (live_symbol_p
);
4728 mark_object ((Lisp_Object
*) &ptr
->value
);
4729 mark_object (&ptr
->function
);
4730 mark_object (&ptr
->plist
);
4732 if (!PURE_POINTER_P (XSTRING (ptr
->xname
)))
4733 MARK_STRING (XSTRING (ptr
->xname
));
4734 MARK_INTERVAL_TREE (STRING_INTERVALS (ptr
->xname
));
4736 /* Note that we do not mark the obarray of the symbol.
4737 It is safe not to do so because nothing accesses that
4738 slot except to check whether it is nil. */
4742 /* For the benefit of the last_marked log. */
4743 objptr
= (Lisp_Object
*)&XSYMBOL (obj
)->next
;
4744 ptrx
= ptr
; /* Use of ptrx avoids compiler bug on Sun */
4745 XSETSYMBOL (obj
, ptrx
);
4746 /* We can't goto loop here because *objptr doesn't contain an
4747 actual Lisp_Object with valid datatype field. */
4754 CHECK_ALLOCATED_AND_LIVE (live_misc_p
);
4755 switch (XMISCTYPE (obj
))
4757 case Lisp_Misc_Marker
:
4758 XMARK (XMARKER (obj
)->chain
);
4759 /* DO NOT mark thru the marker's chain.
4760 The buffer's markers chain does not preserve markers from gc;
4761 instead, markers are removed from the chain when freed by gc. */
4764 case Lisp_Misc_Buffer_Local_Value
:
4765 case Lisp_Misc_Some_Buffer_Local_Value
:
4767 register struct Lisp_Buffer_Local_Value
*ptr
4768 = XBUFFER_LOCAL_VALUE (obj
);
4769 if (XMARKBIT (ptr
->realvalue
)) break;
4770 XMARK (ptr
->realvalue
);
4771 /* If the cdr is nil, avoid recursion for the car. */
4772 if (EQ (ptr
->cdr
, Qnil
))
4774 objptr
= &ptr
->realvalue
;
4777 mark_object (&ptr
->realvalue
);
4778 mark_object (&ptr
->buffer
);
4779 mark_object (&ptr
->frame
);
4784 case Lisp_Misc_Intfwd
:
4785 case Lisp_Misc_Boolfwd
:
4786 case Lisp_Misc_Objfwd
:
4787 case Lisp_Misc_Buffer_Objfwd
:
4788 case Lisp_Misc_Kboard_Objfwd
:
4789 /* Don't bother with Lisp_Buffer_Objfwd,
4790 since all markable slots in current buffer marked anyway. */
4791 /* Don't need to do Lisp_Objfwd, since the places they point
4792 are protected with staticpro. */
4795 case Lisp_Misc_Overlay
:
4797 struct Lisp_Overlay
*ptr
= XOVERLAY (obj
);
4798 if (!XMARKBIT (ptr
->plist
))
4801 mark_object (&ptr
->start
);
4802 mark_object (&ptr
->end
);
4803 objptr
= &ptr
->plist
;
4816 register struct Lisp_Cons
*ptr
= XCONS (obj
);
4817 if (XMARKBIT (ptr
->car
)) break;
4818 CHECK_ALLOCATED_AND_LIVE (live_cons_p
);
4820 /* If the cdr is nil, avoid recursion for the car. */
4821 if (EQ (ptr
->cdr
, Qnil
))
4827 mark_object (&ptr
->car
);
4830 if (cdr_count
== mark_object_loop_halt
)
4836 CHECK_ALLOCATED_AND_LIVE (live_float_p
);
4837 XMARK (XFLOAT (obj
)->type
);
4848 #undef CHECK_ALLOCATED
4849 #undef CHECK_ALLOCATED_AND_LIVE
4852 /* Mark the pointers in a buffer structure. */
4858 register struct buffer
*buffer
= XBUFFER (buf
);
4859 register Lisp_Object
*ptr
;
4860 Lisp_Object base_buffer
;
4862 /* This is the buffer's markbit */
4863 mark_object (&buffer
->name
);
4864 XMARK (buffer
->name
);
4866 MARK_INTERVAL_TREE (BUF_INTERVALS (buffer
));
4868 if (CONSP (buffer
->undo_list
))
4871 tail
= buffer
->undo_list
;
4873 while (CONSP (tail
))
4875 register struct Lisp_Cons
*ptr
= XCONS (tail
);
4877 if (XMARKBIT (ptr
->car
))
4880 if (GC_CONSP (ptr
->car
)
4881 && ! XMARKBIT (XCAR (ptr
->car
))
4882 && GC_MARKERP (XCAR (ptr
->car
)))
4884 XMARK (XCAR_AS_LVALUE (ptr
->car
));
4885 mark_object (&XCDR_AS_LVALUE (ptr
->car
));
4888 mark_object (&ptr
->car
);
4890 if (CONSP (ptr
->cdr
))
4896 mark_object (&XCDR_AS_LVALUE (tail
));
4899 mark_object (&buffer
->undo_list
);
4901 for (ptr
= &buffer
->name
+ 1;
4902 (char *)ptr
< (char *)buffer
+ sizeof (struct buffer
);
4906 /* If this is an indirect buffer, mark its base buffer. */
4907 if (buffer
->base_buffer
&& !XMARKBIT (buffer
->base_buffer
->name
))
4909 XSETBUFFER (base_buffer
, buffer
->base_buffer
);
4910 mark_buffer (base_buffer
);
4915 /* Mark the pointers in the kboard objects. */
4922 for (kb
= all_kboards
; kb
; kb
= kb
->next_kboard
)
4924 if (kb
->kbd_macro_buffer
)
4925 for (p
= kb
->kbd_macro_buffer
; p
< kb
->kbd_macro_ptr
; p
++)
4927 mark_object (&kb
->Voverriding_terminal_local_map
);
4928 mark_object (&kb
->Vlast_command
);
4929 mark_object (&kb
->Vreal_last_command
);
4930 mark_object (&kb
->Vprefix_arg
);
4931 mark_object (&kb
->Vlast_prefix_arg
);
4932 mark_object (&kb
->kbd_queue
);
4933 mark_object (&kb
->defining_kbd_macro
);
4934 mark_object (&kb
->Vlast_kbd_macro
);
4935 mark_object (&kb
->Vsystem_key_alist
);
4936 mark_object (&kb
->system_key_syms
);
4937 mark_object (&kb
->Vdefault_minibuffer_frame
);
4938 mark_object (&kb
->echo_string
);
4943 /* Value is non-zero if OBJ will survive the current GC because it's
4944 either marked or does not need to be marked to survive. */
4952 switch (XGCTYPE (obj
))
4959 survives_p
= XMARKBIT (XSYMBOL (obj
)->plist
);
4963 switch (XMISCTYPE (obj
))
4965 case Lisp_Misc_Marker
:
4966 survives_p
= XMARKBIT (obj
);
4969 case Lisp_Misc_Buffer_Local_Value
:
4970 case Lisp_Misc_Some_Buffer_Local_Value
:
4971 survives_p
= XMARKBIT (XBUFFER_LOCAL_VALUE (obj
)->realvalue
);
4974 case Lisp_Misc_Intfwd
:
4975 case Lisp_Misc_Boolfwd
:
4976 case Lisp_Misc_Objfwd
:
4977 case Lisp_Misc_Buffer_Objfwd
:
4978 case Lisp_Misc_Kboard_Objfwd
:
4982 case Lisp_Misc_Overlay
:
4983 survives_p
= XMARKBIT (XOVERLAY (obj
)->plist
);
4993 struct Lisp_String
*s
= XSTRING (obj
);
4994 survives_p
= STRING_MARKED_P (s
);
4998 case Lisp_Vectorlike
:
4999 if (GC_BUFFERP (obj
))
5000 survives_p
= XMARKBIT (XBUFFER (obj
)->name
);
5001 else if (GC_SUBRP (obj
))
5004 survives_p
= XVECTOR (obj
)->size
& ARRAY_MARK_FLAG
;
5008 survives_p
= XMARKBIT (XCAR (obj
));
5012 survives_p
= XMARKBIT (XFLOAT (obj
)->type
);
5019 return survives_p
|| PURE_POINTER_P ((void *) XPNTR (obj
));
5024 /* Sweep: find all structures not marked, and free them. */
5029 /* Remove or mark entries in weak hash tables.
5030 This must be done before any object is unmarked. */
5031 sweep_weak_hash_tables ();
5034 #ifdef GC_CHECK_STRING_BYTES
5035 if (!noninteractive
)
5036 check_string_bytes (1);
5039 /* Put all unmarked conses on free list */
5041 register struct cons_block
*cblk
;
5042 struct cons_block
**cprev
= &cons_block
;
5043 register int lim
= cons_block_index
;
5044 register int num_free
= 0, num_used
= 0;
5048 for (cblk
= cons_block
; cblk
; cblk
= *cprev
)
5052 for (i
= 0; i
< lim
; i
++)
5053 if (!XMARKBIT (cblk
->conses
[i
].car
))
5056 *(struct Lisp_Cons
**)&cblk
->conses
[i
].cdr
= cons_free_list
;
5057 cons_free_list
= &cblk
->conses
[i
];
5059 cons_free_list
->car
= Vdead
;
5065 XUNMARK (cblk
->conses
[i
].car
);
5067 lim
= CONS_BLOCK_SIZE
;
5068 /* If this block contains only free conses and we have already
5069 seen more than two blocks worth of free conses then deallocate
5071 if (this_free
== CONS_BLOCK_SIZE
&& num_free
> CONS_BLOCK_SIZE
)
5073 *cprev
= cblk
->next
;
5074 /* Unhook from the free list. */
5075 cons_free_list
= *(struct Lisp_Cons
**) &cblk
->conses
[0].cdr
;
5081 num_free
+= this_free
;
5082 cprev
= &cblk
->next
;
5085 total_conses
= num_used
;
5086 total_free_conses
= num_free
;
5089 /* Put all unmarked floats on free list */
5091 register struct float_block
*fblk
;
5092 struct float_block
**fprev
= &float_block
;
5093 register int lim
= float_block_index
;
5094 register int num_free
= 0, num_used
= 0;
5096 float_free_list
= 0;
5098 for (fblk
= float_block
; fblk
; fblk
= *fprev
)
5102 for (i
= 0; i
< lim
; i
++)
5103 if (!XMARKBIT (fblk
->floats
[i
].type
))
5106 *(struct Lisp_Float
**)&fblk
->floats
[i
].data
= float_free_list
;
5107 float_free_list
= &fblk
->floats
[i
];
5109 float_free_list
->type
= Vdead
;
5115 XUNMARK (fblk
->floats
[i
].type
);
5117 lim
= FLOAT_BLOCK_SIZE
;
5118 /* If this block contains only free floats and we have already
5119 seen more than two blocks worth of free floats then deallocate
5121 if (this_free
== FLOAT_BLOCK_SIZE
&& num_free
> FLOAT_BLOCK_SIZE
)
5123 *fprev
= fblk
->next
;
5124 /* Unhook from the free list. */
5125 float_free_list
= *(struct Lisp_Float
**) &fblk
->floats
[0].data
;
5131 num_free
+= this_free
;
5132 fprev
= &fblk
->next
;
5135 total_floats
= num_used
;
5136 total_free_floats
= num_free
;
5139 /* Put all unmarked intervals on free list */
5141 register struct interval_block
*iblk
;
5142 struct interval_block
**iprev
= &interval_block
;
5143 register int lim
= interval_block_index
;
5144 register int num_free
= 0, num_used
= 0;
5146 interval_free_list
= 0;
5148 for (iblk
= interval_block
; iblk
; iblk
= *iprev
)
5153 for (i
= 0; i
< lim
; i
++)
5155 if (! XMARKBIT (iblk
->intervals
[i
].plist
))
5157 SET_INTERVAL_PARENT (&iblk
->intervals
[i
], interval_free_list
);
5158 interval_free_list
= &iblk
->intervals
[i
];
5164 XUNMARK (iblk
->intervals
[i
].plist
);
5167 lim
= INTERVAL_BLOCK_SIZE
;
5168 /* If this block contains only free intervals and we have already
5169 seen more than two blocks worth of free intervals then
5170 deallocate this block. */
5171 if (this_free
== INTERVAL_BLOCK_SIZE
&& num_free
> INTERVAL_BLOCK_SIZE
)
5173 *iprev
= iblk
->next
;
5174 /* Unhook from the free list. */
5175 interval_free_list
= INTERVAL_PARENT (&iblk
->intervals
[0]);
5177 n_interval_blocks
--;
5181 num_free
+= this_free
;
5182 iprev
= &iblk
->next
;
5185 total_intervals
= num_used
;
5186 total_free_intervals
= num_free
;
5189 /* Put all unmarked symbols on free list */
5191 register struct symbol_block
*sblk
;
5192 struct symbol_block
**sprev
= &symbol_block
;
5193 register int lim
= symbol_block_index
;
5194 register int num_free
= 0, num_used
= 0;
5196 symbol_free_list
= NULL
;
5198 for (sblk
= symbol_block
; sblk
; sblk
= *sprev
)
5201 struct Lisp_Symbol
*sym
= sblk
->symbols
;
5202 struct Lisp_Symbol
*end
= sym
+ lim
;
5204 for (; sym
< end
; ++sym
)
5206 /* Check if the symbol was created during loadup. In such a case
5207 it might be pointed to by pure bytecode which we don't trace,
5208 so we conservatively assume that it is live. */
5209 int pure_p
= PURE_POINTER_P (XSTRING (sym
->xname
));
5211 if (!XMARKBIT (sym
->plist
) && !pure_p
)
5213 *(struct Lisp_Symbol
**) &sym
->value
= symbol_free_list
;
5214 symbol_free_list
= sym
;
5216 symbol_free_list
->function
= Vdead
;
5224 UNMARK_STRING (XSTRING (sym
->xname
));
5225 XUNMARK (sym
->plist
);
5229 lim
= SYMBOL_BLOCK_SIZE
;
5230 /* If this block contains only free symbols and we have already
5231 seen more than two blocks worth of free symbols then deallocate
5233 if (this_free
== SYMBOL_BLOCK_SIZE
&& num_free
> SYMBOL_BLOCK_SIZE
)
5235 *sprev
= sblk
->next
;
5236 /* Unhook from the free list. */
5237 symbol_free_list
= *(struct Lisp_Symbol
**)&sblk
->symbols
[0].value
;
5243 num_free
+= this_free
;
5244 sprev
= &sblk
->next
;
5247 total_symbols
= num_used
;
5248 total_free_symbols
= num_free
;
5251 /* Put all unmarked misc's on free list.
5252 For a marker, first unchain it from the buffer it points into. */
5254 register struct marker_block
*mblk
;
5255 struct marker_block
**mprev
= &marker_block
;
5256 register int lim
= marker_block_index
;
5257 register int num_free
= 0, num_used
= 0;
5259 marker_free_list
= 0;
5261 for (mblk
= marker_block
; mblk
; mblk
= *mprev
)
5265 EMACS_INT already_free
= -1;
5267 for (i
= 0; i
< lim
; i
++)
5269 Lisp_Object
*markword
;
5270 switch (mblk
->markers
[i
].u_marker
.type
)
5272 case Lisp_Misc_Marker
:
5273 markword
= &mblk
->markers
[i
].u_marker
.chain
;
5275 case Lisp_Misc_Buffer_Local_Value
:
5276 case Lisp_Misc_Some_Buffer_Local_Value
:
5277 markword
= &mblk
->markers
[i
].u_buffer_local_value
.realvalue
;
5279 case Lisp_Misc_Overlay
:
5280 markword
= &mblk
->markers
[i
].u_overlay
.plist
;
5282 case Lisp_Misc_Free
:
5283 /* If the object was already free, keep it
5284 on the free list. */
5285 markword
= (Lisp_Object
*) &already_free
;
5291 if (markword
&& !XMARKBIT (*markword
))
5294 if (mblk
->markers
[i
].u_marker
.type
== Lisp_Misc_Marker
)
5296 /* tem1 avoids Sun compiler bug */
5297 struct Lisp_Marker
*tem1
= &mblk
->markers
[i
].u_marker
;
5298 XSETMARKER (tem
, tem1
);
5299 unchain_marker (tem
);
5301 /* Set the type of the freed object to Lisp_Misc_Free.
5302 We could leave the type alone, since nobody checks it,
5303 but this might catch bugs faster. */
5304 mblk
->markers
[i
].u_marker
.type
= Lisp_Misc_Free
;
5305 mblk
->markers
[i
].u_free
.chain
= marker_free_list
;
5306 marker_free_list
= &mblk
->markers
[i
];
5313 XUNMARK (*markword
);
5316 lim
= MARKER_BLOCK_SIZE
;
5317 /* If this block contains only free markers and we have already
5318 seen more than two blocks worth of free markers then deallocate
5320 if (this_free
== MARKER_BLOCK_SIZE
&& num_free
> MARKER_BLOCK_SIZE
)
5322 *mprev
= mblk
->next
;
5323 /* Unhook from the free list. */
5324 marker_free_list
= mblk
->markers
[0].u_free
.chain
;
5330 num_free
+= this_free
;
5331 mprev
= &mblk
->next
;
5335 total_markers
= num_used
;
5336 total_free_markers
= num_free
;
5339 /* Free all unmarked buffers */
5341 register struct buffer
*buffer
= all_buffers
, *prev
= 0, *next
;
5344 if (!XMARKBIT (buffer
->name
))
5347 prev
->next
= buffer
->next
;
5349 all_buffers
= buffer
->next
;
5350 next
= buffer
->next
;
5356 XUNMARK (buffer
->name
);
5357 UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer
));
5358 prev
= buffer
, buffer
= buffer
->next
;
5362 /* Free all unmarked vectors */
5364 register struct Lisp_Vector
*vector
= all_vectors
, *prev
= 0, *next
;
5365 total_vector_size
= 0;
5368 if (!(vector
->size
& ARRAY_MARK_FLAG
))
5371 prev
->next
= vector
->next
;
5373 all_vectors
= vector
->next
;
5374 next
= vector
->next
;
5382 vector
->size
&= ~ARRAY_MARK_FLAG
;
5383 if (vector
->size
& PSEUDOVECTOR_FLAG
)
5384 total_vector_size
+= (PSEUDOVECTOR_SIZE_MASK
& vector
->size
);
5386 total_vector_size
+= vector
->size
;
5387 prev
= vector
, vector
= vector
->next
;
5391 #ifdef GC_CHECK_STRING_BYTES
5392 if (!noninteractive
)
5393 check_string_bytes (1);
5400 /* Debugging aids. */
5402 DEFUN ("memory-limit", Fmemory_limit
, Smemory_limit
, 0, 0, 0,
5403 doc
: /* Return the address of the last byte Emacs has allocated, divided by 1024.
5404 This may be helpful in debugging Emacs's memory usage.
5405 We divide the value by 1024 to make sure it fits in a Lisp integer. */)
5410 XSETINT (end
, (EMACS_INT
) sbrk (0) / 1024);
5415 DEFUN ("memory-use-counts", Fmemory_use_counts
, Smemory_use_counts
, 0, 0, 0,
5416 doc
: /* Return a list of counters that measure how much consing there has been.
5417 Each of these counters increments for a certain kind of object.
5418 The counters wrap around from the largest positive integer to zero.
5419 Garbage collection does not decrease them.
5420 The elements of the value are as follows:
5421 (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS STRINGS)
5422 All are in units of 1 = one object consed
5423 except for VECTOR-CELLS and STRING-CHARS, which count the total length of
5425 MISCS include overlays, markers, and some internal types.
5426 Frames, windows, buffers, and subprocesses count as vectors
5427 (but the contents of a buffer's text do not count here). */)
5430 Lisp_Object consed
[8];
5432 consed
[0] = make_number (min (MOST_POSITIVE_FIXNUM
, cons_cells_consed
));
5433 consed
[1] = make_number (min (MOST_POSITIVE_FIXNUM
, floats_consed
));
5434 consed
[2] = make_number (min (MOST_POSITIVE_FIXNUM
, vector_cells_consed
));
5435 consed
[3] = make_number (min (MOST_POSITIVE_FIXNUM
, symbols_consed
));
5436 consed
[4] = make_number (min (MOST_POSITIVE_FIXNUM
, string_chars_consed
));
5437 consed
[5] = make_number (min (MOST_POSITIVE_FIXNUM
, misc_objects_consed
));
5438 consed
[6] = make_number (min (MOST_POSITIVE_FIXNUM
, intervals_consed
));
5439 consed
[7] = make_number (min (MOST_POSITIVE_FIXNUM
, strings_consed
));
5441 return Flist (8, consed
);
5444 int suppress_checking
;
5446 die (msg
, file
, line
)
5451 fprintf (stderr
, "\r\nEmacs fatal error: %s:%d: %s\r\n",
5456 /* Initialization */
5461 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
5463 pure_size
= PURESIZE
;
5464 pure_bytes_used
= 0;
5465 pure_bytes_used_before_overflow
= 0;
5467 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
5469 Vdead
= make_pure_string ("DEAD", 4, 4, 0);
5473 ignore_warnings
= 1;
5474 #ifdef DOUG_LEA_MALLOC
5475 mallopt (M_TRIM_THRESHOLD
, 128*1024); /* trim threshold */
5476 mallopt (M_MMAP_THRESHOLD
, 64*1024); /* mmap threshold */
5477 mallopt (M_MMAP_MAX
, MMAP_MAX_AREAS
); /* max. number of mmap'ed areas */
5487 malloc_hysteresis
= 32;
5489 malloc_hysteresis
= 0;
5492 spare_memory
= (char *) malloc (SPARE_MEMORY
);
5494 ignore_warnings
= 0;
5496 byte_stack_list
= 0;
5498 consing_since_gc
= 0;
5499 gc_cons_threshold
= 100000 * sizeof (Lisp_Object
);
5500 #ifdef VIRT_ADDR_VARIES
5501 malloc_sbrk_unused
= 1<<22; /* A large number */
5502 malloc_sbrk_used
= 100000; /* as reasonable as any number */
5503 #endif /* VIRT_ADDR_VARIES */
5510 byte_stack_list
= 0;
5512 #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
5513 setjmp_tested_p
= longjmps_done
= 0;
5521 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold
,
5522 doc
: /* *Number of bytes of consing between garbage collections.
5523 Garbage collection can happen automatically once this many bytes have been
5524 allocated since the last garbage collection. All data types count.
5526 Garbage collection happens automatically only when `eval' is called.
5528 By binding this temporarily to a large number, you can effectively
5529 prevent garbage collection during a part of the program. */);
5531 DEFVAR_INT ("pure-bytes-used", &pure_bytes_used
,
5532 doc
: /* Number of bytes of sharable Lisp data allocated so far. */);
5534 DEFVAR_INT ("cons-cells-consed", &cons_cells_consed
,
5535 doc
: /* Number of cons cells that have been consed so far. */);
5537 DEFVAR_INT ("floats-consed", &floats_consed
,
5538 doc
: /* Number of floats that have been consed so far. */);
5540 DEFVAR_INT ("vector-cells-consed", &vector_cells_consed
,
5541 doc
: /* Number of vector cells that have been consed so far. */);
5543 DEFVAR_INT ("symbols-consed", &symbols_consed
,
5544 doc
: /* Number of symbols that have been consed so far. */);
5546 DEFVAR_INT ("string-chars-consed", &string_chars_consed
,
5547 doc
: /* Number of string characters that have been consed so far. */);
5549 DEFVAR_INT ("misc-objects-consed", &misc_objects_consed
,
5550 doc
: /* Number of miscellaneous objects that have been consed so far. */);
5552 DEFVAR_INT ("intervals-consed", &intervals_consed
,
5553 doc
: /* Number of intervals that have been consed so far. */);
5555 DEFVAR_INT ("strings-consed", &strings_consed
,
5556 doc
: /* Number of strings that have been consed so far. */);
5558 DEFVAR_LISP ("purify-flag", &Vpurify_flag
,
5559 doc
: /* Non-nil means loading Lisp code in order to dump an executable.
5560 This means that certain objects should be allocated in shared (pure) space. */);
5562 DEFVAR_INT ("undo-limit", &undo_limit
,
5563 doc
: /* Keep no more undo information once it exceeds this size.
5564 This limit is applied when garbage collection happens.
5565 The size is counted as the number of bytes occupied,
5566 which includes both saved text and other data. */);
5569 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit
,
5570 doc
: /* Don't keep more than this much size of undo information.
5571 A command which pushes past this size is itself forgotten.
5572 This limit is applied when garbage collection happens.
5573 The size is counted as the number of bytes occupied,
5574 which includes both saved text and other data. */);
5575 undo_strong_limit
= 30000;
5577 DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages
,
5578 doc
: /* Non-nil means display messages at start and end of garbage collection. */);
5579 garbage_collection_messages
= 0;
5581 DEFVAR_LISP ("post-gc-hook", &Vpost_gc_hook
,
5582 doc
: /* Hook run after garbage collection has finished. */);
5583 Vpost_gc_hook
= Qnil
;
5584 Qpost_gc_hook
= intern ("post-gc-hook");
5585 staticpro (&Qpost_gc_hook
);
5587 DEFVAR_LISP ("memory-signal-data", &Vmemory_signal_data
,
5588 doc
: /* Precomputed `signal' argument for memory-full error. */);
5589 /* We build this in advance because if we wait until we need it, we might
5590 not be able to allocate the memory to hold it. */
5593 build_string ("Memory exhausted--use M-x save-some-buffers then exit and restart Emacs"));
5595 DEFVAR_LISP ("memory-full", &Vmemory_full
,
5596 doc
: /* Non-nil means we are handling a memory-full error. */);
5597 Vmemory_full
= Qnil
;
5599 staticpro (&Qgc_cons_threshold
);
5600 Qgc_cons_threshold
= intern ("gc-cons-threshold");
5602 staticpro (&Qchar_table_extra_slots
);
5603 Qchar_table_extra_slots
= intern ("char-table-extra-slots");
5608 defsubr (&Smake_byte_code
);
5609 defsubr (&Smake_list
);
5610 defsubr (&Smake_vector
);
5611 defsubr (&Smake_char_table
);
5612 defsubr (&Smake_string
);
5613 defsubr (&Smake_bool_vector
);
5614 defsubr (&Smake_symbol
);
5615 defsubr (&Smake_marker
);
5616 defsubr (&Spurecopy
);
5617 defsubr (&Sgarbage_collect
);
5618 defsubr (&Smemory_limit
);
5619 defsubr (&Smemory_use_counts
);
5621 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
5622 defsubr (&Sgc_status
);