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