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