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