.
[emacs.git] / src / alloc.c
blob7120cae26b6e16d65be1f1ec8aa5fa336fbed786
1 /* Storage allocation and gc for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 86, 88, 93, 94, 95, 97, 1998
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Note that this declares bzero on OSF/1. How dumb. */
23 #include <signal.h>
25 #include <config.h>
27 #ifdef STDC_HEADERS
28 #include <stdlib.h>
29 #endif
31 #include "lisp.h"
32 #include "intervals.h"
33 #include "puresize.h"
34 #ifndef standalone
35 #include "buffer.h"
36 #include "window.h"
37 #include "frame.h"
38 #include "blockinput.h"
39 #include "keyboard.h"
40 #include "charset.h"
41 #endif
43 #include "syssignal.h"
45 extern char *sbrk ();
47 #ifdef DOUG_LEA_MALLOC
48 #include <malloc.h>
49 #define __malloc_size_t int
51 /* Specify maximum number of areas to mmap.
52 It would be nice to use a value that explicitly
53 means "no limit". */
54 #define MMAP_MAX_AREAS 100000000
56 #else
57 /* The following come from gmalloc.c. */
59 #if defined (__STDC__) && __STDC__
60 #include <stddef.h>
61 #define __malloc_size_t size_t
62 #else
63 #define __malloc_size_t unsigned int
64 #endif
65 extern __malloc_size_t _bytes_used;
66 extern int __malloc_extra_blocks;
67 #endif /* !defined(DOUG_LEA_MALLOC) */
69 #define max(A,B) ((A) > (B) ? (A) : (B))
70 #define min(A,B) ((A) < (B) ? (A) : (B))
72 /* Macro to verify that storage intended for Lisp objects is not
73 out of range to fit in the space for a pointer.
74 ADDRESS is the start of the block, and SIZE
75 is the amount of space within which objects can start. */
76 #define VALIDATE_LISP_STORAGE(address, size) \
77 do \
78 { \
79 Lisp_Object val; \
80 XSETCONS (val, (char *) address + size); \
81 if ((char *) XCONS (val) != (char *) address + size) \
82 { \
83 xfree (address); \
84 memory_full (); \
85 } \
86 } while (0)
88 /* Value of _bytes_used, when spare_memory was freed. */
89 static __malloc_size_t bytes_used_when_full;
91 /* Number of bytes of consing done since the last gc */
92 int consing_since_gc;
94 /* Count the amount of consing of various sorts of space. */
95 int cons_cells_consed;
96 int floats_consed;
97 int vector_cells_consed;
98 int symbols_consed;
99 int string_chars_consed;
100 int misc_objects_consed;
101 int intervals_consed;
103 /* Number of bytes of consing since gc before another gc should be done. */
104 int gc_cons_threshold;
106 /* Nonzero during gc */
107 int gc_in_progress;
109 /* Nonzero means display messages at beginning and end of GC. */
110 int garbage_collection_messages;
112 #ifndef VIRT_ADDR_VARIES
113 extern
114 #endif /* VIRT_ADDR_VARIES */
115 int malloc_sbrk_used;
117 #ifndef VIRT_ADDR_VARIES
118 extern
119 #endif /* VIRT_ADDR_VARIES */
120 int malloc_sbrk_unused;
122 /* Two limits controlling how much undo information to keep. */
123 int undo_limit;
124 int undo_strong_limit;
126 int total_conses, total_markers, total_symbols, total_string_size, total_vector_size;
127 int total_free_conses, total_free_markers, total_free_symbols;
128 #ifdef LISP_FLOAT_TYPE
129 int total_free_floats, total_floats;
130 #endif /* LISP_FLOAT_TYPE */
132 /* Points to memory space allocated as "spare",
133 to be freed if we run out of memory. */
134 static char *spare_memory;
136 /* Amount of spare memory to keep in reserve. */
137 #define SPARE_MEMORY (1 << 14)
139 /* Number of extra blocks malloc should get when it needs more core. */
140 static int malloc_hysteresis;
142 /* Nonzero when malloc is called for allocating Lisp object space. */
143 int allocating_for_lisp;
145 /* Non-nil means defun should do purecopy on the function definition */
146 Lisp_Object Vpurify_flag;
148 #ifndef HAVE_SHM
149 EMACS_INT pure[PURESIZE / sizeof (EMACS_INT)] = {0,}; /* Force it into data space! */
150 #define PUREBEG (char *) pure
151 #else
152 #define pure PURE_SEG_BITS /* Use shared memory segment */
153 #define PUREBEG (char *)PURE_SEG_BITS
155 /* This variable is used only by the XPNTR macro when HAVE_SHM is
156 defined. If we used the PURESIZE macro directly there, that would
157 make most of emacs dependent on puresize.h, which we don't want -
158 you should be able to change that without too much recompilation.
159 So map_in_data initializes pure_size, and the dependencies work
160 out. */
161 EMACS_INT pure_size;
162 #endif /* not HAVE_SHM */
164 /* Index in pure at which next pure object will be allocated. */
165 int pureptr;
167 /* If nonzero, this is a warning delivered by malloc and not yet displayed. */
168 char *pending_malloc_warning;
170 /* Pre-computed signal argument for use when memory is exhausted. */
171 Lisp_Object memory_signal_data;
173 /* Maximum amount of C stack to save when a GC happens. */
175 #ifndef MAX_SAVE_STACK
176 #define MAX_SAVE_STACK 16000
177 #endif
179 /* Define DONT_COPY_FLAG to be some bit which will always be zero in a
180 pointer to a Lisp_Object, when that pointer is viewed as an integer.
181 (On most machines, pointers are even, so we can use the low bit.
182 Word-addressable architectures may need to override this in the m-file.)
183 When linking references to small strings through the size field, we
184 use this slot to hold the bit that would otherwise be interpreted as
185 the GC mark bit. */
186 #ifndef DONT_COPY_FLAG
187 #define DONT_COPY_FLAG 1
188 #endif /* no DONT_COPY_FLAG */
190 /* Buffer in which we save a copy of the C stack at each GC. */
192 char *stack_copy;
193 int stack_copy_size;
195 /* Non-zero means ignore malloc warnings. Set during initialization. */
196 int ignore_warnings;
198 Lisp_Object Qgc_cons_threshold, Qchar_table_extra_slots;
200 static void mark_buffer (), mark_kboards ();
201 static void gc_sweep ();
202 static void compact_strings ();
203 static void mark_glyph_matrix P_ ((struct glyph_matrix *));
204 static void mark_face_cache P_ ((struct face_cache *));
205 #if 0
206 static void clear_marks ();
207 #endif
209 #ifdef HAVE_WINDOW_SYSTEM
210 static void mark_image P_ ((struct image *));
211 static void mark_image_cache P_ ((struct frame *));
212 #endif /* HAVE_WINDOW_SYSTEM */
215 extern int message_enable_multibyte;
217 /* Versions of malloc and realloc that print warnings as memory gets full. */
219 Lisp_Object
220 malloc_warning_1 (str)
221 Lisp_Object str;
223 Fprinc (str, Vstandard_output);
224 write_string ("\nKilling some buffers may delay running out of memory.\n", -1);
225 write_string ("However, certainly by the time you receive the 95% warning,\n", -1);
226 write_string ("you should clean up, kill this Emacs, and start a new one.", -1);
227 return Qnil;
230 /* malloc calls this if it finds we are near exhausting storage */
232 void
233 malloc_warning (str)
234 char *str;
236 pending_malloc_warning = str;
239 void
240 display_malloc_warning ()
242 register Lisp_Object val;
244 val = build_string (pending_malloc_warning);
245 pending_malloc_warning = 0;
246 internal_with_output_to_temp_buffer (" *Danger*", malloc_warning_1, val);
249 #ifdef DOUG_LEA_MALLOC
250 # define BYTES_USED (mallinfo ().arena)
251 #else
252 # define BYTES_USED _bytes_used
253 #endif
255 /* Called if malloc returns zero */
257 void
258 memory_full ()
260 #ifndef SYSTEM_MALLOC
261 bytes_used_when_full = BYTES_USED;
262 #endif
264 /* The first time we get here, free the spare memory. */
265 if (spare_memory)
267 free (spare_memory);
268 spare_memory = 0;
271 /* This used to call error, but if we've run out of memory, we could get
272 infinite recursion trying to build the string. */
273 while (1)
274 Fsignal (Qnil, memory_signal_data);
277 /* Called if we can't allocate relocatable space for a buffer. */
279 void
280 buffer_memory_full ()
282 /* If buffers use the relocating allocator,
283 no need to free spare_memory, because we may have plenty of malloc
284 space left that we could get, and if we don't, the malloc that fails
285 will itself cause spare_memory to be freed.
286 If buffers don't use the relocating allocator,
287 treat this like any other failing malloc. */
289 #ifndef REL_ALLOC
290 memory_full ();
291 #endif
293 /* This used to call error, but if we've run out of memory, we could get
294 infinite recursion trying to build the string. */
295 while (1)
296 Fsignal (Qerror, memory_signal_data);
299 /* Like malloc routines but check for no memory and block interrupt input. */
301 long *
302 xmalloc (size)
303 int size;
305 register long *val;
307 BLOCK_INPUT;
308 val = (long *) malloc (size);
309 UNBLOCK_INPUT;
311 if (!val && size) memory_full ();
312 return val;
315 long *
316 xrealloc (block, size)
317 long *block;
318 int size;
320 register long *val;
322 BLOCK_INPUT;
323 /* We must call malloc explicitly when BLOCK is 0, since some
324 reallocs don't do this. */
325 if (! block)
326 val = (long *) malloc (size);
327 else
328 val = (long *) realloc (block, size);
329 UNBLOCK_INPUT;
331 if (!val && size) memory_full ();
332 return val;
335 void
336 xfree (block)
337 long *block;
339 BLOCK_INPUT;
340 free (block);
341 UNBLOCK_INPUT;
344 /* Like malloc but used for allocating Lisp data. */
346 long *
347 lisp_malloc (size)
348 int size;
350 register long *val;
352 BLOCK_INPUT;
353 allocating_for_lisp++;
354 val = (long *) malloc (size);
355 allocating_for_lisp--;
356 UNBLOCK_INPUT;
358 if (!val && size) memory_full ();
359 return val;
362 void
363 lisp_free (block)
364 long *block;
366 BLOCK_INPUT;
367 allocating_for_lisp++;
368 free (block);
369 allocating_for_lisp--;
370 UNBLOCK_INPUT;
373 /* Arranging to disable input signals while we're in malloc.
375 This only works with GNU malloc. To help out systems which can't
376 use GNU malloc, all the calls to malloc, realloc, and free
377 elsewhere in the code should be inside a BLOCK_INPUT/UNBLOCK_INPUT
378 pairs; unfortunately, we have no idea what C library functions
379 might call malloc, so we can't really protect them unless you're
380 using GNU malloc. Fortunately, most of the major operating can use
381 GNU malloc. */
383 #ifndef SYSTEM_MALLOC
384 extern void * (*__malloc_hook) ();
385 static void * (*old_malloc_hook) ();
386 extern void * (*__realloc_hook) ();
387 static void * (*old_realloc_hook) ();
388 extern void (*__free_hook) ();
389 static void (*old_free_hook) ();
391 /* This function is used as the hook for free to call. */
393 static void
394 emacs_blocked_free (ptr)
395 void *ptr;
397 BLOCK_INPUT;
398 __free_hook = old_free_hook;
399 free (ptr);
400 /* If we released our reserve (due to running out of memory),
401 and we have a fair amount free once again,
402 try to set aside another reserve in case we run out once more. */
403 if (spare_memory == 0
404 /* Verify there is enough space that even with the malloc
405 hysteresis this call won't run out again.
406 The code here is correct as long as SPARE_MEMORY
407 is substantially larger than the block size malloc uses. */
408 && (bytes_used_when_full
409 > BYTES_USED + max (malloc_hysteresis, 4) * SPARE_MEMORY))
410 spare_memory = (char *) malloc (SPARE_MEMORY);
412 __free_hook = emacs_blocked_free;
413 UNBLOCK_INPUT;
416 /* If we released our reserve (due to running out of memory),
417 and we have a fair amount free once again,
418 try to set aside another reserve in case we run out once more.
420 This is called when a relocatable block is freed in ralloc.c. */
422 void
423 refill_memory_reserve ()
425 if (spare_memory == 0)
426 spare_memory = (char *) malloc (SPARE_MEMORY);
429 /* This function is the malloc hook that Emacs uses. */
431 static void *
432 emacs_blocked_malloc (size)
433 unsigned size;
435 void *value;
437 BLOCK_INPUT;
438 __malloc_hook = old_malloc_hook;
439 #ifdef DOUG_LEA_MALLOC
440 mallopt (M_TOP_PAD, malloc_hysteresis * 4096);
441 #else
442 __malloc_extra_blocks = malloc_hysteresis;
443 #endif
444 value = (void *) malloc (size);
445 __malloc_hook = emacs_blocked_malloc;
446 UNBLOCK_INPUT;
448 return value;
451 static void *
452 emacs_blocked_realloc (ptr, size)
453 void *ptr;
454 unsigned size;
456 void *value;
458 BLOCK_INPUT;
459 __realloc_hook = old_realloc_hook;
460 value = (void *) realloc (ptr, size);
461 __realloc_hook = emacs_blocked_realloc;
462 UNBLOCK_INPUT;
464 return value;
467 void
468 uninterrupt_malloc ()
470 if (__free_hook != emacs_blocked_free)
471 old_free_hook = __free_hook;
472 __free_hook = emacs_blocked_free;
474 if (__malloc_hook != emacs_blocked_malloc)
475 old_malloc_hook = __malloc_hook;
476 __malloc_hook = emacs_blocked_malloc;
478 if (__realloc_hook != emacs_blocked_realloc)
479 old_realloc_hook = __realloc_hook;
480 __realloc_hook = emacs_blocked_realloc;
482 #endif
484 /* Interval allocation. */
486 #ifdef USE_TEXT_PROPERTIES
487 #define INTERVAL_BLOCK_SIZE \
488 ((1020 - sizeof (struct interval_block *)) / sizeof (struct interval))
490 struct interval_block
492 struct interval_block *next;
493 struct interval intervals[INTERVAL_BLOCK_SIZE];
496 struct interval_block *interval_block;
497 static int interval_block_index;
499 INTERVAL interval_free_list;
501 /* Total number of interval blocks now in use. */
502 int n_interval_blocks;
504 static void
505 init_intervals ()
507 interval_block
508 = (struct interval_block *) lisp_malloc (sizeof (struct interval_block));
509 interval_block->next = 0;
510 bzero ((char *) interval_block->intervals, sizeof interval_block->intervals);
511 interval_block_index = 0;
512 interval_free_list = 0;
513 n_interval_blocks = 1;
516 #define INIT_INTERVALS init_intervals ()
518 INTERVAL
519 make_interval ()
521 INTERVAL val;
523 if (interval_free_list)
525 val = interval_free_list;
526 interval_free_list = interval_free_list->parent;
528 else
530 if (interval_block_index == INTERVAL_BLOCK_SIZE)
532 register struct interval_block *newi;
534 newi = (struct interval_block *) lisp_malloc (sizeof (struct interval_block));
536 VALIDATE_LISP_STORAGE (newi, sizeof *newi);
537 newi->next = interval_block;
538 interval_block = newi;
539 interval_block_index = 0;
540 n_interval_blocks++;
542 val = &interval_block->intervals[interval_block_index++];
544 consing_since_gc += sizeof (struct interval);
545 intervals_consed++;
546 RESET_INTERVAL (val);
547 return val;
550 static int total_free_intervals, total_intervals;
552 /* Mark the pointers of one interval. */
554 static void
555 mark_interval (i, dummy)
556 register INTERVAL i;
557 Lisp_Object dummy;
559 if (XMARKBIT (i->plist))
560 abort ();
561 mark_object (&i->plist);
562 XMARK (i->plist);
565 static void
566 mark_interval_tree (tree)
567 register INTERVAL tree;
569 /* No need to test if this tree has been marked already; this
570 function is always called through the MARK_INTERVAL_TREE macro,
571 which takes care of that. */
573 /* XMARK expands to an assignment; the LHS of an assignment can't be
574 a cast. */
575 XMARK (* (Lisp_Object *) &tree->parent);
577 traverse_intervals (tree, 1, 0, mark_interval, Qnil);
580 #define MARK_INTERVAL_TREE(i) \
581 do { \
582 if (!NULL_INTERVAL_P (i) \
583 && ! XMARKBIT (*(Lisp_Object *) &i->parent)) \
584 mark_interval_tree (i); \
585 } while (0)
587 /* The oddity in the call to XUNMARK is necessary because XUNMARK
588 expands to an assignment to its argument, and most C compilers don't
589 support casts on the left operand of `='. */
590 #define UNMARK_BALANCE_INTERVALS(i) \
592 if (! NULL_INTERVAL_P (i)) \
594 XUNMARK (* (Lisp_Object *) (&(i)->parent)); \
595 (i) = balance_intervals (i); \
599 #else /* no interval use */
601 #define INIT_INTERVALS
603 #define UNMARK_BALANCE_INTERVALS(i)
604 #define MARK_INTERVAL_TREE(i)
606 #endif /* no interval use */
608 /* Floating point allocation. */
610 #ifdef LISP_FLOAT_TYPE
611 /* Allocation of float cells, just like conses */
612 /* We store float cells inside of float_blocks, allocating a new
613 float_block with malloc whenever necessary. Float cells reclaimed by
614 GC are put on a free list to be reallocated before allocating
615 any new float cells from the latest float_block.
617 Each float_block is just under 1020 bytes long,
618 since malloc really allocates in units of powers of two
619 and uses 4 bytes for its own overhead. */
621 #define FLOAT_BLOCK_SIZE \
622 ((1020 - sizeof (struct float_block *)) / sizeof (struct Lisp_Float))
624 struct float_block
626 struct float_block *next;
627 struct Lisp_Float floats[FLOAT_BLOCK_SIZE];
630 struct float_block *float_block;
631 int float_block_index;
633 /* Total number of float blocks now in use. */
634 int n_float_blocks;
636 struct Lisp_Float *float_free_list;
638 void
639 init_float ()
641 float_block = (struct float_block *) lisp_malloc (sizeof (struct float_block));
642 float_block->next = 0;
643 bzero ((char *) float_block->floats, sizeof float_block->floats);
644 float_block_index = 0;
645 float_free_list = 0;
646 n_float_blocks = 1;
649 /* Explicitly free a float cell. */
650 void
651 free_float (ptr)
652 struct Lisp_Float *ptr;
654 *(struct Lisp_Float **)&ptr->data = float_free_list;
655 float_free_list = ptr;
658 Lisp_Object
659 make_float (float_value)
660 double float_value;
662 register Lisp_Object val;
664 if (float_free_list)
666 /* We use the data field for chaining the free list
667 so that we won't use the same field that has the mark bit. */
668 XSETFLOAT (val, float_free_list);
669 float_free_list = *(struct Lisp_Float **)&float_free_list->data;
671 else
673 if (float_block_index == FLOAT_BLOCK_SIZE)
675 register struct float_block *new;
677 new = (struct float_block *) lisp_malloc (sizeof (struct float_block));
678 VALIDATE_LISP_STORAGE (new, sizeof *new);
679 new->next = float_block;
680 float_block = new;
681 float_block_index = 0;
682 n_float_blocks++;
684 XSETFLOAT (val, &float_block->floats[float_block_index++]);
686 XFLOAT_DATA (val) = float_value;
687 XSETFASTINT (XFLOAT (val)->type, 0); /* bug chasing -wsr */
688 consing_since_gc += sizeof (struct Lisp_Float);
689 floats_consed++;
690 return val;
693 #endif /* LISP_FLOAT_TYPE */
695 /* Allocation of cons cells */
696 /* We store cons cells inside of cons_blocks, allocating a new
697 cons_block with malloc whenever necessary. Cons cells reclaimed by
698 GC are put on a free list to be reallocated before allocating
699 any new cons cells from the latest cons_block.
701 Each cons_block is just under 1020 bytes long,
702 since malloc really allocates in units of powers of two
703 and uses 4 bytes for its own overhead. */
705 #define CONS_BLOCK_SIZE \
706 ((1020 - sizeof (struct cons_block *)) / sizeof (struct Lisp_Cons))
708 struct cons_block
710 struct cons_block *next;
711 struct Lisp_Cons conses[CONS_BLOCK_SIZE];
714 struct cons_block *cons_block;
715 int cons_block_index;
717 struct Lisp_Cons *cons_free_list;
719 /* Total number of cons blocks now in use. */
720 int n_cons_blocks;
722 void
723 init_cons ()
725 cons_block = (struct cons_block *) lisp_malloc (sizeof (struct cons_block));
726 cons_block->next = 0;
727 bzero ((char *) cons_block->conses, sizeof cons_block->conses);
728 cons_block_index = 0;
729 cons_free_list = 0;
730 n_cons_blocks = 1;
733 /* Explicitly free a cons cell. */
735 void
736 free_cons (ptr)
737 struct Lisp_Cons *ptr;
739 *(struct Lisp_Cons **)&ptr->cdr = cons_free_list;
740 cons_free_list = ptr;
743 DEFUN ("cons", Fcons, Scons, 2, 2, 0,
744 "Create a new cons, give it CAR and CDR as components, and return it.")
745 (car, cdr)
746 Lisp_Object car, cdr;
748 register Lisp_Object val;
750 if (cons_free_list)
752 /* We use the cdr for chaining the free list
753 so that we won't use the same field that has the mark bit. */
754 XSETCONS (val, cons_free_list);
755 cons_free_list = *(struct Lisp_Cons **)&cons_free_list->cdr;
757 else
759 if (cons_block_index == CONS_BLOCK_SIZE)
761 register struct cons_block *new;
762 new = (struct cons_block *) lisp_malloc (sizeof (struct cons_block));
763 VALIDATE_LISP_STORAGE (new, sizeof *new);
764 new->next = cons_block;
765 cons_block = new;
766 cons_block_index = 0;
767 n_cons_blocks++;
769 XSETCONS (val, &cons_block->conses[cons_block_index++]);
771 XCAR (val) = car;
772 XCDR (val) = cdr;
773 consing_since_gc += sizeof (struct Lisp_Cons);
774 cons_cells_consed++;
775 return val;
778 /* Make a list of 2, 3, 4 or 5 specified objects. */
780 Lisp_Object
781 list2 (arg1, arg2)
782 Lisp_Object arg1, arg2;
784 return Fcons (arg1, Fcons (arg2, Qnil));
787 Lisp_Object
788 list3 (arg1, arg2, arg3)
789 Lisp_Object arg1, arg2, arg3;
791 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Qnil)));
794 Lisp_Object
795 list4 (arg1, arg2, arg3, arg4)
796 Lisp_Object arg1, arg2, arg3, arg4;
798 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4, Qnil))));
801 Lisp_Object
802 list5 (arg1, arg2, arg3, arg4, arg5)
803 Lisp_Object arg1, arg2, arg3, arg4, arg5;
805 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4,
806 Fcons (arg5, Qnil)))));
809 DEFUN ("list", Flist, Slist, 0, MANY, 0,
810 "Return a newly created list with specified arguments as elements.\n\
811 Any number of arguments, even zero arguments, are allowed.")
812 (nargs, args)
813 int nargs;
814 register Lisp_Object *args;
816 register Lisp_Object val;
817 val = Qnil;
819 while (nargs > 0)
821 nargs--;
822 val = Fcons (args[nargs], val);
824 return val;
827 DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0,
828 "Return a newly created list of length LENGTH, with each element being INIT.")
829 (length, init)
830 register Lisp_Object length, init;
832 register Lisp_Object val;
833 register int size;
835 CHECK_NATNUM (length, 0);
836 size = XFASTINT (length);
838 val = Qnil;
839 while (size-- > 0)
840 val = Fcons (init, val);
841 return val;
844 /* Allocation of vectors */
846 struct Lisp_Vector *all_vectors;
848 /* Total number of vectorlike objects now in use. */
849 int n_vectors;
851 struct Lisp_Vector *
852 allocate_vectorlike (len)
853 EMACS_INT len;
855 struct Lisp_Vector *p;
857 #ifdef DOUG_LEA_MALLOC
858 /* Prevent mmap'ing the chunk (which is potentially very large). */
859 mallopt (M_MMAP_MAX, 0);
860 #endif
861 p = (struct Lisp_Vector *)lisp_malloc (sizeof (struct Lisp_Vector)
862 + (len - 1) * sizeof (Lisp_Object));
863 #ifdef DOUG_LEA_MALLOC
864 /* Back to a reasonable maximum of mmap'ed areas. */
865 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
866 #endif
867 VALIDATE_LISP_STORAGE (p, 0);
868 consing_since_gc += (sizeof (struct Lisp_Vector)
869 + (len - 1) * sizeof (Lisp_Object));
870 vector_cells_consed += len;
871 n_vectors++;
873 p->next = all_vectors;
874 all_vectors = p;
875 return p;
878 DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0,
879 "Return a newly created vector of length LENGTH, with each element being INIT.\n\
880 See also the function `vector'.")
881 (length, init)
882 register Lisp_Object length, init;
884 Lisp_Object vector;
885 register EMACS_INT sizei;
886 register int index;
887 register struct Lisp_Vector *p;
889 CHECK_NATNUM (length, 0);
890 sizei = XFASTINT (length);
892 p = allocate_vectorlike (sizei);
893 p->size = sizei;
894 for (index = 0; index < sizei; index++)
895 p->contents[index] = init;
897 XSETVECTOR (vector, p);
898 return vector;
901 DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0,
902 "Return a newly created char-table, with purpose PURPOSE.\n\
903 Each element is initialized to INIT, which defaults to nil.\n\
904 PURPOSE should be a symbol which has a `char-table-extra-slots' property.\n\
905 The property's value should be an integer between 0 and 10.")
906 (purpose, init)
907 register Lisp_Object purpose, init;
909 Lisp_Object vector;
910 Lisp_Object n;
911 CHECK_SYMBOL (purpose, 1);
912 n = Fget (purpose, Qchar_table_extra_slots);
913 CHECK_NUMBER (n, 0);
914 if (XINT (n) < 0 || XINT (n) > 10)
915 args_out_of_range (n, Qnil);
916 /* Add 2 to the size for the defalt and parent slots. */
917 vector = Fmake_vector (make_number (CHAR_TABLE_STANDARD_SLOTS + XINT (n)),
918 init);
919 XCHAR_TABLE (vector)->top = Qt;
920 XCHAR_TABLE (vector)->parent = Qnil;
921 XCHAR_TABLE (vector)->purpose = purpose;
922 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
923 return vector;
926 /* Return a newly created sub char table with default value DEFALT.
927 Since a sub char table does not appear as a top level Emacs Lisp
928 object, we don't need a Lisp interface to make it. */
930 Lisp_Object
931 make_sub_char_table (defalt)
932 Lisp_Object defalt;
934 Lisp_Object vector
935 = Fmake_vector (make_number (SUB_CHAR_TABLE_STANDARD_SLOTS), Qnil);
936 XCHAR_TABLE (vector)->top = Qnil;
937 XCHAR_TABLE (vector)->defalt = defalt;
938 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
939 return vector;
942 DEFUN ("vector", Fvector, Svector, 0, MANY, 0,
943 "Return a newly created vector with specified arguments as elements.\n\
944 Any number of arguments, even zero arguments, are allowed.")
945 (nargs, args)
946 register int nargs;
947 Lisp_Object *args;
949 register Lisp_Object len, val;
950 register int index;
951 register struct Lisp_Vector *p;
953 XSETFASTINT (len, nargs);
954 val = Fmake_vector (len, Qnil);
955 p = XVECTOR (val);
956 for (index = 0; index < nargs; index++)
957 p->contents[index] = args[index];
958 return val;
961 DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0,
962 "Create a byte-code object with specified arguments as elements.\n\
963 The arguments should be the arglist, bytecode-string, constant vector,\n\
964 stack size, (optional) doc string, and (optional) interactive spec.\n\
965 The first four arguments are required; at most six have any\n\
966 significance.")
967 (nargs, args)
968 register int nargs;
969 Lisp_Object *args;
971 register Lisp_Object len, val;
972 register int index;
973 register struct Lisp_Vector *p;
975 XSETFASTINT (len, nargs);
976 if (!NILP (Vpurify_flag))
977 val = make_pure_vector ((EMACS_INT) nargs);
978 else
979 val = Fmake_vector (len, Qnil);
980 p = XVECTOR (val);
981 for (index = 0; index < nargs; index++)
983 if (!NILP (Vpurify_flag))
984 args[index] = Fpurecopy (args[index]);
985 p->contents[index] = args[index];
987 XSETCOMPILED (val, p);
988 return val;
991 /* Allocation of symbols.
992 Just like allocation of conses!
994 Each symbol_block is just under 1020 bytes long,
995 since malloc really allocates in units of powers of two
996 and uses 4 bytes for its own overhead. */
998 #define SYMBOL_BLOCK_SIZE \
999 ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol))
1001 struct symbol_block
1003 struct symbol_block *next;
1004 struct Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE];
1007 struct symbol_block *symbol_block;
1008 int symbol_block_index;
1010 struct Lisp_Symbol *symbol_free_list;
1012 /* Total number of symbol blocks now in use. */
1013 int n_symbol_blocks;
1015 void
1016 init_symbol ()
1018 symbol_block = (struct symbol_block *) lisp_malloc (sizeof (struct symbol_block));
1019 symbol_block->next = 0;
1020 bzero ((char *) symbol_block->symbols, sizeof symbol_block->symbols);
1021 symbol_block_index = 0;
1022 symbol_free_list = 0;
1023 n_symbol_blocks = 1;
1026 DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0,
1027 "Return a newly allocated uninterned symbol whose name is NAME.\n\
1028 Its value and function definition are void, and its property list is nil.")
1029 (name)
1030 Lisp_Object name;
1032 register Lisp_Object val;
1033 register struct Lisp_Symbol *p;
1035 CHECK_STRING (name, 0);
1037 if (symbol_free_list)
1039 XSETSYMBOL (val, symbol_free_list);
1040 symbol_free_list = *(struct Lisp_Symbol **)&symbol_free_list->value;
1042 else
1044 if (symbol_block_index == SYMBOL_BLOCK_SIZE)
1046 struct symbol_block *new;
1047 new = (struct symbol_block *) lisp_malloc (sizeof (struct symbol_block));
1048 VALIDATE_LISP_STORAGE (new, sizeof *new);
1049 new->next = symbol_block;
1050 symbol_block = new;
1051 symbol_block_index = 0;
1052 n_symbol_blocks++;
1054 XSETSYMBOL (val, &symbol_block->symbols[symbol_block_index++]);
1056 p = XSYMBOL (val);
1057 p->name = XSTRING (name);
1058 p->obarray = Qnil;
1059 p->plist = Qnil;
1060 p->value = Qunbound;
1061 p->function = Qunbound;
1062 p->next = 0;
1063 consing_since_gc += sizeof (struct Lisp_Symbol);
1064 symbols_consed++;
1065 return val;
1068 /* Allocation of markers and other objects that share that structure.
1069 Works like allocation of conses. */
1071 #define MARKER_BLOCK_SIZE \
1072 ((1020 - sizeof (struct marker_block *)) / sizeof (union Lisp_Misc))
1074 struct marker_block
1076 struct marker_block *next;
1077 union Lisp_Misc markers[MARKER_BLOCK_SIZE];
1080 struct marker_block *marker_block;
1081 int marker_block_index;
1083 union Lisp_Misc *marker_free_list;
1085 /* Total number of marker blocks now in use. */
1086 int n_marker_blocks;
1088 void
1089 init_marker ()
1091 marker_block = (struct marker_block *) lisp_malloc (sizeof (struct marker_block));
1092 marker_block->next = 0;
1093 bzero ((char *) marker_block->markers, sizeof marker_block->markers);
1094 marker_block_index = 0;
1095 marker_free_list = 0;
1096 n_marker_blocks = 1;
1099 /* Return a newly allocated Lisp_Misc object, with no substructure. */
1100 Lisp_Object
1101 allocate_misc ()
1103 Lisp_Object val;
1105 if (marker_free_list)
1107 XSETMISC (val, marker_free_list);
1108 marker_free_list = marker_free_list->u_free.chain;
1110 else
1112 if (marker_block_index == MARKER_BLOCK_SIZE)
1114 struct marker_block *new;
1115 new = (struct marker_block *) lisp_malloc (sizeof (struct marker_block));
1116 VALIDATE_LISP_STORAGE (new, sizeof *new);
1117 new->next = marker_block;
1118 marker_block = new;
1119 marker_block_index = 0;
1120 n_marker_blocks++;
1122 XSETMISC (val, &marker_block->markers[marker_block_index++]);
1124 consing_since_gc += sizeof (union Lisp_Misc);
1125 misc_objects_consed++;
1126 return val;
1129 DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
1130 "Return a newly allocated marker which does not point at any place.")
1133 register Lisp_Object val;
1134 register struct Lisp_Marker *p;
1136 val = allocate_misc ();
1137 XMISCTYPE (val) = Lisp_Misc_Marker;
1138 p = XMARKER (val);
1139 p->buffer = 0;
1140 p->bytepos = 0;
1141 p->charpos = 0;
1142 p->chain = Qnil;
1143 p->insertion_type = 0;
1144 return val;
1147 /* Put MARKER back on the free list after using it temporarily. */
1149 void
1150 free_marker (marker)
1151 Lisp_Object marker;
1153 unchain_marker (marker);
1155 XMISC (marker)->u_marker.type = Lisp_Misc_Free;
1156 XMISC (marker)->u_free.chain = marker_free_list;
1157 marker_free_list = XMISC (marker);
1159 total_free_markers++;
1162 /* Allocation of strings */
1164 /* Strings reside inside of string_blocks. The entire data of the string,
1165 both the size and the contents, live in part of the `chars' component of a string_block.
1166 The `pos' component is the index within `chars' of the first free byte.
1168 first_string_block points to the first string_block ever allocated.
1169 Each block points to the next one with its `next' field.
1170 The `prev' fields chain in reverse order.
1171 The last one allocated is the one currently being filled.
1172 current_string_block points to it.
1174 The string_blocks that hold individual large strings
1175 go in a separate chain, started by large_string_blocks. */
1178 /* String blocks contain this many useful bytes.
1179 8188 is power of 2, minus 4 for malloc overhead. */
1180 #define STRING_BLOCK_SIZE (8188 - sizeof (struct string_block_head))
1182 /* A string bigger than this gets its own specially-made string block
1183 if it doesn't fit in the current one. */
1184 #define STRING_BLOCK_OUTSIZE 1024
1186 struct string_block_head
1188 struct string_block *next, *prev;
1189 EMACS_INT pos;
1192 struct string_block
1194 struct string_block *next, *prev;
1195 EMACS_INT pos;
1196 char chars[STRING_BLOCK_SIZE];
1199 /* This points to the string block we are now allocating strings. */
1201 struct string_block *current_string_block;
1203 /* This points to the oldest string block, the one that starts the chain. */
1205 struct string_block *first_string_block;
1207 /* Last string block in chain of those made for individual large strings. */
1209 struct string_block *large_string_blocks;
1211 /* If SIZE is the length of a string, this returns how many bytes
1212 the string occupies in a string_block (including padding). */
1214 #define STRING_FULLSIZE(size) (((size) + 1 + STRING_BASE_SIZE + STRING_PAD - 1) \
1215 & ~(STRING_PAD - 1))
1216 /* Add 1 for the null terminator,
1217 and add STRING_PAD - 1 as part of rounding up. */
1219 #define STRING_PAD (sizeof (EMACS_INT))
1220 /* Size of the stuff in the string not including its data. */
1221 #define STRING_BASE_SIZE (((sizeof (struct Lisp_String) - 1) / STRING_PAD) * STRING_PAD)
1223 #if 0
1224 #define STRING_FULLSIZE(SIZE) \
1225 (((SIZE) + 2 * sizeof (EMACS_INT)) & ~(sizeof (EMACS_INT) - 1))
1226 #endif
1228 /* Total number of string blocks now in use. */
1229 int n_string_blocks;
1231 void
1232 init_strings ()
1234 current_string_block = (struct string_block *) lisp_malloc (sizeof (struct string_block));
1235 first_string_block = current_string_block;
1236 consing_since_gc += sizeof (struct string_block);
1237 current_string_block->next = 0;
1238 current_string_block->prev = 0;
1239 current_string_block->pos = 0;
1240 large_string_blocks = 0;
1241 n_string_blocks = 1;
1244 DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0,
1245 "Return a newly created string of length LENGTH, with each element being INIT.\n\
1246 Both LENGTH and INIT must be numbers.")
1247 (length, init)
1248 Lisp_Object length, init;
1250 register Lisp_Object val;
1251 register unsigned char *p, *end;
1252 int c, nbytes;
1254 CHECK_NATNUM (length, 0);
1255 CHECK_NUMBER (init, 1);
1257 c = XINT (init);
1258 if (SINGLE_BYTE_CHAR_P (c))
1260 nbytes = XINT (length);
1261 val = make_uninit_string (nbytes);
1262 p = XSTRING (val)->data;
1263 end = p + XSTRING (val)->size;
1264 while (p != end)
1265 *p++ = c;
1267 else
1269 unsigned char work[4], *str;
1270 int len = CHAR_STRING (c, work, str);
1272 nbytes = len * XINT (length);
1273 val = make_uninit_multibyte_string (XINT (length), nbytes);
1274 p = XSTRING (val)->data;
1275 end = p + nbytes;
1276 while (p != end)
1278 bcopy (str, p, len);
1279 p += len;
1282 *p = 0;
1283 return val;
1286 DEFUN ("make-bool-vector", Fmake_bool_vector, Smake_bool_vector, 2, 2, 0,
1287 "Return a new bool-vector of length LENGTH, using INIT for as each element.\n\
1288 LENGTH must be a number. INIT matters only in whether it is t or nil.")
1289 (length, init)
1290 Lisp_Object length, init;
1292 register Lisp_Object val;
1293 struct Lisp_Bool_Vector *p;
1294 int real_init, i;
1295 int length_in_chars, length_in_elts, bits_per_value;
1297 CHECK_NATNUM (length, 0);
1299 bits_per_value = sizeof (EMACS_INT) * BITS_PER_CHAR;
1301 length_in_elts = (XFASTINT (length) + bits_per_value - 1) / bits_per_value;
1302 length_in_chars = ((XFASTINT (length) + BITS_PER_CHAR - 1) / BITS_PER_CHAR);
1304 /* We must allocate one more elements than LENGTH_IN_ELTS for the
1305 slot `size' of the struct Lisp_Bool_Vector. */
1306 val = Fmake_vector (make_number (length_in_elts + 1), Qnil);
1307 p = XBOOL_VECTOR (val);
1308 /* Get rid of any bits that would cause confusion. */
1309 p->vector_size = 0;
1310 XSETBOOL_VECTOR (val, p);
1311 p->size = XFASTINT (length);
1313 real_init = (NILP (init) ? 0 : -1);
1314 for (i = 0; i < length_in_chars ; i++)
1315 p->data[i] = real_init;
1316 /* Clear the extraneous bits in the last byte. */
1317 if (XINT (length) != length_in_chars * BITS_PER_CHAR)
1318 XBOOL_VECTOR (val)->data[length_in_chars - 1]
1319 &= (1 << (XINT (length) % BITS_PER_CHAR)) - 1;
1321 return val;
1324 /* Make a string from NBYTES bytes at CONTENTS,
1325 and compute the number of characters from the contents.
1326 This string may be unibyte or multibyte, depending on the contents. */
1328 Lisp_Object
1329 make_string (contents, nbytes)
1330 char *contents;
1331 int nbytes;
1333 register Lisp_Object val;
1334 int nchars = chars_in_text (contents, nbytes);
1335 val = make_uninit_multibyte_string (nchars, nbytes);
1336 bcopy (contents, XSTRING (val)->data, nbytes);
1337 if (STRING_BYTES (XSTRING (val)) == XSTRING (val)->size)
1338 SET_STRING_BYTES (XSTRING (val), -1);
1339 return val;
1342 /* Make a unibyte string from LENGTH bytes at CONTENTS. */
1344 Lisp_Object
1345 make_unibyte_string (contents, length)
1346 char *contents;
1347 int length;
1349 register Lisp_Object val;
1350 val = make_uninit_string (length);
1351 bcopy (contents, XSTRING (val)->data, length);
1352 SET_STRING_BYTES (XSTRING (val), -1);
1353 return val;
1356 /* Make a multibyte string from NCHARS characters
1357 occupying NBYTES bytes at CONTENTS. */
1359 Lisp_Object
1360 make_multibyte_string (contents, nchars, nbytes)
1361 char *contents;
1362 int nchars, nbytes;
1364 register Lisp_Object val;
1365 val = make_uninit_multibyte_string (nchars, nbytes);
1366 bcopy (contents, XSTRING (val)->data, nbytes);
1367 return val;
1370 /* Make a string from NCHARS characters
1371 occupying NBYTES bytes at CONTENTS.
1372 It is a multibyte string if NBYTES != NCHARS. */
1374 Lisp_Object
1375 make_string_from_bytes (contents, nchars, nbytes)
1376 char *contents;
1377 int nchars, nbytes;
1379 register Lisp_Object val;
1380 val = make_uninit_multibyte_string (nchars, nbytes);
1381 bcopy (contents, XSTRING (val)->data, nbytes);
1382 if (STRING_BYTES (XSTRING (val)) == XSTRING (val)->size)
1383 SET_STRING_BYTES (XSTRING (val), -1);
1384 return val;
1387 /* Make a multibyte string from NCHARS characters
1388 occupying NBYTES bytes at CONTENTS. */
1390 Lisp_Object
1391 make_specified_string (contents, nchars, nbytes, multibyte)
1392 char *contents;
1393 int nchars, nbytes;
1394 int multibyte;
1396 register Lisp_Object val;
1397 val = make_uninit_multibyte_string (nchars, nbytes);
1398 bcopy (contents, XSTRING (val)->data, nbytes);
1399 if (!multibyte)
1400 SET_STRING_BYTES (XSTRING (val), -1);
1401 return val;
1404 /* Make a string from the data at STR,
1405 treating it as multibyte if the data warrants. */
1407 Lisp_Object
1408 build_string (str)
1409 char *str;
1411 return make_string (str, strlen (str));
1414 Lisp_Object
1415 make_uninit_string (length)
1416 int length;
1418 Lisp_Object val;
1419 val = make_uninit_multibyte_string (length, length);
1420 SET_STRING_BYTES (XSTRING (val), -1);
1421 return val;
1424 Lisp_Object
1425 make_uninit_multibyte_string (length, length_byte)
1426 int length, length_byte;
1428 register Lisp_Object val;
1429 register int fullsize = STRING_FULLSIZE (length_byte);
1431 if (length < 0) abort ();
1433 if (fullsize <= STRING_BLOCK_SIZE - current_string_block->pos)
1434 /* This string can fit in the current string block */
1436 XSETSTRING (val,
1437 ((struct Lisp_String *)
1438 (current_string_block->chars + current_string_block->pos)));
1439 current_string_block->pos += fullsize;
1441 else if (fullsize > STRING_BLOCK_OUTSIZE)
1442 /* This string gets its own string block */
1444 register struct string_block *new;
1445 #ifdef DOUG_LEA_MALLOC
1446 /* Prevent mmap'ing the chunk (which is potentially very large). */
1447 mallopt (M_MMAP_MAX, 0);
1448 #endif
1449 new = (struct string_block *) lisp_malloc (sizeof (struct string_block_head) + fullsize);
1450 #ifdef DOUG_LEA_MALLOC
1451 /* Back to a reasonable maximum of mmap'ed areas. */
1452 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
1453 #endif
1454 n_string_blocks++;
1455 VALIDATE_LISP_STORAGE (new, 0);
1456 consing_since_gc += sizeof (struct string_block_head) + fullsize;
1457 new->pos = fullsize;
1458 new->next = large_string_blocks;
1459 large_string_blocks = new;
1460 XSETSTRING (val,
1461 ((struct Lisp_String *)
1462 ((struct string_block_head *)new + 1)));
1464 else
1465 /* Make a new current string block and start it off with this string */
1467 register struct string_block *new;
1468 new = (struct string_block *) lisp_malloc (sizeof (struct string_block));
1469 n_string_blocks++;
1470 VALIDATE_LISP_STORAGE (new, sizeof *new);
1471 consing_since_gc += sizeof (struct string_block);
1472 current_string_block->next = new;
1473 new->prev = current_string_block;
1474 new->next = 0;
1475 current_string_block = new;
1476 new->pos = fullsize;
1477 XSETSTRING (val,
1478 (struct Lisp_String *) current_string_block->chars);
1481 string_chars_consed += fullsize;
1482 XSTRING (val)->size = length;
1483 SET_STRING_BYTES (XSTRING (val), length_byte);
1484 XSTRING (val)->data[length_byte] = 0;
1485 INITIALIZE_INTERVAL (XSTRING (val), NULL_INTERVAL);
1487 return val;
1490 /* Return a newly created vector or string with specified arguments as
1491 elements. If all the arguments are characters that can fit
1492 in a string of events, make a string; otherwise, make a vector.
1494 Any number of arguments, even zero arguments, are allowed. */
1496 Lisp_Object
1497 make_event_array (nargs, args)
1498 register int nargs;
1499 Lisp_Object *args;
1501 int i;
1503 for (i = 0; i < nargs; i++)
1504 /* The things that fit in a string
1505 are characters that are in 0...127,
1506 after discarding the meta bit and all the bits above it. */
1507 if (!INTEGERP (args[i])
1508 || (XUINT (args[i]) & ~(-CHAR_META)) >= 0200)
1509 return Fvector (nargs, args);
1511 /* Since the loop exited, we know that all the things in it are
1512 characters, so we can make a string. */
1514 Lisp_Object result;
1516 result = Fmake_string (make_number (nargs), make_number (0));
1517 for (i = 0; i < nargs; i++)
1519 XSTRING (result)->data[i] = XINT (args[i]);
1520 /* Move the meta bit to the right place for a string char. */
1521 if (XINT (args[i]) & CHAR_META)
1522 XSTRING (result)->data[i] |= 0x80;
1525 return result;
1529 /* Pure storage management. */
1531 /* Must get an error if pure storage is full,
1532 since if it cannot hold a large string
1533 it may be able to hold conses that point to that string;
1534 then the string is not protected from gc. */
1536 Lisp_Object
1537 make_pure_string (data, length, length_byte, multibyte)
1538 char *data;
1539 int length;
1540 int length_byte;
1541 int multibyte;
1544 register Lisp_Object new;
1545 register int size = STRING_FULLSIZE (length_byte);
1547 if (pureptr + size > PURESIZE)
1548 error ("Pure Lisp storage exhausted");
1549 XSETSTRING (new, PUREBEG + pureptr);
1550 XSTRING (new)->size = length;
1551 SET_STRING_BYTES (XSTRING (new), (multibyte ? length_byte : -1));
1552 bcopy (data, XSTRING (new)->data, length_byte);
1553 XSTRING (new)->data[length_byte] = 0;
1555 /* We must give strings in pure storage some kind of interval. So we
1556 give them a null one. */
1557 #if defined (USE_TEXT_PROPERTIES)
1558 XSTRING (new)->intervals = NULL_INTERVAL;
1559 #endif
1560 pureptr += size;
1561 return new;
1564 Lisp_Object
1565 pure_cons (car, cdr)
1566 Lisp_Object car, cdr;
1568 register Lisp_Object new;
1570 if (pureptr + sizeof (struct Lisp_Cons) > PURESIZE)
1571 error ("Pure Lisp storage exhausted");
1572 XSETCONS (new, PUREBEG + pureptr);
1573 pureptr += sizeof (struct Lisp_Cons);
1574 XCAR (new) = Fpurecopy (car);
1575 XCDR (new) = Fpurecopy (cdr);
1576 return new;
1579 #ifdef LISP_FLOAT_TYPE
1581 Lisp_Object
1582 make_pure_float (num)
1583 double num;
1585 register Lisp_Object new;
1587 /* Make sure that PUREBEG + pureptr is aligned on at least a sizeof
1588 (double) boundary. Some architectures (like the sparc) require
1589 this, and I suspect that floats are rare enough that it's no
1590 tragedy for those that do. */
1592 int alignment;
1593 char *p = PUREBEG + pureptr;
1595 #ifdef __GNUC__
1596 #if __GNUC__ >= 2
1597 alignment = __alignof (struct Lisp_Float);
1598 #else
1599 alignment = sizeof (struct Lisp_Float);
1600 #endif
1601 #else
1602 alignment = sizeof (struct Lisp_Float);
1603 #endif
1604 p = (char *) (((unsigned long) p + alignment - 1) & - alignment);
1605 pureptr = p - PUREBEG;
1608 if (pureptr + sizeof (struct Lisp_Float) > PURESIZE)
1609 error ("Pure Lisp storage exhausted");
1610 XSETFLOAT (new, PUREBEG + pureptr);
1611 pureptr += sizeof (struct Lisp_Float);
1612 XFLOAT_DATA (new) = num;
1613 XSETFASTINT (XFLOAT (new)->type, 0); /* bug chasing -wsr */
1614 return new;
1617 #endif /* LISP_FLOAT_TYPE */
1619 Lisp_Object
1620 make_pure_vector (len)
1621 EMACS_INT len;
1623 register Lisp_Object new;
1624 register EMACS_INT size = sizeof (struct Lisp_Vector) + (len - 1) * sizeof (Lisp_Object);
1626 if (pureptr + size > PURESIZE)
1627 error ("Pure Lisp storage exhausted");
1629 XSETVECTOR (new, PUREBEG + pureptr);
1630 pureptr += size;
1631 XVECTOR (new)->size = len;
1632 return new;
1635 DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
1636 "Make a copy of OBJECT in pure storage.\n\
1637 Recursively copies contents of vectors and cons cells.\n\
1638 Does not copy symbols.")
1639 (obj)
1640 register Lisp_Object obj;
1642 if (NILP (Vpurify_flag))
1643 return obj;
1645 if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
1646 && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
1647 return obj;
1649 if (CONSP (obj))
1650 return pure_cons (XCAR (obj), XCDR (obj));
1651 #ifdef LISP_FLOAT_TYPE
1652 else if (FLOATP (obj))
1653 return make_pure_float (XFLOAT_DATA (obj));
1654 #endif /* LISP_FLOAT_TYPE */
1655 else if (STRINGP (obj))
1656 return make_pure_string (XSTRING (obj)->data, XSTRING (obj)->size,
1657 STRING_BYTES (XSTRING (obj)),
1658 STRING_MULTIBYTE (obj));
1659 else if (COMPILEDP (obj) || VECTORP (obj))
1661 register struct Lisp_Vector *vec;
1662 register int i, size;
1664 size = XVECTOR (obj)->size;
1665 if (size & PSEUDOVECTOR_FLAG)
1666 size &= PSEUDOVECTOR_SIZE_MASK;
1667 vec = XVECTOR (make_pure_vector ((EMACS_INT) size));
1668 for (i = 0; i < size; i++)
1669 vec->contents[i] = Fpurecopy (XVECTOR (obj)->contents[i]);
1670 if (COMPILEDP (obj))
1671 XSETCOMPILED (obj, vec);
1672 else
1673 XSETVECTOR (obj, vec);
1674 return obj;
1676 else if (MARKERP (obj))
1677 error ("Attempt to copy a marker to pure storage");
1678 else
1679 return obj;
1682 /* Recording what needs to be marked for gc. */
1684 struct gcpro *gcprolist;
1686 #define NSTATICS 1024
1688 Lisp_Object *staticvec[NSTATICS] = {0};
1690 int staticidx = 0;
1692 /* Put an entry in staticvec, pointing at the variable whose address is given */
1694 void
1695 staticpro (varaddress)
1696 Lisp_Object *varaddress;
1698 staticvec[staticidx++] = varaddress;
1699 if (staticidx >= NSTATICS)
1700 abort ();
1703 struct catchtag
1705 Lisp_Object tag;
1706 Lisp_Object val;
1707 struct catchtag *next;
1708 #if 0 /* We don't need this for GC purposes */
1709 jmp_buf jmp;
1710 #endif
1713 struct backtrace
1715 struct backtrace *next;
1716 Lisp_Object *function;
1717 Lisp_Object *args; /* Points to vector of args. */
1718 int nargs; /* length of vector */
1719 /* if nargs is UNEVALLED, args points to slot holding list of unevalled args */
1720 char evalargs;
1723 /* Garbage collection! */
1725 /* Temporarily prevent garbage collection. */
1728 inhibit_garbage_collection ()
1730 int count = specpdl_ptr - specpdl;
1731 Lisp_Object number;
1732 int nbits = min (VALBITS, BITS_PER_INT);
1734 XSETINT (number, ((EMACS_INT) 1 << (nbits - 1)) - 1);
1736 specbind (Qgc_cons_threshold, number);
1738 return count;
1741 DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
1742 "Reclaim storage for Lisp objects no longer needed.\n\
1743 Returns info on amount of space in use:\n\
1744 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)\n\
1745 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS\n\
1746 (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS))\n\
1747 Garbage collection happens automatically if you cons more than\n\
1748 `gc-cons-threshold' bytes of Lisp data since previous garbage collection.")
1751 register struct gcpro *tail;
1752 register struct specbinding *bind;
1753 struct catchtag *catch;
1754 struct handler *handler;
1755 register struct backtrace *backlist;
1756 char stack_top_variable;
1757 register int i;
1758 int message_p;
1760 /* In case user calls debug_print during GC,
1761 don't let that cause a recursive GC. */
1762 consing_since_gc = 0;
1764 /* Save what's currently displayed in the echo area. */
1765 message_p = push_message ();
1767 /* Save a copy of the contents of the stack, for debugging. */
1768 #if MAX_SAVE_STACK > 0
1769 if (NILP (Vpurify_flag))
1771 i = &stack_top_variable - stack_bottom;
1772 if (i < 0) i = -i;
1773 if (i < MAX_SAVE_STACK)
1775 if (stack_copy == 0)
1776 stack_copy = (char *) xmalloc (stack_copy_size = i);
1777 else if (stack_copy_size < i)
1778 stack_copy = (char *) xrealloc (stack_copy, (stack_copy_size = i));
1779 if (stack_copy)
1781 if ((EMACS_INT) (&stack_top_variable - stack_bottom) > 0)
1782 bcopy (stack_bottom, stack_copy, i);
1783 else
1784 bcopy (&stack_top_variable, stack_copy, i);
1788 #endif /* MAX_SAVE_STACK > 0 */
1790 if (garbage_collection_messages)
1791 message1_nolog ("Garbage collecting...");
1793 BLOCK_INPUT;
1795 shrink_regexp_cache ();
1797 /* Don't keep undo information around forever. */
1799 register struct buffer *nextb = all_buffers;
1801 while (nextb)
1803 /* If a buffer's undo list is Qt, that means that undo is
1804 turned off in that buffer. Calling truncate_undo_list on
1805 Qt tends to return NULL, which effectively turns undo back on.
1806 So don't call truncate_undo_list if undo_list is Qt. */
1807 if (! EQ (nextb->undo_list, Qt))
1808 nextb->undo_list
1809 = truncate_undo_list (nextb->undo_list, undo_limit,
1810 undo_strong_limit);
1811 nextb = nextb->next;
1815 gc_in_progress = 1;
1817 /* clear_marks (); */
1819 /* In each "large string", set the MARKBIT of the size field.
1820 That enables mark_object to recognize them. */
1822 register struct string_block *b;
1823 for (b = large_string_blocks; b; b = b->next)
1824 ((struct Lisp_String *)(&b->chars[0]))->size |= MARKBIT;
1827 /* Mark all the special slots that serve as the roots of accessibility.
1829 Usually the special slots to mark are contained in particular structures.
1830 Then we know no slot is marked twice because the structures don't overlap.
1831 In some cases, the structures point to the slots to be marked.
1832 For these, we use MARKBIT to avoid double marking of the slot. */
1834 for (i = 0; i < staticidx; i++)
1835 mark_object (staticvec[i]);
1836 for (tail = gcprolist; tail; tail = tail->next)
1837 for (i = 0; i < tail->nvars; i++)
1838 if (!XMARKBIT (tail->var[i]))
1840 mark_object (&tail->var[i]);
1841 XMARK (tail->var[i]);
1843 for (bind = specpdl; bind != specpdl_ptr; bind++)
1845 mark_object (&bind->symbol);
1846 mark_object (&bind->old_value);
1848 for (catch = catchlist; catch; catch = catch->next)
1850 mark_object (&catch->tag);
1851 mark_object (&catch->val);
1853 for (handler = handlerlist; handler; handler = handler->next)
1855 mark_object (&handler->handler);
1856 mark_object (&handler->var);
1858 for (backlist = backtrace_list; backlist; backlist = backlist->next)
1860 if (!XMARKBIT (*backlist->function))
1862 mark_object (backlist->function);
1863 XMARK (*backlist->function);
1865 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
1866 i = 0;
1867 else
1868 i = backlist->nargs - 1;
1869 for (; i >= 0; i--)
1870 if (!XMARKBIT (backlist->args[i]))
1872 mark_object (&backlist->args[i]);
1873 XMARK (backlist->args[i]);
1876 mark_kboards ();
1878 /* Look thru every buffer's undo list
1879 for elements that update markers that were not marked,
1880 and delete them. */
1882 register struct buffer *nextb = all_buffers;
1884 while (nextb)
1886 /* If a buffer's undo list is Qt, that means that undo is
1887 turned off in that buffer. Calling truncate_undo_list on
1888 Qt tends to return NULL, which effectively turns undo back on.
1889 So don't call truncate_undo_list if undo_list is Qt. */
1890 if (! EQ (nextb->undo_list, Qt))
1892 Lisp_Object tail, prev;
1893 tail = nextb->undo_list;
1894 prev = Qnil;
1895 while (CONSP (tail))
1897 if (GC_CONSP (XCAR (tail))
1898 && GC_MARKERP (XCAR (XCAR (tail)))
1899 && ! XMARKBIT (XMARKER (XCAR (XCAR (tail)))->chain))
1901 if (NILP (prev))
1902 nextb->undo_list = tail = XCDR (tail);
1903 else
1904 tail = XCDR (prev) = XCDR (tail);
1906 else
1908 prev = tail;
1909 tail = XCDR (tail);
1914 nextb = nextb->next;
1918 gc_sweep ();
1920 /* Clear the mark bits that we set in certain root slots. */
1922 for (tail = gcprolist; tail; tail = tail->next)
1923 for (i = 0; i < tail->nvars; i++)
1924 XUNMARK (tail->var[i]);
1925 for (backlist = backtrace_list; backlist; backlist = backlist->next)
1927 XUNMARK (*backlist->function);
1928 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
1929 i = 0;
1930 else
1931 i = backlist->nargs - 1;
1932 for (; i >= 0; i--)
1933 XUNMARK (backlist->args[i]);
1935 XUNMARK (buffer_defaults.name);
1936 XUNMARK (buffer_local_symbols.name);
1938 UNBLOCK_INPUT;
1940 /* clear_marks (); */
1941 gc_in_progress = 0;
1943 consing_since_gc = 0;
1944 if (gc_cons_threshold < 10000)
1945 gc_cons_threshold = 10000;
1947 if (garbage_collection_messages)
1949 if (message_p || minibuf_level > 0)
1950 restore_message ();
1951 else
1952 message1_nolog ("Garbage collecting...done");
1955 pop_message ();
1957 return Fcons (Fcons (make_number (total_conses),
1958 make_number (total_free_conses)),
1959 Fcons (Fcons (make_number (total_symbols),
1960 make_number (total_free_symbols)),
1961 Fcons (Fcons (make_number (total_markers),
1962 make_number (total_free_markers)),
1963 Fcons (make_number (total_string_size),
1964 Fcons (make_number (total_vector_size),
1965 Fcons (Fcons
1966 #ifdef LISP_FLOAT_TYPE
1967 (make_number (total_floats),
1968 make_number (total_free_floats)),
1969 #else /* not LISP_FLOAT_TYPE */
1970 (make_number (0), make_number (0)),
1971 #endif /* not LISP_FLOAT_TYPE */
1972 Fcons (Fcons
1973 #ifdef USE_TEXT_PROPERTIES
1974 (make_number (total_intervals),
1975 make_number (total_free_intervals)),
1976 #else /* not USE_TEXT_PROPERTIES */
1977 (make_number (0), make_number (0)),
1978 #endif /* not USE_TEXT_PROPERTIES */
1979 Qnil)))))));
1982 #if 0
1983 static void
1984 clear_marks ()
1986 /* Clear marks on all conses */
1988 register struct cons_block *cblk;
1989 register int lim = cons_block_index;
1991 for (cblk = cons_block; cblk; cblk = cblk->next)
1993 register int i;
1994 for (i = 0; i < lim; i++)
1995 XUNMARK (cblk->conses[i].car);
1996 lim = CONS_BLOCK_SIZE;
1999 /* Clear marks on all symbols */
2001 register struct symbol_block *sblk;
2002 register int lim = symbol_block_index;
2004 for (sblk = symbol_block; sblk; sblk = sblk->next)
2006 register int i;
2007 for (i = 0; i < lim; i++)
2009 XUNMARK (sblk->symbols[i].plist);
2011 lim = SYMBOL_BLOCK_SIZE;
2014 /* Clear marks on all markers */
2016 register struct marker_block *sblk;
2017 register int lim = marker_block_index;
2019 for (sblk = marker_block; sblk; sblk = sblk->next)
2021 register int i;
2022 for (i = 0; i < lim; i++)
2023 if (sblk->markers[i].u_marker.type == Lisp_Misc_Marker)
2024 XUNMARK (sblk->markers[i].u_marker.chain);
2025 lim = MARKER_BLOCK_SIZE;
2028 /* Clear mark bits on all buffers */
2030 register struct buffer *nextb = all_buffers;
2032 while (nextb)
2034 XUNMARK (nextb->name);
2035 nextb = nextb->next;
2039 #endif
2041 /* Mark Lisp objects in glyph matrix MATRIX. Currently the
2042 only interesting objects referenced from glyphs are strings. */
2044 static void
2045 mark_glyph_matrix (matrix)
2046 struct glyph_matrix *matrix;
2048 struct glyph_row *row = matrix->rows;
2049 struct glyph_row *end = row + matrix->nrows;
2051 while (row < end)
2053 if (row->enabled_p)
2055 int area;
2056 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
2058 struct glyph *glyph = row->glyphs[area];
2059 struct glyph *end_glyph = glyph + row->used[area];
2061 while (glyph < end_glyph)
2063 if (GC_STRINGP (glyph->object))
2064 mark_object (&glyph->object);
2065 ++glyph;
2070 ++row;
2074 /* Mark Lisp faces in the face cache C. */
2076 static void
2077 mark_face_cache (c)
2078 struct face_cache *c;
2080 if (c)
2082 int i, j;
2083 for (i = 0; i < c->used; ++i)
2085 struct face *face = FACE_FROM_ID (c->f, i);
2087 if (face)
2089 for (j = 0; j < LFACE_VECTOR_SIZE; ++j)
2090 mark_object (&face->lface[j]);
2091 mark_object (&face->registry);
2098 #ifdef HAVE_WINDOW_SYSTEM
2100 /* Mark Lisp objects in image IMG. */
2102 static void
2103 mark_image (img)
2104 struct image *img;
2106 mark_object (&img->spec);
2108 if (!NILP (img->data.lisp_val))
2109 mark_object (&img->data.lisp_val);
2113 /* Mark Lisp objects in image cache of frame F. It's done this way so
2114 that we don't have to include xterm.h here. */
2116 static void
2117 mark_image_cache (f)
2118 struct frame *f;
2120 forall_images_in_image_cache (f, mark_image);
2123 #endif /* HAVE_X_WINDOWS */
2127 /* Mark reference to a Lisp_Object.
2128 If the object referred to has not been seen yet, recursively mark
2129 all the references contained in it.
2131 If the object referenced is a short string, the referencing slot
2132 is threaded into a chain of such slots, pointed to from
2133 the `size' field of the string. The actual string size
2134 lives in the last slot in the chain. We recognize the end
2135 because it is < (unsigned) STRING_BLOCK_SIZE. */
2137 #define LAST_MARKED_SIZE 500
2138 Lisp_Object *last_marked[LAST_MARKED_SIZE];
2139 int last_marked_index;
2141 void
2142 mark_object (argptr)
2143 Lisp_Object *argptr;
2145 Lisp_Object *objptr = argptr;
2146 register Lisp_Object obj;
2148 loop:
2149 obj = *objptr;
2150 loop2:
2151 XUNMARK (obj);
2153 if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
2154 && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
2155 return;
2157 last_marked[last_marked_index++] = objptr;
2158 if (last_marked_index == LAST_MARKED_SIZE)
2159 last_marked_index = 0;
2161 switch (SWITCH_ENUM_CAST (XGCTYPE (obj)))
2163 case Lisp_String:
2165 register struct Lisp_String *ptr = XSTRING (obj);
2167 MARK_INTERVAL_TREE (ptr->intervals);
2168 if (ptr->size & MARKBIT)
2169 /* A large string. Just set ARRAY_MARK_FLAG. */
2170 ptr->size |= ARRAY_MARK_FLAG;
2171 else
2173 /* A small string. Put this reference
2174 into the chain of references to it.
2175 If the address includes MARKBIT, put that bit elsewhere
2176 when we store OBJPTR into the size field. */
2178 if (XMARKBIT (*objptr))
2180 XSETFASTINT (*objptr, ptr->size);
2181 XMARK (*objptr);
2183 else
2184 XSETFASTINT (*objptr, ptr->size);
2186 if ((EMACS_INT) objptr & DONT_COPY_FLAG)
2187 abort ();
2188 ptr->size = (EMACS_INT) objptr;
2189 if (ptr->size & MARKBIT)
2190 ptr->size ^= MARKBIT | DONT_COPY_FLAG;
2193 break;
2195 case Lisp_Vectorlike:
2196 if (GC_BUFFERP (obj))
2198 if (!XMARKBIT (XBUFFER (obj)->name))
2199 mark_buffer (obj);
2201 else if (GC_SUBRP (obj))
2202 break;
2203 else if (GC_COMPILEDP (obj))
2204 /* We could treat this just like a vector, but it is better
2205 to save the COMPILED_CONSTANTS element for last and avoid recursion
2206 there. */
2208 register struct Lisp_Vector *ptr = XVECTOR (obj);
2209 register EMACS_INT size = ptr->size;
2210 /* See comment above under Lisp_Vector. */
2211 struct Lisp_Vector *volatile ptr1 = ptr;
2212 register int i;
2214 if (size & ARRAY_MARK_FLAG)
2215 break; /* Already marked */
2216 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
2217 size &= PSEUDOVECTOR_SIZE_MASK;
2218 for (i = 0; i < size; i++) /* and then mark its elements */
2220 if (i != COMPILED_CONSTANTS)
2221 mark_object (&ptr1->contents[i]);
2223 /* This cast should be unnecessary, but some Mips compiler complains
2224 (MIPS-ABI + SysVR4, DC/OSx, etc). */
2225 objptr = (Lisp_Object *) &ptr1->contents[COMPILED_CONSTANTS];
2226 goto loop;
2228 else if (GC_FRAMEP (obj))
2230 /* See comment above under Lisp_Vector for why this is volatile. */
2231 register struct frame *volatile ptr = XFRAME (obj);
2232 register EMACS_INT size = ptr->size;
2234 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
2235 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
2237 mark_object (&ptr->name);
2238 mark_object (&ptr->icon_name);
2239 mark_object (&ptr->title);
2240 mark_object (&ptr->focus_frame);
2241 mark_object (&ptr->selected_window);
2242 mark_object (&ptr->minibuffer_window);
2243 mark_object (&ptr->param_alist);
2244 mark_object (&ptr->scroll_bars);
2245 mark_object (&ptr->condemned_scroll_bars);
2246 mark_object (&ptr->menu_bar_items);
2247 mark_object (&ptr->face_alist);
2248 mark_object (&ptr->menu_bar_vector);
2249 mark_object (&ptr->buffer_predicate);
2250 mark_object (&ptr->buffer_list);
2251 mark_object (&ptr->menu_bar_window);
2252 mark_object (&ptr->tool_bar_window);
2253 mark_face_cache (ptr->face_cache);
2254 #ifdef HAVE_WINDOW_SYSTEM
2255 mark_image_cache (ptr);
2256 mark_object (&ptr->desired_tool_bar_items);
2257 mark_object (&ptr->current_tool_bar_items);
2258 mark_object (&ptr->desired_tool_bar_string);
2259 mark_object (&ptr->current_tool_bar_string);
2260 #endif /* HAVE_WINDOW_SYSTEM */
2262 else if (GC_BOOL_VECTOR_P (obj))
2264 register struct Lisp_Vector *ptr = XVECTOR (obj);
2266 if (ptr->size & ARRAY_MARK_FLAG)
2267 break; /* Already marked */
2268 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
2270 else if (GC_WINDOWP (obj))
2272 register struct Lisp_Vector *ptr = XVECTOR (obj);
2273 struct window *w = XWINDOW (obj);
2274 register EMACS_INT size = ptr->size;
2275 /* The reason we use ptr1 is to avoid an apparent hardware bug
2276 that happens occasionally on the FSF's HP 300s.
2277 The bug is that a2 gets clobbered by recursive calls to mark_object.
2278 The clobberage seems to happen during function entry,
2279 perhaps in the moveml instruction.
2280 Yes, this is a crock, but we have to do it. */
2281 struct Lisp_Vector *volatile ptr1 = ptr;
2282 register int i;
2284 /* Stop if already marked. */
2285 if (size & ARRAY_MARK_FLAG)
2286 break;
2288 /* Mark it. */
2289 ptr->size |= ARRAY_MARK_FLAG;
2291 /* There is no Lisp data above The member CURRENT_MATRIX in
2292 struct WINDOW. Stop marking when that slot is reached. */
2293 for (i = 0;
2294 (char *) &ptr1->contents[i] < (char *) &w->current_matrix;
2295 i++)
2296 mark_object (&ptr1->contents[i]);
2298 /* Mark glyphs for leaf windows. Marking window matrices is
2299 sufficient because frame matrices use the same glyph
2300 memory. */
2301 if (NILP (w->hchild)
2302 && NILP (w->vchild)
2303 && w->current_matrix)
2305 mark_glyph_matrix (w->current_matrix);
2306 mark_glyph_matrix (w->desired_matrix);
2309 else if (GC_HASH_TABLE_P (obj))
2311 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
2312 EMACS_INT size = h->size;
2314 /* Stop if already marked. */
2315 if (size & ARRAY_MARK_FLAG)
2316 break;
2318 /* Mark it. */
2319 h->size |= ARRAY_MARK_FLAG;
2321 /* Mark contents. */
2322 mark_object (&h->test);
2323 mark_object (&h->weak);
2324 mark_object (&h->rehash_size);
2325 mark_object (&h->rehash_threshold);
2326 mark_object (&h->hash);
2327 mark_object (&h->next);
2328 mark_object (&h->index);
2329 mark_object (&h->user_hash_function);
2330 mark_object (&h->user_cmp_function);
2332 /* If hash table is not weak, mark all keys and values.
2333 For weak tables, mark only the vector. */
2334 if (GC_NILP (h->weak))
2335 mark_object (&h->key_and_value);
2336 else
2337 XVECTOR (h->key_and_value)->size |= ARRAY_MARK_FLAG;
2340 else
2342 register struct Lisp_Vector *ptr = XVECTOR (obj);
2343 register EMACS_INT size = ptr->size;
2344 /* The reason we use ptr1 is to avoid an apparent hardware bug
2345 that happens occasionally on the FSF's HP 300s.
2346 The bug is that a2 gets clobbered by recursive calls to mark_object.
2347 The clobberage seems to happen during function entry,
2348 perhaps in the moveml instruction.
2349 Yes, this is a crock, but we have to do it. */
2350 struct Lisp_Vector *volatile ptr1 = ptr;
2351 register int i;
2353 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
2354 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
2355 if (size & PSEUDOVECTOR_FLAG)
2356 size &= PSEUDOVECTOR_SIZE_MASK;
2358 for (i = 0; i < size; i++) /* and then mark its elements */
2359 mark_object (&ptr1->contents[i]);
2361 break;
2363 case Lisp_Symbol:
2365 /* See comment above under Lisp_Vector for why this is volatile. */
2366 register struct Lisp_Symbol *volatile ptr = XSYMBOL (obj);
2367 struct Lisp_Symbol *ptrx;
2369 if (XMARKBIT (ptr->plist)) break;
2370 XMARK (ptr->plist);
2371 mark_object ((Lisp_Object *) &ptr->value);
2372 mark_object (&ptr->function);
2373 mark_object (&ptr->plist);
2374 XSETTYPE (*(Lisp_Object *) &ptr->name, Lisp_String);
2375 mark_object ((Lisp_Object *) &ptr->name);
2376 /* Note that we do not mark the obarray of the symbol.
2377 It is safe not to do so because nothing accesses that
2378 slot except to check whether it is nil. */
2379 ptr = ptr->next;
2380 if (ptr)
2382 /* For the benefit of the last_marked log. */
2383 objptr = (Lisp_Object *)&XSYMBOL (obj)->next;
2384 ptrx = ptr; /* Use of ptrx avoids compiler bug on Sun */
2385 XSETSYMBOL (obj, ptrx);
2386 /* We can't goto loop here because *objptr doesn't contain an
2387 actual Lisp_Object with valid datatype field. */
2388 goto loop2;
2391 break;
2393 case Lisp_Misc:
2394 switch (XMISCTYPE (obj))
2396 case Lisp_Misc_Marker:
2397 XMARK (XMARKER (obj)->chain);
2398 /* DO NOT mark thru the marker's chain.
2399 The buffer's markers chain does not preserve markers from gc;
2400 instead, markers are removed from the chain when freed by gc. */
2401 break;
2403 case Lisp_Misc_Buffer_Local_Value:
2404 case Lisp_Misc_Some_Buffer_Local_Value:
2406 register struct Lisp_Buffer_Local_Value *ptr
2407 = XBUFFER_LOCAL_VALUE (obj);
2408 if (XMARKBIT (ptr->realvalue)) break;
2409 XMARK (ptr->realvalue);
2410 /* If the cdr is nil, avoid recursion for the car. */
2411 if (EQ (ptr->cdr, Qnil))
2413 objptr = &ptr->realvalue;
2414 goto loop;
2416 mark_object (&ptr->realvalue);
2417 mark_object (&ptr->buffer);
2418 mark_object (&ptr->frame);
2419 /* See comment above under Lisp_Vector for why not use ptr here. */
2420 objptr = &XBUFFER_LOCAL_VALUE (obj)->cdr;
2421 goto loop;
2424 case Lisp_Misc_Intfwd:
2425 case Lisp_Misc_Boolfwd:
2426 case Lisp_Misc_Objfwd:
2427 case Lisp_Misc_Buffer_Objfwd:
2428 case Lisp_Misc_Kboard_Objfwd:
2429 /* Don't bother with Lisp_Buffer_Objfwd,
2430 since all markable slots in current buffer marked anyway. */
2431 /* Don't need to do Lisp_Objfwd, since the places they point
2432 are protected with staticpro. */
2433 break;
2435 case Lisp_Misc_Overlay:
2437 struct Lisp_Overlay *ptr = XOVERLAY (obj);
2438 if (!XMARKBIT (ptr->plist))
2440 XMARK (ptr->plist);
2441 mark_object (&ptr->start);
2442 mark_object (&ptr->end);
2443 objptr = &ptr->plist;
2444 goto loop;
2447 break;
2449 default:
2450 abort ();
2452 break;
2454 case Lisp_Cons:
2456 register struct Lisp_Cons *ptr = XCONS (obj);
2457 if (XMARKBIT (ptr->car)) break;
2458 XMARK (ptr->car);
2459 /* If the cdr is nil, avoid recursion for the car. */
2460 if (EQ (ptr->cdr, Qnil))
2462 objptr = &ptr->car;
2463 goto loop;
2465 mark_object (&ptr->car);
2466 /* See comment above under Lisp_Vector for why not use ptr here. */
2467 objptr = &XCDR (obj);
2468 goto loop;
2471 #ifdef LISP_FLOAT_TYPE
2472 case Lisp_Float:
2473 XMARK (XFLOAT (obj)->type);
2474 break;
2475 #endif /* LISP_FLOAT_TYPE */
2477 case Lisp_Int:
2478 break;
2480 default:
2481 abort ();
2485 /* Mark the pointers in a buffer structure. */
2487 static void
2488 mark_buffer (buf)
2489 Lisp_Object buf;
2491 register struct buffer *buffer = XBUFFER (buf);
2492 register Lisp_Object *ptr;
2493 Lisp_Object base_buffer;
2495 /* This is the buffer's markbit */
2496 mark_object (&buffer->name);
2497 XMARK (buffer->name);
2499 MARK_INTERVAL_TREE (BUF_INTERVALS (buffer));
2501 if (CONSP (buffer->undo_list))
2503 Lisp_Object tail;
2504 tail = buffer->undo_list;
2506 while (CONSP (tail))
2508 register struct Lisp_Cons *ptr = XCONS (tail);
2510 if (XMARKBIT (ptr->car))
2511 break;
2512 XMARK (ptr->car);
2513 if (GC_CONSP (ptr->car)
2514 && ! XMARKBIT (XCAR (ptr->car))
2515 && GC_MARKERP (XCAR (ptr->car)))
2517 XMARK (XCAR (ptr->car));
2518 mark_object (&XCDR (ptr->car));
2520 else
2521 mark_object (&ptr->car);
2523 if (CONSP (ptr->cdr))
2524 tail = ptr->cdr;
2525 else
2526 break;
2529 mark_object (&XCDR (tail));
2531 else
2532 mark_object (&buffer->undo_list);
2534 #if 0
2535 mark_object (buffer->syntax_table);
2537 /* Mark the various string-pointers in the buffer object.
2538 Since the strings may be relocated, we must mark them
2539 in their actual slots. So gc_sweep must convert each slot
2540 back to an ordinary C pointer. */
2541 XSETSTRING (*(Lisp_Object *)&buffer->upcase_table, buffer->upcase_table);
2542 mark_object ((Lisp_Object *)&buffer->upcase_table);
2543 XSETSTRING (*(Lisp_Object *)&buffer->downcase_table, buffer->downcase_table);
2544 mark_object ((Lisp_Object *)&buffer->downcase_table);
2546 XSETSTRING (*(Lisp_Object *)&buffer->sort_table, buffer->sort_table);
2547 mark_object ((Lisp_Object *)&buffer->sort_table);
2548 XSETSTRING (*(Lisp_Object *)&buffer->folding_sort_table, buffer->folding_sort_table);
2549 mark_object ((Lisp_Object *)&buffer->folding_sort_table);
2550 #endif
2552 for (ptr = &buffer->name + 1;
2553 (char *)ptr < (char *)buffer + sizeof (struct buffer);
2554 ptr++)
2555 mark_object (ptr);
2557 /* If this is an indirect buffer, mark its base buffer. */
2558 if (buffer->base_buffer && !XMARKBIT (buffer->base_buffer->name))
2560 XSETBUFFER (base_buffer, buffer->base_buffer);
2561 mark_buffer (base_buffer);
2566 /* Mark the pointers in the kboard objects. */
2568 static void
2569 mark_kboards ()
2571 KBOARD *kb;
2572 Lisp_Object *p;
2573 for (kb = all_kboards; kb; kb = kb->next_kboard)
2575 if (kb->kbd_macro_buffer)
2576 for (p = kb->kbd_macro_buffer; p < kb->kbd_macro_ptr; p++)
2577 mark_object (p);
2578 mark_object (&kb->Voverriding_terminal_local_map);
2579 mark_object (&kb->Vlast_command);
2580 mark_object (&kb->Vreal_last_command);
2581 mark_object (&kb->Vprefix_arg);
2582 mark_object (&kb->Vlast_prefix_arg);
2583 mark_object (&kb->kbd_queue);
2584 mark_object (&kb->defining_kbd_macro);
2585 mark_object (&kb->Vlast_kbd_macro);
2586 mark_object (&kb->Vsystem_key_alist);
2587 mark_object (&kb->system_key_syms);
2588 mark_object (&kb->Vdefault_minibuffer_frame);
2593 /* Value is non-zero if OBJ will survive the current GC because it's
2594 either marked or does not need to be marked to survive. */
2597 survives_gc_p (obj)
2598 Lisp_Object obj;
2600 int survives_p;
2602 switch (XGCTYPE (obj))
2604 case Lisp_Int:
2605 survives_p = 1;
2606 break;
2608 case Lisp_Symbol:
2609 survives_p = XMARKBIT (XSYMBOL (obj)->plist);
2610 break;
2612 case Lisp_Misc:
2613 switch (XMISCTYPE (obj))
2615 case Lisp_Misc_Marker:
2616 survives_p = XMARKBIT (obj);
2617 break;
2619 case Lisp_Misc_Buffer_Local_Value:
2620 case Lisp_Misc_Some_Buffer_Local_Value:
2621 survives_p = XMARKBIT (XBUFFER_LOCAL_VALUE (obj)->realvalue);
2622 break;
2624 case Lisp_Misc_Intfwd:
2625 case Lisp_Misc_Boolfwd:
2626 case Lisp_Misc_Objfwd:
2627 case Lisp_Misc_Buffer_Objfwd:
2628 case Lisp_Misc_Kboard_Objfwd:
2629 survives_p = 1;
2630 break;
2632 case Lisp_Misc_Overlay:
2633 survives_p = XMARKBIT (XOVERLAY (obj)->plist);
2634 break;
2636 default:
2637 abort ();
2639 break;
2641 case Lisp_String:
2643 struct Lisp_String *s = XSTRING (obj);
2645 if (s->size & MARKBIT)
2646 survives_p = s->size & ARRAY_MARK_FLAG;
2647 else
2648 survives_p = (s->size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE;
2650 break;
2652 case Lisp_Vectorlike:
2653 if (GC_BUFFERP (obj))
2654 survives_p = XMARKBIT (XBUFFER (obj)->name);
2655 else if (GC_SUBRP (obj))
2656 survives_p = 1;
2657 else
2658 survives_p = XVECTOR (obj)->size & ARRAY_MARK_FLAG;
2659 break;
2661 case Lisp_Cons:
2662 survives_p = XMARKBIT (XCAR (obj));
2663 break;
2665 #ifdef LISP_FLOAT_TYPE
2666 case Lisp_Float:
2667 survives_p = XMARKBIT (XFLOAT (obj)->type);
2668 break;
2669 #endif /* LISP_FLOAT_TYPE */
2671 default:
2672 abort ();
2675 return survives_p;
2680 /* Sweep: find all structures not marked, and free them. */
2682 static void
2683 gc_sweep ()
2685 /* Remove or mark entries in weak hash tables.
2686 This must be done before any object is unmarked. */
2687 sweep_weak_hash_tables ();
2689 total_string_size = 0;
2690 compact_strings ();
2692 /* Put all unmarked conses on free list */
2694 register struct cons_block *cblk;
2695 struct cons_block **cprev = &cons_block;
2696 register int lim = cons_block_index;
2697 register int num_free = 0, num_used = 0;
2699 cons_free_list = 0;
2701 for (cblk = cons_block; cblk; cblk = *cprev)
2703 register int i;
2704 int this_free = 0;
2705 for (i = 0; i < lim; i++)
2706 if (!XMARKBIT (cblk->conses[i].car))
2708 this_free++;
2709 *(struct Lisp_Cons **)&cblk->conses[i].cdr = cons_free_list;
2710 cons_free_list = &cblk->conses[i];
2712 else
2714 num_used++;
2715 XUNMARK (cblk->conses[i].car);
2717 lim = CONS_BLOCK_SIZE;
2718 /* If this block contains only free conses and we have already
2719 seen more than two blocks worth of free conses then deallocate
2720 this block. */
2721 if (this_free == CONS_BLOCK_SIZE && num_free > CONS_BLOCK_SIZE)
2723 *cprev = cblk->next;
2724 /* Unhook from the free list. */
2725 cons_free_list = *(struct Lisp_Cons **) &cblk->conses[0].cdr;
2726 lisp_free (cblk);
2727 n_cons_blocks--;
2729 else
2731 num_free += this_free;
2732 cprev = &cblk->next;
2735 total_conses = num_used;
2736 total_free_conses = num_free;
2739 #ifdef LISP_FLOAT_TYPE
2740 /* Put all unmarked floats on free list */
2742 register struct float_block *fblk;
2743 struct float_block **fprev = &float_block;
2744 register int lim = float_block_index;
2745 register int num_free = 0, num_used = 0;
2747 float_free_list = 0;
2749 for (fblk = float_block; fblk; fblk = *fprev)
2751 register int i;
2752 int this_free = 0;
2753 for (i = 0; i < lim; i++)
2754 if (!XMARKBIT (fblk->floats[i].type))
2756 this_free++;
2757 *(struct Lisp_Float **)&fblk->floats[i].data = float_free_list;
2758 float_free_list = &fblk->floats[i];
2760 else
2762 num_used++;
2763 XUNMARK (fblk->floats[i].type);
2765 lim = FLOAT_BLOCK_SIZE;
2766 /* If this block contains only free floats and we have already
2767 seen more than two blocks worth of free floats then deallocate
2768 this block. */
2769 if (this_free == FLOAT_BLOCK_SIZE && num_free > FLOAT_BLOCK_SIZE)
2771 *fprev = fblk->next;
2772 /* Unhook from the free list. */
2773 float_free_list = *(struct Lisp_Float **) &fblk->floats[0].data;
2774 lisp_free (fblk);
2775 n_float_blocks--;
2777 else
2779 num_free += this_free;
2780 fprev = &fblk->next;
2783 total_floats = num_used;
2784 total_free_floats = num_free;
2786 #endif /* LISP_FLOAT_TYPE */
2788 #ifdef USE_TEXT_PROPERTIES
2789 /* Put all unmarked intervals on free list */
2791 register struct interval_block *iblk;
2792 struct interval_block **iprev = &interval_block;
2793 register int lim = interval_block_index;
2794 register int num_free = 0, num_used = 0;
2796 interval_free_list = 0;
2798 for (iblk = interval_block; iblk; iblk = *iprev)
2800 register int i;
2801 int this_free = 0;
2803 for (i = 0; i < lim; i++)
2805 if (! XMARKBIT (iblk->intervals[i].plist))
2807 iblk->intervals[i].parent = interval_free_list;
2808 interval_free_list = &iblk->intervals[i];
2809 this_free++;
2811 else
2813 num_used++;
2814 XUNMARK (iblk->intervals[i].plist);
2817 lim = INTERVAL_BLOCK_SIZE;
2818 /* If this block contains only free intervals and we have already
2819 seen more than two blocks worth of free intervals then
2820 deallocate this block. */
2821 if (this_free == INTERVAL_BLOCK_SIZE && num_free > INTERVAL_BLOCK_SIZE)
2823 *iprev = iblk->next;
2824 /* Unhook from the free list. */
2825 interval_free_list = iblk->intervals[0].parent;
2826 lisp_free (iblk);
2827 n_interval_blocks--;
2829 else
2831 num_free += this_free;
2832 iprev = &iblk->next;
2835 total_intervals = num_used;
2836 total_free_intervals = num_free;
2838 #endif /* USE_TEXT_PROPERTIES */
2840 /* Put all unmarked symbols on free list */
2842 register struct symbol_block *sblk;
2843 struct symbol_block **sprev = &symbol_block;
2844 register int lim = symbol_block_index;
2845 register int num_free = 0, num_used = 0;
2847 symbol_free_list = 0;
2849 for (sblk = symbol_block; sblk; sblk = *sprev)
2851 register int i;
2852 int this_free = 0;
2853 for (i = 0; i < lim; i++)
2854 if (!XMARKBIT (sblk->symbols[i].plist))
2856 *(struct Lisp_Symbol **)&sblk->symbols[i].value = symbol_free_list;
2857 symbol_free_list = &sblk->symbols[i];
2858 this_free++;
2860 else
2862 num_used++;
2863 sblk->symbols[i].name
2864 = XSTRING (*(Lisp_Object *) &sblk->symbols[i].name);
2865 XUNMARK (sblk->symbols[i].plist);
2867 lim = SYMBOL_BLOCK_SIZE;
2868 /* If this block contains only free symbols and we have already
2869 seen more than two blocks worth of free symbols then deallocate
2870 this block. */
2871 if (this_free == SYMBOL_BLOCK_SIZE && num_free > SYMBOL_BLOCK_SIZE)
2873 *sprev = sblk->next;
2874 /* Unhook from the free list. */
2875 symbol_free_list = *(struct Lisp_Symbol **)&sblk->symbols[0].value;
2876 lisp_free (sblk);
2877 n_symbol_blocks--;
2879 else
2881 num_free += this_free;
2882 sprev = &sblk->next;
2885 total_symbols = num_used;
2886 total_free_symbols = num_free;
2889 #ifndef standalone
2890 /* Put all unmarked misc's on free list.
2891 For a marker, first unchain it from the buffer it points into. */
2893 register struct marker_block *mblk;
2894 struct marker_block **mprev = &marker_block;
2895 register int lim = marker_block_index;
2896 register int num_free = 0, num_used = 0;
2898 marker_free_list = 0;
2900 for (mblk = marker_block; mblk; mblk = *mprev)
2902 register int i;
2903 int this_free = 0;
2904 EMACS_INT already_free = -1;
2906 for (i = 0; i < lim; i++)
2908 Lisp_Object *markword;
2909 switch (mblk->markers[i].u_marker.type)
2911 case Lisp_Misc_Marker:
2912 markword = &mblk->markers[i].u_marker.chain;
2913 break;
2914 case Lisp_Misc_Buffer_Local_Value:
2915 case Lisp_Misc_Some_Buffer_Local_Value:
2916 markword = &mblk->markers[i].u_buffer_local_value.realvalue;
2917 break;
2918 case Lisp_Misc_Overlay:
2919 markword = &mblk->markers[i].u_overlay.plist;
2920 break;
2921 case Lisp_Misc_Free:
2922 /* If the object was already free, keep it
2923 on the free list. */
2924 markword = (Lisp_Object *) &already_free;
2925 break;
2926 default:
2927 markword = 0;
2928 break;
2930 if (markword && !XMARKBIT (*markword))
2932 Lisp_Object tem;
2933 if (mblk->markers[i].u_marker.type == Lisp_Misc_Marker)
2935 /* tem1 avoids Sun compiler bug */
2936 struct Lisp_Marker *tem1 = &mblk->markers[i].u_marker;
2937 XSETMARKER (tem, tem1);
2938 unchain_marker (tem);
2940 /* Set the type of the freed object to Lisp_Misc_Free.
2941 We could leave the type alone, since nobody checks it,
2942 but this might catch bugs faster. */
2943 mblk->markers[i].u_marker.type = Lisp_Misc_Free;
2944 mblk->markers[i].u_free.chain = marker_free_list;
2945 marker_free_list = &mblk->markers[i];
2946 this_free++;
2948 else
2950 num_used++;
2951 if (markword)
2952 XUNMARK (*markword);
2955 lim = MARKER_BLOCK_SIZE;
2956 /* If this block contains only free markers and we have already
2957 seen more than two blocks worth of free markers then deallocate
2958 this block. */
2959 if (this_free == MARKER_BLOCK_SIZE && num_free > MARKER_BLOCK_SIZE)
2961 *mprev = mblk->next;
2962 /* Unhook from the free list. */
2963 marker_free_list = mblk->markers[0].u_free.chain;
2964 lisp_free (mblk);
2965 n_marker_blocks--;
2967 else
2969 num_free += this_free;
2970 mprev = &mblk->next;
2974 total_markers = num_used;
2975 total_free_markers = num_free;
2978 /* Free all unmarked buffers */
2980 register struct buffer *buffer = all_buffers, *prev = 0, *next;
2982 while (buffer)
2983 if (!XMARKBIT (buffer->name))
2985 if (prev)
2986 prev->next = buffer->next;
2987 else
2988 all_buffers = buffer->next;
2989 next = buffer->next;
2990 xfree (buffer);
2991 buffer = next;
2993 else
2995 XUNMARK (buffer->name);
2996 UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer));
2998 #if 0
2999 /* Each `struct Lisp_String *' was turned into a Lisp_Object
3000 for purposes of marking and relocation.
3001 Turn them back into C pointers now. */
3002 buffer->upcase_table
3003 = XSTRING (*(Lisp_Object *)&buffer->upcase_table);
3004 buffer->downcase_table
3005 = XSTRING (*(Lisp_Object *)&buffer->downcase_table);
3006 buffer->sort_table
3007 = XSTRING (*(Lisp_Object *)&buffer->sort_table);
3008 buffer->folding_sort_table
3009 = XSTRING (*(Lisp_Object *)&buffer->folding_sort_table);
3010 #endif
3012 prev = buffer, buffer = buffer->next;
3016 #endif /* standalone */
3018 /* Free all unmarked vectors */
3020 register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next;
3021 total_vector_size = 0;
3023 while (vector)
3024 if (!(vector->size & ARRAY_MARK_FLAG))
3026 #if 0
3027 if ((vector->size & (PSEUDOVECTOR_FLAG | PVEC_HASH_TABLE))
3028 == (PSEUDOVECTOR_FLAG | PVEC_HASH_TABLE))
3029 fprintf (stderr, "Freeing hash table %p\n", vector);
3030 #endif
3031 if (prev)
3032 prev->next = vector->next;
3033 else
3034 all_vectors = vector->next;
3035 next = vector->next;
3036 lisp_free (vector);
3037 n_vectors--;
3038 vector = next;
3041 else
3043 vector->size &= ~ARRAY_MARK_FLAG;
3044 if (vector->size & PSEUDOVECTOR_FLAG)
3045 total_vector_size += (PSEUDOVECTOR_SIZE_MASK & vector->size);
3046 else
3047 total_vector_size += vector->size;
3048 prev = vector, vector = vector->next;
3052 /* Free all "large strings" not marked with ARRAY_MARK_FLAG. */
3054 register struct string_block *sb = large_string_blocks, *prev = 0, *next;
3055 struct Lisp_String *s;
3057 while (sb)
3059 s = (struct Lisp_String *) &sb->chars[0];
3060 if (s->size & ARRAY_MARK_FLAG)
3062 ((struct Lisp_String *)(&sb->chars[0]))->size
3063 &= ~ARRAY_MARK_FLAG & ~MARKBIT;
3064 UNMARK_BALANCE_INTERVALS (s->intervals);
3065 total_string_size += ((struct Lisp_String *)(&sb->chars[0]))->size;
3066 prev = sb, sb = sb->next;
3068 else
3070 if (prev)
3071 prev->next = sb->next;
3072 else
3073 large_string_blocks = sb->next;
3074 next = sb->next;
3075 lisp_free (sb);
3076 sb = next;
3077 n_string_blocks--;
3083 /* Compactify strings, relocate references, and free empty string blocks. */
3085 static void
3086 compact_strings ()
3088 /* String block of old strings we are scanning. */
3089 register struct string_block *from_sb;
3090 /* A preceding string block (or maybe the same one)
3091 where we are copying the still-live strings to. */
3092 register struct string_block *to_sb;
3093 int pos;
3094 int to_pos;
3096 to_sb = first_string_block;
3097 to_pos = 0;
3099 /* Scan each existing string block sequentially, string by string. */
3100 for (from_sb = first_string_block; from_sb; from_sb = from_sb->next)
3102 pos = 0;
3103 /* POS is the index of the next string in the block. */
3104 while (pos < from_sb->pos)
3106 register struct Lisp_String *nextstr
3107 = (struct Lisp_String *) &from_sb->chars[pos];
3109 register struct Lisp_String *newaddr;
3110 register EMACS_INT size = nextstr->size;
3111 EMACS_INT size_byte = nextstr->size_byte;
3113 /* NEXTSTR is the old address of the next string.
3114 Just skip it if it isn't marked. */
3115 if (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE)
3117 /* It is marked, so its size field is really a chain of refs.
3118 Find the end of the chain, where the actual size lives. */
3119 while (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE)
3121 if (size & DONT_COPY_FLAG)
3122 size ^= MARKBIT | DONT_COPY_FLAG;
3123 size = *(EMACS_INT *)size & ~MARKBIT;
3126 if (size_byte < 0)
3127 size_byte = size;
3129 total_string_size += size_byte;
3131 /* If it won't fit in TO_SB, close it out,
3132 and move to the next sb. Keep doing so until
3133 TO_SB reaches a large enough, empty enough string block.
3134 We know that TO_SB cannot advance past FROM_SB here
3135 since FROM_SB is large enough to contain this string.
3136 Any string blocks skipped here
3137 will be patched out and freed later. */
3138 while (to_pos + STRING_FULLSIZE (size_byte)
3139 > max (to_sb->pos, STRING_BLOCK_SIZE))
3141 to_sb->pos = to_pos;
3142 to_sb = to_sb->next;
3143 to_pos = 0;
3145 /* Compute new address of this string
3146 and update TO_POS for the space being used. */
3147 newaddr = (struct Lisp_String *) &to_sb->chars[to_pos];
3148 to_pos += STRING_FULLSIZE (size_byte);
3150 /* Copy the string itself to the new place. */
3151 if (nextstr != newaddr)
3152 bcopy (nextstr, newaddr, STRING_FULLSIZE (size_byte));
3154 /* Go through NEXTSTR's chain of references
3155 and make each slot in the chain point to
3156 the new address of this string. */
3157 size = newaddr->size;
3158 while (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE)
3160 register Lisp_Object *objptr;
3161 if (size & DONT_COPY_FLAG)
3162 size ^= MARKBIT | DONT_COPY_FLAG;
3163 objptr = (Lisp_Object *)size;
3165 size = XFASTINT (*objptr) & ~MARKBIT;
3166 if (XMARKBIT (*objptr))
3168 XSETSTRING (*objptr, newaddr);
3169 XMARK (*objptr);
3171 else
3172 XSETSTRING (*objptr, newaddr);
3174 /* Store the actual size in the size field. */
3175 newaddr->size = size;
3177 #ifdef USE_TEXT_PROPERTIES
3178 /* Now that the string has been relocated, rebalance its
3179 interval tree, and update the tree's parent pointer. */
3180 if (! NULL_INTERVAL_P (newaddr->intervals))
3182 UNMARK_BALANCE_INTERVALS (newaddr->intervals);
3183 XSETSTRING (* (Lisp_Object *) &newaddr->intervals->parent,
3184 newaddr);
3186 #endif /* USE_TEXT_PROPERTIES */
3188 else if (size_byte < 0)
3189 size_byte = size;
3191 pos += STRING_FULLSIZE (size_byte);
3195 /* Close out the last string block still used and free any that follow. */
3196 to_sb->pos = to_pos;
3197 current_string_block = to_sb;
3199 from_sb = to_sb->next;
3200 to_sb->next = 0;
3201 while (from_sb)
3203 to_sb = from_sb->next;
3204 lisp_free (from_sb);
3205 n_string_blocks--;
3206 from_sb = to_sb;
3209 /* Free any empty string blocks further back in the chain.
3210 This loop will never free first_string_block, but it is very
3211 unlikely that that one will become empty, so why bother checking? */
3213 from_sb = first_string_block;
3214 while ((to_sb = from_sb->next) != 0)
3216 if (to_sb->pos == 0)
3218 if ((from_sb->next = to_sb->next) != 0)
3219 from_sb->next->prev = from_sb;
3220 lisp_free (to_sb);
3221 n_string_blocks--;
3223 else
3224 from_sb = to_sb;
3228 /* Debugging aids. */
3230 DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0,
3231 "Return the address of the last byte Emacs has allocated, divided by 1024.\n\
3232 This may be helpful in debugging Emacs's memory usage.\n\
3233 We divide the value by 1024 to make sure it fits in a Lisp integer.")
3236 Lisp_Object end;
3238 XSETINT (end, (EMACS_INT) sbrk (0) / 1024);
3240 return end;
3243 DEFUN ("memory-use-counts", Fmemory_use_counts, Smemory_use_counts, 0, 0, 0,
3244 "Return a list of counters that measure how much consing there has been.\n\
3245 Each of these counters increments for a certain kind of object.\n\
3246 The counters wrap around from the largest positive integer to zero.\n\
3247 Garbage collection does not decrease them.\n\
3248 The elements of the value are as follows:\n\
3249 (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS)\n\
3250 All are in units of 1 = one object consed\n\
3251 except for VECTOR-CELLS and STRING-CHARS, which count the total length of\n\
3252 objects consed.\n\
3253 MISCS include overlays, markers, and some internal types.\n\
3254 Frames, windows, buffers, and subprocesses count as vectors\n\
3255 (but the contents of a buffer's text do not count here).")
3258 Lisp_Object lisp_cons_cells_consed;
3259 Lisp_Object lisp_floats_consed;
3260 Lisp_Object lisp_vector_cells_consed;
3261 Lisp_Object lisp_symbols_consed;
3262 Lisp_Object lisp_string_chars_consed;
3263 Lisp_Object lisp_misc_objects_consed;
3264 Lisp_Object lisp_intervals_consed;
3266 XSETINT (lisp_cons_cells_consed,
3267 cons_cells_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
3268 XSETINT (lisp_floats_consed,
3269 floats_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
3270 XSETINT (lisp_vector_cells_consed,
3271 vector_cells_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
3272 XSETINT (lisp_symbols_consed,
3273 symbols_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
3274 XSETINT (lisp_string_chars_consed,
3275 string_chars_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
3276 XSETINT (lisp_misc_objects_consed,
3277 misc_objects_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
3278 XSETINT (lisp_intervals_consed,
3279 intervals_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
3281 return Fcons (lisp_cons_cells_consed,
3282 Fcons (lisp_floats_consed,
3283 Fcons (lisp_vector_cells_consed,
3284 Fcons (lisp_symbols_consed,
3285 Fcons (lisp_string_chars_consed,
3286 Fcons (lisp_misc_objects_consed,
3287 Fcons (lisp_intervals_consed,
3288 Qnil)))))));
3291 /* Initialization */
3293 void
3294 init_alloc_once ()
3296 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
3297 pureptr = 0;
3298 #ifdef HAVE_SHM
3299 pure_size = PURESIZE;
3300 #endif
3301 all_vectors = 0;
3302 ignore_warnings = 1;
3303 #ifdef DOUG_LEA_MALLOC
3304 mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */
3305 mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */
3306 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); /* max. number of mmap'ed areas */
3307 #endif
3308 init_strings ();
3309 init_cons ();
3310 init_symbol ();
3311 init_marker ();
3312 #ifdef LISP_FLOAT_TYPE
3313 init_float ();
3314 #endif /* LISP_FLOAT_TYPE */
3315 INIT_INTERVALS;
3317 #ifdef REL_ALLOC
3318 malloc_hysteresis = 32;
3319 #else
3320 malloc_hysteresis = 0;
3321 #endif
3323 spare_memory = (char *) malloc (SPARE_MEMORY);
3325 ignore_warnings = 0;
3326 gcprolist = 0;
3327 staticidx = 0;
3328 consing_since_gc = 0;
3329 gc_cons_threshold = 100000 * sizeof (Lisp_Object);
3330 #ifdef VIRT_ADDR_VARIES
3331 malloc_sbrk_unused = 1<<22; /* A large number */
3332 malloc_sbrk_used = 100000; /* as reasonable as any number */
3333 #endif /* VIRT_ADDR_VARIES */
3336 void
3337 init_alloc ()
3339 gcprolist = 0;
3342 void
3343 syms_of_alloc ()
3345 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold,
3346 "*Number of bytes of consing between garbage collections.\n\
3347 Garbage collection can happen automatically once this many bytes have been\n\
3348 allocated since the last garbage collection. All data types count.\n\n\
3349 Garbage collection happens automatically only when `eval' is called.\n\n\
3350 By binding this temporarily to a large number, you can effectively\n\
3351 prevent garbage collection during a part of the program.");
3353 DEFVAR_INT ("pure-bytes-used", &pureptr,
3354 "Number of bytes of sharable Lisp data allocated so far.");
3356 DEFVAR_INT ("cons-cells-consed", &cons_cells_consed,
3357 "Number of cons cells that have been consed so far.");
3359 DEFVAR_INT ("floats-consed", &floats_consed,
3360 "Number of floats that have been consed so far.");
3362 DEFVAR_INT ("vector-cells-consed", &vector_cells_consed,
3363 "Number of vector cells that have been consed so far.");
3365 DEFVAR_INT ("symbols-consed", &symbols_consed,
3366 "Number of symbols that have been consed so far.");
3368 DEFVAR_INT ("string-chars-consed", &string_chars_consed,
3369 "Number of string characters that have been consed so far.");
3371 DEFVAR_INT ("misc-objects-consed", &misc_objects_consed,
3372 "Number of miscellaneous objects that have been consed so far.");
3374 DEFVAR_INT ("intervals-consed", &intervals_consed,
3375 "Number of intervals that have been consed so far.");
3377 #if 0
3378 DEFVAR_INT ("data-bytes-used", &malloc_sbrk_used,
3379 "Number of bytes of unshared memory allocated in this session.");
3381 DEFVAR_INT ("data-bytes-free", &malloc_sbrk_unused,
3382 "Number of bytes of unshared memory remaining available in this session.");
3383 #endif
3385 DEFVAR_LISP ("purify-flag", &Vpurify_flag,
3386 "Non-nil means loading Lisp code in order to dump an executable.\n\
3387 This means that certain objects should be allocated in shared (pure) space.");
3389 DEFVAR_INT ("undo-limit", &undo_limit,
3390 "Keep no more undo information once it exceeds this size.\n\
3391 This limit is applied when garbage collection happens.\n\
3392 The size is counted as the number of bytes occupied,\n\
3393 which includes both saved text and other data.");
3394 undo_limit = 20000;
3396 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit,
3397 "Don't keep more than this much size of undo information.\n\
3398 A command which pushes past this size is itself forgotten.\n\
3399 This limit is applied when garbage collection happens.\n\
3400 The size is counted as the number of bytes occupied,\n\
3401 which includes both saved text and other data.");
3402 undo_strong_limit = 30000;
3404 DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages,
3405 "Non-nil means display messages at start and end of garbage collection.");
3406 garbage_collection_messages = 0;
3408 /* We build this in advance because if we wait until we need it, we might
3409 not be able to allocate the memory to hold it. */
3410 memory_signal_data
3411 = Fcons (Qerror, Fcons (build_string ("Memory exhausted--use M-x save-some-buffers RET"), Qnil));
3412 staticpro (&memory_signal_data);
3414 staticpro (&Qgc_cons_threshold);
3415 Qgc_cons_threshold = intern ("gc-cons-threshold");
3417 staticpro (&Qchar_table_extra_slots);
3418 Qchar_table_extra_slots = intern ("char-table-extra-slots");
3420 defsubr (&Scons);
3421 defsubr (&Slist);
3422 defsubr (&Svector);
3423 defsubr (&Smake_byte_code);
3424 defsubr (&Smake_list);
3425 defsubr (&Smake_vector);
3426 defsubr (&Smake_char_table);
3427 defsubr (&Smake_string);
3428 defsubr (&Smake_bool_vector);
3429 defsubr (&Smake_symbol);
3430 defsubr (&Smake_marker);
3431 defsubr (&Spurecopy);
3432 defsubr (&Sgarbage_collect);
3433 defsubr (&Smemory_limit);
3434 defsubr (&Smemory_use_counts);