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