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