]> code.delx.au - gnu-emacs/blob - src/alloc.c
(file-name-non-special): If "/:" is the entire file
[gnu-emacs] / src / alloc.c
1 /* Storage allocation and gc for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 86, 88, 93, 94, 95, 97 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 /* Note that this declares bzero on OSF/1. How dumb. */
22 #include <signal.h>
23
24 #include <config.h>
25 #include "lisp.h"
26 #include "intervals.h"
27 #include "puresize.h"
28 #ifndef standalone
29 #include "buffer.h"
30 #include "window.h"
31 #include "frame.h"
32 #include "blockinput.h"
33 #include "keyboard.h"
34 #endif
35
36 #include "syssignal.h"
37
38 extern char *sbrk ();
39
40 #ifdef DOUG_LEA_MALLOC
41 #include <malloc.h>
42 #define __malloc_size_t int
43 #else
44 /* The following come from gmalloc.c. */
45
46 #if defined (__STDC__) && __STDC__
47 #include <stddef.h>
48 #define __malloc_size_t size_t
49 #else
50 #define __malloc_size_t unsigned int
51 #endif
52 extern __malloc_size_t _bytes_used;
53 extern int __malloc_extra_blocks;
54 #endif /* !defined(DOUG_LEA_MALLOC) */
55
56 extern Lisp_Object Vhistory_length;
57
58 #define max(A,B) ((A) > (B) ? (A) : (B))
59 #define min(A,B) ((A) < (B) ? (A) : (B))
60
61 /* Macro to verify that storage intended for Lisp objects is not
62 out of range to fit in the space for a pointer.
63 ADDRESS is the start of the block, and SIZE
64 is the amount of space within which objects can start. */
65 #define VALIDATE_LISP_STORAGE(address, size) \
66 do \
67 { \
68 Lisp_Object val; \
69 XSETCONS (val, (char *) address + size); \
70 if ((char *) XCONS (val) != (char *) address + size) \
71 { \
72 xfree (address); \
73 memory_full (); \
74 } \
75 } while (0)
76
77 /* Value of _bytes_used, when spare_memory was freed. */
78 static __malloc_size_t bytes_used_when_full;
79
80 /* Number of bytes of consing done since the last gc */
81 int consing_since_gc;
82
83 /* Count the amount of consing of various sorts of space. */
84 int cons_cells_consed;
85 int floats_consed;
86 int vector_cells_consed;
87 int symbols_consed;
88 int string_chars_consed;
89 int misc_objects_consed;
90 int intervals_consed;
91
92 /* Number of bytes of consing since gc before another gc should be done. */
93 int gc_cons_threshold;
94
95 /* Nonzero during gc */
96 int gc_in_progress;
97
98 /* Nonzero means display messages at beginning and end of GC. */
99 int garbage_collection_messages;
100
101 #ifndef VIRT_ADDR_VARIES
102 extern
103 #endif /* VIRT_ADDR_VARIES */
104 int malloc_sbrk_used;
105
106 #ifndef VIRT_ADDR_VARIES
107 extern
108 #endif /* VIRT_ADDR_VARIES */
109 int malloc_sbrk_unused;
110
111 /* Two limits controlling how much undo information to keep. */
112 int undo_limit;
113 int undo_strong_limit;
114
115 int total_conses, total_markers, total_symbols, total_string_size, total_vector_size;
116 int total_free_conses, total_free_markers, total_free_symbols;
117 #ifdef LISP_FLOAT_TYPE
118 int total_free_floats, total_floats;
119 #endif /* LISP_FLOAT_TYPE */
120
121 /* Points to memory space allocated as "spare",
122 to be freed if we run out of memory. */
123 static char *spare_memory;
124
125 /* Amount of spare memory to keep in reserve. */
126 #define SPARE_MEMORY (1 << 14)
127
128 /* Number of extra blocks malloc should get when it needs more core. */
129 static int malloc_hysteresis;
130
131 /* Nonzero when malloc is called for allocating Lisp object space. */
132 int allocating_for_lisp;
133
134 /* Non-nil means defun should do purecopy on the function definition */
135 Lisp_Object Vpurify_flag;
136
137 #ifndef HAVE_SHM
138 EMACS_INT pure[PURESIZE / sizeof (EMACS_INT)] = {0,}; /* Force it into data space! */
139 #define PUREBEG (char *) pure
140 #else
141 #define pure PURE_SEG_BITS /* Use shared memory segment */
142 #define PUREBEG (char *)PURE_SEG_BITS
143
144 /* This variable is used only by the XPNTR macro when HAVE_SHM is
145 defined. If we used the PURESIZE macro directly there, that would
146 make most of emacs dependent on puresize.h, which we don't want -
147 you should be able to change that without too much recompilation.
148 So map_in_data initializes pure_size, and the dependencies work
149 out. */
150 EMACS_INT pure_size;
151 #endif /* not HAVE_SHM */
152
153 /* Index in pure at which next pure object will be allocated. */
154 int pureptr;
155
156 /* If nonzero, this is a warning delivered by malloc and not yet displayed. */
157 char *pending_malloc_warning;
158
159 /* Pre-computed signal argument for use when memory is exhausted. */
160 Lisp_Object memory_signal_data;
161
162 /* Maximum amount of C stack to save when a GC happens. */
163
164 #ifndef MAX_SAVE_STACK
165 #define MAX_SAVE_STACK 16000
166 #endif
167
168 /* Define DONT_COPY_FLAG to be some bit which will always be zero in a
169 pointer to a Lisp_Object, when that pointer is viewed as an integer.
170 (On most machines, pointers are even, so we can use the low bit.
171 Word-addressable architectures may need to override this in the m-file.)
172 When linking references to small strings through the size field, we
173 use this slot to hold the bit that would otherwise be interpreted as
174 the GC mark bit. */
175 #ifndef DONT_COPY_FLAG
176 #define DONT_COPY_FLAG 1
177 #endif /* no DONT_COPY_FLAG */
178
179 /* Buffer in which we save a copy of the C stack at each GC. */
180
181 char *stack_copy;
182 int stack_copy_size;
183
184 /* Non-zero means ignore malloc warnings. Set during initialization. */
185 int ignore_warnings;
186
187 Lisp_Object Qgc_cons_threshold, Qchar_table_extra_slots;
188
189 static void mark_object (), mark_buffer (), mark_kboards ();
190 static void clear_marks (), gc_sweep ();
191 static void compact_strings ();
192 \f
193 /* Versions of malloc and realloc that print warnings as memory gets full. */
194
195 Lisp_Object
196 malloc_warning_1 (str)
197 Lisp_Object str;
198 {
199 Fprinc (str, Vstandard_output);
200 write_string ("\nKilling some buffers may delay running out of memory.\n", -1);
201 write_string ("However, certainly by the time you receive the 95% warning,\n", -1);
202 write_string ("you should clean up, kill this Emacs, and start a new one.", -1);
203 return Qnil;
204 }
205
206 /* malloc calls this if it finds we are near exhausting storage */
207 malloc_warning (str)
208 char *str;
209 {
210 pending_malloc_warning = str;
211 }
212
213 display_malloc_warning ()
214 {
215 register Lisp_Object val;
216
217 val = build_string (pending_malloc_warning);
218 pending_malloc_warning = 0;
219 internal_with_output_to_temp_buffer (" *Danger*", malloc_warning_1, val);
220 }
221
222 #ifdef DOUG_LEA_MALLOC
223 # define BYTES_USED (mallinfo ().arena)
224 #else
225 # define BYTES_USED _bytes_used
226 #endif
227
228 /* Called if malloc returns zero */
229
230 memory_full ()
231 {
232 #ifndef SYSTEM_MALLOC
233 bytes_used_when_full = BYTES_USED;
234 #endif
235
236 /* The first time we get here, free the spare memory. */
237 if (spare_memory)
238 {
239 free (spare_memory);
240 spare_memory = 0;
241 }
242
243 /* This used to call error, but if we've run out of memory, we could get
244 infinite recursion trying to build the string. */
245 while (1)
246 Fsignal (Qnil, memory_signal_data);
247 }
248
249 /* Called if we can't allocate relocatable space for a buffer. */
250
251 void
252 buffer_memory_full ()
253 {
254 /* If buffers use the relocating allocator,
255 no need to free spare_memory, because we may have plenty of malloc
256 space left that we could get, and if we don't, the malloc that fails
257 will itself cause spare_memory to be freed.
258 If buffers don't use the relocating allocator,
259 treat this like any other failing malloc. */
260
261 #ifndef REL_ALLOC
262 memory_full ();
263 #endif
264
265 /* This used to call error, but if we've run out of memory, we could get
266 infinite recursion trying to build the string. */
267 while (1)
268 Fsignal (Qerror, memory_signal_data);
269 }
270
271 /* like malloc routines but check for no memory and block interrupt input. */
272
273 long *
274 xmalloc (size)
275 int size;
276 {
277 register long *val;
278
279 BLOCK_INPUT;
280 val = (long *) malloc (size);
281 UNBLOCK_INPUT;
282
283 if (!val && size) memory_full ();
284 return val;
285 }
286
287 long *
288 xrealloc (block, size)
289 long *block;
290 int size;
291 {
292 register long *val;
293
294 BLOCK_INPUT;
295 /* We must call malloc explicitly when BLOCK is 0, since some
296 reallocs don't do this. */
297 if (! block)
298 val = (long *) malloc (size);
299 else
300 val = (long *) realloc (block, size);
301 UNBLOCK_INPUT;
302
303 if (!val && size) memory_full ();
304 return val;
305 }
306
307 void
308 xfree (block)
309 long *block;
310 {
311 BLOCK_INPUT;
312 free (block);
313 UNBLOCK_INPUT;
314 }
315
316 \f
317 /* Arranging to disable input signals while we're in malloc.
318
319 This only works with GNU malloc. To help out systems which can't
320 use GNU malloc, all the calls to malloc, realloc, and free
321 elsewhere in the code should be inside a BLOCK_INPUT/UNBLOCK_INPUT
322 pairs; unfortunately, we have no idea what C library functions
323 might call malloc, so we can't really protect them unless you're
324 using GNU malloc. Fortunately, most of the major operating can use
325 GNU malloc. */
326
327 #ifndef SYSTEM_MALLOC
328 extern void * (*__malloc_hook) ();
329 static void * (*old_malloc_hook) ();
330 extern void * (*__realloc_hook) ();
331 static void * (*old_realloc_hook) ();
332 extern void (*__free_hook) ();
333 static void (*old_free_hook) ();
334
335 /* This function is used as the hook for free to call. */
336
337 static void
338 emacs_blocked_free (ptr)
339 void *ptr;
340 {
341 BLOCK_INPUT;
342 __free_hook = old_free_hook;
343 free (ptr);
344 /* If we released our reserve (due to running out of memory),
345 and we have a fair amount free once again,
346 try to set aside another reserve in case we run out once more. */
347 if (spare_memory == 0
348 /* Verify there is enough space that even with the malloc
349 hysteresis this call won't run out again.
350 The code here is correct as long as SPARE_MEMORY
351 is substantially larger than the block size malloc uses. */
352 && (bytes_used_when_full
353 > BYTES_USED + max (malloc_hysteresis, 4) * SPARE_MEMORY))
354 spare_memory = (char *) malloc (SPARE_MEMORY);
355
356 __free_hook = emacs_blocked_free;
357 UNBLOCK_INPUT;
358 }
359
360 /* If we released our reserve (due to running out of memory),
361 and we have a fair amount free once again,
362 try to set aside another reserve in case we run out once more.
363
364 This is called when a relocatable block is freed in ralloc.c. */
365
366 void
367 refill_memory_reserve ()
368 {
369 if (spare_memory == 0)
370 spare_memory = (char *) malloc (SPARE_MEMORY);
371 }
372
373 /* This function is the malloc hook that Emacs uses. */
374
375 static void *
376 emacs_blocked_malloc (size)
377 unsigned size;
378 {
379 void *value;
380
381 BLOCK_INPUT;
382 __malloc_hook = old_malloc_hook;
383 #ifdef DOUG_LEA_MALLOC
384 mallopt (M_TOP_PAD, malloc_hysteresis * 4096);
385 #else
386 __malloc_extra_blocks = malloc_hysteresis;
387 #endif
388 value = (void *) malloc (size);
389 __malloc_hook = emacs_blocked_malloc;
390 UNBLOCK_INPUT;
391
392 return value;
393 }
394
395 static void *
396 emacs_blocked_realloc (ptr, size)
397 void *ptr;
398 unsigned size;
399 {
400 void *value;
401
402 BLOCK_INPUT;
403 __realloc_hook = old_realloc_hook;
404 value = (void *) realloc (ptr, size);
405 __realloc_hook = emacs_blocked_realloc;
406 UNBLOCK_INPUT;
407
408 return value;
409 }
410
411 void
412 uninterrupt_malloc ()
413 {
414 old_free_hook = __free_hook;
415 __free_hook = emacs_blocked_free;
416
417 old_malloc_hook = __malloc_hook;
418 __malloc_hook = emacs_blocked_malloc;
419
420 old_realloc_hook = __realloc_hook;
421 __realloc_hook = emacs_blocked_realloc;
422 }
423 #endif
424 \f
425 /* Interval allocation. */
426
427 #ifdef USE_TEXT_PROPERTIES
428 #define INTERVAL_BLOCK_SIZE \
429 ((1020 - sizeof (struct interval_block *)) / sizeof (struct interval))
430
431 struct interval_block
432 {
433 struct interval_block *next;
434 struct interval intervals[INTERVAL_BLOCK_SIZE];
435 };
436
437 struct interval_block *interval_block;
438 static int interval_block_index;
439
440 INTERVAL interval_free_list;
441
442 static void
443 init_intervals ()
444 {
445 allocating_for_lisp = 1;
446 interval_block
447 = (struct interval_block *) malloc (sizeof (struct interval_block));
448 allocating_for_lisp = 0;
449 interval_block->next = 0;
450 bzero ((char *) interval_block->intervals, sizeof interval_block->intervals);
451 interval_block_index = 0;
452 interval_free_list = 0;
453 }
454
455 #define INIT_INTERVALS init_intervals ()
456
457 INTERVAL
458 make_interval ()
459 {
460 INTERVAL val;
461
462 if (interval_free_list)
463 {
464 val = interval_free_list;
465 interval_free_list = interval_free_list->parent;
466 }
467 else
468 {
469 if (interval_block_index == INTERVAL_BLOCK_SIZE)
470 {
471 register struct interval_block *newi;
472
473 allocating_for_lisp = 1;
474 newi = (struct interval_block *) xmalloc (sizeof (struct interval_block));
475
476 allocating_for_lisp = 0;
477 VALIDATE_LISP_STORAGE (newi, sizeof *newi);
478 newi->next = interval_block;
479 interval_block = newi;
480 interval_block_index = 0;
481 }
482 val = &interval_block->intervals[interval_block_index++];
483 }
484 consing_since_gc += sizeof (struct interval);
485 intervals_consed++;
486 RESET_INTERVAL (val);
487 return val;
488 }
489
490 static int total_free_intervals, total_intervals;
491
492 /* Mark the pointers of one interval. */
493
494 static void
495 mark_interval (i, dummy)
496 register INTERVAL i;
497 Lisp_Object dummy;
498 {
499 if (XMARKBIT (i->plist))
500 abort ();
501 mark_object (&i->plist);
502 XMARK (i->plist);
503 }
504
505 static void
506 mark_interval_tree (tree)
507 register INTERVAL tree;
508 {
509 /* No need to test if this tree has been marked already; this
510 function is always called through the MARK_INTERVAL_TREE macro,
511 which takes care of that. */
512
513 /* XMARK expands to an assignment; the LHS of an assignment can't be
514 a cast. */
515 XMARK (* (Lisp_Object *) &tree->parent);
516
517 traverse_intervals (tree, 1, 0, mark_interval, Qnil);
518 }
519
520 #define MARK_INTERVAL_TREE(i) \
521 do { \
522 if (!NULL_INTERVAL_P (i) \
523 && ! XMARKBIT (*(Lisp_Object *) &i->parent)) \
524 mark_interval_tree (i); \
525 } while (0)
526
527 /* The oddity in the call to XUNMARK is necessary because XUNMARK
528 expands to an assignment to its argument, and most C compilers don't
529 support casts on the left operand of `='. */
530 #define UNMARK_BALANCE_INTERVALS(i) \
531 { \
532 if (! NULL_INTERVAL_P (i)) \
533 { \
534 XUNMARK (* (Lisp_Object *) (&(i)->parent)); \
535 (i) = balance_intervals (i); \
536 } \
537 }
538
539 #else /* no interval use */
540
541 #define INIT_INTERVALS
542
543 #define UNMARK_BALANCE_INTERVALS(i)
544 #define MARK_INTERVAL_TREE(i)
545
546 #endif /* no interval use */
547 \f
548 /* Floating point allocation. */
549
550 #ifdef LISP_FLOAT_TYPE
551 /* Allocation of float cells, just like conses */
552 /* We store float cells inside of float_blocks, allocating a new
553 float_block with malloc whenever necessary. Float cells reclaimed by
554 GC are put on a free list to be reallocated before allocating
555 any new float cells from the latest float_block.
556
557 Each float_block is just under 1020 bytes long,
558 since malloc really allocates in units of powers of two
559 and uses 4 bytes for its own overhead. */
560
561 #define FLOAT_BLOCK_SIZE \
562 ((1020 - sizeof (struct float_block *)) / sizeof (struct Lisp_Float))
563
564 struct float_block
565 {
566 struct float_block *next;
567 struct Lisp_Float floats[FLOAT_BLOCK_SIZE];
568 };
569
570 struct float_block *float_block;
571 int float_block_index;
572
573 struct Lisp_Float *float_free_list;
574
575 void
576 init_float ()
577 {
578 allocating_for_lisp = 1;
579 float_block = (struct float_block *) malloc (sizeof (struct float_block));
580 allocating_for_lisp = 0;
581 float_block->next = 0;
582 bzero ((char *) float_block->floats, sizeof float_block->floats);
583 float_block_index = 0;
584 float_free_list = 0;
585 }
586
587 /* Explicitly free a float cell. */
588 free_float (ptr)
589 struct Lisp_Float *ptr;
590 {
591 *(struct Lisp_Float **)&ptr->data = float_free_list;
592 float_free_list = ptr;
593 }
594
595 Lisp_Object
596 make_float (float_value)
597 double float_value;
598 {
599 register Lisp_Object val;
600
601 if (float_free_list)
602 {
603 /* We use the data field for chaining the free list
604 so that we won't use the same field that has the mark bit. */
605 XSETFLOAT (val, float_free_list);
606 float_free_list = *(struct Lisp_Float **)&float_free_list->data;
607 }
608 else
609 {
610 if (float_block_index == FLOAT_BLOCK_SIZE)
611 {
612 register struct float_block *new;
613
614 allocating_for_lisp = 1;
615 new = (struct float_block *) xmalloc (sizeof (struct float_block));
616 allocating_for_lisp = 0;
617 VALIDATE_LISP_STORAGE (new, sizeof *new);
618 new->next = float_block;
619 float_block = new;
620 float_block_index = 0;
621 }
622 XSETFLOAT (val, &float_block->floats[float_block_index++]);
623 }
624 XFLOAT (val)->data = float_value;
625 XSETFASTINT (XFLOAT (val)->type, 0); /* bug chasing -wsr */
626 consing_since_gc += sizeof (struct Lisp_Float);
627 floats_consed++;
628 return val;
629 }
630
631 #endif /* LISP_FLOAT_TYPE */
632 \f
633 /* Allocation of cons cells */
634 /* We store cons cells inside of cons_blocks, allocating a new
635 cons_block with malloc whenever necessary. Cons cells reclaimed by
636 GC are put on a free list to be reallocated before allocating
637 any new cons cells from the latest cons_block.
638
639 Each cons_block is just under 1020 bytes long,
640 since malloc really allocates in units of powers of two
641 and uses 4 bytes for its own overhead. */
642
643 #define CONS_BLOCK_SIZE \
644 ((1020 - sizeof (struct cons_block *)) / sizeof (struct Lisp_Cons))
645
646 struct cons_block
647 {
648 struct cons_block *next;
649 struct Lisp_Cons conses[CONS_BLOCK_SIZE];
650 };
651
652 struct cons_block *cons_block;
653 int cons_block_index;
654
655 struct Lisp_Cons *cons_free_list;
656
657 void
658 init_cons ()
659 {
660 allocating_for_lisp = 1;
661 cons_block = (struct cons_block *) malloc (sizeof (struct cons_block));
662 allocating_for_lisp = 0;
663 cons_block->next = 0;
664 bzero ((char *) cons_block->conses, sizeof cons_block->conses);
665 cons_block_index = 0;
666 cons_free_list = 0;
667 }
668
669 /* Explicitly free a cons cell. */
670 free_cons (ptr)
671 struct Lisp_Cons *ptr;
672 {
673 *(struct Lisp_Cons **)&ptr->cdr = cons_free_list;
674 cons_free_list = ptr;
675 }
676
677 DEFUN ("cons", Fcons, Scons, 2, 2, 0,
678 "Create a new cons, give it CAR and CDR as components, and return it.")
679 (car, cdr)
680 Lisp_Object car, cdr;
681 {
682 register Lisp_Object val;
683
684 if (cons_free_list)
685 {
686 /* We use the cdr for chaining the free list
687 so that we won't use the same field that has the mark bit. */
688 XSETCONS (val, cons_free_list);
689 cons_free_list = *(struct Lisp_Cons **)&cons_free_list->cdr;
690 }
691 else
692 {
693 if (cons_block_index == CONS_BLOCK_SIZE)
694 {
695 register struct cons_block *new;
696 allocating_for_lisp = 1;
697 new = (struct cons_block *) xmalloc (sizeof (struct cons_block));
698 allocating_for_lisp = 0;
699 VALIDATE_LISP_STORAGE (new, sizeof *new);
700 new->next = cons_block;
701 cons_block = new;
702 cons_block_index = 0;
703 }
704 XSETCONS (val, &cons_block->conses[cons_block_index++]);
705 }
706 XCONS (val)->car = car;
707 XCONS (val)->cdr = cdr;
708 consing_since_gc += sizeof (struct Lisp_Cons);
709 cons_cells_consed++;
710 return val;
711 }
712
713 DEFUN ("list", Flist, Slist, 0, MANY, 0,
714 "Return a newly created list with specified arguments as elements.\n\
715 Any number of arguments, even zero arguments, are allowed.")
716 (nargs, args)
717 int nargs;
718 register Lisp_Object *args;
719 {
720 register Lisp_Object val;
721 val = Qnil;
722
723 while (nargs > 0)
724 {
725 nargs--;
726 val = Fcons (args[nargs], val);
727 }
728 return val;
729 }
730
731 DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0,
732 "Return a newly created list of length LENGTH, with each element being INIT.")
733 (length, init)
734 register Lisp_Object length, init;
735 {
736 register Lisp_Object val;
737 register int size;
738
739 CHECK_NATNUM (length, 0);
740 size = XFASTINT (length);
741
742 val = Qnil;
743 while (size-- > 0)
744 val = Fcons (init, val);
745 return val;
746 }
747 \f
748 /* Allocation of vectors */
749
750 struct Lisp_Vector *all_vectors;
751
752 struct Lisp_Vector *
753 allocate_vectorlike (len)
754 EMACS_INT len;
755 {
756 struct Lisp_Vector *p;
757
758 allocating_for_lisp = 1;
759 #ifdef DOUG_LEA_MALLOC
760 /* Prevent mmap'ing the chunk (which is potentially very large). */
761 mallopt (M_MMAP_MAX, 0);
762 #endif
763 p = (struct Lisp_Vector *)xmalloc (sizeof (struct Lisp_Vector)
764 + (len - 1) * sizeof (Lisp_Object));
765 #ifdef DOUG_LEA_MALLOC
766 /* Back to a reasonable maximum of mmap'ed areas. */
767 mallopt (M_MMAP_MAX, 64);
768 #endif
769 allocating_for_lisp = 0;
770 VALIDATE_LISP_STORAGE (p, 0);
771 consing_since_gc += (sizeof (struct Lisp_Vector)
772 + (len - 1) * sizeof (Lisp_Object));
773 vector_cells_consed += len;
774
775 p->next = all_vectors;
776 all_vectors = p;
777 return p;
778 }
779
780 DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0,
781 "Return a newly created vector of length LENGTH, with each element being INIT.\n\
782 See also the function `vector'.")
783 (length, init)
784 register Lisp_Object length, init;
785 {
786 Lisp_Object vector;
787 register EMACS_INT sizei;
788 register int index;
789 register struct Lisp_Vector *p;
790
791 CHECK_NATNUM (length, 0);
792 sizei = XFASTINT (length);
793
794 p = allocate_vectorlike (sizei);
795 p->size = sizei;
796 for (index = 0; index < sizei; index++)
797 p->contents[index] = init;
798
799 XSETVECTOR (vector, p);
800 return vector;
801 }
802
803 DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0,
804 "Return a newly created char-table, with purpose PURPOSE.\n\
805 Each element is initialized to INIT, which defaults to nil.\n\
806 PURPOSE should be a symbol which has a `char-table-extra-slots' property.\n\
807 The property's value should be an integer between 0 and 10.")
808 (purpose, init)
809 register Lisp_Object purpose, init;
810 {
811 Lisp_Object vector;
812 Lisp_Object n;
813 CHECK_SYMBOL (purpose, 1);
814 n = Fget (purpose, Qchar_table_extra_slots);
815 CHECK_NUMBER (n, 0);
816 if (XINT (n) < 0 || XINT (n) > 10)
817 args_out_of_range (n, Qnil);
818 /* Add 2 to the size for the defalt and parent slots. */
819 vector = Fmake_vector (make_number (CHAR_TABLE_STANDARD_SLOTS + XINT (n)),
820 init);
821 XCHAR_TABLE (vector)->top = Qt;
822 XCHAR_TABLE (vector)->parent = Qnil;
823 XCHAR_TABLE (vector)->purpose = purpose;
824 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
825 return vector;
826 }
827
828 /* Return a newly created sub char table with default value DEFALT.
829 Since a sub char table does not appear as a top level Emacs Lisp
830 object, we don't need a Lisp interface to make it. */
831
832 Lisp_Object
833 make_sub_char_table (defalt)
834 Lisp_Object defalt;
835 {
836 Lisp_Object vector
837 = Fmake_vector (make_number (SUB_CHAR_TABLE_STANDARD_SLOTS), Qnil);
838 XCHAR_TABLE (vector)->top = Qnil;
839 XCHAR_TABLE (vector)->defalt = defalt;
840 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
841 return vector;
842 }
843
844 DEFUN ("vector", Fvector, Svector, 0, MANY, 0,
845 "Return a newly created vector with specified arguments as elements.\n\
846 Any number of arguments, even zero arguments, are allowed.")
847 (nargs, args)
848 register int nargs;
849 Lisp_Object *args;
850 {
851 register Lisp_Object len, val;
852 register int index;
853 register struct Lisp_Vector *p;
854
855 XSETFASTINT (len, nargs);
856 val = Fmake_vector (len, Qnil);
857 p = XVECTOR (val);
858 for (index = 0; index < nargs; index++)
859 p->contents[index] = args[index];
860 return val;
861 }
862
863 DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0,
864 "Create a byte-code object with specified arguments as elements.\n\
865 The arguments should be the arglist, bytecode-string, constant vector,\n\
866 stack size, (optional) doc string, and (optional) interactive spec.\n\
867 The first four arguments are required; at most six have any\n\
868 significance.")
869 (nargs, args)
870 register int nargs;
871 Lisp_Object *args;
872 {
873 register Lisp_Object len, val;
874 register int index;
875 register struct Lisp_Vector *p;
876
877 XSETFASTINT (len, nargs);
878 if (!NILP (Vpurify_flag))
879 val = make_pure_vector ((EMACS_INT) nargs);
880 else
881 val = Fmake_vector (len, Qnil);
882 p = XVECTOR (val);
883 for (index = 0; index < nargs; index++)
884 {
885 if (!NILP (Vpurify_flag))
886 args[index] = Fpurecopy (args[index]);
887 p->contents[index] = args[index];
888 }
889 XSETCOMPILED (val, p);
890 return val;
891 }
892 \f
893 /* Allocation of symbols.
894 Just like allocation of conses!
895
896 Each symbol_block is just under 1020 bytes long,
897 since malloc really allocates in units of powers of two
898 and uses 4 bytes for its own overhead. */
899
900 #define SYMBOL_BLOCK_SIZE \
901 ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol))
902
903 struct symbol_block
904 {
905 struct symbol_block *next;
906 struct Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE];
907 };
908
909 struct symbol_block *symbol_block;
910 int symbol_block_index;
911
912 struct Lisp_Symbol *symbol_free_list;
913
914 void
915 init_symbol ()
916 {
917 allocating_for_lisp = 1;
918 symbol_block = (struct symbol_block *) malloc (sizeof (struct symbol_block));
919 allocating_for_lisp = 0;
920 symbol_block->next = 0;
921 bzero ((char *) symbol_block->symbols, sizeof symbol_block->symbols);
922 symbol_block_index = 0;
923 symbol_free_list = 0;
924 }
925
926 DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0,
927 "Return a newly allocated uninterned symbol whose name is NAME.\n\
928 Its value and function definition are void, and its property list is nil.")
929 (name)
930 Lisp_Object name;
931 {
932 register Lisp_Object val;
933 register struct Lisp_Symbol *p;
934
935 CHECK_STRING (name, 0);
936
937 if (symbol_free_list)
938 {
939 XSETSYMBOL (val, symbol_free_list);
940 symbol_free_list = *(struct Lisp_Symbol **)&symbol_free_list->value;
941 }
942 else
943 {
944 if (symbol_block_index == SYMBOL_BLOCK_SIZE)
945 {
946 struct symbol_block *new;
947 allocating_for_lisp = 1;
948 new = (struct symbol_block *) xmalloc (sizeof (struct symbol_block));
949 allocating_for_lisp = 0;
950 VALIDATE_LISP_STORAGE (new, sizeof *new);
951 new->next = symbol_block;
952 symbol_block = new;
953 symbol_block_index = 0;
954 }
955 XSETSYMBOL (val, &symbol_block->symbols[symbol_block_index++]);
956 }
957 p = XSYMBOL (val);
958 p->name = XSTRING (name);
959 p->obarray = Qnil;
960 p->plist = Qnil;
961 p->value = Qunbound;
962 p->function = Qunbound;
963 p->next = 0;
964 consing_since_gc += sizeof (struct Lisp_Symbol);
965 symbols_consed++;
966 return val;
967 }
968 \f
969 /* Allocation of markers and other objects that share that structure.
970 Works like allocation of conses. */
971
972 #define MARKER_BLOCK_SIZE \
973 ((1020 - sizeof (struct marker_block *)) / sizeof (union Lisp_Misc))
974
975 struct marker_block
976 {
977 struct marker_block *next;
978 union Lisp_Misc markers[MARKER_BLOCK_SIZE];
979 };
980
981 struct marker_block *marker_block;
982 int marker_block_index;
983
984 union Lisp_Misc *marker_free_list;
985
986 void
987 init_marker ()
988 {
989 allocating_for_lisp = 1;
990 marker_block = (struct marker_block *) malloc (sizeof (struct marker_block));
991 allocating_for_lisp = 0;
992 marker_block->next = 0;
993 bzero ((char *) marker_block->markers, sizeof marker_block->markers);
994 marker_block_index = 0;
995 marker_free_list = 0;
996 }
997
998 /* Return a newly allocated Lisp_Misc object, with no substructure. */
999 Lisp_Object
1000 allocate_misc ()
1001 {
1002 Lisp_Object val;
1003
1004 if (marker_free_list)
1005 {
1006 XSETMISC (val, marker_free_list);
1007 marker_free_list = marker_free_list->u_free.chain;
1008 }
1009 else
1010 {
1011 if (marker_block_index == MARKER_BLOCK_SIZE)
1012 {
1013 struct marker_block *new;
1014 allocating_for_lisp = 1;
1015 new = (struct marker_block *) xmalloc (sizeof (struct marker_block));
1016 allocating_for_lisp = 0;
1017 VALIDATE_LISP_STORAGE (new, sizeof *new);
1018 new->next = marker_block;
1019 marker_block = new;
1020 marker_block_index = 0;
1021 }
1022 XSETMISC (val, &marker_block->markers[marker_block_index++]);
1023 }
1024 consing_since_gc += sizeof (union Lisp_Misc);
1025 misc_objects_consed++;
1026 return val;
1027 }
1028
1029 DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
1030 "Return a newly allocated marker which does not point at any place.")
1031 ()
1032 {
1033 register Lisp_Object val;
1034 register struct Lisp_Marker *p;
1035
1036 val = allocate_misc ();
1037 XMISCTYPE (val) = Lisp_Misc_Marker;
1038 p = XMARKER (val);
1039 p->buffer = 0;
1040 p->bufpos = 0;
1041 p->chain = Qnil;
1042 p->insertion_type = 0;
1043 return val;
1044 }
1045
1046 /* Put MARKER back on the free list after using it temporarily. */
1047
1048 free_marker (marker)
1049 Lisp_Object marker;
1050 {
1051 unchain_marker (marker);
1052
1053 XMISC (marker)->u_marker.type = Lisp_Misc_Free;
1054 XMISC (marker)->u_free.chain = marker_free_list;
1055 marker_free_list = XMISC (marker);
1056
1057 total_free_markers++;
1058 }
1059 \f
1060 /* Allocation of strings */
1061
1062 /* Strings reside inside of string_blocks. The entire data of the string,
1063 both the size and the contents, live in part of the `chars' component of a string_block.
1064 The `pos' component is the index within `chars' of the first free byte.
1065
1066 first_string_block points to the first string_block ever allocated.
1067 Each block points to the next one with its `next' field.
1068 The `prev' fields chain in reverse order.
1069 The last one allocated is the one currently being filled.
1070 current_string_block points to it.
1071
1072 The string_blocks that hold individual large strings
1073 go in a separate chain, started by large_string_blocks. */
1074
1075
1076 /* String blocks contain this many useful bytes.
1077 8188 is power of 2, minus 4 for malloc overhead. */
1078 #define STRING_BLOCK_SIZE (8188 - sizeof (struct string_block_head))
1079
1080 /* A string bigger than this gets its own specially-made string block
1081 if it doesn't fit in the current one. */
1082 #define STRING_BLOCK_OUTSIZE 1024
1083
1084 struct string_block_head
1085 {
1086 struct string_block *next, *prev;
1087 EMACS_INT pos;
1088 };
1089
1090 struct string_block
1091 {
1092 struct string_block *next, *prev;
1093 EMACS_INT pos;
1094 char chars[STRING_BLOCK_SIZE];
1095 };
1096
1097 /* This points to the string block we are now allocating strings. */
1098
1099 struct string_block *current_string_block;
1100
1101 /* This points to the oldest string block, the one that starts the chain. */
1102
1103 struct string_block *first_string_block;
1104
1105 /* Last string block in chain of those made for individual large strings. */
1106
1107 struct string_block *large_string_blocks;
1108
1109 /* If SIZE is the length of a string, this returns how many bytes
1110 the string occupies in a string_block (including padding). */
1111
1112 #define STRING_FULLSIZE(size) (((size) + sizeof (struct Lisp_String) + PAD) \
1113 & ~(PAD - 1))
1114 #define PAD (sizeof (EMACS_INT))
1115
1116 #if 0
1117 #define STRING_FULLSIZE(SIZE) \
1118 (((SIZE) + 2 * sizeof (EMACS_INT)) & ~(sizeof (EMACS_INT) - 1))
1119 #endif
1120
1121 void
1122 init_strings ()
1123 {
1124 allocating_for_lisp = 1;
1125 current_string_block = (struct string_block *) malloc (sizeof (struct string_block));
1126 allocating_for_lisp = 0;
1127 first_string_block = current_string_block;
1128 consing_since_gc += sizeof (struct string_block);
1129 current_string_block->next = 0;
1130 current_string_block->prev = 0;
1131 current_string_block->pos = 0;
1132 large_string_blocks = 0;
1133 }
1134
1135 DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0,
1136 "Return a newly created string of length LENGTH, with each element being INIT.\n\
1137 Both LENGTH and INIT must be numbers.")
1138 (length, init)
1139 Lisp_Object length, init;
1140 {
1141 register Lisp_Object val;
1142 register unsigned char *p, *end, c;
1143
1144 CHECK_NATNUM (length, 0);
1145 CHECK_NUMBER (init, 1);
1146 val = make_uninit_string (XFASTINT (length));
1147 c = XINT (init);
1148 p = XSTRING (val)->data;
1149 end = p + XSTRING (val)->size;
1150 while (p != end)
1151 *p++ = c;
1152 *p = 0;
1153 return val;
1154 }
1155
1156 DEFUN ("make-bool-vector", Fmake_bool_vector, Smake_bool_vector, 2, 2, 0,
1157 "Return a new bool-vector of length LENGTH, using INIT for as each element.\n\
1158 LENGTH must be a number. INIT matters only in whether it is t or nil.")
1159 (length, init)
1160 Lisp_Object length, init;
1161 {
1162 register Lisp_Object val;
1163 struct Lisp_Bool_Vector *p;
1164 int real_init, i;
1165 int length_in_chars, length_in_elts, bits_per_value;
1166
1167 CHECK_NATNUM (length, 0);
1168
1169 bits_per_value = sizeof (EMACS_INT) * BITS_PER_CHAR;
1170
1171 length_in_elts = (XFASTINT (length) + bits_per_value - 1) / bits_per_value;
1172 length_in_chars = length_in_elts * sizeof (EMACS_INT);
1173
1174 /* We must allocate one more elements than LENGTH_IN_ELTS for the
1175 slot `size' of the struct Lisp_Bool_Vector. */
1176 val = Fmake_vector (make_number (length_in_elts + 1), Qnil);
1177 p = XBOOL_VECTOR (val);
1178 /* Get rid of any bits that would cause confusion. */
1179 p->vector_size = 0;
1180 XSETBOOL_VECTOR (val, p);
1181 p->size = XFASTINT (length);
1182
1183 real_init = (NILP (init) ? 0 : -1);
1184 for (i = 0; i < length_in_chars ; i++)
1185 p->data[i] = real_init;
1186
1187 return val;
1188 }
1189
1190 Lisp_Object
1191 make_string (contents, length)
1192 char *contents;
1193 int length;
1194 {
1195 register Lisp_Object val;
1196 val = make_uninit_string (length);
1197 bcopy (contents, XSTRING (val)->data, length);
1198 return val;
1199 }
1200
1201 Lisp_Object
1202 build_string (str)
1203 char *str;
1204 {
1205 return make_string (str, strlen (str));
1206 }
1207
1208 Lisp_Object
1209 make_uninit_string (length)
1210 int length;
1211 {
1212 register Lisp_Object val;
1213 register int fullsize = STRING_FULLSIZE (length);
1214
1215 if (length < 0) abort ();
1216
1217 if (fullsize <= STRING_BLOCK_SIZE - current_string_block->pos)
1218 /* This string can fit in the current string block */
1219 {
1220 XSETSTRING (val,
1221 ((struct Lisp_String *)
1222 (current_string_block->chars + current_string_block->pos)));
1223 current_string_block->pos += fullsize;
1224 }
1225 else if (fullsize > STRING_BLOCK_OUTSIZE)
1226 /* This string gets its own string block */
1227 {
1228 register struct string_block *new;
1229 allocating_for_lisp = 1;
1230 #ifdef DOUG_LEA_MALLOC
1231 /* Prevent mmap'ing the chunk (which is potentially very large). */
1232 mallopt (M_MMAP_MAX, 0);
1233 #endif
1234 new = (struct string_block *) xmalloc (sizeof (struct string_block_head) + fullsize);
1235 #ifdef DOUG_LEA_MALLOC
1236 /* Back to a reasonable maximum of mmap'ed areas. */
1237 mallopt (M_MMAP_MAX, 64);
1238 #endif
1239 allocating_for_lisp = 0;
1240 VALIDATE_LISP_STORAGE (new, 0);
1241 consing_since_gc += sizeof (struct string_block_head) + fullsize;
1242 new->pos = fullsize;
1243 new->next = large_string_blocks;
1244 large_string_blocks = new;
1245 XSETSTRING (val,
1246 ((struct Lisp_String *)
1247 ((struct string_block_head *)new + 1)));
1248 }
1249 else
1250 /* Make a new current string block and start it off with this string */
1251 {
1252 register struct string_block *new;
1253 allocating_for_lisp = 1;
1254 new = (struct string_block *) xmalloc (sizeof (struct string_block));
1255 allocating_for_lisp = 0;
1256 VALIDATE_LISP_STORAGE (new, sizeof *new);
1257 consing_since_gc += sizeof (struct string_block);
1258 current_string_block->next = new;
1259 new->prev = current_string_block;
1260 new->next = 0;
1261 current_string_block = new;
1262 new->pos = fullsize;
1263 XSETSTRING (val,
1264 (struct Lisp_String *) current_string_block->chars);
1265 }
1266
1267 string_chars_consed += fullsize;
1268 XSTRING (val)->size = length;
1269 XSTRING (val)->data[length] = 0;
1270 INITIALIZE_INTERVAL (XSTRING (val), NULL_INTERVAL);
1271
1272 return val;
1273 }
1274
1275 /* Return a newly created vector or string with specified arguments as
1276 elements. If all the arguments are characters that can fit
1277 in a string of events, make a string; otherwise, make a vector.
1278
1279 Any number of arguments, even zero arguments, are allowed. */
1280
1281 Lisp_Object
1282 make_event_array (nargs, args)
1283 register int nargs;
1284 Lisp_Object *args;
1285 {
1286 int i;
1287
1288 for (i = 0; i < nargs; i++)
1289 /* The things that fit in a string
1290 are characters that are in 0...127,
1291 after discarding the meta bit and all the bits above it. */
1292 if (!INTEGERP (args[i])
1293 || (XUINT (args[i]) & ~(-CHAR_META)) >= 0200)
1294 return Fvector (nargs, args);
1295
1296 /* Since the loop exited, we know that all the things in it are
1297 characters, so we can make a string. */
1298 {
1299 Lisp_Object result;
1300
1301 result = Fmake_string (make_number (nargs), make_number (0));
1302 for (i = 0; i < nargs; i++)
1303 {
1304 XSTRING (result)->data[i] = XINT (args[i]);
1305 /* Move the meta bit to the right place for a string char. */
1306 if (XINT (args[i]) & CHAR_META)
1307 XSTRING (result)->data[i] |= 0x80;
1308 }
1309
1310 return result;
1311 }
1312 }
1313 \f
1314 /* Pure storage management. */
1315
1316 /* Must get an error if pure storage is full,
1317 since if it cannot hold a large string
1318 it may be able to hold conses that point to that string;
1319 then the string is not protected from gc. */
1320
1321 Lisp_Object
1322 make_pure_string (data, length)
1323 char *data;
1324 int length;
1325 {
1326 register Lisp_Object new;
1327 register int size = sizeof (EMACS_INT) + INTERVAL_PTR_SIZE + length + 1;
1328
1329 if (pureptr + size > PURESIZE)
1330 error ("Pure Lisp storage exhausted");
1331 XSETSTRING (new, PUREBEG + pureptr);
1332 XSTRING (new)->size = length;
1333 bcopy (data, XSTRING (new)->data, length);
1334 XSTRING (new)->data[length] = 0;
1335
1336 /* We must give strings in pure storage some kind of interval. So we
1337 give them a null one. */
1338 #if defined (USE_TEXT_PROPERTIES)
1339 XSTRING (new)->intervals = NULL_INTERVAL;
1340 #endif
1341 pureptr += (size + sizeof (EMACS_INT) - 1)
1342 / sizeof (EMACS_INT) * sizeof (EMACS_INT);
1343 return new;
1344 }
1345
1346 Lisp_Object
1347 pure_cons (car, cdr)
1348 Lisp_Object car, cdr;
1349 {
1350 register Lisp_Object new;
1351
1352 if (pureptr + sizeof (struct Lisp_Cons) > PURESIZE)
1353 error ("Pure Lisp storage exhausted");
1354 XSETCONS (new, PUREBEG + pureptr);
1355 pureptr += sizeof (struct Lisp_Cons);
1356 XCONS (new)->car = Fpurecopy (car);
1357 XCONS (new)->cdr = Fpurecopy (cdr);
1358 return new;
1359 }
1360
1361 #ifdef LISP_FLOAT_TYPE
1362
1363 Lisp_Object
1364 make_pure_float (num)
1365 double num;
1366 {
1367 register Lisp_Object new;
1368
1369 /* Make sure that PUREBEG + pureptr is aligned on at least a sizeof
1370 (double) boundary. Some architectures (like the sparc) require
1371 this, and I suspect that floats are rare enough that it's no
1372 tragedy for those that do. */
1373 {
1374 int alignment;
1375 char *p = PUREBEG + pureptr;
1376
1377 #ifdef __GNUC__
1378 #if __GNUC__ >= 2
1379 alignment = __alignof (struct Lisp_Float);
1380 #else
1381 alignment = sizeof (struct Lisp_Float);
1382 #endif
1383 #else
1384 alignment = sizeof (struct Lisp_Float);
1385 #endif
1386 p = (char *) (((unsigned long) p + alignment - 1) & - alignment);
1387 pureptr = p - PUREBEG;
1388 }
1389
1390 if (pureptr + sizeof (struct Lisp_Float) > PURESIZE)
1391 error ("Pure Lisp storage exhausted");
1392 XSETFLOAT (new, PUREBEG + pureptr);
1393 pureptr += sizeof (struct Lisp_Float);
1394 XFLOAT (new)->data = num;
1395 XSETFASTINT (XFLOAT (new)->type, 0); /* bug chasing -wsr */
1396 return new;
1397 }
1398
1399 #endif /* LISP_FLOAT_TYPE */
1400
1401 Lisp_Object
1402 make_pure_vector (len)
1403 EMACS_INT len;
1404 {
1405 register Lisp_Object new;
1406 register EMACS_INT size = sizeof (struct Lisp_Vector) + (len - 1) * sizeof (Lisp_Object);
1407
1408 if (pureptr + size > PURESIZE)
1409 error ("Pure Lisp storage exhausted");
1410
1411 XSETVECTOR (new, PUREBEG + pureptr);
1412 pureptr += size;
1413 XVECTOR (new)->size = len;
1414 return new;
1415 }
1416
1417 DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
1418 "Make a copy of OBJECT in pure storage.\n\
1419 Recursively copies contents of vectors and cons cells.\n\
1420 Does not copy symbols.")
1421 (obj)
1422 register Lisp_Object obj;
1423 {
1424 if (NILP (Vpurify_flag))
1425 return obj;
1426
1427 if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
1428 && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
1429 return obj;
1430
1431 if (CONSP (obj))
1432 return pure_cons (XCONS (obj)->car, XCONS (obj)->cdr);
1433 #ifdef LISP_FLOAT_TYPE
1434 else if (FLOATP (obj))
1435 return make_pure_float (XFLOAT (obj)->data);
1436 #endif /* LISP_FLOAT_TYPE */
1437 else if (STRINGP (obj))
1438 return make_pure_string (XSTRING (obj)->data, XSTRING (obj)->size);
1439 else if (COMPILEDP (obj) || VECTORP (obj))
1440 {
1441 register struct Lisp_Vector *vec;
1442 register int i, size;
1443
1444 size = XVECTOR (obj)->size;
1445 if (size & PSEUDOVECTOR_FLAG)
1446 size &= PSEUDOVECTOR_SIZE_MASK;
1447 vec = XVECTOR (make_pure_vector ((EMACS_INT) size));
1448 for (i = 0; i < size; i++)
1449 vec->contents[i] = Fpurecopy (XVECTOR (obj)->contents[i]);
1450 if (COMPILEDP (obj))
1451 XSETCOMPILED (obj, vec);
1452 else
1453 XSETVECTOR (obj, vec);
1454 return obj;
1455 }
1456 else if (MARKERP (obj))
1457 error ("Attempt to copy a marker to pure storage");
1458 else
1459 return obj;
1460 }
1461 \f
1462 /* Recording what needs to be marked for gc. */
1463
1464 struct gcpro *gcprolist;
1465
1466 #define NSTATICS 768
1467
1468 Lisp_Object *staticvec[NSTATICS] = {0};
1469
1470 int staticidx = 0;
1471
1472 /* Put an entry in staticvec, pointing at the variable whose address is given */
1473
1474 void
1475 staticpro (varaddress)
1476 Lisp_Object *varaddress;
1477 {
1478 staticvec[staticidx++] = varaddress;
1479 if (staticidx >= NSTATICS)
1480 abort ();
1481 }
1482
1483 struct catchtag
1484 {
1485 Lisp_Object tag;
1486 Lisp_Object val;
1487 struct catchtag *next;
1488 /* jmp_buf jmp; /* We don't need this for GC purposes */
1489 };
1490
1491 struct backtrace
1492 {
1493 struct backtrace *next;
1494 Lisp_Object *function;
1495 Lisp_Object *args; /* Points to vector of args. */
1496 int nargs; /* length of vector */
1497 /* if nargs is UNEVALLED, args points to slot holding list of unevalled args */
1498 char evalargs;
1499 };
1500 \f
1501 /* Garbage collection! */
1502
1503 /* Temporarily prevent garbage collection. */
1504
1505 int
1506 inhibit_garbage_collection ()
1507 {
1508 int count = specpdl_ptr - specpdl;
1509 Lisp_Object number;
1510 int nbits = min (VALBITS, BITS_PER_INT);
1511
1512 XSETINT (number, ((EMACS_INT) 1 << (nbits - 1)) - 1);
1513
1514 specbind (Qgc_cons_threshold, number);
1515
1516 return count;
1517 }
1518
1519 DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
1520 "Reclaim storage for Lisp objects no longer needed.\n\
1521 Returns info on amount of space in use:\n\
1522 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)\n\
1523 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS\n\
1524 (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS))\n\
1525 Garbage collection happens automatically if you cons more than\n\
1526 `gc-cons-threshold' bytes of Lisp data since previous garbage collection.")
1527 ()
1528 {
1529 register struct gcpro *tail;
1530 register struct specbinding *bind;
1531 struct catchtag *catch;
1532 struct handler *handler;
1533 register struct backtrace *backlist;
1534 register Lisp_Object tem;
1535 char *omessage = echo_area_glyphs;
1536 int omessage_length = echo_area_glyphs_length;
1537 char stack_top_variable;
1538 register int i;
1539
1540 /* In case user calls debug_print during GC,
1541 don't let that cause a recursive GC. */
1542 consing_since_gc = 0;
1543
1544 /* Save a copy of the contents of the stack, for debugging. */
1545 #if MAX_SAVE_STACK > 0
1546 if (NILP (Vpurify_flag))
1547 {
1548 i = &stack_top_variable - stack_bottom;
1549 if (i < 0) i = -i;
1550 if (i < MAX_SAVE_STACK)
1551 {
1552 if (stack_copy == 0)
1553 stack_copy = (char *) xmalloc (stack_copy_size = i);
1554 else if (stack_copy_size < i)
1555 stack_copy = (char *) xrealloc (stack_copy, (stack_copy_size = i));
1556 if (stack_copy)
1557 {
1558 if ((EMACS_INT) (&stack_top_variable - stack_bottom) > 0)
1559 bcopy (stack_bottom, stack_copy, i);
1560 else
1561 bcopy (&stack_top_variable, stack_copy, i);
1562 }
1563 }
1564 }
1565 #endif /* MAX_SAVE_STACK > 0 */
1566
1567 if (garbage_collection_messages)
1568 message1_nolog ("Garbage collecting...");
1569
1570 /* Don't keep command history around forever. */
1571 if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0)
1572 {
1573 tem = Fnthcdr (Vhistory_length, Vcommand_history);
1574 if (CONSP (tem))
1575 XCONS (tem)->cdr = Qnil;
1576 }
1577
1578 /* Likewise for undo information. */
1579 {
1580 register struct buffer *nextb = all_buffers;
1581
1582 while (nextb)
1583 {
1584 /* If a buffer's undo list is Qt, that means that undo is
1585 turned off in that buffer. Calling truncate_undo_list on
1586 Qt tends to return NULL, which effectively turns undo back on.
1587 So don't call truncate_undo_list if undo_list is Qt. */
1588 if (! EQ (nextb->undo_list, Qt))
1589 nextb->undo_list
1590 = truncate_undo_list (nextb->undo_list, undo_limit,
1591 undo_strong_limit);
1592 nextb = nextb->next;
1593 }
1594 }
1595
1596 gc_in_progress = 1;
1597
1598 /* clear_marks (); */
1599
1600 /* In each "large string", set the MARKBIT of the size field.
1601 That enables mark_object to recognize them. */
1602 {
1603 register struct string_block *b;
1604 for (b = large_string_blocks; b; b = b->next)
1605 ((struct Lisp_String *)(&b->chars[0]))->size |= MARKBIT;
1606 }
1607
1608 /* Mark all the special slots that serve as the roots of accessibility.
1609
1610 Usually the special slots to mark are contained in particular structures.
1611 Then we know no slot is marked twice because the structures don't overlap.
1612 In some cases, the structures point to the slots to be marked.
1613 For these, we use MARKBIT to avoid double marking of the slot. */
1614
1615 for (i = 0; i < staticidx; i++)
1616 mark_object (staticvec[i]);
1617 for (tail = gcprolist; tail; tail = tail->next)
1618 for (i = 0; i < tail->nvars; i++)
1619 if (!XMARKBIT (tail->var[i]))
1620 {
1621 mark_object (&tail->var[i]);
1622 XMARK (tail->var[i]);
1623 }
1624 for (bind = specpdl; bind != specpdl_ptr; bind++)
1625 {
1626 mark_object (&bind->symbol);
1627 mark_object (&bind->old_value);
1628 }
1629 for (catch = catchlist; catch; catch = catch->next)
1630 {
1631 mark_object (&catch->tag);
1632 mark_object (&catch->val);
1633 }
1634 for (handler = handlerlist; handler; handler = handler->next)
1635 {
1636 mark_object (&handler->handler);
1637 mark_object (&handler->var);
1638 }
1639 for (backlist = backtrace_list; backlist; backlist = backlist->next)
1640 {
1641 if (!XMARKBIT (*backlist->function))
1642 {
1643 mark_object (backlist->function);
1644 XMARK (*backlist->function);
1645 }
1646 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
1647 i = 0;
1648 else
1649 i = backlist->nargs - 1;
1650 for (; i >= 0; i--)
1651 if (!XMARKBIT (backlist->args[i]))
1652 {
1653 mark_object (&backlist->args[i]);
1654 XMARK (backlist->args[i]);
1655 }
1656 }
1657 mark_kboards ();
1658
1659 gc_sweep ();
1660
1661 /* Clear the mark bits that we set in certain root slots. */
1662
1663 for (tail = gcprolist; tail; tail = tail->next)
1664 for (i = 0; i < tail->nvars; i++)
1665 XUNMARK (tail->var[i]);
1666 for (backlist = backtrace_list; backlist; backlist = backlist->next)
1667 {
1668 XUNMARK (*backlist->function);
1669 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
1670 i = 0;
1671 else
1672 i = backlist->nargs - 1;
1673 for (; i >= 0; i--)
1674 XUNMARK (backlist->args[i]);
1675 }
1676 XUNMARK (buffer_defaults.name);
1677 XUNMARK (buffer_local_symbols.name);
1678
1679 /* clear_marks (); */
1680 gc_in_progress = 0;
1681
1682 consing_since_gc = 0;
1683 if (gc_cons_threshold < 10000)
1684 gc_cons_threshold = 10000;
1685
1686 if (garbage_collection_messages)
1687 {
1688 if (omessage || minibuf_level > 0)
1689 message2_nolog (omessage, omessage_length);
1690 else
1691 message1_nolog ("Garbage collecting...done");
1692 }
1693
1694 return Fcons (Fcons (make_number (total_conses),
1695 make_number (total_free_conses)),
1696 Fcons (Fcons (make_number (total_symbols),
1697 make_number (total_free_symbols)),
1698 Fcons (Fcons (make_number (total_markers),
1699 make_number (total_free_markers)),
1700 Fcons (make_number (total_string_size),
1701 Fcons (make_number (total_vector_size),
1702 Fcons (Fcons
1703 #ifdef LISP_FLOAT_TYPE
1704 (make_number (total_floats),
1705 make_number (total_free_floats)),
1706 #else /* not LISP_FLOAT_TYPE */
1707 (make_number (0), make_number (0)),
1708 #endif /* not LISP_FLOAT_TYPE */
1709 Fcons (Fcons
1710 #ifdef USE_TEXT_PROPERTIES
1711 (make_number (total_intervals),
1712 make_number (total_free_intervals)),
1713 #else /* not USE_TEXT_PROPERTIES */
1714 (make_number (0), make_number (0)),
1715 #endif /* not USE_TEXT_PROPERTIES */
1716 Qnil)))))));
1717 }
1718 \f
1719 #if 0
1720 static void
1721 clear_marks ()
1722 {
1723 /* Clear marks on all conses */
1724 {
1725 register struct cons_block *cblk;
1726 register int lim = cons_block_index;
1727
1728 for (cblk = cons_block; cblk; cblk = cblk->next)
1729 {
1730 register int i;
1731 for (i = 0; i < lim; i++)
1732 XUNMARK (cblk->conses[i].car);
1733 lim = CONS_BLOCK_SIZE;
1734 }
1735 }
1736 /* Clear marks on all symbols */
1737 {
1738 register struct symbol_block *sblk;
1739 register int lim = symbol_block_index;
1740
1741 for (sblk = symbol_block; sblk; sblk = sblk->next)
1742 {
1743 register int i;
1744 for (i = 0; i < lim; i++)
1745 {
1746 XUNMARK (sblk->symbols[i].plist);
1747 }
1748 lim = SYMBOL_BLOCK_SIZE;
1749 }
1750 }
1751 /* Clear marks on all markers */
1752 {
1753 register struct marker_block *sblk;
1754 register int lim = marker_block_index;
1755
1756 for (sblk = marker_block; sblk; sblk = sblk->next)
1757 {
1758 register int i;
1759 for (i = 0; i < lim; i++)
1760 if (sblk->markers[i].u_marker.type == Lisp_Misc_Marker)
1761 XUNMARK (sblk->markers[i].u_marker.chain);
1762 lim = MARKER_BLOCK_SIZE;
1763 }
1764 }
1765 /* Clear mark bits on all buffers */
1766 {
1767 register struct buffer *nextb = all_buffers;
1768
1769 while (nextb)
1770 {
1771 XUNMARK (nextb->name);
1772 nextb = nextb->next;
1773 }
1774 }
1775 }
1776 #endif
1777 \f
1778 /* Mark reference to a Lisp_Object.
1779 If the object referred to has not been seen yet, recursively mark
1780 all the references contained in it.
1781
1782 If the object referenced is a short string, the referencing slot
1783 is threaded into a chain of such slots, pointed to from
1784 the `size' field of the string. The actual string size
1785 lives in the last slot in the chain. We recognize the end
1786 because it is < (unsigned) STRING_BLOCK_SIZE. */
1787
1788 #define LAST_MARKED_SIZE 500
1789 Lisp_Object *last_marked[LAST_MARKED_SIZE];
1790 int last_marked_index;
1791
1792 static void
1793 mark_object (argptr)
1794 Lisp_Object *argptr;
1795 {
1796 Lisp_Object *objptr = argptr;
1797 register Lisp_Object obj;
1798
1799 loop:
1800 obj = *objptr;
1801 loop2:
1802 XUNMARK (obj);
1803
1804 if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
1805 && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
1806 return;
1807
1808 last_marked[last_marked_index++] = objptr;
1809 if (last_marked_index == LAST_MARKED_SIZE)
1810 last_marked_index = 0;
1811
1812 switch (SWITCH_ENUM_CAST (XGCTYPE (obj)))
1813 {
1814 case Lisp_String:
1815 {
1816 register struct Lisp_String *ptr = XSTRING (obj);
1817
1818 MARK_INTERVAL_TREE (ptr->intervals);
1819 if (ptr->size & MARKBIT)
1820 /* A large string. Just set ARRAY_MARK_FLAG. */
1821 ptr->size |= ARRAY_MARK_FLAG;
1822 else
1823 {
1824 /* A small string. Put this reference
1825 into the chain of references to it.
1826 If the address includes MARKBIT, put that bit elsewhere
1827 when we store OBJPTR into the size field. */
1828
1829 if (XMARKBIT (*objptr))
1830 {
1831 XSETFASTINT (*objptr, ptr->size);
1832 XMARK (*objptr);
1833 }
1834 else
1835 XSETFASTINT (*objptr, ptr->size);
1836
1837 if ((EMACS_INT) objptr & DONT_COPY_FLAG)
1838 abort ();
1839 ptr->size = (EMACS_INT) objptr;
1840 if (ptr->size & MARKBIT)
1841 ptr->size ^= MARKBIT | DONT_COPY_FLAG;
1842 }
1843 }
1844 break;
1845
1846 case Lisp_Vectorlike:
1847 if (GC_BUFFERP (obj))
1848 {
1849 if (!XMARKBIT (XBUFFER (obj)->name))
1850 mark_buffer (obj);
1851 }
1852 else if (GC_SUBRP (obj))
1853 break;
1854 else if (GC_COMPILEDP (obj))
1855 /* We could treat this just like a vector, but it is better
1856 to save the COMPILED_CONSTANTS element for last and avoid recursion
1857 there. */
1858 {
1859 register struct Lisp_Vector *ptr = XVECTOR (obj);
1860 register EMACS_INT size = ptr->size;
1861 /* See comment above under Lisp_Vector. */
1862 struct Lisp_Vector *volatile ptr1 = ptr;
1863 register int i;
1864
1865 if (size & ARRAY_MARK_FLAG)
1866 break; /* Already marked */
1867 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
1868 size &= PSEUDOVECTOR_SIZE_MASK;
1869 for (i = 0; i < size; i++) /* and then mark its elements */
1870 {
1871 if (i != COMPILED_CONSTANTS)
1872 mark_object (&ptr1->contents[i]);
1873 }
1874 /* This cast should be unnecessary, but some Mips compiler complains
1875 (MIPS-ABI + SysVR4, DC/OSx, etc). */
1876 objptr = (Lisp_Object *) &ptr1->contents[COMPILED_CONSTANTS];
1877 goto loop;
1878 }
1879 else if (GC_FRAMEP (obj))
1880 {
1881 /* See comment above under Lisp_Vector for why this is volatile. */
1882 register struct frame *volatile ptr = XFRAME (obj);
1883 register EMACS_INT size = ptr->size;
1884
1885 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
1886 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
1887
1888 mark_object (&ptr->name);
1889 mark_object (&ptr->icon_name);
1890 mark_object (&ptr->title);
1891 mark_object (&ptr->focus_frame);
1892 mark_object (&ptr->selected_window);
1893 mark_object (&ptr->minibuffer_window);
1894 mark_object (&ptr->param_alist);
1895 mark_object (&ptr->scroll_bars);
1896 mark_object (&ptr->condemned_scroll_bars);
1897 mark_object (&ptr->menu_bar_items);
1898 mark_object (&ptr->face_alist);
1899 mark_object (&ptr->menu_bar_vector);
1900 mark_object (&ptr->buffer_predicate);
1901 mark_object (&ptr->buffer_list);
1902 }
1903 else if (GC_BOOL_VECTOR_P (obj))
1904 {
1905 register struct Lisp_Vector *ptr = XVECTOR (obj);
1906
1907 if (ptr->size & ARRAY_MARK_FLAG)
1908 break; /* Already marked */
1909 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
1910 }
1911 else
1912 {
1913 register struct Lisp_Vector *ptr = XVECTOR (obj);
1914 register EMACS_INT size = ptr->size;
1915 /* The reason we use ptr1 is to avoid an apparent hardware bug
1916 that happens occasionally on the FSF's HP 300s.
1917 The bug is that a2 gets clobbered by recursive calls to mark_object.
1918 The clobberage seems to happen during function entry,
1919 perhaps in the moveml instruction.
1920 Yes, this is a crock, but we have to do it. */
1921 struct Lisp_Vector *volatile ptr1 = ptr;
1922 register int i;
1923
1924 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
1925 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
1926 if (size & PSEUDOVECTOR_FLAG)
1927 size &= PSEUDOVECTOR_SIZE_MASK;
1928 for (i = 0; i < size; i++) /* and then mark its elements */
1929 mark_object (&ptr1->contents[i]);
1930 }
1931 break;
1932
1933 case Lisp_Symbol:
1934 {
1935 /* See comment above under Lisp_Vector for why this is volatile. */
1936 register struct Lisp_Symbol *volatile ptr = XSYMBOL (obj);
1937 struct Lisp_Symbol *ptrx;
1938
1939 if (XMARKBIT (ptr->plist)) break;
1940 XMARK (ptr->plist);
1941 mark_object ((Lisp_Object *) &ptr->value);
1942 mark_object (&ptr->function);
1943 mark_object (&ptr->plist);
1944 XSETTYPE (*(Lisp_Object *) &ptr->name, Lisp_String);
1945 mark_object (&ptr->name);
1946 ptr = ptr->next;
1947 if (ptr)
1948 {
1949 /* For the benefit of the last_marked log. */
1950 objptr = (Lisp_Object *)&XSYMBOL (obj)->next;
1951 ptrx = ptr; /* Use of ptrx avoids compiler bug on Sun */
1952 XSETSYMBOL (obj, ptrx);
1953 /* We can't goto loop here because *objptr doesn't contain an
1954 actual Lisp_Object with valid datatype field. */
1955 goto loop2;
1956 }
1957 }
1958 break;
1959
1960 case Lisp_Misc:
1961 switch (XMISCTYPE (obj))
1962 {
1963 case Lisp_Misc_Marker:
1964 XMARK (XMARKER (obj)->chain);
1965 /* DO NOT mark thru the marker's chain.
1966 The buffer's markers chain does not preserve markers from gc;
1967 instead, markers are removed from the chain when freed by gc. */
1968 break;
1969
1970 case Lisp_Misc_Buffer_Local_Value:
1971 case Lisp_Misc_Some_Buffer_Local_Value:
1972 {
1973 register struct Lisp_Buffer_Local_Value *ptr
1974 = XBUFFER_LOCAL_VALUE (obj);
1975 if (XMARKBIT (ptr->car)) break;
1976 XMARK (ptr->car);
1977 /* If the cdr is nil, avoid recursion for the car. */
1978 if (EQ (ptr->cdr, Qnil))
1979 {
1980 objptr = &ptr->car;
1981 goto loop;
1982 }
1983 mark_object (&ptr->car);
1984 /* See comment above under Lisp_Vector for why not use ptr here. */
1985 objptr = &XBUFFER_LOCAL_VALUE (obj)->cdr;
1986 goto loop;
1987 }
1988
1989 case Lisp_Misc_Intfwd:
1990 case Lisp_Misc_Boolfwd:
1991 case Lisp_Misc_Objfwd:
1992 case Lisp_Misc_Buffer_Objfwd:
1993 case Lisp_Misc_Kboard_Objfwd:
1994 /* Don't bother with Lisp_Buffer_Objfwd,
1995 since all markable slots in current buffer marked anyway. */
1996 /* Don't need to do Lisp_Objfwd, since the places they point
1997 are protected with staticpro. */
1998 break;
1999
2000 case Lisp_Misc_Overlay:
2001 {
2002 struct Lisp_Overlay *ptr = XOVERLAY (obj);
2003 if (!XMARKBIT (ptr->plist))
2004 {
2005 XMARK (ptr->plist);
2006 mark_object (&ptr->start);
2007 mark_object (&ptr->end);
2008 objptr = &ptr->plist;
2009 goto loop;
2010 }
2011 }
2012 break;
2013
2014 default:
2015 abort ();
2016 }
2017 break;
2018
2019 case Lisp_Cons:
2020 {
2021 register struct Lisp_Cons *ptr = XCONS (obj);
2022 if (XMARKBIT (ptr->car)) break;
2023 XMARK (ptr->car);
2024 /* If the cdr is nil, avoid recursion for the car. */
2025 if (EQ (ptr->cdr, Qnil))
2026 {
2027 objptr = &ptr->car;
2028 goto loop;
2029 }
2030 mark_object (&ptr->car);
2031 /* See comment above under Lisp_Vector for why not use ptr here. */
2032 objptr = &XCONS (obj)->cdr;
2033 goto loop;
2034 }
2035
2036 #ifdef LISP_FLOAT_TYPE
2037 case Lisp_Float:
2038 XMARK (XFLOAT (obj)->type);
2039 break;
2040 #endif /* LISP_FLOAT_TYPE */
2041
2042 case Lisp_Int:
2043 break;
2044
2045 default:
2046 abort ();
2047 }
2048 }
2049
2050 /* Mark the pointers in a buffer structure. */
2051
2052 static void
2053 mark_buffer (buf)
2054 Lisp_Object buf;
2055 {
2056 register struct buffer *buffer = XBUFFER (buf);
2057 register Lisp_Object *ptr;
2058 Lisp_Object base_buffer;
2059
2060 /* This is the buffer's markbit */
2061 mark_object (&buffer->name);
2062 XMARK (buffer->name);
2063
2064 MARK_INTERVAL_TREE (BUF_INTERVALS (buffer));
2065
2066 #if 0
2067 mark_object (buffer->syntax_table);
2068
2069 /* Mark the various string-pointers in the buffer object.
2070 Since the strings may be relocated, we must mark them
2071 in their actual slots. So gc_sweep must convert each slot
2072 back to an ordinary C pointer. */
2073 XSETSTRING (*(Lisp_Object *)&buffer->upcase_table, buffer->upcase_table);
2074 mark_object ((Lisp_Object *)&buffer->upcase_table);
2075 XSETSTRING (*(Lisp_Object *)&buffer->downcase_table, buffer->downcase_table);
2076 mark_object ((Lisp_Object *)&buffer->downcase_table);
2077
2078 XSETSTRING (*(Lisp_Object *)&buffer->sort_table, buffer->sort_table);
2079 mark_object ((Lisp_Object *)&buffer->sort_table);
2080 XSETSTRING (*(Lisp_Object *)&buffer->folding_sort_table, buffer->folding_sort_table);
2081 mark_object ((Lisp_Object *)&buffer->folding_sort_table);
2082 #endif
2083
2084 for (ptr = &buffer->name + 1;
2085 (char *)ptr < (char *)buffer + sizeof (struct buffer);
2086 ptr++)
2087 mark_object (ptr);
2088
2089 /* If this is an indirect buffer, mark its base buffer. */
2090 if (buffer->base_buffer && !XMARKBIT (buffer->base_buffer->name))
2091 {
2092 XSETBUFFER (base_buffer, buffer->base_buffer);
2093 mark_buffer (base_buffer);
2094 }
2095 }
2096
2097
2098 /* Mark the pointers in the kboard objects. */
2099
2100 static void
2101 mark_kboards ()
2102 {
2103 KBOARD *kb;
2104 Lisp_Object *p;
2105 for (kb = all_kboards; kb; kb = kb->next_kboard)
2106 {
2107 if (kb->kbd_macro_buffer)
2108 for (p = kb->kbd_macro_buffer; p < kb->kbd_macro_ptr; p++)
2109 mark_object (p);
2110 mark_object (&kb->Vprefix_arg);
2111 mark_object (&kb->kbd_queue);
2112 mark_object (&kb->Vlast_kbd_macro);
2113 mark_object (&kb->Vsystem_key_alist);
2114 mark_object (&kb->system_key_syms);
2115 }
2116 }
2117 \f
2118 /* Sweep: find all structures not marked, and free them. */
2119
2120 static void
2121 gc_sweep ()
2122 {
2123 total_string_size = 0;
2124 compact_strings ();
2125
2126 /* Put all unmarked conses on free list */
2127 {
2128 register struct cons_block *cblk;
2129 struct cons_block **cprev = &cons_block;
2130 register int lim = cons_block_index;
2131 register int num_free = 0, num_used = 0;
2132
2133 cons_free_list = 0;
2134
2135 for (cblk = cons_block; cblk; cblk = *cprev)
2136 {
2137 register int i;
2138 int this_free = 0;
2139 for (i = 0; i < lim; i++)
2140 if (!XMARKBIT (cblk->conses[i].car))
2141 {
2142 num_free++;
2143 this_free++;
2144 *(struct Lisp_Cons **)&cblk->conses[i].cdr = cons_free_list;
2145 cons_free_list = &cblk->conses[i];
2146 }
2147 else
2148 {
2149 num_used++;
2150 XUNMARK (cblk->conses[i].car);
2151 }
2152 lim = CONS_BLOCK_SIZE;
2153 /* If this block contains only free conses and we have already
2154 seen more than two blocks worth of free conses then deallocate
2155 this block. */
2156 if (this_free == CONS_BLOCK_SIZE && num_free > 2*CONS_BLOCK_SIZE)
2157 {
2158 num_free -= CONS_BLOCK_SIZE;
2159 *cprev = cblk->next;
2160 /* Unhook from the free list. */
2161 cons_free_list = *(struct Lisp_Cons **) &cblk->conses[0].cdr;
2162 xfree (cblk);
2163 }
2164 else
2165 cprev = &cblk->next;
2166 }
2167 total_conses = num_used;
2168 total_free_conses = num_free;
2169 }
2170
2171 #ifdef LISP_FLOAT_TYPE
2172 /* Put all unmarked floats on free list */
2173 {
2174 register struct float_block *fblk;
2175 struct float_block **fprev = &float_block;
2176 register int lim = float_block_index;
2177 register int num_free = 0, num_used = 0;
2178
2179 float_free_list = 0;
2180
2181 for (fblk = float_block; fblk; fblk = *fprev)
2182 {
2183 register int i;
2184 int this_free = 0;
2185 for (i = 0; i < lim; i++)
2186 if (!XMARKBIT (fblk->floats[i].type))
2187 {
2188 num_free++;
2189 this_free++;
2190 *(struct Lisp_Float **)&fblk->floats[i].data = float_free_list;
2191 float_free_list = &fblk->floats[i];
2192 }
2193 else
2194 {
2195 num_used++;
2196 XUNMARK (fblk->floats[i].type);
2197 }
2198 lim = FLOAT_BLOCK_SIZE;
2199 /* If this block contains only free floats and we have already
2200 seen more than two blocks worth of free floats then deallocate
2201 this block. */
2202 if (this_free == FLOAT_BLOCK_SIZE && num_free > 2*FLOAT_BLOCK_SIZE)
2203 {
2204 num_free -= FLOAT_BLOCK_SIZE;
2205 *fprev = fblk->next;
2206 /* Unhook from the free list. */
2207 float_free_list = *(struct Lisp_Float **) &fblk->floats[0].data;
2208 xfree (fblk);
2209 }
2210 else
2211 fprev = &fblk->next;
2212 }
2213 total_floats = num_used;
2214 total_free_floats = num_free;
2215 }
2216 #endif /* LISP_FLOAT_TYPE */
2217
2218 #ifdef USE_TEXT_PROPERTIES
2219 /* Put all unmarked intervals on free list */
2220 {
2221 register struct interval_block *iblk;
2222 struct interval_block **iprev = &interval_block;
2223 register int lim = interval_block_index;
2224 register int num_free = 0, num_used = 0;
2225
2226 interval_free_list = 0;
2227
2228 for (iblk = interval_block; iblk; iblk = *iprev)
2229 {
2230 register int i;
2231 int this_free = 0;
2232
2233 for (i = 0; i < lim; i++)
2234 {
2235 if (! XMARKBIT (iblk->intervals[i].plist))
2236 {
2237 iblk->intervals[i].parent = interval_free_list;
2238 interval_free_list = &iblk->intervals[i];
2239 num_free++;
2240 this_free++;
2241 }
2242 else
2243 {
2244 num_used++;
2245 XUNMARK (iblk->intervals[i].plist);
2246 }
2247 }
2248 lim = INTERVAL_BLOCK_SIZE;
2249 /* If this block contains only free intervals and we have already
2250 seen more than two blocks worth of free intervals then
2251 deallocate this block. */
2252 if (this_free == INTERVAL_BLOCK_SIZE
2253 && num_free > 2*INTERVAL_BLOCK_SIZE)
2254 {
2255 num_free -= INTERVAL_BLOCK_SIZE;
2256 *iprev = iblk->next;
2257 /* Unhook from the free list. */
2258 interval_free_list = iblk->intervals[0].parent;
2259 xfree (iblk);
2260 }
2261 else
2262 iprev = &iblk->next;
2263 }
2264 total_intervals = num_used;
2265 total_free_intervals = num_free;
2266 }
2267 #endif /* USE_TEXT_PROPERTIES */
2268
2269 /* Put all unmarked symbols on free list */
2270 {
2271 register struct symbol_block *sblk;
2272 struct symbol_block **sprev = &symbol_block;
2273 register int lim = symbol_block_index;
2274 register int num_free = 0, num_used = 0;
2275
2276 symbol_free_list = 0;
2277
2278 for (sblk = symbol_block; sblk; sblk = *sprev)
2279 {
2280 register int i;
2281 int this_free = 0;
2282 for (i = 0; i < lim; i++)
2283 if (!XMARKBIT (sblk->symbols[i].plist))
2284 {
2285 *(struct Lisp_Symbol **)&sblk->symbols[i].value = symbol_free_list;
2286 symbol_free_list = &sblk->symbols[i];
2287 num_free++;
2288 this_free++;
2289 }
2290 else
2291 {
2292 num_used++;
2293 sblk->symbols[i].name
2294 = XSTRING (*(Lisp_Object *) &sblk->symbols[i].name);
2295 XUNMARK (sblk->symbols[i].plist);
2296 }
2297 lim = SYMBOL_BLOCK_SIZE;
2298 /* If this block contains only free symbols and we have already
2299 seen more than two blocks worth of free symbols then deallocate
2300 this block. */
2301 if (this_free == SYMBOL_BLOCK_SIZE && num_free > 2*SYMBOL_BLOCK_SIZE)
2302 {
2303 num_free -= SYMBOL_BLOCK_SIZE;
2304 *sprev = sblk->next;
2305 /* Unhook from the free list. */
2306 symbol_free_list = *(struct Lisp_Symbol **)&sblk->symbols[0].value;
2307 xfree (sblk);
2308 }
2309 else
2310 sprev = &sblk->next;
2311 }
2312 total_symbols = num_used;
2313 total_free_symbols = num_free;
2314 }
2315
2316 #ifndef standalone
2317 /* Put all unmarked markers on free list.
2318 Unchain each one first from the buffer it points into,
2319 but only if it's a real marker. */
2320 {
2321 register struct marker_block *mblk;
2322 struct marker_block **mprev = &marker_block;
2323 register int lim = marker_block_index;
2324 register int num_free = 0, num_used = 0;
2325
2326 marker_free_list = 0;
2327
2328 for (mblk = marker_block; mblk; mblk = *mprev)
2329 {
2330 register int i;
2331 int this_free = 0;
2332 EMACS_INT already_free = -1;
2333
2334 for (i = 0; i < lim; i++)
2335 {
2336 Lisp_Object *markword;
2337 switch (mblk->markers[i].u_marker.type)
2338 {
2339 case Lisp_Misc_Marker:
2340 markword = &mblk->markers[i].u_marker.chain;
2341 break;
2342 case Lisp_Misc_Buffer_Local_Value:
2343 case Lisp_Misc_Some_Buffer_Local_Value:
2344 markword = &mblk->markers[i].u_buffer_local_value.car;
2345 break;
2346 case Lisp_Misc_Overlay:
2347 markword = &mblk->markers[i].u_overlay.plist;
2348 break;
2349 case Lisp_Misc_Free:
2350 /* If the object was already free, keep it
2351 on the free list. */
2352 markword = (Lisp_Object *) &already_free;
2353 break;
2354 default:
2355 markword = 0;
2356 break;
2357 }
2358 if (markword && !XMARKBIT (*markword))
2359 {
2360 Lisp_Object tem;
2361 if (mblk->markers[i].u_marker.type == Lisp_Misc_Marker)
2362 {
2363 /* tem1 avoids Sun compiler bug */
2364 struct Lisp_Marker *tem1 = &mblk->markers[i].u_marker;
2365 XSETMARKER (tem, tem1);
2366 unchain_marker (tem);
2367 }
2368 /* Set the type of the freed object to Lisp_Misc_Free.
2369 We could leave the type alone, since nobody checks it,
2370 but this might catch bugs faster. */
2371 mblk->markers[i].u_marker.type = Lisp_Misc_Free;
2372 mblk->markers[i].u_free.chain = marker_free_list;
2373 marker_free_list = &mblk->markers[i];
2374 num_free++;
2375 this_free++;
2376 }
2377 else
2378 {
2379 num_used++;
2380 if (markword)
2381 XUNMARK (*markword);
2382 }
2383 }
2384 lim = MARKER_BLOCK_SIZE;
2385 /* If this block contains only free markers and we have already
2386 seen more than two blocks worth of free markers then deallocate
2387 this block. */
2388 if (this_free == MARKER_BLOCK_SIZE && num_free > 2*MARKER_BLOCK_SIZE)
2389 {
2390 num_free -= MARKER_BLOCK_SIZE;
2391 *mprev = mblk->next;
2392 /* Unhook from the free list. */
2393 marker_free_list = mblk->markers[0].u_free.chain;
2394 xfree (mblk);
2395 }
2396 else
2397 mprev = &mblk->next;
2398 }
2399
2400 total_markers = num_used;
2401 total_free_markers = num_free;
2402 }
2403
2404 /* Free all unmarked buffers */
2405 {
2406 register struct buffer *buffer = all_buffers, *prev = 0, *next;
2407
2408 while (buffer)
2409 if (!XMARKBIT (buffer->name))
2410 {
2411 if (prev)
2412 prev->next = buffer->next;
2413 else
2414 all_buffers = buffer->next;
2415 next = buffer->next;
2416 xfree (buffer);
2417 buffer = next;
2418 }
2419 else
2420 {
2421 XUNMARK (buffer->name);
2422 UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer));
2423
2424 #if 0
2425 /* Each `struct Lisp_String *' was turned into a Lisp_Object
2426 for purposes of marking and relocation.
2427 Turn them back into C pointers now. */
2428 buffer->upcase_table
2429 = XSTRING (*(Lisp_Object *)&buffer->upcase_table);
2430 buffer->downcase_table
2431 = XSTRING (*(Lisp_Object *)&buffer->downcase_table);
2432 buffer->sort_table
2433 = XSTRING (*(Lisp_Object *)&buffer->sort_table);
2434 buffer->folding_sort_table
2435 = XSTRING (*(Lisp_Object *)&buffer->folding_sort_table);
2436 #endif
2437
2438 prev = buffer, buffer = buffer->next;
2439 }
2440 }
2441
2442 #endif /* standalone */
2443
2444 /* Free all unmarked vectors */
2445 {
2446 register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next;
2447 total_vector_size = 0;
2448
2449 while (vector)
2450 if (!(vector->size & ARRAY_MARK_FLAG))
2451 {
2452 if (prev)
2453 prev->next = vector->next;
2454 else
2455 all_vectors = vector->next;
2456 next = vector->next;
2457 xfree (vector);
2458 vector = next;
2459 }
2460 else
2461 {
2462 vector->size &= ~ARRAY_MARK_FLAG;
2463 if (vector->size & PSEUDOVECTOR_FLAG)
2464 total_vector_size += (PSEUDOVECTOR_SIZE_MASK & vector->size);
2465 else
2466 total_vector_size += vector->size;
2467 prev = vector, vector = vector->next;
2468 }
2469 }
2470
2471 /* Free all "large strings" not marked with ARRAY_MARK_FLAG. */
2472 {
2473 register struct string_block *sb = large_string_blocks, *prev = 0, *next;
2474 struct Lisp_String *s;
2475
2476 while (sb)
2477 {
2478 s = (struct Lisp_String *) &sb->chars[0];
2479 if (s->size & ARRAY_MARK_FLAG)
2480 {
2481 ((struct Lisp_String *)(&sb->chars[0]))->size
2482 &= ~ARRAY_MARK_FLAG & ~MARKBIT;
2483 UNMARK_BALANCE_INTERVALS (s->intervals);
2484 total_string_size += ((struct Lisp_String *)(&sb->chars[0]))->size;
2485 prev = sb, sb = sb->next;
2486 }
2487 else
2488 {
2489 if (prev)
2490 prev->next = sb->next;
2491 else
2492 large_string_blocks = sb->next;
2493 next = sb->next;
2494 xfree (sb);
2495 sb = next;
2496 }
2497 }
2498 }
2499 }
2500 \f
2501 /* Compactify strings, relocate references, and free empty string blocks. */
2502
2503 static void
2504 compact_strings ()
2505 {
2506 /* String block of old strings we are scanning. */
2507 register struct string_block *from_sb;
2508 /* A preceding string block (or maybe the same one)
2509 where we are copying the still-live strings to. */
2510 register struct string_block *to_sb;
2511 int pos;
2512 int to_pos;
2513
2514 to_sb = first_string_block;
2515 to_pos = 0;
2516
2517 /* Scan each existing string block sequentially, string by string. */
2518 for (from_sb = first_string_block; from_sb; from_sb = from_sb->next)
2519 {
2520 pos = 0;
2521 /* POS is the index of the next string in the block. */
2522 while (pos < from_sb->pos)
2523 {
2524 register struct Lisp_String *nextstr
2525 = (struct Lisp_String *) &from_sb->chars[pos];
2526
2527 register struct Lisp_String *newaddr;
2528 register EMACS_INT size = nextstr->size;
2529
2530 /* NEXTSTR is the old address of the next string.
2531 Just skip it if it isn't marked. */
2532 if (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE)
2533 {
2534 /* It is marked, so its size field is really a chain of refs.
2535 Find the end of the chain, where the actual size lives. */
2536 while (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE)
2537 {
2538 if (size & DONT_COPY_FLAG)
2539 size ^= MARKBIT | DONT_COPY_FLAG;
2540 size = *(EMACS_INT *)size & ~MARKBIT;
2541 }
2542
2543 total_string_size += size;
2544
2545 /* If it won't fit in TO_SB, close it out,
2546 and move to the next sb. Keep doing so until
2547 TO_SB reaches a large enough, empty enough string block.
2548 We know that TO_SB cannot advance past FROM_SB here
2549 since FROM_SB is large enough to contain this string.
2550 Any string blocks skipped here
2551 will be patched out and freed later. */
2552 while (to_pos + STRING_FULLSIZE (size)
2553 > max (to_sb->pos, STRING_BLOCK_SIZE))
2554 {
2555 to_sb->pos = to_pos;
2556 to_sb = to_sb->next;
2557 to_pos = 0;
2558 }
2559 /* Compute new address of this string
2560 and update TO_POS for the space being used. */
2561 newaddr = (struct Lisp_String *) &to_sb->chars[to_pos];
2562 to_pos += STRING_FULLSIZE (size);
2563
2564 /* Copy the string itself to the new place. */
2565 if (nextstr != newaddr)
2566 bcopy (nextstr, newaddr, size + 1 + sizeof (EMACS_INT)
2567 + INTERVAL_PTR_SIZE);
2568
2569 /* Go through NEXTSTR's chain of references
2570 and make each slot in the chain point to
2571 the new address of this string. */
2572 size = newaddr->size;
2573 while (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE)
2574 {
2575 register Lisp_Object *objptr;
2576 if (size & DONT_COPY_FLAG)
2577 size ^= MARKBIT | DONT_COPY_FLAG;
2578 objptr = (Lisp_Object *)size;
2579
2580 size = XFASTINT (*objptr) & ~MARKBIT;
2581 if (XMARKBIT (*objptr))
2582 {
2583 XSETSTRING (*objptr, newaddr);
2584 XMARK (*objptr);
2585 }
2586 else
2587 XSETSTRING (*objptr, newaddr);
2588 }
2589 /* Store the actual size in the size field. */
2590 newaddr->size = size;
2591
2592 #ifdef USE_TEXT_PROPERTIES
2593 /* Now that the string has been relocated, rebalance its
2594 interval tree, and update the tree's parent pointer. */
2595 if (! NULL_INTERVAL_P (newaddr->intervals))
2596 {
2597 UNMARK_BALANCE_INTERVALS (newaddr->intervals);
2598 XSETSTRING (* (Lisp_Object *) &newaddr->intervals->parent,
2599 newaddr);
2600 }
2601 #endif /* USE_TEXT_PROPERTIES */
2602 }
2603 pos += STRING_FULLSIZE (size);
2604 }
2605 }
2606
2607 /* Close out the last string block still used and free any that follow. */
2608 to_sb->pos = to_pos;
2609 current_string_block = to_sb;
2610
2611 from_sb = to_sb->next;
2612 to_sb->next = 0;
2613 while (from_sb)
2614 {
2615 to_sb = from_sb->next;
2616 xfree (from_sb);
2617 from_sb = to_sb;
2618 }
2619
2620 /* Free any empty string blocks further back in the chain.
2621 This loop will never free first_string_block, but it is very
2622 unlikely that that one will become empty, so why bother checking? */
2623
2624 from_sb = first_string_block;
2625 while (to_sb = from_sb->next)
2626 {
2627 if (to_sb->pos == 0)
2628 {
2629 if (from_sb->next = to_sb->next)
2630 from_sb->next->prev = from_sb;
2631 xfree (to_sb);
2632 }
2633 else
2634 from_sb = to_sb;
2635 }
2636 }
2637 \f
2638 /* Debugging aids. */
2639
2640 DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0,
2641 "Return the address of the last byte Emacs has allocated, divided by 1024.\n\
2642 This may be helpful in debugging Emacs's memory usage.\n\
2643 We divide the value by 1024 to make sure it fits in a Lisp integer.")
2644 ()
2645 {
2646 Lisp_Object end;
2647
2648 XSETINT (end, (EMACS_INT) sbrk (0) / 1024);
2649
2650 return end;
2651 }
2652
2653 DEFUN ("memory-use-counts", Fmemory_use_counts, Smemory_use_counts, 0, 0, 0,
2654 "Return a list of counters that measure how much consing there has been.\n\
2655 Each of these counters increments for a certain kind of object.\n\
2656 The counters wrap around from the largest positive integer to zero.\n\
2657 Garbage collection does not decrease them.\n\
2658 The elements of the value are as follows:\n\
2659 (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS)\n\
2660 All are in units of 1 = one object consed\n\
2661 except for VECTOR-CELLS and STRING-CHARS, which count the total length of\n\
2662 objects consed.\n\
2663 MISCS include overlays, markers, and some internal types.\n\
2664 Frames, windows, buffers, and subprocesses count as vectors\n\
2665 (but the contents of a buffer's text do not count here).")
2666 ()
2667 {
2668 Lisp_Object lisp_cons_cells_consed;
2669 Lisp_Object lisp_floats_consed;
2670 Lisp_Object lisp_vector_cells_consed;
2671 Lisp_Object lisp_symbols_consed;
2672 Lisp_Object lisp_string_chars_consed;
2673 Lisp_Object lisp_misc_objects_consed;
2674 Lisp_Object lisp_intervals_consed;
2675
2676 XSETINT (lisp_cons_cells_consed,
2677 cons_cells_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2678 XSETINT (lisp_floats_consed,
2679 floats_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2680 XSETINT (lisp_vector_cells_consed,
2681 vector_cells_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2682 XSETINT (lisp_symbols_consed,
2683 symbols_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2684 XSETINT (lisp_string_chars_consed,
2685 string_chars_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2686 XSETINT (lisp_misc_objects_consed,
2687 misc_objects_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2688 XSETINT (lisp_intervals_consed,
2689 intervals_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2690
2691 return Fcons (lisp_cons_cells_consed,
2692 Fcons (lisp_floats_consed,
2693 Fcons (lisp_vector_cells_consed,
2694 Fcons (lisp_symbols_consed,
2695 Fcons (lisp_string_chars_consed,
2696 Fcons (lisp_misc_objects_consed,
2697 Fcons (lisp_intervals_consed,
2698 Qnil)))))));
2699 }
2700 \f
2701 /* Initialization */
2702
2703 init_alloc_once ()
2704 {
2705 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
2706 pureptr = 0;
2707 #ifdef HAVE_SHM
2708 pure_size = PURESIZE;
2709 #endif
2710 all_vectors = 0;
2711 ignore_warnings = 1;
2712 #ifdef DOUG_LEA_MALLOC
2713 mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */
2714 mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */
2715 mallopt (M_MMAP_MAX, 64); /* max. number of mmap'ed areas */
2716 #endif
2717 init_strings ();
2718 init_cons ();
2719 init_symbol ();
2720 init_marker ();
2721 #ifdef LISP_FLOAT_TYPE
2722 init_float ();
2723 #endif /* LISP_FLOAT_TYPE */
2724 INIT_INTERVALS;
2725
2726 #ifdef REL_ALLOC
2727 malloc_hysteresis = 32;
2728 #else
2729 malloc_hysteresis = 0;
2730 #endif
2731
2732 spare_memory = (char *) malloc (SPARE_MEMORY);
2733
2734 ignore_warnings = 0;
2735 gcprolist = 0;
2736 staticidx = 0;
2737 consing_since_gc = 0;
2738 gc_cons_threshold = 100000 * sizeof (Lisp_Object);
2739 #ifdef VIRT_ADDR_VARIES
2740 malloc_sbrk_unused = 1<<22; /* A large number */
2741 malloc_sbrk_used = 100000; /* as reasonable as any number */
2742 #endif /* VIRT_ADDR_VARIES */
2743 }
2744
2745 init_alloc ()
2746 {
2747 gcprolist = 0;
2748 }
2749
2750 void
2751 syms_of_alloc ()
2752 {
2753 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold,
2754 "*Number of bytes of consing between garbage collections.\n\
2755 Garbage collection can happen automatically once this many bytes have been\n\
2756 allocated since the last garbage collection. All data types count.\n\n\
2757 Garbage collection happens automatically only when `eval' is called.\n\n\
2758 By binding this temporarily to a large number, you can effectively\n\
2759 prevent garbage collection during a part of the program.");
2760
2761 DEFVAR_INT ("pure-bytes-used", &pureptr,
2762 "Number of bytes of sharable Lisp data allocated so far.");
2763
2764 DEFVAR_INT ("cons-cells-consed", &cons_cells_consed,
2765 "Number of cons cells that have been consed so far.");
2766
2767 DEFVAR_INT ("floats-consed", &floats_consed,
2768 "Number of floats that have been consed so far.");
2769
2770 DEFVAR_INT ("vector-cells-consed", &vector_cells_consed,
2771 "Number of vector cells that have been consed so far.");
2772
2773 DEFVAR_INT ("symbols-consed", &symbols_consed,
2774 "Number of symbols that have been consed so far.");
2775
2776 DEFVAR_INT ("string-chars-consed", &string_chars_consed,
2777 "Number of string characters that have been consed so far.");
2778
2779 DEFVAR_INT ("misc-objects-consed", &misc_objects_consed,
2780 "Number of miscellaneous objects that have been consed so far.");
2781
2782 DEFVAR_INT ("intervals-consed", &intervals_consed,
2783 "Number of intervals that have been consed so far.");
2784
2785 #if 0
2786 DEFVAR_INT ("data-bytes-used", &malloc_sbrk_used,
2787 "Number of bytes of unshared memory allocated in this session.");
2788
2789 DEFVAR_INT ("data-bytes-free", &malloc_sbrk_unused,
2790 "Number of bytes of unshared memory remaining available in this session.");
2791 #endif
2792
2793 DEFVAR_LISP ("purify-flag", &Vpurify_flag,
2794 "Non-nil means loading Lisp code in order to dump an executable.\n\
2795 This means that certain objects should be allocated in shared (pure) space.");
2796
2797 DEFVAR_INT ("undo-limit", &undo_limit,
2798 "Keep no more undo information once it exceeds this size.\n\
2799 This limit is applied when garbage collection happens.\n\
2800 The size is counted as the number of bytes occupied,\n\
2801 which includes both saved text and other data.");
2802 undo_limit = 20000;
2803
2804 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit,
2805 "Don't keep more than this much size of undo information.\n\
2806 A command which pushes past this size is itself forgotten.\n\
2807 This limit is applied when garbage collection happens.\n\
2808 The size is counted as the number of bytes occupied,\n\
2809 which includes both saved text and other data.");
2810 undo_strong_limit = 30000;
2811
2812 DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages,
2813 "Non-nil means display messages at start and end of garbage collection.");
2814 garbage_collection_messages = 0;
2815
2816 /* We build this in advance because if we wait until we need it, we might
2817 not be able to allocate the memory to hold it. */
2818 memory_signal_data
2819 = Fcons (Qerror, Fcons (build_string ("Memory exhausted--use M-x save-some-buffers RET"), Qnil));
2820 staticpro (&memory_signal_data);
2821
2822 staticpro (&Qgc_cons_threshold);
2823 Qgc_cons_threshold = intern ("gc-cons-threshold");
2824
2825 staticpro (&Qchar_table_extra_slots);
2826 Qchar_table_extra_slots = intern ("char-table-extra-slots");
2827
2828 defsubr (&Scons);
2829 defsubr (&Slist);
2830 defsubr (&Svector);
2831 defsubr (&Smake_byte_code);
2832 defsubr (&Smake_list);
2833 defsubr (&Smake_vector);
2834 defsubr (&Smake_char_table);
2835 defsubr (&Smake_string);
2836 defsubr (&Smake_bool_vector);
2837 defsubr (&Smake_symbol);
2838 defsubr (&Smake_marker);
2839 defsubr (&Spurecopy);
2840 defsubr (&Sgarbage_collect);
2841 defsubr (&Smemory_limit);
2842 defsubr (&Smemory_use_counts);
2843 }