2 * Garbage Collection common functions for scavenging, moving and sizing
3 * objects. These are for use with both GC (stop & copy GC) and GENCGC
7 * This software is part of the SBCL system. See the README file for
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>
25 * <ftp://ftp.cs.utexas.edu/pub/garbage/bigsurv.ps>.
28 #define _GNU_SOURCE /* for ffsl(3) from string.h */
38 #include "interrupt.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 #define WANT_SCAV_TRANS_SIZE_TABLES
48 #include "gc-internal.h"
49 #include "forwarding-ptr.h"
52 #ifdef LISP_FEATURE_SPARC
53 #define LONG_FLOAT_SIZE 4
54 #elif defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
55 #define LONG_FLOAT_SIZE 3
58 os_vm_size_t dynamic_space_size
= DEFAULT_DYNAMIC_SPACE_SIZE
;
59 os_vm_size_t thread_control_stack_size
= DEFAULT_CONTROL_STACK_SIZE
;
61 sword_t (*scavtab
[256])(lispobj
*where
, lispobj object
);
62 lispobj (*transother
[256])(lispobj object
);
63 sword_t (*sizetab
[256])(lispobj
*where
);
64 struct weak_pointer
*weak_pointers
;
66 os_vm_size_t bytes_consed_between_gcs
= 12*1024*1024;
72 /* gc_general_copy_object is inline from gc-internal.h */
74 /* to copy a boxed object */
76 copy_object(lispobj object
, sword_t nwords
)
78 return gc_general_copy_object(object
, nwords
, BOXED_PAGE_FLAG
);
82 copy_code_object(lispobj object
, sword_t nwords
)
84 return gc_general_copy_object(object
, nwords
, CODE_PAGE_FLAG
);
87 static sword_t
scav_lose(lispobj
*where
, lispobj object
); /* forward decl */
89 static inline void scav1(lispobj
* object_ptr
, lispobj object
)
92 // * With 32-bit words, is_lisp_pointer(object) returns true if object_ptr
93 // points to a forwarding pointer, so we need a sanity check inside the
94 // branch for is_lisp_pointer(). For maximum efficiency, check that only
95 // after from_space_p() returns false, so that valid pointers into
96 // from_space incur no extra test. This could be improved further by
97 // skipping the FP check if 'object' points within dynamic space, i.e.,
98 // when find_page_index() returns >= 0. That would entail injecting
99 // from_space_p() explicitly into the loop, so as to separate the
100 // "was a page found at all" condition from the page generation test.
102 // * With 64-bit words, is_lisp_pointer(object) is false when object_ptr
103 // points to a forwarding pointer, and the fixnump() test also returns
104 // false, so we'll indirect through scavtab[]. This will safely invoke
105 // scav_lose(), detecting corruption without any extra cost.
106 // The major difference between that and the explicit test is that you
107 // won't see 'start' and 'n_words', but if you need those, chances are
108 // you'll want to run under an external debugger in the first place.
109 // [And btw it sure would be nice to assert statically
110 // that is_lisp_pointer(0x01) is indeed false]
112 #define FIX_POINTER() { \
113 lispobj *ptr = native_pointer(object); \
114 if (forwarding_pointer_p(ptr)) \
115 *object_ptr = LOW_WORD(forwarding_pointer_value(ptr)); \
116 else /* Scavenge that pointer. */ \
117 (void)scavtab[widetag_of(object)](object_ptr, object); \
119 #ifdef LISP_FEATURE_IMMOBILE_SPACE
121 // It would be fine, though suboptimal, to use from_space_p() here.
122 // If it returns false, we don't want to call immobile_space_p()
123 // unless the pointer is *not* into dynamic space.
124 if ((page
= find_page_index((void*)object
)) >= 0) {
125 if (page_table
[page
].gen
== from_space
&& !pinned_p(object
, page
))
127 } else if (immobile_space_p(object
)) {
128 lispobj
*ptr
= native_pointer(object
);
129 if (immobile_obj_gen_bits(ptr
) == from_space
)
130 promote_immobile_obj(ptr
, 1);
133 if (from_space_p(object
)) {
136 #if (N_WORD_BITS == 32) && defined(LISP_FEATURE_GENCGC)
137 if (forwarding_pointer_p(object_ptr
))
138 lose("unexpected forwarding pointer in scavenge @ %p\n",
141 /* It points somewhere other than oldspace. Leave it
147 // Scavenge a block of memory from 'start' to 'end'
148 // that may contain object headers.
149 void heap_scavenge(lispobj
*start
, lispobj
*end
)
153 for (object_ptr
= start
; object_ptr
< end
;) {
154 lispobj object
= *object_ptr
;
155 if (is_lisp_pointer(object
)) {
156 scav1(object_ptr
, object
);
159 else if (fixnump(object
)) {
160 /* It's a fixnum: really easy.. */
163 /* It's some sort of header object or another. */
164 object_ptr
+= (scavtab
[widetag_of(object
)])(object_ptr
, object
);
167 // This assertion is usually the one that fails when something
168 // is subtly wrong with the heap, so definitely always do it.
169 gc_assert_verbose(object_ptr
== end
, "Final object pointer %p, start %p, end %p\n",
170 object_ptr
, start
, end
);
173 // Scavenge a block of memory from 'start' extending for 'n_words'
174 // that must not contain any object headers.
175 sword_t
scavenge(lispobj
*start
, sword_t n_words
)
177 lispobj
*end
= start
+ n_words
;
179 for (object_ptr
= start
; object_ptr
< end
; object_ptr
++) {
180 lispobj object
= *object_ptr
;
181 if (is_lisp_pointer(object
)) scav1(object_ptr
, object
);
186 static lispobj
trans_fun_header(lispobj object
); /* forward decls */
187 static lispobj
trans_short_boxed(lispobj object
);
190 scav_fun_pointer(lispobj
*where
, lispobj object
)
192 gc_dcheck(lowtag_of(object
) == FUN_POINTER_LOWTAG
);
194 /* Object is a pointer into from_space - not a FP. */
195 lispobj
*first_pointer
= native_pointer(object
);
197 /* must transport object -- object may point to either a function
198 * header, a funcallable instance header, or a closure header. */
199 lispobj copy
= widetag_of(*first_pointer
) == SIMPLE_FUN_HEADER_WIDETAG
200 ? trans_fun_header(object
) : trans_short_boxed(object
);
202 if (copy
!= object
) {
203 /* Set forwarding pointer */
204 set_forwarding_pointer(first_pointer
,copy
);
207 CHECK_COPY_POSTCONDITIONS(copy
, FUN_POINTER_LOWTAG
);
216 trans_code(struct code
*code
)
218 /* if object has already been transported, just return pointer */
219 if (forwarding_pointer_p((lispobj
*)code
)) {
221 printf("Was already transported\n");
223 return (struct code
*)native_pointer(forwarding_pointer_value((lispobj
*)code
));
226 gc_dcheck(widetag_of(code
->header
) == CODE_HEADER_WIDETAG
);
228 /* prepare to transport the code vector */
229 lispobj l_code
= (lispobj
) LOW_WORD(code
) | OTHER_POINTER_LOWTAG
;
230 sword_t nheader_words
= code_header_words(code
->header
);
231 sword_t ncode_words
= code_instruction_words(code
->code_size
);
232 sword_t nwords
= nheader_words
+ ncode_words
;
233 lispobj l_new_code
= copy_code_object(l_code
, nwords
);
234 struct code
*new_code
= (struct code
*) native_pointer(l_new_code
);
236 #if defined(DEBUG_CODE_GC)
237 printf("Old code object at 0x%08x, new code object at 0x%08x.\n",
238 (uword_t
) code
, (uword_t
) new_code
);
239 printf("Code object is %d words long.\n", nwords
);
242 #ifdef LISP_FEATURE_GENCGC
243 if (new_code
== code
)
247 set_forwarding_pointer((lispobj
*)code
, l_new_code
);
249 /* set forwarding pointers for all the function headers in the */
250 /* code object. also fix all self pointers */
251 /* Do this by scanning the new code, since the old header is unusable */
253 uword_t displacement
= l_new_code
- l_code
;
255 for_each_simple_fun(i
, nfheaderp
, new_code
, 1, {
256 /* Calculate the old raw function pointer */
257 struct simple_fun
* fheaderp
=
258 (struct simple_fun
*)LOW_WORD((char*)nfheaderp
- displacement
);
259 /* Calculate the new lispobj */
260 lispobj nfheaderl
= make_lispobj(nfheaderp
, FUN_POINTER_LOWTAG
);
263 printf("fheaderp->header (at %x) <- %x\n",
264 &(fheaderp
->header
) , nfheaderl
);
266 set_forwarding_pointer((lispobj
*)fheaderp
, nfheaderl
);
268 /* fix self pointer. */
270 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
271 FUN_RAW_ADDR_OFFSET
+
275 #ifdef LISP_FEATURE_GENCGC
276 /* Cheneygc doesn't need this os_flush_icache, it flushes the whole
277 spaces once when all copying is done. */
278 os_flush_icache((os_vm_address_t
) (((sword_t
*)new_code
) + nheader_words
),
279 ncode_words
* sizeof(sword_t
));
283 #ifdef LISP_FEATURE_X86
284 gencgc_apply_code_fixups(code
, new_code
);
291 scav_code_header(lispobj
*where
, lispobj header
)
293 struct code
*code
= (struct code
*) where
;
294 sword_t n_header_words
= code_header_words(header
);
296 /* Scavenge the boxed section of the code data block. */
297 scavenge(where
+ 1, n_header_words
- 1);
299 /* Scavenge the boxed section of each function object in the
300 * code data block. */
301 for_each_simple_fun(i
, function_ptr
, code
, 1, {
302 scavenge(SIMPLE_FUN_SCAV_START(function_ptr
),
303 SIMPLE_FUN_SCAV_NWORDS(function_ptr
));
306 return n_header_words
+ code_instruction_words(code
->code_size
);
310 trans_code_header(lispobj object
)
312 struct code
*ncode
= trans_code((struct code
*) native_pointer(object
));
313 return (lispobj
) LOW_WORD(ncode
) | OTHER_POINTER_LOWTAG
;
317 size_code_header(lispobj
*where
)
319 return code_header_words(((struct code
*)where
)->header
)
320 + code_instruction_words(((struct code
*)where
)->code_size
);
323 #ifdef RETURN_PC_HEADER_WIDETAG
325 scav_return_pc_header(lispobj
*where
, lispobj object
)
327 lose("attempted to scavenge a return PC header where=%p object=%#lx\n",
328 where
, (uword_t
) object
);
329 return 0; /* bogus return value to satisfy static type checking */
333 trans_return_pc_header(lispobj object
)
335 struct simple_fun
*return_pc
= (struct simple_fun
*) native_pointer(object
);
336 uword_t offset
= HeaderValue(return_pc
->header
) * N_WORD_BYTES
;
338 /* Transport the whole code object */
339 struct code
*code
= (struct code
*) ((uword_t
) return_pc
- offset
);
340 struct code
*ncode
= trans_code(code
);
342 return ((lispobj
) LOW_WORD(ncode
) + offset
) | OTHER_POINTER_LOWTAG
;
344 #endif /* RETURN_PC_HEADER_WIDETAG */
346 /* On the 386, closures hold a pointer to the raw address instead of the
347 * function object, so we can use CALL [$FDEFN+const] to invoke
348 * the function without loading it into a register. Given that code
349 * objects don't move, we don't need to update anything, but we do
350 * have to figure out that the function is still live. */
352 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
354 scav_closure_header(lispobj
*where
, lispobj object
)
356 struct closure
*closure
= (struct closure
*)where
;
357 lispobj fun
= closure
->fun
- FUN_RAW_ADDR_OFFSET
;
359 #ifdef LISP_FEATURE_GENCGC
360 /* The function may have moved so update the raw address. But
361 * don't write unnecessarily. */
362 if (closure
->fun
!= fun
+ FUN_RAW_ADDR_OFFSET
)
363 closure
->fun
= fun
+ FUN_RAW_ADDR_OFFSET
;
369 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
371 scav_fun_header(lispobj
*where
, lispobj object
)
373 lose("attempted to scavenge a function header where=%p object=%#lx\n",
374 where
, (uword_t
) object
);
375 return 0; /* bogus return value to satisfy static type checking */
377 #endif /* LISP_FEATURE_X86 */
380 trans_fun_header(lispobj object
)
382 struct simple_fun
*fheader
= (struct simple_fun
*) native_pointer(object
);
383 uword_t offset
= HeaderValue(fheader
->header
) * N_WORD_BYTES
;
385 /* Transport the whole code object */
386 struct code
*code
= (struct code
*) ((uword_t
) fheader
- offset
);
387 struct code
*ncode
= trans_code(code
);
389 return ((lispobj
) LOW_WORD(ncode
) + offset
) | FUN_POINTER_LOWTAG
;
398 trans_instance(lispobj object
)
400 gc_dcheck(lowtag_of(object
) == INSTANCE_POINTER_LOWTAG
);
401 lispobj header
= *(lispobj
*)(object
- INSTANCE_POINTER_LOWTAG
);
402 return copy_object(object
, 1 + (instance_length(header
)|1));
406 scav_instance_pointer(lispobj
*where
, lispobj object
)
408 /* Object is a pointer into from space - not a FP. */
409 lispobj copy
= trans_instance(object
);
411 gc_dcheck(copy
!= object
);
413 set_forwarding_pointer(native_pointer(object
), copy
);
424 static lispobj
trans_list(lispobj object
);
427 scav_list_pointer(lispobj
*where
, lispobj object
)
429 gc_dcheck(lowtag_of(object
) == LIST_POINTER_LOWTAG
);
431 lispobj copy
= trans_list(object
);
432 gc_dcheck(copy
!= object
);
434 CHECK_COPY_POSTCONDITIONS(copy
, LIST_POINTER_LOWTAG
);
442 trans_list(lispobj object
)
445 struct cons
*copy
= (struct cons
*)
446 gc_general_alloc(sizeof(struct cons
), BOXED_PAGE_FLAG
, ALLOC_QUICK
);
447 lispobj new_list_pointer
= make_lispobj(copy
, LIST_POINTER_LOWTAG
);
448 copy
->car
= CONS(object
)->car
;
449 /* Grab the cdr: set_forwarding_pointer will clobber it in GENCGC */
450 lispobj cdr
= CONS(object
)->cdr
;
451 set_forwarding_pointer((lispobj
*)CONS(object
), new_list_pointer
);
453 /* Try to linearize the list in the cdr direction to help reduce
455 while (lowtag_of(cdr
) == LIST_POINTER_LOWTAG
&& from_space_p(cdr
)) {
456 lispobj
* native_cdr
= (lispobj
*)CONS(cdr
);
457 if (forwarding_pointer_p(native_cdr
)) { // Might as well fix now.
458 cdr
= forwarding_pointer_value(native_cdr
);
462 struct cons
*cdr_copy
= (struct cons
*)
463 gc_general_alloc(sizeof(struct cons
), BOXED_PAGE_FLAG
, ALLOC_QUICK
);
464 cdr_copy
->car
= ((struct cons
*)native_cdr
)->car
;
465 /* Grab the cdr before it is clobbered. */
466 lispobj next
= ((struct cons
*)native_cdr
)->cdr
;
467 /* Set cdr of the predecessor, and store an FP. */
468 set_forwarding_pointer(native_cdr
,
469 copy
->cdr
= make_lispobj(cdr_copy
,
470 LIST_POINTER_LOWTAG
));
475 return new_list_pointer
;
480 * scavenging and transporting other pointers
484 scav_other_pointer(lispobj
*where
, lispobj object
)
486 gc_dcheck(lowtag_of(object
) == OTHER_POINTER_LOWTAG
);
488 /* Object is a pointer into from space - not FP. */
489 lispobj
*first_pointer
= (lispobj
*)(object
- OTHER_POINTER_LOWTAG
);
490 lispobj copy
= transother
[widetag_of(*first_pointer
)](object
);
492 // If the object was large, then instead of transporting it,
493 // gencgc might simply promote the pages and return the same pointer.
494 // That decision is made in general_copy_large_object().
495 if (copy
!= object
) {
496 set_forwarding_pointer(first_pointer
, copy
);
497 #ifdef LISP_FEATURE_GENCGC
501 #ifndef LISP_FEATURE_GENCGC
504 CHECK_COPY_POSTCONDITIONS(copy
, OTHER_POINTER_LOWTAG
);
509 * immediate, boxed, and unboxed objects
513 scav_immediate(lispobj
*where
, lispobj object
)
519 trans_immediate(lispobj object
)
521 lose("trying to transport an immediate\n");
522 return NIL
; /* bogus return value to satisfy static type checking */
526 size_immediate(lispobj
*where
)
532 boolean
positive_bignum_logbitp(int index
, struct bignum
* bignum
)
534 /* If the bignum in the layout has another pointer to it (besides the layout)
535 acting as a root, and which is scavenged first, then transporting the
536 bignum causes the layout to see a FP, as would copying an instance whose
537 layout that is. This is a nearly impossible scenario to create organically
538 in Lisp, because mostly nothing ever looks again at that exact (EQ) bignum
539 except for a few things that would cause it to be pinned anyway,
540 such as it being kept in a local variable during structure manipulation.
541 See 'interleaved-raw.impure.lisp' for a way to trigger this */
542 if (forwarding_pointer_p((lispobj
*)bignum
)) {
543 lispobj forwarded
= forwarding_pointer_value((lispobj
*)bignum
);
545 fprintf(stderr
, "GC bignum_logbitp(): fwd from %p to %p\n",
546 (void*)bignum
, (void*)forwarded
);
548 bignum
= (struct bignum
*)native_pointer(forwarded
);
551 int len
= HeaderValue(bignum
->header
);
552 int word_index
= index
/ N_WORD_BITS
;
553 int bit_index
= index
% N_WORD_BITS
;
554 if (word_index
>= len
) {
555 // just return 0 since the marking logic does not allow negative bignums
558 return (bignum
->digits
[word_index
] >> bit_index
) & 1;
562 struct instance_scanner
{
564 void (*proc
)(lispobj
*, sword_t
);
567 // Helper function for helper function below, since lambda isn't a thing
568 static void instance_scan_range(void* arg
, int offset
, int nwords
)
570 struct instance_scanner
*scanner
= (struct instance_scanner
*)arg
;
571 scanner
->proc(scanner
->base
+ offset
, nwords
);
574 // Helper function for stepping through the tagged slots of an instance in
575 // scav_instance and verify_space.
577 instance_scan(void (*proc
)(lispobj
*, sword_t
),
578 lispobj
*instance_slots
,
580 lispobj layout_bitmap
)
584 /* This code might be made more efficient by run-length-encoding the ranges
585 of words to scan, but probably not by much */
587 if (fixnump(layout_bitmap
)) {
588 sword_t bitmap
= (sword_t
)layout_bitmap
>> N_FIXNUM_TAG_BITS
; // signed integer!
589 for (index
= 0; index
< n_words
; index
++, bitmap
>>= 1)
591 proc(instance_slots
+ index
, 1);
592 } else { /* huge bitmap */
593 struct bignum
* bitmap
;
594 bitmap
= (struct bignum
*)native_pointer(layout_bitmap
);
595 if (forwarding_pointer_p((lispobj
*)bitmap
))
596 bitmap
= (struct bignum
*)
597 native_pointer(forwarding_pointer_value((lispobj
*)bitmap
));
598 struct instance_scanner scanner
;
599 scanner
.base
= instance_slots
;
601 bitmap_scan((uword_t
*)bitmap
->digits
, HeaderValue(bitmap
->header
), 0,
602 instance_scan_range
, &scanner
);
606 void bitmap_scan(uword_t
* bitmap
, int n_bitmap_words
, int flags
,
607 void (*proc
)(void*, int, int), void* arg
)
609 uword_t sense
= (flags
& BIT_SCAN_INVERT
) ? ~0L : 0;
610 int start_word_index
= 0;
612 in_use_marker_t word
;
614 flags
= flags
& BIT_SCAN_CLEAR
;
616 // Rather than bzero'ing we can just clear each nonzero word as it's read,
618 #define BITMAP_REF(j) word = bitmap[j]; if(word && flags) bitmap[j] = 0; word ^= sense
621 int skip_bits
, start_bit
, start_position
, run_length
;
623 if (++start_word_index
>= n_bitmap_words
) break;
624 BITMAP_REF(start_word_index
);
628 // On each loop iteration, the lowest 1 bit is a "relative"
629 // bit index, since the word was already shifted. This is 'skip_bits'.
630 // Adding back in the total shift amount gives 'start_bit',
631 // the true absolute index within the current word.
632 // 'start_position' is absolute within the entire bitmap.
633 skip_bits
= ffsl(word
) - 1;
634 start_bit
= skip_bits
+ shift
;
635 start_position
= N_WORD_BITS
* start_word_index
+ start_bit
;
636 // Compute the number of consecutive 1s in the current word.
638 run_length
= ~word
? ffsl(~word
) - 1 : N_WORD_BITS
;
639 if (start_bit
+ run_length
< N_WORD_BITS
) { // Do not extend to additional words.
641 shift
+= skip_bits
+ run_length
;
643 int end_word_index
= ++start_word_index
;
645 if (end_word_index
>= n_bitmap_words
) {
647 run_length
+= (end_word_index
- start_word_index
) * N_WORD_BITS
;
650 BITMAP_REF(end_word_index
);
654 // end_word_index is the exclusive bound on contiguous
655 // words to include in the range. See if the low bits
656 // from the next word can extend the range.
657 shift
= ffsl(~word
) - 1;
659 run_length
+= (end_word_index
- start_word_index
) * N_WORD_BITS
664 start_word_index
= end_word_index
;
666 proc(arg
, start_position
, run_length
);
672 scav_instance(lispobj
*where
, lispobj header
)
674 lispobj
* layout
= (lispobj
*)instance_layout(where
);
677 layout
= native_pointer((lispobj
)layout
);
678 #ifdef LISP_FEATURE_COMPACT_INSTANCE_HEADER
679 if (__immobile_obj_gen_bits(layout
) == from_space
)
680 promote_immobile_obj(layout
, 1);
682 if (forwarding_pointer_p(layout
))
683 layout
= native_pointer(forwarding_pointer_value(layout
));
686 sword_t nslots
= instance_length(header
) | 1;
687 lispobj lbitmap
= ((struct layout
*)layout
)->bitmap
;
688 if (lbitmap
== make_fixnum(-1))
689 scavenge(where
+1, nslots
);
690 else if (!fixnump(lbitmap
))
691 instance_scan((void(*)(lispobj
*,sword_t
))scavenge
,
692 where
+1, nslots
, lbitmap
);
694 sword_t bitmap
= (sword_t
)lbitmap
>> N_FIXNUM_TAG_BITS
; // signed integer!
697 for ( ; n
-- ; bitmap
>>= 1) {
699 if ((bitmap
& 1) && is_lisp_pointer(obj
= *where
))
706 #ifdef LISP_FEATURE_COMPACT_INSTANCE_HEADER
708 scav_funinstance(lispobj
*where
, lispobj header
)
710 // This works because the layout is in the header word of all instances,
711 // ordinary and funcallable, when compact headers are enabled.
712 // The trampoline slot in the funcallable-instance is raw, but can be
713 // scavenged, because it points to readonly space, never oldspace.
714 // (And for certain backends it looks like a fixnum, not a pointer)
715 return scav_instance(where
, header
);
719 //// Boxed object scav/trans/size functions
721 /// These sizing macros return the number of *payload* words,
722 /// exclusive of the object header word. Payload length is always
723 /// an odd number so that total word count is an even number.
724 #define BOXED_NWORDS(obj) (HeaderValue(obj) | 1)
725 // Payload count expressed in 15 bits
726 #define SHORT_BOXED_NWORDS(obj) ((HeaderValue(obj) & SHORT_HEADER_MAX_WORDS) | 1)
727 // Payload count expressed in 8 bits
728 #define TINY_BOXED_NWORDS(obj) ((HeaderValue(obj) & 0xFF) | 1)
730 #define DEF_SCAV_BOXED(suffix, sizer) \
731 static sword_t __attribute__((unused)) \
732 scav_##suffix(lispobj *where, lispobj header) { \
733 return scavenge(where+1, sizer(header)); \
735 static lispobj trans_##suffix(lispobj object) { \
736 return copy_object(object, 1 + sizer(*native_pointer(object))); \
738 static sword_t size_##suffix(lispobj *where) { return 1 + sizer(*where); }
740 DEF_SCAV_BOXED(boxed
, BOXED_NWORDS
)
741 DEF_SCAV_BOXED(short_boxed
, SHORT_BOXED_NWORDS
)
742 DEF_SCAV_BOXED(tiny_boxed
, TINY_BOXED_NWORDS
)
744 /* Note: on the sparc we don't have to do anything special for fdefns, */
745 /* 'cause the raw-addr has a function lowtag. */
746 #if !defined(LISP_FEATURE_SPARC) && !defined(LISP_FEATURE_ARM)
748 scav_fdefn(lispobj
*where
, lispobj object
)
750 struct fdefn
*fdefn
= (struct fdefn
*)where
;
752 /* FSHOW((stderr, "scav_fdefn, function = %p, raw_addr = %p\n",
753 fdefn->fun, fdefn->raw_addr)); */
755 scavenge(where
+ 1, 2); // 'name' and 'fun'
756 #ifndef LISP_FEATURE_IMMOBILE_CODE
757 lispobj raw_fun
= (lispobj
)fdefn
->raw_addr
;
758 if (raw_fun
> READ_ONLY_SPACE_END
) {
759 lispobj simple_fun
= raw_fun
- FUN_RAW_ADDR_OFFSET
;
760 scavenge(&simple_fun
, 1);
761 /* Don't write unnecessarily. */
762 if (simple_fun
!= raw_fun
- FUN_RAW_ADDR_OFFSET
)
763 fdefn
->raw_addr
= (char *)simple_fun
+ FUN_RAW_ADDR_OFFSET
;
765 #elif defined(LISP_FEATURE_X86_64)
766 lispobj obj
= fdefn_raw_referent(fdefn
);
769 scavenge(&new, 1); // enliven
770 gc_dcheck(new == obj
); // must not move
773 # error "Need to implement scav_fdefn"
780 scav_unboxed(lispobj
*where
, lispobj object
)
782 sword_t length
= HeaderValue(object
) + 1;
783 return CEILING(length
, 2);
787 trans_unboxed(lispobj object
)
789 gc_dcheck(lowtag_of(object
) == OTHER_POINTER_LOWTAG
);
790 sword_t length
= HeaderValue(*native_pointer(object
)) + 1;
791 return copy_unboxed_object(object
, CEILING(length
, 2));
794 /* vector-like objects */
796 trans_vector(lispobj object
)
798 gc_dcheck(lowtag_of(object
) == OTHER_POINTER_LOWTAG
);
801 fixnum_value(((struct vector
*)native_pointer(object
))->length
);
802 return copy_large_object(object
, CEILING(length
+ 2, 2));
806 size_vector(lispobj
*where
)
808 sword_t length
= fixnum_value(((struct vector
*)where
)->length
);
809 return CEILING(length
+ 2, 2);
812 #define DEF_SCAV_TRANS_SIZE_UB(nbits) \
813 DEF_SPECIALIZED_VECTOR(vector_unsigned_byte_##nbits, NWORDS(length, nbits))
814 #define DEF_SPECIALIZED_VECTOR(name, nwords) \
815 static sword_t __attribute__((unused)) scav_##name(lispobj *where, lispobj header) { \
816 sword_t length = fixnum_value(((struct vector*)where)->length); \
817 return CEILING(nwords + 2, 2); \
819 static lispobj __attribute__((unused)) trans_##name(lispobj object) { \
820 gc_dcheck(lowtag_of(object) == OTHER_POINTER_LOWTAG); \
821 sword_t length = fixnum_value(((struct vector*)(object-OTHER_POINTER_LOWTAG))->length); \
822 return copy_large_unboxed_object(object, CEILING(nwords + 2, 2)); \
824 static sword_t __attribute__((unused)) size_##name(lispobj *where) { \
825 sword_t length = fixnum_value(((struct vector*)where)->length); \
826 return CEILING(nwords + 2, 2); \
829 DEF_SPECIALIZED_VECTOR(vector_nil
, 0*length
)
830 DEF_SPECIALIZED_VECTOR(vector_bit
, NWORDS(length
,1))
831 /* NOTE: strings contain one more element of data (a terminating '\0'
832 * to help interface with C functions) than indicated by the length slot.
833 * This is true even for UCS4 strings, despite that C APIs are unlikely
834 * to have a convention that expects 4 zero bytes. */
835 DEF_SPECIALIZED_VECTOR(base_string
, NWORDS((length
+1), 8))
836 DEF_SPECIALIZED_VECTOR(character_string
, NWORDS((length
+1), 32))
837 DEF_SCAV_TRANS_SIZE_UB(2)
838 DEF_SCAV_TRANS_SIZE_UB(4)
839 DEF_SCAV_TRANS_SIZE_UB(8)
840 DEF_SCAV_TRANS_SIZE_UB(16)
841 DEF_SCAV_TRANS_SIZE_UB(32)
842 DEF_SCAV_TRANS_SIZE_UB(64)
843 DEF_SCAV_TRANS_SIZE_UB(128)
844 #ifdef LONG_FLOAT_SIZE
845 DEF_SPECIALIZED_VECTOR(vector_long_float
, length
* LONG_FLOAT_SIZE
)
846 DEF_SPECIALIZED_VECTOR(vector_complex_long_float
, length
* (2 * LONG_FLOAT_SIZE
))
850 trans_weak_pointer(lispobj object
)
853 gc_dcheck(lowtag_of(object
) == OTHER_POINTER_LOWTAG
);
855 #if defined(DEBUG_WEAK)
856 printf("Transporting weak pointer from 0x%08x\n", object
);
859 /* Need to remember where all the weak pointers are that have */
860 /* been transported so they can be fixed up in a post-GC pass. */
862 copy
= copy_object(object
, WEAK_POINTER_NWORDS
);
863 #ifndef LISP_FEATURE_GENCGC
864 struct weak_pointer
*wp
= (struct weak_pointer
*) native_pointer(copy
);
866 gc_dcheck(widetag_of(wp
->header
)==WEAK_POINTER_WIDETAG
);
867 /* Push the weak pointer onto the list of weak pointers. */
868 if (weak_pointer_breakable_p(wp
)) {
869 wp
->next
= (struct weak_pointer
*)LOW_WORD(weak_pointers
);
876 void scan_weak_pointers(void)
878 struct weak_pointer
*wp
, *next_wp
;
879 for (wp
= weak_pointers
, next_wp
= NULL
; wp
!= NULL
; wp
= next_wp
) {
880 gc_assert(widetag_of(wp
->header
)==WEAK_POINTER_WIDETAG
);
884 if (next_wp
== wp
) /* gencgc uses a ref to self for end of list */
887 gc_assert(is_lisp_pointer(wp
->value
));
888 lispobj
*value
= native_pointer(wp
->value
);
890 /* Now, we need to check whether the object has been forwarded. If
891 * it has been, the weak pointer is still good and needs to be
892 * updated. Otherwise, the weak pointer needs to be broken. */
894 if (from_space_p((lispobj
)value
)) {
895 wp
->value
= forwarding_pointer_p(value
) ?
896 LOW_WORD(forwarding_pointer_value(value
)) : UNBOUND_MARKER_WIDETAG
;
898 #ifdef LISP_FEATURE_IMMOBILE_SPACE
899 else if (immobile_space_p((lispobj
)value
) &&
900 immobile_obj_gen_bits(value
) == from_space
) {
901 wp
->value
= UNBOUND_MARKER_WIDETAG
;
905 lose("unbreakable pointer %p", wp
);
912 #if N_WORD_BITS == 32
913 #define EQ_HASH_MASK 0x1fffffff
914 #elif N_WORD_BITS == 64
915 #define EQ_HASH_MASK 0x1fffffffffffffff
918 /* Compute the EQ-hash of KEY. This must match POINTER-HASH in
919 * target-hash-table.lisp. */
920 #define EQ_HASH(key) ((key) & EQ_HASH_MASK)
922 /* List of weak hash tables chained through their NEXT-WEAK-HASH-TABLE
923 * slot. Set to NULL at the end of a collection.
925 * This is not optimal because, when a table is tenured, it won't be
926 * processed automatically; only the yougest generation is GC'd by
927 * default. On the other hand, all applications will need an
928 * occasional full GC anyway, so it's not that bad either. */
929 struct hash_table
*weak_hash_tables
= NULL
;
931 /* Return true if OBJ has already survived the current GC. */
932 static inline int pointer_survived_gc_yet(lispobj obj
)
934 #ifdef LISP_FEATURE_CHENEYGC
935 // This is the most straightforward definition.
936 return (!from_space_p(obj
) || forwarding_pointer_p(native_pointer(obj
)));
938 /* Check for a pointer to dynamic space before considering immobile space.
939 Based on the relative size of the spaces, this should be a win because
940 if the object is in the dynamic space and not the 'from' generation
941 we don't want to test immobile_space_p() at all.
942 Additionally, pinned_p() is both more expensive and less likely than
943 forwarding_pointer_p(), so we want to reverse those conditions, which
944 would not be possible with pinned_p() buried inside from_space_p(). */
945 page_index_t page_index
= find_page_index((void*)obj
);
947 return page_table
[page_index
].gen
!= from_space
||
948 forwarding_pointer_p(native_pointer(obj
)) ||
949 pinned_p(obj
, page_index
);
950 #ifdef LISP_FEATURE_IMMOBILE_SPACE
951 if (immobile_space_p(obj
))
952 return immobile_obj_gen_bits(native_pointer(obj
)) != from_space
;
958 #ifdef EMPTY_HT_SLOT /* only if it's a static symbol */
959 // "ish" because EMPTY_HT_SLOT is of course a pointer.
960 # define ht_cell_nonpointerish(x) (!is_lisp_pointer(x) || x==EMPTY_HT_SLOT)
962 # define ht_cell_nonpointerish(x) !is_lisp_pointer(x)
965 static int survived_gc_yet_KEY(lispobj key
, lispobj value
) {
966 return ht_cell_nonpointerish(key
) || pointer_survived_gc_yet(key
);
968 static int survived_gc_yet_VALUE(lispobj key
, lispobj value
) {
969 return ht_cell_nonpointerish(value
) || pointer_survived_gc_yet(value
);
971 static int survived_gc_yet_AND(lispobj key
, lispobj value
) {
972 int key_nonpointer
= ht_cell_nonpointerish(key
);
973 int val_nonpointer
= ht_cell_nonpointerish(value
);
974 if (key_nonpointer
&& val_nonpointer
) return 1;
975 return (key_nonpointer
|| pointer_survived_gc_yet(key
))
976 && (val_nonpointer
|| pointer_survived_gc_yet(value
));
978 static int survived_gc_yet_OR(lispobj key
, lispobj value
) {
979 int key_nonpointer
= ht_cell_nonpointerish(key
);
980 int val_nonpointer
= ht_cell_nonpointerish(value
);
981 if (key_nonpointer
|| val_nonpointer
) return 1;
982 // Both MUST be pointers
983 return pointer_survived_gc_yet(key
) || pointer_survived_gc_yet(value
);
986 static int (*weak_hash_entry_alivep_fun(lispobj weakness
))(lispobj
,lispobj
)
989 case KEY
: return survived_gc_yet_KEY
;
990 case VALUE
: return survived_gc_yet_VALUE
;
991 case KEY_OR_VALUE
: return survived_gc_yet_OR
;
992 case KEY_AND_VALUE
: return survived_gc_yet_AND
;
993 case NIL
: return NULL
;
994 default: lose("Bad hash table weakness");
998 /* Return the beginning of data in ARRAY (skipping the header and the
999 * length) or NULL if it isn't an array of the specified widetag after
1001 static inline lispobj
*
1002 get_array_data (lispobj array
, int widetag
, uword_t
*length
)
1004 if (is_lisp_pointer(array
) && widetag_of(*native_pointer(array
)) == widetag
) {
1006 *length
= fixnum_value(native_pointer(array
)[1]);
1007 return native_pointer(array
) + 2;
1013 /* Only need to worry about scavenging the _real_ entries in the
1014 * table. Phantom entries such as the hash table itself at index 0 and
1015 * the empty marker at index 1 were scavenged by scav_vector that
1016 * either called this function directly or arranged for it to be
1017 * called later by pushing the hash table onto weak_hash_tables. */
1019 scav_hash_table_entries (struct hash_table
*hash_table
)
1023 lispobj
*index_vector
;
1025 lispobj
*next_vector
;
1026 uword_t next_vector_length
;
1027 lispobj
*hash_vector
;
1028 uword_t hash_vector_length
;
1029 lispobj empty_symbol
;
1030 lispobj weakness
= hash_table
->weakness
;
1033 kv_vector
= get_array_data(hash_table
->table
,
1034 SIMPLE_VECTOR_WIDETAG
, &kv_length
);
1035 if (kv_vector
== NULL
)
1036 lose("invalid kv_vector %x\n", hash_table
->table
);
1038 index_vector
= get_array_data(hash_table
->index_vector
,
1039 SIMPLE_ARRAY_WORD_WIDETAG
, &length
);
1040 if (index_vector
== NULL
)
1041 lose("invalid index_vector %x\n", hash_table
->index_vector
);
1043 next_vector
= get_array_data(hash_table
->next_vector
,
1044 SIMPLE_ARRAY_WORD_WIDETAG
,
1045 &next_vector_length
);
1046 if (next_vector
== NULL
)
1047 lose("invalid next_vector %x\n", hash_table
->next_vector
);
1049 hash_vector
= get_array_data(hash_table
->hash_vector
,
1050 SIMPLE_ARRAY_WORD_WIDETAG
,
1051 &hash_vector_length
);
1052 if (hash_vector
!= NULL
)
1053 gc_assert(hash_vector_length
== next_vector_length
);
1055 /* These lengths could be different as the index_vector can be a
1056 * different length from the others, a larger index_vector could
1057 * help reduce collisions. */
1058 gc_assert(next_vector_length
*2 == kv_length
);
1060 empty_symbol
= kv_vector
[1];
1061 /* fprintf(stderr,"* empty_symbol = %x\n", empty_symbol);*/
1062 if (widetag_of(*native_pointer(empty_symbol
)) != SYMBOL_HEADER_WIDETAG
) {
1063 lose("not a symbol where empty-hash-table-slot symbol expected: %x\n",
1064 *native_pointer(empty_symbol
));
1067 /* Work through the KV vector. */
1068 int (*alivep_test
)(lispobj
,lispobj
) = weak_hash_entry_alivep_fun(weakness
);
1069 #define SCAV_ENTRIES(aliveness_predicate) \
1070 for (i = 1; i < next_vector_length; i++) { \
1071 lispobj old_key = kv_vector[2*i]; \
1072 lispobj __attribute__((unused)) value = kv_vector[2*i+1]; \
1073 if (aliveness_predicate) { \
1074 /* Scavenge the key and value. */ \
1075 scavenge(&kv_vector[2*i], 2); \
1076 /* If an EQ-based key has moved, mark the hash-table for rehash */ \
1077 if (!hash_vector || hash_vector[i] == MAGIC_HASH_VECTOR_VALUE) { \
1078 lispobj new_key = kv_vector[2*i]; \
1079 if (old_key != new_key && new_key != empty_symbol) \
1080 hash_table->needs_rehash_p = T; \
1083 SCAV_ENTRIES(alivep_test(old_key
, value
))
1089 scav_vector (lispobj
*where
, lispobj object
)
1092 struct hash_table
*hash_table
;
1094 /* SB-VM:VECTOR-VALID-HASHING-SUBTYPE is set for EQ-based and weak
1095 * hash tables in the Lisp HASH-TABLE code to indicate need for
1096 * special GC support. */
1097 if ((HeaderValue(object
) & 0xFF) == subtype_VectorNormal
) {
1098 sword_t length
= fixnum_value(((struct vector
*)where
)->length
);
1099 scavenge(where
+ 2, length
);
1100 return CEILING(length
+ 2, 2);
1103 kv_length
= fixnum_value(where
[1]);
1104 /*FSHOW((stderr,"/kv_length = %d\n", kv_length));*/
1106 /* Scavenge element 0, which may be a hash-table structure. */
1107 scavenge(where
+2, 1);
1108 if (!is_lisp_pointer(where
[2])) {
1109 /* This'll happen when REHASH clears the header of old-kv-vector
1110 * and fills it with zero, but some other thread simulatenously
1111 * sets the header in %%PUTHASH.
1114 "Warning: no pointer at %p in hash table: this indicates "
1115 "non-fatal corruption caused by concurrent access to a "
1116 "hash-table from multiple threads. Any accesses to "
1117 "hash-tables shared between threads should be protected "
1118 "by locks.\n", (void*)&where
[2]);
1119 // We've scavenged three words.
1122 hash_table
= (struct hash_table
*)native_pointer(where
[2]);
1123 /*FSHOW((stderr,"/hash_table = %x\n", hash_table));*/
1124 if (widetag_of(hash_table
->header
) != INSTANCE_HEADER_WIDETAG
) {
1125 lose("hash table not instance (%x at %x)\n",
1130 /* Scavenge element 1, which should be some internal symbol that
1131 * the hash table code reserves for marking empty slots. */
1132 scavenge(where
+3, 1);
1133 if (!is_lisp_pointer(where
[3])) {
1134 lose("not empty-hash-table-slot symbol pointer: %x\n", where
[3]);
1137 /* Scavenge hash table, which will fix the positions of the other
1138 * needed objects. */
1139 scavenge((lispobj
*)hash_table
,
1140 CEILING(sizeof(struct hash_table
) / sizeof(lispobj
), 2));
1142 /* Cross-check the kv_vector. */
1143 if (where
!= native_pointer(hash_table
->table
)) {
1144 lose("hash_table table!=this table %x\n", hash_table
->table
);
1147 if (hash_table
->weakness
== NIL
) {
1148 scav_hash_table_entries(hash_table
);
1150 /* Delay scavenging of this table by pushing it onto
1151 * weak_hash_tables (if it's not there already) for the weak
1153 if (hash_table
->next_weak_hash_table
== NIL
) {
1154 hash_table
->next_weak_hash_table
= (lispobj
)weak_hash_tables
;
1155 weak_hash_tables
= hash_table
;
1159 return (CEILING(kv_length
+ 2, 2));
1163 scav_weak_hash_tables (void)
1165 struct hash_table
*table
;
1167 /* Scavenge entries whose triggers are known to survive. */
1168 for (table
= weak_hash_tables
; table
!= NULL
;
1169 table
= (struct hash_table
*)table
->next_weak_hash_table
) {
1170 scav_hash_table_entries(table
);
1174 /* Walk through the chain whose first element is *FIRST and remove
1175 * dead weak entries. */
1177 scan_weak_hash_table_chain (struct hash_table
*hash_table
, lispobj
*prev
,
1178 lispobj
*kv_vector
, lispobj
*index_vector
,
1179 lispobj
*next_vector
, lispobj
*hash_vector
,
1180 lispobj empty_symbol
, int (*alivep_test
)(lispobj
,lispobj
))
1182 unsigned index
= *prev
;
1184 unsigned next
= next_vector
[index
];
1185 lispobj key
= kv_vector
[2 * index
];
1186 lispobj value
= kv_vector
[2 * index
+ 1];
1187 gc_assert(key
!= empty_symbol
);
1188 gc_assert(value
!= empty_symbol
);
1189 if (!alivep_test(key
, value
)) {
1190 unsigned count
= fixnum_value(hash_table
->number_entries
);
1191 gc_assert(count
> 0);
1193 hash_table
->number_entries
= make_fixnum(count
- 1);
1194 next_vector
[index
] = fixnum_value(hash_table
->next_free_kv
);
1195 hash_table
->next_free_kv
= make_fixnum(index
);
1196 kv_vector
[2 * index
] = empty_symbol
;
1197 kv_vector
[2 * index
+ 1] = empty_symbol
;
1199 hash_vector
[index
] = MAGIC_HASH_VECTOR_VALUE
;
1201 prev
= &next_vector
[index
];
1208 scan_weak_hash_table (struct hash_table
*hash_table
)
1211 lispobj
*index_vector
;
1212 uword_t length
= 0; /* prevent warning */
1213 lispobj
*next_vector
;
1214 uword_t next_vector_length
= 0; /* prevent warning */
1215 lispobj
*hash_vector
;
1216 lispobj empty_symbol
;
1217 lispobj weakness
= hash_table
->weakness
;
1220 kv_vector
= get_array_data(hash_table
->table
,
1221 SIMPLE_VECTOR_WIDETAG
, NULL
);
1222 index_vector
= get_array_data(hash_table
->index_vector
,
1223 SIMPLE_ARRAY_WORD_WIDETAG
, &length
);
1224 next_vector
= get_array_data(hash_table
->next_vector
,
1225 SIMPLE_ARRAY_WORD_WIDETAG
,
1226 &next_vector_length
);
1227 hash_vector
= get_array_data(hash_table
->hash_vector
,
1228 SIMPLE_ARRAY_WORD_WIDETAG
, NULL
);
1229 empty_symbol
= kv_vector
[1];
1231 for (i
= 0; i
< length
; i
++) {
1232 scan_weak_hash_table_chain(hash_table
, &index_vector
[i
],
1233 kv_vector
, index_vector
, next_vector
,
1234 hash_vector
, empty_symbol
,
1235 weak_hash_entry_alivep_fun(weakness
));
1239 /* Remove dead entries from weak hash tables. */
1241 scan_weak_hash_tables (void)
1243 struct hash_table
*table
, *next
;
1245 for (table
= weak_hash_tables
; table
!= NULL
; table
= next
) {
1246 next
= (struct hash_table
*)table
->next_weak_hash_table
;
1247 table
->next_weak_hash_table
= NIL
;
1248 scan_weak_hash_table(table
);
1251 weak_hash_tables
= NULL
;
1260 scav_lose(lispobj
*where
, lispobj object
)
1262 lose("no scavenge function for object %p (widetag 0x%x)\n",
1264 widetag_of(*where
));
1266 return 0; /* bogus return value to satisfy static type checking */
1270 trans_lose(lispobj object
)
1272 lose("no transport function for object %p (widetag 0x%x)\n",
1274 widetag_of(*native_pointer(object
)));
1275 return NIL
; /* bogus return value to satisfy static type checking */
1279 size_lose(lispobj
*where
)
1281 lose("no size function for object at %p (widetag 0x%x)\n",
1283 widetag_of(*where
));
1284 return 1; /* bogus return value to satisfy static type checking */
1292 #include "genesis/gc-tables.h"
1295 static lispobj
*search_spaces(void *pointer
)
1298 if (((start
= search_dynamic_space(pointer
)) != NULL
) ||
1299 #ifdef LISP_FEATURE_IMMOBILE_SPACE
1300 ((start
= search_immobile_space(pointer
)) != NULL
) ||
1302 ((start
= search_static_space(pointer
)) != NULL
) ||
1303 ((start
= search_read_only_space(pointer
)) != NULL
))
1308 /* Find the code object for the given pc, or return NULL on
1311 component_ptr_from_pc(lispobj
*pc
)
1313 lispobj
*object
= search_spaces(pc
);
1315 if (object
!= NULL
&& widetag_of(*object
) == CODE_HEADER_WIDETAG
)
1321 /* Scan an area looking for an object which encloses the given pointer.
1322 * Return the object start on success, or NULL on failure. */
1324 gc_search_space3(void *pointer
, lispobj
*start
, void *limit
)
1326 if (pointer
< (void*)start
|| pointer
>= limit
) return NULL
;
1330 /* CAUTION: this code is _significantly_ slower than the production version
1331 due to the extra checks for forwarding. Only use it if debugging */
1332 for ( ; (void*)start
< limit
; start
+= count
) {
1333 lispobj
*forwarded_start
;
1334 if (forwarding_pointer_p(start
))
1335 forwarded_start
= native_pointer(forwarding_pointer_value(start
));
1337 forwarded_start
= start
;
1338 lispobj thing
= *forwarded_start
;
1339 count
= is_cons_half(thing
) ? 2 : sizetab
[widetag_of(thing
)](forwarded_start
);
1340 /* Check whether the pointer is within this object. */
1341 if (pointer
< (void*)(start
+count
)) return start
;
1344 for ( ; (void*)start
< limit
; start
+= count
) {
1345 lispobj thing
= *start
;
1346 count
= is_cons_half(thing
) ? 2 : sizetab
[widetag_of(thing
)](start
);
1347 /* Check whether the pointer is within this object. */
1348 if (pointer
< (void*)(start
+count
)) return start
;
1354 /* Helper for valid_lisp_pointer_p (below) and
1355 * conservative_root_p (gencgc).
1357 * pointer is the pointer to check validity of,
1358 * and start_addr is the address of the enclosing object.
1360 * This is actually quite simple to check: because the heap state is assumed
1361 * consistent, and 'start_addr' is known good, having come from
1362 * gc_search_space(), only the 'pointer' argument is dubious.
1363 * So make 'start_addr' into a tagged pointer and see if that matches 'pointer'.
1364 * If it does, then 'pointer' is valid.
1367 properly_tagged_p_internal(lispobj pointer
, lispobj
*start_addr
)
1369 // If a headerless object, confirm that 'pointer' is a list pointer.
1370 // Given the precondition that the heap is in a valid state,
1371 // it may be assumed that one check of is_cons_half() suffices;
1372 // we don't need to check the other half.
1373 lispobj header
= *start_addr
;
1374 if (is_cons_half(header
))
1375 return make_lispobj(start_addr
, LIST_POINTER_LOWTAG
) == pointer
;
1377 // Because this heap object was not deemed to be a cons,
1378 // it must be an object header. Don't need a check except when paranoid.
1379 gc_dcheck(other_immediate_lowtag_p(header
));
1381 // The space of potential widetags has 64 elements, not 256,
1382 // because of the constant low 2 bits.
1383 int widetag
= widetag_of(header
);
1384 int lowtag
= lowtag_for_widetag
[widetag
>>2];
1385 if (lowtag
&& make_lispobj(start_addr
, lowtag
) == pointer
)
1386 return 1; // instant win
1388 if (widetag
== CODE_HEADER_WIDETAG
) {
1389 // Check for RETURN_PC_HEADER first since it's quicker.
1390 // Then consider the embedded simple-funs.
1391 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
1392 /* The all-architecture test below is good as far as it goes,
1393 * but an LRA object is similar to a FUN-POINTER: It is
1394 * embedded within a CODE-OBJECT pointed to by start_addr, and
1395 * cannot be found by simply walking the heap, therefore we
1396 * need to check for it. -- AB, 2010-Jun-04 */
1397 if (lowtag_of(pointer
) == OTHER_POINTER_LOWTAG
) {
1398 lispobj
*potential_lra
= native_pointer(pointer
);
1399 if ((widetag_of(potential_lra
[0]) == RETURN_PC_HEADER_WIDETAG
) &&
1400 ((potential_lra
- HeaderValue(potential_lra
[0])) == start_addr
)) {
1401 return 1; /* It's as good as we can verify. */
1405 if (lowtag_of(pointer
) == FUN_POINTER_LOWTAG
) {
1406 struct simple_fun
*pfun
=
1407 (struct simple_fun
*)(pointer
-FUN_POINTER_LOWTAG
);
1408 for_each_simple_fun(i
, function
, (struct code
*)start_addr
, 0, {
1409 if (pfun
== function
) return 1;
1413 return 0; // no good
1416 /* META: Note the ambiguous word "validate" in the comment below.
1417 * This means "Decide whether <x> is valid".
1418 * But when you see os_validate() elsewhere, that doesn't mean to ask
1419 * whether something is valid, it says to *make* it valid.
1420 * I think it would be nice if we could avoid using the word in the
1421 * sense in which os_validate() uses it, which would entail renaming
1422 * a bunch of stuff, which is harder than just explaining why
1423 * the comments can be deceptive */
1425 /* Used by the debugger to validate possibly bogus pointers before
1426 * calling MAKE-LISP-OBJ on them.
1428 * FIXME: We would like to make this perfect, because if the debugger
1429 * constructs a reference to a bugs lisp object, and it ends up in a
1430 * location scavenged by the GC all hell breaks loose.
1432 * Whereas conservative_root_p has to be conservative
1433 * and return true for all valid pointers, this could actually be eager
1434 * and lie about a few pointers without bad results... but that should
1435 * be reflected in the name.
1438 valid_lisp_pointer_p(lispobj pointer
)
1440 lispobj
*start
= search_spaces((void*)pointer
);
1442 return properly_tagged_descriptor_p((void*)pointer
, start
);
1447 maybe_gc(os_context_t
*context
)
1449 lispobj gc_happened
;
1450 struct thread
*thread
= arch_os_get_current_thread();
1451 boolean were_in_lisp
= !foreign_function_call_active_p(thread
);
1454 fake_foreign_function_call(context
);
1457 /* SUB-GC may return without GCing if *GC-INHIBIT* is set, in
1458 * which case we will be running with no gc trigger barrier
1459 * thing for a while. But it shouldn't be long until the end
1462 * FIXME: It would be good to protect the end of dynamic space for
1463 * CheneyGC and signal a storage condition from there.
1466 /* Restore the signal mask from the interrupted context before
1467 * calling into Lisp if interrupts are enabled. Why not always?
1469 * Suppose there is a WITHOUT-INTERRUPTS block far, far out. If an
1470 * interrupt hits while in SUB-GC, it is deferred and the
1471 * os_context_sigmask of that interrupt is set to block further
1472 * deferrable interrupts (until the first one is
1473 * handled). Unfortunately, that context refers to this place and
1474 * when we return from here the signals will not be blocked.
1476 * A kludgy alternative is to propagate the sigmask change to the
1479 #if !(defined(LISP_FEATURE_WIN32) || defined(LISP_FEATURE_SB_SAFEPOINT))
1480 check_gc_signals_unblocked_or_lose(os_context_sigmask_addr(context
));
1481 unblock_gc_signals(0, 0);
1483 FSHOW((stderr
, "/maybe_gc: calling SUB_GC\n"));
1484 /* FIXME: Nothing must go wrong during GC else we end up running
1485 * the debugger, error handlers, and user code in general in a
1486 * potentially unsafe place. Running out of the control stack or
1487 * the heap in SUB-GC are ways to lose. Of course, deferrables
1488 * cannot be unblocked because there may be a pending handler, or
1489 * we may even be in a WITHOUT-INTERRUPTS. */
1490 gc_happened
= funcall0(StaticSymbolFunction(SUB_GC
));
1491 FSHOW((stderr
, "/maybe_gc: gc_happened=%s\n",
1492 (gc_happened
== NIL
)
1494 : ((gc_happened
== T
)
1497 /* gc_happened can take three values: T, NIL, 0.
1499 * T means that the thread managed to trigger a GC, and post-gc
1502 * NIL means that the thread is within without-gcing, and no GC
1505 * Finally, 0 means that *a* GC has occurred, but it wasn't
1506 * triggered by this thread; success, but post-gc doesn't have
1509 if ((gc_happened
== T
) &&
1510 /* See if interrupts are enabled or it's possible to enable
1511 * them. POST-GC has a similar check, but we don't want to
1512 * unlock deferrables in that case and get a pending interrupt
1514 ((SymbolValue(INTERRUPTS_ENABLED
,thread
) != NIL
) ||
1515 (SymbolValue(ALLOW_WITH_INTERRUPTS
,thread
) != NIL
))) {
1516 #ifndef LISP_FEATURE_WIN32
1517 sigset_t
*context_sigmask
= os_context_sigmask_addr(context
);
1518 if (!deferrables_blocked_p(context_sigmask
)) {
1519 thread_sigmask(SIG_SETMASK
, context_sigmask
, 0);
1520 #ifndef LISP_FEATURE_SB_SAFEPOINT
1521 check_gc_signals_unblocked_or_lose(0);
1524 FSHOW((stderr
, "/maybe_gc: calling POST_GC\n"));
1525 funcall0(StaticSymbolFunction(POST_GC
));
1526 #ifndef LISP_FEATURE_WIN32
1528 FSHOW((stderr
, "/maybe_gc: punting on POST_GC due to blockage\n"));
1534 undo_fake_foreign_function_call(context
);
1536 /* Otherwise done by undo_fake_foreign_function_call. And
1537 something later wants them to be blocked. What a nice
1539 block_blockable_signals(0);
1542 FSHOW((stderr
, "/maybe_gc: returning\n"));
1543 return (gc_happened
!= NIL
);
1546 #define BYTES_ZERO_BEFORE_END (1<<12)
1548 /* There used to be a similar function called SCRUB-CONTROL-STACK in
1549 * Lisp and another called zero_stack() in cheneygc.c, but since it's
1550 * shorter to express in, and more often called from C, I keep only
1551 * the C one after fixing it. -- MG 2009-03-25 */
1553 /* Zero the unused portion of the control stack so that old objects
1554 * are not kept alive because of uninitialized stack variables.
1556 * "To summarize the problem, since not all allocated stack frame
1557 * slots are guaranteed to be written by the time you call an another
1558 * function or GC, there may be garbage pointers retained in your dead
1559 * stack locations. The stack scrubbing only affects the part of the
1560 * stack from the SP to the end of the allocated stack." - ram, on
1561 * cmucl-imp, Tue, 25 Sep 2001
1563 * So, as an (admittedly lame) workaround, from time to time we call
1564 * scrub-control-stack to zero out all the unused portion. This is
1565 * supposed to happen when the stack is mostly empty, so that we have
1566 * a chance of clearing more of it: callers are currently (2002.07.18)
1567 * REPL, SUB-GC and sig_stop_for_gc_handler. */
1569 /* Take care not to tread on the guard page and the hard guard page as
1570 * it would be unkind to sig_stop_for_gc_handler. Touching the return
1571 * guard page is not dangerous. For this to work the guard page must
1572 * be zeroed when protected. */
1574 /* FIXME: I think there is no guarantee that once
1575 * BYTES_ZERO_BEFORE_END bytes are zero the rest are also zero. This
1576 * may be what the "lame" adjective in the above comment is for. In
1577 * this case, exact gc may lose badly. */
1579 scrub_control_stack()
1581 scrub_thread_control_stack(arch_os_get_current_thread());
1585 scrub_thread_control_stack(struct thread
*th
)
1587 os_vm_address_t guard_page_address
= CONTROL_STACK_GUARD_PAGE(th
);
1588 os_vm_address_t hard_guard_page_address
= CONTROL_STACK_HARD_GUARD_PAGE(th
);
1589 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
1590 /* On these targets scrubbing from C is a bad idea, so we punt to
1591 * a routine in $ARCH-assem.S. */
1592 extern void arch_scrub_control_stack(struct thread
*, os_vm_address_t
, os_vm_address_t
);
1593 arch_scrub_control_stack(th
, guard_page_address
, hard_guard_page_address
);
1595 lispobj
*sp
= access_control_stack_pointer(th
);
1597 if ((((os_vm_address_t
)sp
< (hard_guard_page_address
+ os_vm_page_size
)) &&
1598 ((os_vm_address_t
)sp
>= hard_guard_page_address
)) ||
1599 (((os_vm_address_t
)sp
< (guard_page_address
+ os_vm_page_size
)) &&
1600 ((os_vm_address_t
)sp
>= guard_page_address
) &&
1601 (th
->control_stack_guard_page_protected
!= NIL
)))
1603 #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD
1606 } while (((uword_t
)sp
--) & (BYTES_ZERO_BEFORE_END
- 1));
1607 if ((os_vm_address_t
)sp
< (hard_guard_page_address
+ os_vm_page_size
))
1612 } while (((uword_t
)sp
--) & (BYTES_ZERO_BEFORE_END
- 1));
1616 } while (((uword_t
)++sp
) & (BYTES_ZERO_BEFORE_END
- 1));
1617 if ((os_vm_address_t
)sp
>= hard_guard_page_address
)
1622 } while (((uword_t
)++sp
) & (BYTES_ZERO_BEFORE_END
- 1));
1624 #endif /* LISP_FEATURE_C_STACK_IS_CONTROL_STACK */
1627 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
1630 scavenge_control_stack(struct thread
*th
)
1632 lispobj
*object_ptr
;
1634 /* In order to properly support dynamic-extent allocation of
1635 * non-CONS objects, the control stack requires special handling.
1636 * Rather than calling scavenge() directly, grovel over it fixing
1637 * broken hearts, scavenging pointers to oldspace, and pitching a
1638 * fit when encountering unboxed data. This prevents stray object
1639 * headers from causing the scavenger to blow past the end of the
1640 * stack (an error case checked in scavenge()). We don't worry
1641 * about treating unboxed words as boxed or vice versa, because
1642 * the compiler isn't allowed to store unboxed objects on the
1643 * control stack. -- AB, 2011-Dec-02 */
1645 for (object_ptr
= th
->control_stack_start
;
1646 object_ptr
< access_control_stack_pointer(th
);
1649 lispobj object
= *object_ptr
;
1650 #ifdef LISP_FEATURE_GENCGC
1651 if (forwarding_pointer_p(object_ptr
))
1652 lose("unexpected forwarding pointer in scavenge_control_stack: %p, start=%p, end=%p\n",
1653 object_ptr
, th
->control_stack_start
, access_control_stack_pointer(th
));
1655 if (is_lisp_pointer(object
) && from_space_p(object
)) {
1656 /* It currently points to old space. Check for a
1657 * forwarding pointer. */
1658 lispobj
*ptr
= native_pointer(object
);
1659 if (forwarding_pointer_p(ptr
)) {
1660 /* Yes, there's a forwarding pointer. */
1661 *object_ptr
= LOW_WORD(forwarding_pointer_value(ptr
));
1663 /* Scavenge that pointer. */
1664 long n_words_scavenged
=
1665 (scavtab
[widetag_of(object
)])(object_ptr
, object
);
1666 gc_assert(n_words_scavenged
== 1);
1668 } else if (scavtab
[widetag_of(object
)] == scav_lose
) {
1669 lose("unboxed object in scavenge_control_stack: %p->%x, start=%p, end=%p\n",
1670 object_ptr
, object
, th
->control_stack_start
, access_control_stack_pointer(th
));
1675 /* Scavenging Interrupt Contexts */
1677 static int boxed_registers
[] = BOXED_REGISTERS
;
1679 /* The GC has a notion of an "interior pointer" register, an unboxed
1680 * register that typically contains a pointer to inside an object
1681 * referenced by another pointer. The most obvious of these is the
1682 * program counter, although many compiler backends define a "Lisp
1683 * Interior Pointer" register known to the runtime as reg_LIP, and
1684 * various CPU architectures have other registers that also partake of
1685 * the interior-pointer nature. As the code for pairing an interior
1686 * pointer value up with its "base" register, and fixing it up after
1687 * scavenging is complete is horribly repetitive, a few macros paper
1688 * over the monotony. --AB, 2010-Jul-14 */
1690 /* These macros are only ever used over a lexical environment which
1691 * defines a pointer to an os_context_t called context, thus we don't
1692 * bother to pass that context in as a parameter. */
1694 /* Define how to access a given interior pointer. */
1695 #define ACCESS_INTERIOR_POINTER_pc \
1696 *os_context_pc_addr(context)
1697 #define ACCESS_INTERIOR_POINTER_lip \
1698 *os_context_register_addr(context, reg_LIP)
1699 #define ACCESS_INTERIOR_POINTER_lr \
1700 *os_context_lr_addr(context)
1701 #define ACCESS_INTERIOR_POINTER_npc \
1702 *os_context_npc_addr(context)
1703 #define ACCESS_INTERIOR_POINTER_ctr \
1704 *os_context_ctr_addr(context)
1706 #define INTERIOR_POINTER_VARS(name) \
1707 uword_t name##_offset; \
1708 int name##_register_pair
1710 #define PAIR_INTERIOR_POINTER(name) \
1711 pair_interior_pointer(context, \
1712 ACCESS_INTERIOR_POINTER_##name, \
1714 &name##_register_pair)
1716 /* One complexity here is that if a paired register is not found for
1717 * an interior pointer, then that pointer does not get updated.
1718 * Originally, there was some commentary about using an index of -1
1719 * when calling os_context_register_addr() on SPARC referring to the
1720 * program counter, but the real reason is to allow an interior
1721 * pointer register to point to the runtime, read-only space, or
1722 * static space without problems. */
1723 #define FIXUP_INTERIOR_POINTER(name) \
1725 if (name##_register_pair >= 0) { \
1726 ACCESS_INTERIOR_POINTER_##name = \
1727 (*os_context_register_addr(context, \
1728 name##_register_pair) \
1736 pair_interior_pointer(os_context_t
*context
, uword_t pointer
,
1737 uword_t
*saved_offset
, int *register_pair
)
1742 * I (RLT) think this is trying to find the boxed register that is
1743 * closest to the LIP address, without going past it. Usually, it's
1744 * reg_CODE or reg_LRA. But sometimes, nothing can be found.
1746 /* 0x7FFFFFFF on 32-bit platforms;
1747 0x7FFFFFFFFFFFFFFF on 64-bit platforms */
1748 *saved_offset
= (((uword_t
)1) << (N_WORD_BITS
- 1)) - 1;
1749 *register_pair
= -1;
1750 for (i
= 0; i
< (sizeof(boxed_registers
) / sizeof(int)); i
++) {
1755 index
= boxed_registers
[i
];
1756 reg
= *os_context_register_addr(context
, index
);
1758 /* An interior pointer is never relative to a non-pointer
1759 * register (an oversight in the original implementation).
1760 * The simplest argument for why this is true is to consider
1761 * the fixnum that happens by coincide to be the word-index in
1762 * memory of the header for some object plus two. This is
1763 * happenstance would cause the register containing the fixnum
1764 * to be selected as the register_pair if the interior pointer
1765 * is to anywhere after the first two words of the object.
1766 * The fixnum won't be changed during GC, but the object might
1767 * move, thus destroying the interior pointer. --AB,
1770 if (is_lisp_pointer(reg
) &&
1771 ((reg
& ~LOWTAG_MASK
) <= pointer
)) {
1772 offset
= pointer
- (reg
& ~LOWTAG_MASK
);
1773 if (offset
< *saved_offset
) {
1774 *saved_offset
= offset
;
1775 *register_pair
= index
;
1782 scavenge_interrupt_context(os_context_t
* context
)
1786 /* FIXME: The various #ifdef noise here is precisely that: noise.
1787 * Is it possible to fold it into the macrology so that we have
1788 * one set of #ifdefs and then INTERIOR_POINTER_VARS /et alia/
1789 * compile out for the registers that don't exist on a given
1792 INTERIOR_POINTER_VARS(pc
);
1794 INTERIOR_POINTER_VARS(lip
);
1796 #ifdef ARCH_HAS_LINK_REGISTER
1797 INTERIOR_POINTER_VARS(lr
);
1799 #ifdef ARCH_HAS_NPC_REGISTER
1800 INTERIOR_POINTER_VARS(npc
);
1802 #ifdef LISP_FEATURE_PPC
1803 INTERIOR_POINTER_VARS(ctr
);
1806 PAIR_INTERIOR_POINTER(pc
);
1808 PAIR_INTERIOR_POINTER(lip
);
1810 #ifdef ARCH_HAS_LINK_REGISTER
1811 PAIR_INTERIOR_POINTER(lr
);
1813 #ifdef ARCH_HAS_NPC_REGISTER
1814 PAIR_INTERIOR_POINTER(npc
);
1816 #ifdef LISP_FEATURE_PPC
1817 PAIR_INTERIOR_POINTER(ctr
);
1820 /* Scavenge all boxed registers in the context. */
1821 for (i
= 0; i
< (sizeof(boxed_registers
) / sizeof(int)); i
++) {
1825 index
= boxed_registers
[i
];
1826 foo
= *os_context_register_addr(context
, index
);
1828 *os_context_register_addr(context
, index
) = foo
;
1830 /* this is unlikely to work as intended on bigendian
1831 * 64 bit platforms */
1833 scavenge((lispobj
*) os_context_register_addr(context
, index
), 1);
1836 /* Now that the scavenging is done, repair the various interior
1838 FIXUP_INTERIOR_POINTER(pc
);
1840 FIXUP_INTERIOR_POINTER(lip
);
1842 #ifdef ARCH_HAS_LINK_REGISTER
1843 FIXUP_INTERIOR_POINTER(lr
);
1845 #ifdef ARCH_HAS_NPC_REGISTER
1846 FIXUP_INTERIOR_POINTER(npc
);
1848 #ifdef LISP_FEATURE_PPC
1849 FIXUP_INTERIOR_POINTER(ctr
);
1854 scavenge_interrupt_contexts(struct thread
*th
)
1857 os_context_t
*context
;
1859 index
= fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX
,th
));
1861 #if defined(DEBUG_PRINT_CONTEXT_INDEX)
1862 printf("Number of active contexts: %d\n", index
);
1865 for (i
= 0; i
< index
; i
++) {
1866 context
= th
->interrupt_contexts
[i
];
1867 scavenge_interrupt_context(context
);
1870 #endif /* x86oid targets */
1872 void varint_unpacker_init(struct varint_unpacker
* unpacker
, lispobj integer
)
1874 if (fixnump(integer
)) {
1875 unpacker
->word
= fixnum_value(integer
);
1876 unpacker
->limit
= N_WORD_BYTES
;
1877 unpacker
->data
= (char*)&unpacker
->word
;
1879 struct bignum
* bignum
= (struct bignum
*)(integer
- OTHER_POINTER_LOWTAG
);
1881 unpacker
->limit
= HeaderValue(bignum
->header
) * N_WORD_BYTES
;
1882 unpacker
->data
= (char*)bignum
->digits
;
1884 unpacker
->index
= 0;
1887 // Fetch the next varint from 'unpacker' into 'result'.
1888 // Because there is no length prefix on the number of varints encoded,
1889 // spurious trailing zeros might be observed. The data consumer can
1890 // circumvent that by storing a count as the first value in the series.
1891 // Return 1 for success, 0 for EOF.
1892 int varint_unpack(struct varint_unpacker
* unpacker
, int* result
)
1894 if (unpacker
->index
>= unpacker
->limit
) return 0;
1895 int accumulator
= 0;
1898 #ifdef LISP_FEATURE_LITTLE_ENDIAN
1899 int byte
= unpacker
->data
[unpacker
->index
];
1901 // bignums are little-endian in word order,
1902 // but machine-native within each word.
1903 // We could pack bytes MSB-to-LSB in the bigdigits,
1904 // but that seems less intuitive on the Lisp side.
1905 int word_index
= unpacker
->index
/ N_WORD_BYTES
;
1906 int byte_index
= unpacker
->index
% N_WORD_BYTES
;
1907 int byte
= (((unsigned int*)unpacker
->data
)[word_index
]
1908 >> (byte_index
* 8)) & 0xFF;
1911 accumulator
|= (byte
& 0x7F) << shift
;
1912 if (!(byte
& 0x80)) break;
1913 gc_assert(unpacker
->index
< unpacker
->limit
);
1916 *result
= accumulator
;