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