Replace %CODE-ENTRY-POINTS with an array, remove %SIMPLE-FUN-NEXT.
[sbcl.git] / src / runtime / gc-common.c
blobd4e27831c84e22b7af4a6c673727719241ad2338
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 // The object at 'pointer' might already have been forwarded,
90 // but that's ok. Such occurs primarily when dealing with
91 // code components, because code can be forwarded by scavenging any
92 // pointer to a function that resides within the code.
93 // Testing whether the object had been forwarded would just slow
94 // things down, so we blindly stomp on whatever was there.
95 // Unfortunately this also implies we can't assert
96 // that we're operating on a not-yet-forwarded object here.
97 #ifdef LISP_FEATURE_GENCGC
98 pointer[0]=0x01;
99 pointer[1]=newspace_copy;
100 #else
101 pointer[0]=newspace_copy;
102 #endif
103 return newspace_copy;
106 sword_t (*scavtab[256])(lispobj *where, lispobj object);
107 lispobj (*transother[256])(lispobj object);
108 sword_t (*sizetab[256])(lispobj *where);
109 struct weak_pointer *weak_pointers;
111 os_vm_size_t bytes_consed_between_gcs = 12*1024*1024;
114 * copying objects
117 /* gc_general_copy_object is inline from gc-internal.h */
119 /* to copy a boxed object */
120 lispobj
121 copy_object(lispobj object, sword_t nwords)
123 return gc_general_copy_object(object, nwords, BOXED_PAGE_FLAG);
126 lispobj
127 copy_code_object(lispobj object, sword_t nwords)
129 return gc_general_copy_object(object, nwords, CODE_PAGE_FLAG);
132 static sword_t scav_lose(lispobj *where, lispobj object); /* forward decl */
134 /* FIXME: Most calls end up going to some trouble to compute an
135 * 'n_words' value for this function. The system might be a little
136 * simpler if this function used an 'end' parameter instead. */
137 void
138 scavenge(lispobj *start, sword_t n_words)
140 lispobj *end = start + n_words;
141 lispobj *object_ptr;
143 for (object_ptr = start; object_ptr < end;) {
144 lispobj object = *object_ptr;
145 #ifdef LISP_FEATURE_GENCGC
146 if (forwarding_pointer_p(object_ptr))
147 lose("unexpected forwarding pointer in scavenge: %p, start=%p, n=%ld\n",
148 object_ptr, start, n_words);
149 #endif
150 if (is_lisp_pointer(object)) {
151 if (from_space_p(object)) {
152 /* It currently points to old space. Check for a
153 * forwarding pointer. */
154 lispobj *ptr = native_pointer(object);
155 if (forwarding_pointer_p(ptr)) {
156 /* Yes, there's a forwarding pointer. */
157 *object_ptr = LOW_WORD(forwarding_pointer_value(ptr));
158 object_ptr++;
159 } else {
160 /* Scavenge that pointer. */
161 object_ptr +=
162 (scavtab[widetag_of(object)])(object_ptr, object);
164 #ifdef LISP_FEATURE_IMMOBILE_SPACE
165 } else if (immobile_space_p(object)) {
166 lispobj *ptr = native_pointer(object);
167 if (immobile_obj_gen_bits(ptr) == from_space)
168 promote_immobile_obj(ptr, 1);
169 object_ptr++;
170 #endif
171 } else {
172 /* It points somewhere other than oldspace. Leave it
173 * alone. */
174 object_ptr++;
177 else if (fixnump(object)) {
178 /* It's a fixnum: really easy.. */
179 object_ptr++;
180 } else {
181 /* It's some sort of header object or another. */
182 object_ptr += (scavtab[widetag_of(object)])(object_ptr, object);
185 gc_assert_verbose(object_ptr == end, "Final object pointer %p, start %p, end %p\n",
186 object_ptr, start, end);
189 static lispobj trans_fun_header(lispobj object); /* forward decls */
190 static lispobj trans_boxed(lispobj object);
192 static sword_t
193 scav_fun_pointer(lispobj *where, lispobj object)
195 lispobj *first_pointer;
196 lispobj copy;
198 gc_assert(is_lisp_pointer(object));
200 /* Object is a pointer into from_space - not a FP. */
201 first_pointer = (lispobj *) native_pointer(object);
203 /* must transport object -- object may point to either a function
204 * header, a closure function header, or to a closure header. */
206 switch (widetag_of(*first_pointer)) {
207 case SIMPLE_FUN_HEADER_WIDETAG:
208 copy = trans_fun_header(object);
209 break;
210 default:
211 copy = trans_boxed(object);
212 break;
215 if (copy != object) {
216 /* Set forwarding pointer */
217 set_forwarding_pointer(first_pointer,copy);
220 gc_assert(is_lisp_pointer(copy));
221 gc_assert(!from_space_p(copy));
223 *where = copy;
225 return 1;
229 static struct code *
230 trans_code(struct code *code)
232 /* if object has already been transported, just return pointer */
233 if (forwarding_pointer_p((lispobj *)code)) {
234 #ifdef DEBUG_CODE_GC
235 printf("Was already transported\n");
236 #endif
237 return (struct code *) forwarding_pointer_value
238 ((lispobj *)((pointer_sized_uint_t) code));
241 gc_assert(widetag_of(code->header) == CODE_HEADER_WIDETAG);
243 /* prepare to transport the code vector */
244 lispobj l_code = (lispobj) LOW_WORD(code) | OTHER_POINTER_LOWTAG;
245 sword_t nheader_words = code_header_words(code->header);
246 sword_t ncode_words = code_instruction_words(code->code_size);
247 sword_t nwords = nheader_words + ncode_words;
248 lispobj l_new_code = copy_code_object(l_code, nwords);
249 struct code *new_code = (struct code *) native_pointer(l_new_code);
251 #if defined(DEBUG_CODE_GC)
252 printf("Old code object at 0x%08x, new code object at 0x%08x.\n",
253 (uword_t) code, (uword_t) new_code);
254 printf("Code object is %d words long.\n", nwords);
255 #endif
257 #ifdef LISP_FEATURE_GENCGC
258 if (new_code == code)
259 return new_code;
260 #endif
262 set_forwarding_pointer((lispobj *)code, l_new_code);
264 /* set forwarding pointers for all the function headers in the */
265 /* code object. also fix all self pointers */
266 /* Do this by scanning the new code, since the old header is unusable */
268 uword_t displacement = l_new_code - l_code;
270 for_each_simple_fun(i, nfheaderp, new_code, 1, {
271 /* Calculate the old raw function pointer */
272 struct simple_fun* fheaderp =
273 (struct simple_fun*)((char*)nfheaderp - displacement);
274 /* Calculate the new lispobj */
275 lispobj nfheaderl = make_lispobj(nfheaderp, FUN_POINTER_LOWTAG);
277 #ifdef DEBUG_CODE_GC
278 printf("fheaderp->header (at %x) <- %x\n",
279 &(fheaderp->header) , nfheaderl);
280 #endif
281 set_forwarding_pointer((lispobj *)fheaderp, nfheaderl);
283 /* fix self pointer. */
284 nfheaderp->self =
285 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
286 FUN_RAW_ADDR_OFFSET +
287 #endif
288 nfheaderl;
290 #ifdef LISP_FEATURE_GENCGC
291 /* Cheneygc doesn't need this os_flush_icache, it flushes the whole
292 spaces once when all copying is done. */
293 os_flush_icache((os_vm_address_t) (((sword_t *)new_code) + nheader_words),
294 ncode_words * sizeof(sword_t));
296 #endif
298 #ifdef LISP_FEATURE_X86
299 gencgc_apply_code_fixups(code, new_code);
300 #endif
302 return new_code;
305 static sword_t
306 scav_code_header(lispobj *where, lispobj header)
308 struct code *code = (struct code *) where;
309 sword_t n_header_words = code_header_words(header);
311 /* Scavenge the boxed section of the code data block. */
312 scavenge(where + 1, n_header_words - 1);
314 /* Scavenge the boxed section of each function object in the
315 * code data block. */
316 for_each_simple_fun(i, function_ptr, code, 1, {
317 scavenge(SIMPLE_FUN_SCAV_START(function_ptr),
318 SIMPLE_FUN_SCAV_NWORDS(function_ptr));
321 return n_header_words + code_instruction_words(code->code_size);
324 static lispobj
325 trans_code_header(lispobj object)
327 struct code *ncode;
329 ncode = trans_code((struct code *) native_pointer(object));
330 return (lispobj) LOW_WORD(ncode) | OTHER_POINTER_LOWTAG;
334 static sword_t
335 size_code_header(lispobj *where)
337 return code_header_words(((struct code *)where)->header)
338 + code_instruction_words(((struct code *)where)->code_size);
341 #if !defined(LISP_FEATURE_X86) && ! defined(LISP_FEATURE_X86_64)
342 static sword_t
343 scav_return_pc_header(lispobj *where, lispobj object)
345 lose("attempted to scavenge a return PC header where=%p object=%#lx\n",
346 where, (uword_t) object);
347 return 0; /* bogus return value to satisfy static type checking */
349 #endif /* LISP_FEATURE_X86 */
351 static lispobj
352 trans_return_pc_header(lispobj object)
354 struct simple_fun *return_pc;
355 uword_t offset;
356 struct code *code, *ncode;
358 return_pc = (struct simple_fun *) native_pointer(object);
359 offset = HeaderValue(return_pc->header) * N_WORD_BYTES;
361 /* Transport the whole code object */
362 code = (struct code *) ((uword_t) return_pc - offset);
363 ncode = trans_code(code);
365 return ((lispobj) LOW_WORD(ncode) + offset) | OTHER_POINTER_LOWTAG;
368 /* On the 386, closures hold a pointer to the raw address instead of the
369 * function object, so we can use CALL [$FDEFN+const] to invoke
370 * the function without loading it into a register. Given that code
371 * objects don't move, we don't need to update anything, but we do
372 * have to figure out that the function is still live. */
374 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
375 static sword_t
376 scav_closure_header(lispobj *where, lispobj object)
378 struct closure *closure;
379 lispobj fun;
381 closure = (struct closure *)where;
382 fun = closure->fun - FUN_RAW_ADDR_OFFSET;
383 scavenge(&fun, 1);
384 #ifdef LISP_FEATURE_GENCGC
385 /* The function may have moved so update the raw address. But
386 * don't write unnecessarily. */
387 if (closure->fun != fun + FUN_RAW_ADDR_OFFSET)
388 closure->fun = fun + FUN_RAW_ADDR_OFFSET;
389 #endif
390 return 2;
392 #endif
394 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
395 static sword_t
396 scav_fun_header(lispobj *where, lispobj object)
398 lose("attempted to scavenge a function header where=%p object=%#lx\n",
399 where, (uword_t) object);
400 return 0; /* bogus return value to satisfy static type checking */
402 #endif /* LISP_FEATURE_X86 */
404 static lispobj
405 trans_fun_header(lispobj object)
407 struct simple_fun *fheader;
408 uword_t offset;
409 struct code *code, *ncode;
411 fheader = (struct simple_fun *) native_pointer(object);
412 offset = HeaderValue(fheader->header) * N_WORD_BYTES;
414 /* Transport the whole code object */
415 code = (struct code *) ((uword_t) fheader - offset);
416 ncode = trans_code(code);
418 return ((lispobj) LOW_WORD(ncode) + offset) | FUN_POINTER_LOWTAG;
423 * instances
426 static lispobj
427 trans_instance(lispobj object)
429 lispobj header;
430 uword_t length;
432 gc_assert(is_lisp_pointer(object));
434 header = *((lispobj *) native_pointer(object));
435 length = instance_length(header) + 1;
436 length = CEILING(length, 2);
438 return copy_object(object, length);
441 static sword_t
442 size_instance(lispobj *where)
444 lispobj header;
445 uword_t length;
447 header = *where;
448 length = instance_length(header) + 1;
449 length = CEILING(length, 2);
451 return length;
454 static sword_t
455 scav_instance_pointer(lispobj *where, lispobj object)
457 lispobj copy, *first_pointer;
459 /* Object is a pointer into from space - not a FP. */
460 copy = trans_instance(object);
462 #ifdef LISP_FEATURE_GENCGC
463 gc_assert(copy != object);
464 #endif
466 first_pointer = (lispobj *) native_pointer(object);
467 set_forwarding_pointer(first_pointer,copy);
468 *where = copy;
470 return 1;
475 * lists and conses
478 static lispobj trans_list(lispobj object);
480 static sword_t
481 scav_list_pointer(lispobj *where, lispobj object)
483 lispobj first;
484 gc_assert(is_lisp_pointer(object));
486 first = trans_list(object);
487 gc_assert(first != object);
489 gc_assert(is_lisp_pointer(first));
490 gc_assert(!from_space_p(first));
492 *where = first;
493 return 1;
497 static lispobj
498 trans_list(lispobj object)
500 lispobj new_list_pointer;
501 struct cons *cons, *new_cons;
502 lispobj cdr;
504 cons = (struct cons *) native_pointer(object);
506 /* Copy 'object'. */
507 new_cons = (struct cons *)
508 gc_general_alloc(sizeof(struct cons), BOXED_PAGE_FLAG, ALLOC_QUICK);
509 new_cons->car = cons->car;
510 new_cons->cdr = cons->cdr; /* updated later */
511 new_list_pointer = make_lispobj(new_cons,lowtag_of(object));
513 /* Grab the cdr: set_forwarding_pointer will clobber it in GENCGC */
514 cdr = cons->cdr;
516 set_forwarding_pointer((lispobj *)cons, new_list_pointer);
518 /* Try to linearize the list in the cdr direction to help reduce
519 * paging. */
520 while (1) {
521 lispobj new_cdr;
522 struct cons *cdr_cons, *new_cdr_cons;
524 if(lowtag_of(cdr) != LIST_POINTER_LOWTAG ||
525 !from_space_p(cdr) ||
526 forwarding_pointer_p((lispobj *)native_pointer(cdr)))
527 break;
529 cdr_cons = (struct cons *) native_pointer(cdr);
531 /* Copy 'cdr'. */
532 new_cdr_cons = (struct cons*)
533 gc_general_alloc(sizeof(struct cons), BOXED_PAGE_FLAG, ALLOC_QUICK);
534 new_cdr_cons->car = cdr_cons->car;
535 new_cdr_cons->cdr = cdr_cons->cdr;
536 new_cdr = make_lispobj(new_cdr_cons, lowtag_of(cdr));
538 /* Grab the cdr before it is clobbered. */
539 cdr = cdr_cons->cdr;
540 set_forwarding_pointer((lispobj *)cdr_cons, new_cdr);
542 /* Update the cdr of the last cons copied into new space to
543 * keep the newspace scavenge from having to do it. */
544 new_cons->cdr = new_cdr;
546 new_cons = new_cdr_cons;
549 return new_list_pointer;
554 * scavenging and transporting other pointers
557 static sword_t
558 scav_other_pointer(lispobj *where, lispobj object)
560 lispobj first, *first_pointer;
562 gc_assert(is_lisp_pointer(object));
564 /* Object is a pointer into from space - not FP. */
565 first_pointer = (lispobj *) native_pointer(object);
566 first = (transother[widetag_of(*first_pointer)])(object);
568 // If the object was large, then instead of transporting it,
569 // gencgc might simply promote the pages and return the same pointer.
570 // That decision is made in general_copy_large_object().
571 if (first != object) {
572 set_forwarding_pointer(first_pointer, first);
573 #ifdef LISP_FEATURE_GENCGC
574 *where = first;
575 #endif
577 #ifndef LISP_FEATURE_GENCGC
578 *where = first;
579 #endif
580 gc_assert(is_lisp_pointer(first));
581 gc_assert(!from_space_p(first));
583 return 1;
587 * immediate, boxed, and unboxed objects
590 static sword_t
591 size_pointer(lispobj *where)
593 return 1;
596 static sword_t
597 scav_immediate(lispobj *where, lispobj object)
599 return 1;
602 static lispobj
603 trans_immediate(lispobj object)
605 lose("trying to transport an immediate\n");
606 return NIL; /* bogus return value to satisfy static type checking */
609 static sword_t
610 size_immediate(lispobj *where)
612 return 1;
616 static sword_t
617 scav_boxed(lispobj *where, lispobj object)
619 return 1;
622 boolean positive_bignum_logbitp(int index, struct bignum* bignum)
624 /* If the bignum in the layout has another pointer to it (besides the layout)
625 acting as a root, and which is scavenged first, then transporting the
626 bignum causes the layout to see a FP, as would copying an instance whose
627 layout that is. This is a nearly impossible scenario to create organically
628 in Lisp, because mostly nothing ever looks again at that exact (EQ) bignum
629 except for a few things that would cause it to be pinned anyway,
630 such as it being kept in a local variable during structure manipulation.
631 See 'interleaved-raw.impure.lisp' for a way to trigger this */
632 if (forwarding_pointer_p((lispobj*)bignum)) {
633 lispobj *forwarded = forwarding_pointer_value((lispobj*)bignum);
634 #if 0
635 fprintf(stderr, "GC bignum_logbitp(): fwd from %p to %p\n",
636 (void*)bignum, (void*)forwarded);
637 #endif
638 bignum = (struct bignum*)native_pointer((lispobj)forwarded);
641 int len = HeaderValue(bignum->header);
642 int word_index = index / N_WORD_BITS;
643 int bit_index = index % N_WORD_BITS;
644 if (word_index >= len) {
645 // just return 0 since the marking logic does not allow negative bignums
646 return 0;
647 } else {
648 return (bignum->digits[word_index] >> bit_index) & 1;
652 // Helper function for helper function below, since lambda isn't a thing
653 static void instance_scan_range(void* instance_ptr, int offset, int nwords)
655 scavenge((lispobj*)instance_ptr + offset, nwords);
658 // Helper function for stepping through the tagged slots of an instance in
659 // scav_instance and verify_space.
660 void
661 instance_scan_interleaved(void (*proc)(lispobj*, sword_t),
662 lispobj *instance_ptr,
663 sword_t n_words,
664 lispobj *layout_obj)
666 struct layout *layout = (struct layout*)layout_obj;
667 lispobj layout_bitmap = layout->bitmap;
668 sword_t index;
670 /* This code might be made more efficient by run-length-encoding the ranges
671 of words to scan, but probably not by much */
673 ++instance_ptr; // was supplied as the address of the header word
674 if (fixnump(layout_bitmap)) {
675 sword_t bitmap = (sword_t)layout_bitmap >> N_FIXNUM_TAG_BITS; // signed integer!
676 for (index = 0; index < n_words ; index++, bitmap >>= 1)
677 if (bitmap & 1)
678 proc(instance_ptr + index, 1);
679 } else { /* huge bitmap */
680 struct bignum * bitmap;
681 bitmap = (struct bignum*)native_pointer(layout_bitmap);
682 if (forwarding_pointer_p((lispobj*)bitmap))
683 bitmap = (struct bignum*)
684 native_pointer((lispobj)forwarding_pointer_value((lispobj*)bitmap));
685 bitmap_scan((uword_t*)bitmap->digits, HeaderValue(bitmap->header), 0,
686 instance_scan_range, instance_ptr);
690 void bitmap_scan(uword_t* bitmap, int n_bitmap_words, int flags,
691 void (*proc)(void*, int, int), void* arg)
693 uword_t sense = (flags & BIT_SCAN_INVERT) ? ~0L : 0;
694 int start_word_index = 0;
695 int shift = 0;
696 in_use_marker_t word;
698 flags = flags & BIT_SCAN_CLEAR;
700 // Rather than bzero'ing we can just clear each nonzero word as it's read,
701 // if so specified.
702 #define BITMAP_REF(j) word = bitmap[j]; if(word && flags) bitmap[j] = 0; word ^= sense
703 BITMAP_REF(0);
704 while (1) {
705 int skip_bits, start_bit, start_position, run_length;
706 if (word == 0) {
707 if (++start_word_index >= n_bitmap_words) break;
708 BITMAP_REF(start_word_index);
709 shift = 0;
710 continue;
712 // On each loop iteration, the lowest 1 bit is a "relative"
713 // bit index, since the word was already shifted. This is 'skip_bits'.
714 // Adding back in the total shift amount gives 'start_bit',
715 // the true absolute index within the current word.
716 // 'start_position' is absolute within the entire bitmap.
717 skip_bits = ffsl(word) - 1;
718 start_bit = skip_bits + shift;
719 start_position = N_WORD_BITS * start_word_index + start_bit;
720 // Compute the number of consecutive 1s in the current word.
721 word >>= skip_bits;
722 run_length = ~word ? ffsl(~word) - 1 : N_WORD_BITS;
723 if (start_bit + run_length < N_WORD_BITS) { // Do not extend to additional words.
724 word >>= run_length;
725 shift += skip_bits + run_length;
726 } else {
727 int end_word_index = ++start_word_index;
728 while (1) {
729 if (end_word_index >= n_bitmap_words) {
730 word = 0;
731 run_length += (end_word_index - start_word_index) * N_WORD_BITS;
732 break;
734 BITMAP_REF(end_word_index);
735 if (~word == 0)
736 ++end_word_index;
737 else {
738 // end_word_index is the exclusive bound on contiguous
739 // words to include in the range. See if the low bits
740 // from the next word can extend the range.
741 shift = ffsl(~word) - 1;
742 word >>= shift;
743 run_length += (end_word_index - start_word_index) * N_WORD_BITS
744 + shift;
745 break;
748 start_word_index = end_word_index;
750 proc(arg, start_position, run_length);
752 #undef BITMAP_REF
755 static sword_t
756 scav_instance(lispobj *where, lispobj header)
758 // instance_length() is the number of words following the header including
759 // the layout. If this is an even number, it should be made odd so that
760 // scav_instance() always consumes an even number of words in total.
761 sword_t ntotal = instance_length(header) | 1;
762 lispobj* layout = (lispobj*)instance_layout(where);
764 if (!layout)
765 return 1;
766 layout = native_pointer((lispobj)layout);
767 #ifdef LISP_FEATURE_COMPACT_INSTANCE_HEADER
768 if (__immobile_obj_gen_bits(layout) == from_space)
769 promote_immobile_obj(layout, 1);
770 #else
771 if (forwarding_pointer_p(layout))
772 layout = native_pointer((lispobj)forwarding_pointer_value(layout));
773 #endif
775 if (((struct layout*)layout)->bitmap == make_fixnum(-1))
776 scavenge(where+1, ntotal);
777 else
778 instance_scan_interleaved(scavenge, where, ntotal, layout);
780 return ntotal + 1;
783 static lispobj
784 trans_boxed(lispobj object)
786 lispobj header;
787 uword_t length;
789 gc_assert(is_lisp_pointer(object));
791 header = *((lispobj *) native_pointer(object));
792 length = HeaderValue(header) + 1;
793 length = CEILING(length, 2);
795 return copy_object(object, length);
798 static sword_t
799 size_boxed(lispobj *where)
801 lispobj header;
802 uword_t length;
804 header = *where;
805 length = HeaderValue(header) + 1;
806 length = CEILING(length, 2);
808 return length;
811 static lispobj
812 trans_tiny_boxed(lispobj object)
814 lispobj header;
815 uword_t length;
817 gc_assert(is_lisp_pointer(object));
819 header = *((lispobj *) native_pointer(object));
820 length = (HeaderValue(header) & 0xFF) + 1;
821 length = CEILING(length, 2);
823 return copy_object(object, length);
826 static sword_t
827 size_tiny_boxed(lispobj *where)
829 lispobj header;
830 uword_t length;
832 header = *where;
833 length = (HeaderValue(header) & 0xFF) + 1;
834 length = CEILING(length, 2);
836 return length;
839 /* Note: on the sparc we don't have to do anything special for fdefns, */
840 /* 'cause the raw-addr has a function lowtag. */
841 #if !defined(LISP_FEATURE_SPARC) && !defined(LISP_FEATURE_ARM)
842 static sword_t
843 scav_fdefn(lispobj *where, lispobj object)
845 struct fdefn *fdefn;
847 fdefn = (struct fdefn *)where;
849 /* FSHOW((stderr, "scav_fdefn, function = %p, raw_addr = %p\n",
850 fdefn->fun, fdefn->raw_addr)); */
852 scavenge(where + 1, 2); // 'name' and 'fun'
853 lispobj raw_fun = (lispobj)fdefn->raw_addr;
854 if (raw_fun > READ_ONLY_SPACE_END) {
855 lispobj simple_fun = raw_fun - FUN_RAW_ADDR_OFFSET;
856 scavenge(&simple_fun, 1);
857 /* Don't write unnecessarily. */
858 if (simple_fun != raw_fun - FUN_RAW_ADDR_OFFSET)
859 fdefn->raw_addr = (char *)simple_fun + FUN_RAW_ADDR_OFFSET;
861 return 4;
863 #endif
865 static sword_t
866 scav_unboxed(lispobj *where, lispobj object)
868 uword_t length;
870 length = HeaderValue(object) + 1;
871 length = CEILING(length, 2);
873 return length;
876 static lispobj
877 trans_unboxed(lispobj object)
879 lispobj header;
880 uword_t length;
883 gc_assert(is_lisp_pointer(object));
885 header = *((lispobj *) native_pointer(object));
886 length = HeaderValue(header) + 1;
887 length = CEILING(length, 2);
889 return copy_unboxed_object(object, length);
892 static sword_t
893 size_unboxed(lispobj *where)
895 lispobj header;
896 uword_t length;
898 header = *where;
899 length = HeaderValue(header) + 1;
900 length = CEILING(length, 2);
902 return length;
906 /* vector-like objects */
907 static sword_t
908 scav_base_string(lispobj *where, lispobj object)
910 struct vector *vector;
911 sword_t length, nwords;
913 /* NOTE: Strings contain one more byte of data than the length */
914 /* slot indicates. */
916 vector = (struct vector *) where;
917 length = fixnum_value(vector->length) + 1;
918 nwords = CEILING(NWORDS(length, 8) + 2, 2);
920 return nwords;
922 static lispobj
923 trans_base_string(lispobj object)
925 struct vector *vector;
926 sword_t length, nwords;
928 gc_assert(is_lisp_pointer(object));
930 /* NOTE: A string contains one more byte of data (a terminating
931 * '\0' to help when interfacing with C functions) than indicated
932 * by the length slot. */
934 vector = (struct vector *) native_pointer(object);
935 length = fixnum_value(vector->length) + 1;
936 nwords = CEILING(NWORDS(length, 8) + 2, 2);
938 return copy_large_unboxed_object(object, nwords);
941 static sword_t
942 size_base_string(lispobj *where)
944 struct vector *vector;
945 sword_t length, nwords;
947 /* NOTE: A string contains one more byte of data (a terminating
948 * '\0' to help when interfacing with C functions) than indicated
949 * by the length slot. */
951 vector = (struct vector *) where;
952 length = fixnum_value(vector->length) + 1;
953 nwords = CEILING(NWORDS(length, 8) + 2, 2);
955 return nwords;
958 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
959 static sword_t
960 scav_character_string(lispobj *where, lispobj object)
962 struct vector *vector;
963 int length, nwords;
965 /* NOTE: Strings contain one more byte of data than the length */
966 /* slot indicates. */
968 vector = (struct vector *) where;
969 length = fixnum_value(vector->length) + 1;
970 nwords = CEILING(NWORDS(length, 32) + 2, 2);
972 return nwords;
974 static lispobj
975 trans_character_string(lispobj object)
977 struct vector *vector;
978 int length, nwords;
980 gc_assert(is_lisp_pointer(object));
982 /* NOTE: A string contains one more byte of data (a terminating
983 * '\0' to help when interfacing with C functions) than indicated
984 * by the length slot. */
986 vector = (struct vector *) native_pointer(object);
987 length = fixnum_value(vector->length) + 1;
988 nwords = CEILING(NWORDS(length, 32) + 2, 2);
990 return copy_large_unboxed_object(object, nwords);
993 static sword_t
994 size_character_string(lispobj *where)
996 struct vector *vector;
997 int length, nwords;
999 /* NOTE: A string contains one more byte of data (a terminating
1000 * '\0' to help when interfacing with C functions) than indicated
1001 * by the length slot. */
1003 vector = (struct vector *) where;
1004 length = fixnum_value(vector->length) + 1;
1005 nwords = CEILING(NWORDS(length, 32) + 2, 2);
1007 return nwords;
1009 #endif
1011 static lispobj
1012 trans_vector(lispobj object)
1014 struct vector *vector;
1015 sword_t length, nwords;
1017 gc_assert(is_lisp_pointer(object));
1019 vector = (struct vector *) native_pointer(object);
1021 length = fixnum_value(vector->length);
1022 nwords = CEILING(length + 2, 2);
1024 return copy_large_object(object, nwords);
1027 static sword_t
1028 size_vector(lispobj *where)
1030 struct vector *vector;
1031 sword_t length, nwords;
1033 vector = (struct vector *) where;
1034 length = fixnum_value(vector->length);
1035 nwords = CEILING(length + 2, 2);
1037 return nwords;
1040 static sword_t
1041 scav_vector_nil(lispobj *where, lispobj object)
1043 return 2;
1046 static lispobj
1047 trans_vector_nil(lispobj object)
1049 gc_assert(is_lisp_pointer(object));
1050 return copy_unboxed_object(object, 2);
1053 static sword_t
1054 size_vector_nil(lispobj *where)
1056 /* Just the header word and the length word */
1057 return 2;
1060 static sword_t
1061 scav_vector_bit(lispobj *where, lispobj object)
1063 struct vector *vector;
1064 sword_t length, nwords;
1066 vector = (struct vector *) where;
1067 length = fixnum_value(vector->length);
1068 nwords = CEILING(NWORDS(length, 1) + 2, 2);
1070 return nwords;
1073 static lispobj
1074 trans_vector_bit(lispobj object)
1076 struct vector *vector;
1077 sword_t length, nwords;
1079 gc_assert(is_lisp_pointer(object));
1081 vector = (struct vector *) native_pointer(object);
1082 length = fixnum_value(vector->length);
1083 nwords = CEILING(NWORDS(length, 1) + 2, 2);
1085 return copy_large_unboxed_object(object, nwords);
1088 static sword_t
1089 size_vector_bit(lispobj *where)
1091 struct vector *vector;
1092 sword_t length, nwords;
1094 vector = (struct vector *) where;
1095 length = fixnum_value(vector->length);
1096 nwords = CEILING(NWORDS(length, 1) + 2, 2);
1098 return nwords;
1101 static sword_t
1102 scav_vector_unsigned_byte_2(lispobj *where, lispobj object)
1104 struct vector *vector;
1105 sword_t length, nwords;
1107 vector = (struct vector *) where;
1108 length = fixnum_value(vector->length);
1109 nwords = CEILING(NWORDS(length, 2) + 2, 2);
1111 return nwords;
1114 static lispobj
1115 trans_vector_unsigned_byte_2(lispobj object)
1117 struct vector *vector;
1118 sword_t length, nwords;
1120 gc_assert(is_lisp_pointer(object));
1122 vector = (struct vector *) native_pointer(object);
1123 length = fixnum_value(vector->length);
1124 nwords = CEILING(NWORDS(length, 2) + 2, 2);
1126 return copy_large_unboxed_object(object, nwords);
1129 static sword_t
1130 size_vector_unsigned_byte_2(lispobj *where)
1132 struct vector *vector;
1133 sword_t length, nwords;
1135 vector = (struct vector *) where;
1136 length = fixnum_value(vector->length);
1137 nwords = CEILING(NWORDS(length, 2) + 2, 2);
1139 return nwords;
1142 static sword_t
1143 scav_vector_unsigned_byte_4(lispobj *where, lispobj object)
1145 struct vector *vector;
1146 sword_t length, nwords;
1148 vector = (struct vector *) where;
1149 length = fixnum_value(vector->length);
1150 nwords = CEILING(NWORDS(length, 4) + 2, 2);
1152 return nwords;
1155 static lispobj
1156 trans_vector_unsigned_byte_4(lispobj object)
1158 struct vector *vector;
1159 sword_t length, nwords;
1161 gc_assert(is_lisp_pointer(object));
1163 vector = (struct vector *) native_pointer(object);
1164 length = fixnum_value(vector->length);
1165 nwords = CEILING(NWORDS(length, 4) + 2, 2);
1167 return copy_large_unboxed_object(object, nwords);
1169 static sword_t
1170 size_vector_unsigned_byte_4(lispobj *where)
1172 struct vector *vector;
1173 sword_t length, nwords;
1175 vector = (struct vector *) where;
1176 length = fixnum_value(vector->length);
1177 nwords = CEILING(NWORDS(length, 4) + 2, 2);
1179 return nwords;
1183 static sword_t
1184 scav_vector_unsigned_byte_8(lispobj *where, lispobj object)
1186 struct vector *vector;
1187 sword_t length, nwords;
1189 vector = (struct vector *) where;
1190 length = fixnum_value(vector->length);
1191 nwords = CEILING(NWORDS(length, 8) + 2, 2);
1193 return nwords;
1196 /*********************/
1200 static lispobj
1201 trans_vector_unsigned_byte_8(lispobj object)
1203 struct vector *vector;
1204 sword_t length, nwords;
1206 gc_assert(is_lisp_pointer(object));
1208 vector = (struct vector *) native_pointer(object);
1209 length = fixnum_value(vector->length);
1210 nwords = CEILING(NWORDS(length, 8) + 2, 2);
1212 return copy_large_unboxed_object(object, nwords);
1215 static sword_t
1216 size_vector_unsigned_byte_8(lispobj *where)
1218 struct vector *vector;
1219 sword_t length, nwords;
1221 vector = (struct vector *) where;
1222 length = fixnum_value(vector->length);
1223 nwords = CEILING(NWORDS(length, 8) + 2, 2);
1225 return nwords;
1229 static sword_t
1230 scav_vector_unsigned_byte_16(lispobj *where, lispobj object)
1232 struct vector *vector;
1233 sword_t length, nwords;
1235 vector = (struct vector *) where;
1236 length = fixnum_value(vector->length);
1237 nwords = CEILING(NWORDS(length, 16) + 2, 2);
1239 return nwords;
1242 static lispobj
1243 trans_vector_unsigned_byte_16(lispobj object)
1245 struct vector *vector;
1246 sword_t length, nwords;
1248 gc_assert(is_lisp_pointer(object));
1250 vector = (struct vector *) native_pointer(object);
1251 length = fixnum_value(vector->length);
1252 nwords = CEILING(NWORDS(length, 16) + 2, 2);
1254 return copy_large_unboxed_object(object, nwords);
1257 static sword_t
1258 size_vector_unsigned_byte_16(lispobj *where)
1260 struct vector *vector;
1261 sword_t length, nwords;
1263 vector = (struct vector *) where;
1264 length = fixnum_value(vector->length);
1265 nwords = CEILING(NWORDS(length, 16) + 2, 2);
1267 return nwords;
1270 static sword_t
1271 scav_vector_unsigned_byte_32(lispobj *where, lispobj object)
1273 struct vector *vector;
1274 sword_t length, nwords;
1276 vector = (struct vector *) where;
1277 length = fixnum_value(vector->length);
1278 nwords = CEILING(NWORDS(length, 32) + 2, 2);
1280 return nwords;
1283 static lispobj
1284 trans_vector_unsigned_byte_32(lispobj object)
1286 struct vector *vector;
1287 sword_t length, nwords;
1289 gc_assert(is_lisp_pointer(object));
1291 vector = (struct vector *) native_pointer(object);
1292 length = fixnum_value(vector->length);
1293 nwords = CEILING(NWORDS(length, 32) + 2, 2);
1295 return copy_large_unboxed_object(object, nwords);
1298 static sword_t
1299 size_vector_unsigned_byte_32(lispobj *where)
1301 struct vector *vector;
1302 sword_t length, nwords;
1304 vector = (struct vector *) where;
1305 length = fixnum_value(vector->length);
1306 nwords = CEILING(NWORDS(length, 32) + 2, 2);
1308 return nwords;
1311 #if N_WORD_BITS == 64
1312 static sword_t
1313 scav_vector_unsigned_byte_64(lispobj *where, lispobj object)
1315 struct vector *vector;
1316 sword_t length, nwords;
1318 vector = (struct vector *) where;
1319 length = fixnum_value(vector->length);
1320 nwords = CEILING(NWORDS(length, 64) + 2, 2);
1322 return nwords;
1325 static lispobj
1326 trans_vector_unsigned_byte_64(lispobj object)
1328 struct vector *vector;
1329 sword_t length, nwords;
1331 gc_assert(is_lisp_pointer(object));
1333 vector = (struct vector *) native_pointer(object);
1334 length = fixnum_value(vector->length);
1335 nwords = CEILING(NWORDS(length, 64) + 2, 2);
1337 return copy_large_unboxed_object(object, nwords);
1340 static sword_t
1341 size_vector_unsigned_byte_64(lispobj *where)
1343 struct vector *vector;
1344 sword_t length, nwords;
1346 vector = (struct vector *) where;
1347 length = fixnum_value(vector->length);
1348 nwords = CEILING(NWORDS(length, 64) + 2, 2);
1350 return nwords;
1352 #endif
1354 static sword_t
1355 scav_vector_single_float(lispobj *where, lispobj object)
1357 struct vector *vector;
1358 sword_t length, nwords;
1360 vector = (struct vector *) where;
1361 length = fixnum_value(vector->length);
1362 nwords = CEILING(NWORDS(length, 32) + 2, 2);
1364 return nwords;
1367 static lispobj
1368 trans_vector_single_float(lispobj object)
1370 struct vector *vector;
1371 sword_t length, nwords;
1373 gc_assert(is_lisp_pointer(object));
1375 vector = (struct vector *) native_pointer(object);
1376 length = fixnum_value(vector->length);
1377 nwords = CEILING(NWORDS(length, 32) + 2, 2);
1379 return copy_large_unboxed_object(object, nwords);
1382 static sword_t
1383 size_vector_single_float(lispobj *where)
1385 struct vector *vector;
1386 sword_t length, nwords;
1388 vector = (struct vector *) where;
1389 length = fixnum_value(vector->length);
1390 nwords = CEILING(NWORDS(length, 32) + 2, 2);
1392 return nwords;
1395 static sword_t
1396 scav_vector_double_float(lispobj *where, lispobj object)
1398 struct vector *vector;
1399 sword_t length, nwords;
1401 vector = (struct vector *) where;
1402 length = fixnum_value(vector->length);
1403 nwords = CEILING(NWORDS(length, 64) + 2, 2);
1405 return nwords;
1408 static lispobj
1409 trans_vector_double_float(lispobj object)
1411 struct vector *vector;
1412 sword_t length, nwords;
1414 gc_assert(is_lisp_pointer(object));
1416 vector = (struct vector *) native_pointer(object);
1417 length = fixnum_value(vector->length);
1418 nwords = CEILING(NWORDS(length, 64) + 2, 2);
1420 return copy_large_unboxed_object(object, nwords);
1423 static sword_t
1424 size_vector_double_float(lispobj *where)
1426 struct vector *vector;
1427 sword_t length, nwords;
1429 vector = (struct vector *) where;
1430 length = fixnum_value(vector->length);
1431 nwords = CEILING(NWORDS(length, 64) + 2, 2);
1433 return nwords;
1436 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
1437 static long
1438 scav_vector_long_float(lispobj *where, lispobj object)
1440 struct vector *vector;
1441 long length, nwords;
1443 vector = (struct vector *) where;
1444 length = fixnum_value(vector->length);
1445 nwords = CEILING(length *
1446 LONG_FLOAT_SIZE
1447 + 2, 2);
1448 return nwords;
1451 static lispobj
1452 trans_vector_long_float(lispobj object)
1454 struct vector *vector;
1455 long length, nwords;
1457 gc_assert(is_lisp_pointer(object));
1459 vector = (struct vector *) native_pointer(object);
1460 length = fixnum_value(vector->length);
1461 nwords = CEILING(length * LONG_FLOAT_SIZE + 2, 2);
1463 return copy_large_unboxed_object(object, nwords);
1466 static long
1467 size_vector_long_float(lispobj *where)
1469 struct vector *vector;
1470 sword_t length, nwords;
1472 vector = (struct vector *) where;
1473 length = fixnum_value(vector->length);
1474 nwords = CEILING(length * LONG_FLOAT_SIZE + 2, 2);
1476 return nwords;
1478 #endif
1481 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
1482 static sword_t
1483 scav_vector_complex_single_float(lispobj *where, lispobj object)
1485 struct vector *vector;
1486 sword_t length, nwords;
1488 vector = (struct vector *) where;
1489 length = fixnum_value(vector->length);
1490 nwords = CEILING(NWORDS(length, 64) + 2, 2);
1492 return nwords;
1495 static lispobj
1496 trans_vector_complex_single_float(lispobj object)
1498 struct vector *vector;
1499 sword_t length, nwords;
1501 gc_assert(is_lisp_pointer(object));
1503 vector = (struct vector *) native_pointer(object);
1504 length = fixnum_value(vector->length);
1505 nwords = CEILING(NWORDS(length, 64) + 2, 2);
1507 return copy_large_unboxed_object(object, nwords);
1510 static sword_t
1511 size_vector_complex_single_float(lispobj *where)
1513 struct vector *vector;
1514 sword_t length, nwords;
1516 vector = (struct vector *) where;
1517 length = fixnum_value(vector->length);
1518 nwords = CEILING(NWORDS(length, 64) + 2, 2);
1520 return nwords;
1522 #endif
1524 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
1525 static sword_t
1526 scav_vector_complex_double_float(lispobj *where, lispobj object)
1528 struct vector *vector;
1529 sword_t length, nwords;
1531 vector = (struct vector *) where;
1532 length = fixnum_value(vector->length);
1533 nwords = CEILING(NWORDS(length, 128) + 2, 2);
1535 return nwords;
1538 static lispobj
1539 trans_vector_complex_double_float(lispobj object)
1541 struct vector *vector;
1542 sword_t length, nwords;
1544 gc_assert(is_lisp_pointer(object));
1546 vector = (struct vector *) native_pointer(object);
1547 length = fixnum_value(vector->length);
1548 nwords = CEILING(NWORDS(length, 128) + 2, 2);
1550 return copy_large_unboxed_object(object, nwords);
1553 static sword_t
1554 size_vector_complex_double_float(lispobj *where)
1556 struct vector *vector;
1557 sword_t length, nwords;
1559 vector = (struct vector *) where;
1560 length = fixnum_value(vector->length);
1561 nwords = CEILING(NWORDS(length, 128) + 2, 2);
1563 return nwords;
1565 #endif
1568 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
1569 static long
1570 scav_vector_complex_long_float(lispobj *where, lispobj object)
1572 struct vector *vector;
1573 sword_t length, nwords;
1575 vector = (struct vector *) where;
1576 length = fixnum_value(vector->length);
1577 nwords = CEILING(length * (2* LONG_FLOAT_SIZE) + 2, 2);
1579 return nwords;
1582 static lispobj
1583 trans_vector_complex_long_float(lispobj object)
1585 struct vector *vector;
1586 long length, nwords;
1588 gc_assert(is_lisp_pointer(object));
1590 vector = (struct vector *) native_pointer(object);
1591 length = fixnum_value(vector->length);
1592 nwords = CEILING(length * (2*LONG_FLOAT_SIZE) + 2, 2);
1594 return copy_large_unboxed_object(object, nwords);
1597 static long
1598 size_vector_complex_long_float(lispobj *where)
1600 struct vector *vector;
1601 long length, nwords;
1603 vector = (struct vector *) where;
1604 length = fixnum_value(vector->length);
1605 nwords = CEILING(length * (2*LONG_FLOAT_SIZE) + 2, 2);
1607 return nwords;
1609 #endif
1611 #define WEAK_POINTER_NWORDS \
1612 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
1614 static lispobj
1615 trans_weak_pointer(lispobj object)
1617 lispobj copy;
1618 #ifndef LISP_FEATURE_GENCGC
1619 struct weak_pointer *wp;
1620 #endif
1621 gc_assert(is_lisp_pointer(object));
1623 #if defined(DEBUG_WEAK)
1624 printf("Transporting weak pointer from 0x%08x\n", object);
1625 #endif
1627 /* Need to remember where all the weak pointers are that have */
1628 /* been transported so they can be fixed up in a post-GC pass. */
1630 copy = copy_object(object, WEAK_POINTER_NWORDS);
1631 #ifndef LISP_FEATURE_GENCGC
1632 wp = (struct weak_pointer *) native_pointer(copy);
1634 gc_assert(widetag_of(wp->header)==WEAK_POINTER_WIDETAG);
1635 /* Push the weak pointer onto the list of weak pointers. */
1636 wp->next = (struct weak_pointer *)LOW_WORD(weak_pointers);
1637 weak_pointers = wp;
1638 #endif
1639 return copy;
1642 static sword_t
1643 size_weak_pointer(lispobj *where)
1645 return WEAK_POINTER_NWORDS;
1649 void scan_weak_pointers(void)
1651 struct weak_pointer *wp, *next_wp;
1652 for (wp = weak_pointers, next_wp = NULL; wp != NULL; wp = next_wp) {
1653 lispobj value = wp->value;
1654 lispobj *first_pointer;
1655 gc_assert(widetag_of(wp->header)==WEAK_POINTER_WIDETAG);
1657 next_wp = wp->next;
1658 wp->next = NULL;
1659 if (next_wp == wp) /* gencgc uses a ref to self for end of list */
1660 next_wp = NULL;
1662 if (!is_lisp_pointer(value))
1663 continue;
1665 /* Now, we need to check whether the object has been forwarded. If
1666 * it has been, the weak pointer is still good and needs to be
1667 * updated. Otherwise, the weak pointer needs to be nil'ed
1668 * out. */
1670 if (from_space_p(value)) {
1671 first_pointer = (lispobj *)native_pointer(value);
1673 if (forwarding_pointer_p(first_pointer)) {
1674 wp->value=
1675 (lispobj)LOW_WORD(forwarding_pointer_value(first_pointer));
1676 } else {
1677 /* Break it. */
1678 wp->value = NIL;
1679 wp->broken = T;
1682 #ifdef LISP_FEATURE_IMMOBILE_SPACE
1683 else if (immobile_space_p(value) &&
1684 immobile_obj_gen_bits(native_pointer(value)) == from_space) {
1685 wp->value = NIL;
1686 wp->broken = T;
1688 #endif
1693 /* Hash tables */
1695 #if N_WORD_BITS == 32
1696 #define EQ_HASH_MASK 0x1fffffff
1697 #elif N_WORD_BITS == 64
1698 #define EQ_HASH_MASK 0x1fffffffffffffff
1699 #endif
1701 /* Compute the EQ-hash of KEY. This must match POINTER-HASH in
1702 * target-hash-table.lisp. */
1703 #define EQ_HASH(key) ((key) & EQ_HASH_MASK)
1705 /* List of weak hash tables chained through their NEXT-WEAK-HASH-TABLE
1706 * slot. Set to NULL at the end of a collection.
1708 * This is not optimal because, when a table is tenured, it won't be
1709 * processed automatically; only the yougest generation is GC'd by
1710 * default. On the other hand, all applications will need an
1711 * occasional full GC anyway, so it's not that bad either. */
1712 struct hash_table *weak_hash_tables = NULL;
1714 /* Return true if OBJ has already survived the current GC. */
1715 static inline int
1716 survived_gc_yet (lispobj obj)
1718 #ifdef LISP_FEATURE_IMMOBILE_SPACE
1719 /* If an immobile object's generation# is that of 'from_space', but has been
1720 visited (i.e. is live), then it is conceptually not in 'from_space'.
1721 This can happen when and only when _not_ raising the generation number.
1722 Since the gen_bits() accessor returns the visited bit, the byte value
1723 is numerically unequal to 'from_space', which is what we want */
1724 return !is_lisp_pointer(obj)
1725 || (immobile_space_p(obj)
1726 ? immobile_obj_gen_bits(native_pointer(obj)) != from_space
1727 : (!from_space_p(obj) || forwarding_pointer_p(native_pointer(obj))));
1728 #else
1729 return (!is_lisp_pointer(obj) || !from_space_p(obj) ||
1730 forwarding_pointer_p(native_pointer(obj)));
1731 #endif
1734 static inline int
1735 weak_hash_entry_alivep (lispobj weakness, lispobj key, lispobj value)
1737 switch (weakness) {
1738 case KEY:
1739 return survived_gc_yet(key);
1740 case VALUE:
1741 return survived_gc_yet(value);
1742 case KEY_OR_VALUE:
1743 return (survived_gc_yet(key) || survived_gc_yet(value));
1744 case KEY_AND_VALUE:
1745 return (survived_gc_yet(key) && survived_gc_yet(value));
1746 default:
1747 gc_assert(0);
1748 /* Shut compiler up. */
1749 return 0;
1753 /* Return the beginning of data in ARRAY (skipping the header and the
1754 * length) or NULL if it isn't an array of the specified widetag after
1755 * all. */
1756 static inline lispobj *
1757 get_array_data (lispobj array, int widetag, uword_t *length)
1759 if (is_lisp_pointer(array) &&
1760 (widetag_of(*(lispobj *)native_pointer(array)) == widetag)) {
1761 if (length != NULL)
1762 *length = fixnum_value(((lispobj *)native_pointer(array))[1]);
1763 return ((lispobj *)native_pointer(array)) + 2;
1764 } else {
1765 return NULL;
1769 /* Only need to worry about scavenging the _real_ entries in the
1770 * table. Phantom entries such as the hash table itself at index 0 and
1771 * the empty marker at index 1 were scavenged by scav_vector that
1772 * either called this function directly or arranged for it to be
1773 * called later by pushing the hash table onto weak_hash_tables. */
1774 static void
1775 scav_hash_table_entries (struct hash_table *hash_table)
1777 lispobj *kv_vector;
1778 uword_t kv_length;
1779 lispobj *index_vector;
1780 uword_t length;
1781 lispobj *next_vector;
1782 uword_t next_vector_length;
1783 lispobj *hash_vector;
1784 uword_t hash_vector_length;
1785 lispobj empty_symbol;
1786 lispobj weakness = hash_table->weakness;
1787 uword_t i;
1789 kv_vector = get_array_data(hash_table->table,
1790 SIMPLE_VECTOR_WIDETAG, &kv_length);
1791 if (kv_vector == NULL)
1792 lose("invalid kv_vector %x\n", hash_table->table);
1794 index_vector = get_array_data(hash_table->index_vector,
1795 SIMPLE_ARRAY_WORD_WIDETAG, &length);
1796 if (index_vector == NULL)
1797 lose("invalid index_vector %x\n", hash_table->index_vector);
1799 next_vector = get_array_data(hash_table->next_vector,
1800 SIMPLE_ARRAY_WORD_WIDETAG,
1801 &next_vector_length);
1802 if (next_vector == NULL)
1803 lose("invalid next_vector %x\n", hash_table->next_vector);
1805 hash_vector = get_array_data(hash_table->hash_vector,
1806 SIMPLE_ARRAY_WORD_WIDETAG,
1807 &hash_vector_length);
1808 if (hash_vector != NULL)
1809 gc_assert(hash_vector_length == next_vector_length);
1811 /* These lengths could be different as the index_vector can be a
1812 * different length from the others, a larger index_vector could
1813 * help reduce collisions. */
1814 gc_assert(next_vector_length*2 == kv_length);
1816 empty_symbol = kv_vector[1];
1817 /* fprintf(stderr,"* empty_symbol = %x\n", empty_symbol);*/
1818 if (widetag_of(*(lispobj *)native_pointer(empty_symbol)) !=
1819 SYMBOL_HEADER_WIDETAG) {
1820 lose("not a symbol where empty-hash-table-slot symbol expected: %x\n",
1821 *(lispobj *)native_pointer(empty_symbol));
1824 /* Work through the KV vector. */
1825 for (i = 1; i < next_vector_length; i++) {
1826 lispobj old_key = kv_vector[2*i];
1827 lispobj value = kv_vector[2*i+1];
1828 if ((weakness == NIL) ||
1829 weak_hash_entry_alivep(weakness, old_key, value)) {
1831 /* Scavenge the key and value. */
1832 scavenge(&kv_vector[2*i],2);
1834 /* If an EQ-based key has moved, mark the hash-table for
1835 * rehashing. */
1836 if (!hash_vector || hash_vector[i] == MAGIC_HASH_VECTOR_VALUE) {
1837 lispobj new_key = kv_vector[2*i];
1838 // FIXME: many EQ-based sxhash values are insensitive
1839 // to object movement. The most important one is SYMBOL,
1840 // but others also carry around a hash value: LAYOUT, CLASSOID,
1841 // and STANDARD-[FUNCALLABLE-]INSTANCE.
1842 // If old_key is any of those, don't set needs_rehash_p.
1843 if (old_key != new_key && new_key != empty_symbol) {
1844 hash_table->needs_rehash_p = T;
1851 sword_t
1852 scav_vector (lispobj *where, lispobj object)
1854 uword_t kv_length;
1855 struct hash_table *hash_table;
1857 /* SB-VM:VECTOR-VALID-HASHING-SUBTYPE is set for EQ-based and weak
1858 * hash tables in the Lisp HASH-TABLE code to indicate need for
1859 * special GC support. */
1860 if ((HeaderValue(object) & 0xFF) == subtype_VectorNormal)
1861 return 1;
1863 kv_length = fixnum_value(where[1]);
1864 /*FSHOW((stderr,"/kv_length = %d\n", kv_length));*/
1866 /* Scavenge element 0, which may be a hash-table structure. */
1867 scavenge(where+2, 1);
1868 if (!is_lisp_pointer(where[2])) {
1869 /* This'll happen when REHASH clears the header of old-kv-vector
1870 * and fills it with zero, but some other thread simulatenously
1871 * sets the header in %%PUTHASH.
1873 fprintf(stderr,
1874 "Warning: no pointer at %p in hash table: this indicates "
1875 "non-fatal corruption caused by concurrent access to a "
1876 "hash-table from multiple threads. Any accesses to "
1877 "hash-tables shared between threads should be protected "
1878 "by locks.\n", (void*)&where[2]);
1879 // We've scavenged three words.
1880 return 3;
1882 hash_table = (struct hash_table *)native_pointer(where[2]);
1883 /*FSHOW((stderr,"/hash_table = %x\n", hash_table));*/
1884 if (widetag_of(hash_table->header) != INSTANCE_HEADER_WIDETAG) {
1885 lose("hash table not instance (%x at %x)\n",
1886 hash_table->header,
1887 hash_table);
1890 /* Scavenge element 1, which should be some internal symbol that
1891 * the hash table code reserves for marking empty slots. */
1892 scavenge(where+3, 1);
1893 if (!is_lisp_pointer(where[3])) {
1894 lose("not empty-hash-table-slot symbol pointer: %x\n", where[3]);
1897 /* Scavenge hash table, which will fix the positions of the other
1898 * needed objects. */
1899 scavenge((lispobj *)hash_table,
1900 CEILING(sizeof(struct hash_table) / sizeof(lispobj), 2));
1902 /* Cross-check the kv_vector. */
1903 if (where != (lispobj *)native_pointer(hash_table->table)) {
1904 lose("hash_table table!=this table %x\n", hash_table->table);
1907 if (hash_table->weakness == NIL) {
1908 scav_hash_table_entries(hash_table);
1909 } else {
1910 /* Delay scavenging of this table by pushing it onto
1911 * weak_hash_tables (if it's not there already) for the weak
1912 * object phase. */
1913 if (hash_table->next_weak_hash_table == NIL) {
1914 hash_table->next_weak_hash_table = (lispobj)weak_hash_tables;
1915 weak_hash_tables = hash_table;
1919 return (CEILING(kv_length + 2, 2));
1922 void
1923 scav_weak_hash_tables (void)
1925 struct hash_table *table;
1927 /* Scavenge entries whose triggers are known to survive. */
1928 for (table = weak_hash_tables; table != NULL;
1929 table = (struct hash_table *)table->next_weak_hash_table) {
1930 scav_hash_table_entries(table);
1934 /* Walk through the chain whose first element is *FIRST and remove
1935 * dead weak entries. */
1936 static inline void
1937 scan_weak_hash_table_chain (struct hash_table *hash_table, lispobj *prev,
1938 lispobj *kv_vector, lispobj *index_vector,
1939 lispobj *next_vector, lispobj *hash_vector,
1940 lispobj empty_symbol, lispobj weakness)
1942 unsigned index = *prev;
1943 while (index) {
1944 unsigned next = next_vector[index];
1945 lispobj key = kv_vector[2 * index];
1946 lispobj value = kv_vector[2 * index + 1];
1947 gc_assert(key != empty_symbol);
1948 gc_assert(value != empty_symbol);
1949 if (!weak_hash_entry_alivep(weakness, key, value)) {
1950 unsigned count = fixnum_value(hash_table->number_entries);
1951 gc_assert(count > 0);
1952 *prev = next;
1953 hash_table->number_entries = make_fixnum(count - 1);
1954 next_vector[index] = fixnum_value(hash_table->next_free_kv);
1955 hash_table->next_free_kv = make_fixnum(index);
1956 kv_vector[2 * index] = empty_symbol;
1957 kv_vector[2 * index + 1] = empty_symbol;
1958 if (hash_vector)
1959 hash_vector[index] = MAGIC_HASH_VECTOR_VALUE;
1960 } else {
1961 prev = &next_vector[index];
1963 index = next;
1967 static void
1968 scan_weak_hash_table (struct hash_table *hash_table)
1970 lispobj *kv_vector;
1971 lispobj *index_vector;
1972 uword_t length = 0; /* prevent warning */
1973 lispobj *next_vector;
1974 uword_t next_vector_length = 0; /* prevent warning */
1975 lispobj *hash_vector;
1976 lispobj empty_symbol;
1977 lispobj weakness = hash_table->weakness;
1978 uword_t i;
1980 kv_vector = get_array_data(hash_table->table,
1981 SIMPLE_VECTOR_WIDETAG, NULL);
1982 index_vector = get_array_data(hash_table->index_vector,
1983 SIMPLE_ARRAY_WORD_WIDETAG, &length);
1984 next_vector = get_array_data(hash_table->next_vector,
1985 SIMPLE_ARRAY_WORD_WIDETAG,
1986 &next_vector_length);
1987 hash_vector = get_array_data(hash_table->hash_vector,
1988 SIMPLE_ARRAY_WORD_WIDETAG, NULL);
1989 empty_symbol = kv_vector[1];
1991 for (i = 0; i < length; i++) {
1992 scan_weak_hash_table_chain(hash_table, &index_vector[i],
1993 kv_vector, index_vector, next_vector,
1994 hash_vector, empty_symbol, weakness);
1998 /* Remove dead entries from weak hash tables. */
1999 void
2000 scan_weak_hash_tables (void)
2002 struct hash_table *table, *next;
2004 for (table = weak_hash_tables; table != NULL; table = next) {
2005 next = (struct hash_table *)table->next_weak_hash_table;
2006 table->next_weak_hash_table = NIL;
2007 scan_weak_hash_table(table);
2010 weak_hash_tables = NULL;
2015 * initialization
2018 static sword_t
2019 scav_lose(lispobj *where, lispobj object)
2021 lose("no scavenge function for object %p (widetag 0x%x)\n",
2022 (uword_t)object,
2023 widetag_of(*where));
2025 return 0; /* bogus return value to satisfy static type checking */
2028 static lispobj
2029 trans_lose(lispobj object)
2031 lose("no transport function for object %p (widetag 0x%x)\n",
2032 (void*)object,
2033 widetag_of(*(lispobj*)native_pointer(object)));
2034 return NIL; /* bogus return value to satisfy static type checking */
2037 static sword_t
2038 size_lose(lispobj *where)
2040 lose("no size function for object at %p (widetag 0x%x)\n",
2041 (void*)where,
2042 widetag_of(*where));
2043 return 1; /* bogus return value to satisfy static type checking */
2048 * initialization
2051 void
2052 gc_init_tables(void)
2054 uword_t i, j;
2056 /* Set default value in all slots of scavenge table. FIXME
2057 * replace this gnarly sizeof with something based on
2058 * N_WIDETAG_BITS */
2059 for (i = 0; i < ((sizeof scavtab)/(sizeof scavtab[0])); i++) {
2060 scavtab[i] = scav_lose;
2063 /* For each type which can be selected by the lowtag alone, set
2064 * multiple entries in our widetag scavenge table (one for each
2065 * possible value of the high bits).
2068 for (i = 0; i < (1<<(N_WIDETAG_BITS-N_LOWTAG_BITS)); i++) {
2069 for (j = 0; j < (1<<N_LOWTAG_BITS); j++) {
2070 if (fixnump(j)) {
2071 scavtab[j|(i<<N_LOWTAG_BITS)] = scav_immediate;
2074 scavtab[FUN_POINTER_LOWTAG|(i<<N_LOWTAG_BITS)] = scav_fun_pointer;
2075 /* skipping OTHER_IMMEDIATE_0_LOWTAG */
2076 scavtab[LIST_POINTER_LOWTAG|(i<<N_LOWTAG_BITS)] = scav_list_pointer;
2077 scavtab[INSTANCE_POINTER_LOWTAG|(i<<N_LOWTAG_BITS)] =
2078 scav_instance_pointer;
2079 /* skipping OTHER_IMMEDIATE_1_LOWTAG */
2080 scavtab[OTHER_POINTER_LOWTAG|(i<<N_LOWTAG_BITS)] = scav_other_pointer;
2083 /* Other-pointer types (those selected by all eight bits of the
2084 * tag) get one entry each in the scavenge table. */
2085 scavtab[BIGNUM_WIDETAG] = scav_unboxed;
2086 scavtab[RATIO_WIDETAG] = scav_boxed;
2087 #if N_WORD_BITS == 64
2088 scavtab[SINGLE_FLOAT_WIDETAG] = scav_immediate;
2089 #else
2090 scavtab[SINGLE_FLOAT_WIDETAG] = scav_unboxed;
2091 #endif
2092 scavtab[DOUBLE_FLOAT_WIDETAG] = scav_unboxed;
2093 #ifdef LONG_FLOAT_WIDETAG
2094 scavtab[LONG_FLOAT_WIDETAG] = scav_unboxed;
2095 #endif
2096 scavtab[COMPLEX_WIDETAG] = scav_boxed;
2097 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
2098 scavtab[COMPLEX_SINGLE_FLOAT_WIDETAG] = scav_unboxed;
2099 #endif
2100 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
2101 scavtab[COMPLEX_DOUBLE_FLOAT_WIDETAG] = scav_unboxed;
2102 #endif
2103 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
2104 scavtab[COMPLEX_LONG_FLOAT_WIDETAG] = scav_unboxed;
2105 #endif
2106 #ifdef SIMD_PACK_WIDETAG
2107 scavtab[SIMD_PACK_WIDETAG] = scav_unboxed;
2108 #endif
2109 scavtab[SIMPLE_ARRAY_WIDETAG] = scav_boxed;
2110 scavtab[SIMPLE_BASE_STRING_WIDETAG] = scav_base_string;
2111 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2112 scavtab[SIMPLE_CHARACTER_STRING_WIDETAG] = scav_character_string;
2113 #endif
2114 scavtab[SIMPLE_BIT_VECTOR_WIDETAG] = scav_vector_bit;
2115 scavtab[SIMPLE_ARRAY_NIL_WIDETAG] = scav_vector_nil;
2116 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG] =
2117 scav_vector_unsigned_byte_2;
2118 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG] =
2119 scav_vector_unsigned_byte_4;
2120 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG] =
2121 scav_vector_unsigned_byte_8;
2122 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG] =
2123 scav_vector_unsigned_byte_8;
2124 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG] =
2125 scav_vector_unsigned_byte_16;
2126 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG] =
2127 scav_vector_unsigned_byte_16;
2128 #if (N_WORD_BITS == 32)
2129 scavtab[SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG] =
2130 scav_vector_unsigned_byte_32;
2131 #endif
2132 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG] =
2133 scav_vector_unsigned_byte_32;
2134 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG] =
2135 scav_vector_unsigned_byte_32;
2136 #if (N_WORD_BITS == 64)
2137 scavtab[SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG] =
2138 scav_vector_unsigned_byte_64;
2139 #endif
2140 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2141 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG] =
2142 scav_vector_unsigned_byte_64;
2143 #endif
2144 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2145 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG] =
2146 scav_vector_unsigned_byte_64;
2147 #endif
2148 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2149 scavtab[SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG] = scav_vector_unsigned_byte_8;
2150 #endif
2151 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2152 scavtab[SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG] =
2153 scav_vector_unsigned_byte_16;
2154 #endif
2155 #if (N_WORD_BITS == 32)
2156 scavtab[SIMPLE_ARRAY_FIXNUM_WIDETAG] =
2157 scav_vector_unsigned_byte_32;
2158 #endif
2159 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2160 scavtab[SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG] =
2161 scav_vector_unsigned_byte_32;
2162 #endif
2163 #if (N_WORD_BITS == 64)
2164 scavtab[SIMPLE_ARRAY_FIXNUM_WIDETAG] =
2165 scav_vector_unsigned_byte_64;
2166 #endif
2167 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2168 scavtab[SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG] =
2169 scav_vector_unsigned_byte_64;
2170 #endif
2171 scavtab[SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG] = scav_vector_single_float;
2172 scavtab[SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG] = scav_vector_double_float;
2173 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2174 scavtab[SIMPLE_ARRAY_LONG_FLOAT_WIDETAG] = scav_vector_long_float;
2175 #endif
2176 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2177 scavtab[SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG] =
2178 scav_vector_complex_single_float;
2179 #endif
2180 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2181 scavtab[SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG] =
2182 scav_vector_complex_double_float;
2183 #endif
2184 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2185 scavtab[SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG] =
2186 scav_vector_complex_long_float;
2187 #endif
2188 scavtab[COMPLEX_BASE_STRING_WIDETAG] = scav_boxed;
2189 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
2190 scavtab[COMPLEX_CHARACTER_STRING_WIDETAG] = scav_boxed;
2191 #endif
2192 scavtab[COMPLEX_VECTOR_NIL_WIDETAG] = scav_boxed;
2193 scavtab[COMPLEX_BIT_VECTOR_WIDETAG] = scav_boxed;
2194 scavtab[COMPLEX_VECTOR_WIDETAG] = scav_boxed;
2195 scavtab[COMPLEX_ARRAY_WIDETAG] = scav_boxed;
2196 scavtab[CODE_HEADER_WIDETAG] = scav_code_header;
2197 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
2198 scavtab[SIMPLE_FUN_HEADER_WIDETAG] = scav_fun_header;
2199 scavtab[RETURN_PC_HEADER_WIDETAG] = scav_return_pc_header;
2200 #endif
2201 scavtab[FUNCALLABLE_INSTANCE_HEADER_WIDETAG] = scav_boxed;
2202 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2203 scavtab[CLOSURE_HEADER_WIDETAG] = scav_closure_header;
2204 #else
2205 scavtab[CLOSURE_HEADER_WIDETAG] = scav_boxed;
2206 #endif
2207 scavtab[VALUE_CELL_HEADER_WIDETAG] = scav_boxed;
2208 scavtab[SYMBOL_HEADER_WIDETAG] = scav_boxed;
2209 scavtab[CHARACTER_WIDETAG] = scav_immediate;
2210 scavtab[SAP_WIDETAG] = scav_unboxed;
2211 scavtab[UNBOUND_MARKER_WIDETAG] = scav_immediate;
2212 scavtab[NO_TLS_VALUE_MARKER_WIDETAG] = scav_immediate;
2213 scavtab[INSTANCE_HEADER_WIDETAG] = scav_instance;
2214 #if defined(LISP_FEATURE_SPARC) || defined(LISP_FEATURE_ARM)
2215 scavtab[FDEFN_WIDETAG] = scav_boxed;
2216 #else
2217 scavtab[FDEFN_WIDETAG] = scav_fdefn;
2218 #endif
2219 scavtab[SIMPLE_VECTOR_WIDETAG] = scav_vector;
2221 /* transport other table, initialized same way as scavtab */
2222 for (i = 0; i < ((sizeof transother)/(sizeof transother[0])); i++)
2223 transother[i] = trans_lose;
2224 transother[BIGNUM_WIDETAG] = trans_unboxed;
2225 transother[RATIO_WIDETAG] = trans_boxed;
2227 #if N_WORD_BITS == 64
2228 transother[SINGLE_FLOAT_WIDETAG] = trans_immediate;
2229 #else
2230 transother[SINGLE_FLOAT_WIDETAG] = trans_unboxed;
2231 #endif
2232 transother[DOUBLE_FLOAT_WIDETAG] = trans_unboxed;
2233 #ifdef LONG_FLOAT_WIDETAG
2234 transother[LONG_FLOAT_WIDETAG] = trans_unboxed;
2235 #endif
2236 transother[COMPLEX_WIDETAG] = trans_boxed;
2237 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
2238 transother[COMPLEX_SINGLE_FLOAT_WIDETAG] = trans_unboxed;
2239 #endif
2240 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
2241 transother[COMPLEX_DOUBLE_FLOAT_WIDETAG] = trans_unboxed;
2242 #endif
2243 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
2244 transother[COMPLEX_LONG_FLOAT_WIDETAG] = trans_unboxed;
2245 #endif
2246 transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed; /* but not GENCGC */
2247 transother[SIMPLE_BASE_STRING_WIDETAG] = trans_base_string;
2248 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2249 transother[SIMPLE_CHARACTER_STRING_WIDETAG] = trans_character_string;
2250 #endif
2251 transother[SIMPLE_BIT_VECTOR_WIDETAG] = trans_vector_bit;
2252 transother[SIMPLE_VECTOR_WIDETAG] = trans_vector;
2253 transother[SIMPLE_ARRAY_NIL_WIDETAG] = trans_vector_nil;
2254 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG] =
2255 trans_vector_unsigned_byte_2;
2256 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG] =
2257 trans_vector_unsigned_byte_4;
2258 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG] =
2259 trans_vector_unsigned_byte_8;
2260 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG] =
2261 trans_vector_unsigned_byte_8;
2262 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG] =
2263 trans_vector_unsigned_byte_16;
2264 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG] =
2265 trans_vector_unsigned_byte_16;
2266 #if (N_WORD_BITS == 32)
2267 transother[SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG] =
2268 trans_vector_unsigned_byte_32;
2269 #endif
2270 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG] =
2271 trans_vector_unsigned_byte_32;
2272 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG] =
2273 trans_vector_unsigned_byte_32;
2274 #if (N_WORD_BITS == 64)
2275 transother[SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG] =
2276 trans_vector_unsigned_byte_64;
2277 #endif
2278 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2279 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG] =
2280 trans_vector_unsigned_byte_64;
2281 #endif
2282 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2283 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG] =
2284 trans_vector_unsigned_byte_64;
2285 #endif
2286 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2287 transother[SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG] =
2288 trans_vector_unsigned_byte_8;
2289 #endif
2290 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2291 transother[SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG] =
2292 trans_vector_unsigned_byte_16;
2293 #endif
2294 #if (N_WORD_BITS == 32)
2295 transother[SIMPLE_ARRAY_FIXNUM_WIDETAG] =
2296 trans_vector_unsigned_byte_32;
2297 #endif
2298 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2299 transother[SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG] =
2300 trans_vector_unsigned_byte_32;
2301 #endif
2302 #if (N_WORD_BITS == 64)
2303 transother[SIMPLE_ARRAY_FIXNUM_WIDETAG] =
2304 trans_vector_unsigned_byte_64;
2305 #endif
2306 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2307 transother[SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG] =
2308 trans_vector_unsigned_byte_64;
2309 #endif
2310 transother[SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG] =
2311 trans_vector_single_float;
2312 transother[SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG] =
2313 trans_vector_double_float;
2314 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2315 transother[SIMPLE_ARRAY_LONG_FLOAT_WIDETAG] =
2316 trans_vector_long_float;
2317 #endif
2318 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2319 transother[SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG] =
2320 trans_vector_complex_single_float;
2321 #endif
2322 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2323 transother[SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG] =
2324 trans_vector_complex_double_float;
2325 #endif
2326 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2327 transother[SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG] =
2328 trans_vector_complex_long_float;
2329 #endif
2330 transother[COMPLEX_BASE_STRING_WIDETAG] = trans_boxed;
2331 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
2332 transother[COMPLEX_CHARACTER_STRING_WIDETAG] = trans_boxed;
2333 #endif
2334 transother[COMPLEX_BIT_VECTOR_WIDETAG] = trans_boxed;
2335 transother[COMPLEX_VECTOR_NIL_WIDETAG] = trans_boxed;
2336 transother[COMPLEX_VECTOR_WIDETAG] = trans_boxed;
2337 transother[COMPLEX_ARRAY_WIDETAG] = trans_boxed;
2338 transother[CODE_HEADER_WIDETAG] = trans_code_header;
2339 transother[SIMPLE_FUN_HEADER_WIDETAG] = trans_fun_header;
2340 transother[RETURN_PC_HEADER_WIDETAG] = trans_return_pc_header;
2341 transother[CLOSURE_HEADER_WIDETAG] = trans_boxed;
2342 transother[FUNCALLABLE_INSTANCE_HEADER_WIDETAG] = trans_boxed;
2343 transother[VALUE_CELL_HEADER_WIDETAG] = trans_boxed;
2344 transother[SYMBOL_HEADER_WIDETAG] = trans_tiny_boxed;
2345 transother[CHARACTER_WIDETAG] = trans_immediate;
2346 transother[SAP_WIDETAG] = trans_unboxed;
2347 #ifdef SIMD_PACK_WIDETAG
2348 transother[SIMD_PACK_WIDETAG] = trans_unboxed;
2349 #endif
2350 transother[UNBOUND_MARKER_WIDETAG] = trans_immediate;
2351 transother[NO_TLS_VALUE_MARKER_WIDETAG] = trans_immediate;
2352 transother[WEAK_POINTER_WIDETAG] = trans_weak_pointer;
2353 transother[INSTANCE_HEADER_WIDETAG] = trans_instance;
2354 transother[FDEFN_WIDETAG] = trans_tiny_boxed;
2356 /* size table, initialized the same way as scavtab */
2357 for (i = 0; i < ((sizeof sizetab)/(sizeof sizetab[0])); i++)
2358 sizetab[i] = size_lose;
2359 for (i = 0; i < (1<<(N_WIDETAG_BITS-N_LOWTAG_BITS)); i++) {
2360 for (j = 0; j < (1<<N_LOWTAG_BITS); j++) {
2361 if (fixnump(j)) {
2362 sizetab[j|(i<<N_LOWTAG_BITS)] = size_immediate;
2365 sizetab[FUN_POINTER_LOWTAG|(i<<N_LOWTAG_BITS)] = size_pointer;
2366 /* skipping OTHER_IMMEDIATE_0_LOWTAG */
2367 sizetab[LIST_POINTER_LOWTAG|(i<<N_LOWTAG_BITS)] = size_pointer;
2368 sizetab[INSTANCE_POINTER_LOWTAG|(i<<N_LOWTAG_BITS)] = size_pointer;
2369 /* skipping OTHER_IMMEDIATE_1_LOWTAG */
2370 sizetab[OTHER_POINTER_LOWTAG|(i<<N_LOWTAG_BITS)] = size_pointer;
2372 sizetab[BIGNUM_WIDETAG] = size_unboxed;
2373 sizetab[RATIO_WIDETAG] = size_boxed;
2374 #if N_WORD_BITS == 64
2375 sizetab[SINGLE_FLOAT_WIDETAG] = size_immediate;
2376 #else
2377 sizetab[SINGLE_FLOAT_WIDETAG] = size_unboxed;
2378 #endif
2379 sizetab[DOUBLE_FLOAT_WIDETAG] = size_unboxed;
2380 #ifdef LONG_FLOAT_WIDETAG
2381 sizetab[LONG_FLOAT_WIDETAG] = size_unboxed;
2382 #endif
2383 sizetab[COMPLEX_WIDETAG] = size_boxed;
2384 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
2385 sizetab[COMPLEX_SINGLE_FLOAT_WIDETAG] = size_unboxed;
2386 #endif
2387 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
2388 sizetab[COMPLEX_DOUBLE_FLOAT_WIDETAG] = size_unboxed;
2389 #endif
2390 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
2391 sizetab[COMPLEX_LONG_FLOAT_WIDETAG] = size_unboxed;
2392 #endif
2393 sizetab[SIMPLE_ARRAY_WIDETAG] = size_boxed;
2394 sizetab[SIMPLE_BASE_STRING_WIDETAG] = size_base_string;
2395 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2396 sizetab[SIMPLE_CHARACTER_STRING_WIDETAG] = size_character_string;
2397 #endif
2398 sizetab[SIMPLE_BIT_VECTOR_WIDETAG] = size_vector_bit;
2399 sizetab[SIMPLE_VECTOR_WIDETAG] = size_vector;
2400 sizetab[SIMPLE_ARRAY_NIL_WIDETAG] = size_vector_nil;
2401 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG] =
2402 size_vector_unsigned_byte_2;
2403 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG] =
2404 size_vector_unsigned_byte_4;
2405 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG] =
2406 size_vector_unsigned_byte_8;
2407 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG] =
2408 size_vector_unsigned_byte_8;
2409 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG] =
2410 size_vector_unsigned_byte_16;
2411 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG] =
2412 size_vector_unsigned_byte_16;
2413 #if (N_WORD_BITS == 32)
2414 sizetab[SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG] =
2415 size_vector_unsigned_byte_32;
2416 #endif
2417 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG] =
2418 size_vector_unsigned_byte_32;
2419 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG] =
2420 size_vector_unsigned_byte_32;
2421 #if (N_WORD_BITS == 64)
2422 sizetab[SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG] =
2423 size_vector_unsigned_byte_64;
2424 #endif
2425 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2426 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG] =
2427 size_vector_unsigned_byte_64;
2428 #endif
2429 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2430 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG] =
2431 size_vector_unsigned_byte_64;
2432 #endif
2433 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2434 sizetab[SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG] = size_vector_unsigned_byte_8;
2435 #endif
2436 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2437 sizetab[SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG] =
2438 size_vector_unsigned_byte_16;
2439 #endif
2440 #if (N_WORD_BITS == 32)
2441 sizetab[SIMPLE_ARRAY_FIXNUM_WIDETAG] =
2442 size_vector_unsigned_byte_32;
2443 #endif
2444 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2445 sizetab[SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG] =
2446 size_vector_unsigned_byte_32;
2447 #endif
2448 #if (N_WORD_BITS == 64)
2449 sizetab[SIMPLE_ARRAY_FIXNUM_WIDETAG] =
2450 size_vector_unsigned_byte_64;
2451 #endif
2452 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2453 sizetab[SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG] =
2454 size_vector_unsigned_byte_64;
2455 #endif
2456 sizetab[SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG] = size_vector_single_float;
2457 sizetab[SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG] = size_vector_double_float;
2458 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2459 sizetab[SIMPLE_ARRAY_LONG_FLOAT_WIDETAG] = size_vector_long_float;
2460 #endif
2461 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2462 sizetab[SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG] =
2463 size_vector_complex_single_float;
2464 #endif
2465 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2466 sizetab[SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG] =
2467 size_vector_complex_double_float;
2468 #endif
2469 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2470 sizetab[SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG] =
2471 size_vector_complex_long_float;
2472 #endif
2473 sizetab[COMPLEX_BASE_STRING_WIDETAG] = size_boxed;
2474 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
2475 sizetab[COMPLEX_CHARACTER_STRING_WIDETAG] = size_boxed;
2476 #endif
2477 sizetab[COMPLEX_VECTOR_NIL_WIDETAG] = size_boxed;
2478 sizetab[COMPLEX_BIT_VECTOR_WIDETAG] = size_boxed;
2479 sizetab[COMPLEX_VECTOR_WIDETAG] = size_boxed;
2480 sizetab[COMPLEX_ARRAY_WIDETAG] = size_boxed;
2481 sizetab[CODE_HEADER_WIDETAG] = size_code_header;
2482 #if 0
2483 /* We shouldn't see these, so just lose if it happens. */
2484 sizetab[SIMPLE_FUN_HEADER_WIDETAG] = size_function_header;
2485 sizetab[RETURN_PC_HEADER_WIDETAG] = size_return_pc_header;
2486 #endif
2487 sizetab[CLOSURE_HEADER_WIDETAG] = size_boxed;
2488 sizetab[FUNCALLABLE_INSTANCE_HEADER_WIDETAG] = size_boxed;
2489 sizetab[VALUE_CELL_HEADER_WIDETAG] = size_boxed;
2490 sizetab[SYMBOL_HEADER_WIDETAG] = size_tiny_boxed;
2491 sizetab[CHARACTER_WIDETAG] = size_immediate;
2492 sizetab[SAP_WIDETAG] = size_unboxed;
2493 #ifdef SIMD_PACK_WIDETAG
2494 sizetab[SIMD_PACK_WIDETAG] = size_unboxed;
2495 #endif
2496 sizetab[UNBOUND_MARKER_WIDETAG] = size_immediate;
2497 sizetab[NO_TLS_VALUE_MARKER_WIDETAG] = size_immediate;
2498 sizetab[WEAK_POINTER_WIDETAG] = size_weak_pointer;
2499 sizetab[INSTANCE_HEADER_WIDETAG] = size_instance;
2500 sizetab[FDEFN_WIDETAG] = size_tiny_boxed;
2504 /* Find the code object for the given pc, or return NULL on
2505 failure. */
2506 lispobj *
2507 component_ptr_from_pc(lispobj *pc)
2509 lispobj *object = NULL;
2511 if ( (object = search_read_only_space(pc)) )
2513 else if ( (object = search_static_space(pc)) )
2515 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2516 else if ( (object = search_immobile_space(pc)) )
2518 #endif
2519 else
2520 object = search_dynamic_space(pc);
2522 if (object) /* if we found something */
2523 if (widetag_of(*object) == CODE_HEADER_WIDETAG)
2524 return(object);
2526 return (NULL);
2529 /* Scan an area looking for an object which encloses the given pointer.
2530 * Return the object start on success or NULL on failure. */
2531 lispobj *
2532 gc_search_space(lispobj *start, size_t words, lispobj *pointer)
2534 while (words > 0) {
2535 size_t count = 1;
2536 lispobj *forwarded_start;
2538 if (forwarding_pointer_p(start))
2539 forwarded_start =
2540 native_pointer((lispobj)forwarding_pointer_value(start));
2541 else
2542 forwarded_start = start;
2543 lispobj thing = *forwarded_start;
2544 /* If thing is an immediate then this is a cons. */
2545 if (is_lisp_pointer(thing) || is_lisp_immediate(thing))
2546 count = 2;
2547 else
2548 count = (sizetab[widetag_of(thing)])(forwarded_start);
2550 /* Check whether the pointer is within this object. */
2551 if ((pointer >= start) && (pointer < (start+count))) {
2552 /* found it! */
2553 /*FSHOW((stderr,"/found %x in %x %x\n", pointer, start, thing));*/
2554 return(start);
2557 /* Round up the count. */
2558 count = CEILING(count,2);
2560 start += count;
2561 words -= count;
2563 return (NULL);
2566 /* Helper for valid_lisp_pointer_p (below) and
2567 * conservative_root_p (gencgc).
2569 * pointer is the pointer to check validity of,
2570 * and start_addr is the address of the enclosing object.
2573 properly_tagged_descriptor_p(lispobj pointer, lispobj *start_addr)
2575 if (!is_lisp_pointer(pointer)) {
2576 return 0;
2579 /* Check that the object pointed to is consistent with the pointer
2580 * low tag. */
2581 switch (lowtag_of(pointer)) {
2582 case FUN_POINTER_LOWTAG:
2583 /* Start_addr should be the enclosing code object, or a closure
2584 * header. */
2585 switch (widetag_of(*start_addr)) {
2586 case CODE_HEADER_WIDETAG:
2587 /* Make sure we actually point to a function in the code object,
2588 * as opposed to a random point there. */
2589 if (SIMPLE_FUN_HEADER_WIDETAG==widetag_of(native_pointer(pointer)[0]))
2590 return 1;
2591 else
2592 return 0;
2593 case CLOSURE_HEADER_WIDETAG:
2594 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2595 if (pointer != make_lispobj(start_addr, FUN_POINTER_LOWTAG)) {
2596 return 0;
2598 break;
2599 default:
2600 return 0;
2602 break;
2603 case LIST_POINTER_LOWTAG:
2604 if (pointer != make_lispobj(start_addr, LIST_POINTER_LOWTAG)) {
2605 return 0;
2607 /* Is it plausible cons? */
2608 if ((is_lisp_pointer(start_addr[0]) ||
2609 is_lisp_immediate(start_addr[0])) &&
2610 (is_lisp_pointer(start_addr[1]) ||
2611 is_lisp_immediate(start_addr[1])))
2612 break;
2613 else {
2614 return 0;
2616 case INSTANCE_POINTER_LOWTAG:
2617 if (pointer != make_lispobj(start_addr, INSTANCE_POINTER_LOWTAG)) {
2618 return 0;
2620 if (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
2621 return 0;
2623 break;
2624 case OTHER_POINTER_LOWTAG:
2626 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
2627 /* The all-architecture test below is good as far as it goes,
2628 * but an LRA object is similar to a FUN-POINTER: It is
2629 * embedded within a CODE-OBJECT pointed to by start_addr, and
2630 * cannot be found by simply walking the heap, therefore we
2631 * need to check for it. -- AB, 2010-Jun-04 */
2632 if ((widetag_of(start_addr[0]) == CODE_HEADER_WIDETAG)) {
2633 lispobj *potential_lra = native_pointer(pointer);
2634 if ((widetag_of(potential_lra[0]) == RETURN_PC_HEADER_WIDETAG) &&
2635 ((potential_lra - HeaderValue(potential_lra[0])) == start_addr)) {
2636 return 1; /* It's as good as we can verify. */
2639 #endif
2641 if (pointer != make_lispobj(start_addr, OTHER_POINTER_LOWTAG)) {
2642 return 0;
2644 /* Is it plausible? Not a cons. XXX should check the headers. */
2645 if (is_lisp_pointer(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
2646 return 0;
2648 switch (widetag_of(start_addr[0])) {
2649 case UNBOUND_MARKER_WIDETAG:
2650 case NO_TLS_VALUE_MARKER_WIDETAG:
2651 case CHARACTER_WIDETAG:
2652 #if N_WORD_BITS == 64
2653 case SINGLE_FLOAT_WIDETAG:
2654 #endif
2655 return 0;
2657 /* only pointed to by function pointers? */
2658 case CLOSURE_HEADER_WIDETAG:
2659 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2660 return 0;
2662 case INSTANCE_HEADER_WIDETAG:
2663 return 0;
2665 /* the valid other immediate pointer objects */
2666 case SIMPLE_VECTOR_WIDETAG:
2667 case RATIO_WIDETAG:
2668 case COMPLEX_WIDETAG:
2669 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
2670 case COMPLEX_SINGLE_FLOAT_WIDETAG:
2671 #endif
2672 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
2673 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
2674 #endif
2675 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
2676 case COMPLEX_LONG_FLOAT_WIDETAG:
2677 #endif
2678 #ifdef SIMD_PACK_WIDETAG
2679 case SIMD_PACK_WIDETAG:
2680 #endif
2681 case SIMPLE_ARRAY_WIDETAG:
2682 case COMPLEX_BASE_STRING_WIDETAG:
2683 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
2684 case COMPLEX_CHARACTER_STRING_WIDETAG:
2685 #endif
2686 case COMPLEX_VECTOR_NIL_WIDETAG:
2687 case COMPLEX_BIT_VECTOR_WIDETAG:
2688 case COMPLEX_VECTOR_WIDETAG:
2689 case COMPLEX_ARRAY_WIDETAG:
2690 case VALUE_CELL_HEADER_WIDETAG:
2691 case SYMBOL_HEADER_WIDETAG:
2692 case FDEFN_WIDETAG:
2693 case CODE_HEADER_WIDETAG:
2694 case BIGNUM_WIDETAG:
2695 #if N_WORD_BITS != 64
2696 case SINGLE_FLOAT_WIDETAG:
2697 #endif
2698 case DOUBLE_FLOAT_WIDETAG:
2699 #ifdef LONG_FLOAT_WIDETAG
2700 case LONG_FLOAT_WIDETAG:
2701 #endif
2702 case SIMPLE_BASE_STRING_WIDETAG:
2703 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2704 case SIMPLE_CHARACTER_STRING_WIDETAG:
2705 #endif
2706 case SIMPLE_BIT_VECTOR_WIDETAG:
2707 case SIMPLE_ARRAY_NIL_WIDETAG:
2708 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2709 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2710 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
2711 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2712 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
2713 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2715 case SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG:
2717 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
2718 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2719 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2720 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
2721 #endif
2722 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2723 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
2724 #endif
2725 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2726 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2727 #endif
2728 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2729 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2730 #endif
2732 case SIMPLE_ARRAY_FIXNUM_WIDETAG:
2734 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2735 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2736 #endif
2737 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2738 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
2739 #endif
2740 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2741 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2742 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2743 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2744 #endif
2745 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2746 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2747 #endif
2748 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2749 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2750 #endif
2751 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2752 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2753 #endif
2754 case SAP_WIDETAG:
2755 case WEAK_POINTER_WIDETAG:
2756 break;
2758 default:
2759 return 0;
2761 break;
2762 default:
2763 return 0;
2766 /* looks good */
2767 return 1;
2770 /* META: Note the ambiguous word "validate" in the comment below.
2771 * This means "Decide whether <x> is valid".
2772 * But when you see os_validate() elsewhere, that doesn't mean to ask
2773 * whether something is valid, it says to *make* it valid.
2774 * I think it would be nice if we could avoid using the word in the
2775 * sense in which os_validate() uses it, which would entail renaming
2776 * a bunch of stuff, which is harder than just explaining why
2777 * the comments can be deceptive */
2779 /* Used by the debugger to validate possibly bogus pointers before
2780 * calling MAKE-LISP-OBJ on them.
2782 * FIXME: We would like to make this perfect, because if the debugger
2783 * constructs a reference to a bugs lisp object, and it ends up in a
2784 * location scavenged by the GC all hell breaks loose.
2786 * Whereas conservative_root_p has to be conservative
2787 * and return true for all valid pointers, this could actually be eager
2788 * and lie about a few pointers without bad results... but that should
2789 * be reflected in the name.
2792 valid_lisp_pointer_p(lispobj *pointer)
2794 lispobj *start;
2795 if (((start=search_dynamic_space(pointer))!=NULL) ||
2796 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2797 ((start=search_immobile_space(pointer))!=NULL) ||
2798 #endif
2799 ((start=search_static_space(pointer))!=NULL) ||
2800 ((start=search_read_only_space(pointer))!=NULL))
2801 return properly_tagged_descriptor_p((lispobj)pointer, start);
2802 else
2803 return 0;
2806 boolean
2807 maybe_gc(os_context_t *context)
2809 lispobj gc_happened;
2810 struct thread *thread = arch_os_get_current_thread();
2811 boolean were_in_lisp = !foreign_function_call_active_p(thread);
2813 if (were_in_lisp) {
2814 fake_foreign_function_call(context);
2817 /* SUB-GC may return without GCing if *GC-INHIBIT* is set, in
2818 * which case we will be running with no gc trigger barrier
2819 * thing for a while. But it shouldn't be long until the end
2820 * of WITHOUT-GCING.
2822 * FIXME: It would be good to protect the end of dynamic space for
2823 * CheneyGC and signal a storage condition from there.
2826 /* Restore the signal mask from the interrupted context before
2827 * calling into Lisp if interrupts are enabled. Why not always?
2829 * Suppose there is a WITHOUT-INTERRUPTS block far, far out. If an
2830 * interrupt hits while in SUB-GC, it is deferred and the
2831 * os_context_sigmask of that interrupt is set to block further
2832 * deferrable interrupts (until the first one is
2833 * handled). Unfortunately, that context refers to this place and
2834 * when we return from here the signals will not be blocked.
2836 * A kludgy alternative is to propagate the sigmask change to the
2837 * outer context.
2839 #if !(defined(LISP_FEATURE_WIN32) || defined(LISP_FEATURE_SB_SAFEPOINT))
2840 check_gc_signals_unblocked_or_lose(os_context_sigmask_addr(context));
2841 unblock_gc_signals(0, 0);
2842 #endif
2843 FSHOW((stderr, "/maybe_gc: calling SUB_GC\n"));
2844 /* FIXME: Nothing must go wrong during GC else we end up running
2845 * the debugger, error handlers, and user code in general in a
2846 * potentially unsafe place. Running out of the control stack or
2847 * the heap in SUB-GC are ways to lose. Of course, deferrables
2848 * cannot be unblocked because there may be a pending handler, or
2849 * we may even be in a WITHOUT-INTERRUPTS. */
2850 gc_happened = funcall0(StaticSymbolFunction(SUB_GC));
2851 FSHOW((stderr, "/maybe_gc: gc_happened=%s\n",
2852 (gc_happened == NIL)
2853 ? "NIL"
2854 : ((gc_happened == T)
2855 ? "T"
2856 : "0")));
2857 /* gc_happened can take three values: T, NIL, 0.
2859 * T means that the thread managed to trigger a GC, and post-gc
2860 * must be called.
2862 * NIL means that the thread is within without-gcing, and no GC
2863 * has occurred.
2865 * Finally, 0 means that *a* GC has occurred, but it wasn't
2866 * triggered by this thread; success, but post-gc doesn't have
2867 * to be called.
2869 if ((gc_happened == T) &&
2870 /* See if interrupts are enabled or it's possible to enable
2871 * them. POST-GC has a similar check, but we don't want to
2872 * unlock deferrables in that case and get a pending interrupt
2873 * here. */
2874 ((SymbolValue(INTERRUPTS_ENABLED,thread) != NIL) ||
2875 (SymbolValue(ALLOW_WITH_INTERRUPTS,thread) != NIL))) {
2876 #ifndef LISP_FEATURE_WIN32
2877 sigset_t *context_sigmask = os_context_sigmask_addr(context);
2878 if (!deferrables_blocked_p(context_sigmask)) {
2879 thread_sigmask(SIG_SETMASK, context_sigmask, 0);
2880 #ifndef LISP_FEATURE_SB_SAFEPOINT
2881 check_gc_signals_unblocked_or_lose(0);
2882 #endif
2883 #endif
2884 FSHOW((stderr, "/maybe_gc: calling POST_GC\n"));
2885 funcall0(StaticSymbolFunction(POST_GC));
2886 #ifndef LISP_FEATURE_WIN32
2887 } else {
2888 FSHOW((stderr, "/maybe_gc: punting on POST_GC due to blockage\n"));
2890 #endif
2893 if (were_in_lisp) {
2894 undo_fake_foreign_function_call(context);
2895 } else {
2896 /* Otherwise done by undo_fake_foreign_function_call. And
2897 something later wants them to be blocked. What a nice
2898 interface.*/
2899 block_blockable_signals(0);
2902 FSHOW((stderr, "/maybe_gc: returning\n"));
2903 return (gc_happened != NIL);
2906 #define BYTES_ZERO_BEFORE_END (1<<12)
2908 /* There used to be a similar function called SCRUB-CONTROL-STACK in
2909 * Lisp and another called zero_stack() in cheneygc.c, but since it's
2910 * shorter to express in, and more often called from C, I keep only
2911 * the C one after fixing it. -- MG 2009-03-25 */
2913 /* Zero the unused portion of the control stack so that old objects
2914 * are not kept alive because of uninitialized stack variables.
2916 * "To summarize the problem, since not all allocated stack frame
2917 * slots are guaranteed to be written by the time you call an another
2918 * function or GC, there may be garbage pointers retained in your dead
2919 * stack locations. The stack scrubbing only affects the part of the
2920 * stack from the SP to the end of the allocated stack." - ram, on
2921 * cmucl-imp, Tue, 25 Sep 2001
2923 * So, as an (admittedly lame) workaround, from time to time we call
2924 * scrub-control-stack to zero out all the unused portion. This is
2925 * supposed to happen when the stack is mostly empty, so that we have
2926 * a chance of clearing more of it: callers are currently (2002.07.18)
2927 * REPL, SUB-GC and sig_stop_for_gc_handler. */
2929 /* Take care not to tread on the guard page and the hard guard page as
2930 * it would be unkind to sig_stop_for_gc_handler. Touching the return
2931 * guard page is not dangerous. For this to work the guard page must
2932 * be zeroed when protected. */
2934 /* FIXME: I think there is no guarantee that once
2935 * BYTES_ZERO_BEFORE_END bytes are zero the rest are also zero. This
2936 * may be what the "lame" adjective in the above comment is for. In
2937 * this case, exact gc may lose badly. */
2938 void
2939 scrub_control_stack()
2941 scrub_thread_control_stack(arch_os_get_current_thread());
2944 void
2945 scrub_thread_control_stack(struct thread *th)
2947 os_vm_address_t guard_page_address = CONTROL_STACK_GUARD_PAGE(th);
2948 os_vm_address_t hard_guard_page_address = CONTROL_STACK_HARD_GUARD_PAGE(th);
2949 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
2950 /* On these targets scrubbing from C is a bad idea, so we punt to
2951 * a routine in $ARCH-assem.S. */
2952 extern void arch_scrub_control_stack(struct thread *, os_vm_address_t, os_vm_address_t);
2953 arch_scrub_control_stack(th, guard_page_address, hard_guard_page_address);
2954 #else
2955 lispobj *sp = access_control_stack_pointer(th);
2956 scrub:
2957 if ((((os_vm_address_t)sp < (hard_guard_page_address + os_vm_page_size)) &&
2958 ((os_vm_address_t)sp >= hard_guard_page_address)) ||
2959 (((os_vm_address_t)sp < (guard_page_address + os_vm_page_size)) &&
2960 ((os_vm_address_t)sp >= guard_page_address) &&
2961 (th->control_stack_guard_page_protected != NIL)))
2962 return;
2963 #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD
2964 do {
2965 *sp = 0;
2966 } while (((uword_t)sp--) & (BYTES_ZERO_BEFORE_END - 1));
2967 if ((os_vm_address_t)sp < (hard_guard_page_address + os_vm_page_size))
2968 return;
2969 do {
2970 if (*sp)
2971 goto scrub;
2972 } while (((uword_t)sp--) & (BYTES_ZERO_BEFORE_END - 1));
2973 #else
2974 do {
2975 *sp = 0;
2976 } while (((uword_t)++sp) & (BYTES_ZERO_BEFORE_END - 1));
2977 if ((os_vm_address_t)sp >= hard_guard_page_address)
2978 return;
2979 do {
2980 if (*sp)
2981 goto scrub;
2982 } while (((uword_t)++sp) & (BYTES_ZERO_BEFORE_END - 1));
2983 #endif
2984 #endif /* LISP_FEATURE_C_STACK_IS_CONTROL_STACK */
2987 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
2989 void
2990 scavenge_control_stack(struct thread *th)
2992 lispobj *object_ptr;
2994 /* In order to properly support dynamic-extent allocation of
2995 * non-CONS objects, the control stack requires special handling.
2996 * Rather than calling scavenge() directly, grovel over it fixing
2997 * broken hearts, scavenging pointers to oldspace, and pitching a
2998 * fit when encountering unboxed data. This prevents stray object
2999 * headers from causing the scavenger to blow past the end of the
3000 * stack (an error case checked in scavenge()). We don't worry
3001 * about treating unboxed words as boxed or vice versa, because
3002 * the compiler isn't allowed to store unboxed objects on the
3003 * control stack. -- AB, 2011-Dec-02 */
3005 for (object_ptr = th->control_stack_start;
3006 object_ptr < access_control_stack_pointer(th);
3007 object_ptr++) {
3009 lispobj object = *object_ptr;
3010 #ifdef LISP_FEATURE_GENCGC
3011 if (forwarding_pointer_p(object_ptr))
3012 lose("unexpected forwarding pointer in scavenge_control_stack: %p, start=%p, end=%p\n",
3013 object_ptr, th->control_stack_start, access_control_stack_pointer(th));
3014 #endif
3015 if (is_lisp_pointer(object) && from_space_p(object)) {
3016 /* It currently points to old space. Check for a
3017 * forwarding pointer. */
3018 lispobj *ptr = native_pointer(object);
3019 if (forwarding_pointer_p(ptr)) {
3020 /* Yes, there's a forwarding pointer. */
3021 *object_ptr = LOW_WORD(forwarding_pointer_value(ptr));
3022 } else {
3023 /* Scavenge that pointer. */
3024 long n_words_scavenged =
3025 (scavtab[widetag_of(object)])(object_ptr, object);
3026 gc_assert(n_words_scavenged == 1);
3028 } else if (scavtab[widetag_of(object)] == scav_lose) {
3029 lose("unboxed object in scavenge_control_stack: %p->%x, start=%p, end=%p\n",
3030 object_ptr, object, th->control_stack_start, access_control_stack_pointer(th));
3035 /* Scavenging Interrupt Contexts */
3037 static int boxed_registers[] = BOXED_REGISTERS;
3039 /* The GC has a notion of an "interior pointer" register, an unboxed
3040 * register that typically contains a pointer to inside an object
3041 * referenced by another pointer. The most obvious of these is the
3042 * program counter, although many compiler backends define a "Lisp
3043 * Interior Pointer" register known to the runtime as reg_LIP, and
3044 * various CPU architectures have other registers that also partake of
3045 * the interior-pointer nature. As the code for pairing an interior
3046 * pointer value up with its "base" register, and fixing it up after
3047 * scavenging is complete is horribly repetitive, a few macros paper
3048 * over the monotony. --AB, 2010-Jul-14 */
3050 /* These macros are only ever used over a lexical environment which
3051 * defines a pointer to an os_context_t called context, thus we don't
3052 * bother to pass that context in as a parameter. */
3054 /* Define how to access a given interior pointer. */
3055 #define ACCESS_INTERIOR_POINTER_pc \
3056 *os_context_pc_addr(context)
3057 #define ACCESS_INTERIOR_POINTER_lip \
3058 *os_context_register_addr(context, reg_LIP)
3059 #define ACCESS_INTERIOR_POINTER_lr \
3060 *os_context_lr_addr(context)
3061 #define ACCESS_INTERIOR_POINTER_npc \
3062 *os_context_npc_addr(context)
3063 #define ACCESS_INTERIOR_POINTER_ctr \
3064 *os_context_ctr_addr(context)
3066 #define INTERIOR_POINTER_VARS(name) \
3067 uword_t name##_offset; \
3068 int name##_register_pair
3070 #define PAIR_INTERIOR_POINTER(name) \
3071 pair_interior_pointer(context, \
3072 ACCESS_INTERIOR_POINTER_##name, \
3073 &name##_offset, \
3074 &name##_register_pair)
3076 /* One complexity here is that if a paired register is not found for
3077 * an interior pointer, then that pointer does not get updated.
3078 * Originally, there was some commentary about using an index of -1
3079 * when calling os_context_register_addr() on SPARC referring to the
3080 * program counter, but the real reason is to allow an interior
3081 * pointer register to point to the runtime, read-only space, or
3082 * static space without problems. */
3083 #define FIXUP_INTERIOR_POINTER(name) \
3084 do { \
3085 if (name##_register_pair >= 0) { \
3086 ACCESS_INTERIOR_POINTER_##name = \
3087 (*os_context_register_addr(context, \
3088 name##_register_pair) \
3089 & ~LOWTAG_MASK) \
3090 + name##_offset; \
3092 } while (0)
3095 static void
3096 pair_interior_pointer(os_context_t *context, uword_t pointer,
3097 uword_t *saved_offset, int *register_pair)
3099 int i;
3102 * I (RLT) think this is trying to find the boxed register that is
3103 * closest to the LIP address, without going past it. Usually, it's
3104 * reg_CODE or reg_LRA. But sometimes, nothing can be found.
3106 /* 0x7FFFFFFF on 32-bit platforms;
3107 0x7FFFFFFFFFFFFFFF on 64-bit platforms */
3108 *saved_offset = (((uword_t)1) << (N_WORD_BITS - 1)) - 1;
3109 *register_pair = -1;
3110 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
3111 uword_t reg;
3112 sword_t offset;
3113 int index;
3115 index = boxed_registers[i];
3116 reg = *os_context_register_addr(context, index);
3118 /* An interior pointer is never relative to a non-pointer
3119 * register (an oversight in the original implementation).
3120 * The simplest argument for why this is true is to consider
3121 * the fixnum that happens by coincide to be the word-index in
3122 * memory of the header for some object plus two. This is
3123 * happenstance would cause the register containing the fixnum
3124 * to be selected as the register_pair if the interior pointer
3125 * is to anywhere after the first two words of the object.
3126 * The fixnum won't be changed during GC, but the object might
3127 * move, thus destroying the interior pointer. --AB,
3128 * 2010-Jul-14 */
3130 if (is_lisp_pointer(reg) &&
3131 ((reg & ~LOWTAG_MASK) <= pointer)) {
3132 offset = pointer - (reg & ~LOWTAG_MASK);
3133 if (offset < *saved_offset) {
3134 *saved_offset = offset;
3135 *register_pair = index;
3141 static void
3142 scavenge_interrupt_context(os_context_t * context)
3144 int i;
3146 /* FIXME: The various #ifdef noise here is precisely that: noise.
3147 * Is it possible to fold it into the macrology so that we have
3148 * one set of #ifdefs and then INTERIOR_POINTER_VARS /et alia/
3149 * compile out for the registers that don't exist on a given
3150 * platform? */
3152 INTERIOR_POINTER_VARS(pc);
3153 #ifdef reg_LIP
3154 INTERIOR_POINTER_VARS(lip);
3155 #endif
3156 #ifdef ARCH_HAS_LINK_REGISTER
3157 INTERIOR_POINTER_VARS(lr);
3158 #endif
3159 #ifdef ARCH_HAS_NPC_REGISTER
3160 INTERIOR_POINTER_VARS(npc);
3161 #endif
3162 #ifdef LISP_FEATURE_PPC
3163 INTERIOR_POINTER_VARS(ctr);
3164 #endif
3166 PAIR_INTERIOR_POINTER(pc);
3167 #ifdef reg_LIP
3168 PAIR_INTERIOR_POINTER(lip);
3169 #endif
3170 #ifdef ARCH_HAS_LINK_REGISTER
3171 PAIR_INTERIOR_POINTER(lr);
3172 #endif
3173 #ifdef ARCH_HAS_NPC_REGISTER
3174 PAIR_INTERIOR_POINTER(npc);
3175 #endif
3176 #ifdef LISP_FEATURE_PPC
3177 PAIR_INTERIOR_POINTER(ctr);
3178 #endif
3180 /* Scavenge all boxed registers in the context. */
3181 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
3182 int index;
3183 lispobj foo;
3185 index = boxed_registers[i];
3186 foo = *os_context_register_addr(context, index);
3187 scavenge(&foo, 1);
3188 *os_context_register_addr(context, index) = foo;
3190 /* this is unlikely to work as intended on bigendian
3191 * 64 bit platforms */
3193 scavenge((lispobj *) os_context_register_addr(context, index), 1);
3196 /* Now that the scavenging is done, repair the various interior
3197 * pointers. */
3198 FIXUP_INTERIOR_POINTER(pc);
3199 #ifdef reg_LIP
3200 FIXUP_INTERIOR_POINTER(lip);
3201 #endif
3202 #ifdef ARCH_HAS_LINK_REGISTER
3203 FIXUP_INTERIOR_POINTER(lr);
3204 #endif
3205 #ifdef ARCH_HAS_NPC_REGISTER
3206 FIXUP_INTERIOR_POINTER(npc);
3207 #endif
3208 #ifdef LISP_FEATURE_PPC
3209 FIXUP_INTERIOR_POINTER(ctr);
3210 #endif
3213 void
3214 scavenge_interrupt_contexts(struct thread *th)
3216 int i, index;
3217 os_context_t *context;
3219 index = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3221 #if defined(DEBUG_PRINT_CONTEXT_INDEX)
3222 printf("Number of active contexts: %d\n", index);
3223 #endif
3225 for (i = 0; i < index; i++) {
3226 context = th->interrupt_contexts[i];
3227 scavenge_interrupt_context(context);
3230 #endif /* x86oid targets */
3232 // The following accessors, which take a valid native pointer as input
3233 // and return a Lisp string, are designed to be foolproof during GC,
3234 // hence all the forwarding checks.
3236 #if defined(LISP_FEATURE_SB_LDB)
3237 #include "genesis/classoid.h"
3238 struct vector * symbol_name(lispobj * sym)
3240 if (forwarding_pointer_p(sym))
3241 sym = native_pointer((lispobj)forwarding_pointer_value(sym));
3242 if (lowtag_of(((struct symbol*)sym)->name) != OTHER_POINTER_LOWTAG)
3243 return NULL;
3244 lispobj * name = native_pointer(((struct symbol*)sym)->name);
3245 if (forwarding_pointer_p(name))
3246 name = native_pointer((lispobj)forwarding_pointer_value(name));
3247 return (struct vector*)name;
3249 struct vector * classoid_name(lispobj * classoid)
3251 if (forwarding_pointer_p(classoid))
3252 classoid = native_pointer((lispobj)forwarding_pointer_value(classoid));
3253 lispobj sym = ((struct classoid*)classoid)->name;
3254 return lowtag_of(sym) != OTHER_POINTER_LOWTAG ? NULL
3255 : symbol_name(native_pointer(sym));
3257 struct vector * layout_classoid_name(lispobj * layout)
3259 if (forwarding_pointer_p(layout))
3260 layout = native_pointer((lispobj)forwarding_pointer_value(layout));
3261 lispobj classoid = ((struct layout*)layout)->classoid;
3262 return lowtag_of(classoid) != INSTANCE_POINTER_LOWTAG ? NULL
3263 : classoid_name(native_pointer(classoid));
3265 struct vector * instance_classoid_name(lispobj * instance)
3267 if (forwarding_pointer_p(instance))
3268 instance = native_pointer((lispobj)forwarding_pointer_value(instance));
3269 lispobj layout = instance_layout(instance);
3270 return lowtag_of(layout) != INSTANCE_POINTER_LOWTAG ? NULL
3271 : layout_classoid_name(native_pointer(layout));
3273 void safely_show_lstring(struct vector * string, int quotes, FILE *s)
3275 extern void show_lstring(struct vector*, int, FILE*);
3276 if (forwarding_pointer_p((lispobj*)string))
3277 string = (struct vector*)forwarding_pointer_value((lispobj*)string);
3278 if (
3279 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
3280 widetag_of(string->header) == SIMPLE_CHARACTER_STRING_WIDETAG ||
3281 #endif
3282 widetag_of(string->header) == SIMPLE_BASE_STRING_WIDETAG)
3283 show_lstring(string, quotes, s);
3284 else {
3285 fprintf(s, "#<[widetag=%02X]>", widetag_of(string->header));
3288 #endif