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