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