Put doc strings in comments.
[bpt/emacs.git] / 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, 2000, 2001
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 #include <stdio.h>
24
25 /* Note that this declares bzero on OSF/1. How dumb. */
26
27 #include <signal.h>
28
29 /* GC_MALLOC_CHECK defined means perform validity checks of malloc'd
30 memory. Can do this only if using gmalloc.c. */
31
32 #if defined SYSTEM_MALLOC || defined DOUG_LEA_MALLOC
33 #undef GC_MALLOC_CHECK
34 #endif
35
36 /* This file is part of the core Lisp implementation, and thus must
37 deal with the real data structures. If the Lisp implementation is
38 replaced, this file likely will not be used. */
39
40 #undef HIDE_LISP_IMPLEMENTATION
41 #define DOC_STRINGS_IN_COMMENTS
42 #include "lisp.h"
43 #include "process.h"
44 #include "intervals.h"
45 #include "puresize.h"
46 #include "buffer.h"
47 #include "window.h"
48 #include "keyboard.h"
49 #include "frame.h"
50 #include "blockinput.h"
51 #include "charset.h"
52 #include "syssignal.h"
53 #include <setjmp.h>
54
55 #ifdef HAVE_UNISTD_H
56 #include <unistd.h>
57 #else
58 extern POINTER_TYPE *sbrk ();
59 #endif
60
61 #ifdef DOUG_LEA_MALLOC
62
63 #include <malloc.h>
64 /* malloc.h #defines this as size_t, at least in glibc2. */
65 #ifndef __malloc_size_t
66 #define __malloc_size_t int
67 #endif
68
69 /* Specify maximum number of areas to mmap. It would be nice to use a
70 value that explicitly means "no limit". */
71
72 #define MMAP_MAX_AREAS 100000000
73
74 #else /* not DOUG_LEA_MALLOC */
75
76 /* The following come from gmalloc.c. */
77
78 #define __malloc_size_t size_t
79 extern __malloc_size_t _bytes_used;
80 extern __malloc_size_t __malloc_extra_blocks;
81
82 #endif /* not DOUG_LEA_MALLOC */
83
84 /* Macro to verify that storage intended for Lisp objects is not
85 out of range to fit in the space for a pointer.
86 ADDRESS is the start of the block, and SIZE
87 is the amount of space within which objects can start. */
88
89 #define VALIDATE_LISP_STORAGE(address, size) \
90 do \
91 { \
92 Lisp_Object val; \
93 XSETCONS (val, (char *) address + size); \
94 if ((char *) XCONS (val) != (char *) address + size) \
95 { \
96 xfree (address); \
97 memory_full (); \
98 } \
99 } while (0)
100
101 /* Value of _bytes_used, when spare_memory was freed. */
102
103 static __malloc_size_t bytes_used_when_full;
104
105 /* Mark, unmark, query mark bit of a Lisp string. S must be a pointer
106 to a struct Lisp_String. */
107
108 #define MARK_STRING(S) ((S)->size |= MARKBIT)
109 #define UNMARK_STRING(S) ((S)->size &= ~MARKBIT)
110 #define STRING_MARKED_P(S) ((S)->size & MARKBIT)
111
112 /* Value is the number of bytes/chars of S, a pointer to a struct
113 Lisp_String. This must be used instead of STRING_BYTES (S) or
114 S->size during GC, because S->size contains the mark bit for
115 strings. */
116
117 #define GC_STRING_BYTES(S) (STRING_BYTES (S) & ~MARKBIT)
118 #define GC_STRING_CHARS(S) ((S)->size & ~MARKBIT)
119
120 /* Number of bytes of consing done since the last gc. */
121
122 int consing_since_gc;
123
124 /* Count the amount of consing of various sorts of space. */
125
126 int cons_cells_consed;
127 int floats_consed;
128 int vector_cells_consed;
129 int symbols_consed;
130 int string_chars_consed;
131 int misc_objects_consed;
132 int intervals_consed;
133 int strings_consed;
134
135 /* Number of bytes of consing since GC before another GC should be done. */
136
137 int gc_cons_threshold;
138
139 /* Nonzero during GC. */
140
141 int gc_in_progress;
142
143 /* Nonzero means display messages at beginning and end of GC. */
144
145 int garbage_collection_messages;
146
147 #ifndef VIRT_ADDR_VARIES
148 extern
149 #endif /* VIRT_ADDR_VARIES */
150 int malloc_sbrk_used;
151
152 #ifndef VIRT_ADDR_VARIES
153 extern
154 #endif /* VIRT_ADDR_VARIES */
155 int malloc_sbrk_unused;
156
157 /* Two limits controlling how much undo information to keep. */
158
159 int undo_limit;
160 int undo_strong_limit;
161
162 /* Number of live and free conses etc. */
163
164 static int total_conses, total_markers, total_symbols, total_vector_size;
165 static int total_free_conses, total_free_markers, total_free_symbols;
166 static int total_free_floats, total_floats;
167
168 /* Points to memory space allocated as "spare", to be freed if we run
169 out of memory. */
170
171 static char *spare_memory;
172
173 /* Amount of spare memory to keep in reserve. */
174
175 #define SPARE_MEMORY (1 << 14)
176
177 /* Number of extra blocks malloc should get when it needs more core. */
178
179 static int malloc_hysteresis;
180
181 /* Non-nil means defun should do purecopy on the function definition. */
182
183 Lisp_Object Vpurify_flag;
184
185 #ifndef HAVE_SHM
186
187 /* Force it into data space! */
188
189 EMACS_INT pure[PURESIZE / sizeof (EMACS_INT)] = {0,};
190 #define PUREBEG (char *) pure
191
192 #else /* HAVE_SHM */
193
194 #define pure PURE_SEG_BITS /* Use shared memory segment */
195 #define PUREBEG (char *)PURE_SEG_BITS
196
197 #endif /* HAVE_SHM */
198
199 /* Pointer to the pure area, and its size. */
200
201 static char *purebeg;
202 static size_t pure_size;
203
204 /* Number of bytes of pure storage used before pure storage overflowed.
205 If this is non-zero, this implies that an overflow occurred. */
206
207 static size_t pure_bytes_used_before_overflow;
208
209 /* Value is non-zero if P points into pure space. */
210
211 #define PURE_POINTER_P(P) \
212 (((PNTR_COMPARISON_TYPE) (P) \
213 < (PNTR_COMPARISON_TYPE) ((char *) purebeg + pure_size)) \
214 && ((PNTR_COMPARISON_TYPE) (P) \
215 >= (PNTR_COMPARISON_TYPE) purebeg))
216
217 /* Index in pure at which next pure object will be allocated.. */
218
219 int pure_bytes_used;
220
221 /* If nonzero, this is a warning delivered by malloc and not yet
222 displayed. */
223
224 char *pending_malloc_warning;
225
226 /* Pre-computed signal argument for use when memory is exhausted. */
227
228 Lisp_Object memory_signal_data;
229
230 /* Maximum amount of C stack to save when a GC happens. */
231
232 #ifndef MAX_SAVE_STACK
233 #define MAX_SAVE_STACK 16000
234 #endif
235
236 /* Buffer in which we save a copy of the C stack at each GC. */
237
238 char *stack_copy;
239 int stack_copy_size;
240
241 /* Non-zero means ignore malloc warnings. Set during initialization.
242 Currently not used. */
243
244 int ignore_warnings;
245
246 Lisp_Object Qgc_cons_threshold, Qchar_table_extra_slots;
247
248 /* Hook run after GC has finished. */
249
250 Lisp_Object Vpost_gc_hook, Qpost_gc_hook;
251
252 static void mark_buffer P_ ((Lisp_Object));
253 static void mark_kboards P_ ((void));
254 static void gc_sweep P_ ((void));
255 static void mark_glyph_matrix P_ ((struct glyph_matrix *));
256 static void mark_face_cache P_ ((struct face_cache *));
257
258 #ifdef HAVE_WINDOW_SYSTEM
259 static void mark_image P_ ((struct image *));
260 static void mark_image_cache P_ ((struct frame *));
261 #endif /* HAVE_WINDOW_SYSTEM */
262
263 static struct Lisp_String *allocate_string P_ ((void));
264 static void compact_small_strings P_ ((void));
265 static void free_large_strings P_ ((void));
266 static void sweep_strings P_ ((void));
267
268 extern int message_enable_multibyte;
269
270 /* When scanning the C stack for live Lisp objects, Emacs keeps track
271 of what memory allocated via lisp_malloc is intended for what
272 purpose. This enumeration specifies the type of memory. */
273
274 enum mem_type
275 {
276 MEM_TYPE_NON_LISP,
277 MEM_TYPE_BUFFER,
278 MEM_TYPE_CONS,
279 MEM_TYPE_STRING,
280 MEM_TYPE_MISC,
281 MEM_TYPE_SYMBOL,
282 MEM_TYPE_FLOAT,
283 /* Keep the following vector-like types together, with
284 MEM_TYPE_WINDOW being the last, and MEM_TYPE_VECTOR the
285 first. Or change the code of live_vector_p, for instance. */
286 MEM_TYPE_VECTOR,
287 MEM_TYPE_PROCESS,
288 MEM_TYPE_HASH_TABLE,
289 MEM_TYPE_FRAME,
290 MEM_TYPE_WINDOW
291 };
292
293 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
294
295 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
296 #include <stdio.h> /* For fprintf. */
297 #endif
298
299 /* A unique object in pure space used to make some Lisp objects
300 on free lists recognizable in O(1). */
301
302 Lisp_Object Vdead;
303
304 #ifdef GC_MALLOC_CHECK
305
306 enum mem_type allocated_mem_type;
307 int dont_register_blocks;
308
309 #endif /* GC_MALLOC_CHECK */
310
311 /* A node in the red-black tree describing allocated memory containing
312 Lisp data. Each such block is recorded with its start and end
313 address when it is allocated, and removed from the tree when it
314 is freed.
315
316 A red-black tree is a balanced binary tree with the following
317 properties:
318
319 1. Every node is either red or black.
320 2. Every leaf is black.
321 3. If a node is red, then both of its children are black.
322 4. Every simple path from a node to a descendant leaf contains
323 the same number of black nodes.
324 5. The root is always black.
325
326 When nodes are inserted into the tree, or deleted from the tree,
327 the tree is "fixed" so that these properties are always true.
328
329 A red-black tree with N internal nodes has height at most 2
330 log(N+1). Searches, insertions and deletions are done in O(log N).
331 Please see a text book about data structures for a detailed
332 description of red-black trees. Any book worth its salt should
333 describe them. */
334
335 struct mem_node
336 {
337 struct mem_node *left, *right, *parent;
338
339 /* Start and end of allocated region. */
340 void *start, *end;
341
342 /* Node color. */
343 enum {MEM_BLACK, MEM_RED} color;
344
345 /* Memory type. */
346 enum mem_type type;
347 };
348
349 /* Base address of stack. Set in main. */
350
351 Lisp_Object *stack_base;
352
353 /* Root of the tree describing allocated Lisp memory. */
354
355 static struct mem_node *mem_root;
356
357 /* Lowest and highest known address in the heap. */
358
359 static void *min_heap_address, *max_heap_address;
360
361 /* Sentinel node of the tree. */
362
363 static struct mem_node mem_z;
364 #define MEM_NIL &mem_z
365
366 static POINTER_TYPE *lisp_malloc P_ ((size_t, enum mem_type));
367 static struct Lisp_Vector *allocate_vectorlike P_ ((EMACS_INT, enum mem_type));
368 static void lisp_free P_ ((POINTER_TYPE *));
369 static void mark_stack P_ ((void));
370 static int live_vector_p P_ ((struct mem_node *, void *));
371 static int live_buffer_p P_ ((struct mem_node *, void *));
372 static int live_string_p P_ ((struct mem_node *, void *));
373 static int live_cons_p P_ ((struct mem_node *, void *));
374 static int live_symbol_p P_ ((struct mem_node *, void *));
375 static int live_float_p P_ ((struct mem_node *, void *));
376 static int live_misc_p P_ ((struct mem_node *, void *));
377 static void mark_maybe_object P_ ((Lisp_Object));
378 static void mark_memory P_ ((void *, void *));
379 static void mem_init P_ ((void));
380 static struct mem_node *mem_insert P_ ((void *, void *, enum mem_type));
381 static void mem_insert_fixup P_ ((struct mem_node *));
382 static void mem_rotate_left P_ ((struct mem_node *));
383 static void mem_rotate_right P_ ((struct mem_node *));
384 static void mem_delete P_ ((struct mem_node *));
385 static void mem_delete_fixup P_ ((struct mem_node *));
386 static INLINE struct mem_node *mem_find P_ ((void *));
387
388 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
389 static void check_gcpros P_ ((void));
390 #endif
391
392 #endif /* GC_MARK_STACK || GC_MALLOC_CHECK */
393
394 /* Recording what needs to be marked for gc. */
395
396 struct gcpro *gcprolist;
397
398 /* Addresses of staticpro'd variables. */
399
400 #define NSTATICS 1024
401 Lisp_Object *staticvec[NSTATICS] = {0};
402
403 /* Index of next unused slot in staticvec. */
404
405 int staticidx = 0;
406
407 static POINTER_TYPE *pure_alloc P_ ((size_t, int));
408
409
410 /* Value is SZ rounded up to the next multiple of ALIGNMENT.
411 ALIGNMENT must be a power of 2. */
412
413 #define ALIGN(SZ, ALIGNMENT) \
414 (((SZ) + (ALIGNMENT) - 1) & ~((ALIGNMENT) - 1))
415
416
417 \f
418 /************************************************************************
419 Malloc
420 ************************************************************************/
421
422 /* Write STR to Vstandard_output plus some advice on how to free some
423 memory. Called when memory gets low. */
424
425 Lisp_Object
426 malloc_warning_1 (str)
427 Lisp_Object str;
428 {
429 Fprinc (str, Vstandard_output);
430 write_string ("\nKilling some buffers may delay running out of memory.\n", -1);
431 write_string ("However, certainly by the time you receive the 95% warning,\n", -1);
432 write_string ("you should clean up, kill this Emacs, and start a new one.", -1);
433 return Qnil;
434 }
435
436
437 /* Function malloc calls this if it finds we are near exhausting
438 storage. */
439
440 void
441 malloc_warning (str)
442 char *str;
443 {
444 pending_malloc_warning = str;
445 }
446
447
448 /* Display a malloc warning in buffer *Danger*. */
449
450 void
451 display_malloc_warning ()
452 {
453 register Lisp_Object val;
454
455 val = build_string (pending_malloc_warning);
456 pending_malloc_warning = 0;
457 internal_with_output_to_temp_buffer (" *Danger*", malloc_warning_1, val);
458 }
459
460
461 #ifdef DOUG_LEA_MALLOC
462 # define BYTES_USED (mallinfo ().arena)
463 #else
464 # define BYTES_USED _bytes_used
465 #endif
466
467
468 /* Called if malloc returns zero. */
469
470 void
471 memory_full ()
472 {
473 #ifndef SYSTEM_MALLOC
474 bytes_used_when_full = BYTES_USED;
475 #endif
476
477 /* The first time we get here, free the spare memory. */
478 if (spare_memory)
479 {
480 free (spare_memory);
481 spare_memory = 0;
482 }
483
484 /* This used to call error, but if we've run out of memory, we could
485 get infinite recursion trying to build the string. */
486 while (1)
487 Fsignal (Qnil, memory_signal_data);
488 }
489
490
491 /* Called if we can't allocate relocatable space for a buffer. */
492
493 void
494 buffer_memory_full ()
495 {
496 /* If buffers use the relocating allocator, no need to free
497 spare_memory, because we may have plenty of malloc space left
498 that we could get, and if we don't, the malloc that fails will
499 itself cause spare_memory to be freed. If buffers don't use the
500 relocating allocator, treat this like any other failing
501 malloc. */
502
503 #ifndef REL_ALLOC
504 memory_full ();
505 #endif
506
507 /* This used to call error, but if we've run out of memory, we could
508 get infinite recursion trying to build the string. */
509 while (1)
510 Fsignal (Qerror, memory_signal_data);
511 }
512
513
514 /* Like malloc but check for no memory and block interrupt input.. */
515
516 POINTER_TYPE *
517 xmalloc (size)
518 size_t size;
519 {
520 register POINTER_TYPE *val;
521
522 BLOCK_INPUT;
523 val = (POINTER_TYPE *) malloc (size);
524 UNBLOCK_INPUT;
525
526 if (!val && size)
527 memory_full ();
528 return val;
529 }
530
531
532 /* Like realloc but check for no memory and block interrupt input.. */
533
534 POINTER_TYPE *
535 xrealloc (block, size)
536 POINTER_TYPE *block;
537 size_t size;
538 {
539 register POINTER_TYPE *val;
540
541 BLOCK_INPUT;
542 /* We must call malloc explicitly when BLOCK is 0, since some
543 reallocs don't do this. */
544 if (! block)
545 val = (POINTER_TYPE *) malloc (size);
546 else
547 val = (POINTER_TYPE *) realloc (block, size);
548 UNBLOCK_INPUT;
549
550 if (!val && size) memory_full ();
551 return val;
552 }
553
554
555 /* Like free but block interrupt input.. */
556
557 void
558 xfree (block)
559 POINTER_TYPE *block;
560 {
561 BLOCK_INPUT;
562 free (block);
563 UNBLOCK_INPUT;
564 }
565
566
567 /* Like strdup, but uses xmalloc. */
568
569 char *
570 xstrdup (s)
571 char *s;
572 {
573 size_t len = strlen (s) + 1;
574 char *p = (char *) xmalloc (len);
575 bcopy (s, p, len);
576 return p;
577 }
578
579
580 /* Like malloc but used for allocating Lisp data. NBYTES is the
581 number of bytes to allocate, TYPE describes the intended use of the
582 allcated memory block (for strings, for conses, ...). */
583
584 static POINTER_TYPE *
585 lisp_malloc (nbytes, type)
586 size_t nbytes;
587 enum mem_type type;
588 {
589 register void *val;
590
591 BLOCK_INPUT;
592
593 #ifdef GC_MALLOC_CHECK
594 allocated_mem_type = type;
595 #endif
596
597 val = (void *) malloc (nbytes);
598
599 #if GC_MARK_STACK && !defined GC_MALLOC_CHECK
600 if (val && type != MEM_TYPE_NON_LISP)
601 mem_insert (val, (char *) val + nbytes, type);
602 #endif
603
604 UNBLOCK_INPUT;
605 if (!val && nbytes)
606 memory_full ();
607 return val;
608 }
609
610
611 /* Return a new buffer structure allocated from the heap with
612 a call to lisp_malloc. */
613
614 struct buffer *
615 allocate_buffer ()
616 {
617 struct buffer *b
618 = (struct buffer *) lisp_malloc (sizeof (struct buffer),
619 MEM_TYPE_BUFFER);
620 VALIDATE_LISP_STORAGE (b, sizeof *b);
621 return b;
622 }
623
624
625 /* Free BLOCK. This must be called to free memory allocated with a
626 call to lisp_malloc. */
627
628 static void
629 lisp_free (block)
630 POINTER_TYPE *block;
631 {
632 BLOCK_INPUT;
633 free (block);
634 #if GC_MARK_STACK && !defined GC_MALLOC_CHECK
635 mem_delete (mem_find (block));
636 #endif
637 UNBLOCK_INPUT;
638 }
639
640 \f
641 /* Arranging to disable input signals while we're in malloc.
642
643 This only works with GNU malloc. To help out systems which can't
644 use GNU malloc, all the calls to malloc, realloc, and free
645 elsewhere in the code should be inside a BLOCK_INPUT/UNBLOCK_INPUT
646 pairs; unfortunately, we have no idea what C library functions
647 might call malloc, so we can't really protect them unless you're
648 using GNU malloc. Fortunately, most of the major operating can use
649 GNU malloc. */
650
651 #ifndef SYSTEM_MALLOC
652 #ifndef DOUG_LEA_MALLOC
653 extern void * (*__malloc_hook) P_ ((size_t));
654 extern void * (*__realloc_hook) P_ ((void *, size_t));
655 extern void (*__free_hook) P_ ((void *));
656 /* Else declared in malloc.h, perhaps with an extra arg. */
657 #endif /* DOUG_LEA_MALLOC */
658 static void * (*old_malloc_hook) ();
659 static void * (*old_realloc_hook) ();
660 static void (*old_free_hook) ();
661
662 /* This function is used as the hook for free to call. */
663
664 static void
665 emacs_blocked_free (ptr)
666 void *ptr;
667 {
668 BLOCK_INPUT;
669
670 #ifdef GC_MALLOC_CHECK
671 if (ptr)
672 {
673 struct mem_node *m;
674
675 m = mem_find (ptr);
676 if (m == MEM_NIL || m->start != ptr)
677 {
678 fprintf (stderr,
679 "Freeing `%p' which wasn't allocated with malloc\n", ptr);
680 abort ();
681 }
682 else
683 {
684 /* fprintf (stderr, "free %p...%p (%p)\n", m->start, m->end, ptr); */
685 mem_delete (m);
686 }
687 }
688 #endif /* GC_MALLOC_CHECK */
689
690 __free_hook = old_free_hook;
691 free (ptr);
692
693 /* If we released our reserve (due to running out of memory),
694 and we have a fair amount free once again,
695 try to set aside another reserve in case we run out once more. */
696 if (spare_memory == 0
697 /* Verify there is enough space that even with the malloc
698 hysteresis this call won't run out again.
699 The code here is correct as long as SPARE_MEMORY
700 is substantially larger than the block size malloc uses. */
701 && (bytes_used_when_full
702 > BYTES_USED + max (malloc_hysteresis, 4) * SPARE_MEMORY))
703 spare_memory = (char *) malloc ((size_t) SPARE_MEMORY);
704
705 __free_hook = emacs_blocked_free;
706 UNBLOCK_INPUT;
707 }
708
709
710 /* If we released our reserve (due to running out of memory),
711 and we have a fair amount free once again,
712 try to set aside another reserve in case we run out once more.
713
714 This is called when a relocatable block is freed in ralloc.c. */
715
716 void
717 refill_memory_reserve ()
718 {
719 if (spare_memory == 0)
720 spare_memory = (char *) malloc ((size_t) SPARE_MEMORY);
721 }
722
723
724 /* This function is the malloc hook that Emacs uses. */
725
726 static void *
727 emacs_blocked_malloc (size)
728 size_t size;
729 {
730 void *value;
731
732 BLOCK_INPUT;
733 __malloc_hook = old_malloc_hook;
734 #ifdef DOUG_LEA_MALLOC
735 mallopt (M_TOP_PAD, malloc_hysteresis * 4096);
736 #else
737 __malloc_extra_blocks = malloc_hysteresis;
738 #endif
739
740 value = (void *) malloc (size);
741
742 #ifdef GC_MALLOC_CHECK
743 {
744 struct mem_node *m = mem_find (value);
745 if (m != MEM_NIL)
746 {
747 fprintf (stderr, "Malloc returned %p which is already in use\n",
748 value);
749 fprintf (stderr, "Region in use is %p...%p, %u bytes, type %d\n",
750 m->start, m->end, (char *) m->end - (char *) m->start,
751 m->type);
752 abort ();
753 }
754
755 if (!dont_register_blocks)
756 {
757 mem_insert (value, (char *) value + max (1, size), allocated_mem_type);
758 allocated_mem_type = MEM_TYPE_NON_LISP;
759 }
760 }
761 #endif /* GC_MALLOC_CHECK */
762
763 __malloc_hook = emacs_blocked_malloc;
764 UNBLOCK_INPUT;
765
766 /* fprintf (stderr, "%p malloc\n", value); */
767 return value;
768 }
769
770
771 /* This function is the realloc hook that Emacs uses. */
772
773 static void *
774 emacs_blocked_realloc (ptr, size)
775 void *ptr;
776 size_t size;
777 {
778 void *value;
779
780 BLOCK_INPUT;
781 __realloc_hook = old_realloc_hook;
782
783 #ifdef GC_MALLOC_CHECK
784 if (ptr)
785 {
786 struct mem_node *m = mem_find (ptr);
787 if (m == MEM_NIL || m->start != ptr)
788 {
789 fprintf (stderr,
790 "Realloc of %p which wasn't allocated with malloc\n",
791 ptr);
792 abort ();
793 }
794
795 mem_delete (m);
796 }
797
798 /* fprintf (stderr, "%p -> realloc\n", ptr); */
799
800 /* Prevent malloc from registering blocks. */
801 dont_register_blocks = 1;
802 #endif /* GC_MALLOC_CHECK */
803
804 value = (void *) realloc (ptr, size);
805
806 #ifdef GC_MALLOC_CHECK
807 dont_register_blocks = 0;
808
809 {
810 struct mem_node *m = mem_find (value);
811 if (m != MEM_NIL)
812 {
813 fprintf (stderr, "Realloc returns memory that is already in use\n");
814 abort ();
815 }
816
817 /* Can't handle zero size regions in the red-black tree. */
818 mem_insert (value, (char *) value + max (size, 1), MEM_TYPE_NON_LISP);
819 }
820
821 /* fprintf (stderr, "%p <- realloc\n", value); */
822 #endif /* GC_MALLOC_CHECK */
823
824 __realloc_hook = emacs_blocked_realloc;
825 UNBLOCK_INPUT;
826
827 return value;
828 }
829
830
831 /* Called from main to set up malloc to use our hooks. */
832
833 void
834 uninterrupt_malloc ()
835 {
836 if (__free_hook != emacs_blocked_free)
837 old_free_hook = __free_hook;
838 __free_hook = emacs_blocked_free;
839
840 if (__malloc_hook != emacs_blocked_malloc)
841 old_malloc_hook = __malloc_hook;
842 __malloc_hook = emacs_blocked_malloc;
843
844 if (__realloc_hook != emacs_blocked_realloc)
845 old_realloc_hook = __realloc_hook;
846 __realloc_hook = emacs_blocked_realloc;
847 }
848
849 #endif /* not SYSTEM_MALLOC */
850
851
852 \f
853 /***********************************************************************
854 Interval Allocation
855 ***********************************************************************/
856
857 /* Number of intervals allocated in an interval_block structure.
858 The 1020 is 1024 minus malloc overhead. */
859
860 #define INTERVAL_BLOCK_SIZE \
861 ((1020 - sizeof (struct interval_block *)) / sizeof (struct interval))
862
863 /* Intervals are allocated in chunks in form of an interval_block
864 structure. */
865
866 struct interval_block
867 {
868 struct interval_block *next;
869 struct interval intervals[INTERVAL_BLOCK_SIZE];
870 };
871
872 /* Current interval block. Its `next' pointer points to older
873 blocks. */
874
875 struct interval_block *interval_block;
876
877 /* Index in interval_block above of the next unused interval
878 structure. */
879
880 static int interval_block_index;
881
882 /* Number of free and live intervals. */
883
884 static int total_free_intervals, total_intervals;
885
886 /* List of free intervals. */
887
888 INTERVAL interval_free_list;
889
890 /* Total number of interval blocks now in use. */
891
892 int n_interval_blocks;
893
894
895 /* Initialize interval allocation. */
896
897 static void
898 init_intervals ()
899 {
900 interval_block
901 = (struct interval_block *) lisp_malloc (sizeof *interval_block,
902 MEM_TYPE_NON_LISP);
903 interval_block->next = 0;
904 bzero ((char *) interval_block->intervals, sizeof interval_block->intervals);
905 interval_block_index = 0;
906 interval_free_list = 0;
907 n_interval_blocks = 1;
908 }
909
910
911 /* Return a new interval. */
912
913 INTERVAL
914 make_interval ()
915 {
916 INTERVAL val;
917
918 if (interval_free_list)
919 {
920 val = interval_free_list;
921 interval_free_list = INTERVAL_PARENT (interval_free_list);
922 }
923 else
924 {
925 if (interval_block_index == INTERVAL_BLOCK_SIZE)
926 {
927 register struct interval_block *newi;
928
929 newi = (struct interval_block *) lisp_malloc (sizeof *newi,
930 MEM_TYPE_NON_LISP);
931
932 VALIDATE_LISP_STORAGE (newi, sizeof *newi);
933 newi->next = interval_block;
934 interval_block = newi;
935 interval_block_index = 0;
936 n_interval_blocks++;
937 }
938 val = &interval_block->intervals[interval_block_index++];
939 }
940 consing_since_gc += sizeof (struct interval);
941 intervals_consed++;
942 RESET_INTERVAL (val);
943 return val;
944 }
945
946
947 /* Mark Lisp objects in interval I. */
948
949 static void
950 mark_interval (i, dummy)
951 register INTERVAL i;
952 Lisp_Object dummy;
953 {
954 if (XMARKBIT (i->plist))
955 abort ();
956 mark_object (&i->plist);
957 XMARK (i->plist);
958 }
959
960
961 /* Mark the interval tree rooted in TREE. Don't call this directly;
962 use the macro MARK_INTERVAL_TREE instead. */
963
964 static void
965 mark_interval_tree (tree)
966 register INTERVAL tree;
967 {
968 /* No need to test if this tree has been marked already; this
969 function is always called through the MARK_INTERVAL_TREE macro,
970 which takes care of that. */
971
972 /* XMARK expands to an assignment; the LHS of an assignment can't be
973 a cast. */
974 XMARK (tree->up.obj);
975
976 traverse_intervals_noorder (tree, mark_interval, Qnil);
977 }
978
979
980 /* Mark the interval tree rooted in I. */
981
982 #define MARK_INTERVAL_TREE(i) \
983 do { \
984 if (!NULL_INTERVAL_P (i) \
985 && ! XMARKBIT (i->up.obj)) \
986 mark_interval_tree (i); \
987 } while (0)
988
989
990 /* The oddity in the call to XUNMARK is necessary because XUNMARK
991 expands to an assignment to its argument, and most C compilers
992 don't support casts on the left operand of `='. */
993
994 #define UNMARK_BALANCE_INTERVALS(i) \
995 do { \
996 if (! NULL_INTERVAL_P (i)) \
997 { \
998 XUNMARK ((i)->up.obj); \
999 (i) = balance_intervals (i); \
1000 } \
1001 } while (0)
1002
1003 \f
1004 /* Number support. If NO_UNION_TYPE isn't in effect, we
1005 can't create number objects in macros. */
1006 #ifndef make_number
1007 Lisp_Object
1008 make_number (n)
1009 int n;
1010 {
1011 Lisp_Object obj;
1012 obj.s.val = n;
1013 obj.s.type = Lisp_Int;
1014 return obj;
1015 }
1016 #endif
1017 \f
1018 /***********************************************************************
1019 String Allocation
1020 ***********************************************************************/
1021
1022 /* Lisp_Strings are allocated in string_block structures. When a new
1023 string_block is allocated, all the Lisp_Strings it contains are
1024 added to a free-list stiing_free_list. When a new Lisp_String is
1025 needed, it is taken from that list. During the sweep phase of GC,
1026 string_blocks that are entirely free are freed, except two which
1027 we keep.
1028
1029 String data is allocated from sblock structures. Strings larger
1030 than LARGE_STRING_BYTES, get their own sblock, data for smaller
1031 strings is sub-allocated out of sblocks of size SBLOCK_SIZE.
1032
1033 Sblocks consist internally of sdata structures, one for each
1034 Lisp_String. The sdata structure points to the Lisp_String it
1035 belongs to. The Lisp_String points back to the `u.data' member of
1036 its sdata structure.
1037
1038 When a Lisp_String is freed during GC, it is put back on
1039 string_free_list, and its `data' member and its sdata's `string'
1040 pointer is set to null. The size of the string is recorded in the
1041 `u.nbytes' member of the sdata. So, sdata structures that are no
1042 longer used, can be easily recognized, and it's easy to compact the
1043 sblocks of small strings which we do in compact_small_strings. */
1044
1045 /* Size in bytes of an sblock structure used for small strings. This
1046 is 8192 minus malloc overhead. */
1047
1048 #define SBLOCK_SIZE 8188
1049
1050 /* Strings larger than this are considered large strings. String data
1051 for large strings is allocated from individual sblocks. */
1052
1053 #define LARGE_STRING_BYTES 1024
1054
1055 /* Structure describing string memory sub-allocated from an sblock.
1056 This is where the contents of Lisp strings are stored. */
1057
1058 struct sdata
1059 {
1060 /* Back-pointer to the string this sdata belongs to. If null, this
1061 structure is free, and the NBYTES member of the union below
1062 contains the string's byte size (the same value that STRING_BYTES
1063 would return if STRING were non-null). If non-null, STRING_BYTES
1064 (STRING) is the size of the data, and DATA contains the string's
1065 contents. */
1066 struct Lisp_String *string;
1067
1068 #ifdef GC_CHECK_STRING_BYTES
1069
1070 EMACS_INT nbytes;
1071 unsigned char data[1];
1072
1073 #define SDATA_NBYTES(S) (S)->nbytes
1074 #define SDATA_DATA(S) (S)->data
1075
1076 #else /* not GC_CHECK_STRING_BYTES */
1077
1078 union
1079 {
1080 /* When STRING in non-null. */
1081 unsigned char data[1];
1082
1083 /* When STRING is null. */
1084 EMACS_INT nbytes;
1085 } u;
1086
1087
1088 #define SDATA_NBYTES(S) (S)->u.nbytes
1089 #define SDATA_DATA(S) (S)->u.data
1090
1091 #endif /* not GC_CHECK_STRING_BYTES */
1092 };
1093
1094
1095 /* Structure describing a block of memory which is sub-allocated to
1096 obtain string data memory for strings. Blocks for small strings
1097 are of fixed size SBLOCK_SIZE. Blocks for large strings are made
1098 as large as needed. */
1099
1100 struct sblock
1101 {
1102 /* Next in list. */
1103 struct sblock *next;
1104
1105 /* Pointer to the next free sdata block. This points past the end
1106 of the sblock if there isn't any space left in this block. */
1107 struct sdata *next_free;
1108
1109 /* Start of data. */
1110 struct sdata first_data;
1111 };
1112
1113 /* Number of Lisp strings in a string_block structure. The 1020 is
1114 1024 minus malloc overhead. */
1115
1116 #define STRINGS_IN_STRING_BLOCK \
1117 ((1020 - sizeof (struct string_block *)) / sizeof (struct Lisp_String))
1118
1119 /* Structure describing a block from which Lisp_String structures
1120 are allocated. */
1121
1122 struct string_block
1123 {
1124 struct string_block *next;
1125 struct Lisp_String strings[STRINGS_IN_STRING_BLOCK];
1126 };
1127
1128 /* Head and tail of the list of sblock structures holding Lisp string
1129 data. We always allocate from current_sblock. The NEXT pointers
1130 in the sblock structures go from oldest_sblock to current_sblock. */
1131
1132 static struct sblock *oldest_sblock, *current_sblock;
1133
1134 /* List of sblocks for large strings. */
1135
1136 static struct sblock *large_sblocks;
1137
1138 /* List of string_block structures, and how many there are. */
1139
1140 static struct string_block *string_blocks;
1141 static int n_string_blocks;
1142
1143 /* Free-list of Lisp_Strings. */
1144
1145 static struct Lisp_String *string_free_list;
1146
1147 /* Number of live and free Lisp_Strings. */
1148
1149 static int total_strings, total_free_strings;
1150
1151 /* Number of bytes used by live strings. */
1152
1153 static int total_string_size;
1154
1155 /* Given a pointer to a Lisp_String S which is on the free-list
1156 string_free_list, return a pointer to its successor in the
1157 free-list. */
1158
1159 #define NEXT_FREE_LISP_STRING(S) (*(struct Lisp_String **) (S))
1160
1161 /* Return a pointer to the sdata structure belonging to Lisp string S.
1162 S must be live, i.e. S->data must not be null. S->data is actually
1163 a pointer to the `u.data' member of its sdata structure; the
1164 structure starts at a constant offset in front of that. */
1165
1166 #ifdef GC_CHECK_STRING_BYTES
1167
1168 #define SDATA_OF_STRING(S) \
1169 ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *) \
1170 - sizeof (EMACS_INT)))
1171
1172 #else /* not GC_CHECK_STRING_BYTES */
1173
1174 #define SDATA_OF_STRING(S) \
1175 ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *)))
1176
1177 #endif /* not GC_CHECK_STRING_BYTES */
1178
1179 /* Value is the size of an sdata structure large enough to hold NBYTES
1180 bytes of string data. The value returned includes a terminating
1181 NUL byte, the size of the sdata structure, and padding. */
1182
1183 #ifdef GC_CHECK_STRING_BYTES
1184
1185 #define SDATA_SIZE(NBYTES) \
1186 ((sizeof (struct Lisp_String *) \
1187 + (NBYTES) + 1 \
1188 + sizeof (EMACS_INT) \
1189 + sizeof (EMACS_INT) - 1) \
1190 & ~(sizeof (EMACS_INT) - 1))
1191
1192 #else /* not GC_CHECK_STRING_BYTES */
1193
1194 #define SDATA_SIZE(NBYTES) \
1195 ((sizeof (struct Lisp_String *) \
1196 + (NBYTES) + 1 \
1197 + sizeof (EMACS_INT) - 1) \
1198 & ~(sizeof (EMACS_INT) - 1))
1199
1200 #endif /* not GC_CHECK_STRING_BYTES */
1201
1202 /* Initialize string allocation. Called from init_alloc_once. */
1203
1204 void
1205 init_strings ()
1206 {
1207 total_strings = total_free_strings = total_string_size = 0;
1208 oldest_sblock = current_sblock = large_sblocks = NULL;
1209 string_blocks = NULL;
1210 n_string_blocks = 0;
1211 string_free_list = NULL;
1212 }
1213
1214
1215 #ifdef GC_CHECK_STRING_BYTES
1216
1217 static int check_string_bytes_count;
1218
1219 void check_string_bytes P_ ((int));
1220 void check_sblock P_ ((struct sblock *));
1221
1222 #define CHECK_STRING_BYTES(S) STRING_BYTES (S)
1223
1224
1225 /* Like GC_STRING_BYTES, but with debugging check. */
1226
1227 int
1228 string_bytes (s)
1229 struct Lisp_String *s;
1230 {
1231 int nbytes = (s->size_byte < 0 ? s->size : s->size_byte) & ~MARKBIT;
1232 if (!PURE_POINTER_P (s)
1233 && s->data
1234 && nbytes != SDATA_NBYTES (SDATA_OF_STRING (s)))
1235 abort ();
1236 return nbytes;
1237 }
1238
1239 /* Check validity Lisp strings' string_bytes member in B. */
1240
1241 void
1242 check_sblock (b)
1243 struct sblock *b;
1244 {
1245 struct sdata *from, *end, *from_end;
1246
1247 end = b->next_free;
1248
1249 for (from = &b->first_data; from < end; from = from_end)
1250 {
1251 /* Compute the next FROM here because copying below may
1252 overwrite data we need to compute it. */
1253 int nbytes;
1254
1255 /* Check that the string size recorded in the string is the
1256 same as the one recorded in the sdata structure. */
1257 if (from->string)
1258 CHECK_STRING_BYTES (from->string);
1259
1260 if (from->string)
1261 nbytes = GC_STRING_BYTES (from->string);
1262 else
1263 nbytes = SDATA_NBYTES (from);
1264
1265 nbytes = SDATA_SIZE (nbytes);
1266 from_end = (struct sdata *) ((char *) from + nbytes);
1267 }
1268 }
1269
1270
1271 /* Check validity of Lisp strings' string_bytes member. ALL_P
1272 non-zero means check all strings, otherwise check only most
1273 recently allocated strings. Used for hunting a bug. */
1274
1275 void
1276 check_string_bytes (all_p)
1277 int all_p;
1278 {
1279 if (all_p)
1280 {
1281 struct sblock *b;
1282
1283 for (b = large_sblocks; b; b = b->next)
1284 {
1285 struct Lisp_String *s = b->first_data.string;
1286 if (s)
1287 CHECK_STRING_BYTES (s);
1288 }
1289
1290 for (b = oldest_sblock; b; b = b->next)
1291 check_sblock (b);
1292 }
1293 else
1294 check_sblock (current_sblock);
1295 }
1296
1297 #endif /* GC_CHECK_STRING_BYTES */
1298
1299
1300 /* Return a new Lisp_String. */
1301
1302 static struct Lisp_String *
1303 allocate_string ()
1304 {
1305 struct Lisp_String *s;
1306
1307 /* If the free-list is empty, allocate a new string_block, and
1308 add all the Lisp_Strings in it to the free-list. */
1309 if (string_free_list == NULL)
1310 {
1311 struct string_block *b;
1312 int i;
1313
1314 b = (struct string_block *) lisp_malloc (sizeof *b, MEM_TYPE_STRING);
1315 VALIDATE_LISP_STORAGE (b, sizeof *b);
1316 bzero (b, sizeof *b);
1317 b->next = string_blocks;
1318 string_blocks = b;
1319 ++n_string_blocks;
1320
1321 for (i = STRINGS_IN_STRING_BLOCK - 1; i >= 0; --i)
1322 {
1323 s = b->strings + i;
1324 NEXT_FREE_LISP_STRING (s) = string_free_list;
1325 string_free_list = s;
1326 }
1327
1328 total_free_strings += STRINGS_IN_STRING_BLOCK;
1329 }
1330
1331 /* Pop a Lisp_String off the free-list. */
1332 s = string_free_list;
1333 string_free_list = NEXT_FREE_LISP_STRING (s);
1334
1335 /* Probably not strictly necessary, but play it safe. */
1336 bzero (s, sizeof *s);
1337
1338 --total_free_strings;
1339 ++total_strings;
1340 ++strings_consed;
1341 consing_since_gc += sizeof *s;
1342
1343 #ifdef GC_CHECK_STRING_BYTES
1344 if (!noninteractive
1345 #ifdef macintosh
1346 && current_sblock
1347 #endif
1348 )
1349 {
1350 if (++check_string_bytes_count == 200)
1351 {
1352 check_string_bytes_count = 0;
1353 check_string_bytes (1);
1354 }
1355 else
1356 check_string_bytes (0);
1357 }
1358 #endif /* GC_CHECK_STRING_BYTES */
1359
1360 return s;
1361 }
1362
1363
1364 /* Set up Lisp_String S for holding NCHARS characters, NBYTES bytes,
1365 plus a NUL byte at the end. Allocate an sdata structure for S, and
1366 set S->data to its `u.data' member. Store a NUL byte at the end of
1367 S->data. Set S->size to NCHARS and S->size_byte to NBYTES. Free
1368 S->data if it was initially non-null. */
1369
1370 void
1371 allocate_string_data (s, nchars, nbytes)
1372 struct Lisp_String *s;
1373 int nchars, nbytes;
1374 {
1375 struct sdata *data, *old_data;
1376 struct sblock *b;
1377 int needed, old_nbytes;
1378
1379 /* Determine the number of bytes needed to store NBYTES bytes
1380 of string data. */
1381 needed = SDATA_SIZE (nbytes);
1382
1383 if (nbytes > LARGE_STRING_BYTES)
1384 {
1385 size_t size = sizeof *b - sizeof (struct sdata) + needed;
1386
1387 #ifdef DOUG_LEA_MALLOC
1388 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed
1389 because mapped region contents are not preserved in
1390 a dumped Emacs. */
1391 mallopt (M_MMAP_MAX, 0);
1392 #endif
1393
1394 b = (struct sblock *) lisp_malloc (size, MEM_TYPE_NON_LISP);
1395
1396 #ifdef DOUG_LEA_MALLOC
1397 /* Back to a reasonable maximum of mmap'ed areas. */
1398 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
1399 #endif
1400
1401 b->next_free = &b->first_data;
1402 b->first_data.string = NULL;
1403 b->next = large_sblocks;
1404 large_sblocks = b;
1405 }
1406 else if (current_sblock == NULL
1407 || (((char *) current_sblock + SBLOCK_SIZE
1408 - (char *) current_sblock->next_free)
1409 < needed))
1410 {
1411 /* Not enough room in the current sblock. */
1412 b = (struct sblock *) lisp_malloc (SBLOCK_SIZE, MEM_TYPE_NON_LISP);
1413 b->next_free = &b->first_data;
1414 b->first_data.string = NULL;
1415 b->next = NULL;
1416
1417 if (current_sblock)
1418 current_sblock->next = b;
1419 else
1420 oldest_sblock = b;
1421 current_sblock = b;
1422 }
1423 else
1424 b = current_sblock;
1425
1426 old_data = s->data ? SDATA_OF_STRING (s) : NULL;
1427 old_nbytes = GC_STRING_BYTES (s);
1428
1429 data = b->next_free;
1430 data->string = s;
1431 s->data = SDATA_DATA (data);
1432 #ifdef GC_CHECK_STRING_BYTES
1433 SDATA_NBYTES (data) = nbytes;
1434 #endif
1435 s->size = nchars;
1436 s->size_byte = nbytes;
1437 s->data[nbytes] = '\0';
1438 b->next_free = (struct sdata *) ((char *) data + needed);
1439
1440 /* If S had already data assigned, mark that as free by setting its
1441 string back-pointer to null, and recording the size of the data
1442 in it. */
1443 if (old_data)
1444 {
1445 SDATA_NBYTES (old_data) = old_nbytes;
1446 old_data->string = NULL;
1447 }
1448
1449 consing_since_gc += needed;
1450 }
1451
1452
1453 /* Sweep and compact strings. */
1454
1455 static void
1456 sweep_strings ()
1457 {
1458 struct string_block *b, *next;
1459 struct string_block *live_blocks = NULL;
1460
1461 string_free_list = NULL;
1462 total_strings = total_free_strings = 0;
1463 total_string_size = 0;
1464
1465 /* Scan strings_blocks, free Lisp_Strings that aren't marked. */
1466 for (b = string_blocks; b; b = next)
1467 {
1468 int i, nfree = 0;
1469 struct Lisp_String *free_list_before = string_free_list;
1470
1471 next = b->next;
1472
1473 for (i = 0; i < STRINGS_IN_STRING_BLOCK; ++i)
1474 {
1475 struct Lisp_String *s = b->strings + i;
1476
1477 if (s->data)
1478 {
1479 /* String was not on free-list before. */
1480 if (STRING_MARKED_P (s))
1481 {
1482 /* String is live; unmark it and its intervals. */
1483 UNMARK_STRING (s);
1484
1485 if (!NULL_INTERVAL_P (s->intervals))
1486 UNMARK_BALANCE_INTERVALS (s->intervals);
1487
1488 ++total_strings;
1489 total_string_size += STRING_BYTES (s);
1490 }
1491 else
1492 {
1493 /* String is dead. Put it on the free-list. */
1494 struct sdata *data = SDATA_OF_STRING (s);
1495
1496 /* Save the size of S in its sdata so that we know
1497 how large that is. Reset the sdata's string
1498 back-pointer so that we know it's free. */
1499 #ifdef GC_CHECK_STRING_BYTES
1500 if (GC_STRING_BYTES (s) != SDATA_NBYTES (data))
1501 abort ();
1502 #else
1503 data->u.nbytes = GC_STRING_BYTES (s);
1504 #endif
1505 data->string = NULL;
1506
1507 /* Reset the strings's `data' member so that we
1508 know it's free. */
1509 s->data = NULL;
1510
1511 /* Put the string on the free-list. */
1512 NEXT_FREE_LISP_STRING (s) = string_free_list;
1513 string_free_list = s;
1514 ++nfree;
1515 }
1516 }
1517 else
1518 {
1519 /* S was on the free-list before. Put it there again. */
1520 NEXT_FREE_LISP_STRING (s) = string_free_list;
1521 string_free_list = s;
1522 ++nfree;
1523 }
1524 }
1525
1526 /* Free blocks that contain free Lisp_Strings only, except
1527 the first two of them. */
1528 if (nfree == STRINGS_IN_STRING_BLOCK
1529 && total_free_strings > STRINGS_IN_STRING_BLOCK)
1530 {
1531 lisp_free (b);
1532 --n_string_blocks;
1533 string_free_list = free_list_before;
1534 }
1535 else
1536 {
1537 total_free_strings += nfree;
1538 b->next = live_blocks;
1539 live_blocks = b;
1540 }
1541 }
1542
1543 string_blocks = live_blocks;
1544 free_large_strings ();
1545 compact_small_strings ();
1546 }
1547
1548
1549 /* Free dead large strings. */
1550
1551 static void
1552 free_large_strings ()
1553 {
1554 struct sblock *b, *next;
1555 struct sblock *live_blocks = NULL;
1556
1557 for (b = large_sblocks; b; b = next)
1558 {
1559 next = b->next;
1560
1561 if (b->first_data.string == NULL)
1562 lisp_free (b);
1563 else
1564 {
1565 b->next = live_blocks;
1566 live_blocks = b;
1567 }
1568 }
1569
1570 large_sblocks = live_blocks;
1571 }
1572
1573
1574 /* Compact data of small strings. Free sblocks that don't contain
1575 data of live strings after compaction. */
1576
1577 static void
1578 compact_small_strings ()
1579 {
1580 struct sblock *b, *tb, *next;
1581 struct sdata *from, *to, *end, *tb_end;
1582 struct sdata *to_end, *from_end;
1583
1584 /* TB is the sblock we copy to, TO is the sdata within TB we copy
1585 to, and TB_END is the end of TB. */
1586 tb = oldest_sblock;
1587 tb_end = (struct sdata *) ((char *) tb + SBLOCK_SIZE);
1588 to = &tb->first_data;
1589
1590 /* Step through the blocks from the oldest to the youngest. We
1591 expect that old blocks will stabilize over time, so that less
1592 copying will happen this way. */
1593 for (b = oldest_sblock; b; b = b->next)
1594 {
1595 end = b->next_free;
1596 xassert ((char *) end <= (char *) b + SBLOCK_SIZE);
1597
1598 for (from = &b->first_data; from < end; from = from_end)
1599 {
1600 /* Compute the next FROM here because copying below may
1601 overwrite data we need to compute it. */
1602 int nbytes;
1603
1604 #ifdef GC_CHECK_STRING_BYTES
1605 /* Check that the string size recorded in the string is the
1606 same as the one recorded in the sdata structure. */
1607 if (from->string
1608 && GC_STRING_BYTES (from->string) != SDATA_NBYTES (from))
1609 abort ();
1610 #endif /* GC_CHECK_STRING_BYTES */
1611
1612 if (from->string)
1613 nbytes = GC_STRING_BYTES (from->string);
1614 else
1615 nbytes = SDATA_NBYTES (from);
1616
1617 nbytes = SDATA_SIZE (nbytes);
1618 from_end = (struct sdata *) ((char *) from + nbytes);
1619
1620 /* FROM->string non-null means it's alive. Copy its data. */
1621 if (from->string)
1622 {
1623 /* If TB is full, proceed with the next sblock. */
1624 to_end = (struct sdata *) ((char *) to + nbytes);
1625 if (to_end > tb_end)
1626 {
1627 tb->next_free = to;
1628 tb = tb->next;
1629 tb_end = (struct sdata *) ((char *) tb + SBLOCK_SIZE);
1630 to = &tb->first_data;
1631 to_end = (struct sdata *) ((char *) to + nbytes);
1632 }
1633
1634 /* Copy, and update the string's `data' pointer. */
1635 if (from != to)
1636 {
1637 xassert (tb != b || to <= from);
1638 safe_bcopy ((char *) from, (char *) to, nbytes);
1639 to->string->data = SDATA_DATA (to);
1640 }
1641
1642 /* Advance past the sdata we copied to. */
1643 to = to_end;
1644 }
1645 }
1646 }
1647
1648 /* The rest of the sblocks following TB don't contain live data, so
1649 we can free them. */
1650 for (b = tb->next; b; b = next)
1651 {
1652 next = b->next;
1653 lisp_free (b);
1654 }
1655
1656 tb->next_free = to;
1657 tb->next = NULL;
1658 current_sblock = tb;
1659 }
1660
1661
1662 DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0,
1663 /* Return a newly created string of length LENGTH, with each element being INIT.
1664 Both LENGTH and INIT must be numbers. */
1665 (length, init))
1666 Lisp_Object length, init;
1667 {
1668 register Lisp_Object val;
1669 register unsigned char *p, *end;
1670 int c, nbytes;
1671
1672 CHECK_NATNUM (length, 0);
1673 CHECK_NUMBER (init, 1);
1674
1675 c = XINT (init);
1676 if (SINGLE_BYTE_CHAR_P (c))
1677 {
1678 nbytes = XINT (length);
1679 val = make_uninit_string (nbytes);
1680 p = XSTRING (val)->data;
1681 end = p + XSTRING (val)->size;
1682 while (p != end)
1683 *p++ = c;
1684 }
1685 else
1686 {
1687 unsigned char str[MAX_MULTIBYTE_LENGTH];
1688 int len = CHAR_STRING (c, str);
1689
1690 nbytes = len * XINT (length);
1691 val = make_uninit_multibyte_string (XINT (length), nbytes);
1692 p = XSTRING (val)->data;
1693 end = p + nbytes;
1694 while (p != end)
1695 {
1696 bcopy (str, p, len);
1697 p += len;
1698 }
1699 }
1700
1701 *p = 0;
1702 return val;
1703 }
1704
1705
1706 DEFUN ("make-bool-vector", Fmake_bool_vector, Smake_bool_vector, 2, 2, 0,
1707 /* Return a new bool-vector of length LENGTH, using INIT for as each element.
1708 LENGTH must be a number. INIT matters only in whether it is t or nil. */
1709 (length, init))
1710 Lisp_Object length, init;
1711 {
1712 register Lisp_Object val;
1713 struct Lisp_Bool_Vector *p;
1714 int real_init, i;
1715 int length_in_chars, length_in_elts, bits_per_value;
1716
1717 CHECK_NATNUM (length, 0);
1718
1719 bits_per_value = sizeof (EMACS_INT) * BITS_PER_CHAR;
1720
1721 length_in_elts = (XFASTINT (length) + bits_per_value - 1) / bits_per_value;
1722 length_in_chars = ((XFASTINT (length) + BITS_PER_CHAR - 1) / BITS_PER_CHAR);
1723
1724 /* We must allocate one more elements than LENGTH_IN_ELTS for the
1725 slot `size' of the struct Lisp_Bool_Vector. */
1726 val = Fmake_vector (make_number (length_in_elts + 1), Qnil);
1727 p = XBOOL_VECTOR (val);
1728
1729 /* Get rid of any bits that would cause confusion. */
1730 p->vector_size = 0;
1731 XSETBOOL_VECTOR (val, p);
1732 p->size = XFASTINT (length);
1733
1734 real_init = (NILP (init) ? 0 : -1);
1735 for (i = 0; i < length_in_chars ; i++)
1736 p->data[i] = real_init;
1737
1738 /* Clear the extraneous bits in the last byte. */
1739 if (XINT (length) != length_in_chars * BITS_PER_CHAR)
1740 XBOOL_VECTOR (val)->data[length_in_chars - 1]
1741 &= (1 << (XINT (length) % BITS_PER_CHAR)) - 1;
1742
1743 return val;
1744 }
1745
1746
1747 /* Make a string from NBYTES bytes at CONTENTS, and compute the number
1748 of characters from the contents. This string may be unibyte or
1749 multibyte, depending on the contents. */
1750
1751 Lisp_Object
1752 make_string (contents, nbytes)
1753 char *contents;
1754 int nbytes;
1755 {
1756 register Lisp_Object val;
1757 int nchars, multibyte_nbytes;
1758
1759 parse_str_as_multibyte (contents, nbytes, &nchars, &multibyte_nbytes);
1760 if (nbytes == nchars || nbytes != multibyte_nbytes)
1761 /* CONTENTS contains no multibyte sequences or contains an invalid
1762 multibyte sequence. We must make unibyte string. */
1763 val = make_unibyte_string (contents, nbytes);
1764 else
1765 val = make_multibyte_string (contents, nchars, nbytes);
1766 return val;
1767 }
1768
1769
1770 /* Make an unibyte string from LENGTH bytes at CONTENTS. */
1771
1772 Lisp_Object
1773 make_unibyte_string (contents, length)
1774 char *contents;
1775 int length;
1776 {
1777 register Lisp_Object val;
1778 val = make_uninit_string (length);
1779 bcopy (contents, XSTRING (val)->data, length);
1780 SET_STRING_BYTES (XSTRING (val), -1);
1781 return val;
1782 }
1783
1784
1785 /* Make a multibyte string from NCHARS characters occupying NBYTES
1786 bytes at CONTENTS. */
1787
1788 Lisp_Object
1789 make_multibyte_string (contents, nchars, nbytes)
1790 char *contents;
1791 int nchars, nbytes;
1792 {
1793 register Lisp_Object val;
1794 val = make_uninit_multibyte_string (nchars, nbytes);
1795 bcopy (contents, XSTRING (val)->data, nbytes);
1796 return val;
1797 }
1798
1799
1800 /* Make a string from NCHARS characters occupying NBYTES bytes at
1801 CONTENTS. It is a multibyte string if NBYTES != NCHARS. */
1802
1803 Lisp_Object
1804 make_string_from_bytes (contents, nchars, nbytes)
1805 char *contents;
1806 int nchars, nbytes;
1807 {
1808 register Lisp_Object val;
1809 val = make_uninit_multibyte_string (nchars, nbytes);
1810 bcopy (contents, XSTRING (val)->data, nbytes);
1811 if (STRING_BYTES (XSTRING (val)) == XSTRING (val)->size)
1812 SET_STRING_BYTES (XSTRING (val), -1);
1813 return val;
1814 }
1815
1816
1817 /* Make a string from NCHARS characters occupying NBYTES bytes at
1818 CONTENTS. The argument MULTIBYTE controls whether to label the
1819 string as multibyte. */
1820
1821 Lisp_Object
1822 make_specified_string (contents, nchars, nbytes, multibyte)
1823 char *contents;
1824 int nchars, nbytes;
1825 int multibyte;
1826 {
1827 register Lisp_Object val;
1828 val = make_uninit_multibyte_string (nchars, nbytes);
1829 bcopy (contents, XSTRING (val)->data, nbytes);
1830 if (!multibyte)
1831 SET_STRING_BYTES (XSTRING (val), -1);
1832 return val;
1833 }
1834
1835
1836 /* Make a string from the data at STR, treating it as multibyte if the
1837 data warrants. */
1838
1839 Lisp_Object
1840 build_string (str)
1841 char *str;
1842 {
1843 return make_string (str, strlen (str));
1844 }
1845
1846
1847 /* Return an unibyte Lisp_String set up to hold LENGTH characters
1848 occupying LENGTH bytes. */
1849
1850 Lisp_Object
1851 make_uninit_string (length)
1852 int length;
1853 {
1854 Lisp_Object val;
1855 val = make_uninit_multibyte_string (length, length);
1856 SET_STRING_BYTES (XSTRING (val), -1);
1857 return val;
1858 }
1859
1860
1861 /* Return a multibyte Lisp_String set up to hold NCHARS characters
1862 which occupy NBYTES bytes. */
1863
1864 Lisp_Object
1865 make_uninit_multibyte_string (nchars, nbytes)
1866 int nchars, nbytes;
1867 {
1868 Lisp_Object string;
1869 struct Lisp_String *s;
1870
1871 if (nchars < 0)
1872 abort ();
1873
1874 s = allocate_string ();
1875 allocate_string_data (s, nchars, nbytes);
1876 XSETSTRING (string, s);
1877 string_chars_consed += nbytes;
1878 return string;
1879 }
1880
1881
1882 \f
1883 /***********************************************************************
1884 Float Allocation
1885 ***********************************************************************/
1886
1887 /* We store float cells inside of float_blocks, allocating a new
1888 float_block with malloc whenever necessary. Float cells reclaimed
1889 by GC are put on a free list to be reallocated before allocating
1890 any new float cells from the latest float_block.
1891
1892 Each float_block is just under 1020 bytes long, since malloc really
1893 allocates in units of powers of two and uses 4 bytes for its own
1894 overhead. */
1895
1896 #define FLOAT_BLOCK_SIZE \
1897 ((1020 - sizeof (struct float_block *)) / sizeof (struct Lisp_Float))
1898
1899 struct float_block
1900 {
1901 struct float_block *next;
1902 struct Lisp_Float floats[FLOAT_BLOCK_SIZE];
1903 };
1904
1905 /* Current float_block. */
1906
1907 struct float_block *float_block;
1908
1909 /* Index of first unused Lisp_Float in the current float_block. */
1910
1911 int float_block_index;
1912
1913 /* Total number of float blocks now in use. */
1914
1915 int n_float_blocks;
1916
1917 /* Free-list of Lisp_Floats. */
1918
1919 struct Lisp_Float *float_free_list;
1920
1921
1922 /* Initialize float allocation. */
1923
1924 void
1925 init_float ()
1926 {
1927 float_block = (struct float_block *) lisp_malloc (sizeof *float_block,
1928 MEM_TYPE_FLOAT);
1929 float_block->next = 0;
1930 bzero ((char *) float_block->floats, sizeof float_block->floats);
1931 float_block_index = 0;
1932 float_free_list = 0;
1933 n_float_blocks = 1;
1934 }
1935
1936
1937 /* Explicitly free a float cell by putting it on the free-list. */
1938
1939 void
1940 free_float (ptr)
1941 struct Lisp_Float *ptr;
1942 {
1943 *(struct Lisp_Float **)&ptr->data = float_free_list;
1944 #if GC_MARK_STACK
1945 ptr->type = Vdead;
1946 #endif
1947 float_free_list = ptr;
1948 }
1949
1950
1951 /* Return a new float object with value FLOAT_VALUE. */
1952
1953 Lisp_Object
1954 make_float (float_value)
1955 double float_value;
1956 {
1957 register Lisp_Object val;
1958
1959 if (float_free_list)
1960 {
1961 /* We use the data field for chaining the free list
1962 so that we won't use the same field that has the mark bit. */
1963 XSETFLOAT (val, float_free_list);
1964 float_free_list = *(struct Lisp_Float **)&float_free_list->data;
1965 }
1966 else
1967 {
1968 if (float_block_index == FLOAT_BLOCK_SIZE)
1969 {
1970 register struct float_block *new;
1971
1972 new = (struct float_block *) lisp_malloc (sizeof *new,
1973 MEM_TYPE_FLOAT);
1974 VALIDATE_LISP_STORAGE (new, sizeof *new);
1975 new->next = float_block;
1976 float_block = new;
1977 float_block_index = 0;
1978 n_float_blocks++;
1979 }
1980 XSETFLOAT (val, &float_block->floats[float_block_index++]);
1981 }
1982
1983 XFLOAT_DATA (val) = float_value;
1984 XSETFASTINT (XFLOAT (val)->type, 0); /* bug chasing -wsr */
1985 consing_since_gc += sizeof (struct Lisp_Float);
1986 floats_consed++;
1987 return val;
1988 }
1989
1990
1991 \f
1992 /***********************************************************************
1993 Cons Allocation
1994 ***********************************************************************/
1995
1996 /* We store cons cells inside of cons_blocks, allocating a new
1997 cons_block with malloc whenever necessary. Cons cells reclaimed by
1998 GC are put on a free list to be reallocated before allocating
1999 any new cons cells from the latest cons_block.
2000
2001 Each cons_block is just under 1020 bytes long,
2002 since malloc really allocates in units of powers of two
2003 and uses 4 bytes for its own overhead. */
2004
2005 #define CONS_BLOCK_SIZE \
2006 ((1020 - sizeof (struct cons_block *)) / sizeof (struct Lisp_Cons))
2007
2008 struct cons_block
2009 {
2010 struct cons_block *next;
2011 struct Lisp_Cons conses[CONS_BLOCK_SIZE];
2012 };
2013
2014 /* Current cons_block. */
2015
2016 struct cons_block *cons_block;
2017
2018 /* Index of first unused Lisp_Cons in the current block. */
2019
2020 int cons_block_index;
2021
2022 /* Free-list of Lisp_Cons structures. */
2023
2024 struct Lisp_Cons *cons_free_list;
2025
2026 /* Total number of cons blocks now in use. */
2027
2028 int n_cons_blocks;
2029
2030
2031 /* Initialize cons allocation. */
2032
2033 void
2034 init_cons ()
2035 {
2036 cons_block = (struct cons_block *) lisp_malloc (sizeof *cons_block,
2037 MEM_TYPE_CONS);
2038 cons_block->next = 0;
2039 bzero ((char *) cons_block->conses, sizeof cons_block->conses);
2040 cons_block_index = 0;
2041 cons_free_list = 0;
2042 n_cons_blocks = 1;
2043 }
2044
2045
2046 /* Explicitly free a cons cell by putting it on the free-list. */
2047
2048 void
2049 free_cons (ptr)
2050 struct Lisp_Cons *ptr;
2051 {
2052 *(struct Lisp_Cons **)&ptr->cdr = cons_free_list;
2053 #if GC_MARK_STACK
2054 ptr->car = Vdead;
2055 #endif
2056 cons_free_list = ptr;
2057 }
2058
2059
2060 DEFUN ("cons", Fcons, Scons, 2, 2, 0,
2061 /* Create a new cons, give it CAR and CDR as components, and return it. */
2062 (car, cdr))
2063 Lisp_Object car, cdr;
2064 {
2065 register Lisp_Object val;
2066
2067 if (cons_free_list)
2068 {
2069 /* We use the cdr for chaining the free list
2070 so that we won't use the same field that has the mark bit. */
2071 XSETCONS (val, cons_free_list);
2072 cons_free_list = *(struct Lisp_Cons **)&cons_free_list->cdr;
2073 }
2074 else
2075 {
2076 if (cons_block_index == CONS_BLOCK_SIZE)
2077 {
2078 register struct cons_block *new;
2079 new = (struct cons_block *) lisp_malloc (sizeof *new,
2080 MEM_TYPE_CONS);
2081 VALIDATE_LISP_STORAGE (new, sizeof *new);
2082 new->next = cons_block;
2083 cons_block = new;
2084 cons_block_index = 0;
2085 n_cons_blocks++;
2086 }
2087 XSETCONS (val, &cons_block->conses[cons_block_index++]);
2088 }
2089
2090 XCAR (val) = car;
2091 XCDR (val) = cdr;
2092 consing_since_gc += sizeof (struct Lisp_Cons);
2093 cons_cells_consed++;
2094 return val;
2095 }
2096
2097
2098 /* Make a list of 2, 3, 4 or 5 specified objects. */
2099
2100 Lisp_Object
2101 list2 (arg1, arg2)
2102 Lisp_Object arg1, arg2;
2103 {
2104 return Fcons (arg1, Fcons (arg2, Qnil));
2105 }
2106
2107
2108 Lisp_Object
2109 list3 (arg1, arg2, arg3)
2110 Lisp_Object arg1, arg2, arg3;
2111 {
2112 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Qnil)));
2113 }
2114
2115
2116 Lisp_Object
2117 list4 (arg1, arg2, arg3, arg4)
2118 Lisp_Object arg1, arg2, arg3, arg4;
2119 {
2120 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4, Qnil))));
2121 }
2122
2123
2124 Lisp_Object
2125 list5 (arg1, arg2, arg3, arg4, arg5)
2126 Lisp_Object arg1, arg2, arg3, arg4, arg5;
2127 {
2128 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4,
2129 Fcons (arg5, Qnil)))));
2130 }
2131
2132
2133 DEFUN ("list", Flist, Slist, 0, MANY, 0,
2134 /* Return a newly created list with specified arguments as elements.
2135 Any number of arguments, even zero arguments, are allowed. */
2136 (nargs, args))
2137 int nargs;
2138 register Lisp_Object *args;
2139 {
2140 register Lisp_Object val;
2141 val = Qnil;
2142
2143 while (nargs > 0)
2144 {
2145 nargs--;
2146 val = Fcons (args[nargs], val);
2147 }
2148 return val;
2149 }
2150
2151
2152 DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0,
2153 /* Return a newly created list of length LENGTH, with each element being INIT. */
2154 (length, init))
2155 register Lisp_Object length, init;
2156 {
2157 register Lisp_Object val;
2158 register int size;
2159
2160 CHECK_NATNUM (length, 0);
2161 size = XFASTINT (length);
2162
2163 val = Qnil;
2164 while (size > 0)
2165 {
2166 val = Fcons (init, val);
2167 --size;
2168
2169 if (size > 0)
2170 {
2171 val = Fcons (init, val);
2172 --size;
2173
2174 if (size > 0)
2175 {
2176 val = Fcons (init, val);
2177 --size;
2178
2179 if (size > 0)
2180 {
2181 val = Fcons (init, val);
2182 --size;
2183
2184 if (size > 0)
2185 {
2186 val = Fcons (init, val);
2187 --size;
2188 }
2189 }
2190 }
2191 }
2192
2193 QUIT;
2194 }
2195
2196 return val;
2197 }
2198
2199
2200 \f
2201 /***********************************************************************
2202 Vector Allocation
2203 ***********************************************************************/
2204
2205 /* Singly-linked list of all vectors. */
2206
2207 struct Lisp_Vector *all_vectors;
2208
2209 /* Total number of vector-like objects now in use. */
2210
2211 int n_vectors;
2212
2213
2214 /* Value is a pointer to a newly allocated Lisp_Vector structure
2215 with room for LEN Lisp_Objects. */
2216
2217 static struct Lisp_Vector *
2218 allocate_vectorlike (len, type)
2219 EMACS_INT len;
2220 enum mem_type type;
2221 {
2222 struct Lisp_Vector *p;
2223 size_t nbytes;
2224
2225 #ifdef DOUG_LEA_MALLOC
2226 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed
2227 because mapped region contents are not preserved in
2228 a dumped Emacs. */
2229 mallopt (M_MMAP_MAX, 0);
2230 #endif
2231
2232 nbytes = sizeof *p + (len - 1) * sizeof p->contents[0];
2233 p = (struct Lisp_Vector *) lisp_malloc (nbytes, type);
2234
2235 #ifdef DOUG_LEA_MALLOC
2236 /* Back to a reasonable maximum of mmap'ed areas. */
2237 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
2238 #endif
2239
2240 VALIDATE_LISP_STORAGE (p, 0);
2241 consing_since_gc += nbytes;
2242 vector_cells_consed += len;
2243
2244 p->next = all_vectors;
2245 all_vectors = p;
2246 ++n_vectors;
2247 return p;
2248 }
2249
2250
2251 /* Allocate a vector with NSLOTS slots. */
2252
2253 struct Lisp_Vector *
2254 allocate_vector (nslots)
2255 EMACS_INT nslots;
2256 {
2257 struct Lisp_Vector *v = allocate_vectorlike (nslots, MEM_TYPE_VECTOR);
2258 v->size = nslots;
2259 return v;
2260 }
2261
2262
2263 /* Allocate other vector-like structures. */
2264
2265 struct Lisp_Hash_Table *
2266 allocate_hash_table ()
2267 {
2268 EMACS_INT len = VECSIZE (struct Lisp_Hash_Table);
2269 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_HASH_TABLE);
2270 EMACS_INT i;
2271
2272 v->size = len;
2273 for (i = 0; i < len; ++i)
2274 v->contents[i] = Qnil;
2275
2276 return (struct Lisp_Hash_Table *) v;
2277 }
2278
2279
2280 struct window *
2281 allocate_window ()
2282 {
2283 EMACS_INT len = VECSIZE (struct window);
2284 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_WINDOW);
2285 EMACS_INT i;
2286
2287 for (i = 0; i < len; ++i)
2288 v->contents[i] = Qnil;
2289 v->size = len;
2290
2291 return (struct window *) v;
2292 }
2293
2294
2295 struct frame *
2296 allocate_frame ()
2297 {
2298 EMACS_INT len = VECSIZE (struct frame);
2299 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_FRAME);
2300 EMACS_INT i;
2301
2302 for (i = 0; i < len; ++i)
2303 v->contents[i] = make_number (0);
2304 v->size = len;
2305 return (struct frame *) v;
2306 }
2307
2308
2309 struct Lisp_Process *
2310 allocate_process ()
2311 {
2312 EMACS_INT len = VECSIZE (struct Lisp_Process);
2313 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_PROCESS);
2314 EMACS_INT i;
2315
2316 for (i = 0; i < len; ++i)
2317 v->contents[i] = Qnil;
2318 v->size = len;
2319
2320 return (struct Lisp_Process *) v;
2321 }
2322
2323
2324 struct Lisp_Vector *
2325 allocate_other_vector (len)
2326 EMACS_INT len;
2327 {
2328 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_VECTOR);
2329 EMACS_INT i;
2330
2331 for (i = 0; i < len; ++i)
2332 v->contents[i] = Qnil;
2333 v->size = len;
2334
2335 return v;
2336 }
2337
2338
2339 DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0,
2340 /* Return a newly created vector of length LENGTH, with each element being INIT.
2341 See also the function `vector'. */
2342 (length, init))
2343 register Lisp_Object length, init;
2344 {
2345 Lisp_Object vector;
2346 register EMACS_INT sizei;
2347 register int index;
2348 register struct Lisp_Vector *p;
2349
2350 CHECK_NATNUM (length, 0);
2351 sizei = XFASTINT (length);
2352
2353 p = allocate_vector (sizei);
2354 for (index = 0; index < sizei; index++)
2355 p->contents[index] = init;
2356
2357 XSETVECTOR (vector, p);
2358 return vector;
2359 }
2360
2361
2362 DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0,
2363 /* Return a newly created char-table, with purpose PURPOSE.
2364 Each element is initialized to INIT, which defaults to nil.
2365 PURPOSE should be a symbol which has a `char-table-extra-slots' property.
2366 The property's value should be an integer between 0 and 10. */
2367 (purpose, init))
2368 register Lisp_Object purpose, init;
2369 {
2370 Lisp_Object vector;
2371 Lisp_Object n;
2372 CHECK_SYMBOL (purpose, 1);
2373 n = Fget (purpose, Qchar_table_extra_slots);
2374 CHECK_NUMBER (n, 0);
2375 if (XINT (n) < 0 || XINT (n) > 10)
2376 args_out_of_range (n, Qnil);
2377 /* Add 2 to the size for the defalt and parent slots. */
2378 vector = Fmake_vector (make_number (CHAR_TABLE_STANDARD_SLOTS + XINT (n)),
2379 init);
2380 XCHAR_TABLE (vector)->top = Qt;
2381 XCHAR_TABLE (vector)->parent = Qnil;
2382 XCHAR_TABLE (vector)->purpose = purpose;
2383 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
2384 return vector;
2385 }
2386
2387
2388 /* Return a newly created sub char table with default value DEFALT.
2389 Since a sub char table does not appear as a top level Emacs Lisp
2390 object, we don't need a Lisp interface to make it. */
2391
2392 Lisp_Object
2393 make_sub_char_table (defalt)
2394 Lisp_Object defalt;
2395 {
2396 Lisp_Object vector
2397 = Fmake_vector (make_number (SUB_CHAR_TABLE_STANDARD_SLOTS), Qnil);
2398 XCHAR_TABLE (vector)->top = Qnil;
2399 XCHAR_TABLE (vector)->defalt = defalt;
2400 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
2401 return vector;
2402 }
2403
2404
2405 DEFUN ("vector", Fvector, Svector, 0, MANY, 0,
2406 /* Return a newly created vector with specified arguments as elements.
2407 Any number of arguments, even zero arguments, are allowed. */
2408 (nargs, args))
2409 register int nargs;
2410 Lisp_Object *args;
2411 {
2412 register Lisp_Object len, val;
2413 register int index;
2414 register struct Lisp_Vector *p;
2415
2416 XSETFASTINT (len, nargs);
2417 val = Fmake_vector (len, Qnil);
2418 p = XVECTOR (val);
2419 for (index = 0; index < nargs; index++)
2420 p->contents[index] = args[index];
2421 return val;
2422 }
2423
2424
2425 DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0,
2426 /* Create a byte-code object with specified arguments as elements.
2427 The arguments should be the arglist, bytecode-string, constant vector,
2428 stack size, (optional) doc string, and (optional) interactive spec.
2429 The first four arguments are required; at most six have any
2430 significance. */
2431 (nargs, args))
2432 register int nargs;
2433 Lisp_Object *args;
2434 {
2435 register Lisp_Object len, val;
2436 register int index;
2437 register struct Lisp_Vector *p;
2438
2439 XSETFASTINT (len, nargs);
2440 if (!NILP (Vpurify_flag))
2441 val = make_pure_vector ((EMACS_INT) nargs);
2442 else
2443 val = Fmake_vector (len, Qnil);
2444
2445 if (STRINGP (args[1]) && STRING_MULTIBYTE (args[1]))
2446 /* BYTECODE-STRING must have been produced by Emacs 20.2 or the
2447 earlier because they produced a raw 8-bit string for byte-code
2448 and now such a byte-code string is loaded as multibyte while
2449 raw 8-bit characters converted to multibyte form. Thus, now we
2450 must convert them back to the original unibyte form. */
2451 args[1] = Fstring_as_unibyte (args[1]);
2452
2453 p = XVECTOR (val);
2454 for (index = 0; index < nargs; index++)
2455 {
2456 if (!NILP (Vpurify_flag))
2457 args[index] = Fpurecopy (args[index]);
2458 p->contents[index] = args[index];
2459 }
2460 XSETCOMPILED (val, p);
2461 return val;
2462 }
2463
2464
2465 \f
2466 /***********************************************************************
2467 Symbol Allocation
2468 ***********************************************************************/
2469
2470 /* Each symbol_block is just under 1020 bytes long, since malloc
2471 really allocates in units of powers of two and uses 4 bytes for its
2472 own overhead. */
2473
2474 #define SYMBOL_BLOCK_SIZE \
2475 ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol))
2476
2477 struct symbol_block
2478 {
2479 struct symbol_block *next;
2480 struct Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE];
2481 };
2482
2483 /* Current symbol block and index of first unused Lisp_Symbol
2484 structure in it. */
2485
2486 struct symbol_block *symbol_block;
2487 int symbol_block_index;
2488
2489 /* List of free symbols. */
2490
2491 struct Lisp_Symbol *symbol_free_list;
2492
2493 /* Total number of symbol blocks now in use. */
2494
2495 int n_symbol_blocks;
2496
2497
2498 /* Initialize symbol allocation. */
2499
2500 void
2501 init_symbol ()
2502 {
2503 symbol_block = (struct symbol_block *) lisp_malloc (sizeof *symbol_block,
2504 MEM_TYPE_SYMBOL);
2505 symbol_block->next = 0;
2506 bzero ((char *) symbol_block->symbols, sizeof symbol_block->symbols);
2507 symbol_block_index = 0;
2508 symbol_free_list = 0;
2509 n_symbol_blocks = 1;
2510 }
2511
2512
2513 DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0,
2514 /* Return a newly allocated uninterned symbol whose name is NAME.
2515 Its value and function definition are void, and its property list is nil. */
2516 (name))
2517 Lisp_Object name;
2518 {
2519 register Lisp_Object val;
2520 register struct Lisp_Symbol *p;
2521
2522 CHECK_STRING (name, 0);
2523
2524 if (symbol_free_list)
2525 {
2526 XSETSYMBOL (val, symbol_free_list);
2527 symbol_free_list = *(struct Lisp_Symbol **)&symbol_free_list->value;
2528 }
2529 else
2530 {
2531 if (symbol_block_index == SYMBOL_BLOCK_SIZE)
2532 {
2533 struct symbol_block *new;
2534 new = (struct symbol_block *) lisp_malloc (sizeof *new,
2535 MEM_TYPE_SYMBOL);
2536 VALIDATE_LISP_STORAGE (new, sizeof *new);
2537 new->next = symbol_block;
2538 symbol_block = new;
2539 symbol_block_index = 0;
2540 n_symbol_blocks++;
2541 }
2542 XSETSYMBOL (val, &symbol_block->symbols[symbol_block_index++]);
2543 }
2544
2545 p = XSYMBOL (val);
2546 p->name = XSTRING (name);
2547 p->plist = Qnil;
2548 p->value = Qunbound;
2549 p->function = Qunbound;
2550 p->next = NULL;
2551 p->interned = SYMBOL_UNINTERNED;
2552 p->constant = 0;
2553 p->indirect_variable = 0;
2554 consing_since_gc += sizeof (struct Lisp_Symbol);
2555 symbols_consed++;
2556 return val;
2557 }
2558
2559
2560 \f
2561 /***********************************************************************
2562 Marker (Misc) Allocation
2563 ***********************************************************************/
2564
2565 /* Allocation of markers and other objects that share that structure.
2566 Works like allocation of conses. */
2567
2568 #define MARKER_BLOCK_SIZE \
2569 ((1020 - sizeof (struct marker_block *)) / sizeof (union Lisp_Misc))
2570
2571 struct marker_block
2572 {
2573 struct marker_block *next;
2574 union Lisp_Misc markers[MARKER_BLOCK_SIZE];
2575 };
2576
2577 struct marker_block *marker_block;
2578 int marker_block_index;
2579
2580 union Lisp_Misc *marker_free_list;
2581
2582 /* Total number of marker blocks now in use. */
2583
2584 int n_marker_blocks;
2585
2586 void
2587 init_marker ()
2588 {
2589 marker_block = (struct marker_block *) lisp_malloc (sizeof *marker_block,
2590 MEM_TYPE_MISC);
2591 marker_block->next = 0;
2592 bzero ((char *) marker_block->markers, sizeof marker_block->markers);
2593 marker_block_index = 0;
2594 marker_free_list = 0;
2595 n_marker_blocks = 1;
2596 }
2597
2598 /* Return a newly allocated Lisp_Misc object, with no substructure. */
2599
2600 Lisp_Object
2601 allocate_misc ()
2602 {
2603 Lisp_Object val;
2604
2605 if (marker_free_list)
2606 {
2607 XSETMISC (val, marker_free_list);
2608 marker_free_list = marker_free_list->u_free.chain;
2609 }
2610 else
2611 {
2612 if (marker_block_index == MARKER_BLOCK_SIZE)
2613 {
2614 struct marker_block *new;
2615 new = (struct marker_block *) lisp_malloc (sizeof *new,
2616 MEM_TYPE_MISC);
2617 VALIDATE_LISP_STORAGE (new, sizeof *new);
2618 new->next = marker_block;
2619 marker_block = new;
2620 marker_block_index = 0;
2621 n_marker_blocks++;
2622 }
2623 XSETMISC (val, &marker_block->markers[marker_block_index++]);
2624 }
2625
2626 consing_since_gc += sizeof (union Lisp_Misc);
2627 misc_objects_consed++;
2628 return val;
2629 }
2630
2631 DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
2632 /* Return a newly allocated marker which does not point at any place. */
2633 ())
2634 {
2635 register Lisp_Object val;
2636 register struct Lisp_Marker *p;
2637
2638 val = allocate_misc ();
2639 XMISCTYPE (val) = Lisp_Misc_Marker;
2640 p = XMARKER (val);
2641 p->buffer = 0;
2642 p->bytepos = 0;
2643 p->charpos = 0;
2644 p->chain = Qnil;
2645 p->insertion_type = 0;
2646 return val;
2647 }
2648
2649 /* Put MARKER back on the free list after using it temporarily. */
2650
2651 void
2652 free_marker (marker)
2653 Lisp_Object marker;
2654 {
2655 unchain_marker (marker);
2656
2657 XMISC (marker)->u_marker.type = Lisp_Misc_Free;
2658 XMISC (marker)->u_free.chain = marker_free_list;
2659 marker_free_list = XMISC (marker);
2660
2661 total_free_markers++;
2662 }
2663
2664 \f
2665 /* Return a newly created vector or string with specified arguments as
2666 elements. If all the arguments are characters that can fit
2667 in a string of events, make a string; otherwise, make a vector.
2668
2669 Any number of arguments, even zero arguments, are allowed. */
2670
2671 Lisp_Object
2672 make_event_array (nargs, args)
2673 register int nargs;
2674 Lisp_Object *args;
2675 {
2676 int i;
2677
2678 for (i = 0; i < nargs; i++)
2679 /* The things that fit in a string
2680 are characters that are in 0...127,
2681 after discarding the meta bit and all the bits above it. */
2682 if (!INTEGERP (args[i])
2683 || (XUINT (args[i]) & ~(-CHAR_META)) >= 0200)
2684 return Fvector (nargs, args);
2685
2686 /* Since the loop exited, we know that all the things in it are
2687 characters, so we can make a string. */
2688 {
2689 Lisp_Object result;
2690
2691 result = Fmake_string (make_number (nargs), make_number (0));
2692 for (i = 0; i < nargs; i++)
2693 {
2694 XSTRING (result)->data[i] = XINT (args[i]);
2695 /* Move the meta bit to the right place for a string char. */
2696 if (XINT (args[i]) & CHAR_META)
2697 XSTRING (result)->data[i] |= 0x80;
2698 }
2699
2700 return result;
2701 }
2702 }
2703
2704
2705 \f
2706 /************************************************************************
2707 C Stack Marking
2708 ************************************************************************/
2709
2710 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
2711
2712 /* Initialize this part of alloc.c. */
2713
2714 static void
2715 mem_init ()
2716 {
2717 mem_z.left = mem_z.right = MEM_NIL;
2718 mem_z.parent = NULL;
2719 mem_z.color = MEM_BLACK;
2720 mem_z.start = mem_z.end = NULL;
2721 mem_root = MEM_NIL;
2722 }
2723
2724
2725 /* Value is a pointer to the mem_node containing START. Value is
2726 MEM_NIL if there is no node in the tree containing START. */
2727
2728 static INLINE struct mem_node *
2729 mem_find (start)
2730 void *start;
2731 {
2732 struct mem_node *p;
2733
2734 if (start < min_heap_address || start > max_heap_address)
2735 return MEM_NIL;
2736
2737 /* Make the search always successful to speed up the loop below. */
2738 mem_z.start = start;
2739 mem_z.end = (char *) start + 1;
2740
2741 p = mem_root;
2742 while (start < p->start || start >= p->end)
2743 p = start < p->start ? p->left : p->right;
2744 return p;
2745 }
2746
2747
2748 /* Insert a new node into the tree for a block of memory with start
2749 address START, end address END, and type TYPE. Value is a
2750 pointer to the node that was inserted. */
2751
2752 static struct mem_node *
2753 mem_insert (start, end, type)
2754 void *start, *end;
2755 enum mem_type type;
2756 {
2757 struct mem_node *c, *parent, *x;
2758
2759 if (start < min_heap_address)
2760 min_heap_address = start;
2761 if (end > max_heap_address)
2762 max_heap_address = end;
2763
2764 /* See where in the tree a node for START belongs. In this
2765 particular application, it shouldn't happen that a node is already
2766 present. For debugging purposes, let's check that. */
2767 c = mem_root;
2768 parent = NULL;
2769
2770 #if GC_MARK_STACK != GC_MAKE_GCPROS_NOOPS
2771
2772 while (c != MEM_NIL)
2773 {
2774 if (start >= c->start && start < c->end)
2775 abort ();
2776 parent = c;
2777 c = start < c->start ? c->left : c->right;
2778 }
2779
2780 #else /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
2781
2782 while (c != MEM_NIL)
2783 {
2784 parent = c;
2785 c = start < c->start ? c->left : c->right;
2786 }
2787
2788 #endif /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
2789
2790 /* Create a new node. */
2791 #ifdef GC_MALLOC_CHECK
2792 x = (struct mem_node *) _malloc_internal (sizeof *x);
2793 if (x == NULL)
2794 abort ();
2795 #else
2796 x = (struct mem_node *) xmalloc (sizeof *x);
2797 #endif
2798 x->start = start;
2799 x->end = end;
2800 x->type = type;
2801 x->parent = parent;
2802 x->left = x->right = MEM_NIL;
2803 x->color = MEM_RED;
2804
2805 /* Insert it as child of PARENT or install it as root. */
2806 if (parent)
2807 {
2808 if (start < parent->start)
2809 parent->left = x;
2810 else
2811 parent->right = x;
2812 }
2813 else
2814 mem_root = x;
2815
2816 /* Re-establish red-black tree properties. */
2817 mem_insert_fixup (x);
2818
2819 return x;
2820 }
2821
2822
2823 /* Re-establish the red-black properties of the tree, and thereby
2824 balance the tree, after node X has been inserted; X is always red. */
2825
2826 static void
2827 mem_insert_fixup (x)
2828 struct mem_node *x;
2829 {
2830 while (x != mem_root && x->parent->color == MEM_RED)
2831 {
2832 /* X is red and its parent is red. This is a violation of
2833 red-black tree property #3. */
2834
2835 if (x->parent == x->parent->parent->left)
2836 {
2837 /* We're on the left side of our grandparent, and Y is our
2838 "uncle". */
2839 struct mem_node *y = x->parent->parent->right;
2840
2841 if (y->color == MEM_RED)
2842 {
2843 /* Uncle and parent are red but should be black because
2844 X is red. Change the colors accordingly and proceed
2845 with the grandparent. */
2846 x->parent->color = MEM_BLACK;
2847 y->color = MEM_BLACK;
2848 x->parent->parent->color = MEM_RED;
2849 x = x->parent->parent;
2850 }
2851 else
2852 {
2853 /* Parent and uncle have different colors; parent is
2854 red, uncle is black. */
2855 if (x == x->parent->right)
2856 {
2857 x = x->parent;
2858 mem_rotate_left (x);
2859 }
2860
2861 x->parent->color = MEM_BLACK;
2862 x->parent->parent->color = MEM_RED;
2863 mem_rotate_right (x->parent->parent);
2864 }
2865 }
2866 else
2867 {
2868 /* This is the symmetrical case of above. */
2869 struct mem_node *y = x->parent->parent->left;
2870
2871 if (y->color == MEM_RED)
2872 {
2873 x->parent->color = MEM_BLACK;
2874 y->color = MEM_BLACK;
2875 x->parent->parent->color = MEM_RED;
2876 x = x->parent->parent;
2877 }
2878 else
2879 {
2880 if (x == x->parent->left)
2881 {
2882 x = x->parent;
2883 mem_rotate_right (x);
2884 }
2885
2886 x->parent->color = MEM_BLACK;
2887 x->parent->parent->color = MEM_RED;
2888 mem_rotate_left (x->parent->parent);
2889 }
2890 }
2891 }
2892
2893 /* The root may have been changed to red due to the algorithm. Set
2894 it to black so that property #5 is satisfied. */
2895 mem_root->color = MEM_BLACK;
2896 }
2897
2898
2899 /* (x) (y)
2900 / \ / \
2901 a (y) ===> (x) c
2902 / \ / \
2903 b c a b */
2904
2905 static void
2906 mem_rotate_left (x)
2907 struct mem_node *x;
2908 {
2909 struct mem_node *y;
2910
2911 /* Turn y's left sub-tree into x's right sub-tree. */
2912 y = x->right;
2913 x->right = y->left;
2914 if (y->left != MEM_NIL)
2915 y->left->parent = x;
2916
2917 /* Y's parent was x's parent. */
2918 if (y != MEM_NIL)
2919 y->parent = x->parent;
2920
2921 /* Get the parent to point to y instead of x. */
2922 if (x->parent)
2923 {
2924 if (x == x->parent->left)
2925 x->parent->left = y;
2926 else
2927 x->parent->right = y;
2928 }
2929 else
2930 mem_root = y;
2931
2932 /* Put x on y's left. */
2933 y->left = x;
2934 if (x != MEM_NIL)
2935 x->parent = y;
2936 }
2937
2938
2939 /* (x) (Y)
2940 / \ / \
2941 (y) c ===> a (x)
2942 / \ / \
2943 a b b c */
2944
2945 static void
2946 mem_rotate_right (x)
2947 struct mem_node *x;
2948 {
2949 struct mem_node *y = x->left;
2950
2951 x->left = y->right;
2952 if (y->right != MEM_NIL)
2953 y->right->parent = x;
2954
2955 if (y != MEM_NIL)
2956 y->parent = x->parent;
2957 if (x->parent)
2958 {
2959 if (x == x->parent->right)
2960 x->parent->right = y;
2961 else
2962 x->parent->left = y;
2963 }
2964 else
2965 mem_root = y;
2966
2967 y->right = x;
2968 if (x != MEM_NIL)
2969 x->parent = y;
2970 }
2971
2972
2973 /* Delete node Z from the tree. If Z is null or MEM_NIL, do nothing. */
2974
2975 static void
2976 mem_delete (z)
2977 struct mem_node *z;
2978 {
2979 struct mem_node *x, *y;
2980
2981 if (!z || z == MEM_NIL)
2982 return;
2983
2984 if (z->left == MEM_NIL || z->right == MEM_NIL)
2985 y = z;
2986 else
2987 {
2988 y = z->right;
2989 while (y->left != MEM_NIL)
2990 y = y->left;
2991 }
2992
2993 if (y->left != MEM_NIL)
2994 x = y->left;
2995 else
2996 x = y->right;
2997
2998 x->parent = y->parent;
2999 if (y->parent)
3000 {
3001 if (y == y->parent->left)
3002 y->parent->left = x;
3003 else
3004 y->parent->right = x;
3005 }
3006 else
3007 mem_root = x;
3008
3009 if (y != z)
3010 {
3011 z->start = y->start;
3012 z->end = y->end;
3013 z->type = y->type;
3014 }
3015
3016 if (y->color == MEM_BLACK)
3017 mem_delete_fixup (x);
3018
3019 #ifdef GC_MALLOC_CHECK
3020 _free_internal (y);
3021 #else
3022 xfree (y);
3023 #endif
3024 }
3025
3026
3027 /* Re-establish the red-black properties of the tree, after a
3028 deletion. */
3029
3030 static void
3031 mem_delete_fixup (x)
3032 struct mem_node *x;
3033 {
3034 while (x != mem_root && x->color == MEM_BLACK)
3035 {
3036 if (x == x->parent->left)
3037 {
3038 struct mem_node *w = x->parent->right;
3039
3040 if (w->color == MEM_RED)
3041 {
3042 w->color = MEM_BLACK;
3043 x->parent->color = MEM_RED;
3044 mem_rotate_left (x->parent);
3045 w = x->parent->right;
3046 }
3047
3048 if (w->left->color == MEM_BLACK && w->right->color == MEM_BLACK)
3049 {
3050 w->color = MEM_RED;
3051 x = x->parent;
3052 }
3053 else
3054 {
3055 if (w->right->color == MEM_BLACK)
3056 {
3057 w->left->color = MEM_BLACK;
3058 w->color = MEM_RED;
3059 mem_rotate_right (w);
3060 w = x->parent->right;
3061 }
3062 w->color = x->parent->color;
3063 x->parent->color = MEM_BLACK;
3064 w->right->color = MEM_BLACK;
3065 mem_rotate_left (x->parent);
3066 x = mem_root;
3067 }
3068 }
3069 else
3070 {
3071 struct mem_node *w = x->parent->left;
3072
3073 if (w->color == MEM_RED)
3074 {
3075 w->color = MEM_BLACK;
3076 x->parent->color = MEM_RED;
3077 mem_rotate_right (x->parent);
3078 w = x->parent->left;
3079 }
3080
3081 if (w->right->color == MEM_BLACK && w->left->color == MEM_BLACK)
3082 {
3083 w->color = MEM_RED;
3084 x = x->parent;
3085 }
3086 else
3087 {
3088 if (w->left->color == MEM_BLACK)
3089 {
3090 w->right->color = MEM_BLACK;
3091 w->color = MEM_RED;
3092 mem_rotate_left (w);
3093 w = x->parent->left;
3094 }
3095
3096 w->color = x->parent->color;
3097 x->parent->color = MEM_BLACK;
3098 w->left->color = MEM_BLACK;
3099 mem_rotate_right (x->parent);
3100 x = mem_root;
3101 }
3102 }
3103 }
3104
3105 x->color = MEM_BLACK;
3106 }
3107
3108
3109 /* Value is non-zero if P is a pointer to a live Lisp string on
3110 the heap. M is a pointer to the mem_block for P. */
3111
3112 static INLINE int
3113 live_string_p (m, p)
3114 struct mem_node *m;
3115 void *p;
3116 {
3117 if (m->type == MEM_TYPE_STRING)
3118 {
3119 struct string_block *b = (struct string_block *) m->start;
3120 int offset = (char *) p - (char *) &b->strings[0];
3121
3122 /* P must point to the start of a Lisp_String structure, and it
3123 must not be on the free-list. */
3124 return (offset >= 0
3125 && offset % sizeof b->strings[0] == 0
3126 && ((struct Lisp_String *) p)->data != NULL);
3127 }
3128 else
3129 return 0;
3130 }
3131
3132
3133 /* Value is non-zero if P is a pointer to a live Lisp cons on
3134 the heap. M is a pointer to the mem_block for P. */
3135
3136 static INLINE int
3137 live_cons_p (m, p)
3138 struct mem_node *m;
3139 void *p;
3140 {
3141 if (m->type == MEM_TYPE_CONS)
3142 {
3143 struct cons_block *b = (struct cons_block *) m->start;
3144 int offset = (char *) p - (char *) &b->conses[0];
3145
3146 /* P must point to the start of a Lisp_Cons, not be
3147 one of the unused cells in the current cons block,
3148 and not be on the free-list. */
3149 return (offset >= 0
3150 && offset % sizeof b->conses[0] == 0
3151 && (b != cons_block
3152 || offset / sizeof b->conses[0] < cons_block_index)
3153 && !EQ (((struct Lisp_Cons *) p)->car, Vdead));
3154 }
3155 else
3156 return 0;
3157 }
3158
3159
3160 /* Value is non-zero if P is a pointer to a live Lisp symbol on
3161 the heap. M is a pointer to the mem_block for P. */
3162
3163 static INLINE int
3164 live_symbol_p (m, p)
3165 struct mem_node *m;
3166 void *p;
3167 {
3168 if (m->type == MEM_TYPE_SYMBOL)
3169 {
3170 struct symbol_block *b = (struct symbol_block *) m->start;
3171 int offset = (char *) p - (char *) &b->symbols[0];
3172
3173 /* P must point to the start of a Lisp_Symbol, not be
3174 one of the unused cells in the current symbol block,
3175 and not be on the free-list. */
3176 return (offset >= 0
3177 && offset % sizeof b->symbols[0] == 0
3178 && (b != symbol_block
3179 || offset / sizeof b->symbols[0] < symbol_block_index)
3180 && !EQ (((struct Lisp_Symbol *) p)->function, Vdead));
3181 }
3182 else
3183 return 0;
3184 }
3185
3186
3187 /* Value is non-zero if P is a pointer to a live Lisp float on
3188 the heap. M is a pointer to the mem_block for P. */
3189
3190 static INLINE int
3191 live_float_p (m, p)
3192 struct mem_node *m;
3193 void *p;
3194 {
3195 if (m->type == MEM_TYPE_FLOAT)
3196 {
3197 struct float_block *b = (struct float_block *) m->start;
3198 int offset = (char *) p - (char *) &b->floats[0];
3199
3200 /* P must point to the start of a Lisp_Float, not be
3201 one of the unused cells in the current float block,
3202 and not be on the free-list. */
3203 return (offset >= 0
3204 && offset % sizeof b->floats[0] == 0
3205 && (b != float_block
3206 || offset / sizeof b->floats[0] < float_block_index)
3207 && !EQ (((struct Lisp_Float *) p)->type, Vdead));
3208 }
3209 else
3210 return 0;
3211 }
3212
3213
3214 /* Value is non-zero if P is a pointer to a live Lisp Misc on
3215 the heap. M is a pointer to the mem_block for P. */
3216
3217 static INLINE int
3218 live_misc_p (m, p)
3219 struct mem_node *m;
3220 void *p;
3221 {
3222 if (m->type == MEM_TYPE_MISC)
3223 {
3224 struct marker_block *b = (struct marker_block *) m->start;
3225 int offset = (char *) p - (char *) &b->markers[0];
3226
3227 /* P must point to the start of a Lisp_Misc, not be
3228 one of the unused cells in the current misc block,
3229 and not be on the free-list. */
3230 return (offset >= 0
3231 && offset % sizeof b->markers[0] == 0
3232 && (b != marker_block
3233 || offset / sizeof b->markers[0] < marker_block_index)
3234 && ((union Lisp_Misc *) p)->u_marker.type != Lisp_Misc_Free);
3235 }
3236 else
3237 return 0;
3238 }
3239
3240
3241 /* Value is non-zero if P is a pointer to a live vector-like object.
3242 M is a pointer to the mem_block for P. */
3243
3244 static INLINE int
3245 live_vector_p (m, p)
3246 struct mem_node *m;
3247 void *p;
3248 {
3249 return (p == m->start
3250 && m->type >= MEM_TYPE_VECTOR
3251 && m->type <= MEM_TYPE_WINDOW);
3252 }
3253
3254
3255 /* Value is non-zero of P is a pointer to a live buffer. M is a
3256 pointer to the mem_block for P. */
3257
3258 static INLINE int
3259 live_buffer_p (m, p)
3260 struct mem_node *m;
3261 void *p;
3262 {
3263 /* P must point to the start of the block, and the buffer
3264 must not have been killed. */
3265 return (m->type == MEM_TYPE_BUFFER
3266 && p == m->start
3267 && !NILP (((struct buffer *) p)->name));
3268 }
3269
3270 #endif /* GC_MARK_STACK || defined GC_MALLOC_CHECK */
3271
3272 #if GC_MARK_STACK
3273
3274 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3275
3276 /* Array of objects that are kept alive because the C stack contains
3277 a pattern that looks like a reference to them . */
3278
3279 #define MAX_ZOMBIES 10
3280 static Lisp_Object zombies[MAX_ZOMBIES];
3281
3282 /* Number of zombie objects. */
3283
3284 static int nzombies;
3285
3286 /* Number of garbage collections. */
3287
3288 static int ngcs;
3289
3290 /* Average percentage of zombies per collection. */
3291
3292 static double avg_zombies;
3293
3294 /* Max. number of live and zombie objects. */
3295
3296 static int max_live, max_zombies;
3297
3298 /* Average number of live objects per GC. */
3299
3300 static double avg_live;
3301
3302 DEFUN ("gc-status", Fgc_status, Sgc_status, 0, 0, "",
3303 /* Show information about live and zombie objects. */
3304 ())
3305 {
3306 Lisp_Object args[7];
3307 args[0] = build_string ("%d GCs, avg live/zombies = %.2f/%.2f (%f%%), max %d/%d");
3308 args[1] = make_number (ngcs);
3309 args[2] = make_float (avg_live);
3310 args[3] = make_float (avg_zombies);
3311 args[4] = make_float (avg_zombies / avg_live / 100);
3312 args[5] = make_number (max_live);
3313 args[6] = make_number (max_zombies);
3314 return Fmessage (7, args);
3315 }
3316
3317 #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
3318
3319
3320 /* Mark OBJ if we can prove it's a Lisp_Object. */
3321
3322 static INLINE void
3323 mark_maybe_object (obj)
3324 Lisp_Object obj;
3325 {
3326 void *po = (void *) XPNTR (obj);
3327 struct mem_node *m = mem_find (po);
3328
3329 if (m != MEM_NIL)
3330 {
3331 int mark_p = 0;
3332
3333 switch (XGCTYPE (obj))
3334 {
3335 case Lisp_String:
3336 mark_p = (live_string_p (m, po)
3337 && !STRING_MARKED_P ((struct Lisp_String *) po));
3338 break;
3339
3340 case Lisp_Cons:
3341 mark_p = (live_cons_p (m, po)
3342 && !XMARKBIT (XCONS (obj)->car));
3343 break;
3344
3345 case Lisp_Symbol:
3346 mark_p = (live_symbol_p (m, po)
3347 && !XMARKBIT (XSYMBOL (obj)->plist));
3348 break;
3349
3350 case Lisp_Float:
3351 mark_p = (live_float_p (m, po)
3352 && !XMARKBIT (XFLOAT (obj)->type));
3353 break;
3354
3355 case Lisp_Vectorlike:
3356 /* Note: can't check GC_BUFFERP before we know it's a
3357 buffer because checking that dereferences the pointer
3358 PO which might point anywhere. */
3359 if (live_vector_p (m, po))
3360 mark_p = (!GC_SUBRP (obj)
3361 && !(XVECTOR (obj)->size & ARRAY_MARK_FLAG));
3362 else if (live_buffer_p (m, po))
3363 mark_p = GC_BUFFERP (obj) && !XMARKBIT (XBUFFER (obj)->name);
3364 break;
3365
3366 case Lisp_Misc:
3367 if (live_misc_p (m, po))
3368 {
3369 switch (XMISCTYPE (obj))
3370 {
3371 case Lisp_Misc_Marker:
3372 mark_p = !XMARKBIT (XMARKER (obj)->chain);
3373 break;
3374
3375 case Lisp_Misc_Buffer_Local_Value:
3376 case Lisp_Misc_Some_Buffer_Local_Value:
3377 mark_p = !XMARKBIT (XBUFFER_LOCAL_VALUE (obj)->realvalue);
3378 break;
3379
3380 case Lisp_Misc_Overlay:
3381 mark_p = !XMARKBIT (XOVERLAY (obj)->plist);
3382 break;
3383 }
3384 }
3385 break;
3386
3387 case Lisp_Int:
3388 case Lisp_Type_Limit:
3389 break;
3390 }
3391
3392 if (mark_p)
3393 {
3394 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3395 if (nzombies < MAX_ZOMBIES)
3396 zombies[nzombies] = *p;
3397 ++nzombies;
3398 #endif
3399 mark_object (&obj);
3400 }
3401 }
3402 }
3403
3404
3405 /* If P points to Lisp data, mark that as live if it isn't already
3406 marked. */
3407
3408 static INLINE void
3409 mark_maybe_pointer (p)
3410 void *p;
3411 {
3412 struct mem_node *m;
3413
3414 /* Quickly rule out some values which can't point to Lisp data. We
3415 assume that Lisp data is aligned on even addresses. */
3416 if ((EMACS_INT) p & 1)
3417 return;
3418
3419 m = mem_find (p);
3420 if (m != MEM_NIL)
3421 {
3422 Lisp_Object obj = Qnil;
3423
3424 switch (m->type)
3425 {
3426 case MEM_TYPE_NON_LISP:
3427 /* Nothing to do; not a pointer to Lisp memory. */
3428 break;
3429
3430 case MEM_TYPE_BUFFER:
3431 if (live_buffer_p (m, p)
3432 && !XMARKBIT (((struct buffer *) p)->name))
3433 XSETVECTOR (obj, p);
3434 break;
3435
3436 case MEM_TYPE_CONS:
3437 if (live_cons_p (m, p)
3438 && !XMARKBIT (((struct Lisp_Cons *) p)->car))
3439 XSETCONS (obj, p);
3440 break;
3441
3442 case MEM_TYPE_STRING:
3443 if (live_string_p (m, p)
3444 && !STRING_MARKED_P ((struct Lisp_String *) p))
3445 XSETSTRING (obj, p);
3446 break;
3447
3448 case MEM_TYPE_MISC:
3449 if (live_misc_p (m, p))
3450 {
3451 Lisp_Object tem;
3452 XSETMISC (tem, p);
3453
3454 switch (XMISCTYPE (tem))
3455 {
3456 case Lisp_Misc_Marker:
3457 if (!XMARKBIT (XMARKER (tem)->chain))
3458 obj = tem;
3459 break;
3460
3461 case Lisp_Misc_Buffer_Local_Value:
3462 case Lisp_Misc_Some_Buffer_Local_Value:
3463 if (!XMARKBIT (XBUFFER_LOCAL_VALUE (tem)->realvalue))
3464 obj = tem;
3465 break;
3466
3467 case Lisp_Misc_Overlay:
3468 if (!XMARKBIT (XOVERLAY (tem)->plist))
3469 obj = tem;
3470 break;
3471 }
3472 }
3473 break;
3474
3475 case MEM_TYPE_SYMBOL:
3476 if (live_symbol_p (m, p)
3477 && !XMARKBIT (((struct Lisp_Symbol *) p)->plist))
3478 XSETSYMBOL (obj, p);
3479 break;
3480
3481 case MEM_TYPE_FLOAT:
3482 if (live_float_p (m, p)
3483 && !XMARKBIT (((struct Lisp_Float *) p)->type))
3484 XSETFLOAT (obj, p);
3485 break;
3486
3487 case MEM_TYPE_VECTOR:
3488 case MEM_TYPE_PROCESS:
3489 case MEM_TYPE_HASH_TABLE:
3490 case MEM_TYPE_FRAME:
3491 case MEM_TYPE_WINDOW:
3492 if (live_vector_p (m, p))
3493 {
3494 Lisp_Object tem;
3495 XSETVECTOR (tem, p);
3496 if (!GC_SUBRP (tem)
3497 && !(XVECTOR (tem)->size & ARRAY_MARK_FLAG))
3498 obj = tem;
3499 }
3500 break;
3501
3502 default:
3503 abort ();
3504 }
3505
3506 if (!GC_NILP (obj))
3507 mark_object (&obj);
3508 }
3509 }
3510
3511
3512 /* Mark Lisp objects referenced from the address range START..END. */
3513
3514 static void
3515 mark_memory (start, end)
3516 void *start, *end;
3517 {
3518 Lisp_Object *p;
3519 void **pp;
3520
3521 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3522 nzombies = 0;
3523 #endif
3524
3525 /* Make START the pointer to the start of the memory region,
3526 if it isn't already. */
3527 if (end < start)
3528 {
3529 void *tem = start;
3530 start = end;
3531 end = tem;
3532 }
3533
3534 /* Mark Lisp_Objects. */
3535 for (p = (Lisp_Object *) start; (void *) p < end; ++p)
3536 mark_maybe_object (*p);
3537
3538 /* Mark Lisp data pointed to. This is necessary because, in some
3539 situations, the C compiler optimizes Lisp objects away, so that
3540 only a pointer to them remains. Example:
3541
3542 DEFUN ("testme", Ftestme, Stestme, 0, 0, 0, "")
3543 ()
3544 {
3545 Lisp_Object obj = build_string ("test");
3546 struct Lisp_String *s = XSTRING (obj);
3547 Fgarbage_collect ();
3548 fprintf (stderr, "test `%s'\n", s->data);
3549 return Qnil;
3550 }
3551
3552 Here, `obj' isn't really used, and the compiler optimizes it
3553 away. The only reference to the life string is through the
3554 pointer `s'. */
3555
3556 for (pp = (void **) start; (void *) pp < end; ++pp)
3557 mark_maybe_pointer (*pp);
3558 }
3559
3560
3561 #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
3562
3563 static int setjmp_tested_p, longjmps_done;
3564
3565 #define SETJMP_WILL_LIKELY_WORK "\
3566 \n\
3567 Emacs garbage collector has been changed to use conservative stack\n\
3568 marking. Emacs has determined that the method it uses to do the\n\
3569 marking will likely work on your system, but this isn't sure.\n\
3570 \n\
3571 If you are a system-programmer, or can get the help of a local wizard\n\
3572 who is, please take a look at the function mark_stack in alloc.c, and\n\
3573 verify that the methods used are appropriate for your system.\n\
3574 \n\
3575 Please mail the result to <gerd@gnu.org>.\n\
3576 "
3577
3578 #define SETJMP_WILL_NOT_WORK "\
3579 \n\
3580 Emacs garbage collector has been changed to use conservative stack\n\
3581 marking. Emacs has determined that the default method it uses to do the\n\
3582 marking will not work on your system. We will need a system-dependent\n\
3583 solution for your system.\n\
3584 \n\
3585 Please take a look at the function mark_stack in alloc.c, and\n\
3586 try to find a way to make it work on your system.\n\
3587 Please mail the result to <gerd@gnu.org>.\n\
3588 "
3589
3590
3591 /* Perform a quick check if it looks like setjmp saves registers in a
3592 jmp_buf. Print a message to stderr saying so. When this test
3593 succeeds, this is _not_ a proof that setjmp is sufficient for
3594 conservative stack marking. Only the sources or a disassembly
3595 can prove that. */
3596
3597 static void
3598 test_setjmp ()
3599 {
3600 char buf[10];
3601 register int x;
3602 jmp_buf jbuf;
3603 int result = 0;
3604
3605 /* Arrange for X to be put in a register. */
3606 sprintf (buf, "1");
3607 x = strlen (buf);
3608 x = 2 * x - 1;
3609
3610 setjmp (jbuf);
3611 if (longjmps_done == 1)
3612 {
3613 /* Came here after the longjmp at the end of the function.
3614
3615 If x == 1, the longjmp has restored the register to its
3616 value before the setjmp, and we can hope that setjmp
3617 saves all such registers in the jmp_buf, although that
3618 isn't sure.
3619
3620 For other values of X, either something really strange is
3621 taking place, or the setjmp just didn't save the register. */
3622
3623 if (x == 1)
3624 fprintf (stderr, SETJMP_WILL_LIKELY_WORK);
3625 else
3626 {
3627 fprintf (stderr, SETJMP_WILL_NOT_WORK);
3628 exit (1);
3629 }
3630 }
3631
3632 ++longjmps_done;
3633 x = 2;
3634 if (longjmps_done == 1)
3635 longjmp (jbuf, 1);
3636 }
3637
3638 #endif /* not GC_SAVE_REGISTERS_ON_STACK && not GC_SETJMP_WORKS */
3639
3640
3641 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
3642
3643 /* Abort if anything GCPRO'd doesn't survive the GC. */
3644
3645 static void
3646 check_gcpros ()
3647 {
3648 struct gcpro *p;
3649 int i;
3650
3651 for (p = gcprolist; p; p = p->next)
3652 for (i = 0; i < p->nvars; ++i)
3653 if (!survives_gc_p (p->var[i]))
3654 abort ();
3655 }
3656
3657 #elif GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3658
3659 static void
3660 dump_zombies ()
3661 {
3662 int i;
3663
3664 fprintf (stderr, "\nZombies kept alive = %d:\n", nzombies);
3665 for (i = 0; i < min (MAX_ZOMBIES, nzombies); ++i)
3666 {
3667 fprintf (stderr, " %d = ", i);
3668 debug_print (zombies[i]);
3669 }
3670 }
3671
3672 #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
3673
3674
3675 /* Mark live Lisp objects on the C stack.
3676
3677 There are several system-dependent problems to consider when
3678 porting this to new architectures:
3679
3680 Processor Registers
3681
3682 We have to mark Lisp objects in CPU registers that can hold local
3683 variables or are used to pass parameters.
3684
3685 If GC_SAVE_REGISTERS_ON_STACK is defined, it should expand to
3686 something that either saves relevant registers on the stack, or
3687 calls mark_maybe_object passing it each register's contents.
3688
3689 If GC_SAVE_REGISTERS_ON_STACK is not defined, the current
3690 implementation assumes that calling setjmp saves registers we need
3691 to see in a jmp_buf which itself lies on the stack. This doesn't
3692 have to be true! It must be verified for each system, possibly
3693 by taking a look at the source code of setjmp.
3694
3695 Stack Layout
3696
3697 Architectures differ in the way their processor stack is organized.
3698 For example, the stack might look like this
3699
3700 +----------------+
3701 | Lisp_Object | size = 4
3702 +----------------+
3703 | something else | size = 2
3704 +----------------+
3705 | Lisp_Object | size = 4
3706 +----------------+
3707 | ... |
3708
3709 In such a case, not every Lisp_Object will be aligned equally. To
3710 find all Lisp_Object on the stack it won't be sufficient to walk
3711 the stack in steps of 4 bytes. Instead, two passes will be
3712 necessary, one starting at the start of the stack, and a second
3713 pass starting at the start of the stack + 2. Likewise, if the
3714 minimal alignment of Lisp_Objects on the stack is 1, four passes
3715 would be necessary, each one starting with one byte more offset
3716 from the stack start.
3717
3718 The current code assumes by default that Lisp_Objects are aligned
3719 equally on the stack. */
3720
3721 static void
3722 mark_stack ()
3723 {
3724 jmp_buf j;
3725 volatile int stack_grows_down_p = (char *) &j > (char *) stack_base;
3726 void *end;
3727
3728 /* This trick flushes the register windows so that all the state of
3729 the process is contained in the stack. */
3730 #ifdef sparc
3731 asm ("ta 3");
3732 #endif
3733
3734 /* Save registers that we need to see on the stack. We need to see
3735 registers used to hold register variables and registers used to
3736 pass parameters. */
3737 #ifdef GC_SAVE_REGISTERS_ON_STACK
3738 GC_SAVE_REGISTERS_ON_STACK (end);
3739 #else /* not GC_SAVE_REGISTERS_ON_STACK */
3740
3741 #ifndef GC_SETJMP_WORKS /* If it hasn't been checked yet that
3742 setjmp will definitely work, test it
3743 and print a message with the result
3744 of the test. */
3745 if (!setjmp_tested_p)
3746 {
3747 setjmp_tested_p = 1;
3748 test_setjmp ();
3749 }
3750 #endif /* GC_SETJMP_WORKS */
3751
3752 setjmp (j);
3753 end = stack_grows_down_p ? (char *) &j + sizeof j : (char *) &j;
3754 #endif /* not GC_SAVE_REGISTERS_ON_STACK */
3755
3756 /* This assumes that the stack is a contiguous region in memory. If
3757 that's not the case, something has to be done here to iterate
3758 over the stack segments. */
3759 #if GC_LISP_OBJECT_ALIGNMENT == 1
3760 mark_memory (stack_base, end);
3761 mark_memory ((char *) stack_base + 1, end);
3762 mark_memory ((char *) stack_base + 2, end);
3763 mark_memory ((char *) stack_base + 3, end);
3764 #elif GC_LISP_OBJECT_ALIGNMENT == 2
3765 mark_memory (stack_base, end);
3766 mark_memory ((char *) stack_base + 2, end);
3767 #else
3768 mark_memory (stack_base, end);
3769 #endif
3770
3771 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
3772 check_gcpros ();
3773 #endif
3774 }
3775
3776
3777 #endif /* GC_MARK_STACK != 0 */
3778
3779
3780 \f
3781 /***********************************************************************
3782 Pure Storage Management
3783 ***********************************************************************/
3784
3785 /* Allocate room for SIZE bytes from pure Lisp storage and return a
3786 pointer to it. TYPE is the Lisp type for which the memory is
3787 allocated. TYPE < 0 means it's not used for a Lisp object.
3788
3789 If store_pure_type_info is set and TYPE is >= 0, the type of
3790 the allocated object is recorded in pure_types. */
3791
3792 static POINTER_TYPE *
3793 pure_alloc (size, type)
3794 size_t size;
3795 int type;
3796 {
3797 size_t nbytes;
3798 POINTER_TYPE *result;
3799 char *beg = purebeg;
3800
3801 /* Give Lisp_Floats an extra alignment. */
3802 if (type == Lisp_Float)
3803 {
3804 size_t alignment;
3805 #if defined __GNUC__ && __GNUC__ >= 2
3806 alignment = __alignof (struct Lisp_Float);
3807 #else
3808 alignment = sizeof (struct Lisp_Float);
3809 #endif
3810 pure_bytes_used = ALIGN (pure_bytes_used, alignment);
3811 }
3812
3813 nbytes = ALIGN (size, sizeof (EMACS_INT));
3814
3815 if (pure_bytes_used + nbytes > pure_size)
3816 {
3817 beg = purebeg = (char *) xmalloc (PURESIZE);
3818 pure_size = PURESIZE;
3819 pure_bytes_used_before_overflow += pure_bytes_used;
3820 pure_bytes_used = 0;
3821 }
3822
3823 result = (POINTER_TYPE *) (beg + pure_bytes_used);
3824 pure_bytes_used += nbytes;
3825 return result;
3826 }
3827
3828
3829 /* Signal an error if PURESIZE is too small. */
3830
3831 void
3832 check_pure_size ()
3833 {
3834 if (pure_bytes_used_before_overflow)
3835 error ("Pure Lisp storage overflow (approx. %d bytes needed)",
3836 (int) (pure_bytes_used + pure_bytes_used_before_overflow));
3837 }
3838
3839
3840 /* Return a string allocated in pure space. DATA is a buffer holding
3841 NCHARS characters, and NBYTES bytes of string data. MULTIBYTE
3842 non-zero means make the result string multibyte.
3843
3844 Must get an error if pure storage is full, since if it cannot hold
3845 a large string it may be able to hold conses that point to that
3846 string; then the string is not protected from gc. */
3847
3848 Lisp_Object
3849 make_pure_string (data, nchars, nbytes, multibyte)
3850 char *data;
3851 int nchars, nbytes;
3852 int multibyte;
3853 {
3854 Lisp_Object string;
3855 struct Lisp_String *s;
3856
3857 s = (struct Lisp_String *) pure_alloc (sizeof *s, Lisp_String);
3858 s->data = (unsigned char *) pure_alloc (nbytes + 1, -1);
3859 s->size = nchars;
3860 s->size_byte = multibyte ? nbytes : -1;
3861 bcopy (data, s->data, nbytes);
3862 s->data[nbytes] = '\0';
3863 s->intervals = NULL_INTERVAL;
3864 XSETSTRING (string, s);
3865 return string;
3866 }
3867
3868
3869 /* Return a cons allocated from pure space. Give it pure copies
3870 of CAR as car and CDR as cdr. */
3871
3872 Lisp_Object
3873 pure_cons (car, cdr)
3874 Lisp_Object car, cdr;
3875 {
3876 register Lisp_Object new;
3877 struct Lisp_Cons *p;
3878
3879 p = (struct Lisp_Cons *) pure_alloc (sizeof *p, Lisp_Cons);
3880 XSETCONS (new, p);
3881 XCAR (new) = Fpurecopy (car);
3882 XCDR (new) = Fpurecopy (cdr);
3883 return new;
3884 }
3885
3886
3887 /* Value is a float object with value NUM allocated from pure space. */
3888
3889 Lisp_Object
3890 make_pure_float (num)
3891 double num;
3892 {
3893 register Lisp_Object new;
3894 struct Lisp_Float *p;
3895
3896 p = (struct Lisp_Float *) pure_alloc (sizeof *p, Lisp_Float);
3897 XSETFLOAT (new, p);
3898 XFLOAT_DATA (new) = num;
3899 return new;
3900 }
3901
3902
3903 /* Return a vector with room for LEN Lisp_Objects allocated from
3904 pure space. */
3905
3906 Lisp_Object
3907 make_pure_vector (len)
3908 EMACS_INT len;
3909 {
3910 Lisp_Object new;
3911 struct Lisp_Vector *p;
3912 size_t size = sizeof *p + (len - 1) * sizeof (Lisp_Object);
3913
3914 p = (struct Lisp_Vector *) pure_alloc (size, Lisp_Vectorlike);
3915 XSETVECTOR (new, p);
3916 XVECTOR (new)->size = len;
3917 return new;
3918 }
3919
3920
3921 DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
3922 /* Make a copy of OBJECT in pure storage.
3923 Recursively copies contents of vectors and cons cells.
3924 Does not copy symbols. Copies strings without text properties. */
3925 (obj))
3926 register Lisp_Object obj;
3927 {
3928 if (NILP (Vpurify_flag))
3929 return obj;
3930
3931 if (PURE_POINTER_P (XPNTR (obj)))
3932 return obj;
3933
3934 if (CONSP (obj))
3935 return pure_cons (XCAR (obj), XCDR (obj));
3936 else if (FLOATP (obj))
3937 return make_pure_float (XFLOAT_DATA (obj));
3938 else if (STRINGP (obj))
3939 return make_pure_string (XSTRING (obj)->data, XSTRING (obj)->size,
3940 STRING_BYTES (XSTRING (obj)),
3941 STRING_MULTIBYTE (obj));
3942 else if (COMPILEDP (obj) || VECTORP (obj))
3943 {
3944 register struct Lisp_Vector *vec;
3945 register int i, size;
3946
3947 size = XVECTOR (obj)->size;
3948 if (size & PSEUDOVECTOR_FLAG)
3949 size &= PSEUDOVECTOR_SIZE_MASK;
3950 vec = XVECTOR (make_pure_vector ((EMACS_INT) size));
3951 for (i = 0; i < size; i++)
3952 vec->contents[i] = Fpurecopy (XVECTOR (obj)->contents[i]);
3953 if (COMPILEDP (obj))
3954 XSETCOMPILED (obj, vec);
3955 else
3956 XSETVECTOR (obj, vec);
3957 return obj;
3958 }
3959 else if (MARKERP (obj))
3960 error ("Attempt to copy a marker to pure storage");
3961
3962 return obj;
3963 }
3964
3965
3966 \f
3967 /***********************************************************************
3968 Protection from GC
3969 ***********************************************************************/
3970
3971 /* Put an entry in staticvec, pointing at the variable with address
3972 VARADDRESS. */
3973
3974 void
3975 staticpro (varaddress)
3976 Lisp_Object *varaddress;
3977 {
3978 staticvec[staticidx++] = varaddress;
3979 if (staticidx >= NSTATICS)
3980 abort ();
3981 }
3982
3983 struct catchtag
3984 {
3985 Lisp_Object tag;
3986 Lisp_Object val;
3987 struct catchtag *next;
3988 };
3989
3990 struct backtrace
3991 {
3992 struct backtrace *next;
3993 Lisp_Object *function;
3994 Lisp_Object *args; /* Points to vector of args. */
3995 int nargs; /* Length of vector. */
3996 /* If nargs is UNEVALLED, args points to slot holding list of
3997 unevalled args. */
3998 char evalargs;
3999 };
4000
4001
4002 \f
4003 /***********************************************************************
4004 Protection from GC
4005 ***********************************************************************/
4006
4007 /* Temporarily prevent garbage collection. */
4008
4009 int
4010 inhibit_garbage_collection ()
4011 {
4012 int count = specpdl_ptr - specpdl;
4013 specbind (Qgc_cons_threshold, make_number (MOST_POSITIVE_FIXNUM));
4014 return count;
4015 }
4016
4017
4018 DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
4019 /* Reclaim storage for Lisp objects no longer needed.
4020 Returns info on amount of space in use:
4021 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)
4022 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS
4023 (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS)
4024 (USED-STRINGS . FREE-STRINGS))
4025 Garbage collection happens automatically if you cons more than
4026 `gc-cons-threshold' bytes of Lisp data since previous garbage collection. */
4027 ())
4028 {
4029 register struct gcpro *tail;
4030 register struct specbinding *bind;
4031 struct catchtag *catch;
4032 struct handler *handler;
4033 register struct backtrace *backlist;
4034 char stack_top_variable;
4035 register int i;
4036 int message_p;
4037 Lisp_Object total[8];
4038 int count = BINDING_STACK_SIZE ();
4039
4040 /* Can't GC if pure storage overflowed because we can't determine
4041 if something is a pure object or not. */
4042 if (pure_bytes_used_before_overflow)
4043 return Qnil;
4044
4045 /* In case user calls debug_print during GC,
4046 don't let that cause a recursive GC. */
4047 consing_since_gc = 0;
4048
4049 /* Save what's currently displayed in the echo area. */
4050 message_p = push_message ();
4051 record_unwind_protect (push_message_unwind, Qnil);
4052
4053 /* Save a copy of the contents of the stack, for debugging. */
4054 #if MAX_SAVE_STACK > 0
4055 if (NILP (Vpurify_flag))
4056 {
4057 i = &stack_top_variable - stack_bottom;
4058 if (i < 0) i = -i;
4059 if (i < MAX_SAVE_STACK)
4060 {
4061 if (stack_copy == 0)
4062 stack_copy = (char *) xmalloc (stack_copy_size = i);
4063 else if (stack_copy_size < i)
4064 stack_copy = (char *) xrealloc (stack_copy, (stack_copy_size = i));
4065 if (stack_copy)
4066 {
4067 if ((EMACS_INT) (&stack_top_variable - stack_bottom) > 0)
4068 bcopy (stack_bottom, stack_copy, i);
4069 else
4070 bcopy (&stack_top_variable, stack_copy, i);
4071 }
4072 }
4073 }
4074 #endif /* MAX_SAVE_STACK > 0 */
4075
4076 if (garbage_collection_messages)
4077 message1_nolog ("Garbage collecting...");
4078
4079 BLOCK_INPUT;
4080
4081 shrink_regexp_cache ();
4082
4083 /* Don't keep undo information around forever. */
4084 {
4085 register struct buffer *nextb = all_buffers;
4086
4087 while (nextb)
4088 {
4089 /* If a buffer's undo list is Qt, that means that undo is
4090 turned off in that buffer. Calling truncate_undo_list on
4091 Qt tends to return NULL, which effectively turns undo back on.
4092 So don't call truncate_undo_list if undo_list is Qt. */
4093 if (! EQ (nextb->undo_list, Qt))
4094 nextb->undo_list
4095 = truncate_undo_list (nextb->undo_list, undo_limit,
4096 undo_strong_limit);
4097 nextb = nextb->next;
4098 }
4099 }
4100
4101 gc_in_progress = 1;
4102
4103 /* clear_marks (); */
4104
4105 /* Mark all the special slots that serve as the roots of accessibility.
4106
4107 Usually the special slots to mark are contained in particular structures.
4108 Then we know no slot is marked twice because the structures don't overlap.
4109 In some cases, the structures point to the slots to be marked.
4110 For these, we use MARKBIT to avoid double marking of the slot. */
4111
4112 for (i = 0; i < staticidx; i++)
4113 mark_object (staticvec[i]);
4114
4115 #if (GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS \
4116 || GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS)
4117 mark_stack ();
4118 #else
4119 for (tail = gcprolist; tail; tail = tail->next)
4120 for (i = 0; i < tail->nvars; i++)
4121 if (!XMARKBIT (tail->var[i]))
4122 {
4123 /* Explicit casting prevents compiler warning about
4124 discarding the `volatile' qualifier. */
4125 mark_object ((Lisp_Object *)&tail->var[i]);
4126 XMARK (tail->var[i]);
4127 }
4128 #endif
4129
4130 mark_byte_stack ();
4131 for (bind = specpdl; bind != specpdl_ptr; bind++)
4132 {
4133 mark_object (&bind->symbol);
4134 mark_object (&bind->old_value);
4135 }
4136 for (catch = catchlist; catch; catch = catch->next)
4137 {
4138 mark_object (&catch->tag);
4139 mark_object (&catch->val);
4140 }
4141 for (handler = handlerlist; handler; handler = handler->next)
4142 {
4143 mark_object (&handler->handler);
4144 mark_object (&handler->var);
4145 }
4146 for (backlist = backtrace_list; backlist; backlist = backlist->next)
4147 {
4148 if (!XMARKBIT (*backlist->function))
4149 {
4150 mark_object (backlist->function);
4151 XMARK (*backlist->function);
4152 }
4153 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
4154 i = 0;
4155 else
4156 i = backlist->nargs - 1;
4157 for (; i >= 0; i--)
4158 if (!XMARKBIT (backlist->args[i]))
4159 {
4160 mark_object (&backlist->args[i]);
4161 XMARK (backlist->args[i]);
4162 }
4163 }
4164 mark_kboards ();
4165
4166 /* Look thru every buffer's undo list
4167 for elements that update markers that were not marked,
4168 and delete them. */
4169 {
4170 register struct buffer *nextb = all_buffers;
4171
4172 while (nextb)
4173 {
4174 /* If a buffer's undo list is Qt, that means that undo is
4175 turned off in that buffer. Calling truncate_undo_list on
4176 Qt tends to return NULL, which effectively turns undo back on.
4177 So don't call truncate_undo_list if undo_list is Qt. */
4178 if (! EQ (nextb->undo_list, Qt))
4179 {
4180 Lisp_Object tail, prev;
4181 tail = nextb->undo_list;
4182 prev = Qnil;
4183 while (CONSP (tail))
4184 {
4185 if (GC_CONSP (XCAR (tail))
4186 && GC_MARKERP (XCAR (XCAR (tail)))
4187 && ! XMARKBIT (XMARKER (XCAR (XCAR (tail)))->chain))
4188 {
4189 if (NILP (prev))
4190 nextb->undo_list = tail = XCDR (tail);
4191 else
4192 tail = XCDR (prev) = XCDR (tail);
4193 }
4194 else
4195 {
4196 prev = tail;
4197 tail = XCDR (tail);
4198 }
4199 }
4200 }
4201
4202 nextb = nextb->next;
4203 }
4204 }
4205
4206 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4207 mark_stack ();
4208 #endif
4209
4210 gc_sweep ();
4211
4212 /* Clear the mark bits that we set in certain root slots. */
4213
4214 #if (GC_MARK_STACK == GC_USE_GCPROS_AS_BEFORE \
4215 || GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES)
4216 for (tail = gcprolist; tail; tail = tail->next)
4217 for (i = 0; i < tail->nvars; i++)
4218 XUNMARK (tail->var[i]);
4219 #endif
4220
4221 unmark_byte_stack ();
4222 for (backlist = backtrace_list; backlist; backlist = backlist->next)
4223 {
4224 XUNMARK (*backlist->function);
4225 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
4226 i = 0;
4227 else
4228 i = backlist->nargs - 1;
4229 for (; i >= 0; i--)
4230 XUNMARK (backlist->args[i]);
4231 }
4232 XUNMARK (buffer_defaults.name);
4233 XUNMARK (buffer_local_symbols.name);
4234
4235 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES && 0
4236 dump_zombies ();
4237 #endif
4238
4239 UNBLOCK_INPUT;
4240
4241 /* clear_marks (); */
4242 gc_in_progress = 0;
4243
4244 consing_since_gc = 0;
4245 if (gc_cons_threshold < 10000)
4246 gc_cons_threshold = 10000;
4247
4248 if (garbage_collection_messages)
4249 {
4250 if (message_p || minibuf_level > 0)
4251 restore_message ();
4252 else
4253 message1_nolog ("Garbage collecting...done");
4254 }
4255
4256 unbind_to (count, Qnil);
4257
4258 total[0] = Fcons (make_number (total_conses),
4259 make_number (total_free_conses));
4260 total[1] = Fcons (make_number (total_symbols),
4261 make_number (total_free_symbols));
4262 total[2] = Fcons (make_number (total_markers),
4263 make_number (total_free_markers));
4264 total[3] = make_number (total_string_size);
4265 total[4] = make_number (total_vector_size);
4266 total[5] = Fcons (make_number (total_floats),
4267 make_number (total_free_floats));
4268 total[6] = Fcons (make_number (total_intervals),
4269 make_number (total_free_intervals));
4270 total[7] = Fcons (make_number (total_strings),
4271 make_number (total_free_strings));
4272
4273 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4274 {
4275 /* Compute average percentage of zombies. */
4276 double nlive = 0;
4277
4278 for (i = 0; i < 7; ++i)
4279 nlive += XFASTINT (XCAR (total[i]));
4280
4281 avg_live = (avg_live * ngcs + nlive) / (ngcs + 1);
4282 max_live = max (nlive, max_live);
4283 avg_zombies = (avg_zombies * ngcs + nzombies) / (ngcs + 1);
4284 max_zombies = max (nzombies, max_zombies);
4285 ++ngcs;
4286 }
4287 #endif
4288
4289 if (!NILP (Vpost_gc_hook))
4290 {
4291 int count = inhibit_garbage_collection ();
4292 safe_run_hooks (Qpost_gc_hook);
4293 unbind_to (count, Qnil);
4294 }
4295
4296 return Flist (sizeof total / sizeof *total, total);
4297 }
4298
4299
4300 /* Mark Lisp objects in glyph matrix MATRIX. Currently the
4301 only interesting objects referenced from glyphs are strings. */
4302
4303 static void
4304 mark_glyph_matrix (matrix)
4305 struct glyph_matrix *matrix;
4306 {
4307 struct glyph_row *row = matrix->rows;
4308 struct glyph_row *end = row + matrix->nrows;
4309
4310 for (; row < end; ++row)
4311 if (row->enabled_p)
4312 {
4313 int area;
4314 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
4315 {
4316 struct glyph *glyph = row->glyphs[area];
4317 struct glyph *end_glyph = glyph + row->used[area];
4318
4319 for (; glyph < end_glyph; ++glyph)
4320 if (GC_STRINGP (glyph->object)
4321 && !STRING_MARKED_P (XSTRING (glyph->object)))
4322 mark_object (&glyph->object);
4323 }
4324 }
4325 }
4326
4327
4328 /* Mark Lisp faces in the face cache C. */
4329
4330 static void
4331 mark_face_cache (c)
4332 struct face_cache *c;
4333 {
4334 if (c)
4335 {
4336 int i, j;
4337 for (i = 0; i < c->used; ++i)
4338 {
4339 struct face *face = FACE_FROM_ID (c->f, i);
4340
4341 if (face)
4342 {
4343 for (j = 0; j < LFACE_VECTOR_SIZE; ++j)
4344 mark_object (&face->lface[j]);
4345 }
4346 }
4347 }
4348 }
4349
4350
4351 #ifdef HAVE_WINDOW_SYSTEM
4352
4353 /* Mark Lisp objects in image IMG. */
4354
4355 static void
4356 mark_image (img)
4357 struct image *img;
4358 {
4359 mark_object (&img->spec);
4360
4361 if (!NILP (img->data.lisp_val))
4362 mark_object (&img->data.lisp_val);
4363 }
4364
4365
4366 /* Mark Lisp objects in image cache of frame F. It's done this way so
4367 that we don't have to include xterm.h here. */
4368
4369 static void
4370 mark_image_cache (f)
4371 struct frame *f;
4372 {
4373 forall_images_in_image_cache (f, mark_image);
4374 }
4375
4376 #endif /* HAVE_X_WINDOWS */
4377
4378
4379 \f
4380 /* Mark reference to a Lisp_Object.
4381 If the object referred to has not been seen yet, recursively mark
4382 all the references contained in it. */
4383
4384 #define LAST_MARKED_SIZE 500
4385 Lisp_Object *last_marked[LAST_MARKED_SIZE];
4386 int last_marked_index;
4387
4388 void
4389 mark_object (argptr)
4390 Lisp_Object *argptr;
4391 {
4392 Lisp_Object *objptr = argptr;
4393 register Lisp_Object obj;
4394 #ifdef GC_CHECK_MARKED_OBJECTS
4395 void *po;
4396 struct mem_node *m;
4397 #endif
4398
4399 loop:
4400 obj = *objptr;
4401 loop2:
4402 XUNMARK (obj);
4403
4404 if (PURE_POINTER_P (XPNTR (obj)))
4405 return;
4406
4407 last_marked[last_marked_index++] = objptr;
4408 if (last_marked_index == LAST_MARKED_SIZE)
4409 last_marked_index = 0;
4410
4411 /* Perform some sanity checks on the objects marked here. Abort if
4412 we encounter an object we know is bogus. This increases GC time
4413 by ~80%, and requires compilation with GC_MARK_STACK != 0. */
4414 #ifdef GC_CHECK_MARKED_OBJECTS
4415
4416 po = (void *) XPNTR (obj);
4417
4418 /* Check that the object pointed to by PO is known to be a Lisp
4419 structure allocated from the heap. */
4420 #define CHECK_ALLOCATED() \
4421 do { \
4422 m = mem_find (po); \
4423 if (m == MEM_NIL) \
4424 abort (); \
4425 } while (0)
4426
4427 /* Check that the object pointed to by PO is live, using predicate
4428 function LIVEP. */
4429 #define CHECK_LIVE(LIVEP) \
4430 do { \
4431 if (!LIVEP (m, po)) \
4432 abort (); \
4433 } while (0)
4434
4435 /* Check both of the above conditions. */
4436 #define CHECK_ALLOCATED_AND_LIVE(LIVEP) \
4437 do { \
4438 CHECK_ALLOCATED (); \
4439 CHECK_LIVE (LIVEP); \
4440 } while (0) \
4441
4442 #else /* not GC_CHECK_MARKED_OBJECTS */
4443
4444 #define CHECK_ALLOCATED() (void) 0
4445 #define CHECK_LIVE(LIVEP) (void) 0
4446 #define CHECK_ALLOCATED_AND_LIVE(LIVEP) (void) 0
4447
4448 #endif /* not GC_CHECK_MARKED_OBJECTS */
4449
4450 switch (SWITCH_ENUM_CAST (XGCTYPE (obj)))
4451 {
4452 case Lisp_String:
4453 {
4454 register struct Lisp_String *ptr = XSTRING (obj);
4455 CHECK_ALLOCATED_AND_LIVE (live_string_p);
4456 MARK_INTERVAL_TREE (ptr->intervals);
4457 MARK_STRING (ptr);
4458 #ifdef GC_CHECK_STRING_BYTES
4459 /* Check that the string size recorded in the string is the
4460 same as the one recorded in the sdata structure. */
4461 CHECK_STRING_BYTES (ptr);
4462 #endif /* GC_CHECK_STRING_BYTES */
4463 }
4464 break;
4465
4466 case Lisp_Vectorlike:
4467 #ifdef GC_CHECK_MARKED_OBJECTS
4468 m = mem_find (po);
4469 if (m == MEM_NIL && !GC_SUBRP (obj)
4470 && po != &buffer_defaults
4471 && po != &buffer_local_symbols)
4472 abort ();
4473 #endif /* GC_CHECK_MARKED_OBJECTS */
4474
4475 if (GC_BUFFERP (obj))
4476 {
4477 if (!XMARKBIT (XBUFFER (obj)->name))
4478 {
4479 #ifdef GC_CHECK_MARKED_OBJECTS
4480 if (po != &buffer_defaults && po != &buffer_local_symbols)
4481 {
4482 struct buffer *b;
4483 for (b = all_buffers; b && b != po; b = b->next)
4484 ;
4485 if (b == NULL)
4486 abort ();
4487 }
4488 #endif /* GC_CHECK_MARKED_OBJECTS */
4489 mark_buffer (obj);
4490 }
4491 }
4492 else if (GC_SUBRP (obj))
4493 break;
4494 else if (GC_COMPILEDP (obj))
4495 /* We could treat this just like a vector, but it is better to
4496 save the COMPILED_CONSTANTS element for last and avoid
4497 recursion there. */
4498 {
4499 register struct Lisp_Vector *ptr = XVECTOR (obj);
4500 register EMACS_INT size = ptr->size;
4501 register int i;
4502
4503 if (size & ARRAY_MARK_FLAG)
4504 break; /* Already marked */
4505
4506 CHECK_LIVE (live_vector_p);
4507 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
4508 size &= PSEUDOVECTOR_SIZE_MASK;
4509 for (i = 0; i < size; i++) /* and then mark its elements */
4510 {
4511 if (i != COMPILED_CONSTANTS)
4512 mark_object (&ptr->contents[i]);
4513 }
4514 /* This cast should be unnecessary, but some Mips compiler complains
4515 (MIPS-ABI + SysVR4, DC/OSx, etc). */
4516 objptr = (Lisp_Object *) &ptr->contents[COMPILED_CONSTANTS];
4517 goto loop;
4518 }
4519 else if (GC_FRAMEP (obj))
4520 {
4521 register struct frame *ptr = XFRAME (obj);
4522 register EMACS_INT size = ptr->size;
4523
4524 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
4525 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
4526
4527 CHECK_LIVE (live_vector_p);
4528 mark_object (&ptr->name);
4529 mark_object (&ptr->icon_name);
4530 mark_object (&ptr->title);
4531 mark_object (&ptr->focus_frame);
4532 mark_object (&ptr->selected_window);
4533 mark_object (&ptr->minibuffer_window);
4534 mark_object (&ptr->param_alist);
4535 mark_object (&ptr->scroll_bars);
4536 mark_object (&ptr->condemned_scroll_bars);
4537 mark_object (&ptr->menu_bar_items);
4538 mark_object (&ptr->face_alist);
4539 mark_object (&ptr->menu_bar_vector);
4540 mark_object (&ptr->buffer_predicate);
4541 mark_object (&ptr->buffer_list);
4542 mark_object (&ptr->menu_bar_window);
4543 mark_object (&ptr->tool_bar_window);
4544 mark_face_cache (ptr->face_cache);
4545 #ifdef HAVE_WINDOW_SYSTEM
4546 mark_image_cache (ptr);
4547 mark_object (&ptr->tool_bar_items);
4548 mark_object (&ptr->desired_tool_bar_string);
4549 mark_object (&ptr->current_tool_bar_string);
4550 #endif /* HAVE_WINDOW_SYSTEM */
4551 }
4552 else if (GC_BOOL_VECTOR_P (obj))
4553 {
4554 register struct Lisp_Vector *ptr = XVECTOR (obj);
4555
4556 if (ptr->size & ARRAY_MARK_FLAG)
4557 break; /* Already marked */
4558 CHECK_LIVE (live_vector_p);
4559 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
4560 }
4561 else if (GC_WINDOWP (obj))
4562 {
4563 register struct Lisp_Vector *ptr = XVECTOR (obj);
4564 struct window *w = XWINDOW (obj);
4565 register EMACS_INT size = ptr->size;
4566 register int i;
4567
4568 /* Stop if already marked. */
4569 if (size & ARRAY_MARK_FLAG)
4570 break;
4571
4572 /* Mark it. */
4573 CHECK_LIVE (live_vector_p);
4574 ptr->size |= ARRAY_MARK_FLAG;
4575
4576 /* There is no Lisp data above The member CURRENT_MATRIX in
4577 struct WINDOW. Stop marking when that slot is reached. */
4578 for (i = 0;
4579 (char *) &ptr->contents[i] < (char *) &w->current_matrix;
4580 i++)
4581 mark_object (&ptr->contents[i]);
4582
4583 /* Mark glyphs for leaf windows. Marking window matrices is
4584 sufficient because frame matrices use the same glyph
4585 memory. */
4586 if (NILP (w->hchild)
4587 && NILP (w->vchild)
4588 && w->current_matrix)
4589 {
4590 mark_glyph_matrix (w->current_matrix);
4591 mark_glyph_matrix (w->desired_matrix);
4592 }
4593 }
4594 else if (GC_HASH_TABLE_P (obj))
4595 {
4596 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
4597 EMACS_INT size = h->size;
4598
4599 /* Stop if already marked. */
4600 if (size & ARRAY_MARK_FLAG)
4601 break;
4602
4603 /* Mark it. */
4604 CHECK_LIVE (live_vector_p);
4605 h->size |= ARRAY_MARK_FLAG;
4606
4607 /* Mark contents. */
4608 mark_object (&h->test);
4609 mark_object (&h->weak);
4610 mark_object (&h->rehash_size);
4611 mark_object (&h->rehash_threshold);
4612 mark_object (&h->hash);
4613 mark_object (&h->next);
4614 mark_object (&h->index);
4615 mark_object (&h->user_hash_function);
4616 mark_object (&h->user_cmp_function);
4617
4618 /* If hash table is not weak, mark all keys and values.
4619 For weak tables, mark only the vector. */
4620 if (GC_NILP (h->weak))
4621 mark_object (&h->key_and_value);
4622 else
4623 XVECTOR (h->key_and_value)->size |= ARRAY_MARK_FLAG;
4624
4625 }
4626 else
4627 {
4628 register struct Lisp_Vector *ptr = XVECTOR (obj);
4629 register EMACS_INT size = ptr->size;
4630 register int i;
4631
4632 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
4633 CHECK_LIVE (live_vector_p);
4634 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
4635 if (size & PSEUDOVECTOR_FLAG)
4636 size &= PSEUDOVECTOR_SIZE_MASK;
4637
4638 for (i = 0; i < size; i++) /* and then mark its elements */
4639 mark_object (&ptr->contents[i]);
4640 }
4641 break;
4642
4643 case Lisp_Symbol:
4644 {
4645 register struct Lisp_Symbol *ptr = XSYMBOL (obj);
4646 struct Lisp_Symbol *ptrx;
4647
4648 if (XMARKBIT (ptr->plist)) break;
4649 CHECK_ALLOCATED_AND_LIVE (live_symbol_p);
4650 XMARK (ptr->plist);
4651 mark_object ((Lisp_Object *) &ptr->value);
4652 mark_object (&ptr->function);
4653 mark_object (&ptr->plist);
4654
4655 if (!PURE_POINTER_P (ptr->name))
4656 MARK_STRING (ptr->name);
4657 MARK_INTERVAL_TREE (ptr->name->intervals);
4658
4659 /* Note that we do not mark the obarray of the symbol.
4660 It is safe not to do so because nothing accesses that
4661 slot except to check whether it is nil. */
4662 ptr = ptr->next;
4663 if (ptr)
4664 {
4665 /* For the benefit of the last_marked log. */
4666 objptr = (Lisp_Object *)&XSYMBOL (obj)->next;
4667 ptrx = ptr; /* Use of ptrx avoids compiler bug on Sun */
4668 XSETSYMBOL (obj, ptrx);
4669 /* We can't goto loop here because *objptr doesn't contain an
4670 actual Lisp_Object with valid datatype field. */
4671 goto loop2;
4672 }
4673 }
4674 break;
4675
4676 case Lisp_Misc:
4677 CHECK_ALLOCATED_AND_LIVE (live_misc_p);
4678 switch (XMISCTYPE (obj))
4679 {
4680 case Lisp_Misc_Marker:
4681 XMARK (XMARKER (obj)->chain);
4682 /* DO NOT mark thru the marker's chain.
4683 The buffer's markers chain does not preserve markers from gc;
4684 instead, markers are removed from the chain when freed by gc. */
4685 break;
4686
4687 case Lisp_Misc_Buffer_Local_Value:
4688 case Lisp_Misc_Some_Buffer_Local_Value:
4689 {
4690 register struct Lisp_Buffer_Local_Value *ptr
4691 = XBUFFER_LOCAL_VALUE (obj);
4692 if (XMARKBIT (ptr->realvalue)) break;
4693 XMARK (ptr->realvalue);
4694 /* If the cdr is nil, avoid recursion for the car. */
4695 if (EQ (ptr->cdr, Qnil))
4696 {
4697 objptr = &ptr->realvalue;
4698 goto loop;
4699 }
4700 mark_object (&ptr->realvalue);
4701 mark_object (&ptr->buffer);
4702 mark_object (&ptr->frame);
4703 objptr = &ptr->cdr;
4704 goto loop;
4705 }
4706
4707 case Lisp_Misc_Intfwd:
4708 case Lisp_Misc_Boolfwd:
4709 case Lisp_Misc_Objfwd:
4710 case Lisp_Misc_Buffer_Objfwd:
4711 case Lisp_Misc_Kboard_Objfwd:
4712 /* Don't bother with Lisp_Buffer_Objfwd,
4713 since all markable slots in current buffer marked anyway. */
4714 /* Don't need to do Lisp_Objfwd, since the places they point
4715 are protected with staticpro. */
4716 break;
4717
4718 case Lisp_Misc_Overlay:
4719 {
4720 struct Lisp_Overlay *ptr = XOVERLAY (obj);
4721 if (!XMARKBIT (ptr->plist))
4722 {
4723 XMARK (ptr->plist);
4724 mark_object (&ptr->start);
4725 mark_object (&ptr->end);
4726 objptr = &ptr->plist;
4727 goto loop;
4728 }
4729 }
4730 break;
4731
4732 default:
4733 abort ();
4734 }
4735 break;
4736
4737 case Lisp_Cons:
4738 {
4739 register struct Lisp_Cons *ptr = XCONS (obj);
4740 if (XMARKBIT (ptr->car)) break;
4741 CHECK_ALLOCATED_AND_LIVE (live_cons_p);
4742 XMARK (ptr->car);
4743 /* If the cdr is nil, avoid recursion for the car. */
4744 if (EQ (ptr->cdr, Qnil))
4745 {
4746 objptr = &ptr->car;
4747 goto loop;
4748 }
4749 mark_object (&ptr->car);
4750 objptr = &ptr->cdr;
4751 goto loop;
4752 }
4753
4754 case Lisp_Float:
4755 CHECK_ALLOCATED_AND_LIVE (live_float_p);
4756 XMARK (XFLOAT (obj)->type);
4757 break;
4758
4759 case Lisp_Int:
4760 break;
4761
4762 default:
4763 abort ();
4764 }
4765
4766 #undef CHECK_LIVE
4767 #undef CHECK_ALLOCATED
4768 #undef CHECK_ALLOCATED_AND_LIVE
4769 }
4770
4771 /* Mark the pointers in a buffer structure. */
4772
4773 static void
4774 mark_buffer (buf)
4775 Lisp_Object buf;
4776 {
4777 register struct buffer *buffer = XBUFFER (buf);
4778 register Lisp_Object *ptr;
4779 Lisp_Object base_buffer;
4780
4781 /* This is the buffer's markbit */
4782 mark_object (&buffer->name);
4783 XMARK (buffer->name);
4784
4785 MARK_INTERVAL_TREE (BUF_INTERVALS (buffer));
4786
4787 if (CONSP (buffer->undo_list))
4788 {
4789 Lisp_Object tail;
4790 tail = buffer->undo_list;
4791
4792 while (CONSP (tail))
4793 {
4794 register struct Lisp_Cons *ptr = XCONS (tail);
4795
4796 if (XMARKBIT (ptr->car))
4797 break;
4798 XMARK (ptr->car);
4799 if (GC_CONSP (ptr->car)
4800 && ! XMARKBIT (XCAR (ptr->car))
4801 && GC_MARKERP (XCAR (ptr->car)))
4802 {
4803 XMARK (XCAR (ptr->car));
4804 mark_object (&XCDR (ptr->car));
4805 }
4806 else
4807 mark_object (&ptr->car);
4808
4809 if (CONSP (ptr->cdr))
4810 tail = ptr->cdr;
4811 else
4812 break;
4813 }
4814
4815 mark_object (&XCDR (tail));
4816 }
4817 else
4818 mark_object (&buffer->undo_list);
4819
4820 for (ptr = &buffer->name + 1;
4821 (char *)ptr < (char *)buffer + sizeof (struct buffer);
4822 ptr++)
4823 mark_object (ptr);
4824
4825 /* If this is an indirect buffer, mark its base buffer. */
4826 if (buffer->base_buffer && !XMARKBIT (buffer->base_buffer->name))
4827 {
4828 XSETBUFFER (base_buffer, buffer->base_buffer);
4829 mark_buffer (base_buffer);
4830 }
4831 }
4832
4833
4834 /* Mark the pointers in the kboard objects. */
4835
4836 static void
4837 mark_kboards ()
4838 {
4839 KBOARD *kb;
4840 Lisp_Object *p;
4841 for (kb = all_kboards; kb; kb = kb->next_kboard)
4842 {
4843 if (kb->kbd_macro_buffer)
4844 for (p = kb->kbd_macro_buffer; p < kb->kbd_macro_ptr; p++)
4845 mark_object (p);
4846 mark_object (&kb->Voverriding_terminal_local_map);
4847 mark_object (&kb->Vlast_command);
4848 mark_object (&kb->Vreal_last_command);
4849 mark_object (&kb->Vprefix_arg);
4850 mark_object (&kb->Vlast_prefix_arg);
4851 mark_object (&kb->kbd_queue);
4852 mark_object (&kb->defining_kbd_macro);
4853 mark_object (&kb->Vlast_kbd_macro);
4854 mark_object (&kb->Vsystem_key_alist);
4855 mark_object (&kb->system_key_syms);
4856 mark_object (&kb->Vdefault_minibuffer_frame);
4857 }
4858 }
4859
4860
4861 /* Value is non-zero if OBJ will survive the current GC because it's
4862 either marked or does not need to be marked to survive. */
4863
4864 int
4865 survives_gc_p (obj)
4866 Lisp_Object obj;
4867 {
4868 int survives_p;
4869
4870 switch (XGCTYPE (obj))
4871 {
4872 case Lisp_Int:
4873 survives_p = 1;
4874 break;
4875
4876 case Lisp_Symbol:
4877 survives_p = XMARKBIT (XSYMBOL (obj)->plist);
4878 break;
4879
4880 case Lisp_Misc:
4881 switch (XMISCTYPE (obj))
4882 {
4883 case Lisp_Misc_Marker:
4884 survives_p = XMARKBIT (obj);
4885 break;
4886
4887 case Lisp_Misc_Buffer_Local_Value:
4888 case Lisp_Misc_Some_Buffer_Local_Value:
4889 survives_p = XMARKBIT (XBUFFER_LOCAL_VALUE (obj)->realvalue);
4890 break;
4891
4892 case Lisp_Misc_Intfwd:
4893 case Lisp_Misc_Boolfwd:
4894 case Lisp_Misc_Objfwd:
4895 case Lisp_Misc_Buffer_Objfwd:
4896 case Lisp_Misc_Kboard_Objfwd:
4897 survives_p = 1;
4898 break;
4899
4900 case Lisp_Misc_Overlay:
4901 survives_p = XMARKBIT (XOVERLAY (obj)->plist);
4902 break;
4903
4904 default:
4905 abort ();
4906 }
4907 break;
4908
4909 case Lisp_String:
4910 {
4911 struct Lisp_String *s = XSTRING (obj);
4912 survives_p = STRING_MARKED_P (s);
4913 }
4914 break;
4915
4916 case Lisp_Vectorlike:
4917 if (GC_BUFFERP (obj))
4918 survives_p = XMARKBIT (XBUFFER (obj)->name);
4919 else if (GC_SUBRP (obj))
4920 survives_p = 1;
4921 else
4922 survives_p = XVECTOR (obj)->size & ARRAY_MARK_FLAG;
4923 break;
4924
4925 case Lisp_Cons:
4926 survives_p = XMARKBIT (XCAR (obj));
4927 break;
4928
4929 case Lisp_Float:
4930 survives_p = XMARKBIT (XFLOAT (obj)->type);
4931 break;
4932
4933 default:
4934 abort ();
4935 }
4936
4937 return survives_p || PURE_POINTER_P ((void *) XPNTR (obj));
4938 }
4939
4940
4941 \f
4942 /* Sweep: find all structures not marked, and free them. */
4943
4944 static void
4945 gc_sweep ()
4946 {
4947 /* Remove or mark entries in weak hash tables.
4948 This must be done before any object is unmarked. */
4949 sweep_weak_hash_tables ();
4950
4951 sweep_strings ();
4952 #ifdef GC_CHECK_STRING_BYTES
4953 if (!noninteractive)
4954 check_string_bytes (1);
4955 #endif
4956
4957 /* Put all unmarked conses on free list */
4958 {
4959 register struct cons_block *cblk;
4960 struct cons_block **cprev = &cons_block;
4961 register int lim = cons_block_index;
4962 register int num_free = 0, num_used = 0;
4963
4964 cons_free_list = 0;
4965
4966 for (cblk = cons_block; cblk; cblk = *cprev)
4967 {
4968 register int i;
4969 int this_free = 0;
4970 for (i = 0; i < lim; i++)
4971 if (!XMARKBIT (cblk->conses[i].car))
4972 {
4973 this_free++;
4974 *(struct Lisp_Cons **)&cblk->conses[i].cdr = cons_free_list;
4975 cons_free_list = &cblk->conses[i];
4976 #if GC_MARK_STACK
4977 cons_free_list->car = Vdead;
4978 #endif
4979 }
4980 else
4981 {
4982 num_used++;
4983 XUNMARK (cblk->conses[i].car);
4984 }
4985 lim = CONS_BLOCK_SIZE;
4986 /* If this block contains only free conses and we have already
4987 seen more than two blocks worth of free conses then deallocate
4988 this block. */
4989 if (this_free == CONS_BLOCK_SIZE && num_free > CONS_BLOCK_SIZE)
4990 {
4991 *cprev = cblk->next;
4992 /* Unhook from the free list. */
4993 cons_free_list = *(struct Lisp_Cons **) &cblk->conses[0].cdr;
4994 lisp_free (cblk);
4995 n_cons_blocks--;
4996 }
4997 else
4998 {
4999 num_free += this_free;
5000 cprev = &cblk->next;
5001 }
5002 }
5003 total_conses = num_used;
5004 total_free_conses = num_free;
5005 }
5006
5007 /* Put all unmarked floats on free list */
5008 {
5009 register struct float_block *fblk;
5010 struct float_block **fprev = &float_block;
5011 register int lim = float_block_index;
5012 register int num_free = 0, num_used = 0;
5013
5014 float_free_list = 0;
5015
5016 for (fblk = float_block; fblk; fblk = *fprev)
5017 {
5018 register int i;
5019 int this_free = 0;
5020 for (i = 0; i < lim; i++)
5021 if (!XMARKBIT (fblk->floats[i].type))
5022 {
5023 this_free++;
5024 *(struct Lisp_Float **)&fblk->floats[i].data = float_free_list;
5025 float_free_list = &fblk->floats[i];
5026 #if GC_MARK_STACK
5027 float_free_list->type = Vdead;
5028 #endif
5029 }
5030 else
5031 {
5032 num_used++;
5033 XUNMARK (fblk->floats[i].type);
5034 }
5035 lim = FLOAT_BLOCK_SIZE;
5036 /* If this block contains only free floats and we have already
5037 seen more than two blocks worth of free floats then deallocate
5038 this block. */
5039 if (this_free == FLOAT_BLOCK_SIZE && num_free > FLOAT_BLOCK_SIZE)
5040 {
5041 *fprev = fblk->next;
5042 /* Unhook from the free list. */
5043 float_free_list = *(struct Lisp_Float **) &fblk->floats[0].data;
5044 lisp_free (fblk);
5045 n_float_blocks--;
5046 }
5047 else
5048 {
5049 num_free += this_free;
5050 fprev = &fblk->next;
5051 }
5052 }
5053 total_floats = num_used;
5054 total_free_floats = num_free;
5055 }
5056
5057 /* Put all unmarked intervals on free list */
5058 {
5059 register struct interval_block *iblk;
5060 struct interval_block **iprev = &interval_block;
5061 register int lim = interval_block_index;
5062 register int num_free = 0, num_used = 0;
5063
5064 interval_free_list = 0;
5065
5066 for (iblk = interval_block; iblk; iblk = *iprev)
5067 {
5068 register int i;
5069 int this_free = 0;
5070
5071 for (i = 0; i < lim; i++)
5072 {
5073 if (! XMARKBIT (iblk->intervals[i].plist))
5074 {
5075 SET_INTERVAL_PARENT (&iblk->intervals[i], interval_free_list);
5076 interval_free_list = &iblk->intervals[i];
5077 this_free++;
5078 }
5079 else
5080 {
5081 num_used++;
5082 XUNMARK (iblk->intervals[i].plist);
5083 }
5084 }
5085 lim = INTERVAL_BLOCK_SIZE;
5086 /* If this block contains only free intervals and we have already
5087 seen more than two blocks worth of free intervals then
5088 deallocate this block. */
5089 if (this_free == INTERVAL_BLOCK_SIZE && num_free > INTERVAL_BLOCK_SIZE)
5090 {
5091 *iprev = iblk->next;
5092 /* Unhook from the free list. */
5093 interval_free_list = INTERVAL_PARENT (&iblk->intervals[0]);
5094 lisp_free (iblk);
5095 n_interval_blocks--;
5096 }
5097 else
5098 {
5099 num_free += this_free;
5100 iprev = &iblk->next;
5101 }
5102 }
5103 total_intervals = num_used;
5104 total_free_intervals = num_free;
5105 }
5106
5107 /* Put all unmarked symbols on free list */
5108 {
5109 register struct symbol_block *sblk;
5110 struct symbol_block **sprev = &symbol_block;
5111 register int lim = symbol_block_index;
5112 register int num_free = 0, num_used = 0;
5113
5114 symbol_free_list = NULL;
5115
5116 for (sblk = symbol_block; sblk; sblk = *sprev)
5117 {
5118 int this_free = 0;
5119 struct Lisp_Symbol *sym = sblk->symbols;
5120 struct Lisp_Symbol *end = sym + lim;
5121
5122 for (; sym < end; ++sym)
5123 {
5124 /* Check if the symbol was created during loadup. In such a case
5125 it might be pointed to by pure bytecode which we don't trace,
5126 so we conservatively assume that it is live. */
5127 int pure_p = PURE_POINTER_P (sym->name);
5128
5129 if (!XMARKBIT (sym->plist) && !pure_p)
5130 {
5131 *(struct Lisp_Symbol **) &sym->value = symbol_free_list;
5132 symbol_free_list = sym;
5133 #if GC_MARK_STACK
5134 symbol_free_list->function = Vdead;
5135 #endif
5136 ++this_free;
5137 }
5138 else
5139 {
5140 ++num_used;
5141 if (!pure_p)
5142 UNMARK_STRING (sym->name);
5143 XUNMARK (sym->plist);
5144 }
5145 }
5146
5147 lim = SYMBOL_BLOCK_SIZE;
5148 /* If this block contains only free symbols and we have already
5149 seen more than two blocks worth of free symbols then deallocate
5150 this block. */
5151 if (this_free == SYMBOL_BLOCK_SIZE && num_free > SYMBOL_BLOCK_SIZE)
5152 {
5153 *sprev = sblk->next;
5154 /* Unhook from the free list. */
5155 symbol_free_list = *(struct Lisp_Symbol **)&sblk->symbols[0].value;
5156 lisp_free (sblk);
5157 n_symbol_blocks--;
5158 }
5159 else
5160 {
5161 num_free += this_free;
5162 sprev = &sblk->next;
5163 }
5164 }
5165 total_symbols = num_used;
5166 total_free_symbols = num_free;
5167 }
5168
5169 /* Put all unmarked misc's on free list.
5170 For a marker, first unchain it from the buffer it points into. */
5171 {
5172 register struct marker_block *mblk;
5173 struct marker_block **mprev = &marker_block;
5174 register int lim = marker_block_index;
5175 register int num_free = 0, num_used = 0;
5176
5177 marker_free_list = 0;
5178
5179 for (mblk = marker_block; mblk; mblk = *mprev)
5180 {
5181 register int i;
5182 int this_free = 0;
5183 EMACS_INT already_free = -1;
5184
5185 for (i = 0; i < lim; i++)
5186 {
5187 Lisp_Object *markword;
5188 switch (mblk->markers[i].u_marker.type)
5189 {
5190 case Lisp_Misc_Marker:
5191 markword = &mblk->markers[i].u_marker.chain;
5192 break;
5193 case Lisp_Misc_Buffer_Local_Value:
5194 case Lisp_Misc_Some_Buffer_Local_Value:
5195 markword = &mblk->markers[i].u_buffer_local_value.realvalue;
5196 break;
5197 case Lisp_Misc_Overlay:
5198 markword = &mblk->markers[i].u_overlay.plist;
5199 break;
5200 case Lisp_Misc_Free:
5201 /* If the object was already free, keep it
5202 on the free list. */
5203 markword = (Lisp_Object *) &already_free;
5204 break;
5205 default:
5206 markword = 0;
5207 break;
5208 }
5209 if (markword && !XMARKBIT (*markword))
5210 {
5211 Lisp_Object tem;
5212 if (mblk->markers[i].u_marker.type == Lisp_Misc_Marker)
5213 {
5214 /* tem1 avoids Sun compiler bug */
5215 struct Lisp_Marker *tem1 = &mblk->markers[i].u_marker;
5216 XSETMARKER (tem, tem1);
5217 unchain_marker (tem);
5218 }
5219 /* Set the type of the freed object to Lisp_Misc_Free.
5220 We could leave the type alone, since nobody checks it,
5221 but this might catch bugs faster. */
5222 mblk->markers[i].u_marker.type = Lisp_Misc_Free;
5223 mblk->markers[i].u_free.chain = marker_free_list;
5224 marker_free_list = &mblk->markers[i];
5225 this_free++;
5226 }
5227 else
5228 {
5229 num_used++;
5230 if (markword)
5231 XUNMARK (*markword);
5232 }
5233 }
5234 lim = MARKER_BLOCK_SIZE;
5235 /* If this block contains only free markers and we have already
5236 seen more than two blocks worth of free markers then deallocate
5237 this block. */
5238 if (this_free == MARKER_BLOCK_SIZE && num_free > MARKER_BLOCK_SIZE)
5239 {
5240 *mprev = mblk->next;
5241 /* Unhook from the free list. */
5242 marker_free_list = mblk->markers[0].u_free.chain;
5243 lisp_free (mblk);
5244 n_marker_blocks--;
5245 }
5246 else
5247 {
5248 num_free += this_free;
5249 mprev = &mblk->next;
5250 }
5251 }
5252
5253 total_markers = num_used;
5254 total_free_markers = num_free;
5255 }
5256
5257 /* Free all unmarked buffers */
5258 {
5259 register struct buffer *buffer = all_buffers, *prev = 0, *next;
5260
5261 while (buffer)
5262 if (!XMARKBIT (buffer->name))
5263 {
5264 if (prev)
5265 prev->next = buffer->next;
5266 else
5267 all_buffers = buffer->next;
5268 next = buffer->next;
5269 lisp_free (buffer);
5270 buffer = next;
5271 }
5272 else
5273 {
5274 XUNMARK (buffer->name);
5275 UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer));
5276 prev = buffer, buffer = buffer->next;
5277 }
5278 }
5279
5280 /* Free all unmarked vectors */
5281 {
5282 register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next;
5283 total_vector_size = 0;
5284
5285 while (vector)
5286 if (!(vector->size & ARRAY_MARK_FLAG))
5287 {
5288 if (prev)
5289 prev->next = vector->next;
5290 else
5291 all_vectors = vector->next;
5292 next = vector->next;
5293 lisp_free (vector);
5294 n_vectors--;
5295 vector = next;
5296
5297 }
5298 else
5299 {
5300 vector->size &= ~ARRAY_MARK_FLAG;
5301 if (vector->size & PSEUDOVECTOR_FLAG)
5302 total_vector_size += (PSEUDOVECTOR_SIZE_MASK & vector->size);
5303 else
5304 total_vector_size += vector->size;
5305 prev = vector, vector = vector->next;
5306 }
5307 }
5308
5309 #ifdef GC_CHECK_STRING_BYTES
5310 if (!noninteractive)
5311 check_string_bytes (1);
5312 #endif
5313 }
5314
5315
5316
5317 \f
5318 /* Debugging aids. */
5319
5320 DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0,
5321 /* Return the address of the last byte Emacs has allocated, divided by 1024.
5322 This may be helpful in debugging Emacs's memory usage.
5323 We divide the value by 1024 to make sure it fits in a Lisp integer. */
5324 ())
5325 {
5326 Lisp_Object end;
5327
5328 XSETINT (end, (EMACS_INT) sbrk (0) / 1024);
5329
5330 return end;
5331 }
5332
5333 DEFUN ("memory-use-counts", Fmemory_use_counts, Smemory_use_counts, 0, 0, 0,
5334 /* Return a list of counters that measure how much consing there has been.
5335 Each of these counters increments for a certain kind of object.
5336 The counters wrap around from the largest positive integer to zero.
5337 Garbage collection does not decrease them.
5338 The elements of the value are as follows:
5339 (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS STRINGS)
5340 All are in units of 1 = one object consed
5341 except for VECTOR-CELLS and STRING-CHARS, which count the total length of
5342 objects consed.
5343 MISCS include overlays, markers, and some internal types.
5344 Frames, windows, buffers, and subprocesses count as vectors
5345 (but the contents of a buffer's text do not count here). */
5346 ())
5347 {
5348 Lisp_Object consed[8];
5349
5350 consed[0] = make_number (min (MOST_POSITIVE_FIXNUM, cons_cells_consed));
5351 consed[1] = make_number (min (MOST_POSITIVE_FIXNUM, floats_consed));
5352 consed[2] = make_number (min (MOST_POSITIVE_FIXNUM, vector_cells_consed));
5353 consed[3] = make_number (min (MOST_POSITIVE_FIXNUM, symbols_consed));
5354 consed[4] = make_number (min (MOST_POSITIVE_FIXNUM, string_chars_consed));
5355 consed[5] = make_number (min (MOST_POSITIVE_FIXNUM, misc_objects_consed));
5356 consed[6] = make_number (min (MOST_POSITIVE_FIXNUM, intervals_consed));
5357 consed[7] = make_number (min (MOST_POSITIVE_FIXNUM, strings_consed));
5358
5359 return Flist (8, consed);
5360 }
5361
5362 int suppress_checking;
5363 void
5364 die (msg, file, line)
5365 const char *msg;
5366 const char *file;
5367 int line;
5368 {
5369 fprintf (stderr, "\r\nEmacs fatal error: %s:%d: %s\r\n",
5370 file, line, msg);
5371 abort ();
5372 }
5373 \f
5374 /* Initialization */
5375
5376 void
5377 init_alloc_once ()
5378 {
5379 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
5380 purebeg = PUREBEG;
5381 pure_size = PURESIZE;
5382 pure_bytes_used = 0;
5383 pure_bytes_used_before_overflow = 0;
5384
5385 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
5386 mem_init ();
5387 Vdead = make_pure_string ("DEAD", 4, 4, 0);
5388 #endif
5389
5390 all_vectors = 0;
5391 ignore_warnings = 1;
5392 #ifdef DOUG_LEA_MALLOC
5393 mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */
5394 mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */
5395 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); /* max. number of mmap'ed areas */
5396 #endif
5397 init_strings ();
5398 init_cons ();
5399 init_symbol ();
5400 init_marker ();
5401 init_float ();
5402 init_intervals ();
5403
5404 #ifdef REL_ALLOC
5405 malloc_hysteresis = 32;
5406 #else
5407 malloc_hysteresis = 0;
5408 #endif
5409
5410 spare_memory = (char *) malloc (SPARE_MEMORY);
5411
5412 ignore_warnings = 0;
5413 gcprolist = 0;
5414 byte_stack_list = 0;
5415 staticidx = 0;
5416 consing_since_gc = 0;
5417 gc_cons_threshold = 100000 * sizeof (Lisp_Object);
5418 #ifdef VIRT_ADDR_VARIES
5419 malloc_sbrk_unused = 1<<22; /* A large number */
5420 malloc_sbrk_used = 100000; /* as reasonable as any number */
5421 #endif /* VIRT_ADDR_VARIES */
5422 }
5423
5424 void
5425 init_alloc ()
5426 {
5427 gcprolist = 0;
5428 byte_stack_list = 0;
5429 #if GC_MARK_STACK
5430 #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
5431 setjmp_tested_p = longjmps_done = 0;
5432 #endif
5433 #endif
5434 }
5435
5436 void
5437 syms_of_alloc ()
5438 {
5439 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold
5440 /* *Number of bytes of consing between garbage collections.
5441 Garbage collection can happen automatically once this many bytes have been
5442 allocated since the last garbage collection. All data types count.
5443
5444 Garbage collection happens automatically only when `eval' is called.
5445
5446 By binding this temporarily to a large number, you can effectively
5447 prevent garbage collection during a part of the program. */);
5448
5449 DEFVAR_INT ("pure-bytes-used", &pure_bytes_used
5450 /* Number of bytes of sharable Lisp data allocated so far. */);
5451
5452 DEFVAR_INT ("cons-cells-consed", &cons_cells_consed
5453 /* Number of cons cells that have been consed so far. */);
5454
5455 DEFVAR_INT ("floats-consed", &floats_consed
5456 /* Number of floats that have been consed so far. */);
5457
5458 DEFVAR_INT ("vector-cells-consed", &vector_cells_consed
5459 /* Number of vector cells that have been consed so far. */);
5460
5461 DEFVAR_INT ("symbols-consed", &symbols_consed
5462 /* Number of symbols that have been consed so far. */);
5463
5464 DEFVAR_INT ("string-chars-consed", &string_chars_consed
5465 /* Number of string characters that have been consed so far. */);
5466
5467 DEFVAR_INT ("misc-objects-consed", &misc_objects_consed
5468 /* Number of miscellaneous objects that have been consed so far. */);
5469
5470 DEFVAR_INT ("intervals-consed", &intervals_consed
5471 /* Number of intervals that have been consed so far. */);
5472
5473 DEFVAR_INT ("strings-consed", &strings_consed
5474 /* Number of strings that have been consed so far. */);
5475
5476 DEFVAR_LISP ("purify-flag", &Vpurify_flag
5477 /* Non-nil means loading Lisp code in order to dump an executable.
5478 This means that certain objects should be allocated in shared (pure) space. */);
5479
5480 DEFVAR_INT ("undo-limit", &undo_limit
5481 /* Keep no more undo information once it exceeds this size.
5482 This limit is applied when garbage collection happens.
5483 The size is counted as the number of bytes occupied,
5484 which includes both saved text and other data. */);
5485 undo_limit = 20000;
5486
5487 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit
5488 /* Don't keep more than this much size of undo information.
5489 A command which pushes past this size is itself forgotten.
5490 This limit is applied when garbage collection happens.
5491 The size is counted as the number of bytes occupied,
5492 which includes both saved text and other data. */);
5493 undo_strong_limit = 30000;
5494
5495 DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages
5496 /* Non-nil means display messages at start and end of garbage collection. */);
5497 garbage_collection_messages = 0;
5498
5499 DEFVAR_LISP ("post-gc-hook", &Vpost_gc_hook
5500 /* Hook run after garbage collection has finished. */);
5501 Vpost_gc_hook = Qnil;
5502 Qpost_gc_hook = intern ("post-gc-hook");
5503 staticpro (&Qpost_gc_hook);
5504
5505 /* We build this in advance because if we wait until we need it, we might
5506 not be able to allocate the memory to hold it. */
5507 memory_signal_data
5508 = Fcons (Qerror, Fcons (build_string ("Memory exhausted--use M-x save-some-buffers RET"), Qnil));
5509 staticpro (&memory_signal_data);
5510
5511 staticpro (&Qgc_cons_threshold);
5512 Qgc_cons_threshold = intern ("gc-cons-threshold");
5513
5514 staticpro (&Qchar_table_extra_slots);
5515 Qchar_table_extra_slots = intern ("char-table-extra-slots");
5516
5517 defsubr (&Scons);
5518 defsubr (&Slist);
5519 defsubr (&Svector);
5520 defsubr (&Smake_byte_code);
5521 defsubr (&Smake_list);
5522 defsubr (&Smake_vector);
5523 defsubr (&Smake_char_table);
5524 defsubr (&Smake_string);
5525 defsubr (&Smake_bool_vector);
5526 defsubr (&Smake_symbol);
5527 defsubr (&Smake_marker);
5528 defsubr (&Spurecopy);
5529 defsubr (&Sgarbage_collect);
5530 defsubr (&Smemory_limit);
5531 defsubr (&Smemory_use_counts);
5532
5533 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
5534 defsubr (&Sgc_status);
5535 #endif
5536 }