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