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