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