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