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