Correct author's mail address.
[bpt/emacs.git] / src / alloc.c
CommitLineData
7146af97 1/* Storage allocation and gc for GNU Emacs Lisp interpreter.
2e471eb5 2 Copyright (C) 1985, 86, 88, 93, 94, 95, 97, 98, 1999, 2000
4a2f9c6a 3 Free Software Foundation, Inc.
7146af97
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
7c299e7a 9the Free Software Foundation; either version 2, or (at your option)
7146af97
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
7146af97 21
18160b98 22#include <config.h>
e9b309ac 23#include <stdio.h>
92939d31 24
68c45bf0 25/* Note that this declares bzero on OSF/1. How dumb. */
2e471eb5 26
68c45bf0 27#include <signal.h>
92939d31 28
7539e11f
KR
29/* This file is part of the core Lisp implementation, and thus must
30 deal with the real data structures. If the Lisp implementation is
31 replaced, this file likely will not be used. */
2e471eb5 32
7539e11f 33#undef HIDE_LISP_IMPLEMENTATION
7146af97 34#include "lisp.h"
d5e35230 35#include "intervals.h"
4c0be5f4 36#include "puresize.h"
7146af97
JB
37#include "buffer.h"
38#include "window.h"
502b9b64 39#include "frame.h"
9ac0d9e0 40#include "blockinput.h"
077d751f 41#include "keyboard.h"
e54daa22 42#include "charset.h"
e065a56e 43#include "syssignal.h"
34400008 44#include <setjmp.h>
e065a56e 45
ee1eea5c
KH
46extern char *sbrk ();
47
d1658221 48#ifdef DOUG_LEA_MALLOC
2e471eb5 49
d1658221
RS
50#include <malloc.h>
51#define __malloc_size_t int
81d492d5 52
2e471eb5
GM
53/* Specify maximum number of areas to mmap. It would be nice to use a
54 value that explicitly means "no limit". */
55
81d492d5
RS
56#define MMAP_MAX_AREAS 100000000
57
2e471eb5
GM
58#else /* not DOUG_LEA_MALLOC */
59
276cbe5a
RS
60/* The following come from gmalloc.c. */
61
e9b309ac 62#if defined (STDC_HEADERS)
276cbe5a
RS
63#include <stddef.h>
64#define __malloc_size_t size_t
65#else
66#define __malloc_size_t unsigned int
67#endif
68extern __malloc_size_t _bytes_used;
69extern int __malloc_extra_blocks;
2e471eb5
GM
70
71#endif /* not DOUG_LEA_MALLOC */
276cbe5a 72
7146af97 73#define max(A,B) ((A) > (B) ? (A) : (B))
b580578b 74#define min(A,B) ((A) < (B) ? (A) : (B))
7146af97
JB
75
76/* Macro to verify that storage intended for Lisp objects is not
77 out of range to fit in the space for a pointer.
78 ADDRESS is the start of the block, and SIZE
79 is the amount of space within which objects can start. */
2e471eb5 80
7146af97
JB
81#define VALIDATE_LISP_STORAGE(address, size) \
82do \
83 { \
84 Lisp_Object val; \
45d12a89 85 XSETCONS (val, (char *) address + size); \
7146af97
JB
86 if ((char *) XCONS (val) != (char *) address + size) \
87 { \
9ac0d9e0 88 xfree (address); \
7146af97
JB
89 memory_full (); \
90 } \
91 } while (0)
92
276cbe5a 93/* Value of _bytes_used, when spare_memory was freed. */
2e471eb5 94
276cbe5a
RS
95static __malloc_size_t bytes_used_when_full;
96
2e471eb5
GM
97/* Mark, unmark, query mark bit of a Lisp string. S must be a pointer
98 to a struct Lisp_String. */
99
cc2d8c6b
KR
100#define MARK_STRING(S) ((S)->size |= MARKBIT)
101#define UNMARK_STRING(S) ((S)->size &= ~MARKBIT)
102#define STRING_MARKED_P(S) ((S)->size & MARKBIT)
2e471eb5
GM
103
104/* Value is the number of bytes/chars of S, a pointer to a struct
105 Lisp_String. This must be used instead of STRING_BYTES (S) or
106 S->size during GC, because S->size contains the mark bit for
107 strings. */
108
109#define GC_STRING_BYTES(S) (STRING_BYTES (S) & ~MARKBIT)
110#define GC_STRING_CHARS(S) ((S)->size & ~MARKBIT)
111
112/* Number of bytes of consing done since the last gc. */
113
7146af97
JB
114int consing_since_gc;
115
310ea200 116/* Count the amount of consing of various sorts of space. */
2e471eb5 117
310ea200
RS
118int cons_cells_consed;
119int floats_consed;
120int vector_cells_consed;
121int symbols_consed;
122int string_chars_consed;
123int misc_objects_consed;
124int intervals_consed;
2e471eb5
GM
125int strings_consed;
126
127/* Number of bytes of consing since GC before another GC should be done. */
310ea200 128
b580578b 129int gc_cons_threshold;
7146af97 130
2e471eb5
GM
131/* Nonzero during GC. */
132
7146af97
JB
133int gc_in_progress;
134
299585ee 135/* Nonzero means display messages at beginning and end of GC. */
2e471eb5 136
299585ee
RS
137int garbage_collection_messages;
138
7146af97
JB
139#ifndef VIRT_ADDR_VARIES
140extern
141#endif /* VIRT_ADDR_VARIES */
2e471eb5 142int malloc_sbrk_used;
7146af97
JB
143
144#ifndef VIRT_ADDR_VARIES
145extern
146#endif /* VIRT_ADDR_VARIES */
2e471eb5 147int malloc_sbrk_unused;
7146af97 148
502b9b64 149/* Two limits controlling how much undo information to keep. */
2e471eb5 150
502b9b64
JB
151int undo_limit;
152int undo_strong_limit;
7146af97 153
34400008
GM
154/* Number of live and free conses etc. */
155
156static int total_conses, total_markers, total_symbols, total_vector_size;
157static int total_free_conses, total_free_markers, total_free_symbols;
158static int total_free_floats, total_floats;
fd27a537 159
2e471eb5
GM
160/* Points to memory space allocated as "spare", to be freed if we run
161 out of memory. */
162
276cbe5a
RS
163static char *spare_memory;
164
165/* Amount of spare memory to keep in reserve. */
2e471eb5 166
276cbe5a
RS
167#define SPARE_MEMORY (1 << 14)
168
169/* Number of extra blocks malloc should get when it needs more core. */
2e471eb5 170
276cbe5a
RS
171static int malloc_hysteresis;
172
2e471eb5
GM
173/* Non-nil means defun should do purecopy on the function definition. */
174
7146af97
JB
175Lisp_Object Vpurify_flag;
176
177#ifndef HAVE_SHM
2e471eb5
GM
178
179/* Force it into data space! */
180
181EMACS_INT pure[PURESIZE / sizeof (EMACS_INT)] = {0,};
7146af97 182#define PUREBEG (char *) pure
2e471eb5
GM
183
184#else /* not HAVE_SHM */
185
7146af97
JB
186#define pure PURE_SEG_BITS /* Use shared memory segment */
187#define PUREBEG (char *)PURE_SEG_BITS
4c0be5f4
JB
188
189/* This variable is used only by the XPNTR macro when HAVE_SHM is
190 defined. If we used the PURESIZE macro directly there, that would
2e471eb5 191 make most of Emacs dependent on puresize.h, which we don't want -
4c0be5f4
JB
192 you should be able to change that without too much recompilation.
193 So map_in_data initializes pure_size, and the dependencies work
194 out. */
2e471eb5 195
42607681 196EMACS_INT pure_size;
2e471eb5 197
7146af97
JB
198#endif /* not HAVE_SHM */
199
34400008
GM
200/* Value is non-zero if P points into pure space. */
201
202#define PURE_POINTER_P(P) \
203 (((PNTR_COMPARISON_TYPE) (P) \
204 < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)) \
205 && ((PNTR_COMPARISON_TYPE) (P) \
206 >= (PNTR_COMPARISON_TYPE) pure))
207
2e471eb5
GM
208/* Index in pure at which next pure object will be allocated.. */
209
7146af97
JB
210int pureptr;
211
2e471eb5
GM
212/* If nonzero, this is a warning delivered by malloc and not yet
213 displayed. */
214
7146af97
JB
215char *pending_malloc_warning;
216
bcb61d60 217/* Pre-computed signal argument for use when memory is exhausted. */
2e471eb5 218
cf3540e4 219Lisp_Object memory_signal_data;
bcb61d60 220
7146af97
JB
221/* Maximum amount of C stack to save when a GC happens. */
222
223#ifndef MAX_SAVE_STACK
224#define MAX_SAVE_STACK 16000
225#endif
226
227/* Buffer in which we save a copy of the C stack at each GC. */
228
229char *stack_copy;
230int stack_copy_size;
231
2e471eb5
GM
232/* Non-zero means ignore malloc warnings. Set during initialization.
233 Currently not used. */
234
7146af97 235int ignore_warnings;
350273a4 236
a59de17b 237Lisp_Object Qgc_cons_threshold, Qchar_table_extra_slots;
e8197642 238
2e471eb5
GM
239static void mark_buffer P_ ((Lisp_Object));
240static void mark_kboards P_ ((void));
241static void gc_sweep P_ ((void));
41c28a37
GM
242static void mark_glyph_matrix P_ ((struct glyph_matrix *));
243static void mark_face_cache P_ ((struct face_cache *));
244
245#ifdef HAVE_WINDOW_SYSTEM
246static void mark_image P_ ((struct image *));
247static void mark_image_cache P_ ((struct frame *));
248#endif /* HAVE_WINDOW_SYSTEM */
249
2e471eb5
GM
250static struct Lisp_String *allocate_string P_ ((void));
251static void compact_small_strings P_ ((void));
252static void free_large_strings P_ ((void));
253static void sweep_strings P_ ((void));
7da0b0d3
RS
254
255extern int message_enable_multibyte;
34400008 256
34400008
GM
257/* When scanning the C stack for live Lisp objects, Emacs keeps track
258 of what memory allocated via lisp_malloc is intended for what
259 purpose. This enumeration specifies the type of memory. */
260
261enum mem_type
262{
263 MEM_TYPE_NON_LISP,
264 MEM_TYPE_BUFFER,
265 MEM_TYPE_CONS,
266 MEM_TYPE_STRING,
267 MEM_TYPE_MISC,
268 MEM_TYPE_SYMBOL,
269 MEM_TYPE_FLOAT,
270 MEM_TYPE_VECTOR
271};
272
0b378936
GM
273#if GC_MARK_STACK
274
275#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
276#include <stdio.h> /* For fprintf. */
277#endif
278
279/* A unique object in pure space used to make some Lisp objects
280 on free lists recognizable in O(1). */
281
282Lisp_Object Vdead;
283
34400008
GM
284struct mem_node;
285static void *lisp_malloc P_ ((int, enum mem_type));
286static void mark_stack P_ ((void));
287static void init_stack P_ ((Lisp_Object *));
288static int live_vector_p P_ ((struct mem_node *, void *));
289static int live_buffer_p P_ ((struct mem_node *, void *));
290static int live_string_p P_ ((struct mem_node *, void *));
291static int live_cons_p P_ ((struct mem_node *, void *));
292static int live_symbol_p P_ ((struct mem_node *, void *));
293static int live_float_p P_ ((struct mem_node *, void *));
294static int live_misc_p P_ ((struct mem_node *, void *));
182ff242 295static void mark_maybe_object P_ ((Lisp_Object));
34400008
GM
296static void mark_memory P_ ((void *, void *));
297static void mem_init P_ ((void));
298static struct mem_node *mem_insert P_ ((void *, void *, enum mem_type));
299static void mem_insert_fixup P_ ((struct mem_node *));
300static void mem_rotate_left P_ ((struct mem_node *));
301static void mem_rotate_right P_ ((struct mem_node *));
302static void mem_delete P_ ((struct mem_node *));
303static void mem_delete_fixup P_ ((struct mem_node *));
304static INLINE struct mem_node *mem_find P_ ((void *));
305
306#if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
307static void check_gcpros P_ ((void));
308#endif
309
310#endif /* GC_MARK_STACK != 0 */
311
7146af97 312\f
34400008
GM
313/************************************************************************
314 Malloc
315 ************************************************************************/
316
317/* Write STR to Vstandard_output plus some advice on how to free some
318 memory. Called when memory gets low. */
1a4f1e2c 319
7146af97
JB
320Lisp_Object
321malloc_warning_1 (str)
322 Lisp_Object str;
323{
324 Fprinc (str, Vstandard_output);
325 write_string ("\nKilling some buffers may delay running out of memory.\n", -1);
326 write_string ("However, certainly by the time you receive the 95% warning,\n", -1);
327 write_string ("you should clean up, kill this Emacs, and start a new one.", -1);
328 return Qnil;
329}
330
34400008
GM
331
332/* Function malloc calls this if it finds we are near exhausting
333 storage. */
d457598b
AS
334
335void
7146af97
JB
336malloc_warning (str)
337 char *str;
338{
339 pending_malloc_warning = str;
340}
341
34400008
GM
342
343/* Display a malloc warning in buffer *Danger*. */
344
d457598b 345void
7146af97
JB
346display_malloc_warning ()
347{
348 register Lisp_Object val;
349
350 val = build_string (pending_malloc_warning);
351 pending_malloc_warning = 0;
352 internal_with_output_to_temp_buffer (" *Danger*", malloc_warning_1, val);
353}
354
34400008 355
d1658221 356#ifdef DOUG_LEA_MALLOC
1177ecf6 357# define BYTES_USED (mallinfo ().arena)
d1658221 358#else
1177ecf6 359# define BYTES_USED _bytes_used
d1658221
RS
360#endif
361
34400008 362
2e471eb5 363/* Called if malloc returns zero. */
276cbe5a 364
d457598b 365void
7146af97
JB
366memory_full ()
367{
276cbe5a 368#ifndef SYSTEM_MALLOC
d1658221 369 bytes_used_when_full = BYTES_USED;
276cbe5a
RS
370#endif
371
372 /* The first time we get here, free the spare memory. */
373 if (spare_memory)
374 {
375 free (spare_memory);
376 spare_memory = 0;
377 }
378
2e471eb5
GM
379 /* This used to call error, but if we've run out of memory, we could
380 get infinite recursion trying to build the string. */
276cbe5a 381 while (1)
74d84334 382 Fsignal (Qnil, memory_signal_data);
276cbe5a
RS
383}
384
34400008 385
276cbe5a
RS
386/* Called if we can't allocate relocatable space for a buffer. */
387
388void
389buffer_memory_full ()
390{
2e471eb5
GM
391 /* If buffers use the relocating allocator, no need to free
392 spare_memory, because we may have plenty of malloc space left
393 that we could get, and if we don't, the malloc that fails will
394 itself cause spare_memory to be freed. If buffers don't use the
395 relocating allocator, treat this like any other failing
396 malloc. */
276cbe5a
RS
397
398#ifndef REL_ALLOC
399 memory_full ();
400#endif
401
2e471eb5
GM
402 /* This used to call error, but if we've run out of memory, we could
403 get infinite recursion trying to build the string. */
bcb61d60
KH
404 while (1)
405 Fsignal (Qerror, memory_signal_data);
7146af97
JB
406}
407
34400008
GM
408
409/* Like malloc but check for no memory and block interrupt input.. */
7146af97 410
c971ff9a 411POINTER_TYPE *
7146af97
JB
412xmalloc (size)
413 int size;
414{
c971ff9a 415 register POINTER_TYPE *val;
7146af97 416
9ac0d9e0 417 BLOCK_INPUT;
c971ff9a 418 val = (POINTER_TYPE *) malloc (size);
9ac0d9e0 419 UNBLOCK_INPUT;
7146af97 420
2e471eb5
GM
421 if (!val && size)
422 memory_full ();
7146af97
JB
423 return val;
424}
425
34400008
GM
426
427/* Like realloc but check for no memory and block interrupt input.. */
428
c971ff9a 429POINTER_TYPE *
7146af97 430xrealloc (block, size)
c971ff9a 431 POINTER_TYPE *block;
7146af97
JB
432 int size;
433{
c971ff9a 434 register POINTER_TYPE *val;
7146af97 435
9ac0d9e0 436 BLOCK_INPUT;
56d2031b
JB
437 /* We must call malloc explicitly when BLOCK is 0, since some
438 reallocs don't do this. */
439 if (! block)
c971ff9a 440 val = (POINTER_TYPE *) malloc (size);
f048679d 441 else
c971ff9a 442 val = (POINTER_TYPE *) realloc (block, size);
9ac0d9e0 443 UNBLOCK_INPUT;
7146af97
JB
444
445 if (!val && size) memory_full ();
446 return val;
447}
9ac0d9e0 448
34400008
GM
449
450/* Like free but block interrupt input.. */
451
9ac0d9e0
JB
452void
453xfree (block)
c971ff9a 454 POINTER_TYPE *block;
9ac0d9e0
JB
455{
456 BLOCK_INPUT;
457 free (block);
458 UNBLOCK_INPUT;
459}
460
c8099634 461
dca7c6a8
GM
462/* Like strdup, but uses xmalloc. */
463
464char *
465xstrdup (s)
466 char *s;
467{
468 int len = strlen (s) + 1;
469 char *p = (char *) xmalloc (len);
470 bcopy (s, p, len);
471 return p;
472}
473
474
34400008
GM
475/* Like malloc but used for allocating Lisp data. NBYTES is the
476 number of bytes to allocate, TYPE describes the intended use of the
477 allcated memory block (for strings, for conses, ...). */
478
479static void *
480lisp_malloc (nbytes, type)
481 int nbytes;
482 enum mem_type type;
c8099634 483{
34400008 484 register void *val;
c8099634
RS
485
486 BLOCK_INPUT;
34400008 487 val = (void *) malloc (nbytes);
c8099634 488
34400008 489#if GC_MARK_STACK
dca7c6a8 490 if (val && type != MEM_TYPE_NON_LISP)
34400008
GM
491 mem_insert (val, (char *) val + nbytes, type);
492#endif
493
dca7c6a8
GM
494 UNBLOCK_INPUT;
495 if (!val && nbytes)
496 memory_full ();
c8099634
RS
497 return val;
498}
499
34400008
GM
500
501/* Return a new buffer structure allocated from the heap with
502 a call to lisp_malloc. */
503
504struct buffer *
505allocate_buffer ()
506{
507 return (struct buffer *) lisp_malloc (sizeof (struct buffer),
508 MEM_TYPE_BUFFER);
509}
510
511
512/* Free BLOCK. This must be called to free memory allocated with a
513 call to lisp_malloc. */
514
c8099634
RS
515void
516lisp_free (block)
517 long *block;
518{
519 BLOCK_INPUT;
c8099634 520 free (block);
34400008
GM
521#if GC_MARK_STACK
522 mem_delete (mem_find (block));
523#endif
c8099634
RS
524 UNBLOCK_INPUT;
525}
34400008 526
9ac0d9e0
JB
527\f
528/* Arranging to disable input signals while we're in malloc.
529
530 This only works with GNU malloc. To help out systems which can't
531 use GNU malloc, all the calls to malloc, realloc, and free
532 elsewhere in the code should be inside a BLOCK_INPUT/UNBLOCK_INPUT
533 pairs; unfortunately, we have no idea what C library functions
534 might call malloc, so we can't really protect them unless you're
535 using GNU malloc. Fortunately, most of the major operating can use
536 GNU malloc. */
537
538#ifndef SYSTEM_MALLOC
2e471eb5 539
b0846f52
JB
540extern void * (*__malloc_hook) ();
541static void * (*old_malloc_hook) ();
542extern void * (*__realloc_hook) ();
543static void * (*old_realloc_hook) ();
544extern void (*__free_hook) ();
545static void (*old_free_hook) ();
9ac0d9e0 546
276cbe5a
RS
547/* This function is used as the hook for free to call. */
548
9ac0d9e0
JB
549static void
550emacs_blocked_free (ptr)
551 void *ptr;
552{
553 BLOCK_INPUT;
554 __free_hook = old_free_hook;
555 free (ptr);
276cbe5a
RS
556 /* If we released our reserve (due to running out of memory),
557 and we have a fair amount free once again,
558 try to set aside another reserve in case we run out once more. */
559 if (spare_memory == 0
560 /* Verify there is enough space that even with the malloc
561 hysteresis this call won't run out again.
562 The code here is correct as long as SPARE_MEMORY
563 is substantially larger than the block size malloc uses. */
564 && (bytes_used_when_full
d1658221 565 > BYTES_USED + max (malloc_hysteresis, 4) * SPARE_MEMORY))
276cbe5a
RS
566 spare_memory = (char *) malloc (SPARE_MEMORY);
567
b0846f52 568 __free_hook = emacs_blocked_free;
9ac0d9e0
JB
569 UNBLOCK_INPUT;
570}
571
34400008 572
276cbe5a
RS
573/* If we released our reserve (due to running out of memory),
574 and we have a fair amount free once again,
575 try to set aside another reserve in case we run out once more.
576
577 This is called when a relocatable block is freed in ralloc.c. */
578
579void
580refill_memory_reserve ()
581{
582 if (spare_memory == 0)
583 spare_memory = (char *) malloc (SPARE_MEMORY);
584}
585
34400008 586
276cbe5a
RS
587/* This function is the malloc hook that Emacs uses. */
588
9ac0d9e0
JB
589static void *
590emacs_blocked_malloc (size)
591 unsigned size;
592{
593 void *value;
594
595 BLOCK_INPUT;
596 __malloc_hook = old_malloc_hook;
1177ecf6 597#ifdef DOUG_LEA_MALLOC
d1658221 598 mallopt (M_TOP_PAD, malloc_hysteresis * 4096);
1177ecf6 599#else
d1658221 600 __malloc_extra_blocks = malloc_hysteresis;
1177ecf6 601#endif
2756d8ee 602 value = (void *) malloc (size);
b0846f52 603 __malloc_hook = emacs_blocked_malloc;
9ac0d9e0
JB
604 UNBLOCK_INPUT;
605
606 return value;
607}
608
34400008
GM
609
610/* This function is the realloc hook that Emacs uses. */
611
9ac0d9e0
JB
612static void *
613emacs_blocked_realloc (ptr, size)
614 void *ptr;
615 unsigned size;
616{
617 void *value;
618
619 BLOCK_INPUT;
620 __realloc_hook = old_realloc_hook;
2756d8ee 621 value = (void *) realloc (ptr, size);
b0846f52 622 __realloc_hook = emacs_blocked_realloc;
9ac0d9e0
JB
623 UNBLOCK_INPUT;
624
625 return value;
626}
627
34400008
GM
628
629/* Called from main to set up malloc to use our hooks. */
630
9ac0d9e0
JB
631void
632uninterrupt_malloc ()
633{
c8099634
RS
634 if (__free_hook != emacs_blocked_free)
635 old_free_hook = __free_hook;
b0846f52 636 __free_hook = emacs_blocked_free;
9ac0d9e0 637
c8099634
RS
638 if (__malloc_hook != emacs_blocked_malloc)
639 old_malloc_hook = __malloc_hook;
b0846f52 640 __malloc_hook = emacs_blocked_malloc;
9ac0d9e0 641
c8099634
RS
642 if (__realloc_hook != emacs_blocked_realloc)
643 old_realloc_hook = __realloc_hook;
b0846f52 644 __realloc_hook = emacs_blocked_realloc;
9ac0d9e0 645}
2e471eb5
GM
646
647#endif /* not SYSTEM_MALLOC */
648
649
7146af97 650\f
2e471eb5
GM
651/***********************************************************************
652 Interval Allocation
653 ***********************************************************************/
1a4f1e2c 654
34400008
GM
655/* Number of intervals allocated in an interval_block structure.
656 The 1020 is 1024 minus malloc overhead. */
657
d5e35230
JA
658#define INTERVAL_BLOCK_SIZE \
659 ((1020 - sizeof (struct interval_block *)) / sizeof (struct interval))
660
34400008
GM
661/* Intervals are allocated in chunks in form of an interval_block
662 structure. */
663
d5e35230 664struct interval_block
2e471eb5
GM
665{
666 struct interval_block *next;
667 struct interval intervals[INTERVAL_BLOCK_SIZE];
668};
d5e35230 669
34400008
GM
670/* Current interval block. Its `next' pointer points to older
671 blocks. */
672
d5e35230 673struct interval_block *interval_block;
34400008
GM
674
675/* Index in interval_block above of the next unused interval
676 structure. */
677
d5e35230 678static int interval_block_index;
34400008
GM
679
680/* Number of free and live intervals. */
681
2e471eb5 682static int total_free_intervals, total_intervals;
d5e35230 683
34400008
GM
684/* List of free intervals. */
685
d5e35230
JA
686INTERVAL interval_free_list;
687
c8099634 688/* Total number of interval blocks now in use. */
2e471eb5 689
c8099634
RS
690int n_interval_blocks;
691
34400008
GM
692
693/* Initialize interval allocation. */
694
d5e35230
JA
695static void
696init_intervals ()
697{
698 interval_block
34400008
GM
699 = (struct interval_block *) lisp_malloc (sizeof *interval_block,
700 MEM_TYPE_NON_LISP);
d5e35230 701 interval_block->next = 0;
290c8f1e 702 bzero ((char *) interval_block->intervals, sizeof interval_block->intervals);
d5e35230
JA
703 interval_block_index = 0;
704 interval_free_list = 0;
c8099634 705 n_interval_blocks = 1;
d5e35230
JA
706}
707
34400008
GM
708
709/* Return a new interval. */
d5e35230
JA
710
711INTERVAL
712make_interval ()
713{
714 INTERVAL val;
715
716 if (interval_free_list)
717 {
718 val = interval_free_list;
439d5cb4 719 interval_free_list = INTERVAL_PARENT (interval_free_list);
d5e35230
JA
720 }
721 else
722 {
723 if (interval_block_index == INTERVAL_BLOCK_SIZE)
724 {
3c06d205
KH
725 register struct interval_block *newi;
726
34400008
GM
727 newi = (struct interval_block *) lisp_malloc (sizeof *newi,
728 MEM_TYPE_NON_LISP);
d5e35230
JA
729
730 VALIDATE_LISP_STORAGE (newi, sizeof *newi);
731 newi->next = interval_block;
732 interval_block = newi;
733 interval_block_index = 0;
c8099634 734 n_interval_blocks++;
d5e35230
JA
735 }
736 val = &interval_block->intervals[interval_block_index++];
737 }
738 consing_since_gc += sizeof (struct interval);
310ea200 739 intervals_consed++;
d5e35230
JA
740 RESET_INTERVAL (val);
741 return val;
742}
743
34400008
GM
744
745/* Mark Lisp objects in interval I. */
d5e35230
JA
746
747static void
d393c068 748mark_interval (i, dummy)
d5e35230 749 register INTERVAL i;
d393c068 750 Lisp_Object dummy;
d5e35230
JA
751{
752 if (XMARKBIT (i->plist))
753 abort ();
754 mark_object (&i->plist);
755 XMARK (i->plist);
756}
757
34400008
GM
758
759/* Mark the interval tree rooted in TREE. Don't call this directly;
760 use the macro MARK_INTERVAL_TREE instead. */
761
d5e35230
JA
762static void
763mark_interval_tree (tree)
764 register INTERVAL tree;
765{
e8720644
JB
766 /* No need to test if this tree has been marked already; this
767 function is always called through the MARK_INTERVAL_TREE macro,
768 which takes care of that. */
769
770 /* XMARK expands to an assignment; the LHS of an assignment can't be
771 a cast. */
e0b8c689 772 XMARK (tree->up.obj);
d5e35230 773
d393c068 774 traverse_intervals (tree, 1, 0, mark_interval, Qnil);
d5e35230
JA
775}
776
34400008
GM
777
778/* Mark the interval tree rooted in I. */
779
e8720644
JB
780#define MARK_INTERVAL_TREE(i) \
781 do { \
782 if (!NULL_INTERVAL_P (i) \
e0b8c689 783 && ! XMARKBIT (i->up.obj)) \
e8720644
JB
784 mark_interval_tree (i); \
785 } while (0)
d5e35230 786
34400008 787
1a4f1e2c 788/* The oddity in the call to XUNMARK is necessary because XUNMARK
2e471eb5
GM
789 expands to an assignment to its argument, and most C compilers
790 don't support casts on the left operand of `='. */
791
792#define UNMARK_BALANCE_INTERVALS(i) \
793 do { \
794 if (! NULL_INTERVAL_P (i)) \
795 { \
e0b8c689 796 XUNMARK ((i)->up.obj); \
2e471eb5
GM
797 (i) = balance_intervals (i); \
798 } \
799 } while (0)
d5e35230 800
cc2d8c6b
KR
801\f
802/* Number support. If NO_UNION_TYPE isn't in effect, we
803 can't create number objects in macros. */
804#ifndef make_number
805Lisp_Object
806make_number (n)
807 int n;
808{
809 Lisp_Object obj;
810 obj.s.val = n;
811 obj.s.type = Lisp_Int;
812 return obj;
813}
814#endif
d5e35230 815\f
2e471eb5
GM
816/***********************************************************************
817 String Allocation
818 ***********************************************************************/
1a4f1e2c 819
2e471eb5
GM
820/* Lisp_Strings are allocated in string_block structures. When a new
821 string_block is allocated, all the Lisp_Strings it contains are
822 added to a free-list stiing_free_list. When a new Lisp_String is
823 needed, it is taken from that list. During the sweep phase of GC,
824 string_blocks that are entirely free are freed, except two which
825 we keep.
7146af97 826
2e471eb5
GM
827 String data is allocated from sblock structures. Strings larger
828 than LARGE_STRING_BYTES, get their own sblock, data for smaller
829 strings is sub-allocated out of sblocks of size SBLOCK_SIZE.
7146af97 830
2e471eb5
GM
831 Sblocks consist internally of sdata structures, one for each
832 Lisp_String. The sdata structure points to the Lisp_String it
833 belongs to. The Lisp_String points back to the `u.data' member of
834 its sdata structure.
7146af97 835
2e471eb5
GM
836 When a Lisp_String is freed during GC, it is put back on
837 string_free_list, and its `data' member and its sdata's `string'
838 pointer is set to null. The size of the string is recorded in the
839 `u.nbytes' member of the sdata. So, sdata structures that are no
840 longer used, can be easily recognized, and it's easy to compact the
841 sblocks of small strings which we do in compact_small_strings. */
7146af97 842
2e471eb5
GM
843/* Size in bytes of an sblock structure used for small strings. This
844 is 8192 minus malloc overhead. */
7146af97 845
2e471eb5 846#define SBLOCK_SIZE 8188
c8099634 847
2e471eb5
GM
848/* Strings larger than this are considered large strings. String data
849 for large strings is allocated from individual sblocks. */
7146af97 850
2e471eb5
GM
851#define LARGE_STRING_BYTES 1024
852
853/* Structure describing string memory sub-allocated from an sblock.
854 This is where the contents of Lisp strings are stored. */
855
856struct sdata
7146af97 857{
2e471eb5
GM
858 /* Back-pointer to the string this sdata belongs to. If null, this
859 structure is free, and the NBYTES member of the union below
34400008 860 contains the string's byte size (the same value that STRING_BYTES
2e471eb5
GM
861 would return if STRING were non-null). If non-null, STRING_BYTES
862 (STRING) is the size of the data, and DATA contains the string's
863 contents. */
864 struct Lisp_String *string;
7146af97 865
2e471eb5
GM
866 union
867 {
868 /* When STRING in non-null. */
869 unsigned char data[1];
870
871 /* When STRING is null. */
872 EMACS_INT nbytes;
873 } u;
874};
875
876/* Structure describing a block of memory which is sub-allocated to
877 obtain string data memory for strings. Blocks for small strings
878 are of fixed size SBLOCK_SIZE. Blocks for large strings are made
879 as large as needed. */
880
881struct sblock
7146af97 882{
2e471eb5
GM
883 /* Next in list. */
884 struct sblock *next;
7146af97 885
2e471eb5
GM
886 /* Pointer to the next free sdata block. This points past the end
887 of the sblock if there isn't any space left in this block. */
888 struct sdata *next_free;
889
890 /* Start of data. */
891 struct sdata first_data;
892};
893
894/* Number of Lisp strings in a string_block structure. The 1020 is
895 1024 minus malloc overhead. */
896
897#define STRINGS_IN_STRING_BLOCK \
898 ((1020 - sizeof (struct string_block *)) / sizeof (struct Lisp_String))
899
900/* Structure describing a block from which Lisp_String structures
901 are allocated. */
902
903struct string_block
7146af97 904{
2e471eb5
GM
905 struct string_block *next;
906 struct Lisp_String strings[STRINGS_IN_STRING_BLOCK];
907};
7146af97 908
2e471eb5
GM
909/* Head and tail of the list of sblock structures holding Lisp string
910 data. We always allocate from current_sblock. The NEXT pointers
911 in the sblock structures go from oldest_sblock to current_sblock. */
3c06d205 912
2e471eb5 913static struct sblock *oldest_sblock, *current_sblock;
7146af97 914
2e471eb5 915/* List of sblocks for large strings. */
7146af97 916
2e471eb5 917static struct sblock *large_sblocks;
7146af97 918
2e471eb5 919/* List of string_block structures, and how many there are. */
7146af97 920
2e471eb5
GM
921static struct string_block *string_blocks;
922static int n_string_blocks;
7146af97 923
2e471eb5 924/* Free-list of Lisp_Strings. */
7146af97 925
2e471eb5 926static struct Lisp_String *string_free_list;
7146af97 927
2e471eb5 928/* Number of live and free Lisp_Strings. */
c8099634 929
2e471eb5 930static int total_strings, total_free_strings;
7146af97 931
2e471eb5
GM
932/* Number of bytes used by live strings. */
933
934static int total_string_size;
935
936/* Given a pointer to a Lisp_String S which is on the free-list
937 string_free_list, return a pointer to its successor in the
938 free-list. */
939
940#define NEXT_FREE_LISP_STRING(S) (*(struct Lisp_String **) (S))
941
942/* Return a pointer to the sdata structure belonging to Lisp string S.
943 S must be live, i.e. S->data must not be null. S->data is actually
944 a pointer to the `u.data' member of its sdata structure; the
945 structure starts at a constant offset in front of that. */
946
947#define SDATA_OF_STRING(S) \
948 ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *)))
949
950/* Value is the size of an sdata structure large enough to hold NBYTES
951 bytes of string data. The value returned includes a terminating
952 NUL byte, the size of the sdata structure, and padding. */
953
954#define SDATA_SIZE(NBYTES) \
955 ((sizeof (struct Lisp_String *) \
956 + (NBYTES) + 1 \
957 + sizeof (EMACS_INT) - 1) \
958 & ~(sizeof (EMACS_INT) - 1))
959
960
961/* Initialize string allocation. Called from init_alloc_once. */
d457598b
AS
962
963void
2e471eb5 964init_strings ()
7146af97 965{
2e471eb5
GM
966 total_strings = total_free_strings = total_string_size = 0;
967 oldest_sblock = current_sblock = large_sblocks = NULL;
968 string_blocks = NULL;
969 n_string_blocks = 0;
970 string_free_list = NULL;
7146af97
JB
971}
972
2e471eb5
GM
973
974/* Return a new Lisp_String. */
975
976static struct Lisp_String *
977allocate_string ()
7146af97 978{
2e471eb5 979 struct Lisp_String *s;
7146af97 980
2e471eb5
GM
981 /* If the free-list is empty, allocate a new string_block, and
982 add all the Lisp_Strings in it to the free-list. */
983 if (string_free_list == NULL)
7146af97 984 {
2e471eb5
GM
985 struct string_block *b;
986 int i;
987
34400008 988 b = (struct string_block *) lisp_malloc (sizeof *b, MEM_TYPE_STRING);
2e471eb5
GM
989 VALIDATE_LISP_STORAGE (b, sizeof *b);
990 bzero (b, sizeof *b);
991 b->next = string_blocks;
992 string_blocks = b;
993 ++n_string_blocks;
994
995 for (i = STRINGS_IN_STRING_BLOCK - 1; i >= 0; --i)
7146af97 996 {
2e471eb5
GM
997 s = b->strings + i;
998 NEXT_FREE_LISP_STRING (s) = string_free_list;
999 string_free_list = s;
7146af97 1000 }
2e471eb5
GM
1001
1002 total_free_strings += STRINGS_IN_STRING_BLOCK;
7146af97 1003 }
c0f51373 1004
2e471eb5
GM
1005 /* Pop a Lisp_String off the free-list. */
1006 s = string_free_list;
1007 string_free_list = NEXT_FREE_LISP_STRING (s);
c0f51373 1008
2e471eb5
GM
1009 /* Probably not strictly necessary, but play it safe. */
1010 bzero (s, sizeof *s);
c0f51373 1011
2e471eb5
GM
1012 --total_free_strings;
1013 ++total_strings;
1014 ++strings_consed;
1015 consing_since_gc += sizeof *s;
c0f51373 1016
2e471eb5 1017 return s;
c0f51373 1018}
7146af97 1019
7146af97 1020
2e471eb5
GM
1021/* Set up Lisp_String S for holding NCHARS characters, NBYTES bytes,
1022 plus a NUL byte at the end. Allocate an sdata structure for S, and
1023 set S->data to its `u.data' member. Store a NUL byte at the end of
1024 S->data. Set S->size to NCHARS and S->size_byte to NBYTES. Free
1025 S->data if it was initially non-null. */
7146af97 1026
2e471eb5
GM
1027void
1028allocate_string_data (s, nchars, nbytes)
1029 struct Lisp_String *s;
1030 int nchars, nbytes;
7146af97 1031{
5c5fecb3 1032 struct sdata *data, *old_data;
2e471eb5 1033 struct sblock *b;
5c5fecb3 1034 int needed, old_nbytes;
7146af97 1035
2e471eb5
GM
1036 /* Determine the number of bytes needed to store NBYTES bytes
1037 of string data. */
1038 needed = SDATA_SIZE (nbytes);
7146af97 1039
2e471eb5
GM
1040 if (nbytes > LARGE_STRING_BYTES)
1041 {
1042 int size = sizeof *b - sizeof (struct sdata) + needed;
1043
1044#ifdef DOUG_LEA_MALLOC
1045 /* Prevent mmap'ing the chunk (which is potentially very large). */
1046 mallopt (M_MMAP_MAX, 0);
1047#endif
1048
34400008 1049 b = (struct sblock *) lisp_malloc (size, MEM_TYPE_NON_LISP);
2e471eb5
GM
1050
1051#ifdef DOUG_LEA_MALLOC
1052 /* Back to a reasonable maximum of mmap'ed areas. */
1053 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
1054#endif
1055
1056 b->next_free = &b->first_data;
1057 b->first_data.string = NULL;
1058 b->next = large_sblocks;
1059 large_sblocks = b;
1060 }
1061 else if (current_sblock == NULL
1062 || (((char *) current_sblock + SBLOCK_SIZE
1063 - (char *) current_sblock->next_free)
1064 < needed))
1065 {
1066 /* Not enough room in the current sblock. */
34400008 1067 b = (struct sblock *) lisp_malloc (SBLOCK_SIZE, MEM_TYPE_NON_LISP);
2e471eb5
GM
1068 b->next_free = &b->first_data;
1069 b->first_data.string = NULL;
1070 b->next = NULL;
1071
1072 if (current_sblock)
1073 current_sblock->next = b;
1074 else
1075 oldest_sblock = b;
1076 current_sblock = b;
1077 }
1078 else
1079 b = current_sblock;
5c5fecb3
GM
1080
1081 old_data = s->data ? SDATA_OF_STRING (s) : NULL;
1082 old_nbytes = GC_STRING_BYTES (s);
2e471eb5
GM
1083
1084 data = b->next_free;
1085 data->string = s;
1086 s->data = data->u.data;
1087 s->size = nchars;
1088 s->size_byte = nbytes;
1089 s->data[nbytes] = '\0';
1090 b->next_free = (struct sdata *) ((char *) data + needed);
1091
5c5fecb3
GM
1092 /* If S had already data assigned, mark that as free by setting its
1093 string back-pointer to null, and recording the size of the data
1094 in it.. Copy old string contents to the new sdata. */
1095 if (old_data)
1096 {
1097 bcopy (old_data->u.data, s->data, old_nbytes);
1098 old_data->u.nbytes = old_nbytes;
1099 old_data->string = NULL;
1100 }
1101
2e471eb5
GM
1102 consing_since_gc += needed;
1103}
1104
1105
1106/* Sweep and compact strings. */
1107
1108static void
1109sweep_strings ()
1110{
1111 struct string_block *b, *next;
1112 struct string_block *live_blocks = NULL;
1113
1114 string_free_list = NULL;
1115 total_strings = total_free_strings = 0;
1116 total_string_size = 0;
1117
1118 /* Scan strings_blocks, free Lisp_Strings that aren't marked. */
1119 for (b = string_blocks; b; b = next)
1120 {
1121 int i, nfree = 0;
1122 struct Lisp_String *free_list_before = string_free_list;
1123
1124 next = b->next;
1125
1126 for (i = 0; i < STRINGS_IN_STRING_BLOCK; ++i)
1127 {
1128 struct Lisp_String *s = b->strings + i;
1129
1130 if (s->data)
1131 {
1132 /* String was not on free-list before. */
1133 if (STRING_MARKED_P (s))
1134 {
1135 /* String is live; unmark it and its intervals. */
1136 UNMARK_STRING (s);
1137
1138 if (!NULL_INTERVAL_P (s->intervals))
1139 UNMARK_BALANCE_INTERVALS (s->intervals);
1140
1141 ++total_strings;
1142 total_string_size += STRING_BYTES (s);
1143 }
1144 else
1145 {
1146 /* String is dead. Put it on the free-list. */
1147 struct sdata *data = SDATA_OF_STRING (s);
1148
1149 /* Save the size of S in its sdata so that we know
1150 how large that is. Reset the sdata's string
1151 back-pointer so that we know it's free. */
1152 data->u.nbytes = GC_STRING_BYTES (s);
1153 data->string = NULL;
1154
1155 /* Reset the strings's `data' member so that we
1156 know it's free. */
1157 s->data = NULL;
1158
1159 /* Put the string on the free-list. */
1160 NEXT_FREE_LISP_STRING (s) = string_free_list;
1161 string_free_list = s;
1162 ++nfree;
1163 }
1164 }
1165 else
1166 {
1167 /* S was on the free-list before. Put it there again. */
1168 NEXT_FREE_LISP_STRING (s) = string_free_list;
1169 string_free_list = s;
1170 ++nfree;
1171 }
1172 }
1173
34400008 1174 /* Free blocks that contain free Lisp_Strings only, except
2e471eb5
GM
1175 the first two of them. */
1176 if (nfree == STRINGS_IN_STRING_BLOCK
1177 && total_free_strings > STRINGS_IN_STRING_BLOCK)
1178 {
1179 lisp_free (b);
1180 --n_string_blocks;
1181 string_free_list = free_list_before;
1182 }
1183 else
1184 {
1185 total_free_strings += nfree;
1186 b->next = live_blocks;
1187 live_blocks = b;
1188 }
1189 }
1190
1191 string_blocks = live_blocks;
1192 free_large_strings ();
1193 compact_small_strings ();
1194}
1195
1196
1197/* Free dead large strings. */
1198
1199static void
1200free_large_strings ()
1201{
1202 struct sblock *b, *next;
1203 struct sblock *live_blocks = NULL;
1204
1205 for (b = large_sblocks; b; b = next)
1206 {
1207 next = b->next;
1208
1209 if (b->first_data.string == NULL)
1210 lisp_free (b);
1211 else
1212 {
1213 b->next = live_blocks;
1214 live_blocks = b;
1215 }
1216 }
1217
1218 large_sblocks = live_blocks;
1219}
1220
1221
1222/* Compact data of small strings. Free sblocks that don't contain
1223 data of live strings after compaction. */
1224
1225static void
1226compact_small_strings ()
1227{
1228 struct sblock *b, *tb, *next;
1229 struct sdata *from, *to, *end, *tb_end;
1230 struct sdata *to_end, *from_end;
1231
1232 /* TB is the sblock we copy to, TO is the sdata within TB we copy
1233 to, and TB_END is the end of TB. */
1234 tb = oldest_sblock;
1235 tb_end = (struct sdata *) ((char *) tb + SBLOCK_SIZE);
1236 to = &tb->first_data;
1237
1238 /* Step through the blocks from the oldest to the youngest. We
1239 expect that old blocks will stabilize over time, so that less
1240 copying will happen this way. */
1241 for (b = oldest_sblock; b; b = b->next)
1242 {
1243 end = b->next_free;
1244 xassert ((char *) end <= (char *) b + SBLOCK_SIZE);
1245
1246 for (from = &b->first_data; from < end; from = from_end)
1247 {
1248 /* Compute the next FROM here because copying below may
1249 overwrite data we need to compute it. */
1250 int nbytes;
1251
1252 if (from->string)
1253 nbytes = GC_STRING_BYTES (from->string);
1254 else
1255 nbytes = from->u.nbytes;
1256
1257 nbytes = SDATA_SIZE (nbytes);
1258 from_end = (struct sdata *) ((char *) from + nbytes);
1259
1260 /* FROM->string non-null means it's alive. Copy its data. */
1261 if (from->string)
1262 {
1263 /* If TB is full, proceed with the next sblock. */
1264 to_end = (struct sdata *) ((char *) to + nbytes);
1265 if (to_end > tb_end)
1266 {
1267 tb->next_free = to;
1268 tb = tb->next;
1269 tb_end = (struct sdata *) ((char *) tb + SBLOCK_SIZE);
1270 to = &tb->first_data;
1271 to_end = (struct sdata *) ((char *) to + nbytes);
1272 }
1273
1274 /* Copy, and update the string's `data' pointer. */
1275 if (from != to)
1276 {
1277 bcopy (from, to, nbytes);
1278 to->string->data = to->u.data;
1279 }
1280
1281 /* Advance past the sdata we copied to. */
1282 to = to_end;
1283 }
1284 }
1285 }
1286
1287 /* The rest of the sblocks following TB don't contain live data, so
1288 we can free them. */
1289 for (b = tb->next; b; b = next)
1290 {
1291 next = b->next;
1292 lisp_free (b);
1293 }
1294
1295 tb->next_free = to;
1296 tb->next = NULL;
1297 current_sblock = tb;
1298}
1299
1300
1301DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0,
1302 "Return a newly created string of length LENGTH, with each element being INIT.\n\
1303Both LENGTH and INIT must be numbers.")
1304 (length, init)
1305 Lisp_Object length, init;
1306{
1307 register Lisp_Object val;
1308 register unsigned char *p, *end;
1309 int c, nbytes;
1310
1311 CHECK_NATNUM (length, 0);
1312 CHECK_NUMBER (init, 1);
1313
1314 c = XINT (init);
1315 if (SINGLE_BYTE_CHAR_P (c))
1316 {
1317 nbytes = XINT (length);
1318 val = make_uninit_string (nbytes);
1319 p = XSTRING (val)->data;
1320 end = p + XSTRING (val)->size;
1321 while (p != end)
1322 *p++ = c;
1323 }
1324 else
1325 {
1326 unsigned char str[4];
1327 int len = CHAR_STRING (c, str);
1328
1329 nbytes = len * XINT (length);
1330 val = make_uninit_multibyte_string (XINT (length), nbytes);
1331 p = XSTRING (val)->data;
1332 end = p + nbytes;
1333 while (p != end)
1334 {
1335 bcopy (str, p, len);
1336 p += len;
1337 }
1338 }
1339
1340 *p = 0;
1341 return val;
1342}
1343
1344
1345DEFUN ("make-bool-vector", Fmake_bool_vector, Smake_bool_vector, 2, 2, 0,
1346 "Return a new bool-vector of length LENGTH, using INIT for as each element.\n\
1347LENGTH must be a number. INIT matters only in whether it is t or nil.")
1348 (length, init)
1349 Lisp_Object length, init;
1350{
1351 register Lisp_Object val;
1352 struct Lisp_Bool_Vector *p;
1353 int real_init, i;
1354 int length_in_chars, length_in_elts, bits_per_value;
1355
1356 CHECK_NATNUM (length, 0);
1357
1358 bits_per_value = sizeof (EMACS_INT) * BITS_PER_CHAR;
1359
1360 length_in_elts = (XFASTINT (length) + bits_per_value - 1) / bits_per_value;
1361 length_in_chars = ((XFASTINT (length) + BITS_PER_CHAR - 1) / BITS_PER_CHAR);
1362
1363 /* We must allocate one more elements than LENGTH_IN_ELTS for the
1364 slot `size' of the struct Lisp_Bool_Vector. */
1365 val = Fmake_vector (make_number (length_in_elts + 1), Qnil);
1366 p = XBOOL_VECTOR (val);
34400008 1367
2e471eb5
GM
1368 /* Get rid of any bits that would cause confusion. */
1369 p->vector_size = 0;
1370 XSETBOOL_VECTOR (val, p);
1371 p->size = XFASTINT (length);
1372
1373 real_init = (NILP (init) ? 0 : -1);
1374 for (i = 0; i < length_in_chars ; i++)
1375 p->data[i] = real_init;
34400008 1376
2e471eb5
GM
1377 /* Clear the extraneous bits in the last byte. */
1378 if (XINT (length) != length_in_chars * BITS_PER_CHAR)
1379 XBOOL_VECTOR (val)->data[length_in_chars - 1]
1380 &= (1 << (XINT (length) % BITS_PER_CHAR)) - 1;
1381
1382 return val;
1383}
1384
1385
1386/* Make a string from NBYTES bytes at CONTENTS, and compute the number
1387 of characters from the contents. This string may be unibyte or
1388 multibyte, depending on the contents. */
1389
1390Lisp_Object
1391make_string (contents, nbytes)
1392 char *contents;
1393 int nbytes;
1394{
1395 register Lisp_Object val;
9eac9d59
KH
1396 int nchars, multibyte_nbytes;
1397
1398 parse_str_as_multibyte (contents, nbytes, &nchars, &multibyte_nbytes);
2e471eb5
GM
1399 val = make_uninit_multibyte_string (nchars, nbytes);
1400 bcopy (contents, XSTRING (val)->data, nbytes);
9eac9d59
KH
1401 if (nbytes == nchars || nbytes != multibyte_nbytes)
1402 /* CONTENTS contains no multibyte sequences or contains an invalid
1403 multibyte sequence. We must make unibyte string. */
2e471eb5
GM
1404 SET_STRING_BYTES (XSTRING (val), -1);
1405 return val;
1406}
1407
1408
1409/* Make an unibyte string from LENGTH bytes at CONTENTS. */
1410
1411Lisp_Object
1412make_unibyte_string (contents, length)
1413 char *contents;
1414 int length;
1415{
1416 register Lisp_Object val;
1417 val = make_uninit_string (length);
1418 bcopy (contents, XSTRING (val)->data, length);
1419 SET_STRING_BYTES (XSTRING (val), -1);
1420 return val;
1421}
1422
1423
1424/* Make a multibyte string from NCHARS characters occupying NBYTES
1425 bytes at CONTENTS. */
1426
1427Lisp_Object
1428make_multibyte_string (contents, nchars, nbytes)
1429 char *contents;
1430 int nchars, nbytes;
1431{
1432 register Lisp_Object val;
1433 val = make_uninit_multibyte_string (nchars, nbytes);
1434 bcopy (contents, XSTRING (val)->data, nbytes);
1435 return val;
1436}
1437
1438
1439/* Make a string from NCHARS characters occupying NBYTES bytes at
1440 CONTENTS. It is a multibyte string if NBYTES != NCHARS. */
1441
1442Lisp_Object
1443make_string_from_bytes (contents, nchars, nbytes)
1444 char *contents;
1445 int nchars, nbytes;
1446{
1447 register Lisp_Object val;
1448 val = make_uninit_multibyte_string (nchars, nbytes);
1449 bcopy (contents, XSTRING (val)->data, nbytes);
1450 if (STRING_BYTES (XSTRING (val)) == XSTRING (val)->size)
1451 SET_STRING_BYTES (XSTRING (val), -1);
1452 return val;
1453}
1454
1455
1456/* Make a string from NCHARS characters occupying NBYTES bytes at
1457 CONTENTS. The argument MULTIBYTE controls whether to label the
1458 string as multibyte. */
1459
1460Lisp_Object
1461make_specified_string (contents, nchars, nbytes, multibyte)
1462 char *contents;
1463 int nchars, nbytes;
1464 int multibyte;
1465{
1466 register Lisp_Object val;
1467 val = make_uninit_multibyte_string (nchars, nbytes);
1468 bcopy (contents, XSTRING (val)->data, nbytes);
1469 if (!multibyte)
1470 SET_STRING_BYTES (XSTRING (val), -1);
1471 return val;
1472}
1473
1474
1475/* Make a string from the data at STR, treating it as multibyte if the
1476 data warrants. */
1477
1478Lisp_Object
1479build_string (str)
1480 char *str;
1481{
1482 return make_string (str, strlen (str));
1483}
1484
1485
1486/* Return an unibyte Lisp_String set up to hold LENGTH characters
1487 occupying LENGTH bytes. */
1488
1489Lisp_Object
1490make_uninit_string (length)
1491 int length;
1492{
1493 Lisp_Object val;
1494 val = make_uninit_multibyte_string (length, length);
1495 SET_STRING_BYTES (XSTRING (val), -1);
1496 return val;
1497}
1498
1499
1500/* Return a multibyte Lisp_String set up to hold NCHARS characters
1501 which occupy NBYTES bytes. */
1502
1503Lisp_Object
1504make_uninit_multibyte_string (nchars, nbytes)
1505 int nchars, nbytes;
1506{
1507 Lisp_Object string;
1508 struct Lisp_String *s;
1509
1510 if (nchars < 0)
1511 abort ();
1512
1513 s = allocate_string ();
1514 allocate_string_data (s, nchars, nbytes);
1515 XSETSTRING (string, s);
1516 string_chars_consed += nbytes;
1517 return string;
1518}
1519
1520
1521\f
1522/***********************************************************************
1523 Float Allocation
1524 ***********************************************************************/
1525
2e471eb5
GM
1526/* We store float cells inside of float_blocks, allocating a new
1527 float_block with malloc whenever necessary. Float cells reclaimed
1528 by GC are put on a free list to be reallocated before allocating
1529 any new float cells from the latest float_block.
1530
1531 Each float_block is just under 1020 bytes long, since malloc really
1532 allocates in units of powers of two and uses 4 bytes for its own
1533 overhead. */
1534
1535#define FLOAT_BLOCK_SIZE \
1536 ((1020 - sizeof (struct float_block *)) / sizeof (struct Lisp_Float))
1537
1538struct float_block
1539{
1540 struct float_block *next;
1541 struct Lisp_Float floats[FLOAT_BLOCK_SIZE];
1542};
1543
34400008
GM
1544/* Current float_block. */
1545
2e471eb5 1546struct float_block *float_block;
34400008
GM
1547
1548/* Index of first unused Lisp_Float in the current float_block. */
1549
2e471eb5
GM
1550int float_block_index;
1551
1552/* Total number of float blocks now in use. */
1553
1554int n_float_blocks;
1555
34400008
GM
1556/* Free-list of Lisp_Floats. */
1557
2e471eb5
GM
1558struct Lisp_Float *float_free_list;
1559
34400008
GM
1560
1561/* Initialze float allocation. */
1562
2e471eb5
GM
1563void
1564init_float ()
1565{
34400008
GM
1566 float_block = (struct float_block *) lisp_malloc (sizeof *float_block,
1567 MEM_TYPE_FLOAT);
2e471eb5
GM
1568 float_block->next = 0;
1569 bzero ((char *) float_block->floats, sizeof float_block->floats);
1570 float_block_index = 0;
1571 float_free_list = 0;
1572 n_float_blocks = 1;
1573}
1574
34400008
GM
1575
1576/* Explicitly free a float cell by putting it on the free-list. */
2e471eb5
GM
1577
1578void
1579free_float (ptr)
1580 struct Lisp_Float *ptr;
1581{
1582 *(struct Lisp_Float **)&ptr->data = float_free_list;
34400008
GM
1583#if GC_MARK_STACK
1584 ptr->type = Vdead;
1585#endif
2e471eb5
GM
1586 float_free_list = ptr;
1587}
1588
34400008
GM
1589
1590/* Return a new float object with value FLOAT_VALUE. */
1591
2e471eb5
GM
1592Lisp_Object
1593make_float (float_value)
1594 double float_value;
1595{
1596 register Lisp_Object val;
1597
1598 if (float_free_list)
1599 {
1600 /* We use the data field for chaining the free list
1601 so that we won't use the same field that has the mark bit. */
1602 XSETFLOAT (val, float_free_list);
1603 float_free_list = *(struct Lisp_Float **)&float_free_list->data;
1604 }
1605 else
1606 {
1607 if (float_block_index == FLOAT_BLOCK_SIZE)
1608 {
1609 register struct float_block *new;
1610
34400008
GM
1611 new = (struct float_block *) lisp_malloc (sizeof *new,
1612 MEM_TYPE_FLOAT);
2e471eb5
GM
1613 VALIDATE_LISP_STORAGE (new, sizeof *new);
1614 new->next = float_block;
1615 float_block = new;
1616 float_block_index = 0;
1617 n_float_blocks++;
1618 }
1619 XSETFLOAT (val, &float_block->floats[float_block_index++]);
1620 }
1621
1622 XFLOAT_DATA (val) = float_value;
1623 XSETFASTINT (XFLOAT (val)->type, 0); /* bug chasing -wsr */
1624 consing_since_gc += sizeof (struct Lisp_Float);
1625 floats_consed++;
1626 return val;
1627}
1628
2e471eb5
GM
1629
1630\f
1631/***********************************************************************
1632 Cons Allocation
1633 ***********************************************************************/
1634
1635/* We store cons cells inside of cons_blocks, allocating a new
1636 cons_block with malloc whenever necessary. Cons cells reclaimed by
1637 GC are put on a free list to be reallocated before allocating
1638 any new cons cells from the latest cons_block.
1639
1640 Each cons_block is just under 1020 bytes long,
1641 since malloc really allocates in units of powers of two
1642 and uses 4 bytes for its own overhead. */
1643
1644#define CONS_BLOCK_SIZE \
1645 ((1020 - sizeof (struct cons_block *)) / sizeof (struct Lisp_Cons))
1646
1647struct cons_block
1648{
1649 struct cons_block *next;
1650 struct Lisp_Cons conses[CONS_BLOCK_SIZE];
1651};
1652
34400008
GM
1653/* Current cons_block. */
1654
2e471eb5 1655struct cons_block *cons_block;
34400008
GM
1656
1657/* Index of first unused Lisp_Cons in the current block. */
1658
2e471eb5
GM
1659int cons_block_index;
1660
34400008
GM
1661/* Free-list of Lisp_Cons structures. */
1662
2e471eb5
GM
1663struct Lisp_Cons *cons_free_list;
1664
1665/* Total number of cons blocks now in use. */
1666
1667int n_cons_blocks;
1668
34400008
GM
1669
1670/* Initialize cons allocation. */
1671
2e471eb5
GM
1672void
1673init_cons ()
1674{
34400008
GM
1675 cons_block = (struct cons_block *) lisp_malloc (sizeof *cons_block,
1676 MEM_TYPE_CONS);
2e471eb5
GM
1677 cons_block->next = 0;
1678 bzero ((char *) cons_block->conses, sizeof cons_block->conses);
1679 cons_block_index = 0;
1680 cons_free_list = 0;
1681 n_cons_blocks = 1;
1682}
1683
34400008
GM
1684
1685/* Explicitly free a cons cell by putting it on the free-list. */
2e471eb5
GM
1686
1687void
1688free_cons (ptr)
1689 struct Lisp_Cons *ptr;
1690{
1691 *(struct Lisp_Cons **)&ptr->cdr = cons_free_list;
34400008
GM
1692#if GC_MARK_STACK
1693 ptr->car = Vdead;
1694#endif
2e471eb5
GM
1695 cons_free_list = ptr;
1696}
1697
34400008 1698
2e471eb5
GM
1699DEFUN ("cons", Fcons, Scons, 2, 2, 0,
1700 "Create a new cons, give it CAR and CDR as components, and return it.")
1701 (car, cdr)
1702 Lisp_Object car, cdr;
1703{
1704 register Lisp_Object val;
1705
1706 if (cons_free_list)
1707 {
1708 /* We use the cdr for chaining the free list
1709 so that we won't use the same field that has the mark bit. */
1710 XSETCONS (val, cons_free_list);
1711 cons_free_list = *(struct Lisp_Cons **)&cons_free_list->cdr;
1712 }
1713 else
1714 {
1715 if (cons_block_index == CONS_BLOCK_SIZE)
1716 {
1717 register struct cons_block *new;
34400008
GM
1718 new = (struct cons_block *) lisp_malloc (sizeof *new,
1719 MEM_TYPE_CONS);
2e471eb5
GM
1720 VALIDATE_LISP_STORAGE (new, sizeof *new);
1721 new->next = cons_block;
1722 cons_block = new;
1723 cons_block_index = 0;
1724 n_cons_blocks++;
1725 }
1726 XSETCONS (val, &cons_block->conses[cons_block_index++]);
1727 }
1728
1729 XCAR (val) = car;
1730 XCDR (val) = cdr;
1731 consing_since_gc += sizeof (struct Lisp_Cons);
1732 cons_cells_consed++;
1733 return val;
1734}
1735
34400008 1736
2e471eb5
GM
1737/* Make a list of 2, 3, 4 or 5 specified objects. */
1738
1739Lisp_Object
1740list2 (arg1, arg2)
1741 Lisp_Object arg1, arg2;
1742{
1743 return Fcons (arg1, Fcons (arg2, Qnil));
1744}
1745
34400008 1746
2e471eb5
GM
1747Lisp_Object
1748list3 (arg1, arg2, arg3)
1749 Lisp_Object arg1, arg2, arg3;
1750{
1751 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Qnil)));
1752}
1753
34400008 1754
2e471eb5
GM
1755Lisp_Object
1756list4 (arg1, arg2, arg3, arg4)
1757 Lisp_Object arg1, arg2, arg3, arg4;
1758{
1759 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4, Qnil))));
1760}
1761
34400008 1762
2e471eb5
GM
1763Lisp_Object
1764list5 (arg1, arg2, arg3, arg4, arg5)
1765 Lisp_Object arg1, arg2, arg3, arg4, arg5;
1766{
1767 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4,
1768 Fcons (arg5, Qnil)))));
1769}
1770
34400008 1771
2e471eb5
GM
1772DEFUN ("list", Flist, Slist, 0, MANY, 0,
1773 "Return a newly created list with specified arguments as elements.\n\
1774Any number of arguments, even zero arguments, are allowed.")
1775 (nargs, args)
1776 int nargs;
1777 register Lisp_Object *args;
1778{
1779 register Lisp_Object val;
1780 val = Qnil;
1781
1782 while (nargs > 0)
1783 {
1784 nargs--;
1785 val = Fcons (args[nargs], val);
1786 }
1787 return val;
1788}
1789
34400008 1790
2e471eb5
GM
1791DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0,
1792 "Return a newly created list of length LENGTH, with each element being INIT.")
1793 (length, init)
1794 register Lisp_Object length, init;
1795{
1796 register Lisp_Object val;
1797 register int size;
1798
1799 CHECK_NATNUM (length, 0);
1800 size = XFASTINT (length);
1801
1802 val = Qnil;
7146af97
JB
1803 while (size-- > 0)
1804 val = Fcons (init, val);
1805 return val;
1806}
2e471eb5
GM
1807
1808
7146af97 1809\f
2e471eb5
GM
1810/***********************************************************************
1811 Vector Allocation
1812 ***********************************************************************/
7146af97 1813
34400008
GM
1814/* Singly-linked list of all vectors. */
1815
7146af97
JB
1816struct Lisp_Vector *all_vectors;
1817
2e471eb5
GM
1818/* Total number of vector-like objects now in use. */
1819
c8099634
RS
1820int n_vectors;
1821
34400008
GM
1822
1823/* Value is a pointer to a newly allocated Lisp_Vector structure
1824 with room for LEN Lisp_Objects. */
1825
1825c68d
KH
1826struct Lisp_Vector *
1827allocate_vectorlike (len)
1828 EMACS_INT len;
1829{
1830 struct Lisp_Vector *p;
34400008 1831 int nbytes;
1825c68d 1832
d1658221 1833#ifdef DOUG_LEA_MALLOC
2e471eb5 1834 /* Prevent mmap'ing the chunk (which is potentially very large).. */
d1658221
RS
1835 mallopt (M_MMAP_MAX, 0);
1836#endif
34400008
GM
1837
1838 nbytes = sizeof *p + (len - 1) * sizeof p->contents[0];
1839 p = (struct Lisp_Vector *) lisp_malloc (nbytes, MEM_TYPE_VECTOR);
1840
d1658221 1841#ifdef DOUG_LEA_MALLOC
34400008 1842 /* Back to a reasonable maximum of mmap'ed areas. */
81d492d5 1843 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
d1658221 1844#endif
34400008 1845
1825c68d 1846 VALIDATE_LISP_STORAGE (p, 0);
34400008 1847 consing_since_gc += nbytes;
310ea200 1848 vector_cells_consed += len;
1825c68d
KH
1849
1850 p->next = all_vectors;
1851 all_vectors = p;
34400008 1852 ++n_vectors;
1825c68d
KH
1853 return p;
1854}
1855
34400008 1856
7146af97
JB
1857DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0,
1858 "Return a newly created vector of length LENGTH, with each element being INIT.\n\
1859See also the function `vector'.")
1860 (length, init)
1861 register Lisp_Object length, init;
1862{
1825c68d
KH
1863 Lisp_Object vector;
1864 register EMACS_INT sizei;
1865 register int index;
7146af97
JB
1866 register struct Lisp_Vector *p;
1867
c9dad5ed
KH
1868 CHECK_NATNUM (length, 0);
1869 sizei = XFASTINT (length);
7146af97 1870
1825c68d 1871 p = allocate_vectorlike (sizei);
7146af97 1872 p->size = sizei;
7146af97
JB
1873 for (index = 0; index < sizei; index++)
1874 p->contents[index] = init;
1875
1825c68d 1876 XSETVECTOR (vector, p);
7146af97
JB
1877 return vector;
1878}
1879
34400008 1880
a59de17b 1881DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0,
c58b2b4d 1882 "Return a newly created char-table, with purpose PURPOSE.\n\
7b07587b 1883Each element is initialized to INIT, which defaults to nil.\n\
d7cd5d4f 1884PURPOSE should be a symbol which has a `char-table-extra-slots' property.\n\
a59de17b
RS
1885The property's value should be an integer between 0 and 10.")
1886 (purpose, init)
1887 register Lisp_Object purpose, init;
7b07587b
RS
1888{
1889 Lisp_Object vector;
a59de17b
RS
1890 Lisp_Object n;
1891 CHECK_SYMBOL (purpose, 1);
0551bde3 1892 n = Fget (purpose, Qchar_table_extra_slots);
a59de17b 1893 CHECK_NUMBER (n, 0);
7b07587b
RS
1894 if (XINT (n) < 0 || XINT (n) > 10)
1895 args_out_of_range (n, Qnil);
1896 /* Add 2 to the size for the defalt and parent slots. */
1897 vector = Fmake_vector (make_number (CHAR_TABLE_STANDARD_SLOTS + XINT (n)),
1898 init);
0551bde3 1899 XCHAR_TABLE (vector)->top = Qt;
c96a008c 1900 XCHAR_TABLE (vector)->parent = Qnil;
a59de17b 1901 XCHAR_TABLE (vector)->purpose = purpose;
7b07587b
RS
1902 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
1903 return vector;
1904}
1905
34400008 1906
0551bde3
KH
1907/* Return a newly created sub char table with default value DEFALT.
1908 Since a sub char table does not appear as a top level Emacs Lisp
1909 object, we don't need a Lisp interface to make it. */
1910
1911Lisp_Object
1912make_sub_char_table (defalt)
1913 Lisp_Object defalt;
1914{
1915 Lisp_Object vector
1916 = Fmake_vector (make_number (SUB_CHAR_TABLE_STANDARD_SLOTS), Qnil);
1917 XCHAR_TABLE (vector)->top = Qnil;
1918 XCHAR_TABLE (vector)->defalt = defalt;
1919 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
1920 return vector;
1921}
1922
34400008 1923
7146af97
JB
1924DEFUN ("vector", Fvector, Svector, 0, MANY, 0,
1925 "Return a newly created vector with specified arguments as elements.\n\
1926Any number of arguments, even zero arguments, are allowed.")
1927 (nargs, args)
1928 register int nargs;
1929 Lisp_Object *args;
1930{
1931 register Lisp_Object len, val;
1932 register int index;
1933 register struct Lisp_Vector *p;
1934
67ba9986 1935 XSETFASTINT (len, nargs);
7146af97
JB
1936 val = Fmake_vector (len, Qnil);
1937 p = XVECTOR (val);
1938 for (index = 0; index < nargs; index++)
1939 p->contents[index] = args[index];
1940 return val;
1941}
1942
34400008 1943
7146af97
JB
1944DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0,
1945 "Create a byte-code object with specified arguments as elements.\n\
1946The arguments should be the arglist, bytecode-string, constant vector,\n\
1947stack size, (optional) doc string, and (optional) interactive spec.\n\
1948The first four arguments are required; at most six have any\n\
1949significance.")
1950 (nargs, args)
1951 register int nargs;
1952 Lisp_Object *args;
1953{
1954 register Lisp_Object len, val;
1955 register int index;
1956 register struct Lisp_Vector *p;
1957
67ba9986 1958 XSETFASTINT (len, nargs);
265a9e55 1959 if (!NILP (Vpurify_flag))
5a053ea9 1960 val = make_pure_vector ((EMACS_INT) nargs);
7146af97
JB
1961 else
1962 val = Fmake_vector (len, Qnil);
9eac9d59
KH
1963
1964 if (STRINGP (args[1]) && STRING_MULTIBYTE (args[1]))
1965 /* BYTECODE-STRING must have been produced by Emacs 20.2 or the
1966 earlier because they produced a raw 8-bit string for byte-code
1967 and now such a byte-code string is loaded as multibyte while
1968 raw 8-bit characters converted to multibyte form. Thus, now we
1969 must convert them back to the original unibyte form. */
1970 args[1] = Fstring_as_unibyte (args[1]);
1971
7146af97
JB
1972 p = XVECTOR (val);
1973 for (index = 0; index < nargs; index++)
1974 {
265a9e55 1975 if (!NILP (Vpurify_flag))
7146af97
JB
1976 args[index] = Fpurecopy (args[index]);
1977 p->contents[index] = args[index];
1978 }
50aee051 1979 XSETCOMPILED (val, p);
7146af97
JB
1980 return val;
1981}
2e471eb5 1982
34400008 1983
7146af97 1984\f
2e471eb5
GM
1985/***********************************************************************
1986 Symbol Allocation
1987 ***********************************************************************/
7146af97 1988
2e471eb5
GM
1989/* Each symbol_block is just under 1020 bytes long, since malloc
1990 really allocates in units of powers of two and uses 4 bytes for its
1991 own overhead. */
7146af97
JB
1992
1993#define SYMBOL_BLOCK_SIZE \
1994 ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol))
1995
1996struct symbol_block
2e471eb5
GM
1997{
1998 struct symbol_block *next;
1999 struct Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE];
2000};
7146af97 2001
34400008
GM
2002/* Current symbol block and index of first unused Lisp_Symbol
2003 structure in it. */
2004
7146af97
JB
2005struct symbol_block *symbol_block;
2006int symbol_block_index;
2007
34400008
GM
2008/* List of free symbols. */
2009
7146af97
JB
2010struct Lisp_Symbol *symbol_free_list;
2011
c8099634 2012/* Total number of symbol blocks now in use. */
2e471eb5 2013
c8099634
RS
2014int n_symbol_blocks;
2015
34400008
GM
2016
2017/* Initialize symbol allocation. */
2018
7146af97
JB
2019void
2020init_symbol ()
2021{
34400008
GM
2022 symbol_block = (struct symbol_block *) lisp_malloc (sizeof *symbol_block,
2023 MEM_TYPE_SYMBOL);
7146af97 2024 symbol_block->next = 0;
290c8f1e 2025 bzero ((char *) symbol_block->symbols, sizeof symbol_block->symbols);
7146af97
JB
2026 symbol_block_index = 0;
2027 symbol_free_list = 0;
c8099634 2028 n_symbol_blocks = 1;
7146af97
JB
2029}
2030
34400008 2031
7146af97
JB
2032DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0,
2033 "Return a newly allocated uninterned symbol whose name is NAME.\n\
2034Its value and function definition are void, and its property list is nil.")
54ee42dd
EN
2035 (name)
2036 Lisp_Object name;
7146af97
JB
2037{
2038 register Lisp_Object val;
2039 register struct Lisp_Symbol *p;
2040
54ee42dd 2041 CHECK_STRING (name, 0);
7146af97
JB
2042
2043 if (symbol_free_list)
2044 {
45d12a89 2045 XSETSYMBOL (val, symbol_free_list);
85481507 2046 symbol_free_list = *(struct Lisp_Symbol **)&symbol_free_list->value;
7146af97
JB
2047 }
2048 else
2049 {
2050 if (symbol_block_index == SYMBOL_BLOCK_SIZE)
2051 {
3c06d205 2052 struct symbol_block *new;
34400008
GM
2053 new = (struct symbol_block *) lisp_malloc (sizeof *new,
2054 MEM_TYPE_SYMBOL);
7146af97
JB
2055 VALIDATE_LISP_STORAGE (new, sizeof *new);
2056 new->next = symbol_block;
2057 symbol_block = new;
2058 symbol_block_index = 0;
c8099634 2059 n_symbol_blocks++;
7146af97 2060 }
45d12a89 2061 XSETSYMBOL (val, &symbol_block->symbols[symbol_block_index++]);
7146af97 2062 }
2e471eb5 2063
7146af97 2064 p = XSYMBOL (val);
636b7260 2065 p->name = XSTRING (name);
47d5b31e 2066 p->obarray = Qnil;
7146af97 2067 p->plist = Qnil;
2e471eb5
GM
2068 p->value = Qunbound;
2069 p->function = Qunbound;
2070 p->next = 0;
2071 consing_since_gc += sizeof (struct Lisp_Symbol);
2072 symbols_consed++;
7146af97
JB
2073 return val;
2074}
2075
3f25e183 2076
2e471eb5
GM
2077\f
2078/***********************************************************************
34400008 2079 Marker (Misc) Allocation
2e471eb5 2080 ***********************************************************************/
3f25e183 2081
2e471eb5
GM
2082/* Allocation of markers and other objects that share that structure.
2083 Works like allocation of conses. */
c0696668 2084
2e471eb5
GM
2085#define MARKER_BLOCK_SIZE \
2086 ((1020 - sizeof (struct marker_block *)) / sizeof (union Lisp_Misc))
2087
2088struct marker_block
c0696668 2089{
2e471eb5
GM
2090 struct marker_block *next;
2091 union Lisp_Misc markers[MARKER_BLOCK_SIZE];
2092};
c0696668 2093
2e471eb5
GM
2094struct marker_block *marker_block;
2095int marker_block_index;
c0696668 2096
2e471eb5 2097union Lisp_Misc *marker_free_list;
c0696668 2098
2e471eb5 2099/* Total number of marker blocks now in use. */
3f25e183 2100
2e471eb5
GM
2101int n_marker_blocks;
2102
2103void
2104init_marker ()
3f25e183 2105{
34400008
GM
2106 marker_block = (struct marker_block *) lisp_malloc (sizeof *marker_block,
2107 MEM_TYPE_MISC);
2e471eb5
GM
2108 marker_block->next = 0;
2109 bzero ((char *) marker_block->markers, sizeof marker_block->markers);
2110 marker_block_index = 0;
2111 marker_free_list = 0;
2112 n_marker_blocks = 1;
3f25e183
RS
2113}
2114
2e471eb5
GM
2115/* Return a newly allocated Lisp_Misc object, with no substructure. */
2116
3f25e183 2117Lisp_Object
2e471eb5 2118allocate_misc ()
7146af97 2119{
2e471eb5 2120 Lisp_Object val;
7146af97 2121
2e471eb5 2122 if (marker_free_list)
7146af97 2123 {
2e471eb5
GM
2124 XSETMISC (val, marker_free_list);
2125 marker_free_list = marker_free_list->u_free.chain;
7146af97
JB
2126 }
2127 else
7146af97 2128 {
2e471eb5
GM
2129 if (marker_block_index == MARKER_BLOCK_SIZE)
2130 {
2131 struct marker_block *new;
34400008
GM
2132 new = (struct marker_block *) lisp_malloc (sizeof *new,
2133 MEM_TYPE_MISC);
2e471eb5
GM
2134 VALIDATE_LISP_STORAGE (new, sizeof *new);
2135 new->next = marker_block;
2136 marker_block = new;
2137 marker_block_index = 0;
2138 n_marker_blocks++;
2139 }
2140 XSETMISC (val, &marker_block->markers[marker_block_index++]);
7146af97 2141 }
2e471eb5
GM
2142
2143 consing_since_gc += sizeof (union Lisp_Misc);
2144 misc_objects_consed++;
2145 return val;
2146}
2147
2148DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
2149 "Return a newly allocated marker which does not point at any place.")
2150 ()
2151{
2152 register Lisp_Object val;
2153 register struct Lisp_Marker *p;
7146af97 2154
2e471eb5
GM
2155 val = allocate_misc ();
2156 XMISCTYPE (val) = Lisp_Misc_Marker;
2157 p = XMARKER (val);
2158 p->buffer = 0;
2159 p->bytepos = 0;
2160 p->charpos = 0;
2161 p->chain = Qnil;
2162 p->insertion_type = 0;
7146af97
JB
2163 return val;
2164}
2e471eb5
GM
2165
2166/* Put MARKER back on the free list after using it temporarily. */
2167
2168void
2169free_marker (marker)
2170 Lisp_Object marker;
2171{
2172 unchain_marker (marker);
2173
2174 XMISC (marker)->u_marker.type = Lisp_Misc_Free;
2175 XMISC (marker)->u_free.chain = marker_free_list;
2176 marker_free_list = XMISC (marker);
2177
2178 total_free_markers++;
2179}
2180
c0696668 2181\f
7146af97 2182/* Return a newly created vector or string with specified arguments as
736471d1
RS
2183 elements. If all the arguments are characters that can fit
2184 in a string of events, make a string; otherwise, make a vector.
2185
2186 Any number of arguments, even zero arguments, are allowed. */
7146af97
JB
2187
2188Lisp_Object
736471d1 2189make_event_array (nargs, args)
7146af97
JB
2190 register int nargs;
2191 Lisp_Object *args;
2192{
2193 int i;
2194
2195 for (i = 0; i < nargs; i++)
736471d1 2196 /* The things that fit in a string
c9ca4659
RS
2197 are characters that are in 0...127,
2198 after discarding the meta bit and all the bits above it. */
e687453f 2199 if (!INTEGERP (args[i])
c9ca4659 2200 || (XUINT (args[i]) & ~(-CHAR_META)) >= 0200)
7146af97
JB
2201 return Fvector (nargs, args);
2202
2203 /* Since the loop exited, we know that all the things in it are
2204 characters, so we can make a string. */
2205 {
c13ccad2 2206 Lisp_Object result;
7146af97 2207
50aee051 2208 result = Fmake_string (make_number (nargs), make_number (0));
7146af97 2209 for (i = 0; i < nargs; i++)
736471d1
RS
2210 {
2211 XSTRING (result)->data[i] = XINT (args[i]);
2212 /* Move the meta bit to the right place for a string char. */
2213 if (XINT (args[i]) & CHAR_META)
2214 XSTRING (result)->data[i] |= 0x80;
2215 }
7146af97
JB
2216
2217 return result;
2218 }
2219}
2e471eb5
GM
2220
2221
7146af97 2222\f
34400008
GM
2223/************************************************************************
2224 C Stack Marking
2225 ************************************************************************/
2226
2227#if GC_MARK_STACK
2228
2229
2230/* Base address of stack. Set in main. */
2231
2232Lisp_Object *stack_base;
2233
2234/* A node in the red-black tree describing allocated memory containing
2235 Lisp data. Each such block is recorded with its start and end
2236 address when it is allocated, and removed from the tree when it
2237 is freed.
2238
2239 A red-black tree is a balanced binary tree with the following
2240 properties:
2241
2242 1. Every node is either red or black.
2243 2. Every leaf is black.
2244 3. If a node is red, then both of its children are black.
2245 4. Every simple path from a node to a descendant leaf contains
2246 the same number of black nodes.
2247 5. The root is always black.
2248
2249 When nodes are inserted into the tree, or deleted from the tree,
2250 the tree is "fixed" so that these properties are always true.
2251
2252 A red-black tree with N internal nodes has height at most 2
2253 log(N+1). Searches, insertions and deletions are done in O(log N).
2254 Please see a text book about data structures for a detailed
2255 description of red-black trees. Any book worth its salt should
2256 describe them. */
2257
2258struct mem_node
2259{
2260 struct mem_node *left, *right, *parent;
2261
2262 /* Start and end of allocated region. */
2263 void *start, *end;
2264
2265 /* Node color. */
2266 enum {MEM_BLACK, MEM_RED} color;
2267
2268 /* Memory type. */
2269 enum mem_type type;
2270};
2271
2272/* Root of the tree describing allocated Lisp memory. */
2273
2274static struct mem_node *mem_root;
2275
2276/* Sentinel node of the tree. */
2277
2278static struct mem_node mem_z;
2279#define MEM_NIL &mem_z
2280
2281
2282/* Initialize this part of alloc.c. */
2283
2284static void
2285mem_init ()
2286{
2287 mem_z.left = mem_z.right = MEM_NIL;
2288 mem_z.parent = NULL;
2289 mem_z.color = MEM_BLACK;
2290 mem_z.start = mem_z.end = NULL;
2291 mem_root = MEM_NIL;
2292}
2293
2294
2295/* Value is a pointer to the mem_node containing START. Value is
2296 MEM_NIL if there is no node in the tree containing START. */
2297
2298static INLINE struct mem_node *
2299mem_find (start)
2300 void *start;
2301{
2302 struct mem_node *p;
2303
2304 /* Make the search always successful to speed up the loop below. */
2305 mem_z.start = start;
2306 mem_z.end = (char *) start + 1;
2307
2308 p = mem_root;
2309 while (start < p->start || start >= p->end)
2310 p = start < p->start ? p->left : p->right;
2311 return p;
2312}
2313
2314
2315/* Insert a new node into the tree for a block of memory with start
2316 address START, end address END, and type TYPE. Value is a
2317 pointer to the node that was inserted. */
2318
2319static struct mem_node *
2320mem_insert (start, end, type)
2321 void *start, *end;
2322 enum mem_type type;
2323{
2324 struct mem_node *c, *parent, *x;
2325
2326 /* See where in the tree a node for START belongs. In this
2327 particular application, it shouldn't happen that a node is already
2328 present. For debugging purposes, let's check that. */
2329 c = mem_root;
2330 parent = NULL;
2331
2332#if GC_MARK_STACK != GC_MAKE_GCPROS_NOOPS
2333
2334 while (c != MEM_NIL)
2335 {
2336 if (start >= c->start && start < c->end)
2337 abort ();
2338 parent = c;
2339 c = start < c->start ? c->left : c->right;
2340 }
2341
2342#else /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
2343
2344 while (c != MEM_NIL)
2345 {
2346 parent = c;
2347 c = start < c->start ? c->left : c->right;
2348 }
2349
2350#endif /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
2351
2352 /* Create a new node. */
2353 x = (struct mem_node *) xmalloc (sizeof *x);
2354 x->start = start;
2355 x->end = end;
2356 x->type = type;
2357 x->parent = parent;
2358 x->left = x->right = MEM_NIL;
2359 x->color = MEM_RED;
2360
2361 /* Insert it as child of PARENT or install it as root. */
2362 if (parent)
2363 {
2364 if (start < parent->start)
2365 parent->left = x;
2366 else
2367 parent->right = x;
2368 }
2369 else
2370 mem_root = x;
2371
2372 /* Re-establish red-black tree properties. */
2373 mem_insert_fixup (x);
2374 return x;
2375}
2376
2377
2378/* Re-establish the red-black properties of the tree, and thereby
2379 balance the tree, after node X has been inserted; X is always red. */
2380
2381static void
2382mem_insert_fixup (x)
2383 struct mem_node *x;
2384{
2385 while (x != mem_root && x->parent->color == MEM_RED)
2386 {
2387 /* X is red and its parent is red. This is a violation of
2388 red-black tree property #3. */
2389
2390 if (x->parent == x->parent->parent->left)
2391 {
2392 /* We're on the left side of our grandparent, and Y is our
2393 "uncle". */
2394 struct mem_node *y = x->parent->parent->right;
2395
2396 if (y->color == MEM_RED)
2397 {
2398 /* Uncle and parent are red but should be black because
2399 X is red. Change the colors accordingly and proceed
2400 with the grandparent. */
2401 x->parent->color = MEM_BLACK;
2402 y->color = MEM_BLACK;
2403 x->parent->parent->color = MEM_RED;
2404 x = x->parent->parent;
2405 }
2406 else
2407 {
2408 /* Parent and uncle have different colors; parent is
2409 red, uncle is black. */
2410 if (x == x->parent->right)
2411 {
2412 x = x->parent;
2413 mem_rotate_left (x);
2414 }
2415
2416 x->parent->color = MEM_BLACK;
2417 x->parent->parent->color = MEM_RED;
2418 mem_rotate_right (x->parent->parent);
2419 }
2420 }
2421 else
2422 {
2423 /* This is the symmetrical case of above. */
2424 struct mem_node *y = x->parent->parent->left;
2425
2426 if (y->color == MEM_RED)
2427 {
2428 x->parent->color = MEM_BLACK;
2429 y->color = MEM_BLACK;
2430 x->parent->parent->color = MEM_RED;
2431 x = x->parent->parent;
2432 }
2433 else
2434 {
2435 if (x == x->parent->left)
2436 {
2437 x = x->parent;
2438 mem_rotate_right (x);
2439 }
2440
2441 x->parent->color = MEM_BLACK;
2442 x->parent->parent->color = MEM_RED;
2443 mem_rotate_left (x->parent->parent);
2444 }
2445 }
2446 }
2447
2448 /* The root may have been changed to red due to the algorithm. Set
2449 it to black so that property #5 is satisfied. */
2450 mem_root->color = MEM_BLACK;
2451}
2452
2453
2454/* (x) (y)
2455 / \ / \
2456 a (y) ===> (x) c
2457 / \ / \
2458 b c a b */
2459
2460static void
2461mem_rotate_left (x)
2462 struct mem_node *x;
2463{
2464 struct mem_node *y;
2465
2466 /* Turn y's left sub-tree into x's right sub-tree. */
2467 y = x->right;
2468 x->right = y->left;
2469 if (y->left != MEM_NIL)
2470 y->left->parent = x;
2471
2472 /* Y's parent was x's parent. */
2473 if (y != MEM_NIL)
2474 y->parent = x->parent;
2475
2476 /* Get the parent to point to y instead of x. */
2477 if (x->parent)
2478 {
2479 if (x == x->parent->left)
2480 x->parent->left = y;
2481 else
2482 x->parent->right = y;
2483 }
2484 else
2485 mem_root = y;
2486
2487 /* Put x on y's left. */
2488 y->left = x;
2489 if (x != MEM_NIL)
2490 x->parent = y;
2491}
2492
2493
2494/* (x) (Y)
2495 / \ / \
2496 (y) c ===> a (x)
2497 / \ / \
2498 a b b c */
2499
2500static void
2501mem_rotate_right (x)
2502 struct mem_node *x;
2503{
2504 struct mem_node *y = x->left;
2505
2506 x->left = y->right;
2507 if (y->right != MEM_NIL)
2508 y->right->parent = x;
2509
2510 if (y != MEM_NIL)
2511 y->parent = x->parent;
2512 if (x->parent)
2513 {
2514 if (x == x->parent->right)
2515 x->parent->right = y;
2516 else
2517 x->parent->left = y;
2518 }
2519 else
2520 mem_root = y;
2521
2522 y->right = x;
2523 if (x != MEM_NIL)
2524 x->parent = y;
2525}
2526
2527
2528/* Delete node Z from the tree. If Z is null or MEM_NIL, do nothing. */
2529
2530static void
2531mem_delete (z)
2532 struct mem_node *z;
2533{
2534 struct mem_node *x, *y;
2535
2536 if (!z || z == MEM_NIL)
2537 return;
2538
2539 if (z->left == MEM_NIL || z->right == MEM_NIL)
2540 y = z;
2541 else
2542 {
2543 y = z->right;
2544 while (y->left != MEM_NIL)
2545 y = y->left;
2546 }
2547
2548 if (y->left != MEM_NIL)
2549 x = y->left;
2550 else
2551 x = y->right;
2552
2553 x->parent = y->parent;
2554 if (y->parent)
2555 {
2556 if (y == y->parent->left)
2557 y->parent->left = x;
2558 else
2559 y->parent->right = x;
2560 }
2561 else
2562 mem_root = x;
2563
2564 if (y != z)
2565 {
2566 z->start = y->start;
2567 z->end = y->end;
2568 z->type = y->type;
2569 }
2570
2571 if (y->color == MEM_BLACK)
2572 mem_delete_fixup (x);
2573 xfree (y);
2574}
2575
2576
2577/* Re-establish the red-black properties of the tree, after a
2578 deletion. */
2579
2580static void
2581mem_delete_fixup (x)
2582 struct mem_node *x;
2583{
2584 while (x != mem_root && x->color == MEM_BLACK)
2585 {
2586 if (x == x->parent->left)
2587 {
2588 struct mem_node *w = x->parent->right;
2589
2590 if (w->color == MEM_RED)
2591 {
2592 w->color = MEM_BLACK;
2593 x->parent->color = MEM_RED;
2594 mem_rotate_left (x->parent);
2595 w = x->parent->right;
2596 }
2597
2598 if (w->left->color == MEM_BLACK && w->right->color == MEM_BLACK)
2599 {
2600 w->color = MEM_RED;
2601 x = x->parent;
2602 }
2603 else
2604 {
2605 if (w->right->color == MEM_BLACK)
2606 {
2607 w->left->color = MEM_BLACK;
2608 w->color = MEM_RED;
2609 mem_rotate_right (w);
2610 w = x->parent->right;
2611 }
2612 w->color = x->parent->color;
2613 x->parent->color = MEM_BLACK;
2614 w->right->color = MEM_BLACK;
2615 mem_rotate_left (x->parent);
2616 x = mem_root;
2617 }
2618 }
2619 else
2620 {
2621 struct mem_node *w = x->parent->left;
2622
2623 if (w->color == MEM_RED)
2624 {
2625 w->color = MEM_BLACK;
2626 x->parent->color = MEM_RED;
2627 mem_rotate_right (x->parent);
2628 w = x->parent->left;
2629 }
2630
2631 if (w->right->color == MEM_BLACK && w->left->color == MEM_BLACK)
2632 {
2633 w->color = MEM_RED;
2634 x = x->parent;
2635 }
2636 else
2637 {
2638 if (w->left->color == MEM_BLACK)
2639 {
2640 w->right->color = MEM_BLACK;
2641 w->color = MEM_RED;
2642 mem_rotate_left (w);
2643 w = x->parent->left;
2644 }
2645
2646 w->color = x->parent->color;
2647 x->parent->color = MEM_BLACK;
2648 w->left->color = MEM_BLACK;
2649 mem_rotate_right (x->parent);
2650 x = mem_root;
2651 }
2652 }
2653 }
2654
2655 x->color = MEM_BLACK;
2656}
2657
2658
2659/* Value is non-zero if P is a pointer to a live Lisp string on
2660 the heap. M is a pointer to the mem_block for P. */
2661
2662static INLINE int
2663live_string_p (m, p)
2664 struct mem_node *m;
2665 void *p;
2666{
2667 if (m->type == MEM_TYPE_STRING)
2668 {
2669 struct string_block *b = (struct string_block *) m->start;
2670 int offset = (char *) p - (char *) &b->strings[0];
2671
2672 /* P must point to the start of a Lisp_String structure, and it
2673 must not be on the free-list. */
2674 return (offset % sizeof b->strings[0] == 0
2675 && ((struct Lisp_String *) p)->data != NULL);
2676 }
2677 else
2678 return 0;
2679}
2680
2681
2682/* Value is non-zero if P is a pointer to a live Lisp cons on
2683 the heap. M is a pointer to the mem_block for P. */
2684
2685static INLINE int
2686live_cons_p (m, p)
2687 struct mem_node *m;
2688 void *p;
2689{
2690 if (m->type == MEM_TYPE_CONS)
2691 {
2692 struct cons_block *b = (struct cons_block *) m->start;
2693 int offset = (char *) p - (char *) &b->conses[0];
2694
2695 /* P must point to the start of a Lisp_Cons, not be
2696 one of the unused cells in the current cons block,
2697 and not be on the free-list. */
2698 return (offset % sizeof b->conses[0] == 0
2699 && (b != cons_block
2700 || offset / sizeof b->conses[0] < cons_block_index)
2701 && !EQ (((struct Lisp_Cons *) p)->car, Vdead));
2702 }
2703 else
2704 return 0;
2705}
2706
2707
2708/* Value is non-zero if P is a pointer to a live Lisp symbol on
2709 the heap. M is a pointer to the mem_block for P. */
2710
2711static INLINE int
2712live_symbol_p (m, p)
2713 struct mem_node *m;
2714 void *p;
2715{
2716 if (m->type == MEM_TYPE_SYMBOL)
2717 {
2718 struct symbol_block *b = (struct symbol_block *) m->start;
2719 int offset = (char *) p - (char *) &b->symbols[0];
2720
2721 /* P must point to the start of a Lisp_Symbol, not be
2722 one of the unused cells in the current symbol block,
2723 and not be on the free-list. */
2724 return (offset % sizeof b->symbols[0] == 0
2725 && (b != symbol_block
2726 || offset / sizeof b->symbols[0] < symbol_block_index)
2727 && !EQ (((struct Lisp_Symbol *) p)->function, Vdead));
2728 }
2729 else
2730 return 0;
2731}
2732
2733
2734/* Value is non-zero if P is a pointer to a live Lisp float on
2735 the heap. M is a pointer to the mem_block for P. */
2736
2737static INLINE int
2738live_float_p (m, p)
2739 struct mem_node *m;
2740 void *p;
2741{
2742 if (m->type == MEM_TYPE_FLOAT)
2743 {
2744 struct float_block *b = (struct float_block *) m->start;
2745 int offset = (char *) p - (char *) &b->floats[0];
2746
2747 /* P must point to the start of a Lisp_Float, not be
2748 one of the unused cells in the current float block,
2749 and not be on the free-list. */
2750 return (offset % sizeof b->floats[0] == 0
2751 && (b != float_block
2752 || offset / sizeof b->floats[0] < float_block_index)
2753 && !EQ (((struct Lisp_Float *) p)->type, Vdead));
2754 }
2755 else
2756 return 0;
2757}
2758
2759
2760/* Value is non-zero if P is a pointer to a live Lisp Misc on
2761 the heap. M is a pointer to the mem_block for P. */
2762
2763static INLINE int
2764live_misc_p (m, p)
2765 struct mem_node *m;
2766 void *p;
2767{
2768 if (m->type == MEM_TYPE_MISC)
2769 {
2770 struct marker_block *b = (struct marker_block *) m->start;
2771 int offset = (char *) p - (char *) &b->markers[0];
2772
2773 /* P must point to the start of a Lisp_Misc, not be
2774 one of the unused cells in the current misc block,
2775 and not be on the free-list. */
2776 return (offset % sizeof b->markers[0] == 0
2777 && (b != marker_block
2778 || offset / sizeof b->markers[0] < marker_block_index)
2779 && ((union Lisp_Misc *) p)->u_marker.type != Lisp_Misc_Free);
2780 }
2781 else
2782 return 0;
2783}
2784
2785
2786/* Value is non-zero if P is a pointer to a live vector-like object.
2787 M is a pointer to the mem_block for P. */
2788
2789static INLINE int
2790live_vector_p (m, p)
2791 struct mem_node *m;
2792 void *p;
2793{
2794 return m->type == MEM_TYPE_VECTOR && p == m->start;
2795}
2796
2797
2798/* Value is non-zero of P is a pointer to a live buffer. M is a
2799 pointer to the mem_block for P. */
2800
2801static INLINE int
2802live_buffer_p (m, p)
2803 struct mem_node *m;
2804 void *p;
2805{
2806 /* P must point to the start of the block, and the buffer
2807 must not have been killed. */
2808 return (m->type == MEM_TYPE_BUFFER
2809 && p == m->start
2810 && !NILP (((struct buffer *) p)->name));
2811}
2812
2813
2814#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
2815
2816/* Array of objects that are kept alive because the C stack contains
2817 a pattern that looks like a reference to them . */
2818
2819#define MAX_ZOMBIES 10
2820static Lisp_Object zombies[MAX_ZOMBIES];
2821
2822/* Number of zombie objects. */
2823
2824static int nzombies;
2825
2826/* Number of garbage collections. */
2827
2828static int ngcs;
2829
2830/* Average percentage of zombies per collection. */
2831
2832static double avg_zombies;
2833
2834/* Max. number of live and zombie objects. */
2835
2836static int max_live, max_zombies;
2837
2838/* Average number of live objects per GC. */
2839
2840static double avg_live;
2841
2842DEFUN ("gc-status", Fgc_status, Sgc_status, 0, 0, "",
2843 "Show information about live and zombie objects.")
2844 ()
2845{
2846 Lisp_Object args[7];
2847 args[0] = build_string ("%d GCs, avg live/zombies = %.2f/%.2f (%f%%), max %d/%d");
2848 args[1] = make_number (ngcs);
2849 args[2] = make_float (avg_live);
2850 args[3] = make_float (avg_zombies);
2851 args[4] = make_float (avg_zombies / avg_live / 100);
2852 args[5] = make_number (max_live);
2853 args[6] = make_number (max_zombies);
2854 return Fmessage (7, args);
2855}
2856
2857#endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
2858
2859
182ff242
GM
2860/* Mark OBJ if we can prove it's a Lisp_Object. */
2861
2862static INLINE void
2863mark_maybe_object (obj)
2864 Lisp_Object obj;
2865{
2866 void *po = (void *) XPNTR (obj);
2867 struct mem_node *m = mem_find (po);
2868
2869 if (m != MEM_NIL)
2870 {
2871 int mark_p = 0;
2872
2873 switch (XGCTYPE (obj))
2874 {
2875 case Lisp_String:
2876 mark_p = (live_string_p (m, po)
2877 && !STRING_MARKED_P ((struct Lisp_String *) po));
2878 break;
2879
2880 case Lisp_Cons:
2881 mark_p = (live_cons_p (m, po)
2882 && !XMARKBIT (XCONS (obj)->car));
2883 break;
2884
2885 case Lisp_Symbol:
2886 mark_p = (live_symbol_p (m, po)
2887 && !XMARKBIT (XSYMBOL (obj)->plist));
2888 break;
2889
2890 case Lisp_Float:
2891 mark_p = (live_float_p (m, po)
2892 && !XMARKBIT (XFLOAT (obj)->type));
2893 break;
2894
2895 case Lisp_Vectorlike:
2896 /* Note: can't check GC_BUFFERP before we know it's a
2897 buffer because checking that dereferences the pointer
2898 PO which might point anywhere. */
2899 if (live_vector_p (m, po))
2900 mark_p = (!GC_SUBRP (obj)
2901 && !(XVECTOR (obj)->size & ARRAY_MARK_FLAG));
2902 else if (live_buffer_p (m, po))
2903 mark_p = GC_BUFFERP (obj) && !XMARKBIT (XBUFFER (obj)->name);
2904 break;
2905
2906 case Lisp_Misc:
2907 if (live_misc_p (m, po))
2908 {
2909 switch (XMISCTYPE (obj))
2910 {
2911 case Lisp_Misc_Marker:
2912 mark_p = !XMARKBIT (XMARKER (obj)->chain);
2913 break;
2914
2915 case Lisp_Misc_Buffer_Local_Value:
2916 case Lisp_Misc_Some_Buffer_Local_Value:
2917 mark_p = !XMARKBIT (XBUFFER_LOCAL_VALUE (obj)->realvalue);
2918 break;
2919
2920 case Lisp_Misc_Overlay:
2921 mark_p = !XMARKBIT (XOVERLAY (obj)->plist);
2922 break;
2923 }
2924 }
2925 break;
2926 }
2927
2928 if (mark_p)
2929 {
2930#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
2931 if (nzombies < MAX_ZOMBIES)
2932 zombies[nzombies] = *p;
2933 ++nzombies;
2934#endif
2935 mark_object (&obj);
2936 }
2937 }
2938}
2939
34400008
GM
2940/* Mark Lisp objects in the address range START..END. */
2941
2942static void
2943mark_memory (start, end)
2944 void *start, *end;
2945{
2946 Lisp_Object *p;
2947
2948#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
2949 nzombies = 0;
2950#endif
2951
2952 /* Make START the pointer to the start of the memory region,
2953 if it isn't already. */
2954 if (end < start)
2955 {
2956 void *tem = start;
2957 start = end;
2958 end = tem;
2959 }
182ff242 2960
34400008 2961 for (p = (Lisp_Object *) start; (void *) p < end; ++p)
182ff242
GM
2962 mark_maybe_object (*p);
2963}
2964
2965
2966#if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
2967
2968static int setjmp_tested_p, longjmps_done;
2969
2970#define SETJMP_WILL_LIKELY_WORK "\
2971\n\
2972Emacs garbage collector has been changed to use conservative stack\n\
2973marking. Emacs has determined that the method it uses to do the\n\
2974marking will likely work on your system, but this isn't sure.\n\
2975\n\
2976If you are a system-programmer, or can get the help of a local wizard\n\
2977who is, please take a look at the function mark_stack in alloc.c, and\n\
2978verify that the methods used are appropriate for your system.\n\
2979\n\
2980Please mail the result to <gerd@gnu.org>.\n\
2981"
2982
2983#define SETJMP_WILL_NOT_WORK "\
2984\n\
2985Emacs garbage collector has been changed to use conservative stack\n\
2986marking. Emacs has determined that the default method it uses to do the\n\
2987marking will not work on your system. We will need a system-dependent\n\
2988solution for your system.\n\
2989\n\
2990Please take a look at the function mark_stack in alloc.c, and\n\
2991try to find a way to make it work on your system.\n\
2992Please mail the result to <gerd@gnu.org>.\n\
2993"
2994
2995
2996/* Perform a quick check if it looks like setjmp saves registers in a
2997 jmp_buf. Print a message to stderr saying so. When this test
2998 succeeds, this is _not_ a proof that setjmp is sufficient for
2999 conservative stack marking. Only the sources or a disassembly
3000 can prove that. */
3001
3002static void
3003test_setjmp ()
3004{
3005 char buf[10];
3006 register int x;
3007 jmp_buf jbuf;
3008 int result = 0;
3009
3010 /* Arrange for X to be put in a register. */
3011 sprintf (buf, "1");
3012 x = strlen (buf);
3013 x = 2 * x - 1;
3014
3015 setjmp (jbuf);
3016 if (longjmps_done == 1)
34400008 3017 {
182ff242 3018 /* Came here after the longjmp at the end of the function.
34400008 3019
182ff242
GM
3020 If x == 1, the longjmp has restored the register to its
3021 value before the setjmp, and we can hope that setjmp
3022 saves all such registers in the jmp_buf, although that
3023 isn't sure.
34400008 3024
182ff242
GM
3025 For other values of X, either something really strange is
3026 taking place, or the setjmp just didn't save the register. */
3027
3028 if (x == 1)
3029 fprintf (stderr, SETJMP_WILL_LIKELY_WORK);
3030 else
3031 {
3032 fprintf (stderr, SETJMP_WILL_NOT_WORK);
3033 exit (1);
34400008
GM
3034 }
3035 }
182ff242
GM
3036
3037 ++longjmps_done;
3038 x = 2;
3039 if (longjmps_done == 1)
3040 longjmp (jbuf, 1);
34400008
GM
3041}
3042
182ff242
GM
3043#endif /* not GC_SAVE_REGISTERS_ON_STACK && not GC_SETJMP_WORKS */
3044
34400008
GM
3045
3046#if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
3047
3048/* Abort if anything GCPRO'd doesn't survive the GC. */
3049
3050static void
3051check_gcpros ()
3052{
3053 struct gcpro *p;
3054 int i;
3055
3056 for (p = gcprolist; p; p = p->next)
3057 for (i = 0; i < p->nvars; ++i)
3058 if (!survives_gc_p (p->var[i]))
3059 abort ();
3060}
3061
3062#elif GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3063
3064static void
3065dump_zombies ()
3066{
3067 int i;
3068
3069 fprintf (stderr, "\nZombies kept alive = %d:\n", nzombies);
3070 for (i = 0; i < min (MAX_ZOMBIES, nzombies); ++i)
3071 {
3072 fprintf (stderr, " %d = ", i);
3073 debug_print (zombies[i]);
3074 }
3075}
3076
3077#endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
3078
3079
182ff242
GM
3080/* Mark live Lisp objects on the C stack.
3081
3082 There are several system-dependent problems to consider when
3083 porting this to new architectures:
3084
3085 Processor Registers
3086
3087 We have to mark Lisp objects in CPU registers that can hold local
3088 variables or are used to pass parameters.
3089
3090 If GC_SAVE_REGISTERS_ON_STACK is defined, it should expand to
3091 something that either saves relevant registers on the stack, or
3092 calls mark_maybe_object passing it each register's contents.
3093
3094 If GC_SAVE_REGISTERS_ON_STACK is not defined, the current
3095 implementation assumes that calling setjmp saves registers we need
3096 to see in a jmp_buf which itself lies on the stack. This doesn't
3097 have to be true! It must be verified for each system, possibly
3098 by taking a look at the source code of setjmp.
3099
3100 Stack Layout
3101
3102 Architectures differ in the way their processor stack is organized.
3103 For example, the stack might look like this
3104
3105 +----------------+
3106 | Lisp_Object | size = 4
3107 +----------------+
3108 | something else | size = 2
3109 +----------------+
3110 | Lisp_Object | size = 4
3111 +----------------+
3112 | ... |
3113
3114 In such a case, not every Lisp_Object will be aligned equally. To
3115 find all Lisp_Object on the stack it won't be sufficient to walk
3116 the stack in steps of 4 bytes. Instead, two passes will be
3117 necessary, one starting at the start of the stack, and a second
3118 pass starting at the start of the stack + 2. Likewise, if the
3119 minimal alignment of Lisp_Objects on the stack is 1, four passes
3120 would be necessary, each one starting with one byte more offset
3121 from the stack start.
3122
3123 The current code assumes by default that Lisp_Objects are aligned
3124 equally on the stack. */
34400008
GM
3125
3126static void
3127mark_stack ()
3128{
3129 jmp_buf j;
3130 int stack_grows_down_p = (char *) &j > (char *) stack_base;
3131 void *end;
3132
3133 /* This trick flushes the register windows so that all the state of
3134 the process is contained in the stack. */
3135#ifdef sparc
3136 asm ("ta 3");
3137#endif
3138
3139 /* Save registers that we need to see on the stack. We need to see
3140 registers used to hold register variables and registers used to
3141 pass parameters. */
3142#ifdef GC_SAVE_REGISTERS_ON_STACK
3143 GC_SAVE_REGISTERS_ON_STACK (end);
182ff242
GM
3144#else /* not GC_SAVE_REGISTERS_ON_STACK */
3145
3146#ifndef GC_SETJMP_WORKS /* If it hasn't been checked yet that
3147 setjmp will definitely work, test it
3148 and print a message with the result
3149 of the test. */
3150 if (!setjmp_tested_p)
3151 {
3152 setjmp_tested_p = 1;
3153 test_setjmp ();
3154 }
3155#endif /* GC_SETJMP_WORKS */
3156
34400008
GM
3157 setjmp (j);
3158 end = stack_grows_down_p ? (char *) &j + sizeof j : (char *) &j;
182ff242 3159#endif /* not GC_SAVE_REGISTERS_ON_STACK */
34400008
GM
3160
3161 /* This assumes that the stack is a contiguous region in memory. If
182ff242
GM
3162 that's not the case, something has to be done here to iterate
3163 over the stack segments. */
3164#if GC_LISP_OBJECT_ALIGNMENT == 1
3165 mark_memory (stack_base, end);
3166 mark_memory ((char *) stack_base + 1, end);
3167 mark_memory ((char *) stack_base + 2, end);
3168 mark_memory ((char *) stack_base + 3, end);
3169#elif GC_LISP_OBJECT_ALIGNMENT == 2
3170 mark_memory (stack_base, end);
3171 mark_memory ((char *) stack_base + 2, end);
3172#else
34400008 3173 mark_memory (stack_base, end);
182ff242 3174#endif
34400008
GM
3175
3176#if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
3177 check_gcpros ();
3178#endif
3179}
3180
3181
3182#endif /* GC_MARK_STACK != 0 */
3183
3184
3185\f
2e471eb5
GM
3186/***********************************************************************
3187 Pure Storage Management
3188 ***********************************************************************/
3189
3190/* Return a string allocated in pure space. DATA is a buffer holding
3191 NCHARS characters, and NBYTES bytes of string data. MULTIBYTE
3192 non-zero means make the result string multibyte.
1a4f1e2c 3193
2e471eb5
GM
3194 Must get an error if pure storage is full, since if it cannot hold
3195 a large string it may be able to hold conses that point to that
3196 string; then the string is not protected from gc. */
7146af97
JB
3197
3198Lisp_Object
2e471eb5 3199make_pure_string (data, nchars, nbytes, multibyte)
7146af97 3200 char *data;
2e471eb5 3201 int nchars, nbytes;
c0696668 3202 int multibyte;
7146af97 3203{
2e471eb5
GM
3204 Lisp_Object string;
3205 struct Lisp_String *s;
3206 int string_size, data_size;
c0696668 3207
2e471eb5 3208#define PAD(SZ) (((SZ) + sizeof (EMACS_INT) - 1) & ~(sizeof (EMACS_INT) - 1))
7146af97 3209
2e471eb5
GM
3210 string_size = PAD (sizeof (struct Lisp_String));
3211 data_size = PAD (nbytes + 1);
3212
3213#undef PAD
3214
3215 if (pureptr + string_size + data_size > PURESIZE)
7146af97 3216 error ("Pure Lisp storage exhausted");
2e471eb5
GM
3217
3218 s = (struct Lisp_String *) (PUREBEG + pureptr);
3219 pureptr += string_size;
3220 s->data = (unsigned char *) (PUREBEG + pureptr);
3221 pureptr += data_size;
3222
3223 s->size = nchars;
3224 s->size_byte = multibyte ? nbytes : -1;
3225 bcopy (data, s->data, nbytes);
3226 s->data[nbytes] = '\0';
3227 s->intervals = NULL_INTERVAL;
3228
3229 XSETSTRING (string, s);
3230 return string;
7146af97
JB
3231}
3232
2e471eb5 3233
34400008
GM
3234/* Return a cons allocated from pure space. Give it pure copies
3235 of CAR as car and CDR as cdr. */
3236
7146af97
JB
3237Lisp_Object
3238pure_cons (car, cdr)
3239 Lisp_Object car, cdr;
3240{
3241 register Lisp_Object new;
3242
3243 if (pureptr + sizeof (struct Lisp_Cons) > PURESIZE)
3244 error ("Pure Lisp storage exhausted");
45d12a89 3245 XSETCONS (new, PUREBEG + pureptr);
7146af97 3246 pureptr += sizeof (struct Lisp_Cons);
70949dac
KR
3247 XCAR (new) = Fpurecopy (car);
3248 XCDR (new) = Fpurecopy (cdr);
7146af97
JB
3249 return new;
3250}
3251
7146af97 3252
34400008
GM
3253/* Value is a float object with value NUM allocated from pure space. */
3254
7146af97
JB
3255Lisp_Object
3256make_pure_float (num)
3257 double num;
3258{
3259 register Lisp_Object new;
3260
6d19f28a
JB
3261 /* Make sure that PUREBEG + pureptr is aligned on at least a sizeof
3262 (double) boundary. Some architectures (like the sparc) require
3263 this, and I suspect that floats are rare enough that it's no
3264 tragedy for those that do. */
3265 {
3266 int alignment;
3267 char *p = PUREBEG + pureptr;
3268
fe90ad97
JB
3269#ifdef __GNUC__
3270#if __GNUC__ >= 2
6d19f28a 3271 alignment = __alignof (struct Lisp_Float);
fe90ad97 3272#else
6d19f28a 3273 alignment = sizeof (struct Lisp_Float);
fe90ad97
JB
3274#endif
3275#else
6d19f28a 3276 alignment = sizeof (struct Lisp_Float);
fe90ad97 3277#endif
6d19f28a
JB
3278 p = (char *) (((unsigned long) p + alignment - 1) & - alignment);
3279 pureptr = p - PUREBEG;
3280 }
1a4f1e2c 3281
7146af97
JB
3282 if (pureptr + sizeof (struct Lisp_Float) > PURESIZE)
3283 error ("Pure Lisp storage exhausted");
45d12a89 3284 XSETFLOAT (new, PUREBEG + pureptr);
7146af97 3285 pureptr += sizeof (struct Lisp_Float);
70949dac 3286 XFLOAT_DATA (new) = num;
67ba9986 3287 XSETFASTINT (XFLOAT (new)->type, 0); /* bug chasing -wsr */
7146af97
JB
3288 return new;
3289}
3290
34400008
GM
3291
3292/* Return a vector with room for LEN Lisp_Objects allocated from
3293 pure space. */
3294
7146af97
JB
3295Lisp_Object
3296make_pure_vector (len)
42607681 3297 EMACS_INT len;
7146af97
JB
3298{
3299 register Lisp_Object new;
34400008
GM
3300 register EMACS_INT size = (sizeof (struct Lisp_Vector)
3301 + (len - 1) * sizeof (Lisp_Object));
7146af97
JB
3302
3303 if (pureptr + size > PURESIZE)
3304 error ("Pure Lisp storage exhausted");
3305
45d12a89 3306 XSETVECTOR (new, PUREBEG + pureptr);
7146af97
JB
3307 pureptr += size;
3308 XVECTOR (new)->size = len;
3309 return new;
3310}
3311
34400008 3312
7146af97
JB
3313DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
3314 "Make a copy of OBJECT in pure storage.\n\
3315Recursively copies contents of vectors and cons cells.\n\
d71c0668 3316Does not copy symbols. Copies strings without text properties.")
7146af97
JB
3317 (obj)
3318 register Lisp_Object obj;
3319{
265a9e55 3320 if (NILP (Vpurify_flag))
7146af97
JB
3321 return obj;
3322
3323 if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
3324 && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
3325 return obj;
3326
d6dd74bb 3327 if (CONSP (obj))
70949dac 3328 return pure_cons (XCAR (obj), XCDR (obj));
d6dd74bb 3329 else if (FLOATP (obj))
70949dac 3330 return make_pure_float (XFLOAT_DATA (obj));
d6dd74bb 3331 else if (STRINGP (obj))
3f25e183 3332 return make_pure_string (XSTRING (obj)->data, XSTRING (obj)->size,
c0696668
RS
3333 STRING_BYTES (XSTRING (obj)),
3334 STRING_MULTIBYTE (obj));
d6dd74bb
KH
3335 else if (COMPILEDP (obj) || VECTORP (obj))
3336 {
3337 register struct Lisp_Vector *vec;
3338 register int i, size;
3339
3340 size = XVECTOR (obj)->size;
7d535c68
KH
3341 if (size & PSEUDOVECTOR_FLAG)
3342 size &= PSEUDOVECTOR_SIZE_MASK;
01a4d290 3343 vec = XVECTOR (make_pure_vector ((EMACS_INT) size));
d6dd74bb
KH
3344 for (i = 0; i < size; i++)
3345 vec->contents[i] = Fpurecopy (XVECTOR (obj)->contents[i]);
3346 if (COMPILEDP (obj))
3347 XSETCOMPILED (obj, vec);
3348 else
3349 XSETVECTOR (obj, vec);
7146af97
JB
3350 return obj;
3351 }
d6dd74bb
KH
3352 else if (MARKERP (obj))
3353 error ("Attempt to copy a marker to pure storage");
3354 else
3355 return obj;
7146af97 3356}
2e471eb5 3357
34400008 3358
7146af97 3359\f
34400008
GM
3360/***********************************************************************
3361 Protection from GC
3362 ***********************************************************************/
3363
7146af97
JB
3364/* Recording what needs to be marked for gc. */
3365
3366struct gcpro *gcprolist;
3367
34400008 3368/* Addresses of staticpro'd variables. */
7146af97 3369
34400008 3370#define NSTATICS 1024
7146af97
JB
3371Lisp_Object *staticvec[NSTATICS] = {0};
3372
34400008
GM
3373/* Index of next unused slot in staticvec. */
3374
7146af97
JB
3375int staticidx = 0;
3376
34400008 3377
2e471eb5
GM
3378/* Put an entry in staticvec, pointing at the variable with address
3379 VARADDRESS. */
7146af97
JB
3380
3381void
3382staticpro (varaddress)
3383 Lisp_Object *varaddress;
3384{
3385 staticvec[staticidx++] = varaddress;
3386 if (staticidx >= NSTATICS)
3387 abort ();
3388}
3389
3390struct catchtag
2e471eb5 3391{
7146af97
JB
3392 Lisp_Object tag;
3393 Lisp_Object val;
3394 struct catchtag *next;
2e471eb5 3395};
7146af97
JB
3396
3397struct backtrace
2e471eb5
GM
3398{
3399 struct backtrace *next;
3400 Lisp_Object *function;
3401 Lisp_Object *args; /* Points to vector of args. */
3402 int nargs; /* Length of vector. */
3403 /* If nargs is UNEVALLED, args points to slot holding list of
3404 unevalled args. */
3405 char evalargs;
3406};
3407
34400008 3408
7146af97 3409\f
34400008
GM
3410/***********************************************************************
3411 Protection from GC
3412 ***********************************************************************/
1a4f1e2c 3413
e8197642
RS
3414/* Temporarily prevent garbage collection. */
3415
3416int
3417inhibit_garbage_collection ()
3418{
3419 int count = specpdl_ptr - specpdl;
26b926e1 3420 Lisp_Object number;
68be917d 3421 int nbits = min (VALBITS, BITS_PER_INT);
e8197642 3422
b580578b 3423 XSETINT (number, ((EMACS_INT) 1 << (nbits - 1)) - 1);
26b926e1
RS
3424
3425 specbind (Qgc_cons_threshold, number);
e8197642
RS
3426
3427 return count;
3428}
3429
34400008 3430
7146af97
JB
3431DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
3432 "Reclaim storage for Lisp objects no longer needed.\n\
3433Returns info on amount of space in use:\n\
3434 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)\n\
3435 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS\n\
2e471eb5
GM
3436 (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS\n\
3437 (USED-STRINGS . FREE-STRINGS))\n\
7146af97
JB
3438Garbage collection happens automatically if you cons more than\n\
3439`gc-cons-threshold' bytes of Lisp data since previous garbage collection.")
3440 ()
3441{
3442 register struct gcpro *tail;
3443 register struct specbinding *bind;
3444 struct catchtag *catch;
3445 struct handler *handler;
3446 register struct backtrace *backlist;
7146af97
JB
3447 char stack_top_variable;
3448 register int i;
6efc7df7 3449 int message_p;
2e471eb5 3450 Lisp_Object total[7];
7146af97 3451
58595309
KH
3452 /* In case user calls debug_print during GC,
3453 don't let that cause a recursive GC. */
3454 consing_since_gc = 0;
3455
6efc7df7
GM
3456 /* Save what's currently displayed in the echo area. */
3457 message_p = push_message ();
41c28a37 3458
7146af97
JB
3459 /* Save a copy of the contents of the stack, for debugging. */
3460#if MAX_SAVE_STACK > 0
265a9e55 3461 if (NILP (Vpurify_flag))
7146af97
JB
3462 {
3463 i = &stack_top_variable - stack_bottom;
3464 if (i < 0) i = -i;
3465 if (i < MAX_SAVE_STACK)
3466 {
3467 if (stack_copy == 0)
9ac0d9e0 3468 stack_copy = (char *) xmalloc (stack_copy_size = i);
7146af97 3469 else if (stack_copy_size < i)
9ac0d9e0 3470 stack_copy = (char *) xrealloc (stack_copy, (stack_copy_size = i));
7146af97
JB
3471 if (stack_copy)
3472 {
42607681 3473 if ((EMACS_INT) (&stack_top_variable - stack_bottom) > 0)
7146af97
JB
3474 bcopy (stack_bottom, stack_copy, i);
3475 else
3476 bcopy (&stack_top_variable, stack_copy, i);
3477 }
3478 }
3479 }
3480#endif /* MAX_SAVE_STACK > 0 */
3481
299585ee 3482 if (garbage_collection_messages)
691c4285 3483 message1_nolog ("Garbage collecting...");
7146af97 3484
6e0fca1d
RS
3485 BLOCK_INPUT;
3486
eec7b73d
RS
3487 shrink_regexp_cache ();
3488
4929a878 3489 /* Don't keep undo information around forever. */
7146af97
JB
3490 {
3491 register struct buffer *nextb = all_buffers;
3492
3493 while (nextb)
3494 {
ffd56f97
JB
3495 /* If a buffer's undo list is Qt, that means that undo is
3496 turned off in that buffer. Calling truncate_undo_list on
3497 Qt tends to return NULL, which effectively turns undo back on.
3498 So don't call truncate_undo_list if undo_list is Qt. */
3499 if (! EQ (nextb->undo_list, Qt))
3500 nextb->undo_list
502b9b64
JB
3501 = truncate_undo_list (nextb->undo_list, undo_limit,
3502 undo_strong_limit);
7146af97
JB
3503 nextb = nextb->next;
3504 }
3505 }
3506
3507 gc_in_progress = 1;
3508
c23baf9f 3509 /* clear_marks (); */
7146af97 3510
7146af97
JB
3511 /* Mark all the special slots that serve as the roots of accessibility.
3512
3513 Usually the special slots to mark are contained in particular structures.
3514 Then we know no slot is marked twice because the structures don't overlap.
3515 In some cases, the structures point to the slots to be marked.
3516 For these, we use MARKBIT to avoid double marking of the slot. */
3517
3518 for (i = 0; i < staticidx; i++)
3519 mark_object (staticvec[i]);
34400008
GM
3520
3521#if (GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS \
3522 || GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS)
3523 mark_stack ();
3524#else
7146af97
JB
3525 for (tail = gcprolist; tail; tail = tail->next)
3526 for (i = 0; i < tail->nvars; i++)
3527 if (!XMARKBIT (tail->var[i]))
3528 {
3529 mark_object (&tail->var[i]);
3530 XMARK (tail->var[i]);
3531 }
34400008
GM
3532#endif
3533
630686c8 3534 mark_byte_stack ();
7146af97
JB
3535 for (bind = specpdl; bind != specpdl_ptr; bind++)
3536 {
3537 mark_object (&bind->symbol);
3538 mark_object (&bind->old_value);
3539 }
3540 for (catch = catchlist; catch; catch = catch->next)
3541 {
3542 mark_object (&catch->tag);
3543 mark_object (&catch->val);
3544 }
3545 for (handler = handlerlist; handler; handler = handler->next)
3546 {
3547 mark_object (&handler->handler);
3548 mark_object (&handler->var);
3549 }
3550 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3551 {
3552 if (!XMARKBIT (*backlist->function))
3553 {
3554 mark_object (backlist->function);
3555 XMARK (*backlist->function);
3556 }
3557 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
3558 i = 0;
3559 else
3560 i = backlist->nargs - 1;
3561 for (; i >= 0; i--)
3562 if (!XMARKBIT (backlist->args[i]))
3563 {
3564 mark_object (&backlist->args[i]);
3565 XMARK (backlist->args[i]);
3566 }
3567 }
b875d3f7 3568 mark_kboards ();
7146af97 3569
4c315bda
RS
3570 /* Look thru every buffer's undo list
3571 for elements that update markers that were not marked,
3572 and delete them. */
3573 {
3574 register struct buffer *nextb = all_buffers;
3575
3576 while (nextb)
3577 {
3578 /* If a buffer's undo list is Qt, that means that undo is
3579 turned off in that buffer. Calling truncate_undo_list on
3580 Qt tends to return NULL, which effectively turns undo back on.
3581 So don't call truncate_undo_list if undo_list is Qt. */
3582 if (! EQ (nextb->undo_list, Qt))
3583 {
3584 Lisp_Object tail, prev;
3585 tail = nextb->undo_list;
3586 prev = Qnil;
3587 while (CONSP (tail))
3588 {
70949dac
KR
3589 if (GC_CONSP (XCAR (tail))
3590 && GC_MARKERP (XCAR (XCAR (tail)))
3591 && ! XMARKBIT (XMARKER (XCAR (XCAR (tail)))->chain))
4c315bda
RS
3592 {
3593 if (NILP (prev))
70949dac 3594 nextb->undo_list = tail = XCDR (tail);
4c315bda 3595 else
70949dac 3596 tail = XCDR (prev) = XCDR (tail);
4c315bda
RS
3597 }
3598 else
3599 {
3600 prev = tail;
70949dac 3601 tail = XCDR (tail);
4c315bda
RS
3602 }
3603 }
3604 }
3605
3606 nextb = nextb->next;
3607 }
3608 }
3609
34400008
GM
3610#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3611 mark_stack ();
3612#endif
3613
7146af97
JB
3614 gc_sweep ();
3615
3616 /* Clear the mark bits that we set in certain root slots. */
3617
34400008
GM
3618#if (GC_MARK_STACK == GC_USE_GCPROS_AS_BEFORE \
3619 || GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES)
7146af97
JB
3620 for (tail = gcprolist; tail; tail = tail->next)
3621 for (i = 0; i < tail->nvars; i++)
3622 XUNMARK (tail->var[i]);
34400008
GM
3623#endif
3624
033a5fa3 3625 unmark_byte_stack ();
7146af97
JB
3626 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3627 {
3628 XUNMARK (*backlist->function);
3629 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
3630 i = 0;
3631 else
3632 i = backlist->nargs - 1;
3633 for (; i >= 0; i--)
3634 XUNMARK (backlist->args[i]);
3635 }
3636 XUNMARK (buffer_defaults.name);
3637 XUNMARK (buffer_local_symbols.name);
3638
34400008
GM
3639#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES && 0
3640 dump_zombies ();
3641#endif
3642
6e0fca1d
RS
3643 UNBLOCK_INPUT;
3644
c23baf9f 3645 /* clear_marks (); */
7146af97
JB
3646 gc_in_progress = 0;
3647
3648 consing_since_gc = 0;
3649 if (gc_cons_threshold < 10000)
3650 gc_cons_threshold = 10000;
3651
299585ee
RS
3652 if (garbage_collection_messages)
3653 {
6efc7df7
GM
3654 if (message_p || minibuf_level > 0)
3655 restore_message ();
299585ee
RS
3656 else
3657 message1_nolog ("Garbage collecting...done");
3658 }
7146af97 3659
6efc7df7 3660 pop_message ();
2e471eb5
GM
3661
3662 total[0] = Fcons (make_number (total_conses),
3663 make_number (total_free_conses));
3664 total[1] = Fcons (make_number (total_symbols),
3665 make_number (total_free_symbols));
3666 total[2] = Fcons (make_number (total_markers),
3667 make_number (total_free_markers));
3668 total[3] = Fcons (make_number (total_string_size),
3669 make_number (total_vector_size));
2e471eb5
GM
3670 total[4] = Fcons (make_number (total_floats),
3671 make_number (total_free_floats));
2e471eb5
GM
3672 total[5] = Fcons (make_number (total_intervals),
3673 make_number (total_free_intervals));
3674 total[6] = Fcons (make_number (total_strings),
3675 make_number (total_free_strings));
3676
34400008 3677#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
7146af97 3678 {
34400008
GM
3679 /* Compute average percentage of zombies. */
3680 double nlive = 0;
3681
3682 for (i = 0; i < 7; ++i)
3683 nlive += XFASTINT (XCAR (total[i]));
3684
3685 avg_live = (avg_live * ngcs + nlive) / (ngcs + 1);
3686 max_live = max (nlive, max_live);
3687 avg_zombies = (avg_zombies * ngcs + nzombies) / (ngcs + 1);
3688 max_zombies = max (nzombies, max_zombies);
3689 ++ngcs;
3690 }
3691#endif
7146af97 3692
34400008 3693 return Flist (7, total);
7146af97 3694}
34400008 3695
41c28a37 3696
3770920e
GM
3697/* Mark Lisp objects in glyph matrix MATRIX. Currently the
3698 only interesting objects referenced from glyphs are strings. */
41c28a37
GM
3699
3700static void
3701mark_glyph_matrix (matrix)
3702 struct glyph_matrix *matrix;
3703{
3704 struct glyph_row *row = matrix->rows;
3705 struct glyph_row *end = row + matrix->nrows;
3706
2e471eb5
GM
3707 for (; row < end; ++row)
3708 if (row->enabled_p)
3709 {
3710 int area;
3711 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
3712 {
3713 struct glyph *glyph = row->glyphs[area];
3714 struct glyph *end_glyph = glyph + row->used[area];
3715
3716 for (; glyph < end_glyph; ++glyph)
3717 if (GC_STRINGP (glyph->object)
3718 && !STRING_MARKED_P (XSTRING (glyph->object)))
3719 mark_object (&glyph->object);
3720 }
3721 }
41c28a37
GM
3722}
3723
34400008 3724
41c28a37
GM
3725/* Mark Lisp faces in the face cache C. */
3726
3727static void
3728mark_face_cache (c)
3729 struct face_cache *c;
3730{
3731 if (c)
3732 {
3733 int i, j;
3734 for (i = 0; i < c->used; ++i)
3735 {
3736 struct face *face = FACE_FROM_ID (c->f, i);
3737
3738 if (face)
3739 {
3740 for (j = 0; j < LFACE_VECTOR_SIZE; ++j)
3741 mark_object (&face->lface[j]);
41c28a37
GM
3742 }
3743 }
3744 }
3745}
3746
3747
3748#ifdef HAVE_WINDOW_SYSTEM
3749
3750/* Mark Lisp objects in image IMG. */
3751
3752static void
3753mark_image (img)
3754 struct image *img;
3755{
3756 mark_object (&img->spec);
3757
3758 if (!NILP (img->data.lisp_val))
3759 mark_object (&img->data.lisp_val);
3760}
3761
3762
3763/* Mark Lisp objects in image cache of frame F. It's done this way so
3764 that we don't have to include xterm.h here. */
3765
3766static void
3767mark_image_cache (f)
3768 struct frame *f;
3769{
3770 forall_images_in_image_cache (f, mark_image);
3771}
3772
3773#endif /* HAVE_X_WINDOWS */
3774
3775
7146af97 3776\f
1a4f1e2c 3777/* Mark reference to a Lisp_Object.
2e471eb5
GM
3778 If the object referred to has not been seen yet, recursively mark
3779 all the references contained in it. */
7146af97 3780
785cd37f
RS
3781#define LAST_MARKED_SIZE 500
3782Lisp_Object *last_marked[LAST_MARKED_SIZE];
3783int last_marked_index;
3784
41c28a37 3785void
436c5811
RS
3786mark_object (argptr)
3787 Lisp_Object *argptr;
7146af97 3788{
436c5811 3789 Lisp_Object *objptr = argptr;
7146af97 3790 register Lisp_Object obj;
4f5c1376
GM
3791#ifdef GC_CHECK_MARKED_OBJECTS
3792 void *po;
3793 struct mem_node *m;
3794#endif
7146af97 3795
9149e743 3796 loop:
7146af97 3797 obj = *objptr;
9149e743 3798 loop2:
7146af97
JB
3799 XUNMARK (obj);
3800
34400008 3801 if (PURE_POINTER_P ((PNTR_COMPARISON_TYPE) XPNTR (obj)))
7146af97
JB
3802 return;
3803
785cd37f
RS
3804 last_marked[last_marked_index++] = objptr;
3805 if (last_marked_index == LAST_MARKED_SIZE)
3806 last_marked_index = 0;
3807
4f5c1376
GM
3808 /* Perform some sanity checks on the objects marked here. Abort if
3809 we encounter an object we know is bogus. This increases GC time
3810 by ~80%, and requires compilation with GC_MARK_STACK != 0. */
3811#ifdef GC_CHECK_MARKED_OBJECTS
3812
3813 po = (void *) XPNTR (obj);
3814
3815 /* Check that the object pointed to by PO is known to be a Lisp
3816 structure allocated from the heap. */
3817#define CHECK_ALLOCATED() \
3818 do { \
3819 m = mem_find (po); \
3820 if (m == MEM_NIL) \
3821 abort (); \
3822 } while (0)
3823
3824 /* Check that the object pointed to by PO is live, using predicate
3825 function LIVEP. */
3826#define CHECK_LIVE(LIVEP) \
3827 do { \
3828 if (!LIVEP (m, po)) \
3829 abort (); \
3830 } while (0)
3831
3832 /* Check both of the above conditions. */
3833#define CHECK_ALLOCATED_AND_LIVE(LIVEP) \
3834 do { \
3835 CHECK_ALLOCATED (); \
3836 CHECK_LIVE (LIVEP); \
3837 } while (0) \
3838
3839#else /* not GC_CHECK_MARKED_OBJECTS */
3840
3841#define CHECK_ALLOCATED() (void) 0
3842#define CHECK_LIVE(LIVEP) (void) 0
3843#define CHECK_ALLOCATED_AND_LIVE(LIVEP) (void) 0
3844
3845#endif /* not GC_CHECK_MARKED_OBJECTS */
3846
0220c518 3847 switch (SWITCH_ENUM_CAST (XGCTYPE (obj)))
7146af97
JB
3848 {
3849 case Lisp_String:
3850 {
3851 register struct Lisp_String *ptr = XSTRING (obj);
4f5c1376 3852 CHECK_ALLOCATED_AND_LIVE (live_string_p);
d5e35230 3853 MARK_INTERVAL_TREE (ptr->intervals);
2e471eb5 3854 MARK_STRING (ptr);
7146af97
JB
3855 }
3856 break;
3857
76437631 3858 case Lisp_Vectorlike:
4f5c1376
GM
3859#ifdef GC_CHECK_MARKED_OBJECTS
3860 m = mem_find (po);
3861 if (m == MEM_NIL && !GC_SUBRP (obj)
3862 && po != &buffer_defaults
3863 && po != &buffer_local_symbols)
3864 abort ();
3865#endif /* GC_CHECK_MARKED_OBJECTS */
3866
30e3190a 3867 if (GC_BUFFERP (obj))
6b552283
KH
3868 {
3869 if (!XMARKBIT (XBUFFER (obj)->name))
4f5c1376
GM
3870 {
3871#ifdef GC_CHECK_MARKED_OBJECTS
3872 if (po != &buffer_defaults && po != &buffer_local_symbols)
3873 {
3874 struct buffer *b;
3875 for (b = all_buffers; b && b != po; b = b->next)
3876 ;
3877 if (b == NULL)
3878 abort ();
3879 }
3880#endif /* GC_CHECK_MARKED_OBJECTS */
3881 mark_buffer (obj);
3882 }
6b552283 3883 }
30e3190a 3884 else if (GC_SUBRP (obj))
169ee243
RS
3885 break;
3886 else if (GC_COMPILEDP (obj))
2e471eb5
GM
3887 /* We could treat this just like a vector, but it is better to
3888 save the COMPILED_CONSTANTS element for last and avoid
3889 recursion there. */
169ee243
RS
3890 {
3891 register struct Lisp_Vector *ptr = XVECTOR (obj);
3892 register EMACS_INT size = ptr->size;
3893 /* See comment above under Lisp_Vector. */
3894 struct Lisp_Vector *volatile ptr1 = ptr;
3895 register int i;
3896
3897 if (size & ARRAY_MARK_FLAG)
3898 break; /* Already marked */
4f5c1376
GM
3899
3900 CHECK_LIVE (live_vector_p);
169ee243 3901 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
76437631 3902 size &= PSEUDOVECTOR_SIZE_MASK;
169ee243
RS
3903 for (i = 0; i < size; i++) /* and then mark its elements */
3904 {
3905 if (i != COMPILED_CONSTANTS)
3906 mark_object (&ptr1->contents[i]);
3907 }
3908 /* This cast should be unnecessary, but some Mips compiler complains
3909 (MIPS-ABI + SysVR4, DC/OSx, etc). */
3910 objptr = (Lisp_Object *) &ptr1->contents[COMPILED_CONSTANTS];
3911 goto loop;
3912 }
169ee243
RS
3913 else if (GC_FRAMEP (obj))
3914 {
3915 /* See comment above under Lisp_Vector for why this is volatile. */
3916 register struct frame *volatile ptr = XFRAME (obj);
3917 register EMACS_INT size = ptr->size;
3918
3919 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
3920 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
3921
4f5c1376 3922 CHECK_LIVE (live_vector_p);
169ee243 3923 mark_object (&ptr->name);
894a9d16 3924 mark_object (&ptr->icon_name);
aba6deb8 3925 mark_object (&ptr->title);
169ee243
RS
3926 mark_object (&ptr->focus_frame);
3927 mark_object (&ptr->selected_window);
3928 mark_object (&ptr->minibuffer_window);
3929 mark_object (&ptr->param_alist);
3930 mark_object (&ptr->scroll_bars);
3931 mark_object (&ptr->condemned_scroll_bars);
3932 mark_object (&ptr->menu_bar_items);
3933 mark_object (&ptr->face_alist);
3934 mark_object (&ptr->menu_bar_vector);
3935 mark_object (&ptr->buffer_predicate);
a0e1f185 3936 mark_object (&ptr->buffer_list);
41c28a37 3937 mark_object (&ptr->menu_bar_window);
9ea173e8 3938 mark_object (&ptr->tool_bar_window);
41c28a37
GM
3939 mark_face_cache (ptr->face_cache);
3940#ifdef HAVE_WINDOW_SYSTEM
3941 mark_image_cache (ptr);
9ea173e8
GM
3942 mark_object (&ptr->desired_tool_bar_items);
3943 mark_object (&ptr->current_tool_bar_items);
3944 mark_object (&ptr->desired_tool_bar_string);
3945 mark_object (&ptr->current_tool_bar_string);
41c28a37 3946#endif /* HAVE_WINDOW_SYSTEM */
169ee243 3947 }
7b07587b 3948 else if (GC_BOOL_VECTOR_P (obj))
707788bd
RS
3949 {
3950 register struct Lisp_Vector *ptr = XVECTOR (obj);
3951
3952 if (ptr->size & ARRAY_MARK_FLAG)
3953 break; /* Already marked */
4f5c1376 3954 CHECK_LIVE (live_vector_p);
707788bd
RS
3955 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
3956 }
41c28a37
GM
3957 else if (GC_WINDOWP (obj))
3958 {
3959 register struct Lisp_Vector *ptr = XVECTOR (obj);
3960 struct window *w = XWINDOW (obj);
3961 register EMACS_INT size = ptr->size;
3962 /* The reason we use ptr1 is to avoid an apparent hardware bug
3963 that happens occasionally on the FSF's HP 300s.
3964 The bug is that a2 gets clobbered by recursive calls to mark_object.
3965 The clobberage seems to happen during function entry,
3966 perhaps in the moveml instruction.
3967 Yes, this is a crock, but we have to do it. */
3968 struct Lisp_Vector *volatile ptr1 = ptr;
3969 register int i;
3970
3971 /* Stop if already marked. */
3972 if (size & ARRAY_MARK_FLAG)
3973 break;
3974
3975 /* Mark it. */
4f5c1376 3976 CHECK_LIVE (live_vector_p);
41c28a37
GM
3977 ptr->size |= ARRAY_MARK_FLAG;
3978
3979 /* There is no Lisp data above The member CURRENT_MATRIX in
3980 struct WINDOW. Stop marking when that slot is reached. */
3981 for (i = 0;
3982 (char *) &ptr1->contents[i] < (char *) &w->current_matrix;
3983 i++)
3984 mark_object (&ptr1->contents[i]);
3985
3986 /* Mark glyphs for leaf windows. Marking window matrices is
3987 sufficient because frame matrices use the same glyph
3988 memory. */
3989 if (NILP (w->hchild)
3990 && NILP (w->vchild)
3991 && w->current_matrix)
3992 {
3993 mark_glyph_matrix (w->current_matrix);
3994 mark_glyph_matrix (w->desired_matrix);
3995 }
3996 }
3997 else if (GC_HASH_TABLE_P (obj))
3998 {
3999 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
4000 EMACS_INT size = h->size;
4001
4002 /* Stop if already marked. */
4003 if (size & ARRAY_MARK_FLAG)
4004 break;
4f5c1376 4005
41c28a37 4006 /* Mark it. */
4f5c1376 4007 CHECK_LIVE (live_vector_p);
41c28a37
GM
4008 h->size |= ARRAY_MARK_FLAG;
4009
4010 /* Mark contents. */
4011 mark_object (&h->test);
4012 mark_object (&h->weak);
4013 mark_object (&h->rehash_size);
4014 mark_object (&h->rehash_threshold);
4015 mark_object (&h->hash);
4016 mark_object (&h->next);
4017 mark_object (&h->index);
4018 mark_object (&h->user_hash_function);
4019 mark_object (&h->user_cmp_function);
4020
4021 /* If hash table is not weak, mark all keys and values.
4022 For weak tables, mark only the vector. */
4023 if (GC_NILP (h->weak))
4024 mark_object (&h->key_and_value);
4025 else
4026 XVECTOR (h->key_and_value)->size |= ARRAY_MARK_FLAG;
4027
4028 }
04ff9756 4029 else
169ee243
RS
4030 {
4031 register struct Lisp_Vector *ptr = XVECTOR (obj);
4032 register EMACS_INT size = ptr->size;
4033 /* The reason we use ptr1 is to avoid an apparent hardware bug
4034 that happens occasionally on the FSF's HP 300s.
4035 The bug is that a2 gets clobbered by recursive calls to mark_object.
4036 The clobberage seems to happen during function entry,
4037 perhaps in the moveml instruction.
4038 Yes, this is a crock, but we have to do it. */
4039 struct Lisp_Vector *volatile ptr1 = ptr;
4040 register int i;
4041
4042 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
4f5c1376 4043 CHECK_LIVE (live_vector_p);
169ee243
RS
4044 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
4045 if (size & PSEUDOVECTOR_FLAG)
4046 size &= PSEUDOVECTOR_SIZE_MASK;
41c28a37 4047
169ee243
RS
4048 for (i = 0; i < size; i++) /* and then mark its elements */
4049 mark_object (&ptr1->contents[i]);
4050 }
4051 break;
7146af97 4052
7146af97
JB
4053 case Lisp_Symbol:
4054 {
41f54422
RS
4055 /* See comment above under Lisp_Vector for why this is volatile. */
4056 register struct Lisp_Symbol *volatile ptr = XSYMBOL (obj);
7146af97
JB
4057 struct Lisp_Symbol *ptrx;
4058
4059 if (XMARKBIT (ptr->plist)) break;
4f5c1376 4060 CHECK_ALLOCATED_AND_LIVE (live_symbol_p);
7146af97 4061 XMARK (ptr->plist);
7146af97
JB
4062 mark_object ((Lisp_Object *) &ptr->value);
4063 mark_object (&ptr->function);
4064 mark_object (&ptr->plist);
34400008
GM
4065
4066 if (!PURE_POINTER_P (ptr->name))
4067 MARK_STRING (ptr->name);
2e471eb5 4068 MARK_INTERVAL_TREE (ptr->name->intervals);
2e471eb5 4069
1c6bb482
RS
4070 /* Note that we do not mark the obarray of the symbol.
4071 It is safe not to do so because nothing accesses that
4072 slot except to check whether it is nil. */
7146af97
JB
4073 ptr = ptr->next;
4074 if (ptr)
4075 {
9149e743
KH
4076 /* For the benefit of the last_marked log. */
4077 objptr = (Lisp_Object *)&XSYMBOL (obj)->next;
b0846f52 4078 ptrx = ptr; /* Use of ptrx avoids compiler bug on Sun */
7146af97 4079 XSETSYMBOL (obj, ptrx);
9149e743
KH
4080 /* We can't goto loop here because *objptr doesn't contain an
4081 actual Lisp_Object with valid datatype field. */
4082 goto loop2;
7146af97
JB
4083 }
4084 }
4085 break;
4086
a0a38eb7 4087 case Lisp_Misc:
4f5c1376 4088 CHECK_ALLOCATED_AND_LIVE (live_misc_p);
a5da44fe 4089 switch (XMISCTYPE (obj))
a0a38eb7
KH
4090 {
4091 case Lisp_Misc_Marker:
4092 XMARK (XMARKER (obj)->chain);
4093 /* DO NOT mark thru the marker's chain.
4094 The buffer's markers chain does not preserve markers from gc;
4095 instead, markers are removed from the chain when freed by gc. */
4096 break;
4097
465edf35
KH
4098 case Lisp_Misc_Buffer_Local_Value:
4099 case Lisp_Misc_Some_Buffer_Local_Value:
4100 {
4101 register struct Lisp_Buffer_Local_Value *ptr
4102 = XBUFFER_LOCAL_VALUE (obj);
a9faeabe
RS
4103 if (XMARKBIT (ptr->realvalue)) break;
4104 XMARK (ptr->realvalue);
465edf35
KH
4105 /* If the cdr is nil, avoid recursion for the car. */
4106 if (EQ (ptr->cdr, Qnil))
4107 {
a9faeabe 4108 objptr = &ptr->realvalue;
465edf35
KH
4109 goto loop;
4110 }
a9faeabe
RS
4111 mark_object (&ptr->realvalue);
4112 mark_object (&ptr->buffer);
4113 mark_object (&ptr->frame);
465edf35
KH
4114 /* See comment above under Lisp_Vector for why not use ptr here. */
4115 objptr = &XBUFFER_LOCAL_VALUE (obj)->cdr;
4116 goto loop;
4117 }
4118
c8616056
KH
4119 case Lisp_Misc_Intfwd:
4120 case Lisp_Misc_Boolfwd:
4121 case Lisp_Misc_Objfwd:
4122 case Lisp_Misc_Buffer_Objfwd:
b875d3f7 4123 case Lisp_Misc_Kboard_Objfwd:
c8616056
KH
4124 /* Don't bother with Lisp_Buffer_Objfwd,
4125 since all markable slots in current buffer marked anyway. */
4126 /* Don't need to do Lisp_Objfwd, since the places they point
4127 are protected with staticpro. */
4128 break;
4129
e202fa34
KH
4130 case Lisp_Misc_Overlay:
4131 {
4132 struct Lisp_Overlay *ptr = XOVERLAY (obj);
4133 if (!XMARKBIT (ptr->plist))
4134 {
4135 XMARK (ptr->plist);
4136 mark_object (&ptr->start);
4137 mark_object (&ptr->end);
4138 objptr = &ptr->plist;
4139 goto loop;
4140 }
4141 }
4142 break;
4143
a0a38eb7
KH
4144 default:
4145 abort ();
4146 }
7146af97
JB
4147 break;
4148
4149 case Lisp_Cons:
7146af97
JB
4150 {
4151 register struct Lisp_Cons *ptr = XCONS (obj);
4152 if (XMARKBIT (ptr->car)) break;
4f5c1376 4153 CHECK_ALLOCATED_AND_LIVE (live_cons_p);
7146af97 4154 XMARK (ptr->car);
c54ca951
RS
4155 /* If the cdr is nil, avoid recursion for the car. */
4156 if (EQ (ptr->cdr, Qnil))
4157 {
4158 objptr = &ptr->car;
c54ca951
RS
4159 goto loop;
4160 }
7146af97 4161 mark_object (&ptr->car);
41f54422 4162 /* See comment above under Lisp_Vector for why not use ptr here. */
70949dac 4163 objptr = &XCDR (obj);
7146af97
JB
4164 goto loop;
4165 }
4166
7146af97 4167 case Lisp_Float:
4f5c1376 4168 CHECK_ALLOCATED_AND_LIVE (live_float_p);
7146af97
JB
4169 XMARK (XFLOAT (obj)->type);
4170 break;
7146af97 4171
7146af97 4172 case Lisp_Int:
7146af97
JB
4173 break;
4174
4175 default:
4176 abort ();
4177 }
4f5c1376
GM
4178
4179#undef CHECK_LIVE
4180#undef CHECK_ALLOCATED
4181#undef CHECK_ALLOCATED_AND_LIVE
7146af97
JB
4182}
4183
4184/* Mark the pointers in a buffer structure. */
4185
4186static void
4187mark_buffer (buf)
4188 Lisp_Object buf;
4189{
7146af97
JB
4190 register struct buffer *buffer = XBUFFER (buf);
4191 register Lisp_Object *ptr;
30e3190a 4192 Lisp_Object base_buffer;
7146af97
JB
4193
4194 /* This is the buffer's markbit */
4195 mark_object (&buffer->name);
4196 XMARK (buffer->name);
4197
30e3190a 4198 MARK_INTERVAL_TREE (BUF_INTERVALS (buffer));
d5e35230 4199
4c315bda
RS
4200 if (CONSP (buffer->undo_list))
4201 {
4202 Lisp_Object tail;
4203 tail = buffer->undo_list;
4204
4205 while (CONSP (tail))
4206 {
4207 register struct Lisp_Cons *ptr = XCONS (tail);
4208
4209 if (XMARKBIT (ptr->car))
4210 break;
4211 XMARK (ptr->car);
4212 if (GC_CONSP (ptr->car)
70949dac
KR
4213 && ! XMARKBIT (XCAR (ptr->car))
4214 && GC_MARKERP (XCAR (ptr->car)))
4c315bda 4215 {
70949dac
KR
4216 XMARK (XCAR (ptr->car));
4217 mark_object (&XCDR (ptr->car));
4c315bda
RS
4218 }
4219 else
4220 mark_object (&ptr->car);
4221
4222 if (CONSP (ptr->cdr))
4223 tail = ptr->cdr;
4224 else
4225 break;
4226 }
4227
70949dac 4228 mark_object (&XCDR (tail));
4c315bda
RS
4229 }
4230 else
4231 mark_object (&buffer->undo_list);
4232
7146af97
JB
4233 for (ptr = &buffer->name + 1;
4234 (char *)ptr < (char *)buffer + sizeof (struct buffer);
4235 ptr++)
4236 mark_object (ptr);
30e3190a
RS
4237
4238 /* If this is an indirect buffer, mark its base buffer. */
6b552283 4239 if (buffer->base_buffer && !XMARKBIT (buffer->base_buffer->name))
30e3190a
RS
4240 {
4241 XSETBUFFER (base_buffer, buffer->base_buffer);
4242 mark_buffer (base_buffer);
4243 }
7146af97 4244}
084b1a0c
KH
4245
4246
b875d3f7 4247/* Mark the pointers in the kboard objects. */
084b1a0c
KH
4248
4249static void
b875d3f7 4250mark_kboards ()
084b1a0c 4251{
b875d3f7 4252 KBOARD *kb;
b94daf1e 4253 Lisp_Object *p;
b875d3f7 4254 for (kb = all_kboards; kb; kb = kb->next_kboard)
084b1a0c 4255 {
b94daf1e
KH
4256 if (kb->kbd_macro_buffer)
4257 for (p = kb->kbd_macro_buffer; p < kb->kbd_macro_ptr; p++)
4258 mark_object (p);
4bfd0c4f
RS
4259 mark_object (&kb->Voverriding_terminal_local_map);
4260 mark_object (&kb->Vlast_command);
4261 mark_object (&kb->Vreal_last_command);
9671abc2 4262 mark_object (&kb->Vprefix_arg);
23c73c16 4263 mark_object (&kb->Vlast_prefix_arg);
b875d3f7 4264 mark_object (&kb->kbd_queue);
4bfd0c4f 4265 mark_object (&kb->defining_kbd_macro);
b875d3f7 4266 mark_object (&kb->Vlast_kbd_macro);
b94daf1e 4267 mark_object (&kb->Vsystem_key_alist);
6d03a6fd 4268 mark_object (&kb->system_key_syms);
4bfd0c4f 4269 mark_object (&kb->Vdefault_minibuffer_frame);
084b1a0c
KH
4270 }
4271}
41c28a37
GM
4272
4273
4274/* Value is non-zero if OBJ will survive the current GC because it's
4275 either marked or does not need to be marked to survive. */
4276
4277int
4278survives_gc_p (obj)
4279 Lisp_Object obj;
4280{
4281 int survives_p;
4282
4283 switch (XGCTYPE (obj))
4284 {
4285 case Lisp_Int:
4286 survives_p = 1;
4287 break;
4288
4289 case Lisp_Symbol:
4290 survives_p = XMARKBIT (XSYMBOL (obj)->plist);
4291 break;
4292
4293 case Lisp_Misc:
4294 switch (XMISCTYPE (obj))
4295 {
4296 case Lisp_Misc_Marker:
4297 survives_p = XMARKBIT (obj);
4298 break;
4299
4300 case Lisp_Misc_Buffer_Local_Value:
4301 case Lisp_Misc_Some_Buffer_Local_Value:
4302 survives_p = XMARKBIT (XBUFFER_LOCAL_VALUE (obj)->realvalue);
4303 break;
4304
4305 case Lisp_Misc_Intfwd:
4306 case Lisp_Misc_Boolfwd:
4307 case Lisp_Misc_Objfwd:
4308 case Lisp_Misc_Buffer_Objfwd:
4309 case Lisp_Misc_Kboard_Objfwd:
4310 survives_p = 1;
4311 break;
4312
4313 case Lisp_Misc_Overlay:
4314 survives_p = XMARKBIT (XOVERLAY (obj)->plist);
4315 break;
4316
4317 default:
4318 abort ();
4319 }
4320 break;
4321
4322 case Lisp_String:
4323 {
4324 struct Lisp_String *s = XSTRING (obj);
2e471eb5 4325 survives_p = STRING_MARKED_P (s);
41c28a37
GM
4326 }
4327 break;
4328
4329 case Lisp_Vectorlike:
4330 if (GC_BUFFERP (obj))
4331 survives_p = XMARKBIT (XBUFFER (obj)->name);
4332 else if (GC_SUBRP (obj))
4333 survives_p = 1;
4334 else
4335 survives_p = XVECTOR (obj)->size & ARRAY_MARK_FLAG;
4336 break;
4337
4338 case Lisp_Cons:
4339 survives_p = XMARKBIT (XCAR (obj));
4340 break;
4341
41c28a37
GM
4342 case Lisp_Float:
4343 survives_p = XMARKBIT (XFLOAT (obj)->type);
4344 break;
41c28a37
GM
4345
4346 default:
4347 abort ();
4348 }
4349
34400008 4350 return survives_p || PURE_POINTER_P ((void *) XPNTR (obj));
41c28a37
GM
4351}
4352
4353
7146af97 4354\f
1a4f1e2c 4355/* Sweep: find all structures not marked, and free them. */
7146af97
JB
4356
4357static void
4358gc_sweep ()
4359{
41c28a37
GM
4360 /* Remove or mark entries in weak hash tables.
4361 This must be done before any object is unmarked. */
4362 sweep_weak_hash_tables ();
4363
2e471eb5 4364 sweep_strings ();
7146af97
JB
4365
4366 /* Put all unmarked conses on free list */
4367 {
4368 register struct cons_block *cblk;
6ca94ac9 4369 struct cons_block **cprev = &cons_block;
7146af97
JB
4370 register int lim = cons_block_index;
4371 register int num_free = 0, num_used = 0;
4372
4373 cons_free_list = 0;
4374
6ca94ac9 4375 for (cblk = cons_block; cblk; cblk = *cprev)
7146af97
JB
4376 {
4377 register int i;
6ca94ac9 4378 int this_free = 0;
7146af97
JB
4379 for (i = 0; i < lim; i++)
4380 if (!XMARKBIT (cblk->conses[i].car))
4381 {
6ca94ac9 4382 this_free++;
1cd5fe6a 4383 *(struct Lisp_Cons **)&cblk->conses[i].cdr = cons_free_list;
7146af97 4384 cons_free_list = &cblk->conses[i];
34400008
GM
4385#if GC_MARK_STACK
4386 cons_free_list->car = Vdead;
4387#endif
7146af97
JB
4388 }
4389 else
4390 {
4391 num_used++;
4392 XUNMARK (cblk->conses[i].car);
4393 }
4394 lim = CONS_BLOCK_SIZE;
6ca94ac9
KH
4395 /* If this block contains only free conses and we have already
4396 seen more than two blocks worth of free conses then deallocate
4397 this block. */
6feef451 4398 if (this_free == CONS_BLOCK_SIZE && num_free > CONS_BLOCK_SIZE)
6ca94ac9 4399 {
6ca94ac9
KH
4400 *cprev = cblk->next;
4401 /* Unhook from the free list. */
4402 cons_free_list = *(struct Lisp_Cons **) &cblk->conses[0].cdr;
c8099634
RS
4403 lisp_free (cblk);
4404 n_cons_blocks--;
6ca94ac9
KH
4405 }
4406 else
6feef451
AS
4407 {
4408 num_free += this_free;
4409 cprev = &cblk->next;
4410 }
7146af97
JB
4411 }
4412 total_conses = num_used;
4413 total_free_conses = num_free;
4414 }
4415
7146af97
JB
4416 /* Put all unmarked floats on free list */
4417 {
4418 register struct float_block *fblk;
6ca94ac9 4419 struct float_block **fprev = &float_block;
7146af97
JB
4420 register int lim = float_block_index;
4421 register int num_free = 0, num_used = 0;
4422
4423 float_free_list = 0;
4424
6ca94ac9 4425 for (fblk = float_block; fblk; fblk = *fprev)
7146af97
JB
4426 {
4427 register int i;
6ca94ac9 4428 int this_free = 0;
7146af97
JB
4429 for (i = 0; i < lim; i++)
4430 if (!XMARKBIT (fblk->floats[i].type))
4431 {
6ca94ac9 4432 this_free++;
1cd5fe6a 4433 *(struct Lisp_Float **)&fblk->floats[i].data = float_free_list;
7146af97 4434 float_free_list = &fblk->floats[i];
34400008
GM
4435#if GC_MARK_STACK
4436 float_free_list->type = Vdead;
4437#endif
7146af97
JB
4438 }
4439 else
4440 {
4441 num_used++;
4442 XUNMARK (fblk->floats[i].type);
4443 }
4444 lim = FLOAT_BLOCK_SIZE;
6ca94ac9
KH
4445 /* If this block contains only free floats and we have already
4446 seen more than two blocks worth of free floats then deallocate
4447 this block. */
6feef451 4448 if (this_free == FLOAT_BLOCK_SIZE && num_free > FLOAT_BLOCK_SIZE)
6ca94ac9 4449 {
6ca94ac9
KH
4450 *fprev = fblk->next;
4451 /* Unhook from the free list. */
4452 float_free_list = *(struct Lisp_Float **) &fblk->floats[0].data;
c8099634
RS
4453 lisp_free (fblk);
4454 n_float_blocks--;
6ca94ac9
KH
4455 }
4456 else
6feef451
AS
4457 {
4458 num_free += this_free;
4459 fprev = &fblk->next;
4460 }
7146af97
JB
4461 }
4462 total_floats = num_used;
4463 total_free_floats = num_free;
4464 }
7146af97 4465
d5e35230
JA
4466 /* Put all unmarked intervals on free list */
4467 {
4468 register struct interval_block *iblk;
6ca94ac9 4469 struct interval_block **iprev = &interval_block;
d5e35230
JA
4470 register int lim = interval_block_index;
4471 register int num_free = 0, num_used = 0;
4472
4473 interval_free_list = 0;
4474
6ca94ac9 4475 for (iblk = interval_block; iblk; iblk = *iprev)
d5e35230
JA
4476 {
4477 register int i;
6ca94ac9 4478 int this_free = 0;
d5e35230
JA
4479
4480 for (i = 0; i < lim; i++)
4481 {
4482 if (! XMARKBIT (iblk->intervals[i].plist))
4483 {
439d5cb4 4484 SET_INTERVAL_PARENT (&iblk->intervals[i], interval_free_list);
d5e35230 4485 interval_free_list = &iblk->intervals[i];
6ca94ac9 4486 this_free++;
d5e35230
JA
4487 }
4488 else
4489 {
4490 num_used++;
4491 XUNMARK (iblk->intervals[i].plist);
4492 }
4493 }
4494 lim = INTERVAL_BLOCK_SIZE;
6ca94ac9
KH
4495 /* If this block contains only free intervals and we have already
4496 seen more than two blocks worth of free intervals then
4497 deallocate this block. */
6feef451 4498 if (this_free == INTERVAL_BLOCK_SIZE && num_free > INTERVAL_BLOCK_SIZE)
6ca94ac9 4499 {
6ca94ac9
KH
4500 *iprev = iblk->next;
4501 /* Unhook from the free list. */
439d5cb4 4502 interval_free_list = INTERVAL_PARENT (&iblk->intervals[0]);
c8099634
RS
4503 lisp_free (iblk);
4504 n_interval_blocks--;
6ca94ac9
KH
4505 }
4506 else
6feef451
AS
4507 {
4508 num_free += this_free;
4509 iprev = &iblk->next;
4510 }
d5e35230
JA
4511 }
4512 total_intervals = num_used;
4513 total_free_intervals = num_free;
4514 }
d5e35230 4515
7146af97
JB
4516 /* Put all unmarked symbols on free list */
4517 {
4518 register struct symbol_block *sblk;
6ca94ac9 4519 struct symbol_block **sprev = &symbol_block;
7146af97
JB
4520 register int lim = symbol_block_index;
4521 register int num_free = 0, num_used = 0;
4522
4523 symbol_free_list = 0;
4524
6ca94ac9 4525 for (sblk = symbol_block; sblk; sblk = *sprev)
7146af97
JB
4526 {
4527 register int i;
6ca94ac9 4528 int this_free = 0;
7146af97
JB
4529 for (i = 0; i < lim; i++)
4530 if (!XMARKBIT (sblk->symbols[i].plist))
4531 {
85481507 4532 *(struct Lisp_Symbol **)&sblk->symbols[i].value = symbol_free_list;
7146af97 4533 symbol_free_list = &sblk->symbols[i];
34400008
GM
4534#if GC_MARK_STACK
4535 symbol_free_list->function = Vdead;
4536#endif
6ca94ac9 4537 this_free++;
7146af97
JB
4538 }
4539 else
4540 {
4541 num_used++;
34400008
GM
4542 if (!PURE_POINTER_P (sblk->symbols[i].name))
4543 UNMARK_STRING (sblk->symbols[i].name);
7146af97
JB
4544 XUNMARK (sblk->symbols[i].plist);
4545 }
4546 lim = SYMBOL_BLOCK_SIZE;
6ca94ac9
KH
4547 /* If this block contains only free symbols and we have already
4548 seen more than two blocks worth of free symbols then deallocate
4549 this block. */
6feef451 4550 if (this_free == SYMBOL_BLOCK_SIZE && num_free > SYMBOL_BLOCK_SIZE)
6ca94ac9 4551 {
6ca94ac9
KH
4552 *sprev = sblk->next;
4553 /* Unhook from the free list. */
4554 symbol_free_list = *(struct Lisp_Symbol **)&sblk->symbols[0].value;
c8099634
RS
4555 lisp_free (sblk);
4556 n_symbol_blocks--;
6ca94ac9
KH
4557 }
4558 else
6feef451
AS
4559 {
4560 num_free += this_free;
4561 sprev = &sblk->next;
4562 }
7146af97
JB
4563 }
4564 total_symbols = num_used;
4565 total_free_symbols = num_free;
4566 }
4567
a9faeabe
RS
4568 /* Put all unmarked misc's on free list.
4569 For a marker, first unchain it from the buffer it points into. */
7146af97
JB
4570 {
4571 register struct marker_block *mblk;
6ca94ac9 4572 struct marker_block **mprev = &marker_block;
7146af97
JB
4573 register int lim = marker_block_index;
4574 register int num_free = 0, num_used = 0;
4575
4576 marker_free_list = 0;
4577
6ca94ac9 4578 for (mblk = marker_block; mblk; mblk = *mprev)
7146af97
JB
4579 {
4580 register int i;
6ca94ac9 4581 int this_free = 0;
26b926e1 4582 EMACS_INT already_free = -1;
fa05e253 4583
7146af97 4584 for (i = 0; i < lim; i++)
465edf35
KH
4585 {
4586 Lisp_Object *markword;
a5da44fe 4587 switch (mblk->markers[i].u_marker.type)
465edf35
KH
4588 {
4589 case Lisp_Misc_Marker:
4590 markword = &mblk->markers[i].u_marker.chain;
4591 break;
4592 case Lisp_Misc_Buffer_Local_Value:
4593 case Lisp_Misc_Some_Buffer_Local_Value:
a9faeabe 4594 markword = &mblk->markers[i].u_buffer_local_value.realvalue;
465edf35 4595 break;
e202fa34
KH
4596 case Lisp_Misc_Overlay:
4597 markword = &mblk->markers[i].u_overlay.plist;
4598 break;
fa05e253
RS
4599 case Lisp_Misc_Free:
4600 /* If the object was already free, keep it
4601 on the free list. */
74d84334 4602 markword = (Lisp_Object *) &already_free;
fa05e253 4603 break;
465edf35
KH
4604 default:
4605 markword = 0;
e202fa34 4606 break;
465edf35
KH
4607 }
4608 if (markword && !XMARKBIT (*markword))
4609 {
4610 Lisp_Object tem;
a5da44fe 4611 if (mblk->markers[i].u_marker.type == Lisp_Misc_Marker)
465edf35
KH
4612 {
4613 /* tem1 avoids Sun compiler bug */
4614 struct Lisp_Marker *tem1 = &mblk->markers[i].u_marker;
4615 XSETMARKER (tem, tem1);
4616 unchain_marker (tem);
4617 }
fa05e253
RS
4618 /* Set the type of the freed object to Lisp_Misc_Free.
4619 We could leave the type alone, since nobody checks it,
465edf35 4620 but this might catch bugs faster. */
a5da44fe 4621 mblk->markers[i].u_marker.type = Lisp_Misc_Free;
465edf35
KH
4622 mblk->markers[i].u_free.chain = marker_free_list;
4623 marker_free_list = &mblk->markers[i];
6ca94ac9 4624 this_free++;
465edf35
KH
4625 }
4626 else
4627 {
4628 num_used++;
4629 if (markword)
4630 XUNMARK (*markword);
4631 }
4632 }
7146af97 4633 lim = MARKER_BLOCK_SIZE;
6ca94ac9
KH
4634 /* If this block contains only free markers and we have already
4635 seen more than two blocks worth of free markers then deallocate
4636 this block. */
6feef451 4637 if (this_free == MARKER_BLOCK_SIZE && num_free > MARKER_BLOCK_SIZE)
6ca94ac9 4638 {
6ca94ac9
KH
4639 *mprev = mblk->next;
4640 /* Unhook from the free list. */
4641 marker_free_list = mblk->markers[0].u_free.chain;
c8099634
RS
4642 lisp_free (mblk);
4643 n_marker_blocks--;
6ca94ac9
KH
4644 }
4645 else
6feef451
AS
4646 {
4647 num_free += this_free;
4648 mprev = &mblk->next;
4649 }
7146af97
JB
4650 }
4651
4652 total_markers = num_used;
4653 total_free_markers = num_free;
4654 }
4655
4656 /* Free all unmarked buffers */
4657 {
4658 register struct buffer *buffer = all_buffers, *prev = 0, *next;
4659
4660 while (buffer)
4661 if (!XMARKBIT (buffer->name))
4662 {
4663 if (prev)
4664 prev->next = buffer->next;
4665 else
4666 all_buffers = buffer->next;
4667 next = buffer->next;
34400008 4668 lisp_free (buffer);
7146af97
JB
4669 buffer = next;
4670 }
4671 else
4672 {
4673 XUNMARK (buffer->name);
30e3190a 4674 UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer));
7146af97
JB
4675 prev = buffer, buffer = buffer->next;
4676 }
4677 }
4678
7146af97
JB
4679 /* Free all unmarked vectors */
4680 {
4681 register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next;
4682 total_vector_size = 0;
4683
4684 while (vector)
4685 if (!(vector->size & ARRAY_MARK_FLAG))
4686 {
4687 if (prev)
4688 prev->next = vector->next;
4689 else
4690 all_vectors = vector->next;
4691 next = vector->next;
c8099634
RS
4692 lisp_free (vector);
4693 n_vectors--;
7146af97 4694 vector = next;
41c28a37 4695
7146af97
JB
4696 }
4697 else
4698 {
4699 vector->size &= ~ARRAY_MARK_FLAG;
fa05e253
RS
4700 if (vector->size & PSEUDOVECTOR_FLAG)
4701 total_vector_size += (PSEUDOVECTOR_SIZE_MASK & vector->size);
4702 else
4703 total_vector_size += vector->size;
7146af97
JB
4704 prev = vector, vector = vector->next;
4705 }
4706 }
7146af97 4707}
7146af97 4708
7146af97 4709
7146af97 4710
7146af97 4711\f
20d24714
JB
4712/* Debugging aids. */
4713
31ce1c91 4714DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0,
20d24714
JB
4715 "Return the address of the last byte Emacs has allocated, divided by 1024.\n\
4716This may be helpful in debugging Emacs's memory usage.\n\
e41ae81f 4717We divide the value by 1024 to make sure it fits in a Lisp integer.")
20d24714
JB
4718 ()
4719{
4720 Lisp_Object end;
4721
45d12a89 4722 XSETINT (end, (EMACS_INT) sbrk (0) / 1024);
20d24714
JB
4723
4724 return end;
4725}
4726
310ea200
RS
4727DEFUN ("memory-use-counts", Fmemory_use_counts, Smemory_use_counts, 0, 0, 0,
4728 "Return a list of counters that measure how much consing there has been.\n\
4729Each of these counters increments for a certain kind of object.\n\
4730The counters wrap around from the largest positive integer to zero.\n\
4731Garbage collection does not decrease them.\n\
4732The elements of the value are as follows:\n\
2e471eb5 4733 (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS STRINGS)\n\
310ea200
RS
4734All are in units of 1 = one object consed\n\
4735except for VECTOR-CELLS and STRING-CHARS, which count the total length of\n\
4736objects consed.\n\
4737MISCS include overlays, markers, and some internal types.\n\
4738Frames, windows, buffers, and subprocesses count as vectors\n\
4739 (but the contents of a buffer's text do not count here).")
4740 ()
4741{
2e471eb5 4742 Lisp_Object consed[8];
310ea200 4743
2e471eb5 4744 XSETINT (consed[0],
290c8f1e 4745 cons_cells_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2e471eb5 4746 XSETINT (consed[1],
290c8f1e 4747 floats_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2e471eb5 4748 XSETINT (consed[2],
290c8f1e 4749 vector_cells_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2e471eb5 4750 XSETINT (consed[3],
290c8f1e 4751 symbols_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2e471eb5 4752 XSETINT (consed[4],
290c8f1e 4753 string_chars_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2e471eb5 4754 XSETINT (consed[5],
290c8f1e 4755 misc_objects_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2e471eb5 4756 XSETINT (consed[6],
290c8f1e 4757 intervals_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
2e471eb5
GM
4758 XSETINT (consed[7],
4759 strings_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
310ea200 4760
2e471eb5 4761 return Flist (8, consed);
310ea200 4762}
e0b8c689
KR
4763
4764int suppress_checking;
4765void
4766die (msg, file, line)
4767 const char *msg;
4768 const char *file;
4769 int line;
4770{
4771 fprintf (stderr, "\r\nEmacs fatal error: %s:%d: %s\r\n",
4772 file, line, msg);
4773 abort ();
4774}
20d24714 4775\f
7146af97
JB
4776/* Initialization */
4777
dfcf069d 4778void
7146af97
JB
4779init_alloc_once ()
4780{
4781 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
4782 pureptr = 0;
34400008
GM
4783#if GC_MARK_STACK
4784 mem_init ();
4785 Vdead = make_pure_string ("DEAD", 4, 4, 0);
4786#endif
4c0be5f4
JB
4787#ifdef HAVE_SHM
4788 pure_size = PURESIZE;
4789#endif
7146af97
JB
4790 all_vectors = 0;
4791 ignore_warnings = 1;
d1658221
RS
4792#ifdef DOUG_LEA_MALLOC
4793 mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */
4794 mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */
81d492d5 4795 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); /* max. number of mmap'ed areas */
d1658221 4796#endif
7146af97
JB
4797 init_strings ();
4798 init_cons ();
4799 init_symbol ();
4800 init_marker ();
7146af97 4801 init_float ();
34400008 4802 init_intervals ();
d5e35230 4803
276cbe5a
RS
4804#ifdef REL_ALLOC
4805 malloc_hysteresis = 32;
4806#else
4807 malloc_hysteresis = 0;
4808#endif
4809
4810 spare_memory = (char *) malloc (SPARE_MEMORY);
4811
7146af97
JB
4812 ignore_warnings = 0;
4813 gcprolist = 0;
630686c8 4814 byte_stack_list = 0;
7146af97
JB
4815 staticidx = 0;
4816 consing_since_gc = 0;
7d179cea 4817 gc_cons_threshold = 100000 * sizeof (Lisp_Object);
7146af97
JB
4818#ifdef VIRT_ADDR_VARIES
4819 malloc_sbrk_unused = 1<<22; /* A large number */
4820 malloc_sbrk_used = 100000; /* as reasonable as any number */
4821#endif /* VIRT_ADDR_VARIES */
4822}
4823
dfcf069d 4824void
7146af97
JB
4825init_alloc ()
4826{
4827 gcprolist = 0;
630686c8 4828 byte_stack_list = 0;
182ff242
GM
4829#if GC_MARK_STACK
4830#if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
4831 setjmp_tested_p = longjmps_done = 0;
4832#endif
4833#endif
7146af97
JB
4834}
4835
4836void
4837syms_of_alloc ()
4838{
4839 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold,
4840 "*Number of bytes of consing between garbage collections.\n\
4841Garbage collection can happen automatically once this many bytes have been\n\
4842allocated since the last garbage collection. All data types count.\n\n\
4843Garbage collection happens automatically only when `eval' is called.\n\n\
4844By binding this temporarily to a large number, you can effectively\n\
4845prevent garbage collection during a part of the program.");
4846
4847 DEFVAR_INT ("pure-bytes-used", &pureptr,
4848 "Number of bytes of sharable Lisp data allocated so far.");
4849
0819585c
RS
4850 DEFVAR_INT ("cons-cells-consed", &cons_cells_consed,
4851 "Number of cons cells that have been consed so far.");
4852
4853 DEFVAR_INT ("floats-consed", &floats_consed,
4854 "Number of floats that have been consed so far.");
4855
4856 DEFVAR_INT ("vector-cells-consed", &vector_cells_consed,
4857 "Number of vector cells that have been consed so far.");
4858
4859 DEFVAR_INT ("symbols-consed", &symbols_consed,
4860 "Number of symbols that have been consed so far.");
4861
4862 DEFVAR_INT ("string-chars-consed", &string_chars_consed,
4863 "Number of string characters that have been consed so far.");
4864
4865 DEFVAR_INT ("misc-objects-consed", &misc_objects_consed,
4866 "Number of miscellaneous objects that have been consed so far.");
4867
4868 DEFVAR_INT ("intervals-consed", &intervals_consed,
4869 "Number of intervals that have been consed so far.");
4870
2e471eb5
GM
4871 DEFVAR_INT ("strings-consed", &strings_consed,
4872 "Number of strings that have been consed so far.");
4873
7146af97
JB
4874 DEFVAR_LISP ("purify-flag", &Vpurify_flag,
4875 "Non-nil means loading Lisp code in order to dump an executable.\n\
4876This means that certain objects should be allocated in shared (pure) space.");
4877
502b9b64 4878 DEFVAR_INT ("undo-limit", &undo_limit,
7146af97 4879 "Keep no more undo information once it exceeds this size.\n\
502b9b64 4880This limit is applied when garbage collection happens.\n\
7146af97
JB
4881The size is counted as the number of bytes occupied,\n\
4882which includes both saved text and other data.");
502b9b64 4883 undo_limit = 20000;
7146af97 4884
502b9b64 4885 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit,
7146af97
JB
4886 "Don't keep more than this much size of undo information.\n\
4887A command which pushes past this size is itself forgotten.\n\
502b9b64 4888This limit is applied when garbage collection happens.\n\
7146af97
JB
4889The size is counted as the number of bytes occupied,\n\
4890which includes both saved text and other data.");
502b9b64 4891 undo_strong_limit = 30000;
7146af97 4892
299585ee
RS
4893 DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages,
4894 "Non-nil means display messages at start and end of garbage collection.");
4895 garbage_collection_messages = 0;
4896
bcb61d60
KH
4897 /* We build this in advance because if we wait until we need it, we might
4898 not be able to allocate the memory to hold it. */
cf3540e4 4899 memory_signal_data
276cbe5a 4900 = Fcons (Qerror, Fcons (build_string ("Memory exhausted--use M-x save-some-buffers RET"), Qnil));
bcb61d60
KH
4901 staticpro (&memory_signal_data);
4902
e8197642
RS
4903 staticpro (&Qgc_cons_threshold);
4904 Qgc_cons_threshold = intern ("gc-cons-threshold");
4905
a59de17b
RS
4906 staticpro (&Qchar_table_extra_slots);
4907 Qchar_table_extra_slots = intern ("char-table-extra-slots");
4908
7146af97
JB
4909 defsubr (&Scons);
4910 defsubr (&Slist);
4911 defsubr (&Svector);
4912 defsubr (&Smake_byte_code);
4913 defsubr (&Smake_list);
4914 defsubr (&Smake_vector);
7b07587b 4915 defsubr (&Smake_char_table);
7146af97 4916 defsubr (&Smake_string);
7b07587b 4917 defsubr (&Smake_bool_vector);
7146af97
JB
4918 defsubr (&Smake_symbol);
4919 defsubr (&Smake_marker);
4920 defsubr (&Spurecopy);
4921 defsubr (&Sgarbage_collect);
20d24714 4922 defsubr (&Smemory_limit);
310ea200 4923 defsubr (&Smemory_use_counts);
34400008
GM
4924
4925#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4926 defsubr (&Sgc_status);
4927#endif
7146af97 4928}