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