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