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