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