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