new version
[emacs.git] / src / alloc.c
blobb488a28309cab10e4aac5ada89977c7ec1448d16
1 /* Storage allocation and gc for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 86, 88, 93, 94, 95, 97 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Note that this declares bzero on OSF/1. How dumb. */
22 #include <signal.h>
24 #include <config.h>
25 #include "lisp.h"
26 #include "intervals.h"
27 #include "puresize.h"
28 #ifndef standalone
29 #include "buffer.h"
30 #include "window.h"
31 #include "frame.h"
32 #include "blockinput.h"
33 #include "keyboard.h"
34 #endif
36 #include "syssignal.h"
38 extern char *sbrk ();
40 #ifdef DOUG_LEA_MALLOC
41 #include <malloc.h>
42 #define __malloc_size_t int
43 #else
44 /* The following come from gmalloc.c. */
46 #if defined (__STDC__) && __STDC__
47 #include <stddef.h>
48 #define __malloc_size_t size_t
49 #else
50 #define __malloc_size_t unsigned int
51 #endif
52 extern __malloc_size_t _bytes_used;
53 extern int __malloc_extra_blocks;
54 #endif /* !defined(DOUG_LEA_MALLOC) */
56 extern Lisp_Object Vhistory_length;
58 #define max(A,B) ((A) > (B) ? (A) : (B))
59 #define min(A,B) ((A) < (B) ? (A) : (B))
61 /* Macro to verify that storage intended for Lisp objects is not
62 out of range to fit in the space for a pointer.
63 ADDRESS is the start of the block, and SIZE
64 is the amount of space within which objects can start. */
65 #define VALIDATE_LISP_STORAGE(address, size) \
66 do \
67 { \
68 Lisp_Object val; \
69 XSETCONS (val, (char *) address + size); \
70 if ((char *) XCONS (val) != (char *) address + size) \
71 { \
72 xfree (address); \
73 memory_full (); \
74 } \
75 } while (0)
77 /* Value of _bytes_used, when spare_memory was freed. */
78 static __malloc_size_t bytes_used_when_full;
80 /* Number of bytes of consing done since the last gc */
81 int consing_since_gc;
83 /* Count the amount of consing of various sorts of space. */
84 int cons_cells_consed;
85 int floats_consed;
86 int vector_cells_consed;
87 int symbols_consed;
88 int string_chars_consed;
89 int misc_objects_consed;
90 int intervals_consed;
92 /* Number of bytes of consing since gc before another gc should be done. */
93 int gc_cons_threshold;
95 /* Nonzero during gc */
96 int gc_in_progress;
98 /* Nonzero means display messages at beginning and end of GC. */
99 int garbage_collection_messages;
101 #ifndef VIRT_ADDR_VARIES
102 extern
103 #endif /* VIRT_ADDR_VARIES */
104 int malloc_sbrk_used;
106 #ifndef VIRT_ADDR_VARIES
107 extern
108 #endif /* VIRT_ADDR_VARIES */
109 int malloc_sbrk_unused;
111 /* Two limits controlling how much undo information to keep. */
112 int undo_limit;
113 int undo_strong_limit;
115 int total_conses, total_markers, total_symbols, total_string_size, total_vector_size;
116 int total_free_conses, total_free_markers, total_free_symbols;
117 #ifdef LISP_FLOAT_TYPE
118 int total_free_floats, total_floats;
119 #endif /* LISP_FLOAT_TYPE */
121 /* Points to memory space allocated as "spare",
122 to be freed if we run out of memory. */
123 static char *spare_memory;
125 /* Amount of spare memory to keep in reserve. */
126 #define SPARE_MEMORY (1 << 14)
128 /* Number of extra blocks malloc should get when it needs more core. */
129 static int malloc_hysteresis;
131 /* Nonzero when malloc is called for allocating Lisp object space. */
132 int allocating_for_lisp;
134 /* Non-nil means defun should do purecopy on the function definition */
135 Lisp_Object Vpurify_flag;
137 #ifndef HAVE_SHM
138 EMACS_INT pure[PURESIZE / sizeof (EMACS_INT)] = {0,}; /* Force it into data space! */
139 #define PUREBEG (char *) pure
140 #else
141 #define pure PURE_SEG_BITS /* Use shared memory segment */
142 #define PUREBEG (char *)PURE_SEG_BITS
144 /* This variable is used only by the XPNTR macro when HAVE_SHM is
145 defined. If we used the PURESIZE macro directly there, that would
146 make most of emacs dependent on puresize.h, which we don't want -
147 you should be able to change that without too much recompilation.
148 So map_in_data initializes pure_size, and the dependencies work
149 out. */
150 EMACS_INT pure_size;
151 #endif /* not HAVE_SHM */
153 /* Index in pure at which next pure object will be allocated. */
154 int pureptr;
156 /* If nonzero, this is a warning delivered by malloc and not yet displayed. */
157 char *pending_malloc_warning;
159 /* Pre-computed signal argument for use when memory is exhausted. */
160 Lisp_Object memory_signal_data;
162 /* Maximum amount of C stack to save when a GC happens. */
164 #ifndef MAX_SAVE_STACK
165 #define MAX_SAVE_STACK 16000
166 #endif
168 /* Define DONT_COPY_FLAG to be some bit which will always be zero in a
169 pointer to a Lisp_Object, when that pointer is viewed as an integer.
170 (On most machines, pointers are even, so we can use the low bit.
171 Word-addressable architectures may need to override this in the m-file.)
172 When linking references to small strings through the size field, we
173 use this slot to hold the bit that would otherwise be interpreted as
174 the GC mark bit. */
175 #ifndef DONT_COPY_FLAG
176 #define DONT_COPY_FLAG 1
177 #endif /* no DONT_COPY_FLAG */
179 /* Buffer in which we save a copy of the C stack at each GC. */
181 char *stack_copy;
182 int stack_copy_size;
184 /* Non-zero means ignore malloc warnings. Set during initialization. */
185 int ignore_warnings;
187 Lisp_Object Qgc_cons_threshold, Qchar_table_extra_slots;
189 static void mark_object (), mark_buffer (), mark_kboards ();
190 static void clear_marks (), gc_sweep ();
191 static void compact_strings ();
193 /* Versions of malloc and realloc that print warnings as memory gets full. */
195 Lisp_Object
196 malloc_warning_1 (str)
197 Lisp_Object str;
199 Fprinc (str, Vstandard_output);
200 write_string ("\nKilling some buffers may delay running out of memory.\n", -1);
201 write_string ("However, certainly by the time you receive the 95% warning,\n", -1);
202 write_string ("you should clean up, kill this Emacs, and start a new one.", -1);
203 return Qnil;
206 /* malloc calls this if it finds we are near exhausting storage */
207 malloc_warning (str)
208 char *str;
210 pending_malloc_warning = str;
213 display_malloc_warning ()
215 register Lisp_Object val;
217 val = build_string (pending_malloc_warning);
218 pending_malloc_warning = 0;
219 internal_with_output_to_temp_buffer (" *Danger*", malloc_warning_1, val);
222 #ifdef DOUG_LEA_MALLOC
223 # define BYTES_USED (mallinfo ().arena)
224 #else
225 # define BYTES_USED _bytes_used
226 #endif
228 /* Called if malloc returns zero */
230 memory_full ()
232 #ifndef SYSTEM_MALLOC
233 bytes_used_when_full = BYTES_USED;
234 #endif
236 /* The first time we get here, free the spare memory. */
237 if (spare_memory)
239 free (spare_memory);
240 spare_memory = 0;
243 /* This used to call error, but if we've run out of memory, we could get
244 infinite recursion trying to build the string. */
245 while (1)
246 Fsignal (Qnil, memory_signal_data);
249 /* Called if we can't allocate relocatable space for a buffer. */
251 void
252 buffer_memory_full ()
254 /* If buffers use the relocating allocator,
255 no need to free spare_memory, because we may have plenty of malloc
256 space left that we could get, and if we don't, the malloc that fails
257 will itself cause spare_memory to be freed.
258 If buffers don't use the relocating allocator,
259 treat this like any other failing malloc. */
261 #ifndef REL_ALLOC
262 memory_full ();
263 #endif
265 /* This used to call error, but if we've run out of memory, we could get
266 infinite recursion trying to build the string. */
267 while (1)
268 Fsignal (Qerror, memory_signal_data);
271 /* like malloc routines but check for no memory and block interrupt input. */
273 long *
274 xmalloc (size)
275 int size;
277 register long *val;
279 BLOCK_INPUT;
280 val = (long *) malloc (size);
281 UNBLOCK_INPUT;
283 if (!val && size) memory_full ();
284 return val;
287 long *
288 xrealloc (block, size)
289 long *block;
290 int size;
292 register long *val;
294 BLOCK_INPUT;
295 /* We must call malloc explicitly when BLOCK is 0, since some
296 reallocs don't do this. */
297 if (! block)
298 val = (long *) malloc (size);
299 else
300 val = (long *) realloc (block, size);
301 UNBLOCK_INPUT;
303 if (!val && size) memory_full ();
304 return val;
307 void
308 xfree (block)
309 long *block;
311 BLOCK_INPUT;
312 free (block);
313 UNBLOCK_INPUT;
317 /* Arranging to disable input signals while we're in malloc.
319 This only works with GNU malloc. To help out systems which can't
320 use GNU malloc, all the calls to malloc, realloc, and free
321 elsewhere in the code should be inside a BLOCK_INPUT/UNBLOCK_INPUT
322 pairs; unfortunately, we have no idea what C library functions
323 might call malloc, so we can't really protect them unless you're
324 using GNU malloc. Fortunately, most of the major operating can use
325 GNU malloc. */
327 #ifndef SYSTEM_MALLOC
328 extern void * (*__malloc_hook) ();
329 static void * (*old_malloc_hook) ();
330 extern void * (*__realloc_hook) ();
331 static void * (*old_realloc_hook) ();
332 extern void (*__free_hook) ();
333 static void (*old_free_hook) ();
335 /* This function is used as the hook for free to call. */
337 static void
338 emacs_blocked_free (ptr)
339 void *ptr;
341 BLOCK_INPUT;
342 __free_hook = old_free_hook;
343 free (ptr);
344 /* If we released our reserve (due to running out of memory),
345 and we have a fair amount free once again,
346 try to set aside another reserve in case we run out once more. */
347 if (spare_memory == 0
348 /* Verify there is enough space that even with the malloc
349 hysteresis this call won't run out again.
350 The code here is correct as long as SPARE_MEMORY
351 is substantially larger than the block size malloc uses. */
352 && (bytes_used_when_full
353 > BYTES_USED + max (malloc_hysteresis, 4) * SPARE_MEMORY))
354 spare_memory = (char *) malloc (SPARE_MEMORY);
356 __free_hook = emacs_blocked_free;
357 UNBLOCK_INPUT;
360 /* If we released our reserve (due to running out of memory),
361 and we have a fair amount free once again,
362 try to set aside another reserve in case we run out once more.
364 This is called when a relocatable block is freed in ralloc.c. */
366 void
367 refill_memory_reserve ()
369 if (spare_memory == 0)
370 spare_memory = (char *) malloc (SPARE_MEMORY);
373 /* This function is the malloc hook that Emacs uses. */
375 static void *
376 emacs_blocked_malloc (size)
377 unsigned size;
379 void *value;
381 BLOCK_INPUT;
382 __malloc_hook = old_malloc_hook;
383 #ifdef DOUG_LEA_MALLOC
384 mallopt (M_TOP_PAD, malloc_hysteresis * 4096);
385 #else
386 __malloc_extra_blocks = malloc_hysteresis;
387 #endif
388 value = (void *) malloc (size);
389 __malloc_hook = emacs_blocked_malloc;
390 UNBLOCK_INPUT;
392 return value;
395 static void *
396 emacs_blocked_realloc (ptr, size)
397 void *ptr;
398 unsigned size;
400 void *value;
402 BLOCK_INPUT;
403 __realloc_hook = old_realloc_hook;
404 value = (void *) realloc (ptr, size);
405 __realloc_hook = emacs_blocked_realloc;
406 UNBLOCK_INPUT;
408 return value;
411 void
412 uninterrupt_malloc ()
414 old_free_hook = __free_hook;
415 __free_hook = emacs_blocked_free;
417 old_malloc_hook = __malloc_hook;
418 __malloc_hook = emacs_blocked_malloc;
420 old_realloc_hook = __realloc_hook;
421 __realloc_hook = emacs_blocked_realloc;
423 #endif
425 /* Interval allocation. */
427 #ifdef USE_TEXT_PROPERTIES
428 #define INTERVAL_BLOCK_SIZE \
429 ((1020 - sizeof (struct interval_block *)) / sizeof (struct interval))
431 struct interval_block
433 struct interval_block *next;
434 struct interval intervals[INTERVAL_BLOCK_SIZE];
437 struct interval_block *interval_block;
438 static int interval_block_index;
440 INTERVAL interval_free_list;
442 static void
443 init_intervals ()
445 allocating_for_lisp = 1;
446 interval_block
447 = (struct interval_block *) malloc (sizeof (struct interval_block));
448 allocating_for_lisp = 0;
449 interval_block->next = 0;
450 bzero ((char *) interval_block->intervals, sizeof interval_block->intervals);
451 interval_block_index = 0;
452 interval_free_list = 0;
455 #define INIT_INTERVALS init_intervals ()
457 INTERVAL
458 make_interval ()
460 INTERVAL val;
462 if (interval_free_list)
464 val = interval_free_list;
465 interval_free_list = interval_free_list->parent;
467 else
469 if (interval_block_index == INTERVAL_BLOCK_SIZE)
471 register struct interval_block *newi;
473 allocating_for_lisp = 1;
474 newi = (struct interval_block *) xmalloc (sizeof (struct interval_block));
476 allocating_for_lisp = 0;
477 VALIDATE_LISP_STORAGE (newi, sizeof *newi);
478 newi->next = interval_block;
479 interval_block = newi;
480 interval_block_index = 0;
482 val = &interval_block->intervals[interval_block_index++];
484 consing_since_gc += sizeof (struct interval);
485 intervals_consed++;
486 RESET_INTERVAL (val);
487 return val;
490 static int total_free_intervals, total_intervals;
492 /* Mark the pointers of one interval. */
494 static void
495 mark_interval (i, dummy)
496 register INTERVAL i;
497 Lisp_Object dummy;
499 if (XMARKBIT (i->plist))
500 abort ();
501 mark_object (&i->plist);
502 XMARK (i->plist);
505 static void
506 mark_interval_tree (tree)
507 register INTERVAL tree;
509 /* No need to test if this tree has been marked already; this
510 function is always called through the MARK_INTERVAL_TREE macro,
511 which takes care of that. */
513 /* XMARK expands to an assignment; the LHS of an assignment can't be
514 a cast. */
515 XMARK (* (Lisp_Object *) &tree->parent);
517 traverse_intervals (tree, 1, 0, mark_interval, Qnil);
520 #define MARK_INTERVAL_TREE(i) \
521 do { \
522 if (!NULL_INTERVAL_P (i) \
523 && ! XMARKBIT (*(Lisp_Object *) &i->parent)) \
524 mark_interval_tree (i); \
525 } while (0)
527 /* The oddity in the call to XUNMARK is necessary because XUNMARK
528 expands to an assignment to its argument, and most C compilers don't
529 support casts on the left operand of `='. */
530 #define UNMARK_BALANCE_INTERVALS(i) \
532 if (! NULL_INTERVAL_P (i)) \
534 XUNMARK (* (Lisp_Object *) (&(i)->parent)); \
535 (i) = balance_intervals (i); \
539 #else /* no interval use */
541 #define INIT_INTERVALS
543 #define UNMARK_BALANCE_INTERVALS(i)
544 #define MARK_INTERVAL_TREE(i)
546 #endif /* no interval use */
548 /* Floating point allocation. */
550 #ifdef LISP_FLOAT_TYPE
551 /* Allocation of float cells, just like conses */
552 /* We store float cells inside of float_blocks, allocating a new
553 float_block with malloc whenever necessary. Float cells reclaimed by
554 GC are put on a free list to be reallocated before allocating
555 any new float cells from the latest float_block.
557 Each float_block is just under 1020 bytes long,
558 since malloc really allocates in units of powers of two
559 and uses 4 bytes for its own overhead. */
561 #define FLOAT_BLOCK_SIZE \
562 ((1020 - sizeof (struct float_block *)) / sizeof (struct Lisp_Float))
564 struct float_block
566 struct float_block *next;
567 struct Lisp_Float floats[FLOAT_BLOCK_SIZE];
570 struct float_block *float_block;
571 int float_block_index;
573 struct Lisp_Float *float_free_list;
575 void
576 init_float ()
578 allocating_for_lisp = 1;
579 float_block = (struct float_block *) malloc (sizeof (struct float_block));
580 allocating_for_lisp = 0;
581 float_block->next = 0;
582 bzero ((char *) float_block->floats, sizeof float_block->floats);
583 float_block_index = 0;
584 float_free_list = 0;
587 /* Explicitly free a float cell. */
588 free_float (ptr)
589 struct Lisp_Float *ptr;
591 *(struct Lisp_Float **)&ptr->type = float_free_list;
592 float_free_list = ptr;
595 Lisp_Object
596 make_float (float_value)
597 double float_value;
599 register Lisp_Object val;
601 if (float_free_list)
603 XSETFLOAT (val, float_free_list);
604 float_free_list = *(struct Lisp_Float **)&float_free_list->type;
606 else
608 if (float_block_index == FLOAT_BLOCK_SIZE)
610 register struct float_block *new;
612 allocating_for_lisp = 1;
613 new = (struct float_block *) xmalloc (sizeof (struct float_block));
614 allocating_for_lisp = 0;
615 VALIDATE_LISP_STORAGE (new, sizeof *new);
616 new->next = float_block;
617 float_block = new;
618 float_block_index = 0;
620 XSETFLOAT (val, &float_block->floats[float_block_index++]);
622 XFLOAT (val)->data = float_value;
623 XSETFASTINT (XFLOAT (val)->type, 0); /* bug chasing -wsr */
624 consing_since_gc += sizeof (struct Lisp_Float);
625 floats_consed++;
626 return val;
629 #endif /* LISP_FLOAT_TYPE */
631 /* Allocation of cons cells */
632 /* We store cons cells inside of cons_blocks, allocating a new
633 cons_block with malloc whenever necessary. Cons cells reclaimed by
634 GC are put on a free list to be reallocated before allocating
635 any new cons cells from the latest cons_block.
637 Each cons_block is just under 1020 bytes long,
638 since malloc really allocates in units of powers of two
639 and uses 4 bytes for its own overhead. */
641 #define CONS_BLOCK_SIZE \
642 ((1020 - sizeof (struct cons_block *)) / sizeof (struct Lisp_Cons))
644 struct cons_block
646 struct cons_block *next;
647 struct Lisp_Cons conses[CONS_BLOCK_SIZE];
650 struct cons_block *cons_block;
651 int cons_block_index;
653 struct Lisp_Cons *cons_free_list;
655 void
656 init_cons ()
658 allocating_for_lisp = 1;
659 cons_block = (struct cons_block *) malloc (sizeof (struct cons_block));
660 allocating_for_lisp = 0;
661 cons_block->next = 0;
662 bzero ((char *) cons_block->conses, sizeof cons_block->conses);
663 cons_block_index = 0;
664 cons_free_list = 0;
667 /* Explicitly free a cons cell. */
668 free_cons (ptr)
669 struct Lisp_Cons *ptr;
671 *(struct Lisp_Cons **)&ptr->car = cons_free_list;
672 cons_free_list = ptr;
675 DEFUN ("cons", Fcons, Scons, 2, 2, 0,
676 "Create a new cons, give it CAR and CDR as components, and return it.")
677 (car, cdr)
678 Lisp_Object car, cdr;
680 register Lisp_Object val;
682 if (cons_free_list)
684 XSETCONS (val, cons_free_list);
685 cons_free_list = *(struct Lisp_Cons **)&cons_free_list->car;
687 else
689 if (cons_block_index == CONS_BLOCK_SIZE)
691 register struct cons_block *new;
692 allocating_for_lisp = 1;
693 new = (struct cons_block *) xmalloc (sizeof (struct cons_block));
694 allocating_for_lisp = 0;
695 VALIDATE_LISP_STORAGE (new, sizeof *new);
696 new->next = cons_block;
697 cons_block = new;
698 cons_block_index = 0;
700 XSETCONS (val, &cons_block->conses[cons_block_index++]);
702 XCONS (val)->car = car;
703 XCONS (val)->cdr = cdr;
704 consing_since_gc += sizeof (struct Lisp_Cons);
705 cons_cells_consed++;
706 return val;
709 DEFUN ("list", Flist, Slist, 0, MANY, 0,
710 "Return a newly created list with specified arguments as elements.\n\
711 Any number of arguments, even zero arguments, are allowed.")
712 (nargs, args)
713 int nargs;
714 register Lisp_Object *args;
716 register Lisp_Object val;
717 val = Qnil;
719 while (nargs > 0)
721 nargs--;
722 val = Fcons (args[nargs], val);
724 return val;
727 DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0,
728 "Return a newly created list of length LENGTH, with each element being INIT.")
729 (length, init)
730 register Lisp_Object length, init;
732 register Lisp_Object val;
733 register int size;
735 CHECK_NATNUM (length, 0);
736 size = XFASTINT (length);
738 val = Qnil;
739 while (size-- > 0)
740 val = Fcons (init, val);
741 return val;
744 /* Allocation of vectors */
746 struct Lisp_Vector *all_vectors;
748 struct Lisp_Vector *
749 allocate_vectorlike (len)
750 EMACS_INT len;
752 struct Lisp_Vector *p;
754 allocating_for_lisp = 1;
755 #ifdef DOUG_LEA_MALLOC
756 /* Prevent mmap'ing the chunk (which is potentially very large). */
757 mallopt (M_MMAP_MAX, 0);
758 #endif
759 p = (struct Lisp_Vector *)xmalloc (sizeof (struct Lisp_Vector)
760 + (len - 1) * sizeof (Lisp_Object));
761 #ifdef DOUG_LEA_MALLOC
762 /* Back to a reasonable maximum of mmap'ed areas. */
763 mallopt (M_MMAP_MAX, 64);
764 #endif
765 allocating_for_lisp = 0;
766 VALIDATE_LISP_STORAGE (p, 0);
767 consing_since_gc += (sizeof (struct Lisp_Vector)
768 + (len - 1) * sizeof (Lisp_Object));
769 vector_cells_consed += len;
771 p->next = all_vectors;
772 all_vectors = p;
773 return p;
776 DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0,
777 "Return a newly created vector of length LENGTH, with each element being INIT.\n\
778 See also the function `vector'.")
779 (length, init)
780 register Lisp_Object length, init;
782 Lisp_Object vector;
783 register EMACS_INT sizei;
784 register int index;
785 register struct Lisp_Vector *p;
787 CHECK_NATNUM (length, 0);
788 sizei = XFASTINT (length);
790 p = allocate_vectorlike (sizei);
791 p->size = sizei;
792 for (index = 0; index < sizei; index++)
793 p->contents[index] = init;
795 XSETVECTOR (vector, p);
796 return vector;
799 DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0,
800 "Return a newly created char-table, with purpose PURPOSE.\n\
801 Each element is initialized to INIT, which defaults to nil.\n\
802 PURPOSE should be a symbol which has a `char-table-extra-slots' property.\n\
803 The property's value should be an integer between 0 and 10.")
804 (purpose, init)
805 register Lisp_Object purpose, init;
807 Lisp_Object vector;
808 Lisp_Object n;
809 CHECK_SYMBOL (purpose, 1);
810 n = Fget (purpose, Qchar_table_extra_slots);
811 CHECK_NUMBER (n, 0);
812 if (XINT (n) < 0 || XINT (n) > 10)
813 args_out_of_range (n, Qnil);
814 /* Add 2 to the size for the defalt and parent slots. */
815 vector = Fmake_vector (make_number (CHAR_TABLE_STANDARD_SLOTS + XINT (n)),
816 init);
817 XCHAR_TABLE (vector)->top = Qt;
818 XCHAR_TABLE (vector)->parent = Qnil;
819 XCHAR_TABLE (vector)->purpose = purpose;
820 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
821 return vector;
824 /* Return a newly created sub char table with default value DEFALT.
825 Since a sub char table does not appear as a top level Emacs Lisp
826 object, we don't need a Lisp interface to make it. */
828 Lisp_Object
829 make_sub_char_table (defalt)
830 Lisp_Object defalt;
832 Lisp_Object vector
833 = Fmake_vector (make_number (SUB_CHAR_TABLE_STANDARD_SLOTS), Qnil);
834 XCHAR_TABLE (vector)->top = Qnil;
835 XCHAR_TABLE (vector)->defalt = defalt;
836 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
837 return vector;
840 DEFUN ("vector", Fvector, Svector, 0, MANY, 0,
841 "Return a newly created vector with specified arguments as elements.\n\
842 Any number of arguments, even zero arguments, are allowed.")
843 (nargs, args)
844 register int nargs;
845 Lisp_Object *args;
847 register Lisp_Object len, val;
848 register int index;
849 register struct Lisp_Vector *p;
851 XSETFASTINT (len, nargs);
852 val = Fmake_vector (len, Qnil);
853 p = XVECTOR (val);
854 for (index = 0; index < nargs; index++)
855 p->contents[index] = args[index];
856 return val;
859 DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0,
860 "Create a byte-code object with specified arguments as elements.\n\
861 The arguments should be the arglist, bytecode-string, constant vector,\n\
862 stack size, (optional) doc string, and (optional) interactive spec.\n\
863 The first four arguments are required; at most six have any\n\
864 significance.")
865 (nargs, args)
866 register int nargs;
867 Lisp_Object *args;
869 register Lisp_Object len, val;
870 register int index;
871 register struct Lisp_Vector *p;
873 XSETFASTINT (len, nargs);
874 if (!NILP (Vpurify_flag))
875 val = make_pure_vector ((EMACS_INT) nargs);
876 else
877 val = Fmake_vector (len, Qnil);
878 p = XVECTOR (val);
879 for (index = 0; index < nargs; index++)
881 if (!NILP (Vpurify_flag))
882 args[index] = Fpurecopy (args[index]);
883 p->contents[index] = args[index];
885 XSETCOMPILED (val, p);
886 return val;
889 /* Allocation of symbols.
890 Just like allocation of conses!
892 Each symbol_block is just under 1020 bytes long,
893 since malloc really allocates in units of powers of two
894 and uses 4 bytes for its own overhead. */
896 #define SYMBOL_BLOCK_SIZE \
897 ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol))
899 struct symbol_block
901 struct symbol_block *next;
902 struct Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE];
905 struct symbol_block *symbol_block;
906 int symbol_block_index;
908 struct Lisp_Symbol *symbol_free_list;
910 void
911 init_symbol ()
913 allocating_for_lisp = 1;
914 symbol_block = (struct symbol_block *) malloc (sizeof (struct symbol_block));
915 allocating_for_lisp = 0;
916 symbol_block->next = 0;
917 bzero ((char *) symbol_block->symbols, sizeof symbol_block->symbols);
918 symbol_block_index = 0;
919 symbol_free_list = 0;
922 DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0,
923 "Return a newly allocated uninterned symbol whose name is NAME.\n\
924 Its value and function definition are void, and its property list is nil.")
925 (name)
926 Lisp_Object name;
928 register Lisp_Object val;
929 register struct Lisp_Symbol *p;
931 CHECK_STRING (name, 0);
933 if (symbol_free_list)
935 XSETSYMBOL (val, symbol_free_list);
936 symbol_free_list = *(struct Lisp_Symbol **)&symbol_free_list->value;
938 else
940 if (symbol_block_index == SYMBOL_BLOCK_SIZE)
942 struct symbol_block *new;
943 allocating_for_lisp = 1;
944 new = (struct symbol_block *) xmalloc (sizeof (struct symbol_block));
945 allocating_for_lisp = 0;
946 VALIDATE_LISP_STORAGE (new, sizeof *new);
947 new->next = symbol_block;
948 symbol_block = new;
949 symbol_block_index = 0;
951 XSETSYMBOL (val, &symbol_block->symbols[symbol_block_index++]);
953 p = XSYMBOL (val);
954 p->name = XSTRING (name);
955 p->obarray = Qnil;
956 p->plist = Qnil;
957 p->value = Qunbound;
958 p->function = Qunbound;
959 p->next = 0;
960 consing_since_gc += sizeof (struct Lisp_Symbol);
961 symbols_consed++;
962 return val;
965 /* Allocation of markers and other objects that share that structure.
966 Works like allocation of conses. */
968 #define MARKER_BLOCK_SIZE \
969 ((1020 - sizeof (struct marker_block *)) / sizeof (union Lisp_Misc))
971 struct marker_block
973 struct marker_block *next;
974 union Lisp_Misc markers[MARKER_BLOCK_SIZE];
977 struct marker_block *marker_block;
978 int marker_block_index;
980 union Lisp_Misc *marker_free_list;
982 void
983 init_marker ()
985 allocating_for_lisp = 1;
986 marker_block = (struct marker_block *) malloc (sizeof (struct marker_block));
987 allocating_for_lisp = 0;
988 marker_block->next = 0;
989 bzero ((char *) marker_block->markers, sizeof marker_block->markers);
990 marker_block_index = 0;
991 marker_free_list = 0;
994 /* Return a newly allocated Lisp_Misc object, with no substructure. */
995 Lisp_Object
996 allocate_misc ()
998 Lisp_Object val;
1000 if (marker_free_list)
1002 XSETMISC (val, marker_free_list);
1003 marker_free_list = marker_free_list->u_free.chain;
1005 else
1007 if (marker_block_index == MARKER_BLOCK_SIZE)
1009 struct marker_block *new;
1010 allocating_for_lisp = 1;
1011 new = (struct marker_block *) xmalloc (sizeof (struct marker_block));
1012 allocating_for_lisp = 0;
1013 VALIDATE_LISP_STORAGE (new, sizeof *new);
1014 new->next = marker_block;
1015 marker_block = new;
1016 marker_block_index = 0;
1018 XSETMISC (val, &marker_block->markers[marker_block_index++]);
1020 consing_since_gc += sizeof (union Lisp_Misc);
1021 misc_objects_consed++;
1022 return val;
1025 DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
1026 "Return a newly allocated marker which does not point at any place.")
1029 register Lisp_Object val;
1030 register struct Lisp_Marker *p;
1032 val = allocate_misc ();
1033 XMISCTYPE (val) = Lisp_Misc_Marker;
1034 p = XMARKER (val);
1035 p->buffer = 0;
1036 p->bufpos = 0;
1037 p->chain = Qnil;
1038 p->insertion_type = 0;
1039 return val;
1042 /* Put MARKER back on the free list after using it temporarily. */
1044 free_marker (marker)
1045 Lisp_Object marker;
1047 XMISC (marker)->u_marker.type = Lisp_Misc_Free;
1048 XMISC (marker)->u_free.chain = marker_free_list;
1049 marker_free_list = XMISC (marker);
1051 total_free_markers++;
1054 /* Allocation of strings */
1056 /* Strings reside inside of string_blocks. The entire data of the string,
1057 both the size and the contents, live in part of the `chars' component of a string_block.
1058 The `pos' component is the index within `chars' of the first free byte.
1060 first_string_block points to the first string_block ever allocated.
1061 Each block points to the next one with its `next' field.
1062 The `prev' fields chain in reverse order.
1063 The last one allocated is the one currently being filled.
1064 current_string_block points to it.
1066 The string_blocks that hold individual large strings
1067 go in a separate chain, started by large_string_blocks. */
1070 /* String blocks contain this many useful bytes.
1071 8188 is power of 2, minus 4 for malloc overhead. */
1072 #define STRING_BLOCK_SIZE (8188 - sizeof (struct string_block_head))
1074 /* A string bigger than this gets its own specially-made string block
1075 if it doesn't fit in the current one. */
1076 #define STRING_BLOCK_OUTSIZE 1024
1078 struct string_block_head
1080 struct string_block *next, *prev;
1081 EMACS_INT pos;
1084 struct string_block
1086 struct string_block *next, *prev;
1087 EMACS_INT pos;
1088 char chars[STRING_BLOCK_SIZE];
1091 /* This points to the string block we are now allocating strings. */
1093 struct string_block *current_string_block;
1095 /* This points to the oldest string block, the one that starts the chain. */
1097 struct string_block *first_string_block;
1099 /* Last string block in chain of those made for individual large strings. */
1101 struct string_block *large_string_blocks;
1103 /* If SIZE is the length of a string, this returns how many bytes
1104 the string occupies in a string_block (including padding). */
1106 #define STRING_FULLSIZE(size) (((size) + sizeof (struct Lisp_String) + PAD) \
1107 & ~(PAD - 1))
1108 #define PAD (sizeof (EMACS_INT))
1110 #if 0
1111 #define STRING_FULLSIZE(SIZE) \
1112 (((SIZE) + 2 * sizeof (EMACS_INT)) & ~(sizeof (EMACS_INT) - 1))
1113 #endif
1115 void
1116 init_strings ()
1118 allocating_for_lisp = 1;
1119 current_string_block = (struct string_block *) malloc (sizeof (struct string_block));
1120 allocating_for_lisp = 0;
1121 first_string_block = current_string_block;
1122 consing_since_gc += sizeof (struct string_block);
1123 current_string_block->next = 0;
1124 current_string_block->prev = 0;
1125 current_string_block->pos = 0;
1126 large_string_blocks = 0;
1129 DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0,
1130 "Return a newly created string of length LENGTH, with each element being INIT.\n\
1131 Both LENGTH and INIT must be numbers.")
1132 (length, init)
1133 Lisp_Object length, init;
1135 register Lisp_Object val;
1136 register unsigned char *p, *end, c;
1138 CHECK_NATNUM (length, 0);
1139 CHECK_NUMBER (init, 1);
1140 val = make_uninit_string (XFASTINT (length));
1141 c = XINT (init);
1142 p = XSTRING (val)->data;
1143 end = p + XSTRING (val)->size;
1144 while (p != end)
1145 *p++ = c;
1146 *p = 0;
1147 return val;
1150 DEFUN ("make-bool-vector", Fmake_bool_vector, Smake_bool_vector, 2, 2, 0,
1151 "Return a new bool-vector of length LENGTH, using INIT for as each element.\n\
1152 LENGTH must be a number. INIT matters only in whether it is t or nil.")
1153 (length, init)
1154 Lisp_Object length, init;
1156 register Lisp_Object val;
1157 struct Lisp_Bool_Vector *p;
1158 int real_init, i;
1159 int length_in_chars, length_in_elts, bits_per_value;
1161 CHECK_NATNUM (length, 0);
1163 bits_per_value = sizeof (EMACS_INT) * BITS_PER_CHAR;
1165 length_in_elts = (XFASTINT (length) + bits_per_value - 1) / bits_per_value;
1166 length_in_chars = length_in_elts * sizeof (EMACS_INT);
1168 /* We must allocate one more elements than LENGTH_IN_ELTS for the
1169 slot `size' of the struct Lisp_Bool_Vector. */
1170 val = Fmake_vector (make_number (length_in_elts + 1), Qnil);
1171 p = XBOOL_VECTOR (val);
1172 /* Get rid of any bits that would cause confusion. */
1173 p->vector_size = 0;
1174 XSETBOOL_VECTOR (val, p);
1175 p->size = XFASTINT (length);
1177 real_init = (NILP (init) ? 0 : -1);
1178 for (i = 0; i < length_in_chars ; i++)
1179 p->data[i] = real_init;
1181 return val;
1184 Lisp_Object
1185 make_string (contents, length)
1186 char *contents;
1187 int length;
1189 register Lisp_Object val;
1190 val = make_uninit_string (length);
1191 bcopy (contents, XSTRING (val)->data, length);
1192 return val;
1195 Lisp_Object
1196 build_string (str)
1197 char *str;
1199 return make_string (str, strlen (str));
1202 Lisp_Object
1203 make_uninit_string (length)
1204 int length;
1206 register Lisp_Object val;
1207 register int fullsize = STRING_FULLSIZE (length);
1209 if (length < 0) abort ();
1211 if (fullsize <= STRING_BLOCK_SIZE - current_string_block->pos)
1212 /* This string can fit in the current string block */
1214 XSETSTRING (val,
1215 ((struct Lisp_String *)
1216 (current_string_block->chars + current_string_block->pos)));
1217 current_string_block->pos += fullsize;
1219 else if (fullsize > STRING_BLOCK_OUTSIZE)
1220 /* This string gets its own string block */
1222 register struct string_block *new;
1223 allocating_for_lisp = 1;
1224 #ifdef DOUG_LEA_MALLOC
1225 /* Prevent mmap'ing the chunk (which is potentially very large). */
1226 mallopt (M_MMAP_MAX, 0);
1227 #endif
1228 new = (struct string_block *) xmalloc (sizeof (struct string_block_head) + fullsize);
1229 #ifdef DOUG_LEA_MALLOC
1230 /* Back to a reasonable maximum of mmap'ed areas. */
1231 mallopt (M_MMAP_MAX, 64);
1232 #endif
1233 allocating_for_lisp = 0;
1234 VALIDATE_LISP_STORAGE (new, 0);
1235 consing_since_gc += sizeof (struct string_block_head) + fullsize;
1236 new->pos = fullsize;
1237 new->next = large_string_blocks;
1238 large_string_blocks = new;
1239 XSETSTRING (val,
1240 ((struct Lisp_String *)
1241 ((struct string_block_head *)new + 1)));
1243 else
1244 /* Make a new current string block and start it off with this string */
1246 register struct string_block *new;
1247 allocating_for_lisp = 1;
1248 new = (struct string_block *) xmalloc (sizeof (struct string_block));
1249 allocating_for_lisp = 0;
1250 VALIDATE_LISP_STORAGE (new, sizeof *new);
1251 consing_since_gc += sizeof (struct string_block);
1252 current_string_block->next = new;
1253 new->prev = current_string_block;
1254 new->next = 0;
1255 current_string_block = new;
1256 new->pos = fullsize;
1257 XSETSTRING (val,
1258 (struct Lisp_String *) current_string_block->chars);
1261 string_chars_consed += fullsize;
1262 XSTRING (val)->size = length;
1263 XSTRING (val)->data[length] = 0;
1264 INITIALIZE_INTERVAL (XSTRING (val), NULL_INTERVAL);
1266 return val;
1269 /* Return a newly created vector or string with specified arguments as
1270 elements. If all the arguments are characters that can fit
1271 in a string of events, make a string; otherwise, make a vector.
1273 Any number of arguments, even zero arguments, are allowed. */
1275 Lisp_Object
1276 make_event_array (nargs, args)
1277 register int nargs;
1278 Lisp_Object *args;
1280 int i;
1282 for (i = 0; i < nargs; i++)
1283 /* The things that fit in a string
1284 are characters that are in 0...127,
1285 after discarding the meta bit and all the bits above it. */
1286 if (!INTEGERP (args[i])
1287 || (XUINT (args[i]) & ~(-CHAR_META)) >= 0200)
1288 return Fvector (nargs, args);
1290 /* Since the loop exited, we know that all the things in it are
1291 characters, so we can make a string. */
1293 Lisp_Object result;
1295 result = Fmake_string (make_number (nargs), make_number (0));
1296 for (i = 0; i < nargs; i++)
1298 XSTRING (result)->data[i] = XINT (args[i]);
1299 /* Move the meta bit to the right place for a string char. */
1300 if (XINT (args[i]) & CHAR_META)
1301 XSTRING (result)->data[i] |= 0x80;
1304 return result;
1308 /* Pure storage management. */
1310 /* Must get an error if pure storage is full,
1311 since if it cannot hold a large string
1312 it may be able to hold conses that point to that string;
1313 then the string is not protected from gc. */
1315 Lisp_Object
1316 make_pure_string (data, length)
1317 char *data;
1318 int length;
1320 register Lisp_Object new;
1321 register int size = sizeof (EMACS_INT) + INTERVAL_PTR_SIZE + length + 1;
1323 if (pureptr + size > PURESIZE)
1324 error ("Pure Lisp storage exhausted");
1325 XSETSTRING (new, PUREBEG + pureptr);
1326 XSTRING (new)->size = length;
1327 bcopy (data, XSTRING (new)->data, length);
1328 XSTRING (new)->data[length] = 0;
1330 /* We must give strings in pure storage some kind of interval. So we
1331 give them a null one. */
1332 #if defined (USE_TEXT_PROPERTIES)
1333 XSTRING (new)->intervals = NULL_INTERVAL;
1334 #endif
1335 pureptr += (size + sizeof (EMACS_INT) - 1)
1336 / sizeof (EMACS_INT) * sizeof (EMACS_INT);
1337 return new;
1340 Lisp_Object
1341 pure_cons (car, cdr)
1342 Lisp_Object car, cdr;
1344 register Lisp_Object new;
1346 if (pureptr + sizeof (struct Lisp_Cons) > PURESIZE)
1347 error ("Pure Lisp storage exhausted");
1348 XSETCONS (new, PUREBEG + pureptr);
1349 pureptr += sizeof (struct Lisp_Cons);
1350 XCONS (new)->car = Fpurecopy (car);
1351 XCONS (new)->cdr = Fpurecopy (cdr);
1352 return new;
1355 #ifdef LISP_FLOAT_TYPE
1357 Lisp_Object
1358 make_pure_float (num)
1359 double num;
1361 register Lisp_Object new;
1363 /* Make sure that PUREBEG + pureptr is aligned on at least a sizeof
1364 (double) boundary. Some architectures (like the sparc) require
1365 this, and I suspect that floats are rare enough that it's no
1366 tragedy for those that do. */
1368 int alignment;
1369 char *p = PUREBEG + pureptr;
1371 #ifdef __GNUC__
1372 #if __GNUC__ >= 2
1373 alignment = __alignof (struct Lisp_Float);
1374 #else
1375 alignment = sizeof (struct Lisp_Float);
1376 #endif
1377 #else
1378 alignment = sizeof (struct Lisp_Float);
1379 #endif
1380 p = (char *) (((unsigned long) p + alignment - 1) & - alignment);
1381 pureptr = p - PUREBEG;
1384 if (pureptr + sizeof (struct Lisp_Float) > PURESIZE)
1385 error ("Pure Lisp storage exhausted");
1386 XSETFLOAT (new, PUREBEG + pureptr);
1387 pureptr += sizeof (struct Lisp_Float);
1388 XFLOAT (new)->data = num;
1389 XSETFASTINT (XFLOAT (new)->type, 0); /* bug chasing -wsr */
1390 return new;
1393 #endif /* LISP_FLOAT_TYPE */
1395 Lisp_Object
1396 make_pure_vector (len)
1397 EMACS_INT len;
1399 register Lisp_Object new;
1400 register EMACS_INT size = sizeof (struct Lisp_Vector) + (len - 1) * sizeof (Lisp_Object);
1402 if (pureptr + size > PURESIZE)
1403 error ("Pure Lisp storage exhausted");
1405 XSETVECTOR (new, PUREBEG + pureptr);
1406 pureptr += size;
1407 XVECTOR (new)->size = len;
1408 return new;
1411 DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
1412 "Make a copy of OBJECT in pure storage.\n\
1413 Recursively copies contents of vectors and cons cells.\n\
1414 Does not copy symbols.")
1415 (obj)
1416 register Lisp_Object obj;
1418 if (NILP (Vpurify_flag))
1419 return obj;
1421 if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
1422 && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
1423 return obj;
1425 if (CONSP (obj))
1426 return pure_cons (XCONS (obj)->car, XCONS (obj)->cdr);
1427 #ifdef LISP_FLOAT_TYPE
1428 else if (FLOATP (obj))
1429 return make_pure_float (XFLOAT (obj)->data);
1430 #endif /* LISP_FLOAT_TYPE */
1431 else if (STRINGP (obj))
1432 return make_pure_string (XSTRING (obj)->data, XSTRING (obj)->size);
1433 else if (COMPILEDP (obj) || VECTORP (obj))
1435 register struct Lisp_Vector *vec;
1436 register int i, size;
1438 size = XVECTOR (obj)->size;
1439 if (size & PSEUDOVECTOR_FLAG)
1440 size &= PSEUDOVECTOR_SIZE_MASK;
1441 vec = XVECTOR (make_pure_vector ((EMACS_INT) size));
1442 for (i = 0; i < size; i++)
1443 vec->contents[i] = Fpurecopy (XVECTOR (obj)->contents[i]);
1444 if (COMPILEDP (obj))
1445 XSETCOMPILED (obj, vec);
1446 else
1447 XSETVECTOR (obj, vec);
1448 return obj;
1450 else if (MARKERP (obj))
1451 error ("Attempt to copy a marker to pure storage");
1452 else
1453 return obj;
1456 /* Recording what needs to be marked for gc. */
1458 struct gcpro *gcprolist;
1460 #define NSTATICS 768
1462 Lisp_Object *staticvec[NSTATICS] = {0};
1464 int staticidx = 0;
1466 /* Put an entry in staticvec, pointing at the variable whose address is given */
1468 void
1469 staticpro (varaddress)
1470 Lisp_Object *varaddress;
1472 staticvec[staticidx++] = varaddress;
1473 if (staticidx >= NSTATICS)
1474 abort ();
1477 struct catchtag
1479 Lisp_Object tag;
1480 Lisp_Object val;
1481 struct catchtag *next;
1482 /* jmp_buf jmp; /* We don't need this for GC purposes */
1485 struct backtrace
1487 struct backtrace *next;
1488 Lisp_Object *function;
1489 Lisp_Object *args; /* Points to vector of args. */
1490 int nargs; /* length of vector */
1491 /* if nargs is UNEVALLED, args points to slot holding list of unevalled args */
1492 char evalargs;
1495 /* Garbage collection! */
1497 /* Temporarily prevent garbage collection. */
1500 inhibit_garbage_collection ()
1502 int count = specpdl_ptr - specpdl;
1503 Lisp_Object number;
1504 int nbits = min (VALBITS, BITS_PER_INT);
1506 XSETINT (number, ((EMACS_INT) 1 << (nbits - 1)) - 1);
1508 specbind (Qgc_cons_threshold, number);
1510 return count;
1513 DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
1514 "Reclaim storage for Lisp objects no longer needed.\n\
1515 Returns info on amount of space in use:\n\
1516 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)\n\
1517 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS\n\
1518 (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS))\n\
1519 Garbage collection happens automatically if you cons more than\n\
1520 `gc-cons-threshold' bytes of Lisp data since previous garbage collection.")
1523 register struct gcpro *tail;
1524 register struct specbinding *bind;
1525 struct catchtag *catch;
1526 struct handler *handler;
1527 register struct backtrace *backlist;
1528 register Lisp_Object tem;
1529 char *omessage = echo_area_glyphs;
1530 int omessage_length = echo_area_glyphs_length;
1531 char stack_top_variable;
1532 register int i;
1534 /* In case user calls debug_print during GC,
1535 don't let that cause a recursive GC. */
1536 consing_since_gc = 0;
1538 /* Save a copy of the contents of the stack, for debugging. */
1539 #if MAX_SAVE_STACK > 0
1540 if (NILP (Vpurify_flag))
1542 i = &stack_top_variable - stack_bottom;
1543 if (i < 0) i = -i;
1544 if (i < MAX_SAVE_STACK)
1546 if (stack_copy == 0)
1547 stack_copy = (char *) xmalloc (stack_copy_size = i);
1548 else if (stack_copy_size < i)
1549 stack_copy = (char *) xrealloc (stack_copy, (stack_copy_size = i));
1550 if (stack_copy)
1552 if ((EMACS_INT) (&stack_top_variable - stack_bottom) > 0)
1553 bcopy (stack_bottom, stack_copy, i);
1554 else
1555 bcopy (&stack_top_variable, stack_copy, i);
1559 #endif /* MAX_SAVE_STACK > 0 */
1561 if (garbage_collection_messages)
1562 message1_nolog ("Garbage collecting...");
1564 /* Don't keep command history around forever. */
1565 if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0)
1567 tem = Fnthcdr (Vhistory_length, Vcommand_history);
1568 if (CONSP (tem))
1569 XCONS (tem)->cdr = Qnil;
1572 /* Likewise for undo information. */
1574 register struct buffer *nextb = all_buffers;
1576 while (nextb)
1578 /* If a buffer's undo list is Qt, that means that undo is
1579 turned off in that buffer. Calling truncate_undo_list on
1580 Qt tends to return NULL, which effectively turns undo back on.
1581 So don't call truncate_undo_list if undo_list is Qt. */
1582 if (! EQ (nextb->undo_list, Qt))
1583 nextb->undo_list
1584 = truncate_undo_list (nextb->undo_list, undo_limit,
1585 undo_strong_limit);
1586 nextb = nextb->next;
1590 gc_in_progress = 1;
1592 /* clear_marks (); */
1594 /* In each "large string", set the MARKBIT of the size field.
1595 That enables mark_object to recognize them. */
1597 register struct string_block *b;
1598 for (b = large_string_blocks; b; b = b->next)
1599 ((struct Lisp_String *)(&b->chars[0]))->size |= MARKBIT;
1602 /* Mark all the special slots that serve as the roots of accessibility.
1604 Usually the special slots to mark are contained in particular structures.
1605 Then we know no slot is marked twice because the structures don't overlap.
1606 In some cases, the structures point to the slots to be marked.
1607 For these, we use MARKBIT to avoid double marking of the slot. */
1609 for (i = 0; i < staticidx; i++)
1610 mark_object (staticvec[i]);
1611 for (tail = gcprolist; tail; tail = tail->next)
1612 for (i = 0; i < tail->nvars; i++)
1613 if (!XMARKBIT (tail->var[i]))
1615 mark_object (&tail->var[i]);
1616 XMARK (tail->var[i]);
1618 for (bind = specpdl; bind != specpdl_ptr; bind++)
1620 mark_object (&bind->symbol);
1621 mark_object (&bind->old_value);
1623 for (catch = catchlist; catch; catch = catch->next)
1625 mark_object (&catch->tag);
1626 mark_object (&catch->val);
1628 for (handler = handlerlist; handler; handler = handler->next)
1630 mark_object (&handler->handler);
1631 mark_object (&handler->var);
1633 for (backlist = backtrace_list; backlist; backlist = backlist->next)
1635 if (!XMARKBIT (*backlist->function))
1637 mark_object (backlist->function);
1638 XMARK (*backlist->function);
1640 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
1641 i = 0;
1642 else
1643 i = backlist->nargs - 1;
1644 for (; i >= 0; i--)
1645 if (!XMARKBIT (backlist->args[i]))
1647 mark_object (&backlist->args[i]);
1648 XMARK (backlist->args[i]);
1651 mark_kboards ();
1653 gc_sweep ();
1655 /* Clear the mark bits that we set in certain root slots. */
1657 for (tail = gcprolist; tail; tail = tail->next)
1658 for (i = 0; i < tail->nvars; i++)
1659 XUNMARK (tail->var[i]);
1660 for (backlist = backtrace_list; backlist; backlist = backlist->next)
1662 XUNMARK (*backlist->function);
1663 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
1664 i = 0;
1665 else
1666 i = backlist->nargs - 1;
1667 for (; i >= 0; i--)
1668 XUNMARK (backlist->args[i]);
1670 XUNMARK (buffer_defaults.name);
1671 XUNMARK (buffer_local_symbols.name);
1673 /* clear_marks (); */
1674 gc_in_progress = 0;
1676 consing_since_gc = 0;
1677 if (gc_cons_threshold < 10000)
1678 gc_cons_threshold = 10000;
1680 if (garbage_collection_messages)
1682 if (omessage || minibuf_level > 0)
1683 message2_nolog (omessage, omessage_length);
1684 else
1685 message1_nolog ("Garbage collecting...done");
1688 return Fcons (Fcons (make_number (total_conses),
1689 make_number (total_free_conses)),
1690 Fcons (Fcons (make_number (total_symbols),
1691 make_number (total_free_symbols)),
1692 Fcons (Fcons (make_number (total_markers),
1693 make_number (total_free_markers)),
1694 Fcons (make_number (total_string_size),
1695 Fcons (make_number (total_vector_size),
1696 Fcons (Fcons
1697 #ifdef LISP_FLOAT_TYPE
1698 (make_number (total_floats),
1699 make_number (total_free_floats)),
1700 #else /* not LISP_FLOAT_TYPE */
1701 (make_number (0), make_number (0)),
1702 #endif /* not LISP_FLOAT_TYPE */
1703 Fcons (Fcons
1704 #ifdef USE_TEXT_PROPERTIES
1705 (make_number (total_intervals),
1706 make_number (total_free_intervals)),
1707 #else /* not USE_TEXT_PROPERTIES */
1708 (make_number (0), make_number (0)),
1709 #endif /* not USE_TEXT_PROPERTIES */
1710 Qnil)))))));
1713 #if 0
1714 static void
1715 clear_marks ()
1717 /* Clear marks on all conses */
1719 register struct cons_block *cblk;
1720 register int lim = cons_block_index;
1722 for (cblk = cons_block; cblk; cblk = cblk->next)
1724 register int i;
1725 for (i = 0; i < lim; i++)
1726 XUNMARK (cblk->conses[i].car);
1727 lim = CONS_BLOCK_SIZE;
1730 /* Clear marks on all symbols */
1732 register struct symbol_block *sblk;
1733 register int lim = symbol_block_index;
1735 for (sblk = symbol_block; sblk; sblk = sblk->next)
1737 register int i;
1738 for (i = 0; i < lim; i++)
1740 XUNMARK (sblk->symbols[i].plist);
1742 lim = SYMBOL_BLOCK_SIZE;
1745 /* Clear marks on all markers */
1747 register struct marker_block *sblk;
1748 register int lim = marker_block_index;
1750 for (sblk = marker_block; sblk; sblk = sblk->next)
1752 register int i;
1753 for (i = 0; i < lim; i++)
1754 if (sblk->markers[i].u_marker.type == Lisp_Misc_Marker)
1755 XUNMARK (sblk->markers[i].u_marker.chain);
1756 lim = MARKER_BLOCK_SIZE;
1759 /* Clear mark bits on all buffers */
1761 register struct buffer *nextb = all_buffers;
1763 while (nextb)
1765 XUNMARK (nextb->name);
1766 nextb = nextb->next;
1770 #endif
1772 /* Mark reference to a Lisp_Object.
1773 If the object referred to has not been seen yet, recursively mark
1774 all the references contained in it.
1776 If the object referenced is a short string, the referencing slot
1777 is threaded into a chain of such slots, pointed to from
1778 the `size' field of the string. The actual string size
1779 lives in the last slot in the chain. We recognize the end
1780 because it is < (unsigned) STRING_BLOCK_SIZE. */
1782 #define LAST_MARKED_SIZE 500
1783 Lisp_Object *last_marked[LAST_MARKED_SIZE];
1784 int last_marked_index;
1786 static void
1787 mark_object (argptr)
1788 Lisp_Object *argptr;
1790 Lisp_Object *objptr = argptr;
1791 register Lisp_Object obj;
1793 loop:
1794 obj = *objptr;
1795 loop2:
1796 XUNMARK (obj);
1798 if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
1799 && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
1800 return;
1802 last_marked[last_marked_index++] = objptr;
1803 if (last_marked_index == LAST_MARKED_SIZE)
1804 last_marked_index = 0;
1806 switch (SWITCH_ENUM_CAST (XGCTYPE (obj)))
1808 case Lisp_String:
1810 register struct Lisp_String *ptr = XSTRING (obj);
1812 MARK_INTERVAL_TREE (ptr->intervals);
1813 if (ptr->size & MARKBIT)
1814 /* A large string. Just set ARRAY_MARK_FLAG. */
1815 ptr->size |= ARRAY_MARK_FLAG;
1816 else
1818 /* A small string. Put this reference
1819 into the chain of references to it.
1820 If the address includes MARKBIT, put that bit elsewhere
1821 when we store OBJPTR into the size field. */
1823 if (XMARKBIT (*objptr))
1825 XSETFASTINT (*objptr, ptr->size);
1826 XMARK (*objptr);
1828 else
1829 XSETFASTINT (*objptr, ptr->size);
1831 if ((EMACS_INT) objptr & DONT_COPY_FLAG)
1832 abort ();
1833 ptr->size = (EMACS_INT) objptr;
1834 if (ptr->size & MARKBIT)
1835 ptr->size ^= MARKBIT | DONT_COPY_FLAG;
1838 break;
1840 case Lisp_Vectorlike:
1841 if (GC_BUFFERP (obj))
1843 if (!XMARKBIT (XBUFFER (obj)->name))
1844 mark_buffer (obj);
1846 else if (GC_SUBRP (obj))
1847 break;
1848 else if (GC_COMPILEDP (obj))
1849 /* We could treat this just like a vector, but it is better
1850 to save the COMPILED_CONSTANTS element for last and avoid recursion
1851 there. */
1853 register struct Lisp_Vector *ptr = XVECTOR (obj);
1854 register EMACS_INT size = ptr->size;
1855 /* See comment above under Lisp_Vector. */
1856 struct Lisp_Vector *volatile ptr1 = ptr;
1857 register int i;
1859 if (size & ARRAY_MARK_FLAG)
1860 break; /* Already marked */
1861 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
1862 size &= PSEUDOVECTOR_SIZE_MASK;
1863 for (i = 0; i < size; i++) /* and then mark its elements */
1865 if (i != COMPILED_CONSTANTS)
1866 mark_object (&ptr1->contents[i]);
1868 /* This cast should be unnecessary, but some Mips compiler complains
1869 (MIPS-ABI + SysVR4, DC/OSx, etc). */
1870 objptr = (Lisp_Object *) &ptr1->contents[COMPILED_CONSTANTS];
1871 goto loop;
1873 else if (GC_FRAMEP (obj))
1875 /* See comment above under Lisp_Vector for why this is volatile. */
1876 register struct frame *volatile ptr = XFRAME (obj);
1877 register EMACS_INT size = ptr->size;
1879 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
1880 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
1882 mark_object (&ptr->name);
1883 mark_object (&ptr->icon_name);
1884 mark_object (&ptr->title);
1885 mark_object (&ptr->focus_frame);
1886 mark_object (&ptr->selected_window);
1887 mark_object (&ptr->minibuffer_window);
1888 mark_object (&ptr->param_alist);
1889 mark_object (&ptr->scroll_bars);
1890 mark_object (&ptr->condemned_scroll_bars);
1891 mark_object (&ptr->menu_bar_items);
1892 mark_object (&ptr->face_alist);
1893 mark_object (&ptr->menu_bar_vector);
1894 mark_object (&ptr->buffer_predicate);
1895 mark_object (&ptr->buffer_list);
1897 else if (GC_BOOL_VECTOR_P (obj))
1899 register struct Lisp_Vector *ptr = XVECTOR (obj);
1901 if (ptr->size & ARRAY_MARK_FLAG)
1902 break; /* Already marked */
1903 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
1905 else
1907 register struct Lisp_Vector *ptr = XVECTOR (obj);
1908 register EMACS_INT size = ptr->size;
1909 /* The reason we use ptr1 is to avoid an apparent hardware bug
1910 that happens occasionally on the FSF's HP 300s.
1911 The bug is that a2 gets clobbered by recursive calls to mark_object.
1912 The clobberage seems to happen during function entry,
1913 perhaps in the moveml instruction.
1914 Yes, this is a crock, but we have to do it. */
1915 struct Lisp_Vector *volatile ptr1 = ptr;
1916 register int i;
1918 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
1919 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
1920 if (size & PSEUDOVECTOR_FLAG)
1921 size &= PSEUDOVECTOR_SIZE_MASK;
1922 for (i = 0; i < size; i++) /* and then mark its elements */
1923 mark_object (&ptr1->contents[i]);
1925 break;
1927 case Lisp_Symbol:
1929 /* See comment above under Lisp_Vector for why this is volatile. */
1930 register struct Lisp_Symbol *volatile ptr = XSYMBOL (obj);
1931 struct Lisp_Symbol *ptrx;
1933 if (XMARKBIT (ptr->plist)) break;
1934 XMARK (ptr->plist);
1935 mark_object ((Lisp_Object *) &ptr->value);
1936 mark_object (&ptr->function);
1937 mark_object (&ptr->plist);
1938 XSETTYPE (*(Lisp_Object *) &ptr->name, Lisp_String);
1939 mark_object (&ptr->name);
1940 ptr = ptr->next;
1941 if (ptr)
1943 /* For the benefit of the last_marked log. */
1944 objptr = (Lisp_Object *)&XSYMBOL (obj)->next;
1945 ptrx = ptr; /* Use of ptrx avoids compiler bug on Sun */
1946 XSETSYMBOL (obj, ptrx);
1947 /* We can't goto loop here because *objptr doesn't contain an
1948 actual Lisp_Object with valid datatype field. */
1949 goto loop2;
1952 break;
1954 case Lisp_Misc:
1955 switch (XMISCTYPE (obj))
1957 case Lisp_Misc_Marker:
1958 XMARK (XMARKER (obj)->chain);
1959 /* DO NOT mark thru the marker's chain.
1960 The buffer's markers chain does not preserve markers from gc;
1961 instead, markers are removed from the chain when freed by gc. */
1962 break;
1964 case Lisp_Misc_Buffer_Local_Value:
1965 case Lisp_Misc_Some_Buffer_Local_Value:
1967 register struct Lisp_Buffer_Local_Value *ptr
1968 = XBUFFER_LOCAL_VALUE (obj);
1969 if (XMARKBIT (ptr->car)) break;
1970 XMARK (ptr->car);
1971 /* If the cdr is nil, avoid recursion for the car. */
1972 if (EQ (ptr->cdr, Qnil))
1974 objptr = &ptr->car;
1975 goto loop;
1977 mark_object (&ptr->car);
1978 /* See comment above under Lisp_Vector for why not use ptr here. */
1979 objptr = &XBUFFER_LOCAL_VALUE (obj)->cdr;
1980 goto loop;
1983 case Lisp_Misc_Intfwd:
1984 case Lisp_Misc_Boolfwd:
1985 case Lisp_Misc_Objfwd:
1986 case Lisp_Misc_Buffer_Objfwd:
1987 case Lisp_Misc_Kboard_Objfwd:
1988 /* Don't bother with Lisp_Buffer_Objfwd,
1989 since all markable slots in current buffer marked anyway. */
1990 /* Don't need to do Lisp_Objfwd, since the places they point
1991 are protected with staticpro. */
1992 break;
1994 case Lisp_Misc_Overlay:
1996 struct Lisp_Overlay *ptr = XOVERLAY (obj);
1997 if (!XMARKBIT (ptr->plist))
1999 XMARK (ptr->plist);
2000 mark_object (&ptr->start);
2001 mark_object (&ptr->end);
2002 objptr = &ptr->plist;
2003 goto loop;
2006 break;
2008 default:
2009 abort ();
2011 break;
2013 case Lisp_Cons:
2015 register struct Lisp_Cons *ptr = XCONS (obj);
2016 if (XMARKBIT (ptr->car)) break;
2017 XMARK (ptr->car);
2018 /* If the cdr is nil, avoid recursion for the car. */
2019 if (EQ (ptr->cdr, Qnil))
2021 objptr = &ptr->car;
2022 goto loop;
2024 mark_object (&ptr->car);
2025 /* See comment above under Lisp_Vector for why not use ptr here. */
2026 objptr = &XCONS (obj)->cdr;
2027 goto loop;
2030 #ifdef LISP_FLOAT_TYPE
2031 case Lisp_Float:
2032 XMARK (XFLOAT (obj)->type);
2033 break;
2034 #endif /* LISP_FLOAT_TYPE */
2036 case Lisp_Int:
2037 break;
2039 default:
2040 abort ();
2044 /* Mark the pointers in a buffer structure. */
2046 static void
2047 mark_buffer (buf)
2048 Lisp_Object buf;
2050 register struct buffer *buffer = XBUFFER (buf);
2051 register Lisp_Object *ptr;
2052 Lisp_Object base_buffer;
2054 /* This is the buffer's markbit */
2055 mark_object (&buffer->name);
2056 XMARK (buffer->name);
2058 MARK_INTERVAL_TREE (BUF_INTERVALS (buffer));
2060 #if 0
2061 mark_object (buffer->syntax_table);
2063 /* Mark the various string-pointers in the buffer object.
2064 Since the strings may be relocated, we must mark them
2065 in their actual slots. So gc_sweep must convert each slot
2066 back to an ordinary C pointer. */
2067 XSETSTRING (*(Lisp_Object *)&buffer->upcase_table, buffer->upcase_table);
2068 mark_object ((Lisp_Object *)&buffer->upcase_table);
2069 XSETSTRING (*(Lisp_Object *)&buffer->downcase_table, buffer->downcase_table);
2070 mark_object ((Lisp_Object *)&buffer->downcase_table);
2072 XSETSTRING (*(Lisp_Object *)&buffer->sort_table, buffer->sort_table);
2073 mark_object ((Lisp_Object *)&buffer->sort_table);
2074 XSETSTRING (*(Lisp_Object *)&buffer->folding_sort_table, buffer->folding_sort_table);
2075 mark_object ((Lisp_Object *)&buffer->folding_sort_table);
2076 #endif
2078 for (ptr = &buffer->name + 1;
2079 (char *)ptr < (char *)buffer + sizeof (struct buffer);
2080 ptr++)
2081 mark_object (ptr);
2083 /* If this is an indirect buffer, mark its base buffer. */
2084 if (buffer->base_buffer && !XMARKBIT (buffer->base_buffer->name))
2086 XSETBUFFER (base_buffer, buffer->base_buffer);
2087 mark_buffer (base_buffer);
2092 /* Mark the pointers in the kboard objects. */
2094 static void
2095 mark_kboards ()
2097 KBOARD *kb;
2098 Lisp_Object *p;
2099 for (kb = all_kboards; kb; kb = kb->next_kboard)
2101 if (kb->kbd_macro_buffer)
2102 for (p = kb->kbd_macro_buffer; p < kb->kbd_macro_ptr; p++)
2103 mark_object (p);
2104 mark_object (&kb->Vprefix_arg);
2105 mark_object (&kb->kbd_queue);
2106 mark_object (&kb->Vlast_kbd_macro);
2107 mark_object (&kb->Vsystem_key_alist);
2108 mark_object (&kb->system_key_syms);
2112 /* Sweep: find all structures not marked, and free them. */
2114 static void
2115 gc_sweep ()
2117 total_string_size = 0;
2118 compact_strings ();
2120 /* Put all unmarked conses on free list */
2122 register struct cons_block *cblk;
2123 register int lim = cons_block_index;
2124 register int num_free = 0, num_used = 0;
2126 cons_free_list = 0;
2128 for (cblk = cons_block; cblk; cblk = cblk->next)
2130 register int i;
2131 for (i = 0; i < lim; i++)
2132 if (!XMARKBIT (cblk->conses[i].car))
2134 num_free++;
2135 *(struct Lisp_Cons **)&cblk->conses[i].car = cons_free_list;
2136 cons_free_list = &cblk->conses[i];
2138 else
2140 num_used++;
2141 XUNMARK (cblk->conses[i].car);
2143 lim = CONS_BLOCK_SIZE;
2145 total_conses = num_used;
2146 total_free_conses = num_free;
2149 #ifdef LISP_FLOAT_TYPE
2150 /* Put all unmarked floats on free list */
2152 register struct float_block *fblk;
2153 register int lim = float_block_index;
2154 register int num_free = 0, num_used = 0;
2156 float_free_list = 0;
2158 for (fblk = float_block; fblk; fblk = fblk->next)
2160 register int i;
2161 for (i = 0; i < lim; i++)
2162 if (!XMARKBIT (fblk->floats[i].type))
2164 num_free++;
2165 *(struct Lisp_Float **)&fblk->floats[i].type = float_free_list;
2166 float_free_list = &fblk->floats[i];
2168 else
2170 num_used++;
2171 XUNMARK (fblk->floats[i].type);
2173 lim = FLOAT_BLOCK_SIZE;
2175 total_floats = num_used;
2176 total_free_floats = num_free;
2178 #endif /* LISP_FLOAT_TYPE */
2180 #ifdef USE_TEXT_PROPERTIES
2181 /* Put all unmarked intervals on free list */
2183 register struct interval_block *iblk;
2184 register int lim = interval_block_index;
2185 register int num_free = 0, num_used = 0;
2187 interval_free_list = 0;
2189 for (iblk = interval_block; iblk; iblk = iblk->next)
2191 register int i;
2193 for (i = 0; i < lim; i++)
2195 if (! XMARKBIT (iblk->intervals[i].plist))
2197 iblk->intervals[i].parent = interval_free_list;
2198 interval_free_list = &iblk->intervals[i];
2199 num_free++;
2201 else
2203 num_used++;
2204 XUNMARK (iblk->intervals[i].plist);
2207 lim = INTERVAL_BLOCK_SIZE;
2209 total_intervals = num_used;
2210 total_free_intervals = num_free;
2212 #endif /* USE_TEXT_PROPERTIES */
2214 /* Put all unmarked symbols on free list */
2216 register struct symbol_block *sblk;
2217 register int lim = symbol_block_index;
2218 register int num_free = 0, num_used = 0;
2220 symbol_free_list = 0;
2222 for (sblk = symbol_block; sblk; sblk = sblk->next)
2224 register int i;
2225 for (i = 0; i < lim; i++)
2226 if (!XMARKBIT (sblk->symbols[i].plist))
2228 *(struct Lisp_Symbol **)&sblk->symbols[i].value = symbol_free_list;
2229 symbol_free_list = &sblk->symbols[i];
2230 num_free++;
2232 else
2234 num_used++;
2235 sblk->symbols[i].name
2236 = XSTRING (*(Lisp_Object *) &sblk->symbols[i].name);
2237 XUNMARK (sblk->symbols[i].plist);
2239 lim = SYMBOL_BLOCK_SIZE;
2241 total_symbols = num_used;
2242 total_free_symbols = num_free;
2245 #ifndef standalone
2246 /* Put all unmarked markers on free list.
2247 Unchain each one first from the buffer it points into,
2248 but only if it's a real marker. */
2250 register struct marker_block *mblk;
2251 register int lim = marker_block_index;
2252 register int num_free = 0, num_used = 0;
2254 marker_free_list = 0;
2256 for (mblk = marker_block; mblk; mblk = mblk->next)
2258 register int i;
2259 EMACS_INT already_free = -1;
2261 for (i = 0; i < lim; i++)
2263 Lisp_Object *markword;
2264 switch (mblk->markers[i].u_marker.type)
2266 case Lisp_Misc_Marker:
2267 markword = &mblk->markers[i].u_marker.chain;
2268 break;
2269 case Lisp_Misc_Buffer_Local_Value:
2270 case Lisp_Misc_Some_Buffer_Local_Value:
2271 markword = &mblk->markers[i].u_buffer_local_value.car;
2272 break;
2273 case Lisp_Misc_Overlay:
2274 markword = &mblk->markers[i].u_overlay.plist;
2275 break;
2276 case Lisp_Misc_Free:
2277 /* If the object was already free, keep it
2278 on the free list. */
2279 markword = (Lisp_Object *) &already_free;
2280 break;
2281 default:
2282 markword = 0;
2283 break;
2285 if (markword && !XMARKBIT (*markword))
2287 Lisp_Object tem;
2288 if (mblk->markers[i].u_marker.type == Lisp_Misc_Marker)
2290 /* tem1 avoids Sun compiler bug */
2291 struct Lisp_Marker *tem1 = &mblk->markers[i].u_marker;
2292 XSETMARKER (tem, tem1);
2293 unchain_marker (tem);
2295 /* Set the type of the freed object to Lisp_Misc_Free.
2296 We could leave the type alone, since nobody checks it,
2297 but this might catch bugs faster. */
2298 mblk->markers[i].u_marker.type = Lisp_Misc_Free;
2299 mblk->markers[i].u_free.chain = marker_free_list;
2300 marker_free_list = &mblk->markers[i];
2301 num_free++;
2303 else
2305 num_used++;
2306 if (markword)
2307 XUNMARK (*markword);
2310 lim = MARKER_BLOCK_SIZE;
2313 total_markers = num_used;
2314 total_free_markers = num_free;
2317 /* Free all unmarked buffers */
2319 register struct buffer *buffer = all_buffers, *prev = 0, *next;
2321 while (buffer)
2322 if (!XMARKBIT (buffer->name))
2324 if (prev)
2325 prev->next = buffer->next;
2326 else
2327 all_buffers = buffer->next;
2328 next = buffer->next;
2329 xfree (buffer);
2330 buffer = next;
2332 else
2334 XUNMARK (buffer->name);
2335 UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer));
2337 #if 0
2338 /* Each `struct Lisp_String *' was turned into a Lisp_Object
2339 for purposes of marking and relocation.
2340 Turn them back into C pointers now. */
2341 buffer->upcase_table
2342 = XSTRING (*(Lisp_Object *)&buffer->upcase_table);
2343 buffer->downcase_table
2344 = XSTRING (*(Lisp_Object *)&buffer->downcase_table);
2345 buffer->sort_table
2346 = XSTRING (*(Lisp_Object *)&buffer->sort_table);
2347 buffer->folding_sort_table
2348 = XSTRING (*(Lisp_Object *)&buffer->folding_sort_table);
2349 #endif
2351 prev = buffer, buffer = buffer->next;
2355 #endif /* standalone */
2357 /* Free all unmarked vectors */
2359 register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next;
2360 total_vector_size = 0;
2362 while (vector)
2363 if (!(vector->size & ARRAY_MARK_FLAG))
2365 if (prev)
2366 prev->next = vector->next;
2367 else
2368 all_vectors = vector->next;
2369 next = vector->next;
2370 xfree (vector);
2371 vector = next;
2373 else
2375 vector->size &= ~ARRAY_MARK_FLAG;
2376 if (vector->size & PSEUDOVECTOR_FLAG)
2377 total_vector_size += (PSEUDOVECTOR_SIZE_MASK & vector->size);
2378 else
2379 total_vector_size += vector->size;
2380 prev = vector, vector = vector->next;
2384 /* Free all "large strings" not marked with ARRAY_MARK_FLAG. */
2386 register struct string_block *sb = large_string_blocks, *prev = 0, *next;
2387 struct Lisp_String *s;
2389 while (sb)
2391 s = (struct Lisp_String *) &sb->chars[0];
2392 if (s->size & ARRAY_MARK_FLAG)
2394 ((struct Lisp_String *)(&sb->chars[0]))->size
2395 &= ~ARRAY_MARK_FLAG & ~MARKBIT;
2396 UNMARK_BALANCE_INTERVALS (s->intervals);
2397 total_string_size += ((struct Lisp_String *)(&sb->chars[0]))->size;
2398 prev = sb, sb = sb->next;
2400 else
2402 if (prev)
2403 prev->next = sb->next;
2404 else
2405 large_string_blocks = sb->next;
2406 next = sb->next;
2407 xfree (sb);
2408 sb = next;
2414 /* Compactify strings, relocate references, and free empty string blocks. */
2416 static void
2417 compact_strings ()
2419 /* String block of old strings we are scanning. */
2420 register struct string_block *from_sb;
2421 /* A preceding string block (or maybe the same one)
2422 where we are copying the still-live strings to. */
2423 register struct string_block *to_sb;
2424 int pos;
2425 int to_pos;
2427 to_sb = first_string_block;
2428 to_pos = 0;
2430 /* Scan each existing string block sequentially, string by string. */
2431 for (from_sb = first_string_block; from_sb; from_sb = from_sb->next)
2433 pos = 0;
2434 /* POS is the index of the next string in the block. */
2435 while (pos < from_sb->pos)
2437 register struct Lisp_String *nextstr
2438 = (struct Lisp_String *) &from_sb->chars[pos];
2440 register struct Lisp_String *newaddr;
2441 register EMACS_INT size = nextstr->size;
2443 /* NEXTSTR is the old address of the next string.
2444 Just skip it if it isn't marked. */
2445 if (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE)
2447 /* It is marked, so its size field is really a chain of refs.
2448 Find the end of the chain, where the actual size lives. */
2449 while (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE)
2451 if (size & DONT_COPY_FLAG)
2452 size ^= MARKBIT | DONT_COPY_FLAG;
2453 size = *(EMACS_INT *)size & ~MARKBIT;
2456 total_string_size += size;
2458 /* If it won't fit in TO_SB, close it out,
2459 and move to the next sb. Keep doing so until
2460 TO_SB reaches a large enough, empty enough string block.
2461 We know that TO_SB cannot advance past FROM_SB here
2462 since FROM_SB is large enough to contain this string.
2463 Any string blocks skipped here
2464 will be patched out and freed later. */
2465 while (to_pos + STRING_FULLSIZE (size)
2466 > max (to_sb->pos, STRING_BLOCK_SIZE))
2468 to_sb->pos = to_pos;
2469 to_sb = to_sb->next;
2470 to_pos = 0;
2472 /* Compute new address of this string
2473 and update TO_POS for the space being used. */
2474 newaddr = (struct Lisp_String *) &to_sb->chars[to_pos];
2475 to_pos += STRING_FULLSIZE (size);
2477 /* Copy the string itself to the new place. */
2478 if (nextstr != newaddr)
2479 bcopy (nextstr, newaddr, size + 1 + sizeof (EMACS_INT)
2480 + INTERVAL_PTR_SIZE);
2482 /* Go through NEXTSTR's chain of references
2483 and make each slot in the chain point to
2484 the new address of this string. */
2485 size = newaddr->size;
2486 while (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE)
2488 register Lisp_Object *objptr;
2489 if (size & DONT_COPY_FLAG)
2490 size ^= MARKBIT | DONT_COPY_FLAG;
2491 objptr = (Lisp_Object *)size;
2493 size = XFASTINT (*objptr) & ~MARKBIT;
2494 if (XMARKBIT (*objptr))
2496 XSETSTRING (*objptr, newaddr);
2497 XMARK (*objptr);
2499 else
2500 XSETSTRING (*objptr, newaddr);
2502 /* Store the actual size in the size field. */
2503 newaddr->size = size;
2505 #ifdef USE_TEXT_PROPERTIES
2506 /* Now that the string has been relocated, rebalance its
2507 interval tree, and update the tree's parent pointer. */
2508 if (! NULL_INTERVAL_P (newaddr->intervals))
2510 UNMARK_BALANCE_INTERVALS (newaddr->intervals);
2511 XSETSTRING (* (Lisp_Object *) &newaddr->intervals->parent,
2512 newaddr);
2514 #endif /* USE_TEXT_PROPERTIES */
2516 pos += STRING_FULLSIZE (size);
2520 /* Close out the last string block still used and free any that follow. */
2521 to_sb->pos = to_pos;
2522 current_string_block = to_sb;
2524 from_sb = to_sb->next;
2525 to_sb->next = 0;
2526 while (from_sb)
2528 to_sb = from_sb->next;
2529 xfree (from_sb);
2530 from_sb = to_sb;
2533 /* Free any empty string blocks further back in the chain.
2534 This loop will never free first_string_block, but it is very
2535 unlikely that that one will become empty, so why bother checking? */
2537 from_sb = first_string_block;
2538 while (to_sb = from_sb->next)
2540 if (to_sb->pos == 0)
2542 if (from_sb->next = to_sb->next)
2543 from_sb->next->prev = from_sb;
2544 xfree (to_sb);
2546 else
2547 from_sb = to_sb;
2551 /* Debugging aids. */
2553 DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0,
2554 "Return the address of the last byte Emacs has allocated, divided by 1024.\n\
2555 This may be helpful in debugging Emacs's memory usage.\n\
2556 We divide the value by 1024 to make sure it fits in a Lisp integer.")
2559 Lisp_Object end;
2561 XSETINT (end, (EMACS_INT) sbrk (0) / 1024);
2563 return end;
2566 DEFUN ("memory-use-counts", Fmemory_use_counts, Smemory_use_counts, 0, 0, 0,
2567 "Return a list of counters that measure how much consing there has been.\n\
2568 Each of these counters increments for a certain kind of object.\n\
2569 The counters wrap around from the largest positive integer to zero.\n\
2570 Garbage collection does not decrease them.\n\
2571 The elements of the value are as follows:\n\
2572 (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS)\n\
2573 All are in units of 1 = one object consed\n\
2574 except for VECTOR-CELLS and STRING-CHARS, which count the total length of\n\
2575 objects consed.\n\
2576 MISCS include overlays, markers, and some internal types.\n\
2577 Frames, windows, buffers, and subprocesses count as vectors\n\
2578 (but the contents of a buffer's text do not count here).")
2581 Lisp_Object lisp_cons_cells_consed;
2582 Lisp_Object lisp_floats_consed;
2583 Lisp_Object lisp_vector_cells_consed;
2584 Lisp_Object lisp_symbols_consed;
2585 Lisp_Object lisp_string_chars_consed;
2586 Lisp_Object lisp_misc_objects_consed;
2587 Lisp_Object lisp_intervals_consed;
2589 XSETINT (lisp_cons_cells_consed,
2590 cons_cells_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2591 XSETINT (lisp_floats_consed,
2592 floats_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2593 XSETINT (lisp_vector_cells_consed,
2594 vector_cells_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2595 XSETINT (lisp_symbols_consed,
2596 symbols_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2597 XSETINT (lisp_string_chars_consed,
2598 string_chars_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2599 XSETINT (lisp_misc_objects_consed,
2600 misc_objects_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2601 XSETINT (lisp_intervals_consed,
2602 intervals_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2604 return Fcons (lisp_cons_cells_consed,
2605 Fcons (lisp_floats_consed,
2606 Fcons (lisp_vector_cells_consed,
2607 Fcons (lisp_symbols_consed,
2608 Fcons (lisp_string_chars_consed,
2609 Fcons (lisp_misc_objects_consed,
2610 Fcons (lisp_intervals_consed,
2611 Qnil)))))));
2614 /* Initialization */
2616 init_alloc_once ()
2618 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
2619 pureptr = 0;
2620 #ifdef HAVE_SHM
2621 pure_size = PURESIZE;
2622 #endif
2623 all_vectors = 0;
2624 ignore_warnings = 1;
2625 #ifdef DOUG_LEA_MALLOC
2626 mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */
2627 mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */
2628 mallopt (M_MMAP_MAX, 64); /* max. number of mmap'ed areas */
2629 #endif
2630 init_strings ();
2631 init_cons ();
2632 init_symbol ();
2633 init_marker ();
2634 #ifdef LISP_FLOAT_TYPE
2635 init_float ();
2636 #endif /* LISP_FLOAT_TYPE */
2637 INIT_INTERVALS;
2639 #ifdef REL_ALLOC
2640 malloc_hysteresis = 32;
2641 #else
2642 malloc_hysteresis = 0;
2643 #endif
2645 spare_memory = (char *) malloc (SPARE_MEMORY);
2647 ignore_warnings = 0;
2648 gcprolist = 0;
2649 staticidx = 0;
2650 consing_since_gc = 0;
2651 gc_cons_threshold = 100000 * sizeof (Lisp_Object);
2652 #ifdef VIRT_ADDR_VARIES
2653 malloc_sbrk_unused = 1<<22; /* A large number */
2654 malloc_sbrk_used = 100000; /* as reasonable as any number */
2655 #endif /* VIRT_ADDR_VARIES */
2658 init_alloc ()
2660 gcprolist = 0;
2663 void
2664 syms_of_alloc ()
2666 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold,
2667 "*Number of bytes of consing between garbage collections.\n\
2668 Garbage collection can happen automatically once this many bytes have been\n\
2669 allocated since the last garbage collection. All data types count.\n\n\
2670 Garbage collection happens automatically only when `eval' is called.\n\n\
2671 By binding this temporarily to a large number, you can effectively\n\
2672 prevent garbage collection during a part of the program.");
2674 DEFVAR_INT ("pure-bytes-used", &pureptr,
2675 "Number of bytes of sharable Lisp data allocated so far.");
2677 DEFVAR_INT ("cons-cells-consed", &cons_cells_consed,
2678 "Number of cons cells that have been consed so far.");
2680 DEFVAR_INT ("floats-consed", &floats_consed,
2681 "Number of floats that have been consed so far.");
2683 DEFVAR_INT ("vector-cells-consed", &vector_cells_consed,
2684 "Number of vector cells that have been consed so far.");
2686 DEFVAR_INT ("symbols-consed", &symbols_consed,
2687 "Number of symbols that have been consed so far.");
2689 DEFVAR_INT ("string-chars-consed", &string_chars_consed,
2690 "Number of string characters that have been consed so far.");
2692 DEFVAR_INT ("misc-objects-consed", &misc_objects_consed,
2693 "Number of miscellaneous objects that have been consed so far.");
2695 DEFVAR_INT ("intervals-consed", &intervals_consed,
2696 "Number of intervals that have been consed so far.");
2698 #if 0
2699 DEFVAR_INT ("data-bytes-used", &malloc_sbrk_used,
2700 "Number of bytes of unshared memory allocated in this session.");
2702 DEFVAR_INT ("data-bytes-free", &malloc_sbrk_unused,
2703 "Number of bytes of unshared memory remaining available in this session.");
2704 #endif
2706 DEFVAR_LISP ("purify-flag", &Vpurify_flag,
2707 "Non-nil means loading Lisp code in order to dump an executable.\n\
2708 This means that certain objects should be allocated in shared (pure) space.");
2710 DEFVAR_INT ("undo-limit", &undo_limit,
2711 "Keep no more undo information once it exceeds this size.\n\
2712 This limit is applied when garbage collection happens.\n\
2713 The size is counted as the number of bytes occupied,\n\
2714 which includes both saved text and other data.");
2715 undo_limit = 20000;
2717 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit,
2718 "Don't keep more than this much size of undo information.\n\
2719 A command which pushes past this size is itself forgotten.\n\
2720 This limit is applied when garbage collection happens.\n\
2721 The size is counted as the number of bytes occupied,\n\
2722 which includes both saved text and other data.");
2723 undo_strong_limit = 30000;
2725 DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages,
2726 "Non-nil means display messages at start and end of garbage collection.");
2727 garbage_collection_messages = 0;
2729 /* We build this in advance because if we wait until we need it, we might
2730 not be able to allocate the memory to hold it. */
2731 memory_signal_data
2732 = Fcons (Qerror, Fcons (build_string ("Memory exhausted--use M-x save-some-buffers RET"), Qnil));
2733 staticpro (&memory_signal_data);
2735 staticpro (&Qgc_cons_threshold);
2736 Qgc_cons_threshold = intern ("gc-cons-threshold");
2738 staticpro (&Qchar_table_extra_slots);
2739 Qchar_table_extra_slots = intern ("char-table-extra-slots");
2741 defsubr (&Scons);
2742 defsubr (&Slist);
2743 defsubr (&Svector);
2744 defsubr (&Smake_byte_code);
2745 defsubr (&Smake_list);
2746 defsubr (&Smake_vector);
2747 defsubr (&Smake_char_table);
2748 defsubr (&Smake_string);
2749 defsubr (&Smake_bool_vector);
2750 defsubr (&Smake_symbol);
2751 defsubr (&Smake_marker);
2752 defsubr (&Spurecopy);
2753 defsubr (&Sgarbage_collect);
2754 defsubr (&Smemory_limit);
2755 defsubr (&Smemory_use_counts);