(tags-loop-scan): Set default value to an error form.
[bpt/emacs.git] / src / alloc.c
CommitLineData
7146af97 1/* Storage allocation and gc for GNU Emacs Lisp interpreter.
7c299e7a 2 Copyright (C) 1985, 1986, 1988, 1992, 1993 Free Software Foundation, Inc.
7146af97
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
7c299e7a 8the Free Software Foundation; either version 2, or (at your option)
7146af97
JB
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#include "config.h"
22#include "lisp.h"
d5e35230 23#include "intervals.h"
4c0be5f4 24#include "puresize.h"
7146af97
JB
25#ifndef standalone
26#include "buffer.h"
27#include "window.h"
502b9b64 28#include "frame.h"
7146af97
JB
29#endif
30
e065a56e
JB
31#include "syssignal.h"
32
7146af97
JB
33#define max(A,B) ((A) > (B) ? (A) : (B))
34
35/* Macro to verify that storage intended for Lisp objects is not
36 out of range to fit in the space for a pointer.
37 ADDRESS is the start of the block, and SIZE
38 is the amount of space within which objects can start. */
39#define VALIDATE_LISP_STORAGE(address, size) \
40do \
41 { \
42 Lisp_Object val; \
43 XSET (val, Lisp_Cons, (char *) address + size); \
44 if ((char *) XCONS (val) != (char *) address + size) \
45 { \
46 free (address); \
47 memory_full (); \
48 } \
49 } while (0)
50
51/* Number of bytes of consing done since the last gc */
52int consing_since_gc;
53
54/* Number of bytes of consing since gc before another gc should be done. */
55int gc_cons_threshold;
56
57/* Nonzero during gc */
58int gc_in_progress;
59
60#ifndef VIRT_ADDR_VARIES
61extern
62#endif /* VIRT_ADDR_VARIES */
63 int malloc_sbrk_used;
64
65#ifndef VIRT_ADDR_VARIES
66extern
67#endif /* VIRT_ADDR_VARIES */
68 int malloc_sbrk_unused;
69
502b9b64
JB
70/* Two limits controlling how much undo information to keep. */
71int undo_limit;
72int undo_strong_limit;
7146af97
JB
73
74/* Non-nil means defun should do purecopy on the function definition */
75Lisp_Object Vpurify_flag;
76
77#ifndef HAVE_SHM
78int pure[PURESIZE / sizeof (int)] = {0,}; /* Force it into data space! */
79#define PUREBEG (char *) pure
80#else
81#define pure PURE_SEG_BITS /* Use shared memory segment */
82#define PUREBEG (char *)PURE_SEG_BITS
4c0be5f4
JB
83
84/* This variable is used only by the XPNTR macro when HAVE_SHM is
85 defined. If we used the PURESIZE macro directly there, that would
86 make most of emacs dependent on puresize.h, which we don't want -
87 you should be able to change that without too much recompilation.
88 So map_in_data initializes pure_size, and the dependencies work
89 out. */
90int pure_size;
7146af97
JB
91#endif /* not HAVE_SHM */
92
93/* Index in pure at which next pure object will be allocated. */
94int pureptr;
95
96/* If nonzero, this is a warning delivered by malloc and not yet displayed. */
97char *pending_malloc_warning;
98
99/* Maximum amount of C stack to save when a GC happens. */
100
101#ifndef MAX_SAVE_STACK
102#define MAX_SAVE_STACK 16000
103#endif
104
105/* Buffer in which we save a copy of the C stack at each GC. */
106
107char *stack_copy;
108int stack_copy_size;
109
110/* Non-zero means ignore malloc warnings. Set during initialization. */
111int ignore_warnings;
350273a4
JA
112
113static void mark_object (), mark_buffer ();
114static void clear_marks (), gc_sweep ();
115static void compact_strings ();
7146af97
JB
116\f
117Lisp_Object
118malloc_warning_1 (str)
119 Lisp_Object str;
120{
121 Fprinc (str, Vstandard_output);
122 write_string ("\nKilling some buffers may delay running out of memory.\n", -1);
123 write_string ("However, certainly by the time you receive the 95% warning,\n", -1);
124 write_string ("you should clean up, kill this Emacs, and start a new one.", -1);
125 return Qnil;
126}
127
128/* malloc calls this if it finds we are near exhausting storage */
129malloc_warning (str)
130 char *str;
131{
132 pending_malloc_warning = str;
133}
134
135display_malloc_warning ()
136{
137 register Lisp_Object val;
138
139 val = build_string (pending_malloc_warning);
140 pending_malloc_warning = 0;
141 internal_with_output_to_temp_buffer (" *Danger*", malloc_warning_1, val);
142}
143
144/* Called if malloc returns zero */
145memory_full ()
146{
147 error ("Memory exhausted");
148}
149
150/* like malloc and realloc but check for no memory left */
151
152long *
153xmalloc (size)
154 int size;
155{
156 register long *val;
157
158 val = (long *) malloc (size);
159
160 if (!val && size) memory_full ();
161 return val;
162}
163
164long *
165xrealloc (block, size)
166 long *block;
167 int size;
168{
169 register long *val;
170
56d2031b
JB
171 /* We must call malloc explicitly when BLOCK is 0, since some
172 reallocs don't do this. */
173 if (! block)
174 val = (long *) malloc (size);
f048679d 175 else
56d2031b 176 val = (long *) realloc (block, size);
7146af97
JB
177
178 if (!val && size) memory_full ();
179 return val;
180}
181\f
d5e35230
JA
182#ifdef USE_TEXT_PROPERTIES
183#define INTERVAL_BLOCK_SIZE \
184 ((1020 - sizeof (struct interval_block *)) / sizeof (struct interval))
185
186struct interval_block
187 {
188 struct interval_block *next;
189 struct interval intervals[INTERVAL_BLOCK_SIZE];
190 };
191
192struct interval_block *interval_block;
193static int interval_block_index;
194
195INTERVAL interval_free_list;
196
197static void
198init_intervals ()
199{
200 interval_block
201 = (struct interval_block *) malloc (sizeof (struct interval_block));
202 interval_block->next = 0;
203 bzero (interval_block->intervals, sizeof interval_block->intervals);
204 interval_block_index = 0;
205 interval_free_list = 0;
206}
207
208#define INIT_INTERVALS init_intervals ()
209
210INTERVAL
211make_interval ()
212{
213 INTERVAL val;
214
215 if (interval_free_list)
216 {
217 val = interval_free_list;
218 interval_free_list = interval_free_list->parent;
219 }
220 else
221 {
222 if (interval_block_index == INTERVAL_BLOCK_SIZE)
223 {
224 register struct interval_block *newi
225 = (struct interval_block *) malloc (sizeof (struct interval_block));
226
227 if (!newi)
228 memory_full ();
229
230 VALIDATE_LISP_STORAGE (newi, sizeof *newi);
231 newi->next = interval_block;
232 interval_block = newi;
233 interval_block_index = 0;
234 }
235 val = &interval_block->intervals[interval_block_index++];
236 }
237 consing_since_gc += sizeof (struct interval);
238 RESET_INTERVAL (val);
239 return val;
240}
241
242static int total_free_intervals, total_intervals;
243
244/* Mark the pointers of one interval. */
245
246static void
247mark_interval (i)
248 register INTERVAL i;
249{
250 if (XMARKBIT (i->plist))
251 abort ();
252 mark_object (&i->plist);
253 XMARK (i->plist);
254}
255
256static void
257mark_interval_tree (tree)
258 register INTERVAL tree;
259{
260 if (XMARKBIT (tree->plist))
261 return;
262
433c2175 263 traverse_intervals (tree, 1, 0, &mark_interval);
d5e35230
JA
264}
265
266#define MARK_INTERVAL_TREE(i) \
267 { if (!NULL_INTERVAL_P (i)) mark_interval_tree (i); }
268
269#define UNMARK_BALANCE_INTERVALS(i) \
270{ \
271 if (! NULL_INTERVAL_P (i)) \
272 { \
273 XUNMARK ((Lisp_Object) (i->parent)); \
274 i = balance_intervals (i); \
275 } \
276}
277
278#else /* no interval use */
279
280#define INIT_INTERVALS
281
282#define UNMARK_BALANCE_INTERVALS(i)
283#define MARK_INTERVAL_TREE(i)
284
285#endif /* no interval use */
286\f
7146af97
JB
287#ifdef LISP_FLOAT_TYPE
288/* Allocation of float cells, just like conses */
289/* We store float cells inside of float_blocks, allocating a new
290 float_block with malloc whenever necessary. Float cells reclaimed by
291 GC are put on a free list to be reallocated before allocating
292 any new float cells from the latest float_block.
293
294 Each float_block is just under 1020 bytes long,
295 since malloc really allocates in units of powers of two
296 and uses 4 bytes for its own overhead. */
297
298#define FLOAT_BLOCK_SIZE \
299 ((1020 - sizeof (struct float_block *)) / sizeof (struct Lisp_Float))
300
301struct float_block
302 {
303 struct float_block *next;
304 struct Lisp_Float floats[FLOAT_BLOCK_SIZE];
305 };
306
307struct float_block *float_block;
308int float_block_index;
309
310struct Lisp_Float *float_free_list;
311
312void
313init_float ()
314{
315 float_block = (struct float_block *) malloc (sizeof (struct float_block));
316 float_block->next = 0;
317 bzero (float_block->floats, sizeof float_block->floats);
318 float_block_index = 0;
319 float_free_list = 0;
320}
321
322/* Explicitly free a float cell. */
323free_float (ptr)
324 struct Lisp_Float *ptr;
325{
326 XFASTINT (ptr->type) = (int) float_free_list;
327 float_free_list = ptr;
328}
329
330Lisp_Object
331make_float (float_value)
332 double float_value;
333{
334 register Lisp_Object val;
335
336 if (float_free_list)
337 {
338 XSET (val, Lisp_Float, float_free_list);
339 float_free_list = (struct Lisp_Float *) XFASTINT (float_free_list->type);
340 }
341 else
342 {
343 if (float_block_index == FLOAT_BLOCK_SIZE)
344 {
345 register struct float_block *new = (struct float_block *) malloc (sizeof (struct float_block));
346 if (!new) memory_full ();
347 VALIDATE_LISP_STORAGE (new, sizeof *new);
348 new->next = float_block;
349 float_block = new;
350 float_block_index = 0;
351 }
352 XSET (val, Lisp_Float, &float_block->floats[float_block_index++]);
353 }
354 XFLOAT (val)->data = float_value;
355 XFLOAT (val)->type = 0; /* bug chasing -wsr */
356 consing_since_gc += sizeof (struct Lisp_Float);
357 return val;
358}
359
360#endif /* LISP_FLOAT_TYPE */
361\f
362/* Allocation of cons cells */
363/* We store cons cells inside of cons_blocks, allocating a new
364 cons_block with malloc whenever necessary. Cons cells reclaimed by
365 GC are put on a free list to be reallocated before allocating
366 any new cons cells from the latest cons_block.
367
368 Each cons_block is just under 1020 bytes long,
369 since malloc really allocates in units of powers of two
370 and uses 4 bytes for its own overhead. */
371
372#define CONS_BLOCK_SIZE \
373 ((1020 - sizeof (struct cons_block *)) / sizeof (struct Lisp_Cons))
374
375struct cons_block
376 {
377 struct cons_block *next;
378 struct Lisp_Cons conses[CONS_BLOCK_SIZE];
379 };
380
381struct cons_block *cons_block;
382int cons_block_index;
383
384struct Lisp_Cons *cons_free_list;
385
386void
387init_cons ()
388{
389 cons_block = (struct cons_block *) malloc (sizeof (struct cons_block));
390 cons_block->next = 0;
391 bzero (cons_block->conses, sizeof cons_block->conses);
392 cons_block_index = 0;
393 cons_free_list = 0;
394}
395
396/* Explicitly free a cons cell. */
397free_cons (ptr)
398 struct Lisp_Cons *ptr;
399{
400 XFASTINT (ptr->car) = (int) cons_free_list;
401 cons_free_list = ptr;
402}
403
404DEFUN ("cons", Fcons, Scons, 2, 2, 0,
405 "Create a new cons, give it CAR and CDR as components, and return it.")
406 (car, cdr)
407 Lisp_Object car, cdr;
408{
409 register Lisp_Object val;
410
411 if (cons_free_list)
412 {
413 XSET (val, Lisp_Cons, cons_free_list);
414 cons_free_list = (struct Lisp_Cons *) XFASTINT (cons_free_list->car);
415 }
416 else
417 {
418 if (cons_block_index == CONS_BLOCK_SIZE)
419 {
420 register struct cons_block *new = (struct cons_block *) malloc (sizeof (struct cons_block));
421 if (!new) memory_full ();
422 VALIDATE_LISP_STORAGE (new, sizeof *new);
423 new->next = cons_block;
424 cons_block = new;
425 cons_block_index = 0;
426 }
427 XSET (val, Lisp_Cons, &cons_block->conses[cons_block_index++]);
428 }
429 XCONS (val)->car = car;
430 XCONS (val)->cdr = cdr;
431 consing_since_gc += sizeof (struct Lisp_Cons);
432 return val;
433}
434
435DEFUN ("list", Flist, Slist, 0, MANY, 0,
436 "Return a newly created list with specified arguments as elements.\n\
437Any number of arguments, even zero arguments, are allowed.")
438 (nargs, args)
439 int nargs;
440 register Lisp_Object *args;
441{
442 register Lisp_Object len, val, val_tail;
443
444 XFASTINT (len) = nargs;
445 val = Fmake_list (len, Qnil);
446 val_tail = val;
265a9e55 447 while (!NILP (val_tail))
7146af97
JB
448 {
449 XCONS (val_tail)->car = *args++;
450 val_tail = XCONS (val_tail)->cdr;
451 }
452 return val;
453}
454
455DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0,
456 "Return a newly created list of length LENGTH, with each element being INIT.")
457 (length, init)
458 register Lisp_Object length, init;
459{
460 register Lisp_Object val;
461 register int size;
462
463 if (XTYPE (length) != Lisp_Int || XINT (length) < 0)
464 length = wrong_type_argument (Qnatnump, length);
465 size = XINT (length);
466
467 val = Qnil;
468 while (size-- > 0)
469 val = Fcons (init, val);
470 return val;
471}
472\f
473/* Allocation of vectors */
474
475struct Lisp_Vector *all_vectors;
476
477DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0,
478 "Return a newly created vector of length LENGTH, with each element being INIT.\n\
479See also the function `vector'.")
480 (length, init)
481 register Lisp_Object length, init;
482{
483 register int sizei, index;
484 register Lisp_Object vector;
485 register struct Lisp_Vector *p;
486
487 if (XTYPE (length) != Lisp_Int || XINT (length) < 0)
488 length = wrong_type_argument (Qnatnump, length);
489 sizei = XINT (length);
490
491 p = (struct Lisp_Vector *) malloc (sizeof (struct Lisp_Vector) + (sizei - 1) * sizeof (Lisp_Object));
492 if (p == 0)
493 memory_full ();
494 VALIDATE_LISP_STORAGE (p, 0);
495
496 XSET (vector, Lisp_Vector, p);
497 consing_since_gc += sizeof (struct Lisp_Vector) + (sizei - 1) * sizeof (Lisp_Object);
498
499 p->size = sizei;
500 p->next = all_vectors;
501 all_vectors = p;
502
503 for (index = 0; index < sizei; index++)
504 p->contents[index] = init;
505
506 return vector;
507}
508
509DEFUN ("vector", Fvector, Svector, 0, MANY, 0,
510 "Return a newly created vector with specified arguments as elements.\n\
511Any number of arguments, even zero arguments, are allowed.")
512 (nargs, args)
513 register int nargs;
514 Lisp_Object *args;
515{
516 register Lisp_Object len, val;
517 register int index;
518 register struct Lisp_Vector *p;
519
520 XFASTINT (len) = nargs;
521 val = Fmake_vector (len, Qnil);
522 p = XVECTOR (val);
523 for (index = 0; index < nargs; index++)
524 p->contents[index] = args[index];
525 return val;
526}
527
528DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0,
529 "Create a byte-code object with specified arguments as elements.\n\
530The arguments should be the arglist, bytecode-string, constant vector,\n\
531stack size, (optional) doc string, and (optional) interactive spec.\n\
532The first four arguments are required; at most six have any\n\
533significance.")
534 (nargs, args)
535 register int nargs;
536 Lisp_Object *args;
537{
538 register Lisp_Object len, val;
539 register int index;
540 register struct Lisp_Vector *p;
541
542 XFASTINT (len) = nargs;
265a9e55 543 if (!NILP (Vpurify_flag))
7146af97
JB
544 val = make_pure_vector (len);
545 else
546 val = Fmake_vector (len, Qnil);
547 p = XVECTOR (val);
548 for (index = 0; index < nargs; index++)
549 {
265a9e55 550 if (!NILP (Vpurify_flag))
7146af97
JB
551 args[index] = Fpurecopy (args[index]);
552 p->contents[index] = args[index];
553 }
554 XSETTYPE (val, Lisp_Compiled);
555 return val;
556}
557\f
558/* Allocation of symbols.
559 Just like allocation of conses!
560
561 Each symbol_block is just under 1020 bytes long,
562 since malloc really allocates in units of powers of two
563 and uses 4 bytes for its own overhead. */
564
565#define SYMBOL_BLOCK_SIZE \
566 ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol))
567
568struct symbol_block
569 {
570 struct symbol_block *next;
571 struct Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE];
572 };
573
574struct symbol_block *symbol_block;
575int symbol_block_index;
576
577struct Lisp_Symbol *symbol_free_list;
578
579void
580init_symbol ()
581{
582 symbol_block = (struct symbol_block *) malloc (sizeof (struct symbol_block));
583 symbol_block->next = 0;
584 bzero (symbol_block->symbols, sizeof symbol_block->symbols);
585 symbol_block_index = 0;
586 symbol_free_list = 0;
587}
588
589DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0,
590 "Return a newly allocated uninterned symbol whose name is NAME.\n\
591Its value and function definition are void, and its property list is nil.")
592 (str)
593 Lisp_Object str;
594{
595 register Lisp_Object val;
596 register struct Lisp_Symbol *p;
597
598 CHECK_STRING (str, 0);
599
600 if (symbol_free_list)
601 {
602 XSET (val, Lisp_Symbol, symbol_free_list);
603 symbol_free_list
604 = (struct Lisp_Symbol *) XFASTINT (symbol_free_list->value);
605 }
606 else
607 {
608 if (symbol_block_index == SYMBOL_BLOCK_SIZE)
609 {
610 struct symbol_block *new = (struct symbol_block *) malloc (sizeof (struct symbol_block));
611 if (!new) memory_full ();
612 VALIDATE_LISP_STORAGE (new, sizeof *new);
613 new->next = symbol_block;
614 symbol_block = new;
615 symbol_block_index = 0;
616 }
617 XSET (val, Lisp_Symbol, &symbol_block->symbols[symbol_block_index++]);
618 }
619 p = XSYMBOL (val);
620 p->name = XSTRING (str);
621 p->plist = Qnil;
622 p->value = Qunbound;
623 p->function = Qunbound;
624 p->next = 0;
625 consing_since_gc += sizeof (struct Lisp_Symbol);
626 return val;
627}
628\f
629/* Allocation of markers.
630 Works like allocation of conses. */
631
632#define MARKER_BLOCK_SIZE \
633 ((1020 - sizeof (struct marker_block *)) / sizeof (struct Lisp_Marker))
634
635struct marker_block
636 {
637 struct marker_block *next;
638 struct Lisp_Marker markers[MARKER_BLOCK_SIZE];
639 };
640
641struct marker_block *marker_block;
642int marker_block_index;
643
644struct Lisp_Marker *marker_free_list;
645
646void
647init_marker ()
648{
649 marker_block = (struct marker_block *) malloc (sizeof (struct marker_block));
650 marker_block->next = 0;
651 bzero (marker_block->markers, sizeof marker_block->markers);
652 marker_block_index = 0;
653 marker_free_list = 0;
654}
655
656DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
657 "Return a newly allocated marker which does not point at any place.")
658 ()
659{
660 register Lisp_Object val;
661 register struct Lisp_Marker *p;
e065a56e 662
7146af97
JB
663 if (marker_free_list)
664 {
665 XSET (val, Lisp_Marker, marker_free_list);
666 marker_free_list
667 = (struct Lisp_Marker *) XFASTINT (marker_free_list->chain);
668 }
669 else
670 {
671 if (marker_block_index == MARKER_BLOCK_SIZE)
672 {
673 struct marker_block *new = (struct marker_block *) malloc (sizeof (struct marker_block));
674 if (!new) memory_full ();
675 VALIDATE_LISP_STORAGE (new, sizeof *new);
676 new->next = marker_block;
677 marker_block = new;
678 marker_block_index = 0;
679 }
680 XSET (val, Lisp_Marker, &marker_block->markers[marker_block_index++]);
681 }
682 p = XMARKER (val);
683 p->buffer = 0;
684 p->bufpos = 0;
685 p->chain = Qnil;
686 consing_since_gc += sizeof (struct Lisp_Marker);
687 return val;
688}
689\f
690/* Allocation of strings */
691
692/* Strings reside inside of string_blocks. The entire data of the string,
693 both the size and the contents, live in part of the `chars' component of a string_block.
694 The `pos' component is the index within `chars' of the first free byte.
695
696 first_string_block points to the first string_block ever allocated.
697 Each block points to the next one with its `next' field.
698 The `prev' fields chain in reverse order.
699 The last one allocated is the one currently being filled.
700 current_string_block points to it.
701
702 The string_blocks that hold individual large strings
703 go in a separate chain, started by large_string_blocks. */
704
705
706/* String blocks contain this many useful bytes.
707 8188 is power of 2, minus 4 for malloc overhead. */
708#define STRING_BLOCK_SIZE (8188 - sizeof (struct string_block_head))
709
710/* A string bigger than this gets its own specially-made string block
711 if it doesn't fit in the current one. */
712#define STRING_BLOCK_OUTSIZE 1024
713
714struct string_block_head
715 {
716 struct string_block *next, *prev;
717 int pos;
718 };
719
720struct string_block
721 {
722 struct string_block *next, *prev;
723 int pos;
724 char chars[STRING_BLOCK_SIZE];
725 };
726
727/* This points to the string block we are now allocating strings. */
728
729struct string_block *current_string_block;
730
731/* This points to the oldest string block, the one that starts the chain. */
732
733struct string_block *first_string_block;
734
735/* Last string block in chain of those made for individual large strings. */
736
737struct string_block *large_string_blocks;
738
739/* If SIZE is the length of a string, this returns how many bytes
740 the string occupies in a string_block (including padding). */
741
742#define STRING_FULLSIZE(size) (((size) + sizeof (struct Lisp_String) + PAD) \
743 & ~(PAD - 1))
744#define PAD (sizeof (int))
745
746#if 0
747#define STRING_FULLSIZE(SIZE) \
748(((SIZE) + 2 * sizeof (int)) & ~(sizeof (int) - 1))
749#endif
750
751void
752init_strings ()
753{
754 current_string_block = (struct string_block *) malloc (sizeof (struct string_block));
755 first_string_block = current_string_block;
756 consing_since_gc += sizeof (struct string_block);
757 current_string_block->next = 0;
758 current_string_block->prev = 0;
759 current_string_block->pos = 0;
760 large_string_blocks = 0;
761}
762
763DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0,
764 "Return a newly created string of length LENGTH, with each element being INIT.\n\
765Both LENGTH and INIT must be numbers.")
766 (length, init)
767 Lisp_Object length, init;
768{
769 register Lisp_Object val;
770 register unsigned char *p, *end, c;
771
772 if (XTYPE (length) != Lisp_Int || XINT (length) < 0)
773 length = wrong_type_argument (Qnatnump, length);
774 CHECK_NUMBER (init, 1);
775 val = make_uninit_string (XINT (length));
776 c = XINT (init);
777 p = XSTRING (val)->data;
778 end = p + XSTRING (val)->size;
779 while (p != end)
780 *p++ = c;
781 *p = 0;
782 return val;
783}
784
785Lisp_Object
786make_string (contents, length)
787 char *contents;
788 int length;
789{
790 register Lisp_Object val;
791 val = make_uninit_string (length);
792 bcopy (contents, XSTRING (val)->data, length);
793 return val;
794}
795
796Lisp_Object
797build_string (str)
798 char *str;
799{
800 return make_string (str, strlen (str));
801}
802
803Lisp_Object
804make_uninit_string (length)
805 int length;
806{
807 register Lisp_Object val;
808 register int fullsize = STRING_FULLSIZE (length);
809
810 if (length < 0) abort ();
811
812 if (fullsize <= STRING_BLOCK_SIZE - current_string_block->pos)
813 /* This string can fit in the current string block */
814 {
815 XSET (val, Lisp_String,
816 (struct Lisp_String *) (current_string_block->chars + current_string_block->pos));
817 current_string_block->pos += fullsize;
818 }
819 else if (fullsize > STRING_BLOCK_OUTSIZE)
820 /* This string gets its own string block */
821 {
822 register struct string_block *new
823 = (struct string_block *) malloc (sizeof (struct string_block_head) + fullsize);
824 VALIDATE_LISP_STORAGE (new, 0);
825 if (!new) memory_full ();
826 consing_since_gc += sizeof (struct string_block_head) + fullsize;
827 new->pos = fullsize;
828 new->next = large_string_blocks;
829 large_string_blocks = new;
830 XSET (val, Lisp_String,
831 (struct Lisp_String *) ((struct string_block_head *)new + 1));
832 }
833 else
834 /* Make a new current string block and start it off with this string */
835 {
836 register struct string_block *new
837 = (struct string_block *) malloc (sizeof (struct string_block));
838 if (!new) memory_full ();
839 VALIDATE_LISP_STORAGE (new, sizeof *new);
840 consing_since_gc += sizeof (struct string_block);
841 current_string_block->next = new;
842 new->prev = current_string_block;
843 new->next = 0;
844 current_string_block = new;
845 new->pos = fullsize;
846 XSET (val, Lisp_String,
847 (struct Lisp_String *) current_string_block->chars);
848 }
849
850 XSTRING (val)->size = length;
851 XSTRING (val)->data[length] = 0;
d5e35230 852 INITIALIZE_INTERVAL (XSTRING (val), NULL_INTERVAL);
7146af97
JB
853
854 return val;
855}
856
857/* Return a newly created vector or string with specified arguments as
858 elements. If all the arguments are characters, make a string;
859 otherwise, make a vector. Any number of arguments, even zero
860 arguments, are allowed. */
861
862Lisp_Object
0feac52d 863make_array (nargs, args)
7146af97
JB
864 register int nargs;
865 Lisp_Object *args;
866{
867 int i;
868
869 for (i = 0; i < nargs; i++)
870 if (XTYPE (args[i]) != Lisp_Int
871 || (unsigned) XINT (args[i]) >= 0400)
872 return Fvector (nargs, args);
873
874 /* Since the loop exited, we know that all the things in it are
875 characters, so we can make a string. */
876 {
877 Lisp_Object result = Fmake_string (nargs, make_number (0));
878
879 for (i = 0; i < nargs; i++)
880 XSTRING (result)->data[i] = XINT (args[i]);
881
882 return result;
883 }
884}
885\f
886/* Note: the user cannot manipulate ropes portably by referring
887 to the chars of the string, because combining two chars to make a GLYPH
888 depends on endianness. */
889
890DEFUN ("make-rope", Fmake_rope, Smake_rope, 0, MANY, 0,
d5e7c279 891 "Return a newly created rope containing the arguments of this function.\n\
7146af97
JB
892A rope is a string, except that its contents will be treated as an\n\
893array of glyphs, where a glyph is an integer type that may be larger\n\
894than a character. Emacs is normally configured to use 8-bit glyphs,\n\
895so ropes are normally no different from strings. But Emacs may be\n\
896configured to use 16-bit glyphs, to allow the use of larger fonts.\n\
897\n\
898Each argument (which must be an integer) specifies one glyph, whatever\n\
899size glyphs may be.\n\
900\n\
901See variable `buffer-display-table' for the uses of ropes.")
902 (nargs, args)
903 register int nargs;
904 Lisp_Object *args;
905{
906 register int i;
907 register Lisp_Object val;
908 register GLYPH *p;
909
910 val = make_uninit_string (nargs * sizeof (GLYPH));
911
912 p = (GLYPH *) XSTRING (val)->data;
913 for (i = 0; i < nargs; i++)
914 {
915 CHECK_NUMBER (args[i], i);
916 p[i] = XFASTINT (args[i]);
917 }
918 return val;
919}
920
921DEFUN ("rope-elt", Frope_elt, Srope_elt, 2, 2, 0,
922 "Return an element of rope R at index N.\n\
923A rope is a string in which each pair of bytes is considered an element.\n\
924See variable `buffer-display-table' for the uses of ropes.")
925 (r, n)
5bc1518a 926 Lisp_Object r, n;
7146af97
JB
927{
928 CHECK_STRING (r, 0);
929 CHECK_NUMBER (n, 1);
930 if ((XSTRING (r)->size / sizeof (GLYPH)) <= XINT (n) || XINT (n) < 0)
931 args_out_of_range (r, n);
932 return ((GLYPH *) XSTRING (r)->data)[XFASTINT (n)];
933}
934\f
935/* Must get an error if pure storage is full,
936 since if it cannot hold a large string
937 it may be able to hold conses that point to that string;
938 then the string is not protected from gc. */
939
940Lisp_Object
941make_pure_string (data, length)
942 char *data;
943 int length;
944{
945 register Lisp_Object new;
d5e35230 946 register int size = sizeof (int) + INTERVAL_PTR_SIZE + length + 1;
7146af97
JB
947
948 if (pureptr + size > PURESIZE)
949 error ("Pure Lisp storage exhausted");
950 XSET (new, Lisp_String, PUREBEG + pureptr);
951 XSTRING (new)->size = length;
952 bcopy (data, XSTRING (new)->data, length);
953 XSTRING (new)->data[length] = 0;
954 pureptr += (size + sizeof (int) - 1)
955 / sizeof (int) * sizeof (int);
956 return new;
957}
958
959Lisp_Object
960pure_cons (car, cdr)
961 Lisp_Object car, cdr;
962{
963 register Lisp_Object new;
964
965 if (pureptr + sizeof (struct Lisp_Cons) > PURESIZE)
966 error ("Pure Lisp storage exhausted");
967 XSET (new, Lisp_Cons, PUREBEG + pureptr);
968 pureptr += sizeof (struct Lisp_Cons);
969 XCONS (new)->car = Fpurecopy (car);
970 XCONS (new)->cdr = Fpurecopy (cdr);
971 return new;
972}
973
974#ifdef LISP_FLOAT_TYPE
975
976Lisp_Object
977make_pure_float (num)
978 double num;
979{
980 register Lisp_Object new;
981
982 if (pureptr + sizeof (struct Lisp_Float) > PURESIZE)
983 error ("Pure Lisp storage exhausted");
984 XSET (new, Lisp_Float, PUREBEG + pureptr);
985 pureptr += sizeof (struct Lisp_Float);
986 XFLOAT (new)->data = num;
987 XFLOAT (new)->type = 0; /* bug chasing -wsr */
988 return new;
989}
990
991#endif /* LISP_FLOAT_TYPE */
992
993Lisp_Object
994make_pure_vector (len)
995 int len;
996{
997 register Lisp_Object new;
998 register int size = sizeof (struct Lisp_Vector) + (len - 1) * sizeof (Lisp_Object);
999
1000 if (pureptr + size > PURESIZE)
1001 error ("Pure Lisp storage exhausted");
1002
1003 XSET (new, Lisp_Vector, PUREBEG + pureptr);
1004 pureptr += size;
1005 XVECTOR (new)->size = len;
1006 return new;
1007}
1008
1009DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
1010 "Make a copy of OBJECT in pure storage.\n\
1011Recursively copies contents of vectors and cons cells.\n\
1012Does not copy symbols.")
1013 (obj)
1014 register Lisp_Object obj;
1015{
1016 register Lisp_Object new, tem;
1017 register int i;
1018
265a9e55 1019 if (NILP (Vpurify_flag))
7146af97
JB
1020 return obj;
1021
1022 if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
1023 && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
1024 return obj;
1025
1026#ifdef SWITCH_ENUM_BUG
1027 switch ((int) XTYPE (obj))
1028#else
1029 switch (XTYPE (obj))
1030#endif
1031 {
1032 case Lisp_Marker:
1033 error ("Attempt to copy a marker to pure storage");
1034
1035 case Lisp_Cons:
1036 return pure_cons (XCONS (obj)->car, XCONS (obj)->cdr);
1037
1038#ifdef LISP_FLOAT_TYPE
1039 case Lisp_Float:
1040 return make_pure_float (XFLOAT (obj)->data);
1041#endif /* LISP_FLOAT_TYPE */
1042
1043 case Lisp_String:
1044 return make_pure_string (XSTRING (obj)->data, XSTRING (obj)->size);
1045
1046 case Lisp_Compiled:
1047 case Lisp_Vector:
1048 new = make_pure_vector (XVECTOR (obj)->size);
1049 for (i = 0; i < XVECTOR (obj)->size; i++)
1050 {
1051 tem = XVECTOR (obj)->contents[i];
1052 XVECTOR (new)->contents[i] = Fpurecopy (tem);
1053 }
1054 XSETTYPE (new, XTYPE (obj));
1055 return new;
1056
1057 default:
1058 return obj;
1059 }
1060}
1061\f
1062/* Recording what needs to be marked for gc. */
1063
1064struct gcpro *gcprolist;
1065
daa37602 1066#define NSTATICS 512
7146af97
JB
1067
1068Lisp_Object *staticvec[NSTATICS] = {0};
1069
1070int staticidx = 0;
1071
1072/* Put an entry in staticvec, pointing at the variable whose address is given */
1073
1074void
1075staticpro (varaddress)
1076 Lisp_Object *varaddress;
1077{
1078 staticvec[staticidx++] = varaddress;
1079 if (staticidx >= NSTATICS)
1080 abort ();
1081}
1082
1083struct catchtag
1084 {
1085 Lisp_Object tag;
1086 Lisp_Object val;
1087 struct catchtag *next;
1088/* jmp_buf jmp; /* We don't need this for GC purposes */
1089 };
1090
1091struct backtrace
1092 {
1093 struct backtrace *next;
1094 Lisp_Object *function;
1095 Lisp_Object *args; /* Points to vector of args. */
1096 int nargs; /* length of vector */
1097 /* if nargs is UNEVALLED, args points to slot holding list of unevalled args */
1098 char evalargs;
1099 };
1100
1101/* Two flags that are set during GC in the `size' component
1102 of a string or vector. On some machines, these flags
1103 are defined by the m- file to be different bits. */
1104
1105/* On vector, means it has been marked.
1106 On string size field or a reference to a string,
1107 means not the last reference in the chain. */
1108
1109#ifndef ARRAY_MARK_FLAG
1110#define ARRAY_MARK_FLAG ((MARKBIT >> 1) & ~MARKBIT)
1111#endif /* no ARRAY_MARK_FLAG */
1112
1113/* Any slot that is a Lisp_Object can point to a string
1114 and thus can be put on a string's reference-chain
1115 and thus may need to have its ARRAY_MARK_FLAG set.
1116 This includes the slots whose markbits are used to mark
1117 the containing objects. */
1118
1119#if ARRAY_MARK_FLAG == MARKBIT
1120you lose
1121#endif
1122\f
1123int total_conses, total_markers, total_symbols, total_string_size, total_vector_size;
1124int total_free_conses, total_free_markers, total_free_symbols;
1125#ifdef LISP_FLOAT_TYPE
1126int total_free_floats, total_floats;
1127#endif /* LISP_FLOAT_TYPE */
1128
7146af97
JB
1129DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
1130 "Reclaim storage for Lisp objects no longer needed.\n\
1131Returns info on amount of space in use:\n\
1132 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)\n\
1133 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS\n\
1134 (USED-FLOATS . FREE-FLOATS))\n\
1135Garbage collection happens automatically if you cons more than\n\
1136`gc-cons-threshold' bytes of Lisp data since previous garbage collection.")
1137 ()
1138{
1139 register struct gcpro *tail;
1140 register struct specbinding *bind;
1141 struct catchtag *catch;
1142 struct handler *handler;
1143 register struct backtrace *backlist;
1144 register Lisp_Object tem;
1145 char *omessage = echo_area_glyphs;
1146 char stack_top_variable;
1147 register int i;
1148
7146af97
JB
1149 /* Save a copy of the contents of the stack, for debugging. */
1150#if MAX_SAVE_STACK > 0
265a9e55 1151 if (NILP (Vpurify_flag))
7146af97
JB
1152 {
1153 i = &stack_top_variable - stack_bottom;
1154 if (i < 0) i = -i;
1155 if (i < MAX_SAVE_STACK)
1156 {
1157 if (stack_copy == 0)
1158 stack_copy = (char *) malloc (stack_copy_size = i);
1159 else if (stack_copy_size < i)
1160 stack_copy = (char *) realloc (stack_copy, (stack_copy_size = i));
1161 if (stack_copy)
1162 {
1163 if ((int) (&stack_top_variable - stack_bottom) > 0)
1164 bcopy (stack_bottom, stack_copy, i);
1165 else
1166 bcopy (&stack_top_variable, stack_copy, i);
1167 }
1168 }
1169 }
1170#endif /* MAX_SAVE_STACK > 0 */
1171
1172 if (!noninteractive)
1173 message1 ("Garbage collecting...");
1174
1175 /* Don't keep command history around forever */
1176 tem = Fnthcdr (make_number (30), Vcommand_history);
1177 if (CONSP (tem))
1178 XCONS (tem)->cdr = Qnil;
ffd56f97 1179
7146af97
JB
1180 /* Likewise for undo information. */
1181 {
1182 register struct buffer *nextb = all_buffers;
1183
1184 while (nextb)
1185 {
ffd56f97
JB
1186 /* If a buffer's undo list is Qt, that means that undo is
1187 turned off in that buffer. Calling truncate_undo_list on
1188 Qt tends to return NULL, which effectively turns undo back on.
1189 So don't call truncate_undo_list if undo_list is Qt. */
1190 if (! EQ (nextb->undo_list, Qt))
1191 nextb->undo_list
502b9b64
JB
1192 = truncate_undo_list (nextb->undo_list, undo_limit,
1193 undo_strong_limit);
7146af97
JB
1194 nextb = nextb->next;
1195 }
1196 }
1197
1198 gc_in_progress = 1;
1199
1200/* clear_marks (); */
1201
1202 /* In each "large string", set the MARKBIT of the size field.
1203 That enables mark_object to recognize them. */
1204 {
1205 register struct string_block *b;
1206 for (b = large_string_blocks; b; b = b->next)
1207 ((struct Lisp_String *)(&b->chars[0]))->size |= MARKBIT;
1208 }
1209
1210 /* Mark all the special slots that serve as the roots of accessibility.
1211
1212 Usually the special slots to mark are contained in particular structures.
1213 Then we know no slot is marked twice because the structures don't overlap.
1214 In some cases, the structures point to the slots to be marked.
1215 For these, we use MARKBIT to avoid double marking of the slot. */
1216
1217 for (i = 0; i < staticidx; i++)
1218 mark_object (staticvec[i]);
1219 for (tail = gcprolist; tail; tail = tail->next)
1220 for (i = 0; i < tail->nvars; i++)
1221 if (!XMARKBIT (tail->var[i]))
1222 {
1223 mark_object (&tail->var[i]);
1224 XMARK (tail->var[i]);
1225 }
1226 for (bind = specpdl; bind != specpdl_ptr; bind++)
1227 {
1228 mark_object (&bind->symbol);
1229 mark_object (&bind->old_value);
1230 }
1231 for (catch = catchlist; catch; catch = catch->next)
1232 {
1233 mark_object (&catch->tag);
1234 mark_object (&catch->val);
1235 }
1236 for (handler = handlerlist; handler; handler = handler->next)
1237 {
1238 mark_object (&handler->handler);
1239 mark_object (&handler->var);
1240 }
1241 for (backlist = backtrace_list; backlist; backlist = backlist->next)
1242 {
1243 if (!XMARKBIT (*backlist->function))
1244 {
1245 mark_object (backlist->function);
1246 XMARK (*backlist->function);
1247 }
1248 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
1249 i = 0;
1250 else
1251 i = backlist->nargs - 1;
1252 for (; i >= 0; i--)
1253 if (!XMARKBIT (backlist->args[i]))
1254 {
1255 mark_object (&backlist->args[i]);
1256 XMARK (backlist->args[i]);
1257 }
1258 }
1259
1260 gc_sweep ();
1261
1262 /* Clear the mark bits that we set in certain root slots. */
1263
1264 for (tail = gcprolist; tail; tail = tail->next)
1265 for (i = 0; i < tail->nvars; i++)
1266 XUNMARK (tail->var[i]);
1267 for (backlist = backtrace_list; backlist; backlist = backlist->next)
1268 {
1269 XUNMARK (*backlist->function);
1270 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
1271 i = 0;
1272 else
1273 i = backlist->nargs - 1;
1274 for (; i >= 0; i--)
1275 XUNMARK (backlist->args[i]);
1276 }
1277 XUNMARK (buffer_defaults.name);
1278 XUNMARK (buffer_local_symbols.name);
1279
1280/* clear_marks (); */
1281 gc_in_progress = 0;
1282
1283 consing_since_gc = 0;
1284 if (gc_cons_threshold < 10000)
1285 gc_cons_threshold = 10000;
1286
1287 if (omessage)
1288 message1 (omessage);
1289 else if (!noninteractive)
1290 message1 ("Garbage collecting...done");
1291
7146af97
JB
1292 return Fcons (Fcons (make_number (total_conses),
1293 make_number (total_free_conses)),
1294 Fcons (Fcons (make_number (total_symbols),
1295 make_number (total_free_symbols)),
1296 Fcons (Fcons (make_number (total_markers),
1297 make_number (total_free_markers)),
1298 Fcons (make_number (total_string_size),
1299 Fcons (make_number (total_vector_size),
1300
1301#ifdef LISP_FLOAT_TYPE
1302 Fcons (Fcons (make_number (total_floats),
1303 make_number (total_free_floats)),
1304 Qnil)
1305#else /* not LISP_FLOAT_TYPE */
1306 Qnil
1307#endif /* not LISP_FLOAT_TYPE */
1308 )))));
1309}
1310\f
1311#if 0
1312static void
1313clear_marks ()
1314{
1315 /* Clear marks on all conses */
1316 {
1317 register struct cons_block *cblk;
1318 register int lim = cons_block_index;
1319
1320 for (cblk = cons_block; cblk; cblk = cblk->next)
1321 {
1322 register int i;
1323 for (i = 0; i < lim; i++)
1324 XUNMARK (cblk->conses[i].car);
1325 lim = CONS_BLOCK_SIZE;
1326 }
1327 }
1328 /* Clear marks on all symbols */
1329 {
1330 register struct symbol_block *sblk;
1331 register int lim = symbol_block_index;
1332
1333 for (sblk = symbol_block; sblk; sblk = sblk->next)
1334 {
1335 register int i;
1336 for (i = 0; i < lim; i++)
1337 {
1338 XUNMARK (sblk->symbols[i].plist);
1339 }
1340 lim = SYMBOL_BLOCK_SIZE;
1341 }
1342 }
1343 /* Clear marks on all markers */
1344 {
1345 register struct marker_block *sblk;
1346 register int lim = marker_block_index;
1347
1348 for (sblk = marker_block; sblk; sblk = sblk->next)
1349 {
1350 register int i;
1351 for (i = 0; i < lim; i++)
1352 XUNMARK (sblk->markers[i].chain);
1353 lim = MARKER_BLOCK_SIZE;
1354 }
1355 }
1356 /* Clear mark bits on all buffers */
1357 {
1358 register struct buffer *nextb = all_buffers;
1359
1360 while (nextb)
1361 {
1362 XUNMARK (nextb->name);
1363 nextb = nextb->next;
1364 }
1365 }
1366}
1367#endif
1368\f
1369/* Mark reference to a Lisp_Object. If the object referred to
1370 has not been seen yet, recursively mark all the references contained in it.
1371
1372 If the object referenced is a short string, the referrencing slot
1373 is threaded into a chain of such slots, pointed to from
1374 the `size' field of the string. The actual string size
1375 lives in the last slot in the chain. We recognize the end
1376 because it is < (unsigned) STRING_BLOCK_SIZE. */
1377
785cd37f
RS
1378#define LAST_MARKED_SIZE 500
1379Lisp_Object *last_marked[LAST_MARKED_SIZE];
1380int last_marked_index;
1381
7146af97
JB
1382static void
1383mark_object (objptr)
1384 Lisp_Object *objptr;
1385{
1386 register Lisp_Object obj;
1387
1388 obj = *objptr;
1389 XUNMARK (obj);
1390
1391 loop:
1392
1393 if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
1394 && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
1395 return;
1396
785cd37f
RS
1397 last_marked[last_marked_index++] = objptr;
1398 if (last_marked_index == LAST_MARKED_SIZE)
1399 last_marked_index = 0;
1400
7146af97
JB
1401#ifdef SWITCH_ENUM_BUG
1402 switch ((int) XGCTYPE (obj))
1403#else
1404 switch (XGCTYPE (obj))
1405#endif
1406 {
1407 case Lisp_String:
1408 {
1409 register struct Lisp_String *ptr = XSTRING (obj);
1410
d5e35230 1411 MARK_INTERVAL_TREE (ptr->intervals);
7146af97
JB
1412 if (ptr->size & MARKBIT)
1413 /* A large string. Just set ARRAY_MARK_FLAG. */
1414 ptr->size |= ARRAY_MARK_FLAG;
1415 else
1416 {
1417 /* A small string. Put this reference
1418 into the chain of references to it.
1419 The address OBJPTR is even, so if the address
1420 includes MARKBIT, put it in the low bit
1421 when we store OBJPTR into the size field. */
1422
1423 if (XMARKBIT (*objptr))
1424 {
1425 XFASTINT (*objptr) = ptr->size;
1426 XMARK (*objptr);
1427 }
1428 else
1429 XFASTINT (*objptr) = ptr->size;
1430 if ((int)objptr & 1) abort ();
1431 ptr->size = (int) objptr & ~MARKBIT;
1432 if ((int) objptr & MARKBIT)
1433 ptr->size ++;
1434 }
1435 }
1436 break;
1437
1438 case Lisp_Vector:
1439 case Lisp_Window:
1440 case Lisp_Process:
1441 case Lisp_Window_Configuration:
7146af97
JB
1442 {
1443 register struct Lisp_Vector *ptr = XVECTOR (obj);
1444 register int size = ptr->size;
785cd37f 1445 struct Lisp_Vector *volatile ptr1 = ptr;
7146af97
JB
1446 register int i;
1447
1448 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
1449 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
1450 for (i = 0; i < size; i++) /* and then mark its elements */
785cd37f
RS
1451 {
1452 if (ptr != ptr1)
1453 abort ();
1454 mark_object (&ptr->contents[i]);
1455 }
7146af97
JB
1456 }
1457 break;
1458
c54ca951
RS
1459 case Lisp_Compiled:
1460 /* We could treat this just like a vector, but it is better
1461 to save the COMPILED_CONSTANTS element for last and avoid recursion
1462 there. */
1463 {
1464 register struct Lisp_Vector *ptr = XVECTOR (obj);
1465 register int size = ptr->size;
1466 struct Lisp_Vector *volatile ptr1 = ptr;
1467 register int i;
1468
1469 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
1470 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
1471 for (i = 0; i < size; i++) /* and then mark its elements */
1472 {
1473 if (ptr != ptr1)
1474 abort ();
1475 if (i != COMPILED_CONSTANTS)
1476 mark_object (&ptr->contents[i]);
1477 }
1478 objptr = &ptr->contents[COMPILED_CONSTANTS];
1479 obj = *objptr;
1480 goto loop;
1481 }
1482
502b9b64
JB
1483#ifdef MULTI_FRAME
1484 case Lisp_Frame:
7146af97 1485 {
502b9b64 1486 register struct frame *ptr = XFRAME (obj);
7146af97
JB
1487 register int size = ptr->size;
1488 register int i;
1489
1490 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
1491 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
1492
1493 mark_object (&ptr->name);
502b9b64 1494 mark_object (&ptr->focus_frame);
7146af97
JB
1495 mark_object (&ptr->width);
1496 mark_object (&ptr->height);
1497 mark_object (&ptr->selected_window);
1498 mark_object (&ptr->minibuffer_window);
1499 mark_object (&ptr->param_alist);
7c299e7a
JB
1500 mark_object (&ptr->scrollbars);
1501 mark_object (&ptr->condemned_scrollbars);
7146af97
JB
1502 }
1503 break;
42a9cd6a 1504#endif /* not MULTI_FRAME */
7146af97 1505
7146af97
JB
1506 case Lisp_Symbol:
1507 {
1508 register struct Lisp_Symbol *ptr = XSYMBOL (obj);
1509 struct Lisp_Symbol *ptrx;
1510
1511 if (XMARKBIT (ptr->plist)) break;
1512 XMARK (ptr->plist);
7146af97
JB
1513 mark_object ((Lisp_Object *) &ptr->value);
1514 mark_object (&ptr->function);
1515 mark_object (&ptr->plist);
8aaa7c8a
JB
1516 XSETTYPE (*(Lisp_Object *) &ptr->name, Lisp_String);
1517 mark_object (&ptr->name);
7146af97
JB
1518 ptr = ptr->next;
1519 if (ptr)
1520 {
1521 ptrx = ptr; /* Use pf ptrx avoids compiler bug on Sun */
1522 XSETSYMBOL (obj, ptrx);
1523 goto loop;
1524 }
1525 }
1526 break;
1527
1528 case Lisp_Marker:
1529 XMARK (XMARKER (obj)->chain);
1530 /* DO NOT mark thru the marker's chain.
1531 The buffer's markers chain does not preserve markers from gc;
c54ca951 1532 instead, markers are removed from the chain when freed by gc. */
7146af97
JB
1533 break;
1534
1535 case Lisp_Cons:
1536 case Lisp_Buffer_Local_Value:
1537 case Lisp_Some_Buffer_Local_Value:
1538 {
1539 register struct Lisp_Cons *ptr = XCONS (obj);
1540 if (XMARKBIT (ptr->car)) break;
1541 XMARK (ptr->car);
c54ca951
RS
1542 /* If the cdr is nil, avoid recursion for the car. */
1543 if (EQ (ptr->cdr, Qnil))
1544 {
1545 objptr = &ptr->car;
1546 obj = ptr->car;
1547 XUNMARK (obj);
1548 goto loop;
1549 }
7146af97
JB
1550 mark_object (&ptr->car);
1551 objptr = &ptr->cdr;
1552 obj = ptr->cdr;
1553 goto loop;
1554 }
1555
1556#ifdef LISP_FLOAT_TYPE
1557 case Lisp_Float:
1558 XMARK (XFLOAT (obj)->type);
1559 break;
1560#endif /* LISP_FLOAT_TYPE */
1561
1562 case Lisp_Buffer:
1563 if (!XMARKBIT (XBUFFER (obj)->name))
1564 mark_buffer (obj);
1565 break;
1566
1567 case Lisp_Int:
1568 case Lisp_Void:
1569 case Lisp_Subr:
1570 case Lisp_Intfwd:
1571 case Lisp_Boolfwd:
1572 case Lisp_Objfwd:
1573 case Lisp_Buffer_Objfwd:
1574 case Lisp_Internal_Stream:
1575 /* Don't bother with Lisp_Buffer_Objfwd,
1576 since all markable slots in current buffer marked anyway. */
1577 /* Don't need to do Lisp_Objfwd, since the places they point
1578 are protected with staticpro. */
1579 break;
1580
1581 default:
1582 abort ();
1583 }
1584}
1585
1586/* Mark the pointers in a buffer structure. */
1587
1588static void
1589mark_buffer (buf)
1590 Lisp_Object buf;
1591{
1592 Lisp_Object tem;
1593 register struct buffer *buffer = XBUFFER (buf);
1594 register Lisp_Object *ptr;
1595
1596 /* This is the buffer's markbit */
1597 mark_object (&buffer->name);
1598 XMARK (buffer->name);
1599
d5e35230
JA
1600 MARK_INTERVAL_TREE (buffer->intervals);
1601
7146af97
JB
1602#if 0
1603 mark_object (buffer->syntax_table);
1604
1605 /* Mark the various string-pointers in the buffer object.
1606 Since the strings may be relocated, we must mark them
1607 in their actual slots. So gc_sweep must convert each slot
1608 back to an ordinary C pointer. */
1609 XSET (*(Lisp_Object *)&buffer->upcase_table,
1610 Lisp_String, buffer->upcase_table);
1611 mark_object ((Lisp_Object *)&buffer->upcase_table);
1612 XSET (*(Lisp_Object *)&buffer->downcase_table,
1613 Lisp_String, buffer->downcase_table);
1614 mark_object ((Lisp_Object *)&buffer->downcase_table);
1615
1616 XSET (*(Lisp_Object *)&buffer->sort_table,
1617 Lisp_String, buffer->sort_table);
1618 mark_object ((Lisp_Object *)&buffer->sort_table);
1619 XSET (*(Lisp_Object *)&buffer->folding_sort_table,
1620 Lisp_String, buffer->folding_sort_table);
1621 mark_object ((Lisp_Object *)&buffer->folding_sort_table);
1622#endif
1623
1624 for (ptr = &buffer->name + 1;
1625 (char *)ptr < (char *)buffer + sizeof (struct buffer);
1626 ptr++)
1627 mark_object (ptr);
1628}
1629\f
1630/* Find all structures not marked, and free them. */
1631
1632static void
1633gc_sweep ()
1634{
1635 total_string_size = 0;
1636 compact_strings ();
1637
1638 /* Put all unmarked conses on free list */
1639 {
1640 register struct cons_block *cblk;
1641 register int lim = cons_block_index;
1642 register int num_free = 0, num_used = 0;
1643
1644 cons_free_list = 0;
1645
1646 for (cblk = cons_block; cblk; cblk = cblk->next)
1647 {
1648 register int i;
1649 for (i = 0; i < lim; i++)
1650 if (!XMARKBIT (cblk->conses[i].car))
1651 {
1652 XFASTINT (cblk->conses[i].car) = (int) cons_free_list;
1653 num_free++;
1654 cons_free_list = &cblk->conses[i];
1655 }
1656 else
1657 {
1658 num_used++;
1659 XUNMARK (cblk->conses[i].car);
1660 }
1661 lim = CONS_BLOCK_SIZE;
1662 }
1663 total_conses = num_used;
1664 total_free_conses = num_free;
1665 }
1666
1667#ifdef LISP_FLOAT_TYPE
1668 /* Put all unmarked floats on free list */
1669 {
1670 register struct float_block *fblk;
1671 register int lim = float_block_index;
1672 register int num_free = 0, num_used = 0;
1673
1674 float_free_list = 0;
1675
1676 for (fblk = float_block; fblk; fblk = fblk->next)
1677 {
1678 register int i;
1679 for (i = 0; i < lim; i++)
1680 if (!XMARKBIT (fblk->floats[i].type))
1681 {
1682 XFASTINT (fblk->floats[i].type) = (int) float_free_list;
1683 num_free++;
1684 float_free_list = &fblk->floats[i];
1685 }
1686 else
1687 {
1688 num_used++;
1689 XUNMARK (fblk->floats[i].type);
1690 }
1691 lim = FLOAT_BLOCK_SIZE;
1692 }
1693 total_floats = num_used;
1694 total_free_floats = num_free;
1695 }
1696#endif /* LISP_FLOAT_TYPE */
1697
d5e35230
JA
1698#ifdef USE_TEXT_PROPERTIES
1699 /* Put all unmarked intervals on free list */
1700 {
1701 register struct interval_block *iblk;
1702 register int lim = interval_block_index;
1703 register int num_free = 0, num_used = 0;
1704
1705 interval_free_list = 0;
1706
1707 for (iblk = interval_block; iblk; iblk = iblk->next)
1708 {
1709 register int i;
1710
1711 for (i = 0; i < lim; i++)
1712 {
1713 if (! XMARKBIT (iblk->intervals[i].plist))
1714 {
1715 iblk->intervals[i].parent = interval_free_list;
1716 interval_free_list = &iblk->intervals[i];
1717 num_free++;
1718 }
1719 else
1720 {
1721 num_used++;
1722 XUNMARK (iblk->intervals[i].plist);
1723 }
1724 }
1725 lim = INTERVAL_BLOCK_SIZE;
1726 }
1727 total_intervals = num_used;
1728 total_free_intervals = num_free;
1729 }
1730#endif /* USE_TEXT_PROPERTIES */
1731
7146af97
JB
1732 /* Put all unmarked symbols on free list */
1733 {
1734 register struct symbol_block *sblk;
1735 register int lim = symbol_block_index;
1736 register int num_free = 0, num_used = 0;
1737
1738 symbol_free_list = 0;
1739
1740 for (sblk = symbol_block; sblk; sblk = sblk->next)
1741 {
1742 register int i;
1743 for (i = 0; i < lim; i++)
1744 if (!XMARKBIT (sblk->symbols[i].plist))
1745 {
1746 XFASTINT (sblk->symbols[i].value) = (int) symbol_free_list;
1747 symbol_free_list = &sblk->symbols[i];
1748 num_free++;
1749 }
1750 else
1751 {
1752 num_used++;
1753 sblk->symbols[i].name
1754 = XSTRING (*(Lisp_Object *) &sblk->symbols[i].name);
1755 XUNMARK (sblk->symbols[i].plist);
1756 }
1757 lim = SYMBOL_BLOCK_SIZE;
1758 }
1759 total_symbols = num_used;
1760 total_free_symbols = num_free;
1761 }
1762
1763#ifndef standalone
1764 /* Put all unmarked markers on free list.
1765 Dechain each one first from the buffer it points into. */
1766 {
1767 register struct marker_block *mblk;
1768 struct Lisp_Marker *tem1;
1769 register int lim = marker_block_index;
1770 register int num_free = 0, num_used = 0;
1771
1772 marker_free_list = 0;
1773
1774 for (mblk = marker_block; mblk; mblk = mblk->next)
1775 {
1776 register int i;
1777 for (i = 0; i < lim; i++)
1778 if (!XMARKBIT (mblk->markers[i].chain))
1779 {
1780 Lisp_Object tem;
1781 tem1 = &mblk->markers[i]; /* tem1 avoids Sun compiler bug */
1782 XSET (tem, Lisp_Marker, tem1);
1783 unchain_marker (tem);
1784 XFASTINT (mblk->markers[i].chain) = (int) marker_free_list;
1785 marker_free_list = &mblk->markers[i];
1786 num_free++;
1787 }
1788 else
1789 {
1790 num_used++;
1791 XUNMARK (mblk->markers[i].chain);
1792 }
1793 lim = MARKER_BLOCK_SIZE;
1794 }
1795
1796 total_markers = num_used;
1797 total_free_markers = num_free;
1798 }
1799
1800 /* Free all unmarked buffers */
1801 {
1802 register struct buffer *buffer = all_buffers, *prev = 0, *next;
1803
1804 while (buffer)
1805 if (!XMARKBIT (buffer->name))
1806 {
1807 if (prev)
1808 prev->next = buffer->next;
1809 else
1810 all_buffers = buffer->next;
1811 next = buffer->next;
1812 free (buffer);
1813 buffer = next;
1814 }
1815 else
1816 {
1817 XUNMARK (buffer->name);
d5e35230 1818 UNMARK_BALANCE_INTERVALS (buffer->intervals);
7146af97
JB
1819
1820#if 0
1821 /* Each `struct Lisp_String *' was turned into a Lisp_Object
1822 for purposes of marking and relocation.
1823 Turn them back into C pointers now. */
1824 buffer->upcase_table
1825 = XSTRING (*(Lisp_Object *)&buffer->upcase_table);
1826 buffer->downcase_table
1827 = XSTRING (*(Lisp_Object *)&buffer->downcase_table);
1828 buffer->sort_table
1829 = XSTRING (*(Lisp_Object *)&buffer->sort_table);
1830 buffer->folding_sort_table
1831 = XSTRING (*(Lisp_Object *)&buffer->folding_sort_table);
1832#endif
1833
1834 prev = buffer, buffer = buffer->next;
1835 }
1836 }
1837
1838#endif /* standalone */
1839
1840 /* Free all unmarked vectors */
1841 {
1842 register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next;
1843 total_vector_size = 0;
1844
1845 while (vector)
1846 if (!(vector->size & ARRAY_MARK_FLAG))
1847 {
1848 if (prev)
1849 prev->next = vector->next;
1850 else
1851 all_vectors = vector->next;
1852 next = vector->next;
1853 free (vector);
1854 vector = next;
1855 }
1856 else
1857 {
1858 vector->size &= ~ARRAY_MARK_FLAG;
1859 total_vector_size += vector->size;
1860 prev = vector, vector = vector->next;
1861 }
1862 }
1863
1864 /* Free all "large strings" not marked with ARRAY_MARK_FLAG. */
1865 {
1866 register struct string_block *sb = large_string_blocks, *prev = 0, *next;
1867
1868 while (sb)
1869 if (!(((struct Lisp_String *)(&sb->chars[0]))->size & ARRAY_MARK_FLAG))
1870 {
1871 if (prev)
1872 prev->next = sb->next;
1873 else
1874 large_string_blocks = sb->next;
1875 next = sb->next;
1876 free (sb);
1877 sb = next;
1878 }
1879 else
1880 {
1881 ((struct Lisp_String *)(&sb->chars[0]))->size
1882 &= ~ARRAY_MARK_FLAG & ~MARKBIT;
1883 total_string_size += ((struct Lisp_String *)(&sb->chars[0]))->size;
1884 prev = sb, sb = sb->next;
1885 }
1886 }
1887}
1888\f
1889/* Compactify strings, relocate references to them, and
1890 free any string blocks that become empty. */
1891
1892static void
1893compact_strings ()
1894{
1895 /* String block of old strings we are scanning. */
1896 register struct string_block *from_sb;
1897 /* A preceding string block (or maybe the same one)
1898 where we are copying the still-live strings to. */
1899 register struct string_block *to_sb;
1900 int pos;
1901 int to_pos;
1902
1903 to_sb = first_string_block;
1904 to_pos = 0;
1905
1906 /* Scan each existing string block sequentially, string by string. */
1907 for (from_sb = first_string_block; from_sb; from_sb = from_sb->next)
1908 {
1909 pos = 0;
1910 /* POS is the index of the next string in the block. */
1911 while (pos < from_sb->pos)
1912 {
1913 register struct Lisp_String *nextstr
1914 = (struct Lisp_String *) &from_sb->chars[pos];
1915
1916 register struct Lisp_String *newaddr;
1917 register int size = nextstr->size;
1918
1919 /* NEXTSTR is the old address of the next string.
1920 Just skip it if it isn't marked. */
1921 if ((unsigned) size > STRING_BLOCK_SIZE)
1922 {
1923 /* It is marked, so its size field is really a chain of refs.
1924 Find the end of the chain, where the actual size lives. */
1925 while ((unsigned) size > STRING_BLOCK_SIZE)
1926 {
1927 if (size & 1) size ^= MARKBIT | 1;
1928 size = *(int *)size & ~MARKBIT;
1929 }
1930
1931 total_string_size += size;
1932
1933 /* If it won't fit in TO_SB, close it out,
1934 and move to the next sb. Keep doing so until
1935 TO_SB reaches a large enough, empty enough string block.
1936 We know that TO_SB cannot advance past FROM_SB here
1937 since FROM_SB is large enough to contain this string.
1938 Any string blocks skipped here
1939 will be patched out and freed later. */
1940 while (to_pos + STRING_FULLSIZE (size)
1941 > max (to_sb->pos, STRING_BLOCK_SIZE))
1942 {
1943 to_sb->pos = to_pos;
1944 to_sb = to_sb->next;
1945 to_pos = 0;
1946 }
1947 /* Compute new address of this string
1948 and update TO_POS for the space being used. */
1949 newaddr = (struct Lisp_String *) &to_sb->chars[to_pos];
1950 to_pos += STRING_FULLSIZE (size);
1951
1952 /* Copy the string itself to the new place. */
1953 if (nextstr != newaddr)
d5e35230
JA
1954 bcopy (nextstr, newaddr, size + 1 + sizeof (int)
1955 + INTERVAL_PTR_SIZE);
7146af97
JB
1956
1957 /* Go through NEXTSTR's chain of references
1958 and make each slot in the chain point to
1959 the new address of this string. */
1960 size = newaddr->size;
1961 while ((unsigned) size > STRING_BLOCK_SIZE)
1962 {
1963 register Lisp_Object *objptr;
1964 if (size & 1) size ^= MARKBIT | 1;
1965 objptr = (Lisp_Object *)size;
1966
1967 size = XFASTINT (*objptr) & ~MARKBIT;
1968 if (XMARKBIT (*objptr))
1969 {
1970 XSET (*objptr, Lisp_String, newaddr);
1971 XMARK (*objptr);
1972 }
1973 else
1974 XSET (*objptr, Lisp_String, newaddr);
1975 }
1976 /* Store the actual size in the size field. */
1977 newaddr->size = size;
1978 }
1979 pos += STRING_FULLSIZE (size);
1980 }
1981 }
1982
1983 /* Close out the last string block still used and free any that follow. */
1984 to_sb->pos = to_pos;
1985 current_string_block = to_sb;
1986
1987 from_sb = to_sb->next;
1988 to_sb->next = 0;
1989 while (from_sb)
1990 {
1991 to_sb = from_sb->next;
1992 free (from_sb);
1993 from_sb = to_sb;
1994 }
1995
1996 /* Free any empty string blocks further back in the chain.
1997 This loop will never free first_string_block, but it is very
1998 unlikely that that one will become empty, so why bother checking? */
1999
2000 from_sb = first_string_block;
2001 while (to_sb = from_sb->next)
2002 {
2003 if (to_sb->pos == 0)
2004 {
2005 if (from_sb->next = to_sb->next)
2006 from_sb->next->prev = from_sb;
2007 free (to_sb);
2008 }
2009 else
2010 from_sb = to_sb;
2011 }
2012}
2013\f
20d24714
JB
2014/* Debugging aids. */
2015
2016DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, "",
2017 "Return the address of the last byte Emacs has allocated, divided by 1024.\n\
2018This may be helpful in debugging Emacs's memory usage.\n\
0d73ca81 2019The value is divided by 1024 to make sure it will fit in a lisp integer.")
20d24714
JB
2020 ()
2021{
2022 Lisp_Object end;
2023
0d73ca81 2024 XSET (end, Lisp_Int, (int) sbrk (0) / 1024);
20d24714
JB
2025
2026 return end;
2027}
2028
2029\f
7146af97
JB
2030/* Initialization */
2031
2032init_alloc_once ()
2033{
2034 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
2035 pureptr = 0;
4c0be5f4
JB
2036#ifdef HAVE_SHM
2037 pure_size = PURESIZE;
2038#endif
7146af97
JB
2039 all_vectors = 0;
2040 ignore_warnings = 1;
2041 init_strings ();
2042 init_cons ();
2043 init_symbol ();
2044 init_marker ();
2045#ifdef LISP_FLOAT_TYPE
2046 init_float ();
2047#endif /* LISP_FLOAT_TYPE */
d5e35230
JA
2048 INIT_INTERVALS;
2049
7146af97
JB
2050 ignore_warnings = 0;
2051 gcprolist = 0;
2052 staticidx = 0;
2053 consing_since_gc = 0;
2054 gc_cons_threshold = 100000;
2055#ifdef VIRT_ADDR_VARIES
2056 malloc_sbrk_unused = 1<<22; /* A large number */
2057 malloc_sbrk_used = 100000; /* as reasonable as any number */
2058#endif /* VIRT_ADDR_VARIES */
2059}
2060
2061init_alloc ()
2062{
2063 gcprolist = 0;
2064}
2065
2066void
2067syms_of_alloc ()
2068{
2069 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold,
2070 "*Number of bytes of consing between garbage collections.\n\
2071Garbage collection can happen automatically once this many bytes have been\n\
2072allocated since the last garbage collection. All data types count.\n\n\
2073Garbage collection happens automatically only when `eval' is called.\n\n\
2074By binding this temporarily to a large number, you can effectively\n\
2075prevent garbage collection during a part of the program.");
2076
2077 DEFVAR_INT ("pure-bytes-used", &pureptr,
2078 "Number of bytes of sharable Lisp data allocated so far.");
2079
2080#if 0
2081 DEFVAR_INT ("data-bytes-used", &malloc_sbrk_used,
2082 "Number of bytes of unshared memory allocated in this session.");
2083
2084 DEFVAR_INT ("data-bytes-free", &malloc_sbrk_unused,
2085 "Number of bytes of unshared memory remaining available in this session.");
2086#endif
2087
2088 DEFVAR_LISP ("purify-flag", &Vpurify_flag,
2089 "Non-nil means loading Lisp code in order to dump an executable.\n\
2090This means that certain objects should be allocated in shared (pure) space.");
2091
502b9b64 2092 DEFVAR_INT ("undo-limit", &undo_limit,
7146af97 2093 "Keep no more undo information once it exceeds this size.\n\
502b9b64 2094This limit is applied when garbage collection happens.\n\
7146af97
JB
2095The size is counted as the number of bytes occupied,\n\
2096which includes both saved text and other data.");
502b9b64 2097 undo_limit = 20000;
7146af97 2098
502b9b64 2099 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit,
7146af97
JB
2100 "Don't keep more than this much size of undo information.\n\
2101A command which pushes past this size is itself forgotten.\n\
502b9b64 2102This limit is applied when garbage collection happens.\n\
7146af97
JB
2103The size is counted as the number of bytes occupied,\n\
2104which includes both saved text and other data.");
502b9b64 2105 undo_strong_limit = 30000;
7146af97
JB
2106
2107 defsubr (&Scons);
2108 defsubr (&Slist);
2109 defsubr (&Svector);
2110 defsubr (&Smake_byte_code);
2111 defsubr (&Smake_list);
2112 defsubr (&Smake_vector);
2113 defsubr (&Smake_string);
2114 defsubr (&Smake_rope);
2115 defsubr (&Srope_elt);
2116 defsubr (&Smake_symbol);
2117 defsubr (&Smake_marker);
2118 defsubr (&Spurecopy);
2119 defsubr (&Sgarbage_collect);
20d24714 2120 defsubr (&Smemory_limit);
7146af97 2121}