(search_buffer): Cast p_limit to EMACS_INT not 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. */
72int gc_cons_threshold;
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;
1323
1324 specbind (Qgc_cons_threshold, make_number ((1 << (VALBITS - 1)) - 1));
1325
1326 return count;
1327}
1328
7146af97
JB
1329DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
1330 "Reclaim storage for Lisp objects no longer needed.\n\
1331Returns info on amount of space in use:\n\
1332 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)\n\
1333 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS\n\
1334 (USED-FLOATS . FREE-FLOATS))\n\
1335Garbage collection happens automatically if you cons more than\n\
1336`gc-cons-threshold' bytes of Lisp data since previous garbage collection.")
1337 ()
1338{
1339 register struct gcpro *tail;
1340 register struct specbinding *bind;
1341 struct catchtag *catch;
1342 struct handler *handler;
1343 register struct backtrace *backlist;
1344 register Lisp_Object tem;
1345 char *omessage = echo_area_glyphs;
51056d11 1346 int omessage_length = echo_area_glyphs_length;
7146af97
JB
1347 char stack_top_variable;
1348 register int i;
1349
7146af97
JB
1350 /* Save a copy of the contents of the stack, for debugging. */
1351#if MAX_SAVE_STACK > 0
265a9e55 1352 if (NILP (Vpurify_flag))
7146af97
JB
1353 {
1354 i = &stack_top_variable - stack_bottom;
1355 if (i < 0) i = -i;
1356 if (i < MAX_SAVE_STACK)
1357 {
1358 if (stack_copy == 0)
9ac0d9e0 1359 stack_copy = (char *) xmalloc (stack_copy_size = i);
7146af97 1360 else if (stack_copy_size < i)
9ac0d9e0 1361 stack_copy = (char *) xrealloc (stack_copy, (stack_copy_size = i));
7146af97
JB
1362 if (stack_copy)
1363 {
42607681 1364 if ((EMACS_INT) (&stack_top_variable - stack_bottom) > 0)
7146af97
JB
1365 bcopy (stack_bottom, stack_copy, i);
1366 else
1367 bcopy (&stack_top_variable, stack_copy, i);
1368 }
1369 }
1370 }
1371#endif /* MAX_SAVE_STACK > 0 */
1372
1373 if (!noninteractive)
691c4285 1374 message1_nolog ("Garbage collecting...");
7146af97
JB
1375
1376 /* Don't keep command history around forever */
1377 tem = Fnthcdr (make_number (30), Vcommand_history);
1378 if (CONSP (tem))
1379 XCONS (tem)->cdr = Qnil;
ffd56f97 1380
7146af97
JB
1381 /* Likewise for undo information. */
1382 {
1383 register struct buffer *nextb = all_buffers;
1384
1385 while (nextb)
1386 {
ffd56f97
JB
1387 /* If a buffer's undo list is Qt, that means that undo is
1388 turned off in that buffer. Calling truncate_undo_list on
1389 Qt tends to return NULL, which effectively turns undo back on.
1390 So don't call truncate_undo_list if undo_list is Qt. */
1391 if (! EQ (nextb->undo_list, Qt))
1392 nextb->undo_list
502b9b64
JB
1393 = truncate_undo_list (nextb->undo_list, undo_limit,
1394 undo_strong_limit);
7146af97
JB
1395 nextb = nextb->next;
1396 }
1397 }
1398
1399 gc_in_progress = 1;
1400
1401/* clear_marks (); */
1402
1403 /* In each "large string", set the MARKBIT of the size field.
1404 That enables mark_object to recognize them. */
1405 {
1406 register struct string_block *b;
1407 for (b = large_string_blocks; b; b = b->next)
1408 ((struct Lisp_String *)(&b->chars[0]))->size |= MARKBIT;
1409 }
1410
1411 /* Mark all the special slots that serve as the roots of accessibility.
1412
1413 Usually the special slots to mark are contained in particular structures.
1414 Then we know no slot is marked twice because the structures don't overlap.
1415 In some cases, the structures point to the slots to be marked.
1416 For these, we use MARKBIT to avoid double marking of the slot. */
1417
1418 for (i = 0; i < staticidx; i++)
1419 mark_object (staticvec[i]);
1420 for (tail = gcprolist; tail; tail = tail->next)
1421 for (i = 0; i < tail->nvars; i++)
1422 if (!XMARKBIT (tail->var[i]))
1423 {
1424 mark_object (&tail->var[i]);
1425 XMARK (tail->var[i]);
1426 }
1427 for (bind = specpdl; bind != specpdl_ptr; bind++)
1428 {
1429 mark_object (&bind->symbol);
1430 mark_object (&bind->old_value);
1431 }
1432 for (catch = catchlist; catch; catch = catch->next)
1433 {
1434 mark_object (&catch->tag);
1435 mark_object (&catch->val);
1436 }
1437 for (handler = handlerlist; handler; handler = handler->next)
1438 {
1439 mark_object (&handler->handler);
1440 mark_object (&handler->var);
1441 }
1442 for (backlist = backtrace_list; backlist; backlist = backlist->next)
1443 {
1444 if (!XMARKBIT (*backlist->function))
1445 {
1446 mark_object (backlist->function);
1447 XMARK (*backlist->function);
1448 }
1449 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
1450 i = 0;
1451 else
1452 i = backlist->nargs - 1;
1453 for (; i >= 0; i--)
1454 if (!XMARKBIT (backlist->args[i]))
1455 {
1456 mark_object (&backlist->args[i]);
1457 XMARK (backlist->args[i]);
1458 }
1459 }
b875d3f7 1460 mark_kboards ();
7146af97
JB
1461
1462 gc_sweep ();
1463
1464 /* Clear the mark bits that we set in certain root slots. */
1465
1466 for (tail = gcprolist; tail; tail = tail->next)
1467 for (i = 0; i < tail->nvars; i++)
1468 XUNMARK (tail->var[i]);
1469 for (backlist = backtrace_list; backlist; backlist = backlist->next)
1470 {
1471 XUNMARK (*backlist->function);
1472 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
1473 i = 0;
1474 else
1475 i = backlist->nargs - 1;
1476 for (; i >= 0; i--)
1477 XUNMARK (backlist->args[i]);
1478 }
1479 XUNMARK (buffer_defaults.name);
1480 XUNMARK (buffer_local_symbols.name);
1481
1482/* clear_marks (); */
1483 gc_in_progress = 0;
1484
1485 consing_since_gc = 0;
1486 if (gc_cons_threshold < 10000)
1487 gc_cons_threshold = 10000;
1488
7d385b05 1489 if (omessage || minibuf_level > 0)
691c4285 1490 message2_nolog (omessage, omessage_length);
7146af97 1491 else if (!noninteractive)
691c4285 1492 message1_nolog ("Garbage collecting...done");
7146af97 1493
7146af97
JB
1494 return Fcons (Fcons (make_number (total_conses),
1495 make_number (total_free_conses)),
1496 Fcons (Fcons (make_number (total_symbols),
1497 make_number (total_free_symbols)),
1498 Fcons (Fcons (make_number (total_markers),
1499 make_number (total_free_markers)),
1500 Fcons (make_number (total_string_size),
1501 Fcons (make_number (total_vector_size),
1502
1503#ifdef LISP_FLOAT_TYPE
1504 Fcons (Fcons (make_number (total_floats),
1505 make_number (total_free_floats)),
1506 Qnil)
1507#else /* not LISP_FLOAT_TYPE */
1508 Qnil
1509#endif /* not LISP_FLOAT_TYPE */
1510 )))));
1511}
1512\f
1513#if 0
1514static void
1515clear_marks ()
1516{
1517 /* Clear marks on all conses */
1518 {
1519 register struct cons_block *cblk;
1520 register int lim = cons_block_index;
1521
1522 for (cblk = cons_block; cblk; cblk = cblk->next)
1523 {
1524 register int i;
1525 for (i = 0; i < lim; i++)
1526 XUNMARK (cblk->conses[i].car);
1527 lim = CONS_BLOCK_SIZE;
1528 }
1529 }
1530 /* Clear marks on all symbols */
1531 {
1532 register struct symbol_block *sblk;
1533 register int lim = symbol_block_index;
1534
1535 for (sblk = symbol_block; sblk; sblk = sblk->next)
1536 {
1537 register int i;
1538 for (i = 0; i < lim; i++)
1539 {
1540 XUNMARK (sblk->symbols[i].plist);
1541 }
1542 lim = SYMBOL_BLOCK_SIZE;
1543 }
1544 }
1545 /* Clear marks on all markers */
1546 {
1547 register struct marker_block *sblk;
1548 register int lim = marker_block_index;
1549
1550 for (sblk = marker_block; sblk; sblk = sblk->next)
1551 {
1552 register int i;
1553 for (i = 0; i < lim; i++)
a5da44fe 1554 if (sblk->markers[i].u_marker.type == Lisp_Misc_Marker)
a0a38eb7 1555 XUNMARK (sblk->markers[i].u_marker.chain);
7146af97
JB
1556 lim = MARKER_BLOCK_SIZE;
1557 }
1558 }
1559 /* Clear mark bits on all buffers */
1560 {
1561 register struct buffer *nextb = all_buffers;
1562
1563 while (nextb)
1564 {
1565 XUNMARK (nextb->name);
1566 nextb = nextb->next;
1567 }
1568 }
1569}
1570#endif
1571\f
1a4f1e2c
JB
1572/* Mark reference to a Lisp_Object.
1573 If the object referred to has not been seen yet, recursively mark
1574 all the references contained in it.
7146af97 1575
eb8c3be9 1576 If the object referenced is a short string, the referencing slot
7146af97
JB
1577 is threaded into a chain of such slots, pointed to from
1578 the `size' field of the string. The actual string size
1579 lives in the last slot in the chain. We recognize the end
1580 because it is < (unsigned) STRING_BLOCK_SIZE. */
1581
785cd37f
RS
1582#define LAST_MARKED_SIZE 500
1583Lisp_Object *last_marked[LAST_MARKED_SIZE];
1584int last_marked_index;
1585
7146af97
JB
1586static void
1587mark_object (objptr)
1588 Lisp_Object *objptr;
1589{
1590 register Lisp_Object obj;
1591
9149e743 1592 loop:
7146af97 1593 obj = *objptr;
9149e743 1594 loop2:
7146af97
JB
1595 XUNMARK (obj);
1596
7146af97
JB
1597 if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
1598 && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
1599 return;
1600
785cd37f
RS
1601 last_marked[last_marked_index++] = objptr;
1602 if (last_marked_index == LAST_MARKED_SIZE)
1603 last_marked_index = 0;
1604
0220c518 1605 switch (SWITCH_ENUM_CAST (XGCTYPE (obj)))
7146af97
JB
1606 {
1607 case Lisp_String:
1608 {
1609 register struct Lisp_String *ptr = XSTRING (obj);
1610
d5e35230 1611 MARK_INTERVAL_TREE (ptr->intervals);
7146af97
JB
1612 if (ptr->size & MARKBIT)
1613 /* A large string. Just set ARRAY_MARK_FLAG. */
1614 ptr->size |= ARRAY_MARK_FLAG;
1615 else
1616 {
1617 /* A small string. Put this reference
1618 into the chain of references to it.
1fb577f7 1619 If the address includes MARKBIT, put that bit elsewhere
7146af97
JB
1620 when we store OBJPTR into the size field. */
1621
1622 if (XMARKBIT (*objptr))
1623 {
67ba9986 1624 XSETFASTINT (*objptr, ptr->size);
7146af97
JB
1625 XMARK (*objptr);
1626 }
1627 else
67ba9986 1628 XSETFASTINT (*objptr, ptr->size);
155ffe9c
RS
1629
1630 if ((EMACS_INT) objptr & DONT_COPY_FLAG)
1631 abort ();
1fb577f7
KH
1632 ptr->size = (EMACS_INT) objptr;
1633 if (ptr->size & MARKBIT)
1634 ptr->size ^= MARKBIT | DONT_COPY_FLAG;
7146af97
JB
1635 }
1636 }
1637 break;
1638
76437631 1639 case Lisp_Vectorlike:
30e3190a 1640 if (GC_BUFFERP (obj))
6b552283
KH
1641 {
1642 if (!XMARKBIT (XBUFFER (obj)->name))
1643 mark_buffer (obj);
1644 }
30e3190a 1645 else if (GC_SUBRP (obj))
169ee243
RS
1646 break;
1647 else if (GC_COMPILEDP (obj))
1648 /* We could treat this just like a vector, but it is better
1649 to save the COMPILED_CONSTANTS element for last and avoid recursion
1650 there. */
1651 {
1652 register struct Lisp_Vector *ptr = XVECTOR (obj);
1653 register EMACS_INT size = ptr->size;
1654 /* See comment above under Lisp_Vector. */
1655 struct Lisp_Vector *volatile ptr1 = ptr;
1656 register int i;
1657
1658 if (size & ARRAY_MARK_FLAG)
1659 break; /* Already marked */
1660 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
76437631 1661 size &= PSEUDOVECTOR_SIZE_MASK;
169ee243
RS
1662 for (i = 0; i < size; i++) /* and then mark its elements */
1663 {
1664 if (i != COMPILED_CONSTANTS)
1665 mark_object (&ptr1->contents[i]);
1666 }
1667 /* This cast should be unnecessary, but some Mips compiler complains
1668 (MIPS-ABI + SysVR4, DC/OSx, etc). */
1669 objptr = (Lisp_Object *) &ptr1->contents[COMPILED_CONSTANTS];
1670 goto loop;
1671 }
502b9b64 1672#ifdef MULTI_FRAME
169ee243
RS
1673 else if (GC_FRAMEP (obj))
1674 {
1675 /* See comment above under Lisp_Vector for why this is volatile. */
1676 register struct frame *volatile ptr = XFRAME (obj);
1677 register EMACS_INT size = ptr->size;
1678
1679 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
1680 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
1681
1682 mark_object (&ptr->name);
1683 mark_object (&ptr->focus_frame);
1684 mark_object (&ptr->selected_window);
1685 mark_object (&ptr->minibuffer_window);
1686 mark_object (&ptr->param_alist);
1687 mark_object (&ptr->scroll_bars);
1688 mark_object (&ptr->condemned_scroll_bars);
1689 mark_object (&ptr->menu_bar_items);
1690 mark_object (&ptr->face_alist);
1691 mark_object (&ptr->menu_bar_vector);
1692 mark_object (&ptr->buffer_predicate);
1693 }
12740e58 1694#endif /* MULTI_FRAME */
04ff9756 1695 else
169ee243
RS
1696 {
1697 register struct Lisp_Vector *ptr = XVECTOR (obj);
1698 register EMACS_INT size = ptr->size;
1699 /* The reason we use ptr1 is to avoid an apparent hardware bug
1700 that happens occasionally on the FSF's HP 300s.
1701 The bug is that a2 gets clobbered by recursive calls to mark_object.
1702 The clobberage seems to happen during function entry,
1703 perhaps in the moveml instruction.
1704 Yes, this is a crock, but we have to do it. */
1705 struct Lisp_Vector *volatile ptr1 = ptr;
1706 register int i;
1707
1708 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
1709 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
1710 if (size & PSEUDOVECTOR_FLAG)
1711 size &= PSEUDOVECTOR_SIZE_MASK;
1712 for (i = 0; i < size; i++) /* and then mark its elements */
1713 mark_object (&ptr1->contents[i]);
1714 }
1715 break;
7146af97 1716
7146af97
JB
1717 case Lisp_Symbol:
1718 {
41f54422
RS
1719 /* See comment above under Lisp_Vector for why this is volatile. */
1720 register struct Lisp_Symbol *volatile ptr = XSYMBOL (obj);
7146af97
JB
1721 struct Lisp_Symbol *ptrx;
1722
1723 if (XMARKBIT (ptr->plist)) break;
1724 XMARK (ptr->plist);
7146af97
JB
1725 mark_object ((Lisp_Object *) &ptr->value);
1726 mark_object (&ptr->function);
1727 mark_object (&ptr->plist);
8aaa7c8a
JB
1728 XSETTYPE (*(Lisp_Object *) &ptr->name, Lisp_String);
1729 mark_object (&ptr->name);
7146af97
JB
1730 ptr = ptr->next;
1731 if (ptr)
1732 {
9149e743
KH
1733 /* For the benefit of the last_marked log. */
1734 objptr = (Lisp_Object *)&XSYMBOL (obj)->next;
b0846f52 1735 ptrx = ptr; /* Use of ptrx avoids compiler bug on Sun */
7146af97 1736 XSETSYMBOL (obj, ptrx);
9149e743
KH
1737 /* We can't goto loop here because *objptr doesn't contain an
1738 actual Lisp_Object with valid datatype field. */
1739 goto loop2;
7146af97
JB
1740 }
1741 }
1742 break;
1743
a0a38eb7 1744 case Lisp_Misc:
a5da44fe 1745 switch (XMISCTYPE (obj))
a0a38eb7
KH
1746 {
1747 case Lisp_Misc_Marker:
1748 XMARK (XMARKER (obj)->chain);
1749 /* DO NOT mark thru the marker's chain.
1750 The buffer's markers chain does not preserve markers from gc;
1751 instead, markers are removed from the chain when freed by gc. */
1752 break;
1753
465edf35
KH
1754 case Lisp_Misc_Buffer_Local_Value:
1755 case Lisp_Misc_Some_Buffer_Local_Value:
1756 {
1757 register struct Lisp_Buffer_Local_Value *ptr
1758 = XBUFFER_LOCAL_VALUE (obj);
1759 if (XMARKBIT (ptr->car)) break;
1760 XMARK (ptr->car);
1761 /* If the cdr is nil, avoid recursion for the car. */
1762 if (EQ (ptr->cdr, Qnil))
1763 {
1764 objptr = &ptr->car;
1765 goto loop;
1766 }
1767 mark_object (&ptr->car);
1768 /* See comment above under Lisp_Vector for why not use ptr here. */
1769 objptr = &XBUFFER_LOCAL_VALUE (obj)->cdr;
1770 goto loop;
1771 }
1772
c8616056
KH
1773 case Lisp_Misc_Intfwd:
1774 case Lisp_Misc_Boolfwd:
1775 case Lisp_Misc_Objfwd:
1776 case Lisp_Misc_Buffer_Objfwd:
b875d3f7 1777 case Lisp_Misc_Kboard_Objfwd:
c8616056
KH
1778 /* Don't bother with Lisp_Buffer_Objfwd,
1779 since all markable slots in current buffer marked anyway. */
1780 /* Don't need to do Lisp_Objfwd, since the places they point
1781 are protected with staticpro. */
1782 break;
1783
e202fa34
KH
1784 case Lisp_Misc_Overlay:
1785 {
1786 struct Lisp_Overlay *ptr = XOVERLAY (obj);
1787 if (!XMARKBIT (ptr->plist))
1788 {
1789 XMARK (ptr->plist);
1790 mark_object (&ptr->start);
1791 mark_object (&ptr->end);
1792 objptr = &ptr->plist;
1793 goto loop;
1794 }
1795 }
1796 break;
1797
a0a38eb7
KH
1798 default:
1799 abort ();
1800 }
7146af97
JB
1801 break;
1802
1803 case Lisp_Cons:
7146af97
JB
1804 {
1805 register struct Lisp_Cons *ptr = XCONS (obj);
1806 if (XMARKBIT (ptr->car)) break;
1807 XMARK (ptr->car);
c54ca951
RS
1808 /* If the cdr is nil, avoid recursion for the car. */
1809 if (EQ (ptr->cdr, Qnil))
1810 {
1811 objptr = &ptr->car;
c54ca951
RS
1812 goto loop;
1813 }
7146af97 1814 mark_object (&ptr->car);
41f54422
RS
1815 /* See comment above under Lisp_Vector for why not use ptr here. */
1816 objptr = &XCONS (obj)->cdr;
7146af97
JB
1817 goto loop;
1818 }
1819
1820#ifdef LISP_FLOAT_TYPE
1821 case Lisp_Float:
1822 XMARK (XFLOAT (obj)->type);
1823 break;
1824#endif /* LISP_FLOAT_TYPE */
1825
7146af97 1826 case Lisp_Int:
7146af97
JB
1827 break;
1828
1829 default:
1830 abort ();
1831 }
1832}
1833
1834/* Mark the pointers in a buffer structure. */
1835
1836static void
1837mark_buffer (buf)
1838 Lisp_Object buf;
1839{
7146af97
JB
1840 register struct buffer *buffer = XBUFFER (buf);
1841 register Lisp_Object *ptr;
30e3190a 1842 Lisp_Object base_buffer;
7146af97
JB
1843
1844 /* This is the buffer's markbit */
1845 mark_object (&buffer->name);
1846 XMARK (buffer->name);
1847
30e3190a 1848 MARK_INTERVAL_TREE (BUF_INTERVALS (buffer));
d5e35230 1849
7146af97
JB
1850#if 0
1851 mark_object (buffer->syntax_table);
1852
1853 /* Mark the various string-pointers in the buffer object.
1854 Since the strings may be relocated, we must mark them
1855 in their actual slots. So gc_sweep must convert each slot
1856 back to an ordinary C pointer. */
45d12a89 1857 XSETSTRING (*(Lisp_Object *)&buffer->upcase_table, buffer->upcase_table);
7146af97 1858 mark_object ((Lisp_Object *)&buffer->upcase_table);
45d12a89 1859 XSETSTRING (*(Lisp_Object *)&buffer->downcase_table, buffer->downcase_table);
7146af97
JB
1860 mark_object ((Lisp_Object *)&buffer->downcase_table);
1861
45d12a89 1862 XSETSTRING (*(Lisp_Object *)&buffer->sort_table, buffer->sort_table);
7146af97 1863 mark_object ((Lisp_Object *)&buffer->sort_table);
45d12a89 1864 XSETSTRING (*(Lisp_Object *)&buffer->folding_sort_table, buffer->folding_sort_table);
7146af97
JB
1865 mark_object ((Lisp_Object *)&buffer->folding_sort_table);
1866#endif
1867
1868 for (ptr = &buffer->name + 1;
1869 (char *)ptr < (char *)buffer + sizeof (struct buffer);
1870 ptr++)
1871 mark_object (ptr);
30e3190a
RS
1872
1873 /* If this is an indirect buffer, mark its base buffer. */
6b552283 1874 if (buffer->base_buffer && !XMARKBIT (buffer->base_buffer->name))
30e3190a
RS
1875 {
1876 XSETBUFFER (base_buffer, buffer->base_buffer);
1877 mark_buffer (base_buffer);
1878 }
7146af97 1879}
084b1a0c
KH
1880
1881
b875d3f7 1882/* Mark the pointers in the kboard objects. */
084b1a0c
KH
1883
1884static void
b875d3f7 1885mark_kboards ()
084b1a0c 1886{
b875d3f7 1887 KBOARD *kb;
b94daf1e 1888 Lisp_Object *p;
b875d3f7 1889 for (kb = all_kboards; kb; kb = kb->next_kboard)
084b1a0c 1890 {
b94daf1e
KH
1891 if (kb->kbd_macro_buffer)
1892 for (p = kb->kbd_macro_buffer; p < kb->kbd_macro_ptr; p++)
1893 mark_object (p);
b875d3f7
KH
1894 mark_object (&kb->prefix_factor);
1895 mark_object (&kb->prefix_value);
1896 mark_object (&kb->kbd_queue);
1897 mark_object (&kb->Vlast_kbd_macro);
b94daf1e 1898 mark_object (&kb->Vsystem_key_alist);
084b1a0c
KH
1899 }
1900}
7146af97 1901\f
1a4f1e2c 1902/* Sweep: find all structures not marked, and free them. */
7146af97
JB
1903
1904static void
1905gc_sweep ()
1906{
1907 total_string_size = 0;
1908 compact_strings ();
1909
1910 /* Put all unmarked conses on free list */
1911 {
1912 register struct cons_block *cblk;
1913 register int lim = cons_block_index;
1914 register int num_free = 0, num_used = 0;
1915
1916 cons_free_list = 0;
1917
1918 for (cblk = cons_block; cblk; cblk = cblk->next)
1919 {
1920 register int i;
1921 for (i = 0; i < lim; i++)
1922 if (!XMARKBIT (cblk->conses[i].car))
1923 {
7146af97 1924 num_free++;
85481507 1925 *(struct Lisp_Cons **)&cblk->conses[i].car = cons_free_list;
7146af97
JB
1926 cons_free_list = &cblk->conses[i];
1927 }
1928 else
1929 {
1930 num_used++;
1931 XUNMARK (cblk->conses[i].car);
1932 }
1933 lim = CONS_BLOCK_SIZE;
1934 }
1935 total_conses = num_used;
1936 total_free_conses = num_free;
1937 }
1938
1939#ifdef LISP_FLOAT_TYPE
1940 /* Put all unmarked floats on free list */
1941 {
1942 register struct float_block *fblk;
1943 register int lim = float_block_index;
1944 register int num_free = 0, num_used = 0;
1945
1946 float_free_list = 0;
1947
1948 for (fblk = float_block; fblk; fblk = fblk->next)
1949 {
1950 register int i;
1951 for (i = 0; i < lim; i++)
1952 if (!XMARKBIT (fblk->floats[i].type))
1953 {
7146af97 1954 num_free++;
85481507 1955 *(struct Lisp_Float **)&fblk->floats[i].type = float_free_list;
7146af97
JB
1956 float_free_list = &fblk->floats[i];
1957 }
1958 else
1959 {
1960 num_used++;
1961 XUNMARK (fblk->floats[i].type);
1962 }
1963 lim = FLOAT_BLOCK_SIZE;
1964 }
1965 total_floats = num_used;
1966 total_free_floats = num_free;
1967 }
1968#endif /* LISP_FLOAT_TYPE */
1969
d5e35230
JA
1970#ifdef USE_TEXT_PROPERTIES
1971 /* Put all unmarked intervals on free list */
1972 {
1973 register struct interval_block *iblk;
1974 register int lim = interval_block_index;
1975 register int num_free = 0, num_used = 0;
1976
1977 interval_free_list = 0;
1978
1979 for (iblk = interval_block; iblk; iblk = iblk->next)
1980 {
1981 register int i;
1982
1983 for (i = 0; i < lim; i++)
1984 {
1985 if (! XMARKBIT (iblk->intervals[i].plist))
1986 {
1987 iblk->intervals[i].parent = interval_free_list;
1988 interval_free_list = &iblk->intervals[i];
1989 num_free++;
1990 }
1991 else
1992 {
1993 num_used++;
1994 XUNMARK (iblk->intervals[i].plist);
1995 }
1996 }
1997 lim = INTERVAL_BLOCK_SIZE;
1998 }
1999 total_intervals = num_used;
2000 total_free_intervals = num_free;
2001 }
2002#endif /* USE_TEXT_PROPERTIES */
2003
7146af97
JB
2004 /* Put all unmarked symbols on free list */
2005 {
2006 register struct symbol_block *sblk;
2007 register int lim = symbol_block_index;
2008 register int num_free = 0, num_used = 0;
2009
2010 symbol_free_list = 0;
2011
2012 for (sblk = symbol_block; sblk; sblk = sblk->next)
2013 {
2014 register int i;
2015 for (i = 0; i < lim; i++)
2016 if (!XMARKBIT (sblk->symbols[i].plist))
2017 {
85481507 2018 *(struct Lisp_Symbol **)&sblk->symbols[i].value = symbol_free_list;
7146af97
JB
2019 symbol_free_list = &sblk->symbols[i];
2020 num_free++;
2021 }
2022 else
2023 {
2024 num_used++;
2025 sblk->symbols[i].name
2026 = XSTRING (*(Lisp_Object *) &sblk->symbols[i].name);
2027 XUNMARK (sblk->symbols[i].plist);
2028 }
2029 lim = SYMBOL_BLOCK_SIZE;
2030 }
2031 total_symbols = num_used;
2032 total_free_symbols = num_free;
2033 }
2034
2035#ifndef standalone
2036 /* Put all unmarked markers on free list.
465edf35
KH
2037 Dechain each one first from the buffer it points into,
2038 but only if it's a real marker. */
7146af97
JB
2039 {
2040 register struct marker_block *mblk;
7146af97
JB
2041 register int lim = marker_block_index;
2042 register int num_free = 0, num_used = 0;
2043
2044 marker_free_list = 0;
2045
2046 for (mblk = marker_block; mblk; mblk = mblk->next)
2047 {
2048 register int i;
fa05e253
RS
2049 int already_free = -1;
2050
7146af97 2051 for (i = 0; i < lim; i++)
465edf35
KH
2052 {
2053 Lisp_Object *markword;
a5da44fe 2054 switch (mblk->markers[i].u_marker.type)
465edf35
KH
2055 {
2056 case Lisp_Misc_Marker:
2057 markword = &mblk->markers[i].u_marker.chain;
2058 break;
2059 case Lisp_Misc_Buffer_Local_Value:
2060 case Lisp_Misc_Some_Buffer_Local_Value:
2061 markword = &mblk->markers[i].u_buffer_local_value.car;
2062 break;
e202fa34
KH
2063 case Lisp_Misc_Overlay:
2064 markword = &mblk->markers[i].u_overlay.plist;
2065 break;
fa05e253
RS
2066 case Lisp_Misc_Free:
2067 /* If the object was already free, keep it
2068 on the free list. */
2069 markword = &already_free;
2070 break;
465edf35
KH
2071 default:
2072 markword = 0;
e202fa34 2073 break;
465edf35
KH
2074 }
2075 if (markword && !XMARKBIT (*markword))
2076 {
2077 Lisp_Object tem;
a5da44fe 2078 if (mblk->markers[i].u_marker.type == Lisp_Misc_Marker)
465edf35
KH
2079 {
2080 /* tem1 avoids Sun compiler bug */
2081 struct Lisp_Marker *tem1 = &mblk->markers[i].u_marker;
2082 XSETMARKER (tem, tem1);
2083 unchain_marker (tem);
2084 }
fa05e253
RS
2085 /* Set the type of the freed object to Lisp_Misc_Free.
2086 We could leave the type alone, since nobody checks it,
465edf35 2087 but this might catch bugs faster. */
a5da44fe 2088 mblk->markers[i].u_marker.type = Lisp_Misc_Free;
465edf35
KH
2089 mblk->markers[i].u_free.chain = marker_free_list;
2090 marker_free_list = &mblk->markers[i];
2091 num_free++;
2092 }
2093 else
2094 {
2095 num_used++;
2096 if (markword)
2097 XUNMARK (*markword);
2098 }
2099 }
7146af97
JB
2100 lim = MARKER_BLOCK_SIZE;
2101 }
2102
2103 total_markers = num_used;
2104 total_free_markers = num_free;
2105 }
2106
2107 /* Free all unmarked buffers */
2108 {
2109 register struct buffer *buffer = all_buffers, *prev = 0, *next;
2110
2111 while (buffer)
2112 if (!XMARKBIT (buffer->name))
2113 {
2114 if (prev)
2115 prev->next = buffer->next;
2116 else
2117 all_buffers = buffer->next;
2118 next = buffer->next;
9ac0d9e0 2119 xfree (buffer);
7146af97
JB
2120 buffer = next;
2121 }
2122 else
2123 {
2124 XUNMARK (buffer->name);
30e3190a 2125 UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer));
7146af97
JB
2126
2127#if 0
2128 /* Each `struct Lisp_String *' was turned into a Lisp_Object
2129 for purposes of marking and relocation.
2130 Turn them back into C pointers now. */
2131 buffer->upcase_table
2132 = XSTRING (*(Lisp_Object *)&buffer->upcase_table);
2133 buffer->downcase_table
2134 = XSTRING (*(Lisp_Object *)&buffer->downcase_table);
2135 buffer->sort_table
2136 = XSTRING (*(Lisp_Object *)&buffer->sort_table);
2137 buffer->folding_sort_table
2138 = XSTRING (*(Lisp_Object *)&buffer->folding_sort_table);
2139#endif
2140
2141 prev = buffer, buffer = buffer->next;
2142 }
2143 }
2144
2145#endif /* standalone */
2146
2147 /* Free all unmarked vectors */
2148 {
2149 register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next;
2150 total_vector_size = 0;
2151
2152 while (vector)
2153 if (!(vector->size & ARRAY_MARK_FLAG))
2154 {
2155 if (prev)
2156 prev->next = vector->next;
2157 else
2158 all_vectors = vector->next;
2159 next = vector->next;
9ac0d9e0 2160 xfree (vector);
7146af97
JB
2161 vector = next;
2162 }
2163 else
2164 {
2165 vector->size &= ~ARRAY_MARK_FLAG;
fa05e253
RS
2166 if (vector->size & PSEUDOVECTOR_FLAG)
2167 total_vector_size += (PSEUDOVECTOR_SIZE_MASK & vector->size);
2168 else
2169 total_vector_size += vector->size;
7146af97
JB
2170 prev = vector, vector = vector->next;
2171 }
2172 }
2173
2174 /* Free all "large strings" not marked with ARRAY_MARK_FLAG. */
2175 {
2176 register struct string_block *sb = large_string_blocks, *prev = 0, *next;
e8720644 2177 struct Lisp_String *s;
7146af97
JB
2178
2179 while (sb)
e8720644
JB
2180 {
2181 s = (struct Lisp_String *) &sb->chars[0];
2182 if (s->size & ARRAY_MARK_FLAG)
2183 {
2184 ((struct Lisp_String *)(&sb->chars[0]))->size
1fb577f7 2185 &= ~ARRAY_MARK_FLAG & ~MARKBIT;
e8720644
JB
2186 UNMARK_BALANCE_INTERVALS (s->intervals);
2187 total_string_size += ((struct Lisp_String *)(&sb->chars[0]))->size;
2188 prev = sb, sb = sb->next;
2189 }
2190 else
2191 {
2192 if (prev)
2193 prev->next = sb->next;
2194 else
2195 large_string_blocks = sb->next;
2196 next = sb->next;
2197 xfree (sb);
2198 sb = next;
2199 }
2200 }
7146af97
JB
2201 }
2202}
2203\f
1a4f1e2c 2204/* Compactify strings, relocate references, and free empty string blocks. */
7146af97
JB
2205
2206static void
2207compact_strings ()
2208{
2209 /* String block of old strings we are scanning. */
2210 register struct string_block *from_sb;
2211 /* A preceding string block (or maybe the same one)
2212 where we are copying the still-live strings to. */
2213 register struct string_block *to_sb;
2214 int pos;
2215 int to_pos;
2216
2217 to_sb = first_string_block;
2218 to_pos = 0;
2219
2220 /* Scan each existing string block sequentially, string by string. */
2221 for (from_sb = first_string_block; from_sb; from_sb = from_sb->next)
2222 {
2223 pos = 0;
2224 /* POS is the index of the next string in the block. */
2225 while (pos < from_sb->pos)
2226 {
2227 register struct Lisp_String *nextstr
2228 = (struct Lisp_String *) &from_sb->chars[pos];
2229
2230 register struct Lisp_String *newaddr;
42607681 2231 register EMACS_INT size = nextstr->size;
7146af97
JB
2232
2233 /* NEXTSTR is the old address of the next string.
2234 Just skip it if it isn't marked. */
155ffe9c 2235 if (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE)
7146af97
JB
2236 {
2237 /* It is marked, so its size field is really a chain of refs.
2238 Find the end of the chain, where the actual size lives. */
155ffe9c 2239 while (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE)
7146af97 2240 {
155ffe9c
RS
2241 if (size & DONT_COPY_FLAG)
2242 size ^= MARKBIT | DONT_COPY_FLAG;
42607681 2243 size = *(EMACS_INT *)size & ~MARKBIT;
7146af97
JB
2244 }
2245
2246 total_string_size += size;
2247
2248 /* If it won't fit in TO_SB, close it out,
2249 and move to the next sb. Keep doing so until
2250 TO_SB reaches a large enough, empty enough string block.
2251 We know that TO_SB cannot advance past FROM_SB here
2252 since FROM_SB is large enough to contain this string.
2253 Any string blocks skipped here
2254 will be patched out and freed later. */
2255 while (to_pos + STRING_FULLSIZE (size)
2256 > max (to_sb->pos, STRING_BLOCK_SIZE))
2257 {
2258 to_sb->pos = to_pos;
2259 to_sb = to_sb->next;
2260 to_pos = 0;
2261 }
2262 /* Compute new address of this string
2263 and update TO_POS for the space being used. */
2264 newaddr = (struct Lisp_String *) &to_sb->chars[to_pos];
2265 to_pos += STRING_FULLSIZE (size);
2266
2267 /* Copy the string itself to the new place. */
2268 if (nextstr != newaddr)
42607681 2269 bcopy (nextstr, newaddr, size + 1 + sizeof (EMACS_INT)
d5e35230 2270 + INTERVAL_PTR_SIZE);
7146af97
JB
2271
2272 /* Go through NEXTSTR's chain of references
2273 and make each slot in the chain point to
2274 the new address of this string. */
2275 size = newaddr->size;
155ffe9c 2276 while (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE)
7146af97
JB
2277 {
2278 register Lisp_Object *objptr;
155ffe9c
RS
2279 if (size & DONT_COPY_FLAG)
2280 size ^= MARKBIT | DONT_COPY_FLAG;
7146af97
JB
2281 objptr = (Lisp_Object *)size;
2282
2283 size = XFASTINT (*objptr) & ~MARKBIT;
2284 if (XMARKBIT (*objptr))
2285 {
45d12a89 2286 XSETSTRING (*objptr, newaddr);
7146af97
JB
2287 XMARK (*objptr);
2288 }
2289 else
45d12a89 2290 XSETSTRING (*objptr, newaddr);
7146af97
JB
2291 }
2292 /* Store the actual size in the size field. */
2293 newaddr->size = size;
e8720644 2294
5f60ed47 2295#ifdef USE_TEXT_PROPERTIES
e8720644
JB
2296 /* Now that the string has been relocated, rebalance its
2297 interval tree, and update the tree's parent pointer. */
2298 if (! NULL_INTERVAL_P (newaddr->intervals))
2299 {
2300 UNMARK_BALANCE_INTERVALS (newaddr->intervals);
45d12a89
KH
2301 XSETSTRING (* (Lisp_Object *) &newaddr->intervals->parent,
2302 newaddr);
e8720644 2303 }
5f60ed47 2304#endif /* USE_TEXT_PROPERTIES */
7146af97
JB
2305 }
2306 pos += STRING_FULLSIZE (size);
2307 }
2308 }
2309
2310 /* Close out the last string block still used and free any that follow. */
2311 to_sb->pos = to_pos;
2312 current_string_block = to_sb;
2313
2314 from_sb = to_sb->next;
2315 to_sb->next = 0;
2316 while (from_sb)
2317 {
2318 to_sb = from_sb->next;
9ac0d9e0 2319 xfree (from_sb);
7146af97
JB
2320 from_sb = to_sb;
2321 }
2322
2323 /* Free any empty string blocks further back in the chain.
2324 This loop will never free first_string_block, but it is very
2325 unlikely that that one will become empty, so why bother checking? */
2326
2327 from_sb = first_string_block;
2328 while (to_sb = from_sb->next)
2329 {
2330 if (to_sb->pos == 0)
2331 {
2332 if (from_sb->next = to_sb->next)
2333 from_sb->next->prev = from_sb;
9ac0d9e0 2334 xfree (to_sb);
7146af97
JB
2335 }
2336 else
2337 from_sb = to_sb;
2338 }
2339}
2340\f
20d24714
JB
2341/* Debugging aids. */
2342
31ce1c91 2343DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0,
20d24714
JB
2344 "Return the address of the last byte Emacs has allocated, divided by 1024.\n\
2345This may be helpful in debugging Emacs's memory usage.\n\
e41ae81f 2346We divide the value by 1024 to make sure it fits in a Lisp integer.")
20d24714
JB
2347 ()
2348{
2349 Lisp_Object end;
2350
45d12a89 2351 XSETINT (end, (EMACS_INT) sbrk (0) / 1024);
20d24714
JB
2352
2353 return end;
2354}
2355
2356\f
7146af97
JB
2357/* Initialization */
2358
2359init_alloc_once ()
2360{
2361 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
2362 pureptr = 0;
4c0be5f4
JB
2363#ifdef HAVE_SHM
2364 pure_size = PURESIZE;
2365#endif
7146af97
JB
2366 all_vectors = 0;
2367 ignore_warnings = 1;
2368 init_strings ();
2369 init_cons ();
2370 init_symbol ();
2371 init_marker ();
2372#ifdef LISP_FLOAT_TYPE
2373 init_float ();
2374#endif /* LISP_FLOAT_TYPE */
d5e35230
JA
2375 INIT_INTERVALS;
2376
276cbe5a
RS
2377#ifdef REL_ALLOC
2378 malloc_hysteresis = 32;
2379#else
2380 malloc_hysteresis = 0;
2381#endif
2382
2383 spare_memory = (char *) malloc (SPARE_MEMORY);
2384
7146af97
JB
2385 ignore_warnings = 0;
2386 gcprolist = 0;
2387 staticidx = 0;
2388 consing_since_gc = 0;
d32625c0 2389 gc_cons_threshold = 300000;
7146af97
JB
2390#ifdef VIRT_ADDR_VARIES
2391 malloc_sbrk_unused = 1<<22; /* A large number */
2392 malloc_sbrk_used = 100000; /* as reasonable as any number */
2393#endif /* VIRT_ADDR_VARIES */
2394}
2395
2396init_alloc ()
2397{
2398 gcprolist = 0;
2399}
2400
2401void
2402syms_of_alloc ()
2403{
2404 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold,
2405 "*Number of bytes of consing between garbage collections.\n\
2406Garbage collection can happen automatically once this many bytes have been\n\
2407allocated since the last garbage collection. All data types count.\n\n\
2408Garbage collection happens automatically only when `eval' is called.\n\n\
2409By binding this temporarily to a large number, you can effectively\n\
2410prevent garbage collection during a part of the program.");
2411
2412 DEFVAR_INT ("pure-bytes-used", &pureptr,
2413 "Number of bytes of sharable Lisp data allocated so far.");
2414
2415#if 0
2416 DEFVAR_INT ("data-bytes-used", &malloc_sbrk_used,
2417 "Number of bytes of unshared memory allocated in this session.");
2418
2419 DEFVAR_INT ("data-bytes-free", &malloc_sbrk_unused,
2420 "Number of bytes of unshared memory remaining available in this session.");
2421#endif
2422
2423 DEFVAR_LISP ("purify-flag", &Vpurify_flag,
2424 "Non-nil means loading Lisp code in order to dump an executable.\n\
2425This means that certain objects should be allocated in shared (pure) space.");
2426
502b9b64 2427 DEFVAR_INT ("undo-limit", &undo_limit,
7146af97 2428 "Keep no more undo information once it exceeds this size.\n\
502b9b64 2429This limit is applied when garbage collection happens.\n\
7146af97
JB
2430The size is counted as the number of bytes occupied,\n\
2431which includes both saved text and other data.");
502b9b64 2432 undo_limit = 20000;
7146af97 2433
502b9b64 2434 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit,
7146af97
JB
2435 "Don't keep more than this much size of undo information.\n\
2436A command which pushes past this size is itself forgotten.\n\
502b9b64 2437This limit is applied when garbage collection happens.\n\
7146af97
JB
2438The size is counted as the number of bytes occupied,\n\
2439which includes both saved text and other data.");
502b9b64 2440 undo_strong_limit = 30000;
7146af97 2441
bcb61d60
KH
2442 /* We build this in advance because if we wait until we need it, we might
2443 not be able to allocate the memory to hold it. */
cf3540e4 2444 memory_signal_data
276cbe5a 2445 = Fcons (Qerror, Fcons (build_string ("Memory exhausted--use M-x save-some-buffers RET"), Qnil));
bcb61d60
KH
2446 staticpro (&memory_signal_data);
2447
e8197642
RS
2448 staticpro (&Qgc_cons_threshold);
2449 Qgc_cons_threshold = intern ("gc-cons-threshold");
2450
7146af97
JB
2451 defsubr (&Scons);
2452 defsubr (&Slist);
2453 defsubr (&Svector);
2454 defsubr (&Smake_byte_code);
2455 defsubr (&Smake_list);
2456 defsubr (&Smake_vector);
2457 defsubr (&Smake_string);
7146af97
JB
2458 defsubr (&Smake_symbol);
2459 defsubr (&Smake_marker);
2460 defsubr (&Spurecopy);
2461 defsubr (&Sgarbage_collect);
20d24714 2462 defsubr (&Smemory_limit);
7146af97 2463}