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