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