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