]> code.delx.au - gnu-emacs/blob - src/alloc.c
Comment change.
[gnu-emacs] / src / alloc.c
1 /* Storage allocation and gc for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 86, 88, 93, 94, 95, 97, 98, 1999, 2000, 2001, 2002
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 #ifdef ALLOC_DEBUG
26 #undef INLINE
27 #endif
28
29 /* Note that this declares bzero on OSF/1. How dumb. */
30
31 #include <signal.h>
32
33 /* GC_MALLOC_CHECK defined means perform validity checks of malloc'd
34 memory. Can do this only if using gmalloc.c. */
35
36 #if defined SYSTEM_MALLOC || defined DOUG_LEA_MALLOC
37 #undef GC_MALLOC_CHECK
38 #endif
39
40 /* This file is part of the core Lisp implementation, and thus must
41 deal with the real data structures. If the Lisp implementation is
42 replaced, this file likely will not be used. */
43
44 #undef HIDE_LISP_IMPLEMENTATION
45 #include "lisp.h"
46 #include "process.h"
47 #include "intervals.h"
48 #include "puresize.h"
49 #include "buffer.h"
50 #include "window.h"
51 #include "keyboard.h"
52 #include "frame.h"
53 #include "blockinput.h"
54 #include "charset.h"
55 #include "syssignal.h"
56 #include <setjmp.h>
57
58 #ifdef HAVE_UNISTD_H
59 #include <unistd.h>
60 #else
61 extern POINTER_TYPE *sbrk ();
62 #endif
63
64 #ifdef DOUG_LEA_MALLOC
65
66 #include <malloc.h>
67 /* malloc.h #defines this as size_t, at least in glibc2. */
68 #ifndef __malloc_size_t
69 #define __malloc_size_t int
70 #endif
71
72 /* Specify maximum number of areas to mmap. It would be nice to use a
73 value that explicitly means "no limit". */
74
75 #define MMAP_MAX_AREAS 100000000
76
77 #else /* not DOUG_LEA_MALLOC */
78
79 /* The following come from gmalloc.c. */
80
81 #define __malloc_size_t size_t
82 extern __malloc_size_t _bytes_used;
83 extern __malloc_size_t __malloc_extra_blocks;
84
85 #endif /* not DOUG_LEA_MALLOC */
86
87 /* Macro to verify that storage intended for Lisp objects is not
88 out of range to fit in the space for a pointer.
89 ADDRESS is the start of the block, and SIZE
90 is the amount of space within which objects can start. */
91
92 #define VALIDATE_LISP_STORAGE(address, size) \
93 do \
94 { \
95 Lisp_Object val; \
96 XSETCONS (val, (char *) address + size); \
97 if ((char *) XCONS (val) != (char *) address + size) \
98 { \
99 xfree (address); \
100 memory_full (); \
101 } \
102 } while (0)
103
104 /* Value of _bytes_used, when spare_memory was freed. */
105
106 static __malloc_size_t bytes_used_when_full;
107
108 /* Mark, unmark, query mark bit of a Lisp string. S must be a pointer
109 to a struct Lisp_String. */
110
111 #define MARK_STRING(S) ((S)->size |= MARKBIT)
112 #define UNMARK_STRING(S) ((S)->size &= ~MARKBIT)
113 #define STRING_MARKED_P(S) ((S)->size & MARKBIT)
114
115 /* Value is the number of bytes/chars of S, a pointer to a struct
116 Lisp_String. This must be used instead of STRING_BYTES (S) or
117 S->size during GC, because S->size contains the mark bit for
118 strings. */
119
120 #define GC_STRING_BYTES(S) (STRING_BYTES (S) & ~MARKBIT)
121 #define GC_STRING_CHARS(S) ((S)->size & ~MARKBIT)
122
123 /* Number of bytes of consing done since the last gc. */
124
125 int consing_since_gc;
126
127 /* Count the amount of consing of various sorts of space. */
128
129 EMACS_INT cons_cells_consed;
130 EMACS_INT floats_consed;
131 EMACS_INT vector_cells_consed;
132 EMACS_INT symbols_consed;
133 EMACS_INT string_chars_consed;
134 EMACS_INT misc_objects_consed;
135 EMACS_INT intervals_consed;
136 EMACS_INT strings_consed;
137
138 /* Number of bytes of consing since GC before another GC should be done. */
139
140 EMACS_INT gc_cons_threshold;
141
142 /* Nonzero during GC. */
143
144 int gc_in_progress;
145
146 /* Nonzero means display messages at beginning and end of GC. */
147
148 int garbage_collection_messages;
149
150 #ifndef VIRT_ADDR_VARIES
151 extern
152 #endif /* VIRT_ADDR_VARIES */
153 int malloc_sbrk_used;
154
155 #ifndef VIRT_ADDR_VARIES
156 extern
157 #endif /* VIRT_ADDR_VARIES */
158 int malloc_sbrk_unused;
159
160 /* Two limits controlling how much undo information to keep. */
161
162 EMACS_INT undo_limit;
163 EMACS_INT undo_strong_limit;
164
165 /* Number of live and free conses etc. */
166
167 static int total_conses, total_markers, total_symbols, total_vector_size;
168 static int total_free_conses, total_free_markers, total_free_symbols;
169 static int total_free_floats, total_floats;
170
171 /* Points to memory space allocated as "spare", to be freed if we run
172 out of memory. */
173
174 static char *spare_memory;
175
176 /* Amount of spare memory to keep in reserve. */
177
178 #define SPARE_MEMORY (1 << 14)
179
180 /* Number of extra blocks malloc should get when it needs more core. */
181
182 static int malloc_hysteresis;
183
184 /* Non-nil means defun should do purecopy on the function definition. */
185
186 Lisp_Object Vpurify_flag;
187
188 /* Non-nil means we are handling a memory-full error. */
189
190 Lisp_Object Vmemory_full;
191
192 #ifndef HAVE_SHM
193
194 /* Force it into data space! */
195
196 EMACS_INT pure[PURESIZE / sizeof (EMACS_INT)] = {0,};
197 #define PUREBEG (char *) pure
198
199 #else /* HAVE_SHM */
200
201 #define pure PURE_SEG_BITS /* Use shared memory segment */
202 #define PUREBEG (char *)PURE_SEG_BITS
203
204 #endif /* HAVE_SHM */
205
206 /* Pointer to the pure area, and its size. */
207
208 static char *purebeg;
209 static size_t pure_size;
210
211 /* Number of bytes of pure storage used before pure storage overflowed.
212 If this is non-zero, this implies that an overflow occurred. */
213
214 static size_t pure_bytes_used_before_overflow;
215
216 /* Value is non-zero if P points into pure space. */
217
218 #define PURE_POINTER_P(P) \
219 (((PNTR_COMPARISON_TYPE) (P) \
220 < (PNTR_COMPARISON_TYPE) ((char *) purebeg + pure_size)) \
221 && ((PNTR_COMPARISON_TYPE) (P) \
222 >= (PNTR_COMPARISON_TYPE) purebeg))
223
224 /* Index in pure at which next pure object will be allocated.. */
225
226 EMACS_INT pure_bytes_used;
227
228 /* If nonzero, this is a warning delivered by malloc and not yet
229 displayed. */
230
231 char *pending_malloc_warning;
232
233 /* Pre-computed signal argument for use when memory is exhausted. */
234
235 Lisp_Object Vmemory_signal_data;
236
237 /* Maximum amount of C stack to save when a GC happens. */
238
239 #ifndef MAX_SAVE_STACK
240 #define MAX_SAVE_STACK 16000
241 #endif
242
243 /* Buffer in which we save a copy of the C stack at each GC. */
244
245 char *stack_copy;
246 int stack_copy_size;
247
248 /* Non-zero means ignore malloc warnings. Set during initialization.
249 Currently not used. */
250
251 int ignore_warnings;
252
253 Lisp_Object Qgc_cons_threshold, Qchar_table_extra_slots;
254
255 /* Hook run after GC has finished. */
256
257 Lisp_Object Vpost_gc_hook, Qpost_gc_hook;
258
259 static void mark_buffer P_ ((Lisp_Object));
260 static void mark_kboards P_ ((void));
261 static void gc_sweep P_ ((void));
262 static void mark_glyph_matrix P_ ((struct glyph_matrix *));
263 static void mark_face_cache P_ ((struct face_cache *));
264
265 #ifdef HAVE_WINDOW_SYSTEM
266 static void mark_image P_ ((struct image *));
267 static void mark_image_cache P_ ((struct frame *));
268 #endif /* HAVE_WINDOW_SYSTEM */
269
270 static struct Lisp_String *allocate_string P_ ((void));
271 static void compact_small_strings P_ ((void));
272 static void free_large_strings P_ ((void));
273 static void sweep_strings P_ ((void));
274
275 extern int message_enable_multibyte;
276
277 /* When scanning the C stack for live Lisp objects, Emacs keeps track
278 of what memory allocated via lisp_malloc is intended for what
279 purpose. This enumeration specifies the type of memory. */
280
281 enum mem_type
282 {
283 MEM_TYPE_NON_LISP,
284 MEM_TYPE_BUFFER,
285 MEM_TYPE_CONS,
286 MEM_TYPE_STRING,
287 MEM_TYPE_MISC,
288 MEM_TYPE_SYMBOL,
289 MEM_TYPE_FLOAT,
290 /* Keep the following vector-like types together, with
291 MEM_TYPE_WINDOW being the last, and MEM_TYPE_VECTOR the
292 first. Or change the code of live_vector_p, for instance. */
293 MEM_TYPE_VECTOR,
294 MEM_TYPE_PROCESS,
295 MEM_TYPE_HASH_TABLE,
296 MEM_TYPE_FRAME,
297 MEM_TYPE_WINDOW
298 };
299
300 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
301
302 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
303 #include <stdio.h> /* For fprintf. */
304 #endif
305
306 /* A unique object in pure space used to make some Lisp objects
307 on free lists recognizable in O(1). */
308
309 Lisp_Object Vdead;
310
311 #ifdef GC_MALLOC_CHECK
312
313 enum mem_type allocated_mem_type;
314 int dont_register_blocks;
315
316 #endif /* GC_MALLOC_CHECK */
317
318 /* A node in the red-black tree describing allocated memory containing
319 Lisp data. Each such block is recorded with its start and end
320 address when it is allocated, and removed from the tree when it
321 is freed.
322
323 A red-black tree is a balanced binary tree with the following
324 properties:
325
326 1. Every node is either red or black.
327 2. Every leaf is black.
328 3. If a node is red, then both of its children are black.
329 4. Every simple path from a node to a descendant leaf contains
330 the same number of black nodes.
331 5. The root is always black.
332
333 When nodes are inserted into the tree, or deleted from the tree,
334 the tree is "fixed" so that these properties are always true.
335
336 A red-black tree with N internal nodes has height at most 2
337 log(N+1). Searches, insertions and deletions are done in O(log N).
338 Please see a text book about data structures for a detailed
339 description of red-black trees. Any book worth its salt should
340 describe them. */
341
342 struct mem_node
343 {
344 /* Children of this node. These pointers are never NULL. When there
345 is no child, the value is MEM_NIL, which points to a dummy node. */
346 struct mem_node *left, *right;
347
348 /* The parent of this node. In the root node, this is NULL. */
349 struct mem_node *parent;
350
351 /* Start and end of allocated region. */
352 void *start, *end;
353
354 /* Node color. */
355 enum {MEM_BLACK, MEM_RED} color;
356
357 /* Memory type. */
358 enum mem_type type;
359 };
360
361 /* Base address of stack. Set in main. */
362
363 Lisp_Object *stack_base;
364
365 /* Root of the tree describing allocated Lisp memory. */
366
367 static struct mem_node *mem_root;
368
369 /* Lowest and highest known address in the heap. */
370
371 static void *min_heap_address, *max_heap_address;
372
373 /* Sentinel node of the tree. */
374
375 static struct mem_node mem_z;
376 #define MEM_NIL &mem_z
377
378 static POINTER_TYPE *lisp_malloc P_ ((size_t, enum mem_type));
379 static struct Lisp_Vector *allocate_vectorlike P_ ((EMACS_INT, enum mem_type));
380 static void lisp_free P_ ((POINTER_TYPE *));
381 static void mark_stack P_ ((void));
382 static int live_vector_p P_ ((struct mem_node *, void *));
383 static int live_buffer_p P_ ((struct mem_node *, void *));
384 static int live_string_p P_ ((struct mem_node *, void *));
385 static int live_cons_p P_ ((struct mem_node *, void *));
386 static int live_symbol_p P_ ((struct mem_node *, void *));
387 static int live_float_p P_ ((struct mem_node *, void *));
388 static int live_misc_p P_ ((struct mem_node *, void *));
389 static void mark_maybe_object P_ ((Lisp_Object));
390 static void mark_memory P_ ((void *, void *));
391 static void mem_init P_ ((void));
392 static struct mem_node *mem_insert P_ ((void *, void *, enum mem_type));
393 static void mem_insert_fixup P_ ((struct mem_node *));
394 static void mem_rotate_left P_ ((struct mem_node *));
395 static void mem_rotate_right P_ ((struct mem_node *));
396 static void mem_delete P_ ((struct mem_node *));
397 static void mem_delete_fixup P_ ((struct mem_node *));
398 static INLINE struct mem_node *mem_find P_ ((void *));
399
400 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
401 static void check_gcpros P_ ((void));
402 #endif
403
404 #endif /* GC_MARK_STACK || GC_MALLOC_CHECK */
405
406 /* Recording what needs to be marked for gc. */
407
408 struct gcpro *gcprolist;
409
410 /* Addresses of staticpro'd variables. */
411
412 #define NSTATICS 1280
413 Lisp_Object *staticvec[NSTATICS] = {0};
414
415 /* Index of next unused slot in staticvec. */
416
417 int staticidx = 0;
418
419 static POINTER_TYPE *pure_alloc P_ ((size_t, int));
420
421
422 /* Value is SZ rounded up to the next multiple of ALIGNMENT.
423 ALIGNMENT must be a power of 2. */
424
425 #define ALIGN(SZ, ALIGNMENT) \
426 (((SZ) + (ALIGNMENT) - 1) & ~((ALIGNMENT) - 1))
427
428
429 \f
430 /************************************************************************
431 Malloc
432 ************************************************************************/
433
434 /* Function malloc calls this if it finds we are near exhausting storage. */
435
436 void
437 malloc_warning (str)
438 char *str;
439 {
440 pending_malloc_warning = str;
441 }
442
443
444 /* Display an already-pending malloc warning. */
445
446 void
447 display_malloc_warning ()
448 {
449 call3 (intern ("display-warning"),
450 intern ("alloc"),
451 build_string (pending_malloc_warning),
452 intern ("emergency"));
453 pending_malloc_warning = 0;
454 }
455
456
457 #ifdef DOUG_LEA_MALLOC
458 # define BYTES_USED (mallinfo ().arena)
459 #else
460 # define BYTES_USED _bytes_used
461 #endif
462
463
464 /* Called if malloc returns zero. */
465
466 void
467 memory_full ()
468 {
469 Vmemory_full = Qt;
470
471 #ifndef SYSTEM_MALLOC
472 bytes_used_when_full = BYTES_USED;
473 #endif
474
475 /* The first time we get here, free the spare memory. */
476 if (spare_memory)
477 {
478 free (spare_memory);
479 spare_memory = 0;
480 }
481
482 /* This used to call error, but if we've run out of memory, we could
483 get infinite recursion trying to build the string. */
484 while (1)
485 Fsignal (Qnil, Vmemory_signal_data);
486 }
487
488
489 /* Called if we can't allocate relocatable space for a buffer. */
490
491 void
492 buffer_memory_full ()
493 {
494 /* If buffers use the relocating allocator, no need to free
495 spare_memory, because we may have plenty of malloc space left
496 that we could get, and if we don't, the malloc that fails will
497 itself cause spare_memory to be freed. If buffers don't use the
498 relocating allocator, treat this like any other failing
499 malloc. */
500
501 #ifndef REL_ALLOC
502 memory_full ();
503 #endif
504
505 Vmemory_full = Qt;
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 (Qnil, Vmemory_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 const 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 string_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 MAC_OS8
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 doc: /* 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);
1673 CHECK_NUMBER (init);
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 = SDATA (val);
1681 end = p + SCHARS (val);
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 = SDATA (val);
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 doc: /* 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);
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 const 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 const char *contents;
1775 int length;
1776 {
1777 register Lisp_Object val;
1778 val = make_uninit_string (length);
1779 bcopy (contents, SDATA (val), length);
1780 STRING_SET_UNIBYTE (val);
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 const char *contents;
1791 int nchars, nbytes;
1792 {
1793 register Lisp_Object val;
1794 val = make_uninit_multibyte_string (nchars, nbytes);
1795 bcopy (contents, SDATA (val), 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, SDATA (val), nbytes);
1811 if (SBYTES (val) == SCHARS (val))
1812 STRING_SET_UNIBYTE (val);
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, SDATA (val), nbytes);
1830 if (!multibyte)
1831 STRING_SET_UNIBYTE (val);
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 const 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 STRING_SET_UNIBYTE (val);
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 doc: /* 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 XSETCAR (val, car);
2091 XSETCDR (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 doc: /* Return a newly created list with specified arguments as elements.
2135 Any number of arguments, even zero arguments, are allowed.
2136 usage: (list &rest OBJECTS) */)
2137 (nargs, args)
2138 int nargs;
2139 register Lisp_Object *args;
2140 {
2141 register Lisp_Object val;
2142 val = Qnil;
2143
2144 while (nargs > 0)
2145 {
2146 nargs--;
2147 val = Fcons (args[nargs], val);
2148 }
2149 return val;
2150 }
2151
2152
2153 DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0,
2154 doc: /* Return a newly created list of length LENGTH, with each element being INIT. */)
2155 (length, init)
2156 register Lisp_Object length, init;
2157 {
2158 register Lisp_Object val;
2159 register int size;
2160
2161 CHECK_NATNUM (length);
2162 size = XFASTINT (length);
2163
2164 val = Qnil;
2165 while (size > 0)
2166 {
2167 val = Fcons (init, val);
2168 --size;
2169
2170 if (size > 0)
2171 {
2172 val = Fcons (init, val);
2173 --size;
2174
2175 if (size > 0)
2176 {
2177 val = Fcons (init, val);
2178 --size;
2179
2180 if (size > 0)
2181 {
2182 val = Fcons (init, val);
2183 --size;
2184
2185 if (size > 0)
2186 {
2187 val = Fcons (init, val);
2188 --size;
2189 }
2190 }
2191 }
2192 }
2193
2194 QUIT;
2195 }
2196
2197 return val;
2198 }
2199
2200
2201 \f
2202 /***********************************************************************
2203 Vector Allocation
2204 ***********************************************************************/
2205
2206 /* Singly-linked list of all vectors. */
2207
2208 struct Lisp_Vector *all_vectors;
2209
2210 /* Total number of vector-like objects now in use. */
2211
2212 int n_vectors;
2213
2214
2215 /* Value is a pointer to a newly allocated Lisp_Vector structure
2216 with room for LEN Lisp_Objects. */
2217
2218 static struct Lisp_Vector *
2219 allocate_vectorlike (len, type)
2220 EMACS_INT len;
2221 enum mem_type type;
2222 {
2223 struct Lisp_Vector *p;
2224 size_t nbytes;
2225
2226 #ifdef DOUG_LEA_MALLOC
2227 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed
2228 because mapped region contents are not preserved in
2229 a dumped Emacs. */
2230 mallopt (M_MMAP_MAX, 0);
2231 #endif
2232
2233 nbytes = sizeof *p + (len - 1) * sizeof p->contents[0];
2234 p = (struct Lisp_Vector *) lisp_malloc (nbytes, type);
2235
2236 #ifdef DOUG_LEA_MALLOC
2237 /* Back to a reasonable maximum of mmap'ed areas. */
2238 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
2239 #endif
2240
2241 VALIDATE_LISP_STORAGE (p, 0);
2242 consing_since_gc += nbytes;
2243 vector_cells_consed += len;
2244
2245 p->next = all_vectors;
2246 all_vectors = p;
2247 ++n_vectors;
2248 return p;
2249 }
2250
2251
2252 /* Allocate a vector with NSLOTS slots. */
2253
2254 struct Lisp_Vector *
2255 allocate_vector (nslots)
2256 EMACS_INT nslots;
2257 {
2258 struct Lisp_Vector *v = allocate_vectorlike (nslots, MEM_TYPE_VECTOR);
2259 v->size = nslots;
2260 return v;
2261 }
2262
2263
2264 /* Allocate other vector-like structures. */
2265
2266 struct Lisp_Hash_Table *
2267 allocate_hash_table ()
2268 {
2269 EMACS_INT len = VECSIZE (struct Lisp_Hash_Table);
2270 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_HASH_TABLE);
2271 EMACS_INT i;
2272
2273 v->size = len;
2274 for (i = 0; i < len; ++i)
2275 v->contents[i] = Qnil;
2276
2277 return (struct Lisp_Hash_Table *) v;
2278 }
2279
2280
2281 struct window *
2282 allocate_window ()
2283 {
2284 EMACS_INT len = VECSIZE (struct window);
2285 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_WINDOW);
2286 EMACS_INT i;
2287
2288 for (i = 0; i < len; ++i)
2289 v->contents[i] = Qnil;
2290 v->size = len;
2291
2292 return (struct window *) v;
2293 }
2294
2295
2296 struct frame *
2297 allocate_frame ()
2298 {
2299 EMACS_INT len = VECSIZE (struct frame);
2300 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_FRAME);
2301 EMACS_INT i;
2302
2303 for (i = 0; i < len; ++i)
2304 v->contents[i] = make_number (0);
2305 v->size = len;
2306 return (struct frame *) v;
2307 }
2308
2309
2310 struct Lisp_Process *
2311 allocate_process ()
2312 {
2313 EMACS_INT len = VECSIZE (struct Lisp_Process);
2314 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_PROCESS);
2315 EMACS_INT i;
2316
2317 for (i = 0; i < len; ++i)
2318 v->contents[i] = Qnil;
2319 v->size = len;
2320
2321 return (struct Lisp_Process *) v;
2322 }
2323
2324
2325 struct Lisp_Vector *
2326 allocate_other_vector (len)
2327 EMACS_INT len;
2328 {
2329 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_VECTOR);
2330 EMACS_INT i;
2331
2332 for (i = 0; i < len; ++i)
2333 v->contents[i] = Qnil;
2334 v->size = len;
2335
2336 return v;
2337 }
2338
2339
2340 DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0,
2341 doc: /* Return a newly created vector of length LENGTH, with each element being INIT.
2342 See also the function `vector'. */)
2343 (length, init)
2344 register Lisp_Object length, init;
2345 {
2346 Lisp_Object vector;
2347 register EMACS_INT sizei;
2348 register int index;
2349 register struct Lisp_Vector *p;
2350
2351 CHECK_NATNUM (length);
2352 sizei = XFASTINT (length);
2353
2354 p = allocate_vector (sizei);
2355 for (index = 0; index < sizei; index++)
2356 p->contents[index] = init;
2357
2358 XSETVECTOR (vector, p);
2359 return vector;
2360 }
2361
2362
2363 DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0,
2364 doc: /* Return a newly created char-table, with purpose PURPOSE.
2365 Each element is initialized to INIT, which defaults to nil.
2366 PURPOSE should be a symbol which has a `char-table-extra-slots' property.
2367 The property's value should be an integer between 0 and 10. */)
2368 (purpose, init)
2369 register Lisp_Object purpose, init;
2370 {
2371 Lisp_Object vector;
2372 Lisp_Object n;
2373 CHECK_SYMBOL (purpose);
2374 n = Fget (purpose, Qchar_table_extra_slots);
2375 CHECK_NUMBER (n);
2376 if (XINT (n) < 0 || XINT (n) > 10)
2377 args_out_of_range (n, Qnil);
2378 /* Add 2 to the size for the defalt and parent slots. */
2379 vector = Fmake_vector (make_number (CHAR_TABLE_STANDARD_SLOTS + XINT (n)),
2380 init);
2381 XCHAR_TABLE (vector)->top = Qt;
2382 XCHAR_TABLE (vector)->parent = Qnil;
2383 XCHAR_TABLE (vector)->purpose = purpose;
2384 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
2385 return vector;
2386 }
2387
2388
2389 /* Return a newly created sub char table with default value DEFALT.
2390 Since a sub char table does not appear as a top level Emacs Lisp
2391 object, we don't need a Lisp interface to make it. */
2392
2393 Lisp_Object
2394 make_sub_char_table (defalt)
2395 Lisp_Object defalt;
2396 {
2397 Lisp_Object vector
2398 = Fmake_vector (make_number (SUB_CHAR_TABLE_STANDARD_SLOTS), Qnil);
2399 XCHAR_TABLE (vector)->top = Qnil;
2400 XCHAR_TABLE (vector)->defalt = defalt;
2401 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
2402 return vector;
2403 }
2404
2405
2406 DEFUN ("vector", Fvector, Svector, 0, MANY, 0,
2407 doc: /* Return a newly created vector with specified arguments as elements.
2408 Any number of arguments, even zero arguments, are allowed.
2409 usage: (vector &rest OBJECTS) */)
2410 (nargs, args)
2411 register int nargs;
2412 Lisp_Object *args;
2413 {
2414 register Lisp_Object len, val;
2415 register int index;
2416 register struct Lisp_Vector *p;
2417
2418 XSETFASTINT (len, nargs);
2419 val = Fmake_vector (len, Qnil);
2420 p = XVECTOR (val);
2421 for (index = 0; index < nargs; index++)
2422 p->contents[index] = args[index];
2423 return val;
2424 }
2425
2426
2427 DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0,
2428 doc: /* Create a byte-code object with specified arguments as elements.
2429 The arguments should be the arglist, bytecode-string, constant vector,
2430 stack size, (optional) doc string, and (optional) interactive spec.
2431 The first four arguments are required; at most six have any
2432 significance.
2433 usage: (make-byte-code &rest ELEMENTS) */)
2434 (nargs, args)
2435 register int nargs;
2436 Lisp_Object *args;
2437 {
2438 register Lisp_Object len, val;
2439 register int index;
2440 register struct Lisp_Vector *p;
2441
2442 XSETFASTINT (len, nargs);
2443 if (!NILP (Vpurify_flag))
2444 val = make_pure_vector ((EMACS_INT) nargs);
2445 else
2446 val = Fmake_vector (len, Qnil);
2447
2448 if (STRINGP (args[1]) && STRING_MULTIBYTE (args[1]))
2449 /* BYTECODE-STRING must have been produced by Emacs 20.2 or the
2450 earlier because they produced a raw 8-bit string for byte-code
2451 and now such a byte-code string is loaded as multibyte while
2452 raw 8-bit characters converted to multibyte form. Thus, now we
2453 must convert them back to the original unibyte form. */
2454 args[1] = Fstring_as_unibyte (args[1]);
2455
2456 p = XVECTOR (val);
2457 for (index = 0; index < nargs; index++)
2458 {
2459 if (!NILP (Vpurify_flag))
2460 args[index] = Fpurecopy (args[index]);
2461 p->contents[index] = args[index];
2462 }
2463 XSETCOMPILED (val, p);
2464 return val;
2465 }
2466
2467
2468 \f
2469 /***********************************************************************
2470 Symbol Allocation
2471 ***********************************************************************/
2472
2473 /* Each symbol_block is just under 1020 bytes long, since malloc
2474 really allocates in units of powers of two and uses 4 bytes for its
2475 own overhead. */
2476
2477 #define SYMBOL_BLOCK_SIZE \
2478 ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol))
2479
2480 struct symbol_block
2481 {
2482 struct symbol_block *next;
2483 struct Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE];
2484 };
2485
2486 /* Current symbol block and index of first unused Lisp_Symbol
2487 structure in it. */
2488
2489 struct symbol_block *symbol_block;
2490 int symbol_block_index;
2491
2492 /* List of free symbols. */
2493
2494 struct Lisp_Symbol *symbol_free_list;
2495
2496 /* Total number of symbol blocks now in use. */
2497
2498 int n_symbol_blocks;
2499
2500
2501 /* Initialize symbol allocation. */
2502
2503 void
2504 init_symbol ()
2505 {
2506 symbol_block = (struct symbol_block *) lisp_malloc (sizeof *symbol_block,
2507 MEM_TYPE_SYMBOL);
2508 symbol_block->next = 0;
2509 bzero ((char *) symbol_block->symbols, sizeof symbol_block->symbols);
2510 symbol_block_index = 0;
2511 symbol_free_list = 0;
2512 n_symbol_blocks = 1;
2513 }
2514
2515
2516 DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0,
2517 doc: /* Return a newly allocated uninterned symbol whose name is NAME.
2518 Its value and function definition are void, and its property list is nil. */)
2519 (name)
2520 Lisp_Object name;
2521 {
2522 register Lisp_Object val;
2523 register struct Lisp_Symbol *p;
2524
2525 CHECK_STRING (name);
2526
2527 if (symbol_free_list)
2528 {
2529 XSETSYMBOL (val, symbol_free_list);
2530 symbol_free_list = *(struct Lisp_Symbol **)&symbol_free_list->value;
2531 }
2532 else
2533 {
2534 if (symbol_block_index == SYMBOL_BLOCK_SIZE)
2535 {
2536 struct symbol_block *new;
2537 new = (struct symbol_block *) lisp_malloc (sizeof *new,
2538 MEM_TYPE_SYMBOL);
2539 VALIDATE_LISP_STORAGE (new, sizeof *new);
2540 new->next = symbol_block;
2541 symbol_block = new;
2542 symbol_block_index = 0;
2543 n_symbol_blocks++;
2544 }
2545 XSETSYMBOL (val, &symbol_block->symbols[symbol_block_index++]);
2546 }
2547
2548 p = XSYMBOL (val);
2549 p->xname = name;
2550 p->plist = Qnil;
2551 p->value = Qunbound;
2552 p->function = Qunbound;
2553 p->next = NULL;
2554 p->interned = SYMBOL_UNINTERNED;
2555 p->constant = 0;
2556 p->indirect_variable = 0;
2557 consing_since_gc += sizeof (struct Lisp_Symbol);
2558 symbols_consed++;
2559 return val;
2560 }
2561
2562
2563 \f
2564 /***********************************************************************
2565 Marker (Misc) Allocation
2566 ***********************************************************************/
2567
2568 /* Allocation of markers and other objects that share that structure.
2569 Works like allocation of conses. */
2570
2571 #define MARKER_BLOCK_SIZE \
2572 ((1020 - sizeof (struct marker_block *)) / sizeof (union Lisp_Misc))
2573
2574 struct marker_block
2575 {
2576 struct marker_block *next;
2577 union Lisp_Misc markers[MARKER_BLOCK_SIZE];
2578 };
2579
2580 struct marker_block *marker_block;
2581 int marker_block_index;
2582
2583 union Lisp_Misc *marker_free_list;
2584
2585 /* Total number of marker blocks now in use. */
2586
2587 int n_marker_blocks;
2588
2589 void
2590 init_marker ()
2591 {
2592 marker_block = (struct marker_block *) lisp_malloc (sizeof *marker_block,
2593 MEM_TYPE_MISC);
2594 marker_block->next = 0;
2595 bzero ((char *) marker_block->markers, sizeof marker_block->markers);
2596 marker_block_index = 0;
2597 marker_free_list = 0;
2598 n_marker_blocks = 1;
2599 }
2600
2601 /* Return a newly allocated Lisp_Misc object, with no substructure. */
2602
2603 Lisp_Object
2604 allocate_misc ()
2605 {
2606 Lisp_Object val;
2607
2608 if (marker_free_list)
2609 {
2610 XSETMISC (val, marker_free_list);
2611 marker_free_list = marker_free_list->u_free.chain;
2612 }
2613 else
2614 {
2615 if (marker_block_index == MARKER_BLOCK_SIZE)
2616 {
2617 struct marker_block *new;
2618 new = (struct marker_block *) lisp_malloc (sizeof *new,
2619 MEM_TYPE_MISC);
2620 VALIDATE_LISP_STORAGE (new, sizeof *new);
2621 new->next = marker_block;
2622 marker_block = new;
2623 marker_block_index = 0;
2624 n_marker_blocks++;
2625 }
2626 XSETMISC (val, &marker_block->markers[marker_block_index++]);
2627 }
2628
2629 consing_since_gc += sizeof (union Lisp_Misc);
2630 misc_objects_consed++;
2631 return val;
2632 }
2633
2634 DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
2635 doc: /* Return a newly allocated marker which does not point at any place. */)
2636 ()
2637 {
2638 register Lisp_Object val;
2639 register struct Lisp_Marker *p;
2640
2641 val = allocate_misc ();
2642 XMISCTYPE (val) = Lisp_Misc_Marker;
2643 p = XMARKER (val);
2644 p->buffer = 0;
2645 p->bytepos = 0;
2646 p->charpos = 0;
2647 p->chain = Qnil;
2648 p->insertion_type = 0;
2649 return val;
2650 }
2651
2652 /* Put MARKER back on the free list after using it temporarily. */
2653
2654 void
2655 free_marker (marker)
2656 Lisp_Object marker;
2657 {
2658 unchain_marker (marker);
2659
2660 XMISC (marker)->u_marker.type = Lisp_Misc_Free;
2661 XMISC (marker)->u_free.chain = marker_free_list;
2662 marker_free_list = XMISC (marker);
2663
2664 total_free_markers++;
2665 }
2666
2667 \f
2668 /* Return a newly created vector or string with specified arguments as
2669 elements. If all the arguments are characters that can fit
2670 in a string of events, make a string; otherwise, make a vector.
2671
2672 Any number of arguments, even zero arguments, are allowed. */
2673
2674 Lisp_Object
2675 make_event_array (nargs, args)
2676 register int nargs;
2677 Lisp_Object *args;
2678 {
2679 int i;
2680
2681 for (i = 0; i < nargs; i++)
2682 /* The things that fit in a string
2683 are characters that are in 0...127,
2684 after discarding the meta bit and all the bits above it. */
2685 if (!INTEGERP (args[i])
2686 || (XUINT (args[i]) & ~(-CHAR_META)) >= 0200)
2687 return Fvector (nargs, args);
2688
2689 /* Since the loop exited, we know that all the things in it are
2690 characters, so we can make a string. */
2691 {
2692 Lisp_Object result;
2693
2694 result = Fmake_string (make_number (nargs), make_number (0));
2695 for (i = 0; i < nargs; i++)
2696 {
2697 SSET (result, i, XINT (args[i]));
2698 /* Move the meta bit to the right place for a string char. */
2699 if (XINT (args[i]) & CHAR_META)
2700 SSET (result, i, SREF (result, i) | 0x80);
2701 }
2702
2703 return result;
2704 }
2705 }
2706
2707
2708 \f
2709 /************************************************************************
2710 C Stack Marking
2711 ************************************************************************/
2712
2713 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
2714
2715 /* Conservative C stack marking requires a method to identify possibly
2716 live Lisp objects given a pointer value. We do this by keeping
2717 track of blocks of Lisp data that are allocated in a red-black tree
2718 (see also the comment of mem_node which is the type of nodes in
2719 that tree). Function lisp_malloc adds information for an allocated
2720 block to the red-black tree with calls to mem_insert, and function
2721 lisp_free removes it with mem_delete. Functions live_string_p etc
2722 call mem_find to lookup information about a given pointer in the
2723 tree, and use that to determine if the pointer points to a Lisp
2724 object or not. */
2725
2726 /* Initialize this part of alloc.c. */
2727
2728 static void
2729 mem_init ()
2730 {
2731 mem_z.left = mem_z.right = MEM_NIL;
2732 mem_z.parent = NULL;
2733 mem_z.color = MEM_BLACK;
2734 mem_z.start = mem_z.end = NULL;
2735 mem_root = MEM_NIL;
2736 }
2737
2738
2739 /* Value is a pointer to the mem_node containing START. Value is
2740 MEM_NIL if there is no node in the tree containing START. */
2741
2742 static INLINE struct mem_node *
2743 mem_find (start)
2744 void *start;
2745 {
2746 struct mem_node *p;
2747
2748 if (start < min_heap_address || start > max_heap_address)
2749 return MEM_NIL;
2750
2751 /* Make the search always successful to speed up the loop below. */
2752 mem_z.start = start;
2753 mem_z.end = (char *) start + 1;
2754
2755 p = mem_root;
2756 while (start < p->start || start >= p->end)
2757 p = start < p->start ? p->left : p->right;
2758 return p;
2759 }
2760
2761
2762 /* Insert a new node into the tree for a block of memory with start
2763 address START, end address END, and type TYPE. Value is a
2764 pointer to the node that was inserted. */
2765
2766 static struct mem_node *
2767 mem_insert (start, end, type)
2768 void *start, *end;
2769 enum mem_type type;
2770 {
2771 struct mem_node *c, *parent, *x;
2772
2773 if (start < min_heap_address)
2774 min_heap_address = start;
2775 if (end > max_heap_address)
2776 max_heap_address = end;
2777
2778 /* See where in the tree a node for START belongs. In this
2779 particular application, it shouldn't happen that a node is already
2780 present. For debugging purposes, let's check that. */
2781 c = mem_root;
2782 parent = NULL;
2783
2784 #if GC_MARK_STACK != GC_MAKE_GCPROS_NOOPS
2785
2786 while (c != MEM_NIL)
2787 {
2788 if (start >= c->start && start < c->end)
2789 abort ();
2790 parent = c;
2791 c = start < c->start ? c->left : c->right;
2792 }
2793
2794 #else /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
2795
2796 while (c != MEM_NIL)
2797 {
2798 parent = c;
2799 c = start < c->start ? c->left : c->right;
2800 }
2801
2802 #endif /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
2803
2804 /* Create a new node. */
2805 #ifdef GC_MALLOC_CHECK
2806 x = (struct mem_node *) _malloc_internal (sizeof *x);
2807 if (x == NULL)
2808 abort ();
2809 #else
2810 x = (struct mem_node *) xmalloc (sizeof *x);
2811 #endif
2812 x->start = start;
2813 x->end = end;
2814 x->type = type;
2815 x->parent = parent;
2816 x->left = x->right = MEM_NIL;
2817 x->color = MEM_RED;
2818
2819 /* Insert it as child of PARENT or install it as root. */
2820 if (parent)
2821 {
2822 if (start < parent->start)
2823 parent->left = x;
2824 else
2825 parent->right = x;
2826 }
2827 else
2828 mem_root = x;
2829
2830 /* Re-establish red-black tree properties. */
2831 mem_insert_fixup (x);
2832
2833 return x;
2834 }
2835
2836
2837 /* Re-establish the red-black properties of the tree, and thereby
2838 balance the tree, after node X has been inserted; X is always red. */
2839
2840 static void
2841 mem_insert_fixup (x)
2842 struct mem_node *x;
2843 {
2844 while (x != mem_root && x->parent->color == MEM_RED)
2845 {
2846 /* X is red and its parent is red. This is a violation of
2847 red-black tree property #3. */
2848
2849 if (x->parent == x->parent->parent->left)
2850 {
2851 /* We're on the left side of our grandparent, and Y is our
2852 "uncle". */
2853 struct mem_node *y = x->parent->parent->right;
2854
2855 if (y->color == MEM_RED)
2856 {
2857 /* Uncle and parent are red but should be black because
2858 X is red. Change the colors accordingly and proceed
2859 with the grandparent. */
2860 x->parent->color = MEM_BLACK;
2861 y->color = MEM_BLACK;
2862 x->parent->parent->color = MEM_RED;
2863 x = x->parent->parent;
2864 }
2865 else
2866 {
2867 /* Parent and uncle have different colors; parent is
2868 red, uncle is black. */
2869 if (x == x->parent->right)
2870 {
2871 x = x->parent;
2872 mem_rotate_left (x);
2873 }
2874
2875 x->parent->color = MEM_BLACK;
2876 x->parent->parent->color = MEM_RED;
2877 mem_rotate_right (x->parent->parent);
2878 }
2879 }
2880 else
2881 {
2882 /* This is the symmetrical case of above. */
2883 struct mem_node *y = x->parent->parent->left;
2884
2885 if (y->color == MEM_RED)
2886 {
2887 x->parent->color = MEM_BLACK;
2888 y->color = MEM_BLACK;
2889 x->parent->parent->color = MEM_RED;
2890 x = x->parent->parent;
2891 }
2892 else
2893 {
2894 if (x == x->parent->left)
2895 {
2896 x = x->parent;
2897 mem_rotate_right (x);
2898 }
2899
2900 x->parent->color = MEM_BLACK;
2901 x->parent->parent->color = MEM_RED;
2902 mem_rotate_left (x->parent->parent);
2903 }
2904 }
2905 }
2906
2907 /* The root may have been changed to red due to the algorithm. Set
2908 it to black so that property #5 is satisfied. */
2909 mem_root->color = MEM_BLACK;
2910 }
2911
2912
2913 /* (x) (y)
2914 / \ / \
2915 a (y) ===> (x) c
2916 / \ / \
2917 b c a b */
2918
2919 static void
2920 mem_rotate_left (x)
2921 struct mem_node *x;
2922 {
2923 struct mem_node *y;
2924
2925 /* Turn y's left sub-tree into x's right sub-tree. */
2926 y = x->right;
2927 x->right = y->left;
2928 if (y->left != MEM_NIL)
2929 y->left->parent = x;
2930
2931 /* Y's parent was x's parent. */
2932 if (y != MEM_NIL)
2933 y->parent = x->parent;
2934
2935 /* Get the parent to point to y instead of x. */
2936 if (x->parent)
2937 {
2938 if (x == x->parent->left)
2939 x->parent->left = y;
2940 else
2941 x->parent->right = y;
2942 }
2943 else
2944 mem_root = y;
2945
2946 /* Put x on y's left. */
2947 y->left = x;
2948 if (x != MEM_NIL)
2949 x->parent = y;
2950 }
2951
2952
2953 /* (x) (Y)
2954 / \ / \
2955 (y) c ===> a (x)
2956 / \ / \
2957 a b b c */
2958
2959 static void
2960 mem_rotate_right (x)
2961 struct mem_node *x;
2962 {
2963 struct mem_node *y = x->left;
2964
2965 x->left = y->right;
2966 if (y->right != MEM_NIL)
2967 y->right->parent = x;
2968
2969 if (y != MEM_NIL)
2970 y->parent = x->parent;
2971 if (x->parent)
2972 {
2973 if (x == x->parent->right)
2974 x->parent->right = y;
2975 else
2976 x->parent->left = y;
2977 }
2978 else
2979 mem_root = y;
2980
2981 y->right = x;
2982 if (x != MEM_NIL)
2983 x->parent = y;
2984 }
2985
2986
2987 /* Delete node Z from the tree. If Z is null or MEM_NIL, do nothing. */
2988
2989 static void
2990 mem_delete (z)
2991 struct mem_node *z;
2992 {
2993 struct mem_node *x, *y;
2994
2995 if (!z || z == MEM_NIL)
2996 return;
2997
2998 if (z->left == MEM_NIL || z->right == MEM_NIL)
2999 y = z;
3000 else
3001 {
3002 y = z->right;
3003 while (y->left != MEM_NIL)
3004 y = y->left;
3005 }
3006
3007 if (y->left != MEM_NIL)
3008 x = y->left;
3009 else
3010 x = y->right;
3011
3012 x->parent = y->parent;
3013 if (y->parent)
3014 {
3015 if (y == y->parent->left)
3016 y->parent->left = x;
3017 else
3018 y->parent->right = x;
3019 }
3020 else
3021 mem_root = x;
3022
3023 if (y != z)
3024 {
3025 z->start = y->start;
3026 z->end = y->end;
3027 z->type = y->type;
3028 }
3029
3030 if (y->color == MEM_BLACK)
3031 mem_delete_fixup (x);
3032
3033 #ifdef GC_MALLOC_CHECK
3034 _free_internal (y);
3035 #else
3036 xfree (y);
3037 #endif
3038 }
3039
3040
3041 /* Re-establish the red-black properties of the tree, after a
3042 deletion. */
3043
3044 static void
3045 mem_delete_fixup (x)
3046 struct mem_node *x;
3047 {
3048 while (x != mem_root && x->color == MEM_BLACK)
3049 {
3050 if (x == x->parent->left)
3051 {
3052 struct mem_node *w = x->parent->right;
3053
3054 if (w->color == MEM_RED)
3055 {
3056 w->color = MEM_BLACK;
3057 x->parent->color = MEM_RED;
3058 mem_rotate_left (x->parent);
3059 w = x->parent->right;
3060 }
3061
3062 if (w->left->color == MEM_BLACK && w->right->color == MEM_BLACK)
3063 {
3064 w->color = MEM_RED;
3065 x = x->parent;
3066 }
3067 else
3068 {
3069 if (w->right->color == MEM_BLACK)
3070 {
3071 w->left->color = MEM_BLACK;
3072 w->color = MEM_RED;
3073 mem_rotate_right (w);
3074 w = x->parent->right;
3075 }
3076 w->color = x->parent->color;
3077 x->parent->color = MEM_BLACK;
3078 w->right->color = MEM_BLACK;
3079 mem_rotate_left (x->parent);
3080 x = mem_root;
3081 }
3082 }
3083 else
3084 {
3085 struct mem_node *w = x->parent->left;
3086
3087 if (w->color == MEM_RED)
3088 {
3089 w->color = MEM_BLACK;
3090 x->parent->color = MEM_RED;
3091 mem_rotate_right (x->parent);
3092 w = x->parent->left;
3093 }
3094
3095 if (w->right->color == MEM_BLACK && w->left->color == MEM_BLACK)
3096 {
3097 w->color = MEM_RED;
3098 x = x->parent;
3099 }
3100 else
3101 {
3102 if (w->left->color == MEM_BLACK)
3103 {
3104 w->right->color = MEM_BLACK;
3105 w->color = MEM_RED;
3106 mem_rotate_left (w);
3107 w = x->parent->left;
3108 }
3109
3110 w->color = x->parent->color;
3111 x->parent->color = MEM_BLACK;
3112 w->left->color = MEM_BLACK;
3113 mem_rotate_right (x->parent);
3114 x = mem_root;
3115 }
3116 }
3117 }
3118
3119 x->color = MEM_BLACK;
3120 }
3121
3122
3123 /* Value is non-zero if P is a pointer to a live Lisp string on
3124 the heap. M is a pointer to the mem_block for P. */
3125
3126 static INLINE int
3127 live_string_p (m, p)
3128 struct mem_node *m;
3129 void *p;
3130 {
3131 if (m->type == MEM_TYPE_STRING)
3132 {
3133 struct string_block *b = (struct string_block *) m->start;
3134 int offset = (char *) p - (char *) &b->strings[0];
3135
3136 /* P must point to the start of a Lisp_String structure, and it
3137 must not be on the free-list. */
3138 return (offset >= 0
3139 && offset % sizeof b->strings[0] == 0
3140 && ((struct Lisp_String *) p)->data != NULL);
3141 }
3142 else
3143 return 0;
3144 }
3145
3146
3147 /* Value is non-zero if P is a pointer to a live Lisp cons on
3148 the heap. M is a pointer to the mem_block for P. */
3149
3150 static INLINE int
3151 live_cons_p (m, p)
3152 struct mem_node *m;
3153 void *p;
3154 {
3155 if (m->type == MEM_TYPE_CONS)
3156 {
3157 struct cons_block *b = (struct cons_block *) m->start;
3158 int offset = (char *) p - (char *) &b->conses[0];
3159
3160 /* P must point to the start of a Lisp_Cons, not be
3161 one of the unused cells in the current cons block,
3162 and not be on the free-list. */
3163 return (offset >= 0
3164 && offset % sizeof b->conses[0] == 0
3165 && (b != cons_block
3166 || offset / sizeof b->conses[0] < cons_block_index)
3167 && !EQ (((struct Lisp_Cons *) p)->car, Vdead));
3168 }
3169 else
3170 return 0;
3171 }
3172
3173
3174 /* Value is non-zero if P is a pointer to a live Lisp symbol on
3175 the heap. M is a pointer to the mem_block for P. */
3176
3177 static INLINE int
3178 live_symbol_p (m, p)
3179 struct mem_node *m;
3180 void *p;
3181 {
3182 if (m->type == MEM_TYPE_SYMBOL)
3183 {
3184 struct symbol_block *b = (struct symbol_block *) m->start;
3185 int offset = (char *) p - (char *) &b->symbols[0];
3186
3187 /* P must point to the start of a Lisp_Symbol, not be
3188 one of the unused cells in the current symbol block,
3189 and not be on the free-list. */
3190 return (offset >= 0
3191 && offset % sizeof b->symbols[0] == 0
3192 && (b != symbol_block
3193 || offset / sizeof b->symbols[0] < symbol_block_index)
3194 && !EQ (((struct Lisp_Symbol *) p)->function, Vdead));
3195 }
3196 else
3197 return 0;
3198 }
3199
3200
3201 /* Value is non-zero if P is a pointer to a live Lisp float on
3202 the heap. M is a pointer to the mem_block for P. */
3203
3204 static INLINE int
3205 live_float_p (m, p)
3206 struct mem_node *m;
3207 void *p;
3208 {
3209 if (m->type == MEM_TYPE_FLOAT)
3210 {
3211 struct float_block *b = (struct float_block *) m->start;
3212 int offset = (char *) p - (char *) &b->floats[0];
3213
3214 /* P must point to the start of a Lisp_Float, not be
3215 one of the unused cells in the current float block,
3216 and not be on the free-list. */
3217 return (offset >= 0
3218 && offset % sizeof b->floats[0] == 0
3219 && (b != float_block
3220 || offset / sizeof b->floats[0] < float_block_index)
3221 && !EQ (((struct Lisp_Float *) p)->type, Vdead));
3222 }
3223 else
3224 return 0;
3225 }
3226
3227
3228 /* Value is non-zero if P is a pointer to a live Lisp Misc on
3229 the heap. M is a pointer to the mem_block for P. */
3230
3231 static INLINE int
3232 live_misc_p (m, p)
3233 struct mem_node *m;
3234 void *p;
3235 {
3236 if (m->type == MEM_TYPE_MISC)
3237 {
3238 struct marker_block *b = (struct marker_block *) m->start;
3239 int offset = (char *) p - (char *) &b->markers[0];
3240
3241 /* P must point to the start of a Lisp_Misc, not be
3242 one of the unused cells in the current misc block,
3243 and not be on the free-list. */
3244 return (offset >= 0
3245 && offset % sizeof b->markers[0] == 0
3246 && (b != marker_block
3247 || offset / sizeof b->markers[0] < marker_block_index)
3248 && ((union Lisp_Misc *) p)->u_marker.type != Lisp_Misc_Free);
3249 }
3250 else
3251 return 0;
3252 }
3253
3254
3255 /* Value is non-zero if P is a pointer to a live vector-like object.
3256 M is a pointer to the mem_block for P. */
3257
3258 static INLINE int
3259 live_vector_p (m, p)
3260 struct mem_node *m;
3261 void *p;
3262 {
3263 return (p == m->start
3264 && m->type >= MEM_TYPE_VECTOR
3265 && m->type <= MEM_TYPE_WINDOW);
3266 }
3267
3268
3269 /* Value is non-zero of P is a pointer to a live buffer. M is a
3270 pointer to the mem_block for P. */
3271
3272 static INLINE int
3273 live_buffer_p (m, p)
3274 struct mem_node *m;
3275 void *p;
3276 {
3277 /* P must point to the start of the block, and the buffer
3278 must not have been killed. */
3279 return (m->type == MEM_TYPE_BUFFER
3280 && p == m->start
3281 && !NILP (((struct buffer *) p)->name));
3282 }
3283
3284 #endif /* GC_MARK_STACK || defined GC_MALLOC_CHECK */
3285
3286 #if GC_MARK_STACK
3287
3288 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3289
3290 /* Array of objects that are kept alive because the C stack contains
3291 a pattern that looks like a reference to them . */
3292
3293 #define MAX_ZOMBIES 10
3294 static Lisp_Object zombies[MAX_ZOMBIES];
3295
3296 /* Number of zombie objects. */
3297
3298 static int nzombies;
3299
3300 /* Number of garbage collections. */
3301
3302 static int ngcs;
3303
3304 /* Average percentage of zombies per collection. */
3305
3306 static double avg_zombies;
3307
3308 /* Max. number of live and zombie objects. */
3309
3310 static int max_live, max_zombies;
3311
3312 /* Average number of live objects per GC. */
3313
3314 static double avg_live;
3315
3316 DEFUN ("gc-status", Fgc_status, Sgc_status, 0, 0, "",
3317 doc: /* Show information about live and zombie objects. */)
3318 ()
3319 {
3320 Lisp_Object args[7];
3321 args[0] = build_string ("%d GCs, avg live/zombies = %.2f/%.2f (%f%%), max %d/%d");
3322 args[1] = make_number (ngcs);
3323 args[2] = make_float (avg_live);
3324 args[3] = make_float (avg_zombies);
3325 args[4] = make_float (avg_zombies / avg_live / 100);
3326 args[5] = make_number (max_live);
3327 args[6] = make_number (max_zombies);
3328 return Fmessage (7, args);
3329 }
3330
3331 #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
3332
3333
3334 /* Mark OBJ if we can prove it's a Lisp_Object. */
3335
3336 static INLINE void
3337 mark_maybe_object (obj)
3338 Lisp_Object obj;
3339 {
3340 void *po = (void *) XPNTR (obj);
3341 struct mem_node *m = mem_find (po);
3342
3343 if (m != MEM_NIL)
3344 {
3345 int mark_p = 0;
3346
3347 switch (XGCTYPE (obj))
3348 {
3349 case Lisp_String:
3350 mark_p = (live_string_p (m, po)
3351 && !STRING_MARKED_P ((struct Lisp_String *) po));
3352 break;
3353
3354 case Lisp_Cons:
3355 mark_p = (live_cons_p (m, po)
3356 && !XMARKBIT (XCONS (obj)->car));
3357 break;
3358
3359 case Lisp_Symbol:
3360 mark_p = (live_symbol_p (m, po)
3361 && !XMARKBIT (XSYMBOL (obj)->plist));
3362 break;
3363
3364 case Lisp_Float:
3365 mark_p = (live_float_p (m, po)
3366 && !XMARKBIT (XFLOAT (obj)->type));
3367 break;
3368
3369 case Lisp_Vectorlike:
3370 /* Note: can't check GC_BUFFERP before we know it's a
3371 buffer because checking that dereferences the pointer
3372 PO which might point anywhere. */
3373 if (live_vector_p (m, po))
3374 mark_p = (!GC_SUBRP (obj)
3375 && !(XVECTOR (obj)->size & ARRAY_MARK_FLAG));
3376 else if (live_buffer_p (m, po))
3377 mark_p = GC_BUFFERP (obj) && !XMARKBIT (XBUFFER (obj)->name);
3378 break;
3379
3380 case Lisp_Misc:
3381 if (live_misc_p (m, po))
3382 {
3383 switch (XMISCTYPE (obj))
3384 {
3385 case Lisp_Misc_Marker:
3386 mark_p = !XMARKBIT (XMARKER (obj)->chain);
3387 break;
3388
3389 case Lisp_Misc_Buffer_Local_Value:
3390 case Lisp_Misc_Some_Buffer_Local_Value:
3391 mark_p = !XMARKBIT (XBUFFER_LOCAL_VALUE (obj)->realvalue);
3392 break;
3393
3394 case Lisp_Misc_Overlay:
3395 mark_p = !XMARKBIT (XOVERLAY (obj)->plist);
3396 break;
3397 }
3398 }
3399 break;
3400
3401 case Lisp_Int:
3402 case Lisp_Type_Limit:
3403 break;
3404 }
3405
3406 if (mark_p)
3407 {
3408 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3409 if (nzombies < MAX_ZOMBIES)
3410 zombies[nzombies] = *p;
3411 ++nzombies;
3412 #endif
3413 mark_object (&obj);
3414 }
3415 }
3416 }
3417
3418
3419 /* If P points to Lisp data, mark that as live if it isn't already
3420 marked. */
3421
3422 static INLINE void
3423 mark_maybe_pointer (p)
3424 void *p;
3425 {
3426 struct mem_node *m;
3427
3428 /* Quickly rule out some values which can't point to Lisp data. We
3429 assume that Lisp data is aligned on even addresses. */
3430 if ((EMACS_INT) p & 1)
3431 return;
3432
3433 m = mem_find (p);
3434 if (m != MEM_NIL)
3435 {
3436 Lisp_Object obj = Qnil;
3437
3438 switch (m->type)
3439 {
3440 case MEM_TYPE_NON_LISP:
3441 /* Nothing to do; not a pointer to Lisp memory. */
3442 break;
3443
3444 case MEM_TYPE_BUFFER:
3445 if (live_buffer_p (m, p)
3446 && !XMARKBIT (((struct buffer *) p)->name))
3447 XSETVECTOR (obj, p);
3448 break;
3449
3450 case MEM_TYPE_CONS:
3451 if (live_cons_p (m, p)
3452 && !XMARKBIT (((struct Lisp_Cons *) p)->car))
3453 XSETCONS (obj, p);
3454 break;
3455
3456 case MEM_TYPE_STRING:
3457 if (live_string_p (m, p)
3458 && !STRING_MARKED_P ((struct Lisp_String *) p))
3459 XSETSTRING (obj, p);
3460 break;
3461
3462 case MEM_TYPE_MISC:
3463 if (live_misc_p (m, p))
3464 {
3465 Lisp_Object tem;
3466 XSETMISC (tem, p);
3467
3468 switch (XMISCTYPE (tem))
3469 {
3470 case Lisp_Misc_Marker:
3471 if (!XMARKBIT (XMARKER (tem)->chain))
3472 obj = tem;
3473 break;
3474
3475 case Lisp_Misc_Buffer_Local_Value:
3476 case Lisp_Misc_Some_Buffer_Local_Value:
3477 if (!XMARKBIT (XBUFFER_LOCAL_VALUE (tem)->realvalue))
3478 obj = tem;
3479 break;
3480
3481 case Lisp_Misc_Overlay:
3482 if (!XMARKBIT (XOVERLAY (tem)->plist))
3483 obj = tem;
3484 break;
3485 }
3486 }
3487 break;
3488
3489 case MEM_TYPE_SYMBOL:
3490 if (live_symbol_p (m, p)
3491 && !XMARKBIT (((struct Lisp_Symbol *) p)->plist))
3492 XSETSYMBOL (obj, p);
3493 break;
3494
3495 case MEM_TYPE_FLOAT:
3496 if (live_float_p (m, p)
3497 && !XMARKBIT (((struct Lisp_Float *) p)->type))
3498 XSETFLOAT (obj, p);
3499 break;
3500
3501 case MEM_TYPE_VECTOR:
3502 case MEM_TYPE_PROCESS:
3503 case MEM_TYPE_HASH_TABLE:
3504 case MEM_TYPE_FRAME:
3505 case MEM_TYPE_WINDOW:
3506 if (live_vector_p (m, p))
3507 {
3508 Lisp_Object tem;
3509 XSETVECTOR (tem, p);
3510 if (!GC_SUBRP (tem)
3511 && !(XVECTOR (tem)->size & ARRAY_MARK_FLAG))
3512 obj = tem;
3513 }
3514 break;
3515
3516 default:
3517 abort ();
3518 }
3519
3520 if (!GC_NILP (obj))
3521 mark_object (&obj);
3522 }
3523 }
3524
3525
3526 /* Mark Lisp objects referenced from the address range START..END. */
3527
3528 static void
3529 mark_memory (start, end)
3530 void *start, *end;
3531 {
3532 Lisp_Object *p;
3533 void **pp;
3534
3535 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3536 nzombies = 0;
3537 #endif
3538
3539 /* Make START the pointer to the start of the memory region,
3540 if it isn't already. */
3541 if (end < start)
3542 {
3543 void *tem = start;
3544 start = end;
3545 end = tem;
3546 }
3547
3548 /* Mark Lisp_Objects. */
3549 for (p = (Lisp_Object *) start; (void *) p < end; ++p)
3550 mark_maybe_object (*p);
3551
3552 /* Mark Lisp data pointed to. This is necessary because, in some
3553 situations, the C compiler optimizes Lisp objects away, so that
3554 only a pointer to them remains. Example:
3555
3556 DEFUN ("testme", Ftestme, Stestme, 0, 0, 0, "")
3557 ()
3558 {
3559 Lisp_Object obj = build_string ("test");
3560 struct Lisp_String *s = XSTRING (obj);
3561 Fgarbage_collect ();
3562 fprintf (stderr, "test `%s'\n", s->data);
3563 return Qnil;
3564 }
3565
3566 Here, `obj' isn't really used, and the compiler optimizes it
3567 away. The only reference to the life string is through the
3568 pointer `s'. */
3569
3570 for (pp = (void **) start; (void *) pp < end; ++pp)
3571 mark_maybe_pointer (*pp);
3572 }
3573
3574 /* setjmp will work with GCC unless NON_SAVING_SETJMP is defined in
3575 the GCC system configuration. In gcc 3.2, the only systems for
3576 which this is so are i386-sco5 non-ELF, i386-sysv3 (maybe included
3577 by others?) and ns32k-pc532-min. */
3578
3579 #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
3580
3581 static int setjmp_tested_p, longjmps_done;
3582
3583 #define SETJMP_WILL_LIKELY_WORK "\
3584 \n\
3585 Emacs garbage collector has been changed to use conservative stack\n\
3586 marking. Emacs has determined that the method it uses to do the\n\
3587 marking will likely work on your system, but this isn't sure.\n\
3588 \n\
3589 If you are a system-programmer, or can get the help of a local wizard\n\
3590 who is, please take a look at the function mark_stack in alloc.c, and\n\
3591 verify that the methods used are appropriate for your system.\n\
3592 \n\
3593 Please mail the result to <emacs-devel@gnu.org>.\n\
3594 "
3595
3596 #define SETJMP_WILL_NOT_WORK "\
3597 \n\
3598 Emacs garbage collector has been changed to use conservative stack\n\
3599 marking. Emacs has determined that the default method it uses to do the\n\
3600 marking will not work on your system. We will need a system-dependent\n\
3601 solution for your system.\n\
3602 \n\
3603 Please take a look at the function mark_stack in alloc.c, and\n\
3604 try to find a way to make it work on your system.\n\
3605 \n\
3606 Note that you may get false negatives, depending on the compiler.\n\
3607 In particular, you need to use -O with GCC for this test.\n\
3608 \n\
3609 Please mail the result to <emacs-devel@gnu.org>.\n\
3610 "
3611
3612
3613 /* Perform a quick check if it looks like setjmp saves registers in a
3614 jmp_buf. Print a message to stderr saying so. When this test
3615 succeeds, this is _not_ a proof that setjmp is sufficient for
3616 conservative stack marking. Only the sources or a disassembly
3617 can prove that. */
3618
3619 static void
3620 test_setjmp ()
3621 {
3622 char buf[10];
3623 register int x;
3624 jmp_buf jbuf;
3625 int result = 0;
3626
3627 /* Arrange for X to be put in a register. */
3628 sprintf (buf, "1");
3629 x = strlen (buf);
3630 x = 2 * x - 1;
3631
3632 setjmp (jbuf);
3633 if (longjmps_done == 1)
3634 {
3635 /* Came here after the longjmp at the end of the function.
3636
3637 If x == 1, the longjmp has restored the register to its
3638 value before the setjmp, and we can hope that setjmp
3639 saves all such registers in the jmp_buf, although that
3640 isn't sure.
3641
3642 For other values of X, either something really strange is
3643 taking place, or the setjmp just didn't save the register. */
3644
3645 if (x == 1)
3646 fprintf (stderr, SETJMP_WILL_LIKELY_WORK);
3647 else
3648 {
3649 fprintf (stderr, SETJMP_WILL_NOT_WORK);
3650 exit (1);
3651 }
3652 }
3653
3654 ++longjmps_done;
3655 x = 2;
3656 if (longjmps_done == 1)
3657 longjmp (jbuf, 1);
3658 }
3659
3660 #endif /* not GC_SAVE_REGISTERS_ON_STACK && not GC_SETJMP_WORKS */
3661
3662
3663 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
3664
3665 /* Abort if anything GCPRO'd doesn't survive the GC. */
3666
3667 static void
3668 check_gcpros ()
3669 {
3670 struct gcpro *p;
3671 int i;
3672
3673 for (p = gcprolist; p; p = p->next)
3674 for (i = 0; i < p->nvars; ++i)
3675 if (!survives_gc_p (p->var[i]))
3676 abort ();
3677 }
3678
3679 #elif GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3680
3681 static void
3682 dump_zombies ()
3683 {
3684 int i;
3685
3686 fprintf (stderr, "\nZombies kept alive = %d:\n", nzombies);
3687 for (i = 0; i < min (MAX_ZOMBIES, nzombies); ++i)
3688 {
3689 fprintf (stderr, " %d = ", i);
3690 debug_print (zombies[i]);
3691 }
3692 }
3693
3694 #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
3695
3696
3697 /* Mark live Lisp objects on the C stack.
3698
3699 There are several system-dependent problems to consider when
3700 porting this to new architectures:
3701
3702 Processor Registers
3703
3704 We have to mark Lisp objects in CPU registers that can hold local
3705 variables or are used to pass parameters.
3706
3707 If GC_SAVE_REGISTERS_ON_STACK is defined, it should expand to
3708 something that either saves relevant registers on the stack, or
3709 calls mark_maybe_object passing it each register's contents.
3710
3711 If GC_SAVE_REGISTERS_ON_STACK is not defined, the current
3712 implementation assumes that calling setjmp saves registers we need
3713 to see in a jmp_buf which itself lies on the stack. This doesn't
3714 have to be true! It must be verified for each system, possibly
3715 by taking a look at the source code of setjmp.
3716
3717 Stack Layout
3718
3719 Architectures differ in the way their processor stack is organized.
3720 For example, the stack might look like this
3721
3722 +----------------+
3723 | Lisp_Object | size = 4
3724 +----------------+
3725 | something else | size = 2
3726 +----------------+
3727 | Lisp_Object | size = 4
3728 +----------------+
3729 | ... |
3730
3731 In such a case, not every Lisp_Object will be aligned equally. To
3732 find all Lisp_Object on the stack it won't be sufficient to walk
3733 the stack in steps of 4 bytes. Instead, two passes will be
3734 necessary, one starting at the start of the stack, and a second
3735 pass starting at the start of the stack + 2. Likewise, if the
3736 minimal alignment of Lisp_Objects on the stack is 1, four passes
3737 would be necessary, each one starting with one byte more offset
3738 from the stack start.
3739
3740 The current code assumes by default that Lisp_Objects are aligned
3741 equally on the stack. */
3742
3743 static void
3744 mark_stack ()
3745 {
3746 int i;
3747 jmp_buf j;
3748 volatile int stack_grows_down_p = (char *) &j > (char *) stack_base;
3749 void *end;
3750
3751 /* This trick flushes the register windows so that all the state of
3752 the process is contained in the stack. */
3753 #ifdef sparc
3754 asm ("ta 3");
3755 #endif
3756
3757 /* Save registers that we need to see on the stack. We need to see
3758 registers used to hold register variables and registers used to
3759 pass parameters. */
3760 #ifdef GC_SAVE_REGISTERS_ON_STACK
3761 GC_SAVE_REGISTERS_ON_STACK (end);
3762 #else /* not GC_SAVE_REGISTERS_ON_STACK */
3763
3764 #ifndef GC_SETJMP_WORKS /* If it hasn't been checked yet that
3765 setjmp will definitely work, test it
3766 and print a message with the result
3767 of the test. */
3768 if (!setjmp_tested_p)
3769 {
3770 setjmp_tested_p = 1;
3771 test_setjmp ();
3772 }
3773 #endif /* GC_SETJMP_WORKS */
3774
3775 setjmp (j);
3776 end = stack_grows_down_p ? (char *) &j + sizeof j : (char *) &j;
3777 #endif /* not GC_SAVE_REGISTERS_ON_STACK */
3778
3779 /* This assumes that the stack is a contiguous region in memory. If
3780 that's not the case, something has to be done here to iterate
3781 over the stack segments. */
3782 #ifndef GC_LISP_OBJECT_ALIGNMENT
3783 #define GC_LISP_OBJECT_ALIGNMENT sizeof (Lisp_Object)
3784 #endif
3785 for (i = 0; i < sizeof (Lisp_Object); i += GC_LISP_OBJECT_ALIGNMENT)
3786 mark_memory ((char *) stack_base + i, end);
3787
3788 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
3789 check_gcpros ();
3790 #endif
3791 }
3792
3793
3794 #endif /* GC_MARK_STACK != 0 */
3795
3796
3797 \f
3798 /***********************************************************************
3799 Pure Storage Management
3800 ***********************************************************************/
3801
3802 /* Allocate room for SIZE bytes from pure Lisp storage and return a
3803 pointer to it. TYPE is the Lisp type for which the memory is
3804 allocated. TYPE < 0 means it's not used for a Lisp object.
3805
3806 If store_pure_type_info is set and TYPE is >= 0, the type of
3807 the allocated object is recorded in pure_types. */
3808
3809 static POINTER_TYPE *
3810 pure_alloc (size, type)
3811 size_t size;
3812 int type;
3813 {
3814 size_t nbytes;
3815 POINTER_TYPE *result;
3816 char *beg = purebeg;
3817
3818 /* Give Lisp_Floats an extra alignment. */
3819 if (type == Lisp_Float)
3820 {
3821 size_t alignment;
3822 #if defined __GNUC__ && __GNUC__ >= 2
3823 alignment = __alignof (struct Lisp_Float);
3824 #else
3825 alignment = sizeof (struct Lisp_Float);
3826 #endif
3827 pure_bytes_used = ALIGN (pure_bytes_used, alignment);
3828 }
3829
3830 nbytes = ALIGN (size, sizeof (EMACS_INT));
3831
3832 if (pure_bytes_used + nbytes > pure_size)
3833 {
3834 /* Don't allocate a large amount here,
3835 because it might get mmap'd and then its address
3836 might not be usable. */
3837 beg = purebeg = (char *) xmalloc (10000);
3838 pure_size = 10000;
3839 pure_bytes_used_before_overflow += pure_bytes_used;
3840 pure_bytes_used = 0;
3841 }
3842
3843 result = (POINTER_TYPE *) (beg + pure_bytes_used);
3844 pure_bytes_used += nbytes;
3845 return result;
3846 }
3847
3848
3849 /* Print a warning if PURESIZE is too small. */
3850
3851 void
3852 check_pure_size ()
3853 {
3854 if (pure_bytes_used_before_overflow)
3855 message ("Pure Lisp storage overflow (approx. %d bytes needed)",
3856 (int) (pure_bytes_used + pure_bytes_used_before_overflow));
3857 }
3858
3859
3860 /* Return a string allocated in pure space. DATA is a buffer holding
3861 NCHARS characters, and NBYTES bytes of string data. MULTIBYTE
3862 non-zero means make the result string multibyte.
3863
3864 Must get an error if pure storage is full, since if it cannot hold
3865 a large string it may be able to hold conses that point to that
3866 string; then the string is not protected from gc. */
3867
3868 Lisp_Object
3869 make_pure_string (data, nchars, nbytes, multibyte)
3870 char *data;
3871 int nchars, nbytes;
3872 int multibyte;
3873 {
3874 Lisp_Object string;
3875 struct Lisp_String *s;
3876
3877 s = (struct Lisp_String *) pure_alloc (sizeof *s, Lisp_String);
3878 s->data = (unsigned char *) pure_alloc (nbytes + 1, -1);
3879 s->size = nchars;
3880 s->size_byte = multibyte ? nbytes : -1;
3881 bcopy (data, s->data, nbytes);
3882 s->data[nbytes] = '\0';
3883 s->intervals = NULL_INTERVAL;
3884 XSETSTRING (string, s);
3885 return string;
3886 }
3887
3888
3889 /* Return a cons allocated from pure space. Give it pure copies
3890 of CAR as car and CDR as cdr. */
3891
3892 Lisp_Object
3893 pure_cons (car, cdr)
3894 Lisp_Object car, cdr;
3895 {
3896 register Lisp_Object new;
3897 struct Lisp_Cons *p;
3898
3899 p = (struct Lisp_Cons *) pure_alloc (sizeof *p, Lisp_Cons);
3900 XSETCONS (new, p);
3901 XSETCAR (new, Fpurecopy (car));
3902 XSETCDR (new, Fpurecopy (cdr));
3903 return new;
3904 }
3905
3906
3907 /* Value is a float object with value NUM allocated from pure space. */
3908
3909 Lisp_Object
3910 make_pure_float (num)
3911 double num;
3912 {
3913 register Lisp_Object new;
3914 struct Lisp_Float *p;
3915
3916 p = (struct Lisp_Float *) pure_alloc (sizeof *p, Lisp_Float);
3917 XSETFLOAT (new, p);
3918 XFLOAT_DATA (new) = num;
3919 return new;
3920 }
3921
3922
3923 /* Return a vector with room for LEN Lisp_Objects allocated from
3924 pure space. */
3925
3926 Lisp_Object
3927 make_pure_vector (len)
3928 EMACS_INT len;
3929 {
3930 Lisp_Object new;
3931 struct Lisp_Vector *p;
3932 size_t size = sizeof *p + (len - 1) * sizeof (Lisp_Object);
3933
3934 p = (struct Lisp_Vector *) pure_alloc (size, Lisp_Vectorlike);
3935 XSETVECTOR (new, p);
3936 XVECTOR (new)->size = len;
3937 return new;
3938 }
3939
3940
3941 DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
3942 doc: /* Make a copy of OBJECT in pure storage.
3943 Recursively copies contents of vectors and cons cells.
3944 Does not copy symbols. Copies strings without text properties. */)
3945 (obj)
3946 register Lisp_Object obj;
3947 {
3948 if (NILP (Vpurify_flag))
3949 return obj;
3950
3951 if (PURE_POINTER_P (XPNTR (obj)))
3952 return obj;
3953
3954 if (CONSP (obj))
3955 return pure_cons (XCAR (obj), XCDR (obj));
3956 else if (FLOATP (obj))
3957 return make_pure_float (XFLOAT_DATA (obj));
3958 else if (STRINGP (obj))
3959 return make_pure_string (SDATA (obj), SCHARS (obj),
3960 SBYTES (obj),
3961 STRING_MULTIBYTE (obj));
3962 else if (COMPILEDP (obj) || VECTORP (obj))
3963 {
3964 register struct Lisp_Vector *vec;
3965 register int i, size;
3966
3967 size = XVECTOR (obj)->size;
3968 if (size & PSEUDOVECTOR_FLAG)
3969 size &= PSEUDOVECTOR_SIZE_MASK;
3970 vec = XVECTOR (make_pure_vector ((EMACS_INT) size));
3971 for (i = 0; i < size; i++)
3972 vec->contents[i] = Fpurecopy (XVECTOR (obj)->contents[i]);
3973 if (COMPILEDP (obj))
3974 XSETCOMPILED (obj, vec);
3975 else
3976 XSETVECTOR (obj, vec);
3977 return obj;
3978 }
3979 else if (MARKERP (obj))
3980 error ("Attempt to copy a marker to pure storage");
3981
3982 return obj;
3983 }
3984
3985
3986 \f
3987 /***********************************************************************
3988 Protection from GC
3989 ***********************************************************************/
3990
3991 /* Put an entry in staticvec, pointing at the variable with address
3992 VARADDRESS. */
3993
3994 void
3995 staticpro (varaddress)
3996 Lisp_Object *varaddress;
3997 {
3998 staticvec[staticidx++] = varaddress;
3999 if (staticidx >= NSTATICS)
4000 abort ();
4001 }
4002
4003 struct catchtag
4004 {
4005 Lisp_Object tag;
4006 Lisp_Object val;
4007 struct catchtag *next;
4008 };
4009
4010 struct backtrace
4011 {
4012 struct backtrace *next;
4013 Lisp_Object *function;
4014 Lisp_Object *args; /* Points to vector of args. */
4015 int nargs; /* Length of vector. */
4016 /* If nargs is UNEVALLED, args points to slot holding list of
4017 unevalled args. */
4018 char evalargs;
4019 };
4020
4021
4022 \f
4023 /***********************************************************************
4024 Protection from GC
4025 ***********************************************************************/
4026
4027 /* Temporarily prevent garbage collection. */
4028
4029 int
4030 inhibit_garbage_collection ()
4031 {
4032 int count = SPECPDL_INDEX ();
4033 int nbits = min (VALBITS, BITS_PER_INT);
4034
4035 specbind (Qgc_cons_threshold, make_number (((EMACS_INT) 1 << (nbits - 1)) - 1));
4036 return count;
4037 }
4038
4039
4040 DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
4041 doc: /* Reclaim storage for Lisp objects no longer needed.
4042 Returns info on amount of space in use:
4043 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)
4044 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS
4045 (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS)
4046 (USED-STRINGS . FREE-STRINGS))
4047 Garbage collection happens automatically if you cons more than
4048 `gc-cons-threshold' bytes of Lisp data since previous garbage collection. */)
4049 ()
4050 {
4051 register struct gcpro *tail;
4052 register struct specbinding *bind;
4053 struct catchtag *catch;
4054 struct handler *handler;
4055 register struct backtrace *backlist;
4056 char stack_top_variable;
4057 register int i;
4058 int message_p;
4059 Lisp_Object total[8];
4060 int count = SPECPDL_INDEX ();
4061
4062 /* Can't GC if pure storage overflowed because we can't determine
4063 if something is a pure object or not. */
4064 if (pure_bytes_used_before_overflow)
4065 return Qnil;
4066
4067 /* In case user calls debug_print during GC,
4068 don't let that cause a recursive GC. */
4069 consing_since_gc = 0;
4070
4071 /* Save what's currently displayed in the echo area. */
4072 message_p = push_message ();
4073 record_unwind_protect (pop_message_unwind, Qnil);
4074
4075 /* Save a copy of the contents of the stack, for debugging. */
4076 #if MAX_SAVE_STACK > 0
4077 if (NILP (Vpurify_flag))
4078 {
4079 i = &stack_top_variable - stack_bottom;
4080 if (i < 0) i = -i;
4081 if (i < MAX_SAVE_STACK)
4082 {
4083 if (stack_copy == 0)
4084 stack_copy = (char *) xmalloc (stack_copy_size = i);
4085 else if (stack_copy_size < i)
4086 stack_copy = (char *) xrealloc (stack_copy, (stack_copy_size = i));
4087 if (stack_copy)
4088 {
4089 if ((EMACS_INT) (&stack_top_variable - stack_bottom) > 0)
4090 bcopy (stack_bottom, stack_copy, i);
4091 else
4092 bcopy (&stack_top_variable, stack_copy, i);
4093 }
4094 }
4095 }
4096 #endif /* MAX_SAVE_STACK > 0 */
4097
4098 if (garbage_collection_messages)
4099 message1_nolog ("Garbage collecting...");
4100
4101 BLOCK_INPUT;
4102
4103 shrink_regexp_cache ();
4104
4105 /* Don't keep undo information around forever. */
4106 {
4107 register struct buffer *nextb = all_buffers;
4108
4109 while (nextb)
4110 {
4111 /* If a buffer's undo list is Qt, that means that undo is
4112 turned off in that buffer. Calling truncate_undo_list on
4113 Qt tends to return NULL, which effectively turns undo back on.
4114 So don't call truncate_undo_list if undo_list is Qt. */
4115 if (! EQ (nextb->undo_list, Qt))
4116 nextb->undo_list
4117 = truncate_undo_list (nextb->undo_list, undo_limit,
4118 undo_strong_limit);
4119
4120 /* Shrink buffer gaps, but skip indirect and dead buffers. */
4121 if (nextb->base_buffer == 0 && !NILP (nextb->name))
4122 {
4123 /* If a buffer's gap size is more than 10% of the buffer
4124 size, or larger than 2000 bytes, then shrink it
4125 accordingly. Keep a minimum size of 20 bytes. */
4126 int size = min (2000, max (20, (nextb->text->z_byte / 10)));
4127
4128 if (nextb->text->gap_size > size)
4129 {
4130 struct buffer *save_current = current_buffer;
4131 current_buffer = nextb;
4132 make_gap (-(nextb->text->gap_size - size));
4133 current_buffer = save_current;
4134 }
4135 }
4136
4137 nextb = nextb->next;
4138 }
4139 }
4140
4141 gc_in_progress = 1;
4142
4143 /* clear_marks (); */
4144
4145 /* Mark all the special slots that serve as the roots of accessibility.
4146
4147 Usually the special slots to mark are contained in particular structures.
4148 Then we know no slot is marked twice because the structures don't overlap.
4149 In some cases, the structures point to the slots to be marked.
4150 For these, we use MARKBIT to avoid double marking of the slot. */
4151
4152 for (i = 0; i < staticidx; i++)
4153 mark_object (staticvec[i]);
4154
4155 #if (GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS \
4156 || GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS)
4157 mark_stack ();
4158 #else
4159 for (tail = gcprolist; tail; tail = tail->next)
4160 for (i = 0; i < tail->nvars; i++)
4161 if (!XMARKBIT (tail->var[i]))
4162 {
4163 /* Explicit casting prevents compiler warning about
4164 discarding the `volatile' qualifier. */
4165 mark_object ((Lisp_Object *)&tail->var[i]);
4166 XMARK (tail->var[i]);
4167 }
4168 #endif
4169
4170 mark_byte_stack ();
4171 for (bind = specpdl; bind != specpdl_ptr; bind++)
4172 {
4173 mark_object (&bind->symbol);
4174 mark_object (&bind->old_value);
4175 }
4176 for (catch = catchlist; catch; catch = catch->next)
4177 {
4178 mark_object (&catch->tag);
4179 mark_object (&catch->val);
4180 }
4181 for (handler = handlerlist; handler; handler = handler->next)
4182 {
4183 mark_object (&handler->handler);
4184 mark_object (&handler->var);
4185 }
4186 for (backlist = backtrace_list; backlist; backlist = backlist->next)
4187 {
4188 if (!XMARKBIT (*backlist->function))
4189 {
4190 mark_object (backlist->function);
4191 XMARK (*backlist->function);
4192 }
4193 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
4194 i = 0;
4195 else
4196 i = backlist->nargs - 1;
4197 for (; i >= 0; i--)
4198 if (!XMARKBIT (backlist->args[i]))
4199 {
4200 mark_object (&backlist->args[i]);
4201 XMARK (backlist->args[i]);
4202 }
4203 }
4204 mark_kboards ();
4205
4206 /* Look thru every buffer's undo list
4207 for elements that update markers that were not marked,
4208 and delete them. */
4209 {
4210 register struct buffer *nextb = all_buffers;
4211
4212 while (nextb)
4213 {
4214 /* If a buffer's undo list is Qt, that means that undo is
4215 turned off in that buffer. Calling truncate_undo_list on
4216 Qt tends to return NULL, which effectively turns undo back on.
4217 So don't call truncate_undo_list if undo_list is Qt. */
4218 if (! EQ (nextb->undo_list, Qt))
4219 {
4220 Lisp_Object tail, prev;
4221 tail = nextb->undo_list;
4222 prev = Qnil;
4223 while (CONSP (tail))
4224 {
4225 if (GC_CONSP (XCAR (tail))
4226 && GC_MARKERP (XCAR (XCAR (tail)))
4227 && ! XMARKBIT (XMARKER (XCAR (XCAR (tail)))->chain))
4228 {
4229 if (NILP (prev))
4230 nextb->undo_list = tail = XCDR (tail);
4231 else
4232 {
4233 tail = XCDR (tail);
4234 XSETCDR (prev, tail);
4235 }
4236 }
4237 else
4238 {
4239 prev = tail;
4240 tail = XCDR (tail);
4241 }
4242 }
4243 }
4244
4245 nextb = nextb->next;
4246 }
4247 }
4248
4249 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4250 mark_stack ();
4251 #endif
4252
4253 gc_sweep ();
4254
4255 /* Clear the mark bits that we set in certain root slots. */
4256
4257 #if (GC_MARK_STACK == GC_USE_GCPROS_AS_BEFORE \
4258 || GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES)
4259 for (tail = gcprolist; tail; tail = tail->next)
4260 for (i = 0; i < tail->nvars; i++)
4261 XUNMARK (tail->var[i]);
4262 #endif
4263
4264 unmark_byte_stack ();
4265 for (backlist = backtrace_list; backlist; backlist = backlist->next)
4266 {
4267 XUNMARK (*backlist->function);
4268 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
4269 i = 0;
4270 else
4271 i = backlist->nargs - 1;
4272 for (; i >= 0; i--)
4273 XUNMARK (backlist->args[i]);
4274 }
4275 XUNMARK (buffer_defaults.name);
4276 XUNMARK (buffer_local_symbols.name);
4277
4278 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES && 0
4279 dump_zombies ();
4280 #endif
4281
4282 UNBLOCK_INPUT;
4283
4284 /* clear_marks (); */
4285 gc_in_progress = 0;
4286
4287 consing_since_gc = 0;
4288 if (gc_cons_threshold < 10000)
4289 gc_cons_threshold = 10000;
4290
4291 if (garbage_collection_messages)
4292 {
4293 if (message_p || minibuf_level > 0)
4294 restore_message ();
4295 else
4296 message1_nolog ("Garbage collecting...done");
4297 }
4298
4299 unbind_to (count, Qnil);
4300
4301 total[0] = Fcons (make_number (total_conses),
4302 make_number (total_free_conses));
4303 total[1] = Fcons (make_number (total_symbols),
4304 make_number (total_free_symbols));
4305 total[2] = Fcons (make_number (total_markers),
4306 make_number (total_free_markers));
4307 total[3] = make_number (total_string_size);
4308 total[4] = make_number (total_vector_size);
4309 total[5] = Fcons (make_number (total_floats),
4310 make_number (total_free_floats));
4311 total[6] = Fcons (make_number (total_intervals),
4312 make_number (total_free_intervals));
4313 total[7] = Fcons (make_number (total_strings),
4314 make_number (total_free_strings));
4315
4316 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4317 {
4318 /* Compute average percentage of zombies. */
4319 double nlive = 0;
4320
4321 for (i = 0; i < 7; ++i)
4322 nlive += XFASTINT (XCAR (total[i]));
4323
4324 avg_live = (avg_live * ngcs + nlive) / (ngcs + 1);
4325 max_live = max (nlive, max_live);
4326 avg_zombies = (avg_zombies * ngcs + nzombies) / (ngcs + 1);
4327 max_zombies = max (nzombies, max_zombies);
4328 ++ngcs;
4329 }
4330 #endif
4331
4332 if (!NILP (Vpost_gc_hook))
4333 {
4334 int count = inhibit_garbage_collection ();
4335 safe_run_hooks (Qpost_gc_hook);
4336 unbind_to (count, Qnil);
4337 }
4338
4339 return Flist (sizeof total / sizeof *total, total);
4340 }
4341
4342
4343 /* Mark Lisp objects in glyph matrix MATRIX. Currently the
4344 only interesting objects referenced from glyphs are strings. */
4345
4346 static void
4347 mark_glyph_matrix (matrix)
4348 struct glyph_matrix *matrix;
4349 {
4350 struct glyph_row *row = matrix->rows;
4351 struct glyph_row *end = row + matrix->nrows;
4352
4353 for (; row < end; ++row)
4354 if (row->enabled_p)
4355 {
4356 int area;
4357 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
4358 {
4359 struct glyph *glyph = row->glyphs[area];
4360 struct glyph *end_glyph = glyph + row->used[area];
4361
4362 for (; glyph < end_glyph; ++glyph)
4363 if (GC_STRINGP (glyph->object)
4364 && !STRING_MARKED_P (XSTRING (glyph->object)))
4365 mark_object (&glyph->object);
4366 }
4367 }
4368 }
4369
4370
4371 /* Mark Lisp faces in the face cache C. */
4372
4373 static void
4374 mark_face_cache (c)
4375 struct face_cache *c;
4376 {
4377 if (c)
4378 {
4379 int i, j;
4380 for (i = 0; i < c->used; ++i)
4381 {
4382 struct face *face = FACE_FROM_ID (c->f, i);
4383
4384 if (face)
4385 {
4386 for (j = 0; j < LFACE_VECTOR_SIZE; ++j)
4387 mark_object (&face->lface[j]);
4388 }
4389 }
4390 }
4391 }
4392
4393
4394 #ifdef HAVE_WINDOW_SYSTEM
4395
4396 /* Mark Lisp objects in image IMG. */
4397
4398 static void
4399 mark_image (img)
4400 struct image *img;
4401 {
4402 mark_object (&img->spec);
4403
4404 if (!NILP (img->data.lisp_val))
4405 mark_object (&img->data.lisp_val);
4406 }
4407
4408
4409 /* Mark Lisp objects in image cache of frame F. It's done this way so
4410 that we don't have to include xterm.h here. */
4411
4412 static void
4413 mark_image_cache (f)
4414 struct frame *f;
4415 {
4416 forall_images_in_image_cache (f, mark_image);
4417 }
4418
4419 #endif /* HAVE_X_WINDOWS */
4420
4421
4422 \f
4423 /* Mark reference to a Lisp_Object.
4424 If the object referred to has not been seen yet, recursively mark
4425 all the references contained in it. */
4426
4427 #define LAST_MARKED_SIZE 500
4428 Lisp_Object *last_marked[LAST_MARKED_SIZE];
4429 int last_marked_index;
4430
4431 /* For debugging--call abort when we cdr down this many
4432 links of a list, in mark_object. In debugging,
4433 the call to abort will hit a breakpoint.
4434 Normally this is zero and the check never goes off. */
4435 int mark_object_loop_halt;
4436
4437 void
4438 mark_object (argptr)
4439 Lisp_Object *argptr;
4440 {
4441 Lisp_Object *objptr = argptr;
4442 register Lisp_Object obj;
4443 #ifdef GC_CHECK_MARKED_OBJECTS
4444 void *po;
4445 struct mem_node *m;
4446 #endif
4447 int cdr_count = 0;
4448
4449 loop:
4450 obj = *objptr;
4451 loop2:
4452 XUNMARK (obj);
4453
4454 if (PURE_POINTER_P (XPNTR (obj)))
4455 return;
4456
4457 last_marked[last_marked_index++] = objptr;
4458 if (last_marked_index == LAST_MARKED_SIZE)
4459 last_marked_index = 0;
4460
4461 /* Perform some sanity checks on the objects marked here. Abort if
4462 we encounter an object we know is bogus. This increases GC time
4463 by ~80%, and requires compilation with GC_MARK_STACK != 0. */
4464 #ifdef GC_CHECK_MARKED_OBJECTS
4465
4466 po = (void *) XPNTR (obj);
4467
4468 /* Check that the object pointed to by PO is known to be a Lisp
4469 structure allocated from the heap. */
4470 #define CHECK_ALLOCATED() \
4471 do { \
4472 m = mem_find (po); \
4473 if (m == MEM_NIL) \
4474 abort (); \
4475 } while (0)
4476
4477 /* Check that the object pointed to by PO is live, using predicate
4478 function LIVEP. */
4479 #define CHECK_LIVE(LIVEP) \
4480 do { \
4481 if (!LIVEP (m, po)) \
4482 abort (); \
4483 } while (0)
4484
4485 /* Check both of the above conditions. */
4486 #define CHECK_ALLOCATED_AND_LIVE(LIVEP) \
4487 do { \
4488 CHECK_ALLOCATED (); \
4489 CHECK_LIVE (LIVEP); \
4490 } while (0) \
4491
4492 #else /* not GC_CHECK_MARKED_OBJECTS */
4493
4494 #define CHECK_ALLOCATED() (void) 0
4495 #define CHECK_LIVE(LIVEP) (void) 0
4496 #define CHECK_ALLOCATED_AND_LIVE(LIVEP) (void) 0
4497
4498 #endif /* not GC_CHECK_MARKED_OBJECTS */
4499
4500 switch (SWITCH_ENUM_CAST (XGCTYPE (obj)))
4501 {
4502 case Lisp_String:
4503 {
4504 register struct Lisp_String *ptr = XSTRING (obj);
4505 CHECK_ALLOCATED_AND_LIVE (live_string_p);
4506 MARK_INTERVAL_TREE (ptr->intervals);
4507 MARK_STRING (ptr);
4508 #ifdef GC_CHECK_STRING_BYTES
4509 /* Check that the string size recorded in the string is the
4510 same as the one recorded in the sdata structure. */
4511 CHECK_STRING_BYTES (ptr);
4512 #endif /* GC_CHECK_STRING_BYTES */
4513 }
4514 break;
4515
4516 case Lisp_Vectorlike:
4517 #ifdef GC_CHECK_MARKED_OBJECTS
4518 m = mem_find (po);
4519 if (m == MEM_NIL && !GC_SUBRP (obj)
4520 && po != &buffer_defaults
4521 && po != &buffer_local_symbols)
4522 abort ();
4523 #endif /* GC_CHECK_MARKED_OBJECTS */
4524
4525 if (GC_BUFFERP (obj))
4526 {
4527 if (!XMARKBIT (XBUFFER (obj)->name))
4528 {
4529 #ifdef GC_CHECK_MARKED_OBJECTS
4530 if (po != &buffer_defaults && po != &buffer_local_symbols)
4531 {
4532 struct buffer *b;
4533 for (b = all_buffers; b && b != po; b = b->next)
4534 ;
4535 if (b == NULL)
4536 abort ();
4537 }
4538 #endif /* GC_CHECK_MARKED_OBJECTS */
4539 mark_buffer (obj);
4540 }
4541 }
4542 else if (GC_SUBRP (obj))
4543 break;
4544 else if (GC_COMPILEDP (obj))
4545 /* We could treat this just like a vector, but it is better to
4546 save the COMPILED_CONSTANTS element for last and avoid
4547 recursion there. */
4548 {
4549 register struct Lisp_Vector *ptr = XVECTOR (obj);
4550 register EMACS_INT size = ptr->size;
4551 register int i;
4552
4553 if (size & ARRAY_MARK_FLAG)
4554 break; /* Already marked */
4555
4556 CHECK_LIVE (live_vector_p);
4557 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
4558 size &= PSEUDOVECTOR_SIZE_MASK;
4559 for (i = 0; i < size; i++) /* and then mark its elements */
4560 {
4561 if (i != COMPILED_CONSTANTS)
4562 mark_object (&ptr->contents[i]);
4563 }
4564 /* This cast should be unnecessary, but some Mips compiler complains
4565 (MIPS-ABI + SysVR4, DC/OSx, etc). */
4566 objptr = (Lisp_Object *) &ptr->contents[COMPILED_CONSTANTS];
4567 goto loop;
4568 }
4569 else if (GC_FRAMEP (obj))
4570 {
4571 register struct frame *ptr = XFRAME (obj);
4572 register EMACS_INT size = ptr->size;
4573
4574 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
4575 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
4576
4577 CHECK_LIVE (live_vector_p);
4578 mark_object (&ptr->name);
4579 mark_object (&ptr->icon_name);
4580 mark_object (&ptr->title);
4581 mark_object (&ptr->focus_frame);
4582 mark_object (&ptr->selected_window);
4583 mark_object (&ptr->minibuffer_window);
4584 mark_object (&ptr->param_alist);
4585 mark_object (&ptr->scroll_bars);
4586 mark_object (&ptr->condemned_scroll_bars);
4587 mark_object (&ptr->menu_bar_items);
4588 mark_object (&ptr->face_alist);
4589 mark_object (&ptr->menu_bar_vector);
4590 mark_object (&ptr->buffer_predicate);
4591 mark_object (&ptr->buffer_list);
4592 mark_object (&ptr->menu_bar_window);
4593 mark_object (&ptr->tool_bar_window);
4594 mark_face_cache (ptr->face_cache);
4595 #ifdef HAVE_WINDOW_SYSTEM
4596 mark_image_cache (ptr);
4597 mark_object (&ptr->tool_bar_items);
4598 mark_object (&ptr->desired_tool_bar_string);
4599 mark_object (&ptr->current_tool_bar_string);
4600 #endif /* HAVE_WINDOW_SYSTEM */
4601 }
4602 else if (GC_BOOL_VECTOR_P (obj))
4603 {
4604 register struct Lisp_Vector *ptr = XVECTOR (obj);
4605
4606 if (ptr->size & ARRAY_MARK_FLAG)
4607 break; /* Already marked */
4608 CHECK_LIVE (live_vector_p);
4609 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
4610 }
4611 else if (GC_WINDOWP (obj))
4612 {
4613 register struct Lisp_Vector *ptr = XVECTOR (obj);
4614 struct window *w = XWINDOW (obj);
4615 register EMACS_INT size = ptr->size;
4616 register int i;
4617
4618 /* Stop if already marked. */
4619 if (size & ARRAY_MARK_FLAG)
4620 break;
4621
4622 /* Mark it. */
4623 CHECK_LIVE (live_vector_p);
4624 ptr->size |= ARRAY_MARK_FLAG;
4625
4626 /* There is no Lisp data above The member CURRENT_MATRIX in
4627 struct WINDOW. Stop marking when that slot is reached. */
4628 for (i = 0;
4629 (char *) &ptr->contents[i] < (char *) &w->current_matrix;
4630 i++)
4631 mark_object (&ptr->contents[i]);
4632
4633 /* Mark glyphs for leaf windows. Marking window matrices is
4634 sufficient because frame matrices use the same glyph
4635 memory. */
4636 if (NILP (w->hchild)
4637 && NILP (w->vchild)
4638 && w->current_matrix)
4639 {
4640 mark_glyph_matrix (w->current_matrix);
4641 mark_glyph_matrix (w->desired_matrix);
4642 }
4643 }
4644 else if (GC_HASH_TABLE_P (obj))
4645 {
4646 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
4647 EMACS_INT size = h->size;
4648
4649 /* Stop if already marked. */
4650 if (size & ARRAY_MARK_FLAG)
4651 break;
4652
4653 /* Mark it. */
4654 CHECK_LIVE (live_vector_p);
4655 h->size |= ARRAY_MARK_FLAG;
4656
4657 /* Mark contents. */
4658 /* Do not mark next_free or next_weak.
4659 Being in the next_weak chain
4660 should not keep the hash table alive.
4661 No need to mark `count' since it is an integer. */
4662 mark_object (&h->test);
4663 mark_object (&h->weak);
4664 mark_object (&h->rehash_size);
4665 mark_object (&h->rehash_threshold);
4666 mark_object (&h->hash);
4667 mark_object (&h->next);
4668 mark_object (&h->index);
4669 mark_object (&h->user_hash_function);
4670 mark_object (&h->user_cmp_function);
4671
4672 /* If hash table is not weak, mark all keys and values.
4673 For weak tables, mark only the vector. */
4674 if (GC_NILP (h->weak))
4675 mark_object (&h->key_and_value);
4676 else
4677 XVECTOR (h->key_and_value)->size |= ARRAY_MARK_FLAG;
4678
4679 }
4680 else
4681 {
4682 register struct Lisp_Vector *ptr = XVECTOR (obj);
4683 register EMACS_INT size = ptr->size;
4684 register int i;
4685
4686 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
4687 CHECK_LIVE (live_vector_p);
4688 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
4689 if (size & PSEUDOVECTOR_FLAG)
4690 size &= PSEUDOVECTOR_SIZE_MASK;
4691
4692 for (i = 0; i < size; i++) /* and then mark its elements */
4693 mark_object (&ptr->contents[i]);
4694 }
4695 break;
4696
4697 case Lisp_Symbol:
4698 {
4699 register struct Lisp_Symbol *ptr = XSYMBOL (obj);
4700 struct Lisp_Symbol *ptrx;
4701
4702 if (XMARKBIT (ptr->plist)) break;
4703 CHECK_ALLOCATED_AND_LIVE (live_symbol_p);
4704 XMARK (ptr->plist);
4705 mark_object ((Lisp_Object *) &ptr->value);
4706 mark_object (&ptr->function);
4707 mark_object (&ptr->plist);
4708
4709 if (!PURE_POINTER_P (XSTRING (ptr->xname)))
4710 MARK_STRING (XSTRING (ptr->xname));
4711 MARK_INTERVAL_TREE (STRING_INTERVALS (ptr->xname));
4712
4713 /* Note that we do not mark the obarray of the symbol.
4714 It is safe not to do so because nothing accesses that
4715 slot except to check whether it is nil. */
4716 ptr = ptr->next;
4717 if (ptr)
4718 {
4719 /* For the benefit of the last_marked log. */
4720 objptr = (Lisp_Object *)&XSYMBOL (obj)->next;
4721 ptrx = ptr; /* Use of ptrx avoids compiler bug on Sun */
4722 XSETSYMBOL (obj, ptrx);
4723 /* We can't goto loop here because *objptr doesn't contain an
4724 actual Lisp_Object with valid datatype field. */
4725 goto loop2;
4726 }
4727 }
4728 break;
4729
4730 case Lisp_Misc:
4731 CHECK_ALLOCATED_AND_LIVE (live_misc_p);
4732 switch (XMISCTYPE (obj))
4733 {
4734 case Lisp_Misc_Marker:
4735 XMARK (XMARKER (obj)->chain);
4736 /* DO NOT mark thru the marker's chain.
4737 The buffer's markers chain does not preserve markers from gc;
4738 instead, markers are removed from the chain when freed by gc. */
4739 break;
4740
4741 case Lisp_Misc_Buffer_Local_Value:
4742 case Lisp_Misc_Some_Buffer_Local_Value:
4743 {
4744 register struct Lisp_Buffer_Local_Value *ptr
4745 = XBUFFER_LOCAL_VALUE (obj);
4746 if (XMARKBIT (ptr->realvalue)) break;
4747 XMARK (ptr->realvalue);
4748 /* If the cdr is nil, avoid recursion for the car. */
4749 if (EQ (ptr->cdr, Qnil))
4750 {
4751 objptr = &ptr->realvalue;
4752 goto loop;
4753 }
4754 mark_object (&ptr->realvalue);
4755 mark_object (&ptr->buffer);
4756 mark_object (&ptr->frame);
4757 objptr = &ptr->cdr;
4758 goto loop;
4759 }
4760
4761 case Lisp_Misc_Intfwd:
4762 case Lisp_Misc_Boolfwd:
4763 case Lisp_Misc_Objfwd:
4764 case Lisp_Misc_Buffer_Objfwd:
4765 case Lisp_Misc_Kboard_Objfwd:
4766 /* Don't bother with Lisp_Buffer_Objfwd,
4767 since all markable slots in current buffer marked anyway. */
4768 /* Don't need to do Lisp_Objfwd, since the places they point
4769 are protected with staticpro. */
4770 break;
4771
4772 case Lisp_Misc_Overlay:
4773 {
4774 struct Lisp_Overlay *ptr = XOVERLAY (obj);
4775 if (!XMARKBIT (ptr->plist))
4776 {
4777 XMARK (ptr->plist);
4778 mark_object (&ptr->start);
4779 mark_object (&ptr->end);
4780 objptr = &ptr->plist;
4781 goto loop;
4782 }
4783 }
4784 break;
4785
4786 default:
4787 abort ();
4788 }
4789 break;
4790
4791 case Lisp_Cons:
4792 {
4793 register struct Lisp_Cons *ptr = XCONS (obj);
4794 if (XMARKBIT (ptr->car)) break;
4795 CHECK_ALLOCATED_AND_LIVE (live_cons_p);
4796 XMARK (ptr->car);
4797 /* If the cdr is nil, avoid recursion for the car. */
4798 if (EQ (ptr->cdr, Qnil))
4799 {
4800 objptr = &ptr->car;
4801 cdr_count = 0;
4802 goto loop;
4803 }
4804 mark_object (&ptr->car);
4805 objptr = &ptr->cdr;
4806 cdr_count++;
4807 if (cdr_count == mark_object_loop_halt)
4808 abort ();
4809 goto loop;
4810 }
4811
4812 case Lisp_Float:
4813 CHECK_ALLOCATED_AND_LIVE (live_float_p);
4814 XMARK (XFLOAT (obj)->type);
4815 break;
4816
4817 case Lisp_Int:
4818 break;
4819
4820 default:
4821 abort ();
4822 }
4823
4824 #undef CHECK_LIVE
4825 #undef CHECK_ALLOCATED
4826 #undef CHECK_ALLOCATED_AND_LIVE
4827 }
4828
4829 /* Mark the pointers in a buffer structure. */
4830
4831 static void
4832 mark_buffer (buf)
4833 Lisp_Object buf;
4834 {
4835 register struct buffer *buffer = XBUFFER (buf);
4836 register Lisp_Object *ptr;
4837 Lisp_Object base_buffer;
4838
4839 /* This is the buffer's markbit */
4840 mark_object (&buffer->name);
4841 XMARK (buffer->name);
4842
4843 MARK_INTERVAL_TREE (BUF_INTERVALS (buffer));
4844
4845 if (CONSP (buffer->undo_list))
4846 {
4847 Lisp_Object tail;
4848 tail = buffer->undo_list;
4849
4850 while (CONSP (tail))
4851 {
4852 register struct Lisp_Cons *ptr = XCONS (tail);
4853
4854 if (XMARKBIT (ptr->car))
4855 break;
4856 XMARK (ptr->car);
4857 if (GC_CONSP (ptr->car)
4858 && ! XMARKBIT (XCAR (ptr->car))
4859 && GC_MARKERP (XCAR (ptr->car)))
4860 {
4861 XMARK (XCAR_AS_LVALUE (ptr->car));
4862 mark_object (&XCDR_AS_LVALUE (ptr->car));
4863 }
4864 else
4865 mark_object (&ptr->car);
4866
4867 if (CONSP (ptr->cdr))
4868 tail = ptr->cdr;
4869 else
4870 break;
4871 }
4872
4873 mark_object (&XCDR_AS_LVALUE (tail));
4874 }
4875 else
4876 mark_object (&buffer->undo_list);
4877
4878 for (ptr = &buffer->name + 1;
4879 (char *)ptr < (char *)buffer + sizeof (struct buffer);
4880 ptr++)
4881 mark_object (ptr);
4882
4883 /* If this is an indirect buffer, mark its base buffer. */
4884 if (buffer->base_buffer && !XMARKBIT (buffer->base_buffer->name))
4885 {
4886 XSETBUFFER (base_buffer, buffer->base_buffer);
4887 mark_buffer (base_buffer);
4888 }
4889 }
4890
4891
4892 /* Mark the pointers in the kboard objects. */
4893
4894 static void
4895 mark_kboards ()
4896 {
4897 KBOARD *kb;
4898 Lisp_Object *p;
4899 for (kb = all_kboards; kb; kb = kb->next_kboard)
4900 {
4901 if (kb->kbd_macro_buffer)
4902 for (p = kb->kbd_macro_buffer; p < kb->kbd_macro_ptr; p++)
4903 mark_object (p);
4904 mark_object (&kb->Voverriding_terminal_local_map);
4905 mark_object (&kb->Vlast_command);
4906 mark_object (&kb->Vreal_last_command);
4907 mark_object (&kb->Vprefix_arg);
4908 mark_object (&kb->Vlast_prefix_arg);
4909 mark_object (&kb->kbd_queue);
4910 mark_object (&kb->defining_kbd_macro);
4911 mark_object (&kb->Vlast_kbd_macro);
4912 mark_object (&kb->Vsystem_key_alist);
4913 mark_object (&kb->system_key_syms);
4914 mark_object (&kb->Vdefault_minibuffer_frame);
4915 mark_object (&kb->echo_string);
4916 }
4917 }
4918
4919
4920 /* Value is non-zero if OBJ will survive the current GC because it's
4921 either marked or does not need to be marked to survive. */
4922
4923 int
4924 survives_gc_p (obj)
4925 Lisp_Object obj;
4926 {
4927 int survives_p;
4928
4929 switch (XGCTYPE (obj))
4930 {
4931 case Lisp_Int:
4932 survives_p = 1;
4933 break;
4934
4935 case Lisp_Symbol:
4936 survives_p = XMARKBIT (XSYMBOL (obj)->plist);
4937 break;
4938
4939 case Lisp_Misc:
4940 switch (XMISCTYPE (obj))
4941 {
4942 case Lisp_Misc_Marker:
4943 survives_p = XMARKBIT (obj);
4944 break;
4945
4946 case Lisp_Misc_Buffer_Local_Value:
4947 case Lisp_Misc_Some_Buffer_Local_Value:
4948 survives_p = XMARKBIT (XBUFFER_LOCAL_VALUE (obj)->realvalue);
4949 break;
4950
4951 case Lisp_Misc_Intfwd:
4952 case Lisp_Misc_Boolfwd:
4953 case Lisp_Misc_Objfwd:
4954 case Lisp_Misc_Buffer_Objfwd:
4955 case Lisp_Misc_Kboard_Objfwd:
4956 survives_p = 1;
4957 break;
4958
4959 case Lisp_Misc_Overlay:
4960 survives_p = XMARKBIT (XOVERLAY (obj)->plist);
4961 break;
4962
4963 default:
4964 abort ();
4965 }
4966 break;
4967
4968 case Lisp_String:
4969 {
4970 struct Lisp_String *s = XSTRING (obj);
4971 survives_p = STRING_MARKED_P (s);
4972 }
4973 break;
4974
4975 case Lisp_Vectorlike:
4976 if (GC_BUFFERP (obj))
4977 survives_p = XMARKBIT (XBUFFER (obj)->name);
4978 else if (GC_SUBRP (obj))
4979 survives_p = 1;
4980 else
4981 survives_p = XVECTOR (obj)->size & ARRAY_MARK_FLAG;
4982 break;
4983
4984 case Lisp_Cons:
4985 survives_p = XMARKBIT (XCAR (obj));
4986 break;
4987
4988 case Lisp_Float:
4989 survives_p = XMARKBIT (XFLOAT (obj)->type);
4990 break;
4991
4992 default:
4993 abort ();
4994 }
4995
4996 return survives_p || PURE_POINTER_P ((void *) XPNTR (obj));
4997 }
4998
4999
5000 \f
5001 /* Sweep: find all structures not marked, and free them. */
5002
5003 static void
5004 gc_sweep ()
5005 {
5006 /* Remove or mark entries in weak hash tables.
5007 This must be done before any object is unmarked. */
5008 sweep_weak_hash_tables ();
5009
5010 sweep_strings ();
5011 #ifdef GC_CHECK_STRING_BYTES
5012 if (!noninteractive)
5013 check_string_bytes (1);
5014 #endif
5015
5016 /* Put all unmarked conses on free list */
5017 {
5018 register struct cons_block *cblk;
5019 struct cons_block **cprev = &cons_block;
5020 register int lim = cons_block_index;
5021 register int num_free = 0, num_used = 0;
5022
5023 cons_free_list = 0;
5024
5025 for (cblk = cons_block; cblk; cblk = *cprev)
5026 {
5027 register int i;
5028 int this_free = 0;
5029 for (i = 0; i < lim; i++)
5030 if (!XMARKBIT (cblk->conses[i].car))
5031 {
5032 this_free++;
5033 *(struct Lisp_Cons **)&cblk->conses[i].cdr = cons_free_list;
5034 cons_free_list = &cblk->conses[i];
5035 #if GC_MARK_STACK
5036 cons_free_list->car = Vdead;
5037 #endif
5038 }
5039 else
5040 {
5041 num_used++;
5042 XUNMARK (cblk->conses[i].car);
5043 }
5044 lim = CONS_BLOCK_SIZE;
5045 /* If this block contains only free conses and we have already
5046 seen more than two blocks worth of free conses then deallocate
5047 this block. */
5048 if (this_free == CONS_BLOCK_SIZE && num_free > CONS_BLOCK_SIZE)
5049 {
5050 *cprev = cblk->next;
5051 /* Unhook from the free list. */
5052 cons_free_list = *(struct Lisp_Cons **) &cblk->conses[0].cdr;
5053 lisp_free (cblk);
5054 n_cons_blocks--;
5055 }
5056 else
5057 {
5058 num_free += this_free;
5059 cprev = &cblk->next;
5060 }
5061 }
5062 total_conses = num_used;
5063 total_free_conses = num_free;
5064 }
5065
5066 /* Put all unmarked floats on free list */
5067 {
5068 register struct float_block *fblk;
5069 struct float_block **fprev = &float_block;
5070 register int lim = float_block_index;
5071 register int num_free = 0, num_used = 0;
5072
5073 float_free_list = 0;
5074
5075 for (fblk = float_block; fblk; fblk = *fprev)
5076 {
5077 register int i;
5078 int this_free = 0;
5079 for (i = 0; i < lim; i++)
5080 if (!XMARKBIT (fblk->floats[i].type))
5081 {
5082 this_free++;
5083 *(struct Lisp_Float **)&fblk->floats[i].data = float_free_list;
5084 float_free_list = &fblk->floats[i];
5085 #if GC_MARK_STACK
5086 float_free_list->type = Vdead;
5087 #endif
5088 }
5089 else
5090 {
5091 num_used++;
5092 XUNMARK (fblk->floats[i].type);
5093 }
5094 lim = FLOAT_BLOCK_SIZE;
5095 /* If this block contains only free floats and we have already
5096 seen more than two blocks worth of free floats then deallocate
5097 this block. */
5098 if (this_free == FLOAT_BLOCK_SIZE && num_free > FLOAT_BLOCK_SIZE)
5099 {
5100 *fprev = fblk->next;
5101 /* Unhook from the free list. */
5102 float_free_list = *(struct Lisp_Float **) &fblk->floats[0].data;
5103 lisp_free (fblk);
5104 n_float_blocks--;
5105 }
5106 else
5107 {
5108 num_free += this_free;
5109 fprev = &fblk->next;
5110 }
5111 }
5112 total_floats = num_used;
5113 total_free_floats = num_free;
5114 }
5115
5116 /* Put all unmarked intervals on free list */
5117 {
5118 register struct interval_block *iblk;
5119 struct interval_block **iprev = &interval_block;
5120 register int lim = interval_block_index;
5121 register int num_free = 0, num_used = 0;
5122
5123 interval_free_list = 0;
5124
5125 for (iblk = interval_block; iblk; iblk = *iprev)
5126 {
5127 register int i;
5128 int this_free = 0;
5129
5130 for (i = 0; i < lim; i++)
5131 {
5132 if (! XMARKBIT (iblk->intervals[i].plist))
5133 {
5134 SET_INTERVAL_PARENT (&iblk->intervals[i], interval_free_list);
5135 interval_free_list = &iblk->intervals[i];
5136 this_free++;
5137 }
5138 else
5139 {
5140 num_used++;
5141 XUNMARK (iblk->intervals[i].plist);
5142 }
5143 }
5144 lim = INTERVAL_BLOCK_SIZE;
5145 /* If this block contains only free intervals and we have already
5146 seen more than two blocks worth of free intervals then
5147 deallocate this block. */
5148 if (this_free == INTERVAL_BLOCK_SIZE && num_free > INTERVAL_BLOCK_SIZE)
5149 {
5150 *iprev = iblk->next;
5151 /* Unhook from the free list. */
5152 interval_free_list = INTERVAL_PARENT (&iblk->intervals[0]);
5153 lisp_free (iblk);
5154 n_interval_blocks--;
5155 }
5156 else
5157 {
5158 num_free += this_free;
5159 iprev = &iblk->next;
5160 }
5161 }
5162 total_intervals = num_used;
5163 total_free_intervals = num_free;
5164 }
5165
5166 /* Put all unmarked symbols on free list */
5167 {
5168 register struct symbol_block *sblk;
5169 struct symbol_block **sprev = &symbol_block;
5170 register int lim = symbol_block_index;
5171 register int num_free = 0, num_used = 0;
5172
5173 symbol_free_list = NULL;
5174
5175 for (sblk = symbol_block; sblk; sblk = *sprev)
5176 {
5177 int this_free = 0;
5178 struct Lisp_Symbol *sym = sblk->symbols;
5179 struct Lisp_Symbol *end = sym + lim;
5180
5181 for (; sym < end; ++sym)
5182 {
5183 /* Check if the symbol was created during loadup. In such a case
5184 it might be pointed to by pure bytecode which we don't trace,
5185 so we conservatively assume that it is live. */
5186 int pure_p = PURE_POINTER_P (XSTRING (sym->xname));
5187
5188 if (!XMARKBIT (sym->plist) && !pure_p)
5189 {
5190 *(struct Lisp_Symbol **) &sym->value = symbol_free_list;
5191 symbol_free_list = sym;
5192 #if GC_MARK_STACK
5193 symbol_free_list->function = Vdead;
5194 #endif
5195 ++this_free;
5196 }
5197 else
5198 {
5199 ++num_used;
5200 if (!pure_p)
5201 UNMARK_STRING (XSTRING (sym->xname));
5202 XUNMARK (sym->plist);
5203 }
5204 }
5205
5206 lim = SYMBOL_BLOCK_SIZE;
5207 /* If this block contains only free symbols and we have already
5208 seen more than two blocks worth of free symbols then deallocate
5209 this block. */
5210 if (this_free == SYMBOL_BLOCK_SIZE && num_free > SYMBOL_BLOCK_SIZE)
5211 {
5212 *sprev = sblk->next;
5213 /* Unhook from the free list. */
5214 symbol_free_list = *(struct Lisp_Symbol **)&sblk->symbols[0].value;
5215 lisp_free (sblk);
5216 n_symbol_blocks--;
5217 }
5218 else
5219 {
5220 num_free += this_free;
5221 sprev = &sblk->next;
5222 }
5223 }
5224 total_symbols = num_used;
5225 total_free_symbols = num_free;
5226 }
5227
5228 /* Put all unmarked misc's on free list.
5229 For a marker, first unchain it from the buffer it points into. */
5230 {
5231 register struct marker_block *mblk;
5232 struct marker_block **mprev = &marker_block;
5233 register int lim = marker_block_index;
5234 register int num_free = 0, num_used = 0;
5235
5236 marker_free_list = 0;
5237
5238 for (mblk = marker_block; mblk; mblk = *mprev)
5239 {
5240 register int i;
5241 int this_free = 0;
5242 EMACS_INT already_free = -1;
5243
5244 for (i = 0; i < lim; i++)
5245 {
5246 Lisp_Object *markword;
5247 switch (mblk->markers[i].u_marker.type)
5248 {
5249 case Lisp_Misc_Marker:
5250 markword = &mblk->markers[i].u_marker.chain;
5251 break;
5252 case Lisp_Misc_Buffer_Local_Value:
5253 case Lisp_Misc_Some_Buffer_Local_Value:
5254 markword = &mblk->markers[i].u_buffer_local_value.realvalue;
5255 break;
5256 case Lisp_Misc_Overlay:
5257 markword = &mblk->markers[i].u_overlay.plist;
5258 break;
5259 case Lisp_Misc_Free:
5260 /* If the object was already free, keep it
5261 on the free list. */
5262 markword = (Lisp_Object *) &already_free;
5263 break;
5264 default:
5265 markword = 0;
5266 break;
5267 }
5268 if (markword && !XMARKBIT (*markword))
5269 {
5270 Lisp_Object tem;
5271 if (mblk->markers[i].u_marker.type == Lisp_Misc_Marker)
5272 {
5273 /* tem1 avoids Sun compiler bug */
5274 struct Lisp_Marker *tem1 = &mblk->markers[i].u_marker;
5275 XSETMARKER (tem, tem1);
5276 unchain_marker (tem);
5277 }
5278 /* Set the type of the freed object to Lisp_Misc_Free.
5279 We could leave the type alone, since nobody checks it,
5280 but this might catch bugs faster. */
5281 mblk->markers[i].u_marker.type = Lisp_Misc_Free;
5282 mblk->markers[i].u_free.chain = marker_free_list;
5283 marker_free_list = &mblk->markers[i];
5284 this_free++;
5285 }
5286 else
5287 {
5288 num_used++;
5289 if (markword)
5290 XUNMARK (*markword);
5291 }
5292 }
5293 lim = MARKER_BLOCK_SIZE;
5294 /* If this block contains only free markers and we have already
5295 seen more than two blocks worth of free markers then deallocate
5296 this block. */
5297 if (this_free == MARKER_BLOCK_SIZE && num_free > MARKER_BLOCK_SIZE)
5298 {
5299 *mprev = mblk->next;
5300 /* Unhook from the free list. */
5301 marker_free_list = mblk->markers[0].u_free.chain;
5302 lisp_free (mblk);
5303 n_marker_blocks--;
5304 }
5305 else
5306 {
5307 num_free += this_free;
5308 mprev = &mblk->next;
5309 }
5310 }
5311
5312 total_markers = num_used;
5313 total_free_markers = num_free;
5314 }
5315
5316 /* Free all unmarked buffers */
5317 {
5318 register struct buffer *buffer = all_buffers, *prev = 0, *next;
5319
5320 while (buffer)
5321 if (!XMARKBIT (buffer->name))
5322 {
5323 if (prev)
5324 prev->next = buffer->next;
5325 else
5326 all_buffers = buffer->next;
5327 next = buffer->next;
5328 lisp_free (buffer);
5329 buffer = next;
5330 }
5331 else
5332 {
5333 XUNMARK (buffer->name);
5334 UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer));
5335 prev = buffer, buffer = buffer->next;
5336 }
5337 }
5338
5339 /* Free all unmarked vectors */
5340 {
5341 register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next;
5342 total_vector_size = 0;
5343
5344 while (vector)
5345 if (!(vector->size & ARRAY_MARK_FLAG))
5346 {
5347 if (prev)
5348 prev->next = vector->next;
5349 else
5350 all_vectors = vector->next;
5351 next = vector->next;
5352 lisp_free (vector);
5353 n_vectors--;
5354 vector = next;
5355
5356 }
5357 else
5358 {
5359 vector->size &= ~ARRAY_MARK_FLAG;
5360 if (vector->size & PSEUDOVECTOR_FLAG)
5361 total_vector_size += (PSEUDOVECTOR_SIZE_MASK & vector->size);
5362 else
5363 total_vector_size += vector->size;
5364 prev = vector, vector = vector->next;
5365 }
5366 }
5367
5368 #ifdef GC_CHECK_STRING_BYTES
5369 if (!noninteractive)
5370 check_string_bytes (1);
5371 #endif
5372 }
5373
5374
5375
5376 \f
5377 /* Debugging aids. */
5378
5379 DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0,
5380 doc: /* Return the address of the last byte Emacs has allocated, divided by 1024.
5381 This may be helpful in debugging Emacs's memory usage.
5382 We divide the value by 1024 to make sure it fits in a Lisp integer. */)
5383 ()
5384 {
5385 Lisp_Object end;
5386
5387 XSETINT (end, (EMACS_INT) sbrk (0) / 1024);
5388
5389 return end;
5390 }
5391
5392 DEFUN ("memory-use-counts", Fmemory_use_counts, Smemory_use_counts, 0, 0, 0,
5393 doc: /* Return a list of counters that measure how much consing there has been.
5394 Each of these counters increments for a certain kind of object.
5395 The counters wrap around from the largest positive integer to zero.
5396 Garbage collection does not decrease them.
5397 The elements of the value are as follows:
5398 (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS STRINGS)
5399 All are in units of 1 = one object consed
5400 except for VECTOR-CELLS and STRING-CHARS, which count the total length of
5401 objects consed.
5402 MISCS include overlays, markers, and some internal types.
5403 Frames, windows, buffers, and subprocesses count as vectors
5404 (but the contents of a buffer's text do not count here). */)
5405 ()
5406 {
5407 Lisp_Object consed[8];
5408
5409 consed[0] = make_number (min (MOST_POSITIVE_FIXNUM, cons_cells_consed));
5410 consed[1] = make_number (min (MOST_POSITIVE_FIXNUM, floats_consed));
5411 consed[2] = make_number (min (MOST_POSITIVE_FIXNUM, vector_cells_consed));
5412 consed[3] = make_number (min (MOST_POSITIVE_FIXNUM, symbols_consed));
5413 consed[4] = make_number (min (MOST_POSITIVE_FIXNUM, string_chars_consed));
5414 consed[5] = make_number (min (MOST_POSITIVE_FIXNUM, misc_objects_consed));
5415 consed[6] = make_number (min (MOST_POSITIVE_FIXNUM, intervals_consed));
5416 consed[7] = make_number (min (MOST_POSITIVE_FIXNUM, strings_consed));
5417
5418 return Flist (8, consed);
5419 }
5420
5421 int suppress_checking;
5422 void
5423 die (msg, file, line)
5424 const char *msg;
5425 const char *file;
5426 int line;
5427 {
5428 fprintf (stderr, "\r\nEmacs fatal error: %s:%d: %s\r\n",
5429 file, line, msg);
5430 abort ();
5431 }
5432 \f
5433 /* Initialization */
5434
5435 void
5436 init_alloc_once ()
5437 {
5438 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
5439 purebeg = PUREBEG;
5440 pure_size = PURESIZE;
5441 pure_bytes_used = 0;
5442 pure_bytes_used_before_overflow = 0;
5443
5444 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
5445 mem_init ();
5446 Vdead = make_pure_string ("DEAD", 4, 4, 0);
5447 #endif
5448
5449 all_vectors = 0;
5450 ignore_warnings = 1;
5451 #ifdef DOUG_LEA_MALLOC
5452 mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */
5453 mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */
5454 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); /* max. number of mmap'ed areas */
5455 #endif
5456 init_strings ();
5457 init_cons ();
5458 init_symbol ();
5459 init_marker ();
5460 init_float ();
5461 init_intervals ();
5462
5463 #ifdef REL_ALLOC
5464 malloc_hysteresis = 32;
5465 #else
5466 malloc_hysteresis = 0;
5467 #endif
5468
5469 spare_memory = (char *) malloc (SPARE_MEMORY);
5470
5471 ignore_warnings = 0;
5472 gcprolist = 0;
5473 byte_stack_list = 0;
5474 staticidx = 0;
5475 consing_since_gc = 0;
5476 gc_cons_threshold = 100000 * sizeof (Lisp_Object);
5477 #ifdef VIRT_ADDR_VARIES
5478 malloc_sbrk_unused = 1<<22; /* A large number */
5479 malloc_sbrk_used = 100000; /* as reasonable as any number */
5480 #endif /* VIRT_ADDR_VARIES */
5481 }
5482
5483 void
5484 init_alloc ()
5485 {
5486 gcprolist = 0;
5487 byte_stack_list = 0;
5488 #if GC_MARK_STACK
5489 #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
5490 setjmp_tested_p = longjmps_done = 0;
5491 #endif
5492 #endif
5493 }
5494
5495 void
5496 syms_of_alloc ()
5497 {
5498 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold,
5499 doc: /* *Number of bytes of consing between garbage collections.
5500 Garbage collection can happen automatically once this many bytes have been
5501 allocated since the last garbage collection. All data types count.
5502
5503 Garbage collection happens automatically only when `eval' is called.
5504
5505 By binding this temporarily to a large number, you can effectively
5506 prevent garbage collection during a part of the program. */);
5507
5508 DEFVAR_INT ("pure-bytes-used", &pure_bytes_used,
5509 doc: /* Number of bytes of sharable Lisp data allocated so far. */);
5510
5511 DEFVAR_INT ("cons-cells-consed", &cons_cells_consed,
5512 doc: /* Number of cons cells that have been consed so far. */);
5513
5514 DEFVAR_INT ("floats-consed", &floats_consed,
5515 doc: /* Number of floats that have been consed so far. */);
5516
5517 DEFVAR_INT ("vector-cells-consed", &vector_cells_consed,
5518 doc: /* Number of vector cells that have been consed so far. */);
5519
5520 DEFVAR_INT ("symbols-consed", &symbols_consed,
5521 doc: /* Number of symbols that have been consed so far. */);
5522
5523 DEFVAR_INT ("string-chars-consed", &string_chars_consed,
5524 doc: /* Number of string characters that have been consed so far. */);
5525
5526 DEFVAR_INT ("misc-objects-consed", &misc_objects_consed,
5527 doc: /* Number of miscellaneous objects that have been consed so far. */);
5528
5529 DEFVAR_INT ("intervals-consed", &intervals_consed,
5530 doc: /* Number of intervals that have been consed so far. */);
5531
5532 DEFVAR_INT ("strings-consed", &strings_consed,
5533 doc: /* Number of strings that have been consed so far. */);
5534
5535 DEFVAR_LISP ("purify-flag", &Vpurify_flag,
5536 doc: /* Non-nil means loading Lisp code in order to dump an executable.
5537 This means that certain objects should be allocated in shared (pure) space. */);
5538
5539 DEFVAR_INT ("undo-limit", &undo_limit,
5540 doc: /* Keep no more undo information once it exceeds this size.
5541 This limit is applied when garbage collection happens.
5542 The size is counted as the number of bytes occupied,
5543 which includes both saved text and other data. */);
5544 undo_limit = 20000;
5545
5546 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit,
5547 doc: /* Don't keep more than this much size of undo information.
5548 A command which pushes past this size is itself forgotten.
5549 This limit is applied when garbage collection happens.
5550 The size is counted as the number of bytes occupied,
5551 which includes both saved text and other data. */);
5552 undo_strong_limit = 30000;
5553
5554 DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages,
5555 doc: /* Non-nil means display messages at start and end of garbage collection. */);
5556 garbage_collection_messages = 0;
5557
5558 DEFVAR_LISP ("post-gc-hook", &Vpost_gc_hook,
5559 doc: /* Hook run after garbage collection has finished. */);
5560 Vpost_gc_hook = Qnil;
5561 Qpost_gc_hook = intern ("post-gc-hook");
5562 staticpro (&Qpost_gc_hook);
5563
5564 DEFVAR_LISP ("memory-signal-data", &Vmemory_signal_data,
5565 doc: /* Precomputed `signal' argument for memory-full error. */);
5566 /* We build this in advance because if we wait until we need it, we might
5567 not be able to allocate the memory to hold it. */
5568 Vmemory_signal_data
5569 = list2 (Qerror,
5570 build_string ("Memory exhausted--use M-x save-some-buffers then exit and restart Emacs"));
5571
5572 DEFVAR_LISP ("memory-full", &Vmemory_full,
5573 doc: /* Non-nil means we are handling a memory-full error. */);
5574 Vmemory_full = Qnil;
5575
5576 staticpro (&Qgc_cons_threshold);
5577 Qgc_cons_threshold = intern ("gc-cons-threshold");
5578
5579 staticpro (&Qchar_table_extra_slots);
5580 Qchar_table_extra_slots = intern ("char-table-extra-slots");
5581
5582 defsubr (&Scons);
5583 defsubr (&Slist);
5584 defsubr (&Svector);
5585 defsubr (&Smake_byte_code);
5586 defsubr (&Smake_list);
5587 defsubr (&Smake_vector);
5588 defsubr (&Smake_char_table);
5589 defsubr (&Smake_string);
5590 defsubr (&Smake_bool_vector);
5591 defsubr (&Smake_symbol);
5592 defsubr (&Smake_marker);
5593 defsubr (&Spurecopy);
5594 defsubr (&Sgarbage_collect);
5595 defsubr (&Smemory_limit);
5596 defsubr (&Smemory_use_counts);
5597
5598 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
5599 defsubr (&Sgc_status);
5600 #endif
5601 }