(mark_byte_stack): Update calls to mark_object.
[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;
d5e35230 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
GM
3470 return (offset >= 0
3471 && offset % sizeof b->floats[0] == 0
34400008 3472 && (b != float_block
ab6780cd 3473 || offset / sizeof b->floats[0] < float_block_index));
34400008
GM
3474 }
3475 else
3476 return 0;
3477}
3478
3479
3480/* Value is non-zero if P is a pointer to a live Lisp Misc on
3481 the heap. M is a pointer to the mem_block for P. */
3482
3483static INLINE int
3484live_misc_p (m, p)
3485 struct mem_node *m;
3486 void *p;
3487{
3488 if (m->type == MEM_TYPE_MISC)
3489 {
3490 struct marker_block *b = (struct marker_block *) m->start;
3491 int offset = (char *) p - (char *) &b->markers[0];
177c0ea7 3492
34400008
GM
3493 /* P must point to the start of a Lisp_Misc, not be
3494 one of the unused cells in the current misc block,
3495 and not be on the free-list. */
176bc847
GM
3496 return (offset >= 0
3497 && offset % sizeof b->markers[0] == 0
34400008
GM
3498 && (b != marker_block
3499 || offset / sizeof b->markers[0] < marker_block_index)
3500 && ((union Lisp_Misc *) p)->u_marker.type != Lisp_Misc_Free);
3501 }
3502 else
3503 return 0;
3504}
3505
3506
3507/* Value is non-zero if P is a pointer to a live vector-like object.
3508 M is a pointer to the mem_block for P. */
3509
3510static INLINE int
3511live_vector_p (m, p)
3512 struct mem_node *m;
3513 void *p;
3514{
ece93c02
GM
3515 return (p == m->start
3516 && m->type >= MEM_TYPE_VECTOR
3517 && m->type <= MEM_TYPE_WINDOW);
34400008
GM
3518}
3519
3520
2336fe58 3521/* Value is non-zero if P is a pointer to a live buffer. M is a
34400008
GM
3522 pointer to the mem_block for P. */
3523
3524static INLINE int
3525live_buffer_p (m, p)
3526 struct mem_node *m;
3527 void *p;
3528{
3529 /* P must point to the start of the block, and the buffer
3530 must not have been killed. */
3531 return (m->type == MEM_TYPE_BUFFER
3532 && p == m->start
3533 && !NILP (((struct buffer *) p)->name));
3534}
3535
13c844fb
GM
3536#endif /* GC_MARK_STACK || defined GC_MALLOC_CHECK */
3537
3538#if GC_MARK_STACK
3539
34400008
GM
3540#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3541
3542/* Array of objects that are kept alive because the C stack contains
3543 a pattern that looks like a reference to them . */
3544
3545#define MAX_ZOMBIES 10
3546static Lisp_Object zombies[MAX_ZOMBIES];
3547
3548/* Number of zombie objects. */
3549
3550static int nzombies;
3551
3552/* Number of garbage collections. */
3553
3554static int ngcs;
3555
3556/* Average percentage of zombies per collection. */
3557
3558static double avg_zombies;
3559
3560/* Max. number of live and zombie objects. */
3561
3562static int max_live, max_zombies;
3563
3564/* Average number of live objects per GC. */
3565
3566static double avg_live;
3567
3568DEFUN ("gc-status", Fgc_status, Sgc_status, 0, 0, "",
7ee72033
MB
3569 doc: /* Show information about live and zombie objects. */)
3570 ()
34400008 3571{
83fc9c63
DL
3572 Lisp_Object args[8], zombie_list = Qnil;
3573 int i;
3574 for (i = 0; i < nzombies; i++)
3575 zombie_list = Fcons (zombies[i], zombie_list);
3576 args[0] = build_string ("%d GCs, avg live/zombies = %.2f/%.2f (%f%%), max %d/%d\nzombies: %S");
34400008
GM
3577 args[1] = make_number (ngcs);
3578 args[2] = make_float (avg_live);
3579 args[3] = make_float (avg_zombies);
3580 args[4] = make_float (avg_zombies / avg_live / 100);
3581 args[5] = make_number (max_live);
3582 args[6] = make_number (max_zombies);
83fc9c63
DL
3583 args[7] = zombie_list;
3584 return Fmessage (8, args);
34400008
GM
3585}
3586
3587#endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
3588
3589
182ff242
GM
3590/* Mark OBJ if we can prove it's a Lisp_Object. */
3591
3592static INLINE void
3593mark_maybe_object (obj)
3594 Lisp_Object obj;
3595{
3596 void *po = (void *) XPNTR (obj);
3597 struct mem_node *m = mem_find (po);
177c0ea7 3598
182ff242
GM
3599 if (m != MEM_NIL)
3600 {
3601 int mark_p = 0;
3602
3603 switch (XGCTYPE (obj))
3604 {
3605 case Lisp_String:
3606 mark_p = (live_string_p (m, po)
3607 && !STRING_MARKED_P ((struct Lisp_String *) po));
3608 break;
3609
3610 case Lisp_Cons:
3611 mark_p = (live_cons_p (m, po)
3612 && !XMARKBIT (XCONS (obj)->car));
3613 break;
3614
3615 case Lisp_Symbol:
2336fe58 3616 mark_p = (live_symbol_p (m, po) && !XSYMBOL (obj)->gcmarkbit);
182ff242
GM
3617 break;
3618
3619 case Lisp_Float:
ab6780cd 3620 mark_p = (live_float_p (m, po) && !FLOAT_MARKED_P (XFLOAT (obj)));
182ff242
GM
3621 break;
3622
3623 case Lisp_Vectorlike:
3624 /* Note: can't check GC_BUFFERP before we know it's a
3625 buffer because checking that dereferences the pointer
3626 PO which might point anywhere. */
3627 if (live_vector_p (m, po))
3ef06d12 3628 mark_p = !GC_SUBRP (obj) && !VECTOR_MARKED_P (XVECTOR (obj));
182ff242 3629 else if (live_buffer_p (m, po))
3ef06d12 3630 mark_p = GC_BUFFERP (obj) && !VECTOR_MARKED_P (XBUFFER (obj));
182ff242
GM
3631 break;
3632
3633 case Lisp_Misc:
2336fe58 3634 mark_p = (live_misc_p (m, po) && !XMARKER (obj)->gcmarkbit);
182ff242 3635 break;
6bbd7a29
GM
3636
3637 case Lisp_Int:
31d929e5 3638 case Lisp_Type_Limit:
6bbd7a29 3639 break;
182ff242
GM
3640 }
3641
3642 if (mark_p)
3643 {
3644#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3645 if (nzombies < MAX_ZOMBIES)
83fc9c63 3646 zombies[nzombies] = obj;
182ff242
GM
3647 ++nzombies;
3648#endif
3649 mark_object (&obj);
3650 }
3651 }
3652}
ece93c02
GM
3653
3654
3655/* If P points to Lisp data, mark that as live if it isn't already
3656 marked. */
3657
3658static INLINE void
3659mark_maybe_pointer (p)
3660 void *p;
3661{
3662 struct mem_node *m;
3663
3664 /* Quickly rule out some values which can't point to Lisp data. We
3665 assume that Lisp data is aligned on even addresses. */
3666 if ((EMACS_INT) p & 1)
3667 return;
177c0ea7 3668
ece93c02
GM
3669 m = mem_find (p);
3670 if (m != MEM_NIL)
3671 {
3672 Lisp_Object obj = Qnil;
177c0ea7 3673
ece93c02
GM
3674 switch (m->type)
3675 {
3676 case MEM_TYPE_NON_LISP:
2fe50224 3677 /* Nothing to do; not a pointer to Lisp memory. */
ece93c02 3678 break;
177c0ea7 3679
ece93c02 3680 case MEM_TYPE_BUFFER:
3ef06d12 3681 if (live_buffer_p (m, p) && !VECTOR_MARKED_P((struct buffer *)p))
ece93c02
GM
3682 XSETVECTOR (obj, p);
3683 break;
177c0ea7 3684
ece93c02
GM
3685 case MEM_TYPE_CONS:
3686 if (live_cons_p (m, p)
3687 && !XMARKBIT (((struct Lisp_Cons *) p)->car))
3688 XSETCONS (obj, p);
3689 break;
177c0ea7 3690
ece93c02
GM
3691 case MEM_TYPE_STRING:
3692 if (live_string_p (m, p)
3693 && !STRING_MARKED_P ((struct Lisp_String *) p))
3694 XSETSTRING (obj, p);
3695 break;
3696
3697 case MEM_TYPE_MISC:
2336fe58
SM
3698 if (live_misc_p (m, p) && !((struct Lisp_Free *) p)->gcmarkbit)
3699 XSETMISC (obj, p);
ece93c02 3700 break;
177c0ea7 3701
ece93c02 3702 case MEM_TYPE_SYMBOL:
2336fe58 3703 if (live_symbol_p (m, p) && !((struct Lisp_Symbol *) p)->gcmarkbit)
ece93c02
GM
3704 XSETSYMBOL (obj, p);
3705 break;
177c0ea7 3706
ece93c02 3707 case MEM_TYPE_FLOAT:
ab6780cd 3708 if (live_float_p (m, p) && !FLOAT_MARKED_P (p))
ece93c02
GM
3709 XSETFLOAT (obj, p);
3710 break;
177c0ea7 3711
ece93c02
GM
3712 case MEM_TYPE_VECTOR:
3713 case MEM_TYPE_PROCESS:
3714 case MEM_TYPE_HASH_TABLE:
3715 case MEM_TYPE_FRAME:
3716 case MEM_TYPE_WINDOW:
3717 if (live_vector_p (m, p))
3718 {
3719 Lisp_Object tem;
3720 XSETVECTOR (tem, p);
3ef06d12 3721 if (!GC_SUBRP (tem) && !VECTOR_MARKED_P (XVECTOR (tem)))
ece93c02
GM
3722 obj = tem;
3723 }
3724 break;
3725
3726 default:
3727 abort ();
3728 }
3729
3730 if (!GC_NILP (obj))
3731 mark_object (&obj);
3732 }
3733}
3734
3735
3736/* Mark Lisp objects referenced from the address range START..END. */
34400008 3737
177c0ea7 3738static void
34400008
GM
3739mark_memory (start, end)
3740 void *start, *end;
3741{
3742 Lisp_Object *p;
ece93c02 3743 void **pp;
34400008
GM
3744
3745#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3746 nzombies = 0;
3747#endif
3748
3749 /* Make START the pointer to the start of the memory region,
3750 if it isn't already. */
3751 if (end < start)
3752 {
3753 void *tem = start;
3754 start = end;
3755 end = tem;
3756 }
ece93c02
GM
3757
3758 /* Mark Lisp_Objects. */
34400008 3759 for (p = (Lisp_Object *) start; (void *) p < end; ++p)
182ff242 3760 mark_maybe_object (*p);
ece93c02
GM
3761
3762 /* Mark Lisp data pointed to. This is necessary because, in some
3763 situations, the C compiler optimizes Lisp objects away, so that
3764 only a pointer to them remains. Example:
3765
3766 DEFUN ("testme", Ftestme, Stestme, 0, 0, 0, "")
7ee72033 3767 ()
ece93c02
GM
3768 {
3769 Lisp_Object obj = build_string ("test");
3770 struct Lisp_String *s = XSTRING (obj);
3771 Fgarbage_collect ();
3772 fprintf (stderr, "test `%s'\n", s->data);
3773 return Qnil;
3774 }
3775
3776 Here, `obj' isn't really used, and the compiler optimizes it
3777 away. The only reference to the life string is through the
3778 pointer `s'. */
177c0ea7 3779
ece93c02
GM
3780 for (pp = (void **) start; (void *) pp < end; ++pp)
3781 mark_maybe_pointer (*pp);
182ff242
GM
3782}
3783
30f637f8
DL
3784/* setjmp will work with GCC unless NON_SAVING_SETJMP is defined in
3785 the GCC system configuration. In gcc 3.2, the only systems for
3786 which this is so are i386-sco5 non-ELF, i386-sysv3 (maybe included
3787 by others?) and ns32k-pc532-min. */
182ff242
GM
3788
3789#if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
3790
3791static int setjmp_tested_p, longjmps_done;
3792
3793#define SETJMP_WILL_LIKELY_WORK "\
3794\n\
3795Emacs garbage collector has been changed to use conservative stack\n\
3796marking. Emacs has determined that the method it uses to do the\n\
3797marking will likely work on your system, but this isn't sure.\n\
3798\n\
3799If you are a system-programmer, or can get the help of a local wizard\n\
3800who is, please take a look at the function mark_stack in alloc.c, and\n\
3801verify that the methods used are appropriate for your system.\n\
3802\n\
d191623b 3803Please mail the result to <emacs-devel@gnu.org>.\n\
182ff242
GM
3804"
3805
3806#define SETJMP_WILL_NOT_WORK "\
3807\n\
3808Emacs garbage collector has been changed to use conservative stack\n\
3809marking. Emacs has determined that the default method it uses to do the\n\
3810marking will not work on your system. We will need a system-dependent\n\
3811solution for your system.\n\
3812\n\
3813Please take a look at the function mark_stack in alloc.c, and\n\
3814try to find a way to make it work on your system.\n\
30f637f8
DL
3815\n\
3816Note that you may get false negatives, depending on the compiler.\n\
3817In particular, you need to use -O with GCC for this test.\n\
3818\n\
d191623b 3819Please mail the result to <emacs-devel@gnu.org>.\n\
182ff242
GM
3820"
3821
3822
3823/* Perform a quick check if it looks like setjmp saves registers in a
3824 jmp_buf. Print a message to stderr saying so. When this test
3825 succeeds, this is _not_ a proof that setjmp is sufficient for
3826 conservative stack marking. Only the sources or a disassembly
3827 can prove that. */
3828
3829static void
3830test_setjmp ()
3831{
3832 char buf[10];
3833 register int x;
3834 jmp_buf jbuf;
3835 int result = 0;
3836
3837 /* Arrange for X to be put in a register. */
3838 sprintf (buf, "1");
3839 x = strlen (buf);
3840 x = 2 * x - 1;
3841
3842 setjmp (jbuf);
3843 if (longjmps_done == 1)
34400008 3844 {
182ff242 3845 /* Came here after the longjmp at the end of the function.
34400008 3846
182ff242
GM
3847 If x == 1, the longjmp has restored the register to its
3848 value before the setjmp, and we can hope that setjmp
3849 saves all such registers in the jmp_buf, although that
3850 isn't sure.
34400008 3851
182ff242
GM
3852 For other values of X, either something really strange is
3853 taking place, or the setjmp just didn't save the register. */
3854
3855 if (x == 1)
3856 fprintf (stderr, SETJMP_WILL_LIKELY_WORK);
3857 else
3858 {
3859 fprintf (stderr, SETJMP_WILL_NOT_WORK);
3860 exit (1);
34400008
GM
3861 }
3862 }
182ff242
GM
3863
3864 ++longjmps_done;
3865 x = 2;
3866 if (longjmps_done == 1)
3867 longjmp (jbuf, 1);
34400008
GM
3868}
3869
182ff242
GM
3870#endif /* not GC_SAVE_REGISTERS_ON_STACK && not GC_SETJMP_WORKS */
3871
34400008
GM
3872
3873#if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
3874
3875/* Abort if anything GCPRO'd doesn't survive the GC. */
3876
3877static void
3878check_gcpros ()
3879{
3880 struct gcpro *p;
3881 int i;
3882
3883 for (p = gcprolist; p; p = p->next)
3884 for (i = 0; i < p->nvars; ++i)
3885 if (!survives_gc_p (p->var[i]))
92cc28b2
SM
3886 /* FIXME: It's not necessarily a bug. It might just be that the
3887 GCPRO is unnecessary or should release the object sooner. */
34400008
GM
3888 abort ();
3889}
3890
3891#elif GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3892
3893static void
3894dump_zombies ()
3895{
3896 int i;
3897
3898 fprintf (stderr, "\nZombies kept alive = %d:\n", nzombies);
3899 for (i = 0; i < min (MAX_ZOMBIES, nzombies); ++i)
3900 {
3901 fprintf (stderr, " %d = ", i);
3902 debug_print (zombies[i]);
3903 }
3904}
3905
3906#endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
3907
3908
182ff242
GM
3909/* Mark live Lisp objects on the C stack.
3910
3911 There are several system-dependent problems to consider when
3912 porting this to new architectures:
3913
3914 Processor Registers
3915
3916 We have to mark Lisp objects in CPU registers that can hold local
3917 variables or are used to pass parameters.
3918
3919 If GC_SAVE_REGISTERS_ON_STACK is defined, it should expand to
3920 something that either saves relevant registers on the stack, or
3921 calls mark_maybe_object passing it each register's contents.
3922
3923 If GC_SAVE_REGISTERS_ON_STACK is not defined, the current
3924 implementation assumes that calling setjmp saves registers we need
3925 to see in a jmp_buf which itself lies on the stack. This doesn't
3926 have to be true! It must be verified for each system, possibly
3927 by taking a look at the source code of setjmp.
3928
3929 Stack Layout
3930
3931 Architectures differ in the way their processor stack is organized.
3932 For example, the stack might look like this
3933
3934 +----------------+
3935 | Lisp_Object | size = 4
3936 +----------------+
3937 | something else | size = 2
3938 +----------------+
3939 | Lisp_Object | size = 4
3940 +----------------+
3941 | ... |
3942
3943 In such a case, not every Lisp_Object will be aligned equally. To
3944 find all Lisp_Object on the stack it won't be sufficient to walk
3945 the stack in steps of 4 bytes. Instead, two passes will be
3946 necessary, one starting at the start of the stack, and a second
3947 pass starting at the start of the stack + 2. Likewise, if the
3948 minimal alignment of Lisp_Objects on the stack is 1, four passes
3949 would be necessary, each one starting with one byte more offset
3950 from the stack start.
3951
3952 The current code assumes by default that Lisp_Objects are aligned
3953 equally on the stack. */
34400008
GM
3954
3955static void
3956mark_stack ()
3957{
630909a5 3958 int i;
34400008 3959 jmp_buf j;
6bbd7a29 3960 volatile int stack_grows_down_p = (char *) &j > (char *) stack_base;
34400008
GM
3961 void *end;
3962
3963 /* This trick flushes the register windows so that all the state of
3964 the process is contained in the stack. */
ab6780cd 3965 /* Fixme: Code in the Boehm GC suggests flushing (with `flushrs') is
422eec7e
DL
3966 needed on ia64 too. See mach_dep.c, where it also says inline
3967 assembler doesn't work with relevant proprietary compilers. */
34400008
GM
3968#ifdef sparc
3969 asm ("ta 3");
3970#endif
177c0ea7 3971
34400008
GM
3972 /* Save registers that we need to see on the stack. We need to see
3973 registers used to hold register variables and registers used to
3974 pass parameters. */
3975#ifdef GC_SAVE_REGISTERS_ON_STACK
3976 GC_SAVE_REGISTERS_ON_STACK (end);
182ff242 3977#else /* not GC_SAVE_REGISTERS_ON_STACK */
177c0ea7 3978
182ff242
GM
3979#ifndef GC_SETJMP_WORKS /* If it hasn't been checked yet that
3980 setjmp will definitely work, test it
3981 and print a message with the result
3982 of the test. */
3983 if (!setjmp_tested_p)
3984 {
3985 setjmp_tested_p = 1;
3986 test_setjmp ();
3987 }
3988#endif /* GC_SETJMP_WORKS */
177c0ea7 3989
34400008
GM
3990 setjmp (j);
3991 end = stack_grows_down_p ? (char *) &j + sizeof j : (char *) &j;
182ff242 3992#endif /* not GC_SAVE_REGISTERS_ON_STACK */
34400008
GM
3993
3994 /* This assumes that the stack is a contiguous region in memory. If
182ff242
GM
3995 that's not the case, something has to be done here to iterate
3996 over the stack segments. */
630909a5 3997#ifndef GC_LISP_OBJECT_ALIGNMENT
422eec7e
DL
3998#ifdef __GNUC__
3999#define GC_LISP_OBJECT_ALIGNMENT __alignof__ (Lisp_Object)
4000#else
630909a5 4001#define GC_LISP_OBJECT_ALIGNMENT sizeof (Lisp_Object)
422eec7e 4002#endif
182ff242 4003#endif
24452cd5 4004 for (i = 0; i < sizeof (Lisp_Object); i += GC_LISP_OBJECT_ALIGNMENT)
630909a5 4005 mark_memory ((char *) stack_base + i, end);
34400008
GM
4006
4007#if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
4008 check_gcpros ();
4009#endif
4010}
4011
4012
4013#endif /* GC_MARK_STACK != 0 */
4014
4015
4016\f
2e471eb5
GM
4017/***********************************************************************
4018 Pure Storage Management
4019 ***********************************************************************/
4020
1f0b3fd2
GM
4021/* Allocate room for SIZE bytes from pure Lisp storage and return a
4022 pointer to it. TYPE is the Lisp type for which the memory is
4023 allocated. TYPE < 0 means it's not used for a Lisp object.
4024
4025 If store_pure_type_info is set and TYPE is >= 0, the type of
4026 the allocated object is recorded in pure_types. */
4027
4028static POINTER_TYPE *
4029pure_alloc (size, type)
4030 size_t size;
4031 int type;
4032{
1f0b3fd2 4033 POINTER_TYPE *result;
44117420 4034 size_t alignment = sizeof (EMACS_INT);
1f0b3fd2
GM
4035
4036 /* Give Lisp_Floats an extra alignment. */
4037 if (type == Lisp_Float)
4038 {
1f0b3fd2
GM
4039#if defined __GNUC__ && __GNUC__ >= 2
4040 alignment = __alignof (struct Lisp_Float);
4041#else
4042 alignment = sizeof (struct Lisp_Float);
4043#endif
9e713715 4044 }
1f0b3fd2 4045
44117420 4046 again:
ab6780cd 4047 result = ALIGN (purebeg + pure_bytes_used, alignment);
44117420
KS
4048 pure_bytes_used = ((char *)result - (char *)purebeg) + size;
4049
4050 if (pure_bytes_used <= pure_size)
4051 return result;
4052
4053 /* Don't allocate a large amount here,
4054 because it might get mmap'd and then its address
4055 might not be usable. */
4056 purebeg = (char *) xmalloc (10000);
4057 pure_size = 10000;
4058 pure_bytes_used_before_overflow += pure_bytes_used - size;
4059 pure_bytes_used = 0;
4060 goto again;
1f0b3fd2
GM
4061}
4062
4063
852f8cdc 4064/* Print a warning if PURESIZE is too small. */
9e713715
GM
4065
4066void
4067check_pure_size ()
4068{
4069 if (pure_bytes_used_before_overflow)
a4d35afd
SM
4070 message ("Pure Lisp storage overflow (approx. %d bytes needed)",
4071 (int) (pure_bytes_used + pure_bytes_used_before_overflow));
9e713715
GM
4072}
4073
4074
2e471eb5
GM
4075/* Return a string allocated in pure space. DATA is a buffer holding
4076 NCHARS characters, and NBYTES bytes of string data. MULTIBYTE
4077 non-zero means make the result string multibyte.
1a4f1e2c 4078
2e471eb5
GM
4079 Must get an error if pure storage is full, since if it cannot hold
4080 a large string it may be able to hold conses that point to that
4081 string; then the string is not protected from gc. */
7146af97
JB
4082
4083Lisp_Object
2e471eb5 4084make_pure_string (data, nchars, nbytes, multibyte)
7146af97 4085 char *data;
2e471eb5 4086 int nchars, nbytes;
c0696668 4087 int multibyte;
7146af97 4088{
2e471eb5
GM
4089 Lisp_Object string;
4090 struct Lisp_String *s;
c0696668 4091
1f0b3fd2
GM
4092 s = (struct Lisp_String *) pure_alloc (sizeof *s, Lisp_String);
4093 s->data = (unsigned char *) pure_alloc (nbytes + 1, -1);
2e471eb5
GM
4094 s->size = nchars;
4095 s->size_byte = multibyte ? nbytes : -1;
4096 bcopy (data, s->data, nbytes);
4097 s->data[nbytes] = '\0';
4098 s->intervals = NULL_INTERVAL;
2e471eb5
GM
4099 XSETSTRING (string, s);
4100 return string;
7146af97
JB
4101}
4102
2e471eb5 4103
34400008
GM
4104/* Return a cons allocated from pure space. Give it pure copies
4105 of CAR as car and CDR as cdr. */
4106
7146af97
JB
4107Lisp_Object
4108pure_cons (car, cdr)
4109 Lisp_Object car, cdr;
4110{
4111 register Lisp_Object new;
1f0b3fd2 4112 struct Lisp_Cons *p;
7146af97 4113
1f0b3fd2
GM
4114 p = (struct Lisp_Cons *) pure_alloc (sizeof *p, Lisp_Cons);
4115 XSETCONS (new, p);
f3fbd155
KR
4116 XSETCAR (new, Fpurecopy (car));
4117 XSETCDR (new, Fpurecopy (cdr));
7146af97
JB
4118 return new;
4119}
4120
7146af97 4121
34400008
GM
4122/* Value is a float object with value NUM allocated from pure space. */
4123
7146af97
JB
4124Lisp_Object
4125make_pure_float (num)
4126 double num;
4127{
4128 register Lisp_Object new;
1f0b3fd2 4129 struct Lisp_Float *p;
7146af97 4130
1f0b3fd2
GM
4131 p = (struct Lisp_Float *) pure_alloc (sizeof *p, Lisp_Float);
4132 XSETFLOAT (new, p);
70949dac 4133 XFLOAT_DATA (new) = num;
7146af97
JB
4134 return new;
4135}
4136
34400008
GM
4137
4138/* Return a vector with room for LEN Lisp_Objects allocated from
4139 pure space. */
4140
7146af97
JB
4141Lisp_Object
4142make_pure_vector (len)
42607681 4143 EMACS_INT len;
7146af97 4144{
1f0b3fd2
GM
4145 Lisp_Object new;
4146 struct Lisp_Vector *p;
4147 size_t size = sizeof *p + (len - 1) * sizeof (Lisp_Object);
7146af97 4148
1f0b3fd2
GM
4149 p = (struct Lisp_Vector *) pure_alloc (size, Lisp_Vectorlike);
4150 XSETVECTOR (new, p);
7146af97
JB
4151 XVECTOR (new)->size = len;
4152 return new;
4153}
4154
34400008 4155
7146af97 4156DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
7ee72033 4157 doc: /* Make a copy of OBJECT in pure storage.
228299fa 4158Recursively copies contents of vectors and cons cells.
7ee72033
MB
4159Does not copy symbols. Copies strings without text properties. */)
4160 (obj)
7146af97
JB
4161 register Lisp_Object obj;
4162{
265a9e55 4163 if (NILP (Vpurify_flag))
7146af97
JB
4164 return obj;
4165
1f0b3fd2 4166 if (PURE_POINTER_P (XPNTR (obj)))
7146af97
JB
4167 return obj;
4168
d6dd74bb 4169 if (CONSP (obj))
70949dac 4170 return pure_cons (XCAR (obj), XCDR (obj));
d6dd74bb 4171 else if (FLOATP (obj))
70949dac 4172 return make_pure_float (XFLOAT_DATA (obj));
d6dd74bb 4173 else if (STRINGP (obj))
d5db4077
KR
4174 return make_pure_string (SDATA (obj), SCHARS (obj),
4175 SBYTES (obj),
c0696668 4176 STRING_MULTIBYTE (obj));
d6dd74bb
KH
4177 else if (COMPILEDP (obj) || VECTORP (obj))
4178 {
4179 register struct Lisp_Vector *vec;
4180 register int i, size;
4181
4182 size = XVECTOR (obj)->size;
7d535c68
KH
4183 if (size & PSEUDOVECTOR_FLAG)
4184 size &= PSEUDOVECTOR_SIZE_MASK;
01a4d290 4185 vec = XVECTOR (make_pure_vector ((EMACS_INT) size));
d6dd74bb
KH
4186 for (i = 0; i < size; i++)
4187 vec->contents[i] = Fpurecopy (XVECTOR (obj)->contents[i]);
4188 if (COMPILEDP (obj))
4189 XSETCOMPILED (obj, vec);
4190 else
4191 XSETVECTOR (obj, vec);
7146af97
JB
4192 return obj;
4193 }
d6dd74bb
KH
4194 else if (MARKERP (obj))
4195 error ("Attempt to copy a marker to pure storage");
6bbd7a29
GM
4196
4197 return obj;
7146af97 4198}
2e471eb5 4199
34400008 4200
7146af97 4201\f
34400008
GM
4202/***********************************************************************
4203 Protection from GC
4204 ***********************************************************************/
4205
2e471eb5
GM
4206/* Put an entry in staticvec, pointing at the variable with address
4207 VARADDRESS. */
7146af97
JB
4208
4209void
4210staticpro (varaddress)
4211 Lisp_Object *varaddress;
4212{
4213 staticvec[staticidx++] = varaddress;
4214 if (staticidx >= NSTATICS)
4215 abort ();
4216}
4217
4218struct catchtag
2e471eb5 4219{
7146af97
JB
4220 Lisp_Object tag;
4221 Lisp_Object val;
4222 struct catchtag *next;
2e471eb5 4223};
7146af97
JB
4224
4225struct backtrace
2e471eb5
GM
4226{
4227 struct backtrace *next;
4228 Lisp_Object *function;
4229 Lisp_Object *args; /* Points to vector of args. */
4230 int nargs; /* Length of vector. */
4231 /* If nargs is UNEVALLED, args points to slot holding list of
4232 unevalled args. */
4233 char evalargs;
4234};
4235
34400008 4236
7146af97 4237\f
34400008
GM
4238/***********************************************************************
4239 Protection from GC
4240 ***********************************************************************/
1a4f1e2c 4241
e8197642
RS
4242/* Temporarily prevent garbage collection. */
4243
4244int
4245inhibit_garbage_collection ()
4246{
aed13378 4247 int count = SPECPDL_INDEX ();
54defd0d
AS
4248 int nbits = min (VALBITS, BITS_PER_INT);
4249
4250 specbind (Qgc_cons_threshold, make_number (((EMACS_INT) 1 << (nbits - 1)) - 1));
e8197642
RS
4251 return count;
4252}
4253
34400008 4254
7146af97 4255DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
7ee72033 4256 doc: /* Reclaim storage for Lisp objects no longer needed.
228299fa
GM
4257Returns info on amount of space in use:
4258 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)
4259 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS
4260 (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS)
4261 (USED-STRINGS . FREE-STRINGS))
4262Garbage collection happens automatically if you cons more than
7ee72033
MB
4263`gc-cons-threshold' bytes of Lisp data since previous garbage collection. */)
4264 ()
7146af97 4265{
7146af97
JB
4266 register struct specbinding *bind;
4267 struct catchtag *catch;
4268 struct handler *handler;
4269 register struct backtrace *backlist;
7146af97
JB
4270 char stack_top_variable;
4271 register int i;
6efc7df7 4272 int message_p;
96117bc7 4273 Lisp_Object total[8];
331379bf 4274 int count = SPECPDL_INDEX ();
2c5bd608
DL
4275 EMACS_TIME t1, t2, t3;
4276
3de0effb
RS
4277 if (abort_on_gc)
4278 abort ();
4279
2c5bd608 4280 EMACS_GET_TIME (t1);
7146af97 4281
9e713715
GM
4282 /* Can't GC if pure storage overflowed because we can't determine
4283 if something is a pure object or not. */
4284 if (pure_bytes_used_before_overflow)
4285 return Qnil;
4286
58595309
KH
4287 /* In case user calls debug_print during GC,
4288 don't let that cause a recursive GC. */
4289 consing_since_gc = 0;
4290
6efc7df7
GM
4291 /* Save what's currently displayed in the echo area. */
4292 message_p = push_message ();
c55b0da6 4293 record_unwind_protect (pop_message_unwind, Qnil);
41c28a37 4294
7146af97
JB
4295 /* Save a copy of the contents of the stack, for debugging. */
4296#if MAX_SAVE_STACK > 0
265a9e55 4297 if (NILP (Vpurify_flag))
7146af97
JB
4298 {
4299 i = &stack_top_variable - stack_bottom;
4300 if (i < 0) i = -i;
4301 if (i < MAX_SAVE_STACK)
4302 {
4303 if (stack_copy == 0)
9ac0d9e0 4304 stack_copy = (char *) xmalloc (stack_copy_size = i);
7146af97 4305 else if (stack_copy_size < i)
9ac0d9e0 4306 stack_copy = (char *) xrealloc (stack_copy, (stack_copy_size = i));
7146af97
JB
4307 if (stack_copy)
4308 {
42607681 4309 if ((EMACS_INT) (&stack_top_variable - stack_bottom) > 0)
7146af97
JB
4310 bcopy (stack_bottom, stack_copy, i);
4311 else
4312 bcopy (&stack_top_variable, stack_copy, i);
4313 }
4314 }
4315 }
4316#endif /* MAX_SAVE_STACK > 0 */
4317
299585ee 4318 if (garbage_collection_messages)
691c4285 4319 message1_nolog ("Garbage collecting...");
7146af97 4320
6e0fca1d
RS
4321 BLOCK_INPUT;
4322
eec7b73d
RS
4323 shrink_regexp_cache ();
4324
4929a878 4325 /* Don't keep undo information around forever. */
7146af97
JB
4326 {
4327 register struct buffer *nextb = all_buffers;
4328
4329 while (nextb)
4330 {
ffd56f97
JB
4331 /* If a buffer's undo list is Qt, that means that undo is
4332 turned off in that buffer. Calling truncate_undo_list on
4333 Qt tends to return NULL, which effectively turns undo back on.
4334 So don't call truncate_undo_list if undo_list is Qt. */
4335 if (! EQ (nextb->undo_list, Qt))
177c0ea7 4336 nextb->undo_list
502b9b64
JB
4337 = truncate_undo_list (nextb->undo_list, undo_limit,
4338 undo_strong_limit);
e0fead5d
AI
4339
4340 /* Shrink buffer gaps, but skip indirect and dead buffers. */
4341 if (nextb->base_buffer == 0 && !NILP (nextb->name))
4342 {
4343 /* If a buffer's gap size is more than 10% of the buffer
4344 size, or larger than 2000 bytes, then shrink it
4345 accordingly. Keep a minimum size of 20 bytes. */
4346 int size = min (2000, max (20, (nextb->text->z_byte / 10)));
4347
4348 if (nextb->text->gap_size > size)
4349 {
4350 struct buffer *save_current = current_buffer;
4351 current_buffer = nextb;
4352 make_gap (-(nextb->text->gap_size - size));
4353 current_buffer = save_current;
4354 }
4355 }
4356
7146af97
JB
4357 nextb = nextb->next;
4358 }
4359 }
4360
4361 gc_in_progress = 1;
4362
c23baf9f 4363 /* clear_marks (); */
7146af97 4364
7146af97
JB
4365 /* Mark all the special slots that serve as the roots of accessibility.
4366
4367 Usually the special slots to mark are contained in particular structures.
4368 Then we know no slot is marked twice because the structures don't overlap.
4369 In some cases, the structures point to the slots to be marked.
4370 For these, we use MARKBIT to avoid double marking of the slot. */
4371
4372 for (i = 0; i < staticidx; i++)
4373 mark_object (staticvec[i]);
34400008
GM
4374
4375#if (GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS \
4376 || GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS)
4377 mark_stack ();
4378#else
acf5f7d3
SM
4379 {
4380 register struct gcpro *tail;
4381 for (tail = gcprolist; tail; tail = tail->next)
4382 for (i = 0; i < tail->nvars; i++)
4383 if (!XMARKBIT (tail->var[i]))
4384 {
4385 /* Explicit casting prevents compiler warning about
4386 discarding the `volatile' qualifier. */
4387 mark_object ((Lisp_Object *)&tail->var[i]);
4388 XMARK (tail->var[i]);
4389 }
4390 }
34400008 4391#endif
177c0ea7 4392
630686c8 4393 mark_byte_stack ();
7146af97
JB
4394 for (bind = specpdl; bind != specpdl_ptr; bind++)
4395 {
fa42e88f
RS
4396 /* These casts avoid a warning for discarding `volatile'. */
4397 mark_object ((Lisp_Object *) &bind->symbol);
4398 mark_object ((Lisp_Object *) &bind->old_value);
7146af97
JB
4399 }
4400 for (catch = catchlist; catch; catch = catch->next)
4401 {
4402 mark_object (&catch->tag);
4403 mark_object (&catch->val);
177c0ea7 4404 }
7146af97
JB
4405 for (handler = handlerlist; handler; handler = handler->next)
4406 {
4407 mark_object (&handler->handler);
4408 mark_object (&handler->var);
177c0ea7 4409 }
7146af97
JB
4410 for (backlist = backtrace_list; backlist; backlist = backlist->next)
4411 {
4412 if (!XMARKBIT (*backlist->function))
4413 {
4414 mark_object (backlist->function);
4415 XMARK (*backlist->function);
4416 }
4417 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
4418 i = 0;
4419 else
4420 i = backlist->nargs - 1;
4421 for (; i >= 0; i--)
4422 if (!XMARKBIT (backlist->args[i]))
4423 {
4424 mark_object (&backlist->args[i]);
4425 XMARK (backlist->args[i]);
4426 }
177c0ea7 4427 }
b875d3f7 4428 mark_kboards ();
7146af97 4429
4c315bda
RS
4430 /* Look thru every buffer's undo list
4431 for elements that update markers that were not marked,
4432 and delete them. */
4433 {
4434 register struct buffer *nextb = all_buffers;
4435
4436 while (nextb)
4437 {
4438 /* If a buffer's undo list is Qt, that means that undo is
4439 turned off in that buffer. Calling truncate_undo_list on
4440 Qt tends to return NULL, which effectively turns undo back on.
4441 So don't call truncate_undo_list if undo_list is Qt. */
4442 if (! EQ (nextb->undo_list, Qt))
4443 {
4444 Lisp_Object tail, prev;
4445 tail = nextb->undo_list;
4446 prev = Qnil;
4447 while (CONSP (tail))
4448 {
70949dac
KR
4449 if (GC_CONSP (XCAR (tail))
4450 && GC_MARKERP (XCAR (XCAR (tail)))
2336fe58 4451 && !XMARKER (XCAR (XCAR (tail)))->gcmarkbit)
4c315bda
RS
4452 {
4453 if (NILP (prev))
70949dac 4454 nextb->undo_list = tail = XCDR (tail);
4c315bda 4455 else
f3fbd155
KR
4456 {
4457 tail = XCDR (tail);
4458 XSETCDR (prev, tail);
4459 }
4c315bda
RS
4460 }
4461 else
4462 {
4463 prev = tail;
70949dac 4464 tail = XCDR (tail);
4c315bda
RS
4465 }
4466 }
4467 }
4468
4469 nextb = nextb->next;
4470 }
4471 }
4472
34400008
GM
4473#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4474 mark_stack ();
4475#endif
4476
488dd4c4
JD
4477#ifdef USE_GTK
4478 {
4479 extern void xg_mark_data ();
4480 xg_mark_data ();
4481 }
4482#endif
4483
7146af97
JB
4484 gc_sweep ();
4485
4486 /* Clear the mark bits that we set in certain root slots. */
4487
34400008
GM
4488#if (GC_MARK_STACK == GC_USE_GCPROS_AS_BEFORE \
4489 || GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES)
d22be14d
AS
4490 {
4491 register struct gcpro *tail;
4492
4493 for (tail = gcprolist; tail; tail = tail->next)
4494 for (i = 0; i < tail->nvars; i++)
4495 XUNMARK (tail->var[i]);
4496 }
34400008 4497#endif
177c0ea7 4498
033a5fa3 4499 unmark_byte_stack ();
7146af97
JB
4500 for (backlist = backtrace_list; backlist; backlist = backlist->next)
4501 {
4502 XUNMARK (*backlist->function);
4503 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
4504 i = 0;
4505 else
4506 i = backlist->nargs - 1;
4507 for (; i >= 0; i--)
4508 XUNMARK (backlist->args[i]);
177c0ea7 4509 }
3ef06d12
SM
4510 VECTOR_UNMARK (&buffer_defaults);
4511 VECTOR_UNMARK (&buffer_local_symbols);
7146af97 4512
34400008
GM
4513#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES && 0
4514 dump_zombies ();
4515#endif
4516
6e0fca1d
RS
4517 UNBLOCK_INPUT;
4518
c23baf9f 4519 /* clear_marks (); */
7146af97
JB
4520 gc_in_progress = 0;
4521
4522 consing_since_gc = 0;
4523 if (gc_cons_threshold < 10000)
4524 gc_cons_threshold = 10000;
4525
299585ee
RS
4526 if (garbage_collection_messages)
4527 {
6efc7df7
GM
4528 if (message_p || minibuf_level > 0)
4529 restore_message ();
299585ee
RS
4530 else
4531 message1_nolog ("Garbage collecting...done");
4532 }
7146af97 4533
98edb5ff 4534 unbind_to (count, Qnil);
2e471eb5
GM
4535
4536 total[0] = Fcons (make_number (total_conses),
4537 make_number (total_free_conses));
4538 total[1] = Fcons (make_number (total_symbols),
4539 make_number (total_free_symbols));
4540 total[2] = Fcons (make_number (total_markers),
4541 make_number (total_free_markers));
96117bc7
GM
4542 total[3] = make_number (total_string_size);
4543 total[4] = make_number (total_vector_size);
4544 total[5] = Fcons (make_number (total_floats),
2e471eb5 4545 make_number (total_free_floats));
96117bc7 4546 total[6] = Fcons (make_number (total_intervals),
2e471eb5 4547 make_number (total_free_intervals));
96117bc7 4548 total[7] = Fcons (make_number (total_strings),
2e471eb5
GM
4549 make_number (total_free_strings));
4550
34400008 4551#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
7146af97 4552 {
34400008
GM
4553 /* Compute average percentage of zombies. */
4554 double nlive = 0;
177c0ea7 4555
34400008 4556 for (i = 0; i < 7; ++i)
83fc9c63
DL
4557 if (CONSP (total[i]))
4558 nlive += XFASTINT (XCAR (total[i]));
34400008
GM
4559
4560 avg_live = (avg_live * ngcs + nlive) / (ngcs + 1);
4561 max_live = max (nlive, max_live);
4562 avg_zombies = (avg_zombies * ngcs + nzombies) / (ngcs + 1);
4563 max_zombies = max (nzombies, max_zombies);
4564 ++ngcs;
4565 }
4566#endif
7146af97 4567
9e713715
GM
4568 if (!NILP (Vpost_gc_hook))
4569 {
4570 int count = inhibit_garbage_collection ();
4571 safe_run_hooks (Qpost_gc_hook);
4572 unbind_to (count, Qnil);
4573 }
2c5bd608
DL
4574
4575 /* Accumulate statistics. */
4576 EMACS_GET_TIME (t2);
4577 EMACS_SUB_TIME (t3, t2, t1);
4578 if (FLOATP (Vgc_elapsed))
69ab9f85
SM
4579 Vgc_elapsed = make_float (XFLOAT_DATA (Vgc_elapsed) +
4580 EMACS_SECS (t3) +
4581 EMACS_USECS (t3) * 1.0e-6);
2c5bd608
DL
4582 gcs_done++;
4583
96117bc7 4584 return Flist (sizeof total / sizeof *total, total);
7146af97 4585}
34400008 4586
41c28a37 4587
3770920e
GM
4588/* Mark Lisp objects in glyph matrix MATRIX. Currently the
4589 only interesting objects referenced from glyphs are strings. */
41c28a37
GM
4590
4591static void
4592mark_glyph_matrix (matrix)
4593 struct glyph_matrix *matrix;
4594{
4595 struct glyph_row *row = matrix->rows;
4596 struct glyph_row *end = row + matrix->nrows;
4597
2e471eb5
GM
4598 for (; row < end; ++row)
4599 if (row->enabled_p)
4600 {
4601 int area;
4602 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
4603 {
4604 struct glyph *glyph = row->glyphs[area];
4605 struct glyph *end_glyph = glyph + row->used[area];
177c0ea7 4606
2e471eb5
GM
4607 for (; glyph < end_glyph; ++glyph)
4608 if (GC_STRINGP (glyph->object)
4609 && !STRING_MARKED_P (XSTRING (glyph->object)))
4610 mark_object (&glyph->object);
4611 }
4612 }
41c28a37
GM
4613}
4614
34400008 4615
41c28a37
GM
4616/* Mark Lisp faces in the face cache C. */
4617
4618static void
4619mark_face_cache (c)
4620 struct face_cache *c;
4621{
4622 if (c)
4623 {
4624 int i, j;
4625 for (i = 0; i < c->used; ++i)
4626 {
4627 struct face *face = FACE_FROM_ID (c->f, i);
4628
4629 if (face)
4630 {
4631 for (j = 0; j < LFACE_VECTOR_SIZE; ++j)
4632 mark_object (&face->lface[j]);
41c28a37
GM
4633 }
4634 }
4635 }
4636}
4637
4638
4639#ifdef HAVE_WINDOW_SYSTEM
4640
4641/* Mark Lisp objects in image IMG. */
4642
4643static void
4644mark_image (img)
4645 struct image *img;
4646{
4647 mark_object (&img->spec);
177c0ea7 4648
3e60b029 4649 if (!NILP (img->data.lisp_val))
41c28a37
GM
4650 mark_object (&img->data.lisp_val);
4651}
4652
4653
4654/* Mark Lisp objects in image cache of frame F. It's done this way so
4655 that we don't have to include xterm.h here. */
4656
4657static void
4658mark_image_cache (f)
4659 struct frame *f;
4660{
4661 forall_images_in_image_cache (f, mark_image);
4662}
4663
4664#endif /* HAVE_X_WINDOWS */
4665
4666
7146af97 4667\f
1a4f1e2c 4668/* Mark reference to a Lisp_Object.
2e471eb5
GM
4669 If the object referred to has not been seen yet, recursively mark
4670 all the references contained in it. */
7146af97 4671
785cd37f
RS
4672#define LAST_MARKED_SIZE 500
4673Lisp_Object *last_marked[LAST_MARKED_SIZE];
4674int last_marked_index;
4675
1342fc6f
RS
4676/* For debugging--call abort when we cdr down this many
4677 links of a list, in mark_object. In debugging,
4678 the call to abort will hit a breakpoint.
4679 Normally this is zero and the check never goes off. */
4680int mark_object_loop_halt;
4681
41c28a37 4682void
436c5811
RS
4683mark_object (argptr)
4684 Lisp_Object *argptr;
7146af97 4685{
436c5811 4686 Lisp_Object *objptr = argptr;
7146af97 4687 register Lisp_Object obj;
4f5c1376
GM
4688#ifdef GC_CHECK_MARKED_OBJECTS
4689 void *po;
4690 struct mem_node *m;
4691#endif
1342fc6f 4692 int cdr_count = 0;
7146af97 4693
9149e743 4694 loop:
7146af97 4695 obj = *objptr;
9149e743 4696 loop2:
7146af97
JB
4697 XUNMARK (obj);
4698
1f0b3fd2 4699 if (PURE_POINTER_P (XPNTR (obj)))
7146af97
JB
4700 return;
4701
785cd37f
RS
4702 last_marked[last_marked_index++] = objptr;
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)
c70bbf06 4807 mark_object (&ptr->contents[i]);
169ee243
RS
4808 }
4809 /* This cast should be unnecessary, but some Mips compiler complains
4810 (MIPS-ABI + SysVR4, DC/OSx, etc). */
c70bbf06 4811 objptr = (Lisp_Object *) &ptr->contents[COMPILED_CONSTANTS];
169ee243
RS
4812 goto loop;
4813 }
169ee243
RS
4814 else if (GC_FRAMEP (obj))
4815 {
c70bbf06 4816 register struct frame *ptr = XFRAME (obj);
169ee243 4817
3ef06d12
SM
4818 if (VECTOR_MARKED_P (ptr)) break; /* Already marked */
4819 VECTOR_MARK (ptr); /* Else mark it */
169ee243 4820
4f5c1376 4821 CHECK_LIVE (live_vector_p);
169ee243 4822 mark_object (&ptr->name);
894a9d16 4823 mark_object (&ptr->icon_name);
aba6deb8 4824 mark_object (&ptr->title);
169ee243
RS
4825 mark_object (&ptr->focus_frame);
4826 mark_object (&ptr->selected_window);
4827 mark_object (&ptr->minibuffer_window);
4828 mark_object (&ptr->param_alist);
4829 mark_object (&ptr->scroll_bars);
4830 mark_object (&ptr->condemned_scroll_bars);
4831 mark_object (&ptr->menu_bar_items);
4832 mark_object (&ptr->face_alist);
4833 mark_object (&ptr->menu_bar_vector);
4834 mark_object (&ptr->buffer_predicate);
a0e1f185 4835 mark_object (&ptr->buffer_list);
41c28a37 4836 mark_object (&ptr->menu_bar_window);
9ea173e8 4837 mark_object (&ptr->tool_bar_window);
41c28a37
GM
4838 mark_face_cache (ptr->face_cache);
4839#ifdef HAVE_WINDOW_SYSTEM
4840 mark_image_cache (ptr);
e2c556b4 4841 mark_object (&ptr->tool_bar_items);
9ea173e8
GM
4842 mark_object (&ptr->desired_tool_bar_string);
4843 mark_object (&ptr->current_tool_bar_string);
41c28a37 4844#endif /* HAVE_WINDOW_SYSTEM */
169ee243 4845 }
7b07587b 4846 else if (GC_BOOL_VECTOR_P (obj))
707788bd
RS
4847 {
4848 register struct Lisp_Vector *ptr = XVECTOR (obj);
4849
3ef06d12 4850 if (VECTOR_MARKED_P (ptr))
707788bd 4851 break; /* Already marked */
4f5c1376 4852 CHECK_LIVE (live_vector_p);
3ef06d12 4853 VECTOR_MARK (ptr); /* Else mark it */
707788bd 4854 }
41c28a37
GM
4855 else if (GC_WINDOWP (obj))
4856 {
4857 register struct Lisp_Vector *ptr = XVECTOR (obj);
4858 struct window *w = XWINDOW (obj);
41c28a37
GM
4859 register int i;
4860
4861 /* Stop if already marked. */
3ef06d12 4862 if (VECTOR_MARKED_P (ptr))
41c28a37
GM
4863 break;
4864
4865 /* Mark it. */
4f5c1376 4866 CHECK_LIVE (live_vector_p);
3ef06d12 4867 VECTOR_MARK (ptr);
41c28a37
GM
4868
4869 /* There is no Lisp data above The member CURRENT_MATRIX in
4870 struct WINDOW. Stop marking when that slot is reached. */
4871 for (i = 0;
c70bbf06 4872 (char *) &ptr->contents[i] < (char *) &w->current_matrix;
41c28a37 4873 i++)
c70bbf06 4874 mark_object (&ptr->contents[i]);
41c28a37
GM
4875
4876 /* Mark glyphs for leaf windows. Marking window matrices is
4877 sufficient because frame matrices use the same glyph
4878 memory. */
4879 if (NILP (w->hchild)
4880 && NILP (w->vchild)
4881 && w->current_matrix)
4882 {
4883 mark_glyph_matrix (w->current_matrix);
4884 mark_glyph_matrix (w->desired_matrix);
4885 }
4886 }
4887 else if (GC_HASH_TABLE_P (obj))
4888 {
4889 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
177c0ea7 4890
41c28a37 4891 /* Stop if already marked. */
3ef06d12 4892 if (VECTOR_MARKED_P (h))
41c28a37 4893 break;
177c0ea7 4894
41c28a37 4895 /* Mark it. */
4f5c1376 4896 CHECK_LIVE (live_vector_p);
3ef06d12 4897 VECTOR_MARK (h);
41c28a37
GM
4898
4899 /* Mark contents. */
94a877ef 4900 /* Do not mark next_free or next_weak.
177c0ea7 4901 Being in the next_weak chain
94a877ef
RS
4902 should not keep the hash table alive.
4903 No need to mark `count' since it is an integer. */
41c28a37
GM
4904 mark_object (&h->test);
4905 mark_object (&h->weak);
4906 mark_object (&h->rehash_size);
4907 mark_object (&h->rehash_threshold);
4908 mark_object (&h->hash);
4909 mark_object (&h->next);
4910 mark_object (&h->index);
4911 mark_object (&h->user_hash_function);
4912 mark_object (&h->user_cmp_function);
4913
4914 /* If hash table is not weak, mark all keys and values.
4915 For weak tables, mark only the vector. */
4916 if (GC_NILP (h->weak))
4917 mark_object (&h->key_and_value);
4918 else
3ef06d12 4919 VECTOR_MARK (XVECTOR (h->key_and_value));
41c28a37 4920 }
04ff9756 4921 else
169ee243
RS
4922 {
4923 register struct Lisp_Vector *ptr = XVECTOR (obj);
4924 register EMACS_INT size = ptr->size;
169ee243
RS
4925 register int i;
4926
3ef06d12 4927 if (VECTOR_MARKED_P (ptr)) break; /* Already marked */
4f5c1376 4928 CHECK_LIVE (live_vector_p);
3ef06d12 4929 VECTOR_MARK (ptr); /* Else mark it */
169ee243
RS
4930 if (size & PSEUDOVECTOR_FLAG)
4931 size &= PSEUDOVECTOR_SIZE_MASK;
41c28a37 4932
169ee243 4933 for (i = 0; i < size; i++) /* and then mark its elements */
c70bbf06 4934 mark_object (&ptr->contents[i]);
169ee243
RS
4935 }
4936 break;
7146af97 4937
7146af97
JB
4938 case Lisp_Symbol:
4939 {
c70bbf06 4940 register struct Lisp_Symbol *ptr = XSYMBOL (obj);
7146af97
JB
4941 struct Lisp_Symbol *ptrx;
4942
2336fe58 4943 if (ptr->gcmarkbit) break;
4f5c1376 4944 CHECK_ALLOCATED_AND_LIVE (live_symbol_p);
2336fe58 4945 ptr->gcmarkbit = 1;
7146af97
JB
4946 mark_object ((Lisp_Object *) &ptr->value);
4947 mark_object (&ptr->function);
4948 mark_object (&ptr->plist);
34400008 4949
8fe5665d
KR
4950 if (!PURE_POINTER_P (XSTRING (ptr->xname)))
4951 MARK_STRING (XSTRING (ptr->xname));
d5db4077 4952 MARK_INTERVAL_TREE (STRING_INTERVALS (ptr->xname));
177c0ea7 4953
1c6bb482
RS
4954 /* Note that we do not mark the obarray of the symbol.
4955 It is safe not to do so because nothing accesses that
4956 slot except to check whether it is nil. */
7146af97
JB
4957 ptr = ptr->next;
4958 if (ptr)
4959 {
9149e743
KH
4960 /* For the benefit of the last_marked log. */
4961 objptr = (Lisp_Object *)&XSYMBOL (obj)->next;
b0846f52 4962 ptrx = ptr; /* Use of ptrx avoids compiler bug on Sun */
7146af97 4963 XSETSYMBOL (obj, ptrx);
9149e743
KH
4964 /* We can't goto loop here because *objptr doesn't contain an
4965 actual Lisp_Object with valid datatype field. */
4966 goto loop2;
7146af97
JB
4967 }
4968 }
4969 break;
4970
a0a38eb7 4971 case Lisp_Misc:
4f5c1376 4972 CHECK_ALLOCATED_AND_LIVE (live_misc_p);
2336fe58
SM
4973 if (XMARKER (obj)->gcmarkbit)
4974 break;
4975 XMARKER (obj)->gcmarkbit = 1;
a5da44fe 4976 switch (XMISCTYPE (obj))
a0a38eb7 4977 {
465edf35
KH
4978 case Lisp_Misc_Buffer_Local_Value:
4979 case Lisp_Misc_Some_Buffer_Local_Value:
4980 {
4981 register struct Lisp_Buffer_Local_Value *ptr
4982 = XBUFFER_LOCAL_VALUE (obj);
465edf35
KH
4983 /* If the cdr is nil, avoid recursion for the car. */
4984 if (EQ (ptr->cdr, Qnil))
4985 {
a9faeabe 4986 objptr = &ptr->realvalue;
465edf35
KH
4987 goto loop;
4988 }
a9faeabe
RS
4989 mark_object (&ptr->realvalue);
4990 mark_object (&ptr->buffer);
4991 mark_object (&ptr->frame);
c70bbf06 4992 objptr = &ptr->cdr;
465edf35
KH
4993 goto loop;
4994 }
4995
2336fe58
SM
4996 case Lisp_Misc_Marker:
4997 /* DO NOT mark thru the marker's chain.
4998 The buffer's markers chain does not preserve markers from gc;
4999 instead, markers are removed from the chain when freed by gc. */
c8616056
KH
5000 case Lisp_Misc_Intfwd:
5001 case Lisp_Misc_Boolfwd:
5002 case Lisp_Misc_Objfwd:
5003 case Lisp_Misc_Buffer_Objfwd:
b875d3f7 5004 case Lisp_Misc_Kboard_Objfwd:
c8616056
KH
5005 /* Don't bother with Lisp_Buffer_Objfwd,
5006 since all markable slots in current buffer marked anyway. */
5007 /* Don't need to do Lisp_Objfwd, since the places they point
5008 are protected with staticpro. */
5009 break;
5010
e202fa34
KH
5011 case Lisp_Misc_Overlay:
5012 {
5013 struct Lisp_Overlay *ptr = XOVERLAY (obj);
2336fe58
SM
5014 mark_object (&ptr->start);
5015 mark_object (&ptr->end);
5016 objptr = &ptr->plist;
5017 goto loop;
e202fa34
KH
5018 }
5019 break;
5020
a0a38eb7
KH
5021 default:
5022 abort ();
5023 }
7146af97
JB
5024 break;
5025
5026 case Lisp_Cons:
7146af97
JB
5027 {
5028 register struct Lisp_Cons *ptr = XCONS (obj);
5029 if (XMARKBIT (ptr->car)) break;
4f5c1376 5030 CHECK_ALLOCATED_AND_LIVE (live_cons_p);
7146af97 5031 XMARK (ptr->car);
c54ca951
RS
5032 /* If the cdr is nil, avoid recursion for the car. */
5033 if (EQ (ptr->cdr, Qnil))
5034 {
5035 objptr = &ptr->car;
1342fc6f 5036 cdr_count = 0;
c54ca951
RS
5037 goto loop;
5038 }
7146af97 5039 mark_object (&ptr->car);
c70bbf06 5040 objptr = &ptr->cdr;
1342fc6f
RS
5041 cdr_count++;
5042 if (cdr_count == mark_object_loop_halt)
5043 abort ();
7146af97
JB
5044 goto loop;
5045 }
5046
7146af97 5047 case Lisp_Float:
4f5c1376 5048 CHECK_ALLOCATED_AND_LIVE (live_float_p);
ab6780cd 5049 FLOAT_MARK (XFLOAT (obj));
7146af97 5050 break;
7146af97 5051
7146af97 5052 case Lisp_Int:
7146af97
JB
5053 break;
5054
5055 default:
5056 abort ();
5057 }
4f5c1376
GM
5058
5059#undef CHECK_LIVE
5060#undef CHECK_ALLOCATED
5061#undef CHECK_ALLOCATED_AND_LIVE
7146af97
JB
5062}
5063
5064/* Mark the pointers in a buffer structure. */
5065
5066static void
5067mark_buffer (buf)
5068 Lisp_Object buf;
5069{
7146af97
JB
5070 register struct buffer *buffer = XBUFFER (buf);
5071 register Lisp_Object *ptr;
30e3190a 5072 Lisp_Object base_buffer;
7146af97 5073
3ef06d12 5074 VECTOR_MARK (buffer);
7146af97 5075
30e3190a 5076 MARK_INTERVAL_TREE (BUF_INTERVALS (buffer));
d5e35230 5077
4c315bda
RS
5078 if (CONSP (buffer->undo_list))
5079 {
5080 Lisp_Object tail;
5081 tail = buffer->undo_list;
5082
2336fe58
SM
5083 /* We mark the undo list specially because
5084 its pointers to markers should be weak. */
5085
4c315bda
RS
5086 while (CONSP (tail))
5087 {
5088 register struct Lisp_Cons *ptr = XCONS (tail);
5089
5090 if (XMARKBIT (ptr->car))
5091 break;
5092 XMARK (ptr->car);
5093 if (GC_CONSP (ptr->car)
70949dac
KR
5094 && ! XMARKBIT (XCAR (ptr->car))
5095 && GC_MARKERP (XCAR (ptr->car)))
4c315bda 5096 {
f3fbd155
KR
5097 XMARK (XCAR_AS_LVALUE (ptr->car));
5098 mark_object (&XCDR_AS_LVALUE (ptr->car));
4c315bda
RS
5099 }
5100 else
5101 mark_object (&ptr->car);
5102
5103 if (CONSP (ptr->cdr))
5104 tail = ptr->cdr;
5105 else
5106 break;
5107 }
5108
f3fbd155 5109 mark_object (&XCDR_AS_LVALUE (tail));
4c315bda
RS
5110 }
5111 else
5112 mark_object (&buffer->undo_list);
5113
3ef06d12 5114 for (ptr = &buffer->name;
7146af97
JB
5115 (char *)ptr < (char *)buffer + sizeof (struct buffer);
5116 ptr++)
5117 mark_object (ptr);
30e3190a
RS
5118
5119 /* If this is an indirect buffer, mark its base buffer. */
349bd9ed 5120 if (buffer->base_buffer && !VECTOR_MARKED_P (buffer->base_buffer))
30e3190a 5121 {
177c0ea7 5122 XSETBUFFER (base_buffer, buffer->base_buffer);
30e3190a
RS
5123 mark_buffer (base_buffer);
5124 }
7146af97 5125}
084b1a0c
KH
5126
5127
41c28a37
GM
5128/* Value is non-zero if OBJ will survive the current GC because it's
5129 either marked or does not need to be marked to survive. */
5130
5131int
5132survives_gc_p (obj)
5133 Lisp_Object obj;
5134{
5135 int survives_p;
177c0ea7 5136
41c28a37
GM
5137 switch (XGCTYPE (obj))
5138 {
5139 case Lisp_Int:
5140 survives_p = 1;
5141 break;
5142
5143 case Lisp_Symbol:
2336fe58 5144 survives_p = XSYMBOL (obj)->gcmarkbit;
41c28a37
GM
5145 break;
5146
5147 case Lisp_Misc:
ef89c2ce 5148 survives_p = XMARKER (obj)->gcmarkbit;
41c28a37
GM
5149 break;
5150
5151 case Lisp_String:
5152 {
5153 struct Lisp_String *s = XSTRING (obj);
2e471eb5 5154 survives_p = STRING_MARKED_P (s);
41c28a37
GM
5155 }
5156 break;
5157
5158 case Lisp_Vectorlike:
5159 if (GC_BUFFERP (obj))
3ef06d12 5160 survives_p = VECTOR_MARKED_P (XBUFFER (obj));
41c28a37
GM
5161 else if (GC_SUBRP (obj))
5162 survives_p = 1;
5163 else
3ef06d12 5164 survives_p = VECTOR_MARKED_P (XVECTOR (obj));
41c28a37
GM
5165 break;
5166
5167 case Lisp_Cons:
5168 survives_p = XMARKBIT (XCAR (obj));
5169 break;
5170
41c28a37 5171 case Lisp_Float:
ab6780cd 5172 survives_p = FLOAT_MARKED_P (XFLOAT (obj));
41c28a37 5173 break;
41c28a37
GM
5174
5175 default:
5176 abort ();
5177 }
5178
34400008 5179 return survives_p || PURE_POINTER_P ((void *) XPNTR (obj));
41c28a37
GM
5180}
5181
5182
7146af97 5183\f
1a4f1e2c 5184/* Sweep: find all structures not marked, and free them. */
7146af97
JB
5185
5186static void
5187gc_sweep ()
5188{
41c28a37
GM
5189 /* Remove or mark entries in weak hash tables.
5190 This must be done before any object is unmarked. */
5191 sweep_weak_hash_tables ();
5192
2e471eb5 5193 sweep_strings ();
676a7251
GM
5194#ifdef GC_CHECK_STRING_BYTES
5195 if (!noninteractive)
5196 check_string_bytes (1);
5197#endif
7146af97
JB
5198
5199 /* Put all unmarked conses on free list */
5200 {
5201 register struct cons_block *cblk;
6ca94ac9 5202 struct cons_block **cprev = &cons_block;
7146af97
JB
5203 register int lim = cons_block_index;
5204 register int num_free = 0, num_used = 0;
5205
5206 cons_free_list = 0;
177c0ea7 5207
6ca94ac9 5208 for (cblk = cons_block; cblk; cblk = *cprev)
7146af97
JB
5209 {
5210 register int i;
6ca94ac9 5211 int this_free = 0;
7146af97
JB
5212 for (i = 0; i < lim; i++)
5213 if (!XMARKBIT (cblk->conses[i].car))
5214 {
6ca94ac9 5215 this_free++;
1cd5fe6a 5216 *(struct Lisp_Cons **)&cblk->conses[i].cdr = cons_free_list;
7146af97 5217 cons_free_list = &cblk->conses[i];
34400008
GM
5218#if GC_MARK_STACK
5219 cons_free_list->car = Vdead;
5220#endif
7146af97
JB
5221 }
5222 else
5223 {
5224 num_used++;
5225 XUNMARK (cblk->conses[i].car);
5226 }
5227 lim = CONS_BLOCK_SIZE;
6ca94ac9
KH
5228 /* If this block contains only free conses and we have already
5229 seen more than two blocks worth of free conses then deallocate
5230 this block. */
6feef451 5231 if (this_free == CONS_BLOCK_SIZE && num_free > CONS_BLOCK_SIZE)
6ca94ac9 5232 {
6ca94ac9
KH
5233 *cprev = cblk->next;
5234 /* Unhook from the free list. */
5235 cons_free_list = *(struct Lisp_Cons **) &cblk->conses[0].cdr;
c8099634
RS
5236 lisp_free (cblk);
5237 n_cons_blocks--;
6ca94ac9
KH
5238 }
5239 else
6feef451
AS
5240 {
5241 num_free += this_free;
5242 cprev = &cblk->next;
5243 }
7146af97
JB
5244 }
5245 total_conses = num_used;
5246 total_free_conses = num_free;
5247 }
5248
7146af97
JB
5249 /* Put all unmarked floats on free list */
5250 {
5251 register struct float_block *fblk;
6ca94ac9 5252 struct float_block **fprev = &float_block;
7146af97
JB
5253 register int lim = float_block_index;
5254 register int num_free = 0, num_used = 0;
5255
5256 float_free_list = 0;
177c0ea7 5257
6ca94ac9 5258 for (fblk = float_block; fblk; fblk = *fprev)
7146af97
JB
5259 {
5260 register int i;
6ca94ac9 5261 int this_free = 0;
7146af97 5262 for (i = 0; i < lim; i++)
ab6780cd 5263 if (!FLOAT_MARKED_P (&fblk->floats[i]))
7146af97 5264 {
6ca94ac9 5265 this_free++;
1cd5fe6a 5266 *(struct Lisp_Float **)&fblk->floats[i].data = float_free_list;
7146af97
JB
5267 float_free_list = &fblk->floats[i];
5268 }
5269 else
5270 {
5271 num_used++;
ab6780cd 5272 FLOAT_UNMARK (&fblk->floats[i]);
7146af97
JB
5273 }
5274 lim = FLOAT_BLOCK_SIZE;
6ca94ac9
KH
5275 /* If this block contains only free floats and we have already
5276 seen more than two blocks worth of free floats then deallocate
5277 this block. */
6feef451 5278 if (this_free == FLOAT_BLOCK_SIZE && num_free > FLOAT_BLOCK_SIZE)
6ca94ac9 5279 {
6ca94ac9
KH
5280 *fprev = fblk->next;
5281 /* Unhook from the free list. */
5282 float_free_list = *(struct Lisp_Float **) &fblk->floats[0].data;
ab6780cd 5283 lisp_align_free (fblk);
c8099634 5284 n_float_blocks--;
6ca94ac9
KH
5285 }
5286 else
6feef451
AS
5287 {
5288 num_free += this_free;
5289 fprev = &fblk->next;
5290 }
7146af97
JB
5291 }
5292 total_floats = num_used;
5293 total_free_floats = num_free;
5294 }
7146af97 5295
d5e35230
JA
5296 /* Put all unmarked intervals on free list */
5297 {
5298 register struct interval_block *iblk;
6ca94ac9 5299 struct interval_block **iprev = &interval_block;
d5e35230
JA
5300 register int lim = interval_block_index;
5301 register int num_free = 0, num_used = 0;
5302
5303 interval_free_list = 0;
5304
6ca94ac9 5305 for (iblk = interval_block; iblk; iblk = *iprev)
d5e35230
JA
5306 {
5307 register int i;
6ca94ac9 5308 int this_free = 0;
d5e35230
JA
5309
5310 for (i = 0; i < lim; i++)
5311 {
2336fe58 5312 if (!iblk->intervals[i].gcmarkbit)
d5e35230 5313 {
439d5cb4 5314 SET_INTERVAL_PARENT (&iblk->intervals[i], interval_free_list);
d5e35230 5315 interval_free_list = &iblk->intervals[i];
6ca94ac9 5316 this_free++;
d5e35230
JA
5317 }
5318 else
5319 {
5320 num_used++;
2336fe58 5321 iblk->intervals[i].gcmarkbit = 0;
d5e35230
JA
5322 }
5323 }
5324 lim = INTERVAL_BLOCK_SIZE;
6ca94ac9
KH
5325 /* If this block contains only free intervals and we have already
5326 seen more than two blocks worth of free intervals then
5327 deallocate this block. */
6feef451 5328 if (this_free == INTERVAL_BLOCK_SIZE && num_free > INTERVAL_BLOCK_SIZE)
6ca94ac9 5329 {
6ca94ac9
KH
5330 *iprev = iblk->next;
5331 /* Unhook from the free list. */
439d5cb4 5332 interval_free_list = INTERVAL_PARENT (&iblk->intervals[0]);
c8099634
RS
5333 lisp_free (iblk);
5334 n_interval_blocks--;
6ca94ac9
KH
5335 }
5336 else
6feef451
AS
5337 {
5338 num_free += this_free;
5339 iprev = &iblk->next;
5340 }
d5e35230
JA
5341 }
5342 total_intervals = num_used;
5343 total_free_intervals = num_free;
5344 }
d5e35230 5345
7146af97
JB
5346 /* Put all unmarked symbols on free list */
5347 {
5348 register struct symbol_block *sblk;
6ca94ac9 5349 struct symbol_block **sprev = &symbol_block;
7146af97
JB
5350 register int lim = symbol_block_index;
5351 register int num_free = 0, num_used = 0;
5352
d285b373 5353 symbol_free_list = NULL;
177c0ea7 5354
6ca94ac9 5355 for (sblk = symbol_block; sblk; sblk = *sprev)
7146af97 5356 {
6ca94ac9 5357 int this_free = 0;
d285b373
GM
5358 struct Lisp_Symbol *sym = sblk->symbols;
5359 struct Lisp_Symbol *end = sym + lim;
5360
5361 for (; sym < end; ++sym)
5362 {
20035321
SM
5363 /* Check if the symbol was created during loadup. In such a case
5364 it might be pointed to by pure bytecode which we don't trace,
5365 so we conservatively assume that it is live. */
8fe5665d 5366 int pure_p = PURE_POINTER_P (XSTRING (sym->xname));
177c0ea7 5367
2336fe58 5368 if (!sym->gcmarkbit && !pure_p)
d285b373
GM
5369 {
5370 *(struct Lisp_Symbol **) &sym->value = symbol_free_list;
5371 symbol_free_list = sym;
34400008 5372#if GC_MARK_STACK
d285b373 5373 symbol_free_list->function = Vdead;
34400008 5374#endif
d285b373
GM
5375 ++this_free;
5376 }
5377 else
5378 {
5379 ++num_used;
5380 if (!pure_p)
8fe5665d 5381 UNMARK_STRING (XSTRING (sym->xname));
2336fe58 5382 sym->gcmarkbit = 0;
d285b373
GM
5383 }
5384 }
177c0ea7 5385
7146af97 5386 lim = SYMBOL_BLOCK_SIZE;
6ca94ac9
KH
5387 /* If this block contains only free symbols and we have already
5388 seen more than two blocks worth of free symbols then deallocate
5389 this block. */
6feef451 5390 if (this_free == SYMBOL_BLOCK_SIZE && num_free > SYMBOL_BLOCK_SIZE)
6ca94ac9 5391 {
6ca94ac9
KH
5392 *sprev = sblk->next;
5393 /* Unhook from the free list. */
5394 symbol_free_list = *(struct Lisp_Symbol **)&sblk->symbols[0].value;
c8099634
RS
5395 lisp_free (sblk);
5396 n_symbol_blocks--;
6ca94ac9
KH
5397 }
5398 else
6feef451
AS
5399 {
5400 num_free += this_free;
5401 sprev = &sblk->next;
5402 }
7146af97
JB
5403 }
5404 total_symbols = num_used;
5405 total_free_symbols = num_free;
5406 }
5407
a9faeabe
RS
5408 /* Put all unmarked misc's on free list.
5409 For a marker, first unchain it from the buffer it points into. */
7146af97
JB
5410 {
5411 register struct marker_block *mblk;
6ca94ac9 5412 struct marker_block **mprev = &marker_block;
7146af97
JB
5413 register int lim = marker_block_index;
5414 register int num_free = 0, num_used = 0;
5415
5416 marker_free_list = 0;
177c0ea7 5417
6ca94ac9 5418 for (mblk = marker_block; mblk; mblk = *mprev)
7146af97
JB
5419 {
5420 register int i;
6ca94ac9 5421 int this_free = 0;
fa05e253 5422
7146af97 5423 for (i = 0; i < lim; i++)
465edf35 5424 {
2336fe58 5425 if (!mblk->markers[i].u_marker.gcmarkbit)
465edf35 5426 {
a5da44fe 5427 if (mblk->markers[i].u_marker.type == Lisp_Misc_Marker)
ef89c2ce 5428 unchain_marker (&mblk->markers[i].u_marker);
fa05e253
RS
5429 /* Set the type of the freed object to Lisp_Misc_Free.
5430 We could leave the type alone, since nobody checks it,
465edf35 5431 but this might catch bugs faster. */
a5da44fe 5432 mblk->markers[i].u_marker.type = Lisp_Misc_Free;
465edf35
KH
5433 mblk->markers[i].u_free.chain = marker_free_list;
5434 marker_free_list = &mblk->markers[i];
6ca94ac9 5435 this_free++;
465edf35
KH
5436 }
5437 else
5438 {
5439 num_used++;
2336fe58 5440 mblk->markers[i].u_marker.gcmarkbit = 0;
465edf35
KH
5441 }
5442 }
7146af97 5443 lim = MARKER_BLOCK_SIZE;
6ca94ac9
KH
5444 /* If this block contains only free markers and we have already
5445 seen more than two blocks worth of free markers then deallocate
5446 this block. */
6feef451 5447 if (this_free == MARKER_BLOCK_SIZE && num_free > MARKER_BLOCK_SIZE)
6ca94ac9 5448 {
6ca94ac9
KH
5449 *mprev = mblk->next;
5450 /* Unhook from the free list. */
5451 marker_free_list = mblk->markers[0].u_free.chain;
c8099634
RS
5452 lisp_free (mblk);
5453 n_marker_blocks--;
6ca94ac9
KH
5454 }
5455 else
6feef451
AS
5456 {
5457 num_free += this_free;
5458 mprev = &mblk->next;
5459 }
7146af97
JB
5460 }
5461
5462 total_markers = num_used;
5463 total_free_markers = num_free;
5464 }
5465
5466 /* Free all unmarked buffers */
5467 {
5468 register struct buffer *buffer = all_buffers, *prev = 0, *next;
5469
5470 while (buffer)
3ef06d12 5471 if (!VECTOR_MARKED_P (buffer))
7146af97
JB
5472 {
5473 if (prev)
5474 prev->next = buffer->next;
5475 else
5476 all_buffers = buffer->next;
5477 next = buffer->next;
34400008 5478 lisp_free (buffer);
7146af97
JB
5479 buffer = next;
5480 }
5481 else
5482 {
3ef06d12 5483 VECTOR_UNMARK (buffer);
30e3190a 5484 UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer));
7146af97
JB
5485 prev = buffer, buffer = buffer->next;
5486 }
5487 }
5488
7146af97
JB
5489 /* Free all unmarked vectors */
5490 {
5491 register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next;
5492 total_vector_size = 0;
5493
5494 while (vector)
3ef06d12 5495 if (!VECTOR_MARKED_P (vector))
7146af97
JB
5496 {
5497 if (prev)
5498 prev->next = vector->next;
5499 else
5500 all_vectors = vector->next;
5501 next = vector->next;
c8099634
RS
5502 lisp_free (vector);
5503 n_vectors--;
7146af97 5504 vector = next;
41c28a37 5505
7146af97
JB
5506 }
5507 else
5508 {
3ef06d12 5509 VECTOR_UNMARK (vector);
fa05e253
RS
5510 if (vector->size & PSEUDOVECTOR_FLAG)
5511 total_vector_size += (PSEUDOVECTOR_SIZE_MASK & vector->size);
5512 else
5513 total_vector_size += vector->size;
7146af97
JB
5514 prev = vector, vector = vector->next;
5515 }
5516 }
177c0ea7 5517
676a7251
GM
5518#ifdef GC_CHECK_STRING_BYTES
5519 if (!noninteractive)
5520 check_string_bytes (1);
5521#endif
7146af97 5522}
7146af97 5523
7146af97 5524
7146af97 5525
7146af97 5526\f
20d24714
JB
5527/* Debugging aids. */
5528
31ce1c91 5529DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0,
a6266d23 5530 doc: /* Return the address of the last byte Emacs has allocated, divided by 1024.
228299fa 5531This may be helpful in debugging Emacs's memory usage.
7ee72033
MB
5532We divide the value by 1024 to make sure it fits in a Lisp integer. */)
5533 ()
20d24714
JB
5534{
5535 Lisp_Object end;
5536
45d12a89 5537 XSETINT (end, (EMACS_INT) sbrk (0) / 1024);
20d24714
JB
5538
5539 return end;
5540}
5541
310ea200 5542DEFUN ("memory-use-counts", Fmemory_use_counts, Smemory_use_counts, 0, 0, 0,
a6266d23 5543 doc: /* Return a list of counters that measure how much consing there has been.
228299fa
GM
5544Each of these counters increments for a certain kind of object.
5545The counters wrap around from the largest positive integer to zero.
5546Garbage collection does not decrease them.
5547The elements of the value are as follows:
5548 (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS STRINGS)
5549All are in units of 1 = one object consed
5550except for VECTOR-CELLS and STRING-CHARS, which count the total length of
5551objects consed.
5552MISCS include overlays, markers, and some internal types.
5553Frames, windows, buffers, and subprocesses count as vectors
7ee72033
MB
5554 (but the contents of a buffer's text do not count here). */)
5555 ()
310ea200 5556{
2e471eb5 5557 Lisp_Object consed[8];
310ea200 5558
78e985eb
GM
5559 consed[0] = make_number (min (MOST_POSITIVE_FIXNUM, cons_cells_consed));
5560 consed[1] = make_number (min (MOST_POSITIVE_FIXNUM, floats_consed));
5561 consed[2] = make_number (min (MOST_POSITIVE_FIXNUM, vector_cells_consed));
5562 consed[3] = make_number (min (MOST_POSITIVE_FIXNUM, symbols_consed));
5563 consed[4] = make_number (min (MOST_POSITIVE_FIXNUM, string_chars_consed));
5564 consed[5] = make_number (min (MOST_POSITIVE_FIXNUM, misc_objects_consed));
5565 consed[6] = make_number (min (MOST_POSITIVE_FIXNUM, intervals_consed));
5566 consed[7] = make_number (min (MOST_POSITIVE_FIXNUM, strings_consed));
310ea200 5567
2e471eb5 5568 return Flist (8, consed);
310ea200 5569}
e0b8c689
KR
5570
5571int suppress_checking;
5572void
5573die (msg, file, line)
5574 const char *msg;
5575 const char *file;
5576 int line;
5577{
5578 fprintf (stderr, "\r\nEmacs fatal error: %s:%d: %s\r\n",
5579 file, line, msg);
5580 abort ();
5581}
20d24714 5582\f
7146af97
JB
5583/* Initialization */
5584
dfcf069d 5585void
7146af97
JB
5586init_alloc_once ()
5587{
5588 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
9e713715
GM
5589 purebeg = PUREBEG;
5590 pure_size = PURESIZE;
1f0b3fd2 5591 pure_bytes_used = 0;
9e713715
GM
5592 pure_bytes_used_before_overflow = 0;
5593
ab6780cd
SM
5594 /* Initialize the list of free aligned blocks. */
5595 free_ablock = NULL;
5596
877935b1 5597#if GC_MARK_STACK || defined GC_MALLOC_CHECK
34400008
GM
5598 mem_init ();
5599 Vdead = make_pure_string ("DEAD", 4, 4, 0);
5600#endif
9e713715 5601
7146af97
JB
5602 all_vectors = 0;
5603 ignore_warnings = 1;
d1658221
RS
5604#ifdef DOUG_LEA_MALLOC
5605 mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */
5606 mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */
81d492d5 5607 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); /* max. number of mmap'ed areas */
d1658221 5608#endif
7146af97
JB
5609 init_strings ();
5610 init_cons ();
5611 init_symbol ();
5612 init_marker ();
7146af97 5613 init_float ();
34400008 5614 init_intervals ();
d5e35230 5615
276cbe5a
RS
5616#ifdef REL_ALLOC
5617 malloc_hysteresis = 32;
5618#else
5619 malloc_hysteresis = 0;
5620#endif
5621
5622 spare_memory = (char *) malloc (SPARE_MEMORY);
5623
7146af97
JB
5624 ignore_warnings = 0;
5625 gcprolist = 0;
630686c8 5626 byte_stack_list = 0;
7146af97
JB
5627 staticidx = 0;
5628 consing_since_gc = 0;
7d179cea 5629 gc_cons_threshold = 100000 * sizeof (Lisp_Object);
7146af97
JB
5630#ifdef VIRT_ADDR_VARIES
5631 malloc_sbrk_unused = 1<<22; /* A large number */
5632 malloc_sbrk_used = 100000; /* as reasonable as any number */
5633#endif /* VIRT_ADDR_VARIES */
5634}
5635
dfcf069d 5636void
7146af97
JB
5637init_alloc ()
5638{
5639 gcprolist = 0;
630686c8 5640 byte_stack_list = 0;
182ff242
GM
5641#if GC_MARK_STACK
5642#if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
5643 setjmp_tested_p = longjmps_done = 0;
5644#endif
5645#endif
2c5bd608
DL
5646 Vgc_elapsed = make_float (0.0);
5647 gcs_done = 0;
7146af97
JB
5648}
5649
5650void
5651syms_of_alloc ()
5652{
7ee72033 5653 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold,
a6266d23 5654 doc: /* *Number of bytes of consing between garbage collections.
228299fa
GM
5655Garbage collection can happen automatically once this many bytes have been
5656allocated since the last garbage collection. All data types count.
7146af97 5657
228299fa 5658Garbage collection happens automatically only when `eval' is called.
7146af97 5659
228299fa
GM
5660By binding this temporarily to a large number, you can effectively
5661prevent garbage collection during a part of the program. */);
0819585c 5662
7ee72033 5663 DEFVAR_INT ("pure-bytes-used", &pure_bytes_used,
a6266d23 5664 doc: /* Number of bytes of sharable Lisp data allocated so far. */);
0819585c 5665
7ee72033 5666 DEFVAR_INT ("cons-cells-consed", &cons_cells_consed,
a6266d23 5667 doc: /* Number of cons cells that have been consed so far. */);
0819585c 5668
7ee72033 5669 DEFVAR_INT ("floats-consed", &floats_consed,
a6266d23 5670 doc: /* Number of floats that have been consed so far. */);
0819585c 5671
7ee72033 5672 DEFVAR_INT ("vector-cells-consed", &vector_cells_consed,
a6266d23 5673 doc: /* Number of vector cells that have been consed so far. */);
0819585c 5674
7ee72033 5675 DEFVAR_INT ("symbols-consed", &symbols_consed,
a6266d23 5676 doc: /* Number of symbols that have been consed so far. */);
0819585c 5677
7ee72033 5678 DEFVAR_INT ("string-chars-consed", &string_chars_consed,
a6266d23 5679 doc: /* Number of string characters that have been consed so far. */);
0819585c 5680
7ee72033 5681 DEFVAR_INT ("misc-objects-consed", &misc_objects_consed,
a6266d23 5682 doc: /* Number of miscellaneous objects that have been consed so far. */);
2e471eb5 5683
7ee72033 5684 DEFVAR_INT ("intervals-consed", &intervals_consed,
a6266d23 5685 doc: /* Number of intervals that have been consed so far. */);
7146af97 5686
7ee72033 5687 DEFVAR_INT ("strings-consed", &strings_consed,
a6266d23 5688 doc: /* Number of strings that have been consed so far. */);
228299fa 5689
7ee72033 5690 DEFVAR_LISP ("purify-flag", &Vpurify_flag,
a6266d23 5691 doc: /* Non-nil means loading Lisp code in order to dump an executable.
228299fa
GM
5692This means that certain objects should be allocated in shared (pure) space. */);
5693
7ee72033 5694 DEFVAR_INT ("undo-limit", &undo_limit,
a6266d23 5695 doc: /* Keep no more undo information once it exceeds this size.
228299fa
GM
5696This limit is applied when garbage collection happens.
5697The size is counted as the number of bytes occupied,
5698which includes both saved text and other data. */);
502b9b64 5699 undo_limit = 20000;
7146af97 5700
7ee72033 5701 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit,
a6266d23 5702 doc: /* Don't keep more than this much size of undo information.
228299fa
GM
5703A command which pushes past this size is itself forgotten.
5704This limit is applied when garbage collection happens.
5705The size is counted as the number of bytes occupied,
5706which includes both saved text and other data. */);
502b9b64 5707 undo_strong_limit = 30000;
7146af97 5708
7ee72033 5709 DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages,
a6266d23 5710 doc: /* Non-nil means display messages at start and end of garbage collection. */);
299585ee
RS
5711 garbage_collection_messages = 0;
5712
7ee72033 5713 DEFVAR_LISP ("post-gc-hook", &Vpost_gc_hook,
a6266d23 5714 doc: /* Hook run after garbage collection has finished. */);
9e713715
GM
5715 Vpost_gc_hook = Qnil;
5716 Qpost_gc_hook = intern ("post-gc-hook");
5717 staticpro (&Qpost_gc_hook);
5718
74a54b04
RS
5719 DEFVAR_LISP ("memory-signal-data", &Vmemory_signal_data,
5720 doc: /* Precomputed `signal' argument for memory-full error. */);
bcb61d60
KH
5721 /* We build this in advance because if we wait until we need it, we might
5722 not be able to allocate the memory to hold it. */
74a54b04
RS
5723 Vmemory_signal_data
5724 = list2 (Qerror,
5725 build_string ("Memory exhausted--use M-x save-some-buffers then exit and restart Emacs"));
5726
5727 DEFVAR_LISP ("memory-full", &Vmemory_full,
5728 doc: /* Non-nil means we are handling a memory-full error. */);
5729 Vmemory_full = Qnil;
bcb61d60 5730
e8197642
RS
5731 staticpro (&Qgc_cons_threshold);
5732 Qgc_cons_threshold = intern ("gc-cons-threshold");
5733
a59de17b
RS
5734 staticpro (&Qchar_table_extra_slots);
5735 Qchar_table_extra_slots = intern ("char-table-extra-slots");
5736
2c5bd608
DL
5737 DEFVAR_LISP ("gc-elapsed", &Vgc_elapsed,
5738 doc: /* Accumulated time elapsed in garbage collections.
5739The time is in seconds as a floating point value.
5740Programs may reset this to get statistics in a specific period. */);
5741 DEFVAR_INT ("gcs-done", &gcs_done,
5742 doc: /* Accumulated number of garbage collections done.
5743Programs may reset this to get statistics in a specific period. */);
5744
7146af97
JB
5745 defsubr (&Scons);
5746 defsubr (&Slist);
5747 defsubr (&Svector);
5748 defsubr (&Smake_byte_code);
5749 defsubr (&Smake_list);
5750 defsubr (&Smake_vector);
7b07587b 5751 defsubr (&Smake_char_table);
7146af97 5752 defsubr (&Smake_string);
7b07587b 5753 defsubr (&Smake_bool_vector);
7146af97
JB
5754 defsubr (&Smake_symbol);
5755 defsubr (&Smake_marker);
5756 defsubr (&Spurecopy);
5757 defsubr (&Sgarbage_collect);
20d24714 5758 defsubr (&Smemory_limit);
310ea200 5759 defsubr (&Smemory_use_counts);
34400008
GM
5760
5761#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
5762 defsubr (&Sgc_status);
5763#endif
7146af97 5764}