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