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