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