For glibc's malloc, include <malloc.h> for mallinfo,
[emacs.git] / src / alloc.c
blob3468e8a9e3e67fa5a7fb49f852b7c435c8a4c0f9
1 /* Storage allocation and gc for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 86, 88, 93, 94, 95 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 /* Points to memory space allocated as "spare",
116 to be freed if we run out of memory. */
117 static char *spare_memory;
119 /* Amount of spare memory to keep in reserve. */
120 #define SPARE_MEMORY (1 << 14)
122 /* Number of extra blocks malloc should get when it needs more core. */
123 static int malloc_hysteresis;
125 /* Nonzero when malloc is called for allocating Lisp object space. */
126 int allocating_for_lisp;
128 /* Non-nil means defun should do purecopy on the function definition */
129 Lisp_Object Vpurify_flag;
131 #ifndef HAVE_SHM
132 EMACS_INT pure[PURESIZE / sizeof (EMACS_INT)] = {0,}; /* Force it into data space! */
133 #define PUREBEG (char *) pure
134 #else
135 #define pure PURE_SEG_BITS /* Use shared memory segment */
136 #define PUREBEG (char *)PURE_SEG_BITS
138 /* This variable is used only by the XPNTR macro when HAVE_SHM is
139 defined. If we used the PURESIZE macro directly there, that would
140 make most of emacs dependent on puresize.h, which we don't want -
141 you should be able to change that without too much recompilation.
142 So map_in_data initializes pure_size, and the dependencies work
143 out. */
144 EMACS_INT pure_size;
145 #endif /* not HAVE_SHM */
147 /* Index in pure at which next pure object will be allocated. */
148 int pureptr;
150 /* If nonzero, this is a warning delivered by malloc and not yet displayed. */
151 char *pending_malloc_warning;
153 /* Pre-computed signal argument for use when memory is exhausted. */
154 Lisp_Object memory_signal_data;
156 /* Maximum amount of C stack to save when a GC happens. */
158 #ifndef MAX_SAVE_STACK
159 #define MAX_SAVE_STACK 16000
160 #endif
162 /* Define DONT_COPY_FLAG to be some bit which will always be zero in a
163 pointer to a Lisp_Object, when that pointer is viewed as an integer.
164 (On most machines, pointers are even, so we can use the low bit.
165 Word-addressable architectures may need to override this in the m-file.)
166 When linking references to small strings through the size field, we
167 use this slot to hold the bit that would otherwise be interpreted as
168 the GC mark bit. */
169 #ifndef DONT_COPY_FLAG
170 #define DONT_COPY_FLAG 1
171 #endif /* no DONT_COPY_FLAG */
173 /* Buffer in which we save a copy of the C stack at each GC. */
175 char *stack_copy;
176 int stack_copy_size;
178 /* Non-zero means ignore malloc warnings. Set during initialization. */
179 int ignore_warnings;
181 Lisp_Object Qgc_cons_threshold, Qchar_table_extra_slots;
183 static void mark_object (), mark_buffer (), mark_kboards ();
184 static void clear_marks (), gc_sweep ();
185 static void compact_strings ();
187 /* Versions of malloc and realloc that print warnings as memory gets full. */
189 Lisp_Object
190 malloc_warning_1 (str)
191 Lisp_Object str;
193 Fprinc (str, Vstandard_output);
194 write_string ("\nKilling some buffers may delay running out of memory.\n", -1);
195 write_string ("However, certainly by the time you receive the 95% warning,\n", -1);
196 write_string ("you should clean up, kill this Emacs, and start a new one.", -1);
197 return Qnil;
200 /* malloc calls this if it finds we are near exhausting storage */
201 malloc_warning (str)
202 char *str;
204 pending_malloc_warning = str;
207 display_malloc_warning ()
209 register Lisp_Object val;
211 val = build_string (pending_malloc_warning);
212 pending_malloc_warning = 0;
213 internal_with_output_to_temp_buffer (" *Danger*", malloc_warning_1, val);
216 #ifdef DOUG_LEA_MALLOC
217 #define BYTES_USED (mallinfo ().arena)
218 #else
219 #define BYTES_USED _bytes_used
220 #endif
222 /* Called if malloc returns zero */
224 memory_full ()
226 #ifndef SYSTEM_MALLOC
227 bytes_used_when_full = BYTES_USED;
228 #endif
230 /* The first time we get here, free the spare memory. */
231 if (spare_memory)
233 free (spare_memory);
234 spare_memory = 0;
237 /* This used to call error, but if we've run out of memory, we could get
238 infinite recursion trying to build the string. */
239 while (1)
240 Fsignal (Qerror, memory_signal_data);
243 /* Called if we can't allocate relocatable space for a buffer. */
245 void
246 buffer_memory_full ()
248 /* If buffers use the relocating allocator,
249 no need to free spare_memory, because we may have plenty of malloc
250 space left that we could get, and if we don't, the malloc that fails
251 will itself cause spare_memory to be freed.
252 If buffers don't use the relocating allocator,
253 treat this like any other failing malloc. */
255 #ifndef REL_ALLOC
256 memory_full ();
257 #endif
259 /* This used to call error, but if we've run out of memory, we could get
260 infinite recursion trying to build the string. */
261 while (1)
262 Fsignal (Qerror, memory_signal_data);
265 /* like malloc routines but check for no memory and block interrupt input. */
267 long *
268 xmalloc (size)
269 int size;
271 register long *val;
273 BLOCK_INPUT;
274 val = (long *) malloc (size);
275 UNBLOCK_INPUT;
277 if (!val && size) memory_full ();
278 return val;
281 long *
282 xrealloc (block, size)
283 long *block;
284 int size;
286 register long *val;
288 BLOCK_INPUT;
289 /* We must call malloc explicitly when BLOCK is 0, since some
290 reallocs don't do this. */
291 if (! block)
292 val = (long *) malloc (size);
293 else
294 val = (long *) realloc (block, size);
295 UNBLOCK_INPUT;
297 if (!val && size) memory_full ();
298 return val;
301 void
302 xfree (block)
303 long *block;
305 BLOCK_INPUT;
306 free (block);
307 UNBLOCK_INPUT;
311 /* Arranging to disable input signals while we're in malloc.
313 This only works with GNU malloc. To help out systems which can't
314 use GNU malloc, all the calls to malloc, realloc, and free
315 elsewhere in the code should be inside a BLOCK_INPUT/UNBLOCK_INPUT
316 pairs; unfortunately, we have no idea what C library functions
317 might call malloc, so we can't really protect them unless you're
318 using GNU malloc. Fortunately, most of the major operating can use
319 GNU malloc. */
321 #ifndef SYSTEM_MALLOC
322 extern void * (*__malloc_hook) ();
323 static void * (*old_malloc_hook) ();
324 extern void * (*__realloc_hook) ();
325 static void * (*old_realloc_hook) ();
326 extern void (*__free_hook) ();
327 static void (*old_free_hook) ();
329 /* This function is used as the hook for free to call. */
331 static void
332 emacs_blocked_free (ptr)
333 void *ptr;
335 BLOCK_INPUT;
336 __free_hook = old_free_hook;
337 free (ptr);
338 /* If we released our reserve (due to running out of memory),
339 and we have a fair amount free once again,
340 try to set aside another reserve in case we run out once more. */
341 if (spare_memory == 0
342 /* Verify there is enough space that even with the malloc
343 hysteresis this call won't run out again.
344 The code here is correct as long as SPARE_MEMORY
345 is substantially larger than the block size malloc uses. */
346 && (bytes_used_when_full
347 > BYTES_USED + max (malloc_hysteresis, 4) * SPARE_MEMORY))
348 spare_memory = (char *) malloc (SPARE_MEMORY);
350 __free_hook = emacs_blocked_free;
351 UNBLOCK_INPUT;
354 /* If we released our reserve (due to running out of memory),
355 and we have a fair amount free once again,
356 try to set aside another reserve in case we run out once more.
358 This is called when a relocatable block is freed in ralloc.c. */
360 void
361 refill_memory_reserve ()
363 if (spare_memory == 0)
364 spare_memory = (char *) malloc (SPARE_MEMORY);
367 /* This function is the malloc hook that Emacs uses. */
369 static void *
370 emacs_blocked_malloc (size)
371 unsigned size;
373 void *value;
375 BLOCK_INPUT;
376 __malloc_hook = old_malloc_hook;
377 #ifdef DOUG_LEA_MALLOC
378 mallopt (M_TOP_PAD, malloc_hysteresis * 4096);
379 #else
380 __malloc_extra_blocks = malloc_hysteresis;
381 #endif
382 value = (void *) malloc (size);
383 __malloc_hook = emacs_blocked_malloc;
384 UNBLOCK_INPUT;
386 return value;
389 static void *
390 emacs_blocked_realloc (ptr, size)
391 void *ptr;
392 unsigned size;
394 void *value;
396 BLOCK_INPUT;
397 __realloc_hook = old_realloc_hook;
398 value = (void *) realloc (ptr, size);
399 __realloc_hook = emacs_blocked_realloc;
400 UNBLOCK_INPUT;
402 return value;
405 void
406 uninterrupt_malloc ()
408 old_free_hook = __free_hook;
409 __free_hook = emacs_blocked_free;
411 old_malloc_hook = __malloc_hook;
412 __malloc_hook = emacs_blocked_malloc;
414 old_realloc_hook = __realloc_hook;
415 __realloc_hook = emacs_blocked_realloc;
417 #endif
419 /* Interval allocation. */
421 #ifdef USE_TEXT_PROPERTIES
422 #define INTERVAL_BLOCK_SIZE \
423 ((1020 - sizeof (struct interval_block *)) / sizeof (struct interval))
425 struct interval_block
427 struct interval_block *next;
428 struct interval intervals[INTERVAL_BLOCK_SIZE];
431 struct interval_block *interval_block;
432 static int interval_block_index;
434 INTERVAL interval_free_list;
436 static void
437 init_intervals ()
439 allocating_for_lisp = 1;
440 interval_block
441 = (struct interval_block *) malloc (sizeof (struct interval_block));
442 allocating_for_lisp = 0;
443 interval_block->next = 0;
444 bzero ((char *) interval_block->intervals, sizeof interval_block->intervals);
445 interval_block_index = 0;
446 interval_free_list = 0;
449 #define INIT_INTERVALS init_intervals ()
451 INTERVAL
452 make_interval ()
454 INTERVAL val;
456 if (interval_free_list)
458 val = interval_free_list;
459 interval_free_list = interval_free_list->parent;
461 else
463 if (interval_block_index == INTERVAL_BLOCK_SIZE)
465 register struct interval_block *newi;
467 allocating_for_lisp = 1;
468 newi = (struct interval_block *) xmalloc (sizeof (struct interval_block));
470 allocating_for_lisp = 0;
471 VALIDATE_LISP_STORAGE (newi, sizeof *newi);
472 newi->next = interval_block;
473 interval_block = newi;
474 interval_block_index = 0;
476 val = &interval_block->intervals[interval_block_index++];
478 consing_since_gc += sizeof (struct interval);
479 intervals_consed++;
480 RESET_INTERVAL (val);
481 return val;
484 static int total_free_intervals, total_intervals;
486 /* Mark the pointers of one interval. */
488 static void
489 mark_interval (i, dummy)
490 register INTERVAL i;
491 Lisp_Object dummy;
493 if (XMARKBIT (i->plist))
494 abort ();
495 mark_object (&i->plist);
496 XMARK (i->plist);
499 static void
500 mark_interval_tree (tree)
501 register INTERVAL tree;
503 /* No need to test if this tree has been marked already; this
504 function is always called through the MARK_INTERVAL_TREE macro,
505 which takes care of that. */
507 /* XMARK expands to an assignment; the LHS of an assignment can't be
508 a cast. */
509 XMARK (* (Lisp_Object *) &tree->parent);
511 traverse_intervals (tree, 1, 0, mark_interval, Qnil);
514 #define MARK_INTERVAL_TREE(i) \
515 do { \
516 if (!NULL_INTERVAL_P (i) \
517 && ! XMARKBIT ((Lisp_Object) i->parent)) \
518 mark_interval_tree (i); \
519 } while (0)
521 /* The oddity in the call to XUNMARK is necessary because XUNMARK
522 expands to an assignment to its argument, and most C compilers don't
523 support casts on the left operand of `='. */
524 #define UNMARK_BALANCE_INTERVALS(i) \
526 if (! NULL_INTERVAL_P (i)) \
528 XUNMARK (* (Lisp_Object *) (&(i)->parent)); \
529 (i) = balance_intervals (i); \
533 #else /* no interval use */
535 #define INIT_INTERVALS
537 #define UNMARK_BALANCE_INTERVALS(i)
538 #define MARK_INTERVAL_TREE(i)
540 #endif /* no interval use */
542 /* Floating point allocation. */
544 #ifdef LISP_FLOAT_TYPE
545 /* Allocation of float cells, just like conses */
546 /* We store float cells inside of float_blocks, allocating a new
547 float_block with malloc whenever necessary. Float cells reclaimed by
548 GC are put on a free list to be reallocated before allocating
549 any new float cells from the latest float_block.
551 Each float_block is just under 1020 bytes long,
552 since malloc really allocates in units of powers of two
553 and uses 4 bytes for its own overhead. */
555 #define FLOAT_BLOCK_SIZE \
556 ((1020 - sizeof (struct float_block *)) / sizeof (struct Lisp_Float))
558 struct float_block
560 struct float_block *next;
561 struct Lisp_Float floats[FLOAT_BLOCK_SIZE];
564 struct float_block *float_block;
565 int float_block_index;
567 struct Lisp_Float *float_free_list;
569 void
570 init_float ()
572 allocating_for_lisp = 1;
573 float_block = (struct float_block *) malloc (sizeof (struct float_block));
574 allocating_for_lisp = 0;
575 float_block->next = 0;
576 bzero ((char *) float_block->floats, sizeof float_block->floats);
577 float_block_index = 0;
578 float_free_list = 0;
581 /* Explicitly free a float cell. */
582 free_float (ptr)
583 struct Lisp_Float *ptr;
585 *(struct Lisp_Float **)&ptr->type = float_free_list;
586 float_free_list = ptr;
589 Lisp_Object
590 make_float (float_value)
591 double float_value;
593 register Lisp_Object val;
595 if (float_free_list)
597 XSETFLOAT (val, float_free_list);
598 float_free_list = *(struct Lisp_Float **)&float_free_list->type;
600 else
602 if (float_block_index == FLOAT_BLOCK_SIZE)
604 register struct float_block *new;
606 allocating_for_lisp = 1;
607 new = (struct float_block *) xmalloc (sizeof (struct float_block));
608 allocating_for_lisp = 0;
609 VALIDATE_LISP_STORAGE (new, sizeof *new);
610 new->next = float_block;
611 float_block = new;
612 float_block_index = 0;
614 XSETFLOAT (val, &float_block->floats[float_block_index++]);
616 XFLOAT (val)->data = float_value;
617 XSETFASTINT (XFLOAT (val)->type, 0); /* bug chasing -wsr */
618 consing_since_gc += sizeof (struct Lisp_Float);
619 floats_consed++;
620 return val;
623 #endif /* LISP_FLOAT_TYPE */
625 /* Allocation of cons cells */
626 /* We store cons cells inside of cons_blocks, allocating a new
627 cons_block with malloc whenever necessary. Cons cells reclaimed by
628 GC are put on a free list to be reallocated before allocating
629 any new cons cells from the latest cons_block.
631 Each cons_block is just under 1020 bytes long,
632 since malloc really allocates in units of powers of two
633 and uses 4 bytes for its own overhead. */
635 #define CONS_BLOCK_SIZE \
636 ((1020 - sizeof (struct cons_block *)) / sizeof (struct Lisp_Cons))
638 struct cons_block
640 struct cons_block *next;
641 struct Lisp_Cons conses[CONS_BLOCK_SIZE];
644 struct cons_block *cons_block;
645 int cons_block_index;
647 struct Lisp_Cons *cons_free_list;
649 void
650 init_cons ()
652 allocating_for_lisp = 1;
653 cons_block = (struct cons_block *) malloc (sizeof (struct cons_block));
654 allocating_for_lisp = 0;
655 cons_block->next = 0;
656 bzero ((char *) cons_block->conses, sizeof cons_block->conses);
657 cons_block_index = 0;
658 cons_free_list = 0;
661 /* Explicitly free a cons cell. */
662 free_cons (ptr)
663 struct Lisp_Cons *ptr;
665 *(struct Lisp_Cons **)&ptr->car = cons_free_list;
666 cons_free_list = ptr;
669 DEFUN ("cons", Fcons, Scons, 2, 2, 0,
670 "Create a new cons, give it CAR and CDR as components, and return it.")
671 (car, cdr)
672 Lisp_Object car, cdr;
674 register Lisp_Object val;
676 if (cons_free_list)
678 XSETCONS (val, cons_free_list);
679 cons_free_list = *(struct Lisp_Cons **)&cons_free_list->car;
681 else
683 if (cons_block_index == CONS_BLOCK_SIZE)
685 register struct cons_block *new;
686 allocating_for_lisp = 1;
687 new = (struct cons_block *) xmalloc (sizeof (struct cons_block));
688 allocating_for_lisp = 0;
689 VALIDATE_LISP_STORAGE (new, sizeof *new);
690 new->next = cons_block;
691 cons_block = new;
692 cons_block_index = 0;
694 XSETCONS (val, &cons_block->conses[cons_block_index++]);
696 XCONS (val)->car = car;
697 XCONS (val)->cdr = cdr;
698 consing_since_gc += sizeof (struct Lisp_Cons);
699 cons_cells_consed++;
700 return val;
703 DEFUN ("list", Flist, Slist, 0, MANY, 0,
704 "Return a newly created list with specified arguments as elements.\n\
705 Any number of arguments, even zero arguments, are allowed.")
706 (nargs, args)
707 int nargs;
708 register Lisp_Object *args;
710 register Lisp_Object val;
711 val = Qnil;
713 while (nargs > 0)
715 nargs--;
716 val = Fcons (args[nargs], val);
718 return val;
721 DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0,
722 "Return a newly created list of length LENGTH, with each element being INIT.")
723 (length, init)
724 register Lisp_Object length, init;
726 register Lisp_Object val;
727 register int size;
729 CHECK_NATNUM (length, 0);
730 size = XFASTINT (length);
732 val = Qnil;
733 while (size-- > 0)
734 val = Fcons (init, val);
735 return val;
738 /* Allocation of vectors */
740 struct Lisp_Vector *all_vectors;
742 struct Lisp_Vector *
743 allocate_vectorlike (len)
744 EMACS_INT len;
746 struct Lisp_Vector *p;
748 allocating_for_lisp = 1;
749 #ifdef DOUG_LEA_MALLOC
750 /* Prevent mmap'ing the chunk (which is potentially very large). */
751 mallopt (M_MMAP_MAX, 0);
752 #endif
753 p = (struct Lisp_Vector *)xmalloc (sizeof (struct Lisp_Vector)
754 + (len - 1) * sizeof (Lisp_Object));
755 #ifdef DOUG_LEA_MALLOC
756 /* Back to a reasonable maximum of mmap'ed areas. */
757 mallopt (M_MMAP_MAX, 64);
758 #endif
759 allocating_for_lisp = 0;
760 VALIDATE_LISP_STORAGE (p, 0);
761 consing_since_gc += (sizeof (struct Lisp_Vector)
762 + (len - 1) * sizeof (Lisp_Object));
763 vector_cells_consed += len;
765 p->next = all_vectors;
766 all_vectors = p;
767 return p;
770 DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0,
771 "Return a newly created vector of length LENGTH, with each element being INIT.\n\
772 See also the function `vector'.")
773 (length, init)
774 register Lisp_Object length, init;
776 Lisp_Object vector;
777 register EMACS_INT sizei;
778 register int index;
779 register struct Lisp_Vector *p;
781 CHECK_NATNUM (length, 0);
782 sizei = XFASTINT (length);
784 p = allocate_vectorlike (sizei);
785 p->size = sizei;
786 for (index = 0; index < sizei; index++)
787 p->contents[index] = init;
789 XSETVECTOR (vector, p);
790 return vector;
793 DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0,
794 "Return a newly created char-table, with purpose PURPOSE.\n\
795 Each element is initialized to INIT, which defaults to nil.\n\
796 PURPOSE should be a symbol which has a `char-table-extra-slots' property.\n\
797 The property's value should be an integer between 0 and 10.")
798 (purpose, init)
799 register Lisp_Object purpose, init;
801 Lisp_Object vector;
802 Lisp_Object n;
803 CHECK_SYMBOL (purpose, 1);
804 n = Fget (purpose, Qchar_table_extra_slots);
805 CHECK_NUMBER (n, 0);
806 if (XINT (n) < 0 || XINT (n) > 10)
807 args_out_of_range (n, Qnil);
808 /* Add 2 to the size for the defalt and parent slots. */
809 vector = Fmake_vector (make_number (CHAR_TABLE_STANDARD_SLOTS + XINT (n)),
810 init);
811 XCHAR_TABLE (vector)->top = Qt;
812 XCHAR_TABLE (vector)->parent = Qnil;
813 XCHAR_TABLE (vector)->purpose = purpose;
814 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
815 return vector;
818 /* Return a newly created sub char table with default value DEFALT.
819 Since a sub char table does not appear as a top level Emacs Lisp
820 object, we don't need a Lisp interface to make it. */
822 Lisp_Object
823 make_sub_char_table (defalt)
824 Lisp_Object defalt;
826 Lisp_Object vector
827 = Fmake_vector (make_number (SUB_CHAR_TABLE_STANDARD_SLOTS), Qnil);
828 XCHAR_TABLE (vector)->top = Qnil;
829 XCHAR_TABLE (vector)->defalt = defalt;
830 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
831 return vector;
834 DEFUN ("vector", Fvector, Svector, 0, MANY, 0,
835 "Return a newly created vector with specified arguments as elements.\n\
836 Any number of arguments, even zero arguments, are allowed.")
837 (nargs, args)
838 register int nargs;
839 Lisp_Object *args;
841 register Lisp_Object len, val;
842 register int index;
843 register struct Lisp_Vector *p;
845 XSETFASTINT (len, nargs);
846 val = Fmake_vector (len, Qnil);
847 p = XVECTOR (val);
848 for (index = 0; index < nargs; index++)
849 p->contents[index] = args[index];
850 return val;
853 DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0,
854 "Create a byte-code object with specified arguments as elements.\n\
855 The arguments should be the arglist, bytecode-string, constant vector,\n\
856 stack size, (optional) doc string, and (optional) interactive spec.\n\
857 The first four arguments are required; at most six have any\n\
858 significance.")
859 (nargs, args)
860 register int nargs;
861 Lisp_Object *args;
863 register Lisp_Object len, val;
864 register int index;
865 register struct Lisp_Vector *p;
867 XSETFASTINT (len, nargs);
868 if (!NILP (Vpurify_flag))
869 val = make_pure_vector ((EMACS_INT) nargs);
870 else
871 val = Fmake_vector (len, Qnil);
872 p = XVECTOR (val);
873 for (index = 0; index < nargs; index++)
875 if (!NILP (Vpurify_flag))
876 args[index] = Fpurecopy (args[index]);
877 p->contents[index] = args[index];
879 XSETCOMPILED (val, val);
880 return val;
883 /* Allocation of symbols.
884 Just like allocation of conses!
886 Each symbol_block is just under 1020 bytes long,
887 since malloc really allocates in units of powers of two
888 and uses 4 bytes for its own overhead. */
890 #define SYMBOL_BLOCK_SIZE \
891 ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol))
893 struct symbol_block
895 struct symbol_block *next;
896 struct Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE];
899 struct symbol_block *symbol_block;
900 int symbol_block_index;
902 struct Lisp_Symbol *symbol_free_list;
904 void
905 init_symbol ()
907 allocating_for_lisp = 1;
908 symbol_block = (struct symbol_block *) malloc (sizeof (struct symbol_block));
909 allocating_for_lisp = 0;
910 symbol_block->next = 0;
911 bzero ((char *) symbol_block->symbols, sizeof symbol_block->symbols);
912 symbol_block_index = 0;
913 symbol_free_list = 0;
916 DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0,
917 "Return a newly allocated uninterned symbol whose name is NAME.\n\
918 Its value and function definition are void, and its property list is nil.")
919 (name)
920 Lisp_Object name;
922 register Lisp_Object val;
923 register struct Lisp_Symbol *p;
925 CHECK_STRING (name, 0);
927 if (symbol_free_list)
929 XSETSYMBOL (val, symbol_free_list);
930 symbol_free_list = *(struct Lisp_Symbol **)&symbol_free_list->value;
932 else
934 if (symbol_block_index == SYMBOL_BLOCK_SIZE)
936 struct symbol_block *new;
937 allocating_for_lisp = 1;
938 new = (struct symbol_block *) xmalloc (sizeof (struct symbol_block));
939 allocating_for_lisp = 0;
940 VALIDATE_LISP_STORAGE (new, sizeof *new);
941 new->next = symbol_block;
942 symbol_block = new;
943 symbol_block_index = 0;
945 XSETSYMBOL (val, &symbol_block->symbols[symbol_block_index++]);
947 p = XSYMBOL (val);
948 p->name = XSTRING (name);
949 p->obarray = Qnil;
950 p->plist = Qnil;
951 p->value = Qunbound;
952 p->function = Qunbound;
953 p->next = 0;
954 consing_since_gc += sizeof (struct Lisp_Symbol);
955 symbols_consed++;
956 return val;
959 /* Allocation of markers and other objects that share that structure.
960 Works like allocation of conses. */
962 #define MARKER_BLOCK_SIZE \
963 ((1020 - sizeof (struct marker_block *)) / sizeof (union Lisp_Misc))
965 struct marker_block
967 struct marker_block *next;
968 union Lisp_Misc markers[MARKER_BLOCK_SIZE];
971 struct marker_block *marker_block;
972 int marker_block_index;
974 union Lisp_Misc *marker_free_list;
976 void
977 init_marker ()
979 allocating_for_lisp = 1;
980 marker_block = (struct marker_block *) malloc (sizeof (struct marker_block));
981 allocating_for_lisp = 0;
982 marker_block->next = 0;
983 bzero ((char *) marker_block->markers, sizeof marker_block->markers);
984 marker_block_index = 0;
985 marker_free_list = 0;
988 /* Return a newly allocated Lisp_Misc object, with no substructure. */
989 Lisp_Object
990 allocate_misc ()
992 Lisp_Object val;
994 if (marker_free_list)
996 XSETMISC (val, marker_free_list);
997 marker_free_list = marker_free_list->u_free.chain;
999 else
1001 if (marker_block_index == MARKER_BLOCK_SIZE)
1003 struct marker_block *new;
1004 allocating_for_lisp = 1;
1005 new = (struct marker_block *) xmalloc (sizeof (struct marker_block));
1006 allocating_for_lisp = 0;
1007 VALIDATE_LISP_STORAGE (new, sizeof *new);
1008 new->next = marker_block;
1009 marker_block = new;
1010 marker_block_index = 0;
1012 XSETMISC (val, &marker_block->markers[marker_block_index++]);
1014 consing_since_gc += sizeof (union Lisp_Misc);
1015 misc_objects_consed++;
1016 return val;
1019 DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
1020 "Return a newly allocated marker which does not point at any place.")
1023 register Lisp_Object val;
1024 register struct Lisp_Marker *p;
1026 val = allocate_misc ();
1027 XMISCTYPE (val) = Lisp_Misc_Marker;
1028 p = XMARKER (val);
1029 p->buffer = 0;
1030 p->bufpos = 0;
1031 p->chain = Qnil;
1032 p->insertion_type = 0;
1033 return val;
1036 /* Allocation of strings */
1038 /* Strings reside inside of string_blocks. The entire data of the string,
1039 both the size and the contents, live in part of the `chars' component of a string_block.
1040 The `pos' component is the index within `chars' of the first free byte.
1042 first_string_block points to the first string_block ever allocated.
1043 Each block points to the next one with its `next' field.
1044 The `prev' fields chain in reverse order.
1045 The last one allocated is the one currently being filled.
1046 current_string_block points to it.
1048 The string_blocks that hold individual large strings
1049 go in a separate chain, started by large_string_blocks. */
1052 /* String blocks contain this many useful bytes.
1053 8188 is power of 2, minus 4 for malloc overhead. */
1054 #define STRING_BLOCK_SIZE (8188 - sizeof (struct string_block_head))
1056 /* A string bigger than this gets its own specially-made string block
1057 if it doesn't fit in the current one. */
1058 #define STRING_BLOCK_OUTSIZE 1024
1060 struct string_block_head
1062 struct string_block *next, *prev;
1063 EMACS_INT pos;
1066 struct string_block
1068 struct string_block *next, *prev;
1069 EMACS_INT pos;
1070 char chars[STRING_BLOCK_SIZE];
1073 /* This points to the string block we are now allocating strings. */
1075 struct string_block *current_string_block;
1077 /* This points to the oldest string block, the one that starts the chain. */
1079 struct string_block *first_string_block;
1081 /* Last string block in chain of those made for individual large strings. */
1083 struct string_block *large_string_blocks;
1085 /* If SIZE is the length of a string, this returns how many bytes
1086 the string occupies in a string_block (including padding). */
1088 #define STRING_FULLSIZE(size) (((size) + sizeof (struct Lisp_String) + PAD) \
1089 & ~(PAD - 1))
1090 #define PAD (sizeof (EMACS_INT))
1092 #if 0
1093 #define STRING_FULLSIZE(SIZE) \
1094 (((SIZE) + 2 * sizeof (EMACS_INT)) & ~(sizeof (EMACS_INT) - 1))
1095 #endif
1097 void
1098 init_strings ()
1100 allocating_for_lisp = 1;
1101 current_string_block = (struct string_block *) malloc (sizeof (struct string_block));
1102 allocating_for_lisp = 0;
1103 first_string_block = current_string_block;
1104 consing_since_gc += sizeof (struct string_block);
1105 current_string_block->next = 0;
1106 current_string_block->prev = 0;
1107 current_string_block->pos = 0;
1108 large_string_blocks = 0;
1111 DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0,
1112 "Return a newly created string of length LENGTH, with each element being INIT.\n\
1113 Both LENGTH and INIT must be numbers.")
1114 (length, init)
1115 Lisp_Object length, init;
1117 register Lisp_Object val;
1118 register unsigned char *p, *end, c;
1120 CHECK_NATNUM (length, 0);
1121 CHECK_NUMBER (init, 1);
1122 val = make_uninit_string (XFASTINT (length));
1123 c = XINT (init);
1124 p = XSTRING (val)->data;
1125 end = p + XSTRING (val)->size;
1126 while (p != end)
1127 *p++ = c;
1128 *p = 0;
1129 return val;
1132 DEFUN ("make-bool-vector", Fmake_bool_vector, Smake_bool_vector, 2, 2, 0,
1133 "Return a newly created bitstring of length LENGTH, with INIT as each element.\n\
1134 Both LENGTH and INIT must be numbers. INIT matters only in whether it is t or nil.")
1135 (length, init)
1136 Lisp_Object length, init;
1138 register Lisp_Object val;
1139 struct Lisp_Bool_Vector *p;
1140 int real_init, i;
1141 int length_in_chars, length_in_elts, bits_per_value;
1143 CHECK_NATNUM (length, 0);
1145 bits_per_value = sizeof (EMACS_INT) * BITS_PER_CHAR;
1147 length_in_elts = (XFASTINT (length) + bits_per_value - 1) / bits_per_value;
1148 length_in_chars = length_in_elts * sizeof (EMACS_INT);
1150 /* We must allocate one more elements than LENGTH_IN_ELTS for the
1151 slot `size' of the struct Lisp_Bool_Vector. */
1152 val = Fmake_vector (make_number (length_in_elts + 1), Qnil);
1153 p = XBOOL_VECTOR (val);
1154 /* Get rid of any bits that would cause confusion. */
1155 p->vector_size = 0;
1156 XSETBOOL_VECTOR (val, p);
1157 p->size = XFASTINT (length);
1159 real_init = (NILP (init) ? 0 : -1);
1160 for (i = 0; i < length_in_chars ; i++)
1161 p->data[i] = real_init;
1163 return val;
1166 Lisp_Object
1167 make_string (contents, length)
1168 char *contents;
1169 int length;
1171 register Lisp_Object val;
1172 val = make_uninit_string (length);
1173 bcopy (contents, XSTRING (val)->data, length);
1174 return val;
1177 Lisp_Object
1178 build_string (str)
1179 char *str;
1181 return make_string (str, strlen (str));
1184 Lisp_Object
1185 make_uninit_string (length)
1186 int length;
1188 register Lisp_Object val;
1189 register int fullsize = STRING_FULLSIZE (length);
1191 if (length < 0) abort ();
1193 if (fullsize <= STRING_BLOCK_SIZE - current_string_block->pos)
1194 /* This string can fit in the current string block */
1196 XSETSTRING (val,
1197 ((struct Lisp_String *)
1198 (current_string_block->chars + current_string_block->pos)));
1199 current_string_block->pos += fullsize;
1201 else if (fullsize > STRING_BLOCK_OUTSIZE)
1202 /* This string gets its own string block */
1204 register struct string_block *new;
1205 allocating_for_lisp = 1;
1206 #ifdef DOUG_LEA_MALLOC
1207 /* Prevent mmap'ing the chunk (which is potentially very large). */
1208 mallopt (M_MMAP_MAX, 0);
1209 #endif
1210 new = (struct string_block *) xmalloc (sizeof (struct string_block_head) + fullsize);
1211 #ifdef DOUG_LEA_MALLOC
1212 /* Back to a reasonable maximum of mmap'ed areas. */
1213 mallopt (M_MMAP_MAX, 64);
1214 #endif
1215 allocating_for_lisp = 0;
1216 VALIDATE_LISP_STORAGE (new, 0);
1217 consing_since_gc += sizeof (struct string_block_head) + fullsize;
1218 new->pos = fullsize;
1219 new->next = large_string_blocks;
1220 large_string_blocks = new;
1221 XSETSTRING (val,
1222 ((struct Lisp_String *)
1223 ((struct string_block_head *)new + 1)));
1225 else
1226 /* Make a new current string block and start it off with this string */
1228 register struct string_block *new;
1229 allocating_for_lisp = 1;
1230 new = (struct string_block *) xmalloc (sizeof (struct string_block));
1231 allocating_for_lisp = 0;
1232 VALIDATE_LISP_STORAGE (new, sizeof *new);
1233 consing_since_gc += sizeof (struct string_block);
1234 current_string_block->next = new;
1235 new->prev = current_string_block;
1236 new->next = 0;
1237 current_string_block = new;
1238 new->pos = fullsize;
1239 XSETSTRING (val,
1240 (struct Lisp_String *) current_string_block->chars);
1243 string_chars_consed += fullsize;
1244 XSTRING (val)->size = length;
1245 XSTRING (val)->data[length] = 0;
1246 INITIALIZE_INTERVAL (XSTRING (val), NULL_INTERVAL);
1248 return val;
1251 /* Return a newly created vector or string with specified arguments as
1252 elements. If all the arguments are characters that can fit
1253 in a string of events, make a string; otherwise, make a vector.
1255 Any number of arguments, even zero arguments, are allowed. */
1257 Lisp_Object
1258 make_event_array (nargs, args)
1259 register int nargs;
1260 Lisp_Object *args;
1262 int i;
1264 for (i = 0; i < nargs; i++)
1265 /* The things that fit in a string
1266 are characters that are in 0...127,
1267 after discarding the meta bit and all the bits above it. */
1268 if (!INTEGERP (args[i])
1269 || (XUINT (args[i]) & ~(-CHAR_META)) >= 0200)
1270 return Fvector (nargs, args);
1272 /* Since the loop exited, we know that all the things in it are
1273 characters, so we can make a string. */
1275 Lisp_Object result;
1277 result = Fmake_string (nargs, make_number (0));
1278 for (i = 0; i < nargs; i++)
1280 XSTRING (result)->data[i] = XINT (args[i]);
1281 /* Move the meta bit to the right place for a string char. */
1282 if (XINT (args[i]) & CHAR_META)
1283 XSTRING (result)->data[i] |= 0x80;
1286 return result;
1290 /* Pure storage management. */
1292 /* Must get an error if pure storage is full,
1293 since if it cannot hold a large string
1294 it may be able to hold conses that point to that string;
1295 then the string is not protected from gc. */
1297 Lisp_Object
1298 make_pure_string (data, length)
1299 char *data;
1300 int length;
1302 register Lisp_Object new;
1303 register int size = sizeof (EMACS_INT) + INTERVAL_PTR_SIZE + length + 1;
1305 if (pureptr + size > PURESIZE)
1306 error ("Pure Lisp storage exhausted");
1307 XSETSTRING (new, PUREBEG + pureptr);
1308 XSTRING (new)->size = length;
1309 bcopy (data, XSTRING (new)->data, length);
1310 XSTRING (new)->data[length] = 0;
1312 /* We must give strings in pure storage some kind of interval. So we
1313 give them a null one. */
1314 #if defined (USE_TEXT_PROPERTIES)
1315 XSTRING (new)->intervals = NULL_INTERVAL;
1316 #endif
1317 pureptr += (size + sizeof (EMACS_INT) - 1)
1318 / sizeof (EMACS_INT) * sizeof (EMACS_INT);
1319 return new;
1322 Lisp_Object
1323 pure_cons (car, cdr)
1324 Lisp_Object car, cdr;
1326 register Lisp_Object new;
1328 if (pureptr + sizeof (struct Lisp_Cons) > PURESIZE)
1329 error ("Pure Lisp storage exhausted");
1330 XSETCONS (new, PUREBEG + pureptr);
1331 pureptr += sizeof (struct Lisp_Cons);
1332 XCONS (new)->car = Fpurecopy (car);
1333 XCONS (new)->cdr = Fpurecopy (cdr);
1334 return new;
1337 #ifdef LISP_FLOAT_TYPE
1339 Lisp_Object
1340 make_pure_float (num)
1341 double num;
1343 register Lisp_Object new;
1345 /* Make sure that PUREBEG + pureptr is aligned on at least a sizeof
1346 (double) boundary. Some architectures (like the sparc) require
1347 this, and I suspect that floats are rare enough that it's no
1348 tragedy for those that do. */
1350 int alignment;
1351 char *p = PUREBEG + pureptr;
1353 #ifdef __GNUC__
1354 #if __GNUC__ >= 2
1355 alignment = __alignof (struct Lisp_Float);
1356 #else
1357 alignment = sizeof (struct Lisp_Float);
1358 #endif
1359 #else
1360 alignment = sizeof (struct Lisp_Float);
1361 #endif
1362 p = (char *) (((unsigned long) p + alignment - 1) & - alignment);
1363 pureptr = p - PUREBEG;
1366 if (pureptr + sizeof (struct Lisp_Float) > PURESIZE)
1367 error ("Pure Lisp storage exhausted");
1368 XSETFLOAT (new, PUREBEG + pureptr);
1369 pureptr += sizeof (struct Lisp_Float);
1370 XFLOAT (new)->data = num;
1371 XSETFASTINT (XFLOAT (new)->type, 0); /* bug chasing -wsr */
1372 return new;
1375 #endif /* LISP_FLOAT_TYPE */
1377 Lisp_Object
1378 make_pure_vector (len)
1379 EMACS_INT len;
1381 register Lisp_Object new;
1382 register EMACS_INT size = sizeof (struct Lisp_Vector) + (len - 1) * sizeof (Lisp_Object);
1384 if (pureptr + size > PURESIZE)
1385 error ("Pure Lisp storage exhausted");
1387 XSETVECTOR (new, PUREBEG + pureptr);
1388 pureptr += size;
1389 XVECTOR (new)->size = len;
1390 return new;
1393 DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
1394 "Make a copy of OBJECT in pure storage.\n\
1395 Recursively copies contents of vectors and cons cells.\n\
1396 Does not copy symbols.")
1397 (obj)
1398 register Lisp_Object obj;
1400 if (NILP (Vpurify_flag))
1401 return obj;
1403 if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
1404 && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
1405 return obj;
1407 if (CONSP (obj))
1408 return pure_cons (XCONS (obj)->car, XCONS (obj)->cdr);
1409 #ifdef LISP_FLOAT_TYPE
1410 else if (FLOATP (obj))
1411 return make_pure_float (XFLOAT (obj)->data);
1412 #endif /* LISP_FLOAT_TYPE */
1413 else if (STRINGP (obj))
1414 return make_pure_string (XSTRING (obj)->data, XSTRING (obj)->size);
1415 else if (COMPILEDP (obj) || VECTORP (obj))
1417 register struct Lisp_Vector *vec;
1418 register int i, size;
1420 size = XVECTOR (obj)->size;
1421 if (size & PSEUDOVECTOR_FLAG)
1422 size &= PSEUDOVECTOR_SIZE_MASK;
1423 vec = XVECTOR (make_pure_vector ((EMACS_INT) size));
1424 for (i = 0; i < size; i++)
1425 vec->contents[i] = Fpurecopy (XVECTOR (obj)->contents[i]);
1426 if (COMPILEDP (obj))
1427 XSETCOMPILED (obj, vec);
1428 else
1429 XSETVECTOR (obj, vec);
1430 return obj;
1432 else if (MARKERP (obj))
1433 error ("Attempt to copy a marker to pure storage");
1434 else
1435 return obj;
1438 /* Recording what needs to be marked for gc. */
1440 struct gcpro *gcprolist;
1442 #define NSTATICS 768
1444 Lisp_Object *staticvec[NSTATICS] = {0};
1446 int staticidx = 0;
1448 /* Put an entry in staticvec, pointing at the variable whose address is given */
1450 void
1451 staticpro (varaddress)
1452 Lisp_Object *varaddress;
1454 staticvec[staticidx++] = varaddress;
1455 if (staticidx >= NSTATICS)
1456 abort ();
1459 struct catchtag
1461 Lisp_Object tag;
1462 Lisp_Object val;
1463 struct catchtag *next;
1464 /* jmp_buf jmp; /* We don't need this for GC purposes */
1467 struct backtrace
1469 struct backtrace *next;
1470 Lisp_Object *function;
1471 Lisp_Object *args; /* Points to vector of args. */
1472 int nargs; /* length of vector */
1473 /* if nargs is UNEVALLED, args points to slot holding list of unevalled args */
1474 char evalargs;
1477 /* Garbage collection! */
1479 int total_conses, total_markers, total_symbols, total_string_size, total_vector_size;
1480 int total_free_conses, total_free_markers, total_free_symbols;
1481 #ifdef LISP_FLOAT_TYPE
1482 int total_free_floats, total_floats;
1483 #endif /* LISP_FLOAT_TYPE */
1485 /* Temporarily prevent garbage collection. */
1488 inhibit_garbage_collection ()
1490 int count = specpdl_ptr - specpdl;
1491 Lisp_Object number;
1492 int nbits = min (VALBITS, BITS_PER_INT);
1494 XSETINT (number, ((EMACS_INT) 1 << (nbits - 1)) - 1);
1496 specbind (Qgc_cons_threshold, number);
1498 return count;
1501 DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
1502 "Reclaim storage for Lisp objects no longer needed.\n\
1503 Returns info on amount of space in use:\n\
1504 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)\n\
1505 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS\n\
1506 (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS))\n\
1507 Garbage collection happens automatically if you cons more than\n\
1508 `gc-cons-threshold' bytes of Lisp data since previous garbage collection.")
1511 register struct gcpro *tail;
1512 register struct specbinding *bind;
1513 struct catchtag *catch;
1514 struct handler *handler;
1515 register struct backtrace *backlist;
1516 register Lisp_Object tem;
1517 char *omessage = echo_area_glyphs;
1518 int omessage_length = echo_area_glyphs_length;
1519 char stack_top_variable;
1520 register int i;
1522 /* In case user calls debug_print during GC,
1523 don't let that cause a recursive GC. */
1524 consing_since_gc = 0;
1526 /* Save a copy of the contents of the stack, for debugging. */
1527 #if MAX_SAVE_STACK > 0
1528 if (NILP (Vpurify_flag))
1530 i = &stack_top_variable - stack_bottom;
1531 if (i < 0) i = -i;
1532 if (i < MAX_SAVE_STACK)
1534 if (stack_copy == 0)
1535 stack_copy = (char *) xmalloc (stack_copy_size = i);
1536 else if (stack_copy_size < i)
1537 stack_copy = (char *) xrealloc (stack_copy, (stack_copy_size = i));
1538 if (stack_copy)
1540 if ((EMACS_INT) (&stack_top_variable - stack_bottom) > 0)
1541 bcopy (stack_bottom, stack_copy, i);
1542 else
1543 bcopy (&stack_top_variable, stack_copy, i);
1547 #endif /* MAX_SAVE_STACK > 0 */
1549 if (garbage_collection_messages)
1550 message1_nolog ("Garbage collecting...");
1552 /* Don't keep command history around forever. */
1553 if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0)
1555 tem = Fnthcdr (Vhistory_length, Vcommand_history);
1556 if (CONSP (tem))
1557 XCONS (tem)->cdr = Qnil;
1560 /* Likewise for undo information. */
1562 register struct buffer *nextb = all_buffers;
1564 while (nextb)
1566 /* If a buffer's undo list is Qt, that means that undo is
1567 turned off in that buffer. Calling truncate_undo_list on
1568 Qt tends to return NULL, which effectively turns undo back on.
1569 So don't call truncate_undo_list if undo_list is Qt. */
1570 if (! EQ (nextb->undo_list, Qt))
1571 nextb->undo_list
1572 = truncate_undo_list (nextb->undo_list, undo_limit,
1573 undo_strong_limit);
1574 nextb = nextb->next;
1578 gc_in_progress = 1;
1580 /* clear_marks (); */
1582 /* In each "large string", set the MARKBIT of the size field.
1583 That enables mark_object to recognize them. */
1585 register struct string_block *b;
1586 for (b = large_string_blocks; b; b = b->next)
1587 ((struct Lisp_String *)(&b->chars[0]))->size |= MARKBIT;
1590 /* Mark all the special slots that serve as the roots of accessibility.
1592 Usually the special slots to mark are contained in particular structures.
1593 Then we know no slot is marked twice because the structures don't overlap.
1594 In some cases, the structures point to the slots to be marked.
1595 For these, we use MARKBIT to avoid double marking of the slot. */
1597 for (i = 0; i < staticidx; i++)
1598 mark_object (staticvec[i]);
1599 for (tail = gcprolist; tail; tail = tail->next)
1600 for (i = 0; i < tail->nvars; i++)
1601 if (!XMARKBIT (tail->var[i]))
1603 mark_object (&tail->var[i]);
1604 XMARK (tail->var[i]);
1606 for (bind = specpdl; bind != specpdl_ptr; bind++)
1608 mark_object (&bind->symbol);
1609 mark_object (&bind->old_value);
1611 for (catch = catchlist; catch; catch = catch->next)
1613 mark_object (&catch->tag);
1614 mark_object (&catch->val);
1616 for (handler = handlerlist; handler; handler = handler->next)
1618 mark_object (&handler->handler);
1619 mark_object (&handler->var);
1621 for (backlist = backtrace_list; backlist; backlist = backlist->next)
1623 if (!XMARKBIT (*backlist->function))
1625 mark_object (backlist->function);
1626 XMARK (*backlist->function);
1628 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
1629 i = 0;
1630 else
1631 i = backlist->nargs - 1;
1632 for (; i >= 0; i--)
1633 if (!XMARKBIT (backlist->args[i]))
1635 mark_object (&backlist->args[i]);
1636 XMARK (backlist->args[i]);
1639 mark_kboards ();
1641 gc_sweep ();
1643 /* Clear the mark bits that we set in certain root slots. */
1645 for (tail = gcprolist; tail; tail = tail->next)
1646 for (i = 0; i < tail->nvars; i++)
1647 XUNMARK (tail->var[i]);
1648 for (backlist = backtrace_list; backlist; backlist = backlist->next)
1650 XUNMARK (*backlist->function);
1651 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
1652 i = 0;
1653 else
1654 i = backlist->nargs - 1;
1655 for (; i >= 0; i--)
1656 XUNMARK (backlist->args[i]);
1658 XUNMARK (buffer_defaults.name);
1659 XUNMARK (buffer_local_symbols.name);
1661 /* clear_marks (); */
1662 gc_in_progress = 0;
1664 consing_since_gc = 0;
1665 if (gc_cons_threshold < 10000)
1666 gc_cons_threshold = 10000;
1668 if (garbage_collection_messages)
1670 if (omessage || minibuf_level > 0)
1671 message2_nolog (omessage, omessage_length);
1672 else
1673 message1_nolog ("Garbage collecting...done");
1676 return Fcons (Fcons (make_number (total_conses),
1677 make_number (total_free_conses)),
1678 Fcons (Fcons (make_number (total_symbols),
1679 make_number (total_free_symbols)),
1680 Fcons (Fcons (make_number (total_markers),
1681 make_number (total_free_markers)),
1682 Fcons (make_number (total_string_size),
1683 Fcons (make_number (total_vector_size),
1684 Fcons (Fcons
1685 #ifdef LISP_FLOAT_TYPE
1686 (make_number (total_floats),
1687 make_number (total_free_floats)),
1688 #else /* not LISP_FLOAT_TYPE */
1689 (make_number (0), make_number (0)),
1690 #endif /* not LISP_FLOAT_TYPE */
1691 Fcons (Fcons
1692 #ifdef USE_TEXT_PROPERTIES
1693 (make_number (total_intervals),
1694 make_number (total_free_intervals)),
1695 #else /* not USE_TEXT_PROPERTIES */
1696 (make_number (0), make_number (0)),
1697 #endif /* not USE_TEXT_PROPERTIES */
1698 Qnil)))))));
1701 #if 0
1702 static void
1703 clear_marks ()
1705 /* Clear marks on all conses */
1707 register struct cons_block *cblk;
1708 register int lim = cons_block_index;
1710 for (cblk = cons_block; cblk; cblk = cblk->next)
1712 register int i;
1713 for (i = 0; i < lim; i++)
1714 XUNMARK (cblk->conses[i].car);
1715 lim = CONS_BLOCK_SIZE;
1718 /* Clear marks on all symbols */
1720 register struct symbol_block *sblk;
1721 register int lim = symbol_block_index;
1723 for (sblk = symbol_block; sblk; sblk = sblk->next)
1725 register int i;
1726 for (i = 0; i < lim; i++)
1728 XUNMARK (sblk->symbols[i].plist);
1730 lim = SYMBOL_BLOCK_SIZE;
1733 /* Clear marks on all markers */
1735 register struct marker_block *sblk;
1736 register int lim = marker_block_index;
1738 for (sblk = marker_block; sblk; sblk = sblk->next)
1740 register int i;
1741 for (i = 0; i < lim; i++)
1742 if (sblk->markers[i].u_marker.type == Lisp_Misc_Marker)
1743 XUNMARK (sblk->markers[i].u_marker.chain);
1744 lim = MARKER_BLOCK_SIZE;
1747 /* Clear mark bits on all buffers */
1749 register struct buffer *nextb = all_buffers;
1751 while (nextb)
1753 XUNMARK (nextb->name);
1754 nextb = nextb->next;
1758 #endif
1760 /* Mark reference to a Lisp_Object.
1761 If the object referred to has not been seen yet, recursively mark
1762 all the references contained in it.
1764 If the object referenced is a short string, the referencing slot
1765 is threaded into a chain of such slots, pointed to from
1766 the `size' field of the string. The actual string size
1767 lives in the last slot in the chain. We recognize the end
1768 because it is < (unsigned) STRING_BLOCK_SIZE. */
1770 #define LAST_MARKED_SIZE 500
1771 Lisp_Object *last_marked[LAST_MARKED_SIZE];
1772 int last_marked_index;
1774 static void
1775 mark_object (argptr)
1776 Lisp_Object *argptr;
1778 Lisp_Object *objptr = argptr;
1779 register Lisp_Object obj;
1781 loop:
1782 obj = *objptr;
1783 loop2:
1784 XUNMARK (obj);
1786 if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
1787 && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
1788 return;
1790 last_marked[last_marked_index++] = objptr;
1791 if (last_marked_index == LAST_MARKED_SIZE)
1792 last_marked_index = 0;
1794 switch (SWITCH_ENUM_CAST (XGCTYPE (obj)))
1796 case Lisp_String:
1798 register struct Lisp_String *ptr = XSTRING (obj);
1800 MARK_INTERVAL_TREE (ptr->intervals);
1801 if (ptr->size & MARKBIT)
1802 /* A large string. Just set ARRAY_MARK_FLAG. */
1803 ptr->size |= ARRAY_MARK_FLAG;
1804 else
1806 /* A small string. Put this reference
1807 into the chain of references to it.
1808 If the address includes MARKBIT, put that bit elsewhere
1809 when we store OBJPTR into the size field. */
1811 if (XMARKBIT (*objptr))
1813 XSETFASTINT (*objptr, ptr->size);
1814 XMARK (*objptr);
1816 else
1817 XSETFASTINT (*objptr, ptr->size);
1819 if ((EMACS_INT) objptr & DONT_COPY_FLAG)
1820 abort ();
1821 ptr->size = (EMACS_INT) objptr;
1822 if (ptr->size & MARKBIT)
1823 ptr->size ^= MARKBIT | DONT_COPY_FLAG;
1826 break;
1828 case Lisp_Vectorlike:
1829 if (GC_BUFFERP (obj))
1831 if (!XMARKBIT (XBUFFER (obj)->name))
1832 mark_buffer (obj);
1834 else if (GC_SUBRP (obj))
1835 break;
1836 else if (GC_COMPILEDP (obj))
1837 /* We could treat this just like a vector, but it is better
1838 to save the COMPILED_CONSTANTS element for last and avoid recursion
1839 there. */
1841 register struct Lisp_Vector *ptr = XVECTOR (obj);
1842 register EMACS_INT size = ptr->size;
1843 /* See comment above under Lisp_Vector. */
1844 struct Lisp_Vector *volatile ptr1 = ptr;
1845 register int i;
1847 if (size & ARRAY_MARK_FLAG)
1848 break; /* Already marked */
1849 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
1850 size &= PSEUDOVECTOR_SIZE_MASK;
1851 for (i = 0; i < size; i++) /* and then mark its elements */
1853 if (i != COMPILED_CONSTANTS)
1854 mark_object (&ptr1->contents[i]);
1856 /* This cast should be unnecessary, but some Mips compiler complains
1857 (MIPS-ABI + SysVR4, DC/OSx, etc). */
1858 objptr = (Lisp_Object *) &ptr1->contents[COMPILED_CONSTANTS];
1859 goto loop;
1861 else if (GC_FRAMEP (obj))
1863 /* See comment above under Lisp_Vector for why this is volatile. */
1864 register struct frame *volatile ptr = XFRAME (obj);
1865 register EMACS_INT size = ptr->size;
1867 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
1868 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
1870 mark_object (&ptr->name);
1871 mark_object (&ptr->icon_name);
1872 mark_object (&ptr->title);
1873 mark_object (&ptr->focus_frame);
1874 mark_object (&ptr->selected_window);
1875 mark_object (&ptr->minibuffer_window);
1876 mark_object (&ptr->param_alist);
1877 mark_object (&ptr->scroll_bars);
1878 mark_object (&ptr->condemned_scroll_bars);
1879 mark_object (&ptr->menu_bar_items);
1880 mark_object (&ptr->face_alist);
1881 mark_object (&ptr->menu_bar_vector);
1882 mark_object (&ptr->buffer_predicate);
1883 mark_object (&ptr->buffer_list);
1885 else if (GC_BOOL_VECTOR_P (obj))
1887 register struct Lisp_Vector *ptr = XVECTOR (obj);
1889 if (ptr->size & ARRAY_MARK_FLAG)
1890 break; /* Already marked */
1891 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
1893 else
1895 register struct Lisp_Vector *ptr = XVECTOR (obj);
1896 register EMACS_INT size = ptr->size;
1897 /* The reason we use ptr1 is to avoid an apparent hardware bug
1898 that happens occasionally on the FSF's HP 300s.
1899 The bug is that a2 gets clobbered by recursive calls to mark_object.
1900 The clobberage seems to happen during function entry,
1901 perhaps in the moveml instruction.
1902 Yes, this is a crock, but we have to do it. */
1903 struct Lisp_Vector *volatile ptr1 = ptr;
1904 register int i;
1906 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
1907 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
1908 if (size & PSEUDOVECTOR_FLAG)
1909 size &= PSEUDOVECTOR_SIZE_MASK;
1910 for (i = 0; i < size; i++) /* and then mark its elements */
1911 mark_object (&ptr1->contents[i]);
1913 break;
1915 case Lisp_Symbol:
1917 /* See comment above under Lisp_Vector for why this is volatile. */
1918 register struct Lisp_Symbol *volatile ptr = XSYMBOL (obj);
1919 struct Lisp_Symbol *ptrx;
1921 if (XMARKBIT (ptr->plist)) break;
1922 XMARK (ptr->plist);
1923 mark_object ((Lisp_Object *) &ptr->value);
1924 mark_object (&ptr->function);
1925 mark_object (&ptr->plist);
1926 XSETTYPE (*(Lisp_Object *) &ptr->name, Lisp_String);
1927 mark_object (&ptr->name);
1928 ptr = ptr->next;
1929 if (ptr)
1931 /* For the benefit of the last_marked log. */
1932 objptr = (Lisp_Object *)&XSYMBOL (obj)->next;
1933 ptrx = ptr; /* Use of ptrx avoids compiler bug on Sun */
1934 XSETSYMBOL (obj, ptrx);
1935 /* We can't goto loop here because *objptr doesn't contain an
1936 actual Lisp_Object with valid datatype field. */
1937 goto loop2;
1940 break;
1942 case Lisp_Misc:
1943 switch (XMISCTYPE (obj))
1945 case Lisp_Misc_Marker:
1946 XMARK (XMARKER (obj)->chain);
1947 /* DO NOT mark thru the marker's chain.
1948 The buffer's markers chain does not preserve markers from gc;
1949 instead, markers are removed from the chain when freed by gc. */
1950 break;
1952 case Lisp_Misc_Buffer_Local_Value:
1953 case Lisp_Misc_Some_Buffer_Local_Value:
1955 register struct Lisp_Buffer_Local_Value *ptr
1956 = XBUFFER_LOCAL_VALUE (obj);
1957 if (XMARKBIT (ptr->car)) break;
1958 XMARK (ptr->car);
1959 /* If the cdr is nil, avoid recursion for the car. */
1960 if (EQ (ptr->cdr, Qnil))
1962 objptr = &ptr->car;
1963 goto loop;
1965 mark_object (&ptr->car);
1966 /* See comment above under Lisp_Vector for why not use ptr here. */
1967 objptr = &XBUFFER_LOCAL_VALUE (obj)->cdr;
1968 goto loop;
1971 case Lisp_Misc_Intfwd:
1972 case Lisp_Misc_Boolfwd:
1973 case Lisp_Misc_Objfwd:
1974 case Lisp_Misc_Buffer_Objfwd:
1975 case Lisp_Misc_Kboard_Objfwd:
1976 /* Don't bother with Lisp_Buffer_Objfwd,
1977 since all markable slots in current buffer marked anyway. */
1978 /* Don't need to do Lisp_Objfwd, since the places they point
1979 are protected with staticpro. */
1980 break;
1982 case Lisp_Misc_Overlay:
1984 struct Lisp_Overlay *ptr = XOVERLAY (obj);
1985 if (!XMARKBIT (ptr->plist))
1987 XMARK (ptr->plist);
1988 mark_object (&ptr->start);
1989 mark_object (&ptr->end);
1990 objptr = &ptr->plist;
1991 goto loop;
1994 break;
1996 default:
1997 abort ();
1999 break;
2001 case Lisp_Cons:
2003 register struct Lisp_Cons *ptr = XCONS (obj);
2004 if (XMARKBIT (ptr->car)) break;
2005 XMARK (ptr->car);
2006 /* If the cdr is nil, avoid recursion for the car. */
2007 if (EQ (ptr->cdr, Qnil))
2009 objptr = &ptr->car;
2010 goto loop;
2012 mark_object (&ptr->car);
2013 /* See comment above under Lisp_Vector for why not use ptr here. */
2014 objptr = &XCONS (obj)->cdr;
2015 goto loop;
2018 #ifdef LISP_FLOAT_TYPE
2019 case Lisp_Float:
2020 XMARK (XFLOAT (obj)->type);
2021 break;
2022 #endif /* LISP_FLOAT_TYPE */
2024 case Lisp_Int:
2025 break;
2027 default:
2028 abort ();
2032 /* Mark the pointers in a buffer structure. */
2034 static void
2035 mark_buffer (buf)
2036 Lisp_Object buf;
2038 register struct buffer *buffer = XBUFFER (buf);
2039 register Lisp_Object *ptr;
2040 Lisp_Object base_buffer;
2042 /* This is the buffer's markbit */
2043 mark_object (&buffer->name);
2044 XMARK (buffer->name);
2046 MARK_INTERVAL_TREE (BUF_INTERVALS (buffer));
2048 #if 0
2049 mark_object (buffer->syntax_table);
2051 /* Mark the various string-pointers in the buffer object.
2052 Since the strings may be relocated, we must mark them
2053 in their actual slots. So gc_sweep must convert each slot
2054 back to an ordinary C pointer. */
2055 XSETSTRING (*(Lisp_Object *)&buffer->upcase_table, buffer->upcase_table);
2056 mark_object ((Lisp_Object *)&buffer->upcase_table);
2057 XSETSTRING (*(Lisp_Object *)&buffer->downcase_table, buffer->downcase_table);
2058 mark_object ((Lisp_Object *)&buffer->downcase_table);
2060 XSETSTRING (*(Lisp_Object *)&buffer->sort_table, buffer->sort_table);
2061 mark_object ((Lisp_Object *)&buffer->sort_table);
2062 XSETSTRING (*(Lisp_Object *)&buffer->folding_sort_table, buffer->folding_sort_table);
2063 mark_object ((Lisp_Object *)&buffer->folding_sort_table);
2064 #endif
2066 for (ptr = &buffer->name + 1;
2067 (char *)ptr < (char *)buffer + sizeof (struct buffer);
2068 ptr++)
2069 mark_object (ptr);
2071 /* If this is an indirect buffer, mark its base buffer. */
2072 if (buffer->base_buffer && !XMARKBIT (buffer->base_buffer->name))
2074 XSETBUFFER (base_buffer, buffer->base_buffer);
2075 mark_buffer (base_buffer);
2080 /* Mark the pointers in the kboard objects. */
2082 static void
2083 mark_kboards ()
2085 KBOARD *kb;
2086 Lisp_Object *p;
2087 for (kb = all_kboards; kb; kb = kb->next_kboard)
2089 if (kb->kbd_macro_buffer)
2090 for (p = kb->kbd_macro_buffer; p < kb->kbd_macro_ptr; p++)
2091 mark_object (p);
2092 mark_object (&kb->Vprefix_arg);
2093 mark_object (&kb->kbd_queue);
2094 mark_object (&kb->Vlast_kbd_macro);
2095 mark_object (&kb->Vsystem_key_alist);
2096 mark_object (&kb->system_key_syms);
2100 /* Sweep: find all structures not marked, and free them. */
2102 static void
2103 gc_sweep ()
2105 total_string_size = 0;
2106 compact_strings ();
2108 /* Put all unmarked conses on free list */
2110 register struct cons_block *cblk;
2111 register int lim = cons_block_index;
2112 register int num_free = 0, num_used = 0;
2114 cons_free_list = 0;
2116 for (cblk = cons_block; cblk; cblk = cblk->next)
2118 register int i;
2119 for (i = 0; i < lim; i++)
2120 if (!XMARKBIT (cblk->conses[i].car))
2122 num_free++;
2123 *(struct Lisp_Cons **)&cblk->conses[i].car = cons_free_list;
2124 cons_free_list = &cblk->conses[i];
2126 else
2128 num_used++;
2129 XUNMARK (cblk->conses[i].car);
2131 lim = CONS_BLOCK_SIZE;
2133 total_conses = num_used;
2134 total_free_conses = num_free;
2137 #ifdef LISP_FLOAT_TYPE
2138 /* Put all unmarked floats on free list */
2140 register struct float_block *fblk;
2141 register int lim = float_block_index;
2142 register int num_free = 0, num_used = 0;
2144 float_free_list = 0;
2146 for (fblk = float_block; fblk; fblk = fblk->next)
2148 register int i;
2149 for (i = 0; i < lim; i++)
2150 if (!XMARKBIT (fblk->floats[i].type))
2152 num_free++;
2153 *(struct Lisp_Float **)&fblk->floats[i].type = float_free_list;
2154 float_free_list = &fblk->floats[i];
2156 else
2158 num_used++;
2159 XUNMARK (fblk->floats[i].type);
2161 lim = FLOAT_BLOCK_SIZE;
2163 total_floats = num_used;
2164 total_free_floats = num_free;
2166 #endif /* LISP_FLOAT_TYPE */
2168 #ifdef USE_TEXT_PROPERTIES
2169 /* Put all unmarked intervals on free list */
2171 register struct interval_block *iblk;
2172 register int lim = interval_block_index;
2173 register int num_free = 0, num_used = 0;
2175 interval_free_list = 0;
2177 for (iblk = interval_block; iblk; iblk = iblk->next)
2179 register int i;
2181 for (i = 0; i < lim; i++)
2183 if (! XMARKBIT (iblk->intervals[i].plist))
2185 iblk->intervals[i].parent = interval_free_list;
2186 interval_free_list = &iblk->intervals[i];
2187 num_free++;
2189 else
2191 num_used++;
2192 XUNMARK (iblk->intervals[i].plist);
2195 lim = INTERVAL_BLOCK_SIZE;
2197 total_intervals = num_used;
2198 total_free_intervals = num_free;
2200 #endif /* USE_TEXT_PROPERTIES */
2202 /* Put all unmarked symbols on free list */
2204 register struct symbol_block *sblk;
2205 register int lim = symbol_block_index;
2206 register int num_free = 0, num_used = 0;
2208 symbol_free_list = 0;
2210 for (sblk = symbol_block; sblk; sblk = sblk->next)
2212 register int i;
2213 for (i = 0; i < lim; i++)
2214 if (!XMARKBIT (sblk->symbols[i].plist))
2216 *(struct Lisp_Symbol **)&sblk->symbols[i].value = symbol_free_list;
2217 symbol_free_list = &sblk->symbols[i];
2218 num_free++;
2220 else
2222 num_used++;
2223 sblk->symbols[i].name
2224 = XSTRING (*(Lisp_Object *) &sblk->symbols[i].name);
2225 XUNMARK (sblk->symbols[i].plist);
2227 lim = SYMBOL_BLOCK_SIZE;
2229 total_symbols = num_used;
2230 total_free_symbols = num_free;
2233 #ifndef standalone
2234 /* Put all unmarked markers on free list.
2235 Unchain each one first from the buffer it points into,
2236 but only if it's a real marker. */
2238 register struct marker_block *mblk;
2239 register int lim = marker_block_index;
2240 register int num_free = 0, num_used = 0;
2242 marker_free_list = 0;
2244 for (mblk = marker_block; mblk; mblk = mblk->next)
2246 register int i;
2247 EMACS_INT already_free = -1;
2249 for (i = 0; i < lim; i++)
2251 Lisp_Object *markword;
2252 switch (mblk->markers[i].u_marker.type)
2254 case Lisp_Misc_Marker:
2255 markword = &mblk->markers[i].u_marker.chain;
2256 break;
2257 case Lisp_Misc_Buffer_Local_Value:
2258 case Lisp_Misc_Some_Buffer_Local_Value:
2259 markword = &mblk->markers[i].u_buffer_local_value.car;
2260 break;
2261 case Lisp_Misc_Overlay:
2262 markword = &mblk->markers[i].u_overlay.plist;
2263 break;
2264 case Lisp_Misc_Free:
2265 /* If the object was already free, keep it
2266 on the free list. */
2267 markword = &already_free;
2268 break;
2269 default:
2270 markword = 0;
2271 break;
2273 if (markword && !XMARKBIT (*markword))
2275 Lisp_Object tem;
2276 if (mblk->markers[i].u_marker.type == Lisp_Misc_Marker)
2278 /* tem1 avoids Sun compiler bug */
2279 struct Lisp_Marker *tem1 = &mblk->markers[i].u_marker;
2280 XSETMARKER (tem, tem1);
2281 unchain_marker (tem);
2283 /* Set the type of the freed object to Lisp_Misc_Free.
2284 We could leave the type alone, since nobody checks it,
2285 but this might catch bugs faster. */
2286 mblk->markers[i].u_marker.type = Lisp_Misc_Free;
2287 mblk->markers[i].u_free.chain = marker_free_list;
2288 marker_free_list = &mblk->markers[i];
2289 num_free++;
2291 else
2293 num_used++;
2294 if (markword)
2295 XUNMARK (*markword);
2298 lim = MARKER_BLOCK_SIZE;
2301 total_markers = num_used;
2302 total_free_markers = num_free;
2305 /* Free all unmarked buffers */
2307 register struct buffer *buffer = all_buffers, *prev = 0, *next;
2309 while (buffer)
2310 if (!XMARKBIT (buffer->name))
2312 if (prev)
2313 prev->next = buffer->next;
2314 else
2315 all_buffers = buffer->next;
2316 next = buffer->next;
2317 xfree (buffer);
2318 buffer = next;
2320 else
2322 XUNMARK (buffer->name);
2323 UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer));
2325 #if 0
2326 /* Each `struct Lisp_String *' was turned into a Lisp_Object
2327 for purposes of marking and relocation.
2328 Turn them back into C pointers now. */
2329 buffer->upcase_table
2330 = XSTRING (*(Lisp_Object *)&buffer->upcase_table);
2331 buffer->downcase_table
2332 = XSTRING (*(Lisp_Object *)&buffer->downcase_table);
2333 buffer->sort_table
2334 = XSTRING (*(Lisp_Object *)&buffer->sort_table);
2335 buffer->folding_sort_table
2336 = XSTRING (*(Lisp_Object *)&buffer->folding_sort_table);
2337 #endif
2339 prev = buffer, buffer = buffer->next;
2343 #endif /* standalone */
2345 /* Free all unmarked vectors */
2347 register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next;
2348 total_vector_size = 0;
2350 while (vector)
2351 if (!(vector->size & ARRAY_MARK_FLAG))
2353 if (prev)
2354 prev->next = vector->next;
2355 else
2356 all_vectors = vector->next;
2357 next = vector->next;
2358 xfree (vector);
2359 vector = next;
2361 else
2363 vector->size &= ~ARRAY_MARK_FLAG;
2364 if (vector->size & PSEUDOVECTOR_FLAG)
2365 total_vector_size += (PSEUDOVECTOR_SIZE_MASK & vector->size);
2366 else
2367 total_vector_size += vector->size;
2368 prev = vector, vector = vector->next;
2372 /* Free all "large strings" not marked with ARRAY_MARK_FLAG. */
2374 register struct string_block *sb = large_string_blocks, *prev = 0, *next;
2375 struct Lisp_String *s;
2377 while (sb)
2379 s = (struct Lisp_String *) &sb->chars[0];
2380 if (s->size & ARRAY_MARK_FLAG)
2382 ((struct Lisp_String *)(&sb->chars[0]))->size
2383 &= ~ARRAY_MARK_FLAG & ~MARKBIT;
2384 UNMARK_BALANCE_INTERVALS (s->intervals);
2385 total_string_size += ((struct Lisp_String *)(&sb->chars[0]))->size;
2386 prev = sb, sb = sb->next;
2388 else
2390 if (prev)
2391 prev->next = sb->next;
2392 else
2393 large_string_blocks = sb->next;
2394 next = sb->next;
2395 xfree (sb);
2396 sb = next;
2402 /* Compactify strings, relocate references, and free empty string blocks. */
2404 static void
2405 compact_strings ()
2407 /* String block of old strings we are scanning. */
2408 register struct string_block *from_sb;
2409 /* A preceding string block (or maybe the same one)
2410 where we are copying the still-live strings to. */
2411 register struct string_block *to_sb;
2412 int pos;
2413 int to_pos;
2415 to_sb = first_string_block;
2416 to_pos = 0;
2418 /* Scan each existing string block sequentially, string by string. */
2419 for (from_sb = first_string_block; from_sb; from_sb = from_sb->next)
2421 pos = 0;
2422 /* POS is the index of the next string in the block. */
2423 while (pos < from_sb->pos)
2425 register struct Lisp_String *nextstr
2426 = (struct Lisp_String *) &from_sb->chars[pos];
2428 register struct Lisp_String *newaddr;
2429 register EMACS_INT size = nextstr->size;
2431 /* NEXTSTR is the old address of the next string.
2432 Just skip it if it isn't marked. */
2433 if (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE)
2435 /* It is marked, so its size field is really a chain of refs.
2436 Find the end of the chain, where the actual size lives. */
2437 while (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE)
2439 if (size & DONT_COPY_FLAG)
2440 size ^= MARKBIT | DONT_COPY_FLAG;
2441 size = *(EMACS_INT *)size & ~MARKBIT;
2444 total_string_size += size;
2446 /* If it won't fit in TO_SB, close it out,
2447 and move to the next sb. Keep doing so until
2448 TO_SB reaches a large enough, empty enough string block.
2449 We know that TO_SB cannot advance past FROM_SB here
2450 since FROM_SB is large enough to contain this string.
2451 Any string blocks skipped here
2452 will be patched out and freed later. */
2453 while (to_pos + STRING_FULLSIZE (size)
2454 > max (to_sb->pos, STRING_BLOCK_SIZE))
2456 to_sb->pos = to_pos;
2457 to_sb = to_sb->next;
2458 to_pos = 0;
2460 /* Compute new address of this string
2461 and update TO_POS for the space being used. */
2462 newaddr = (struct Lisp_String *) &to_sb->chars[to_pos];
2463 to_pos += STRING_FULLSIZE (size);
2465 /* Copy the string itself to the new place. */
2466 if (nextstr != newaddr)
2467 bcopy (nextstr, newaddr, size + 1 + sizeof (EMACS_INT)
2468 + INTERVAL_PTR_SIZE);
2470 /* Go through NEXTSTR's chain of references
2471 and make each slot in the chain point to
2472 the new address of this string. */
2473 size = newaddr->size;
2474 while (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE)
2476 register Lisp_Object *objptr;
2477 if (size & DONT_COPY_FLAG)
2478 size ^= MARKBIT | DONT_COPY_FLAG;
2479 objptr = (Lisp_Object *)size;
2481 size = XFASTINT (*objptr) & ~MARKBIT;
2482 if (XMARKBIT (*objptr))
2484 XSETSTRING (*objptr, newaddr);
2485 XMARK (*objptr);
2487 else
2488 XSETSTRING (*objptr, newaddr);
2490 /* Store the actual size in the size field. */
2491 newaddr->size = size;
2493 #ifdef USE_TEXT_PROPERTIES
2494 /* Now that the string has been relocated, rebalance its
2495 interval tree, and update the tree's parent pointer. */
2496 if (! NULL_INTERVAL_P (newaddr->intervals))
2498 UNMARK_BALANCE_INTERVALS (newaddr->intervals);
2499 XSETSTRING (* (Lisp_Object *) &newaddr->intervals->parent,
2500 newaddr);
2502 #endif /* USE_TEXT_PROPERTIES */
2504 pos += STRING_FULLSIZE (size);
2508 /* Close out the last string block still used and free any that follow. */
2509 to_sb->pos = to_pos;
2510 current_string_block = to_sb;
2512 from_sb = to_sb->next;
2513 to_sb->next = 0;
2514 while (from_sb)
2516 to_sb = from_sb->next;
2517 xfree (from_sb);
2518 from_sb = to_sb;
2521 /* Free any empty string blocks further back in the chain.
2522 This loop will never free first_string_block, but it is very
2523 unlikely that that one will become empty, so why bother checking? */
2525 from_sb = first_string_block;
2526 while (to_sb = from_sb->next)
2528 if (to_sb->pos == 0)
2530 if (from_sb->next = to_sb->next)
2531 from_sb->next->prev = from_sb;
2532 xfree (to_sb);
2534 else
2535 from_sb = to_sb;
2539 /* Debugging aids. */
2541 DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0,
2542 "Return the address of the last byte Emacs has allocated, divided by 1024.\n\
2543 This may be helpful in debugging Emacs's memory usage.\n\
2544 We divide the value by 1024 to make sure it fits in a Lisp integer.")
2547 Lisp_Object end;
2549 XSETINT (end, (EMACS_INT) sbrk (0) / 1024);
2551 return end;
2554 DEFUN ("memory-use-counts", Fmemory_use_counts, Smemory_use_counts, 0, 0, 0,
2555 "Return a list of counters that measure how much consing there has been.\n\
2556 Each of these counters increments for a certain kind of object.\n\
2557 The counters wrap around from the largest positive integer to zero.\n\
2558 Garbage collection does not decrease them.\n\
2559 The elements of the value are as follows:\n\
2560 (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS)\n\
2561 All are in units of 1 = one object consed\n\
2562 except for VECTOR-CELLS and STRING-CHARS, which count the total length of\n\
2563 objects consed.\n\
2564 MISCS include overlays, markers, and some internal types.\n\
2565 Frames, windows, buffers, and subprocesses count as vectors\n\
2566 (but the contents of a buffer's text do not count here).")
2569 Lisp_Object lisp_cons_cells_consed;
2570 Lisp_Object lisp_floats_consed;
2571 Lisp_Object lisp_vector_cells_consed;
2572 Lisp_Object lisp_symbols_consed;
2573 Lisp_Object lisp_string_chars_consed;
2574 Lisp_Object lisp_misc_objects_consed;
2575 Lisp_Object lisp_intervals_consed;
2577 XSETINT (lisp_cons_cells_consed,
2578 cons_cells_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2579 XSETINT (lisp_floats_consed,
2580 floats_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2581 XSETINT (lisp_vector_cells_consed,
2582 vector_cells_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2583 XSETINT (lisp_symbols_consed,
2584 symbols_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2585 XSETINT (lisp_string_chars_consed,
2586 string_chars_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2587 XSETINT (lisp_misc_objects_consed,
2588 misc_objects_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2589 XSETINT (lisp_intervals_consed,
2590 intervals_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2592 return Fcons (lisp_cons_cells_consed,
2593 Fcons (lisp_floats_consed,
2594 Fcons (lisp_vector_cells_consed,
2595 Fcons (lisp_symbols_consed,
2596 Fcons (lisp_string_chars_consed,
2597 Fcons (lisp_misc_objects_consed,
2598 Fcons (lisp_intervals_consed,
2599 Qnil)))))));
2602 /* Initialization */
2604 init_alloc_once ()
2606 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
2607 pureptr = 0;
2608 #ifdef HAVE_SHM
2609 pure_size = PURESIZE;
2610 #endif
2611 all_vectors = 0;
2612 ignore_warnings = 1;
2613 #ifdef DOUG_LEA_MALLOC
2614 mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */
2615 mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */
2616 mallopt (M_MMAP_MAX, 64); /* max. number of mmap'ed areas */
2617 #endif
2618 init_strings ();
2619 init_cons ();
2620 init_symbol ();
2621 init_marker ();
2622 #ifdef LISP_FLOAT_TYPE
2623 init_float ();
2624 #endif /* LISP_FLOAT_TYPE */
2625 INIT_INTERVALS;
2627 #ifdef REL_ALLOC
2628 malloc_hysteresis = 32;
2629 #else
2630 malloc_hysteresis = 0;
2631 #endif
2633 spare_memory = (char *) malloc (SPARE_MEMORY);
2635 ignore_warnings = 0;
2636 gcprolist = 0;
2637 staticidx = 0;
2638 consing_since_gc = 0;
2639 gc_cons_threshold = 100000 * sizeof (Lisp_Object);
2640 #ifdef VIRT_ADDR_VARIES
2641 malloc_sbrk_unused = 1<<22; /* A large number */
2642 malloc_sbrk_used = 100000; /* as reasonable as any number */
2643 #endif /* VIRT_ADDR_VARIES */
2646 init_alloc ()
2648 gcprolist = 0;
2651 void
2652 syms_of_alloc ()
2654 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold,
2655 "*Number of bytes of consing between garbage collections.\n\
2656 Garbage collection can happen automatically once this many bytes have been\n\
2657 allocated since the last garbage collection. All data types count.\n\n\
2658 Garbage collection happens automatically only when `eval' is called.\n\n\
2659 By binding this temporarily to a large number, you can effectively\n\
2660 prevent garbage collection during a part of the program.");
2662 DEFVAR_INT ("pure-bytes-used", &pureptr,
2663 "Number of bytes of sharable Lisp data allocated so far.");
2665 DEFVAR_INT ("cons-cells-consed", &cons_cells_consed,
2666 "Number of cons cells that have been consed so far.");
2668 DEFVAR_INT ("floats-consed", &floats_consed,
2669 "Number of floats that have been consed so far.");
2671 DEFVAR_INT ("vector-cells-consed", &vector_cells_consed,
2672 "Number of vector cells that have been consed so far.");
2674 DEFVAR_INT ("symbols-consed", &symbols_consed,
2675 "Number of symbols that have been consed so far.");
2677 DEFVAR_INT ("string-chars-consed", &string_chars_consed,
2678 "Number of string characters that have been consed so far.");
2680 DEFVAR_INT ("misc-objects-consed", &misc_objects_consed,
2681 "Number of miscellaneous objects that have been consed so far.");
2683 DEFVAR_INT ("intervals-consed", &intervals_consed,
2684 "Number of intervals that have been consed so far.");
2686 #if 0
2687 DEFVAR_INT ("data-bytes-used", &malloc_sbrk_used,
2688 "Number of bytes of unshared memory allocated in this session.");
2690 DEFVAR_INT ("data-bytes-free", &malloc_sbrk_unused,
2691 "Number of bytes of unshared memory remaining available in this session.");
2692 #endif
2694 DEFVAR_LISP ("purify-flag", &Vpurify_flag,
2695 "Non-nil means loading Lisp code in order to dump an executable.\n\
2696 This means that certain objects should be allocated in shared (pure) space.");
2698 DEFVAR_INT ("undo-limit", &undo_limit,
2699 "Keep no more undo information once it exceeds this size.\n\
2700 This limit is applied when garbage collection happens.\n\
2701 The size is counted as the number of bytes occupied,\n\
2702 which includes both saved text and other data.");
2703 undo_limit = 20000;
2705 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit,
2706 "Don't keep more than this much size of undo information.\n\
2707 A command which pushes past this size is itself forgotten.\n\
2708 This limit is applied when garbage collection happens.\n\
2709 The size is counted as the number of bytes occupied,\n\
2710 which includes both saved text and other data.");
2711 undo_strong_limit = 30000;
2713 DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages,
2714 "Non-nil means display messages at start and end of garbage collection.");
2715 garbage_collection_messages = 0;
2717 /* We build this in advance because if we wait until we need it, we might
2718 not be able to allocate the memory to hold it. */
2719 memory_signal_data
2720 = Fcons (Qerror, Fcons (build_string ("Memory exhausted--use M-x save-some-buffers RET"), Qnil));
2721 staticpro (&memory_signal_data);
2723 staticpro (&Qgc_cons_threshold);
2724 Qgc_cons_threshold = intern ("gc-cons-threshold");
2726 staticpro (&Qchar_table_extra_slots);
2727 Qchar_table_extra_slots = intern ("char-table-extra-slots");
2729 defsubr (&Scons);
2730 defsubr (&Slist);
2731 defsubr (&Svector);
2732 defsubr (&Smake_byte_code);
2733 defsubr (&Smake_list);
2734 defsubr (&Smake_vector);
2735 defsubr (&Smake_char_table);
2736 defsubr (&Smake_string);
2737 defsubr (&Smake_bool_vector);
2738 defsubr (&Smake_symbol);
2739 defsubr (&Smake_marker);
2740 defsubr (&Spurecopy);
2741 defsubr (&Sgarbage_collect);
2742 defsubr (&Smemory_limit);
2743 defsubr (&Smemory_use_counts);