(mark_kboards): Mark all the Lisp_Object fields.
[bpt/emacs.git] / src / alloc.c
CommitLineData
7146af97 1/* Storage allocation and gc for GNU Emacs Lisp interpreter.
4a2f9c6a
RS
2 Copyright (C) 1985, 86, 88, 93, 94, 95, 97, 1998
3 Free Software Foundation, Inc.
7146af97
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
7c299e7a 9the Free Software Foundation; either version 2, or (at your option)
7146af97
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
7146af97 21
290c8f1e 22/* Note that this declares bzero on OSF/1. How dumb. */
cf026b25 23#include <signal.h>
7146af97 24
18160b98 25#include <config.h>
7146af97 26#include "lisp.h"
d5e35230 27#include "intervals.h"
4c0be5f4 28#include "puresize.h"
7146af97
JB
29#ifndef standalone
30#include "buffer.h"
31#include "window.h"
502b9b64 32#include "frame.h"
9ac0d9e0 33#include "blockinput.h"
077d751f 34#include "keyboard.h"
e54daa22 35#include "charset.h"
7146af97
JB
36#endif
37
e065a56e
JB
38#include "syssignal.h"
39
ee1eea5c
KH
40extern char *sbrk ();
41
d1658221
RS
42#ifdef DOUG_LEA_MALLOC
43#include <malloc.h>
44#define __malloc_size_t int
45#else
276cbe5a
RS
46/* The following come from gmalloc.c. */
47
48#if defined (__STDC__) && __STDC__
49#include <stddef.h>
50#define __malloc_size_t size_t
51#else
52#define __malloc_size_t unsigned int
53#endif
54extern __malloc_size_t _bytes_used;
55extern int __malloc_extra_blocks;
d1658221 56#endif /* !defined(DOUG_LEA_MALLOC) */
276cbe5a 57
7146af97 58#define max(A,B) ((A) > (B) ? (A) : (B))
b580578b 59#define min(A,B) ((A) < (B) ? (A) : (B))
7146af97
JB
60
61/* Macro to verify that storage intended for Lisp objects is not
62 out of range to fit in the space for a pointer.
63 ADDRESS is the start of the block, and SIZE
64 is the amount of space within which objects can start. */
65#define VALIDATE_LISP_STORAGE(address, size) \
66do \
67 { \
68 Lisp_Object val; \
45d12a89 69 XSETCONS (val, (char *) address + size); \
7146af97
JB
70 if ((char *) XCONS (val) != (char *) address + size) \
71 { \
9ac0d9e0 72 xfree (address); \
7146af97
JB
73 memory_full (); \
74 } \
75 } while (0)
76
276cbe5a
RS
77/* Value of _bytes_used, when spare_memory was freed. */
78static __malloc_size_t bytes_used_when_full;
79
7146af97
JB
80/* Number of bytes of consing done since the last gc */
81int consing_since_gc;
82
310ea200
RS
83/* Count the amount of consing of various sorts of space. */
84int cons_cells_consed;
85int floats_consed;
86int vector_cells_consed;
87int symbols_consed;
88int string_chars_consed;
89int misc_objects_consed;
90int intervals_consed;
91
7146af97 92/* Number of bytes of consing since gc before another gc should be done. */
b580578b 93int gc_cons_threshold;
7146af97
JB
94
95/* Nonzero during gc */
96int gc_in_progress;
97
299585ee
RS
98/* Nonzero means display messages at beginning and end of GC. */
99int garbage_collection_messages;
100
7146af97
JB
101#ifndef VIRT_ADDR_VARIES
102extern
103#endif /* VIRT_ADDR_VARIES */
104 int malloc_sbrk_used;
105
106#ifndef VIRT_ADDR_VARIES
107extern
108#endif /* VIRT_ADDR_VARIES */
109 int malloc_sbrk_unused;
110
502b9b64
JB
111/* Two limits controlling how much undo information to keep. */
112int undo_limit;
113int undo_strong_limit;
7146af97 114
fd27a537
RS
115int total_conses, total_markers, total_symbols, total_string_size, total_vector_size;
116int total_free_conses, total_free_markers, total_free_symbols;
117#ifdef LISP_FLOAT_TYPE
118int total_free_floats, total_floats;
119#endif /* LISP_FLOAT_TYPE */
120
276cbe5a
RS
121/* Points to memory space allocated as "spare",
122 to be freed if we run out of memory. */
123static char *spare_memory;
124
125/* Amount of spare memory to keep in reserve. */
126#define SPARE_MEMORY (1 << 14)
127
128/* Number of extra blocks malloc should get when it needs more core. */
129static int malloc_hysteresis;
130
3c06d205
KH
131/* Nonzero when malloc is called for allocating Lisp object space. */
132int allocating_for_lisp;
133
7146af97
JB
134/* Non-nil means defun should do purecopy on the function definition */
135Lisp_Object Vpurify_flag;
136
137#ifndef HAVE_SHM
42607681 138EMACS_INT pure[PURESIZE / sizeof (EMACS_INT)] = {0,}; /* Force it into data space! */
7146af97
JB
139#define PUREBEG (char *) pure
140#else
141#define pure PURE_SEG_BITS /* Use shared memory segment */
142#define PUREBEG (char *)PURE_SEG_BITS
4c0be5f4
JB
143
144/* This variable is used only by the XPNTR macro when HAVE_SHM is
145 defined. If we used the PURESIZE macro directly there, that would
146 make most of emacs dependent on puresize.h, which we don't want -
147 you should be able to change that without too much recompilation.
148 So map_in_data initializes pure_size, and the dependencies work
149 out. */
42607681 150EMACS_INT pure_size;
7146af97
JB
151#endif /* not HAVE_SHM */
152
153/* Index in pure at which next pure object will be allocated. */
154int pureptr;
155
156/* If nonzero, this is a warning delivered by malloc and not yet displayed. */
157char *pending_malloc_warning;
158
bcb61d60 159/* Pre-computed signal argument for use when memory is exhausted. */
cf3540e4 160Lisp_Object memory_signal_data;
bcb61d60 161
7146af97
JB
162/* Maximum amount of C stack to save when a GC happens. */
163
164#ifndef MAX_SAVE_STACK
165#define MAX_SAVE_STACK 16000
166#endif
167
1fb577f7
KH
168/* Define DONT_COPY_FLAG to be some bit which will always be zero in a
169 pointer to a Lisp_Object, when that pointer is viewed as an integer.
170 (On most machines, pointers are even, so we can use the low bit.
8e6208c5 171 Word-addressable architectures may need to override this in the m-file.)
1fb577f7
KH
172 When linking references to small strings through the size field, we
173 use this slot to hold the bit that would otherwise be interpreted as
174 the GC mark bit. */
155ffe9c 175#ifndef DONT_COPY_FLAG
1fb577f7 176#define DONT_COPY_FLAG 1
155ffe9c
RS
177#endif /* no DONT_COPY_FLAG */
178
7146af97
JB
179/* Buffer in which we save a copy of the C stack at each GC. */
180
181char *stack_copy;
182int stack_copy_size;
183
184/* Non-zero means ignore malloc warnings. Set during initialization. */
185int ignore_warnings;
350273a4 186
a59de17b 187Lisp_Object Qgc_cons_threshold, Qchar_table_extra_slots;
e8197642 188
b875d3f7 189static void mark_object (), mark_buffer (), mark_kboards ();
350273a4
JA
190static void clear_marks (), gc_sweep ();
191static void compact_strings ();
7da0b0d3
RS
192
193extern int message_enable_multibyte;
7146af97 194\f
1a4f1e2c
JB
195/* Versions of malloc and realloc that print warnings as memory gets full. */
196
7146af97
JB
197Lisp_Object
198malloc_warning_1 (str)
199 Lisp_Object str;
200{
201 Fprinc (str, Vstandard_output);
202 write_string ("\nKilling some buffers may delay running out of memory.\n", -1);
203 write_string ("However, certainly by the time you receive the 95% warning,\n", -1);
204 write_string ("you should clean up, kill this Emacs, and start a new one.", -1);
205 return Qnil;
206}
207
208/* malloc calls this if it finds we are near exhausting storage */
d457598b
AS
209
210void
7146af97
JB
211malloc_warning (str)
212 char *str;
213{
214 pending_malloc_warning = str;
215}
216
d457598b 217void
7146af97
JB
218display_malloc_warning ()
219{
220 register Lisp_Object val;
221
222 val = build_string (pending_malloc_warning);
223 pending_malloc_warning = 0;
224 internal_with_output_to_temp_buffer (" *Danger*", malloc_warning_1, val);
225}
226
d1658221 227#ifdef DOUG_LEA_MALLOC
1177ecf6 228# define BYTES_USED (mallinfo ().arena)
d1658221 229#else
1177ecf6 230# define BYTES_USED _bytes_used
d1658221
RS
231#endif
232
7146af97 233/* Called if malloc returns zero */
276cbe5a 234
d457598b 235void
7146af97
JB
236memory_full ()
237{
276cbe5a 238#ifndef SYSTEM_MALLOC
d1658221 239 bytes_used_when_full = BYTES_USED;
276cbe5a
RS
240#endif
241
242 /* The first time we get here, free the spare memory. */
243 if (spare_memory)
244 {
245 free (spare_memory);
246 spare_memory = 0;
247 }
248
249 /* This used to call error, but if we've run out of memory, we could get
250 infinite recursion trying to build the string. */
251 while (1)
74d84334 252 Fsignal (Qnil, memory_signal_data);
276cbe5a
RS
253}
254
255/* Called if we can't allocate relocatable space for a buffer. */
256
257void
258buffer_memory_full ()
259{
260 /* If buffers use the relocating allocator,
261 no need to free spare_memory, because we may have plenty of malloc
262 space left that we could get, and if we don't, the malloc that fails
263 will itself cause spare_memory to be freed.
264 If buffers don't use the relocating allocator,
265 treat this like any other failing malloc. */
266
267#ifndef REL_ALLOC
268 memory_full ();
269#endif
270
bcb61d60
KH
271 /* This used to call error, but if we've run out of memory, we could get
272 infinite recursion trying to build the string. */
273 while (1)
274 Fsignal (Qerror, memory_signal_data);
7146af97
JB
275}
276
9ac0d9e0 277/* like malloc routines but check for no memory and block interrupt input. */
7146af97
JB
278
279long *
280xmalloc (size)
281 int size;
282{
283 register long *val;
284
9ac0d9e0 285 BLOCK_INPUT;
7146af97 286 val = (long *) malloc (size);
9ac0d9e0 287 UNBLOCK_INPUT;
7146af97
JB
288
289 if (!val && size) memory_full ();
290 return val;
291}
292
293long *
294xrealloc (block, size)
295 long *block;
296 int size;
297{
298 register long *val;
299
9ac0d9e0 300 BLOCK_INPUT;
56d2031b
JB
301 /* We must call malloc explicitly when BLOCK is 0, since some
302 reallocs don't do this. */
303 if (! block)
304 val = (long *) malloc (size);
f048679d 305 else
56d2031b 306 val = (long *) realloc (block, size);
9ac0d9e0 307 UNBLOCK_INPUT;
7146af97
JB
308
309 if (!val && size) memory_full ();
310 return val;
311}
9ac0d9e0
JB
312
313void
314xfree (block)
315 long *block;
316{
317 BLOCK_INPUT;
318 free (block);
319 UNBLOCK_INPUT;
320}
321
322\f
323/* Arranging to disable input signals while we're in malloc.
324
325 This only works with GNU malloc. To help out systems which can't
326 use GNU malloc, all the calls to malloc, realloc, and free
327 elsewhere in the code should be inside a BLOCK_INPUT/UNBLOCK_INPUT
328 pairs; unfortunately, we have no idea what C library functions
329 might call malloc, so we can't really protect them unless you're
330 using GNU malloc. Fortunately, most of the major operating can use
331 GNU malloc. */
332
333#ifndef SYSTEM_MALLOC
b0846f52
JB
334extern void * (*__malloc_hook) ();
335static void * (*old_malloc_hook) ();
336extern void * (*__realloc_hook) ();
337static void * (*old_realloc_hook) ();
338extern void (*__free_hook) ();
339static void (*old_free_hook) ();
9ac0d9e0 340
276cbe5a
RS
341/* This function is used as the hook for free to call. */
342
9ac0d9e0
JB
343static void
344emacs_blocked_free (ptr)
345 void *ptr;
346{
347 BLOCK_INPUT;
348 __free_hook = old_free_hook;
349 free (ptr);
276cbe5a
RS
350 /* If we released our reserve (due to running out of memory),
351 and we have a fair amount free once again,
352 try to set aside another reserve in case we run out once more. */
353 if (spare_memory == 0
354 /* Verify there is enough space that even with the malloc
355 hysteresis this call won't run out again.
356 The code here is correct as long as SPARE_MEMORY
357 is substantially larger than the block size malloc uses. */
358 && (bytes_used_when_full
d1658221 359 > BYTES_USED + max (malloc_hysteresis, 4) * SPARE_MEMORY))
276cbe5a
RS
360 spare_memory = (char *) malloc (SPARE_MEMORY);
361
b0846f52 362 __free_hook = emacs_blocked_free;
9ac0d9e0
JB
363 UNBLOCK_INPUT;
364}
365
276cbe5a
RS
366/* If we released our reserve (due to running out of memory),
367 and we have a fair amount free once again,
368 try to set aside another reserve in case we run out once more.
369
370 This is called when a relocatable block is freed in ralloc.c. */
371
372void
373refill_memory_reserve ()
374{
375 if (spare_memory == 0)
376 spare_memory = (char *) malloc (SPARE_MEMORY);
377}
378
379/* This function is the malloc hook that Emacs uses. */
380
9ac0d9e0
JB
381static void *
382emacs_blocked_malloc (size)
383 unsigned size;
384{
385 void *value;
386
387 BLOCK_INPUT;
388 __malloc_hook = old_malloc_hook;
1177ecf6 389#ifdef DOUG_LEA_MALLOC
d1658221 390 mallopt (M_TOP_PAD, malloc_hysteresis * 4096);
1177ecf6 391#else
d1658221 392 __malloc_extra_blocks = malloc_hysteresis;
1177ecf6 393#endif
2756d8ee 394 value = (void *) malloc (size);
b0846f52 395 __malloc_hook = emacs_blocked_malloc;
9ac0d9e0
JB
396 UNBLOCK_INPUT;
397
398 return value;
399}
400
401static void *
402emacs_blocked_realloc (ptr, size)
403 void *ptr;
404 unsigned size;
405{
406 void *value;
407
408 BLOCK_INPUT;
409 __realloc_hook = old_realloc_hook;
2756d8ee 410 value = (void *) realloc (ptr, size);
b0846f52 411 __realloc_hook = emacs_blocked_realloc;
9ac0d9e0
JB
412 UNBLOCK_INPUT;
413
414 return value;
415}
416
417void
418uninterrupt_malloc ()
419{
420 old_free_hook = __free_hook;
b0846f52 421 __free_hook = emacs_blocked_free;
9ac0d9e0
JB
422
423 old_malloc_hook = __malloc_hook;
b0846f52 424 __malloc_hook = emacs_blocked_malloc;
9ac0d9e0
JB
425
426 old_realloc_hook = __realloc_hook;
b0846f52 427 __realloc_hook = emacs_blocked_realloc;
9ac0d9e0
JB
428}
429#endif
7146af97 430\f
1a4f1e2c
JB
431/* Interval allocation. */
432
d5e35230
JA
433#ifdef USE_TEXT_PROPERTIES
434#define INTERVAL_BLOCK_SIZE \
435 ((1020 - sizeof (struct interval_block *)) / sizeof (struct interval))
436
437struct interval_block
438 {
439 struct interval_block *next;
440 struct interval intervals[INTERVAL_BLOCK_SIZE];
441 };
442
443struct interval_block *interval_block;
444static int interval_block_index;
445
446INTERVAL interval_free_list;
447
448static void
449init_intervals ()
450{
3c06d205 451 allocating_for_lisp = 1;
d5e35230
JA
452 interval_block
453 = (struct interval_block *) malloc (sizeof (struct interval_block));
3c06d205 454 allocating_for_lisp = 0;
d5e35230 455 interval_block->next = 0;
290c8f1e 456 bzero ((char *) interval_block->intervals, sizeof interval_block->intervals);
d5e35230
JA
457 interval_block_index = 0;
458 interval_free_list = 0;
459}
460
461#define INIT_INTERVALS init_intervals ()
462
463INTERVAL
464make_interval ()
465{
466 INTERVAL val;
467
468 if (interval_free_list)
469 {
470 val = interval_free_list;
471 interval_free_list = interval_free_list->parent;
472 }
473 else
474 {
475 if (interval_block_index == INTERVAL_BLOCK_SIZE)
476 {
3c06d205
KH
477 register struct interval_block *newi;
478
479 allocating_for_lisp = 1;
480 newi = (struct interval_block *) xmalloc (sizeof (struct interval_block));
d5e35230 481
3c06d205 482 allocating_for_lisp = 0;
d5e35230
JA
483 VALIDATE_LISP_STORAGE (newi, sizeof *newi);
484 newi->next = interval_block;
485 interval_block = newi;
486 interval_block_index = 0;
487 }
488 val = &interval_block->intervals[interval_block_index++];
489 }
490 consing_since_gc += sizeof (struct interval);
310ea200 491 intervals_consed++;
d5e35230
JA
492 RESET_INTERVAL (val);
493 return val;
494}
495
496static int total_free_intervals, total_intervals;
497
498/* Mark the pointers of one interval. */
499
500static void
d393c068 501mark_interval (i, dummy)
d5e35230 502 register INTERVAL i;
d393c068 503 Lisp_Object dummy;
d5e35230
JA
504{
505 if (XMARKBIT (i->plist))
506 abort ();
507 mark_object (&i->plist);
508 XMARK (i->plist);
509}
510
511static void
512mark_interval_tree (tree)
513 register INTERVAL tree;
514{
e8720644
JB
515 /* No need to test if this tree has been marked already; this
516 function is always called through the MARK_INTERVAL_TREE macro,
517 which takes care of that. */
518
519 /* XMARK expands to an assignment; the LHS of an assignment can't be
520 a cast. */
521 XMARK (* (Lisp_Object *) &tree->parent);
d5e35230 522
d393c068 523 traverse_intervals (tree, 1, 0, mark_interval, Qnil);
d5e35230
JA
524}
525
e8720644
JB
526#define MARK_INTERVAL_TREE(i) \
527 do { \
528 if (!NULL_INTERVAL_P (i) \
74d84334 529 && ! XMARKBIT (*(Lisp_Object *) &i->parent)) \
e8720644
JB
530 mark_interval_tree (i); \
531 } while (0)
d5e35230 532
1a4f1e2c 533/* The oddity in the call to XUNMARK is necessary because XUNMARK
eb8c3be9 534 expands to an assignment to its argument, and most C compilers don't
1a4f1e2c
JB
535 support casts on the left operand of `='. */
536#define UNMARK_BALANCE_INTERVALS(i) \
537{ \
538 if (! NULL_INTERVAL_P (i)) \
539 { \
540 XUNMARK (* (Lisp_Object *) (&(i)->parent)); \
541 (i) = balance_intervals (i); \
542 } \
d5e35230
JA
543}
544
545#else /* no interval use */
546
547#define INIT_INTERVALS
548
549#define UNMARK_BALANCE_INTERVALS(i)
550#define MARK_INTERVAL_TREE(i)
551
552#endif /* no interval use */
553\f
1a4f1e2c
JB
554/* Floating point allocation. */
555
7146af97
JB
556#ifdef LISP_FLOAT_TYPE
557/* Allocation of float cells, just like conses */
558/* We store float cells inside of float_blocks, allocating a new
559 float_block with malloc whenever necessary. Float cells reclaimed by
560 GC are put on a free list to be reallocated before allocating
561 any new float cells from the latest float_block.
562
563 Each float_block is just under 1020 bytes long,
564 since malloc really allocates in units of powers of two
565 and uses 4 bytes for its own overhead. */
566
567#define FLOAT_BLOCK_SIZE \
568 ((1020 - sizeof (struct float_block *)) / sizeof (struct Lisp_Float))
569
570struct float_block
571 {
572 struct float_block *next;
573 struct Lisp_Float floats[FLOAT_BLOCK_SIZE];
574 };
575
576struct float_block *float_block;
577int float_block_index;
578
579struct Lisp_Float *float_free_list;
580
581void
582init_float ()
583{
3c06d205 584 allocating_for_lisp = 1;
7146af97 585 float_block = (struct float_block *) malloc (sizeof (struct float_block));
3c06d205 586 allocating_for_lisp = 0;
7146af97 587 float_block->next = 0;
290c8f1e 588 bzero ((char *) float_block->floats, sizeof float_block->floats);
7146af97
JB
589 float_block_index = 0;
590 float_free_list = 0;
591}
592
593/* Explicitly free a float cell. */
dfcf069d 594void
7146af97
JB
595free_float (ptr)
596 struct Lisp_Float *ptr;
597{
1cd5fe6a 598 *(struct Lisp_Float **)&ptr->data = float_free_list;
7146af97
JB
599 float_free_list = ptr;
600}
601
602Lisp_Object
603make_float (float_value)
604 double float_value;
605{
606 register Lisp_Object val;
607
608 if (float_free_list)
609 {
1cd5fe6a
RS
610 /* We use the data field for chaining the free list
611 so that we won't use the same field that has the mark bit. */
45d12a89 612 XSETFLOAT (val, float_free_list);
1cd5fe6a 613 float_free_list = *(struct Lisp_Float **)&float_free_list->data;
7146af97
JB
614 }
615 else
616 {
617 if (float_block_index == FLOAT_BLOCK_SIZE)
618 {
3c06d205
KH
619 register struct float_block *new;
620
621 allocating_for_lisp = 1;
622 new = (struct float_block *) xmalloc (sizeof (struct float_block));
623 allocating_for_lisp = 0;
7146af97
JB
624 VALIDATE_LISP_STORAGE (new, sizeof *new);
625 new->next = float_block;
626 float_block = new;
627 float_block_index = 0;
628 }
45d12a89 629 XSETFLOAT (val, &float_block->floats[float_block_index++]);
7146af97
JB
630 }
631 XFLOAT (val)->data = float_value;
67ba9986 632 XSETFASTINT (XFLOAT (val)->type, 0); /* bug chasing -wsr */
7146af97 633 consing_since_gc += sizeof (struct Lisp_Float);
310ea200 634 floats_consed++;
7146af97
JB
635 return val;
636}
637
638#endif /* LISP_FLOAT_TYPE */
639\f
640/* Allocation of cons cells */
641/* We store cons cells inside of cons_blocks, allocating a new
642 cons_block with malloc whenever necessary. Cons cells reclaimed by
643 GC are put on a free list to be reallocated before allocating
644 any new cons cells from the latest cons_block.
645
646 Each cons_block is just under 1020 bytes long,
647 since malloc really allocates in units of powers of two
648 and uses 4 bytes for its own overhead. */
649
650#define CONS_BLOCK_SIZE \
651 ((1020 - sizeof (struct cons_block *)) / sizeof (struct Lisp_Cons))
652
653struct cons_block
654 {
655 struct cons_block *next;
656 struct Lisp_Cons conses[CONS_BLOCK_SIZE];
657 };
658
659struct cons_block *cons_block;
660int cons_block_index;
661
662struct Lisp_Cons *cons_free_list;
663
664void
665init_cons ()
666{
3c06d205 667 allocating_for_lisp = 1;
7146af97 668 cons_block = (struct cons_block *) malloc (sizeof (struct cons_block));
3c06d205 669 allocating_for_lisp = 0;
7146af97 670 cons_block->next = 0;
290c8f1e 671 bzero ((char *) cons_block->conses, sizeof cons_block->conses);
7146af97
JB
672 cons_block_index = 0;
673 cons_free_list = 0;
674}
675
676/* Explicitly free a cons cell. */
d457598b
AS
677
678void
7146af97
JB
679free_cons (ptr)
680 struct Lisp_Cons *ptr;
681{
1cd5fe6a 682 *(struct Lisp_Cons **)&ptr->cdr = cons_free_list;
7146af97
JB
683 cons_free_list = ptr;
684}
685
686DEFUN ("cons", Fcons, Scons, 2, 2, 0,
687 "Create a new cons, give it CAR and CDR as components, and return it.")
688 (car, cdr)
689 Lisp_Object car, cdr;
690{
691 register Lisp_Object val;
692
693 if (cons_free_list)
694 {
1cd5fe6a
RS
695 /* We use the cdr for chaining the free list
696 so that we won't use the same field that has the mark bit. */
45d12a89 697 XSETCONS (val, cons_free_list);
1cd5fe6a 698 cons_free_list = *(struct Lisp_Cons **)&cons_free_list->cdr;
7146af97
JB
699 }
700 else
701 {
702 if (cons_block_index == CONS_BLOCK_SIZE)
703 {
3c06d205
KH
704 register struct cons_block *new;
705 allocating_for_lisp = 1;
706 new = (struct cons_block *) xmalloc (sizeof (struct cons_block));
707 allocating_for_lisp = 0;
7146af97
JB
708 VALIDATE_LISP_STORAGE (new, sizeof *new);
709 new->next = cons_block;
710 cons_block = new;
711 cons_block_index = 0;
712 }
45d12a89 713 XSETCONS (val, &cons_block->conses[cons_block_index++]);
7146af97
JB
714 }
715 XCONS (val)->car = car;
716 XCONS (val)->cdr = cdr;
717 consing_since_gc += sizeof (struct Lisp_Cons);
310ea200 718 cons_cells_consed++;
7146af97
JB
719 return val;
720}
c0f51373
RS
721\f
722/* Make a list of 2, 3, 4 or 5 specified objects. */
723
724Lisp_Object
725list2 (arg1, arg2)
726 Lisp_Object arg1, arg2;
727{
728 return Fcons (arg1, Fcons (arg2, Qnil));
729}
730
731Lisp_Object
732list3 (arg1, arg2, arg3)
733 Lisp_Object arg1, arg2, arg3;
734{
735 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Qnil)));
736}
737
738Lisp_Object
739list4 (arg1, arg2, arg3, arg4)
740 Lisp_Object arg1, arg2, arg3, arg4;
741{
742 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4, Qnil))));
743}
744
745Lisp_Object
746list5 (arg1, arg2, arg3, arg4, arg5)
747 Lisp_Object arg1, arg2, arg3, arg4, arg5;
748{
749 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4,
750 Fcons (arg5, Qnil)))));
751}
7146af97
JB
752
753DEFUN ("list", Flist, Slist, 0, MANY, 0,
754 "Return a newly created list with specified arguments as elements.\n\
755Any number of arguments, even zero arguments, are allowed.")
756 (nargs, args)
757 int nargs;
758 register Lisp_Object *args;
759{
128a5b55
RS
760 register Lisp_Object val;
761 val = Qnil;
7146af97 762
128a5b55
RS
763 while (nargs > 0)
764 {
765 nargs--;
766 val = Fcons (args[nargs], val);
767 }
7146af97
JB
768 return val;
769}
770
771DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0,
772 "Return a newly created list of length LENGTH, with each element being INIT.")
773 (length, init)
774 register Lisp_Object length, init;
775{
776 register Lisp_Object val;
777 register int size;
778
c9dad5ed
KH
779 CHECK_NATNUM (length, 0);
780 size = XFASTINT (length);
7146af97
JB
781
782 val = Qnil;
783 while (size-- > 0)
784 val = Fcons (init, val);
785 return val;
786}
787\f
788/* Allocation of vectors */
789
790struct Lisp_Vector *all_vectors;
791
1825c68d
KH
792struct Lisp_Vector *
793allocate_vectorlike (len)
794 EMACS_INT len;
795{
796 struct Lisp_Vector *p;
797
3c06d205 798 allocating_for_lisp = 1;
d1658221
RS
799#ifdef DOUG_LEA_MALLOC
800 /* Prevent mmap'ing the chunk (which is potentially very large). */
801 mallopt (M_MMAP_MAX, 0);
802#endif
1825c68d
KH
803 p = (struct Lisp_Vector *)xmalloc (sizeof (struct Lisp_Vector)
804 + (len - 1) * sizeof (Lisp_Object));
d1658221
RS
805#ifdef DOUG_LEA_MALLOC
806 /* Back to a reasonable maximum of mmap'ed areas. */
807 mallopt (M_MMAP_MAX, 64);
808#endif
3c06d205 809 allocating_for_lisp = 0;
1825c68d
KH
810 VALIDATE_LISP_STORAGE (p, 0);
811 consing_since_gc += (sizeof (struct Lisp_Vector)
812 + (len - 1) * sizeof (Lisp_Object));
310ea200 813 vector_cells_consed += len;
1825c68d
KH
814
815 p->next = all_vectors;
816 all_vectors = p;
817 return p;
818}
819
7146af97
JB
820DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0,
821 "Return a newly created vector of length LENGTH, with each element being INIT.\n\
822See also the function `vector'.")
823 (length, init)
824 register Lisp_Object length, init;
825{
1825c68d
KH
826 Lisp_Object vector;
827 register EMACS_INT sizei;
828 register int index;
7146af97
JB
829 register struct Lisp_Vector *p;
830
c9dad5ed
KH
831 CHECK_NATNUM (length, 0);
832 sizei = XFASTINT (length);
7146af97 833
1825c68d 834 p = allocate_vectorlike (sizei);
7146af97 835 p->size = sizei;
7146af97
JB
836 for (index = 0; index < sizei; index++)
837 p->contents[index] = init;
838
1825c68d 839 XSETVECTOR (vector, p);
7146af97
JB
840 return vector;
841}
842
a59de17b 843DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0,
c58b2b4d 844 "Return a newly created char-table, with purpose PURPOSE.\n\
7b07587b 845Each element is initialized to INIT, which defaults to nil.\n\
d7cd5d4f 846PURPOSE should be a symbol which has a `char-table-extra-slots' property.\n\
a59de17b
RS
847The property's value should be an integer between 0 and 10.")
848 (purpose, init)
849 register Lisp_Object purpose, init;
7b07587b
RS
850{
851 Lisp_Object vector;
a59de17b
RS
852 Lisp_Object n;
853 CHECK_SYMBOL (purpose, 1);
0551bde3 854 n = Fget (purpose, Qchar_table_extra_slots);
a59de17b 855 CHECK_NUMBER (n, 0);
7b07587b
RS
856 if (XINT (n) < 0 || XINT (n) > 10)
857 args_out_of_range (n, Qnil);
858 /* Add 2 to the size for the defalt and parent slots. */
859 vector = Fmake_vector (make_number (CHAR_TABLE_STANDARD_SLOTS + XINT (n)),
860 init);
0551bde3 861 XCHAR_TABLE (vector)->top = Qt;
c96a008c 862 XCHAR_TABLE (vector)->parent = Qnil;
a59de17b 863 XCHAR_TABLE (vector)->purpose = purpose;
7b07587b
RS
864 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
865 return vector;
866}
867
0551bde3
KH
868/* Return a newly created sub char table with default value DEFALT.
869 Since a sub char table does not appear as a top level Emacs Lisp
870 object, we don't need a Lisp interface to make it. */
871
872Lisp_Object
873make_sub_char_table (defalt)
874 Lisp_Object defalt;
875{
876 Lisp_Object vector
877 = Fmake_vector (make_number (SUB_CHAR_TABLE_STANDARD_SLOTS), Qnil);
878 XCHAR_TABLE (vector)->top = Qnil;
879 XCHAR_TABLE (vector)->defalt = defalt;
880 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
881 return vector;
882}
883
7146af97
JB
884DEFUN ("vector", Fvector, Svector, 0, MANY, 0,
885 "Return a newly created vector with specified arguments as elements.\n\
886Any number of arguments, even zero arguments, are allowed.")
887 (nargs, args)
888 register int nargs;
889 Lisp_Object *args;
890{
891 register Lisp_Object len, val;
892 register int index;
893 register struct Lisp_Vector *p;
894
67ba9986 895 XSETFASTINT (len, nargs);
7146af97
JB
896 val = Fmake_vector (len, Qnil);
897 p = XVECTOR (val);
898 for (index = 0; index < nargs; index++)
899 p->contents[index] = args[index];
900 return val;
901}
902
903DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0,
904 "Create a byte-code object with specified arguments as elements.\n\
905The arguments should be the arglist, bytecode-string, constant vector,\n\
906stack size, (optional) doc string, and (optional) interactive spec.\n\
907The first four arguments are required; at most six have any\n\
908significance.")
909 (nargs, args)
910 register int nargs;
911 Lisp_Object *args;
912{
913 register Lisp_Object len, val;
914 register int index;
915 register struct Lisp_Vector *p;
916
67ba9986 917 XSETFASTINT (len, nargs);
265a9e55 918 if (!NILP (Vpurify_flag))
5a053ea9 919 val = make_pure_vector ((EMACS_INT) nargs);
7146af97
JB
920 else
921 val = Fmake_vector (len, Qnil);
922 p = XVECTOR (val);
923 for (index = 0; index < nargs; index++)
924 {
265a9e55 925 if (!NILP (Vpurify_flag))
7146af97
JB
926 args[index] = Fpurecopy (args[index]);
927 p->contents[index] = args[index];
928 }
50aee051 929 XSETCOMPILED (val, p);
7146af97
JB
930 return val;
931}
932\f
933/* Allocation of symbols.
934 Just like allocation of conses!
935
936 Each symbol_block is just under 1020 bytes long,
937 since malloc really allocates in units of powers of two
938 and uses 4 bytes for its own overhead. */
939
940#define SYMBOL_BLOCK_SIZE \
941 ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol))
942
943struct symbol_block
944 {
945 struct symbol_block *next;
946 struct Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE];
947 };
948
949struct symbol_block *symbol_block;
950int symbol_block_index;
951
952struct Lisp_Symbol *symbol_free_list;
953
954void
955init_symbol ()
956{
3c06d205 957 allocating_for_lisp = 1;
7146af97 958 symbol_block = (struct symbol_block *) malloc (sizeof (struct symbol_block));
3c06d205 959 allocating_for_lisp = 0;
7146af97 960 symbol_block->next = 0;
290c8f1e 961 bzero ((char *) symbol_block->symbols, sizeof symbol_block->symbols);
7146af97
JB
962 symbol_block_index = 0;
963 symbol_free_list = 0;
964}
965
966DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0,
967 "Return a newly allocated uninterned symbol whose name is NAME.\n\
968Its value and function definition are void, and its property list is nil.")
54ee42dd
EN
969 (name)
970 Lisp_Object name;
7146af97
JB
971{
972 register Lisp_Object val;
973 register struct Lisp_Symbol *p;
974
54ee42dd 975 CHECK_STRING (name, 0);
7146af97
JB
976
977 if (symbol_free_list)
978 {
45d12a89 979 XSETSYMBOL (val, symbol_free_list);
85481507 980 symbol_free_list = *(struct Lisp_Symbol **)&symbol_free_list->value;
7146af97
JB
981 }
982 else
983 {
984 if (symbol_block_index == SYMBOL_BLOCK_SIZE)
985 {
3c06d205
KH
986 struct symbol_block *new;
987 allocating_for_lisp = 1;
988 new = (struct symbol_block *) xmalloc (sizeof (struct symbol_block));
989 allocating_for_lisp = 0;
7146af97
JB
990 VALIDATE_LISP_STORAGE (new, sizeof *new);
991 new->next = symbol_block;
992 symbol_block = new;
993 symbol_block_index = 0;
994 }
45d12a89 995 XSETSYMBOL (val, &symbol_block->symbols[symbol_block_index++]);
7146af97
JB
996 }
997 p = XSYMBOL (val);
636b7260 998 p->name = XSTRING (name);
47d5b31e 999 p->obarray = Qnil;
7146af97
JB
1000 p->plist = Qnil;
1001 p->value = Qunbound;
1002 p->function = Qunbound;
1003 p->next = 0;
1004 consing_since_gc += sizeof (struct Lisp_Symbol);
310ea200 1005 symbols_consed++;
7146af97
JB
1006 return val;
1007}
1008\f
a0a38eb7 1009/* Allocation of markers and other objects that share that structure.
7146af97
JB
1010 Works like allocation of conses. */
1011
1012#define MARKER_BLOCK_SIZE \
a0a38eb7 1013 ((1020 - sizeof (struct marker_block *)) / sizeof (union Lisp_Misc))
7146af97
JB
1014
1015struct marker_block
1016 {
1017 struct marker_block *next;
a0a38eb7 1018 union Lisp_Misc markers[MARKER_BLOCK_SIZE];
7146af97
JB
1019 };
1020
1021struct marker_block *marker_block;
1022int marker_block_index;
1023
a0a38eb7 1024union Lisp_Misc *marker_free_list;
7146af97
JB
1025
1026void
1027init_marker ()
1028{
3c06d205 1029 allocating_for_lisp = 1;
7146af97 1030 marker_block = (struct marker_block *) malloc (sizeof (struct marker_block));
3c06d205 1031 allocating_for_lisp = 0;
7146af97 1032 marker_block->next = 0;
290c8f1e 1033 bzero ((char *) marker_block->markers, sizeof marker_block->markers);
7146af97
JB
1034 marker_block_index = 0;
1035 marker_free_list = 0;
1036}
1037
a0a38eb7
KH
1038/* Return a newly allocated Lisp_Misc object, with no substructure. */
1039Lisp_Object
1040allocate_misc ()
7146af97 1041{
a0a38eb7 1042 Lisp_Object val;
e065a56e 1043
7146af97
JB
1044 if (marker_free_list)
1045 {
a0a38eb7
KH
1046 XSETMISC (val, marker_free_list);
1047 marker_free_list = marker_free_list->u_free.chain;
7146af97
JB
1048 }
1049 else
1050 {
1051 if (marker_block_index == MARKER_BLOCK_SIZE)
1052 {
3c06d205
KH
1053 struct marker_block *new;
1054 allocating_for_lisp = 1;
1055 new = (struct marker_block *) xmalloc (sizeof (struct marker_block));
1056 allocating_for_lisp = 0;
7146af97
JB
1057 VALIDATE_LISP_STORAGE (new, sizeof *new);
1058 new->next = marker_block;
1059 marker_block = new;
1060 marker_block_index = 0;
1061 }
a0a38eb7 1062 XSETMISC (val, &marker_block->markers[marker_block_index++]);
7146af97 1063 }
a0a38eb7 1064 consing_since_gc += sizeof (union Lisp_Misc);
310ea200 1065 misc_objects_consed++;
a0a38eb7
KH
1066 return val;
1067}
1068
1069DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
1070 "Return a newly allocated marker which does not point at any place.")
1071 ()
1072{
1073 register Lisp_Object val;
1074 register struct Lisp_Marker *p;
1075
1076 val = allocate_misc ();
a5da44fe 1077 XMISCTYPE (val) = Lisp_Misc_Marker;
7146af97
JB
1078 p = XMARKER (val);
1079 p->buffer = 0;
193b12ca
RS
1080 p->bytepos = 0;
1081 p->charpos = 0;
7146af97 1082 p->chain = Qnil;
6ef5c4bd 1083 p->insertion_type = 0;
7146af97
JB
1084 return val;
1085}
fd27a537
RS
1086
1087/* Put MARKER back on the free list after using it temporarily. */
1088
d457598b 1089void
fd27a537
RS
1090free_marker (marker)
1091 Lisp_Object marker;
1092{
5c5631cf
RS
1093 unchain_marker (marker);
1094
fd27a537
RS
1095 XMISC (marker)->u_marker.type = Lisp_Misc_Free;
1096 XMISC (marker)->u_free.chain = marker_free_list;
1097 marker_free_list = XMISC (marker);
1098
1099 total_free_markers++;
1100}
7146af97
JB
1101\f
1102/* Allocation of strings */
1103
1104/* Strings reside inside of string_blocks. The entire data of the string,
1105 both the size and the contents, live in part of the `chars' component of a string_block.
1106 The `pos' component is the index within `chars' of the first free byte.
1107
1108 first_string_block points to the first string_block ever allocated.
1109 Each block points to the next one with its `next' field.
1110 The `prev' fields chain in reverse order.
1111 The last one allocated is the one currently being filled.
1112 current_string_block points to it.
1113
1114 The string_blocks that hold individual large strings
1115 go in a separate chain, started by large_string_blocks. */
1116
1117
1118/* String blocks contain this many useful bytes.
1119 8188 is power of 2, minus 4 for malloc overhead. */
1120#define STRING_BLOCK_SIZE (8188 - sizeof (struct string_block_head))
1121
1122/* A string bigger than this gets its own specially-made string block
1123 if it doesn't fit in the current one. */
1124#define STRING_BLOCK_OUTSIZE 1024
1125
1126struct string_block_head
1127 {
1128 struct string_block *next, *prev;
0e7ff58f 1129 EMACS_INT pos;
7146af97
JB
1130 };
1131
1132struct string_block
1133 {
1134 struct string_block *next, *prev;
42607681 1135 EMACS_INT pos;
7146af97
JB
1136 char chars[STRING_BLOCK_SIZE];
1137 };
1138
1139/* This points to the string block we are now allocating strings. */
1140
1141struct string_block *current_string_block;
1142
1143/* This points to the oldest string block, the one that starts the chain. */
1144
1145struct string_block *first_string_block;
1146
1147/* Last string block in chain of those made for individual large strings. */
1148
1149struct string_block *large_string_blocks;
1150
1151/* If SIZE is the length of a string, this returns how many bytes
1152 the string occupies in a string_block (including padding). */
1153
b3fd4d8f
KH
1154#define STRING_FULLSIZE(size) (((size) + 1 + STRING_BASE_SIZE + STRING_PAD - 1) \
1155 & ~(STRING_PAD - 1))
1156 /* Add 1 for the null terminator,
1157 and add STRING_PAD - 1 as part of rounding up. */
1158
1159#define STRING_PAD (sizeof (EMACS_INT))
1160/* Size of the stuff in the string not including its data. */
1161#define STRING_BASE_SIZE (((sizeof (struct Lisp_String) - 1) / STRING_PAD) * STRING_PAD)
7146af97
JB
1162
1163#if 0
1164#define STRING_FULLSIZE(SIZE) \
42607681 1165(((SIZE) + 2 * sizeof (EMACS_INT)) & ~(sizeof (EMACS_INT) - 1))
7146af97
JB
1166#endif
1167
1168void
1169init_strings ()
1170{
3c06d205 1171 allocating_for_lisp = 1;
7146af97 1172 current_string_block = (struct string_block *) malloc (sizeof (struct string_block));
3c06d205 1173 allocating_for_lisp = 0;
7146af97
JB
1174 first_string_block = current_string_block;
1175 consing_since_gc += sizeof (struct string_block);
1176 current_string_block->next = 0;
1177 current_string_block->prev = 0;
1178 current_string_block->pos = 0;
1179 large_string_blocks = 0;
1180}
c0696668 1181\f
7146af97
JB
1182DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0,
1183 "Return a newly created string of length LENGTH, with each element being INIT.\n\
1184Both LENGTH and INIT must be numbers.")
1185 (length, init)
1186 Lisp_Object length, init;
1187{
1188 register Lisp_Object val;
e54daa22
RS
1189 register unsigned char *p, *end;
1190 int c, nbytes;
7146af97 1191
c9dad5ed 1192 CHECK_NATNUM (length, 0);
7146af97 1193 CHECK_NUMBER (init, 1);
e54daa22 1194
7146af97 1195 c = XINT (init);
e54daa22
RS
1196 if (SINGLE_BYTE_CHAR_P (c))
1197 {
1198 nbytes = XINT (length);
c0696668 1199 val = make_uninit_string (nbytes);
e54daa22
RS
1200 p = XSTRING (val)->data;
1201 end = p + XSTRING (val)->size;
1202 while (p != end)
1203 *p++ = c;
1204 }
1205 else
1206 {
1207 unsigned char work[4], *str;
1208 int len = CHAR_STRING (c, work, str);
1209
1210 nbytes = len * XINT (length);
1211 val = make_uninit_multibyte_string (XINT (length), nbytes);
1212 p = XSTRING (val)->data;
1213 end = p + nbytes;
1214 while (p != end)
1215 {
1216 bcopy (str, p, len);
1217 p += len;
1218 }
1219 }
7146af97
JB
1220 *p = 0;
1221 return val;
1222}
1223
7b07587b 1224DEFUN ("make-bool-vector", Fmake_bool_vector, Smake_bool_vector, 2, 2, 0,
41ab2240
RS
1225 "Return a new bool-vector of length LENGTH, using INIT for as each element.\n\
1226LENGTH must be a number. INIT matters only in whether it is t or nil.")
7b07587b
RS
1227 (length, init)
1228 Lisp_Object length, init;
1229{
1230 register Lisp_Object val;
1231 struct Lisp_Bool_Vector *p;
1232 int real_init, i;
1233 int length_in_chars, length_in_elts, bits_per_value;
1234
1235 CHECK_NATNUM (length, 0);
1236
68be917d 1237 bits_per_value = sizeof (EMACS_INT) * BITS_PER_CHAR;
7b07587b
RS
1238
1239 length_in_elts = (XFASTINT (length) + bits_per_value - 1) / bits_per_value;
a558a05d 1240 length_in_chars = ((XFASTINT (length) + BITS_PER_CHAR - 1) / BITS_PER_CHAR);
7b07587b 1241
38a1965a
KH
1242 /* We must allocate one more elements than LENGTH_IN_ELTS for the
1243 slot `size' of the struct Lisp_Bool_Vector. */
1244 val = Fmake_vector (make_number (length_in_elts + 1), Qnil);
7b07587b
RS
1245 p = XBOOL_VECTOR (val);
1246 /* Get rid of any bits that would cause confusion. */
1247 p->vector_size = 0;
1248 XSETBOOL_VECTOR (val, p);
1249 p->size = XFASTINT (length);
1250
1251 real_init = (NILP (init) ? 0 : -1);
1252 for (i = 0; i < length_in_chars ; i++)
1253 p->data[i] = real_init;
a558a05d
RS
1254 /* Clear the extraneous bits in the last byte. */
1255 if (XINT (length) != length_in_chars * BITS_PER_CHAR)
1256 XBOOL_VECTOR (val)->data[length_in_chars - 1]
1257 &= (1 << (XINT (length) % BITS_PER_CHAR)) - 1;
7b07587b
RS
1258
1259 return val;
1260}
c0696668 1261\f
3f25e183 1262/* Make a string from NBYTES bytes at CONTENTS,
c0696668
RS
1263 and compute the number of characters from the contents.
1264 This string may be unibyte or multibyte, depending on the contents. */
3f25e183 1265
7146af97 1266Lisp_Object
3f25e183
RS
1267make_string (contents, nbytes)
1268 char *contents;
1269 int nbytes;
1270{
1271 register Lisp_Object val;
1272 int nchars = chars_in_text (contents, nbytes);
1273 val = make_uninit_multibyte_string (nchars, nbytes);
1274 bcopy (contents, XSTRING (val)->data, nbytes);
c0696668
RS
1275 if (STRING_BYTES (XSTRING (val)) == XSTRING (val)->size)
1276 SET_STRING_BYTES (XSTRING (val), -1);
3f25e183
RS
1277 return val;
1278}
1279
c0696668 1280/* Make a unibyte string from LENGTH bytes at CONTENTS. */
3f25e183
RS
1281
1282Lisp_Object
1283make_unibyte_string (contents, length)
7146af97
JB
1284 char *contents;
1285 int length;
1286{
1287 register Lisp_Object val;
1288 val = make_uninit_string (length);
1289 bcopy (contents, XSTRING (val)->data, length);
c0696668 1290 SET_STRING_BYTES (XSTRING (val), -1);
7146af97
JB
1291 return val;
1292}
1293
c0696668
RS
1294/* Make a multibyte string from NCHARS characters
1295 occupying NBYTES bytes at CONTENTS. */
3f25e183
RS
1296
1297Lisp_Object
1298make_multibyte_string (contents, nchars, nbytes)
1299 char *contents;
1300 int nchars, nbytes;
1301{
1302 register Lisp_Object val;
1303 val = make_uninit_multibyte_string (nchars, nbytes);
1304 bcopy (contents, XSTRING (val)->data, nbytes);
1305 return val;
1306}
1307
c0696668
RS
1308/* Make a string from NCHARS characters
1309 occupying NBYTES bytes at CONTENTS.
1310 It is a multibyte string if NBYTES != NCHARS. */
1311
1312Lisp_Object
1313make_string_from_bytes (contents, nchars, nbytes)
1314 char *contents;
1315 int nchars, nbytes;
1316{
1317 register Lisp_Object val;
1318 val = make_uninit_multibyte_string (nchars, nbytes);
1319 bcopy (contents, XSTRING (val)->data, nbytes);
1320 if (STRING_BYTES (XSTRING (val)) == XSTRING (val)->size)
1321 SET_STRING_BYTES (XSTRING (val), -1);
1322 return val;
1323}
1324
1325/* Make a multibyte string from NCHARS characters
1326 occupying NBYTES bytes at CONTENTS. */
1327
1328Lisp_Object
1329make_specified_string (contents, nchars, nbytes, multibyte)
1330 char *contents;
1331 int nchars, nbytes;
1332 int multibyte;
1333{
1334 register Lisp_Object val;
1335 val = make_uninit_multibyte_string (nchars, nbytes);
1336 bcopy (contents, XSTRING (val)->data, nbytes);
1337 if (!multibyte)
1338 SET_STRING_BYTES (XSTRING (val), -1);
1339 return val;
1340}
1341
3f25e183
RS
1342/* Make a string from the data at STR,
1343 treating it as multibyte if the data warrants. */
1344
7146af97
JB
1345Lisp_Object
1346build_string (str)
1347 char *str;
1348{
1349 return make_string (str, strlen (str));
1350}
c0696668 1351\f
7146af97
JB
1352Lisp_Object
1353make_uninit_string (length)
1354 int length;
3f25e183 1355{
c0696668
RS
1356 Lisp_Object val;
1357 val = make_uninit_multibyte_string (length, length);
1358 SET_STRING_BYTES (XSTRING (val), -1);
1359 return val;
3f25e183
RS
1360}
1361
1362Lisp_Object
1363make_uninit_multibyte_string (length, length_byte)
1364 int length, length_byte;
7146af97
JB
1365{
1366 register Lisp_Object val;
3f25e183 1367 register int fullsize = STRING_FULLSIZE (length_byte);
7146af97
JB
1368
1369 if (length < 0) abort ();
1370
1371 if (fullsize <= STRING_BLOCK_SIZE - current_string_block->pos)
1372 /* This string can fit in the current string block */
1373 {
45d12a89
KH
1374 XSETSTRING (val,
1375 ((struct Lisp_String *)
1376 (current_string_block->chars + current_string_block->pos)));
7146af97
JB
1377 current_string_block->pos += fullsize;
1378 }
1379 else if (fullsize > STRING_BLOCK_OUTSIZE)
1380 /* This string gets its own string block */
1381 {
3c06d205
KH
1382 register struct string_block *new;
1383 allocating_for_lisp = 1;
d1658221
RS
1384#ifdef DOUG_LEA_MALLOC
1385 /* Prevent mmap'ing the chunk (which is potentially very large). */
1386 mallopt (M_MMAP_MAX, 0);
1387#endif
3c06d205 1388 new = (struct string_block *) xmalloc (sizeof (struct string_block_head) + fullsize);
d1658221
RS
1389#ifdef DOUG_LEA_MALLOC
1390 /* Back to a reasonable maximum of mmap'ed areas. */
1391 mallopt (M_MMAP_MAX, 64);
1392#endif
3c06d205 1393 allocating_for_lisp = 0;
7146af97 1394 VALIDATE_LISP_STORAGE (new, 0);
7146af97
JB
1395 consing_since_gc += sizeof (struct string_block_head) + fullsize;
1396 new->pos = fullsize;
1397 new->next = large_string_blocks;
1398 large_string_blocks = new;
45d12a89
KH
1399 XSETSTRING (val,
1400 ((struct Lisp_String *)
1401 ((struct string_block_head *)new + 1)));
7146af97
JB
1402 }
1403 else
1404 /* Make a new current string block and start it off with this string */
1405 {
3c06d205
KH
1406 register struct string_block *new;
1407 allocating_for_lisp = 1;
1408 new = (struct string_block *) xmalloc (sizeof (struct string_block));
1409 allocating_for_lisp = 0;
7146af97
JB
1410 VALIDATE_LISP_STORAGE (new, sizeof *new);
1411 consing_since_gc += sizeof (struct string_block);
1412 current_string_block->next = new;
1413 new->prev = current_string_block;
1414 new->next = 0;
1415 current_string_block = new;
1416 new->pos = fullsize;
45d12a89
KH
1417 XSETSTRING (val,
1418 (struct Lisp_String *) current_string_block->chars);
7146af97
JB
1419 }
1420
310ea200 1421 string_chars_consed += fullsize;
7146af97 1422 XSTRING (val)->size = length;
fc932ac6 1423 SET_STRING_BYTES (XSTRING (val), length_byte);
3f25e183 1424 XSTRING (val)->data[length_byte] = 0;
d5e35230 1425 INITIALIZE_INTERVAL (XSTRING (val), NULL_INTERVAL);
7146af97
JB
1426
1427 return val;
1428}
c0696668 1429\f
7146af97 1430/* Return a newly created vector or string with specified arguments as
736471d1
RS
1431 elements. If all the arguments are characters that can fit
1432 in a string of events, make a string; otherwise, make a vector.
1433
1434 Any number of arguments, even zero arguments, are allowed. */
7146af97
JB
1435
1436Lisp_Object
736471d1 1437make_event_array (nargs, args)
7146af97
JB
1438 register int nargs;
1439 Lisp_Object *args;
1440{
1441 int i;
1442
1443 for (i = 0; i < nargs; i++)
736471d1 1444 /* The things that fit in a string
c9ca4659
RS
1445 are characters that are in 0...127,
1446 after discarding the meta bit and all the bits above it. */
e687453f 1447 if (!INTEGERP (args[i])
c9ca4659 1448 || (XUINT (args[i]) & ~(-CHAR_META)) >= 0200)
7146af97
JB
1449 return Fvector (nargs, args);
1450
1451 /* Since the loop exited, we know that all the things in it are
1452 characters, so we can make a string. */
1453 {
c13ccad2 1454 Lisp_Object result;
7146af97 1455
50aee051 1456 result = Fmake_string (make_number (nargs), make_number (0));
7146af97 1457 for (i = 0; i < nargs; i++)
736471d1
RS
1458 {
1459 XSTRING (result)->data[i] = XINT (args[i]);
1460 /* Move the meta bit to the right place for a string char. */
1461 if (XINT (args[i]) & CHAR_META)
1462 XSTRING (result)->data[i] |= 0x80;
1463 }
7146af97
JB
1464
1465 return result;
1466 }
1467}
1468\f
1a4f1e2c
JB
1469/* Pure storage management. */
1470
7146af97
JB
1471/* Must get an error if pure storage is full,
1472 since if it cannot hold a large string
1473 it may be able to hold conses that point to that string;
1474 then the string is not protected from gc. */
1475
1476Lisp_Object
c0696668 1477make_pure_string (data, length, length_byte, multibyte)
7146af97
JB
1478 char *data;
1479 int length;
3f25e183 1480 int length_byte;
c0696668 1481 int multibyte;
7146af97 1482{
c0696668 1483
7146af97 1484 register Lisp_Object new;
b3fd4d8f 1485 register int size = STRING_FULLSIZE (length_byte);
7146af97
JB
1486
1487 if (pureptr + size > PURESIZE)
1488 error ("Pure Lisp storage exhausted");
45d12a89 1489 XSETSTRING (new, PUREBEG + pureptr);
7146af97 1490 XSTRING (new)->size = length;
c0696668 1491 SET_STRING_BYTES (XSTRING (new), (multibyte ? length_byte : -1));
3f25e183
RS
1492 bcopy (data, XSTRING (new)->data, length_byte);
1493 XSTRING (new)->data[length_byte] = 0;
06c5fe00
RS
1494
1495 /* We must give strings in pure storage some kind of interval. So we
1496 give them a null one. */
1497#if defined (USE_TEXT_PROPERTIES)
1498 XSTRING (new)->intervals = NULL_INTERVAL;
1499#endif
b3fd4d8f 1500 pureptr += size;
7146af97
JB
1501 return new;
1502}
1503
1504Lisp_Object
1505pure_cons (car, cdr)
1506 Lisp_Object car, cdr;
1507{
1508 register Lisp_Object new;
1509
1510 if (pureptr + sizeof (struct Lisp_Cons) > PURESIZE)
1511 error ("Pure Lisp storage exhausted");
45d12a89 1512 XSETCONS (new, PUREBEG + pureptr);
7146af97
JB
1513 pureptr += sizeof (struct Lisp_Cons);
1514 XCONS (new)->car = Fpurecopy (car);
1515 XCONS (new)->cdr = Fpurecopy (cdr);
1516 return new;
1517}
1518
1519#ifdef LISP_FLOAT_TYPE
1520
1521Lisp_Object
1522make_pure_float (num)
1523 double num;
1524{
1525 register Lisp_Object new;
1526
6d19f28a
JB
1527 /* Make sure that PUREBEG + pureptr is aligned on at least a sizeof
1528 (double) boundary. Some architectures (like the sparc) require
1529 this, and I suspect that floats are rare enough that it's no
1530 tragedy for those that do. */
1531 {
1532 int alignment;
1533 char *p = PUREBEG + pureptr;
1534
fe90ad97
JB
1535#ifdef __GNUC__
1536#if __GNUC__ >= 2
6d19f28a 1537 alignment = __alignof (struct Lisp_Float);
fe90ad97 1538#else
6d19f28a 1539 alignment = sizeof (struct Lisp_Float);
fe90ad97
JB
1540#endif
1541#else
6d19f28a 1542 alignment = sizeof (struct Lisp_Float);
fe90ad97 1543#endif
6d19f28a
JB
1544 p = (char *) (((unsigned long) p + alignment - 1) & - alignment);
1545 pureptr = p - PUREBEG;
1546 }
1a4f1e2c 1547
7146af97
JB
1548 if (pureptr + sizeof (struct Lisp_Float) > PURESIZE)
1549 error ("Pure Lisp storage exhausted");
45d12a89 1550 XSETFLOAT (new, PUREBEG + pureptr);
7146af97
JB
1551 pureptr += sizeof (struct Lisp_Float);
1552 XFLOAT (new)->data = num;
67ba9986 1553 XSETFASTINT (XFLOAT (new)->type, 0); /* bug chasing -wsr */
7146af97
JB
1554 return new;
1555}
1556
1557#endif /* LISP_FLOAT_TYPE */
1558
1559Lisp_Object
1560make_pure_vector (len)
42607681 1561 EMACS_INT len;
7146af97
JB
1562{
1563 register Lisp_Object new;
42607681 1564 register EMACS_INT size = sizeof (struct Lisp_Vector) + (len - 1) * sizeof (Lisp_Object);
7146af97
JB
1565
1566 if (pureptr + size > PURESIZE)
1567 error ("Pure Lisp storage exhausted");
1568
45d12a89 1569 XSETVECTOR (new, PUREBEG + pureptr);
7146af97
JB
1570 pureptr += size;
1571 XVECTOR (new)->size = len;
1572 return new;
1573}
1574
1575DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
1576 "Make a copy of OBJECT in pure storage.\n\
1577Recursively copies contents of vectors and cons cells.\n\
1578Does not copy symbols.")
1579 (obj)
1580 register Lisp_Object obj;
1581{
265a9e55 1582 if (NILP (Vpurify_flag))
7146af97
JB
1583 return obj;
1584
1585 if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
1586 && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
1587 return obj;
1588
d6dd74bb
KH
1589 if (CONSP (obj))
1590 return pure_cons (XCONS (obj)->car, XCONS (obj)->cdr);
7146af97 1591#ifdef LISP_FLOAT_TYPE
d6dd74bb
KH
1592 else if (FLOATP (obj))
1593 return make_pure_float (XFLOAT (obj)->data);
7146af97 1594#endif /* LISP_FLOAT_TYPE */
d6dd74bb 1595 else if (STRINGP (obj))
3f25e183 1596 return make_pure_string (XSTRING (obj)->data, XSTRING (obj)->size,
c0696668
RS
1597 STRING_BYTES (XSTRING (obj)),
1598 STRING_MULTIBYTE (obj));
d6dd74bb
KH
1599 else if (COMPILEDP (obj) || VECTORP (obj))
1600 {
1601 register struct Lisp_Vector *vec;
1602 register int i, size;
1603
1604 size = XVECTOR (obj)->size;
7d535c68
KH
1605 if (size & PSEUDOVECTOR_FLAG)
1606 size &= PSEUDOVECTOR_SIZE_MASK;
01a4d290 1607 vec = XVECTOR (make_pure_vector ((EMACS_INT) size));
d6dd74bb
KH
1608 for (i = 0; i < size; i++)
1609 vec->contents[i] = Fpurecopy (XVECTOR (obj)->contents[i]);
1610 if (COMPILEDP (obj))
1611 XSETCOMPILED (obj, vec);
1612 else
1613 XSETVECTOR (obj, vec);
7146af97
JB
1614 return obj;
1615 }
d6dd74bb
KH
1616 else if (MARKERP (obj))
1617 error ("Attempt to copy a marker to pure storage");
1618 else
1619 return obj;
7146af97
JB
1620}
1621\f
1622/* Recording what needs to be marked for gc. */
1623
1624struct gcpro *gcprolist;
1625
4cb7d267 1626#define NSTATICS 768
7146af97
JB
1627
1628Lisp_Object *staticvec[NSTATICS] = {0};
1629
1630int staticidx = 0;
1631
1632/* Put an entry in staticvec, pointing at the variable whose address is given */
1633
1634void
1635staticpro (varaddress)
1636 Lisp_Object *varaddress;
1637{
1638 staticvec[staticidx++] = varaddress;
1639 if (staticidx >= NSTATICS)
1640 abort ();
1641}
1642
1643struct catchtag
1644 {
1645 Lisp_Object tag;
1646 Lisp_Object val;
1647 struct catchtag *next;
cd67c797
KH
1648#if 0 /* We don't need this for GC purposes */
1649 jmp_buf jmp;
1650#endif
7146af97
JB
1651 };
1652
1653struct backtrace
1654 {
1655 struct backtrace *next;
1656 Lisp_Object *function;
1657 Lisp_Object *args; /* Points to vector of args. */
1658 int nargs; /* length of vector */
1659 /* if nargs is UNEVALLED, args points to slot holding list of unevalled args */
1660 char evalargs;
1661 };
7146af97 1662\f
1a4f1e2c
JB
1663/* Garbage collection! */
1664
e8197642
RS
1665/* Temporarily prevent garbage collection. */
1666
1667int
1668inhibit_garbage_collection ()
1669{
1670 int count = specpdl_ptr - specpdl;
26b926e1 1671 Lisp_Object number;
68be917d 1672 int nbits = min (VALBITS, BITS_PER_INT);
e8197642 1673
b580578b 1674 XSETINT (number, ((EMACS_INT) 1 << (nbits - 1)) - 1);
26b926e1
RS
1675
1676 specbind (Qgc_cons_threshold, number);
e8197642
RS
1677
1678 return count;
1679}
1680
7146af97
JB
1681DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
1682 "Reclaim storage for Lisp objects no longer needed.\n\
1683Returns info on amount of space in use:\n\
1684 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)\n\
1685 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS\n\
fa9e8864 1686 (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS))\n\
7146af97
JB
1687Garbage collection happens automatically if you cons more than\n\
1688`gc-cons-threshold' bytes of Lisp data since previous garbage collection.")
1689 ()
1690{
1691 register struct gcpro *tail;
1692 register struct specbinding *bind;
1693 struct catchtag *catch;
1694 struct handler *handler;
1695 register struct backtrace *backlist;
1696 register Lisp_Object tem;
1697 char *omessage = echo_area_glyphs;
51056d11 1698 int omessage_length = echo_area_glyphs_length;
7da0b0d3 1699 int oldmultibyte = message_enable_multibyte;
7146af97
JB
1700 char stack_top_variable;
1701 register int i;
1702
58595309
KH
1703 /* In case user calls debug_print during GC,
1704 don't let that cause a recursive GC. */
1705 consing_since_gc = 0;
1706
7146af97
JB
1707 /* Save a copy of the contents of the stack, for debugging. */
1708#if MAX_SAVE_STACK > 0
265a9e55 1709 if (NILP (Vpurify_flag))
7146af97
JB
1710 {
1711 i = &stack_top_variable - stack_bottom;
1712 if (i < 0) i = -i;
1713 if (i < MAX_SAVE_STACK)
1714 {
1715 if (stack_copy == 0)
9ac0d9e0 1716 stack_copy = (char *) xmalloc (stack_copy_size = i);
7146af97 1717 else if (stack_copy_size < i)
9ac0d9e0 1718 stack_copy = (char *) xrealloc (stack_copy, (stack_copy_size = i));
7146af97
JB
1719 if (stack_copy)
1720 {
42607681 1721 if ((EMACS_INT) (&stack_top_variable - stack_bottom) > 0)
7146af97
JB
1722 bcopy (stack_bottom, stack_copy, i);
1723 else
1724 bcopy (&stack_top_variable, stack_copy, i);
1725 }
1726 }
1727 }
1728#endif /* MAX_SAVE_STACK > 0 */
1729
299585ee 1730 if (garbage_collection_messages)
691c4285 1731 message1_nolog ("Garbage collecting...");
7146af97 1732
eec7b73d
RS
1733 shrink_regexp_cache ();
1734
4929a878 1735 /* Don't keep undo information around forever. */
7146af97
JB
1736 {
1737 register struct buffer *nextb = all_buffers;
1738
1739 while (nextb)
1740 {
ffd56f97
JB
1741 /* If a buffer's undo list is Qt, that means that undo is
1742 turned off in that buffer. Calling truncate_undo_list on
1743 Qt tends to return NULL, which effectively turns undo back on.
1744 So don't call truncate_undo_list if undo_list is Qt. */
1745 if (! EQ (nextb->undo_list, Qt))
1746 nextb->undo_list
502b9b64
JB
1747 = truncate_undo_list (nextb->undo_list, undo_limit,
1748 undo_strong_limit);
7146af97
JB
1749 nextb = nextb->next;
1750 }
1751 }
1752
1753 gc_in_progress = 1;
1754
c23baf9f 1755 /* clear_marks (); */
7146af97
JB
1756
1757 /* In each "large string", set the MARKBIT of the size field.
1758 That enables mark_object to recognize them. */
1759 {
1760 register struct string_block *b;
1761 for (b = large_string_blocks; b; b = b->next)
1762 ((struct Lisp_String *)(&b->chars[0]))->size |= MARKBIT;
1763 }
1764
1765 /* Mark all the special slots that serve as the roots of accessibility.
1766
1767 Usually the special slots to mark are contained in particular structures.
1768 Then we know no slot is marked twice because the structures don't overlap.
1769 In some cases, the structures point to the slots to be marked.
1770 For these, we use MARKBIT to avoid double marking of the slot. */
1771
1772 for (i = 0; i < staticidx; i++)
1773 mark_object (staticvec[i]);
1774 for (tail = gcprolist; tail; tail = tail->next)
1775 for (i = 0; i < tail->nvars; i++)
1776 if (!XMARKBIT (tail->var[i]))
1777 {
1778 mark_object (&tail->var[i]);
1779 XMARK (tail->var[i]);
1780 }
1781 for (bind = specpdl; bind != specpdl_ptr; bind++)
1782 {
1783 mark_object (&bind->symbol);
1784 mark_object (&bind->old_value);
1785 }
1786 for (catch = catchlist; catch; catch = catch->next)
1787 {
1788 mark_object (&catch->tag);
1789 mark_object (&catch->val);
1790 }
1791 for (handler = handlerlist; handler; handler = handler->next)
1792 {
1793 mark_object (&handler->handler);
1794 mark_object (&handler->var);
1795 }
1796 for (backlist = backtrace_list; backlist; backlist = backlist->next)
1797 {
1798 if (!XMARKBIT (*backlist->function))
1799 {
1800 mark_object (backlist->function);
1801 XMARK (*backlist->function);
1802 }
1803 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
1804 i = 0;
1805 else
1806 i = backlist->nargs - 1;
1807 for (; i >= 0; i--)
1808 if (!XMARKBIT (backlist->args[i]))
1809 {
1810 mark_object (&backlist->args[i]);
1811 XMARK (backlist->args[i]);
1812 }
1813 }
b875d3f7 1814 mark_kboards ();
7146af97 1815
4c315bda
RS
1816 /* Look thru every buffer's undo list
1817 for elements that update markers that were not marked,
1818 and delete them. */
1819 {
1820 register struct buffer *nextb = all_buffers;
1821
1822 while (nextb)
1823 {
1824 /* If a buffer's undo list is Qt, that means that undo is
1825 turned off in that buffer. Calling truncate_undo_list on
1826 Qt tends to return NULL, which effectively turns undo back on.
1827 So don't call truncate_undo_list if undo_list is Qt. */
1828 if (! EQ (nextb->undo_list, Qt))
1829 {
1830 Lisp_Object tail, prev;
1831 tail = nextb->undo_list;
1832 prev = Qnil;
1833 while (CONSP (tail))
1834 {
1835 if (GC_CONSP (XCONS (tail)->car)
1836 && GC_MARKERP (XCONS (XCONS (tail)->car)->car)
1837 && ! XMARKBIT (XMARKER (XCONS (XCONS (tail)->car)->car)->chain))
1838 {
1839 if (NILP (prev))
1840 nextb->undo_list = tail = XCONS (tail)->cdr;
1841 else
1842 tail = XCONS (prev)->cdr = XCONS (tail)->cdr;
1843 }
1844 else
1845 {
1846 prev = tail;
1847 tail = XCONS (tail)->cdr;
1848 }
1849 }
1850 }
1851
1852 nextb = nextb->next;
1853 }
1854 }
1855
7146af97
JB
1856 gc_sweep ();
1857
1858 /* Clear the mark bits that we set in certain root slots. */
1859
1860 for (tail = gcprolist; tail; tail = tail->next)
1861 for (i = 0; i < tail->nvars; i++)
1862 XUNMARK (tail->var[i]);
1863 for (backlist = backtrace_list; backlist; backlist = backlist->next)
1864 {
1865 XUNMARK (*backlist->function);
1866 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
1867 i = 0;
1868 else
1869 i = backlist->nargs - 1;
1870 for (; i >= 0; i--)
1871 XUNMARK (backlist->args[i]);
1872 }
1873 XUNMARK (buffer_defaults.name);
1874 XUNMARK (buffer_local_symbols.name);
1875
c23baf9f 1876 /* clear_marks (); */
7146af97
JB
1877 gc_in_progress = 0;
1878
1879 consing_since_gc = 0;
1880 if (gc_cons_threshold < 10000)
1881 gc_cons_threshold = 10000;
1882
299585ee
RS
1883 if (garbage_collection_messages)
1884 {
1885 if (omessage || minibuf_level > 0)
7da0b0d3 1886 message2_nolog (omessage, omessage_length, oldmultibyte);
299585ee
RS
1887 else
1888 message1_nolog ("Garbage collecting...done");
1889 }
7146af97 1890
7146af97
JB
1891 return Fcons (Fcons (make_number (total_conses),
1892 make_number (total_free_conses)),
1893 Fcons (Fcons (make_number (total_symbols),
1894 make_number (total_free_symbols)),
1895 Fcons (Fcons (make_number (total_markers),
1896 make_number (total_free_markers)),
1897 Fcons (make_number (total_string_size),
1898 Fcons (make_number (total_vector_size),
fa9e8864 1899 Fcons (Fcons
7146af97 1900#ifdef LISP_FLOAT_TYPE
fa9e8864
RS
1901 (make_number (total_floats),
1902 make_number (total_free_floats)),
7146af97 1903#else /* not LISP_FLOAT_TYPE */
fa9e8864 1904 (make_number (0), make_number (0)),
7146af97 1905#endif /* not LISP_FLOAT_TYPE */
fa9e8864
RS
1906 Fcons (Fcons
1907#ifdef USE_TEXT_PROPERTIES
1908 (make_number (total_intervals),
1909 make_number (total_free_intervals)),
1910#else /* not USE_TEXT_PROPERTIES */
1911 (make_number (0), make_number (0)),
1912#endif /* not USE_TEXT_PROPERTIES */
1913 Qnil)))))));
7146af97
JB
1914}
1915\f
1916#if 0
1917static void
1918clear_marks ()
1919{
1920 /* Clear marks on all conses */
1921 {
1922 register struct cons_block *cblk;
1923 register int lim = cons_block_index;
1924
1925 for (cblk = cons_block; cblk; cblk = cblk->next)
1926 {
1927 register int i;
1928 for (i = 0; i < lim; i++)
1929 XUNMARK (cblk->conses[i].car);
1930 lim = CONS_BLOCK_SIZE;
1931 }
1932 }
1933 /* Clear marks on all symbols */
1934 {
1935 register struct symbol_block *sblk;
1936 register int lim = symbol_block_index;
1937
1938 for (sblk = symbol_block; sblk; sblk = sblk->next)
1939 {
1940 register int i;
1941 for (i = 0; i < lim; i++)
1942 {
1943 XUNMARK (sblk->symbols[i].plist);
1944 }
1945 lim = SYMBOL_BLOCK_SIZE;
1946 }
1947 }
1948 /* Clear marks on all markers */
1949 {
1950 register struct marker_block *sblk;
1951 register int lim = marker_block_index;
1952
1953 for (sblk = marker_block; sblk; sblk = sblk->next)
1954 {
1955 register int i;
1956 for (i = 0; i < lim; i++)
a5da44fe 1957 if (sblk->markers[i].u_marker.type == Lisp_Misc_Marker)
a0a38eb7 1958 XUNMARK (sblk->markers[i].u_marker.chain);
7146af97
JB
1959 lim = MARKER_BLOCK_SIZE;
1960 }
1961 }
1962 /* Clear mark bits on all buffers */
1963 {
1964 register struct buffer *nextb = all_buffers;
1965
1966 while (nextb)
1967 {
1968 XUNMARK (nextb->name);
1969 nextb = nextb->next;
1970 }
1971 }
1972}
1973#endif
1974\f
1a4f1e2c
JB
1975/* Mark reference to a Lisp_Object.
1976 If the object referred to has not been seen yet, recursively mark
1977 all the references contained in it.
7146af97 1978
eb8c3be9 1979 If the object referenced is a short string, the referencing slot
7146af97
JB
1980 is threaded into a chain of such slots, pointed to from
1981 the `size' field of the string. The actual string size
1982 lives in the last slot in the chain. We recognize the end
1983 because it is < (unsigned) STRING_BLOCK_SIZE. */
1984
785cd37f
RS
1985#define LAST_MARKED_SIZE 500
1986Lisp_Object *last_marked[LAST_MARKED_SIZE];
1987int last_marked_index;
1988
7146af97 1989static void
436c5811
RS
1990mark_object (argptr)
1991 Lisp_Object *argptr;
7146af97 1992{
436c5811 1993 Lisp_Object *objptr = argptr;
7146af97
JB
1994 register Lisp_Object obj;
1995
9149e743 1996 loop:
7146af97 1997 obj = *objptr;
9149e743 1998 loop2:
7146af97
JB
1999 XUNMARK (obj);
2000
7146af97
JB
2001 if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
2002 && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
2003 return;
2004
785cd37f
RS
2005 last_marked[last_marked_index++] = objptr;
2006 if (last_marked_index == LAST_MARKED_SIZE)
2007 last_marked_index = 0;
2008
0220c518 2009 switch (SWITCH_ENUM_CAST (XGCTYPE (obj)))
7146af97
JB
2010 {
2011 case Lisp_String:
2012 {
2013 register struct Lisp_String *ptr = XSTRING (obj);
2014
d5e35230 2015 MARK_INTERVAL_TREE (ptr->intervals);
7146af97
JB
2016 if (ptr->size & MARKBIT)
2017 /* A large string. Just set ARRAY_MARK_FLAG. */
2018 ptr->size |= ARRAY_MARK_FLAG;
2019 else
2020 {
2021 /* A small string. Put this reference
2022 into the chain of references to it.
1fb577f7 2023 If the address includes MARKBIT, put that bit elsewhere
7146af97
JB
2024 when we store OBJPTR into the size field. */
2025
2026 if (XMARKBIT (*objptr))
2027 {
67ba9986 2028 XSETFASTINT (*objptr, ptr->size);
7146af97
JB
2029 XMARK (*objptr);
2030 }
2031 else
67ba9986 2032 XSETFASTINT (*objptr, ptr->size);
155ffe9c
RS
2033
2034 if ((EMACS_INT) objptr & DONT_COPY_FLAG)
2035 abort ();
1fb577f7
KH
2036 ptr->size = (EMACS_INT) objptr;
2037 if (ptr->size & MARKBIT)
2038 ptr->size ^= MARKBIT | DONT_COPY_FLAG;
7146af97
JB
2039 }
2040 }
2041 break;
2042
76437631 2043 case Lisp_Vectorlike:
30e3190a 2044 if (GC_BUFFERP (obj))
6b552283
KH
2045 {
2046 if (!XMARKBIT (XBUFFER (obj)->name))
2047 mark_buffer (obj);
2048 }
30e3190a 2049 else if (GC_SUBRP (obj))
169ee243
RS
2050 break;
2051 else if (GC_COMPILEDP (obj))
2052 /* We could treat this just like a vector, but it is better
2053 to save the COMPILED_CONSTANTS element for last and avoid recursion
2054 there. */
2055 {
2056 register struct Lisp_Vector *ptr = XVECTOR (obj);
2057 register EMACS_INT size = ptr->size;
2058 /* See comment above under Lisp_Vector. */
2059 struct Lisp_Vector *volatile ptr1 = ptr;
2060 register int i;
2061
2062 if (size & ARRAY_MARK_FLAG)
2063 break; /* Already marked */
2064 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
76437631 2065 size &= PSEUDOVECTOR_SIZE_MASK;
169ee243
RS
2066 for (i = 0; i < size; i++) /* and then mark its elements */
2067 {
2068 if (i != COMPILED_CONSTANTS)
2069 mark_object (&ptr1->contents[i]);
2070 }
2071 /* This cast should be unnecessary, but some Mips compiler complains
2072 (MIPS-ABI + SysVR4, DC/OSx, etc). */
2073 objptr = (Lisp_Object *) &ptr1->contents[COMPILED_CONSTANTS];
2074 goto loop;
2075 }
169ee243
RS
2076 else if (GC_FRAMEP (obj))
2077 {
2078 /* See comment above under Lisp_Vector for why this is volatile. */
2079 register struct frame *volatile ptr = XFRAME (obj);
2080 register EMACS_INT size = ptr->size;
2081
2082 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
2083 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
2084
2085 mark_object (&ptr->name);
894a9d16 2086 mark_object (&ptr->icon_name);
aba6deb8 2087 mark_object (&ptr->title);
169ee243
RS
2088 mark_object (&ptr->focus_frame);
2089 mark_object (&ptr->selected_window);
2090 mark_object (&ptr->minibuffer_window);
2091 mark_object (&ptr->param_alist);
2092 mark_object (&ptr->scroll_bars);
2093 mark_object (&ptr->condemned_scroll_bars);
2094 mark_object (&ptr->menu_bar_items);
2095 mark_object (&ptr->face_alist);
2096 mark_object (&ptr->menu_bar_vector);
2097 mark_object (&ptr->buffer_predicate);
a0e1f185 2098 mark_object (&ptr->buffer_list);
169ee243 2099 }
7b07587b 2100 else if (GC_BOOL_VECTOR_P (obj))
707788bd
RS
2101 {
2102 register struct Lisp_Vector *ptr = XVECTOR (obj);
2103
2104 if (ptr->size & ARRAY_MARK_FLAG)
2105 break; /* Already marked */
2106 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
2107 }
04ff9756 2108 else
169ee243
RS
2109 {
2110 register struct Lisp_Vector *ptr = XVECTOR (obj);
2111 register EMACS_INT size = ptr->size;
2112 /* The reason we use ptr1 is to avoid an apparent hardware bug
2113 that happens occasionally on the FSF's HP 300s.
2114 The bug is that a2 gets clobbered by recursive calls to mark_object.
2115 The clobberage seems to happen during function entry,
2116 perhaps in the moveml instruction.
2117 Yes, this is a crock, but we have to do it. */
2118 struct Lisp_Vector *volatile ptr1 = ptr;
2119 register int i;
2120
2121 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
2122 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
2123 if (size & PSEUDOVECTOR_FLAG)
2124 size &= PSEUDOVECTOR_SIZE_MASK;
2125 for (i = 0; i < size; i++) /* and then mark its elements */
2126 mark_object (&ptr1->contents[i]);
2127 }
2128 break;
7146af97 2129
7146af97
JB
2130 case Lisp_Symbol:
2131 {
41f54422
RS
2132 /* See comment above under Lisp_Vector for why this is volatile. */
2133 register struct Lisp_Symbol *volatile ptr = XSYMBOL (obj);
7146af97
JB
2134 struct Lisp_Symbol *ptrx;
2135
2136 if (XMARKBIT (ptr->plist)) break;
2137 XMARK (ptr->plist);
7146af97
JB
2138 mark_object ((Lisp_Object *) &ptr->value);
2139 mark_object (&ptr->function);
2140 mark_object (&ptr->plist);
8aaa7c8a
JB
2141 XSETTYPE (*(Lisp_Object *) &ptr->name, Lisp_String);
2142 mark_object (&ptr->name);
1c6bb482
RS
2143 /* Note that we do not mark the obarray of the symbol.
2144 It is safe not to do so because nothing accesses that
2145 slot except to check whether it is nil. */
7146af97
JB
2146 ptr = ptr->next;
2147 if (ptr)
2148 {
9149e743
KH
2149 /* For the benefit of the last_marked log. */
2150 objptr = (Lisp_Object *)&XSYMBOL (obj)->next;
b0846f52 2151 ptrx = ptr; /* Use of ptrx avoids compiler bug on Sun */
7146af97 2152 XSETSYMBOL (obj, ptrx);
9149e743
KH
2153 /* We can't goto loop here because *objptr doesn't contain an
2154 actual Lisp_Object with valid datatype field. */
2155 goto loop2;
7146af97
JB
2156 }
2157 }
2158 break;
2159
a0a38eb7 2160 case Lisp_Misc:
a5da44fe 2161 switch (XMISCTYPE (obj))
a0a38eb7
KH
2162 {
2163 case Lisp_Misc_Marker:
2164 XMARK (XMARKER (obj)->chain);
2165 /* DO NOT mark thru the marker's chain.
2166 The buffer's markers chain does not preserve markers from gc;
2167 instead, markers are removed from the chain when freed by gc. */
2168 break;
2169
465edf35
KH
2170 case Lisp_Misc_Buffer_Local_Value:
2171 case Lisp_Misc_Some_Buffer_Local_Value:
2172 {
2173 register struct Lisp_Buffer_Local_Value *ptr
2174 = XBUFFER_LOCAL_VALUE (obj);
a9faeabe
RS
2175 if (XMARKBIT (ptr->realvalue)) break;
2176 XMARK (ptr->realvalue);
465edf35
KH
2177 /* If the cdr is nil, avoid recursion for the car. */
2178 if (EQ (ptr->cdr, Qnil))
2179 {
a9faeabe 2180 objptr = &ptr->realvalue;
465edf35
KH
2181 goto loop;
2182 }
a9faeabe
RS
2183 mark_object (&ptr->realvalue);
2184 mark_object (&ptr->buffer);
2185 mark_object (&ptr->frame);
465edf35
KH
2186 /* See comment above under Lisp_Vector for why not use ptr here. */
2187 objptr = &XBUFFER_LOCAL_VALUE (obj)->cdr;
2188 goto loop;
2189 }
2190
c8616056
KH
2191 case Lisp_Misc_Intfwd:
2192 case Lisp_Misc_Boolfwd:
2193 case Lisp_Misc_Objfwd:
2194 case Lisp_Misc_Buffer_Objfwd:
b875d3f7 2195 case Lisp_Misc_Kboard_Objfwd:
c8616056
KH
2196 /* Don't bother with Lisp_Buffer_Objfwd,
2197 since all markable slots in current buffer marked anyway. */
2198 /* Don't need to do Lisp_Objfwd, since the places they point
2199 are protected with staticpro. */
2200 break;
2201
e202fa34
KH
2202 case Lisp_Misc_Overlay:
2203 {
2204 struct Lisp_Overlay *ptr = XOVERLAY (obj);
2205 if (!XMARKBIT (ptr->plist))
2206 {
2207 XMARK (ptr->plist);
2208 mark_object (&ptr->start);
2209 mark_object (&ptr->end);
2210 objptr = &ptr->plist;
2211 goto loop;
2212 }
2213 }
2214 break;
2215
a0a38eb7
KH
2216 default:
2217 abort ();
2218 }
7146af97
JB
2219 break;
2220
2221 case Lisp_Cons:
7146af97
JB
2222 {
2223 register struct Lisp_Cons *ptr = XCONS (obj);
2224 if (XMARKBIT (ptr->car)) break;
2225 XMARK (ptr->car);
c54ca951
RS
2226 /* If the cdr is nil, avoid recursion for the car. */
2227 if (EQ (ptr->cdr, Qnil))
2228 {
2229 objptr = &ptr->car;
c54ca951
RS
2230 goto loop;
2231 }
7146af97 2232 mark_object (&ptr->car);
41f54422
RS
2233 /* See comment above under Lisp_Vector for why not use ptr here. */
2234 objptr = &XCONS (obj)->cdr;
7146af97
JB
2235 goto loop;
2236 }
2237
2238#ifdef LISP_FLOAT_TYPE
2239 case Lisp_Float:
2240 XMARK (XFLOAT (obj)->type);
2241 break;
2242#endif /* LISP_FLOAT_TYPE */
2243
7146af97 2244 case Lisp_Int:
7146af97
JB
2245 break;
2246
2247 default:
2248 abort ();
2249 }
2250}
2251
2252/* Mark the pointers in a buffer structure. */
2253
2254static void
2255mark_buffer (buf)
2256 Lisp_Object buf;
2257{
7146af97
JB
2258 register struct buffer *buffer = XBUFFER (buf);
2259 register Lisp_Object *ptr;
30e3190a 2260 Lisp_Object base_buffer;
7146af97
JB
2261
2262 /* This is the buffer's markbit */
2263 mark_object (&buffer->name);
2264 XMARK (buffer->name);
2265
30e3190a 2266 MARK_INTERVAL_TREE (BUF_INTERVALS (buffer));
d5e35230 2267
4c315bda
RS
2268 if (CONSP (buffer->undo_list))
2269 {
2270 Lisp_Object tail;
2271 tail = buffer->undo_list;
2272
2273 while (CONSP (tail))
2274 {
2275 register struct Lisp_Cons *ptr = XCONS (tail);
2276
2277 if (XMARKBIT (ptr->car))
2278 break;
2279 XMARK (ptr->car);
2280 if (GC_CONSP (ptr->car)
2281 && ! XMARKBIT (XCONS (ptr->car)->car)
2282 && GC_MARKERP (XCONS (ptr->car)->car))
2283 {
2284 XMARK (XCONS (ptr->car)->car);
2285 mark_object (&XCONS (ptr->car)->cdr);
2286 }
2287 else
2288 mark_object (&ptr->car);
2289
2290 if (CONSP (ptr->cdr))
2291 tail = ptr->cdr;
2292 else
2293 break;
2294 }
2295
2296 mark_object (&XCONS (tail)->cdr);
2297 }
2298 else
2299 mark_object (&buffer->undo_list);
2300
7146af97
JB
2301#if 0
2302 mark_object (buffer->syntax_table);
2303
2304 /* Mark the various string-pointers in the buffer object.
2305 Since the strings may be relocated, we must mark them
2306 in their actual slots. So gc_sweep must convert each slot
2307 back to an ordinary C pointer. */
45d12a89 2308 XSETSTRING (*(Lisp_Object *)&buffer->upcase_table, buffer->upcase_table);
7146af97 2309 mark_object ((Lisp_Object *)&buffer->upcase_table);
45d12a89 2310 XSETSTRING (*(Lisp_Object *)&buffer->downcase_table, buffer->downcase_table);
7146af97
JB
2311 mark_object ((Lisp_Object *)&buffer->downcase_table);
2312
45d12a89 2313 XSETSTRING (*(Lisp_Object *)&buffer->sort_table, buffer->sort_table);
7146af97 2314 mark_object ((Lisp_Object *)&buffer->sort_table);
45d12a89 2315 XSETSTRING (*(Lisp_Object *)&buffer->folding_sort_table, buffer->folding_sort_table);
7146af97
JB
2316 mark_object ((Lisp_Object *)&buffer->folding_sort_table);
2317#endif
2318
2319 for (ptr = &buffer->name + 1;
2320 (char *)ptr < (char *)buffer + sizeof (struct buffer);
2321 ptr++)
2322 mark_object (ptr);
30e3190a
RS
2323
2324 /* If this is an indirect buffer, mark its base buffer. */
6b552283 2325 if (buffer->base_buffer && !XMARKBIT (buffer->base_buffer->name))
30e3190a
RS
2326 {
2327 XSETBUFFER (base_buffer, buffer->base_buffer);
2328 mark_buffer (base_buffer);
2329 }
7146af97 2330}
084b1a0c
KH
2331
2332
b875d3f7 2333/* Mark the pointers in the kboard objects. */
084b1a0c
KH
2334
2335static void
b875d3f7 2336mark_kboards ()
084b1a0c 2337{
b875d3f7 2338 KBOARD *kb;
b94daf1e 2339 Lisp_Object *p;
b875d3f7 2340 for (kb = all_kboards; kb; kb = kb->next_kboard)
084b1a0c 2341 {
b94daf1e
KH
2342 if (kb->kbd_macro_buffer)
2343 for (p = kb->kbd_macro_buffer; p < kb->kbd_macro_ptr; p++)
2344 mark_object (p);
4bfd0c4f
RS
2345 mark_object (&kb->Voverriding_terminal_local_map);
2346 mark_object (&kb->Vlast_command);
2347 mark_object (&kb->Vreal_last_command);
9671abc2 2348 mark_object (&kb->Vprefix_arg);
b875d3f7 2349 mark_object (&kb->kbd_queue);
4bfd0c4f 2350 mark_object (&kb->defining_kbd_macro);
b875d3f7 2351 mark_object (&kb->Vlast_kbd_macro);
b94daf1e 2352 mark_object (&kb->Vsystem_key_alist);
6d03a6fd 2353 mark_object (&kb->system_key_syms);
4bfd0c4f 2354 mark_object (&kb->Vdefault_minibuffer_frame);
084b1a0c
KH
2355 }
2356}
7146af97 2357\f
1a4f1e2c 2358/* Sweep: find all structures not marked, and free them. */
7146af97
JB
2359
2360static void
2361gc_sweep ()
2362{
2363 total_string_size = 0;
2364 compact_strings ();
2365
2366 /* Put all unmarked conses on free list */
2367 {
2368 register struct cons_block *cblk;
6ca94ac9 2369 struct cons_block **cprev = &cons_block;
7146af97
JB
2370 register int lim = cons_block_index;
2371 register int num_free = 0, num_used = 0;
2372
2373 cons_free_list = 0;
2374
6ca94ac9 2375 for (cblk = cons_block; cblk; cblk = *cprev)
7146af97
JB
2376 {
2377 register int i;
6ca94ac9 2378 int this_free = 0;
7146af97
JB
2379 for (i = 0; i < lim; i++)
2380 if (!XMARKBIT (cblk->conses[i].car))
2381 {
6ca94ac9 2382 this_free++;
1cd5fe6a 2383 *(struct Lisp_Cons **)&cblk->conses[i].cdr = cons_free_list;
7146af97
JB
2384 cons_free_list = &cblk->conses[i];
2385 }
2386 else
2387 {
2388 num_used++;
2389 XUNMARK (cblk->conses[i].car);
2390 }
2391 lim = CONS_BLOCK_SIZE;
6ca94ac9
KH
2392 /* If this block contains only free conses and we have already
2393 seen more than two blocks worth of free conses then deallocate
2394 this block. */
6feef451 2395 if (this_free == CONS_BLOCK_SIZE && num_free > CONS_BLOCK_SIZE)
6ca94ac9 2396 {
6ca94ac9
KH
2397 *cprev = cblk->next;
2398 /* Unhook from the free list. */
2399 cons_free_list = *(struct Lisp_Cons **) &cblk->conses[0].cdr;
2400 xfree (cblk);
2401 }
2402 else
6feef451
AS
2403 {
2404 num_free += this_free;
2405 cprev = &cblk->next;
2406 }
7146af97
JB
2407 }
2408 total_conses = num_used;
2409 total_free_conses = num_free;
2410 }
2411
2412#ifdef LISP_FLOAT_TYPE
2413 /* Put all unmarked floats on free list */
2414 {
2415 register struct float_block *fblk;
6ca94ac9 2416 struct float_block **fprev = &float_block;
7146af97
JB
2417 register int lim = float_block_index;
2418 register int num_free = 0, num_used = 0;
2419
2420 float_free_list = 0;
2421
6ca94ac9 2422 for (fblk = float_block; fblk; fblk = *fprev)
7146af97
JB
2423 {
2424 register int i;
6ca94ac9 2425 int this_free = 0;
7146af97
JB
2426 for (i = 0; i < lim; i++)
2427 if (!XMARKBIT (fblk->floats[i].type))
2428 {
6ca94ac9 2429 this_free++;
1cd5fe6a 2430 *(struct Lisp_Float **)&fblk->floats[i].data = float_free_list;
7146af97
JB
2431 float_free_list = &fblk->floats[i];
2432 }
2433 else
2434 {
2435 num_used++;
2436 XUNMARK (fblk->floats[i].type);
2437 }
2438 lim = FLOAT_BLOCK_SIZE;
6ca94ac9
KH
2439 /* If this block contains only free floats and we have already
2440 seen more than two blocks worth of free floats then deallocate
2441 this block. */
6feef451 2442 if (this_free == FLOAT_BLOCK_SIZE && num_free > FLOAT_BLOCK_SIZE)
6ca94ac9 2443 {
6ca94ac9
KH
2444 *fprev = fblk->next;
2445 /* Unhook from the free list. */
2446 float_free_list = *(struct Lisp_Float **) &fblk->floats[0].data;
2447 xfree (fblk);
2448 }
2449 else
6feef451
AS
2450 {
2451 num_free += this_free;
2452 fprev = &fblk->next;
2453 }
7146af97
JB
2454 }
2455 total_floats = num_used;
2456 total_free_floats = num_free;
2457 }
2458#endif /* LISP_FLOAT_TYPE */
2459
d5e35230
JA
2460#ifdef USE_TEXT_PROPERTIES
2461 /* Put all unmarked intervals on free list */
2462 {
2463 register struct interval_block *iblk;
6ca94ac9 2464 struct interval_block **iprev = &interval_block;
d5e35230
JA
2465 register int lim = interval_block_index;
2466 register int num_free = 0, num_used = 0;
2467
2468 interval_free_list = 0;
2469
6ca94ac9 2470 for (iblk = interval_block; iblk; iblk = *iprev)
d5e35230
JA
2471 {
2472 register int i;
6ca94ac9 2473 int this_free = 0;
d5e35230
JA
2474
2475 for (i = 0; i < lim; i++)
2476 {
2477 if (! XMARKBIT (iblk->intervals[i].plist))
2478 {
2479 iblk->intervals[i].parent = interval_free_list;
2480 interval_free_list = &iblk->intervals[i];
6ca94ac9 2481 this_free++;
d5e35230
JA
2482 }
2483 else
2484 {
2485 num_used++;
2486 XUNMARK (iblk->intervals[i].plist);
2487 }
2488 }
2489 lim = INTERVAL_BLOCK_SIZE;
6ca94ac9
KH
2490 /* If this block contains only free intervals and we have already
2491 seen more than two blocks worth of free intervals then
2492 deallocate this block. */
6feef451 2493 if (this_free == INTERVAL_BLOCK_SIZE && num_free > INTERVAL_BLOCK_SIZE)
6ca94ac9 2494 {
6ca94ac9
KH
2495 *iprev = iblk->next;
2496 /* Unhook from the free list. */
2497 interval_free_list = iblk->intervals[0].parent;
2498 xfree (iblk);
2499 }
2500 else
6feef451
AS
2501 {
2502 num_free += this_free;
2503 iprev = &iblk->next;
2504 }
d5e35230
JA
2505 }
2506 total_intervals = num_used;
2507 total_free_intervals = num_free;
2508 }
2509#endif /* USE_TEXT_PROPERTIES */
2510
7146af97
JB
2511 /* Put all unmarked symbols on free list */
2512 {
2513 register struct symbol_block *sblk;
6ca94ac9 2514 struct symbol_block **sprev = &symbol_block;
7146af97
JB
2515 register int lim = symbol_block_index;
2516 register int num_free = 0, num_used = 0;
2517
2518 symbol_free_list = 0;
2519
6ca94ac9 2520 for (sblk = symbol_block; sblk; sblk = *sprev)
7146af97
JB
2521 {
2522 register int i;
6ca94ac9 2523 int this_free = 0;
7146af97
JB
2524 for (i = 0; i < lim; i++)
2525 if (!XMARKBIT (sblk->symbols[i].plist))
2526 {
85481507 2527 *(struct Lisp_Symbol **)&sblk->symbols[i].value = symbol_free_list;
7146af97 2528 symbol_free_list = &sblk->symbols[i];
6ca94ac9 2529 this_free++;
7146af97
JB
2530 }
2531 else
2532 {
2533 num_used++;
2534 sblk->symbols[i].name
2535 = XSTRING (*(Lisp_Object *) &sblk->symbols[i].name);
2536 XUNMARK (sblk->symbols[i].plist);
2537 }
2538 lim = SYMBOL_BLOCK_SIZE;
6ca94ac9
KH
2539 /* If this block contains only free symbols and we have already
2540 seen more than two blocks worth of free symbols then deallocate
2541 this block. */
6feef451 2542 if (this_free == SYMBOL_BLOCK_SIZE && num_free > SYMBOL_BLOCK_SIZE)
6ca94ac9 2543 {
6ca94ac9
KH
2544 *sprev = sblk->next;
2545 /* Unhook from the free list. */
2546 symbol_free_list = *(struct Lisp_Symbol **)&sblk->symbols[0].value;
2547 xfree (sblk);
2548 }
2549 else
6feef451
AS
2550 {
2551 num_free += this_free;
2552 sprev = &sblk->next;
2553 }
7146af97
JB
2554 }
2555 total_symbols = num_used;
2556 total_free_symbols = num_free;
2557 }
2558
2559#ifndef standalone
a9faeabe
RS
2560 /* Put all unmarked misc's on free list.
2561 For a marker, first unchain it from the buffer it points into. */
7146af97
JB
2562 {
2563 register struct marker_block *mblk;
6ca94ac9 2564 struct marker_block **mprev = &marker_block;
7146af97
JB
2565 register int lim = marker_block_index;
2566 register int num_free = 0, num_used = 0;
2567
2568 marker_free_list = 0;
2569
6ca94ac9 2570 for (mblk = marker_block; mblk; mblk = *mprev)
7146af97
JB
2571 {
2572 register int i;
6ca94ac9 2573 int this_free = 0;
26b926e1 2574 EMACS_INT already_free = -1;
fa05e253 2575
7146af97 2576 for (i = 0; i < lim; i++)
465edf35
KH
2577 {
2578 Lisp_Object *markword;
a5da44fe 2579 switch (mblk->markers[i].u_marker.type)
465edf35
KH
2580 {
2581 case Lisp_Misc_Marker:
2582 markword = &mblk->markers[i].u_marker.chain;
2583 break;
2584 case Lisp_Misc_Buffer_Local_Value:
2585 case Lisp_Misc_Some_Buffer_Local_Value:
a9faeabe 2586 markword = &mblk->markers[i].u_buffer_local_value.realvalue;
465edf35 2587 break;
e202fa34
KH
2588 case Lisp_Misc_Overlay:
2589 markword = &mblk->markers[i].u_overlay.plist;
2590 break;
fa05e253
RS
2591 case Lisp_Misc_Free:
2592 /* If the object was already free, keep it
2593 on the free list. */
74d84334 2594 markword = (Lisp_Object *) &already_free;
fa05e253 2595 break;
465edf35
KH
2596 default:
2597 markword = 0;
e202fa34 2598 break;
465edf35
KH
2599 }
2600 if (markword && !XMARKBIT (*markword))
2601 {
2602 Lisp_Object tem;
a5da44fe 2603 if (mblk->markers[i].u_marker.type == Lisp_Misc_Marker)
465edf35
KH
2604 {
2605 /* tem1 avoids Sun compiler bug */
2606 struct Lisp_Marker *tem1 = &mblk->markers[i].u_marker;
2607 XSETMARKER (tem, tem1);
2608 unchain_marker (tem);
2609 }
fa05e253
RS
2610 /* Set the type of the freed object to Lisp_Misc_Free.
2611 We could leave the type alone, since nobody checks it,
465edf35 2612 but this might catch bugs faster. */
a5da44fe 2613 mblk->markers[i].u_marker.type = Lisp_Misc_Free;
465edf35
KH
2614 mblk->markers[i].u_free.chain = marker_free_list;
2615 marker_free_list = &mblk->markers[i];
6ca94ac9 2616 this_free++;
465edf35
KH
2617 }
2618 else
2619 {
2620 num_used++;
2621 if (markword)
2622 XUNMARK (*markword);
2623 }
2624 }
7146af97 2625 lim = MARKER_BLOCK_SIZE;
6ca94ac9
KH
2626 /* If this block contains only free markers and we have already
2627 seen more than two blocks worth of free markers then deallocate
2628 this block. */
6feef451 2629 if (this_free == MARKER_BLOCK_SIZE && num_free > MARKER_BLOCK_SIZE)
6ca94ac9 2630 {
6ca94ac9
KH
2631 *mprev = mblk->next;
2632 /* Unhook from the free list. */
2633 marker_free_list = mblk->markers[0].u_free.chain;
2634 xfree (mblk);
2635 }
2636 else
6feef451
AS
2637 {
2638 num_free += this_free;
2639 mprev = &mblk->next;
2640 }
7146af97
JB
2641 }
2642
2643 total_markers = num_used;
2644 total_free_markers = num_free;
2645 }
2646
2647 /* Free all unmarked buffers */
2648 {
2649 register struct buffer *buffer = all_buffers, *prev = 0, *next;
2650
2651 while (buffer)
2652 if (!XMARKBIT (buffer->name))
2653 {
2654 if (prev)
2655 prev->next = buffer->next;
2656 else
2657 all_buffers = buffer->next;
2658 next = buffer->next;
9ac0d9e0 2659 xfree (buffer);
7146af97
JB
2660 buffer = next;
2661 }
2662 else
2663 {
2664 XUNMARK (buffer->name);
30e3190a 2665 UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer));
7146af97
JB
2666
2667#if 0
2668 /* Each `struct Lisp_String *' was turned into a Lisp_Object
2669 for purposes of marking and relocation.
2670 Turn them back into C pointers now. */
2671 buffer->upcase_table
2672 = XSTRING (*(Lisp_Object *)&buffer->upcase_table);
2673 buffer->downcase_table
2674 = XSTRING (*(Lisp_Object *)&buffer->downcase_table);
2675 buffer->sort_table
2676 = XSTRING (*(Lisp_Object *)&buffer->sort_table);
2677 buffer->folding_sort_table
2678 = XSTRING (*(Lisp_Object *)&buffer->folding_sort_table);
2679#endif
2680
2681 prev = buffer, buffer = buffer->next;
2682 }
2683 }
2684
2685#endif /* standalone */
2686
2687 /* Free all unmarked vectors */
2688 {
2689 register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next;
2690 total_vector_size = 0;
2691
2692 while (vector)
2693 if (!(vector->size & ARRAY_MARK_FLAG))
2694 {
2695 if (prev)
2696 prev->next = vector->next;
2697 else
2698 all_vectors = vector->next;
2699 next = vector->next;
9ac0d9e0 2700 xfree (vector);
7146af97
JB
2701 vector = next;
2702 }
2703 else
2704 {
2705 vector->size &= ~ARRAY_MARK_FLAG;
fa05e253
RS
2706 if (vector->size & PSEUDOVECTOR_FLAG)
2707 total_vector_size += (PSEUDOVECTOR_SIZE_MASK & vector->size);
2708 else
2709 total_vector_size += vector->size;
7146af97
JB
2710 prev = vector, vector = vector->next;
2711 }
2712 }
2713
2714 /* Free all "large strings" not marked with ARRAY_MARK_FLAG. */
2715 {
2716 register struct string_block *sb = large_string_blocks, *prev = 0, *next;
e8720644 2717 struct Lisp_String *s;
7146af97
JB
2718
2719 while (sb)
e8720644
JB
2720 {
2721 s = (struct Lisp_String *) &sb->chars[0];
2722 if (s->size & ARRAY_MARK_FLAG)
2723 {
2724 ((struct Lisp_String *)(&sb->chars[0]))->size
1fb577f7 2725 &= ~ARRAY_MARK_FLAG & ~MARKBIT;
e8720644
JB
2726 UNMARK_BALANCE_INTERVALS (s->intervals);
2727 total_string_size += ((struct Lisp_String *)(&sb->chars[0]))->size;
2728 prev = sb, sb = sb->next;
2729 }
2730 else
2731 {
2732 if (prev)
2733 prev->next = sb->next;
2734 else
2735 large_string_blocks = sb->next;
2736 next = sb->next;
2737 xfree (sb);
2738 sb = next;
2739 }
2740 }
7146af97
JB
2741 }
2742}
2743\f
1a4f1e2c 2744/* Compactify strings, relocate references, and free empty string blocks. */
7146af97
JB
2745
2746static void
2747compact_strings ()
2748{
2749 /* String block of old strings we are scanning. */
2750 register struct string_block *from_sb;
2751 /* A preceding string block (or maybe the same one)
2752 where we are copying the still-live strings to. */
2753 register struct string_block *to_sb;
2754 int pos;
2755 int to_pos;
2756
2757 to_sb = first_string_block;
2758 to_pos = 0;
2759
2760 /* Scan each existing string block sequentially, string by string. */
2761 for (from_sb = first_string_block; from_sb; from_sb = from_sb->next)
2762 {
2763 pos = 0;
2764 /* POS is the index of the next string in the block. */
2765 while (pos < from_sb->pos)
2766 {
2767 register struct Lisp_String *nextstr
2768 = (struct Lisp_String *) &from_sb->chars[pos];
2769
2770 register struct Lisp_String *newaddr;
42607681 2771 register EMACS_INT size = nextstr->size;
c0696668 2772 EMACS_INT size_byte = nextstr->size_byte;
7146af97
JB
2773
2774 /* NEXTSTR is the old address of the next string.
2775 Just skip it if it isn't marked. */
155ffe9c 2776 if (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE)
7146af97
JB
2777 {
2778 /* It is marked, so its size field is really a chain of refs.
2779 Find the end of the chain, where the actual size lives. */
155ffe9c 2780 while (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE)
7146af97 2781 {
155ffe9c
RS
2782 if (size & DONT_COPY_FLAG)
2783 size ^= MARKBIT | DONT_COPY_FLAG;
42607681 2784 size = *(EMACS_INT *)size & ~MARKBIT;
7146af97
JB
2785 }
2786
c0696668
RS
2787 if (size_byte < 0)
2788 size_byte = size;
2789
3f25e183 2790 total_string_size += size_byte;
7146af97
JB
2791
2792 /* If it won't fit in TO_SB, close it out,
2793 and move to the next sb. Keep doing so until
2794 TO_SB reaches a large enough, empty enough string block.
2795 We know that TO_SB cannot advance past FROM_SB here
2796 since FROM_SB is large enough to contain this string.
2797 Any string blocks skipped here
2798 will be patched out and freed later. */
3f25e183 2799 while (to_pos + STRING_FULLSIZE (size_byte)
7146af97
JB
2800 > max (to_sb->pos, STRING_BLOCK_SIZE))
2801 {
2802 to_sb->pos = to_pos;
2803 to_sb = to_sb->next;
2804 to_pos = 0;
2805 }
2806 /* Compute new address of this string
2807 and update TO_POS for the space being used. */
2808 newaddr = (struct Lisp_String *) &to_sb->chars[to_pos];
3f25e183 2809 to_pos += STRING_FULLSIZE (size_byte);
7146af97
JB
2810
2811 /* Copy the string itself to the new place. */
2812 if (nextstr != newaddr)
3f25e183 2813 bcopy (nextstr, newaddr, STRING_FULLSIZE (size_byte));
7146af97
JB
2814
2815 /* Go through NEXTSTR's chain of references
2816 and make each slot in the chain point to
2817 the new address of this string. */
2818 size = newaddr->size;
155ffe9c 2819 while (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE)
7146af97
JB
2820 {
2821 register Lisp_Object *objptr;
155ffe9c
RS
2822 if (size & DONT_COPY_FLAG)
2823 size ^= MARKBIT | DONT_COPY_FLAG;
7146af97
JB
2824 objptr = (Lisp_Object *)size;
2825
2826 size = XFASTINT (*objptr) & ~MARKBIT;
2827 if (XMARKBIT (*objptr))
2828 {
45d12a89 2829 XSETSTRING (*objptr, newaddr);
7146af97
JB
2830 XMARK (*objptr);
2831 }
2832 else
45d12a89 2833 XSETSTRING (*objptr, newaddr);
7146af97
JB
2834 }
2835 /* Store the actual size in the size field. */
2836 newaddr->size = size;
e8720644 2837
5f60ed47 2838#ifdef USE_TEXT_PROPERTIES
e8720644
JB
2839 /* Now that the string has been relocated, rebalance its
2840 interval tree, and update the tree's parent pointer. */
2841 if (! NULL_INTERVAL_P (newaddr->intervals))
2842 {
2843 UNMARK_BALANCE_INTERVALS (newaddr->intervals);
45d12a89
KH
2844 XSETSTRING (* (Lisp_Object *) &newaddr->intervals->parent,
2845 newaddr);
e8720644 2846 }
5f60ed47 2847#endif /* USE_TEXT_PROPERTIES */
7146af97 2848 }
c0696668
RS
2849 else if (size_byte < 0)
2850 size_byte = size;
2851
3f25e183 2852 pos += STRING_FULLSIZE (size_byte);
7146af97
JB
2853 }
2854 }
2855
2856 /* Close out the last string block still used and free any that follow. */
2857 to_sb->pos = to_pos;
2858 current_string_block = to_sb;
2859
2860 from_sb = to_sb->next;
2861 to_sb->next = 0;
2862 while (from_sb)
2863 {
2864 to_sb = from_sb->next;
9ac0d9e0 2865 xfree (from_sb);
7146af97
JB
2866 from_sb = to_sb;
2867 }
2868
2869 /* Free any empty string blocks further back in the chain.
2870 This loop will never free first_string_block, but it is very
2871 unlikely that that one will become empty, so why bother checking? */
2872
2873 from_sb = first_string_block;
2874 while (to_sb = from_sb->next)
2875 {
2876 if (to_sb->pos == 0)
2877 {
2878 if (from_sb->next = to_sb->next)
2879 from_sb->next->prev = from_sb;
9ac0d9e0 2880 xfree (to_sb);
7146af97
JB
2881 }
2882 else
2883 from_sb = to_sb;
2884 }
2885}
2886\f
20d24714
JB
2887/* Debugging aids. */
2888
31ce1c91 2889DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0,
20d24714
JB
2890 "Return the address of the last byte Emacs has allocated, divided by 1024.\n\
2891This may be helpful in debugging Emacs's memory usage.\n\
e41ae81f 2892We divide the value by 1024 to make sure it fits in a Lisp integer.")
20d24714
JB
2893 ()
2894{
2895 Lisp_Object end;
2896
45d12a89 2897 XSETINT (end, (EMACS_INT) sbrk (0) / 1024);
20d24714
JB
2898
2899 return end;
2900}
2901
310ea200
RS
2902DEFUN ("memory-use-counts", Fmemory_use_counts, Smemory_use_counts, 0, 0, 0,
2903 "Return a list of counters that measure how much consing there has been.\n\
2904Each of these counters increments for a certain kind of object.\n\
2905The counters wrap around from the largest positive integer to zero.\n\
2906Garbage collection does not decrease them.\n\
2907The elements of the value are as follows:\n\
2908 (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS)\n\
2909All are in units of 1 = one object consed\n\
2910except for VECTOR-CELLS and STRING-CHARS, which count the total length of\n\
2911objects consed.\n\
2912MISCS include overlays, markers, and some internal types.\n\
2913Frames, windows, buffers, and subprocesses count as vectors\n\
2914 (but the contents of a buffer's text do not count here).")
2915 ()
2916{
2917 Lisp_Object lisp_cons_cells_consed;
2918 Lisp_Object lisp_floats_consed;
2919 Lisp_Object lisp_vector_cells_consed;
2920 Lisp_Object lisp_symbols_consed;
2921 Lisp_Object lisp_string_chars_consed;
2922 Lisp_Object lisp_misc_objects_consed;
2923 Lisp_Object lisp_intervals_consed;
2924
2925 XSETINT (lisp_cons_cells_consed,
290c8f1e 2926 cons_cells_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
310ea200 2927 XSETINT (lisp_floats_consed,
290c8f1e 2928 floats_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
310ea200 2929 XSETINT (lisp_vector_cells_consed,
290c8f1e 2930 vector_cells_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
310ea200 2931 XSETINT (lisp_symbols_consed,
290c8f1e 2932 symbols_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
310ea200 2933 XSETINT (lisp_string_chars_consed,
290c8f1e 2934 string_chars_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
310ea200 2935 XSETINT (lisp_misc_objects_consed,
290c8f1e 2936 misc_objects_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
310ea200 2937 XSETINT (lisp_intervals_consed,
290c8f1e 2938 intervals_consed & ~(((EMACS_INT) 1) << (VALBITS - 1)));
310ea200
RS
2939
2940 return Fcons (lisp_cons_cells_consed,
2941 Fcons (lisp_floats_consed,
2942 Fcons (lisp_vector_cells_consed,
2943 Fcons (lisp_symbols_consed,
2944 Fcons (lisp_string_chars_consed,
2945 Fcons (lisp_misc_objects_consed,
2946 Fcons (lisp_intervals_consed,
2947 Qnil)))))));
2948}
20d24714 2949\f
7146af97
JB
2950/* Initialization */
2951
dfcf069d 2952void
7146af97
JB
2953init_alloc_once ()
2954{
2955 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
2956 pureptr = 0;
4c0be5f4
JB
2957#ifdef HAVE_SHM
2958 pure_size = PURESIZE;
2959#endif
7146af97
JB
2960 all_vectors = 0;
2961 ignore_warnings = 1;
d1658221
RS
2962#ifdef DOUG_LEA_MALLOC
2963 mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */
2964 mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */
2965 mallopt (M_MMAP_MAX, 64); /* max. number of mmap'ed areas */
2966#endif
7146af97
JB
2967 init_strings ();
2968 init_cons ();
2969 init_symbol ();
2970 init_marker ();
2971#ifdef LISP_FLOAT_TYPE
2972 init_float ();
2973#endif /* LISP_FLOAT_TYPE */
d5e35230
JA
2974 INIT_INTERVALS;
2975
276cbe5a
RS
2976#ifdef REL_ALLOC
2977 malloc_hysteresis = 32;
2978#else
2979 malloc_hysteresis = 0;
2980#endif
2981
2982 spare_memory = (char *) malloc (SPARE_MEMORY);
2983
7146af97
JB
2984 ignore_warnings = 0;
2985 gcprolist = 0;
2986 staticidx = 0;
2987 consing_since_gc = 0;
7d179cea 2988 gc_cons_threshold = 100000 * sizeof (Lisp_Object);
7146af97
JB
2989#ifdef VIRT_ADDR_VARIES
2990 malloc_sbrk_unused = 1<<22; /* A large number */
2991 malloc_sbrk_used = 100000; /* as reasonable as any number */
2992#endif /* VIRT_ADDR_VARIES */
2993}
2994
dfcf069d 2995void
7146af97
JB
2996init_alloc ()
2997{
2998 gcprolist = 0;
2999}
3000
3001void
3002syms_of_alloc ()
3003{
3004 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold,
3005 "*Number of bytes of consing between garbage collections.\n\
3006Garbage collection can happen automatically once this many bytes have been\n\
3007allocated since the last garbage collection. All data types count.\n\n\
3008Garbage collection happens automatically only when `eval' is called.\n\n\
3009By binding this temporarily to a large number, you can effectively\n\
3010prevent garbage collection during a part of the program.");
3011
3012 DEFVAR_INT ("pure-bytes-used", &pureptr,
3013 "Number of bytes of sharable Lisp data allocated so far.");
3014
0819585c
RS
3015 DEFVAR_INT ("cons-cells-consed", &cons_cells_consed,
3016 "Number of cons cells that have been consed so far.");
3017
3018 DEFVAR_INT ("floats-consed", &floats_consed,
3019 "Number of floats that have been consed so far.");
3020
3021 DEFVAR_INT ("vector-cells-consed", &vector_cells_consed,
3022 "Number of vector cells that have been consed so far.");
3023
3024 DEFVAR_INT ("symbols-consed", &symbols_consed,
3025 "Number of symbols that have been consed so far.");
3026
3027 DEFVAR_INT ("string-chars-consed", &string_chars_consed,
3028 "Number of string characters that have been consed so far.");
3029
3030 DEFVAR_INT ("misc-objects-consed", &misc_objects_consed,
3031 "Number of miscellaneous objects that have been consed so far.");
3032
3033 DEFVAR_INT ("intervals-consed", &intervals_consed,
3034 "Number of intervals that have been consed so far.");
3035
7146af97
JB
3036#if 0
3037 DEFVAR_INT ("data-bytes-used", &malloc_sbrk_used,
3038 "Number of bytes of unshared memory allocated in this session.");
3039
3040 DEFVAR_INT ("data-bytes-free", &malloc_sbrk_unused,
3041 "Number of bytes of unshared memory remaining available in this session.");
3042#endif
3043
3044 DEFVAR_LISP ("purify-flag", &Vpurify_flag,
3045 "Non-nil means loading Lisp code in order to dump an executable.\n\
3046This means that certain objects should be allocated in shared (pure) space.");
3047
502b9b64 3048 DEFVAR_INT ("undo-limit", &undo_limit,
7146af97 3049 "Keep no more undo information once it exceeds this size.\n\
502b9b64 3050This limit is applied when garbage collection happens.\n\
7146af97
JB
3051The size is counted as the number of bytes occupied,\n\
3052which includes both saved text and other data.");
502b9b64 3053 undo_limit = 20000;
7146af97 3054
502b9b64 3055 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit,
7146af97
JB
3056 "Don't keep more than this much size of undo information.\n\
3057A command which pushes past this size is itself forgotten.\n\
502b9b64 3058This limit is applied when garbage collection happens.\n\
7146af97
JB
3059The size is counted as the number of bytes occupied,\n\
3060which includes both saved text and other data.");
502b9b64 3061 undo_strong_limit = 30000;
7146af97 3062
299585ee
RS
3063 DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages,
3064 "Non-nil means display messages at start and end of garbage collection.");
3065 garbage_collection_messages = 0;
3066
bcb61d60
KH
3067 /* We build this in advance because if we wait until we need it, we might
3068 not be able to allocate the memory to hold it. */
cf3540e4 3069 memory_signal_data
276cbe5a 3070 = Fcons (Qerror, Fcons (build_string ("Memory exhausted--use M-x save-some-buffers RET"), Qnil));
bcb61d60
KH
3071 staticpro (&memory_signal_data);
3072
e8197642
RS
3073 staticpro (&Qgc_cons_threshold);
3074 Qgc_cons_threshold = intern ("gc-cons-threshold");
3075
a59de17b
RS
3076 staticpro (&Qchar_table_extra_slots);
3077 Qchar_table_extra_slots = intern ("char-table-extra-slots");
3078
7146af97
JB
3079 defsubr (&Scons);
3080 defsubr (&Slist);
3081 defsubr (&Svector);
3082 defsubr (&Smake_byte_code);
3083 defsubr (&Smake_list);
3084 defsubr (&Smake_vector);
7b07587b 3085 defsubr (&Smake_char_table);
7146af97 3086 defsubr (&Smake_string);
7b07587b 3087 defsubr (&Smake_bool_vector);
7146af97
JB
3088 defsubr (&Smake_symbol);
3089 defsubr (&Smake_marker);
3090 defsubr (&Spurecopy);
3091 defsubr (&Sgarbage_collect);
20d24714 3092 defsubr (&Smemory_limit);
310ea200 3093 defsubr (&Smemory_use_counts);
7146af97 3094}