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