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