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