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