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