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