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