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 Lisp_Object Vgc_elapsed
; /* accumulated elapsed time in GC */
260 EMACS_INT gcs_done
; /* accumulated GCs */
262 static void mark_buffer
P_ ((Lisp_Object
));
263 static void mark_kboards
P_ ((void));
264 static void gc_sweep
P_ ((void));
265 static void mark_glyph_matrix
P_ ((struct glyph_matrix
*));
266 static void mark_face_cache
P_ ((struct face_cache
*));
268 #ifdef HAVE_WINDOW_SYSTEM
269 static void mark_image
P_ ((struct image
*));
270 static void mark_image_cache
P_ ((struct frame
*));
271 #endif /* HAVE_WINDOW_SYSTEM */
273 static struct Lisp_String
*allocate_string
P_ ((void));
274 static void compact_small_strings
P_ ((void));
275 static void free_large_strings
P_ ((void));
276 static void sweep_strings
P_ ((void));
278 extern int message_enable_multibyte
;
280 /* When scanning the C stack for live Lisp objects, Emacs keeps track
281 of what memory allocated via lisp_malloc is intended for what
282 purpose. This enumeration specifies the type of memory. */
293 /* Keep the following vector-like types together, with
294 MEM_TYPE_WINDOW being the last, and MEM_TYPE_VECTOR the
295 first. Or change the code of live_vector_p, for instance. */
303 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
305 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
306 #include <stdio.h> /* For fprintf. */
309 /* A unique object in pure space used to make some Lisp objects
310 on free lists recognizable in O(1). */
314 #ifdef GC_MALLOC_CHECK
316 enum mem_type allocated_mem_type
;
317 int dont_register_blocks
;
319 #endif /* GC_MALLOC_CHECK */
321 /* A node in the red-black tree describing allocated memory containing
322 Lisp data. Each such block is recorded with its start and end
323 address when it is allocated, and removed from the tree when it
326 A red-black tree is a balanced binary tree with the following
329 1. Every node is either red or black.
330 2. Every leaf is black.
331 3. If a node is red, then both of its children are black.
332 4. Every simple path from a node to a descendant leaf contains
333 the same number of black nodes.
334 5. The root is always black.
336 When nodes are inserted into the tree, or deleted from the tree,
337 the tree is "fixed" so that these properties are always true.
339 A red-black tree with N internal nodes has height at most 2
340 log(N+1). Searches, insertions and deletions are done in O(log N).
341 Please see a text book about data structures for a detailed
342 description of red-black trees. Any book worth its salt should
347 /* Children of this node. These pointers are never NULL. When there
348 is no child, the value is MEM_NIL, which points to a dummy node. */
349 struct mem_node
*left
, *right
;
351 /* The parent of this node. In the root node, this is NULL. */
352 struct mem_node
*parent
;
354 /* Start and end of allocated region. */
358 enum {MEM_BLACK
, MEM_RED
} color
;
364 /* Base address of stack. Set in main. */
366 Lisp_Object
*stack_base
;
368 /* Root of the tree describing allocated Lisp memory. */
370 static struct mem_node
*mem_root
;
372 /* Lowest and highest known address in the heap. */
374 static void *min_heap_address
, *max_heap_address
;
376 /* Sentinel node of the tree. */
378 static struct mem_node mem_z
;
379 #define MEM_NIL &mem_z
381 static POINTER_TYPE
*lisp_malloc
P_ ((size_t, enum mem_type
));
382 static struct Lisp_Vector
*allocate_vectorlike
P_ ((EMACS_INT
, enum mem_type
));
383 static void lisp_free
P_ ((POINTER_TYPE
*));
384 static void mark_stack
P_ ((void));
385 static int live_vector_p
P_ ((struct mem_node
*, void *));
386 static int live_buffer_p
P_ ((struct mem_node
*, void *));
387 static int live_string_p
P_ ((struct mem_node
*, void *));
388 static int live_cons_p
P_ ((struct mem_node
*, void *));
389 static int live_symbol_p
P_ ((struct mem_node
*, void *));
390 static int live_float_p
P_ ((struct mem_node
*, void *));
391 static int live_misc_p
P_ ((struct mem_node
*, void *));
392 static void mark_maybe_object
P_ ((Lisp_Object
));
393 static void mark_memory
P_ ((void *, void *));
394 static void mem_init
P_ ((void));
395 static struct mem_node
*mem_insert
P_ ((void *, void *, enum mem_type
));
396 static void mem_insert_fixup
P_ ((struct mem_node
*));
397 static void mem_rotate_left
P_ ((struct mem_node
*));
398 static void mem_rotate_right
P_ ((struct mem_node
*));
399 static void mem_delete
P_ ((struct mem_node
*));
400 static void mem_delete_fixup
P_ ((struct mem_node
*));
401 static INLINE
struct mem_node
*mem_find
P_ ((void *));
403 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
404 static void check_gcpros
P_ ((void));
407 #endif /* GC_MARK_STACK || GC_MALLOC_CHECK */
409 /* Recording what needs to be marked for gc. */
411 struct gcpro
*gcprolist
;
413 /* Addresses of staticpro'd variables. */
415 #define NSTATICS 1280
416 Lisp_Object
*staticvec
[NSTATICS
] = {0};
418 /* Index of next unused slot in staticvec. */
422 static POINTER_TYPE
*pure_alloc
P_ ((size_t, int));
425 /* Value is SZ rounded up to the next multiple of ALIGNMENT.
426 ALIGNMENT must be a power of 2. */
428 #define ALIGN(SZ, ALIGNMENT) \
429 (((SZ) + (ALIGNMENT) - 1) & ~((ALIGNMENT) - 1))
433 /************************************************************************
435 ************************************************************************/
437 /* Function malloc calls this if it finds we are near exhausting storage. */
443 pending_malloc_warning
= str
;
447 /* Display an already-pending malloc warning. */
450 display_malloc_warning ()
452 call3 (intern ("display-warning"),
454 build_string (pending_malloc_warning
),
455 intern ("emergency"));
456 pending_malloc_warning
= 0;
460 #ifdef DOUG_LEA_MALLOC
461 # define BYTES_USED (mallinfo ().arena)
463 # define BYTES_USED _bytes_used
467 /* Called if malloc returns zero. */
474 #ifndef SYSTEM_MALLOC
475 bytes_used_when_full
= BYTES_USED
;
478 /* The first time we get here, free the spare memory. */
485 /* This used to call error, but if we've run out of memory, we could
486 get infinite recursion trying to build the string. */
488 Fsignal (Qnil
, Vmemory_signal_data
);
492 /* Called if we can't allocate relocatable space for a buffer. */
495 buffer_memory_full ()
497 /* If buffers use the relocating allocator, no need to free
498 spare_memory, because we may have plenty of malloc space left
499 that we could get, and if we don't, the malloc that fails will
500 itself cause spare_memory to be freed. If buffers don't use the
501 relocating allocator, treat this like any other failing
510 /* This used to call error, but if we've run out of memory, we could
511 get infinite recursion trying to build the string. */
513 Fsignal (Qnil
, Vmemory_signal_data
);
517 /* Like malloc but check for no memory and block interrupt input.. */
523 register POINTER_TYPE
*val
;
526 val
= (POINTER_TYPE
*) malloc (size
);
535 /* Like realloc but check for no memory and block interrupt input.. */
538 xrealloc (block
, size
)
542 register POINTER_TYPE
*val
;
545 /* We must call malloc explicitly when BLOCK is 0, since some
546 reallocs don't do this. */
548 val
= (POINTER_TYPE
*) malloc (size
);
550 val
= (POINTER_TYPE
*) realloc (block
, size
);
553 if (!val
&& size
) memory_full ();
558 /* Like free but block interrupt input.. */
570 /* Like strdup, but uses xmalloc. */
576 size_t len
= strlen (s
) + 1;
577 char *p
= (char *) xmalloc (len
);
583 /* Like malloc but used for allocating Lisp data. NBYTES is the
584 number of bytes to allocate, TYPE describes the intended use of the
585 allcated memory block (for strings, for conses, ...). */
587 static POINTER_TYPE
*
588 lisp_malloc (nbytes
, type
)
596 #ifdef GC_MALLOC_CHECK
597 allocated_mem_type
= type
;
600 val
= (void *) malloc (nbytes
);
602 #if GC_MARK_STACK && !defined GC_MALLOC_CHECK
603 if (val
&& type
!= MEM_TYPE_NON_LISP
)
604 mem_insert (val
, (char *) val
+ nbytes
, type
);
614 /* Return a new buffer structure allocated from the heap with
615 a call to lisp_malloc. */
621 = (struct buffer
*) lisp_malloc (sizeof (struct buffer
),
623 VALIDATE_LISP_STORAGE (b
, sizeof *b
);
628 /* Free BLOCK. This must be called to free memory allocated with a
629 call to lisp_malloc. */
637 #if GC_MARK_STACK && !defined GC_MALLOC_CHECK
638 mem_delete (mem_find (block
));
644 /* Arranging to disable input signals while we're in malloc.
646 This only works with GNU malloc. To help out systems which can't
647 use GNU malloc, all the calls to malloc, realloc, and free
648 elsewhere in the code should be inside a BLOCK_INPUT/UNBLOCK_INPUT
649 pairs; unfortunately, we have no idea what C library functions
650 might call malloc, so we can't really protect them unless you're
651 using GNU malloc. Fortunately, most of the major operating systems
652 can use GNU malloc. */
654 #ifndef SYSTEM_MALLOC
655 #ifndef DOUG_LEA_MALLOC
656 extern void * (*__malloc_hook
) P_ ((size_t));
657 extern void * (*__realloc_hook
) P_ ((void *, size_t));
658 extern void (*__free_hook
) P_ ((void *));
659 /* Else declared in malloc.h, perhaps with an extra arg. */
660 #endif /* DOUG_LEA_MALLOC */
661 static void * (*old_malloc_hook
) ();
662 static void * (*old_realloc_hook
) ();
663 static void (*old_free_hook
) ();
665 /* This function is used as the hook for free to call. */
668 emacs_blocked_free (ptr
)
673 #ifdef GC_MALLOC_CHECK
679 if (m
== MEM_NIL
|| m
->start
!= ptr
)
682 "Freeing `%p' which wasn't allocated with malloc\n", ptr
);
687 /* fprintf (stderr, "free %p...%p (%p)\n", m->start, m->end, ptr); */
691 #endif /* GC_MALLOC_CHECK */
693 __free_hook
= old_free_hook
;
696 /* If we released our reserve (due to running out of memory),
697 and we have a fair amount free once again,
698 try to set aside another reserve in case we run out once more. */
699 if (spare_memory
== 0
700 /* Verify there is enough space that even with the malloc
701 hysteresis this call won't run out again.
702 The code here is correct as long as SPARE_MEMORY
703 is substantially larger than the block size malloc uses. */
704 && (bytes_used_when_full
705 > BYTES_USED
+ max (malloc_hysteresis
, 4) * SPARE_MEMORY
))
706 spare_memory
= (char *) malloc ((size_t) SPARE_MEMORY
);
708 __free_hook
= emacs_blocked_free
;
713 /* If we released our reserve (due to running out of memory),
714 and we have a fair amount free once again,
715 try to set aside another reserve in case we run out once more.
717 This is called when a relocatable block is freed in ralloc.c. */
720 refill_memory_reserve ()
722 if (spare_memory
== 0)
723 spare_memory
= (char *) malloc ((size_t) SPARE_MEMORY
);
727 /* This function is the malloc hook that Emacs uses. */
730 emacs_blocked_malloc (size
)
736 __malloc_hook
= old_malloc_hook
;
737 #ifdef DOUG_LEA_MALLOC
738 mallopt (M_TOP_PAD
, malloc_hysteresis
* 4096);
740 __malloc_extra_blocks
= malloc_hysteresis
;
743 value
= (void *) malloc (size
);
745 #ifdef GC_MALLOC_CHECK
747 struct mem_node
*m
= mem_find (value
);
750 fprintf (stderr
, "Malloc returned %p which is already in use\n",
752 fprintf (stderr
, "Region in use is %p...%p, %u bytes, type %d\n",
753 m
->start
, m
->end
, (char *) m
->end
- (char *) m
->start
,
758 if (!dont_register_blocks
)
760 mem_insert (value
, (char *) value
+ max (1, size
), allocated_mem_type
);
761 allocated_mem_type
= MEM_TYPE_NON_LISP
;
764 #endif /* GC_MALLOC_CHECK */
766 __malloc_hook
= emacs_blocked_malloc
;
769 /* fprintf (stderr, "%p malloc\n", value); */
774 /* This function is the realloc hook that Emacs uses. */
777 emacs_blocked_realloc (ptr
, size
)
784 __realloc_hook
= old_realloc_hook
;
786 #ifdef GC_MALLOC_CHECK
789 struct mem_node
*m
= mem_find (ptr
);
790 if (m
== MEM_NIL
|| m
->start
!= ptr
)
793 "Realloc of %p which wasn't allocated with malloc\n",
801 /* fprintf (stderr, "%p -> realloc\n", ptr); */
803 /* Prevent malloc from registering blocks. */
804 dont_register_blocks
= 1;
805 #endif /* GC_MALLOC_CHECK */
807 value
= (void *) realloc (ptr
, size
);
809 #ifdef GC_MALLOC_CHECK
810 dont_register_blocks
= 0;
813 struct mem_node
*m
= mem_find (value
);
816 fprintf (stderr
, "Realloc returns memory that is already in use\n");
820 /* Can't handle zero size regions in the red-black tree. */
821 mem_insert (value
, (char *) value
+ max (size
, 1), MEM_TYPE_NON_LISP
);
824 /* fprintf (stderr, "%p <- realloc\n", value); */
825 #endif /* GC_MALLOC_CHECK */
827 __realloc_hook
= emacs_blocked_realloc
;
834 /* Called from main to set up malloc to use our hooks. */
837 uninterrupt_malloc ()
839 if (__free_hook
!= emacs_blocked_free
)
840 old_free_hook
= __free_hook
;
841 __free_hook
= emacs_blocked_free
;
843 if (__malloc_hook
!= emacs_blocked_malloc
)
844 old_malloc_hook
= __malloc_hook
;
845 __malloc_hook
= emacs_blocked_malloc
;
847 if (__realloc_hook
!= emacs_blocked_realloc
)
848 old_realloc_hook
= __realloc_hook
;
849 __realloc_hook
= emacs_blocked_realloc
;
852 #endif /* not SYSTEM_MALLOC */
856 /***********************************************************************
858 ***********************************************************************/
860 /* Number of intervals allocated in an interval_block structure.
861 The 1020 is 1024 minus malloc overhead. */
863 #define INTERVAL_BLOCK_SIZE \
864 ((1020 - sizeof (struct interval_block *)) / sizeof (struct interval))
866 /* Intervals are allocated in chunks in form of an interval_block
869 struct interval_block
871 struct interval_block
*next
;
872 struct interval intervals
[INTERVAL_BLOCK_SIZE
];
875 /* Current interval block. Its `next' pointer points to older
878 struct interval_block
*interval_block
;
880 /* Index in interval_block above of the next unused interval
883 static int interval_block_index
;
885 /* Number of free and live intervals. */
887 static int total_free_intervals
, total_intervals
;
889 /* List of free intervals. */
891 INTERVAL interval_free_list
;
893 /* Total number of interval blocks now in use. */
895 int n_interval_blocks
;
898 /* Initialize interval allocation. */
904 = (struct interval_block
*) lisp_malloc (sizeof *interval_block
,
906 interval_block
->next
= 0;
907 bzero ((char *) interval_block
->intervals
, sizeof interval_block
->intervals
);
908 interval_block_index
= 0;
909 interval_free_list
= 0;
910 n_interval_blocks
= 1;
914 /* Return a new interval. */
921 if (interval_free_list
)
923 val
= interval_free_list
;
924 interval_free_list
= INTERVAL_PARENT (interval_free_list
);
928 if (interval_block_index
== INTERVAL_BLOCK_SIZE
)
930 register struct interval_block
*newi
;
932 newi
= (struct interval_block
*) lisp_malloc (sizeof *newi
,
935 VALIDATE_LISP_STORAGE (newi
, sizeof *newi
);
936 newi
->next
= interval_block
;
937 interval_block
= newi
;
938 interval_block_index
= 0;
941 val
= &interval_block
->intervals
[interval_block_index
++];
943 consing_since_gc
+= sizeof (struct interval
);
945 RESET_INTERVAL (val
);
950 /* Mark Lisp objects in interval I. */
953 mark_interval (i
, dummy
)
957 if (XMARKBIT (i
->plist
))
959 mark_object (&i
->plist
);
964 /* Mark the interval tree rooted in TREE. Don't call this directly;
965 use the macro MARK_INTERVAL_TREE instead. */
968 mark_interval_tree (tree
)
969 register INTERVAL tree
;
971 /* No need to test if this tree has been marked already; this
972 function is always called through the MARK_INTERVAL_TREE macro,
973 which takes care of that. */
975 /* XMARK expands to an assignment; the LHS of an assignment can't be
977 XMARK (tree
->up
.obj
);
979 traverse_intervals_noorder (tree
, mark_interval
, Qnil
);
983 /* Mark the interval tree rooted in I. */
985 #define MARK_INTERVAL_TREE(i) \
987 if (!NULL_INTERVAL_P (i) \
988 && ! XMARKBIT (i->up.obj)) \
989 mark_interval_tree (i); \
993 /* The oddity in the call to XUNMARK is necessary because XUNMARK
994 expands to an assignment to its argument, and most C compilers
995 don't support casts on the left operand of `='. */
997 #define UNMARK_BALANCE_INTERVALS(i) \
999 if (! NULL_INTERVAL_P (i)) \
1001 XUNMARK ((i)->up.obj); \
1002 (i) = balance_intervals (i); \
1007 /* Number support. If NO_UNION_TYPE isn't in effect, we
1008 can't create number objects in macros. */
1016 obj
.s
.type
= Lisp_Int
;
1021 /***********************************************************************
1023 ***********************************************************************/
1025 /* Lisp_Strings are allocated in string_block structures. When a new
1026 string_block is allocated, all the Lisp_Strings it contains are
1027 added to a free-list string_free_list. When a new Lisp_String is
1028 needed, it is taken from that list. During the sweep phase of GC,
1029 string_blocks that are entirely free are freed, except two which
1032 String data is allocated from sblock structures. Strings larger
1033 than LARGE_STRING_BYTES, get their own sblock, data for smaller
1034 strings is sub-allocated out of sblocks of size SBLOCK_SIZE.
1036 Sblocks consist internally of sdata structures, one for each
1037 Lisp_String. The sdata structure points to the Lisp_String it
1038 belongs to. The Lisp_String points back to the `u.data' member of
1039 its sdata structure.
1041 When a Lisp_String is freed during GC, it is put back on
1042 string_free_list, and its `data' member and its sdata's `string'
1043 pointer is set to null. The size of the string is recorded in the
1044 `u.nbytes' member of the sdata. So, sdata structures that are no
1045 longer used, can be easily recognized, and it's easy to compact the
1046 sblocks of small strings which we do in compact_small_strings. */
1048 /* Size in bytes of an sblock structure used for small strings. This
1049 is 8192 minus malloc overhead. */
1051 #define SBLOCK_SIZE 8188
1053 /* Strings larger than this are considered large strings. String data
1054 for large strings is allocated from individual sblocks. */
1056 #define LARGE_STRING_BYTES 1024
1058 /* Structure describing string memory sub-allocated from an sblock.
1059 This is where the contents of Lisp strings are stored. */
1063 /* Back-pointer to the string this sdata belongs to. If null, this
1064 structure is free, and the NBYTES member of the union below
1065 contains the string's byte size (the same value that STRING_BYTES
1066 would return if STRING were non-null). If non-null, STRING_BYTES
1067 (STRING) is the size of the data, and DATA contains the string's
1069 struct Lisp_String
*string
;
1071 #ifdef GC_CHECK_STRING_BYTES
1074 unsigned char data
[1];
1076 #define SDATA_NBYTES(S) (S)->nbytes
1077 #define SDATA_DATA(S) (S)->data
1079 #else /* not GC_CHECK_STRING_BYTES */
1083 /* When STRING in non-null. */
1084 unsigned char data
[1];
1086 /* When STRING is null. */
1091 #define SDATA_NBYTES(S) (S)->u.nbytes
1092 #define SDATA_DATA(S) (S)->u.data
1094 #endif /* not GC_CHECK_STRING_BYTES */
1098 /* Structure describing a block of memory which is sub-allocated to
1099 obtain string data memory for strings. Blocks for small strings
1100 are of fixed size SBLOCK_SIZE. Blocks for large strings are made
1101 as large as needed. */
1106 struct sblock
*next
;
1108 /* Pointer to the next free sdata block. This points past the end
1109 of the sblock if there isn't any space left in this block. */
1110 struct sdata
*next_free
;
1112 /* Start of data. */
1113 struct sdata first_data
;
1116 /* Number of Lisp strings in a string_block structure. The 1020 is
1117 1024 minus malloc overhead. */
1119 #define STRINGS_IN_STRING_BLOCK \
1120 ((1020 - sizeof (struct string_block *)) / sizeof (struct Lisp_String))
1122 /* Structure describing a block from which Lisp_String structures
1127 struct string_block
*next
;
1128 struct Lisp_String strings
[STRINGS_IN_STRING_BLOCK
];
1131 /* Head and tail of the list of sblock structures holding Lisp string
1132 data. We always allocate from current_sblock. The NEXT pointers
1133 in the sblock structures go from oldest_sblock to current_sblock. */
1135 static struct sblock
*oldest_sblock
, *current_sblock
;
1137 /* List of sblocks for large strings. */
1139 static struct sblock
*large_sblocks
;
1141 /* List of string_block structures, and how many there are. */
1143 static struct string_block
*string_blocks
;
1144 static int n_string_blocks
;
1146 /* Free-list of Lisp_Strings. */
1148 static struct Lisp_String
*string_free_list
;
1150 /* Number of live and free Lisp_Strings. */
1152 static int total_strings
, total_free_strings
;
1154 /* Number of bytes used by live strings. */
1156 static int total_string_size
;
1158 /* Given a pointer to a Lisp_String S which is on the free-list
1159 string_free_list, return a pointer to its successor in the
1162 #define NEXT_FREE_LISP_STRING(S) (*(struct Lisp_String **) (S))
1164 /* Return a pointer to the sdata structure belonging to Lisp string S.
1165 S must be live, i.e. S->data must not be null. S->data is actually
1166 a pointer to the `u.data' member of its sdata structure; the
1167 structure starts at a constant offset in front of that. */
1169 #ifdef GC_CHECK_STRING_BYTES
1171 #define SDATA_OF_STRING(S) \
1172 ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *) \
1173 - sizeof (EMACS_INT)))
1175 #else /* not GC_CHECK_STRING_BYTES */
1177 #define SDATA_OF_STRING(S) \
1178 ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *)))
1180 #endif /* not GC_CHECK_STRING_BYTES */
1182 /* Value is the size of an sdata structure large enough to hold NBYTES
1183 bytes of string data. The value returned includes a terminating
1184 NUL byte, the size of the sdata structure, and padding. */
1186 #ifdef GC_CHECK_STRING_BYTES
1188 #define SDATA_SIZE(NBYTES) \
1189 ((sizeof (struct Lisp_String *) \
1191 + sizeof (EMACS_INT) \
1192 + sizeof (EMACS_INT) - 1) \
1193 & ~(sizeof (EMACS_INT) - 1))
1195 #else /* not GC_CHECK_STRING_BYTES */
1197 #define SDATA_SIZE(NBYTES) \
1198 ((sizeof (struct Lisp_String *) \
1200 + sizeof (EMACS_INT) - 1) \
1201 & ~(sizeof (EMACS_INT) - 1))
1203 #endif /* not GC_CHECK_STRING_BYTES */
1205 /* Initialize string allocation. Called from init_alloc_once. */
1210 total_strings
= total_free_strings
= total_string_size
= 0;
1211 oldest_sblock
= current_sblock
= large_sblocks
= NULL
;
1212 string_blocks
= NULL
;
1213 n_string_blocks
= 0;
1214 string_free_list
= NULL
;
1218 #ifdef GC_CHECK_STRING_BYTES
1220 static int check_string_bytes_count
;
1222 void check_string_bytes
P_ ((int));
1223 void check_sblock
P_ ((struct sblock
*));
1225 #define CHECK_STRING_BYTES(S) STRING_BYTES (S)
1228 /* Like GC_STRING_BYTES, but with debugging check. */
1232 struct Lisp_String
*s
;
1234 int nbytes
= (s
->size_byte
< 0 ? s
->size
: s
->size_byte
) & ~MARKBIT
;
1235 if (!PURE_POINTER_P (s
)
1237 && nbytes
!= SDATA_NBYTES (SDATA_OF_STRING (s
)))
1242 /* Check validity of Lisp strings' string_bytes member in B. */
1248 struct sdata
*from
, *end
, *from_end
;
1252 for (from
= &b
->first_data
; from
< end
; from
= from_end
)
1254 /* Compute the next FROM here because copying below may
1255 overwrite data we need to compute it. */
1258 /* Check that the string size recorded in the string is the
1259 same as the one recorded in the sdata structure. */
1261 CHECK_STRING_BYTES (from
->string
);
1264 nbytes
= GC_STRING_BYTES (from
->string
);
1266 nbytes
= SDATA_NBYTES (from
);
1268 nbytes
= SDATA_SIZE (nbytes
);
1269 from_end
= (struct sdata
*) ((char *) from
+ nbytes
);
1274 /* Check validity of Lisp strings' string_bytes member. ALL_P
1275 non-zero means check all strings, otherwise check only most
1276 recently allocated strings. Used for hunting a bug. */
1279 check_string_bytes (all_p
)
1286 for (b
= large_sblocks
; b
; b
= b
->next
)
1288 struct Lisp_String
*s
= b
->first_data
.string
;
1290 CHECK_STRING_BYTES (s
);
1293 for (b
= oldest_sblock
; b
; b
= b
->next
)
1297 check_sblock (current_sblock
);
1300 #endif /* GC_CHECK_STRING_BYTES */
1303 /* Return a new Lisp_String. */
1305 static struct Lisp_String
*
1308 struct Lisp_String
*s
;
1310 /* If the free-list is empty, allocate a new string_block, and
1311 add all the Lisp_Strings in it to the free-list. */
1312 if (string_free_list
== NULL
)
1314 struct string_block
*b
;
1317 b
= (struct string_block
*) lisp_malloc (sizeof *b
, MEM_TYPE_STRING
);
1318 VALIDATE_LISP_STORAGE (b
, sizeof *b
);
1319 bzero (b
, sizeof *b
);
1320 b
->next
= string_blocks
;
1324 for (i
= STRINGS_IN_STRING_BLOCK
- 1; i
>= 0; --i
)
1327 NEXT_FREE_LISP_STRING (s
) = string_free_list
;
1328 string_free_list
= s
;
1331 total_free_strings
+= STRINGS_IN_STRING_BLOCK
;
1334 /* Pop a Lisp_String off the free-list. */
1335 s
= string_free_list
;
1336 string_free_list
= NEXT_FREE_LISP_STRING (s
);
1338 /* Probably not strictly necessary, but play it safe. */
1339 bzero (s
, sizeof *s
);
1341 --total_free_strings
;
1344 consing_since_gc
+= sizeof *s
;
1346 #ifdef GC_CHECK_STRING_BYTES
1353 if (++check_string_bytes_count
== 200)
1355 check_string_bytes_count
= 0;
1356 check_string_bytes (1);
1359 check_string_bytes (0);
1361 #endif /* GC_CHECK_STRING_BYTES */
1367 /* Set up Lisp_String S for holding NCHARS characters, NBYTES bytes,
1368 plus a NUL byte at the end. Allocate an sdata structure for S, and
1369 set S->data to its `u.data' member. Store a NUL byte at the end of
1370 S->data. Set S->size to NCHARS and S->size_byte to NBYTES. Free
1371 S->data if it was initially non-null. */
1374 allocate_string_data (s
, nchars
, nbytes
)
1375 struct Lisp_String
*s
;
1378 struct sdata
*data
, *old_data
;
1380 int needed
, old_nbytes
;
1382 /* Determine the number of bytes needed to store NBYTES bytes
1384 needed
= SDATA_SIZE (nbytes
);
1386 if (nbytes
> LARGE_STRING_BYTES
)
1388 size_t size
= sizeof *b
- sizeof (struct sdata
) + needed
;
1390 #ifdef DOUG_LEA_MALLOC
1391 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed
1392 because mapped region contents are not preserved in
1394 mallopt (M_MMAP_MAX
, 0);
1397 b
= (struct sblock
*) lisp_malloc (size
, MEM_TYPE_NON_LISP
);
1399 #ifdef DOUG_LEA_MALLOC
1400 /* Back to a reasonable maximum of mmap'ed areas. */
1401 mallopt (M_MMAP_MAX
, MMAP_MAX_AREAS
);
1404 b
->next_free
= &b
->first_data
;
1405 b
->first_data
.string
= NULL
;
1406 b
->next
= large_sblocks
;
1409 else if (current_sblock
== NULL
1410 || (((char *) current_sblock
+ SBLOCK_SIZE
1411 - (char *) current_sblock
->next_free
)
1414 /* Not enough room in the current sblock. */
1415 b
= (struct sblock
*) lisp_malloc (SBLOCK_SIZE
, MEM_TYPE_NON_LISP
);
1416 b
->next_free
= &b
->first_data
;
1417 b
->first_data
.string
= NULL
;
1421 current_sblock
->next
= b
;
1429 old_data
= s
->data
? SDATA_OF_STRING (s
) : NULL
;
1430 old_nbytes
= GC_STRING_BYTES (s
);
1432 data
= b
->next_free
;
1434 s
->data
= SDATA_DATA (data
);
1435 #ifdef GC_CHECK_STRING_BYTES
1436 SDATA_NBYTES (data
) = nbytes
;
1439 s
->size_byte
= nbytes
;
1440 s
->data
[nbytes
] = '\0';
1441 b
->next_free
= (struct sdata
*) ((char *) data
+ needed
);
1443 /* If S had already data assigned, mark that as free by setting its
1444 string back-pointer to null, and recording the size of the data
1448 SDATA_NBYTES (old_data
) = old_nbytes
;
1449 old_data
->string
= NULL
;
1452 consing_since_gc
+= needed
;
1456 /* Sweep and compact strings. */
1461 struct string_block
*b
, *next
;
1462 struct string_block
*live_blocks
= NULL
;
1464 string_free_list
= NULL
;
1465 total_strings
= total_free_strings
= 0;
1466 total_string_size
= 0;
1468 /* Scan strings_blocks, free Lisp_Strings that aren't marked. */
1469 for (b
= string_blocks
; b
; b
= next
)
1472 struct Lisp_String
*free_list_before
= string_free_list
;
1476 for (i
= 0; i
< STRINGS_IN_STRING_BLOCK
; ++i
)
1478 struct Lisp_String
*s
= b
->strings
+ i
;
1482 /* String was not on free-list before. */
1483 if (STRING_MARKED_P (s
))
1485 /* String is live; unmark it and its intervals. */
1488 if (!NULL_INTERVAL_P (s
->intervals
))
1489 UNMARK_BALANCE_INTERVALS (s
->intervals
);
1492 total_string_size
+= STRING_BYTES (s
);
1496 /* String is dead. Put it on the free-list. */
1497 struct sdata
*data
= SDATA_OF_STRING (s
);
1499 /* Save the size of S in its sdata so that we know
1500 how large that is. Reset the sdata's string
1501 back-pointer so that we know it's free. */
1502 #ifdef GC_CHECK_STRING_BYTES
1503 if (GC_STRING_BYTES (s
) != SDATA_NBYTES (data
))
1506 data
->u
.nbytes
= GC_STRING_BYTES (s
);
1508 data
->string
= NULL
;
1510 /* Reset the strings's `data' member so that we
1514 /* Put the string on the free-list. */
1515 NEXT_FREE_LISP_STRING (s
) = string_free_list
;
1516 string_free_list
= s
;
1522 /* S was on the free-list before. Put it there again. */
1523 NEXT_FREE_LISP_STRING (s
) = string_free_list
;
1524 string_free_list
= s
;
1529 /* Free blocks that contain free Lisp_Strings only, except
1530 the first two of them. */
1531 if (nfree
== STRINGS_IN_STRING_BLOCK
1532 && total_free_strings
> STRINGS_IN_STRING_BLOCK
)
1536 string_free_list
= free_list_before
;
1540 total_free_strings
+= nfree
;
1541 b
->next
= live_blocks
;
1546 string_blocks
= live_blocks
;
1547 free_large_strings ();
1548 compact_small_strings ();
1552 /* Free dead large strings. */
1555 free_large_strings ()
1557 struct sblock
*b
, *next
;
1558 struct sblock
*live_blocks
= NULL
;
1560 for (b
= large_sblocks
; b
; b
= next
)
1564 if (b
->first_data
.string
== NULL
)
1568 b
->next
= live_blocks
;
1573 large_sblocks
= live_blocks
;
1577 /* Compact data of small strings. Free sblocks that don't contain
1578 data of live strings after compaction. */
1581 compact_small_strings ()
1583 struct sblock
*b
, *tb
, *next
;
1584 struct sdata
*from
, *to
, *end
, *tb_end
;
1585 struct sdata
*to_end
, *from_end
;
1587 /* TB is the sblock we copy to, TO is the sdata within TB we copy
1588 to, and TB_END is the end of TB. */
1590 tb_end
= (struct sdata
*) ((char *) tb
+ SBLOCK_SIZE
);
1591 to
= &tb
->first_data
;
1593 /* Step through the blocks from the oldest to the youngest. We
1594 expect that old blocks will stabilize over time, so that less
1595 copying will happen this way. */
1596 for (b
= oldest_sblock
; b
; b
= b
->next
)
1599 xassert ((char *) end
<= (char *) b
+ SBLOCK_SIZE
);
1601 for (from
= &b
->first_data
; from
< end
; from
= from_end
)
1603 /* Compute the next FROM here because copying below may
1604 overwrite data we need to compute it. */
1607 #ifdef GC_CHECK_STRING_BYTES
1608 /* Check that the string size recorded in the string is the
1609 same as the one recorded in the sdata structure. */
1611 && GC_STRING_BYTES (from
->string
) != SDATA_NBYTES (from
))
1613 #endif /* GC_CHECK_STRING_BYTES */
1616 nbytes
= GC_STRING_BYTES (from
->string
);
1618 nbytes
= SDATA_NBYTES (from
);
1620 nbytes
= SDATA_SIZE (nbytes
);
1621 from_end
= (struct sdata
*) ((char *) from
+ nbytes
);
1623 /* FROM->string non-null means it's alive. Copy its data. */
1626 /* If TB is full, proceed with the next sblock. */
1627 to_end
= (struct sdata
*) ((char *) to
+ nbytes
);
1628 if (to_end
> tb_end
)
1632 tb_end
= (struct sdata
*) ((char *) tb
+ SBLOCK_SIZE
);
1633 to
= &tb
->first_data
;
1634 to_end
= (struct sdata
*) ((char *) to
+ nbytes
);
1637 /* Copy, and update the string's `data' pointer. */
1640 xassert (tb
!= b
|| to
<= from
);
1641 safe_bcopy ((char *) from
, (char *) to
, nbytes
);
1642 to
->string
->data
= SDATA_DATA (to
);
1645 /* Advance past the sdata we copied to. */
1651 /* The rest of the sblocks following TB don't contain live data, so
1652 we can free them. */
1653 for (b
= tb
->next
; b
; b
= next
)
1661 current_sblock
= tb
;
1665 DEFUN ("make-string", Fmake_string
, Smake_string
, 2, 2, 0,
1666 doc
: /* Return a newly created string of length LENGTH, with each element being INIT.
1667 Both LENGTH and INIT must be numbers. */)
1669 Lisp_Object length
, init
;
1671 register Lisp_Object val
;
1672 register unsigned char *p
, *end
;
1675 CHECK_NATNUM (length
);
1676 CHECK_NUMBER (init
);
1679 if (SINGLE_BYTE_CHAR_P (c
))
1681 nbytes
= XINT (length
);
1682 val
= make_uninit_string (nbytes
);
1684 end
= p
+ SCHARS (val
);
1690 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
1691 int len
= CHAR_STRING (c
, str
);
1693 nbytes
= len
* XINT (length
);
1694 val
= make_uninit_multibyte_string (XINT (length
), nbytes
);
1699 bcopy (str
, p
, len
);
1709 DEFUN ("make-bool-vector", Fmake_bool_vector
, Smake_bool_vector
, 2, 2, 0,
1710 doc
: /* Return a new bool-vector of length LENGTH, using INIT for as each element.
1711 LENGTH must be a number. INIT matters only in whether it is t or nil. */)
1713 Lisp_Object length
, init
;
1715 register Lisp_Object val
;
1716 struct Lisp_Bool_Vector
*p
;
1718 int length_in_chars
, length_in_elts
, bits_per_value
;
1720 CHECK_NATNUM (length
);
1722 bits_per_value
= sizeof (EMACS_INT
) * BITS_PER_CHAR
;
1724 length_in_elts
= (XFASTINT (length
) + bits_per_value
- 1) / bits_per_value
;
1725 length_in_chars
= ((XFASTINT (length
) + BITS_PER_CHAR
- 1) / BITS_PER_CHAR
);
1727 /* We must allocate one more elements than LENGTH_IN_ELTS for the
1728 slot `size' of the struct Lisp_Bool_Vector. */
1729 val
= Fmake_vector (make_number (length_in_elts
+ 1), Qnil
);
1730 p
= XBOOL_VECTOR (val
);
1732 /* Get rid of any bits that would cause confusion. */
1734 XSETBOOL_VECTOR (val
, p
);
1735 p
->size
= XFASTINT (length
);
1737 real_init
= (NILP (init
) ? 0 : -1);
1738 for (i
= 0; i
< length_in_chars
; i
++)
1739 p
->data
[i
] = real_init
;
1741 /* Clear the extraneous bits in the last byte. */
1742 if (XINT (length
) != length_in_chars
* BITS_PER_CHAR
)
1743 XBOOL_VECTOR (val
)->data
[length_in_chars
- 1]
1744 &= (1 << (XINT (length
) % BITS_PER_CHAR
)) - 1;
1750 /* Make a string from NBYTES bytes at CONTENTS, and compute the number
1751 of characters from the contents. This string may be unibyte or
1752 multibyte, depending on the contents. */
1755 make_string (contents
, nbytes
)
1756 const char *contents
;
1759 register Lisp_Object val
;
1760 int nchars
, multibyte_nbytes
;
1762 parse_str_as_multibyte (contents
, nbytes
, &nchars
, &multibyte_nbytes
);
1763 if (nbytes
== nchars
|| nbytes
!= multibyte_nbytes
)
1764 /* CONTENTS contains no multibyte sequences or contains an invalid
1765 multibyte sequence. We must make unibyte string. */
1766 val
= make_unibyte_string (contents
, nbytes
);
1768 val
= make_multibyte_string (contents
, nchars
, nbytes
);
1773 /* Make an unibyte string from LENGTH bytes at CONTENTS. */
1776 make_unibyte_string (contents
, length
)
1777 const char *contents
;
1780 register Lisp_Object val
;
1781 val
= make_uninit_string (length
);
1782 bcopy (contents
, SDATA (val
), length
);
1783 STRING_SET_UNIBYTE (val
);
1788 /* Make a multibyte string from NCHARS characters occupying NBYTES
1789 bytes at CONTENTS. */
1792 make_multibyte_string (contents
, nchars
, nbytes
)
1793 const char *contents
;
1796 register Lisp_Object val
;
1797 val
= make_uninit_multibyte_string (nchars
, nbytes
);
1798 bcopy (contents
, SDATA (val
), nbytes
);
1803 /* Make a string from NCHARS characters occupying NBYTES bytes at
1804 CONTENTS. It is a multibyte string if NBYTES != NCHARS. */
1807 make_string_from_bytes (contents
, nchars
, nbytes
)
1811 register Lisp_Object val
;
1812 val
= make_uninit_multibyte_string (nchars
, nbytes
);
1813 bcopy (contents
, SDATA (val
), nbytes
);
1814 if (SBYTES (val
) == SCHARS (val
))
1815 STRING_SET_UNIBYTE (val
);
1820 /* Make a string from NCHARS characters occupying NBYTES bytes at
1821 CONTENTS. The argument MULTIBYTE controls whether to label the
1822 string as multibyte. */
1825 make_specified_string (contents
, nchars
, nbytes
, multibyte
)
1830 register Lisp_Object val
;
1831 val
= make_uninit_multibyte_string (nchars
, nbytes
);
1832 bcopy (contents
, SDATA (val
), nbytes
);
1834 STRING_SET_UNIBYTE (val
);
1839 /* Make a string from the data at STR, treating it as multibyte if the
1846 return make_string (str
, strlen (str
));
1850 /* Return an unibyte Lisp_String set up to hold LENGTH characters
1851 occupying LENGTH bytes. */
1854 make_uninit_string (length
)
1858 val
= make_uninit_multibyte_string (length
, length
);
1859 STRING_SET_UNIBYTE (val
);
1864 /* Return a multibyte Lisp_String set up to hold NCHARS characters
1865 which occupy NBYTES bytes. */
1868 make_uninit_multibyte_string (nchars
, nbytes
)
1872 struct Lisp_String
*s
;
1877 s
= allocate_string ();
1878 allocate_string_data (s
, nchars
, nbytes
);
1879 XSETSTRING (string
, s
);
1880 string_chars_consed
+= nbytes
;
1886 /***********************************************************************
1888 ***********************************************************************/
1890 /* We store float cells inside of float_blocks, allocating a new
1891 float_block with malloc whenever necessary. Float cells reclaimed
1892 by GC are put on a free list to be reallocated before allocating
1893 any new float cells from the latest float_block.
1895 Each float_block is just under 1020 bytes long, since malloc really
1896 allocates in units of powers of two and uses 4 bytes for its own
1899 #define FLOAT_BLOCK_SIZE \
1900 ((1020 - sizeof (struct float_block *)) / sizeof (struct Lisp_Float))
1904 struct float_block
*next
;
1905 struct Lisp_Float floats
[FLOAT_BLOCK_SIZE
];
1908 /* Current float_block. */
1910 struct float_block
*float_block
;
1912 /* Index of first unused Lisp_Float in the current float_block. */
1914 int float_block_index
;
1916 /* Total number of float blocks now in use. */
1920 /* Free-list of Lisp_Floats. */
1922 struct Lisp_Float
*float_free_list
;
1925 /* Initialize float allocation. */
1930 float_block
= (struct float_block
*) lisp_malloc (sizeof *float_block
,
1932 float_block
->next
= 0;
1933 bzero ((char *) float_block
->floats
, sizeof float_block
->floats
);
1934 float_block_index
= 0;
1935 float_free_list
= 0;
1940 /* Explicitly free a float cell by putting it on the free-list. */
1944 struct Lisp_Float
*ptr
;
1946 *(struct Lisp_Float
**)&ptr
->data
= float_free_list
;
1950 float_free_list
= ptr
;
1954 /* Return a new float object with value FLOAT_VALUE. */
1957 make_float (float_value
)
1960 register Lisp_Object val
;
1962 if (float_free_list
)
1964 /* We use the data field for chaining the free list
1965 so that we won't use the same field that has the mark bit. */
1966 XSETFLOAT (val
, float_free_list
);
1967 float_free_list
= *(struct Lisp_Float
**)&float_free_list
->data
;
1971 if (float_block_index
== FLOAT_BLOCK_SIZE
)
1973 register struct float_block
*new;
1975 new = (struct float_block
*) lisp_malloc (sizeof *new,
1977 VALIDATE_LISP_STORAGE (new, sizeof *new);
1978 new->next
= float_block
;
1980 float_block_index
= 0;
1983 XSETFLOAT (val
, &float_block
->floats
[float_block_index
++]);
1986 XFLOAT_DATA (val
) = float_value
;
1987 XSETFASTINT (XFLOAT (val
)->type
, 0); /* bug chasing -wsr */
1988 consing_since_gc
+= sizeof (struct Lisp_Float
);
1995 /***********************************************************************
1997 ***********************************************************************/
1999 /* We store cons cells inside of cons_blocks, allocating a new
2000 cons_block with malloc whenever necessary. Cons cells reclaimed by
2001 GC are put on a free list to be reallocated before allocating
2002 any new cons cells from the latest cons_block.
2004 Each cons_block is just under 1020 bytes long,
2005 since malloc really allocates in units of powers of two
2006 and uses 4 bytes for its own overhead. */
2008 #define CONS_BLOCK_SIZE \
2009 ((1020 - sizeof (struct cons_block *)) / sizeof (struct Lisp_Cons))
2013 struct cons_block
*next
;
2014 struct Lisp_Cons conses
[CONS_BLOCK_SIZE
];
2017 /* Current cons_block. */
2019 struct cons_block
*cons_block
;
2021 /* Index of first unused Lisp_Cons in the current block. */
2023 int cons_block_index
;
2025 /* Free-list of Lisp_Cons structures. */
2027 struct Lisp_Cons
*cons_free_list
;
2029 /* Total number of cons blocks now in use. */
2034 /* Initialize cons allocation. */
2039 cons_block
= (struct cons_block
*) lisp_malloc (sizeof *cons_block
,
2041 cons_block
->next
= 0;
2042 bzero ((char *) cons_block
->conses
, sizeof cons_block
->conses
);
2043 cons_block_index
= 0;
2049 /* Explicitly free a cons cell by putting it on the free-list. */
2053 struct Lisp_Cons
*ptr
;
2055 *(struct Lisp_Cons
**)&ptr
->cdr
= cons_free_list
;
2059 cons_free_list
= ptr
;
2063 DEFUN ("cons", Fcons
, Scons
, 2, 2, 0,
2064 doc
: /* Create a new cons, give it CAR and CDR as components, and return it. */)
2066 Lisp_Object car
, cdr
;
2068 register Lisp_Object val
;
2072 /* We use the cdr for chaining the free list
2073 so that we won't use the same field that has the mark bit. */
2074 XSETCONS (val
, cons_free_list
);
2075 cons_free_list
= *(struct Lisp_Cons
**)&cons_free_list
->cdr
;
2079 if (cons_block_index
== CONS_BLOCK_SIZE
)
2081 register struct cons_block
*new;
2082 new = (struct cons_block
*) lisp_malloc (sizeof *new,
2084 VALIDATE_LISP_STORAGE (new, sizeof *new);
2085 new->next
= cons_block
;
2087 cons_block_index
= 0;
2090 XSETCONS (val
, &cons_block
->conses
[cons_block_index
++]);
2095 consing_since_gc
+= sizeof (struct Lisp_Cons
);
2096 cons_cells_consed
++;
2101 /* Make a list of 2, 3, 4 or 5 specified objects. */
2105 Lisp_Object arg1
, arg2
;
2107 return Fcons (arg1
, Fcons (arg2
, Qnil
));
2112 list3 (arg1
, arg2
, arg3
)
2113 Lisp_Object arg1
, arg2
, arg3
;
2115 return Fcons (arg1
, Fcons (arg2
, Fcons (arg3
, Qnil
)));
2120 list4 (arg1
, arg2
, arg3
, arg4
)
2121 Lisp_Object arg1
, arg2
, arg3
, arg4
;
2123 return Fcons (arg1
, Fcons (arg2
, Fcons (arg3
, Fcons (arg4
, Qnil
))));
2128 list5 (arg1
, arg2
, arg3
, arg4
, arg5
)
2129 Lisp_Object arg1
, arg2
, arg3
, arg4
, arg5
;
2131 return Fcons (arg1
, Fcons (arg2
, Fcons (arg3
, Fcons (arg4
,
2132 Fcons (arg5
, Qnil
)))));
2136 DEFUN ("list", Flist
, Slist
, 0, MANY
, 0,
2137 doc
: /* Return a newly created list with specified arguments as elements.
2138 Any number of arguments, even zero arguments, are allowed.
2139 usage: (list &rest OBJECTS) */)
2142 register Lisp_Object
*args
;
2144 register Lisp_Object val
;
2150 val
= Fcons (args
[nargs
], val
);
2156 DEFUN ("make-list", Fmake_list
, Smake_list
, 2, 2, 0,
2157 doc
: /* Return a newly created list of length LENGTH, with each element being INIT. */)
2159 register Lisp_Object length
, init
;
2161 register Lisp_Object val
;
2164 CHECK_NATNUM (length
);
2165 size
= XFASTINT (length
);
2170 val
= Fcons (init
, val
);
2175 val
= Fcons (init
, val
);
2180 val
= Fcons (init
, val
);
2185 val
= Fcons (init
, val
);
2190 val
= Fcons (init
, val
);
2205 /***********************************************************************
2207 ***********************************************************************/
2209 /* Singly-linked list of all vectors. */
2211 struct Lisp_Vector
*all_vectors
;
2213 /* Total number of vector-like objects now in use. */
2218 /* Value is a pointer to a newly allocated Lisp_Vector structure
2219 with room for LEN Lisp_Objects. */
2221 static struct Lisp_Vector
*
2222 allocate_vectorlike (len
, type
)
2226 struct Lisp_Vector
*p
;
2229 #ifdef DOUG_LEA_MALLOC
2230 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed
2231 because mapped region contents are not preserved in
2233 mallopt (M_MMAP_MAX
, 0);
2236 nbytes
= sizeof *p
+ (len
- 1) * sizeof p
->contents
[0];
2237 p
= (struct Lisp_Vector
*) lisp_malloc (nbytes
, type
);
2239 #ifdef DOUG_LEA_MALLOC
2240 /* Back to a reasonable maximum of mmap'ed areas. */
2241 mallopt (M_MMAP_MAX
, MMAP_MAX_AREAS
);
2244 VALIDATE_LISP_STORAGE (p
, 0);
2245 consing_since_gc
+= nbytes
;
2246 vector_cells_consed
+= len
;
2248 p
->next
= all_vectors
;
2255 /* Allocate a vector with NSLOTS slots. */
2257 struct Lisp_Vector
*
2258 allocate_vector (nslots
)
2261 struct Lisp_Vector
*v
= allocate_vectorlike (nslots
, MEM_TYPE_VECTOR
);
2267 /* Allocate other vector-like structures. */
2269 struct Lisp_Hash_Table
*
2270 allocate_hash_table ()
2272 EMACS_INT len
= VECSIZE (struct Lisp_Hash_Table
);
2273 struct Lisp_Vector
*v
= allocate_vectorlike (len
, MEM_TYPE_HASH_TABLE
);
2277 for (i
= 0; i
< len
; ++i
)
2278 v
->contents
[i
] = Qnil
;
2280 return (struct Lisp_Hash_Table
*) v
;
2287 EMACS_INT len
= VECSIZE (struct window
);
2288 struct Lisp_Vector
*v
= allocate_vectorlike (len
, MEM_TYPE_WINDOW
);
2291 for (i
= 0; i
< len
; ++i
)
2292 v
->contents
[i
] = Qnil
;
2295 return (struct window
*) v
;
2302 EMACS_INT len
= VECSIZE (struct frame
);
2303 struct Lisp_Vector
*v
= allocate_vectorlike (len
, MEM_TYPE_FRAME
);
2306 for (i
= 0; i
< len
; ++i
)
2307 v
->contents
[i
] = make_number (0);
2309 return (struct frame
*) v
;
2313 struct Lisp_Process
*
2316 EMACS_INT len
= VECSIZE (struct Lisp_Process
);
2317 struct Lisp_Vector
*v
= allocate_vectorlike (len
, MEM_TYPE_PROCESS
);
2320 for (i
= 0; i
< len
; ++i
)
2321 v
->contents
[i
] = Qnil
;
2324 return (struct Lisp_Process
*) v
;
2328 struct Lisp_Vector
*
2329 allocate_other_vector (len
)
2332 struct Lisp_Vector
*v
= allocate_vectorlike (len
, MEM_TYPE_VECTOR
);
2335 for (i
= 0; i
< len
; ++i
)
2336 v
->contents
[i
] = Qnil
;
2343 DEFUN ("make-vector", Fmake_vector
, Smake_vector
, 2, 2, 0,
2344 doc
: /* Return a newly created vector of length LENGTH, with each element being INIT.
2345 See also the function `vector'. */)
2347 register Lisp_Object length
, init
;
2350 register EMACS_INT sizei
;
2352 register struct Lisp_Vector
*p
;
2354 CHECK_NATNUM (length
);
2355 sizei
= XFASTINT (length
);
2357 p
= allocate_vector (sizei
);
2358 for (index
= 0; index
< sizei
; index
++)
2359 p
->contents
[index
] = init
;
2361 XSETVECTOR (vector
, p
);
2366 DEFUN ("make-char-table", Fmake_char_table
, Smake_char_table
, 1, 2, 0,
2367 doc
: /* Return a newly created char-table, with purpose PURPOSE.
2368 Each element is initialized to INIT, which defaults to nil.
2369 PURPOSE should be a symbol which has a `char-table-extra-slots' property.
2370 The property's value should be an integer between 0 and 10. */)
2372 register Lisp_Object purpose
, init
;
2376 CHECK_SYMBOL (purpose
);
2377 n
= Fget (purpose
, Qchar_table_extra_slots
);
2379 if (XINT (n
) < 0 || XINT (n
) > 10)
2380 args_out_of_range (n
, Qnil
);
2381 /* Add 2 to the size for the defalt and parent slots. */
2382 vector
= Fmake_vector (make_number (CHAR_TABLE_STANDARD_SLOTS
+ XINT (n
)),
2384 XCHAR_TABLE (vector
)->top
= Qt
;
2385 XCHAR_TABLE (vector
)->parent
= Qnil
;
2386 XCHAR_TABLE (vector
)->purpose
= purpose
;
2387 XSETCHAR_TABLE (vector
, XCHAR_TABLE (vector
));
2392 /* Return a newly created sub char table with default value DEFALT.
2393 Since a sub char table does not appear as a top level Emacs Lisp
2394 object, we don't need a Lisp interface to make it. */
2397 make_sub_char_table (defalt
)
2401 = Fmake_vector (make_number (SUB_CHAR_TABLE_STANDARD_SLOTS
), Qnil
);
2402 XCHAR_TABLE (vector
)->top
= Qnil
;
2403 XCHAR_TABLE (vector
)->defalt
= defalt
;
2404 XSETCHAR_TABLE (vector
, XCHAR_TABLE (vector
));
2409 DEFUN ("vector", Fvector
, Svector
, 0, MANY
, 0,
2410 doc
: /* Return a newly created vector with specified arguments as elements.
2411 Any number of arguments, even zero arguments, are allowed.
2412 usage: (vector &rest OBJECTS) */)
2417 register Lisp_Object len
, val
;
2419 register struct Lisp_Vector
*p
;
2421 XSETFASTINT (len
, nargs
);
2422 val
= Fmake_vector (len
, Qnil
);
2424 for (index
= 0; index
< nargs
; index
++)
2425 p
->contents
[index
] = args
[index
];
2430 DEFUN ("make-byte-code", Fmake_byte_code
, Smake_byte_code
, 4, MANY
, 0,
2431 doc
: /* Create a byte-code object with specified arguments as elements.
2432 The arguments should be the arglist, bytecode-string, constant vector,
2433 stack size, (optional) doc string, and (optional) interactive spec.
2434 The first four arguments are required; at most six have any
2436 usage: (make-byte-code &rest ELEMENTS) */)
2441 register Lisp_Object len
, val
;
2443 register struct Lisp_Vector
*p
;
2445 XSETFASTINT (len
, nargs
);
2446 if (!NILP (Vpurify_flag
))
2447 val
= make_pure_vector ((EMACS_INT
) nargs
);
2449 val
= Fmake_vector (len
, Qnil
);
2451 if (STRINGP (args
[1]) && STRING_MULTIBYTE (args
[1]))
2452 /* BYTECODE-STRING must have been produced by Emacs 20.2 or the
2453 earlier because they produced a raw 8-bit string for byte-code
2454 and now such a byte-code string is loaded as multibyte while
2455 raw 8-bit characters converted to multibyte form. Thus, now we
2456 must convert them back to the original unibyte form. */
2457 args
[1] = Fstring_as_unibyte (args
[1]);
2460 for (index
= 0; index
< nargs
; index
++)
2462 if (!NILP (Vpurify_flag
))
2463 args
[index
] = Fpurecopy (args
[index
]);
2464 p
->contents
[index
] = args
[index
];
2466 XSETCOMPILED (val
, p
);
2472 /***********************************************************************
2474 ***********************************************************************/
2476 /* Each symbol_block is just under 1020 bytes long, since malloc
2477 really allocates in units of powers of two and uses 4 bytes for its
2480 #define SYMBOL_BLOCK_SIZE \
2481 ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol))
2485 struct symbol_block
*next
;
2486 struct Lisp_Symbol symbols
[SYMBOL_BLOCK_SIZE
];
2489 /* Current symbol block and index of first unused Lisp_Symbol
2492 struct symbol_block
*symbol_block
;
2493 int symbol_block_index
;
2495 /* List of free symbols. */
2497 struct Lisp_Symbol
*symbol_free_list
;
2499 /* Total number of symbol blocks now in use. */
2501 int n_symbol_blocks
;
2504 /* Initialize symbol allocation. */
2509 symbol_block
= (struct symbol_block
*) lisp_malloc (sizeof *symbol_block
,
2511 symbol_block
->next
= 0;
2512 bzero ((char *) symbol_block
->symbols
, sizeof symbol_block
->symbols
);
2513 symbol_block_index
= 0;
2514 symbol_free_list
= 0;
2515 n_symbol_blocks
= 1;
2519 DEFUN ("make-symbol", Fmake_symbol
, Smake_symbol
, 1, 1, 0,
2520 doc
: /* Return a newly allocated uninterned symbol whose name is NAME.
2521 Its value and function definition are void, and its property list is nil. */)
2525 register Lisp_Object val
;
2526 register struct Lisp_Symbol
*p
;
2528 CHECK_STRING (name
);
2530 if (symbol_free_list
)
2532 XSETSYMBOL (val
, symbol_free_list
);
2533 symbol_free_list
= *(struct Lisp_Symbol
**)&symbol_free_list
->value
;
2537 if (symbol_block_index
== SYMBOL_BLOCK_SIZE
)
2539 struct symbol_block
*new;
2540 new = (struct symbol_block
*) lisp_malloc (sizeof *new,
2542 VALIDATE_LISP_STORAGE (new, sizeof *new);
2543 new->next
= symbol_block
;
2545 symbol_block_index
= 0;
2548 XSETSYMBOL (val
, &symbol_block
->symbols
[symbol_block_index
++]);
2554 p
->value
= Qunbound
;
2555 p
->function
= Qunbound
;
2557 p
->interned
= SYMBOL_UNINTERNED
;
2559 p
->indirect_variable
= 0;
2560 consing_since_gc
+= sizeof (struct Lisp_Symbol
);
2567 /***********************************************************************
2568 Marker (Misc) Allocation
2569 ***********************************************************************/
2571 /* Allocation of markers and other objects that share that structure.
2572 Works like allocation of conses. */
2574 #define MARKER_BLOCK_SIZE \
2575 ((1020 - sizeof (struct marker_block *)) / sizeof (union Lisp_Misc))
2579 struct marker_block
*next
;
2580 union Lisp_Misc markers
[MARKER_BLOCK_SIZE
];
2583 struct marker_block
*marker_block
;
2584 int marker_block_index
;
2586 union Lisp_Misc
*marker_free_list
;
2588 /* Total number of marker blocks now in use. */
2590 int n_marker_blocks
;
2595 marker_block
= (struct marker_block
*) lisp_malloc (sizeof *marker_block
,
2597 marker_block
->next
= 0;
2598 bzero ((char *) marker_block
->markers
, sizeof marker_block
->markers
);
2599 marker_block_index
= 0;
2600 marker_free_list
= 0;
2601 n_marker_blocks
= 1;
2604 /* Return a newly allocated Lisp_Misc object, with no substructure. */
2611 if (marker_free_list
)
2613 XSETMISC (val
, marker_free_list
);
2614 marker_free_list
= marker_free_list
->u_free
.chain
;
2618 if (marker_block_index
== MARKER_BLOCK_SIZE
)
2620 struct marker_block
*new;
2621 new = (struct marker_block
*) lisp_malloc (sizeof *new,
2623 VALIDATE_LISP_STORAGE (new, sizeof *new);
2624 new->next
= marker_block
;
2626 marker_block_index
= 0;
2629 XSETMISC (val
, &marker_block
->markers
[marker_block_index
++]);
2632 consing_since_gc
+= sizeof (union Lisp_Misc
);
2633 misc_objects_consed
++;
2637 /* Return a Lisp_Misc_Save_Value object containing POINTER and
2638 INTEGER. This is used to package C values to call record_unwind_protect.
2639 The unwind function can get the C values back using XSAVE_VALUE. */
2642 make_save_value (pointer
, integer
)
2646 register Lisp_Object val
;
2647 register struct Lisp_Save_Value
*p
;
2649 val
= allocate_misc ();
2650 XMISCTYPE (val
) = Lisp_Misc_Save_Value
;
2651 p
= XSAVE_VALUE (val
);
2652 p
->pointer
= pointer
;
2653 p
->integer
= integer
;
2657 DEFUN ("make-marker", Fmake_marker
, Smake_marker
, 0, 0, 0,
2658 doc
: /* Return a newly allocated marker which does not point at any place. */)
2661 register Lisp_Object val
;
2662 register struct Lisp_Marker
*p
;
2664 val
= allocate_misc ();
2665 XMISCTYPE (val
) = Lisp_Misc_Marker
;
2671 p
->insertion_type
= 0;
2675 /* Put MARKER back on the free list after using it temporarily. */
2678 free_marker (marker
)
2681 unchain_marker (marker
);
2683 XMISC (marker
)->u_marker
.type
= Lisp_Misc_Free
;
2684 XMISC (marker
)->u_free
.chain
= marker_free_list
;
2685 marker_free_list
= XMISC (marker
);
2687 total_free_markers
++;
2691 /* Return a newly created vector or string with specified arguments as
2692 elements. If all the arguments are characters that can fit
2693 in a string of events, make a string; otherwise, make a vector.
2695 Any number of arguments, even zero arguments, are allowed. */
2698 make_event_array (nargs
, args
)
2704 for (i
= 0; i
< nargs
; i
++)
2705 /* The things that fit in a string
2706 are characters that are in 0...127,
2707 after discarding the meta bit and all the bits above it. */
2708 if (!INTEGERP (args
[i
])
2709 || (XUINT (args
[i
]) & ~(-CHAR_META
)) >= 0200)
2710 return Fvector (nargs
, args
);
2712 /* Since the loop exited, we know that all the things in it are
2713 characters, so we can make a string. */
2717 result
= Fmake_string (make_number (nargs
), make_number (0));
2718 for (i
= 0; i
< nargs
; i
++)
2720 SSET (result
, i
, XINT (args
[i
]));
2721 /* Move the meta bit to the right place for a string char. */
2722 if (XINT (args
[i
]) & CHAR_META
)
2723 SSET (result
, i
, SREF (result
, i
) | 0x80);
2732 /************************************************************************
2734 ************************************************************************/
2736 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
2738 /* Conservative C stack marking requires a method to identify possibly
2739 live Lisp objects given a pointer value. We do this by keeping
2740 track of blocks of Lisp data that are allocated in a red-black tree
2741 (see also the comment of mem_node which is the type of nodes in
2742 that tree). Function lisp_malloc adds information for an allocated
2743 block to the red-black tree with calls to mem_insert, and function
2744 lisp_free removes it with mem_delete. Functions live_string_p etc
2745 call mem_find to lookup information about a given pointer in the
2746 tree, and use that to determine if the pointer points to a Lisp
2749 /* Initialize this part of alloc.c. */
2754 mem_z
.left
= mem_z
.right
= MEM_NIL
;
2755 mem_z
.parent
= NULL
;
2756 mem_z
.color
= MEM_BLACK
;
2757 mem_z
.start
= mem_z
.end
= NULL
;
2762 /* Value is a pointer to the mem_node containing START. Value is
2763 MEM_NIL if there is no node in the tree containing START. */
2765 static INLINE
struct mem_node
*
2771 if (start
< min_heap_address
|| start
> max_heap_address
)
2774 /* Make the search always successful to speed up the loop below. */
2775 mem_z
.start
= start
;
2776 mem_z
.end
= (char *) start
+ 1;
2779 while (start
< p
->start
|| start
>= p
->end
)
2780 p
= start
< p
->start
? p
->left
: p
->right
;
2785 /* Insert a new node into the tree for a block of memory with start
2786 address START, end address END, and type TYPE. Value is a
2787 pointer to the node that was inserted. */
2789 static struct mem_node
*
2790 mem_insert (start
, end
, type
)
2794 struct mem_node
*c
, *parent
, *x
;
2796 if (start
< min_heap_address
)
2797 min_heap_address
= start
;
2798 if (end
> max_heap_address
)
2799 max_heap_address
= end
;
2801 /* See where in the tree a node for START belongs. In this
2802 particular application, it shouldn't happen that a node is already
2803 present. For debugging purposes, let's check that. */
2807 #if GC_MARK_STACK != GC_MAKE_GCPROS_NOOPS
2809 while (c
!= MEM_NIL
)
2811 if (start
>= c
->start
&& start
< c
->end
)
2814 c
= start
< c
->start
? c
->left
: c
->right
;
2817 #else /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
2819 while (c
!= MEM_NIL
)
2822 c
= start
< c
->start
? c
->left
: c
->right
;
2825 #endif /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
2827 /* Create a new node. */
2828 #ifdef GC_MALLOC_CHECK
2829 x
= (struct mem_node
*) _malloc_internal (sizeof *x
);
2833 x
= (struct mem_node
*) xmalloc (sizeof *x
);
2839 x
->left
= x
->right
= MEM_NIL
;
2842 /* Insert it as child of PARENT or install it as root. */
2845 if (start
< parent
->start
)
2853 /* Re-establish red-black tree properties. */
2854 mem_insert_fixup (x
);
2860 /* Re-establish the red-black properties of the tree, and thereby
2861 balance the tree, after node X has been inserted; X is always red. */
2864 mem_insert_fixup (x
)
2867 while (x
!= mem_root
&& x
->parent
->color
== MEM_RED
)
2869 /* X is red and its parent is red. This is a violation of
2870 red-black tree property #3. */
2872 if (x
->parent
== x
->parent
->parent
->left
)
2874 /* We're on the left side of our grandparent, and Y is our
2876 struct mem_node
*y
= x
->parent
->parent
->right
;
2878 if (y
->color
== MEM_RED
)
2880 /* Uncle and parent are red but should be black because
2881 X is red. Change the colors accordingly and proceed
2882 with the grandparent. */
2883 x
->parent
->color
= MEM_BLACK
;
2884 y
->color
= MEM_BLACK
;
2885 x
->parent
->parent
->color
= MEM_RED
;
2886 x
= x
->parent
->parent
;
2890 /* Parent and uncle have different colors; parent is
2891 red, uncle is black. */
2892 if (x
== x
->parent
->right
)
2895 mem_rotate_left (x
);
2898 x
->parent
->color
= MEM_BLACK
;
2899 x
->parent
->parent
->color
= MEM_RED
;
2900 mem_rotate_right (x
->parent
->parent
);
2905 /* This is the symmetrical case of above. */
2906 struct mem_node
*y
= x
->parent
->parent
->left
;
2908 if (y
->color
== MEM_RED
)
2910 x
->parent
->color
= MEM_BLACK
;
2911 y
->color
= MEM_BLACK
;
2912 x
->parent
->parent
->color
= MEM_RED
;
2913 x
= x
->parent
->parent
;
2917 if (x
== x
->parent
->left
)
2920 mem_rotate_right (x
);
2923 x
->parent
->color
= MEM_BLACK
;
2924 x
->parent
->parent
->color
= MEM_RED
;
2925 mem_rotate_left (x
->parent
->parent
);
2930 /* The root may have been changed to red due to the algorithm. Set
2931 it to black so that property #5 is satisfied. */
2932 mem_root
->color
= MEM_BLACK
;
2948 /* Turn y's left sub-tree into x's right sub-tree. */
2951 if (y
->left
!= MEM_NIL
)
2952 y
->left
->parent
= x
;
2954 /* Y's parent was x's parent. */
2956 y
->parent
= x
->parent
;
2958 /* Get the parent to point to y instead of x. */
2961 if (x
== x
->parent
->left
)
2962 x
->parent
->left
= y
;
2964 x
->parent
->right
= y
;
2969 /* Put x on y's left. */
2983 mem_rotate_right (x
)
2986 struct mem_node
*y
= x
->left
;
2989 if (y
->right
!= MEM_NIL
)
2990 y
->right
->parent
= x
;
2993 y
->parent
= x
->parent
;
2996 if (x
== x
->parent
->right
)
2997 x
->parent
->right
= y
;
2999 x
->parent
->left
= y
;
3010 /* Delete node Z from the tree. If Z is null or MEM_NIL, do nothing. */
3016 struct mem_node
*x
, *y
;
3018 if (!z
|| z
== MEM_NIL
)
3021 if (z
->left
== MEM_NIL
|| z
->right
== MEM_NIL
)
3026 while (y
->left
!= MEM_NIL
)
3030 if (y
->left
!= MEM_NIL
)
3035 x
->parent
= y
->parent
;
3038 if (y
== y
->parent
->left
)
3039 y
->parent
->left
= x
;
3041 y
->parent
->right
= x
;
3048 z
->start
= y
->start
;
3053 if (y
->color
== MEM_BLACK
)
3054 mem_delete_fixup (x
);
3056 #ifdef GC_MALLOC_CHECK
3064 /* Re-establish the red-black properties of the tree, after a
3068 mem_delete_fixup (x
)
3071 while (x
!= mem_root
&& x
->color
== MEM_BLACK
)
3073 if (x
== x
->parent
->left
)
3075 struct mem_node
*w
= x
->parent
->right
;
3077 if (w
->color
== MEM_RED
)
3079 w
->color
= MEM_BLACK
;
3080 x
->parent
->color
= MEM_RED
;
3081 mem_rotate_left (x
->parent
);
3082 w
= x
->parent
->right
;
3085 if (w
->left
->color
== MEM_BLACK
&& w
->right
->color
== MEM_BLACK
)
3092 if (w
->right
->color
== MEM_BLACK
)
3094 w
->left
->color
= MEM_BLACK
;
3096 mem_rotate_right (w
);
3097 w
= x
->parent
->right
;
3099 w
->color
= x
->parent
->color
;
3100 x
->parent
->color
= MEM_BLACK
;
3101 w
->right
->color
= MEM_BLACK
;
3102 mem_rotate_left (x
->parent
);
3108 struct mem_node
*w
= x
->parent
->left
;
3110 if (w
->color
== MEM_RED
)
3112 w
->color
= MEM_BLACK
;
3113 x
->parent
->color
= MEM_RED
;
3114 mem_rotate_right (x
->parent
);
3115 w
= x
->parent
->left
;
3118 if (w
->right
->color
== MEM_BLACK
&& w
->left
->color
== MEM_BLACK
)
3125 if (w
->left
->color
== MEM_BLACK
)
3127 w
->right
->color
= MEM_BLACK
;
3129 mem_rotate_left (w
);
3130 w
= x
->parent
->left
;
3133 w
->color
= x
->parent
->color
;
3134 x
->parent
->color
= MEM_BLACK
;
3135 w
->left
->color
= MEM_BLACK
;
3136 mem_rotate_right (x
->parent
);
3142 x
->color
= MEM_BLACK
;
3146 /* Value is non-zero if P is a pointer to a live Lisp string on
3147 the heap. M is a pointer to the mem_block for P. */
3150 live_string_p (m
, p
)
3154 if (m
->type
== MEM_TYPE_STRING
)
3156 struct string_block
*b
= (struct string_block
*) m
->start
;
3157 int offset
= (char *) p
- (char *) &b
->strings
[0];
3159 /* P must point to the start of a Lisp_String structure, and it
3160 must not be on the free-list. */
3162 && offset
% sizeof b
->strings
[0] == 0
3163 && ((struct Lisp_String
*) p
)->data
!= NULL
);
3170 /* Value is non-zero if P is a pointer to a live Lisp cons on
3171 the heap. M is a pointer to the mem_block for P. */
3178 if (m
->type
== MEM_TYPE_CONS
)
3180 struct cons_block
*b
= (struct cons_block
*) m
->start
;
3181 int offset
= (char *) p
- (char *) &b
->conses
[0];
3183 /* P must point to the start of a Lisp_Cons, not be
3184 one of the unused cells in the current cons block,
3185 and not be on the free-list. */
3187 && offset
% sizeof b
->conses
[0] == 0
3189 || offset
/ sizeof b
->conses
[0] < cons_block_index
)
3190 && !EQ (((struct Lisp_Cons
*) p
)->car
, Vdead
));
3197 /* Value is non-zero if P is a pointer to a live Lisp symbol on
3198 the heap. M is a pointer to the mem_block for P. */
3201 live_symbol_p (m
, p
)
3205 if (m
->type
== MEM_TYPE_SYMBOL
)
3207 struct symbol_block
*b
= (struct symbol_block
*) m
->start
;
3208 int offset
= (char *) p
- (char *) &b
->symbols
[0];
3210 /* P must point to the start of a Lisp_Symbol, not be
3211 one of the unused cells in the current symbol block,
3212 and not be on the free-list. */
3214 && offset
% sizeof b
->symbols
[0] == 0
3215 && (b
!= symbol_block
3216 || offset
/ sizeof b
->symbols
[0] < symbol_block_index
)
3217 && !EQ (((struct Lisp_Symbol
*) p
)->function
, Vdead
));
3224 /* Value is non-zero if P is a pointer to a live Lisp float on
3225 the heap. M is a pointer to the mem_block for P. */
3232 if (m
->type
== MEM_TYPE_FLOAT
)
3234 struct float_block
*b
= (struct float_block
*) m
->start
;
3235 int offset
= (char *) p
- (char *) &b
->floats
[0];
3237 /* P must point to the start of a Lisp_Float, not be
3238 one of the unused cells in the current float block,
3239 and not be on the free-list. */
3241 && offset
% sizeof b
->floats
[0] == 0
3242 && (b
!= float_block
3243 || offset
/ sizeof b
->floats
[0] < float_block_index
)
3244 && !EQ (((struct Lisp_Float
*) p
)->type
, Vdead
));
3251 /* Value is non-zero if P is a pointer to a live Lisp Misc on
3252 the heap. M is a pointer to the mem_block for P. */
3259 if (m
->type
== MEM_TYPE_MISC
)
3261 struct marker_block
*b
= (struct marker_block
*) m
->start
;
3262 int offset
= (char *) p
- (char *) &b
->markers
[0];
3264 /* P must point to the start of a Lisp_Misc, not be
3265 one of the unused cells in the current misc block,
3266 and not be on the free-list. */
3268 && offset
% sizeof b
->markers
[0] == 0
3269 && (b
!= marker_block
3270 || offset
/ sizeof b
->markers
[0] < marker_block_index
)
3271 && ((union Lisp_Misc
*) p
)->u_marker
.type
!= Lisp_Misc_Free
);
3278 /* Value is non-zero if P is a pointer to a live vector-like object.
3279 M is a pointer to the mem_block for P. */
3282 live_vector_p (m
, p
)
3286 return (p
== m
->start
3287 && m
->type
>= MEM_TYPE_VECTOR
3288 && m
->type
<= MEM_TYPE_WINDOW
);
3292 /* Value is non-zero of P is a pointer to a live buffer. M is a
3293 pointer to the mem_block for P. */
3296 live_buffer_p (m
, p
)
3300 /* P must point to the start of the block, and the buffer
3301 must not have been killed. */
3302 return (m
->type
== MEM_TYPE_BUFFER
3304 && !NILP (((struct buffer
*) p
)->name
));
3307 #endif /* GC_MARK_STACK || defined GC_MALLOC_CHECK */
3311 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3313 /* Array of objects that are kept alive because the C stack contains
3314 a pattern that looks like a reference to them . */
3316 #define MAX_ZOMBIES 10
3317 static Lisp_Object zombies
[MAX_ZOMBIES
];
3319 /* Number of zombie objects. */
3321 static int nzombies
;
3323 /* Number of garbage collections. */
3327 /* Average percentage of zombies per collection. */
3329 static double avg_zombies
;
3331 /* Max. number of live and zombie objects. */
3333 static int max_live
, max_zombies
;
3335 /* Average number of live objects per GC. */
3337 static double avg_live
;
3339 DEFUN ("gc-status", Fgc_status
, Sgc_status
, 0, 0, "",
3340 doc
: /* Show information about live and zombie objects. */)
3343 Lisp_Object args
[8], zombie_list
= Qnil
;
3345 for (i
= 0; i
< nzombies
; i
++)
3346 zombie_list
= Fcons (zombies
[i
], zombie_list
);
3347 args
[0] = build_string ("%d GCs, avg live/zombies = %.2f/%.2f (%f%%), max %d/%d\nzombies: %S");
3348 args
[1] = make_number (ngcs
);
3349 args
[2] = make_float (avg_live
);
3350 args
[3] = make_float (avg_zombies
);
3351 args
[4] = make_float (avg_zombies
/ avg_live
/ 100);
3352 args
[5] = make_number (max_live
);
3353 args
[6] = make_number (max_zombies
);
3354 args
[7] = zombie_list
;
3355 return Fmessage (8, args
);
3358 #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
3361 /* Mark OBJ if we can prove it's a Lisp_Object. */
3364 mark_maybe_object (obj
)
3367 void *po
= (void *) XPNTR (obj
);
3368 struct mem_node
*m
= mem_find (po
);
3374 switch (XGCTYPE (obj
))
3377 mark_p
= (live_string_p (m
, po
)
3378 && !STRING_MARKED_P ((struct Lisp_String
*) po
));
3382 mark_p
= (live_cons_p (m
, po
)
3383 && !XMARKBIT (XCONS (obj
)->car
));
3387 mark_p
= (live_symbol_p (m
, po
)
3388 && !XMARKBIT (XSYMBOL (obj
)->plist
));
3392 mark_p
= (live_float_p (m
, po
)
3393 && !XMARKBIT (XFLOAT (obj
)->type
));
3396 case Lisp_Vectorlike
:
3397 /* Note: can't check GC_BUFFERP before we know it's a
3398 buffer because checking that dereferences the pointer
3399 PO which might point anywhere. */
3400 if (live_vector_p (m
, po
))
3401 mark_p
= (!GC_SUBRP (obj
)
3402 && !(XVECTOR (obj
)->size
& ARRAY_MARK_FLAG
));
3403 else if (live_buffer_p (m
, po
))
3404 mark_p
= GC_BUFFERP (obj
) && !XMARKBIT (XBUFFER (obj
)->name
);
3408 if (live_misc_p (m
, po
))
3410 switch (XMISCTYPE (obj
))
3412 case Lisp_Misc_Marker
:
3413 mark_p
= !XMARKBIT (XMARKER (obj
)->chain
);
3416 case Lisp_Misc_Buffer_Local_Value
:
3417 case Lisp_Misc_Some_Buffer_Local_Value
:
3418 mark_p
= !XMARKBIT (XBUFFER_LOCAL_VALUE (obj
)->realvalue
);
3421 case Lisp_Misc_Overlay
:
3422 mark_p
= !XMARKBIT (XOVERLAY (obj
)->plist
);
3429 case Lisp_Type_Limit
:
3435 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3436 if (nzombies
< MAX_ZOMBIES
)
3437 zombies
[nzombies
] = obj
;
3446 /* If P points to Lisp data, mark that as live if it isn't already
3450 mark_maybe_pointer (p
)
3455 /* Quickly rule out some values which can't point to Lisp data. We
3456 assume that Lisp data is aligned on even addresses. */
3457 if ((EMACS_INT
) p
& 1)
3463 Lisp_Object obj
= Qnil
;
3467 case MEM_TYPE_NON_LISP
:
3468 /* Nothing to do; not a pointer to Lisp memory. */
3471 case MEM_TYPE_BUFFER
:
3472 if (live_buffer_p (m
, p
)
3473 && !XMARKBIT (((struct buffer
*) p
)->name
))
3474 XSETVECTOR (obj
, p
);
3478 if (live_cons_p (m
, p
)
3479 && !XMARKBIT (((struct Lisp_Cons
*) p
)->car
))
3483 case MEM_TYPE_STRING
:
3484 if (live_string_p (m
, p
)
3485 && !STRING_MARKED_P ((struct Lisp_String
*) p
))
3486 XSETSTRING (obj
, p
);
3490 if (live_misc_p (m
, p
))
3495 switch (XMISCTYPE (tem
))
3497 case Lisp_Misc_Marker
:
3498 if (!XMARKBIT (XMARKER (tem
)->chain
))
3502 case Lisp_Misc_Buffer_Local_Value
:
3503 case Lisp_Misc_Some_Buffer_Local_Value
:
3504 if (!XMARKBIT (XBUFFER_LOCAL_VALUE (tem
)->realvalue
))
3508 case Lisp_Misc_Overlay
:
3509 if (!XMARKBIT (XOVERLAY (tem
)->plist
))
3516 case MEM_TYPE_SYMBOL
:
3517 if (live_symbol_p (m
, p
)
3518 && !XMARKBIT (((struct Lisp_Symbol
*) p
)->plist
))
3519 XSETSYMBOL (obj
, p
);
3522 case MEM_TYPE_FLOAT
:
3523 if (live_float_p (m
, p
)
3524 && !XMARKBIT (((struct Lisp_Float
*) p
)->type
))
3528 case MEM_TYPE_VECTOR
:
3529 case MEM_TYPE_PROCESS
:
3530 case MEM_TYPE_HASH_TABLE
:
3531 case MEM_TYPE_FRAME
:
3532 case MEM_TYPE_WINDOW
:
3533 if (live_vector_p (m
, p
))
3536 XSETVECTOR (tem
, p
);
3538 && !(XVECTOR (tem
)->size
& ARRAY_MARK_FLAG
))
3553 /* Mark Lisp objects referenced from the address range START..END. */
3556 mark_memory (start
, end
)
3562 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3566 /* Make START the pointer to the start of the memory region,
3567 if it isn't already. */
3575 /* Mark Lisp_Objects. */
3576 for (p
= (Lisp_Object
*) start
; (void *) p
< end
; ++p
)
3577 mark_maybe_object (*p
);
3579 /* Mark Lisp data pointed to. This is necessary because, in some
3580 situations, the C compiler optimizes Lisp objects away, so that
3581 only a pointer to them remains. Example:
3583 DEFUN ("testme", Ftestme, Stestme, 0, 0, 0, "")
3586 Lisp_Object obj = build_string ("test");
3587 struct Lisp_String *s = XSTRING (obj);
3588 Fgarbage_collect ();
3589 fprintf (stderr, "test `%s'\n", s->data);
3593 Here, `obj' isn't really used, and the compiler optimizes it
3594 away. The only reference to the life string is through the
3597 for (pp
= (void **) start
; (void *) pp
< end
; ++pp
)
3598 mark_maybe_pointer (*pp
);
3601 /* setjmp will work with GCC unless NON_SAVING_SETJMP is defined in
3602 the GCC system configuration. In gcc 3.2, the only systems for
3603 which this is so are i386-sco5 non-ELF, i386-sysv3 (maybe included
3604 by others?) and ns32k-pc532-min. */
3606 #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
3608 static int setjmp_tested_p
, longjmps_done
;
3610 #define SETJMP_WILL_LIKELY_WORK "\
3612 Emacs garbage collector has been changed to use conservative stack\n\
3613 marking. Emacs has determined that the method it uses to do the\n\
3614 marking will likely work on your system, but this isn't sure.\n\
3616 If you are a system-programmer, or can get the help of a local wizard\n\
3617 who is, please take a look at the function mark_stack in alloc.c, and\n\
3618 verify that the methods used are appropriate for your system.\n\
3620 Please mail the result to <emacs-devel@gnu.org>.\n\
3623 #define SETJMP_WILL_NOT_WORK "\
3625 Emacs garbage collector has been changed to use conservative stack\n\
3626 marking. Emacs has determined that the default method it uses to do the\n\
3627 marking will not work on your system. We will need a system-dependent\n\
3628 solution for your system.\n\
3630 Please take a look at the function mark_stack in alloc.c, and\n\
3631 try to find a way to make it work on your system.\n\
3633 Note that you may get false negatives, depending on the compiler.\n\
3634 In particular, you need to use -O with GCC for this test.\n\
3636 Please mail the result to <emacs-devel@gnu.org>.\n\
3640 /* Perform a quick check if it looks like setjmp saves registers in a
3641 jmp_buf. Print a message to stderr saying so. When this test
3642 succeeds, this is _not_ a proof that setjmp is sufficient for
3643 conservative stack marking. Only the sources or a disassembly
3654 /* Arrange for X to be put in a register. */
3660 if (longjmps_done
== 1)
3662 /* Came here after the longjmp at the end of the function.
3664 If x == 1, the longjmp has restored the register to its
3665 value before the setjmp, and we can hope that setjmp
3666 saves all such registers in the jmp_buf, although that
3669 For other values of X, either something really strange is
3670 taking place, or the setjmp just didn't save the register. */
3673 fprintf (stderr
, SETJMP_WILL_LIKELY_WORK
);
3676 fprintf (stderr
, SETJMP_WILL_NOT_WORK
);
3683 if (longjmps_done
== 1)
3687 #endif /* not GC_SAVE_REGISTERS_ON_STACK && not GC_SETJMP_WORKS */
3690 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
3692 /* Abort if anything GCPRO'd doesn't survive the GC. */
3700 for (p
= gcprolist
; p
; p
= p
->next
)
3701 for (i
= 0; i
< p
->nvars
; ++i
)
3702 if (!survives_gc_p (p
->var
[i
]))
3706 #elif GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3713 fprintf (stderr
, "\nZombies kept alive = %d:\n", nzombies
);
3714 for (i
= 0; i
< min (MAX_ZOMBIES
, nzombies
); ++i
)
3716 fprintf (stderr
, " %d = ", i
);
3717 debug_print (zombies
[i
]);
3721 #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
3724 /* Mark live Lisp objects on the C stack.
3726 There are several system-dependent problems to consider when
3727 porting this to new architectures:
3731 We have to mark Lisp objects in CPU registers that can hold local
3732 variables or are used to pass parameters.
3734 If GC_SAVE_REGISTERS_ON_STACK is defined, it should expand to
3735 something that either saves relevant registers on the stack, or
3736 calls mark_maybe_object passing it each register's contents.
3738 If GC_SAVE_REGISTERS_ON_STACK is not defined, the current
3739 implementation assumes that calling setjmp saves registers we need
3740 to see in a jmp_buf which itself lies on the stack. This doesn't
3741 have to be true! It must be verified for each system, possibly
3742 by taking a look at the source code of setjmp.
3746 Architectures differ in the way their processor stack is organized.
3747 For example, the stack might look like this
3750 | Lisp_Object | size = 4
3752 | something else | size = 2
3754 | Lisp_Object | size = 4
3758 In such a case, not every Lisp_Object will be aligned equally. To
3759 find all Lisp_Object on the stack it won't be sufficient to walk
3760 the stack in steps of 4 bytes. Instead, two passes will be
3761 necessary, one starting at the start of the stack, and a second
3762 pass starting at the start of the stack + 2. Likewise, if the
3763 minimal alignment of Lisp_Objects on the stack is 1, four passes
3764 would be necessary, each one starting with one byte more offset
3765 from the stack start.
3767 The current code assumes by default that Lisp_Objects are aligned
3768 equally on the stack. */
3775 volatile int stack_grows_down_p
= (char *) &j
> (char *) stack_base
;
3778 /* This trick flushes the register windows so that all the state of
3779 the process is contained in the stack. */
3780 /* Fixme: Code in the Boehm GC sugests flushing (with `flushrs') is
3781 needed on ia64 too. See mach_dep.c, where it also says inline
3782 assembler doesn't work with relevant proprietary compilers. */
3787 /* Save registers that we need to see on the stack. We need to see
3788 registers used to hold register variables and registers used to
3790 #ifdef GC_SAVE_REGISTERS_ON_STACK
3791 GC_SAVE_REGISTERS_ON_STACK (end
);
3792 #else /* not GC_SAVE_REGISTERS_ON_STACK */
3794 #ifndef GC_SETJMP_WORKS /* If it hasn't been checked yet that
3795 setjmp will definitely work, test it
3796 and print a message with the result
3798 if (!setjmp_tested_p
)
3800 setjmp_tested_p
= 1;
3803 #endif /* GC_SETJMP_WORKS */
3806 end
= stack_grows_down_p
? (char *) &j
+ sizeof j
: (char *) &j
;
3807 #endif /* not GC_SAVE_REGISTERS_ON_STACK */
3809 /* This assumes that the stack is a contiguous region in memory. If
3810 that's not the case, something has to be done here to iterate
3811 over the stack segments. */
3812 #ifndef GC_LISP_OBJECT_ALIGNMENT
3814 #define GC_LISP_OBJECT_ALIGNMENT __alignof__ (Lisp_Object)
3816 #define GC_LISP_OBJECT_ALIGNMENT sizeof (Lisp_Object)
3819 for (i
= 0; i
< sizeof (Lisp_Object
); i
+= GC_LISP_OBJECT_ALIGNMENT
)
3820 mark_memory ((char *) stack_base
+ i
, end
);
3822 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
3828 #endif /* GC_MARK_STACK != 0 */
3832 /***********************************************************************
3833 Pure Storage Management
3834 ***********************************************************************/
3836 /* Allocate room for SIZE bytes from pure Lisp storage and return a
3837 pointer to it. TYPE is the Lisp type for which the memory is
3838 allocated. TYPE < 0 means it's not used for a Lisp object.
3840 If store_pure_type_info is set and TYPE is >= 0, the type of
3841 the allocated object is recorded in pure_types. */
3843 static POINTER_TYPE
*
3844 pure_alloc (size
, type
)
3848 POINTER_TYPE
*result
;
3849 size_t alignment
= sizeof (EMACS_INT
);
3851 /* Give Lisp_Floats an extra alignment. */
3852 if (type
== Lisp_Float
)
3854 #if defined __GNUC__ && __GNUC__ >= 2
3855 alignment
= __alignof (struct Lisp_Float
);
3857 alignment
= sizeof (struct Lisp_Float
);
3862 result
= (POINTER_TYPE
*) ALIGN ((EMACS_UINT
)purebeg
+ pure_bytes_used
, alignment
);
3863 pure_bytes_used
= ((char *)result
- (char *)purebeg
) + size
;
3865 if (pure_bytes_used
<= pure_size
)
3868 /* Don't allocate a large amount here,
3869 because it might get mmap'd and then its address
3870 might not be usable. */
3871 purebeg
= (char *) xmalloc (10000);
3873 pure_bytes_used_before_overflow
+= pure_bytes_used
- size
;
3874 pure_bytes_used
= 0;
3879 /* Print a warning if PURESIZE is too small. */
3884 if (pure_bytes_used_before_overflow
)
3885 message ("Pure Lisp storage overflow (approx. %d bytes needed)",
3886 (int) (pure_bytes_used
+ pure_bytes_used_before_overflow
));
3890 /* Return a string allocated in pure space. DATA is a buffer holding
3891 NCHARS characters, and NBYTES bytes of string data. MULTIBYTE
3892 non-zero means make the result string multibyte.
3894 Must get an error if pure storage is full, since if it cannot hold
3895 a large string it may be able to hold conses that point to that
3896 string; then the string is not protected from gc. */
3899 make_pure_string (data
, nchars
, nbytes
, multibyte
)
3905 struct Lisp_String
*s
;
3907 s
= (struct Lisp_String
*) pure_alloc (sizeof *s
, Lisp_String
);
3908 s
->data
= (unsigned char *) pure_alloc (nbytes
+ 1, -1);
3910 s
->size_byte
= multibyte
? nbytes
: -1;
3911 bcopy (data
, s
->data
, nbytes
);
3912 s
->data
[nbytes
] = '\0';
3913 s
->intervals
= NULL_INTERVAL
;
3914 XSETSTRING (string
, s
);
3919 /* Return a cons allocated from pure space. Give it pure copies
3920 of CAR as car and CDR as cdr. */
3923 pure_cons (car
, cdr
)
3924 Lisp_Object car
, cdr
;
3926 register Lisp_Object
new;
3927 struct Lisp_Cons
*p
;
3929 p
= (struct Lisp_Cons
*) pure_alloc (sizeof *p
, Lisp_Cons
);
3931 XSETCAR (new, Fpurecopy (car
));
3932 XSETCDR (new, Fpurecopy (cdr
));
3937 /* Value is a float object with value NUM allocated from pure space. */
3940 make_pure_float (num
)
3943 register Lisp_Object
new;
3944 struct Lisp_Float
*p
;
3946 p
= (struct Lisp_Float
*) pure_alloc (sizeof *p
, Lisp_Float
);
3948 XFLOAT_DATA (new) = num
;
3953 /* Return a vector with room for LEN Lisp_Objects allocated from
3957 make_pure_vector (len
)
3961 struct Lisp_Vector
*p
;
3962 size_t size
= sizeof *p
+ (len
- 1) * sizeof (Lisp_Object
);
3964 p
= (struct Lisp_Vector
*) pure_alloc (size
, Lisp_Vectorlike
);
3965 XSETVECTOR (new, p
);
3966 XVECTOR (new)->size
= len
;
3971 DEFUN ("purecopy", Fpurecopy
, Spurecopy
, 1, 1, 0,
3972 doc
: /* Make a copy of OBJECT in pure storage.
3973 Recursively copies contents of vectors and cons cells.
3974 Does not copy symbols. Copies strings without text properties. */)
3976 register Lisp_Object obj
;
3978 if (NILP (Vpurify_flag
))
3981 if (PURE_POINTER_P (XPNTR (obj
)))
3985 return pure_cons (XCAR (obj
), XCDR (obj
));
3986 else if (FLOATP (obj
))
3987 return make_pure_float (XFLOAT_DATA (obj
));
3988 else if (STRINGP (obj
))
3989 return make_pure_string (SDATA (obj
), SCHARS (obj
),
3991 STRING_MULTIBYTE (obj
));
3992 else if (COMPILEDP (obj
) || VECTORP (obj
))
3994 register struct Lisp_Vector
*vec
;
3995 register int i
, size
;
3997 size
= XVECTOR (obj
)->size
;
3998 if (size
& PSEUDOVECTOR_FLAG
)
3999 size
&= PSEUDOVECTOR_SIZE_MASK
;
4000 vec
= XVECTOR (make_pure_vector ((EMACS_INT
) size
));
4001 for (i
= 0; i
< size
; i
++)
4002 vec
->contents
[i
] = Fpurecopy (XVECTOR (obj
)->contents
[i
]);
4003 if (COMPILEDP (obj
))
4004 XSETCOMPILED (obj
, vec
);
4006 XSETVECTOR (obj
, vec
);
4009 else if (MARKERP (obj
))
4010 error ("Attempt to copy a marker to pure storage");
4017 /***********************************************************************
4019 ***********************************************************************/
4021 /* Put an entry in staticvec, pointing at the variable with address
4025 staticpro (varaddress
)
4026 Lisp_Object
*varaddress
;
4028 staticvec
[staticidx
++] = varaddress
;
4029 if (staticidx
>= NSTATICS
)
4037 struct catchtag
*next
;
4042 struct backtrace
*next
;
4043 Lisp_Object
*function
;
4044 Lisp_Object
*args
; /* Points to vector of args. */
4045 int nargs
; /* Length of vector. */
4046 /* If nargs is UNEVALLED, args points to slot holding list of
4053 /***********************************************************************
4055 ***********************************************************************/
4057 /* Temporarily prevent garbage collection. */
4060 inhibit_garbage_collection ()
4062 int count
= SPECPDL_INDEX ();
4063 int nbits
= min (VALBITS
, BITS_PER_INT
);
4065 specbind (Qgc_cons_threshold
, make_number (((EMACS_INT
) 1 << (nbits
- 1)) - 1));
4070 DEFUN ("garbage-collect", Fgarbage_collect
, Sgarbage_collect
, 0, 0, "",
4071 doc
: /* Reclaim storage for Lisp objects no longer needed.
4072 Returns info on amount of space in use:
4073 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)
4074 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS
4075 (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS)
4076 (USED-STRINGS . FREE-STRINGS))
4077 Garbage collection happens automatically if you cons more than
4078 `gc-cons-threshold' bytes of Lisp data since previous garbage collection. */)
4081 register struct gcpro
*tail
;
4082 register struct specbinding
*bind
;
4083 struct catchtag
*catch;
4084 struct handler
*handler
;
4085 register struct backtrace
*backlist
;
4086 char stack_top_variable
;
4089 Lisp_Object total
[8];
4090 int count
= SPECPDL_INDEX ();
4091 EMACS_TIME t1
, t2
, t3
;
4093 EMACS_GET_TIME (t1
);
4095 /* Can't GC if pure storage overflowed because we can't determine
4096 if something is a pure object or not. */
4097 if (pure_bytes_used_before_overflow
)
4100 /* In case user calls debug_print during GC,
4101 don't let that cause a recursive GC. */
4102 consing_since_gc
= 0;
4104 /* Save what's currently displayed in the echo area. */
4105 message_p
= push_message ();
4106 record_unwind_protect (pop_message_unwind
, Qnil
);
4108 /* Save a copy of the contents of the stack, for debugging. */
4109 #if MAX_SAVE_STACK > 0
4110 if (NILP (Vpurify_flag
))
4112 i
= &stack_top_variable
- stack_bottom
;
4114 if (i
< MAX_SAVE_STACK
)
4116 if (stack_copy
== 0)
4117 stack_copy
= (char *) xmalloc (stack_copy_size
= i
);
4118 else if (stack_copy_size
< i
)
4119 stack_copy
= (char *) xrealloc (stack_copy
, (stack_copy_size
= i
));
4122 if ((EMACS_INT
) (&stack_top_variable
- stack_bottom
) > 0)
4123 bcopy (stack_bottom
, stack_copy
, i
);
4125 bcopy (&stack_top_variable
, stack_copy
, i
);
4129 #endif /* MAX_SAVE_STACK > 0 */
4131 if (garbage_collection_messages
)
4132 message1_nolog ("Garbage collecting...");
4136 shrink_regexp_cache ();
4138 /* Don't keep undo information around forever. */
4140 register struct buffer
*nextb
= all_buffers
;
4144 /* If a buffer's undo list is Qt, that means that undo is
4145 turned off in that buffer. Calling truncate_undo_list on
4146 Qt tends to return NULL, which effectively turns undo back on.
4147 So don't call truncate_undo_list if undo_list is Qt. */
4148 if (! EQ (nextb
->undo_list
, Qt
))
4150 = truncate_undo_list (nextb
->undo_list
, undo_limit
,
4153 /* Shrink buffer gaps, but skip indirect and dead buffers. */
4154 if (nextb
->base_buffer
== 0 && !NILP (nextb
->name
))
4156 /* If a buffer's gap size is more than 10% of the buffer
4157 size, or larger than 2000 bytes, then shrink it
4158 accordingly. Keep a minimum size of 20 bytes. */
4159 int size
= min (2000, max (20, (nextb
->text
->z_byte
/ 10)));
4161 if (nextb
->text
->gap_size
> size
)
4163 struct buffer
*save_current
= current_buffer
;
4164 current_buffer
= nextb
;
4165 make_gap (-(nextb
->text
->gap_size
- size
));
4166 current_buffer
= save_current
;
4170 nextb
= nextb
->next
;
4176 /* clear_marks (); */
4178 /* Mark all the special slots that serve as the roots of accessibility.
4180 Usually the special slots to mark are contained in particular structures.
4181 Then we know no slot is marked twice because the structures don't overlap.
4182 In some cases, the structures point to the slots to be marked.
4183 For these, we use MARKBIT to avoid double marking of the slot. */
4185 for (i
= 0; i
< staticidx
; i
++)
4186 mark_object (staticvec
[i
]);
4188 #if (GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS \
4189 || GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS)
4192 for (tail
= gcprolist
; tail
; tail
= tail
->next
)
4193 for (i
= 0; i
< tail
->nvars
; i
++)
4194 if (!XMARKBIT (tail
->var
[i
]))
4196 /* Explicit casting prevents compiler warning about
4197 discarding the `volatile' qualifier. */
4198 mark_object ((Lisp_Object
*)&tail
->var
[i
]);
4199 XMARK (tail
->var
[i
]);
4204 for (bind
= specpdl
; bind
!= specpdl_ptr
; bind
++)
4206 mark_object (&bind
->symbol
);
4207 mark_object (&bind
->old_value
);
4209 for (catch = catchlist
; catch; catch = catch->next
)
4211 mark_object (&catch->tag
);
4212 mark_object (&catch->val
);
4214 for (handler
= handlerlist
; handler
; handler
= handler
->next
)
4216 mark_object (&handler
->handler
);
4217 mark_object (&handler
->var
);
4219 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
4221 if (!XMARKBIT (*backlist
->function
))
4223 mark_object (backlist
->function
);
4224 XMARK (*backlist
->function
);
4226 if (backlist
->nargs
== UNEVALLED
|| backlist
->nargs
== MANY
)
4229 i
= backlist
->nargs
- 1;
4231 if (!XMARKBIT (backlist
->args
[i
]))
4233 mark_object (&backlist
->args
[i
]);
4234 XMARK (backlist
->args
[i
]);
4239 /* Look thru every buffer's undo list
4240 for elements that update markers that were not marked,
4243 register struct buffer
*nextb
= all_buffers
;
4247 /* If a buffer's undo list is Qt, that means that undo is
4248 turned off in that buffer. Calling truncate_undo_list on
4249 Qt tends to return NULL, which effectively turns undo back on.
4250 So don't call truncate_undo_list if undo_list is Qt. */
4251 if (! EQ (nextb
->undo_list
, Qt
))
4253 Lisp_Object tail
, prev
;
4254 tail
= nextb
->undo_list
;
4256 while (CONSP (tail
))
4258 if (GC_CONSP (XCAR (tail
))
4259 && GC_MARKERP (XCAR (XCAR (tail
)))
4260 && ! XMARKBIT (XMARKER (XCAR (XCAR (tail
)))->chain
))
4263 nextb
->undo_list
= tail
= XCDR (tail
);
4267 XSETCDR (prev
, tail
);
4278 nextb
= nextb
->next
;
4282 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4288 extern void xg_mark_data ();
4295 /* Clear the mark bits that we set in certain root slots. */
4297 #if (GC_MARK_STACK == GC_USE_GCPROS_AS_BEFORE \
4298 || GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES)
4299 for (tail
= gcprolist
; tail
; tail
= tail
->next
)
4300 for (i
= 0; i
< tail
->nvars
; i
++)
4301 XUNMARK (tail
->var
[i
]);
4304 unmark_byte_stack ();
4305 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
4307 XUNMARK (*backlist
->function
);
4308 if (backlist
->nargs
== UNEVALLED
|| backlist
->nargs
== MANY
)
4311 i
= backlist
->nargs
- 1;
4313 XUNMARK (backlist
->args
[i
]);
4315 XUNMARK (buffer_defaults
.name
);
4316 XUNMARK (buffer_local_symbols
.name
);
4318 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES && 0
4324 /* clear_marks (); */
4327 consing_since_gc
= 0;
4328 if (gc_cons_threshold
< 10000)
4329 gc_cons_threshold
= 10000;
4331 if (garbage_collection_messages
)
4333 if (message_p
|| minibuf_level
> 0)
4336 message1_nolog ("Garbage collecting...done");
4339 unbind_to (count
, Qnil
);
4341 total
[0] = Fcons (make_number (total_conses
),
4342 make_number (total_free_conses
));
4343 total
[1] = Fcons (make_number (total_symbols
),
4344 make_number (total_free_symbols
));
4345 total
[2] = Fcons (make_number (total_markers
),
4346 make_number (total_free_markers
));
4347 total
[3] = make_number (total_string_size
);
4348 total
[4] = make_number (total_vector_size
);
4349 total
[5] = Fcons (make_number (total_floats
),
4350 make_number (total_free_floats
));
4351 total
[6] = Fcons (make_number (total_intervals
),
4352 make_number (total_free_intervals
));
4353 total
[7] = Fcons (make_number (total_strings
),
4354 make_number (total_free_strings
));
4356 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4358 /* Compute average percentage of zombies. */
4361 for (i
= 0; i
< 7; ++i
)
4362 if (CONSP (total
[i
]))
4363 nlive
+= XFASTINT (XCAR (total
[i
]));
4365 avg_live
= (avg_live
* ngcs
+ nlive
) / (ngcs
+ 1);
4366 max_live
= max (nlive
, max_live
);
4367 avg_zombies
= (avg_zombies
* ngcs
+ nzombies
) / (ngcs
+ 1);
4368 max_zombies
= max (nzombies
, max_zombies
);
4373 if (!NILP (Vpost_gc_hook
))
4375 int count
= inhibit_garbage_collection ();
4376 safe_run_hooks (Qpost_gc_hook
);
4377 unbind_to (count
, Qnil
);
4380 /* Accumulate statistics. */
4381 EMACS_GET_TIME (t2
);
4382 EMACS_SUB_TIME (t3
, t2
, t1
);
4383 if (FLOATP (Vgc_elapsed
))
4384 XSETFLOAT (Vgc_elapsed
, make_float (XFLOAT_DATA (Vgc_elapsed
) +
4386 EMACS_USECS (t3
) * 1.0e-6));
4389 return Flist (sizeof total
/ sizeof *total
, total
);
4393 /* Mark Lisp objects in glyph matrix MATRIX. Currently the
4394 only interesting objects referenced from glyphs are strings. */
4397 mark_glyph_matrix (matrix
)
4398 struct glyph_matrix
*matrix
;
4400 struct glyph_row
*row
= matrix
->rows
;
4401 struct glyph_row
*end
= row
+ matrix
->nrows
;
4403 for (; row
< end
; ++row
)
4407 for (area
= LEFT_MARGIN_AREA
; area
< LAST_AREA
; ++area
)
4409 struct glyph
*glyph
= row
->glyphs
[area
];
4410 struct glyph
*end_glyph
= glyph
+ row
->used
[area
];
4412 for (; glyph
< end_glyph
; ++glyph
)
4413 if (GC_STRINGP (glyph
->object
)
4414 && !STRING_MARKED_P (XSTRING (glyph
->object
)))
4415 mark_object (&glyph
->object
);
4421 /* Mark Lisp faces in the face cache C. */
4425 struct face_cache
*c
;
4430 for (i
= 0; i
< c
->used
; ++i
)
4432 struct face
*face
= FACE_FROM_ID (c
->f
, i
);
4436 for (j
= 0; j
< LFACE_VECTOR_SIZE
; ++j
)
4437 mark_object (&face
->lface
[j
]);
4444 #ifdef HAVE_WINDOW_SYSTEM
4446 /* Mark Lisp objects in image IMG. */
4452 mark_object (&img
->spec
);
4454 if (!NILP (img
->data
.lisp_val
))
4455 mark_object (&img
->data
.lisp_val
);
4459 /* Mark Lisp objects in image cache of frame F. It's done this way so
4460 that we don't have to include xterm.h here. */
4463 mark_image_cache (f
)
4466 forall_images_in_image_cache (f
, mark_image
);
4469 #endif /* HAVE_X_WINDOWS */
4473 /* Mark reference to a Lisp_Object.
4474 If the object referred to has not been seen yet, recursively mark
4475 all the references contained in it. */
4477 #define LAST_MARKED_SIZE 500
4478 Lisp_Object
*last_marked
[LAST_MARKED_SIZE
];
4479 int last_marked_index
;
4481 /* For debugging--call abort when we cdr down this many
4482 links of a list, in mark_object. In debugging,
4483 the call to abort will hit a breakpoint.
4484 Normally this is zero and the check never goes off. */
4485 int mark_object_loop_halt
;
4488 mark_object (argptr
)
4489 Lisp_Object
*argptr
;
4491 Lisp_Object
*objptr
= argptr
;
4492 register Lisp_Object obj
;
4493 #ifdef GC_CHECK_MARKED_OBJECTS
4504 if (PURE_POINTER_P (XPNTR (obj
)))
4507 last_marked
[last_marked_index
++] = objptr
;
4508 if (last_marked_index
== LAST_MARKED_SIZE
)
4509 last_marked_index
= 0;
4511 /* Perform some sanity checks on the objects marked here. Abort if
4512 we encounter an object we know is bogus. This increases GC time
4513 by ~80%, and requires compilation with GC_MARK_STACK != 0. */
4514 #ifdef GC_CHECK_MARKED_OBJECTS
4516 po
= (void *) XPNTR (obj
);
4518 /* Check that the object pointed to by PO is known to be a Lisp
4519 structure allocated from the heap. */
4520 #define CHECK_ALLOCATED() \
4522 m = mem_find (po); \
4527 /* Check that the object pointed to by PO is live, using predicate
4529 #define CHECK_LIVE(LIVEP) \
4531 if (!LIVEP (m, po)) \
4535 /* Check both of the above conditions. */
4536 #define CHECK_ALLOCATED_AND_LIVE(LIVEP) \
4538 CHECK_ALLOCATED (); \
4539 CHECK_LIVE (LIVEP); \
4542 #else /* not GC_CHECK_MARKED_OBJECTS */
4544 #define CHECK_ALLOCATED() (void) 0
4545 #define CHECK_LIVE(LIVEP) (void) 0
4546 #define CHECK_ALLOCATED_AND_LIVE(LIVEP) (void) 0
4548 #endif /* not GC_CHECK_MARKED_OBJECTS */
4550 switch (SWITCH_ENUM_CAST (XGCTYPE (obj
)))
4554 register struct Lisp_String
*ptr
= XSTRING (obj
);
4555 CHECK_ALLOCATED_AND_LIVE (live_string_p
);
4556 MARK_INTERVAL_TREE (ptr
->intervals
);
4558 #ifdef GC_CHECK_STRING_BYTES
4559 /* Check that the string size recorded in the string is the
4560 same as the one recorded in the sdata structure. */
4561 CHECK_STRING_BYTES (ptr
);
4562 #endif /* GC_CHECK_STRING_BYTES */
4566 case Lisp_Vectorlike
:
4567 #ifdef GC_CHECK_MARKED_OBJECTS
4569 if (m
== MEM_NIL
&& !GC_SUBRP (obj
)
4570 && po
!= &buffer_defaults
4571 && po
!= &buffer_local_symbols
)
4573 #endif /* GC_CHECK_MARKED_OBJECTS */
4575 if (GC_BUFFERP (obj
))
4577 if (!XMARKBIT (XBUFFER (obj
)->name
))
4579 #ifdef GC_CHECK_MARKED_OBJECTS
4580 if (po
!= &buffer_defaults
&& po
!= &buffer_local_symbols
)
4583 for (b
= all_buffers
; b
&& b
!= po
; b
= b
->next
)
4588 #endif /* GC_CHECK_MARKED_OBJECTS */
4592 else if (GC_SUBRP (obj
))
4594 else if (GC_COMPILEDP (obj
))
4595 /* We could treat this just like a vector, but it is better to
4596 save the COMPILED_CONSTANTS element for last and avoid
4599 register struct Lisp_Vector
*ptr
= XVECTOR (obj
);
4600 register EMACS_INT size
= ptr
->size
;
4603 if (size
& ARRAY_MARK_FLAG
)
4604 break; /* Already marked */
4606 CHECK_LIVE (live_vector_p
);
4607 ptr
->size
|= ARRAY_MARK_FLAG
; /* Else mark it */
4608 size
&= PSEUDOVECTOR_SIZE_MASK
;
4609 for (i
= 0; i
< size
; i
++) /* and then mark its elements */
4611 if (i
!= COMPILED_CONSTANTS
)
4612 mark_object (&ptr
->contents
[i
]);
4614 /* This cast should be unnecessary, but some Mips compiler complains
4615 (MIPS-ABI + SysVR4, DC/OSx, etc). */
4616 objptr
= (Lisp_Object
*) &ptr
->contents
[COMPILED_CONSTANTS
];
4619 else if (GC_FRAMEP (obj
))
4621 register struct frame
*ptr
= XFRAME (obj
);
4622 register EMACS_INT size
= ptr
->size
;
4624 if (size
& ARRAY_MARK_FLAG
) break; /* Already marked */
4625 ptr
->size
|= ARRAY_MARK_FLAG
; /* Else mark it */
4627 CHECK_LIVE (live_vector_p
);
4628 mark_object (&ptr
->name
);
4629 mark_object (&ptr
->icon_name
);
4630 mark_object (&ptr
->title
);
4631 mark_object (&ptr
->focus_frame
);
4632 mark_object (&ptr
->selected_window
);
4633 mark_object (&ptr
->minibuffer_window
);
4634 mark_object (&ptr
->param_alist
);
4635 mark_object (&ptr
->scroll_bars
);
4636 mark_object (&ptr
->condemned_scroll_bars
);
4637 mark_object (&ptr
->menu_bar_items
);
4638 mark_object (&ptr
->face_alist
);
4639 mark_object (&ptr
->menu_bar_vector
);
4640 mark_object (&ptr
->buffer_predicate
);
4641 mark_object (&ptr
->buffer_list
);
4642 mark_object (&ptr
->menu_bar_window
);
4643 mark_object (&ptr
->tool_bar_window
);
4644 mark_face_cache (ptr
->face_cache
);
4645 #ifdef HAVE_WINDOW_SYSTEM
4646 mark_image_cache (ptr
);
4647 mark_object (&ptr
->tool_bar_items
);
4648 mark_object (&ptr
->desired_tool_bar_string
);
4649 mark_object (&ptr
->current_tool_bar_string
);
4650 #endif /* HAVE_WINDOW_SYSTEM */
4652 else if (GC_BOOL_VECTOR_P (obj
))
4654 register struct Lisp_Vector
*ptr
= XVECTOR (obj
);
4656 if (ptr
->size
& ARRAY_MARK_FLAG
)
4657 break; /* Already marked */
4658 CHECK_LIVE (live_vector_p
);
4659 ptr
->size
|= ARRAY_MARK_FLAG
; /* Else mark it */
4661 else if (GC_WINDOWP (obj
))
4663 register struct Lisp_Vector
*ptr
= XVECTOR (obj
);
4664 struct window
*w
= XWINDOW (obj
);
4665 register EMACS_INT size
= ptr
->size
;
4668 /* Stop if already marked. */
4669 if (size
& ARRAY_MARK_FLAG
)
4673 CHECK_LIVE (live_vector_p
);
4674 ptr
->size
|= ARRAY_MARK_FLAG
;
4676 /* There is no Lisp data above The member CURRENT_MATRIX in
4677 struct WINDOW. Stop marking when that slot is reached. */
4679 (char *) &ptr
->contents
[i
] < (char *) &w
->current_matrix
;
4681 mark_object (&ptr
->contents
[i
]);
4683 /* Mark glyphs for leaf windows. Marking window matrices is
4684 sufficient because frame matrices use the same glyph
4686 if (NILP (w
->hchild
)
4688 && w
->current_matrix
)
4690 mark_glyph_matrix (w
->current_matrix
);
4691 mark_glyph_matrix (w
->desired_matrix
);
4694 else if (GC_HASH_TABLE_P (obj
))
4696 struct Lisp_Hash_Table
*h
= XHASH_TABLE (obj
);
4697 EMACS_INT size
= h
->size
;
4699 /* Stop if already marked. */
4700 if (size
& ARRAY_MARK_FLAG
)
4704 CHECK_LIVE (live_vector_p
);
4705 h
->size
|= ARRAY_MARK_FLAG
;
4707 /* Mark contents. */
4708 /* Do not mark next_free or next_weak.
4709 Being in the next_weak chain
4710 should not keep the hash table alive.
4711 No need to mark `count' since it is an integer. */
4712 mark_object (&h
->test
);
4713 mark_object (&h
->weak
);
4714 mark_object (&h
->rehash_size
);
4715 mark_object (&h
->rehash_threshold
);
4716 mark_object (&h
->hash
);
4717 mark_object (&h
->next
);
4718 mark_object (&h
->index
);
4719 mark_object (&h
->user_hash_function
);
4720 mark_object (&h
->user_cmp_function
);
4722 /* If hash table is not weak, mark all keys and values.
4723 For weak tables, mark only the vector. */
4724 if (GC_NILP (h
->weak
))
4725 mark_object (&h
->key_and_value
);
4727 XVECTOR (h
->key_and_value
)->size
|= ARRAY_MARK_FLAG
;
4732 register struct Lisp_Vector
*ptr
= XVECTOR (obj
);
4733 register EMACS_INT size
= ptr
->size
;
4736 if (size
& ARRAY_MARK_FLAG
) break; /* Already marked */
4737 CHECK_LIVE (live_vector_p
);
4738 ptr
->size
|= ARRAY_MARK_FLAG
; /* Else mark it */
4739 if (size
& PSEUDOVECTOR_FLAG
)
4740 size
&= PSEUDOVECTOR_SIZE_MASK
;
4742 for (i
= 0; i
< size
; i
++) /* and then mark its elements */
4743 mark_object (&ptr
->contents
[i
]);
4749 register struct Lisp_Symbol
*ptr
= XSYMBOL (obj
);
4750 struct Lisp_Symbol
*ptrx
;
4752 if (XMARKBIT (ptr
->plist
)) break;
4753 CHECK_ALLOCATED_AND_LIVE (live_symbol_p
);
4755 mark_object ((Lisp_Object
*) &ptr
->value
);
4756 mark_object (&ptr
->function
);
4757 mark_object (&ptr
->plist
);
4759 if (!PURE_POINTER_P (XSTRING (ptr
->xname
)))
4760 MARK_STRING (XSTRING (ptr
->xname
));
4761 MARK_INTERVAL_TREE (STRING_INTERVALS (ptr
->xname
));
4763 /* Note that we do not mark the obarray of the symbol.
4764 It is safe not to do so because nothing accesses that
4765 slot except to check whether it is nil. */
4769 /* For the benefit of the last_marked log. */
4770 objptr
= (Lisp_Object
*)&XSYMBOL (obj
)->next
;
4771 ptrx
= ptr
; /* Use of ptrx avoids compiler bug on Sun */
4772 XSETSYMBOL (obj
, ptrx
);
4773 /* We can't goto loop here because *objptr doesn't contain an
4774 actual Lisp_Object with valid datatype field. */
4781 CHECK_ALLOCATED_AND_LIVE (live_misc_p
);
4782 switch (XMISCTYPE (obj
))
4784 case Lisp_Misc_Marker
:
4785 XMARK (XMARKER (obj
)->chain
);
4786 /* DO NOT mark thru the marker's chain.
4787 The buffer's markers chain does not preserve markers from gc;
4788 instead, markers are removed from the chain when freed by gc. */
4791 case Lisp_Misc_Buffer_Local_Value
:
4792 case Lisp_Misc_Some_Buffer_Local_Value
:
4794 register struct Lisp_Buffer_Local_Value
*ptr
4795 = XBUFFER_LOCAL_VALUE (obj
);
4796 if (XMARKBIT (ptr
->realvalue
)) break;
4797 XMARK (ptr
->realvalue
);
4798 /* If the cdr is nil, avoid recursion for the car. */
4799 if (EQ (ptr
->cdr
, Qnil
))
4801 objptr
= &ptr
->realvalue
;
4804 mark_object (&ptr
->realvalue
);
4805 mark_object (&ptr
->buffer
);
4806 mark_object (&ptr
->frame
);
4811 case Lisp_Misc_Intfwd
:
4812 case Lisp_Misc_Boolfwd
:
4813 case Lisp_Misc_Objfwd
:
4814 case Lisp_Misc_Buffer_Objfwd
:
4815 case Lisp_Misc_Kboard_Objfwd
:
4816 /* Don't bother with Lisp_Buffer_Objfwd,
4817 since all markable slots in current buffer marked anyway. */
4818 /* Don't need to do Lisp_Objfwd, since the places they point
4819 are protected with staticpro. */
4822 case Lisp_Misc_Overlay
:
4824 struct Lisp_Overlay
*ptr
= XOVERLAY (obj
);
4825 if (!XMARKBIT (ptr
->plist
))
4828 mark_object (&ptr
->start
);
4829 mark_object (&ptr
->end
);
4830 objptr
= &ptr
->plist
;
4843 register struct Lisp_Cons
*ptr
= XCONS (obj
);
4844 if (XMARKBIT (ptr
->car
)) break;
4845 CHECK_ALLOCATED_AND_LIVE (live_cons_p
);
4847 /* If the cdr is nil, avoid recursion for the car. */
4848 if (EQ (ptr
->cdr
, Qnil
))
4854 mark_object (&ptr
->car
);
4857 if (cdr_count
== mark_object_loop_halt
)
4863 CHECK_ALLOCATED_AND_LIVE (live_float_p
);
4864 XMARK (XFLOAT (obj
)->type
);
4875 #undef CHECK_ALLOCATED
4876 #undef CHECK_ALLOCATED_AND_LIVE
4879 /* Mark the pointers in a buffer structure. */
4885 register struct buffer
*buffer
= XBUFFER (buf
);
4886 register Lisp_Object
*ptr
;
4887 Lisp_Object base_buffer
;
4889 /* This is the buffer's markbit */
4890 mark_object (&buffer
->name
);
4891 XMARK (buffer
->name
);
4893 MARK_INTERVAL_TREE (BUF_INTERVALS (buffer
));
4895 if (CONSP (buffer
->undo_list
))
4898 tail
= buffer
->undo_list
;
4900 while (CONSP (tail
))
4902 register struct Lisp_Cons
*ptr
= XCONS (tail
);
4904 if (XMARKBIT (ptr
->car
))
4907 if (GC_CONSP (ptr
->car
)
4908 && ! XMARKBIT (XCAR (ptr
->car
))
4909 && GC_MARKERP (XCAR (ptr
->car
)))
4911 XMARK (XCAR_AS_LVALUE (ptr
->car
));
4912 mark_object (&XCDR_AS_LVALUE (ptr
->car
));
4915 mark_object (&ptr
->car
);
4917 if (CONSP (ptr
->cdr
))
4923 mark_object (&XCDR_AS_LVALUE (tail
));
4926 mark_object (&buffer
->undo_list
);
4928 for (ptr
= &buffer
->name
+ 1;
4929 (char *)ptr
< (char *)buffer
+ sizeof (struct buffer
);
4933 /* If this is an indirect buffer, mark its base buffer. */
4934 if (buffer
->base_buffer
&& !XMARKBIT (buffer
->base_buffer
->name
))
4936 XSETBUFFER (base_buffer
, buffer
->base_buffer
);
4937 mark_buffer (base_buffer
);
4942 /* Mark the pointers in the kboard objects. */
4949 for (kb
= all_kboards
; kb
; kb
= kb
->next_kboard
)
4951 if (kb
->kbd_macro_buffer
)
4952 for (p
= kb
->kbd_macro_buffer
; p
< kb
->kbd_macro_ptr
; p
++)
4954 mark_object (&kb
->Voverriding_terminal_local_map
);
4955 mark_object (&kb
->Vlast_command
);
4956 mark_object (&kb
->Vreal_last_command
);
4957 mark_object (&kb
->Vprefix_arg
);
4958 mark_object (&kb
->Vlast_prefix_arg
);
4959 mark_object (&kb
->kbd_queue
);
4960 mark_object (&kb
->defining_kbd_macro
);
4961 mark_object (&kb
->Vlast_kbd_macro
);
4962 mark_object (&kb
->Vsystem_key_alist
);
4963 mark_object (&kb
->system_key_syms
);
4964 mark_object (&kb
->Vdefault_minibuffer_frame
);
4965 mark_object (&kb
->echo_string
);
4970 /* Value is non-zero if OBJ will survive the current GC because it's
4971 either marked or does not need to be marked to survive. */
4979 switch (XGCTYPE (obj
))
4986 survives_p
= XMARKBIT (XSYMBOL (obj
)->plist
);
4990 switch (XMISCTYPE (obj
))
4992 case Lisp_Misc_Marker
:
4993 survives_p
= XMARKBIT (obj
);
4996 case Lisp_Misc_Buffer_Local_Value
:
4997 case Lisp_Misc_Some_Buffer_Local_Value
:
4998 survives_p
= XMARKBIT (XBUFFER_LOCAL_VALUE (obj
)->realvalue
);
5001 case Lisp_Misc_Intfwd
:
5002 case Lisp_Misc_Boolfwd
:
5003 case Lisp_Misc_Objfwd
:
5004 case Lisp_Misc_Buffer_Objfwd
:
5005 case Lisp_Misc_Kboard_Objfwd
:
5009 case Lisp_Misc_Overlay
:
5010 survives_p
= XMARKBIT (XOVERLAY (obj
)->plist
);
5020 struct Lisp_String
*s
= XSTRING (obj
);
5021 survives_p
= STRING_MARKED_P (s
);
5025 case Lisp_Vectorlike
:
5026 if (GC_BUFFERP (obj
))
5027 survives_p
= XMARKBIT (XBUFFER (obj
)->name
);
5028 else if (GC_SUBRP (obj
))
5031 survives_p
= XVECTOR (obj
)->size
& ARRAY_MARK_FLAG
;
5035 survives_p
= XMARKBIT (XCAR (obj
));
5039 survives_p
= XMARKBIT (XFLOAT (obj
)->type
);
5046 return survives_p
|| PURE_POINTER_P ((void *) XPNTR (obj
));
5051 /* Sweep: find all structures not marked, and free them. */
5056 /* Remove or mark entries in weak hash tables.
5057 This must be done before any object is unmarked. */
5058 sweep_weak_hash_tables ();
5061 #ifdef GC_CHECK_STRING_BYTES
5062 if (!noninteractive
)
5063 check_string_bytes (1);
5066 /* Put all unmarked conses on free list */
5068 register struct cons_block
*cblk
;
5069 struct cons_block
**cprev
= &cons_block
;
5070 register int lim
= cons_block_index
;
5071 register int num_free
= 0, num_used
= 0;
5075 for (cblk
= cons_block
; cblk
; cblk
= *cprev
)
5079 for (i
= 0; i
< lim
; i
++)
5080 if (!XMARKBIT (cblk
->conses
[i
].car
))
5083 *(struct Lisp_Cons
**)&cblk
->conses
[i
].cdr
= cons_free_list
;
5084 cons_free_list
= &cblk
->conses
[i
];
5086 cons_free_list
->car
= Vdead
;
5092 XUNMARK (cblk
->conses
[i
].car
);
5094 lim
= CONS_BLOCK_SIZE
;
5095 /* If this block contains only free conses and we have already
5096 seen more than two blocks worth of free conses then deallocate
5098 if (this_free
== CONS_BLOCK_SIZE
&& num_free
> CONS_BLOCK_SIZE
)
5100 *cprev
= cblk
->next
;
5101 /* Unhook from the free list. */
5102 cons_free_list
= *(struct Lisp_Cons
**) &cblk
->conses
[0].cdr
;
5108 num_free
+= this_free
;
5109 cprev
= &cblk
->next
;
5112 total_conses
= num_used
;
5113 total_free_conses
= num_free
;
5116 /* Put all unmarked floats on free list */
5118 register struct float_block
*fblk
;
5119 struct float_block
**fprev
= &float_block
;
5120 register int lim
= float_block_index
;
5121 register int num_free
= 0, num_used
= 0;
5123 float_free_list
= 0;
5125 for (fblk
= float_block
; fblk
; fblk
= *fprev
)
5129 for (i
= 0; i
< lim
; i
++)
5130 if (!XMARKBIT (fblk
->floats
[i
].type
))
5133 *(struct Lisp_Float
**)&fblk
->floats
[i
].data
= float_free_list
;
5134 float_free_list
= &fblk
->floats
[i
];
5136 float_free_list
->type
= Vdead
;
5142 XUNMARK (fblk
->floats
[i
].type
);
5144 lim
= FLOAT_BLOCK_SIZE
;
5145 /* If this block contains only free floats and we have already
5146 seen more than two blocks worth of free floats then deallocate
5148 if (this_free
== FLOAT_BLOCK_SIZE
&& num_free
> FLOAT_BLOCK_SIZE
)
5150 *fprev
= fblk
->next
;
5151 /* Unhook from the free list. */
5152 float_free_list
= *(struct Lisp_Float
**) &fblk
->floats
[0].data
;
5158 num_free
+= this_free
;
5159 fprev
= &fblk
->next
;
5162 total_floats
= num_used
;
5163 total_free_floats
= num_free
;
5166 /* Put all unmarked intervals on free list */
5168 register struct interval_block
*iblk
;
5169 struct interval_block
**iprev
= &interval_block
;
5170 register int lim
= interval_block_index
;
5171 register int num_free
= 0, num_used
= 0;
5173 interval_free_list
= 0;
5175 for (iblk
= interval_block
; iblk
; iblk
= *iprev
)
5180 for (i
= 0; i
< lim
; i
++)
5182 if (! XMARKBIT (iblk
->intervals
[i
].plist
))
5184 SET_INTERVAL_PARENT (&iblk
->intervals
[i
], interval_free_list
);
5185 interval_free_list
= &iblk
->intervals
[i
];
5191 XUNMARK (iblk
->intervals
[i
].plist
);
5194 lim
= INTERVAL_BLOCK_SIZE
;
5195 /* If this block contains only free intervals and we have already
5196 seen more than two blocks worth of free intervals then
5197 deallocate this block. */
5198 if (this_free
== INTERVAL_BLOCK_SIZE
&& num_free
> INTERVAL_BLOCK_SIZE
)
5200 *iprev
= iblk
->next
;
5201 /* Unhook from the free list. */
5202 interval_free_list
= INTERVAL_PARENT (&iblk
->intervals
[0]);
5204 n_interval_blocks
--;
5208 num_free
+= this_free
;
5209 iprev
= &iblk
->next
;
5212 total_intervals
= num_used
;
5213 total_free_intervals
= num_free
;
5216 /* Put all unmarked symbols on free list */
5218 register struct symbol_block
*sblk
;
5219 struct symbol_block
**sprev
= &symbol_block
;
5220 register int lim
= symbol_block_index
;
5221 register int num_free
= 0, num_used
= 0;
5223 symbol_free_list
= NULL
;
5225 for (sblk
= symbol_block
; sblk
; sblk
= *sprev
)
5228 struct Lisp_Symbol
*sym
= sblk
->symbols
;
5229 struct Lisp_Symbol
*end
= sym
+ lim
;
5231 for (; sym
< end
; ++sym
)
5233 /* Check if the symbol was created during loadup. In such a case
5234 it might be pointed to by pure bytecode which we don't trace,
5235 so we conservatively assume that it is live. */
5236 int pure_p
= PURE_POINTER_P (XSTRING (sym
->xname
));
5238 if (!XMARKBIT (sym
->plist
) && !pure_p
)
5240 *(struct Lisp_Symbol
**) &sym
->value
= symbol_free_list
;
5241 symbol_free_list
= sym
;
5243 symbol_free_list
->function
= Vdead
;
5251 UNMARK_STRING (XSTRING (sym
->xname
));
5252 XUNMARK (sym
->plist
);
5256 lim
= SYMBOL_BLOCK_SIZE
;
5257 /* If this block contains only free symbols and we have already
5258 seen more than two blocks worth of free symbols then deallocate
5260 if (this_free
== SYMBOL_BLOCK_SIZE
&& num_free
> SYMBOL_BLOCK_SIZE
)
5262 *sprev
= sblk
->next
;
5263 /* Unhook from the free list. */
5264 symbol_free_list
= *(struct Lisp_Symbol
**)&sblk
->symbols
[0].value
;
5270 num_free
+= this_free
;
5271 sprev
= &sblk
->next
;
5274 total_symbols
= num_used
;
5275 total_free_symbols
= num_free
;
5278 /* Put all unmarked misc's on free list.
5279 For a marker, first unchain it from the buffer it points into. */
5281 register struct marker_block
*mblk
;
5282 struct marker_block
**mprev
= &marker_block
;
5283 register int lim
= marker_block_index
;
5284 register int num_free
= 0, num_used
= 0;
5286 marker_free_list
= 0;
5288 for (mblk
= marker_block
; mblk
; mblk
= *mprev
)
5292 EMACS_INT already_free
= -1;
5294 for (i
= 0; i
< lim
; i
++)
5296 Lisp_Object
*markword
;
5297 switch (mblk
->markers
[i
].u_marker
.type
)
5299 case Lisp_Misc_Marker
:
5300 markword
= &mblk
->markers
[i
].u_marker
.chain
;
5302 case Lisp_Misc_Buffer_Local_Value
:
5303 case Lisp_Misc_Some_Buffer_Local_Value
:
5304 markword
= &mblk
->markers
[i
].u_buffer_local_value
.realvalue
;
5306 case Lisp_Misc_Overlay
:
5307 markword
= &mblk
->markers
[i
].u_overlay
.plist
;
5309 case Lisp_Misc_Free
:
5310 /* If the object was already free, keep it
5311 on the free list. */
5312 markword
= (Lisp_Object
*) &already_free
;
5318 if (markword
&& !XMARKBIT (*markword
))
5321 if (mblk
->markers
[i
].u_marker
.type
== Lisp_Misc_Marker
)
5323 /* tem1 avoids Sun compiler bug */
5324 struct Lisp_Marker
*tem1
= &mblk
->markers
[i
].u_marker
;
5325 XSETMARKER (tem
, tem1
);
5326 unchain_marker (tem
);
5328 /* Set the type of the freed object to Lisp_Misc_Free.
5329 We could leave the type alone, since nobody checks it,
5330 but this might catch bugs faster. */
5331 mblk
->markers
[i
].u_marker
.type
= Lisp_Misc_Free
;
5332 mblk
->markers
[i
].u_free
.chain
= marker_free_list
;
5333 marker_free_list
= &mblk
->markers
[i
];
5340 XUNMARK (*markword
);
5343 lim
= MARKER_BLOCK_SIZE
;
5344 /* If this block contains only free markers and we have already
5345 seen more than two blocks worth of free markers then deallocate
5347 if (this_free
== MARKER_BLOCK_SIZE
&& num_free
> MARKER_BLOCK_SIZE
)
5349 *mprev
= mblk
->next
;
5350 /* Unhook from the free list. */
5351 marker_free_list
= mblk
->markers
[0].u_free
.chain
;
5357 num_free
+= this_free
;
5358 mprev
= &mblk
->next
;
5362 total_markers
= num_used
;
5363 total_free_markers
= num_free
;
5366 /* Free all unmarked buffers */
5368 register struct buffer
*buffer
= all_buffers
, *prev
= 0, *next
;
5371 if (!XMARKBIT (buffer
->name
))
5374 prev
->next
= buffer
->next
;
5376 all_buffers
= buffer
->next
;
5377 next
= buffer
->next
;
5383 XUNMARK (buffer
->name
);
5384 UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer
));
5385 prev
= buffer
, buffer
= buffer
->next
;
5389 /* Free all unmarked vectors */
5391 register struct Lisp_Vector
*vector
= all_vectors
, *prev
= 0, *next
;
5392 total_vector_size
= 0;
5395 if (!(vector
->size
& ARRAY_MARK_FLAG
))
5398 prev
->next
= vector
->next
;
5400 all_vectors
= vector
->next
;
5401 next
= vector
->next
;
5409 vector
->size
&= ~ARRAY_MARK_FLAG
;
5410 if (vector
->size
& PSEUDOVECTOR_FLAG
)
5411 total_vector_size
+= (PSEUDOVECTOR_SIZE_MASK
& vector
->size
);
5413 total_vector_size
+= vector
->size
;
5414 prev
= vector
, vector
= vector
->next
;
5418 #ifdef GC_CHECK_STRING_BYTES
5419 if (!noninteractive
)
5420 check_string_bytes (1);
5427 /* Debugging aids. */
5429 DEFUN ("memory-limit", Fmemory_limit
, Smemory_limit
, 0, 0, 0,
5430 doc
: /* Return the address of the last byte Emacs has allocated, divided by 1024.
5431 This may be helpful in debugging Emacs's memory usage.
5432 We divide the value by 1024 to make sure it fits in a Lisp integer. */)
5437 XSETINT (end
, (EMACS_INT
) sbrk (0) / 1024);
5442 DEFUN ("memory-use-counts", Fmemory_use_counts
, Smemory_use_counts
, 0, 0, 0,
5443 doc
: /* Return a list of counters that measure how much consing there has been.
5444 Each of these counters increments for a certain kind of object.
5445 The counters wrap around from the largest positive integer to zero.
5446 Garbage collection does not decrease them.
5447 The elements of the value are as follows:
5448 (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS STRINGS)
5449 All are in units of 1 = one object consed
5450 except for VECTOR-CELLS and STRING-CHARS, which count the total length of
5452 MISCS include overlays, markers, and some internal types.
5453 Frames, windows, buffers, and subprocesses count as vectors
5454 (but the contents of a buffer's text do not count here). */)
5457 Lisp_Object consed
[8];
5459 consed
[0] = make_number (min (MOST_POSITIVE_FIXNUM
, cons_cells_consed
));
5460 consed
[1] = make_number (min (MOST_POSITIVE_FIXNUM
, floats_consed
));
5461 consed
[2] = make_number (min (MOST_POSITIVE_FIXNUM
, vector_cells_consed
));
5462 consed
[3] = make_number (min (MOST_POSITIVE_FIXNUM
, symbols_consed
));
5463 consed
[4] = make_number (min (MOST_POSITIVE_FIXNUM
, string_chars_consed
));
5464 consed
[5] = make_number (min (MOST_POSITIVE_FIXNUM
, misc_objects_consed
));
5465 consed
[6] = make_number (min (MOST_POSITIVE_FIXNUM
, intervals_consed
));
5466 consed
[7] = make_number (min (MOST_POSITIVE_FIXNUM
, strings_consed
));
5468 return Flist (8, consed
);
5471 int suppress_checking
;
5473 die (msg
, file
, line
)
5478 fprintf (stderr
, "\r\nEmacs fatal error: %s:%d: %s\r\n",
5483 /* Initialization */
5488 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
5490 pure_size
= PURESIZE
;
5491 pure_bytes_used
= 0;
5492 pure_bytes_used_before_overflow
= 0;
5494 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
5496 Vdead
= make_pure_string ("DEAD", 4, 4, 0);
5500 ignore_warnings
= 1;
5501 #ifdef DOUG_LEA_MALLOC
5502 mallopt (M_TRIM_THRESHOLD
, 128*1024); /* trim threshold */
5503 mallopt (M_MMAP_THRESHOLD
, 64*1024); /* mmap threshold */
5504 mallopt (M_MMAP_MAX
, MMAP_MAX_AREAS
); /* max. number of mmap'ed areas */
5514 malloc_hysteresis
= 32;
5516 malloc_hysteresis
= 0;
5519 spare_memory
= (char *) malloc (SPARE_MEMORY
);
5521 ignore_warnings
= 0;
5523 byte_stack_list
= 0;
5525 consing_since_gc
= 0;
5526 gc_cons_threshold
= 100000 * sizeof (Lisp_Object
);
5527 #ifdef VIRT_ADDR_VARIES
5528 malloc_sbrk_unused
= 1<<22; /* A large number */
5529 malloc_sbrk_used
= 100000; /* as reasonable as any number */
5530 #endif /* VIRT_ADDR_VARIES */
5537 byte_stack_list
= 0;
5539 #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
5540 setjmp_tested_p
= longjmps_done
= 0;
5543 Vgc_elapsed
= make_float (0.0);
5550 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold
,
5551 doc
: /* *Number of bytes of consing between garbage collections.
5552 Garbage collection can happen automatically once this many bytes have been
5553 allocated since the last garbage collection. All data types count.
5555 Garbage collection happens automatically only when `eval' is called.
5557 By binding this temporarily to a large number, you can effectively
5558 prevent garbage collection during a part of the program. */);
5560 DEFVAR_INT ("pure-bytes-used", &pure_bytes_used
,
5561 doc
: /* Number of bytes of sharable Lisp data allocated so far. */);
5563 DEFVAR_INT ("cons-cells-consed", &cons_cells_consed
,
5564 doc
: /* Number of cons cells that have been consed so far. */);
5566 DEFVAR_INT ("floats-consed", &floats_consed
,
5567 doc
: /* Number of floats that have been consed so far. */);
5569 DEFVAR_INT ("vector-cells-consed", &vector_cells_consed
,
5570 doc
: /* Number of vector cells that have been consed so far. */);
5572 DEFVAR_INT ("symbols-consed", &symbols_consed
,
5573 doc
: /* Number of symbols that have been consed so far. */);
5575 DEFVAR_INT ("string-chars-consed", &string_chars_consed
,
5576 doc
: /* Number of string characters that have been consed so far. */);
5578 DEFVAR_INT ("misc-objects-consed", &misc_objects_consed
,
5579 doc
: /* Number of miscellaneous objects that have been consed so far. */);
5581 DEFVAR_INT ("intervals-consed", &intervals_consed
,
5582 doc
: /* Number of intervals that have been consed so far. */);
5584 DEFVAR_INT ("strings-consed", &strings_consed
,
5585 doc
: /* Number of strings that have been consed so far. */);
5587 DEFVAR_LISP ("purify-flag", &Vpurify_flag
,
5588 doc
: /* Non-nil means loading Lisp code in order to dump an executable.
5589 This means that certain objects should be allocated in shared (pure) space. */);
5591 DEFVAR_INT ("undo-limit", &undo_limit
,
5592 doc
: /* Keep no more undo information once it exceeds this size.
5593 This limit is applied when garbage collection happens.
5594 The size is counted as the number of bytes occupied,
5595 which includes both saved text and other data. */);
5598 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit
,
5599 doc
: /* Don't keep more than this much size of undo information.
5600 A command which pushes past this size is itself forgotten.
5601 This limit is applied when garbage collection happens.
5602 The size is counted as the number of bytes occupied,
5603 which includes both saved text and other data. */);
5604 undo_strong_limit
= 30000;
5606 DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages
,
5607 doc
: /* Non-nil means display messages at start and end of garbage collection. */);
5608 garbage_collection_messages
= 0;
5610 DEFVAR_LISP ("post-gc-hook", &Vpost_gc_hook
,
5611 doc
: /* Hook run after garbage collection has finished. */);
5612 Vpost_gc_hook
= Qnil
;
5613 Qpost_gc_hook
= intern ("post-gc-hook");
5614 staticpro (&Qpost_gc_hook
);
5616 DEFVAR_LISP ("memory-signal-data", &Vmemory_signal_data
,
5617 doc
: /* Precomputed `signal' argument for memory-full error. */);
5618 /* We build this in advance because if we wait until we need it, we might
5619 not be able to allocate the memory to hold it. */
5622 build_string ("Memory exhausted--use M-x save-some-buffers then exit and restart Emacs"));
5624 DEFVAR_LISP ("memory-full", &Vmemory_full
,
5625 doc
: /* Non-nil means we are handling a memory-full error. */);
5626 Vmemory_full
= Qnil
;
5628 staticpro (&Qgc_cons_threshold
);
5629 Qgc_cons_threshold
= intern ("gc-cons-threshold");
5631 staticpro (&Qchar_table_extra_slots
);
5632 Qchar_table_extra_slots
= intern ("char-table-extra-slots");
5634 DEFVAR_LISP ("gc-elapsed", &Vgc_elapsed
,
5635 doc
: /* Accumulated time elapsed in garbage collections.
5636 The time is in seconds as a floating point value.
5637 Programs may reset this to get statistics in a specific period. */);
5638 DEFVAR_INT ("gcs-done", &gcs_done
,
5639 doc
: /* Accumulated number of garbage collections done.
5640 Programs may reset this to get statistics in a specific period. */);
5645 defsubr (&Smake_byte_code
);
5646 defsubr (&Smake_list
);
5647 defsubr (&Smake_vector
);
5648 defsubr (&Smake_char_table
);
5649 defsubr (&Smake_string
);
5650 defsubr (&Smake_bool_vector
);
5651 defsubr (&Smake_symbol
);
5652 defsubr (&Smake_marker
);
5653 defsubr (&Spurecopy
);
5654 defsubr (&Sgarbage_collect
);
5655 defsubr (&Smemory_limit
);
5656 defsubr (&Smemory_use_counts
);
5658 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
5659 defsubr (&Sgc_status
);