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