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