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