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