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