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