Add :compact-instance-header feature for x86-64.
[sbcl.git] / src / runtime / gc-common.c
blob0ef89a6d9f3b54424e8990f472c97cb213df2f05
1 /*
2 * Garbage Collection common functions for scavenging, moving and sizing
3 * objects. These are for use with both GC (stop & copy GC) and GENCGC
4 */
6 /*
7 * This software is part of the SBCL system. See the README file for
8 * more information.
10 * This software is derived from the CMU CL system, which was
11 * written at Carnegie Mellon University and released into the
12 * public domain. The software is in the public domain and is
13 * provided with absolutely no warranty. See the COPYING and CREDITS
14 * files for more information.
18 * For a review of garbage collection techniques (e.g. generational
19 * GC) and terminology (e.g. "scavenging") see Paul R. Wilson,
20 * "Uniprocessor Garbage Collection Techniques". As of 20000618, this
21 * had been accepted for _ACM Computing Surveys_ and was available
22 * as a PostScript preprint through
23 * <http://www.cs.utexas.edu/users/oops/papers.html>
24 * as
25 * <ftp://ftp.cs.utexas.edu/pub/garbage/bigsurv.ps>.
28 #define _GNU_SOURCE /* for ffsl(3) from string.h */
30 #include <stdio.h>
31 #include <signal.h>
32 #include <string.h>
33 #include "sbcl.h"
34 #include "runtime.h"
35 #include "os.h"
36 #include "interr.h"
37 #include "globals.h"
38 #include "interrupt.h"
39 #include "validate.h"
40 #include "lispregs.h"
41 #include "arch.h"
42 #include "gc.h"
43 #include "genesis/primitive-objects.h"
44 #include "genesis/static-symbols.h"
45 #include "genesis/layout.h"
46 #include "genesis/hash-table.h"
47 #include "gc-internal.h"
49 #ifdef LISP_FEATURE_SPARC
50 #define LONG_FLOAT_SIZE 4
51 #else
52 #ifdef LISP_FEATURE_X86
53 #define LONG_FLOAT_SIZE 3
54 #endif
55 #endif
57 os_vm_size_t dynamic_space_size = DEFAULT_DYNAMIC_SPACE_SIZE;
58 os_vm_size_t thread_control_stack_size = DEFAULT_CONTROL_STACK_SIZE;
60 #ifndef LISP_FEATURE_GENCGC
61 inline static boolean
62 in_gc_p(void) {
63 return current_dynamic_space == from_space;
65 #endif
67 inline static boolean
68 forwarding_pointer_p(lispobj *pointer) {
69 lispobj first_word=*pointer;
70 #ifdef LISP_FEATURE_GENCGC
71 return (first_word == 0x01);
72 #else
73 return (is_lisp_pointer(first_word)
74 && in_gc_p() /* cheneygc new_space_p() is broken when not in gc */
75 && new_space_p(first_word));
76 #endif
79 static inline lispobj *
80 forwarding_pointer_value(lispobj *pointer) {
81 #ifdef LISP_FEATURE_GENCGC
82 return (lispobj *) ((pointer_sized_uint_t) pointer[1]);
83 #else
84 return (lispobj *) ((pointer_sized_uint_t) pointer[0]);
85 #endif
87 static inline lispobj
88 set_forwarding_pointer(lispobj * pointer, lispobj newspace_copy) {
89 #ifdef LISP_FEATURE_GENCGC
90 pointer[0]=0x01;
91 pointer[1]=newspace_copy;
92 #else
93 pointer[0]=newspace_copy;
94 #endif
95 return newspace_copy;
98 sword_t (*scavtab[256])(lispobj *where, lispobj object);
99 lispobj (*transother[256])(lispobj object);
100 sword_t (*sizetab[256])(lispobj *where);
101 struct weak_pointer *weak_pointers;
103 os_vm_size_t bytes_consed_between_gcs = 12*1024*1024;
106 * copying objects
109 /* gc_general_copy_object is inline from gc-internal.h */
111 /* to copy a boxed object */
112 lispobj
113 copy_object(lispobj object, sword_t nwords)
115 return gc_general_copy_object(object, nwords, BOXED_PAGE_FLAG);
118 lispobj
119 copy_code_object(lispobj object, sword_t nwords)
121 return gc_general_copy_object(object, nwords, CODE_PAGE_FLAG);
124 static sword_t scav_lose(lispobj *where, lispobj object); /* forward decl */
126 /* FIXME: Most calls end up going to some trouble to compute an
127 * 'n_words' value for this function. The system might be a little
128 * simpler if this function used an 'end' parameter instead. */
129 void
130 scavenge(lispobj *start, sword_t n_words)
132 lispobj *end = start + n_words;
133 lispobj *object_ptr;
135 for (object_ptr = start; object_ptr < end;) {
136 lispobj object = *object_ptr;
137 #ifdef LISP_FEATURE_GENCGC
138 if (forwarding_pointer_p(object_ptr))
139 lose("unexpect forwarding pointer in scavenge: %p, start=%p, n=%ld\n",
140 object_ptr, start, n_words);
141 #endif
142 if (is_lisp_pointer(object)) {
143 if (from_space_p(object)) {
144 /* It currently points to old space. Check for a
145 * forwarding pointer. */
146 lispobj *ptr = native_pointer(object);
147 if (forwarding_pointer_p(ptr)) {
148 /* Yes, there's a forwarding pointer. */
149 *object_ptr = LOW_WORD(forwarding_pointer_value(ptr));
150 object_ptr++;
151 } else {
152 /* Scavenge that pointer. */
153 object_ptr +=
154 (scavtab[widetag_of(object)])(object_ptr, object);
156 #ifdef LISP_FEATURE_IMMOBILE_SPACE
157 } else if (immobile_space_p(object)) {
158 lispobj *ptr = native_pointer(object);
159 if (immobile_obj_gen_bits(ptr) == from_space)
160 promote_immobile_obj(ptr);
161 object_ptr++;
162 #endif
163 } else {
164 /* It points somewhere other than oldspace. Leave it
165 * alone. */
166 object_ptr++;
169 else if (fixnump(object)) {
170 /* It's a fixnum: really easy.. */
171 object_ptr++;
172 } else {
173 /* It's some sort of header object or another. */
174 object_ptr += (scavtab[widetag_of(object)])(object_ptr, object);
177 gc_assert_verbose(object_ptr == end, "Final object pointer %p, start %p, end %p\n",
178 object_ptr, start, end);
181 static lispobj trans_fun_header(lispobj object); /* forward decls */
182 static lispobj trans_boxed(lispobj object);
184 static sword_t
185 scav_fun_pointer(lispobj *where, lispobj object)
187 lispobj *first_pointer;
188 lispobj copy;
190 gc_assert(is_lisp_pointer(object));
192 /* Object is a pointer into from_space - not a FP. */
193 first_pointer = (lispobj *) native_pointer(object);
195 /* must transport object -- object may point to either a function
196 * header, a closure function header, or to a closure header. */
198 switch (widetag_of(*first_pointer)) {
199 case SIMPLE_FUN_HEADER_WIDETAG:
200 copy = trans_fun_header(object);
201 break;
202 default:
203 copy = trans_boxed(object);
204 break;
207 if (copy != object) {
208 /* Set forwarding pointer */
209 set_forwarding_pointer(first_pointer,copy);
212 gc_assert(is_lisp_pointer(copy));
213 gc_assert(!from_space_p(copy));
215 *where = copy;
217 return 1;
221 static struct code *
222 trans_code(struct code *code)
224 struct code *new_code;
225 lispobj l_code, l_new_code;
226 uword_t nheader_words, ncode_words, nwords;
227 uword_t displacement;
228 lispobj fheaderl, *prev_pointer;
230 /* if object has already been transported, just return pointer */
231 if (forwarding_pointer_p((lispobj *)code)) {
232 #ifdef DEBUG_CODE_GC
233 printf("Was already transported\n");
234 #endif
235 return (struct code *) forwarding_pointer_value
236 ((lispobj *)((pointer_sized_uint_t) code));
239 gc_assert(widetag_of(code->header) == CODE_HEADER_WIDETAG);
241 /* prepare to transport the code vector */
242 l_code = (lispobj) LOW_WORD(code) | OTHER_POINTER_LOWTAG;
244 ncode_words = code_instruction_words(code->code_size);
245 nheader_words = code_header_words(code->header);
246 nwords = ncode_words + nheader_words;
247 nwords = CEILING(nwords, 2);
249 l_new_code = copy_code_object(l_code, nwords);
250 new_code = (struct code *) native_pointer(l_new_code);
252 #if defined(DEBUG_CODE_GC)
253 printf("Old code object at 0x%08x, new code object at 0x%08x.\n",
254 (uword_t) code, (uword_t) new_code);
255 printf("Code object is %d words long.\n", nwords);
256 #endif
258 #ifdef LISP_FEATURE_GENCGC
259 if (new_code == code)
260 return new_code;
261 #endif
263 displacement = l_new_code - l_code;
265 set_forwarding_pointer((lispobj *)code, l_new_code);
267 /* set forwarding pointers for all the function headers in the */
268 /* code object. also fix all self pointers */
270 fheaderl = code->entry_points;
271 prev_pointer = &new_code->entry_points;
273 while (fheaderl != NIL) {
274 struct simple_fun *fheaderp, *nfheaderp;
275 lispobj nfheaderl;
277 fheaderp = (struct simple_fun *) native_pointer(fheaderl);
278 gc_assert(widetag_of(fheaderp->header) == SIMPLE_FUN_HEADER_WIDETAG);
280 /* Calculate the new function pointer and the new */
281 /* function header. */
282 nfheaderl = fheaderl + displacement;
283 nfheaderp = (struct simple_fun *) native_pointer(nfheaderl);
285 #ifdef DEBUG_CODE_GC
286 printf("fheaderp->header (at %x) <- %x\n",
287 &(fheaderp->header) , nfheaderl);
288 #endif
289 set_forwarding_pointer((lispobj *)fheaderp, nfheaderl);
291 /* fix self pointer. */
292 nfheaderp->self =
293 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
294 FUN_RAW_ADDR_OFFSET +
295 #endif
296 nfheaderl;
298 *prev_pointer = nfheaderl;
300 fheaderl = fheaderp->next;
301 prev_pointer = &nfheaderp->next;
303 #ifdef LISP_FEATURE_GENCGC
304 /* Cheneygc doesn't need this os_flush_icache, it flushes the whole
305 spaces once when all copying is done. */
306 os_flush_icache((os_vm_address_t) (((sword_t *)new_code) + nheader_words),
307 ncode_words * sizeof(sword_t));
309 #endif
311 #ifdef LISP_FEATURE_X86
312 gencgc_apply_code_fixups(code, new_code);
313 #endif
315 return new_code;
318 static sword_t
319 scav_code_header(lispobj *where, lispobj object)
321 struct code *code;
322 sword_t n_header_words, n_code_words, n_words;
323 lispobj entry_point; /* tagged pointer to entry point */
324 struct simple_fun *function_ptr; /* untagged pointer to entry point */
326 code = (struct code *) where;
327 n_code_words = code_instruction_words(code->code_size);
328 n_header_words = code_header_words(object);
329 n_words = n_code_words + n_header_words;
330 n_words = CEILING(n_words, 2);
332 /* Scavenge the boxed section of the code data block. */
333 scavenge(where + 1, n_header_words - 1);
335 /* Scavenge the boxed section of each function object in the
336 * code data block. */
337 for (entry_point = code->entry_points;
338 entry_point != NIL;
339 entry_point = function_ptr->next) {
341 gc_assert_verbose(is_lisp_pointer(entry_point),
342 "Entry point %lx\n is not a lisp pointer.",
343 (sword_t)entry_point);
345 function_ptr = (struct simple_fun *) native_pointer(entry_point);
346 gc_assert(widetag_of(function_ptr->header)==SIMPLE_FUN_HEADER_WIDETAG);
347 scavenge(SIMPLE_FUN_SCAV_START(function_ptr),
348 SIMPLE_FUN_SCAV_NWORDS(function_ptr));
351 return n_words;
354 static lispobj
355 trans_code_header(lispobj object)
357 struct code *ncode;
359 ncode = trans_code((struct code *) native_pointer(object));
360 return (lispobj) LOW_WORD(ncode) | OTHER_POINTER_LOWTAG;
364 static sword_t
365 size_code_header(lispobj *where)
367 struct code *code;
368 sword_t nheader_words, ncode_words, nwords;
370 code = (struct code *) where;
372 ncode_words = code_instruction_words(code->code_size);
373 nheader_words = code_header_words(code->header);
374 nwords = ncode_words + nheader_words;
375 nwords = CEILING(nwords, 2);
377 return nwords;
380 #if !defined(LISP_FEATURE_X86) && ! defined(LISP_FEATURE_X86_64)
381 static sword_t
382 scav_return_pc_header(lispobj *where, lispobj object)
384 lose("attempted to scavenge a return PC header where=0x%08x object=0x%08x\n",
385 (uword_t) where,
386 (uword_t) object);
387 return 0; /* bogus return value to satisfy static type checking */
389 #endif /* LISP_FEATURE_X86 */
391 static lispobj
392 trans_return_pc_header(lispobj object)
394 struct simple_fun *return_pc;
395 uword_t offset;
396 struct code *code, *ncode;
398 return_pc = (struct simple_fun *) native_pointer(object);
399 /* FIXME: was times 4, should it really be N_WORD_BYTES? */
400 offset = HeaderValue(return_pc->header) * N_WORD_BYTES;
402 /* Transport the whole code object */
403 code = (struct code *) ((uword_t) return_pc - offset);
404 ncode = trans_code(code);
406 return ((lispobj) LOW_WORD(ncode) + offset) | OTHER_POINTER_LOWTAG;
409 /* On the 386, closures hold a pointer to the raw address instead of the
410 * function object, so we can use CALL [$FDEFN+const] to invoke
411 * the function without loading it into a register. Given that code
412 * objects don't move, we don't need to update anything, but we do
413 * have to figure out that the function is still live. */
415 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
416 static sword_t
417 scav_closure_header(lispobj *where, lispobj object)
419 struct closure *closure;
420 lispobj fun;
422 closure = (struct closure *)where;
423 fun = closure->fun - FUN_RAW_ADDR_OFFSET;
424 scavenge(&fun, 1);
425 #ifdef LISP_FEATURE_GENCGC
426 /* The function may have moved so update the raw address. But
427 * don't write unnecessarily. */
428 if (closure->fun != fun + FUN_RAW_ADDR_OFFSET)
429 closure->fun = fun + FUN_RAW_ADDR_OFFSET;
430 #endif
431 return 2;
433 #endif
435 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
436 static sword_t
437 scav_fun_header(lispobj *where, lispobj object)
439 lose("attempted to scavenge a function header where=0x%08x object=0x%08x\n",
440 (uword_t) where,
441 (uword_t) object);
442 return 0; /* bogus return value to satisfy static type checking */
444 #endif /* LISP_FEATURE_X86 */
446 static lispobj
447 trans_fun_header(lispobj object)
449 struct simple_fun *fheader;
450 uword_t offset;
451 struct code *code, *ncode;
453 fheader = (struct simple_fun *) native_pointer(object);
454 /* FIXME: was times 4, should it really be N_WORD_BYTES? */
455 offset = HeaderValue(fheader->header) * N_WORD_BYTES;
457 /* Transport the whole code object */
458 code = (struct code *) ((uword_t) fheader - offset);
459 ncode = trans_code(code);
461 return ((lispobj) LOW_WORD(ncode) + offset) | FUN_POINTER_LOWTAG;
466 * instances
469 static lispobj
470 trans_instance(lispobj object)
472 lispobj header;
473 uword_t length;
475 gc_assert(is_lisp_pointer(object));
477 header = *((lispobj *) native_pointer(object));
478 length = instance_length(header) + 1;
479 length = CEILING(length, 2);
481 return copy_object(object, length);
484 static sword_t
485 size_instance(lispobj *where)
487 lispobj header;
488 uword_t length;
490 header = *where;
491 length = instance_length(header) + 1;
492 length = CEILING(length, 2);
494 return length;
497 static sword_t
498 scav_instance_pointer(lispobj *where, lispobj object)
500 lispobj copy, *first_pointer;
502 /* Object is a pointer into from space - not a FP. */
503 copy = trans_instance(object);
505 #ifdef LISP_FEATURE_GENCGC
506 gc_assert(copy != object);
507 #endif
509 first_pointer = (lispobj *) native_pointer(object);
510 set_forwarding_pointer(first_pointer,copy);
511 *where = copy;
513 return 1;
518 * lists and conses
521 static lispobj trans_list(lispobj object);
523 static sword_t
524 scav_list_pointer(lispobj *where, lispobj object)
526 lispobj first, *first_pointer;
528 gc_assert(is_lisp_pointer(object));
530 /* Object is a pointer into from space - not FP. */
531 first_pointer = (lispobj *) native_pointer(object);
533 first = trans_list(object);
534 gc_assert(first != object);
536 /* Set forwarding pointer */
537 set_forwarding_pointer(first_pointer, first);
539 gc_assert(is_lisp_pointer(first));
540 gc_assert(!from_space_p(first));
542 *where = first;
543 return 1;
547 static lispobj
548 trans_list(lispobj object)
550 lispobj new_list_pointer;
551 struct cons *cons, *new_cons;
552 lispobj cdr;
554 cons = (struct cons *) native_pointer(object);
556 /* Copy 'object'. */
557 new_cons = (struct cons *)
558 gc_general_alloc(sizeof(struct cons), BOXED_PAGE_FLAG, ALLOC_QUICK);
559 new_cons->car = cons->car;
560 new_cons->cdr = cons->cdr; /* updated later */
561 new_list_pointer = make_lispobj(new_cons,lowtag_of(object));
563 /* Grab the cdr: set_forwarding_pointer will clobber it in GENCGC */
564 cdr = cons->cdr;
566 set_forwarding_pointer((lispobj *)cons, new_list_pointer);
568 /* Try to linearize the list in the cdr direction to help reduce
569 * paging. */
570 while (1) {
571 lispobj new_cdr;
572 struct cons *cdr_cons, *new_cdr_cons;
574 if(lowtag_of(cdr) != LIST_POINTER_LOWTAG ||
575 !from_space_p(cdr) ||
576 forwarding_pointer_p((lispobj *)native_pointer(cdr)))
577 break;
579 cdr_cons = (struct cons *) native_pointer(cdr);
581 /* Copy 'cdr'. */
582 new_cdr_cons = (struct cons*)
583 gc_general_alloc(sizeof(struct cons), BOXED_PAGE_FLAG, ALLOC_QUICK);
584 new_cdr_cons->car = cdr_cons->car;
585 new_cdr_cons->cdr = cdr_cons->cdr;
586 new_cdr = make_lispobj(new_cdr_cons, lowtag_of(cdr));
588 /* Grab the cdr before it is clobbered. */
589 cdr = cdr_cons->cdr;
590 set_forwarding_pointer((lispobj *)cdr_cons, new_cdr);
592 /* Update the cdr of the last cons copied into new space to
593 * keep the newspace scavenge from having to do it. */
594 new_cons->cdr = new_cdr;
596 new_cons = new_cdr_cons;
599 return new_list_pointer;
604 * scavenging and transporting other pointers
607 static sword_t
608 scav_other_pointer(lispobj *where, lispobj object)
610 lispobj first, *first_pointer;
612 gc_assert(is_lisp_pointer(object));
614 /* Object is a pointer into from space - not FP. */
615 first_pointer = (lispobj *) native_pointer(object);
616 first = (transother[widetag_of(*first_pointer)])(object);
618 if (first != object) {
619 set_forwarding_pointer(first_pointer, first);
620 #ifdef LISP_FEATURE_GENCGC
621 *where = first;
622 #endif
624 #ifndef LISP_FEATURE_GENCGC
625 *where = first;
626 #endif
627 gc_assert(is_lisp_pointer(first));
628 gc_assert(!from_space_p(first));
630 return 1;
634 * immediate, boxed, and unboxed objects
637 static sword_t
638 size_pointer(lispobj *where)
640 return 1;
643 static sword_t
644 scav_immediate(lispobj *where, lispobj object)
646 return 1;
649 static lispobj
650 trans_immediate(lispobj object)
652 lose("trying to transport an immediate\n");
653 return NIL; /* bogus return value to satisfy static type checking */
656 static sword_t
657 size_immediate(lispobj *where)
659 return 1;
663 static sword_t
664 scav_boxed(lispobj *where, lispobj object)
666 return 1;
669 boolean positive_bignum_logbitp(int index, struct bignum* bignum)
671 /* If the bignum in the layout has another pointer to it (besides the layout)
672 acting as a root, and which is scavenged first, then transporting the
673 bignum causes the layout to see a FP, as would copying an instance whose
674 layout that is. This is a nearly impossible scenario to create organically
675 in Lisp, because mostly nothing ever looks again at that exact (EQ) bignum
676 except for a few things that would cause it to be pinned anyway,
677 such as it being kept in a local variable during structure manipulation.
678 See 'interleaved-raw.impure.lisp' for a way to trigger this */
679 if (forwarding_pointer_p((lispobj*)bignum)) {
680 lispobj *forwarded = forwarding_pointer_value((lispobj*)bignum);
681 #if 0
682 fprintf(stderr, "GC bignum_logbitp(): fwd from %p to %p\n",
683 (void*)bignum, (void*)forwarded);
684 #endif
685 bignum = (struct bignum*)native_pointer((lispobj)forwarded);
688 int len = HeaderValue(bignum->header);
689 int word_index = index / N_WORD_BITS;
690 int bit_index = index % N_WORD_BITS;
691 if (word_index >= len) {
692 // just return 0 since the marking logic does not allow negative bignums
693 return 0;
694 } else {
695 return (bignum->digits[word_index] >> bit_index) & 1;
699 // Helper function for helper function below, since lambda isn't a thing
700 static void instance_scan_range(void* instance_ptr, int offset, int nwords)
702 scavenge((lispobj*)instance_ptr + offset, nwords);
705 // Helper function for stepping through the tagged slots of an instance in
706 // scav_instance and verify_space.
707 void
708 instance_scan_interleaved(void (*proc)(lispobj*, sword_t),
709 lispobj *instance_ptr,
710 sword_t n_words,
711 lispobj *layout_obj)
713 struct layout *layout = (struct layout*)layout_obj;
714 lispobj layout_bitmap = layout->bitmap;
715 sword_t index;
717 /* This code might be made more efficient by run-length-encoding the ranges
718 of words to scan, but probably not by much */
720 ++instance_ptr; // was supplied as the address of the header word
721 if (fixnump(layout_bitmap)) {
722 sword_t bitmap = (sword_t)layout_bitmap >> N_FIXNUM_TAG_BITS; // signed integer!
723 for (index = 0; index < n_words ; index++, bitmap >>= 1)
724 if (bitmap & 1)
725 proc(instance_ptr + index, 1);
726 } else { /* huge bitmap */
727 struct bignum * bitmap;
728 bitmap = (struct bignum*)native_pointer(layout_bitmap);
729 if (forwarding_pointer_p((lispobj*)bitmap))
730 bitmap = (struct bignum*)
731 native_pointer((lispobj)forwarding_pointer_value((lispobj*)bitmap));
732 bitmap_scan((uword_t*)bitmap->digits, HeaderValue(bitmap->header), 0,
733 instance_scan_range, instance_ptr);
737 void bitmap_scan(uword_t* bitmap, int n_bitmap_words, int flags,
738 void (*proc)(void*, int, int), void* arg)
740 uword_t sense = (flags & BIT_SCAN_INVERT) ? ~0L : 0;
741 int start_word_index = 0;
742 int shift = 0;
743 in_use_marker_t word;
745 flags = flags & BIT_SCAN_CLEAR;
747 // Rather than bzero'ing we can just clear each nonzero word as it's read,
748 // if so specified.
749 #define BITMAP_REF(j) word = bitmap[j]; if(word && flags) bitmap[j] = 0; word ^= sense
750 BITMAP_REF(0);
751 while (1) {
752 int skip_bits, start_bit, start_position, run_length;
753 if (word == 0) {
754 if (++start_word_index >= n_bitmap_words) break;
755 BITMAP_REF(start_word_index);
756 shift = 0;
757 continue;
759 // On each loop iteration, the lowest 1 bit is a "relative"
760 // bit index, since the word was already shifted. This is 'skip_bits'.
761 // Adding back in the total shift amount gives 'start_bit',
762 // the true absolute index within the current word.
763 // 'start_position' is absolute within the entire bitmap.
764 skip_bits = ffsl(word) - 1;
765 start_bit = skip_bits + shift;
766 start_position = N_WORD_BITS * start_word_index + start_bit;
767 // Compute the number of consecutive 1s in the current word.
768 word >>= skip_bits;
769 run_length = ~word ? ffsl(~word) - 1 : N_WORD_BITS;
770 if (start_bit + run_length < N_WORD_BITS) { // Do not extend to additional words.
771 word >>= run_length;
772 shift += skip_bits + run_length;
773 } else {
774 int end_word_index = ++start_word_index;
775 while (1) {
776 if (end_word_index >= n_bitmap_words) {
777 word = 0;
778 run_length += (end_word_index - start_word_index) * N_WORD_BITS;
779 break;
781 BITMAP_REF(end_word_index);
782 if (~word == 0)
783 ++end_word_index;
784 else {
785 // end_word_index is the exclusive bound on contiguous
786 // words to include in the range. See if the low bits
787 // from the next word can extend the range.
788 shift = ffsl(~word) - 1;
789 word >>= shift;
790 run_length += (end_word_index - start_word_index) * N_WORD_BITS
791 + shift;
792 break;
795 start_word_index = end_word_index;
797 proc(arg, start_position, run_length);
799 #undef BITMAP_REF
802 static sword_t
803 scav_instance(lispobj *where, lispobj header)
805 // instance_length() is the number of words following the header including
806 // the layout. If this is an even number, it should be made odd so that
807 // scav_instance() always consumes an even number of words in total.
808 sword_t ntotal = instance_length(header) | 1;
809 lispobj* layout = (lispobj*)instance_layout(where);
811 if (!layout)
812 return 1;
813 layout = native_pointer((lispobj)layout);
814 #ifdef LISP_FEATURE_COMPACT_INSTANCE_HEADER
815 if (__immobile_obj_gen_bits(layout) == from_space)
816 promote_immobile_obj(layout);
817 #else
818 if (forwarding_pointer_p(layout))
819 layout = native_pointer((lispobj)forwarding_pointer_value(layout));
820 #endif
822 if (((struct layout*)layout)->bitmap == make_fixnum(-1))
823 scavenge(where+1, ntotal);
824 else
825 instance_scan_interleaved(scavenge, where, ntotal, layout);
827 return ntotal + 1;
830 static lispobj
831 trans_boxed(lispobj object)
833 lispobj header;
834 uword_t length;
836 gc_assert(is_lisp_pointer(object));
838 header = *((lispobj *) native_pointer(object));
839 length = HeaderValue(header) + 1;
840 length = CEILING(length, 2);
842 return copy_object(object, length);
845 static sword_t
846 size_boxed(lispobj *where)
848 lispobj header;
849 uword_t length;
851 header = *where;
852 length = HeaderValue(header) + 1;
853 length = CEILING(length, 2);
855 return length;
858 static lispobj
859 trans_tiny_boxed(lispobj object)
861 lispobj header;
862 uword_t length;
864 gc_assert(is_lisp_pointer(object));
866 header = *((lispobj *) native_pointer(object));
867 length = (HeaderValue(header) & 0xFF) + 1;
868 length = CEILING(length, 2);
870 return copy_object(object, length);
873 static sword_t
874 size_tiny_boxed(lispobj *where)
876 lispobj header;
877 uword_t length;
879 header = *where;
880 length = (HeaderValue(header) & 0xFF) + 1;
881 length = CEILING(length, 2);
883 return length;
886 /* Note: on the sparc we don't have to do anything special for fdefns, */
887 /* 'cause the raw-addr has a function lowtag. */
888 #if !defined(LISP_FEATURE_SPARC) && !defined(LISP_FEATURE_ARM)
889 static sword_t
890 scav_fdefn(lispobj *where, lispobj object)
892 struct fdefn *fdefn;
894 fdefn = (struct fdefn *)where;
896 /* FSHOW((stderr, "scav_fdefn, function = %p, raw_addr = %p\n",
897 fdefn->fun, fdefn->raw_addr)); */
899 if ((char *)(fdefn->fun + FUN_RAW_ADDR_OFFSET) == fdefn->raw_addr) {
900 scavenge(where + 1, sizeof(struct fdefn)/sizeof(lispobj) - 1);
902 /* Don't write unnecessarily. */
903 if (fdefn->raw_addr != (char *)(fdefn->fun + FUN_RAW_ADDR_OFFSET))
904 fdefn->raw_addr = (char *)(fdefn->fun + FUN_RAW_ADDR_OFFSET);
905 /* gc.c has more casts here, which may be relevant or alternatively
906 may be compiler warning defeaters. try
907 fdefn->raw_addr = ((char *) LOW_WORD(fdefn->fun)) + FUN_RAW_ADDR_OFFSET;
909 return sizeof(struct fdefn) / sizeof(lispobj);
910 } else {
911 return 1;
914 #endif
916 static sword_t
917 scav_unboxed(lispobj *where, lispobj object)
919 uword_t length;
921 length = HeaderValue(object) + 1;
922 length = CEILING(length, 2);
924 return length;
927 static lispobj
928 trans_unboxed(lispobj object)
930 lispobj header;
931 uword_t length;
934 gc_assert(is_lisp_pointer(object));
936 header = *((lispobj *) native_pointer(object));
937 length = HeaderValue(header) + 1;
938 length = CEILING(length, 2);
940 return copy_unboxed_object(object, length);
943 static sword_t
944 size_unboxed(lispobj *where)
946 lispobj header;
947 uword_t length;
949 header = *where;
950 length = HeaderValue(header) + 1;
951 length = CEILING(length, 2);
953 return length;
957 /* vector-like objects */
958 static sword_t
959 scav_base_string(lispobj *where, lispobj object)
961 struct vector *vector;
962 sword_t length, nwords;
964 /* NOTE: Strings contain one more byte of data than the length */
965 /* slot indicates. */
967 vector = (struct vector *) where;
968 length = fixnum_value(vector->length) + 1;
969 nwords = CEILING(NWORDS(length, 8) + 2, 2);
971 return nwords;
973 static lispobj
974 trans_base_string(lispobj object)
976 struct vector *vector;
977 sword_t length, nwords;
979 gc_assert(is_lisp_pointer(object));
981 /* NOTE: A string contains one more byte of data (a terminating
982 * '\0' to help when interfacing with C functions) than indicated
983 * by the length slot. */
985 vector = (struct vector *) native_pointer(object);
986 length = fixnum_value(vector->length) + 1;
987 nwords = CEILING(NWORDS(length, 8) + 2, 2);
989 return copy_large_unboxed_object(object, nwords);
992 static sword_t
993 size_base_string(lispobj *where)
995 struct vector *vector;
996 sword_t length, nwords;
998 /* NOTE: A string contains one more byte of data (a terminating
999 * '\0' to help when interfacing with C functions) than indicated
1000 * by the length slot. */
1002 vector = (struct vector *) where;
1003 length = fixnum_value(vector->length) + 1;
1004 nwords = CEILING(NWORDS(length, 8) + 2, 2);
1006 return nwords;
1009 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
1010 static sword_t
1011 scav_character_string(lispobj *where, lispobj object)
1013 struct vector *vector;
1014 int length, nwords;
1016 /* NOTE: Strings contain one more byte of data than the length */
1017 /* slot indicates. */
1019 vector = (struct vector *) where;
1020 length = fixnum_value(vector->length) + 1;
1021 nwords = CEILING(NWORDS(length, 32) + 2, 2);
1023 return nwords;
1025 static lispobj
1026 trans_character_string(lispobj object)
1028 struct vector *vector;
1029 int length, nwords;
1031 gc_assert(is_lisp_pointer(object));
1033 /* NOTE: A string contains one more byte of data (a terminating
1034 * '\0' to help when interfacing with C functions) than indicated
1035 * by the length slot. */
1037 vector = (struct vector *) native_pointer(object);
1038 length = fixnum_value(vector->length) + 1;
1039 nwords = CEILING(NWORDS(length, 32) + 2, 2);
1041 return copy_large_unboxed_object(object, nwords);
1044 static sword_t
1045 size_character_string(lispobj *where)
1047 struct vector *vector;
1048 int length, nwords;
1050 /* NOTE: A string contains one more byte of data (a terminating
1051 * '\0' to help when interfacing with C functions) than indicated
1052 * by the length slot. */
1054 vector = (struct vector *) where;
1055 length = fixnum_value(vector->length) + 1;
1056 nwords = CEILING(NWORDS(length, 32) + 2, 2);
1058 return nwords;
1060 #endif
1062 static lispobj
1063 trans_vector(lispobj object)
1065 struct vector *vector;
1066 sword_t length, nwords;
1068 gc_assert(is_lisp_pointer(object));
1070 vector = (struct vector *) native_pointer(object);
1072 length = fixnum_value(vector->length);
1073 nwords = CEILING(length + 2, 2);
1075 return copy_large_object(object, nwords);
1078 static sword_t
1079 size_vector(lispobj *where)
1081 struct vector *vector;
1082 sword_t length, nwords;
1084 vector = (struct vector *) where;
1085 length = fixnum_value(vector->length);
1086 nwords = CEILING(length + 2, 2);
1088 return nwords;
1091 static sword_t
1092 scav_vector_nil(lispobj *where, lispobj object)
1094 return 2;
1097 static lispobj
1098 trans_vector_nil(lispobj object)
1100 gc_assert(is_lisp_pointer(object));
1101 return copy_unboxed_object(object, 2);
1104 static sword_t
1105 size_vector_nil(lispobj *where)
1107 /* Just the header word and the length word */
1108 return 2;
1111 static sword_t
1112 scav_vector_bit(lispobj *where, lispobj object)
1114 struct vector *vector;
1115 sword_t length, nwords;
1117 vector = (struct vector *) where;
1118 length = fixnum_value(vector->length);
1119 nwords = CEILING(NWORDS(length, 1) + 2, 2);
1121 return nwords;
1124 static lispobj
1125 trans_vector_bit(lispobj object)
1127 struct vector *vector;
1128 sword_t length, nwords;
1130 gc_assert(is_lisp_pointer(object));
1132 vector = (struct vector *) native_pointer(object);
1133 length = fixnum_value(vector->length);
1134 nwords = CEILING(NWORDS(length, 1) + 2, 2);
1136 return copy_large_unboxed_object(object, nwords);
1139 static sword_t
1140 size_vector_bit(lispobj *where)
1142 struct vector *vector;
1143 sword_t length, nwords;
1145 vector = (struct vector *) where;
1146 length = fixnum_value(vector->length);
1147 nwords = CEILING(NWORDS(length, 1) + 2, 2);
1149 return nwords;
1152 static sword_t
1153 scav_vector_unsigned_byte_2(lispobj *where, lispobj object)
1155 struct vector *vector;
1156 sword_t length, nwords;
1158 vector = (struct vector *) where;
1159 length = fixnum_value(vector->length);
1160 nwords = CEILING(NWORDS(length, 2) + 2, 2);
1162 return nwords;
1165 static lispobj
1166 trans_vector_unsigned_byte_2(lispobj object)
1168 struct vector *vector;
1169 sword_t length, nwords;
1171 gc_assert(is_lisp_pointer(object));
1173 vector = (struct vector *) native_pointer(object);
1174 length = fixnum_value(vector->length);
1175 nwords = CEILING(NWORDS(length, 2) + 2, 2);
1177 return copy_large_unboxed_object(object, nwords);
1180 static sword_t
1181 size_vector_unsigned_byte_2(lispobj *where)
1183 struct vector *vector;
1184 sword_t length, nwords;
1186 vector = (struct vector *) where;
1187 length = fixnum_value(vector->length);
1188 nwords = CEILING(NWORDS(length, 2) + 2, 2);
1190 return nwords;
1193 static sword_t
1194 scav_vector_unsigned_byte_4(lispobj *where, lispobj object)
1196 struct vector *vector;
1197 sword_t length, nwords;
1199 vector = (struct vector *) where;
1200 length = fixnum_value(vector->length);
1201 nwords = CEILING(NWORDS(length, 4) + 2, 2);
1203 return nwords;
1206 static lispobj
1207 trans_vector_unsigned_byte_4(lispobj object)
1209 struct vector *vector;
1210 sword_t length, nwords;
1212 gc_assert(is_lisp_pointer(object));
1214 vector = (struct vector *) native_pointer(object);
1215 length = fixnum_value(vector->length);
1216 nwords = CEILING(NWORDS(length, 4) + 2, 2);
1218 return copy_large_unboxed_object(object, nwords);
1220 static sword_t
1221 size_vector_unsigned_byte_4(lispobj *where)
1223 struct vector *vector;
1224 sword_t length, nwords;
1226 vector = (struct vector *) where;
1227 length = fixnum_value(vector->length);
1228 nwords = CEILING(NWORDS(length, 4) + 2, 2);
1230 return nwords;
1234 static sword_t
1235 scav_vector_unsigned_byte_8(lispobj *where, lispobj object)
1237 struct vector *vector;
1238 sword_t length, nwords;
1240 vector = (struct vector *) where;
1241 length = fixnum_value(vector->length);
1242 nwords = CEILING(NWORDS(length, 8) + 2, 2);
1244 return nwords;
1247 /*********************/
1251 static lispobj
1252 trans_vector_unsigned_byte_8(lispobj object)
1254 struct vector *vector;
1255 sword_t length, nwords;
1257 gc_assert(is_lisp_pointer(object));
1259 vector = (struct vector *) native_pointer(object);
1260 length = fixnum_value(vector->length);
1261 nwords = CEILING(NWORDS(length, 8) + 2, 2);
1263 return copy_large_unboxed_object(object, nwords);
1266 static sword_t
1267 size_vector_unsigned_byte_8(lispobj *where)
1269 struct vector *vector;
1270 sword_t length, nwords;
1272 vector = (struct vector *) where;
1273 length = fixnum_value(vector->length);
1274 nwords = CEILING(NWORDS(length, 8) + 2, 2);
1276 return nwords;
1280 static sword_t
1281 scav_vector_unsigned_byte_16(lispobj *where, lispobj object)
1283 struct vector *vector;
1284 sword_t length, nwords;
1286 vector = (struct vector *) where;
1287 length = fixnum_value(vector->length);
1288 nwords = CEILING(NWORDS(length, 16) + 2, 2);
1290 return nwords;
1293 static lispobj
1294 trans_vector_unsigned_byte_16(lispobj object)
1296 struct vector *vector;
1297 sword_t length, nwords;
1299 gc_assert(is_lisp_pointer(object));
1301 vector = (struct vector *) native_pointer(object);
1302 length = fixnum_value(vector->length);
1303 nwords = CEILING(NWORDS(length, 16) + 2, 2);
1305 return copy_large_unboxed_object(object, nwords);
1308 static sword_t
1309 size_vector_unsigned_byte_16(lispobj *where)
1311 struct vector *vector;
1312 sword_t length, nwords;
1314 vector = (struct vector *) where;
1315 length = fixnum_value(vector->length);
1316 nwords = CEILING(NWORDS(length, 16) + 2, 2);
1318 return nwords;
1321 static sword_t
1322 scav_vector_unsigned_byte_32(lispobj *where, lispobj object)
1324 struct vector *vector;
1325 sword_t length, nwords;
1327 vector = (struct vector *) where;
1328 length = fixnum_value(vector->length);
1329 nwords = CEILING(NWORDS(length, 32) + 2, 2);
1331 return nwords;
1334 static lispobj
1335 trans_vector_unsigned_byte_32(lispobj object)
1337 struct vector *vector;
1338 sword_t length, nwords;
1340 gc_assert(is_lisp_pointer(object));
1342 vector = (struct vector *) native_pointer(object);
1343 length = fixnum_value(vector->length);
1344 nwords = CEILING(NWORDS(length, 32) + 2, 2);
1346 return copy_large_unboxed_object(object, nwords);
1349 static sword_t
1350 size_vector_unsigned_byte_32(lispobj *where)
1352 struct vector *vector;
1353 sword_t length, nwords;
1355 vector = (struct vector *) where;
1356 length = fixnum_value(vector->length);
1357 nwords = CEILING(NWORDS(length, 32) + 2, 2);
1359 return nwords;
1362 #if N_WORD_BITS == 64
1363 static sword_t
1364 scav_vector_unsigned_byte_64(lispobj *where, lispobj object)
1366 struct vector *vector;
1367 sword_t length, nwords;
1369 vector = (struct vector *) where;
1370 length = fixnum_value(vector->length);
1371 nwords = CEILING(NWORDS(length, 64) + 2, 2);
1373 return nwords;
1376 static lispobj
1377 trans_vector_unsigned_byte_64(lispobj object)
1379 struct vector *vector;
1380 sword_t length, nwords;
1382 gc_assert(is_lisp_pointer(object));
1384 vector = (struct vector *) native_pointer(object);
1385 length = fixnum_value(vector->length);
1386 nwords = CEILING(NWORDS(length, 64) + 2, 2);
1388 return copy_large_unboxed_object(object, nwords);
1391 static sword_t
1392 size_vector_unsigned_byte_64(lispobj *where)
1394 struct vector *vector;
1395 sword_t length, nwords;
1397 vector = (struct vector *) where;
1398 length = fixnum_value(vector->length);
1399 nwords = CEILING(NWORDS(length, 64) + 2, 2);
1401 return nwords;
1403 #endif
1405 static sword_t
1406 scav_vector_single_float(lispobj *where, lispobj object)
1408 struct vector *vector;
1409 sword_t length, nwords;
1411 vector = (struct vector *) where;
1412 length = fixnum_value(vector->length);
1413 nwords = CEILING(NWORDS(length, 32) + 2, 2);
1415 return nwords;
1418 static lispobj
1419 trans_vector_single_float(lispobj object)
1421 struct vector *vector;
1422 sword_t length, nwords;
1424 gc_assert(is_lisp_pointer(object));
1426 vector = (struct vector *) native_pointer(object);
1427 length = fixnum_value(vector->length);
1428 nwords = CEILING(NWORDS(length, 32) + 2, 2);
1430 return copy_large_unboxed_object(object, nwords);
1433 static sword_t
1434 size_vector_single_float(lispobj *where)
1436 struct vector *vector;
1437 sword_t length, nwords;
1439 vector = (struct vector *) where;
1440 length = fixnum_value(vector->length);
1441 nwords = CEILING(NWORDS(length, 32) + 2, 2);
1443 return nwords;
1446 static sword_t
1447 scav_vector_double_float(lispobj *where, lispobj object)
1449 struct vector *vector;
1450 sword_t length, nwords;
1452 vector = (struct vector *) where;
1453 length = fixnum_value(vector->length);
1454 nwords = CEILING(NWORDS(length, 64) + 2, 2);
1456 return nwords;
1459 static lispobj
1460 trans_vector_double_float(lispobj object)
1462 struct vector *vector;
1463 sword_t length, nwords;
1465 gc_assert(is_lisp_pointer(object));
1467 vector = (struct vector *) native_pointer(object);
1468 length = fixnum_value(vector->length);
1469 nwords = CEILING(NWORDS(length, 64) + 2, 2);
1471 return copy_large_unboxed_object(object, nwords);
1474 static sword_t
1475 size_vector_double_float(lispobj *where)
1477 struct vector *vector;
1478 sword_t length, nwords;
1480 vector = (struct vector *) where;
1481 length = fixnum_value(vector->length);
1482 nwords = CEILING(NWORDS(length, 64) + 2, 2);
1484 return nwords;
1487 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
1488 static long
1489 scav_vector_long_float(lispobj *where, lispobj object)
1491 struct vector *vector;
1492 long length, nwords;
1494 vector = (struct vector *) where;
1495 length = fixnum_value(vector->length);
1496 nwords = CEILING(length *
1497 LONG_FLOAT_SIZE
1498 + 2, 2);
1499 return nwords;
1502 static lispobj
1503 trans_vector_long_float(lispobj object)
1505 struct vector *vector;
1506 long length, nwords;
1508 gc_assert(is_lisp_pointer(object));
1510 vector = (struct vector *) native_pointer(object);
1511 length = fixnum_value(vector->length);
1512 nwords = CEILING(length * LONG_FLOAT_SIZE + 2, 2);
1514 return copy_large_unboxed_object(object, nwords);
1517 static long
1518 size_vector_long_float(lispobj *where)
1520 struct vector *vector;
1521 sword_t length, nwords;
1523 vector = (struct vector *) where;
1524 length = fixnum_value(vector->length);
1525 nwords = CEILING(length * LONG_FLOAT_SIZE + 2, 2);
1527 return nwords;
1529 #endif
1532 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
1533 static sword_t
1534 scav_vector_complex_single_float(lispobj *where, lispobj object)
1536 struct vector *vector;
1537 sword_t length, nwords;
1539 vector = (struct vector *) where;
1540 length = fixnum_value(vector->length);
1541 nwords = CEILING(NWORDS(length, 64) + 2, 2);
1543 return nwords;
1546 static lispobj
1547 trans_vector_complex_single_float(lispobj object)
1549 struct vector *vector;
1550 sword_t length, nwords;
1552 gc_assert(is_lisp_pointer(object));
1554 vector = (struct vector *) native_pointer(object);
1555 length = fixnum_value(vector->length);
1556 nwords = CEILING(NWORDS(length, 64) + 2, 2);
1558 return copy_large_unboxed_object(object, nwords);
1561 static sword_t
1562 size_vector_complex_single_float(lispobj *where)
1564 struct vector *vector;
1565 sword_t length, nwords;
1567 vector = (struct vector *) where;
1568 length = fixnum_value(vector->length);
1569 nwords = CEILING(NWORDS(length, 64) + 2, 2);
1571 return nwords;
1573 #endif
1575 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
1576 static sword_t
1577 scav_vector_complex_double_float(lispobj *where, lispobj object)
1579 struct vector *vector;
1580 sword_t length, nwords;
1582 vector = (struct vector *) where;
1583 length = fixnum_value(vector->length);
1584 nwords = CEILING(NWORDS(length, 128) + 2, 2);
1586 return nwords;
1589 static lispobj
1590 trans_vector_complex_double_float(lispobj object)
1592 struct vector *vector;
1593 sword_t length, nwords;
1595 gc_assert(is_lisp_pointer(object));
1597 vector = (struct vector *) native_pointer(object);
1598 length = fixnum_value(vector->length);
1599 nwords = CEILING(NWORDS(length, 128) + 2, 2);
1601 return copy_large_unboxed_object(object, nwords);
1604 static sword_t
1605 size_vector_complex_double_float(lispobj *where)
1607 struct vector *vector;
1608 sword_t length, nwords;
1610 vector = (struct vector *) where;
1611 length = fixnum_value(vector->length);
1612 nwords = CEILING(NWORDS(length, 128) + 2, 2);
1614 return nwords;
1616 #endif
1619 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
1620 static long
1621 scav_vector_complex_long_float(lispobj *where, lispobj object)
1623 struct vector *vector;
1624 sword_t length, nwords;
1626 vector = (struct vector *) where;
1627 length = fixnum_value(vector->length);
1628 nwords = CEILING(length * (2* LONG_FLOAT_SIZE) + 2, 2);
1630 return nwords;
1633 static lispobj
1634 trans_vector_complex_long_float(lispobj object)
1636 struct vector *vector;
1637 long length, nwords;
1639 gc_assert(is_lisp_pointer(object));
1641 vector = (struct vector *) native_pointer(object);
1642 length = fixnum_value(vector->length);
1643 nwords = CEILING(length * (2*LONG_FLOAT_SIZE) + 2, 2);
1645 return copy_large_unboxed_object(object, nwords);
1648 static long
1649 size_vector_complex_long_float(lispobj *where)
1651 struct vector *vector;
1652 long length, nwords;
1654 vector = (struct vector *) where;
1655 length = fixnum_value(vector->length);
1656 nwords = CEILING(length * (2*LONG_FLOAT_SIZE) + 2, 2);
1658 return nwords;
1660 #endif
1662 #define WEAK_POINTER_NWORDS \
1663 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
1665 static lispobj
1666 trans_weak_pointer(lispobj object)
1668 lispobj copy;
1669 #ifndef LISP_FEATURE_GENCGC
1670 struct weak_pointer *wp;
1671 #endif
1672 gc_assert(is_lisp_pointer(object));
1674 #if defined(DEBUG_WEAK)
1675 printf("Transporting weak pointer from 0x%08x\n", object);
1676 #endif
1678 /* Need to remember where all the weak pointers are that have */
1679 /* been transported so they can be fixed up in a post-GC pass. */
1681 copy = copy_object(object, WEAK_POINTER_NWORDS);
1682 #ifndef LISP_FEATURE_GENCGC
1683 wp = (struct weak_pointer *) native_pointer(copy);
1685 gc_assert(widetag_of(wp->header)==WEAK_POINTER_WIDETAG);
1686 /* Push the weak pointer onto the list of weak pointers. */
1687 wp->next = (struct weak_pointer *)LOW_WORD(weak_pointers);
1688 weak_pointers = wp;
1689 #endif
1690 return copy;
1693 static sword_t
1694 size_weak_pointer(lispobj *where)
1696 return WEAK_POINTER_NWORDS;
1700 void scan_weak_pointers(void)
1702 struct weak_pointer *wp, *next_wp;
1703 for (wp = weak_pointers, next_wp = NULL; wp != NULL; wp = next_wp) {
1704 lispobj value = wp->value;
1705 lispobj *first_pointer;
1706 gc_assert(widetag_of(wp->header)==WEAK_POINTER_WIDETAG);
1708 next_wp = wp->next;
1709 wp->next = NULL;
1710 if (next_wp == wp) /* gencgc uses a ref to self for end of list */
1711 next_wp = NULL;
1713 if (!is_lisp_pointer(value))
1714 continue;
1716 /* Now, we need to check whether the object has been forwarded. If
1717 * it has been, the weak pointer is still good and needs to be
1718 * updated. Otherwise, the weak pointer needs to be nil'ed
1719 * out. */
1721 if (from_space_p(value)) {
1722 first_pointer = (lispobj *)native_pointer(value);
1724 if (forwarding_pointer_p(first_pointer)) {
1725 wp->value=
1726 (lispobj)LOW_WORD(forwarding_pointer_value(first_pointer));
1727 } else {
1728 /* Break it. */
1729 wp->value = NIL;
1730 wp->broken = T;
1733 #ifdef LISP_FEATURE_IMMOBILE_SPACE
1734 else if (immobile_space_p(value) &&
1735 immobile_obj_gen_bits(native_pointer(value)) == from_space) {
1736 wp->value = NIL;
1737 wp->broken = T;
1739 #endif
1744 /* Hash tables */
1746 #if N_WORD_BITS == 32
1747 #define EQ_HASH_MASK 0x1fffffff
1748 #elif N_WORD_BITS == 64
1749 #define EQ_HASH_MASK 0x1fffffffffffffff
1750 #endif
1752 /* Compute the EQ-hash of KEY. This must match POINTER-HASH in
1753 * target-hash-table.lisp. */
1754 #define EQ_HASH(key) ((key) & EQ_HASH_MASK)
1756 /* List of weak hash tables chained through their NEXT-WEAK-HASH-TABLE
1757 * slot. Set to NULL at the end of a collection.
1759 * This is not optimal because, when a table is tenured, it won't be
1760 * processed automatically; only the yougest generation is GC'd by
1761 * default. On the other hand, all applications will need an
1762 * occasional full GC anyway, so it's not that bad either. */
1763 struct hash_table *weak_hash_tables = NULL;
1765 /* Return true if OBJ has already survived the current GC. */
1766 static inline int
1767 survived_gc_yet (lispobj obj)
1769 #ifdef LISP_FEATURE_IMMOBILE_SPACE
1770 /* If an immobile object's generation# is that of 'from_space', but has been
1771 visited (i.e. is live), then it is conceptually not in 'from_space'.
1772 This can happen when and only when _not_ raising the generation number.
1773 Since the gen_bits() accessor returns the visited bit, the byte value
1774 is numerically unequal to 'from_space', which is what we want */
1775 return !is_lisp_pointer(obj)
1776 || (immobile_space_p(obj)
1777 ? immobile_obj_gen_bits(native_pointer(obj)) != from_space
1778 : (!from_space_p(obj) || forwarding_pointer_p(native_pointer(obj))));
1779 #else
1780 return (!is_lisp_pointer(obj) || !from_space_p(obj) ||
1781 forwarding_pointer_p(native_pointer(obj)));
1782 #endif
1785 static inline int
1786 weak_hash_entry_alivep (lispobj weakness, lispobj key, lispobj value)
1788 switch (weakness) {
1789 case KEY:
1790 return survived_gc_yet(key);
1791 case VALUE:
1792 return survived_gc_yet(value);
1793 case KEY_OR_VALUE:
1794 return (survived_gc_yet(key) || survived_gc_yet(value));
1795 case KEY_AND_VALUE:
1796 return (survived_gc_yet(key) && survived_gc_yet(value));
1797 default:
1798 gc_assert(0);
1799 /* Shut compiler up. */
1800 return 0;
1804 /* Return the beginning of data in ARRAY (skipping the header and the
1805 * length) or NULL if it isn't an array of the specified widetag after
1806 * all. */
1807 static inline lispobj *
1808 get_array_data (lispobj array, int widetag, uword_t *length)
1810 if (is_lisp_pointer(array) &&
1811 (widetag_of(*(lispobj *)native_pointer(array)) == widetag)) {
1812 if (length != NULL)
1813 *length = fixnum_value(((lispobj *)native_pointer(array))[1]);
1814 return ((lispobj *)native_pointer(array)) + 2;
1815 } else {
1816 return NULL;
1820 /* Only need to worry about scavenging the _real_ entries in the
1821 * table. Phantom entries such as the hash table itself at index 0 and
1822 * the empty marker at index 1 were scavenged by scav_vector that
1823 * either called this function directly or arranged for it to be
1824 * called later by pushing the hash table onto weak_hash_tables. */
1825 static void
1826 scav_hash_table_entries (struct hash_table *hash_table)
1828 lispobj *kv_vector;
1829 uword_t kv_length;
1830 lispobj *index_vector;
1831 uword_t length;
1832 lispobj *next_vector;
1833 uword_t next_vector_length;
1834 lispobj *hash_vector;
1835 uword_t hash_vector_length;
1836 lispobj empty_symbol;
1837 lispobj weakness = hash_table->weakness;
1838 uword_t i;
1840 kv_vector = get_array_data(hash_table->table,
1841 SIMPLE_VECTOR_WIDETAG, &kv_length);
1842 if (kv_vector == NULL)
1843 lose("invalid kv_vector %x\n", hash_table->table);
1845 index_vector = get_array_data(hash_table->index_vector,
1846 SIMPLE_ARRAY_WORD_WIDETAG, &length);
1847 if (index_vector == NULL)
1848 lose("invalid index_vector %x\n", hash_table->index_vector);
1850 next_vector = get_array_data(hash_table->next_vector,
1851 SIMPLE_ARRAY_WORD_WIDETAG,
1852 &next_vector_length);
1853 if (next_vector == NULL)
1854 lose("invalid next_vector %x\n", hash_table->next_vector);
1856 hash_vector = get_array_data(hash_table->hash_vector,
1857 SIMPLE_ARRAY_WORD_WIDETAG,
1858 &hash_vector_length);
1859 if (hash_vector != NULL)
1860 gc_assert(hash_vector_length == next_vector_length);
1862 /* These lengths could be different as the index_vector can be a
1863 * different length from the others, a larger index_vector could
1864 * help reduce collisions. */
1865 gc_assert(next_vector_length*2 == kv_length);
1867 empty_symbol = kv_vector[1];
1868 /* fprintf(stderr,"* empty_symbol = %x\n", empty_symbol);*/
1869 if (widetag_of(*(lispobj *)native_pointer(empty_symbol)) !=
1870 SYMBOL_HEADER_WIDETAG) {
1871 lose("not a symbol where empty-hash-table-slot symbol expected: %x\n",
1872 *(lispobj *)native_pointer(empty_symbol));
1875 /* Work through the KV vector. */
1876 for (i = 1; i < next_vector_length; i++) {
1877 lispobj old_key = kv_vector[2*i];
1878 lispobj value = kv_vector[2*i+1];
1879 if ((weakness == NIL) ||
1880 weak_hash_entry_alivep(weakness, old_key, value)) {
1882 /* Scavenge the key and value. */
1883 scavenge(&kv_vector[2*i],2);
1885 /* If an EQ-based key has moved, mark the hash-table for
1886 * rehashing. */
1887 if (!hash_vector || hash_vector[i] == MAGIC_HASH_VECTOR_VALUE) {
1888 lispobj new_key = kv_vector[2*i];
1889 // FIXME: many EQ-based sxhash values are insensitive
1890 // to object movement. The most important one is SYMBOL,
1891 // but others also carry around a hash value: LAYOUT, CLASSOID,
1892 // and STANDARD-[FUNCALLABLE-]INSTANCE.
1893 // If old_key is any of those, don't set needs_rehash_p.
1894 if (old_key != new_key && new_key != empty_symbol) {
1895 hash_table->needs_rehash_p = T;
1902 sword_t
1903 scav_vector (lispobj *where, lispobj object)
1905 uword_t kv_length;
1906 struct hash_table *hash_table;
1908 /* SB-VM:VECTOR-VALID-HASHING-SUBTYPE is set for EQ-based and weak
1909 * hash tables in the Lisp HASH-TABLE code to indicate need for
1910 * special GC support. */
1911 if ((HeaderValue(object) & 0xFF) == subtype_VectorNormal)
1912 return 1;
1914 kv_length = fixnum_value(where[1]);
1915 /*FSHOW((stderr,"/kv_length = %d\n", kv_length));*/
1917 /* Scavenge element 0, which may be a hash-table structure. */
1918 scavenge(where+2, 1);
1919 if (!is_lisp_pointer(where[2])) {
1920 /* This'll happen when REHASH clears the header of old-kv-vector
1921 * and fills it with zero, but some other thread simulatenously
1922 * sets the header in %%PUTHASH.
1924 fprintf(stderr,
1925 "Warning: no pointer at %p in hash table: this indicates "
1926 "non-fatal corruption caused by concurrent access to a "
1927 "hash-table from multiple threads. Any accesses to "
1928 "hash-tables shared between threads should be protected "
1929 "by locks.\n", (void*)&where[2]);
1930 // We've scavenged three words.
1931 return 3;
1933 hash_table = (struct hash_table *)native_pointer(where[2]);
1934 /*FSHOW((stderr,"/hash_table = %x\n", hash_table));*/
1935 if (widetag_of(hash_table->header) != INSTANCE_HEADER_WIDETAG) {
1936 lose("hash table not instance (%x at %x)\n",
1937 hash_table->header,
1938 hash_table);
1941 /* Scavenge element 1, which should be some internal symbol that
1942 * the hash table code reserves for marking empty slots. */
1943 scavenge(where+3, 1);
1944 if (!is_lisp_pointer(where[3])) {
1945 lose("not empty-hash-table-slot symbol pointer: %x\n", where[3]);
1948 /* Scavenge hash table, which will fix the positions of the other
1949 * needed objects. */
1950 scavenge((lispobj *)hash_table,
1951 CEILING(sizeof(struct hash_table) / sizeof(lispobj), 2));
1953 /* Cross-check the kv_vector. */
1954 if (where != (lispobj *)native_pointer(hash_table->table)) {
1955 lose("hash_table table!=this table %x\n", hash_table->table);
1958 if (hash_table->weakness == NIL) {
1959 scav_hash_table_entries(hash_table);
1960 } else {
1961 /* Delay scavenging of this table by pushing it onto
1962 * weak_hash_tables (if it's not there already) for the weak
1963 * object phase. */
1964 if (hash_table->next_weak_hash_table == NIL) {
1965 hash_table->next_weak_hash_table = (lispobj)weak_hash_tables;
1966 weak_hash_tables = hash_table;
1970 return (CEILING(kv_length + 2, 2));
1973 void
1974 scav_weak_hash_tables (void)
1976 struct hash_table *table;
1978 /* Scavenge entries whose triggers are known to survive. */
1979 for (table = weak_hash_tables; table != NULL;
1980 table = (struct hash_table *)table->next_weak_hash_table) {
1981 scav_hash_table_entries(table);
1985 /* Walk through the chain whose first element is *FIRST and remove
1986 * dead weak entries. */
1987 static inline void
1988 scan_weak_hash_table_chain (struct hash_table *hash_table, lispobj *prev,
1989 lispobj *kv_vector, lispobj *index_vector,
1990 lispobj *next_vector, lispobj *hash_vector,
1991 lispobj empty_symbol, lispobj weakness)
1993 unsigned index = *prev;
1994 while (index) {
1995 unsigned next = next_vector[index];
1996 lispobj key = kv_vector[2 * index];
1997 lispobj value = kv_vector[2 * index + 1];
1998 gc_assert(key != empty_symbol);
1999 gc_assert(value != empty_symbol);
2000 if (!weak_hash_entry_alivep(weakness, key, value)) {
2001 unsigned count = fixnum_value(hash_table->number_entries);
2002 gc_assert(count > 0);
2003 *prev = next;
2004 hash_table->number_entries = make_fixnum(count - 1);
2005 next_vector[index] = fixnum_value(hash_table->next_free_kv);
2006 hash_table->next_free_kv = make_fixnum(index);
2007 kv_vector[2 * index] = empty_symbol;
2008 kv_vector[2 * index + 1] = empty_symbol;
2009 if (hash_vector)
2010 hash_vector[index] = MAGIC_HASH_VECTOR_VALUE;
2011 } else {
2012 prev = &next_vector[index];
2014 index = next;
2018 static void
2019 scan_weak_hash_table (struct hash_table *hash_table)
2021 lispobj *kv_vector;
2022 lispobj *index_vector;
2023 uword_t length = 0; /* prevent warning */
2024 lispobj *next_vector;
2025 uword_t next_vector_length = 0; /* prevent warning */
2026 lispobj *hash_vector;
2027 lispobj empty_symbol;
2028 lispobj weakness = hash_table->weakness;
2029 uword_t i;
2031 kv_vector = get_array_data(hash_table->table,
2032 SIMPLE_VECTOR_WIDETAG, NULL);
2033 index_vector = get_array_data(hash_table->index_vector,
2034 SIMPLE_ARRAY_WORD_WIDETAG, &length);
2035 next_vector = get_array_data(hash_table->next_vector,
2036 SIMPLE_ARRAY_WORD_WIDETAG,
2037 &next_vector_length);
2038 hash_vector = get_array_data(hash_table->hash_vector,
2039 SIMPLE_ARRAY_WORD_WIDETAG, NULL);
2040 empty_symbol = kv_vector[1];
2042 for (i = 0; i < length; i++) {
2043 scan_weak_hash_table_chain(hash_table, &index_vector[i],
2044 kv_vector, index_vector, next_vector,
2045 hash_vector, empty_symbol, weakness);
2049 /* Remove dead entries from weak hash tables. */
2050 void
2051 scan_weak_hash_tables (void)
2053 struct hash_table *table, *next;
2055 for (table = weak_hash_tables; table != NULL; table = next) {
2056 next = (struct hash_table *)table->next_weak_hash_table;
2057 table->next_weak_hash_table = NIL;
2058 scan_weak_hash_table(table);
2061 weak_hash_tables = NULL;
2066 * initialization
2069 static sword_t
2070 scav_lose(lispobj *where, lispobj object)
2072 lose("no scavenge function for object %p (widetag 0x%x)\n",
2073 (uword_t)object,
2074 widetag_of(*where));
2076 return 0; /* bogus return value to satisfy static type checking */
2079 static lispobj
2080 trans_lose(lispobj object)
2082 lose("no transport function for object %p (widetag 0x%x)\n",
2083 (void*)object,
2084 widetag_of(*(lispobj*)native_pointer(object)));
2085 return NIL; /* bogus return value to satisfy static type checking */
2088 static sword_t
2089 size_lose(lispobj *where)
2091 lose("no size function for object at %p (widetag 0x%x)\n",
2092 (void*)where,
2093 widetag_of(*where));
2094 return 1; /* bogus return value to satisfy static type checking */
2099 * initialization
2102 void
2103 gc_init_tables(void)
2105 uword_t i, j;
2107 /* Set default value in all slots of scavenge table. FIXME
2108 * replace this gnarly sizeof with something based on
2109 * N_WIDETAG_BITS */
2110 for (i = 0; i < ((sizeof scavtab)/(sizeof scavtab[0])); i++) {
2111 scavtab[i] = scav_lose;
2114 /* For each type which can be selected by the lowtag alone, set
2115 * multiple entries in our widetag scavenge table (one for each
2116 * possible value of the high bits).
2119 for (i = 0; i < (1<<(N_WIDETAG_BITS-N_LOWTAG_BITS)); i++) {
2120 for (j = 0; j < (1<<N_LOWTAG_BITS); j++) {
2121 if (fixnump(j)) {
2122 scavtab[j|(i<<N_LOWTAG_BITS)] = scav_immediate;
2125 scavtab[FUN_POINTER_LOWTAG|(i<<N_LOWTAG_BITS)] = scav_fun_pointer;
2126 /* skipping OTHER_IMMEDIATE_0_LOWTAG */
2127 scavtab[LIST_POINTER_LOWTAG|(i<<N_LOWTAG_BITS)] = scav_list_pointer;
2128 scavtab[INSTANCE_POINTER_LOWTAG|(i<<N_LOWTAG_BITS)] =
2129 scav_instance_pointer;
2130 /* skipping OTHER_IMMEDIATE_1_LOWTAG */
2131 scavtab[OTHER_POINTER_LOWTAG|(i<<N_LOWTAG_BITS)] = scav_other_pointer;
2134 /* Other-pointer types (those selected by all eight bits of the
2135 * tag) get one entry each in the scavenge table. */
2136 scavtab[BIGNUM_WIDETAG] = scav_unboxed;
2137 scavtab[RATIO_WIDETAG] = scav_boxed;
2138 #if N_WORD_BITS == 64
2139 scavtab[SINGLE_FLOAT_WIDETAG] = scav_immediate;
2140 #else
2141 scavtab[SINGLE_FLOAT_WIDETAG] = scav_unboxed;
2142 #endif
2143 scavtab[DOUBLE_FLOAT_WIDETAG] = scav_unboxed;
2144 #ifdef LONG_FLOAT_WIDETAG
2145 scavtab[LONG_FLOAT_WIDETAG] = scav_unboxed;
2146 #endif
2147 scavtab[COMPLEX_WIDETAG] = scav_boxed;
2148 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
2149 scavtab[COMPLEX_SINGLE_FLOAT_WIDETAG] = scav_unboxed;
2150 #endif
2151 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
2152 scavtab[COMPLEX_DOUBLE_FLOAT_WIDETAG] = scav_unboxed;
2153 #endif
2154 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
2155 scavtab[COMPLEX_LONG_FLOAT_WIDETAG] = scav_unboxed;
2156 #endif
2157 #ifdef SIMD_PACK_WIDETAG
2158 scavtab[SIMD_PACK_WIDETAG] = scav_unboxed;
2159 #endif
2160 scavtab[SIMPLE_ARRAY_WIDETAG] = scav_boxed;
2161 scavtab[SIMPLE_BASE_STRING_WIDETAG] = scav_base_string;
2162 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2163 scavtab[SIMPLE_CHARACTER_STRING_WIDETAG] = scav_character_string;
2164 #endif
2165 scavtab[SIMPLE_BIT_VECTOR_WIDETAG] = scav_vector_bit;
2166 scavtab[SIMPLE_ARRAY_NIL_WIDETAG] = scav_vector_nil;
2167 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG] =
2168 scav_vector_unsigned_byte_2;
2169 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG] =
2170 scav_vector_unsigned_byte_4;
2171 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG] =
2172 scav_vector_unsigned_byte_8;
2173 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG] =
2174 scav_vector_unsigned_byte_8;
2175 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG] =
2176 scav_vector_unsigned_byte_16;
2177 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG] =
2178 scav_vector_unsigned_byte_16;
2179 #if (N_WORD_BITS == 32)
2180 scavtab[SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG] =
2181 scav_vector_unsigned_byte_32;
2182 #endif
2183 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG] =
2184 scav_vector_unsigned_byte_32;
2185 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG] =
2186 scav_vector_unsigned_byte_32;
2187 #if (N_WORD_BITS == 64)
2188 scavtab[SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG] =
2189 scav_vector_unsigned_byte_64;
2190 #endif
2191 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2192 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG] =
2193 scav_vector_unsigned_byte_64;
2194 #endif
2195 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2196 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG] =
2197 scav_vector_unsigned_byte_64;
2198 #endif
2199 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2200 scavtab[SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG] = scav_vector_unsigned_byte_8;
2201 #endif
2202 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2203 scavtab[SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG] =
2204 scav_vector_unsigned_byte_16;
2205 #endif
2206 #if (N_WORD_BITS == 32)
2207 scavtab[SIMPLE_ARRAY_FIXNUM_WIDETAG] =
2208 scav_vector_unsigned_byte_32;
2209 #endif
2210 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2211 scavtab[SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG] =
2212 scav_vector_unsigned_byte_32;
2213 #endif
2214 #if (N_WORD_BITS == 64)
2215 scavtab[SIMPLE_ARRAY_FIXNUM_WIDETAG] =
2216 scav_vector_unsigned_byte_64;
2217 #endif
2218 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2219 scavtab[SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG] =
2220 scav_vector_unsigned_byte_64;
2221 #endif
2222 scavtab[SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG] = scav_vector_single_float;
2223 scavtab[SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG] = scav_vector_double_float;
2224 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2225 scavtab[SIMPLE_ARRAY_LONG_FLOAT_WIDETAG] = scav_vector_long_float;
2226 #endif
2227 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2228 scavtab[SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG] =
2229 scav_vector_complex_single_float;
2230 #endif
2231 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2232 scavtab[SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG] =
2233 scav_vector_complex_double_float;
2234 #endif
2235 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2236 scavtab[SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG] =
2237 scav_vector_complex_long_float;
2238 #endif
2239 scavtab[COMPLEX_BASE_STRING_WIDETAG] = scav_boxed;
2240 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
2241 scavtab[COMPLEX_CHARACTER_STRING_WIDETAG] = scav_boxed;
2242 #endif
2243 scavtab[COMPLEX_VECTOR_NIL_WIDETAG] = scav_boxed;
2244 scavtab[COMPLEX_BIT_VECTOR_WIDETAG] = scav_boxed;
2245 scavtab[COMPLEX_VECTOR_WIDETAG] = scav_boxed;
2246 scavtab[COMPLEX_ARRAY_WIDETAG] = scav_boxed;
2247 scavtab[CODE_HEADER_WIDETAG] = scav_code_header;
2248 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
2249 scavtab[SIMPLE_FUN_HEADER_WIDETAG] = scav_fun_header;
2250 scavtab[RETURN_PC_HEADER_WIDETAG] = scav_return_pc_header;
2251 #endif
2252 scavtab[FUNCALLABLE_INSTANCE_HEADER_WIDETAG] = scav_boxed;
2253 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2254 scavtab[CLOSURE_HEADER_WIDETAG] = scav_closure_header;
2255 #else
2256 scavtab[CLOSURE_HEADER_WIDETAG] = scav_boxed;
2257 #endif
2258 scavtab[VALUE_CELL_HEADER_WIDETAG] = scav_boxed;
2259 scavtab[SYMBOL_HEADER_WIDETAG] = scav_boxed;
2260 scavtab[CHARACTER_WIDETAG] = scav_immediate;
2261 scavtab[SAP_WIDETAG] = scav_unboxed;
2262 scavtab[UNBOUND_MARKER_WIDETAG] = scav_immediate;
2263 scavtab[NO_TLS_VALUE_MARKER_WIDETAG] = scav_immediate;
2264 scavtab[INSTANCE_HEADER_WIDETAG] = scav_instance;
2265 #if defined(LISP_FEATURE_SPARC) || defined(LISP_FEATURE_ARM)
2266 scavtab[FDEFN_WIDETAG] = scav_boxed;
2267 #else
2268 scavtab[FDEFN_WIDETAG] = scav_fdefn;
2269 #endif
2270 scavtab[SIMPLE_VECTOR_WIDETAG] = scav_vector;
2272 /* transport other table, initialized same way as scavtab */
2273 for (i = 0; i < ((sizeof transother)/(sizeof transother[0])); i++)
2274 transother[i] = trans_lose;
2275 transother[BIGNUM_WIDETAG] = trans_unboxed;
2276 transother[RATIO_WIDETAG] = trans_boxed;
2278 #if N_WORD_BITS == 64
2279 transother[SINGLE_FLOAT_WIDETAG] = trans_immediate;
2280 #else
2281 transother[SINGLE_FLOAT_WIDETAG] = trans_unboxed;
2282 #endif
2283 transother[DOUBLE_FLOAT_WIDETAG] = trans_unboxed;
2284 #ifdef LONG_FLOAT_WIDETAG
2285 transother[LONG_FLOAT_WIDETAG] = trans_unboxed;
2286 #endif
2287 transother[COMPLEX_WIDETAG] = trans_boxed;
2288 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
2289 transother[COMPLEX_SINGLE_FLOAT_WIDETAG] = trans_unboxed;
2290 #endif
2291 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
2292 transother[COMPLEX_DOUBLE_FLOAT_WIDETAG] = trans_unboxed;
2293 #endif
2294 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
2295 transother[COMPLEX_LONG_FLOAT_WIDETAG] = trans_unboxed;
2296 #endif
2297 transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed; /* but not GENCGC */
2298 transother[SIMPLE_BASE_STRING_WIDETAG] = trans_base_string;
2299 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2300 transother[SIMPLE_CHARACTER_STRING_WIDETAG] = trans_character_string;
2301 #endif
2302 transother[SIMPLE_BIT_VECTOR_WIDETAG] = trans_vector_bit;
2303 transother[SIMPLE_VECTOR_WIDETAG] = trans_vector;
2304 transother[SIMPLE_ARRAY_NIL_WIDETAG] = trans_vector_nil;
2305 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG] =
2306 trans_vector_unsigned_byte_2;
2307 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG] =
2308 trans_vector_unsigned_byte_4;
2309 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG] =
2310 trans_vector_unsigned_byte_8;
2311 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG] =
2312 trans_vector_unsigned_byte_8;
2313 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG] =
2314 trans_vector_unsigned_byte_16;
2315 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG] =
2316 trans_vector_unsigned_byte_16;
2317 #if (N_WORD_BITS == 32)
2318 transother[SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG] =
2319 trans_vector_unsigned_byte_32;
2320 #endif
2321 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG] =
2322 trans_vector_unsigned_byte_32;
2323 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG] =
2324 trans_vector_unsigned_byte_32;
2325 #if (N_WORD_BITS == 64)
2326 transother[SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG] =
2327 trans_vector_unsigned_byte_64;
2328 #endif
2329 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2330 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG] =
2331 trans_vector_unsigned_byte_64;
2332 #endif
2333 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2334 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG] =
2335 trans_vector_unsigned_byte_64;
2336 #endif
2337 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2338 transother[SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG] =
2339 trans_vector_unsigned_byte_8;
2340 #endif
2341 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2342 transother[SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG] =
2343 trans_vector_unsigned_byte_16;
2344 #endif
2345 #if (N_WORD_BITS == 32)
2346 transother[SIMPLE_ARRAY_FIXNUM_WIDETAG] =
2347 trans_vector_unsigned_byte_32;
2348 #endif
2349 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2350 transother[SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG] =
2351 trans_vector_unsigned_byte_32;
2352 #endif
2353 #if (N_WORD_BITS == 64)
2354 transother[SIMPLE_ARRAY_FIXNUM_WIDETAG] =
2355 trans_vector_unsigned_byte_64;
2356 #endif
2357 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2358 transother[SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG] =
2359 trans_vector_unsigned_byte_64;
2360 #endif
2361 transother[SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG] =
2362 trans_vector_single_float;
2363 transother[SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG] =
2364 trans_vector_double_float;
2365 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2366 transother[SIMPLE_ARRAY_LONG_FLOAT_WIDETAG] =
2367 trans_vector_long_float;
2368 #endif
2369 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2370 transother[SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG] =
2371 trans_vector_complex_single_float;
2372 #endif
2373 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2374 transother[SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG] =
2375 trans_vector_complex_double_float;
2376 #endif
2377 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2378 transother[SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG] =
2379 trans_vector_complex_long_float;
2380 #endif
2381 transother[COMPLEX_BASE_STRING_WIDETAG] = trans_boxed;
2382 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
2383 transother[COMPLEX_CHARACTER_STRING_WIDETAG] = trans_boxed;
2384 #endif
2385 transother[COMPLEX_BIT_VECTOR_WIDETAG] = trans_boxed;
2386 transother[COMPLEX_VECTOR_NIL_WIDETAG] = trans_boxed;
2387 transother[COMPLEX_VECTOR_WIDETAG] = trans_boxed;
2388 transother[COMPLEX_ARRAY_WIDETAG] = trans_boxed;
2389 transother[CODE_HEADER_WIDETAG] = trans_code_header;
2390 transother[SIMPLE_FUN_HEADER_WIDETAG] = trans_fun_header;
2391 transother[RETURN_PC_HEADER_WIDETAG] = trans_return_pc_header;
2392 transother[CLOSURE_HEADER_WIDETAG] = trans_boxed;
2393 transother[FUNCALLABLE_INSTANCE_HEADER_WIDETAG] = trans_boxed;
2394 transother[VALUE_CELL_HEADER_WIDETAG] = trans_boxed;
2395 transother[SYMBOL_HEADER_WIDETAG] = trans_tiny_boxed;
2396 transother[CHARACTER_WIDETAG] = trans_immediate;
2397 transother[SAP_WIDETAG] = trans_unboxed;
2398 #ifdef SIMD_PACK_WIDETAG
2399 transother[SIMD_PACK_WIDETAG] = trans_unboxed;
2400 #endif
2401 transother[UNBOUND_MARKER_WIDETAG] = trans_immediate;
2402 transother[NO_TLS_VALUE_MARKER_WIDETAG] = trans_immediate;
2403 transother[WEAK_POINTER_WIDETAG] = trans_weak_pointer;
2404 transother[INSTANCE_HEADER_WIDETAG] = trans_instance;
2405 transother[FDEFN_WIDETAG] = trans_tiny_boxed;
2407 /* size table, initialized the same way as scavtab */
2408 for (i = 0; i < ((sizeof sizetab)/(sizeof sizetab[0])); i++)
2409 sizetab[i] = size_lose;
2410 for (i = 0; i < (1<<(N_WIDETAG_BITS-N_LOWTAG_BITS)); i++) {
2411 for (j = 0; j < (1<<N_LOWTAG_BITS); j++) {
2412 if (fixnump(j)) {
2413 sizetab[j|(i<<N_LOWTAG_BITS)] = size_immediate;
2416 sizetab[FUN_POINTER_LOWTAG|(i<<N_LOWTAG_BITS)] = size_pointer;
2417 /* skipping OTHER_IMMEDIATE_0_LOWTAG */
2418 sizetab[LIST_POINTER_LOWTAG|(i<<N_LOWTAG_BITS)] = size_pointer;
2419 sizetab[INSTANCE_POINTER_LOWTAG|(i<<N_LOWTAG_BITS)] = size_pointer;
2420 /* skipping OTHER_IMMEDIATE_1_LOWTAG */
2421 sizetab[OTHER_POINTER_LOWTAG|(i<<N_LOWTAG_BITS)] = size_pointer;
2423 sizetab[BIGNUM_WIDETAG] = size_unboxed;
2424 sizetab[RATIO_WIDETAG] = size_boxed;
2425 #if N_WORD_BITS == 64
2426 sizetab[SINGLE_FLOAT_WIDETAG] = size_immediate;
2427 #else
2428 sizetab[SINGLE_FLOAT_WIDETAG] = size_unboxed;
2429 #endif
2430 sizetab[DOUBLE_FLOAT_WIDETAG] = size_unboxed;
2431 #ifdef LONG_FLOAT_WIDETAG
2432 sizetab[LONG_FLOAT_WIDETAG] = size_unboxed;
2433 #endif
2434 sizetab[COMPLEX_WIDETAG] = size_boxed;
2435 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
2436 sizetab[COMPLEX_SINGLE_FLOAT_WIDETAG] = size_unboxed;
2437 #endif
2438 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
2439 sizetab[COMPLEX_DOUBLE_FLOAT_WIDETAG] = size_unboxed;
2440 #endif
2441 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
2442 sizetab[COMPLEX_LONG_FLOAT_WIDETAG] = size_unboxed;
2443 #endif
2444 sizetab[SIMPLE_ARRAY_WIDETAG] = size_boxed;
2445 sizetab[SIMPLE_BASE_STRING_WIDETAG] = size_base_string;
2446 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2447 sizetab[SIMPLE_CHARACTER_STRING_WIDETAG] = size_character_string;
2448 #endif
2449 sizetab[SIMPLE_BIT_VECTOR_WIDETAG] = size_vector_bit;
2450 sizetab[SIMPLE_VECTOR_WIDETAG] = size_vector;
2451 sizetab[SIMPLE_ARRAY_NIL_WIDETAG] = size_vector_nil;
2452 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG] =
2453 size_vector_unsigned_byte_2;
2454 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG] =
2455 size_vector_unsigned_byte_4;
2456 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG] =
2457 size_vector_unsigned_byte_8;
2458 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG] =
2459 size_vector_unsigned_byte_8;
2460 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG] =
2461 size_vector_unsigned_byte_16;
2462 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG] =
2463 size_vector_unsigned_byte_16;
2464 #if (N_WORD_BITS == 32)
2465 sizetab[SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG] =
2466 size_vector_unsigned_byte_32;
2467 #endif
2468 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG] =
2469 size_vector_unsigned_byte_32;
2470 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG] =
2471 size_vector_unsigned_byte_32;
2472 #if (N_WORD_BITS == 64)
2473 sizetab[SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG] =
2474 size_vector_unsigned_byte_64;
2475 #endif
2476 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2477 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG] =
2478 size_vector_unsigned_byte_64;
2479 #endif
2480 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2481 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG] =
2482 size_vector_unsigned_byte_64;
2483 #endif
2484 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2485 sizetab[SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG] = size_vector_unsigned_byte_8;
2486 #endif
2487 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2488 sizetab[SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG] =
2489 size_vector_unsigned_byte_16;
2490 #endif
2491 #if (N_WORD_BITS == 32)
2492 sizetab[SIMPLE_ARRAY_FIXNUM_WIDETAG] =
2493 size_vector_unsigned_byte_32;
2494 #endif
2495 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2496 sizetab[SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG] =
2497 size_vector_unsigned_byte_32;
2498 #endif
2499 #if (N_WORD_BITS == 64)
2500 sizetab[SIMPLE_ARRAY_FIXNUM_WIDETAG] =
2501 size_vector_unsigned_byte_64;
2502 #endif
2503 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2504 sizetab[SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG] =
2505 size_vector_unsigned_byte_64;
2506 #endif
2507 sizetab[SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG] = size_vector_single_float;
2508 sizetab[SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG] = size_vector_double_float;
2509 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2510 sizetab[SIMPLE_ARRAY_LONG_FLOAT_WIDETAG] = size_vector_long_float;
2511 #endif
2512 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2513 sizetab[SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG] =
2514 size_vector_complex_single_float;
2515 #endif
2516 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2517 sizetab[SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG] =
2518 size_vector_complex_double_float;
2519 #endif
2520 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2521 sizetab[SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG] =
2522 size_vector_complex_long_float;
2523 #endif
2524 sizetab[COMPLEX_BASE_STRING_WIDETAG] = size_boxed;
2525 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
2526 sizetab[COMPLEX_CHARACTER_STRING_WIDETAG] = size_boxed;
2527 #endif
2528 sizetab[COMPLEX_VECTOR_NIL_WIDETAG] = size_boxed;
2529 sizetab[COMPLEX_BIT_VECTOR_WIDETAG] = size_boxed;
2530 sizetab[COMPLEX_VECTOR_WIDETAG] = size_boxed;
2531 sizetab[COMPLEX_ARRAY_WIDETAG] = size_boxed;
2532 sizetab[CODE_HEADER_WIDETAG] = size_code_header;
2533 #if 0
2534 /* We shouldn't see these, so just lose if it happens. */
2535 sizetab[SIMPLE_FUN_HEADER_WIDETAG] = size_function_header;
2536 sizetab[RETURN_PC_HEADER_WIDETAG] = size_return_pc_header;
2537 #endif
2538 sizetab[CLOSURE_HEADER_WIDETAG] = size_boxed;
2539 sizetab[FUNCALLABLE_INSTANCE_HEADER_WIDETAG] = size_boxed;
2540 sizetab[VALUE_CELL_HEADER_WIDETAG] = size_boxed;
2541 sizetab[SYMBOL_HEADER_WIDETAG] = size_tiny_boxed;
2542 sizetab[CHARACTER_WIDETAG] = size_immediate;
2543 sizetab[SAP_WIDETAG] = size_unboxed;
2544 #ifdef SIMD_PACK_WIDETAG
2545 sizetab[SIMD_PACK_WIDETAG] = size_unboxed;
2546 #endif
2547 sizetab[UNBOUND_MARKER_WIDETAG] = size_immediate;
2548 sizetab[NO_TLS_VALUE_MARKER_WIDETAG] = size_immediate;
2549 sizetab[WEAK_POINTER_WIDETAG] = size_weak_pointer;
2550 sizetab[INSTANCE_HEADER_WIDETAG] = size_instance;
2551 sizetab[FDEFN_WIDETAG] = size_tiny_boxed;
2555 /* Find the code object for the given pc, or return NULL on
2556 failure. */
2557 lispobj *
2558 component_ptr_from_pc(lispobj *pc)
2560 lispobj *object = NULL;
2562 if ( (object = search_read_only_space(pc)) )
2564 else if ( (object = search_static_space(pc)) )
2566 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2567 else if ( (object = search_immobile_space(pc)) )
2569 #endif
2570 else
2571 object = search_dynamic_space(pc);
2573 if (object) /* if we found something */
2574 if (widetag_of(*object) == CODE_HEADER_WIDETAG)
2575 return(object);
2577 return (NULL);
2580 /* Scan an area looking for an object which encloses the given pointer.
2581 * Return the object start on success or NULL on failure. */
2582 lispobj *
2583 gc_search_space(lispobj *start, size_t words, lispobj *pointer)
2585 while (words > 0) {
2586 size_t count = 1;
2587 lispobj *forwarded_start;
2589 if (forwarding_pointer_p(start))
2590 forwarded_start =
2591 native_pointer((lispobj)forwarding_pointer_value(start));
2592 else
2593 forwarded_start = start;
2594 lispobj thing = *forwarded_start;
2595 /* If thing is an immediate then this is a cons. */
2596 if (is_lisp_pointer(thing) || is_lisp_immediate(thing))
2597 count = 2;
2598 else
2599 count = (sizetab[widetag_of(thing)])(forwarded_start);
2601 /* Check whether the pointer is within this object. */
2602 if ((pointer >= start) && (pointer < (start+count))) {
2603 /* found it! */
2604 /*FSHOW((stderr,"/found %x in %x %x\n", pointer, start, thing));*/
2605 return(start);
2608 /* Round up the count. */
2609 count = CEILING(count,2);
2611 start += count;
2612 words -= count;
2614 return (NULL);
2617 /* Helper for valid_lisp_pointer_p (below) and
2618 * possibly_valid_dynamic_space_pointer (gencgc).
2620 * pointer is the pointer to validate, and start_addr is the address
2621 * of the enclosing object.
2624 looks_like_valid_lisp_pointer_p(lispobj pointer, lispobj *start_addr)
2626 if (!is_lisp_pointer(pointer)) {
2627 return 0;
2630 /* Check that the object pointed to is consistent with the pointer
2631 * low tag. */
2632 switch (lowtag_of(pointer)) {
2633 case FUN_POINTER_LOWTAG:
2634 /* Start_addr should be the enclosing code object, or a closure
2635 * header. */
2636 switch (widetag_of(*start_addr)) {
2637 case CODE_HEADER_WIDETAG:
2638 /* Make sure we actually point to a function in the code object,
2639 * as opposed to a random point there. */
2640 if (SIMPLE_FUN_HEADER_WIDETAG==widetag_of(native_pointer(pointer)[0]))
2641 return 1;
2642 else
2643 return 0;
2644 case CLOSURE_HEADER_WIDETAG:
2645 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2646 if (pointer != make_lispobj(start_addr, FUN_POINTER_LOWTAG)) {
2647 return 0;
2649 break;
2650 default:
2651 return 0;
2653 break;
2654 case LIST_POINTER_LOWTAG:
2655 if (pointer != make_lispobj(start_addr, LIST_POINTER_LOWTAG)) {
2656 return 0;
2658 /* Is it plausible cons? */
2659 if ((is_lisp_pointer(start_addr[0]) ||
2660 is_lisp_immediate(start_addr[0])) &&
2661 (is_lisp_pointer(start_addr[1]) ||
2662 is_lisp_immediate(start_addr[1])))
2663 break;
2664 else {
2665 return 0;
2667 case INSTANCE_POINTER_LOWTAG:
2668 if (pointer != make_lispobj(start_addr, INSTANCE_POINTER_LOWTAG)) {
2669 return 0;
2671 if (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
2672 return 0;
2674 break;
2675 case OTHER_POINTER_LOWTAG:
2677 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
2678 /* The all-architecture test below is good as far as it goes,
2679 * but an LRA object is similar to a FUN-POINTER: It is
2680 * embedded within a CODE-OBJECT pointed to by start_addr, and
2681 * cannot be found by simply walking the heap, therefore we
2682 * need to check for it. -- AB, 2010-Jun-04 */
2683 if ((widetag_of(start_addr[0]) == CODE_HEADER_WIDETAG)) {
2684 lispobj *potential_lra = native_pointer(pointer);
2685 if ((widetag_of(potential_lra[0]) == RETURN_PC_HEADER_WIDETAG) &&
2686 ((potential_lra - HeaderValue(potential_lra[0])) == start_addr)) {
2687 return 1; /* It's as good as we can verify. */
2690 #endif
2692 if (pointer != make_lispobj(start_addr, OTHER_POINTER_LOWTAG)) {
2693 return 0;
2695 /* Is it plausible? Not a cons. XXX should check the headers. */
2696 if (is_lisp_pointer(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
2697 return 0;
2699 switch (widetag_of(start_addr[0])) {
2700 case UNBOUND_MARKER_WIDETAG:
2701 case NO_TLS_VALUE_MARKER_WIDETAG:
2702 case CHARACTER_WIDETAG:
2703 #if N_WORD_BITS == 64
2704 case SINGLE_FLOAT_WIDETAG:
2705 #endif
2706 return 0;
2708 /* only pointed to by function pointers? */
2709 case CLOSURE_HEADER_WIDETAG:
2710 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2711 return 0;
2713 case INSTANCE_HEADER_WIDETAG:
2714 return 0;
2716 /* the valid other immediate pointer objects */
2717 case SIMPLE_VECTOR_WIDETAG:
2718 case RATIO_WIDETAG:
2719 case COMPLEX_WIDETAG:
2720 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
2721 case COMPLEX_SINGLE_FLOAT_WIDETAG:
2722 #endif
2723 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
2724 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
2725 #endif
2726 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
2727 case COMPLEX_LONG_FLOAT_WIDETAG:
2728 #endif
2729 #ifdef SIMD_PACK_WIDETAG
2730 case SIMD_PACK_WIDETAG:
2731 #endif
2732 case SIMPLE_ARRAY_WIDETAG:
2733 case COMPLEX_BASE_STRING_WIDETAG:
2734 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
2735 case COMPLEX_CHARACTER_STRING_WIDETAG:
2736 #endif
2737 case COMPLEX_VECTOR_NIL_WIDETAG:
2738 case COMPLEX_BIT_VECTOR_WIDETAG:
2739 case COMPLEX_VECTOR_WIDETAG:
2740 case COMPLEX_ARRAY_WIDETAG:
2741 case VALUE_CELL_HEADER_WIDETAG:
2742 case SYMBOL_HEADER_WIDETAG:
2743 case FDEFN_WIDETAG:
2744 case CODE_HEADER_WIDETAG:
2745 case BIGNUM_WIDETAG:
2746 #if N_WORD_BITS != 64
2747 case SINGLE_FLOAT_WIDETAG:
2748 #endif
2749 case DOUBLE_FLOAT_WIDETAG:
2750 #ifdef LONG_FLOAT_WIDETAG
2751 case LONG_FLOAT_WIDETAG:
2752 #endif
2753 case SIMPLE_BASE_STRING_WIDETAG:
2754 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2755 case SIMPLE_CHARACTER_STRING_WIDETAG:
2756 #endif
2757 case SIMPLE_BIT_VECTOR_WIDETAG:
2758 case SIMPLE_ARRAY_NIL_WIDETAG:
2759 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2760 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2761 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
2762 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2763 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
2764 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2766 case SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG:
2768 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
2769 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2770 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2771 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
2772 #endif
2773 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2774 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
2775 #endif
2776 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2777 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2778 #endif
2779 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2780 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2781 #endif
2783 case SIMPLE_ARRAY_FIXNUM_WIDETAG:
2785 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2786 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2787 #endif
2788 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2789 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
2790 #endif
2791 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2792 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2793 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2794 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2795 #endif
2796 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2797 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2798 #endif
2799 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2800 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2801 #endif
2802 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2803 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2804 #endif
2805 case SAP_WIDETAG:
2806 case WEAK_POINTER_WIDETAG:
2807 break;
2809 default:
2810 return 0;
2812 break;
2813 default:
2814 return 0;
2817 /* looks good */
2818 return 1;
2821 /* META: Note the ambiguous word "validate" in the comment below.
2822 * This means "Decide whether <x> is valid".
2823 * But when you see os_validate() elsewhere, that doesn't mean to ask
2824 * whether something is valid, it says to *make* it valid.
2825 * I think it would be nice if we could avoid using the word in the
2826 * sense in which os_validate() uses it, which would entail renaming
2827 * a bunch of stuff, which is harder than just explaining why
2828 * the comments can be deceptive */
2830 /* Used by the debugger to validate possibly bogus pointers before
2831 * calling MAKE-LISP-OBJ on them.
2833 * FIXME: We would like to make this perfect, because if the debugger
2834 * constructs a reference to a bugs lisp object, and it ends up in a
2835 * location scavenged by the GC all hell breaks loose.
2837 * Whereas possibly_valid_dynamic_space_pointer has to be conservative
2838 * and return true for all valid pointers, this could actually be eager
2839 * and lie about a few pointers without bad results... but that should
2840 * be reflected in the name.
2843 valid_lisp_pointer_p(lispobj *pointer)
2845 lispobj *start;
2846 if (((start=search_dynamic_space(pointer))!=NULL) ||
2847 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2848 ((start=search_immobile_space(pointer))!=NULL) ||
2849 #endif
2850 ((start=search_static_space(pointer))!=NULL) ||
2851 ((start=search_read_only_space(pointer))!=NULL))
2852 return looks_like_valid_lisp_pointer_p((lispobj)pointer, start);
2853 else
2854 return 0;
2857 boolean
2858 maybe_gc(os_context_t *context)
2860 lispobj gc_happened;
2861 struct thread *thread = arch_os_get_current_thread();
2862 boolean were_in_lisp = !foreign_function_call_active_p(thread);
2864 if (were_in_lisp) {
2865 fake_foreign_function_call(context);
2868 /* SUB-GC may return without GCing if *GC-INHIBIT* is set, in
2869 * which case we will be running with no gc trigger barrier
2870 * thing for a while. But it shouldn't be long until the end
2871 * of WITHOUT-GCING.
2873 * FIXME: It would be good to protect the end of dynamic space for
2874 * CheneyGC and signal a storage condition from there.
2877 /* Restore the signal mask from the interrupted context before
2878 * calling into Lisp if interrupts are enabled. Why not always?
2880 * Suppose there is a WITHOUT-INTERRUPTS block far, far out. If an
2881 * interrupt hits while in SUB-GC, it is deferred and the
2882 * os_context_sigmask of that interrupt is set to block further
2883 * deferrable interrupts (until the first one is
2884 * handled). Unfortunately, that context refers to this place and
2885 * when we return from here the signals will not be blocked.
2887 * A kludgy alternative is to propagate the sigmask change to the
2888 * outer context.
2890 #if !(defined(LISP_FEATURE_WIN32) || defined(LISP_FEATURE_SB_SAFEPOINT))
2891 check_gc_signals_unblocked_or_lose(os_context_sigmask_addr(context));
2892 unblock_gc_signals(0, 0);
2893 #endif
2894 FSHOW((stderr, "/maybe_gc: calling SUB_GC\n"));
2895 /* FIXME: Nothing must go wrong during GC else we end up running
2896 * the debugger, error handlers, and user code in general in a
2897 * potentially unsafe place. Running out of the control stack or
2898 * the heap in SUB-GC are ways to lose. Of course, deferrables
2899 * cannot be unblocked because there may be a pending handler, or
2900 * we may even be in a WITHOUT-INTERRUPTS. */
2901 gc_happened = funcall0(StaticSymbolFunction(SUB_GC));
2902 FSHOW((stderr, "/maybe_gc: gc_happened=%s\n",
2903 (gc_happened == NIL)
2904 ? "NIL"
2905 : ((gc_happened == T)
2906 ? "T"
2907 : "0")));
2908 /* gc_happened can take three values: T, NIL, 0.
2910 * T means that the thread managed to trigger a GC, and post-gc
2911 * must be called.
2913 * NIL means that the thread is within without-gcing, and no GC
2914 * has occurred.
2916 * Finally, 0 means that *a* GC has occurred, but it wasn't
2917 * triggered by this thread; success, but post-gc doesn't have
2918 * to be called.
2920 if ((gc_happened == T) &&
2921 /* See if interrupts are enabled or it's possible to enable
2922 * them. POST-GC has a similar check, but we don't want to
2923 * unlock deferrables in that case and get a pending interrupt
2924 * here. */
2925 ((SymbolValue(INTERRUPTS_ENABLED,thread) != NIL) ||
2926 (SymbolValue(ALLOW_WITH_INTERRUPTS,thread) != NIL))) {
2927 #ifndef LISP_FEATURE_WIN32
2928 sigset_t *context_sigmask = os_context_sigmask_addr(context);
2929 if (!deferrables_blocked_p(context_sigmask)) {
2930 thread_sigmask(SIG_SETMASK, context_sigmask, 0);
2931 #ifndef LISP_FEATURE_SB_SAFEPOINT
2932 check_gc_signals_unblocked_or_lose(0);
2933 #endif
2934 #endif
2935 FSHOW((stderr, "/maybe_gc: calling POST_GC\n"));
2936 funcall0(StaticSymbolFunction(POST_GC));
2937 #ifndef LISP_FEATURE_WIN32
2938 } else {
2939 FSHOW((stderr, "/maybe_gc: punting on POST_GC due to blockage\n"));
2941 #endif
2944 if (were_in_lisp) {
2945 undo_fake_foreign_function_call(context);
2946 } else {
2947 /* Otherwise done by undo_fake_foreign_function_call. And
2948 something later wants them to be blocked. What a nice
2949 interface.*/
2950 block_blockable_signals(0);
2953 FSHOW((stderr, "/maybe_gc: returning\n"));
2954 return (gc_happened != NIL);
2957 #define BYTES_ZERO_BEFORE_END (1<<12)
2959 /* There used to be a similar function called SCRUB-CONTROL-STACK in
2960 * Lisp and another called zero_stack() in cheneygc.c, but since it's
2961 * shorter to express in, and more often called from C, I keep only
2962 * the C one after fixing it. -- MG 2009-03-25 */
2964 /* Zero the unused portion of the control stack so that old objects
2965 * are not kept alive because of uninitialized stack variables.
2967 * "To summarize the problem, since not all allocated stack frame
2968 * slots are guaranteed to be written by the time you call an another
2969 * function or GC, there may be garbage pointers retained in your dead
2970 * stack locations. The stack scrubbing only affects the part of the
2971 * stack from the SP to the end of the allocated stack." - ram, on
2972 * cmucl-imp, Tue, 25 Sep 2001
2974 * So, as an (admittedly lame) workaround, from time to time we call
2975 * scrub-control-stack to zero out all the unused portion. This is
2976 * supposed to happen when the stack is mostly empty, so that we have
2977 * a chance of clearing more of it: callers are currently (2002.07.18)
2978 * REPL, SUB-GC and sig_stop_for_gc_handler. */
2980 /* Take care not to tread on the guard page and the hard guard page as
2981 * it would be unkind to sig_stop_for_gc_handler. Touching the return
2982 * guard page is not dangerous. For this to work the guard page must
2983 * be zeroed when protected. */
2985 /* FIXME: I think there is no guarantee that once
2986 * BYTES_ZERO_BEFORE_END bytes are zero the rest are also zero. This
2987 * may be what the "lame" adjective in the above comment is for. In
2988 * this case, exact gc may lose badly. */
2989 void
2990 scrub_control_stack()
2992 scrub_thread_control_stack(arch_os_get_current_thread());
2995 void
2996 scrub_thread_control_stack(struct thread *th)
2998 os_vm_address_t guard_page_address = CONTROL_STACK_GUARD_PAGE(th);
2999 os_vm_address_t hard_guard_page_address = CONTROL_STACK_HARD_GUARD_PAGE(th);
3000 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
3001 /* On these targets scrubbing from C is a bad idea, so we punt to
3002 * a routine in $ARCH-assem.S. */
3003 extern void arch_scrub_control_stack(struct thread *, os_vm_address_t, os_vm_address_t);
3004 arch_scrub_control_stack(th, guard_page_address, hard_guard_page_address);
3005 #else
3006 lispobj *sp = access_control_stack_pointer(th);
3007 scrub:
3008 if ((((os_vm_address_t)sp < (hard_guard_page_address + os_vm_page_size)) &&
3009 ((os_vm_address_t)sp >= hard_guard_page_address)) ||
3010 (((os_vm_address_t)sp < (guard_page_address + os_vm_page_size)) &&
3011 ((os_vm_address_t)sp >= guard_page_address) &&
3012 (th->control_stack_guard_page_protected != NIL)))
3013 return;
3014 #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD
3015 do {
3016 *sp = 0;
3017 } while (((uword_t)sp--) & (BYTES_ZERO_BEFORE_END - 1));
3018 if ((os_vm_address_t)sp < (hard_guard_page_address + os_vm_page_size))
3019 return;
3020 do {
3021 if (*sp)
3022 goto scrub;
3023 } while (((uword_t)sp--) & (BYTES_ZERO_BEFORE_END - 1));
3024 #else
3025 do {
3026 *sp = 0;
3027 } while (((uword_t)++sp) & (BYTES_ZERO_BEFORE_END - 1));
3028 if ((os_vm_address_t)sp >= hard_guard_page_address)
3029 return;
3030 do {
3031 if (*sp)
3032 goto scrub;
3033 } while (((uword_t)++sp) & (BYTES_ZERO_BEFORE_END - 1));
3034 #endif
3035 #endif /* LISP_FEATURE_C_STACK_IS_CONTROL_STACK */
3038 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
3040 void
3041 scavenge_control_stack(struct thread *th)
3043 lispobj *object_ptr;
3045 /* In order to properly support dynamic-extent allocation of
3046 * non-CONS objects, the control stack requires special handling.
3047 * Rather than calling scavenge() directly, grovel over it fixing
3048 * broken hearts, scavenging pointers to oldspace, and pitching a
3049 * fit when encountering unboxed data. This prevents stray object
3050 * headers from causing the scavenger to blow past the end of the
3051 * stack (an error case checked in scavenge()). We don't worry
3052 * about treating unboxed words as boxed or vice versa, because
3053 * the compiler isn't allowed to store unboxed objects on the
3054 * control stack. -- AB, 2011-Dec-02 */
3056 for (object_ptr = th->control_stack_start;
3057 object_ptr < access_control_stack_pointer(th);
3058 object_ptr++) {
3060 lispobj object = *object_ptr;
3061 #ifdef LISP_FEATURE_GENCGC
3062 if (forwarding_pointer_p(object_ptr))
3063 lose("unexpected forwarding pointer in scavenge_control_stack: %p, start=%p, end=%p\n",
3064 object_ptr, th->control_stack_start, access_control_stack_pointer(th));
3065 #endif
3066 if (is_lisp_pointer(object) && from_space_p(object)) {
3067 /* It currently points to old space. Check for a
3068 * forwarding pointer. */
3069 lispobj *ptr = native_pointer(object);
3070 if (forwarding_pointer_p(ptr)) {
3071 /* Yes, there's a forwarding pointer. */
3072 *object_ptr = LOW_WORD(forwarding_pointer_value(ptr));
3073 } else {
3074 /* Scavenge that pointer. */
3075 long n_words_scavenged =
3076 (scavtab[widetag_of(object)])(object_ptr, object);
3077 gc_assert(n_words_scavenged == 1);
3079 } else if (scavtab[widetag_of(object)] == scav_lose) {
3080 lose("unboxed object in scavenge_control_stack: %p->%x, start=%p, end=%p\n",
3081 object_ptr, object, th->control_stack_start, access_control_stack_pointer(th));
3086 /* Scavenging Interrupt Contexts */
3088 static int boxed_registers[] = BOXED_REGISTERS;
3090 /* The GC has a notion of an "interior pointer" register, an unboxed
3091 * register that typically contains a pointer to inside an object
3092 * referenced by another pointer. The most obvious of these is the
3093 * program counter, although many compiler backends define a "Lisp
3094 * Interior Pointer" register known to the runtime as reg_LIP, and
3095 * various CPU architectures have other registers that also partake of
3096 * the interior-pointer nature. As the code for pairing an interior
3097 * pointer value up with its "base" register, and fixing it up after
3098 * scavenging is complete is horribly repetitive, a few macros paper
3099 * over the monotony. --AB, 2010-Jul-14 */
3101 /* These macros are only ever used over a lexical environment which
3102 * defines a pointer to an os_context_t called context, thus we don't
3103 * bother to pass that context in as a parameter. */
3105 /* Define how to access a given interior pointer. */
3106 #define ACCESS_INTERIOR_POINTER_pc \
3107 *os_context_pc_addr(context)
3108 #define ACCESS_INTERIOR_POINTER_lip \
3109 *os_context_register_addr(context, reg_LIP)
3110 #define ACCESS_INTERIOR_POINTER_lr \
3111 *os_context_lr_addr(context)
3112 #define ACCESS_INTERIOR_POINTER_npc \
3113 *os_context_npc_addr(context)
3114 #define ACCESS_INTERIOR_POINTER_ctr \
3115 *os_context_ctr_addr(context)
3117 #define INTERIOR_POINTER_VARS(name) \
3118 uword_t name##_offset; \
3119 int name##_register_pair
3121 #define PAIR_INTERIOR_POINTER(name) \
3122 pair_interior_pointer(context, \
3123 ACCESS_INTERIOR_POINTER_##name, \
3124 &name##_offset, \
3125 &name##_register_pair)
3127 /* One complexity here is that if a paired register is not found for
3128 * an interior pointer, then that pointer does not get updated.
3129 * Originally, there was some commentary about using an index of -1
3130 * when calling os_context_register_addr() on SPARC referring to the
3131 * program counter, but the real reason is to allow an interior
3132 * pointer register to point to the runtime, read-only space, or
3133 * static space without problems. */
3134 #define FIXUP_INTERIOR_POINTER(name) \
3135 do { \
3136 if (name##_register_pair >= 0) { \
3137 ACCESS_INTERIOR_POINTER_##name = \
3138 (*os_context_register_addr(context, \
3139 name##_register_pair) \
3140 & ~LOWTAG_MASK) \
3141 + name##_offset; \
3143 } while (0)
3146 static void
3147 pair_interior_pointer(os_context_t *context, uword_t pointer,
3148 uword_t *saved_offset, int *register_pair)
3150 int i;
3153 * I (RLT) think this is trying to find the boxed register that is
3154 * closest to the LIP address, without going past it. Usually, it's
3155 * reg_CODE or reg_LRA. But sometimes, nothing can be found.
3157 /* 0x7FFFFFFF on 32-bit platforms;
3158 0x7FFFFFFFFFFFFFFF on 64-bit platforms */
3159 *saved_offset = (((uword_t)1) << (N_WORD_BITS - 1)) - 1;
3160 *register_pair = -1;
3161 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
3162 uword_t reg;
3163 sword_t offset;
3164 int index;
3166 index = boxed_registers[i];
3167 reg = *os_context_register_addr(context, index);
3169 /* An interior pointer is never relative to a non-pointer
3170 * register (an oversight in the original implementation).
3171 * The simplest argument for why this is true is to consider
3172 * the fixnum that happens by coincide to be the word-index in
3173 * memory of the header for some object plus two. This is
3174 * happenstance would cause the register containing the fixnum
3175 * to be selected as the register_pair if the interior pointer
3176 * is to anywhere after the first two words of the object.
3177 * The fixnum won't be changed during GC, but the object might
3178 * move, thus destroying the interior pointer. --AB,
3179 * 2010-Jul-14 */
3181 if (is_lisp_pointer(reg) &&
3182 ((reg & ~LOWTAG_MASK) <= pointer)) {
3183 offset = pointer - (reg & ~LOWTAG_MASK);
3184 if (offset < *saved_offset) {
3185 *saved_offset = offset;
3186 *register_pair = index;
3192 static void
3193 scavenge_interrupt_context(os_context_t * context)
3195 int i;
3197 /* FIXME: The various #ifdef noise here is precisely that: noise.
3198 * Is it possible to fold it into the macrology so that we have
3199 * one set of #ifdefs and then INTERIOR_POINTER_VARS /et alia/
3200 * compile out for the registers that don't exist on a given
3201 * platform? */
3203 INTERIOR_POINTER_VARS(pc);
3204 #ifdef reg_LIP
3205 INTERIOR_POINTER_VARS(lip);
3206 #endif
3207 #ifdef ARCH_HAS_LINK_REGISTER
3208 INTERIOR_POINTER_VARS(lr);
3209 #endif
3210 #ifdef ARCH_HAS_NPC_REGISTER
3211 INTERIOR_POINTER_VARS(npc);
3212 #endif
3213 #ifdef LISP_FEATURE_PPC
3214 INTERIOR_POINTER_VARS(ctr);
3215 #endif
3217 PAIR_INTERIOR_POINTER(pc);
3218 #ifdef reg_LIP
3219 PAIR_INTERIOR_POINTER(lip);
3220 #endif
3221 #ifdef ARCH_HAS_LINK_REGISTER
3222 PAIR_INTERIOR_POINTER(lr);
3223 #endif
3224 #ifdef ARCH_HAS_NPC_REGISTER
3225 PAIR_INTERIOR_POINTER(npc);
3226 #endif
3227 #ifdef LISP_FEATURE_PPC
3228 PAIR_INTERIOR_POINTER(ctr);
3229 #endif
3231 /* Scavenge all boxed registers in the context. */
3232 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
3233 int index;
3234 lispobj foo;
3236 index = boxed_registers[i];
3237 foo = *os_context_register_addr(context, index);
3238 scavenge(&foo, 1);
3239 *os_context_register_addr(context, index) = foo;
3241 /* this is unlikely to work as intended on bigendian
3242 * 64 bit platforms */
3244 scavenge((lispobj *) os_context_register_addr(context, index), 1);
3247 /* Now that the scavenging is done, repair the various interior
3248 * pointers. */
3249 FIXUP_INTERIOR_POINTER(pc);
3250 #ifdef reg_LIP
3251 FIXUP_INTERIOR_POINTER(lip);
3252 #endif
3253 #ifdef ARCH_HAS_LINK_REGISTER
3254 FIXUP_INTERIOR_POINTER(lr);
3255 #endif
3256 #ifdef ARCH_HAS_NPC_REGISTER
3257 FIXUP_INTERIOR_POINTER(npc);
3258 #endif
3259 #ifdef LISP_FEATURE_PPC
3260 FIXUP_INTERIOR_POINTER(ctr);
3261 #endif
3264 void
3265 scavenge_interrupt_contexts(struct thread *th)
3267 int i, index;
3268 os_context_t *context;
3270 index = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3272 #if defined(DEBUG_PRINT_CONTEXT_INDEX)
3273 printf("Number of active contexts: %d\n", index);
3274 #endif
3276 for (i = 0; i < index; i++) {
3277 context = th->interrupt_contexts[i];
3278 scavenge_interrupt_context(context);
3281 #endif /* x86oid targets */
3283 // The following accessors, which take a valid native pointer as input
3284 // and return a Lisp string, are designed to be foolproof during GC,
3285 // hence all the forwarding checks.
3287 #if defined(LISP_FEATURE_SB_LDB)
3288 #include "genesis/classoid.h"
3289 struct vector * symbol_name(lispobj * sym)
3291 if (forwarding_pointer_p(sym))
3292 sym = native_pointer((lispobj)forwarding_pointer_value(sym));
3293 if (lowtag_of(((struct symbol*)sym)->name) != OTHER_POINTER_LOWTAG)
3294 return NULL;
3295 lispobj * name = native_pointer(((struct symbol*)sym)->name);
3296 if (forwarding_pointer_p(name))
3297 name = native_pointer((lispobj)forwarding_pointer_value(name));
3298 return (struct vector*)name;
3300 struct vector * classoid_name(lispobj * classoid)
3302 if (forwarding_pointer_p(classoid))
3303 classoid = native_pointer((lispobj)forwarding_pointer_value(classoid));
3304 lispobj sym = ((struct classoid*)classoid)->name;
3305 return lowtag_of(sym) != OTHER_POINTER_LOWTAG ? NULL
3306 : symbol_name(native_pointer(sym));
3308 struct vector * layout_classoid_name(lispobj * layout)
3310 if (forwarding_pointer_p(layout))
3311 layout = native_pointer((lispobj)forwarding_pointer_value(layout));
3312 lispobj classoid = ((struct layout*)layout)->classoid;
3313 return lowtag_of(classoid) != INSTANCE_POINTER_LOWTAG ? NULL
3314 : classoid_name(native_pointer(classoid));
3316 struct vector * instance_classoid_name(lispobj * instance)
3318 if (forwarding_pointer_p(instance))
3319 instance = native_pointer((lispobj)forwarding_pointer_value(instance));
3320 lispobj layout = instance_layout(instance);
3321 return lowtag_of(layout) != INSTANCE_POINTER_LOWTAG ? NULL
3322 : layout_classoid_name(native_pointer(layout));
3324 void safely_show_lstring(struct vector * string, int quotes, FILE *s)
3326 extern void show_lstring(struct vector*, int, FILE*);
3327 if (forwarding_pointer_p((lispobj*)string))
3328 string = (struct vector*)forwarding_pointer_value((lispobj*)string);
3329 if (
3330 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
3331 widetag_of(string->header) == SIMPLE_CHARACTER_STRING_WIDETAG ||
3332 #endif
3333 widetag_of(string->header) == SIMPLE_BASE_STRING_WIDETAG)
3334 show_lstring(string, quotes, s);
3335 else {
3336 fprintf(s, "#<[widetag=%02X]>", widetag_of(string->header));
3339 #endif