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