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