(sh-font-lock-paren): Handle continued lines in patterns.
[emacs.git] / src / alloc.c
blob08bba475e767f5947e192c6ef33ec8d473740687
1 /* Storage allocation and gc for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 1986, 1988, 1993, 1994, 1995, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include <config.h>
23 #include <stdio.h>
24 #include <limits.h> /* For CHAR_BIT. */
26 #ifdef ALLOC_DEBUG
27 #undef INLINE
28 #endif
30 /* Note that this declares bzero on OSF/1. How dumb. */
32 #include <signal.h>
34 #ifdef HAVE_GTK_AND_PTHREAD
35 #include <pthread.h>
36 #endif
38 /* This file is part of the core Lisp implementation, and thus must
39 deal with the real data structures. If the Lisp implementation is
40 replaced, this file likely will not be used. */
42 #undef HIDE_LISP_IMPLEMENTATION
43 #include "lisp.h"
44 #include "process.h"
45 #include "intervals.h"
46 #include "puresize.h"
47 #include "buffer.h"
48 #include "window.h"
49 #include "keyboard.h"
50 #include "frame.h"
51 #include "blockinput.h"
52 #include "charset.h"
53 #include "syssignal.h"
54 #include <setjmp.h>
56 /* GC_MALLOC_CHECK defined means perform validity checks of malloc'd
57 memory. Can do this only if using gmalloc.c. */
59 #if defined SYSTEM_MALLOC || defined DOUG_LEA_MALLOC
60 #undef GC_MALLOC_CHECK
61 #endif
63 #ifdef HAVE_UNISTD_H
64 #include <unistd.h>
65 #else
66 extern POINTER_TYPE *sbrk ();
67 #endif
69 #ifdef DOUG_LEA_MALLOC
71 #include <malloc.h>
72 /* malloc.h #defines this as size_t, at least in glibc2. */
73 #ifndef __malloc_size_t
74 #define __malloc_size_t int
75 #endif
77 /* Specify maximum number of areas to mmap. It would be nice to use a
78 value that explicitly means "no limit". */
80 #define MMAP_MAX_AREAS 100000000
82 #else /* not DOUG_LEA_MALLOC */
84 /* The following come from gmalloc.c. */
86 #define __malloc_size_t size_t
87 extern __malloc_size_t _bytes_used;
88 extern __malloc_size_t __malloc_extra_blocks;
90 #endif /* not DOUG_LEA_MALLOC */
92 #if ! defined (SYSTEM_MALLOC) && defined (HAVE_GTK_AND_PTHREAD)
94 /* When GTK uses the file chooser dialog, different backends can be loaded
95 dynamically. One such a backend is the Gnome VFS backend that gets loaded
96 if you run Gnome. That backend creates several threads and also allocates
97 memory with malloc.
99 If Emacs sets malloc hooks (! SYSTEM_MALLOC) and the emacs_blocked_*
100 functions below are called from malloc, there is a chance that one
101 of these threads preempts the Emacs main thread and the hook variables
102 end up in an inconsistent state. So we have a mutex to prevent that (note
103 that the backend handles concurrent access to malloc within its own threads
104 but Emacs code running in the main thread is not included in that control).
106 When UNBLOCK_INPUT is called, reinvoke_input_signal may be called. If this
107 happens in one of the backend threads we will have two threads that tries
108 to run Emacs code at once, and the code is not prepared for that.
109 To prevent that, we only call BLOCK/UNBLOCK from the main thread. */
111 static pthread_mutex_t alloc_mutex;
113 #define BLOCK_INPUT_ALLOC \
114 do \
116 pthread_mutex_lock (&alloc_mutex); \
117 if (pthread_self () == main_thread) \
118 BLOCK_INPUT; \
120 while (0)
121 #define UNBLOCK_INPUT_ALLOC \
122 do \
124 if (pthread_self () == main_thread) \
125 UNBLOCK_INPUT; \
126 pthread_mutex_unlock (&alloc_mutex); \
128 while (0)
130 #else /* SYSTEM_MALLOC || not HAVE_GTK_AND_PTHREAD */
132 #define BLOCK_INPUT_ALLOC BLOCK_INPUT
133 #define UNBLOCK_INPUT_ALLOC UNBLOCK_INPUT
135 #endif /* SYSTEM_MALLOC || not HAVE_GTK_AND_PTHREAD */
137 /* Value of _bytes_used, when spare_memory was freed. */
139 static __malloc_size_t bytes_used_when_full;
141 static __malloc_size_t bytes_used_when_reconsidered;
143 /* Mark, unmark, query mark bit of a Lisp string. S must be a pointer
144 to a struct Lisp_String. */
146 #define MARK_STRING(S) ((S)->size |= ARRAY_MARK_FLAG)
147 #define UNMARK_STRING(S) ((S)->size &= ~ARRAY_MARK_FLAG)
148 #define STRING_MARKED_P(S) (((S)->size & ARRAY_MARK_FLAG) != 0)
150 #define VECTOR_MARK(V) ((V)->size |= ARRAY_MARK_FLAG)
151 #define VECTOR_UNMARK(V) ((V)->size &= ~ARRAY_MARK_FLAG)
152 #define VECTOR_MARKED_P(V) (((V)->size & ARRAY_MARK_FLAG) != 0)
154 /* Value is the number of bytes/chars of S, a pointer to a struct
155 Lisp_String. This must be used instead of STRING_BYTES (S) or
156 S->size during GC, because S->size contains the mark bit for
157 strings. */
159 #define GC_STRING_BYTES(S) (STRING_BYTES (S))
160 #define GC_STRING_CHARS(S) ((S)->size & ~ARRAY_MARK_FLAG)
162 /* Number of bytes of consing done since the last gc. */
164 int consing_since_gc;
166 /* Count the amount of consing of various sorts of space. */
168 EMACS_INT cons_cells_consed;
169 EMACS_INT floats_consed;
170 EMACS_INT vector_cells_consed;
171 EMACS_INT symbols_consed;
172 EMACS_INT string_chars_consed;
173 EMACS_INT misc_objects_consed;
174 EMACS_INT intervals_consed;
175 EMACS_INT strings_consed;
177 /* Minimum number of bytes of consing since GC before next GC. */
179 EMACS_INT gc_cons_threshold;
181 /* Similar minimum, computed from Vgc_cons_percentage. */
183 EMACS_INT gc_relative_threshold;
185 static Lisp_Object Vgc_cons_percentage;
187 /* Minimum number of bytes of consing since GC before next GC,
188 when memory is full. */
190 EMACS_INT memory_full_cons_threshold;
192 /* Nonzero during GC. */
194 int gc_in_progress;
196 /* Nonzero means abort if try to GC.
197 This is for code which is written on the assumption that
198 no GC will happen, so as to verify that assumption. */
200 int abort_on_gc;
202 /* Nonzero means display messages at beginning and end of GC. */
204 int garbage_collection_messages;
206 #ifndef VIRT_ADDR_VARIES
207 extern
208 #endif /* VIRT_ADDR_VARIES */
209 int malloc_sbrk_used;
211 #ifndef VIRT_ADDR_VARIES
212 extern
213 #endif /* VIRT_ADDR_VARIES */
214 int malloc_sbrk_unused;
216 /* Number of live and free conses etc. */
218 static int total_conses, total_markers, total_symbols, total_vector_size;
219 static int total_free_conses, total_free_markers, total_free_symbols;
220 static int total_free_floats, total_floats;
222 /* Points to memory space allocated as "spare", to be freed if we run
223 out of memory. We keep one large block, four cons-blocks, and
224 two string blocks. */
226 char *spare_memory[7];
228 /* Amount of spare memory to keep in large reserve block. */
230 #define SPARE_MEMORY (1 << 14)
232 /* Number of extra blocks malloc should get when it needs more core. */
234 static int malloc_hysteresis;
236 /* Non-nil means defun should do purecopy on the function definition. */
238 Lisp_Object Vpurify_flag;
240 /* Non-nil means we are handling a memory-full error. */
242 Lisp_Object Vmemory_full;
244 #ifndef HAVE_SHM
246 /* Initialize it to a nonzero value to force it into data space
247 (rather than bss space). That way unexec will remap it into text
248 space (pure), on some systems. We have not implemented the
249 remapping on more recent systems because this is less important
250 nowadays than in the days of small memories and timesharing. */
252 EMACS_INT pure[PURESIZE / sizeof (EMACS_INT)] = {1,};
253 #define PUREBEG (char *) pure
255 #else /* HAVE_SHM */
257 #define pure PURE_SEG_BITS /* Use shared memory segment */
258 #define PUREBEG (char *)PURE_SEG_BITS
260 #endif /* HAVE_SHM */
262 /* Pointer to the pure area, and its size. */
264 static char *purebeg;
265 static size_t pure_size;
267 /* Number of bytes of pure storage used before pure storage overflowed.
268 If this is non-zero, this implies that an overflow occurred. */
270 static size_t pure_bytes_used_before_overflow;
272 /* Value is non-zero if P points into pure space. */
274 #define PURE_POINTER_P(P) \
275 (((PNTR_COMPARISON_TYPE) (P) \
276 < (PNTR_COMPARISON_TYPE) ((char *) purebeg + pure_size)) \
277 && ((PNTR_COMPARISON_TYPE) (P) \
278 >= (PNTR_COMPARISON_TYPE) purebeg))
280 /* Index in pure at which next pure object will be allocated.. */
282 EMACS_INT pure_bytes_used;
284 /* If nonzero, this is a warning delivered by malloc and not yet
285 displayed. */
287 char *pending_malloc_warning;
289 /* Pre-computed signal argument for use when memory is exhausted. */
291 Lisp_Object Vmemory_signal_data;
293 /* Maximum amount of C stack to save when a GC happens. */
295 #ifndef MAX_SAVE_STACK
296 #define MAX_SAVE_STACK 16000
297 #endif
299 /* Buffer in which we save a copy of the C stack at each GC. */
301 char *stack_copy;
302 int stack_copy_size;
304 /* Non-zero means ignore malloc warnings. Set during initialization.
305 Currently not used. */
307 int ignore_warnings;
309 Lisp_Object Qgc_cons_threshold, Qchar_table_extra_slots;
311 /* Hook run after GC has finished. */
313 Lisp_Object Vpost_gc_hook, Qpost_gc_hook;
315 Lisp_Object Vgc_elapsed; /* accumulated elapsed time in GC */
316 EMACS_INT gcs_done; /* accumulated GCs */
318 static void mark_buffer P_ ((Lisp_Object));
319 extern void mark_kboards P_ ((void));
320 extern void mark_backtrace P_ ((void));
321 static void gc_sweep P_ ((void));
322 static void mark_glyph_matrix P_ ((struct glyph_matrix *));
323 static void mark_face_cache P_ ((struct face_cache *));
325 #ifdef HAVE_WINDOW_SYSTEM
326 extern void mark_fringe_data P_ ((void));
327 static void mark_image P_ ((struct image *));
328 static void mark_image_cache P_ ((struct frame *));
329 #endif /* HAVE_WINDOW_SYSTEM */
331 static struct Lisp_String *allocate_string P_ ((void));
332 static void compact_small_strings P_ ((void));
333 static void free_large_strings P_ ((void));
334 static void sweep_strings P_ ((void));
336 extern int message_enable_multibyte;
338 /* When scanning the C stack for live Lisp objects, Emacs keeps track
339 of what memory allocated via lisp_malloc is intended for what
340 purpose. This enumeration specifies the type of memory. */
342 enum mem_type
344 MEM_TYPE_NON_LISP,
345 MEM_TYPE_BUFFER,
346 MEM_TYPE_CONS,
347 MEM_TYPE_STRING,
348 MEM_TYPE_MISC,
349 MEM_TYPE_SYMBOL,
350 MEM_TYPE_FLOAT,
351 /* Keep the following vector-like types together, with
352 MEM_TYPE_WINDOW being the last, and MEM_TYPE_VECTOR the
353 first. Or change the code of live_vector_p, for instance. */
354 MEM_TYPE_VECTOR,
355 MEM_TYPE_PROCESS,
356 MEM_TYPE_HASH_TABLE,
357 MEM_TYPE_FRAME,
358 MEM_TYPE_WINDOW
361 static POINTER_TYPE *lisp_align_malloc P_ ((size_t, enum mem_type));
362 static POINTER_TYPE *lisp_malloc P_ ((size_t, enum mem_type));
363 void refill_memory_reserve ();
366 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
368 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
369 #include <stdio.h> /* For fprintf. */
370 #endif
372 /* A unique object in pure space used to make some Lisp objects
373 on free lists recognizable in O(1). */
375 Lisp_Object Vdead;
377 #ifdef GC_MALLOC_CHECK
379 enum mem_type allocated_mem_type;
380 int dont_register_blocks;
382 #endif /* GC_MALLOC_CHECK */
384 /* A node in the red-black tree describing allocated memory containing
385 Lisp data. Each such block is recorded with its start and end
386 address when it is allocated, and removed from the tree when it
387 is freed.
389 A red-black tree is a balanced binary tree with the following
390 properties:
392 1. Every node is either red or black.
393 2. Every leaf is black.
394 3. If a node is red, then both of its children are black.
395 4. Every simple path from a node to a descendant leaf contains
396 the same number of black nodes.
397 5. The root is always black.
399 When nodes are inserted into the tree, or deleted from the tree,
400 the tree is "fixed" so that these properties are always true.
402 A red-black tree with N internal nodes has height at most 2
403 log(N+1). Searches, insertions and deletions are done in O(log N).
404 Please see a text book about data structures for a detailed
405 description of red-black trees. Any book worth its salt should
406 describe them. */
408 struct mem_node
410 /* Children of this node. These pointers are never NULL. When there
411 is no child, the value is MEM_NIL, which points to a dummy node. */
412 struct mem_node *left, *right;
414 /* The parent of this node. In the root node, this is NULL. */
415 struct mem_node *parent;
417 /* Start and end of allocated region. */
418 void *start, *end;
420 /* Node color. */
421 enum {MEM_BLACK, MEM_RED} color;
423 /* Memory type. */
424 enum mem_type type;
427 /* Base address of stack. Set in main. */
429 Lisp_Object *stack_base;
431 /* Root of the tree describing allocated Lisp memory. */
433 static struct mem_node *mem_root;
435 /* Lowest and highest known address in the heap. */
437 static void *min_heap_address, *max_heap_address;
439 /* Sentinel node of the tree. */
441 static struct mem_node mem_z;
442 #define MEM_NIL &mem_z
444 static POINTER_TYPE *lisp_malloc P_ ((size_t, enum mem_type));
445 static struct Lisp_Vector *allocate_vectorlike P_ ((EMACS_INT, enum mem_type));
446 static void lisp_free P_ ((POINTER_TYPE *));
447 static void mark_stack P_ ((void));
448 static int live_vector_p P_ ((struct mem_node *, void *));
449 static int live_buffer_p P_ ((struct mem_node *, void *));
450 static int live_string_p P_ ((struct mem_node *, void *));
451 static int live_cons_p P_ ((struct mem_node *, void *));
452 static int live_symbol_p P_ ((struct mem_node *, void *));
453 static int live_float_p P_ ((struct mem_node *, void *));
454 static int live_misc_p P_ ((struct mem_node *, void *));
455 static void mark_maybe_object P_ ((Lisp_Object));
456 static void mark_memory P_ ((void *, void *));
457 static void mem_init P_ ((void));
458 static struct mem_node *mem_insert P_ ((void *, void *, enum mem_type));
459 static void mem_insert_fixup P_ ((struct mem_node *));
460 static void mem_rotate_left P_ ((struct mem_node *));
461 static void mem_rotate_right P_ ((struct mem_node *));
462 static void mem_delete P_ ((struct mem_node *));
463 static void mem_delete_fixup P_ ((struct mem_node *));
464 static INLINE struct mem_node *mem_find P_ ((void *));
467 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
468 static void check_gcpros P_ ((void));
469 #endif
471 #endif /* GC_MARK_STACK || GC_MALLOC_CHECK */
473 /* Recording what needs to be marked for gc. */
475 struct gcpro *gcprolist;
477 /* Addresses of staticpro'd variables. Initialize it to a nonzero
478 value; otherwise some compilers put it into BSS. */
480 #define NSTATICS 1280
481 Lisp_Object *staticvec[NSTATICS] = {&Vpurify_flag};
483 /* Index of next unused slot in staticvec. */
485 int staticidx = 0;
487 static POINTER_TYPE *pure_alloc P_ ((size_t, int));
490 /* Value is SZ rounded up to the next multiple of ALIGNMENT.
491 ALIGNMENT must be a power of 2. */
493 #define ALIGN(ptr, ALIGNMENT) \
494 ((POINTER_TYPE *) ((((EMACS_UINT)(ptr)) + (ALIGNMENT) - 1) \
495 & ~((ALIGNMENT) - 1)))
499 /************************************************************************
500 Malloc
501 ************************************************************************/
503 /* Function malloc calls this if it finds we are near exhausting storage. */
505 void
506 malloc_warning (str)
507 char *str;
509 pending_malloc_warning = str;
513 /* Display an already-pending malloc warning. */
515 void
516 display_malloc_warning ()
518 call3 (intern ("display-warning"),
519 intern ("alloc"),
520 build_string (pending_malloc_warning),
521 intern ("emergency"));
522 pending_malloc_warning = 0;
526 #ifdef DOUG_LEA_MALLOC
527 # define BYTES_USED (mallinfo ().uordblks)
528 #else
529 # define BYTES_USED _bytes_used
530 #endif
532 /* Called if we can't allocate relocatable space for a buffer. */
534 void
535 buffer_memory_full ()
537 /* If buffers use the relocating allocator, no need to free
538 spare_memory, because we may have plenty of malloc space left
539 that we could get, and if we don't, the malloc that fails will
540 itself cause spare_memory to be freed. If buffers don't use the
541 relocating allocator, treat this like any other failing
542 malloc. */
544 #ifndef REL_ALLOC
545 memory_full ();
546 #endif
548 /* This used to call error, but if we've run out of memory, we could
549 get infinite recursion trying to build the string. */
550 while (1)
551 Fsignal (Qnil, Vmemory_signal_data);
555 #ifdef XMALLOC_OVERRUN_CHECK
557 /* Check for overrun in malloc'ed buffers by wrapping a 16 byte header
558 and a 16 byte trailer around each block.
560 The header consists of 12 fixed bytes + a 4 byte integer contaning the
561 original block size, while the trailer consists of 16 fixed bytes.
563 The header is used to detect whether this block has been allocated
564 through these functions -- as it seems that some low-level libc
565 functions may bypass the malloc hooks.
569 #define XMALLOC_OVERRUN_CHECK_SIZE 16
571 static char xmalloc_overrun_check_header[XMALLOC_OVERRUN_CHECK_SIZE-4] =
572 { 0x9a, 0x9b, 0xae, 0xaf,
573 0xbf, 0xbe, 0xce, 0xcf,
574 0xea, 0xeb, 0xec, 0xed };
576 static char xmalloc_overrun_check_trailer[XMALLOC_OVERRUN_CHECK_SIZE] =
577 { 0xaa, 0xab, 0xac, 0xad,
578 0xba, 0xbb, 0xbc, 0xbd,
579 0xca, 0xcb, 0xcc, 0xcd,
580 0xda, 0xdb, 0xdc, 0xdd };
582 /* Macros to insert and extract the block size in the header. */
584 #define XMALLOC_PUT_SIZE(ptr, size) \
585 (ptr[-1] = (size & 0xff), \
586 ptr[-2] = ((size >> 8) & 0xff), \
587 ptr[-3] = ((size >> 16) & 0xff), \
588 ptr[-4] = ((size >> 24) & 0xff))
590 #define XMALLOC_GET_SIZE(ptr) \
591 (size_t)((unsigned)(ptr[-1]) | \
592 ((unsigned)(ptr[-2]) << 8) | \
593 ((unsigned)(ptr[-3]) << 16) | \
594 ((unsigned)(ptr[-4]) << 24))
597 /* The call depth in overrun_check functions. For example, this might happen:
598 xmalloc()
599 overrun_check_malloc()
600 -> malloc -> (via hook)_-> emacs_blocked_malloc
601 -> overrun_check_malloc
602 call malloc (hooks are NULL, so real malloc is called).
603 malloc returns 10000.
604 add overhead, return 10016.
605 <- (back in overrun_check_malloc)
606 add overhead again, return 10032
607 xmalloc returns 10032.
609 (time passes).
611 xfree(10032)
612 overrun_check_free(10032)
613 decrease overhed
614 free(10016) <- crash, because 10000 is the original pointer. */
616 static int check_depth;
618 /* Like malloc, but wraps allocated block with header and trailer. */
620 POINTER_TYPE *
621 overrun_check_malloc (size)
622 size_t size;
624 register unsigned char *val;
625 size_t overhead = ++check_depth == 1 ? XMALLOC_OVERRUN_CHECK_SIZE*2 : 0;
627 val = (unsigned char *) malloc (size + overhead);
628 if (val && check_depth == 1)
630 bcopy (xmalloc_overrun_check_header, val, XMALLOC_OVERRUN_CHECK_SIZE - 4);
631 val += XMALLOC_OVERRUN_CHECK_SIZE;
632 XMALLOC_PUT_SIZE(val, size);
633 bcopy (xmalloc_overrun_check_trailer, val + size, XMALLOC_OVERRUN_CHECK_SIZE);
635 --check_depth;
636 return (POINTER_TYPE *)val;
640 /* Like realloc, but checks old block for overrun, and wraps new block
641 with header and trailer. */
643 POINTER_TYPE *
644 overrun_check_realloc (block, size)
645 POINTER_TYPE *block;
646 size_t size;
648 register unsigned char *val = (unsigned char *)block;
649 size_t overhead = ++check_depth == 1 ? XMALLOC_OVERRUN_CHECK_SIZE*2 : 0;
651 if (val
652 && check_depth == 1
653 && bcmp (xmalloc_overrun_check_header,
654 val - XMALLOC_OVERRUN_CHECK_SIZE,
655 XMALLOC_OVERRUN_CHECK_SIZE - 4) == 0)
657 size_t osize = XMALLOC_GET_SIZE (val);
658 if (bcmp (xmalloc_overrun_check_trailer,
659 val + osize,
660 XMALLOC_OVERRUN_CHECK_SIZE))
661 abort ();
662 bzero (val + osize, XMALLOC_OVERRUN_CHECK_SIZE);
663 val -= XMALLOC_OVERRUN_CHECK_SIZE;
664 bzero (val, XMALLOC_OVERRUN_CHECK_SIZE);
667 val = (unsigned char *) realloc ((POINTER_TYPE *)val, size + overhead);
669 if (val && check_depth == 1)
671 bcopy (xmalloc_overrun_check_header, val, XMALLOC_OVERRUN_CHECK_SIZE - 4);
672 val += XMALLOC_OVERRUN_CHECK_SIZE;
673 XMALLOC_PUT_SIZE(val, size);
674 bcopy (xmalloc_overrun_check_trailer, val + size, XMALLOC_OVERRUN_CHECK_SIZE);
676 --check_depth;
677 return (POINTER_TYPE *)val;
680 /* Like free, but checks block for overrun. */
682 void
683 overrun_check_free (block)
684 POINTER_TYPE *block;
686 unsigned char *val = (unsigned char *)block;
688 ++check_depth;
689 if (val
690 && check_depth == 1
691 && bcmp (xmalloc_overrun_check_header,
692 val - XMALLOC_OVERRUN_CHECK_SIZE,
693 XMALLOC_OVERRUN_CHECK_SIZE - 4) == 0)
695 size_t osize = XMALLOC_GET_SIZE (val);
696 if (bcmp (xmalloc_overrun_check_trailer,
697 val + osize,
698 XMALLOC_OVERRUN_CHECK_SIZE))
699 abort ();
700 #ifdef XMALLOC_CLEAR_FREE_MEMORY
701 val -= XMALLOC_OVERRUN_CHECK_SIZE;
702 memset (val, 0xff, osize + XMALLOC_OVERRUN_CHECK_SIZE*2);
703 #else
704 bzero (val + osize, XMALLOC_OVERRUN_CHECK_SIZE);
705 val -= XMALLOC_OVERRUN_CHECK_SIZE;
706 bzero (val, XMALLOC_OVERRUN_CHECK_SIZE);
707 #endif
710 free (val);
711 --check_depth;
714 #undef malloc
715 #undef realloc
716 #undef free
717 #define malloc overrun_check_malloc
718 #define realloc overrun_check_realloc
719 #define free overrun_check_free
720 #endif
723 /* Like malloc but check for no memory and block interrupt input.. */
725 POINTER_TYPE *
726 xmalloc (size)
727 size_t size;
729 register POINTER_TYPE *val;
731 BLOCK_INPUT;
732 val = (POINTER_TYPE *) malloc (size);
733 UNBLOCK_INPUT;
735 if (!val && size)
736 memory_full ();
737 return val;
741 /* Like realloc but check for no memory and block interrupt input.. */
743 POINTER_TYPE *
744 xrealloc (block, size)
745 POINTER_TYPE *block;
746 size_t size;
748 register POINTER_TYPE *val;
750 BLOCK_INPUT;
751 /* We must call malloc explicitly when BLOCK is 0, since some
752 reallocs don't do this. */
753 if (! block)
754 val = (POINTER_TYPE *) malloc (size);
755 else
756 val = (POINTER_TYPE *) realloc (block, size);
757 UNBLOCK_INPUT;
759 if (!val && size) memory_full ();
760 return val;
764 /* Like free but block interrupt input. */
766 void
767 xfree (block)
768 POINTER_TYPE *block;
770 BLOCK_INPUT;
771 free (block);
772 UNBLOCK_INPUT;
773 /* We don't call refill_memory_reserve here
774 because that duplicates doing so in emacs_blocked_free
775 and the criterion should go there. */
779 /* Like strdup, but uses xmalloc. */
781 char *
782 xstrdup (s)
783 const char *s;
785 size_t len = strlen (s) + 1;
786 char *p = (char *) xmalloc (len);
787 bcopy (s, p, len);
788 return p;
792 /* Unwind for SAFE_ALLOCA */
794 Lisp_Object
795 safe_alloca_unwind (arg)
796 Lisp_Object arg;
798 register struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
800 p->dogc = 0;
801 xfree (p->pointer);
802 p->pointer = 0;
803 free_misc (arg);
804 return Qnil;
808 /* Like malloc but used for allocating Lisp data. NBYTES is the
809 number of bytes to allocate, TYPE describes the intended use of the
810 allcated memory block (for strings, for conses, ...). */
812 #ifndef USE_LSB_TAG
813 static void *lisp_malloc_loser;
814 #endif
816 static POINTER_TYPE *
817 lisp_malloc (nbytes, type)
818 size_t nbytes;
819 enum mem_type type;
821 register void *val;
823 BLOCK_INPUT;
825 #ifdef GC_MALLOC_CHECK
826 allocated_mem_type = type;
827 #endif
829 val = (void *) malloc (nbytes);
831 #ifndef USE_LSB_TAG
832 /* If the memory just allocated cannot be addressed thru a Lisp
833 object's pointer, and it needs to be,
834 that's equivalent to running out of memory. */
835 if (val && type != MEM_TYPE_NON_LISP)
837 Lisp_Object tem;
838 XSETCONS (tem, (char *) val + nbytes - 1);
839 if ((char *) XCONS (tem) != (char *) val + nbytes - 1)
841 lisp_malloc_loser = val;
842 free (val);
843 val = 0;
846 #endif
848 #if GC_MARK_STACK && !defined GC_MALLOC_CHECK
849 if (val && type != MEM_TYPE_NON_LISP)
850 mem_insert (val, (char *) val + nbytes, type);
851 #endif
853 UNBLOCK_INPUT;
854 if (!val && nbytes)
855 memory_full ();
856 return val;
859 /* Free BLOCK. This must be called to free memory allocated with a
860 call to lisp_malloc. */
862 static void
863 lisp_free (block)
864 POINTER_TYPE *block;
866 BLOCK_INPUT;
867 free (block);
868 #if GC_MARK_STACK && !defined GC_MALLOC_CHECK
869 mem_delete (mem_find (block));
870 #endif
871 UNBLOCK_INPUT;
874 /* Allocation of aligned blocks of memory to store Lisp data. */
875 /* The entry point is lisp_align_malloc which returns blocks of at most */
876 /* BLOCK_BYTES and guarantees they are aligned on a BLOCK_ALIGN boundary. */
879 /* BLOCK_ALIGN has to be a power of 2. */
880 #define BLOCK_ALIGN (1 << 10)
882 /* Padding to leave at the end of a malloc'd block. This is to give
883 malloc a chance to minimize the amount of memory wasted to alignment.
884 It should be tuned to the particular malloc library used.
885 On glibc-2.3.2, malloc never tries to align, so a padding of 0 is best.
886 posix_memalign on the other hand would ideally prefer a value of 4
887 because otherwise, there's 1020 bytes wasted between each ablocks.
888 In Emacs, testing shows that those 1020 can most of the time be
889 efficiently used by malloc to place other objects, so a value of 0 can
890 still preferable unless you have a lot of aligned blocks and virtually
891 nothing else. */
892 #define BLOCK_PADDING 0
893 #define BLOCK_BYTES \
894 (BLOCK_ALIGN - sizeof (struct ablock *) - BLOCK_PADDING)
896 /* Internal data structures and constants. */
898 #define ABLOCKS_SIZE 16
900 /* An aligned block of memory. */
901 struct ablock
903 union
905 char payload[BLOCK_BYTES];
906 struct ablock *next_free;
907 } x;
908 /* `abase' is the aligned base of the ablocks. */
909 /* It is overloaded to hold the virtual `busy' field that counts
910 the number of used ablock in the parent ablocks.
911 The first ablock has the `busy' field, the others have the `abase'
912 field. To tell the difference, we assume that pointers will have
913 integer values larger than 2 * ABLOCKS_SIZE. The lowest bit of `busy'
914 is used to tell whether the real base of the parent ablocks is `abase'
915 (if not, the word before the first ablock holds a pointer to the
916 real base). */
917 struct ablocks *abase;
918 /* The padding of all but the last ablock is unused. The padding of
919 the last ablock in an ablocks is not allocated. */
920 #if BLOCK_PADDING
921 char padding[BLOCK_PADDING];
922 #endif
925 /* A bunch of consecutive aligned blocks. */
926 struct ablocks
928 struct ablock blocks[ABLOCKS_SIZE];
931 /* Size of the block requested from malloc or memalign. */
932 #define ABLOCKS_BYTES (sizeof (struct ablocks) - BLOCK_PADDING)
934 #define ABLOCK_ABASE(block) \
935 (((unsigned long) (block)->abase) <= (1 + 2 * ABLOCKS_SIZE) \
936 ? (struct ablocks *)(block) \
937 : (block)->abase)
939 /* Virtual `busy' field. */
940 #define ABLOCKS_BUSY(abase) ((abase)->blocks[0].abase)
942 /* Pointer to the (not necessarily aligned) malloc block. */
943 #ifdef HAVE_POSIX_MEMALIGN
944 #define ABLOCKS_BASE(abase) (abase)
945 #else
946 #define ABLOCKS_BASE(abase) \
947 (1 & (long) ABLOCKS_BUSY (abase) ? abase : ((void**)abase)[-1])
948 #endif
950 /* The list of free ablock. */
951 static struct ablock *free_ablock;
953 /* Allocate an aligned block of nbytes.
954 Alignment is on a multiple of BLOCK_ALIGN and `nbytes' has to be
955 smaller or equal to BLOCK_BYTES. */
956 static POINTER_TYPE *
957 lisp_align_malloc (nbytes, type)
958 size_t nbytes;
959 enum mem_type type;
961 void *base, *val;
962 struct ablocks *abase;
964 eassert (nbytes <= BLOCK_BYTES);
966 BLOCK_INPUT;
968 #ifdef GC_MALLOC_CHECK
969 allocated_mem_type = type;
970 #endif
972 if (!free_ablock)
974 int i;
975 EMACS_INT aligned; /* int gets warning casting to 64-bit pointer. */
977 #ifdef DOUG_LEA_MALLOC
978 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed
979 because mapped region contents are not preserved in
980 a dumped Emacs. */
981 mallopt (M_MMAP_MAX, 0);
982 #endif
984 #ifdef HAVE_POSIX_MEMALIGN
986 int err = posix_memalign (&base, BLOCK_ALIGN, ABLOCKS_BYTES);
987 if (err)
988 base = NULL;
989 abase = base;
991 #else
992 base = malloc (ABLOCKS_BYTES);
993 abase = ALIGN (base, BLOCK_ALIGN);
994 #endif
996 if (base == 0)
998 UNBLOCK_INPUT;
999 memory_full ();
1002 aligned = (base == abase);
1003 if (!aligned)
1004 ((void**)abase)[-1] = base;
1006 #ifdef DOUG_LEA_MALLOC
1007 /* Back to a reasonable maximum of mmap'ed areas. */
1008 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
1009 #endif
1011 #ifndef USE_LSB_TAG
1012 /* If the memory just allocated cannot be addressed thru a Lisp
1013 object's pointer, and it needs to be, that's equivalent to
1014 running out of memory. */
1015 if (type != MEM_TYPE_NON_LISP)
1017 Lisp_Object tem;
1018 char *end = (char *) base + ABLOCKS_BYTES - 1;
1019 XSETCONS (tem, end);
1020 if ((char *) XCONS (tem) != end)
1022 lisp_malloc_loser = base;
1023 free (base);
1024 UNBLOCK_INPUT;
1025 memory_full ();
1028 #endif
1030 /* Initialize the blocks and put them on the free list.
1031 Is `base' was not properly aligned, we can't use the last block. */
1032 for (i = 0; i < (aligned ? ABLOCKS_SIZE : ABLOCKS_SIZE - 1); i++)
1034 abase->blocks[i].abase = abase;
1035 abase->blocks[i].x.next_free = free_ablock;
1036 free_ablock = &abase->blocks[i];
1038 ABLOCKS_BUSY (abase) = (struct ablocks *) (long) aligned;
1040 eassert (0 == ((EMACS_UINT)abase) % BLOCK_ALIGN);
1041 eassert (ABLOCK_ABASE (&abase->blocks[3]) == abase); /* 3 is arbitrary */
1042 eassert (ABLOCK_ABASE (&abase->blocks[0]) == abase);
1043 eassert (ABLOCKS_BASE (abase) == base);
1044 eassert (aligned == (long) ABLOCKS_BUSY (abase));
1047 abase = ABLOCK_ABASE (free_ablock);
1048 ABLOCKS_BUSY (abase) = (struct ablocks *) (2 + (long) ABLOCKS_BUSY (abase));
1049 val = free_ablock;
1050 free_ablock = free_ablock->x.next_free;
1052 #if GC_MARK_STACK && !defined GC_MALLOC_CHECK
1053 if (val && type != MEM_TYPE_NON_LISP)
1054 mem_insert (val, (char *) val + nbytes, type);
1055 #endif
1057 UNBLOCK_INPUT;
1058 if (!val && nbytes)
1059 memory_full ();
1061 eassert (0 == ((EMACS_UINT)val) % BLOCK_ALIGN);
1062 return val;
1065 static void
1066 lisp_align_free (block)
1067 POINTER_TYPE *block;
1069 struct ablock *ablock = block;
1070 struct ablocks *abase = ABLOCK_ABASE (ablock);
1072 BLOCK_INPUT;
1073 #if GC_MARK_STACK && !defined GC_MALLOC_CHECK
1074 mem_delete (mem_find (block));
1075 #endif
1076 /* Put on free list. */
1077 ablock->x.next_free = free_ablock;
1078 free_ablock = ablock;
1079 /* Update busy count. */
1080 ABLOCKS_BUSY (abase) = (struct ablocks *) (-2 + (long) ABLOCKS_BUSY (abase));
1082 if (2 > (long) ABLOCKS_BUSY (abase))
1083 { /* All the blocks are free. */
1084 int i = 0, aligned = (long) ABLOCKS_BUSY (abase);
1085 struct ablock **tem = &free_ablock;
1086 struct ablock *atop = &abase->blocks[aligned ? ABLOCKS_SIZE : ABLOCKS_SIZE - 1];
1088 while (*tem)
1090 if (*tem >= (struct ablock *) abase && *tem < atop)
1092 i++;
1093 *tem = (*tem)->x.next_free;
1095 else
1096 tem = &(*tem)->x.next_free;
1098 eassert ((aligned & 1) == aligned);
1099 eassert (i == (aligned ? ABLOCKS_SIZE : ABLOCKS_SIZE - 1));
1100 free (ABLOCKS_BASE (abase));
1102 UNBLOCK_INPUT;
1105 /* Return a new buffer structure allocated from the heap with
1106 a call to lisp_malloc. */
1108 struct buffer *
1109 allocate_buffer ()
1111 struct buffer *b
1112 = (struct buffer *) lisp_malloc (sizeof (struct buffer),
1113 MEM_TYPE_BUFFER);
1114 return b;
1118 #ifndef SYSTEM_MALLOC
1120 /* Arranging to disable input signals while we're in malloc.
1122 This only works with GNU malloc. To help out systems which can't
1123 use GNU malloc, all the calls to malloc, realloc, and free
1124 elsewhere in the code should be inside a BLOCK_INPUT/UNBLOCK_INPUT
1125 pair; unfortunately, we have no idea what C library functions
1126 might call malloc, so we can't really protect them unless you're
1127 using GNU malloc. Fortunately, most of the major operating systems
1128 can use GNU malloc. */
1130 #ifndef SYNC_INPUT
1132 #ifndef DOUG_LEA_MALLOC
1133 extern void * (*__malloc_hook) P_ ((size_t, const void *));
1134 extern void * (*__realloc_hook) P_ ((void *, size_t, const void *));
1135 extern void (*__free_hook) P_ ((void *, const void *));
1136 /* Else declared in malloc.h, perhaps with an extra arg. */
1137 #endif /* DOUG_LEA_MALLOC */
1138 static void * (*old_malloc_hook) P_ ((size_t, const void *));
1139 static void * (*old_realloc_hook) P_ ((void *, size_t, const void*));
1140 static void (*old_free_hook) P_ ((void*, const void*));
1142 /* This function is used as the hook for free to call. */
1144 static void
1145 emacs_blocked_free (ptr, ptr2)
1146 void *ptr;
1147 const void *ptr2;
1149 EMACS_INT bytes_used_now;
1151 BLOCK_INPUT_ALLOC;
1153 #ifdef GC_MALLOC_CHECK
1154 if (ptr)
1156 struct mem_node *m;
1158 m = mem_find (ptr);
1159 if (m == MEM_NIL || m->start != ptr)
1161 fprintf (stderr,
1162 "Freeing `%p' which wasn't allocated with malloc\n", ptr);
1163 abort ();
1165 else
1167 /* fprintf (stderr, "free %p...%p (%p)\n", m->start, m->end, ptr); */
1168 mem_delete (m);
1171 #endif /* GC_MALLOC_CHECK */
1173 __free_hook = old_free_hook;
1174 free (ptr);
1176 /* If we released our reserve (due to running out of memory),
1177 and we have a fair amount free once again,
1178 try to set aside another reserve in case we run out once more. */
1179 if (! NILP (Vmemory_full)
1180 /* Verify there is enough space that even with the malloc
1181 hysteresis this call won't run out again.
1182 The code here is correct as long as SPARE_MEMORY
1183 is substantially larger than the block size malloc uses. */
1184 && (bytes_used_when_full
1185 > ((bytes_used_when_reconsidered = BYTES_USED)
1186 + max (malloc_hysteresis, 4) * SPARE_MEMORY)))
1187 refill_memory_reserve ();
1189 __free_hook = emacs_blocked_free;
1190 UNBLOCK_INPUT_ALLOC;
1194 /* This function is the malloc hook that Emacs uses. */
1196 static void *
1197 emacs_blocked_malloc (size, ptr)
1198 size_t size;
1199 const void *ptr;
1201 void *value;
1203 BLOCK_INPUT_ALLOC;
1204 __malloc_hook = old_malloc_hook;
1205 #ifdef DOUG_LEA_MALLOC
1206 mallopt (M_TOP_PAD, malloc_hysteresis * 4096);
1207 #else
1208 __malloc_extra_blocks = malloc_hysteresis;
1209 #endif
1211 value = (void *) malloc (size);
1213 #ifdef GC_MALLOC_CHECK
1215 struct mem_node *m = mem_find (value);
1216 if (m != MEM_NIL)
1218 fprintf (stderr, "Malloc returned %p which is already in use\n",
1219 value);
1220 fprintf (stderr, "Region in use is %p...%p, %u bytes, type %d\n",
1221 m->start, m->end, (char *) m->end - (char *) m->start,
1222 m->type);
1223 abort ();
1226 if (!dont_register_blocks)
1228 mem_insert (value, (char *) value + max (1, size), allocated_mem_type);
1229 allocated_mem_type = MEM_TYPE_NON_LISP;
1232 #endif /* GC_MALLOC_CHECK */
1234 __malloc_hook = emacs_blocked_malloc;
1235 UNBLOCK_INPUT_ALLOC;
1237 /* fprintf (stderr, "%p malloc\n", value); */
1238 return value;
1242 /* This function is the realloc hook that Emacs uses. */
1244 static void *
1245 emacs_blocked_realloc (ptr, size, ptr2)
1246 void *ptr;
1247 size_t size;
1248 const void *ptr2;
1250 void *value;
1252 BLOCK_INPUT_ALLOC;
1253 __realloc_hook = old_realloc_hook;
1255 #ifdef GC_MALLOC_CHECK
1256 if (ptr)
1258 struct mem_node *m = mem_find (ptr);
1259 if (m == MEM_NIL || m->start != ptr)
1261 fprintf (stderr,
1262 "Realloc of %p which wasn't allocated with malloc\n",
1263 ptr);
1264 abort ();
1267 mem_delete (m);
1270 /* fprintf (stderr, "%p -> realloc\n", ptr); */
1272 /* Prevent malloc from registering blocks. */
1273 dont_register_blocks = 1;
1274 #endif /* GC_MALLOC_CHECK */
1276 value = (void *) realloc (ptr, size);
1278 #ifdef GC_MALLOC_CHECK
1279 dont_register_blocks = 0;
1282 struct mem_node *m = mem_find (value);
1283 if (m != MEM_NIL)
1285 fprintf (stderr, "Realloc returns memory that is already in use\n");
1286 abort ();
1289 /* Can't handle zero size regions in the red-black tree. */
1290 mem_insert (value, (char *) value + max (size, 1), MEM_TYPE_NON_LISP);
1293 /* fprintf (stderr, "%p <- realloc\n", value); */
1294 #endif /* GC_MALLOC_CHECK */
1296 __realloc_hook = emacs_blocked_realloc;
1297 UNBLOCK_INPUT_ALLOC;
1299 return value;
1303 #ifdef HAVE_GTK_AND_PTHREAD
1304 /* Called from Fdump_emacs so that when the dumped Emacs starts, it has a
1305 normal malloc. Some thread implementations need this as they call
1306 malloc before main. The pthread_self call in BLOCK_INPUT_ALLOC then
1307 calls malloc because it is the first call, and we have an endless loop. */
1309 void
1310 reset_malloc_hooks ()
1312 __free_hook = 0;
1313 __malloc_hook = 0;
1314 __realloc_hook = 0;
1316 #endif /* HAVE_GTK_AND_PTHREAD */
1319 /* Called from main to set up malloc to use our hooks. */
1321 void
1322 uninterrupt_malloc ()
1324 #ifdef HAVE_GTK_AND_PTHREAD
1325 pthread_mutexattr_t attr;
1327 /* GLIBC has a faster way to do this, but lets keep it portable.
1328 This is according to the Single UNIX Specification. */
1329 pthread_mutexattr_init (&attr);
1330 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
1331 pthread_mutex_init (&alloc_mutex, &attr);
1332 #endif /* HAVE_GTK_AND_PTHREAD */
1334 if (__free_hook != emacs_blocked_free)
1335 old_free_hook = __free_hook;
1336 __free_hook = emacs_blocked_free;
1338 if (__malloc_hook != emacs_blocked_malloc)
1339 old_malloc_hook = __malloc_hook;
1340 __malloc_hook = emacs_blocked_malloc;
1342 if (__realloc_hook != emacs_blocked_realloc)
1343 old_realloc_hook = __realloc_hook;
1344 __realloc_hook = emacs_blocked_realloc;
1347 #endif /* not SYNC_INPUT */
1348 #endif /* not SYSTEM_MALLOC */
1352 /***********************************************************************
1353 Interval Allocation
1354 ***********************************************************************/
1356 /* Number of intervals allocated in an interval_block structure.
1357 The 1020 is 1024 minus malloc overhead. */
1359 #define INTERVAL_BLOCK_SIZE \
1360 ((1020 - sizeof (struct interval_block *)) / sizeof (struct interval))
1362 /* Intervals are allocated in chunks in form of an interval_block
1363 structure. */
1365 struct interval_block
1367 /* Place `intervals' first, to preserve alignment. */
1368 struct interval intervals[INTERVAL_BLOCK_SIZE];
1369 struct interval_block *next;
1372 /* Current interval block. Its `next' pointer points to older
1373 blocks. */
1375 struct interval_block *interval_block;
1377 /* Index in interval_block above of the next unused interval
1378 structure. */
1380 static int interval_block_index;
1382 /* Number of free and live intervals. */
1384 static int total_free_intervals, total_intervals;
1386 /* List of free intervals. */
1388 INTERVAL interval_free_list;
1390 /* Total number of interval blocks now in use. */
1392 int n_interval_blocks;
1395 /* Initialize interval allocation. */
1397 static void
1398 init_intervals ()
1400 interval_block = NULL;
1401 interval_block_index = INTERVAL_BLOCK_SIZE;
1402 interval_free_list = 0;
1403 n_interval_blocks = 0;
1407 /* Return a new interval. */
1409 INTERVAL
1410 make_interval ()
1412 INTERVAL val;
1414 if (interval_free_list)
1416 val = interval_free_list;
1417 interval_free_list = INTERVAL_PARENT (interval_free_list);
1419 else
1421 if (interval_block_index == INTERVAL_BLOCK_SIZE)
1423 register struct interval_block *newi;
1425 newi = (struct interval_block *) lisp_malloc (sizeof *newi,
1426 MEM_TYPE_NON_LISP);
1428 newi->next = interval_block;
1429 interval_block = newi;
1430 interval_block_index = 0;
1431 n_interval_blocks++;
1433 val = &interval_block->intervals[interval_block_index++];
1435 consing_since_gc += sizeof (struct interval);
1436 intervals_consed++;
1437 RESET_INTERVAL (val);
1438 val->gcmarkbit = 0;
1439 return val;
1443 /* Mark Lisp objects in interval I. */
1445 static void
1446 mark_interval (i, dummy)
1447 register INTERVAL i;
1448 Lisp_Object dummy;
1450 eassert (!i->gcmarkbit); /* Intervals are never shared. */
1451 i->gcmarkbit = 1;
1452 mark_object (i->plist);
1456 /* Mark the interval tree rooted in TREE. Don't call this directly;
1457 use the macro MARK_INTERVAL_TREE instead. */
1459 static void
1460 mark_interval_tree (tree)
1461 register INTERVAL tree;
1463 /* No need to test if this tree has been marked already; this
1464 function is always called through the MARK_INTERVAL_TREE macro,
1465 which takes care of that. */
1467 traverse_intervals_noorder (tree, mark_interval, Qnil);
1471 /* Mark the interval tree rooted in I. */
1473 #define MARK_INTERVAL_TREE(i) \
1474 do { \
1475 if (!NULL_INTERVAL_P (i) && !i->gcmarkbit) \
1476 mark_interval_tree (i); \
1477 } while (0)
1480 #define UNMARK_BALANCE_INTERVALS(i) \
1481 do { \
1482 if (! NULL_INTERVAL_P (i)) \
1483 (i) = balance_intervals (i); \
1484 } while (0)
1487 /* Number support. If NO_UNION_TYPE isn't in effect, we
1488 can't create number objects in macros. */
1489 #ifndef make_number
1490 Lisp_Object
1491 make_number (n)
1492 EMACS_INT n;
1494 Lisp_Object obj;
1495 obj.s.val = n;
1496 obj.s.type = Lisp_Int;
1497 return obj;
1499 #endif
1501 /***********************************************************************
1502 String Allocation
1503 ***********************************************************************/
1505 /* Lisp_Strings are allocated in string_block structures. When a new
1506 string_block is allocated, all the Lisp_Strings it contains are
1507 added to a free-list string_free_list. When a new Lisp_String is
1508 needed, it is taken from that list. During the sweep phase of GC,
1509 string_blocks that are entirely free are freed, except two which
1510 we keep.
1512 String data is allocated from sblock structures. Strings larger
1513 than LARGE_STRING_BYTES, get their own sblock, data for smaller
1514 strings is sub-allocated out of sblocks of size SBLOCK_SIZE.
1516 Sblocks consist internally of sdata structures, one for each
1517 Lisp_String. The sdata structure points to the Lisp_String it
1518 belongs to. The Lisp_String points back to the `u.data' member of
1519 its sdata structure.
1521 When a Lisp_String is freed during GC, it is put back on
1522 string_free_list, and its `data' member and its sdata's `string'
1523 pointer is set to null. The size of the string is recorded in the
1524 `u.nbytes' member of the sdata. So, sdata structures that are no
1525 longer used, can be easily recognized, and it's easy to compact the
1526 sblocks of small strings which we do in compact_small_strings. */
1528 /* Size in bytes of an sblock structure used for small strings. This
1529 is 8192 minus malloc overhead. */
1531 #define SBLOCK_SIZE 8188
1533 /* Strings larger than this are considered large strings. String data
1534 for large strings is allocated from individual sblocks. */
1536 #define LARGE_STRING_BYTES 1024
1538 /* Structure describing string memory sub-allocated from an sblock.
1539 This is where the contents of Lisp strings are stored. */
1541 struct sdata
1543 /* Back-pointer to the string this sdata belongs to. If null, this
1544 structure is free, and the NBYTES member of the union below
1545 contains the string's byte size (the same value that STRING_BYTES
1546 would return if STRING were non-null). If non-null, STRING_BYTES
1547 (STRING) is the size of the data, and DATA contains the string's
1548 contents. */
1549 struct Lisp_String *string;
1551 #ifdef GC_CHECK_STRING_BYTES
1553 EMACS_INT nbytes;
1554 unsigned char data[1];
1556 #define SDATA_NBYTES(S) (S)->nbytes
1557 #define SDATA_DATA(S) (S)->data
1559 #else /* not GC_CHECK_STRING_BYTES */
1561 union
1563 /* When STRING in non-null. */
1564 unsigned char data[1];
1566 /* When STRING is null. */
1567 EMACS_INT nbytes;
1568 } u;
1571 #define SDATA_NBYTES(S) (S)->u.nbytes
1572 #define SDATA_DATA(S) (S)->u.data
1574 #endif /* not GC_CHECK_STRING_BYTES */
1578 /* Structure describing a block of memory which is sub-allocated to
1579 obtain string data memory for strings. Blocks for small strings
1580 are of fixed size SBLOCK_SIZE. Blocks for large strings are made
1581 as large as needed. */
1583 struct sblock
1585 /* Next in list. */
1586 struct sblock *next;
1588 /* Pointer to the next free sdata block. This points past the end
1589 of the sblock if there isn't any space left in this block. */
1590 struct sdata *next_free;
1592 /* Start of data. */
1593 struct sdata first_data;
1596 /* Number of Lisp strings in a string_block structure. The 1020 is
1597 1024 minus malloc overhead. */
1599 #define STRING_BLOCK_SIZE \
1600 ((1020 - sizeof (struct string_block *)) / sizeof (struct Lisp_String))
1602 /* Structure describing a block from which Lisp_String structures
1603 are allocated. */
1605 struct string_block
1607 /* Place `strings' first, to preserve alignment. */
1608 struct Lisp_String strings[STRING_BLOCK_SIZE];
1609 struct string_block *next;
1612 /* Head and tail of the list of sblock structures holding Lisp string
1613 data. We always allocate from current_sblock. The NEXT pointers
1614 in the sblock structures go from oldest_sblock to current_sblock. */
1616 static struct sblock *oldest_sblock, *current_sblock;
1618 /* List of sblocks for large strings. */
1620 static struct sblock *large_sblocks;
1622 /* List of string_block structures, and how many there are. */
1624 static struct string_block *string_blocks;
1625 static int n_string_blocks;
1627 /* Free-list of Lisp_Strings. */
1629 static struct Lisp_String *string_free_list;
1631 /* Number of live and free Lisp_Strings. */
1633 static int total_strings, total_free_strings;
1635 /* Number of bytes used by live strings. */
1637 static int total_string_size;
1639 /* Given a pointer to a Lisp_String S which is on the free-list
1640 string_free_list, return a pointer to its successor in the
1641 free-list. */
1643 #define NEXT_FREE_LISP_STRING(S) (*(struct Lisp_String **) (S))
1645 /* Return a pointer to the sdata structure belonging to Lisp string S.
1646 S must be live, i.e. S->data must not be null. S->data is actually
1647 a pointer to the `u.data' member of its sdata structure; the
1648 structure starts at a constant offset in front of that. */
1650 #ifdef GC_CHECK_STRING_BYTES
1652 #define SDATA_OF_STRING(S) \
1653 ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *) \
1654 - sizeof (EMACS_INT)))
1656 #else /* not GC_CHECK_STRING_BYTES */
1658 #define SDATA_OF_STRING(S) \
1659 ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *)))
1661 #endif /* not GC_CHECK_STRING_BYTES */
1664 #ifdef GC_CHECK_STRING_OVERRUN
1666 /* We check for overrun in string data blocks by appending a small
1667 "cookie" after each allocated string data block, and check for the
1668 presence of this cookie during GC. */
1670 #define GC_STRING_OVERRUN_COOKIE_SIZE 4
1671 static char string_overrun_cookie[GC_STRING_OVERRUN_COOKIE_SIZE] =
1672 { 0xde, 0xad, 0xbe, 0xef };
1674 #else
1675 #define GC_STRING_OVERRUN_COOKIE_SIZE 0
1676 #endif
1678 /* Value is the size of an sdata structure large enough to hold NBYTES
1679 bytes of string data. The value returned includes a terminating
1680 NUL byte, the size of the sdata structure, and padding. */
1682 #ifdef GC_CHECK_STRING_BYTES
1684 #define SDATA_SIZE(NBYTES) \
1685 ((sizeof (struct Lisp_String *) \
1686 + (NBYTES) + 1 \
1687 + sizeof (EMACS_INT) \
1688 + sizeof (EMACS_INT) - 1) \
1689 & ~(sizeof (EMACS_INT) - 1))
1691 #else /* not GC_CHECK_STRING_BYTES */
1693 #define SDATA_SIZE(NBYTES) \
1694 ((sizeof (struct Lisp_String *) \
1695 + (NBYTES) + 1 \
1696 + sizeof (EMACS_INT) - 1) \
1697 & ~(sizeof (EMACS_INT) - 1))
1699 #endif /* not GC_CHECK_STRING_BYTES */
1701 /* Extra bytes to allocate for each string. */
1703 #define GC_STRING_EXTRA (GC_STRING_OVERRUN_COOKIE_SIZE)
1705 /* Initialize string allocation. Called from init_alloc_once. */
1707 void
1708 init_strings ()
1710 total_strings = total_free_strings = total_string_size = 0;
1711 oldest_sblock = current_sblock = large_sblocks = NULL;
1712 string_blocks = NULL;
1713 n_string_blocks = 0;
1714 string_free_list = NULL;
1718 #ifdef GC_CHECK_STRING_BYTES
1720 static int check_string_bytes_count;
1722 void check_string_bytes P_ ((int));
1723 void check_sblock P_ ((struct sblock *));
1725 #define CHECK_STRING_BYTES(S) STRING_BYTES (S)
1728 /* Like GC_STRING_BYTES, but with debugging check. */
1731 string_bytes (s)
1732 struct Lisp_String *s;
1734 int nbytes = (s->size_byte < 0 ? s->size & ~ARRAY_MARK_FLAG : s->size_byte);
1735 if (!PURE_POINTER_P (s)
1736 && s->data
1737 && nbytes != SDATA_NBYTES (SDATA_OF_STRING (s)))
1738 abort ();
1739 return nbytes;
1742 /* Check validity of Lisp strings' string_bytes member in B. */
1744 void
1745 check_sblock (b)
1746 struct sblock *b;
1748 struct sdata *from, *end, *from_end;
1750 end = b->next_free;
1752 for (from = &b->first_data; from < end; from = from_end)
1754 /* Compute the next FROM here because copying below may
1755 overwrite data we need to compute it. */
1756 int nbytes;
1758 /* Check that the string size recorded in the string is the
1759 same as the one recorded in the sdata structure. */
1760 if (from->string)
1761 CHECK_STRING_BYTES (from->string);
1763 if (from->string)
1764 nbytes = GC_STRING_BYTES (from->string);
1765 else
1766 nbytes = SDATA_NBYTES (from);
1768 nbytes = SDATA_SIZE (nbytes);
1769 from_end = (struct sdata *) ((char *) from + nbytes + GC_STRING_EXTRA);
1774 /* Check validity of Lisp strings' string_bytes member. ALL_P
1775 non-zero means check all strings, otherwise check only most
1776 recently allocated strings. Used for hunting a bug. */
1778 void
1779 check_string_bytes (all_p)
1780 int all_p;
1782 if (all_p)
1784 struct sblock *b;
1786 for (b = large_sblocks; b; b = b->next)
1788 struct Lisp_String *s = b->first_data.string;
1789 if (s)
1790 CHECK_STRING_BYTES (s);
1793 for (b = oldest_sblock; b; b = b->next)
1794 check_sblock (b);
1796 else
1797 check_sblock (current_sblock);
1800 #endif /* GC_CHECK_STRING_BYTES */
1802 #ifdef GC_CHECK_STRING_FREE_LIST
1804 /* Walk through the string free list looking for bogus next pointers.
1805 This may catch buffer overrun from a previous string. */
1807 static void
1808 check_string_free_list ()
1810 struct Lisp_String *s;
1812 /* Pop a Lisp_String off the free-list. */
1813 s = string_free_list;
1814 while (s != NULL)
1816 if ((unsigned)s < 1024)
1817 abort();
1818 s = NEXT_FREE_LISP_STRING (s);
1821 #else
1822 #define check_string_free_list()
1823 #endif
1825 /* Return a new Lisp_String. */
1827 static struct Lisp_String *
1828 allocate_string ()
1830 struct Lisp_String *s;
1832 /* If the free-list is empty, allocate a new string_block, and
1833 add all the Lisp_Strings in it to the free-list. */
1834 if (string_free_list == NULL)
1836 struct string_block *b;
1837 int i;
1839 b = (struct string_block *) lisp_malloc (sizeof *b, MEM_TYPE_STRING);
1840 bzero (b, sizeof *b);
1841 b->next = string_blocks;
1842 string_blocks = b;
1843 ++n_string_blocks;
1845 for (i = STRING_BLOCK_SIZE - 1; i >= 0; --i)
1847 s = b->strings + i;
1848 NEXT_FREE_LISP_STRING (s) = string_free_list;
1849 string_free_list = s;
1852 total_free_strings += STRING_BLOCK_SIZE;
1855 check_string_free_list ();
1857 /* Pop a Lisp_String off the free-list. */
1858 s = string_free_list;
1859 string_free_list = NEXT_FREE_LISP_STRING (s);
1861 /* Probably not strictly necessary, but play it safe. */
1862 bzero (s, sizeof *s);
1864 --total_free_strings;
1865 ++total_strings;
1866 ++strings_consed;
1867 consing_since_gc += sizeof *s;
1869 #ifdef GC_CHECK_STRING_BYTES
1870 if (!noninteractive
1871 #ifdef MAC_OS8
1872 && current_sblock
1873 #endif
1876 if (++check_string_bytes_count == 200)
1878 check_string_bytes_count = 0;
1879 check_string_bytes (1);
1881 else
1882 check_string_bytes (0);
1884 #endif /* GC_CHECK_STRING_BYTES */
1886 return s;
1890 /* Set up Lisp_String S for holding NCHARS characters, NBYTES bytes,
1891 plus a NUL byte at the end. Allocate an sdata structure for S, and
1892 set S->data to its `u.data' member. Store a NUL byte at the end of
1893 S->data. Set S->size to NCHARS and S->size_byte to NBYTES. Free
1894 S->data if it was initially non-null. */
1896 void
1897 allocate_string_data (s, nchars, nbytes)
1898 struct Lisp_String *s;
1899 int nchars, nbytes;
1901 struct sdata *data, *old_data;
1902 struct sblock *b;
1903 int needed, old_nbytes;
1905 /* Determine the number of bytes needed to store NBYTES bytes
1906 of string data. */
1907 needed = SDATA_SIZE (nbytes);
1909 if (nbytes > LARGE_STRING_BYTES)
1911 size_t size = sizeof *b - sizeof (struct sdata) + needed;
1913 #ifdef DOUG_LEA_MALLOC
1914 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed
1915 because mapped region contents are not preserved in
1916 a dumped Emacs.
1918 In case you think of allowing it in a dumped Emacs at the
1919 cost of not being able to re-dump, there's another reason:
1920 mmap'ed data typically have an address towards the top of the
1921 address space, which won't fit into an EMACS_INT (at least on
1922 32-bit systems with the current tagging scheme). --fx */
1923 BLOCK_INPUT;
1924 mallopt (M_MMAP_MAX, 0);
1925 UNBLOCK_INPUT;
1926 #endif
1928 b = (struct sblock *) lisp_malloc (size + GC_STRING_EXTRA, MEM_TYPE_NON_LISP);
1930 #ifdef DOUG_LEA_MALLOC
1931 /* Back to a reasonable maximum of mmap'ed areas. */
1932 BLOCK_INPUT;
1933 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
1934 UNBLOCK_INPUT;
1935 #endif
1937 b->next_free = &b->first_data;
1938 b->first_data.string = NULL;
1939 b->next = large_sblocks;
1940 large_sblocks = b;
1942 else if (current_sblock == NULL
1943 || (((char *) current_sblock + SBLOCK_SIZE
1944 - (char *) current_sblock->next_free)
1945 < (needed + GC_STRING_EXTRA)))
1947 /* Not enough room in the current sblock. */
1948 b = (struct sblock *) lisp_malloc (SBLOCK_SIZE, MEM_TYPE_NON_LISP);
1949 b->next_free = &b->first_data;
1950 b->first_data.string = NULL;
1951 b->next = NULL;
1953 if (current_sblock)
1954 current_sblock->next = b;
1955 else
1956 oldest_sblock = b;
1957 current_sblock = b;
1959 else
1960 b = current_sblock;
1962 old_data = s->data ? SDATA_OF_STRING (s) : NULL;
1963 old_nbytes = GC_STRING_BYTES (s);
1965 data = b->next_free;
1966 data->string = s;
1967 s->data = SDATA_DATA (data);
1968 #ifdef GC_CHECK_STRING_BYTES
1969 SDATA_NBYTES (data) = nbytes;
1970 #endif
1971 s->size = nchars;
1972 s->size_byte = nbytes;
1973 s->data[nbytes] = '\0';
1974 #ifdef GC_CHECK_STRING_OVERRUN
1975 bcopy (string_overrun_cookie, (char *) data + needed,
1976 GC_STRING_OVERRUN_COOKIE_SIZE);
1977 #endif
1978 b->next_free = (struct sdata *) ((char *) data + needed + GC_STRING_EXTRA);
1980 /* If S had already data assigned, mark that as free by setting its
1981 string back-pointer to null, and recording the size of the data
1982 in it. */
1983 if (old_data)
1985 SDATA_NBYTES (old_data) = old_nbytes;
1986 old_data->string = NULL;
1989 consing_since_gc += needed;
1993 /* Sweep and compact strings. */
1995 static void
1996 sweep_strings ()
1998 struct string_block *b, *next;
1999 struct string_block *live_blocks = NULL;
2001 string_free_list = NULL;
2002 total_strings = total_free_strings = 0;
2003 total_string_size = 0;
2005 /* Scan strings_blocks, free Lisp_Strings that aren't marked. */
2006 for (b = string_blocks; b; b = next)
2008 int i, nfree = 0;
2009 struct Lisp_String *free_list_before = string_free_list;
2011 next = b->next;
2013 for (i = 0; i < STRING_BLOCK_SIZE; ++i)
2015 struct Lisp_String *s = b->strings + i;
2017 if (s->data)
2019 /* String was not on free-list before. */
2020 if (STRING_MARKED_P (s))
2022 /* String is live; unmark it and its intervals. */
2023 UNMARK_STRING (s);
2025 if (!NULL_INTERVAL_P (s->intervals))
2026 UNMARK_BALANCE_INTERVALS (s->intervals);
2028 ++total_strings;
2029 total_string_size += STRING_BYTES (s);
2031 else
2033 /* String is dead. Put it on the free-list. */
2034 struct sdata *data = SDATA_OF_STRING (s);
2036 /* Save the size of S in its sdata so that we know
2037 how large that is. Reset the sdata's string
2038 back-pointer so that we know it's free. */
2039 #ifdef GC_CHECK_STRING_BYTES
2040 if (GC_STRING_BYTES (s) != SDATA_NBYTES (data))
2041 abort ();
2042 #else
2043 data->u.nbytes = GC_STRING_BYTES (s);
2044 #endif
2045 data->string = NULL;
2047 /* Reset the strings's `data' member so that we
2048 know it's free. */
2049 s->data = NULL;
2051 /* Put the string on the free-list. */
2052 NEXT_FREE_LISP_STRING (s) = string_free_list;
2053 string_free_list = s;
2054 ++nfree;
2057 else
2059 /* S was on the free-list before. Put it there again. */
2060 NEXT_FREE_LISP_STRING (s) = string_free_list;
2061 string_free_list = s;
2062 ++nfree;
2066 /* Free blocks that contain free Lisp_Strings only, except
2067 the first two of them. */
2068 if (nfree == STRING_BLOCK_SIZE
2069 && total_free_strings > STRING_BLOCK_SIZE)
2071 lisp_free (b);
2072 --n_string_blocks;
2073 string_free_list = free_list_before;
2075 else
2077 total_free_strings += nfree;
2078 b->next = live_blocks;
2079 live_blocks = b;
2083 check_string_free_list ();
2085 string_blocks = live_blocks;
2086 free_large_strings ();
2087 compact_small_strings ();
2089 check_string_free_list ();
2093 /* Free dead large strings. */
2095 static void
2096 free_large_strings ()
2098 struct sblock *b, *next;
2099 struct sblock *live_blocks = NULL;
2101 for (b = large_sblocks; b; b = next)
2103 next = b->next;
2105 if (b->first_data.string == NULL)
2106 lisp_free (b);
2107 else
2109 b->next = live_blocks;
2110 live_blocks = b;
2114 large_sblocks = live_blocks;
2118 /* Compact data of small strings. Free sblocks that don't contain
2119 data of live strings after compaction. */
2121 static void
2122 compact_small_strings ()
2124 struct sblock *b, *tb, *next;
2125 struct sdata *from, *to, *end, *tb_end;
2126 struct sdata *to_end, *from_end;
2128 /* TB is the sblock we copy to, TO is the sdata within TB we copy
2129 to, and TB_END is the end of TB. */
2130 tb = oldest_sblock;
2131 tb_end = (struct sdata *) ((char *) tb + SBLOCK_SIZE);
2132 to = &tb->first_data;
2134 /* Step through the blocks from the oldest to the youngest. We
2135 expect that old blocks will stabilize over time, so that less
2136 copying will happen this way. */
2137 for (b = oldest_sblock; b; b = b->next)
2139 end = b->next_free;
2140 xassert ((char *) end <= (char *) b + SBLOCK_SIZE);
2142 for (from = &b->first_data; from < end; from = from_end)
2144 /* Compute the next FROM here because copying below may
2145 overwrite data we need to compute it. */
2146 int nbytes;
2148 #ifdef GC_CHECK_STRING_BYTES
2149 /* Check that the string size recorded in the string is the
2150 same as the one recorded in the sdata structure. */
2151 if (from->string
2152 && GC_STRING_BYTES (from->string) != SDATA_NBYTES (from))
2153 abort ();
2154 #endif /* GC_CHECK_STRING_BYTES */
2156 if (from->string)
2157 nbytes = GC_STRING_BYTES (from->string);
2158 else
2159 nbytes = SDATA_NBYTES (from);
2161 if (nbytes > LARGE_STRING_BYTES)
2162 abort ();
2164 nbytes = SDATA_SIZE (nbytes);
2165 from_end = (struct sdata *) ((char *) from + nbytes + GC_STRING_EXTRA);
2167 #ifdef GC_CHECK_STRING_OVERRUN
2168 if (bcmp (string_overrun_cookie,
2169 ((char *) from_end) - GC_STRING_OVERRUN_COOKIE_SIZE,
2170 GC_STRING_OVERRUN_COOKIE_SIZE))
2171 abort ();
2172 #endif
2174 /* FROM->string non-null means it's alive. Copy its data. */
2175 if (from->string)
2177 /* If TB is full, proceed with the next sblock. */
2178 to_end = (struct sdata *) ((char *) to + nbytes + GC_STRING_EXTRA);
2179 if (to_end > tb_end)
2181 tb->next_free = to;
2182 tb = tb->next;
2183 tb_end = (struct sdata *) ((char *) tb + SBLOCK_SIZE);
2184 to = &tb->first_data;
2185 to_end = (struct sdata *) ((char *) to + nbytes + GC_STRING_EXTRA);
2188 /* Copy, and update the string's `data' pointer. */
2189 if (from != to)
2191 xassert (tb != b || to <= from);
2192 safe_bcopy ((char *) from, (char *) to, nbytes + GC_STRING_EXTRA);
2193 to->string->data = SDATA_DATA (to);
2196 /* Advance past the sdata we copied to. */
2197 to = to_end;
2202 /* The rest of the sblocks following TB don't contain live data, so
2203 we can free them. */
2204 for (b = tb->next; b; b = next)
2206 next = b->next;
2207 lisp_free (b);
2210 tb->next_free = to;
2211 tb->next = NULL;
2212 current_sblock = tb;
2216 DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0,
2217 doc: /* Return a newly created string of length LENGTH, with INIT in each element.
2218 LENGTH must be an integer.
2219 INIT must be an integer that represents a character. */)
2220 (length, init)
2221 Lisp_Object length, init;
2223 register Lisp_Object val;
2224 register unsigned char *p, *end;
2225 int c, nbytes;
2227 CHECK_NATNUM (length);
2228 CHECK_NUMBER (init);
2230 c = XINT (init);
2231 if (SINGLE_BYTE_CHAR_P (c))
2233 nbytes = XINT (length);
2234 val = make_uninit_string (nbytes);
2235 p = SDATA (val);
2236 end = p + SCHARS (val);
2237 while (p != end)
2238 *p++ = c;
2240 else
2242 unsigned char str[MAX_MULTIBYTE_LENGTH];
2243 int len = CHAR_STRING (c, str);
2245 nbytes = len * XINT (length);
2246 val = make_uninit_multibyte_string (XINT (length), nbytes);
2247 p = SDATA (val);
2248 end = p + nbytes;
2249 while (p != end)
2251 bcopy (str, p, len);
2252 p += len;
2256 *p = 0;
2257 return val;
2261 DEFUN ("make-bool-vector", Fmake_bool_vector, Smake_bool_vector, 2, 2, 0,
2262 doc: /* Return a new bool-vector of length LENGTH, using INIT for as each element.
2263 LENGTH must be a number. INIT matters only in whether it is t or nil. */)
2264 (length, init)
2265 Lisp_Object length, init;
2267 register Lisp_Object val;
2268 struct Lisp_Bool_Vector *p;
2269 int real_init, i;
2270 int length_in_chars, length_in_elts, bits_per_value;
2272 CHECK_NATNUM (length);
2274 bits_per_value = sizeof (EMACS_INT) * BOOL_VECTOR_BITS_PER_CHAR;
2276 length_in_elts = (XFASTINT (length) + bits_per_value - 1) / bits_per_value;
2277 length_in_chars = ((XFASTINT (length) + BOOL_VECTOR_BITS_PER_CHAR - 1)
2278 / BOOL_VECTOR_BITS_PER_CHAR);
2280 /* We must allocate one more elements than LENGTH_IN_ELTS for the
2281 slot `size' of the struct Lisp_Bool_Vector. */
2282 val = Fmake_vector (make_number (length_in_elts + 1), Qnil);
2283 p = XBOOL_VECTOR (val);
2285 /* Get rid of any bits that would cause confusion. */
2286 p->vector_size = 0;
2287 XSETBOOL_VECTOR (val, p);
2288 p->size = XFASTINT (length);
2290 real_init = (NILP (init) ? 0 : -1);
2291 for (i = 0; i < length_in_chars ; i++)
2292 p->data[i] = real_init;
2294 /* Clear the extraneous bits in the last byte. */
2295 if (XINT (length) != length_in_chars * BOOL_VECTOR_BITS_PER_CHAR)
2296 XBOOL_VECTOR (val)->data[length_in_chars - 1]
2297 &= (1 << (XINT (length) % BOOL_VECTOR_BITS_PER_CHAR)) - 1;
2299 return val;
2303 /* Make a string from NBYTES bytes at CONTENTS, and compute the number
2304 of characters from the contents. This string may be unibyte or
2305 multibyte, depending on the contents. */
2307 Lisp_Object
2308 make_string (contents, nbytes)
2309 const char *contents;
2310 int nbytes;
2312 register Lisp_Object val;
2313 int nchars, multibyte_nbytes;
2315 parse_str_as_multibyte (contents, nbytes, &nchars, &multibyte_nbytes);
2316 if (nbytes == nchars || nbytes != multibyte_nbytes)
2317 /* CONTENTS contains no multibyte sequences or contains an invalid
2318 multibyte sequence. We must make unibyte string. */
2319 val = make_unibyte_string (contents, nbytes);
2320 else
2321 val = make_multibyte_string (contents, nchars, nbytes);
2322 return val;
2326 /* Make an unibyte string from LENGTH bytes at CONTENTS. */
2328 Lisp_Object
2329 make_unibyte_string (contents, length)
2330 const char *contents;
2331 int length;
2333 register Lisp_Object val;
2334 val = make_uninit_string (length);
2335 bcopy (contents, SDATA (val), length);
2336 STRING_SET_UNIBYTE (val);
2337 return val;
2341 /* Make a multibyte string from NCHARS characters occupying NBYTES
2342 bytes at CONTENTS. */
2344 Lisp_Object
2345 make_multibyte_string (contents, nchars, nbytes)
2346 const char *contents;
2347 int nchars, nbytes;
2349 register Lisp_Object val;
2350 val = make_uninit_multibyte_string (nchars, nbytes);
2351 bcopy (contents, SDATA (val), nbytes);
2352 return val;
2356 /* Make a string from NCHARS characters occupying NBYTES bytes at
2357 CONTENTS. It is a multibyte string if NBYTES != NCHARS. */
2359 Lisp_Object
2360 make_string_from_bytes (contents, nchars, nbytes)
2361 const char *contents;
2362 int nchars, nbytes;
2364 register Lisp_Object val;
2365 val = make_uninit_multibyte_string (nchars, nbytes);
2366 bcopy (contents, SDATA (val), nbytes);
2367 if (SBYTES (val) == SCHARS (val))
2368 STRING_SET_UNIBYTE (val);
2369 return val;
2373 /* Make a string from NCHARS characters occupying NBYTES bytes at
2374 CONTENTS. The argument MULTIBYTE controls whether to label the
2375 string as multibyte. If NCHARS is negative, it counts the number of
2376 characters by itself. */
2378 Lisp_Object
2379 make_specified_string (contents, nchars, nbytes, multibyte)
2380 const char *contents;
2381 int nchars, nbytes;
2382 int multibyte;
2384 register Lisp_Object val;
2386 if (nchars < 0)
2388 if (multibyte)
2389 nchars = multibyte_chars_in_text (contents, nbytes);
2390 else
2391 nchars = nbytes;
2393 val = make_uninit_multibyte_string (nchars, nbytes);
2394 bcopy (contents, SDATA (val), nbytes);
2395 if (!multibyte)
2396 STRING_SET_UNIBYTE (val);
2397 return val;
2401 /* Make a string from the data at STR, treating it as multibyte if the
2402 data warrants. */
2404 Lisp_Object
2405 build_string (str)
2406 const char *str;
2408 return make_string (str, strlen (str));
2412 /* Return an unibyte Lisp_String set up to hold LENGTH characters
2413 occupying LENGTH bytes. */
2415 Lisp_Object
2416 make_uninit_string (length)
2417 int length;
2419 Lisp_Object val;
2420 val = make_uninit_multibyte_string (length, length);
2421 STRING_SET_UNIBYTE (val);
2422 return val;
2426 /* Return a multibyte Lisp_String set up to hold NCHARS characters
2427 which occupy NBYTES bytes. */
2429 Lisp_Object
2430 make_uninit_multibyte_string (nchars, nbytes)
2431 int nchars, nbytes;
2433 Lisp_Object string;
2434 struct Lisp_String *s;
2436 if (nchars < 0)
2437 abort ();
2439 s = allocate_string ();
2440 allocate_string_data (s, nchars, nbytes);
2441 XSETSTRING (string, s);
2442 string_chars_consed += nbytes;
2443 return string;
2448 /***********************************************************************
2449 Float Allocation
2450 ***********************************************************************/
2452 /* We store float cells inside of float_blocks, allocating a new
2453 float_block with malloc whenever necessary. Float cells reclaimed
2454 by GC are put on a free list to be reallocated before allocating
2455 any new float cells from the latest float_block. */
2457 #define FLOAT_BLOCK_SIZE \
2458 (((BLOCK_BYTES - sizeof (struct float_block *) \
2459 /* The compiler might add padding at the end. */ \
2460 - (sizeof (struct Lisp_Float) - sizeof (int))) * CHAR_BIT) \
2461 / (sizeof (struct Lisp_Float) * CHAR_BIT + 1))
2463 #define GETMARKBIT(block,n) \
2464 (((block)->gcmarkbits[(n) / (sizeof(int) * CHAR_BIT)] \
2465 >> ((n) % (sizeof(int) * CHAR_BIT))) \
2466 & 1)
2468 #define SETMARKBIT(block,n) \
2469 (block)->gcmarkbits[(n) / (sizeof(int) * CHAR_BIT)] \
2470 |= 1 << ((n) % (sizeof(int) * CHAR_BIT))
2472 #define UNSETMARKBIT(block,n) \
2473 (block)->gcmarkbits[(n) / (sizeof(int) * CHAR_BIT)] \
2474 &= ~(1 << ((n) % (sizeof(int) * CHAR_BIT)))
2476 #define FLOAT_BLOCK(fptr) \
2477 ((struct float_block *)(((EMACS_UINT)(fptr)) & ~(BLOCK_ALIGN - 1)))
2479 #define FLOAT_INDEX(fptr) \
2480 ((((EMACS_UINT)(fptr)) & (BLOCK_ALIGN - 1)) / sizeof (struct Lisp_Float))
2482 struct float_block
2484 /* Place `floats' at the beginning, to ease up FLOAT_INDEX's job. */
2485 struct Lisp_Float floats[FLOAT_BLOCK_SIZE];
2486 int gcmarkbits[1 + FLOAT_BLOCK_SIZE / (sizeof(int) * CHAR_BIT)];
2487 struct float_block *next;
2490 #define FLOAT_MARKED_P(fptr) \
2491 GETMARKBIT (FLOAT_BLOCK (fptr), FLOAT_INDEX ((fptr)))
2493 #define FLOAT_MARK(fptr) \
2494 SETMARKBIT (FLOAT_BLOCK (fptr), FLOAT_INDEX ((fptr)))
2496 #define FLOAT_UNMARK(fptr) \
2497 UNSETMARKBIT (FLOAT_BLOCK (fptr), FLOAT_INDEX ((fptr)))
2499 /* Current float_block. */
2501 struct float_block *float_block;
2503 /* Index of first unused Lisp_Float in the current float_block. */
2505 int float_block_index;
2507 /* Total number of float blocks now in use. */
2509 int n_float_blocks;
2511 /* Free-list of Lisp_Floats. */
2513 struct Lisp_Float *float_free_list;
2516 /* Initialize float allocation. */
2518 void
2519 init_float ()
2521 float_block = NULL;
2522 float_block_index = FLOAT_BLOCK_SIZE; /* Force alloc of new float_block. */
2523 float_free_list = 0;
2524 n_float_blocks = 0;
2528 /* Explicitly free a float cell by putting it on the free-list. */
2530 void
2531 free_float (ptr)
2532 struct Lisp_Float *ptr;
2534 ptr->u.chain = float_free_list;
2535 float_free_list = ptr;
2539 /* Return a new float object with value FLOAT_VALUE. */
2541 Lisp_Object
2542 make_float (float_value)
2543 double float_value;
2545 register Lisp_Object val;
2547 if (float_free_list)
2549 /* We use the data field for chaining the free list
2550 so that we won't use the same field that has the mark bit. */
2551 XSETFLOAT (val, float_free_list);
2552 float_free_list = float_free_list->u.chain;
2554 else
2556 if (float_block_index == FLOAT_BLOCK_SIZE)
2558 register struct float_block *new;
2560 new = (struct float_block *) lisp_align_malloc (sizeof *new,
2561 MEM_TYPE_FLOAT);
2562 new->next = float_block;
2563 bzero ((char *) new->gcmarkbits, sizeof new->gcmarkbits);
2564 float_block = new;
2565 float_block_index = 0;
2566 n_float_blocks++;
2568 XSETFLOAT (val, &float_block->floats[float_block_index]);
2569 float_block_index++;
2572 XFLOAT_DATA (val) = float_value;
2573 eassert (!FLOAT_MARKED_P (XFLOAT (val)));
2574 consing_since_gc += sizeof (struct Lisp_Float);
2575 floats_consed++;
2576 return val;
2581 /***********************************************************************
2582 Cons Allocation
2583 ***********************************************************************/
2585 /* We store cons cells inside of cons_blocks, allocating a new
2586 cons_block with malloc whenever necessary. Cons cells reclaimed by
2587 GC are put on a free list to be reallocated before allocating
2588 any new cons cells from the latest cons_block. */
2590 #define CONS_BLOCK_SIZE \
2591 (((BLOCK_BYTES - sizeof (struct cons_block *)) * CHAR_BIT) \
2592 / (sizeof (struct Lisp_Cons) * CHAR_BIT + 1))
2594 #define CONS_BLOCK(fptr) \
2595 ((struct cons_block *)(((EMACS_UINT)(fptr)) & ~(BLOCK_ALIGN - 1)))
2597 #define CONS_INDEX(fptr) \
2598 ((((EMACS_UINT)(fptr)) & (BLOCK_ALIGN - 1)) / sizeof (struct Lisp_Cons))
2600 struct cons_block
2602 /* Place `conses' at the beginning, to ease up CONS_INDEX's job. */
2603 struct Lisp_Cons conses[CONS_BLOCK_SIZE];
2604 int gcmarkbits[1 + CONS_BLOCK_SIZE / (sizeof(int) * CHAR_BIT)];
2605 struct cons_block *next;
2608 #define CONS_MARKED_P(fptr) \
2609 GETMARKBIT (CONS_BLOCK (fptr), CONS_INDEX ((fptr)))
2611 #define CONS_MARK(fptr) \
2612 SETMARKBIT (CONS_BLOCK (fptr), CONS_INDEX ((fptr)))
2614 #define CONS_UNMARK(fptr) \
2615 UNSETMARKBIT (CONS_BLOCK (fptr), CONS_INDEX ((fptr)))
2617 /* Current cons_block. */
2619 struct cons_block *cons_block;
2621 /* Index of first unused Lisp_Cons in the current block. */
2623 int cons_block_index;
2625 /* Free-list of Lisp_Cons structures. */
2627 struct Lisp_Cons *cons_free_list;
2629 /* Total number of cons blocks now in use. */
2631 int n_cons_blocks;
2634 /* Initialize cons allocation. */
2636 void
2637 init_cons ()
2639 cons_block = NULL;
2640 cons_block_index = CONS_BLOCK_SIZE; /* Force alloc of new cons_block. */
2641 cons_free_list = 0;
2642 n_cons_blocks = 0;
2646 /* Explicitly free a cons cell by putting it on the free-list. */
2648 void
2649 free_cons (ptr)
2650 struct Lisp_Cons *ptr;
2652 ptr->u.chain = cons_free_list;
2653 #if GC_MARK_STACK
2654 ptr->car = Vdead;
2655 #endif
2656 cons_free_list = ptr;
2659 DEFUN ("cons", Fcons, Scons, 2, 2, 0,
2660 doc: /* Create a new cons, give it CAR and CDR as components, and return it. */)
2661 (car, cdr)
2662 Lisp_Object car, cdr;
2664 register Lisp_Object val;
2666 if (cons_free_list)
2668 /* We use the cdr for chaining the free list
2669 so that we won't use the same field that has the mark bit. */
2670 XSETCONS (val, cons_free_list);
2671 cons_free_list = cons_free_list->u.chain;
2673 else
2675 if (cons_block_index == CONS_BLOCK_SIZE)
2677 register struct cons_block *new;
2678 new = (struct cons_block *) lisp_align_malloc (sizeof *new,
2679 MEM_TYPE_CONS);
2680 bzero ((char *) new->gcmarkbits, sizeof new->gcmarkbits);
2681 new->next = cons_block;
2682 cons_block = new;
2683 cons_block_index = 0;
2684 n_cons_blocks++;
2686 XSETCONS (val, &cons_block->conses[cons_block_index]);
2687 cons_block_index++;
2690 XSETCAR (val, car);
2691 XSETCDR (val, cdr);
2692 eassert (!CONS_MARKED_P (XCONS (val)));
2693 consing_since_gc += sizeof (struct Lisp_Cons);
2694 cons_cells_consed++;
2695 return val;
2698 /* Get an error now if there's any junk in the cons free list. */
2699 void
2700 check_cons_list ()
2702 #ifdef GC_CHECK_CONS_LIST
2703 struct Lisp_Cons *tail = cons_free_list;
2705 while (tail)
2706 tail = tail->u.chain;
2707 #endif
2710 /* Make a list of 2, 3, 4 or 5 specified objects. */
2712 Lisp_Object
2713 list2 (arg1, arg2)
2714 Lisp_Object arg1, arg2;
2716 return Fcons (arg1, Fcons (arg2, Qnil));
2720 Lisp_Object
2721 list3 (arg1, arg2, arg3)
2722 Lisp_Object arg1, arg2, arg3;
2724 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Qnil)));
2728 Lisp_Object
2729 list4 (arg1, arg2, arg3, arg4)
2730 Lisp_Object arg1, arg2, arg3, arg4;
2732 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4, Qnil))));
2736 Lisp_Object
2737 list5 (arg1, arg2, arg3, arg4, arg5)
2738 Lisp_Object arg1, arg2, arg3, arg4, arg5;
2740 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4,
2741 Fcons (arg5, Qnil)))));
2745 DEFUN ("list", Flist, Slist, 0, MANY, 0,
2746 doc: /* Return a newly created list with specified arguments as elements.
2747 Any number of arguments, even zero arguments, are allowed.
2748 usage: (list &rest OBJECTS) */)
2749 (nargs, args)
2750 int nargs;
2751 register Lisp_Object *args;
2753 register Lisp_Object val;
2754 val = Qnil;
2756 while (nargs > 0)
2758 nargs--;
2759 val = Fcons (args[nargs], val);
2761 return val;
2765 DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0,
2766 doc: /* Return a newly created list of length LENGTH, with each element being INIT. */)
2767 (length, init)
2768 register Lisp_Object length, init;
2770 register Lisp_Object val;
2771 register int size;
2773 CHECK_NATNUM (length);
2774 size = XFASTINT (length);
2776 val = Qnil;
2777 while (size > 0)
2779 val = Fcons (init, val);
2780 --size;
2782 if (size > 0)
2784 val = Fcons (init, val);
2785 --size;
2787 if (size > 0)
2789 val = Fcons (init, val);
2790 --size;
2792 if (size > 0)
2794 val = Fcons (init, val);
2795 --size;
2797 if (size > 0)
2799 val = Fcons (init, val);
2800 --size;
2806 QUIT;
2809 return val;
2814 /***********************************************************************
2815 Vector Allocation
2816 ***********************************************************************/
2818 /* Singly-linked list of all vectors. */
2820 struct Lisp_Vector *all_vectors;
2822 /* Total number of vector-like objects now in use. */
2824 int n_vectors;
2827 /* Value is a pointer to a newly allocated Lisp_Vector structure
2828 with room for LEN Lisp_Objects. */
2830 static struct Lisp_Vector *
2831 allocate_vectorlike (len, type)
2832 EMACS_INT len;
2833 enum mem_type type;
2835 struct Lisp_Vector *p;
2836 size_t nbytes;
2838 #ifdef DOUG_LEA_MALLOC
2839 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed
2840 because mapped region contents are not preserved in
2841 a dumped Emacs. */
2842 BLOCK_INPUT;
2843 mallopt (M_MMAP_MAX, 0);
2844 UNBLOCK_INPUT;
2845 #endif
2847 nbytes = sizeof *p + (len - 1) * sizeof p->contents[0];
2848 p = (struct Lisp_Vector *) lisp_malloc (nbytes, type);
2850 #ifdef DOUG_LEA_MALLOC
2851 /* Back to a reasonable maximum of mmap'ed areas. */
2852 BLOCK_INPUT;
2853 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
2854 UNBLOCK_INPUT;
2855 #endif
2857 consing_since_gc += nbytes;
2858 vector_cells_consed += len;
2860 p->next = all_vectors;
2861 all_vectors = p;
2862 ++n_vectors;
2863 return p;
2867 /* Allocate a vector with NSLOTS slots. */
2869 struct Lisp_Vector *
2870 allocate_vector (nslots)
2871 EMACS_INT nslots;
2873 struct Lisp_Vector *v = allocate_vectorlike (nslots, MEM_TYPE_VECTOR);
2874 v->size = nslots;
2875 return v;
2879 /* Allocate other vector-like structures. */
2881 struct Lisp_Hash_Table *
2882 allocate_hash_table ()
2884 EMACS_INT len = VECSIZE (struct Lisp_Hash_Table);
2885 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_HASH_TABLE);
2886 EMACS_INT i;
2888 v->size = len;
2889 for (i = 0; i < len; ++i)
2890 v->contents[i] = Qnil;
2892 return (struct Lisp_Hash_Table *) v;
2896 struct window *
2897 allocate_window ()
2899 EMACS_INT len = VECSIZE (struct window);
2900 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_WINDOW);
2901 EMACS_INT i;
2903 for (i = 0; i < len; ++i)
2904 v->contents[i] = Qnil;
2905 v->size = len;
2907 return (struct window *) v;
2911 struct frame *
2912 allocate_frame ()
2914 EMACS_INT len = VECSIZE (struct frame);
2915 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_FRAME);
2916 EMACS_INT i;
2918 for (i = 0; i < len; ++i)
2919 v->contents[i] = make_number (0);
2920 v->size = len;
2921 return (struct frame *) v;
2925 struct Lisp_Process *
2926 allocate_process ()
2928 EMACS_INT len = VECSIZE (struct Lisp_Process);
2929 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_PROCESS);
2930 EMACS_INT i;
2932 for (i = 0; i < len; ++i)
2933 v->contents[i] = Qnil;
2934 v->size = len;
2936 return (struct Lisp_Process *) v;
2940 struct Lisp_Vector *
2941 allocate_other_vector (len)
2942 EMACS_INT len;
2944 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_VECTOR);
2945 EMACS_INT i;
2947 for (i = 0; i < len; ++i)
2948 v->contents[i] = Qnil;
2949 v->size = len;
2951 return v;
2955 DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0,
2956 doc: /* Return a newly created vector of length LENGTH, with each element being INIT.
2957 See also the function `vector'. */)
2958 (length, init)
2959 register Lisp_Object length, init;
2961 Lisp_Object vector;
2962 register EMACS_INT sizei;
2963 register int index;
2964 register struct Lisp_Vector *p;
2966 CHECK_NATNUM (length);
2967 sizei = XFASTINT (length);
2969 p = allocate_vector (sizei);
2970 for (index = 0; index < sizei; index++)
2971 p->contents[index] = init;
2973 XSETVECTOR (vector, p);
2974 return vector;
2978 DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0,
2979 doc: /* Return a newly created char-table, with purpose PURPOSE.
2980 Each element is initialized to INIT, which defaults to nil.
2981 PURPOSE should be a symbol which has a `char-table-extra-slots' property.
2982 The property's value should be an integer between 0 and 10. */)
2983 (purpose, init)
2984 register Lisp_Object purpose, init;
2986 Lisp_Object vector;
2987 Lisp_Object n;
2988 CHECK_SYMBOL (purpose);
2989 n = Fget (purpose, Qchar_table_extra_slots);
2990 CHECK_NUMBER (n);
2991 if (XINT (n) < 0 || XINT (n) > 10)
2992 args_out_of_range (n, Qnil);
2993 /* Add 2 to the size for the defalt and parent slots. */
2994 vector = Fmake_vector (make_number (CHAR_TABLE_STANDARD_SLOTS + XINT (n)),
2995 init);
2996 XCHAR_TABLE (vector)->top = Qt;
2997 XCHAR_TABLE (vector)->parent = Qnil;
2998 XCHAR_TABLE (vector)->purpose = purpose;
2999 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
3000 return vector;
3004 /* Return a newly created sub char table with slots initialized by INIT.
3005 Since a sub char table does not appear as a top level Emacs Lisp
3006 object, we don't need a Lisp interface to make it. */
3008 Lisp_Object
3009 make_sub_char_table (init)
3010 Lisp_Object init;
3012 Lisp_Object vector
3013 = Fmake_vector (make_number (SUB_CHAR_TABLE_STANDARD_SLOTS), init);
3014 XCHAR_TABLE (vector)->top = Qnil;
3015 XCHAR_TABLE (vector)->defalt = Qnil;
3016 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
3017 return vector;
3021 DEFUN ("vector", Fvector, Svector, 0, MANY, 0,
3022 doc: /* Return a newly created vector with specified arguments as elements.
3023 Any number of arguments, even zero arguments, are allowed.
3024 usage: (vector &rest OBJECTS) */)
3025 (nargs, args)
3026 register int nargs;
3027 Lisp_Object *args;
3029 register Lisp_Object len, val;
3030 register int index;
3031 register struct Lisp_Vector *p;
3033 XSETFASTINT (len, nargs);
3034 val = Fmake_vector (len, Qnil);
3035 p = XVECTOR (val);
3036 for (index = 0; index < nargs; index++)
3037 p->contents[index] = args[index];
3038 return val;
3042 DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0,
3043 doc: /* Create a byte-code object with specified arguments as elements.
3044 The arguments should be the arglist, bytecode-string, constant vector,
3045 stack size, (optional) doc string, and (optional) interactive spec.
3046 The first four arguments are required; at most six have any
3047 significance.
3048 usage: (make-byte-code ARGLIST BYTE-CODE CONSTANTS DEPTH &optional DOCSTRING INTERACTIVE-SPEC &rest ELEMENTS) */)
3049 (nargs, args)
3050 register int nargs;
3051 Lisp_Object *args;
3053 register Lisp_Object len, val;
3054 register int index;
3055 register struct Lisp_Vector *p;
3057 XSETFASTINT (len, nargs);
3058 if (!NILP (Vpurify_flag))
3059 val = make_pure_vector ((EMACS_INT) nargs);
3060 else
3061 val = Fmake_vector (len, Qnil);
3063 if (STRINGP (args[1]) && STRING_MULTIBYTE (args[1]))
3064 /* BYTECODE-STRING must have been produced by Emacs 20.2 or the
3065 earlier because they produced a raw 8-bit string for byte-code
3066 and now such a byte-code string is loaded as multibyte while
3067 raw 8-bit characters converted to multibyte form. Thus, now we
3068 must convert them back to the original unibyte form. */
3069 args[1] = Fstring_as_unibyte (args[1]);
3071 p = XVECTOR (val);
3072 for (index = 0; index < nargs; index++)
3074 if (!NILP (Vpurify_flag))
3075 args[index] = Fpurecopy (args[index]);
3076 p->contents[index] = args[index];
3078 XSETCOMPILED (val, p);
3079 return val;
3084 /***********************************************************************
3085 Symbol Allocation
3086 ***********************************************************************/
3088 /* Each symbol_block is just under 1020 bytes long, since malloc
3089 really allocates in units of powers of two and uses 4 bytes for its
3090 own overhead. */
3092 #define SYMBOL_BLOCK_SIZE \
3093 ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol))
3095 struct symbol_block
3097 /* Place `symbols' first, to preserve alignment. */
3098 struct Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE];
3099 struct symbol_block *next;
3102 /* Current symbol block and index of first unused Lisp_Symbol
3103 structure in it. */
3105 struct symbol_block *symbol_block;
3106 int symbol_block_index;
3108 /* List of free symbols. */
3110 struct Lisp_Symbol *symbol_free_list;
3112 /* Total number of symbol blocks now in use. */
3114 int n_symbol_blocks;
3117 /* Initialize symbol allocation. */
3119 void
3120 init_symbol ()
3122 symbol_block = NULL;
3123 symbol_block_index = SYMBOL_BLOCK_SIZE;
3124 symbol_free_list = 0;
3125 n_symbol_blocks = 0;
3129 DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0,
3130 doc: /* Return a newly allocated uninterned symbol whose name is NAME.
3131 Its value and function definition are void, and its property list is nil. */)
3132 (name)
3133 Lisp_Object name;
3135 register Lisp_Object val;
3136 register struct Lisp_Symbol *p;
3138 CHECK_STRING (name);
3140 if (symbol_free_list)
3142 XSETSYMBOL (val, symbol_free_list);
3143 symbol_free_list = symbol_free_list->next;
3145 else
3147 if (symbol_block_index == SYMBOL_BLOCK_SIZE)
3149 struct symbol_block *new;
3150 new = (struct symbol_block *) lisp_malloc (sizeof *new,
3151 MEM_TYPE_SYMBOL);
3152 new->next = symbol_block;
3153 symbol_block = new;
3154 symbol_block_index = 0;
3155 n_symbol_blocks++;
3157 XSETSYMBOL (val, &symbol_block->symbols[symbol_block_index]);
3158 symbol_block_index++;
3161 p = XSYMBOL (val);
3162 p->xname = name;
3163 p->plist = Qnil;
3164 p->value = Qunbound;
3165 p->function = Qunbound;
3166 p->next = NULL;
3167 p->gcmarkbit = 0;
3168 p->interned = SYMBOL_UNINTERNED;
3169 p->constant = 0;
3170 p->indirect_variable = 0;
3171 consing_since_gc += sizeof (struct Lisp_Symbol);
3172 symbols_consed++;
3173 return val;
3178 /***********************************************************************
3179 Marker (Misc) Allocation
3180 ***********************************************************************/
3182 /* Allocation of markers and other objects that share that structure.
3183 Works like allocation of conses. */
3185 #define MARKER_BLOCK_SIZE \
3186 ((1020 - sizeof (struct marker_block *)) / sizeof (union Lisp_Misc))
3188 struct marker_block
3190 /* Place `markers' first, to preserve alignment. */
3191 union Lisp_Misc markers[MARKER_BLOCK_SIZE];
3192 struct marker_block *next;
3195 struct marker_block *marker_block;
3196 int marker_block_index;
3198 union Lisp_Misc *marker_free_list;
3200 /* Total number of marker blocks now in use. */
3202 int n_marker_blocks;
3204 void
3205 init_marker ()
3207 marker_block = NULL;
3208 marker_block_index = MARKER_BLOCK_SIZE;
3209 marker_free_list = 0;
3210 n_marker_blocks = 0;
3213 /* Return a newly allocated Lisp_Misc object, with no substructure. */
3215 Lisp_Object
3216 allocate_misc ()
3218 Lisp_Object val;
3220 if (marker_free_list)
3222 XSETMISC (val, marker_free_list);
3223 marker_free_list = marker_free_list->u_free.chain;
3225 else
3227 if (marker_block_index == MARKER_BLOCK_SIZE)
3229 struct marker_block *new;
3230 new = (struct marker_block *) lisp_malloc (sizeof *new,
3231 MEM_TYPE_MISC);
3232 new->next = marker_block;
3233 marker_block = new;
3234 marker_block_index = 0;
3235 n_marker_blocks++;
3236 total_free_markers += MARKER_BLOCK_SIZE;
3238 XSETMISC (val, &marker_block->markers[marker_block_index]);
3239 marker_block_index++;
3242 --total_free_markers;
3243 consing_since_gc += sizeof (union Lisp_Misc);
3244 misc_objects_consed++;
3245 XMARKER (val)->gcmarkbit = 0;
3246 return val;
3249 /* Free a Lisp_Misc object */
3251 void
3252 free_misc (misc)
3253 Lisp_Object misc;
3255 XMISC (misc)->u_marker.type = Lisp_Misc_Free;
3256 XMISC (misc)->u_free.chain = marker_free_list;
3257 marker_free_list = XMISC (misc);
3259 total_free_markers++;
3262 /* Return a Lisp_Misc_Save_Value object containing POINTER and
3263 INTEGER. This is used to package C values to call record_unwind_protect.
3264 The unwind function can get the C values back using XSAVE_VALUE. */
3266 Lisp_Object
3267 make_save_value (pointer, integer)
3268 void *pointer;
3269 int integer;
3271 register Lisp_Object val;
3272 register struct Lisp_Save_Value *p;
3274 val = allocate_misc ();
3275 XMISCTYPE (val) = Lisp_Misc_Save_Value;
3276 p = XSAVE_VALUE (val);
3277 p->pointer = pointer;
3278 p->integer = integer;
3279 p->dogc = 0;
3280 return val;
3283 DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
3284 doc: /* Return a newly allocated marker which does not point at any place. */)
3287 register Lisp_Object val;
3288 register struct Lisp_Marker *p;
3290 val = allocate_misc ();
3291 XMISCTYPE (val) = Lisp_Misc_Marker;
3292 p = XMARKER (val);
3293 p->buffer = 0;
3294 p->bytepos = 0;
3295 p->charpos = 0;
3296 p->next = NULL;
3297 p->insertion_type = 0;
3298 return val;
3301 /* Put MARKER back on the free list after using it temporarily. */
3303 void
3304 free_marker (marker)
3305 Lisp_Object marker;
3307 unchain_marker (XMARKER (marker));
3308 free_misc (marker);
3312 /* Return a newly created vector or string with specified arguments as
3313 elements. If all the arguments are characters that can fit
3314 in a string of events, make a string; otherwise, make a vector.
3316 Any number of arguments, even zero arguments, are allowed. */
3318 Lisp_Object
3319 make_event_array (nargs, args)
3320 register int nargs;
3321 Lisp_Object *args;
3323 int i;
3325 for (i = 0; i < nargs; i++)
3326 /* The things that fit in a string
3327 are characters that are in 0...127,
3328 after discarding the meta bit and all the bits above it. */
3329 if (!INTEGERP (args[i])
3330 || (XUINT (args[i]) & ~(-CHAR_META)) >= 0200)
3331 return Fvector (nargs, args);
3333 /* Since the loop exited, we know that all the things in it are
3334 characters, so we can make a string. */
3336 Lisp_Object result;
3338 result = Fmake_string (make_number (nargs), make_number (0));
3339 for (i = 0; i < nargs; i++)
3341 SSET (result, i, XINT (args[i]));
3342 /* Move the meta bit to the right place for a string char. */
3343 if (XINT (args[i]) & CHAR_META)
3344 SSET (result, i, SREF (result, i) | 0x80);
3347 return result;
3353 /************************************************************************
3354 Memory Full Handling
3355 ************************************************************************/
3358 /* Called if malloc returns zero. */
3360 void
3361 memory_full ()
3363 int i;
3365 Vmemory_full = Qt;
3367 memory_full_cons_threshold = sizeof (struct cons_block);
3369 /* The first time we get here, free the spare memory. */
3370 for (i = 0; i < sizeof (spare_memory) / sizeof (char *); i++)
3371 if (spare_memory[i])
3373 if (i == 0)
3374 free (spare_memory[i]);
3375 else if (i >= 1 && i <= 4)
3376 lisp_align_free (spare_memory[i]);
3377 else
3378 lisp_free (spare_memory[i]);
3379 spare_memory[i] = 0;
3382 /* Record the space now used. When it decreases substantially,
3383 we can refill the memory reserve. */
3384 #ifndef SYSTEM_MALLOC
3385 bytes_used_when_full = BYTES_USED;
3386 #endif
3388 /* This used to call error, but if we've run out of memory, we could
3389 get infinite recursion trying to build the string. */
3390 while (1)
3391 Fsignal (Qnil, Vmemory_signal_data);
3394 /* If we released our reserve (due to running out of memory),
3395 and we have a fair amount free once again,
3396 try to set aside another reserve in case we run out once more.
3398 This is called when a relocatable block is freed in ralloc.c,
3399 and also directly from this file, in case we're not using ralloc.c. */
3401 void
3402 refill_memory_reserve ()
3404 #ifndef SYSTEM_MALLOC
3405 if (spare_memory[0] == 0)
3406 spare_memory[0] = (char *) malloc ((size_t) SPARE_MEMORY);
3407 if (spare_memory[1] == 0)
3408 spare_memory[1] = (char *) lisp_align_malloc (sizeof (struct cons_block),
3409 MEM_TYPE_CONS);
3410 if (spare_memory[2] == 0)
3411 spare_memory[2] = (char *) lisp_align_malloc (sizeof (struct cons_block),
3412 MEM_TYPE_CONS);
3413 if (spare_memory[3] == 0)
3414 spare_memory[3] = (char *) lisp_align_malloc (sizeof (struct cons_block),
3415 MEM_TYPE_CONS);
3416 if (spare_memory[4] == 0)
3417 spare_memory[4] = (char *) lisp_align_malloc (sizeof (struct cons_block),
3418 MEM_TYPE_CONS);
3419 if (spare_memory[5] == 0)
3420 spare_memory[5] = (char *) lisp_malloc (sizeof (struct string_block),
3421 MEM_TYPE_STRING);
3422 if (spare_memory[6] == 0)
3423 spare_memory[6] = (char *) lisp_malloc (sizeof (struct string_block),
3424 MEM_TYPE_STRING);
3425 if (spare_memory[0] && spare_memory[1] && spare_memory[5])
3426 Vmemory_full = Qnil;
3427 #endif
3430 /************************************************************************
3431 C Stack Marking
3432 ************************************************************************/
3434 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
3436 /* Conservative C stack marking requires a method to identify possibly
3437 live Lisp objects given a pointer value. We do this by keeping
3438 track of blocks of Lisp data that are allocated in a red-black tree
3439 (see also the comment of mem_node which is the type of nodes in
3440 that tree). Function lisp_malloc adds information for an allocated
3441 block to the red-black tree with calls to mem_insert, and function
3442 lisp_free removes it with mem_delete. Functions live_string_p etc
3443 call mem_find to lookup information about a given pointer in the
3444 tree, and use that to determine if the pointer points to a Lisp
3445 object or not. */
3447 /* Initialize this part of alloc.c. */
3449 static void
3450 mem_init ()
3452 mem_z.left = mem_z.right = MEM_NIL;
3453 mem_z.parent = NULL;
3454 mem_z.color = MEM_BLACK;
3455 mem_z.start = mem_z.end = NULL;
3456 mem_root = MEM_NIL;
3460 /* Value is a pointer to the mem_node containing START. Value is
3461 MEM_NIL if there is no node in the tree containing START. */
3463 static INLINE struct mem_node *
3464 mem_find (start)
3465 void *start;
3467 struct mem_node *p;
3469 if (start < min_heap_address || start > max_heap_address)
3470 return MEM_NIL;
3472 /* Make the search always successful to speed up the loop below. */
3473 mem_z.start = start;
3474 mem_z.end = (char *) start + 1;
3476 p = mem_root;
3477 while (start < p->start || start >= p->end)
3478 p = start < p->start ? p->left : p->right;
3479 return p;
3483 /* Insert a new node into the tree for a block of memory with start
3484 address START, end address END, and type TYPE. Value is a
3485 pointer to the node that was inserted. */
3487 static struct mem_node *
3488 mem_insert (start, end, type)
3489 void *start, *end;
3490 enum mem_type type;
3492 struct mem_node *c, *parent, *x;
3494 if (start < min_heap_address)
3495 min_heap_address = start;
3496 if (end > max_heap_address)
3497 max_heap_address = end;
3499 /* See where in the tree a node for START belongs. In this
3500 particular application, it shouldn't happen that a node is already
3501 present. For debugging purposes, let's check that. */
3502 c = mem_root;
3503 parent = NULL;
3505 #if GC_MARK_STACK != GC_MAKE_GCPROS_NOOPS
3507 while (c != MEM_NIL)
3509 if (start >= c->start && start < c->end)
3510 abort ();
3511 parent = c;
3512 c = start < c->start ? c->left : c->right;
3515 #else /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
3517 while (c != MEM_NIL)
3519 parent = c;
3520 c = start < c->start ? c->left : c->right;
3523 #endif /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
3525 /* Create a new node. */
3526 #ifdef GC_MALLOC_CHECK
3527 x = (struct mem_node *) _malloc_internal (sizeof *x);
3528 if (x == NULL)
3529 abort ();
3530 #else
3531 x = (struct mem_node *) xmalloc (sizeof *x);
3532 #endif
3533 x->start = start;
3534 x->end = end;
3535 x->type = type;
3536 x->parent = parent;
3537 x->left = x->right = MEM_NIL;
3538 x->color = MEM_RED;
3540 /* Insert it as child of PARENT or install it as root. */
3541 if (parent)
3543 if (start < parent->start)
3544 parent->left = x;
3545 else
3546 parent->right = x;
3548 else
3549 mem_root = x;
3551 /* Re-establish red-black tree properties. */
3552 mem_insert_fixup (x);
3554 return x;
3558 /* Re-establish the red-black properties of the tree, and thereby
3559 balance the tree, after node X has been inserted; X is always red. */
3561 static void
3562 mem_insert_fixup (x)
3563 struct mem_node *x;
3565 while (x != mem_root && x->parent->color == MEM_RED)
3567 /* X is red and its parent is red. This is a violation of
3568 red-black tree property #3. */
3570 if (x->parent == x->parent->parent->left)
3572 /* We're on the left side of our grandparent, and Y is our
3573 "uncle". */
3574 struct mem_node *y = x->parent->parent->right;
3576 if (y->color == MEM_RED)
3578 /* Uncle and parent are red but should be black because
3579 X is red. Change the colors accordingly and proceed
3580 with the grandparent. */
3581 x->parent->color = MEM_BLACK;
3582 y->color = MEM_BLACK;
3583 x->parent->parent->color = MEM_RED;
3584 x = x->parent->parent;
3586 else
3588 /* Parent and uncle have different colors; parent is
3589 red, uncle is black. */
3590 if (x == x->parent->right)
3592 x = x->parent;
3593 mem_rotate_left (x);
3596 x->parent->color = MEM_BLACK;
3597 x->parent->parent->color = MEM_RED;
3598 mem_rotate_right (x->parent->parent);
3601 else
3603 /* This is the symmetrical case of above. */
3604 struct mem_node *y = x->parent->parent->left;
3606 if (y->color == MEM_RED)
3608 x->parent->color = MEM_BLACK;
3609 y->color = MEM_BLACK;
3610 x->parent->parent->color = MEM_RED;
3611 x = x->parent->parent;
3613 else
3615 if (x == x->parent->left)
3617 x = x->parent;
3618 mem_rotate_right (x);
3621 x->parent->color = MEM_BLACK;
3622 x->parent->parent->color = MEM_RED;
3623 mem_rotate_left (x->parent->parent);
3628 /* The root may have been changed to red due to the algorithm. Set
3629 it to black so that property #5 is satisfied. */
3630 mem_root->color = MEM_BLACK;
3634 /* (x) (y)
3635 / \ / \
3636 a (y) ===> (x) c
3637 / \ / \
3638 b c a b */
3640 static void
3641 mem_rotate_left (x)
3642 struct mem_node *x;
3644 struct mem_node *y;
3646 /* Turn y's left sub-tree into x's right sub-tree. */
3647 y = x->right;
3648 x->right = y->left;
3649 if (y->left != MEM_NIL)
3650 y->left->parent = x;
3652 /* Y's parent was x's parent. */
3653 if (y != MEM_NIL)
3654 y->parent = x->parent;
3656 /* Get the parent to point to y instead of x. */
3657 if (x->parent)
3659 if (x == x->parent->left)
3660 x->parent->left = y;
3661 else
3662 x->parent->right = y;
3664 else
3665 mem_root = y;
3667 /* Put x on y's left. */
3668 y->left = x;
3669 if (x != MEM_NIL)
3670 x->parent = y;
3674 /* (x) (Y)
3675 / \ / \
3676 (y) c ===> a (x)
3677 / \ / \
3678 a b b c */
3680 static void
3681 mem_rotate_right (x)
3682 struct mem_node *x;
3684 struct mem_node *y = x->left;
3686 x->left = y->right;
3687 if (y->right != MEM_NIL)
3688 y->right->parent = x;
3690 if (y != MEM_NIL)
3691 y->parent = x->parent;
3692 if (x->parent)
3694 if (x == x->parent->right)
3695 x->parent->right = y;
3696 else
3697 x->parent->left = y;
3699 else
3700 mem_root = y;
3702 y->right = x;
3703 if (x != MEM_NIL)
3704 x->parent = y;
3708 /* Delete node Z from the tree. If Z is null or MEM_NIL, do nothing. */
3710 static void
3711 mem_delete (z)
3712 struct mem_node *z;
3714 struct mem_node *x, *y;
3716 if (!z || z == MEM_NIL)
3717 return;
3719 if (z->left == MEM_NIL || z->right == MEM_NIL)
3720 y = z;
3721 else
3723 y = z->right;
3724 while (y->left != MEM_NIL)
3725 y = y->left;
3728 if (y->left != MEM_NIL)
3729 x = y->left;
3730 else
3731 x = y->right;
3733 x->parent = y->parent;
3734 if (y->parent)
3736 if (y == y->parent->left)
3737 y->parent->left = x;
3738 else
3739 y->parent->right = x;
3741 else
3742 mem_root = x;
3744 if (y != z)
3746 z->start = y->start;
3747 z->end = y->end;
3748 z->type = y->type;
3751 if (y->color == MEM_BLACK)
3752 mem_delete_fixup (x);
3754 #ifdef GC_MALLOC_CHECK
3755 _free_internal (y);
3756 #else
3757 xfree (y);
3758 #endif
3762 /* Re-establish the red-black properties of the tree, after a
3763 deletion. */
3765 static void
3766 mem_delete_fixup (x)
3767 struct mem_node *x;
3769 while (x != mem_root && x->color == MEM_BLACK)
3771 if (x == x->parent->left)
3773 struct mem_node *w = x->parent->right;
3775 if (w->color == MEM_RED)
3777 w->color = MEM_BLACK;
3778 x->parent->color = MEM_RED;
3779 mem_rotate_left (x->parent);
3780 w = x->parent->right;
3783 if (w->left->color == MEM_BLACK && w->right->color == MEM_BLACK)
3785 w->color = MEM_RED;
3786 x = x->parent;
3788 else
3790 if (w->right->color == MEM_BLACK)
3792 w->left->color = MEM_BLACK;
3793 w->color = MEM_RED;
3794 mem_rotate_right (w);
3795 w = x->parent->right;
3797 w->color = x->parent->color;
3798 x->parent->color = MEM_BLACK;
3799 w->right->color = MEM_BLACK;
3800 mem_rotate_left (x->parent);
3801 x = mem_root;
3804 else
3806 struct mem_node *w = x->parent->left;
3808 if (w->color == MEM_RED)
3810 w->color = MEM_BLACK;
3811 x->parent->color = MEM_RED;
3812 mem_rotate_right (x->parent);
3813 w = x->parent->left;
3816 if (w->right->color == MEM_BLACK && w->left->color == MEM_BLACK)
3818 w->color = MEM_RED;
3819 x = x->parent;
3821 else
3823 if (w->left->color == MEM_BLACK)
3825 w->right->color = MEM_BLACK;
3826 w->color = MEM_RED;
3827 mem_rotate_left (w);
3828 w = x->parent->left;
3831 w->color = x->parent->color;
3832 x->parent->color = MEM_BLACK;
3833 w->left->color = MEM_BLACK;
3834 mem_rotate_right (x->parent);
3835 x = mem_root;
3840 x->color = MEM_BLACK;
3844 /* Value is non-zero if P is a pointer to a live Lisp string on
3845 the heap. M is a pointer to the mem_block for P. */
3847 static INLINE int
3848 live_string_p (m, p)
3849 struct mem_node *m;
3850 void *p;
3852 if (m->type == MEM_TYPE_STRING)
3854 struct string_block *b = (struct string_block *) m->start;
3855 int offset = (char *) p - (char *) &b->strings[0];
3857 /* P must point to the start of a Lisp_String structure, and it
3858 must not be on the free-list. */
3859 return (offset >= 0
3860 && offset % sizeof b->strings[0] == 0
3861 && offset < (STRING_BLOCK_SIZE * sizeof b->strings[0])
3862 && ((struct Lisp_String *) p)->data != NULL);
3864 else
3865 return 0;
3869 /* Value is non-zero if P is a pointer to a live Lisp cons on
3870 the heap. M is a pointer to the mem_block for P. */
3872 static INLINE int
3873 live_cons_p (m, p)
3874 struct mem_node *m;
3875 void *p;
3877 if (m->type == MEM_TYPE_CONS)
3879 struct cons_block *b = (struct cons_block *) m->start;
3880 int offset = (char *) p - (char *) &b->conses[0];
3882 /* P must point to the start of a Lisp_Cons, not be
3883 one of the unused cells in the current cons block,
3884 and not be on the free-list. */
3885 return (offset >= 0
3886 && offset % sizeof b->conses[0] == 0
3887 && offset < (CONS_BLOCK_SIZE * sizeof b->conses[0])
3888 && (b != cons_block
3889 || offset / sizeof b->conses[0] < cons_block_index)
3890 && !EQ (((struct Lisp_Cons *) p)->car, Vdead));
3892 else
3893 return 0;
3897 /* Value is non-zero if P is a pointer to a live Lisp symbol on
3898 the heap. M is a pointer to the mem_block for P. */
3900 static INLINE int
3901 live_symbol_p (m, p)
3902 struct mem_node *m;
3903 void *p;
3905 if (m->type == MEM_TYPE_SYMBOL)
3907 struct symbol_block *b = (struct symbol_block *) m->start;
3908 int offset = (char *) p - (char *) &b->symbols[0];
3910 /* P must point to the start of a Lisp_Symbol, not be
3911 one of the unused cells in the current symbol block,
3912 and not be on the free-list. */
3913 return (offset >= 0
3914 && offset % sizeof b->symbols[0] == 0
3915 && offset < (SYMBOL_BLOCK_SIZE * sizeof b->symbols[0])
3916 && (b != symbol_block
3917 || offset / sizeof b->symbols[0] < symbol_block_index)
3918 && !EQ (((struct Lisp_Symbol *) p)->function, Vdead));
3920 else
3921 return 0;
3925 /* Value is non-zero if P is a pointer to a live Lisp float on
3926 the heap. M is a pointer to the mem_block for P. */
3928 static INLINE int
3929 live_float_p (m, p)
3930 struct mem_node *m;
3931 void *p;
3933 if (m->type == MEM_TYPE_FLOAT)
3935 struct float_block *b = (struct float_block *) m->start;
3936 int offset = (char *) p - (char *) &b->floats[0];
3938 /* P must point to the start of a Lisp_Float and not be
3939 one of the unused cells in the current float block. */
3940 return (offset >= 0
3941 && offset % sizeof b->floats[0] == 0
3942 && offset < (FLOAT_BLOCK_SIZE * sizeof b->floats[0])
3943 && (b != float_block
3944 || offset / sizeof b->floats[0] < float_block_index));
3946 else
3947 return 0;
3951 /* Value is non-zero if P is a pointer to a live Lisp Misc on
3952 the heap. M is a pointer to the mem_block for P. */
3954 static INLINE int
3955 live_misc_p (m, p)
3956 struct mem_node *m;
3957 void *p;
3959 if (m->type == MEM_TYPE_MISC)
3961 struct marker_block *b = (struct marker_block *) m->start;
3962 int offset = (char *) p - (char *) &b->markers[0];
3964 /* P must point to the start of a Lisp_Misc, not be
3965 one of the unused cells in the current misc block,
3966 and not be on the free-list. */
3967 return (offset >= 0
3968 && offset % sizeof b->markers[0] == 0
3969 && offset < (MARKER_BLOCK_SIZE * sizeof b->markers[0])
3970 && (b != marker_block
3971 || offset / sizeof b->markers[0] < marker_block_index)
3972 && ((union Lisp_Misc *) p)->u_marker.type != Lisp_Misc_Free);
3974 else
3975 return 0;
3979 /* Value is non-zero if P is a pointer to a live vector-like object.
3980 M is a pointer to the mem_block for P. */
3982 static INLINE int
3983 live_vector_p (m, p)
3984 struct mem_node *m;
3985 void *p;
3987 return (p == m->start
3988 && m->type >= MEM_TYPE_VECTOR
3989 && m->type <= MEM_TYPE_WINDOW);
3993 /* Value is non-zero if P is a pointer to a live buffer. M is a
3994 pointer to the mem_block for P. */
3996 static INLINE int
3997 live_buffer_p (m, p)
3998 struct mem_node *m;
3999 void *p;
4001 /* P must point to the start of the block, and the buffer
4002 must not have been killed. */
4003 return (m->type == MEM_TYPE_BUFFER
4004 && p == m->start
4005 && !NILP (((struct buffer *) p)->name));
4008 #endif /* GC_MARK_STACK || defined GC_MALLOC_CHECK */
4010 #if GC_MARK_STACK
4012 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4014 /* Array of objects that are kept alive because the C stack contains
4015 a pattern that looks like a reference to them . */
4017 #define MAX_ZOMBIES 10
4018 static Lisp_Object zombies[MAX_ZOMBIES];
4020 /* Number of zombie objects. */
4022 static int nzombies;
4024 /* Number of garbage collections. */
4026 static int ngcs;
4028 /* Average percentage of zombies per collection. */
4030 static double avg_zombies;
4032 /* Max. number of live and zombie objects. */
4034 static int max_live, max_zombies;
4036 /* Average number of live objects per GC. */
4038 static double avg_live;
4040 DEFUN ("gc-status", Fgc_status, Sgc_status, 0, 0, "",
4041 doc: /* Show information about live and zombie objects. */)
4044 Lisp_Object args[8], zombie_list = Qnil;
4045 int i;
4046 for (i = 0; i < nzombies; i++)
4047 zombie_list = Fcons (zombies[i], zombie_list);
4048 args[0] = build_string ("%d GCs, avg live/zombies = %.2f/%.2f (%f%%), max %d/%d\nzombies: %S");
4049 args[1] = make_number (ngcs);
4050 args[2] = make_float (avg_live);
4051 args[3] = make_float (avg_zombies);
4052 args[4] = make_float (avg_zombies / avg_live / 100);
4053 args[5] = make_number (max_live);
4054 args[6] = make_number (max_zombies);
4055 args[7] = zombie_list;
4056 return Fmessage (8, args);
4059 #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
4062 /* Mark OBJ if we can prove it's a Lisp_Object. */
4064 static INLINE void
4065 mark_maybe_object (obj)
4066 Lisp_Object obj;
4068 void *po = (void *) XPNTR (obj);
4069 struct mem_node *m = mem_find (po);
4071 if (m != MEM_NIL)
4073 int mark_p = 0;
4075 switch (XGCTYPE (obj))
4077 case Lisp_String:
4078 mark_p = (live_string_p (m, po)
4079 && !STRING_MARKED_P ((struct Lisp_String *) po));
4080 break;
4082 case Lisp_Cons:
4083 mark_p = (live_cons_p (m, po) && !CONS_MARKED_P (XCONS (obj)));
4084 break;
4086 case Lisp_Symbol:
4087 mark_p = (live_symbol_p (m, po) && !XSYMBOL (obj)->gcmarkbit);
4088 break;
4090 case Lisp_Float:
4091 mark_p = (live_float_p (m, po) && !FLOAT_MARKED_P (XFLOAT (obj)));
4092 break;
4094 case Lisp_Vectorlike:
4095 /* Note: can't check GC_BUFFERP before we know it's a
4096 buffer because checking that dereferences the pointer
4097 PO which might point anywhere. */
4098 if (live_vector_p (m, po))
4099 mark_p = !GC_SUBRP (obj) && !VECTOR_MARKED_P (XVECTOR (obj));
4100 else if (live_buffer_p (m, po))
4101 mark_p = GC_BUFFERP (obj) && !VECTOR_MARKED_P (XBUFFER (obj));
4102 break;
4104 case Lisp_Misc:
4105 mark_p = (live_misc_p (m, po) && !XMARKER (obj)->gcmarkbit);
4106 break;
4108 case Lisp_Int:
4109 case Lisp_Type_Limit:
4110 break;
4113 if (mark_p)
4115 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4116 if (nzombies < MAX_ZOMBIES)
4117 zombies[nzombies] = obj;
4118 ++nzombies;
4119 #endif
4120 mark_object (obj);
4126 /* If P points to Lisp data, mark that as live if it isn't already
4127 marked. */
4129 static INLINE void
4130 mark_maybe_pointer (p)
4131 void *p;
4133 struct mem_node *m;
4135 /* Quickly rule out some values which can't point to Lisp data. We
4136 assume that Lisp data is aligned on even addresses. */
4137 if ((EMACS_INT) p & 1)
4138 return;
4140 m = mem_find (p);
4141 if (m != MEM_NIL)
4143 Lisp_Object obj = Qnil;
4145 switch (m->type)
4147 case MEM_TYPE_NON_LISP:
4148 /* Nothing to do; not a pointer to Lisp memory. */
4149 break;
4151 case MEM_TYPE_BUFFER:
4152 if (live_buffer_p (m, p) && !VECTOR_MARKED_P((struct buffer *)p))
4153 XSETVECTOR (obj, p);
4154 break;
4156 case MEM_TYPE_CONS:
4157 if (live_cons_p (m, p) && !CONS_MARKED_P ((struct Lisp_Cons *) p))
4158 XSETCONS (obj, p);
4159 break;
4161 case MEM_TYPE_STRING:
4162 if (live_string_p (m, p)
4163 && !STRING_MARKED_P ((struct Lisp_String *) p))
4164 XSETSTRING (obj, p);
4165 break;
4167 case MEM_TYPE_MISC:
4168 if (live_misc_p (m, p) && !((struct Lisp_Free *) p)->gcmarkbit)
4169 XSETMISC (obj, p);
4170 break;
4172 case MEM_TYPE_SYMBOL:
4173 if (live_symbol_p (m, p) && !((struct Lisp_Symbol *) p)->gcmarkbit)
4174 XSETSYMBOL (obj, p);
4175 break;
4177 case MEM_TYPE_FLOAT:
4178 if (live_float_p (m, p) && !FLOAT_MARKED_P (p))
4179 XSETFLOAT (obj, p);
4180 break;
4182 case MEM_TYPE_VECTOR:
4183 case MEM_TYPE_PROCESS:
4184 case MEM_TYPE_HASH_TABLE:
4185 case MEM_TYPE_FRAME:
4186 case MEM_TYPE_WINDOW:
4187 if (live_vector_p (m, p))
4189 Lisp_Object tem;
4190 XSETVECTOR (tem, p);
4191 if (!GC_SUBRP (tem) && !VECTOR_MARKED_P (XVECTOR (tem)))
4192 obj = tem;
4194 break;
4196 default:
4197 abort ();
4200 if (!GC_NILP (obj))
4201 mark_object (obj);
4206 /* Mark Lisp objects referenced from the address range START..END. */
4208 static void
4209 mark_memory (start, end)
4210 void *start, *end;
4212 Lisp_Object *p;
4213 void **pp;
4215 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4216 nzombies = 0;
4217 #endif
4219 /* Make START the pointer to the start of the memory region,
4220 if it isn't already. */
4221 if (end < start)
4223 void *tem = start;
4224 start = end;
4225 end = tem;
4228 /* Mark Lisp_Objects. */
4229 for (p = (Lisp_Object *) start; (void *) p < end; ++p)
4230 mark_maybe_object (*p);
4232 /* Mark Lisp data pointed to. This is necessary because, in some
4233 situations, the C compiler optimizes Lisp objects away, so that
4234 only a pointer to them remains. Example:
4236 DEFUN ("testme", Ftestme, Stestme, 0, 0, 0, "")
4239 Lisp_Object obj = build_string ("test");
4240 struct Lisp_String *s = XSTRING (obj);
4241 Fgarbage_collect ();
4242 fprintf (stderr, "test `%s'\n", s->data);
4243 return Qnil;
4246 Here, `obj' isn't really used, and the compiler optimizes it
4247 away. The only reference to the life string is through the
4248 pointer `s'. */
4250 for (pp = (void **) start; (void *) pp < end; ++pp)
4251 mark_maybe_pointer (*pp);
4254 /* setjmp will work with GCC unless NON_SAVING_SETJMP is defined in
4255 the GCC system configuration. In gcc 3.2, the only systems for
4256 which this is so are i386-sco5 non-ELF, i386-sysv3 (maybe included
4257 by others?) and ns32k-pc532-min. */
4259 #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
4261 static int setjmp_tested_p, longjmps_done;
4263 #define SETJMP_WILL_LIKELY_WORK "\
4265 Emacs garbage collector has been changed to use conservative stack\n\
4266 marking. Emacs has determined that the method it uses to do the\n\
4267 marking will likely work on your system, but this isn't sure.\n\
4269 If you are a system-programmer, or can get the help of a local wizard\n\
4270 who is, please take a look at the function mark_stack in alloc.c, and\n\
4271 verify that the methods used are appropriate for your system.\n\
4273 Please mail the result to <emacs-devel@gnu.org>.\n\
4276 #define SETJMP_WILL_NOT_WORK "\
4278 Emacs garbage collector has been changed to use conservative stack\n\
4279 marking. Emacs has determined that the default method it uses to do the\n\
4280 marking will not work on your system. We will need a system-dependent\n\
4281 solution for your system.\n\
4283 Please take a look at the function mark_stack in alloc.c, and\n\
4284 try to find a way to make it work on your system.\n\
4286 Note that you may get false negatives, depending on the compiler.\n\
4287 In particular, you need to use -O with GCC for this test.\n\
4289 Please mail the result to <emacs-devel@gnu.org>.\n\
4293 /* Perform a quick check if it looks like setjmp saves registers in a
4294 jmp_buf. Print a message to stderr saying so. When this test
4295 succeeds, this is _not_ a proof that setjmp is sufficient for
4296 conservative stack marking. Only the sources or a disassembly
4297 can prove that. */
4299 static void
4300 test_setjmp ()
4302 char buf[10];
4303 register int x;
4304 jmp_buf jbuf;
4305 int result = 0;
4307 /* Arrange for X to be put in a register. */
4308 sprintf (buf, "1");
4309 x = strlen (buf);
4310 x = 2 * x - 1;
4312 setjmp (jbuf);
4313 if (longjmps_done == 1)
4315 /* Came here after the longjmp at the end of the function.
4317 If x == 1, the longjmp has restored the register to its
4318 value before the setjmp, and we can hope that setjmp
4319 saves all such registers in the jmp_buf, although that
4320 isn't sure.
4322 For other values of X, either something really strange is
4323 taking place, or the setjmp just didn't save the register. */
4325 if (x == 1)
4326 fprintf (stderr, SETJMP_WILL_LIKELY_WORK);
4327 else
4329 fprintf (stderr, SETJMP_WILL_NOT_WORK);
4330 exit (1);
4334 ++longjmps_done;
4335 x = 2;
4336 if (longjmps_done == 1)
4337 longjmp (jbuf, 1);
4340 #endif /* not GC_SAVE_REGISTERS_ON_STACK && not GC_SETJMP_WORKS */
4343 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
4345 /* Abort if anything GCPRO'd doesn't survive the GC. */
4347 static void
4348 check_gcpros ()
4350 struct gcpro *p;
4351 int i;
4353 for (p = gcprolist; p; p = p->next)
4354 for (i = 0; i < p->nvars; ++i)
4355 if (!survives_gc_p (p->var[i]))
4356 /* FIXME: It's not necessarily a bug. It might just be that the
4357 GCPRO is unnecessary or should release the object sooner. */
4358 abort ();
4361 #elif GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4363 static void
4364 dump_zombies ()
4366 int i;
4368 fprintf (stderr, "\nZombies kept alive = %d:\n", nzombies);
4369 for (i = 0; i < min (MAX_ZOMBIES, nzombies); ++i)
4371 fprintf (stderr, " %d = ", i);
4372 debug_print (zombies[i]);
4376 #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
4379 /* Mark live Lisp objects on the C stack.
4381 There are several system-dependent problems to consider when
4382 porting this to new architectures:
4384 Processor Registers
4386 We have to mark Lisp objects in CPU registers that can hold local
4387 variables or are used to pass parameters.
4389 If GC_SAVE_REGISTERS_ON_STACK is defined, it should expand to
4390 something that either saves relevant registers on the stack, or
4391 calls mark_maybe_object passing it each register's contents.
4393 If GC_SAVE_REGISTERS_ON_STACK is not defined, the current
4394 implementation assumes that calling setjmp saves registers we need
4395 to see in a jmp_buf which itself lies on the stack. This doesn't
4396 have to be true! It must be verified for each system, possibly
4397 by taking a look at the source code of setjmp.
4399 Stack Layout
4401 Architectures differ in the way their processor stack is organized.
4402 For example, the stack might look like this
4404 +----------------+
4405 | Lisp_Object | size = 4
4406 +----------------+
4407 | something else | size = 2
4408 +----------------+
4409 | Lisp_Object | size = 4
4410 +----------------+
4411 | ... |
4413 In such a case, not every Lisp_Object will be aligned equally. To
4414 find all Lisp_Object on the stack it won't be sufficient to walk
4415 the stack in steps of 4 bytes. Instead, two passes will be
4416 necessary, one starting at the start of the stack, and a second
4417 pass starting at the start of the stack + 2. Likewise, if the
4418 minimal alignment of Lisp_Objects on the stack is 1, four passes
4419 would be necessary, each one starting with one byte more offset
4420 from the stack start.
4422 The current code assumes by default that Lisp_Objects are aligned
4423 equally on the stack. */
4425 static void
4426 mark_stack ()
4428 int i;
4429 jmp_buf j;
4430 volatile int stack_grows_down_p = (char *) &j > (char *) stack_base;
4431 void *end;
4433 /* This trick flushes the register windows so that all the state of
4434 the process is contained in the stack. */
4435 /* Fixme: Code in the Boehm GC suggests flushing (with `flushrs') is
4436 needed on ia64 too. See mach_dep.c, where it also says inline
4437 assembler doesn't work with relevant proprietary compilers. */
4438 #ifdef sparc
4439 asm ("ta 3");
4440 #endif
4442 /* Save registers that we need to see on the stack. We need to see
4443 registers used to hold register variables and registers used to
4444 pass parameters. */
4445 #ifdef GC_SAVE_REGISTERS_ON_STACK
4446 GC_SAVE_REGISTERS_ON_STACK (end);
4447 #else /* not GC_SAVE_REGISTERS_ON_STACK */
4449 #ifndef GC_SETJMP_WORKS /* If it hasn't been checked yet that
4450 setjmp will definitely work, test it
4451 and print a message with the result
4452 of the test. */
4453 if (!setjmp_tested_p)
4455 setjmp_tested_p = 1;
4456 test_setjmp ();
4458 #endif /* GC_SETJMP_WORKS */
4460 setjmp (j);
4461 end = stack_grows_down_p ? (char *) &j + sizeof j : (char *) &j;
4462 #endif /* not GC_SAVE_REGISTERS_ON_STACK */
4464 /* This assumes that the stack is a contiguous region in memory. If
4465 that's not the case, something has to be done here to iterate
4466 over the stack segments. */
4467 #ifndef GC_LISP_OBJECT_ALIGNMENT
4468 #ifdef __GNUC__
4469 #define GC_LISP_OBJECT_ALIGNMENT __alignof__ (Lisp_Object)
4470 #else
4471 #define GC_LISP_OBJECT_ALIGNMENT sizeof (Lisp_Object)
4472 #endif
4473 #endif
4474 for (i = 0; i < sizeof (Lisp_Object); i += GC_LISP_OBJECT_ALIGNMENT)
4475 mark_memory ((char *) stack_base + i, end);
4476 /* Allow for marking a secondary stack, like the register stack on the
4477 ia64. */
4478 #ifdef GC_MARK_SECONDARY_STACK
4479 GC_MARK_SECONDARY_STACK ();
4480 #endif
4482 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
4483 check_gcpros ();
4484 #endif
4487 #endif /* GC_MARK_STACK != 0 */
4491 /* Return 1 if OBJ is a valid lisp object.
4492 Return 0 if OBJ is NOT a valid lisp object.
4493 Return -1 if we cannot validate OBJ.
4497 valid_lisp_object_p (obj)
4498 Lisp_Object obj;
4500 #if !GC_MARK_STACK
4501 /* Cannot determine this. */
4502 return -1;
4503 #else
4504 void *p;
4505 struct mem_node *m;
4507 if (INTEGERP (obj))
4508 return 1;
4510 p = (void *) XPNTR (obj);
4512 if (PURE_POINTER_P (p))
4513 return 1;
4515 m = mem_find (p);
4517 if (m == MEM_NIL)
4518 return 0;
4520 switch (m->type)
4522 case MEM_TYPE_NON_LISP:
4523 return 0;
4525 case MEM_TYPE_BUFFER:
4526 return live_buffer_p (m, p);
4528 case MEM_TYPE_CONS:
4529 return live_cons_p (m, p);
4531 case MEM_TYPE_STRING:
4532 return live_string_p (m, p);
4534 case MEM_TYPE_MISC:
4535 return live_misc_p (m, p);
4537 case MEM_TYPE_SYMBOL:
4538 return live_symbol_p (m, p);
4540 case MEM_TYPE_FLOAT:
4541 return live_float_p (m, p);
4543 case MEM_TYPE_VECTOR:
4544 case MEM_TYPE_PROCESS:
4545 case MEM_TYPE_HASH_TABLE:
4546 case MEM_TYPE_FRAME:
4547 case MEM_TYPE_WINDOW:
4548 return live_vector_p (m, p);
4550 default:
4551 break;
4554 return 0;
4555 #endif
4561 /***********************************************************************
4562 Pure Storage Management
4563 ***********************************************************************/
4565 /* Allocate room for SIZE bytes from pure Lisp storage and return a
4566 pointer to it. TYPE is the Lisp type for which the memory is
4567 allocated. TYPE < 0 means it's not used for a Lisp object.
4569 If store_pure_type_info is set and TYPE is >= 0, the type of
4570 the allocated object is recorded in pure_types. */
4572 static POINTER_TYPE *
4573 pure_alloc (size, type)
4574 size_t size;
4575 int type;
4577 POINTER_TYPE *result;
4578 #ifdef USE_LSB_TAG
4579 size_t alignment = (1 << GCTYPEBITS);
4580 #else
4581 size_t alignment = sizeof (EMACS_INT);
4583 /* Give Lisp_Floats an extra alignment. */
4584 if (type == Lisp_Float)
4586 #if defined __GNUC__ && __GNUC__ >= 2
4587 alignment = __alignof (struct Lisp_Float);
4588 #else
4589 alignment = sizeof (struct Lisp_Float);
4590 #endif
4592 #endif
4594 again:
4595 result = ALIGN (purebeg + pure_bytes_used, alignment);
4596 pure_bytes_used = ((char *)result - (char *)purebeg) + size;
4598 if (pure_bytes_used <= pure_size)
4599 return result;
4601 /* Don't allocate a large amount here,
4602 because it might get mmap'd and then its address
4603 might not be usable. */
4604 purebeg = (char *) xmalloc (10000);
4605 pure_size = 10000;
4606 pure_bytes_used_before_overflow += pure_bytes_used - size;
4607 pure_bytes_used = 0;
4608 goto again;
4612 /* Print a warning if PURESIZE is too small. */
4614 void
4615 check_pure_size ()
4617 if (pure_bytes_used_before_overflow)
4618 message ("Pure Lisp storage overflow (approx. %d bytes needed)",
4619 (int) (pure_bytes_used + pure_bytes_used_before_overflow));
4623 /* Return a string allocated in pure space. DATA is a buffer holding
4624 NCHARS characters, and NBYTES bytes of string data. MULTIBYTE
4625 non-zero means make the result string multibyte.
4627 Must get an error if pure storage is full, since if it cannot hold
4628 a large string it may be able to hold conses that point to that
4629 string; then the string is not protected from gc. */
4631 Lisp_Object
4632 make_pure_string (data, nchars, nbytes, multibyte)
4633 char *data;
4634 int nchars, nbytes;
4635 int multibyte;
4637 Lisp_Object string;
4638 struct Lisp_String *s;
4640 s = (struct Lisp_String *) pure_alloc (sizeof *s, Lisp_String);
4641 s->data = (unsigned char *) pure_alloc (nbytes + 1, -1);
4642 s->size = nchars;
4643 s->size_byte = multibyte ? nbytes : -1;
4644 bcopy (data, s->data, nbytes);
4645 s->data[nbytes] = '\0';
4646 s->intervals = NULL_INTERVAL;
4647 XSETSTRING (string, s);
4648 return string;
4652 /* Return a cons allocated from pure space. Give it pure copies
4653 of CAR as car and CDR as cdr. */
4655 Lisp_Object
4656 pure_cons (car, cdr)
4657 Lisp_Object car, cdr;
4659 register Lisp_Object new;
4660 struct Lisp_Cons *p;
4662 p = (struct Lisp_Cons *) pure_alloc (sizeof *p, Lisp_Cons);
4663 XSETCONS (new, p);
4664 XSETCAR (new, Fpurecopy (car));
4665 XSETCDR (new, Fpurecopy (cdr));
4666 return new;
4670 /* Value is a float object with value NUM allocated from pure space. */
4672 Lisp_Object
4673 make_pure_float (num)
4674 double num;
4676 register Lisp_Object new;
4677 struct Lisp_Float *p;
4679 p = (struct Lisp_Float *) pure_alloc (sizeof *p, Lisp_Float);
4680 XSETFLOAT (new, p);
4681 XFLOAT_DATA (new) = num;
4682 return new;
4686 /* Return a vector with room for LEN Lisp_Objects allocated from
4687 pure space. */
4689 Lisp_Object
4690 make_pure_vector (len)
4691 EMACS_INT len;
4693 Lisp_Object new;
4694 struct Lisp_Vector *p;
4695 size_t size = sizeof *p + (len - 1) * sizeof (Lisp_Object);
4697 p = (struct Lisp_Vector *) pure_alloc (size, Lisp_Vectorlike);
4698 XSETVECTOR (new, p);
4699 XVECTOR (new)->size = len;
4700 return new;
4704 DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
4705 doc: /* Make a copy of OBJECT in pure storage.
4706 Recursively copies contents of vectors and cons cells.
4707 Does not copy symbols. Copies strings without text properties. */)
4708 (obj)
4709 register Lisp_Object obj;
4711 if (NILP (Vpurify_flag))
4712 return obj;
4714 if (PURE_POINTER_P (XPNTR (obj)))
4715 return obj;
4717 if (CONSP (obj))
4718 return pure_cons (XCAR (obj), XCDR (obj));
4719 else if (FLOATP (obj))
4720 return make_pure_float (XFLOAT_DATA (obj));
4721 else if (STRINGP (obj))
4722 return make_pure_string (SDATA (obj), SCHARS (obj),
4723 SBYTES (obj),
4724 STRING_MULTIBYTE (obj));
4725 else if (COMPILEDP (obj) || VECTORP (obj))
4727 register struct Lisp_Vector *vec;
4728 register int i;
4729 EMACS_INT size;
4731 size = XVECTOR (obj)->size;
4732 if (size & PSEUDOVECTOR_FLAG)
4733 size &= PSEUDOVECTOR_SIZE_MASK;
4734 vec = XVECTOR (make_pure_vector (size));
4735 for (i = 0; i < size; i++)
4736 vec->contents[i] = Fpurecopy (XVECTOR (obj)->contents[i]);
4737 if (COMPILEDP (obj))
4738 XSETCOMPILED (obj, vec);
4739 else
4740 XSETVECTOR (obj, vec);
4741 return obj;
4743 else if (MARKERP (obj))
4744 error ("Attempt to copy a marker to pure storage");
4746 return obj;
4751 /***********************************************************************
4752 Protection from GC
4753 ***********************************************************************/
4755 /* Put an entry in staticvec, pointing at the variable with address
4756 VARADDRESS. */
4758 void
4759 staticpro (varaddress)
4760 Lisp_Object *varaddress;
4762 staticvec[staticidx++] = varaddress;
4763 if (staticidx >= NSTATICS)
4764 abort ();
4767 struct catchtag
4769 Lisp_Object tag;
4770 Lisp_Object val;
4771 struct catchtag *next;
4775 /***********************************************************************
4776 Protection from GC
4777 ***********************************************************************/
4779 /* Temporarily prevent garbage collection. */
4782 inhibit_garbage_collection ()
4784 int count = SPECPDL_INDEX ();
4785 int nbits = min (VALBITS, BITS_PER_INT);
4787 specbind (Qgc_cons_threshold, make_number (((EMACS_INT) 1 << (nbits - 1)) - 1));
4788 return count;
4792 DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
4793 doc: /* Reclaim storage for Lisp objects no longer needed.
4794 Garbage collection happens automatically if you cons more than
4795 `gc-cons-threshold' bytes of Lisp data since previous garbage collection.
4796 `garbage-collect' normally returns a list with info on amount of space in use:
4797 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)
4798 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS
4799 (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS)
4800 (USED-STRINGS . FREE-STRINGS))
4801 However, if there was overflow in pure space, `garbage-collect'
4802 returns nil, because real GC can't be done. */)
4805 register struct specbinding *bind;
4806 struct catchtag *catch;
4807 struct handler *handler;
4808 char stack_top_variable;
4809 register int i;
4810 int message_p;
4811 Lisp_Object total[8];
4812 int count = SPECPDL_INDEX ();
4813 EMACS_TIME t1, t2, t3;
4815 if (abort_on_gc)
4816 abort ();
4818 /* Can't GC if pure storage overflowed because we can't determine
4819 if something is a pure object or not. */
4820 if (pure_bytes_used_before_overflow)
4821 return Qnil;
4823 CHECK_CONS_LIST ();
4825 /* Don't keep undo information around forever.
4826 Do this early on, so it is no problem if the user quits. */
4828 register struct buffer *nextb = all_buffers;
4830 while (nextb)
4832 /* If a buffer's undo list is Qt, that means that undo is
4833 turned off in that buffer. Calling truncate_undo_list on
4834 Qt tends to return NULL, which effectively turns undo back on.
4835 So don't call truncate_undo_list if undo_list is Qt. */
4836 if (! NILP (nextb->name) && ! EQ (nextb->undo_list, Qt))
4837 truncate_undo_list (nextb);
4839 /* Shrink buffer gaps, but skip indirect and dead buffers. */
4840 if (nextb->base_buffer == 0 && !NILP (nextb->name))
4842 /* If a buffer's gap size is more than 10% of the buffer
4843 size, or larger than 2000 bytes, then shrink it
4844 accordingly. Keep a minimum size of 20 bytes. */
4845 int size = min (2000, max (20, (nextb->text->z_byte / 10)));
4847 if (nextb->text->gap_size > size)
4849 struct buffer *save_current = current_buffer;
4850 current_buffer = nextb;
4851 make_gap (-(nextb->text->gap_size - size));
4852 current_buffer = save_current;
4856 nextb = nextb->next;
4860 EMACS_GET_TIME (t1);
4862 /* In case user calls debug_print during GC,
4863 don't let that cause a recursive GC. */
4864 consing_since_gc = 0;
4866 /* Save what's currently displayed in the echo area. */
4867 message_p = push_message ();
4868 record_unwind_protect (pop_message_unwind, Qnil);
4870 /* Save a copy of the contents of the stack, for debugging. */
4871 #if MAX_SAVE_STACK > 0
4872 if (NILP (Vpurify_flag))
4874 i = &stack_top_variable - stack_bottom;
4875 if (i < 0) i = -i;
4876 if (i < MAX_SAVE_STACK)
4878 if (stack_copy == 0)
4879 stack_copy = (char *) xmalloc (stack_copy_size = i);
4880 else if (stack_copy_size < i)
4881 stack_copy = (char *) xrealloc (stack_copy, (stack_copy_size = i));
4882 if (stack_copy)
4884 if ((EMACS_INT) (&stack_top_variable - stack_bottom) > 0)
4885 bcopy (stack_bottom, stack_copy, i);
4886 else
4887 bcopy (&stack_top_variable, stack_copy, i);
4891 #endif /* MAX_SAVE_STACK > 0 */
4893 if (garbage_collection_messages)
4894 message1_nolog ("Garbage collecting...");
4896 BLOCK_INPUT;
4898 shrink_regexp_cache ();
4900 gc_in_progress = 1;
4902 /* clear_marks (); */
4904 /* Mark all the special slots that serve as the roots of accessibility. */
4906 for (i = 0; i < staticidx; i++)
4907 mark_object (*staticvec[i]);
4909 for (bind = specpdl; bind != specpdl_ptr; bind++)
4911 mark_object (bind->symbol);
4912 mark_object (bind->old_value);
4914 mark_kboards ();
4916 #ifdef USE_GTK
4918 extern void xg_mark_data ();
4919 xg_mark_data ();
4921 #endif
4923 #if (GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS \
4924 || GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS)
4925 mark_stack ();
4926 #else
4928 register struct gcpro *tail;
4929 for (tail = gcprolist; tail; tail = tail->next)
4930 for (i = 0; i < tail->nvars; i++)
4931 mark_object (tail->var[i]);
4933 #endif
4935 mark_byte_stack ();
4936 for (catch = catchlist; catch; catch = catch->next)
4938 mark_object (catch->tag);
4939 mark_object (catch->val);
4941 for (handler = handlerlist; handler; handler = handler->next)
4943 mark_object (handler->handler);
4944 mark_object (handler->var);
4946 mark_backtrace ();
4948 #ifdef HAVE_WINDOW_SYSTEM
4949 mark_fringe_data ();
4950 #endif
4952 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4953 mark_stack ();
4954 #endif
4956 /* Everything is now marked, except for the things that require special
4957 finalization, i.e. the undo_list.
4958 Look thru every buffer's undo list
4959 for elements that update markers that were not marked,
4960 and delete them. */
4962 register struct buffer *nextb = all_buffers;
4964 while (nextb)
4966 /* If a buffer's undo list is Qt, that means that undo is
4967 turned off in that buffer. Calling truncate_undo_list on
4968 Qt tends to return NULL, which effectively turns undo back on.
4969 So don't call truncate_undo_list if undo_list is Qt. */
4970 if (! EQ (nextb->undo_list, Qt))
4972 Lisp_Object tail, prev;
4973 tail = nextb->undo_list;
4974 prev = Qnil;
4975 while (CONSP (tail))
4977 if (GC_CONSP (XCAR (tail))
4978 && GC_MARKERP (XCAR (XCAR (tail)))
4979 && !XMARKER (XCAR (XCAR (tail)))->gcmarkbit)
4981 if (NILP (prev))
4982 nextb->undo_list = tail = XCDR (tail);
4983 else
4985 tail = XCDR (tail);
4986 XSETCDR (prev, tail);
4989 else
4991 prev = tail;
4992 tail = XCDR (tail);
4996 /* Now that we have stripped the elements that need not be in the
4997 undo_list any more, we can finally mark the list. */
4998 mark_object (nextb->undo_list);
5000 nextb = nextb->next;
5004 gc_sweep ();
5006 /* Clear the mark bits that we set in certain root slots. */
5008 unmark_byte_stack ();
5009 VECTOR_UNMARK (&buffer_defaults);
5010 VECTOR_UNMARK (&buffer_local_symbols);
5012 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES && 0
5013 dump_zombies ();
5014 #endif
5016 UNBLOCK_INPUT;
5018 CHECK_CONS_LIST ();
5020 /* clear_marks (); */
5021 gc_in_progress = 0;
5023 consing_since_gc = 0;
5024 if (gc_cons_threshold < 10000)
5025 gc_cons_threshold = 10000;
5027 if (FLOATP (Vgc_cons_percentage))
5028 { /* Set gc_cons_combined_threshold. */
5029 EMACS_INT total = 0;
5031 total += total_conses * sizeof (struct Lisp_Cons);
5032 total += total_symbols * sizeof (struct Lisp_Symbol);
5033 total += total_markers * sizeof (union Lisp_Misc);
5034 total += total_string_size;
5035 total += total_vector_size * sizeof (Lisp_Object);
5036 total += total_floats * sizeof (struct Lisp_Float);
5037 total += total_intervals * sizeof (struct interval);
5038 total += total_strings * sizeof (struct Lisp_String);
5040 gc_relative_threshold = total * XFLOAT_DATA (Vgc_cons_percentage);
5042 else
5043 gc_relative_threshold = 0;
5045 if (garbage_collection_messages)
5047 if (message_p || minibuf_level > 0)
5048 restore_message ();
5049 else
5050 message1_nolog ("Garbage collecting...done");
5053 unbind_to (count, Qnil);
5055 total[0] = Fcons (make_number (total_conses),
5056 make_number (total_free_conses));
5057 total[1] = Fcons (make_number (total_symbols),
5058 make_number (total_free_symbols));
5059 total[2] = Fcons (make_number (total_markers),
5060 make_number (total_free_markers));
5061 total[3] = make_number (total_string_size);
5062 total[4] = make_number (total_vector_size);
5063 total[5] = Fcons (make_number (total_floats),
5064 make_number (total_free_floats));
5065 total[6] = Fcons (make_number (total_intervals),
5066 make_number (total_free_intervals));
5067 total[7] = Fcons (make_number (total_strings),
5068 make_number (total_free_strings));
5070 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
5072 /* Compute average percentage of zombies. */
5073 double nlive = 0;
5075 for (i = 0; i < 7; ++i)
5076 if (CONSP (total[i]))
5077 nlive += XFASTINT (XCAR (total[i]));
5079 avg_live = (avg_live * ngcs + nlive) / (ngcs + 1);
5080 max_live = max (nlive, max_live);
5081 avg_zombies = (avg_zombies * ngcs + nzombies) / (ngcs + 1);
5082 max_zombies = max (nzombies, max_zombies);
5083 ++ngcs;
5085 #endif
5087 if (!NILP (Vpost_gc_hook))
5089 int count = inhibit_garbage_collection ();
5090 safe_run_hooks (Qpost_gc_hook);
5091 unbind_to (count, Qnil);
5094 /* Accumulate statistics. */
5095 EMACS_GET_TIME (t2);
5096 EMACS_SUB_TIME (t3, t2, t1);
5097 if (FLOATP (Vgc_elapsed))
5098 Vgc_elapsed = make_float (XFLOAT_DATA (Vgc_elapsed) +
5099 EMACS_SECS (t3) +
5100 EMACS_USECS (t3) * 1.0e-6);
5101 gcs_done++;
5103 return Flist (sizeof total / sizeof *total, total);
5107 /* Mark Lisp objects in glyph matrix MATRIX. Currently the
5108 only interesting objects referenced from glyphs are strings. */
5110 static void
5111 mark_glyph_matrix (matrix)
5112 struct glyph_matrix *matrix;
5114 struct glyph_row *row = matrix->rows;
5115 struct glyph_row *end = row + matrix->nrows;
5117 for (; row < end; ++row)
5118 if (row->enabled_p)
5120 int area;
5121 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
5123 struct glyph *glyph = row->glyphs[area];
5124 struct glyph *end_glyph = glyph + row->used[area];
5126 for (; glyph < end_glyph; ++glyph)
5127 if (GC_STRINGP (glyph->object)
5128 && !STRING_MARKED_P (XSTRING (glyph->object)))
5129 mark_object (glyph->object);
5135 /* Mark Lisp faces in the face cache C. */
5137 static void
5138 mark_face_cache (c)
5139 struct face_cache *c;
5141 if (c)
5143 int i, j;
5144 for (i = 0; i < c->used; ++i)
5146 struct face *face = FACE_FROM_ID (c->f, i);
5148 if (face)
5150 for (j = 0; j < LFACE_VECTOR_SIZE; ++j)
5151 mark_object (face->lface[j]);
5158 #ifdef HAVE_WINDOW_SYSTEM
5160 /* Mark Lisp objects in image IMG. */
5162 static void
5163 mark_image (img)
5164 struct image *img;
5166 mark_object (img->spec);
5168 if (!NILP (img->data.lisp_val))
5169 mark_object (img->data.lisp_val);
5173 /* Mark Lisp objects in image cache of frame F. It's done this way so
5174 that we don't have to include xterm.h here. */
5176 static void
5177 mark_image_cache (f)
5178 struct frame *f;
5180 forall_images_in_image_cache (f, mark_image);
5183 #endif /* HAVE_X_WINDOWS */
5187 /* Mark reference to a Lisp_Object.
5188 If the object referred to has not been seen yet, recursively mark
5189 all the references contained in it. */
5191 #define LAST_MARKED_SIZE 500
5192 Lisp_Object last_marked[LAST_MARKED_SIZE];
5193 int last_marked_index;
5195 /* For debugging--call abort when we cdr down this many
5196 links of a list, in mark_object. In debugging,
5197 the call to abort will hit a breakpoint.
5198 Normally this is zero and the check never goes off. */
5199 int mark_object_loop_halt;
5201 void
5202 mark_object (arg)
5203 Lisp_Object arg;
5205 register Lisp_Object obj = arg;
5206 #ifdef GC_CHECK_MARKED_OBJECTS
5207 void *po;
5208 struct mem_node *m;
5209 #endif
5210 int cdr_count = 0;
5212 loop:
5214 if (PURE_POINTER_P (XPNTR (obj)))
5215 return;
5217 last_marked[last_marked_index++] = obj;
5218 if (last_marked_index == LAST_MARKED_SIZE)
5219 last_marked_index = 0;
5221 /* Perform some sanity checks on the objects marked here. Abort if
5222 we encounter an object we know is bogus. This increases GC time
5223 by ~80%, and requires compilation with GC_MARK_STACK != 0. */
5224 #ifdef GC_CHECK_MARKED_OBJECTS
5226 po = (void *) XPNTR (obj);
5228 /* Check that the object pointed to by PO is known to be a Lisp
5229 structure allocated from the heap. */
5230 #define CHECK_ALLOCATED() \
5231 do { \
5232 m = mem_find (po); \
5233 if (m == MEM_NIL) \
5234 abort (); \
5235 } while (0)
5237 /* Check that the object pointed to by PO is live, using predicate
5238 function LIVEP. */
5239 #define CHECK_LIVE(LIVEP) \
5240 do { \
5241 if (!LIVEP (m, po)) \
5242 abort (); \
5243 } while (0)
5245 /* Check both of the above conditions. */
5246 #define CHECK_ALLOCATED_AND_LIVE(LIVEP) \
5247 do { \
5248 CHECK_ALLOCATED (); \
5249 CHECK_LIVE (LIVEP); \
5250 } while (0) \
5252 #else /* not GC_CHECK_MARKED_OBJECTS */
5254 #define CHECK_ALLOCATED() (void) 0
5255 #define CHECK_LIVE(LIVEP) (void) 0
5256 #define CHECK_ALLOCATED_AND_LIVE(LIVEP) (void) 0
5258 #endif /* not GC_CHECK_MARKED_OBJECTS */
5260 switch (SWITCH_ENUM_CAST (XGCTYPE (obj)))
5262 case Lisp_String:
5264 register struct Lisp_String *ptr = XSTRING (obj);
5265 CHECK_ALLOCATED_AND_LIVE (live_string_p);
5266 MARK_INTERVAL_TREE (ptr->intervals);
5267 MARK_STRING (ptr);
5268 #ifdef GC_CHECK_STRING_BYTES
5269 /* Check that the string size recorded in the string is the
5270 same as the one recorded in the sdata structure. */
5271 CHECK_STRING_BYTES (ptr);
5272 #endif /* GC_CHECK_STRING_BYTES */
5274 break;
5276 case Lisp_Vectorlike:
5277 #ifdef GC_CHECK_MARKED_OBJECTS
5278 m = mem_find (po);
5279 if (m == MEM_NIL && !GC_SUBRP (obj)
5280 && po != &buffer_defaults
5281 && po != &buffer_local_symbols)
5282 abort ();
5283 #endif /* GC_CHECK_MARKED_OBJECTS */
5285 if (GC_BUFFERP (obj))
5287 if (!VECTOR_MARKED_P (XBUFFER (obj)))
5289 #ifdef GC_CHECK_MARKED_OBJECTS
5290 if (po != &buffer_defaults && po != &buffer_local_symbols)
5292 struct buffer *b;
5293 for (b = all_buffers; b && b != po; b = b->next)
5295 if (b == NULL)
5296 abort ();
5298 #endif /* GC_CHECK_MARKED_OBJECTS */
5299 mark_buffer (obj);
5302 else if (GC_SUBRP (obj))
5303 break;
5304 else if (GC_COMPILEDP (obj))
5305 /* We could treat this just like a vector, but it is better to
5306 save the COMPILED_CONSTANTS element for last and avoid
5307 recursion there. */
5309 register struct Lisp_Vector *ptr = XVECTOR (obj);
5310 register EMACS_INT size = ptr->size;
5311 register int i;
5313 if (VECTOR_MARKED_P (ptr))
5314 break; /* Already marked */
5316 CHECK_LIVE (live_vector_p);
5317 VECTOR_MARK (ptr); /* Else mark it */
5318 size &= PSEUDOVECTOR_SIZE_MASK;
5319 for (i = 0; i < size; i++) /* and then mark its elements */
5321 if (i != COMPILED_CONSTANTS)
5322 mark_object (ptr->contents[i]);
5324 obj = ptr->contents[COMPILED_CONSTANTS];
5325 goto loop;
5327 else if (GC_FRAMEP (obj))
5329 register struct frame *ptr = XFRAME (obj);
5331 if (VECTOR_MARKED_P (ptr)) break; /* Already marked */
5332 VECTOR_MARK (ptr); /* Else mark it */
5334 CHECK_LIVE (live_vector_p);
5335 mark_object (ptr->name);
5336 mark_object (ptr->icon_name);
5337 mark_object (ptr->title);
5338 mark_object (ptr->focus_frame);
5339 mark_object (ptr->selected_window);
5340 mark_object (ptr->minibuffer_window);
5341 mark_object (ptr->param_alist);
5342 mark_object (ptr->scroll_bars);
5343 mark_object (ptr->condemned_scroll_bars);
5344 mark_object (ptr->menu_bar_items);
5345 mark_object (ptr->face_alist);
5346 mark_object (ptr->menu_bar_vector);
5347 mark_object (ptr->buffer_predicate);
5348 mark_object (ptr->buffer_list);
5349 mark_object (ptr->menu_bar_window);
5350 mark_object (ptr->tool_bar_window);
5351 mark_face_cache (ptr->face_cache);
5352 #ifdef HAVE_WINDOW_SYSTEM
5353 mark_image_cache (ptr);
5354 mark_object (ptr->tool_bar_items);
5355 mark_object (ptr->desired_tool_bar_string);
5356 mark_object (ptr->current_tool_bar_string);
5357 #endif /* HAVE_WINDOW_SYSTEM */
5359 else if (GC_BOOL_VECTOR_P (obj))
5361 register struct Lisp_Vector *ptr = XVECTOR (obj);
5363 if (VECTOR_MARKED_P (ptr))
5364 break; /* Already marked */
5365 CHECK_LIVE (live_vector_p);
5366 VECTOR_MARK (ptr); /* Else mark it */
5368 else if (GC_WINDOWP (obj))
5370 register struct Lisp_Vector *ptr = XVECTOR (obj);
5371 struct window *w = XWINDOW (obj);
5372 register int i;
5374 /* Stop if already marked. */
5375 if (VECTOR_MARKED_P (ptr))
5376 break;
5378 /* Mark it. */
5379 CHECK_LIVE (live_vector_p);
5380 VECTOR_MARK (ptr);
5382 /* There is no Lisp data above The member CURRENT_MATRIX in
5383 struct WINDOW. Stop marking when that slot is reached. */
5384 for (i = 0;
5385 (char *) &ptr->contents[i] < (char *) &w->current_matrix;
5386 i++)
5387 mark_object (ptr->contents[i]);
5389 /* Mark glyphs for leaf windows. Marking window matrices is
5390 sufficient because frame matrices use the same glyph
5391 memory. */
5392 if (NILP (w->hchild)
5393 && NILP (w->vchild)
5394 && w->current_matrix)
5396 mark_glyph_matrix (w->current_matrix);
5397 mark_glyph_matrix (w->desired_matrix);
5400 else if (GC_HASH_TABLE_P (obj))
5402 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
5404 /* Stop if already marked. */
5405 if (VECTOR_MARKED_P (h))
5406 break;
5408 /* Mark it. */
5409 CHECK_LIVE (live_vector_p);
5410 VECTOR_MARK (h);
5412 /* Mark contents. */
5413 /* Do not mark next_free or next_weak.
5414 Being in the next_weak chain
5415 should not keep the hash table alive.
5416 No need to mark `count' since it is an integer. */
5417 mark_object (h->test);
5418 mark_object (h->weak);
5419 mark_object (h->rehash_size);
5420 mark_object (h->rehash_threshold);
5421 mark_object (h->hash);
5422 mark_object (h->next);
5423 mark_object (h->index);
5424 mark_object (h->user_hash_function);
5425 mark_object (h->user_cmp_function);
5427 /* If hash table is not weak, mark all keys and values.
5428 For weak tables, mark only the vector. */
5429 if (GC_NILP (h->weak))
5430 mark_object (h->key_and_value);
5431 else
5432 VECTOR_MARK (XVECTOR (h->key_and_value));
5434 else
5436 register struct Lisp_Vector *ptr = XVECTOR (obj);
5437 register EMACS_INT size = ptr->size;
5438 register int i;
5440 if (VECTOR_MARKED_P (ptr)) break; /* Already marked */
5441 CHECK_LIVE (live_vector_p);
5442 VECTOR_MARK (ptr); /* Else mark it */
5443 if (size & PSEUDOVECTOR_FLAG)
5444 size &= PSEUDOVECTOR_SIZE_MASK;
5446 for (i = 0; i < size; i++) /* and then mark its elements */
5447 mark_object (ptr->contents[i]);
5449 break;
5451 case Lisp_Symbol:
5453 register struct Lisp_Symbol *ptr = XSYMBOL (obj);
5454 struct Lisp_Symbol *ptrx;
5456 if (ptr->gcmarkbit) break;
5457 CHECK_ALLOCATED_AND_LIVE (live_symbol_p);
5458 ptr->gcmarkbit = 1;
5459 mark_object (ptr->value);
5460 mark_object (ptr->function);
5461 mark_object (ptr->plist);
5463 if (!PURE_POINTER_P (XSTRING (ptr->xname)))
5464 MARK_STRING (XSTRING (ptr->xname));
5465 MARK_INTERVAL_TREE (STRING_INTERVALS (ptr->xname));
5467 /* Note that we do not mark the obarray of the symbol.
5468 It is safe not to do so because nothing accesses that
5469 slot except to check whether it is nil. */
5470 ptr = ptr->next;
5471 if (ptr)
5473 ptrx = ptr; /* Use of ptrx avoids compiler bug on Sun */
5474 XSETSYMBOL (obj, ptrx);
5475 goto loop;
5478 break;
5480 case Lisp_Misc:
5481 CHECK_ALLOCATED_AND_LIVE (live_misc_p);
5482 if (XMARKER (obj)->gcmarkbit)
5483 break;
5484 XMARKER (obj)->gcmarkbit = 1;
5486 switch (XMISCTYPE (obj))
5488 case Lisp_Misc_Buffer_Local_Value:
5489 case Lisp_Misc_Some_Buffer_Local_Value:
5491 register struct Lisp_Buffer_Local_Value *ptr
5492 = XBUFFER_LOCAL_VALUE (obj);
5493 /* If the cdr is nil, avoid recursion for the car. */
5494 if (EQ (ptr->cdr, Qnil))
5496 obj = ptr->realvalue;
5497 goto loop;
5499 mark_object (ptr->realvalue);
5500 mark_object (ptr->buffer);
5501 mark_object (ptr->frame);
5502 obj = ptr->cdr;
5503 goto loop;
5506 case Lisp_Misc_Marker:
5507 /* DO NOT mark thru the marker's chain.
5508 The buffer's markers chain does not preserve markers from gc;
5509 instead, markers are removed from the chain when freed by gc. */
5510 break;
5512 case Lisp_Misc_Intfwd:
5513 case Lisp_Misc_Boolfwd:
5514 case Lisp_Misc_Objfwd:
5515 case Lisp_Misc_Buffer_Objfwd:
5516 case Lisp_Misc_Kboard_Objfwd:
5517 /* Don't bother with Lisp_Buffer_Objfwd,
5518 since all markable slots in current buffer marked anyway. */
5519 /* Don't need to do Lisp_Objfwd, since the places they point
5520 are protected with staticpro. */
5521 break;
5523 case Lisp_Misc_Save_Value:
5524 #if GC_MARK_STACK
5526 register struct Lisp_Save_Value *ptr = XSAVE_VALUE (obj);
5527 /* If DOGC is set, POINTER is the address of a memory
5528 area containing INTEGER potential Lisp_Objects. */
5529 if (ptr->dogc)
5531 Lisp_Object *p = (Lisp_Object *) ptr->pointer;
5532 int nelt;
5533 for (nelt = ptr->integer; nelt > 0; nelt--, p++)
5534 mark_maybe_object (*p);
5537 #endif
5538 break;
5540 case Lisp_Misc_Overlay:
5542 struct Lisp_Overlay *ptr = XOVERLAY (obj);
5543 mark_object (ptr->start);
5544 mark_object (ptr->end);
5545 mark_object (ptr->plist);
5546 if (ptr->next)
5548 XSETMISC (obj, ptr->next);
5549 goto loop;
5552 break;
5554 default:
5555 abort ();
5557 break;
5559 case Lisp_Cons:
5561 register struct Lisp_Cons *ptr = XCONS (obj);
5562 if (CONS_MARKED_P (ptr)) break;
5563 CHECK_ALLOCATED_AND_LIVE (live_cons_p);
5564 CONS_MARK (ptr);
5565 /* If the cdr is nil, avoid recursion for the car. */
5566 if (EQ (ptr->u.cdr, Qnil))
5568 obj = ptr->car;
5569 cdr_count = 0;
5570 goto loop;
5572 mark_object (ptr->car);
5573 obj = ptr->u.cdr;
5574 cdr_count++;
5575 if (cdr_count == mark_object_loop_halt)
5576 abort ();
5577 goto loop;
5580 case Lisp_Float:
5581 CHECK_ALLOCATED_AND_LIVE (live_float_p);
5582 FLOAT_MARK (XFLOAT (obj));
5583 break;
5585 case Lisp_Int:
5586 break;
5588 default:
5589 abort ();
5592 #undef CHECK_LIVE
5593 #undef CHECK_ALLOCATED
5594 #undef CHECK_ALLOCATED_AND_LIVE
5597 /* Mark the pointers in a buffer structure. */
5599 static void
5600 mark_buffer (buf)
5601 Lisp_Object buf;
5603 register struct buffer *buffer = XBUFFER (buf);
5604 register Lisp_Object *ptr, tmp;
5605 Lisp_Object base_buffer;
5607 VECTOR_MARK (buffer);
5609 MARK_INTERVAL_TREE (BUF_INTERVALS (buffer));
5611 /* For now, we just don't mark the undo_list. It's done later in
5612 a special way just before the sweep phase, and after stripping
5613 some of its elements that are not needed any more. */
5615 if (buffer->overlays_before)
5617 XSETMISC (tmp, buffer->overlays_before);
5618 mark_object (tmp);
5620 if (buffer->overlays_after)
5622 XSETMISC (tmp, buffer->overlays_after);
5623 mark_object (tmp);
5626 for (ptr = &buffer->name;
5627 (char *)ptr < (char *)buffer + sizeof (struct buffer);
5628 ptr++)
5629 mark_object (*ptr);
5631 /* If this is an indirect buffer, mark its base buffer. */
5632 if (buffer->base_buffer && !VECTOR_MARKED_P (buffer->base_buffer))
5634 XSETBUFFER (base_buffer, buffer->base_buffer);
5635 mark_buffer (base_buffer);
5640 /* Value is non-zero if OBJ will survive the current GC because it's
5641 either marked or does not need to be marked to survive. */
5644 survives_gc_p (obj)
5645 Lisp_Object obj;
5647 int survives_p;
5649 switch (XGCTYPE (obj))
5651 case Lisp_Int:
5652 survives_p = 1;
5653 break;
5655 case Lisp_Symbol:
5656 survives_p = XSYMBOL (obj)->gcmarkbit;
5657 break;
5659 case Lisp_Misc:
5660 survives_p = XMARKER (obj)->gcmarkbit;
5661 break;
5663 case Lisp_String:
5664 survives_p = STRING_MARKED_P (XSTRING (obj));
5665 break;
5667 case Lisp_Vectorlike:
5668 survives_p = GC_SUBRP (obj) || VECTOR_MARKED_P (XVECTOR (obj));
5669 break;
5671 case Lisp_Cons:
5672 survives_p = CONS_MARKED_P (XCONS (obj));
5673 break;
5675 case Lisp_Float:
5676 survives_p = FLOAT_MARKED_P (XFLOAT (obj));
5677 break;
5679 default:
5680 abort ();
5683 return survives_p || PURE_POINTER_P ((void *) XPNTR (obj));
5688 /* Sweep: find all structures not marked, and free them. */
5690 static void
5691 gc_sweep ()
5693 /* Remove or mark entries in weak hash tables.
5694 This must be done before any object is unmarked. */
5695 sweep_weak_hash_tables ();
5697 sweep_strings ();
5698 #ifdef GC_CHECK_STRING_BYTES
5699 if (!noninteractive)
5700 check_string_bytes (1);
5701 #endif
5703 /* Put all unmarked conses on free list */
5705 register struct cons_block *cblk;
5706 struct cons_block **cprev = &cons_block;
5707 register int lim = cons_block_index;
5708 register int num_free = 0, num_used = 0;
5710 cons_free_list = 0;
5712 for (cblk = cons_block; cblk; cblk = *cprev)
5714 register int i;
5715 int this_free = 0;
5716 for (i = 0; i < lim; i++)
5717 if (!CONS_MARKED_P (&cblk->conses[i]))
5719 this_free++;
5720 cblk->conses[i].u.chain = cons_free_list;
5721 cons_free_list = &cblk->conses[i];
5722 #if GC_MARK_STACK
5723 cons_free_list->car = Vdead;
5724 #endif
5726 else
5728 num_used++;
5729 CONS_UNMARK (&cblk->conses[i]);
5731 lim = CONS_BLOCK_SIZE;
5732 /* If this block contains only free conses and we have already
5733 seen more than two blocks worth of free conses then deallocate
5734 this block. */
5735 if (this_free == CONS_BLOCK_SIZE && num_free > CONS_BLOCK_SIZE)
5737 *cprev = cblk->next;
5738 /* Unhook from the free list. */
5739 cons_free_list = cblk->conses[0].u.chain;
5740 lisp_align_free (cblk);
5741 n_cons_blocks--;
5743 else
5745 num_free += this_free;
5746 cprev = &cblk->next;
5749 total_conses = num_used;
5750 total_free_conses = num_free;
5753 /* Put all unmarked floats on free list */
5755 register struct float_block *fblk;
5756 struct float_block **fprev = &float_block;
5757 register int lim = float_block_index;
5758 register int num_free = 0, num_used = 0;
5760 float_free_list = 0;
5762 for (fblk = float_block; fblk; fblk = *fprev)
5764 register int i;
5765 int this_free = 0;
5766 for (i = 0; i < lim; i++)
5767 if (!FLOAT_MARKED_P (&fblk->floats[i]))
5769 this_free++;
5770 fblk->floats[i].u.chain = float_free_list;
5771 float_free_list = &fblk->floats[i];
5773 else
5775 num_used++;
5776 FLOAT_UNMARK (&fblk->floats[i]);
5778 lim = FLOAT_BLOCK_SIZE;
5779 /* If this block contains only free floats and we have already
5780 seen more than two blocks worth of free floats then deallocate
5781 this block. */
5782 if (this_free == FLOAT_BLOCK_SIZE && num_free > FLOAT_BLOCK_SIZE)
5784 *fprev = fblk->next;
5785 /* Unhook from the free list. */
5786 float_free_list = fblk->floats[0].u.chain;
5787 lisp_align_free (fblk);
5788 n_float_blocks--;
5790 else
5792 num_free += this_free;
5793 fprev = &fblk->next;
5796 total_floats = num_used;
5797 total_free_floats = num_free;
5800 /* Put all unmarked intervals on free list */
5802 register struct interval_block *iblk;
5803 struct interval_block **iprev = &interval_block;
5804 register int lim = interval_block_index;
5805 register int num_free = 0, num_used = 0;
5807 interval_free_list = 0;
5809 for (iblk = interval_block; iblk; iblk = *iprev)
5811 register int i;
5812 int this_free = 0;
5814 for (i = 0; i < lim; i++)
5816 if (!iblk->intervals[i].gcmarkbit)
5818 SET_INTERVAL_PARENT (&iblk->intervals[i], interval_free_list);
5819 interval_free_list = &iblk->intervals[i];
5820 this_free++;
5822 else
5824 num_used++;
5825 iblk->intervals[i].gcmarkbit = 0;
5828 lim = INTERVAL_BLOCK_SIZE;
5829 /* If this block contains only free intervals and we have already
5830 seen more than two blocks worth of free intervals then
5831 deallocate this block. */
5832 if (this_free == INTERVAL_BLOCK_SIZE && num_free > INTERVAL_BLOCK_SIZE)
5834 *iprev = iblk->next;
5835 /* Unhook from the free list. */
5836 interval_free_list = INTERVAL_PARENT (&iblk->intervals[0]);
5837 lisp_free (iblk);
5838 n_interval_blocks--;
5840 else
5842 num_free += this_free;
5843 iprev = &iblk->next;
5846 total_intervals = num_used;
5847 total_free_intervals = num_free;
5850 /* Put all unmarked symbols on free list */
5852 register struct symbol_block *sblk;
5853 struct symbol_block **sprev = &symbol_block;
5854 register int lim = symbol_block_index;
5855 register int num_free = 0, num_used = 0;
5857 symbol_free_list = NULL;
5859 for (sblk = symbol_block; sblk; sblk = *sprev)
5861 int this_free = 0;
5862 struct Lisp_Symbol *sym = sblk->symbols;
5863 struct Lisp_Symbol *end = sym + lim;
5865 for (; sym < end; ++sym)
5867 /* Check if the symbol was created during loadup. In such a case
5868 it might be pointed to by pure bytecode which we don't trace,
5869 so we conservatively assume that it is live. */
5870 int pure_p = PURE_POINTER_P (XSTRING (sym->xname));
5872 if (!sym->gcmarkbit && !pure_p)
5874 sym->next = symbol_free_list;
5875 symbol_free_list = sym;
5876 #if GC_MARK_STACK
5877 symbol_free_list->function = Vdead;
5878 #endif
5879 ++this_free;
5881 else
5883 ++num_used;
5884 if (!pure_p)
5885 UNMARK_STRING (XSTRING (sym->xname));
5886 sym->gcmarkbit = 0;
5890 lim = SYMBOL_BLOCK_SIZE;
5891 /* If this block contains only free symbols and we have already
5892 seen more than two blocks worth of free symbols then deallocate
5893 this block. */
5894 if (this_free == SYMBOL_BLOCK_SIZE && num_free > SYMBOL_BLOCK_SIZE)
5896 *sprev = sblk->next;
5897 /* Unhook from the free list. */
5898 symbol_free_list = sblk->symbols[0].next;
5899 lisp_free (sblk);
5900 n_symbol_blocks--;
5902 else
5904 num_free += this_free;
5905 sprev = &sblk->next;
5908 total_symbols = num_used;
5909 total_free_symbols = num_free;
5912 /* Put all unmarked misc's on free list.
5913 For a marker, first unchain it from the buffer it points into. */
5915 register struct marker_block *mblk;
5916 struct marker_block **mprev = &marker_block;
5917 register int lim = marker_block_index;
5918 register int num_free = 0, num_used = 0;
5920 marker_free_list = 0;
5922 for (mblk = marker_block; mblk; mblk = *mprev)
5924 register int i;
5925 int this_free = 0;
5927 for (i = 0; i < lim; i++)
5929 if (!mblk->markers[i].u_marker.gcmarkbit)
5931 if (mblk->markers[i].u_marker.type == Lisp_Misc_Marker)
5932 unchain_marker (&mblk->markers[i].u_marker);
5933 /* Set the type of the freed object to Lisp_Misc_Free.
5934 We could leave the type alone, since nobody checks it,
5935 but this might catch bugs faster. */
5936 mblk->markers[i].u_marker.type = Lisp_Misc_Free;
5937 mblk->markers[i].u_free.chain = marker_free_list;
5938 marker_free_list = &mblk->markers[i];
5939 this_free++;
5941 else
5943 num_used++;
5944 mblk->markers[i].u_marker.gcmarkbit = 0;
5947 lim = MARKER_BLOCK_SIZE;
5948 /* If this block contains only free markers and we have already
5949 seen more than two blocks worth of free markers then deallocate
5950 this block. */
5951 if (this_free == MARKER_BLOCK_SIZE && num_free > MARKER_BLOCK_SIZE)
5953 *mprev = mblk->next;
5954 /* Unhook from the free list. */
5955 marker_free_list = mblk->markers[0].u_free.chain;
5956 lisp_free (mblk);
5957 n_marker_blocks--;
5959 else
5961 num_free += this_free;
5962 mprev = &mblk->next;
5966 total_markers = num_used;
5967 total_free_markers = num_free;
5970 /* Free all unmarked buffers */
5972 register struct buffer *buffer = all_buffers, *prev = 0, *next;
5974 while (buffer)
5975 if (!VECTOR_MARKED_P (buffer))
5977 if (prev)
5978 prev->next = buffer->next;
5979 else
5980 all_buffers = buffer->next;
5981 next = buffer->next;
5982 lisp_free (buffer);
5983 buffer = next;
5985 else
5987 VECTOR_UNMARK (buffer);
5988 UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer));
5989 prev = buffer, buffer = buffer->next;
5993 /* Free all unmarked vectors */
5995 register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next;
5996 total_vector_size = 0;
5998 while (vector)
5999 if (!VECTOR_MARKED_P (vector))
6001 if (prev)
6002 prev->next = vector->next;
6003 else
6004 all_vectors = vector->next;
6005 next = vector->next;
6006 lisp_free (vector);
6007 n_vectors--;
6008 vector = next;
6011 else
6013 VECTOR_UNMARK (vector);
6014 if (vector->size & PSEUDOVECTOR_FLAG)
6015 total_vector_size += (PSEUDOVECTOR_SIZE_MASK & vector->size);
6016 else
6017 total_vector_size += vector->size;
6018 prev = vector, vector = vector->next;
6022 #ifdef GC_CHECK_STRING_BYTES
6023 if (!noninteractive)
6024 check_string_bytes (1);
6025 #endif
6031 /* Debugging aids. */
6033 DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0,
6034 doc: /* Return the address of the last byte Emacs has allocated, divided by 1024.
6035 This may be helpful in debugging Emacs's memory usage.
6036 We divide the value by 1024 to make sure it fits in a Lisp integer. */)
6039 Lisp_Object end;
6041 XSETINT (end, (EMACS_INT) sbrk (0) / 1024);
6043 return end;
6046 DEFUN ("memory-use-counts", Fmemory_use_counts, Smemory_use_counts, 0, 0, 0,
6047 doc: /* Return a list of counters that measure how much consing there has been.
6048 Each of these counters increments for a certain kind of object.
6049 The counters wrap around from the largest positive integer to zero.
6050 Garbage collection does not decrease them.
6051 The elements of the value are as follows:
6052 (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS STRINGS)
6053 All are in units of 1 = one object consed
6054 except for VECTOR-CELLS and STRING-CHARS, which count the total length of
6055 objects consed.
6056 MISCS include overlays, markers, and some internal types.
6057 Frames, windows, buffers, and subprocesses count as vectors
6058 (but the contents of a buffer's text do not count here). */)
6061 Lisp_Object consed[8];
6063 consed[0] = make_number (min (MOST_POSITIVE_FIXNUM, cons_cells_consed));
6064 consed[1] = make_number (min (MOST_POSITIVE_FIXNUM, floats_consed));
6065 consed[2] = make_number (min (MOST_POSITIVE_FIXNUM, vector_cells_consed));
6066 consed[3] = make_number (min (MOST_POSITIVE_FIXNUM, symbols_consed));
6067 consed[4] = make_number (min (MOST_POSITIVE_FIXNUM, string_chars_consed));
6068 consed[5] = make_number (min (MOST_POSITIVE_FIXNUM, misc_objects_consed));
6069 consed[6] = make_number (min (MOST_POSITIVE_FIXNUM, intervals_consed));
6070 consed[7] = make_number (min (MOST_POSITIVE_FIXNUM, strings_consed));
6072 return Flist (8, consed);
6075 int suppress_checking;
6076 void
6077 die (msg, file, line)
6078 const char *msg;
6079 const char *file;
6080 int line;
6082 fprintf (stderr, "\r\nEmacs fatal error: %s:%d: %s\r\n",
6083 file, line, msg);
6084 abort ();
6087 /* Initialization */
6089 void
6090 init_alloc_once ()
6092 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
6093 purebeg = PUREBEG;
6094 pure_size = PURESIZE;
6095 pure_bytes_used = 0;
6096 pure_bytes_used_before_overflow = 0;
6098 /* Initialize the list of free aligned blocks. */
6099 free_ablock = NULL;
6101 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
6102 mem_init ();
6103 Vdead = make_pure_string ("DEAD", 4, 4, 0);
6104 #endif
6106 all_vectors = 0;
6107 ignore_warnings = 1;
6108 #ifdef DOUG_LEA_MALLOC
6109 mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */
6110 mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */
6111 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); /* max. number of mmap'ed areas */
6112 #endif
6113 init_strings ();
6114 init_cons ();
6115 init_symbol ();
6116 init_marker ();
6117 init_float ();
6118 init_intervals ();
6120 #ifdef REL_ALLOC
6121 malloc_hysteresis = 32;
6122 #else
6123 malloc_hysteresis = 0;
6124 #endif
6126 refill_memory_reserve ();
6128 ignore_warnings = 0;
6129 gcprolist = 0;
6130 byte_stack_list = 0;
6131 staticidx = 0;
6132 consing_since_gc = 0;
6133 gc_cons_threshold = 100000 * sizeof (Lisp_Object);
6134 gc_relative_threshold = 0;
6136 #ifdef VIRT_ADDR_VARIES
6137 malloc_sbrk_unused = 1<<22; /* A large number */
6138 malloc_sbrk_used = 100000; /* as reasonable as any number */
6139 #endif /* VIRT_ADDR_VARIES */
6142 void
6143 init_alloc ()
6145 gcprolist = 0;
6146 byte_stack_list = 0;
6147 #if GC_MARK_STACK
6148 #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
6149 setjmp_tested_p = longjmps_done = 0;
6150 #endif
6151 #endif
6152 Vgc_elapsed = make_float (0.0);
6153 gcs_done = 0;
6156 void
6157 syms_of_alloc ()
6159 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold,
6160 doc: /* *Number of bytes of consing between garbage collections.
6161 Garbage collection can happen automatically once this many bytes have been
6162 allocated since the last garbage collection. All data types count.
6164 Garbage collection happens automatically only when `eval' is called.
6166 By binding this temporarily to a large number, you can effectively
6167 prevent garbage collection during a part of the program.
6168 See also `gc-cons-percentage'. */);
6170 DEFVAR_LISP ("gc-cons-percentage", &Vgc_cons_percentage,
6171 doc: /* *Portion of the heap used for allocation.
6172 Garbage collection can happen automatically once this portion of the heap
6173 has been allocated since the last garbage collection.
6174 If this portion is smaller than `gc-cons-threshold', this is ignored. */);
6175 Vgc_cons_percentage = make_float (0.1);
6177 DEFVAR_INT ("pure-bytes-used", &pure_bytes_used,
6178 doc: /* Number of bytes of sharable Lisp data allocated so far. */);
6180 DEFVAR_INT ("cons-cells-consed", &cons_cells_consed,
6181 doc: /* Number of cons cells that have been consed so far. */);
6183 DEFVAR_INT ("floats-consed", &floats_consed,
6184 doc: /* Number of floats that have been consed so far. */);
6186 DEFVAR_INT ("vector-cells-consed", &vector_cells_consed,
6187 doc: /* Number of vector cells that have been consed so far. */);
6189 DEFVAR_INT ("symbols-consed", &symbols_consed,
6190 doc: /* Number of symbols that have been consed so far. */);
6192 DEFVAR_INT ("string-chars-consed", &string_chars_consed,
6193 doc: /* Number of string characters that have been consed so far. */);
6195 DEFVAR_INT ("misc-objects-consed", &misc_objects_consed,
6196 doc: /* Number of miscellaneous objects that have been consed so far. */);
6198 DEFVAR_INT ("intervals-consed", &intervals_consed,
6199 doc: /* Number of intervals that have been consed so far. */);
6201 DEFVAR_INT ("strings-consed", &strings_consed,
6202 doc: /* Number of strings that have been consed so far. */);
6204 DEFVAR_LISP ("purify-flag", &Vpurify_flag,
6205 doc: /* Non-nil means loading Lisp code in order to dump an executable.
6206 This means that certain objects should be allocated in shared (pure) space. */);
6208 DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages,
6209 doc: /* Non-nil means display messages at start and end of garbage collection. */);
6210 garbage_collection_messages = 0;
6212 DEFVAR_LISP ("post-gc-hook", &Vpost_gc_hook,
6213 doc: /* Hook run after garbage collection has finished. */);
6214 Vpost_gc_hook = Qnil;
6215 Qpost_gc_hook = intern ("post-gc-hook");
6216 staticpro (&Qpost_gc_hook);
6218 DEFVAR_LISP ("memory-signal-data", &Vmemory_signal_data,
6219 doc: /* Precomputed `signal' argument for memory-full error. */);
6220 /* We build this in advance because if we wait until we need it, we might
6221 not be able to allocate the memory to hold it. */
6222 Vmemory_signal_data
6223 = list2 (Qerror,
6224 build_string ("Memory exhausted--use M-x save-some-buffers then exit and restart Emacs"));
6226 DEFVAR_LISP ("memory-full", &Vmemory_full,
6227 doc: /* Non-nil means Emacs cannot get much more Lisp memory. */);
6228 Vmemory_full = Qnil;
6230 staticpro (&Qgc_cons_threshold);
6231 Qgc_cons_threshold = intern ("gc-cons-threshold");
6233 staticpro (&Qchar_table_extra_slots);
6234 Qchar_table_extra_slots = intern ("char-table-extra-slots");
6236 DEFVAR_LISP ("gc-elapsed", &Vgc_elapsed,
6237 doc: /* Accumulated time elapsed in garbage collections.
6238 The time is in seconds as a floating point value. */);
6239 DEFVAR_INT ("gcs-done", &gcs_done,
6240 doc: /* Accumulated number of garbage collections done. */);
6242 defsubr (&Scons);
6243 defsubr (&Slist);
6244 defsubr (&Svector);
6245 defsubr (&Smake_byte_code);
6246 defsubr (&Smake_list);
6247 defsubr (&Smake_vector);
6248 defsubr (&Smake_char_table);
6249 defsubr (&Smake_string);
6250 defsubr (&Smake_bool_vector);
6251 defsubr (&Smake_symbol);
6252 defsubr (&Smake_marker);
6253 defsubr (&Spurecopy);
6254 defsubr (&Sgarbage_collect);
6255 defsubr (&Smemory_limit);
6256 defsubr (&Smemory_use_counts);
6258 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
6259 defsubr (&Sgc_status);
6260 #endif
6263 /* arch-tag: 6695ca10-e3c5-4c2c-8bc3-ed26a7dda857
6264 (do not change this comment) */