Change immobile space free pointers to alien vars
[sbcl.git] / src / runtime / gc-common.c
bloba18fb3bd700f35b243f3789b8b11f908d27d823d
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 #include <stdio.h>
29 #include <signal.h>
30 #include <string.h>
31 #include "sbcl.h"
32 #include "runtime.h"
33 #include "os.h"
34 #include "interr.h"
35 #include "globals.h"
36 #include "interrupt.h"
37 #include "validate.h"
38 #include "lispregs.h"
39 #include "arch.h"
40 #include "gc.h"
41 #include "hopscotch.h"
42 #include "genesis/primitive-objects.h"
43 #include "genesis/static-symbols.h"
44 #include "genesis/layout.h"
45 #include "genesis/hash-table.h"
46 #define WANT_SCAV_TRANS_SIZE_TABLES
47 #include "gc-internal.h"
48 #include "forwarding-ptr.h"
49 #include "var-io.h"
51 #ifdef LISP_FEATURE_SPARC
52 #define LONG_FLOAT_SIZE 4
53 #elif defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
54 #define LONG_FLOAT_SIZE 3
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 sword_t (*scavtab[256])(lispobj *where, lispobj object);
61 static lispobj (*transother[64])(lispobj object);
62 sword_t (*sizetab[256])(lispobj *where);
63 struct weak_pointer *weak_pointers;
65 os_vm_size_t bytes_consed_between_gcs = 12*1024*1024;
67 /// These sizing macros return the number of *payload* words,
68 /// exclusive of the object header word. Payload length is always
69 /// an odd number so that total word count is an even number.
70 #define BOXED_NWORDS(obj) (HeaderValue(obj) | 1)
71 // Payload count expressed in 15 bits
72 #define SHORT_BOXED_NWORDS(obj) ((HeaderValue(obj) & SHORT_HEADER_MAX_WORDS) | 1)
73 // Payload count expressed in 8 bits
74 #define TINY_BOXED_NWORDS(obj) ((HeaderValue(obj) & 0xFF) | 1)
77 * copying objects
80 /* gc_general_copy_object is inline from gc-internal.h */
82 /* to copy a boxed object */
83 lispobj
84 copy_object(lispobj object, sword_t nwords)
86 return gc_general_copy_object(object, nwords, BOXED_PAGE_FLAG);
89 static sword_t scav_lose(lispobj *where, lispobj object); /* forward decl */
91 static inline void scav1(lispobj* object_ptr, lispobj object)
93 // GENCGC only:
94 // * With 32-bit words, is_lisp_pointer(object) returns true if object_ptr
95 // points to a forwarding pointer, so we need a sanity check inside the
96 // branch for is_lisp_pointer(). For maximum efficiency, check that only
97 // after from_space_p() returns false, so that valid pointers into
98 // from_space incur no extra test. This could be improved further by
99 // skipping the FP check if 'object' points within dynamic space, i.e.,
100 // when find_page_index() returns >= 0. That would entail injecting
101 // from_space_p() explicitly into the loop, so as to separate the
102 // "was a page found at all" condition from the page generation test.
104 // * With 64-bit words, is_lisp_pointer(object) is false when object_ptr
105 // points to a forwarding pointer, and the fixnump() test also returns
106 // false, so we'll indirect through scavtab[]. This will safely invoke
107 // scav_lose(), detecting corruption without any extra cost.
108 // The major difference between that and the explicit test is that you
109 // won't see 'start' and 'n_words', but if you need those, chances are
110 // you'll want to run under an external debugger in the first place.
111 // [And btw it sure would be nice to assert statically
112 // that is_lisp_pointer(0x01) is indeed false]
114 #define FIX_POINTER() { \
115 lispobj *ptr = native_pointer(object); \
116 if (forwarding_pointer_p(ptr)) \
117 *object_ptr = LOW_WORD(forwarding_pointer_value(ptr)); \
118 else /* Scavenge that pointer. */ \
119 (void)scavtab[widetag_of(object)](object_ptr, object); \
121 #ifdef LISP_FEATURE_IMMOBILE_SPACE
122 page_index_t page;
123 // It would be fine, though suboptimal, to use from_space_p() here.
124 // If it returns false, we don't want to call immobile_space_p()
125 // unless the pointer is *not* into dynamic space.
126 if ((page = find_page_index((void*)object)) >= 0) {
127 if (page_table[page].gen == from_space && !pinned_p(object, page))
128 FIX_POINTER();
129 } else if (immobile_space_p(object)) {
130 lispobj *ptr = native_pointer(object);
131 if (immobile_obj_gen_bits(ptr) == from_space)
132 enliven_immobile_obj(ptr, 1);
134 #else
135 if (from_space_p(object)) {
136 FIX_POINTER();
137 } else {
138 #if (N_WORD_BITS == 32) && defined(LISP_FEATURE_GENCGC)
139 if (forwarding_pointer_p(object_ptr))
140 lose("unexpected forwarding pointer in scavenge @ %p\n",
141 object_ptr);
142 #endif
143 /* It points somewhere other than oldspace. Leave it
144 * alone. */
146 #endif
149 static inline void scav_pair(lispobj where[2])
151 lispobj object = where[0];
152 if (is_lisp_pointer(object))
153 scav1(where, object);
154 object = where[1];
155 if (is_lisp_pointer(object))
156 scav1(where+1, object);
159 // Scavenge a block of memory from 'start' to 'end'
160 // that may contain object headers.
161 void heap_scavenge(lispobj *start, lispobj *end)
163 lispobj *object_ptr;
165 for (object_ptr = start; object_ptr < end;) {
166 lispobj object = *object_ptr;
167 if (other_immediate_lowtag_p(object))
168 /* It's some sort of header object or another. */
169 object_ptr += (scavtab[widetag_of(object)])(object_ptr, object);
170 else { // it's a cons
171 scav_pair(object_ptr);
172 object_ptr += 2;
175 // This assertion is usually the one that fails when something
176 // is subtly wrong with the heap, so definitely always do it.
177 gc_assert_verbose(object_ptr == end, "Final object pointer %p, start %p, end %p\n",
178 object_ptr, start, end);
181 // Scavenge a block of memory from 'start' extending for 'n_words'
182 // that must not contain any object headers.
183 sword_t scavenge(lispobj *start, sword_t n_words)
185 lispobj *end = start + n_words;
186 lispobj *object_ptr;
187 for (object_ptr = start; object_ptr < end; object_ptr++) {
188 lispobj object = *object_ptr;
189 if (is_lisp_pointer(object)) scav1(object_ptr, object);
191 return n_words;
194 void scav_binding_stack(lispobj* where, lispobj* end)
196 #ifdef LISP_FEATURE_SB_THREAD
197 // The binding stack stores TLS indices where symbols would be,
198 // and there's no reason to scavenge those words since they're fixnums.
199 // This means a symbol can not be enlivened if it exists *solely* on
200 // the binding stack - which is, practically speaking, impossible.
201 lispobj *object_ptr;
202 for (object_ptr = where; object_ptr < end; object_ptr += 2) {
203 lispobj object = *object_ptr;
204 if (is_lisp_pointer(object)) scav1(object_ptr, object);
206 #else
207 scavenge(where, end - where);
208 #endif
211 static lispobj trans_fun_header(lispobj object); /* forward decls */
212 static lispobj trans_short_boxed(lispobj object);
214 static sword_t
215 scav_fun_pointer(lispobj *where, lispobj object)
217 gc_dcheck(lowtag_of(object) == FUN_POINTER_LOWTAG);
219 /* Object is a pointer into from_space - not a FP. */
220 lispobj *first_pointer = native_pointer(object);
222 /* must transport object -- object may point to either a function
223 * header, a funcallable instance header, or a closure header. */
224 lispobj copy = widetag_of(*first_pointer) == SIMPLE_FUN_WIDETAG
225 ? trans_fun_header(object) : trans_short_boxed(object);
227 if (copy != object) {
228 /* Set forwarding pointer */
229 set_forwarding_pointer(first_pointer,copy);
232 CHECK_COPY_POSTCONDITIONS(copy, FUN_POINTER_LOWTAG);
234 *where = copy;
236 return 1;
240 static struct code *
241 trans_code(struct code *code)
243 /* if object has already been transported, just return pointer */
244 if (forwarding_pointer_p((lispobj *)code)) {
245 #ifdef DEBUG_CODE_GC
246 printf("Was already transported\n");
247 #endif
248 return (struct code *)native_pointer(forwarding_pointer_value((lispobj*)code));
251 gc_dcheck(widetag_of(code->header) == CODE_HEADER_WIDETAG);
253 /* prepare to transport the code vector */
254 lispobj l_code = (lispobj) LOW_WORD(code) | OTHER_POINTER_LOWTAG;
255 sword_t nheader_words = code_header_words(code->header);
256 sword_t ncode_words = code_instruction_words(code->code_size);
257 sword_t nwords = nheader_words + ncode_words;
258 lispobj l_new_code = gc_general_copy_object(l_code, nwords, CODE_PAGE_FLAG);
259 struct code *new_code = (struct code *) native_pointer(l_new_code);
261 #if defined(DEBUG_CODE_GC)
262 printf("Old code object at 0x%08x, new code object at 0x%08x.\n",
263 (uword_t) code, (uword_t) new_code);
264 printf("Code object is %d words long.\n", nwords);
265 #endif
267 #ifdef LISP_FEATURE_GENCGC
268 if (new_code == code)
269 return new_code;
270 #endif
272 set_forwarding_pointer((lispobj *)code, l_new_code);
274 /* set forwarding pointers for all the function headers in the */
275 /* code object. also fix all self pointers */
276 /* Do this by scanning the new code, since the old header is unusable */
278 uword_t displacement = l_new_code - l_code;
280 for_each_simple_fun(i, nfheaderp, new_code, 1, {
281 /* Calculate the old raw function pointer */
282 struct simple_fun* fheaderp =
283 (struct simple_fun*)LOW_WORD((char*)nfheaderp - displacement);
284 /* Calculate the new lispobj */
285 lispobj nfheaderl = make_lispobj(nfheaderp, FUN_POINTER_LOWTAG);
287 #ifdef DEBUG_CODE_GC
288 printf("fheaderp->header (at %x) <- %x\n",
289 &(fheaderp->header) , nfheaderl);
290 #endif
291 set_forwarding_pointer((lispobj *)fheaderp, nfheaderl);
293 /* fix self pointer. */
294 nfheaderp->self =
295 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
296 FUN_RAW_ADDR_OFFSET +
297 #endif
298 nfheaderl;
300 #ifdef LISP_FEATURE_GENCGC
301 /* Cheneygc doesn't need this os_flush_icache, it flushes the whole
302 spaces once when all copying is done. */
303 os_flush_icache((os_vm_address_t) (((sword_t *)new_code) + nheader_words),
304 ncode_words * sizeof(sword_t));
306 #endif
308 gencgc_apply_code_fixups(code, new_code);
310 return new_code;
313 static sword_t
314 scav_code_header(lispobj *where, lispobj header)
316 struct code *code = (struct code *) where;
317 sword_t n_header_words = code_header_words(header);
319 /* Scavenge the boxed section of the code data block. */
320 scavenge(where + 1, n_header_words - 1);
322 /* Scavenge the boxed section of each function object in the
323 * code data block. */
324 for_each_simple_fun(i, function_ptr, code, 1, {
325 scavenge(SIMPLE_FUN_SCAV_START(function_ptr),
326 SIMPLE_FUN_SCAV_NWORDS(function_ptr));
329 return n_header_words + code_instruction_words(code->code_size);
332 static lispobj
333 trans_code_header(lispobj object)
335 struct code *ncode = trans_code((struct code *) native_pointer(object));
336 return (lispobj) LOW_WORD(ncode) | OTHER_POINTER_LOWTAG;
339 static sword_t
340 size_code_header(lispobj *where)
342 return code_header_words(((struct code *)where)->header)
343 + code_instruction_words(((struct code *)where)->code_size);
346 #ifdef RETURN_PC_WIDETAG
347 static sword_t
348 scav_return_pc_header(lispobj *where, lispobj object)
350 lose("attempted to scavenge a return PC header where=%p object=%#lx\n",
351 where, (uword_t) object);
352 return 0; /* bogus return value to satisfy static type checking */
355 static lispobj
356 trans_return_pc_header(lispobj object)
358 struct simple_fun *return_pc = (struct simple_fun *) native_pointer(object);
359 uword_t offset = HeaderValue(return_pc->header) * N_WORD_BYTES;
361 /* Transport the whole code object */
362 struct code *code = (struct code *) ((uword_t) return_pc - offset);
363 struct code *ncode = trans_code(code);
365 return ((lispobj) LOW_WORD(ncode) + offset) | OTHER_POINTER_LOWTAG;
367 #endif /* RETURN_PC_WIDETAG */
369 /* On the 386, closures hold a pointer to the raw address instead of the
370 * function object, so we can use CALL [$FDEFN+const] to invoke
371 * the function without loading it into a register. Given that code
372 * objects don't move, we don't need to update anything, but we do
373 * have to figure out that the function is still live. */
375 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
376 static sword_t
377 scav_closure(lispobj *where, lispobj header)
379 struct closure *closure = (struct closure *)where;
380 int payload_words = SHORT_BOXED_NWORDS(header);
381 lispobj fun = closure->fun - FUN_RAW_ADDR_OFFSET;
382 scavenge(&fun, 1);
383 #ifdef LISP_FEATURE_GENCGC
384 /* The function may have moved so update the raw address. But
385 * don't write unnecessarily. */
386 if (closure->fun != fun + FUN_RAW_ADDR_OFFSET)
387 closure->fun = fun + FUN_RAW_ADDR_OFFSET;
388 #endif
389 // Payload includes 'fun' which was just looked at, so subtract it.
390 scavenge(closure->info, payload_words - 1);
391 return 1 + payload_words;
393 #endif
395 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
396 static sword_t
397 scav_fun_header(lispobj *where, lispobj object)
399 lose("attempted to scavenge a function header where=%p object=%#lx\n",
400 where, (uword_t) object);
401 return 0; /* bogus return value to satisfy static type checking */
403 #endif /* LISP_FEATURE_X86 */
405 static lispobj
406 trans_fun_header(lispobj object)
408 struct simple_fun *fheader = (struct simple_fun *) native_pointer(object);
409 uword_t offset =
410 (HeaderValue(fheader->header) & FUN_HEADER_NWORDS_MASK) * N_WORD_BYTES;
412 /* Transport the whole code object */
413 struct code *code = (struct code *) ((uword_t) fheader - offset);
414 struct code *ncode = trans_code(code);
416 return ((lispobj) LOW_WORD(ncode) + offset) | FUN_POINTER_LOWTAG;
421 * instances
424 static lispobj
425 trans_instance(lispobj object)
427 gc_dcheck(lowtag_of(object) == INSTANCE_POINTER_LOWTAG);
428 lispobj header = *(lispobj*)(object - INSTANCE_POINTER_LOWTAG);
429 return copy_object(object, 1 + (instance_length(header)|1));
432 static sword_t
433 scav_instance_pointer(lispobj *where, lispobj object)
435 /* Object is a pointer into from space - not a FP. */
436 lispobj copy = trans_instance(object);
438 gc_dcheck(copy != object);
440 set_forwarding_pointer(native_pointer(object), copy);
441 *where = copy;
443 return 1;
448 * lists and conses
451 static lispobj trans_list(lispobj object);
453 static sword_t
454 scav_list_pointer(lispobj *where, lispobj object)
456 gc_dcheck(lowtag_of(object) == LIST_POINTER_LOWTAG);
458 lispobj copy = trans_list(object);
459 gc_dcheck(copy != object);
461 CHECK_COPY_POSTCONDITIONS(copy, LIST_POINTER_LOWTAG);
463 *where = copy;
464 return 1;
468 static lispobj
469 trans_list(lispobj object)
471 /* Copy 'object'. */
472 struct cons *copy = (struct cons *)
473 gc_general_alloc(sizeof(struct cons), BOXED_PAGE_FLAG, ALLOC_QUICK);
474 lispobj new_list_pointer = make_lispobj(copy, LIST_POINTER_LOWTAG);
475 copy->car = CONS(object)->car;
476 /* Grab the cdr: set_forwarding_pointer will clobber it in GENCGC */
477 lispobj cdr = CONS(object)->cdr;
478 set_forwarding_pointer((lispobj *)CONS(object), new_list_pointer);
480 /* Try to linearize the list in the cdr direction to help reduce
481 * paging. */
482 while (lowtag_of(cdr) == LIST_POINTER_LOWTAG && from_space_p(cdr)) {
483 lispobj* native_cdr = (lispobj*)CONS(cdr);
484 if (forwarding_pointer_p(native_cdr)) { // Might as well fix now.
485 cdr = forwarding_pointer_value(native_cdr);
486 break;
488 /* Copy 'cdr'. */
489 struct cons *cdr_copy = (struct cons*)
490 gc_general_alloc(sizeof(struct cons), BOXED_PAGE_FLAG, ALLOC_QUICK);
491 cdr_copy->car = ((struct cons*)native_cdr)->car;
492 /* Grab the cdr before it is clobbered. */
493 lispobj next = ((struct cons*)native_cdr)->cdr;
494 /* Set cdr of the predecessor, and store an FP. */
495 set_forwarding_pointer(native_cdr,
496 copy->cdr = make_lispobj(cdr_copy,
497 LIST_POINTER_LOWTAG));
498 copy = cdr_copy;
499 cdr = next;
501 copy->cdr = cdr;
502 return new_list_pointer;
507 * scavenging and transporting other pointers
510 static sword_t
511 scav_other_pointer(lispobj *where, lispobj object)
513 gc_dcheck(lowtag_of(object) == OTHER_POINTER_LOWTAG);
515 /* Object is a pointer into from space - not FP. */
516 lispobj *first_pointer = (lispobj *)(object - OTHER_POINTER_LOWTAG);
517 int tag = widetag_of(*first_pointer);
518 lispobj copy = transother[other_immediate_lowtag_p(tag)?tag>>2:0](object);
520 // If the object was large, then instead of transporting it,
521 // gencgc might simply promote the pages and return the same pointer.
522 // That decision is made in general_copy_large_object().
523 if (copy != object) {
524 set_forwarding_pointer(first_pointer, copy);
525 #ifdef LISP_FEATURE_GENCGC
526 *where = copy;
527 #endif
529 #ifndef LISP_FEATURE_GENCGC
530 *where = copy;
531 #endif
532 CHECK_COPY_POSTCONDITIONS(copy, OTHER_POINTER_LOWTAG);
533 return 1;
537 * immediate, boxed, and unboxed objects
540 /* The immediate object scavenger basically wants to be "scav_cons",
541 * and so returns 2. To see why it's right, observe that scavenge() will
542 * not invoke a scavtab entry on any object except for one satisfying
543 * is_lisp_pointer(). So if a scavtab[] function got here,
544 * then it must be via heap_scavenge(). But heap_scavenge() should only
545 * dispatch via scavtab[] if it thought it saw an object header.
546 * So why do we act like it saw a cons? Because conses can contain an
547 * immediate object that satisfies both other_immediate_lowtag_p()
548 * and is_lisp_immediate(), namely, the objects specifically mentioned at
549 * is_cons_half(). So heap_scavenge() is nearly testing is_cons_half()
550 * but even more efficiently, by ignoring the unusual immediate widetags
551 * until we get to scav_immediate.
553 * And just to hammer the point home: we won't blow past the end of a specific
554 * range of words when scavenging a binding or control stack or anything else,
555 * because scavenge() skips immediate objects all by itself,
556 * or rather it skips anything not satisfying is_lisp_pointer().
558 * As to the unbound marker, see rev. 09c78105eabc6bf2b339f421d4ed1df4678003db
559 * which says that we might see it in conses for reasons somewhat unknown.
561 static sword_t
562 scav_immediate(lispobj *where, lispobj object)
564 object = *++where;
565 if (is_lisp_pointer(object)) scav1(where, object);
566 return 2;
569 static lispobj
570 trans_immediate(lispobj object)
572 lose("trying to transport an immediate\n");
573 return NIL; /* bogus return value to satisfy static type checking */
576 static sword_t
577 size_immediate(lispobj *where)
579 return 1;
582 static inline boolean bignum_logbitp_inline(int index, struct bignum* bignum)
584 int len = HeaderValue(bignum->header);
585 int word_index = index / N_WORD_BITS;
586 int bit_index = index % N_WORD_BITS;
587 return word_index < len ? (bignum->digits[word_index] >> bit_index) & 1 : 0;
589 boolean positive_bignum_logbitp(int index, struct bignum* bignum)
591 /* If the bignum in the layout has another pointer to it (besides the layout)
592 acting as a root, and which is scavenged first, then transporting the
593 bignum causes the layout to see a FP, as would copying an instance whose
594 layout that is. This is a nearly impossible scenario to create organically
595 in Lisp, because mostly nothing ever looks again at that exact (EQ) bignum
596 except for a few things that would cause it to be pinned anyway,
597 such as it being kept in a local variable during structure manipulation.
598 See 'interleaved-raw.impure.lisp' for a way to trigger this */
599 if (forwarding_pointer_p((lispobj*)bignum)) {
600 lispobj forwarded = forwarding_pointer_value((lispobj*)bignum);
601 #if 0
602 fprintf(stderr, "GC bignum_logbitp(): fwd from %p to %p\n",
603 (void*)bignum, (void*)forwarded);
604 #endif
605 bignum = (struct bignum*)native_pointer(forwarded);
607 return bignum_logbitp_inline(index, bignum);
610 // Helper function for stepping through the tagged slots of an instance in
611 // scav_instance and verify_space.
612 void
613 instance_scan(void (*proc)(lispobj*, sword_t),
614 lispobj *instance_slots,
615 sword_t nslots, /* number of payload words */
616 lispobj layout_bitmap)
618 sword_t index;
620 if (fixnump(layout_bitmap)) {
621 if (layout_bitmap == make_fixnum(-1))
622 proc(instance_slots, nslots);
623 else {
624 sword_t bitmap = (sword_t)layout_bitmap >> N_FIXNUM_TAG_BITS; // signed integer!
625 for (index = 0; index < nslots ; index++, bitmap >>= 1)
626 if (bitmap & 1)
627 proc(instance_slots + index, 1);
629 } else { /* huge bitmap */
630 struct bignum * bitmap;
631 bitmap = (struct bignum*)native_pointer(layout_bitmap);
632 if (forwarding_pointer_p((lispobj*)bitmap))
633 bitmap = (struct bignum*)
634 native_pointer(forwarding_pointer_value((lispobj*)bitmap));
635 for (index = 0; index < nslots ; index++)
636 if (bignum_logbitp_inline(index, bitmap))
637 proc(instance_slots + index, 1);
641 static sword_t
642 scav_instance(lispobj *where, lispobj header)
644 lispobj* layout = (lispobj*)instance_layout(where);
645 lispobj lbitmap = make_fixnum(-1);
647 if (layout) {
648 layout = native_pointer((lispobj)layout);
649 #ifdef LISP_FEATURE_COMPACT_INSTANCE_HEADER
650 if (__immobile_obj_gen_bits(layout) == from_space)
651 enliven_immobile_obj(layout, 1);
652 #else
653 if (forwarding_pointer_p(layout))
654 layout = native_pointer(forwarding_pointer_value(layout));
655 #endif
656 lbitmap = ((struct layout*)layout)->bitmap;
658 sword_t nslots = instance_length(header) | 1;
659 if (lbitmap == make_fixnum(-1))
660 scavenge(where+1, nslots);
661 else if (!fixnump(lbitmap))
662 instance_scan((void(*)(lispobj*,sword_t))scavenge,
663 where+1, nslots, lbitmap);
664 else {
665 sword_t bitmap = (sword_t)lbitmap >> N_FIXNUM_TAG_BITS; // signed integer!
666 sword_t n = nslots;
667 lispobj obj;
668 for ( ; n-- ; bitmap >>= 1) {
669 ++where;
670 if ((bitmap & 1) && is_lisp_pointer(obj = *where))
671 scav1(where, obj);
674 return 1 + nslots;
677 #ifdef LISP_FEATURE_COMPACT_INSTANCE_HEADER
678 static sword_t
679 scav_funinstance(lispobj *where, lispobj header)
681 // This works because the layout is in the header word of all instances,
682 // ordinary and funcallable, when compact headers are enabled.
683 // The trampoline slot in the funcallable-instance is raw, but can be
684 // scavenged, because it points to readonly space, never oldspace.
685 // (And for certain backends it looks like a fixnum, not a pointer)
686 return scav_instance(where, header);
688 #endif
690 //// Boxed object scav/trans/size functions
692 #define DEF_SCAV_BOXED(suffix, sizer) \
693 static sword_t __attribute__((unused)) \
694 scav_##suffix(lispobj *where, lispobj header) { \
695 return 1 + scavenge(where+1, sizer(header)); \
697 static lispobj trans_##suffix(lispobj object) { \
698 return copy_object(object, 1 + sizer(*native_pointer(object))); \
700 static sword_t size_##suffix(lispobj *where) { return 1 + sizer(*where); }
702 DEF_SCAV_BOXED(boxed, BOXED_NWORDS)
703 DEF_SCAV_BOXED(short_boxed, SHORT_BOXED_NWORDS)
704 DEF_SCAV_BOXED(tiny_boxed, TINY_BOXED_NWORDS)
706 /* Note: on the sparc we don't have to do anything special for fdefns, */
707 /* 'cause the raw-addr has a function lowtag. */
708 #if !defined(LISP_FEATURE_SPARC) && !defined(LISP_FEATURE_ARM)
709 static sword_t
710 scav_fdefn(lispobj *where, lispobj object)
712 struct fdefn *fdefn = (struct fdefn *)where;
714 /* FSHOW((stderr, "scav_fdefn, function = %p, raw_addr = %p\n",
715 fdefn->fun, fdefn->raw_addr)); */
717 scavenge(where + 1, 2); // 'name' and 'fun'
718 #ifndef LISP_FEATURE_IMMOBILE_CODE
719 lispobj raw_fun = (lispobj)fdefn->raw_addr;
720 if (raw_fun > READ_ONLY_SPACE_END) {
721 lispobj simple_fun = raw_fun - FUN_RAW_ADDR_OFFSET;
722 scavenge(&simple_fun, 1);
723 /* Don't write unnecessarily. */
724 if (simple_fun != raw_fun - FUN_RAW_ADDR_OFFSET)
725 fdefn->raw_addr = (char *)simple_fun + FUN_RAW_ADDR_OFFSET;
727 #elif defined(LISP_FEATURE_X86_64)
728 lispobj obj = fdefn_raw_referent(fdefn);
729 if (obj) {
730 lispobj new = obj;
731 scavenge(&new, 1); // enliven
732 gc_dcheck(new == obj); // must not move
734 #else
735 # error "Need to implement scav_fdefn"
736 #endif
737 return 4;
739 #endif
741 static sword_t
742 scav_unboxed(lispobj *where, lispobj object)
744 sword_t length = HeaderValue(object) + 1;
745 return CEILING(length, 2);
748 static lispobj
749 trans_unboxed(lispobj object)
751 gc_dcheck(lowtag_of(object) == OTHER_POINTER_LOWTAG);
752 sword_t length = HeaderValue(*native_pointer(object)) + 1;
753 return copy_unboxed_object(object, CEILING(length, 2));
756 static lispobj
757 trans_ratio_or_complex(lispobj object)
759 gc_dcheck(lowtag_of(object) == OTHER_POINTER_LOWTAG);
760 lispobj* x = native_pointer(object);
761 lispobj a = x[1];
762 lispobj b = x[2];
764 /* A zero ratio or complex means it was just allocated by fixed-alloc and
765 a bignum can still be written there. Not a problem with a conservative GC
766 since it will be pinned down. */
767 if (fixnump(a) && fixnump(b)
768 #ifndef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
769 && a && b
770 #endif
773 return copy_unboxed_object(object, 4);
775 return copy_object(object, 4);
778 /* vector-like objects */
779 static lispobj
780 trans_vector(lispobj object)
782 gc_dcheck(lowtag_of(object) == OTHER_POINTER_LOWTAG);
784 sword_t length = fixnum_value(VECTOR(object)->length);
785 return copy_large_object(object, CEILING(length + 2, 2));
788 static sword_t
789 size_vector(lispobj *where)
791 sword_t length = fixnum_value(((struct vector*)where)->length);
792 return CEILING(length + 2, 2);
795 static inline uword_t
796 NWORDS(uword_t x, uword_t n_bits)
798 /* A good compiler should be able to constant-fold this whole thing,
799 even with the conditional. */
800 if(n_bits <= N_WORD_BITS) {
801 uword_t elements_per_word = N_WORD_BITS/n_bits;
803 return CEILING(x, elements_per_word)/elements_per_word;
805 else {
806 /* FIXME: should have some sort of assertion that N_WORD_BITS
807 evenly divides n_bits */
808 return x * (n_bits/N_WORD_BITS);
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(VECTOR(object)->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))
847 #endif
849 static lispobj
850 trans_weak_pointer(lispobj object)
852 lispobj copy;
853 gc_dcheck(lowtag_of(object) == OTHER_POINTER_LOWTAG);
855 #if defined(DEBUG_WEAK)
856 printf("Transporting weak pointer from 0x%08x\n", object);
857 #endif
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);
870 weak_pointers = wp;
872 #endif
873 return copy;
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);
882 next_wp = wp->next;
883 wp->next = NULL;
884 if (next_wp == wp) /* gencgc uses a ref to self for end of list */
885 next_wp = NULL;
887 lispobj pointee = wp->value;
888 gc_assert(is_lisp_pointer(pointee));
889 lispobj *objaddr = native_pointer(pointee);
891 /* Now, we need to check whether the object has been forwarded. If
892 * it has been, the weak pointer is still good and needs to be
893 * updated. Otherwise, the weak pointer needs to be broken. */
895 if (from_space_p(pointee)) {
896 wp->value = forwarding_pointer_p(objaddr) ?
897 LOW_WORD(forwarding_pointer_value(objaddr)) : UNBOUND_MARKER_WIDETAG;
899 #ifdef LISP_FEATURE_IMMOBILE_SPACE
900 else if (immobile_space_p(pointee)) {
901 if (immobile_obj_gen_bits(objaddr) == from_space)
902 wp->value = UNBOUND_MARKER_WIDETAG;
904 #endif
905 else
906 lose("unbreakable pointer %p", wp);
911 /* Hash tables */
913 #if N_WORD_BITS == 32
914 #define EQ_HASH_MASK 0x1fffffff
915 #elif N_WORD_BITS == 64
916 #define EQ_HASH_MASK 0x1fffffffffffffff
917 #endif
919 /* Compute the EQ-hash of KEY. This must match POINTER-HASH in
920 * target-hash-table.lisp. */
921 #define EQ_HASH(key) ((key) & EQ_HASH_MASK)
923 /* List of weak hash tables chained through their NEXT-WEAK-HASH-TABLE
924 * slot. Set to NULL at the end of a collection.
926 * This is not optimal because, when a table is tenured, it won't be
927 * processed automatically; only the yougest generation is GC'd by
928 * default. On the other hand, all applications will need an
929 * occasional full GC anyway, so it's not that bad either. */
930 struct hash_table *weak_hash_tables = NULL;
932 /* Return true if OBJ has already survived the current GC. */
933 static inline int pointer_survived_gc_yet(lispobj obj)
935 #ifdef LISP_FEATURE_CHENEYGC
936 // This is the most straightforward definition.
937 return (!from_space_p(obj) || forwarding_pointer_p(native_pointer(obj)));
938 #else
939 /* Check for a pointer to dynamic space before considering immobile space.
940 Based on the relative size of the spaces, this should be a win because
941 if the object is in the dynamic space and not the 'from' generation
942 we don't want to test immobile_space_p() at all.
943 Additionally, pinned_p() is both more expensive and less likely than
944 forwarding_pointer_p(), so we want to reverse those conditions, which
945 would not be possible with pinned_p() buried inside from_space_p(). */
946 page_index_t page_index = find_page_index((void*)obj);
947 if (page_index >= 0)
948 return page_table[page_index].gen != from_space ||
949 forwarding_pointer_p(native_pointer(obj)) ||
950 pinned_p(obj, page_index);
951 #ifdef LISP_FEATURE_IMMOBILE_SPACE
952 if (immobile_space_p(obj))
953 return immobile_obj_gen_bits(native_pointer(obj)) != from_space;
954 #endif
955 return 1;
956 #endif
959 static int survived_gc_yet_KEY(lispobj key, lispobj value) {
960 return !is_lisp_pointer(key) || pointer_survived_gc_yet(key);
962 static int survived_gc_yet_VALUE(lispobj key, lispobj value) {
963 return !is_lisp_pointer(value) || pointer_survived_gc_yet(value);
965 static int survived_gc_yet_AND(lispobj key, lispobj value) {
966 int key_nonpointer = !is_lisp_pointer(key);
967 int val_nonpointer = !is_lisp_pointer(value);
968 if (key_nonpointer && val_nonpointer) return 1;
969 return (key_nonpointer || pointer_survived_gc_yet(key))
970 && (val_nonpointer || pointer_survived_gc_yet(value));
972 static int survived_gc_yet_OR(lispobj key, lispobj value) {
973 int key_nonpointer = !is_lisp_pointer(key);
974 int val_nonpointer = !is_lisp_pointer(value);
975 if (key_nonpointer || val_nonpointer) return 1;
976 // Both MUST be pointers
977 return pointer_survived_gc_yet(key) || pointer_survived_gc_yet(value);
980 static int (*weak_hash_entry_alivep_fun[5])(lispobj,lispobj) = {
981 NULL,
982 survived_gc_yet_KEY,
983 survived_gc_yet_VALUE,
984 survived_gc_yet_AND,
985 survived_gc_yet_OR
988 /* Return the beginning of data in ARRAY (skipping the header and the
989 * length) or NULL if it isn't an array of the specified widetag after
990 * all. */
991 static inline lispobj *
992 get_array_data (lispobj array, int widetag, uword_t *length)
994 if (is_lisp_pointer(array) && widetag_of(*native_pointer(array)) == widetag) {
995 if (length != NULL)
996 *length = fixnum_value(native_pointer(array)[1]);
997 return native_pointer(array) + 2;
998 } else {
999 return NULL;
1003 /* Only need to worry about scavenging the _real_ entries in the
1004 * table. Phantom entries such as the hash table itself at index 0 and
1005 * the empty marker at index 1 were scavenged by scav_vector that
1006 * either called this function directly or arranged for it to be
1007 * called later by pushing the hash table onto weak_hash_tables. */
1008 static void
1009 scav_hash_table_entries (struct hash_table *hash_table)
1011 lispobj *kv_vector;
1012 uword_t kv_length;
1013 lispobj *index_vector;
1014 uword_t length;
1015 lispobj *next_vector;
1016 uword_t next_vector_length;
1017 lispobj *hash_vector;
1018 uword_t hash_vector_length;
1019 lispobj empty_symbol;
1020 uword_t i;
1022 kv_vector = get_array_data(hash_table->table,
1023 SIMPLE_VECTOR_WIDETAG, &kv_length);
1024 if (kv_vector == NULL)
1025 lose("invalid kv_vector %x\n", hash_table->table);
1027 index_vector = get_array_data(hash_table->index_vector,
1028 SIMPLE_ARRAY_WORD_WIDETAG, &length);
1029 if (index_vector == NULL)
1030 lose("invalid index_vector %x\n", hash_table->index_vector);
1032 next_vector = get_array_data(hash_table->next_vector,
1033 SIMPLE_ARRAY_WORD_WIDETAG,
1034 &next_vector_length);
1035 if (next_vector == NULL)
1036 lose("invalid next_vector %x\n", hash_table->next_vector);
1038 hash_vector = get_array_data(hash_table->hash_vector,
1039 SIMPLE_ARRAY_WORD_WIDETAG,
1040 &hash_vector_length);
1041 if (hash_vector != NULL)
1042 gc_assert(hash_vector_length == next_vector_length);
1044 /* These lengths could be different as the index_vector can be a
1045 * different length from the others, a larger index_vector could
1046 * help reduce collisions. */
1047 gc_assert(next_vector_length*2 == kv_length);
1049 if ((empty_symbol = kv_vector[1]) != UNBOUND_MARKER_WIDETAG)
1050 lose("unexpected empty-hash-table-slot marker: %p\n", empty_symbol);
1052 /* Work through the KV vector. */
1053 int (*alivep_test)(lispobj,lispobj)
1054 = weak_hash_entry_alivep_fun[fixnum_value(hash_table->_weakness)];
1055 #define SCAV_ENTRIES(aliveness_predicate) \
1056 for (i = 1; i < next_vector_length; i++) { \
1057 lispobj old_key = kv_vector[2*i]; \
1058 lispobj __attribute__((unused)) value = kv_vector[2*i+1]; \
1059 if (aliveness_predicate) { \
1060 /* Scavenge the key and value. */ \
1061 scav_pair(&kv_vector[2*i]); \
1062 /* If an EQ-based key has moved, mark the hash-table for rehash */ \
1063 if (!hash_vector || hash_vector[i] == MAGIC_HASH_VECTOR_VALUE) { \
1064 lispobj new_key = kv_vector[2*i]; \
1065 if (old_key != new_key && new_key != empty_symbol) \
1066 hash_table->needs_rehash_p = T; \
1068 if (alivep_test)
1069 SCAV_ENTRIES(alivep_test(old_key, value))
1070 else
1071 SCAV_ENTRIES(1)
1074 sword_t
1075 scav_vector (lispobj *where, lispobj object)
1077 sword_t kv_length = fixnum_value(where[1]);
1078 struct hash_table *hash_table;
1080 /* SB-VM:VECTOR-VALID-HASHING-SUBTYPE is set for EQ-based and weak
1081 * hash tables in the Lisp HASH-TABLE code to indicate need for
1082 * special GC support. */
1083 if ((HeaderValue(object) & 0xFF) == subtype_VectorNormal) {
1084 normal:
1085 scavenge(where + 2, kv_length);
1086 return CEILING(kv_length + 2, 2);
1089 /* Scavenge element 0, which may be a hash-table structure. */
1090 scavenge(where+2, 1);
1091 if (!is_lisp_pointer(where[2])) {
1092 /* This'll happen when REHASH clears the header of old-kv-vector
1093 * and fills it with zero, but some other thread simulatenously
1094 * sets the header in %%PUTHASH.
1096 fprintf(stderr,
1097 "Warning: no pointer at %p in hash table: this indicates "
1098 "non-fatal corruption caused by concurrent access to a "
1099 "hash-table from multiple threads. Any accesses to "
1100 "hash-tables shared between threads should be protected "
1101 "by locks.\n", (void*)&where[2]);
1102 goto normal;
1104 hash_table = (struct hash_table *)native_pointer(where[2]);
1105 /*FSHOW((stderr,"/hash_table = %x\n", hash_table));*/
1106 if (widetag_of(hash_table->header) != INSTANCE_WIDETAG) {
1107 lose("hash table not instance (%x at %x)\n",
1108 hash_table->header,
1109 hash_table);
1112 /* Verify that vector element 1 is as expected.
1113 Don't bother scavenging it, since we lose() if it's not an immediate. */
1114 if (where[3] != UNBOUND_MARKER_WIDETAG)
1115 lose("unexpected empty-hash-table-slot marker: %p\n", where[3]);
1117 /* Scavenge hash table, which will fix the positions of the other
1118 * needed objects. */
1119 scav_instance((lispobj *)hash_table, hash_table->header);
1121 /* Cross-check the kv_vector. */
1122 if (where != native_pointer(hash_table->table)) {
1123 lose("hash_table table!=this table %x\n", hash_table->table);
1126 if (!hash_table->_weakness) {
1127 scav_hash_table_entries(hash_table);
1128 } else {
1129 /* Delay scavenging of this table by pushing it onto
1130 * weak_hash_tables (if it's not there already) for the weak
1131 * object phase. */
1132 if (hash_table->next_weak_hash_table == NIL) {
1133 hash_table->next_weak_hash_table = (lispobj)weak_hash_tables;
1134 weak_hash_tables = hash_table;
1138 return (CEILING(kv_length + 2, 2));
1141 void
1142 scav_weak_hash_tables (void)
1144 struct hash_table *table;
1146 /* Scavenge entries whose triggers are known to survive. */
1147 for (table = weak_hash_tables; table != NULL;
1148 table = (struct hash_table *)table->next_weak_hash_table) {
1149 scav_hash_table_entries(table);
1153 /* Walk through the chain whose first element is *FIRST and remove
1154 * dead weak entries. */
1155 static inline void
1156 scan_weak_hash_table_chain (struct hash_table *hash_table, lispobj *prev,
1157 lispobj *kv_vector, lispobj *index_vector,
1158 lispobj *next_vector, lispobj *hash_vector,
1159 lispobj empty_symbol, int (*alivep_test)(lispobj,lispobj))
1161 unsigned index = *prev;
1162 while (index) {
1163 unsigned next = next_vector[index];
1164 lispobj key = kv_vector[2 * index];
1165 lispobj value = kv_vector[2 * index + 1];
1166 gc_assert(key != empty_symbol);
1167 gc_assert(value != empty_symbol);
1168 if (!alivep_test(key, value)) {
1169 unsigned count = fixnum_value(hash_table->number_entries);
1170 gc_assert(count > 0);
1171 *prev = next;
1172 hash_table->number_entries = make_fixnum(count - 1);
1173 next_vector[index] = fixnum_value(hash_table->next_free_kv);
1174 hash_table->next_free_kv = make_fixnum(index);
1175 kv_vector[2 * index] = empty_symbol;
1176 kv_vector[2 * index + 1] = empty_symbol;
1177 if (hash_vector)
1178 hash_vector[index] = MAGIC_HASH_VECTOR_VALUE;
1179 } else {
1180 prev = &next_vector[index];
1182 index = next;
1186 static void
1187 scan_weak_hash_table (struct hash_table *hash_table)
1189 lispobj *kv_vector;
1190 lispobj *index_vector;
1191 uword_t length = 0; /* prevent warning */
1192 lispobj *next_vector;
1193 uword_t next_vector_length = 0; /* prevent warning */
1194 lispobj *hash_vector;
1195 lispobj empty_symbol;
1196 int (*alivep_test)(lispobj,lispobj) =
1197 weak_hash_entry_alivep_fun[fixnum_value(hash_table->_weakness)];
1198 uword_t i;
1200 kv_vector = get_array_data(hash_table->table,
1201 SIMPLE_VECTOR_WIDETAG, NULL);
1202 index_vector = get_array_data(hash_table->index_vector,
1203 SIMPLE_ARRAY_WORD_WIDETAG, &length);
1204 next_vector = get_array_data(hash_table->next_vector,
1205 SIMPLE_ARRAY_WORD_WIDETAG,
1206 &next_vector_length);
1207 hash_vector = get_array_data(hash_table->hash_vector,
1208 SIMPLE_ARRAY_WORD_WIDETAG, NULL);
1209 empty_symbol = kv_vector[1];
1211 for (i = 0; i < length; i++) {
1212 scan_weak_hash_table_chain(hash_table, &index_vector[i],
1213 kv_vector, index_vector, next_vector,
1214 hash_vector, empty_symbol, alivep_test);
1218 /* Remove dead entries from weak hash tables. */
1219 void
1220 scan_weak_hash_tables (void)
1222 struct hash_table *table, *next;
1224 for (table = weak_hash_tables; table != NULL; table = next) {
1225 next = (struct hash_table *)table->next_weak_hash_table;
1226 table->next_weak_hash_table = NIL;
1227 scan_weak_hash_table(table);
1230 weak_hash_tables = NULL;
1235 * initialization
1238 static sword_t
1239 scav_lose(lispobj *where, lispobj object)
1241 lose("no scavenge function for object %p (widetag 0x%x)\n",
1242 (uword_t)object,
1243 widetag_of(*where));
1245 return 0; /* bogus return value to satisfy static type checking */
1248 static lispobj
1249 trans_lose(lispobj object)
1251 lose("no transport function for object %p (widetag 0x%x)\n",
1252 (void*)object,
1253 widetag_of(*native_pointer(object)));
1254 return NIL; /* bogus return value to satisfy static type checking */
1257 static sword_t
1258 size_lose(lispobj *where)
1260 lose("no size function for object at %p (widetag 0x%x)\n",
1261 (void*)where,
1262 widetag_of(*where));
1263 return 1; /* bogus return value to satisfy static type checking */
1268 * initialization
1271 #include "genesis/gc-tables.h"
1274 static lispobj *search_spaces(void *pointer)
1276 lispobj *start;
1277 if (((start = search_dynamic_space(pointer)) != NULL) ||
1278 #ifdef LISP_FEATURE_IMMOBILE_SPACE
1279 ((start = search_immobile_space(pointer)) != NULL) ||
1280 #endif
1281 ((start = search_static_space(pointer)) != NULL) ||
1282 ((start = search_read_only_space(pointer)) != NULL))
1283 return start;
1284 return NULL;
1287 /* Find the code object for the given pc, or return NULL on
1288 failure. */
1289 lispobj *
1290 component_ptr_from_pc(lispobj *pc)
1292 lispobj *object = search_spaces(pc);
1294 if (object != NULL && widetag_of(*object) == CODE_HEADER_WIDETAG)
1295 return object;
1297 return NULL;
1300 /* Scan an area looking for an object which encloses the given pointer.
1301 * Return the object start on success, or NULL on failure. */
1302 lispobj *
1303 gc_search_space3(void *pointer, lispobj *start, void *limit)
1305 if (pointer < (void*)start || pointer >= limit) return NULL;
1307 size_t count;
1308 #if 0
1309 /* CAUTION: this code is _significantly_ slower than the production version
1310 due to the extra checks for forwarding. Only use it if debugging */
1311 for ( ; (void*)start < limit ; start += count) {
1312 lispobj *forwarded_start;
1313 if (forwarding_pointer_p(start))
1314 forwarded_start = native_pointer(forwarding_pointer_value(start));
1315 else
1316 forwarded_start = start;
1317 lispobj thing = *forwarded_start;
1318 count = OBJECT_SIZE(thing, forwarded_start);
1319 /* Check whether the pointer is within this object. */
1320 if (pointer < (void*)(start+count)) return start;
1322 #else
1323 for ( ; (void*)start < limit ; start += count) {
1324 lispobj thing = *start;
1325 count = OBJECT_SIZE(thing, start);
1326 /* Check whether the pointer is within this object. */
1327 if (pointer < (void*)(start+count)) return start;
1329 #endif
1330 return NULL;
1333 /* Helper for valid_lisp_pointer_p (below) and
1334 * conservative_root_p (gencgc).
1336 * pointer is the pointer to check validity of,
1337 * and start_addr is the address of the enclosing object.
1339 * This is actually quite simple to check: because the heap state is assumed
1340 * consistent, and 'start_addr' is known good, having come from
1341 * gc_search_space(), only the 'pointer' argument is dubious.
1342 * So make 'start_addr' into a tagged pointer and see if that matches 'pointer'.
1343 * If it does, then 'pointer' is valid.
1346 properly_tagged_p_internal(lispobj pointer, lispobj *start_addr)
1348 // If a headerless object, confirm that 'pointer' is a list pointer.
1349 // Given the precondition that the heap is in a valid state,
1350 // it may be assumed that one check of is_cons_half() suffices;
1351 // we don't need to check the other half.
1352 lispobj header = *start_addr;
1353 if (is_cons_half(header))
1354 return make_lispobj(start_addr, LIST_POINTER_LOWTAG) == pointer;
1356 // Because this heap object was not deemed to be a cons,
1357 // it must be an object header. Don't need a check except when paranoid.
1358 gc_dcheck(other_immediate_lowtag_p(header));
1360 // The space of potential widetags has 64 elements, not 256,
1361 // because of the constant low 2 bits.
1362 int widetag = widetag_of(header);
1363 int lowtag = lowtag_for_widetag[widetag>>2];
1364 if (lowtag && make_lispobj(start_addr, lowtag) == pointer)
1365 return 1; // instant win
1367 if (widetag == CODE_HEADER_WIDETAG) {
1368 // Check for RETURN_PC_HEADER first since it's quicker.
1369 // Then consider the embedded simple-funs.
1370 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
1371 /* The all-architecture test below is good as far as it goes,
1372 * but an LRA object is similar to a FUN-POINTER: It is
1373 * embedded within a CODE-OBJECT pointed to by start_addr, and
1374 * cannot be found by simply walking the heap, therefore we
1375 * need to check for it. -- AB, 2010-Jun-04 */
1376 if (lowtag_of(pointer) == OTHER_POINTER_LOWTAG) {
1377 lispobj *potential_lra = native_pointer(pointer);
1378 if ((widetag_of(potential_lra[0]) == RETURN_PC_WIDETAG) &&
1379 ((potential_lra - HeaderValue(potential_lra[0])) == start_addr)) {
1380 return 1; /* It's as good as we can verify. */
1383 #endif
1384 if (lowtag_of(pointer) == FUN_POINTER_LOWTAG) {
1385 struct simple_fun *pfun =
1386 (struct simple_fun*)(pointer-FUN_POINTER_LOWTAG);
1387 for_each_simple_fun(i, function, (struct code*)start_addr, 0, {
1388 if (pfun == function) return 1;
1392 return 0; // no good
1395 /* META: Note the ambiguous word "validate" in the comment below.
1396 * This means "Decide whether <x> is valid".
1397 * But when you see os_validate() elsewhere, that doesn't mean to ask
1398 * whether something is valid, it says to *make* it valid.
1399 * I think it would be nice if we could avoid using the word in the
1400 * sense in which os_validate() uses it, which would entail renaming
1401 * a bunch of stuff, which is harder than just explaining why
1402 * the comments can be deceptive */
1404 /* Used by the debugger to validate possibly bogus pointers before
1405 * calling MAKE-LISP-OBJ on them.
1407 * FIXME: We would like to make this perfect, because if the debugger
1408 * constructs a reference to a bugs lisp object, and it ends up in a
1409 * location scavenged by the GC all hell breaks loose.
1411 * Whereas conservative_root_p has to be conservative
1412 * and return true for all valid pointers, this could actually be eager
1413 * and lie about a few pointers without bad results... but that should
1414 * be reflected in the name.
1417 valid_lisp_pointer_p(lispobj pointer)
1419 lispobj *start = search_spaces((void*)pointer);
1420 if (start != NULL)
1421 return properly_tagged_descriptor_p((void*)pointer, start);
1422 return 0;
1425 boolean
1426 maybe_gc(os_context_t *context)
1428 lispobj gc_happened;
1429 struct thread *thread = arch_os_get_current_thread();
1430 boolean were_in_lisp = !foreign_function_call_active_p(thread);
1432 if (were_in_lisp) {
1433 fake_foreign_function_call(context);
1436 /* SUB-GC may return without GCing if *GC-INHIBIT* is set, in
1437 * which case we will be running with no gc trigger barrier
1438 * thing for a while. But it shouldn't be long until the end
1439 * of WITHOUT-GCING.
1441 * FIXME: It would be good to protect the end of dynamic space for
1442 * CheneyGC and signal a storage condition from there.
1445 /* Restore the signal mask from the interrupted context before
1446 * calling into Lisp if interrupts are enabled. Why not always?
1448 * Suppose there is a WITHOUT-INTERRUPTS block far, far out. If an
1449 * interrupt hits while in SUB-GC, it is deferred and the
1450 * os_context_sigmask of that interrupt is set to block further
1451 * deferrable interrupts (until the first one is
1452 * handled). Unfortunately, that context refers to this place and
1453 * when we return from here the signals will not be blocked.
1455 * A kludgy alternative is to propagate the sigmask change to the
1456 * outer context.
1458 #if !(defined(LISP_FEATURE_WIN32) || defined(LISP_FEATURE_SB_SAFEPOINT))
1459 check_gc_signals_unblocked_or_lose(os_context_sigmask_addr(context));
1460 unblock_gc_signals(0, 0);
1461 #endif
1462 FSHOW((stderr, "/maybe_gc: calling SUB_GC\n"));
1463 /* FIXME: Nothing must go wrong during GC else we end up running
1464 * the debugger, error handlers, and user code in general in a
1465 * potentially unsafe place. Running out of the control stack or
1466 * the heap in SUB-GC are ways to lose. Of course, deferrables
1467 * cannot be unblocked because there may be a pending handler, or
1468 * we may even be in a WITHOUT-INTERRUPTS. */
1469 gc_happened = funcall0(StaticSymbolFunction(SUB_GC));
1470 FSHOW((stderr, "/maybe_gc: gc_happened=%s\n",
1471 (gc_happened == NIL)
1472 ? "NIL"
1473 : ((gc_happened == T)
1474 ? "T"
1475 : "0")));
1476 /* gc_happened can take three values: T, NIL, 0.
1478 * T means that the thread managed to trigger a GC, and post-gc
1479 * must be called.
1481 * NIL means that the thread is within without-gcing, and no GC
1482 * has occurred.
1484 * Finally, 0 means that *a* GC has occurred, but it wasn't
1485 * triggered by this thread; success, but post-gc doesn't have
1486 * to be called.
1488 if ((gc_happened == T) &&
1489 /* See if interrupts are enabled or it's possible to enable
1490 * them. POST-GC has a similar check, but we don't want to
1491 * unlock deferrables in that case and get a pending interrupt
1492 * here. */
1493 ((SymbolValue(INTERRUPTS_ENABLED,thread) != NIL) ||
1494 (SymbolValue(ALLOW_WITH_INTERRUPTS,thread) != NIL))) {
1495 #ifndef LISP_FEATURE_WIN32
1496 sigset_t *context_sigmask = os_context_sigmask_addr(context);
1497 if (!deferrables_blocked_p(context_sigmask)) {
1498 thread_sigmask(SIG_SETMASK, context_sigmask, 0);
1499 #ifndef LISP_FEATURE_SB_SAFEPOINT
1500 check_gc_signals_unblocked_or_lose(0);
1501 #endif
1502 #endif
1503 FSHOW((stderr, "/maybe_gc: calling POST_GC\n"));
1504 funcall0(StaticSymbolFunction(POST_GC));
1505 #ifndef LISP_FEATURE_WIN32
1506 } else {
1507 FSHOW((stderr, "/maybe_gc: punting on POST_GC due to blockage\n"));
1509 #endif
1512 if (were_in_lisp) {
1513 undo_fake_foreign_function_call(context);
1514 } else {
1515 /* Otherwise done by undo_fake_foreign_function_call. And
1516 something later wants them to be blocked. What a nice
1517 interface.*/
1518 block_blockable_signals(0);
1521 FSHOW((stderr, "/maybe_gc: returning\n"));
1522 return (gc_happened != NIL);
1525 #define BYTES_ZERO_BEFORE_END (1<<12)
1527 /* There used to be a similar function called SCRUB-CONTROL-STACK in
1528 * Lisp and another called zero_stack() in cheneygc.c, but since it's
1529 * shorter to express in, and more often called from C, I keep only
1530 * the C one after fixing it. -- MG 2009-03-25 */
1532 /* Zero the unused portion of the control stack so that old objects
1533 * are not kept alive because of uninitialized stack variables.
1535 * "To summarize the problem, since not all allocated stack frame
1536 * slots are guaranteed to be written by the time you call an another
1537 * function or GC, there may be garbage pointers retained in your dead
1538 * stack locations. The stack scrubbing only affects the part of the
1539 * stack from the SP to the end of the allocated stack." - ram, on
1540 * cmucl-imp, Tue, 25 Sep 2001
1542 * So, as an (admittedly lame) workaround, from time to time we call
1543 * scrub-control-stack to zero out all the unused portion. This is
1544 * supposed to happen when the stack is mostly empty, so that we have
1545 * a chance of clearing more of it: callers are currently (2002.07.18)
1546 * REPL, SUB-GC and sig_stop_for_gc_handler. */
1548 /* Take care not to tread on the guard page and the hard guard page as
1549 * it would be unkind to sig_stop_for_gc_handler. Touching the return
1550 * guard page is not dangerous. For this to work the guard page must
1551 * be zeroed when protected. */
1553 /* FIXME: I think there is no guarantee that once
1554 * BYTES_ZERO_BEFORE_END bytes are zero the rest are also zero. This
1555 * may be what the "lame" adjective in the above comment is for. In
1556 * this case, exact gc may lose badly. */
1557 void
1558 scrub_control_stack()
1560 scrub_thread_control_stack(arch_os_get_current_thread());
1563 void
1564 scrub_thread_control_stack(struct thread *th)
1566 os_vm_address_t guard_page_address = CONTROL_STACK_GUARD_PAGE(th);
1567 os_vm_address_t hard_guard_page_address = CONTROL_STACK_HARD_GUARD_PAGE(th);
1568 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
1569 /* On these targets scrubbing from C is a bad idea, so we punt to
1570 * a routine in $ARCH-assem.S. */
1571 extern void arch_scrub_control_stack(struct thread *, os_vm_address_t, os_vm_address_t);
1572 arch_scrub_control_stack(th, guard_page_address, hard_guard_page_address);
1573 #else
1574 lispobj *sp = access_control_stack_pointer(th);
1575 scrub:
1576 if ((((os_vm_address_t)sp < (hard_guard_page_address + os_vm_page_size)) &&
1577 ((os_vm_address_t)sp >= hard_guard_page_address)) ||
1578 (((os_vm_address_t)sp < (guard_page_address + os_vm_page_size)) &&
1579 ((os_vm_address_t)sp >= guard_page_address) &&
1580 (th->control_stack_guard_page_protected != NIL)))
1581 return;
1582 #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD
1583 do {
1584 *sp = 0;
1585 } while (((uword_t)sp--) & (BYTES_ZERO_BEFORE_END - 1));
1586 if ((os_vm_address_t)sp < (hard_guard_page_address + os_vm_page_size))
1587 return;
1588 do {
1589 if (*sp)
1590 goto scrub;
1591 } while (((uword_t)sp--) & (BYTES_ZERO_BEFORE_END - 1));
1592 #else
1593 do {
1594 *sp = 0;
1595 } while (((uword_t)++sp) & (BYTES_ZERO_BEFORE_END - 1));
1596 if ((os_vm_address_t)sp >= hard_guard_page_address)
1597 return;
1598 do {
1599 if (*sp)
1600 goto scrub;
1601 } while (((uword_t)++sp) & (BYTES_ZERO_BEFORE_END - 1));
1602 #endif
1603 #endif /* LISP_FEATURE_C_STACK_IS_CONTROL_STACK */
1606 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
1608 void
1609 scavenge_control_stack(struct thread *th)
1611 lispobj *object_ptr;
1613 /* In order to properly support dynamic-extent allocation of
1614 * non-CONS objects, the control stack requires special handling.
1615 * Rather than calling scavenge() directly, grovel over it fixing
1616 * broken hearts, scavenging pointers to oldspace, and pitching a
1617 * fit when encountering unboxed data. This prevents stray object
1618 * headers from causing the scavenger to blow past the end of the
1619 * stack (an error case checked in scavenge()). We don't worry
1620 * about treating unboxed words as boxed or vice versa, because
1621 * the compiler isn't allowed to store unboxed objects on the
1622 * control stack. -- AB, 2011-Dec-02 */
1624 for (object_ptr = th->control_stack_start;
1625 object_ptr < access_control_stack_pointer(th);
1626 object_ptr++) {
1628 lispobj object = *object_ptr;
1629 #ifdef LISP_FEATURE_GENCGC
1630 if (forwarding_pointer_p(object_ptr))
1631 lose("unexpected forwarding pointer in scavenge_control_stack: %p, start=%p, end=%p\n",
1632 object_ptr, th->control_stack_start, access_control_stack_pointer(th));
1633 #endif
1634 if (is_lisp_pointer(object) && from_space_p(object)) {
1635 /* It currently points to old space. Check for a
1636 * forwarding pointer. */
1637 lispobj *ptr = native_pointer(object);
1638 if (forwarding_pointer_p(ptr)) {
1639 /* Yes, there's a forwarding pointer. */
1640 *object_ptr = LOW_WORD(forwarding_pointer_value(ptr));
1641 } else {
1642 /* Scavenge that pointer. */
1643 long n_words_scavenged =
1644 (scavtab[widetag_of(object)])(object_ptr, object);
1645 gc_assert(n_words_scavenged == 1);
1647 } else if (scavtab[widetag_of(object)] == scav_lose) {
1648 lose("unboxed object in scavenge_control_stack: %p->%x, start=%p, end=%p\n",
1649 object_ptr, object, th->control_stack_start, access_control_stack_pointer(th));
1654 /* Scavenging Interrupt Contexts */
1656 static int boxed_registers[] = BOXED_REGISTERS;
1658 /* The GC has a notion of an "interior pointer" register, an unboxed
1659 * register that typically contains a pointer to inside an object
1660 * referenced by another pointer. The most obvious of these is the
1661 * program counter, although many compiler backends define a "Lisp
1662 * Interior Pointer" register known to the runtime as reg_LIP, and
1663 * various CPU architectures have other registers that also partake of
1664 * the interior-pointer nature. As the code for pairing an interior
1665 * pointer value up with its "base" register, and fixing it up after
1666 * scavenging is complete is horribly repetitive, a few macros paper
1667 * over the monotony. --AB, 2010-Jul-14 */
1669 /* These macros are only ever used over a lexical environment which
1670 * defines a pointer to an os_context_t called context, thus we don't
1671 * bother to pass that context in as a parameter. */
1673 /* Define how to access a given interior pointer. */
1674 #define ACCESS_INTERIOR_POINTER_pc \
1675 *os_context_pc_addr(context)
1676 #define ACCESS_INTERIOR_POINTER_lip \
1677 *os_context_register_addr(context, reg_LIP)
1678 #define ACCESS_INTERIOR_POINTER_lr \
1679 *os_context_lr_addr(context)
1680 #define ACCESS_INTERIOR_POINTER_npc \
1681 *os_context_npc_addr(context)
1682 #define ACCESS_INTERIOR_POINTER_ctr \
1683 *os_context_ctr_addr(context)
1685 #define INTERIOR_POINTER_VARS(name) \
1686 uword_t name##_offset; \
1687 int name##_register_pair
1689 #define PAIR_INTERIOR_POINTER(name) \
1690 pair_interior_pointer(context, \
1691 ACCESS_INTERIOR_POINTER_##name, \
1692 &name##_offset, \
1693 &name##_register_pair)
1695 /* One complexity here is that if a paired register is not found for
1696 * an interior pointer, then that pointer does not get updated.
1697 * Originally, there was some commentary about using an index of -1
1698 * when calling os_context_register_addr() on SPARC referring to the
1699 * program counter, but the real reason is to allow an interior
1700 * pointer register to point to the runtime, read-only space, or
1701 * static space without problems. */
1702 #define FIXUP_INTERIOR_POINTER(name) \
1703 do { \
1704 if (name##_register_pair >= 0) { \
1705 ACCESS_INTERIOR_POINTER_##name = \
1706 (*os_context_register_addr(context, \
1707 name##_register_pair) \
1708 & ~LOWTAG_MASK) \
1709 + name##_offset; \
1711 } while (0)
1714 static void
1715 pair_interior_pointer(os_context_t *context, uword_t pointer,
1716 uword_t *saved_offset, int *register_pair)
1718 unsigned int i;
1721 * I (RLT) think this is trying to find the boxed register that is
1722 * closest to the LIP address, without going past it. Usually, it's
1723 * reg_CODE or reg_LRA. But sometimes, nothing can be found.
1725 /* 0x7FFFFFFF on 32-bit platforms;
1726 0x7FFFFFFFFFFFFFFF on 64-bit platforms */
1727 *saved_offset = (((uword_t)1) << (N_WORD_BITS - 1)) - 1;
1728 *register_pair = -1;
1729 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
1730 uword_t reg;
1731 uword_t offset;
1732 int index;
1734 index = boxed_registers[i];
1735 reg = *os_context_register_addr(context, index);
1737 /* An interior pointer is never relative to a non-pointer
1738 * register (an oversight in the original implementation).
1739 * The simplest argument for why this is true is to consider
1740 * the fixnum that happens by coincide to be the word-index in
1741 * memory of the header for some object plus two. This is
1742 * happenstance would cause the register containing the fixnum
1743 * to be selected as the register_pair if the interior pointer
1744 * is to anywhere after the first two words of the object.
1745 * The fixnum won't be changed during GC, but the object might
1746 * move, thus destroying the interior pointer. --AB,
1747 * 2010-Jul-14 */
1749 if (is_lisp_pointer(reg) &&
1750 ((reg & ~LOWTAG_MASK) <= pointer)) {
1751 offset = pointer - (reg & ~LOWTAG_MASK);
1752 if (offset < *saved_offset) {
1753 *saved_offset = offset;
1754 *register_pair = index;
1760 static void
1761 scavenge_interrupt_context(os_context_t * context)
1763 unsigned int i;
1765 /* FIXME: The various #ifdef noise here is precisely that: noise.
1766 * Is it possible to fold it into the macrology so that we have
1767 * one set of #ifdefs and then INTERIOR_POINTER_VARS /et alia/
1768 * compile out for the registers that don't exist on a given
1769 * platform? */
1771 INTERIOR_POINTER_VARS(pc);
1772 #ifdef reg_LIP
1773 INTERIOR_POINTER_VARS(lip);
1774 #endif
1775 #ifdef ARCH_HAS_LINK_REGISTER
1776 INTERIOR_POINTER_VARS(lr);
1777 #endif
1778 #ifdef ARCH_HAS_NPC_REGISTER
1779 INTERIOR_POINTER_VARS(npc);
1780 #endif
1781 #ifdef LISP_FEATURE_PPC
1782 INTERIOR_POINTER_VARS(ctr);
1783 #endif
1785 PAIR_INTERIOR_POINTER(pc);
1786 #ifdef reg_LIP
1787 PAIR_INTERIOR_POINTER(lip);
1788 #endif
1789 #ifdef ARCH_HAS_LINK_REGISTER
1790 PAIR_INTERIOR_POINTER(lr);
1791 #endif
1792 #ifdef ARCH_HAS_NPC_REGISTER
1793 PAIR_INTERIOR_POINTER(npc);
1794 #endif
1795 #ifdef LISP_FEATURE_PPC
1796 PAIR_INTERIOR_POINTER(ctr);
1797 #endif
1799 /* Scavenge all boxed registers in the context. */
1800 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
1801 os_context_register_t *boxed_reg;
1802 lispobj datum;
1804 /* We can't "just" cast os_context_register_addr() to a
1805 * pointer to lispobj and pass it to scavenge, because some
1806 * systems can have a wider register width than we use for
1807 * lisp objects, and on big-endian systems casting a pointer
1808 * to a narrower target type doesn't work properly.
1809 * Therefore, we copy the value out to a temporary lispobj
1810 * variable, scavenge there, and copy the value back in.
1812 * FIXME: lispobj is unsigned, os_context_register_t may be
1813 * signed or unsigned, are we truncating or sign-extending
1814 * values here that shouldn't be modified? Possibly affects
1815 * any architecture that has 32-bit and 64-bit variants where
1816 * we run in 32-bit mode on 64-bit hardware when the OS is set
1817 * up for 64-bit from the start. Or an environment with
1818 * 32-bit addresses and 64-bit registers. */
1820 boxed_reg = os_context_register_addr(context, boxed_registers[i]);
1821 datum = *boxed_reg;
1822 scavenge(&datum, 1);
1823 *boxed_reg = datum;
1826 /* Now that the scavenging is done, repair the various interior
1827 * pointers. */
1828 FIXUP_INTERIOR_POINTER(pc);
1829 #ifdef reg_LIP
1830 FIXUP_INTERIOR_POINTER(lip);
1831 #endif
1832 #ifdef ARCH_HAS_LINK_REGISTER
1833 FIXUP_INTERIOR_POINTER(lr);
1834 #endif
1835 #ifdef ARCH_HAS_NPC_REGISTER
1836 FIXUP_INTERIOR_POINTER(npc);
1837 #endif
1838 #ifdef LISP_FEATURE_PPC
1839 FIXUP_INTERIOR_POINTER(ctr);
1840 #endif
1843 void
1844 scavenge_interrupt_contexts(struct thread *th)
1846 int i, index;
1847 os_context_t *context;
1849 index = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
1851 #if defined(DEBUG_PRINT_CONTEXT_INDEX)
1852 printf("Number of active contexts: %d\n", index);
1853 #endif
1855 for (i = 0; i < index; i++) {
1856 context = th->interrupt_contexts[i];
1857 scavenge_interrupt_context(context);
1860 #endif /* x86oid targets */
1862 void varint_unpacker_init(struct varint_unpacker* unpacker, lispobj integer)
1864 if (fixnump(integer)) {
1865 unpacker->word = fixnum_value(integer);
1866 unpacker->limit = N_WORD_BYTES;
1867 unpacker->data = (char*)&unpacker->word;
1868 } else {
1869 struct bignum* bignum = (struct bignum*)(integer - OTHER_POINTER_LOWTAG);
1870 unpacker->word = 0;
1871 unpacker->limit = HeaderValue(bignum->header) * N_WORD_BYTES;
1872 unpacker->data = (char*)bignum->digits;
1874 unpacker->index = 0;
1877 // Fetch the next varint from 'unpacker' into 'result'.
1878 // Because there is no length prefix on the number of varints encoded,
1879 // spurious trailing zeros might be observed. The data consumer can
1880 // circumvent that by storing a count as the first value in the series.
1881 // Return 1 for success, 0 for EOF.
1882 int varint_unpack(struct varint_unpacker* unpacker, int* result)
1884 if (unpacker->index >= unpacker->limit) return 0;
1885 int accumulator = 0;
1886 int shift = 0;
1887 while (1) {
1888 #ifdef LISP_FEATURE_LITTLE_ENDIAN
1889 int byte = unpacker->data[unpacker->index];
1890 #else
1891 // bignums are little-endian in word order,
1892 // but machine-native within each word.
1893 // We could pack bytes MSB-to-LSB in the bigdigits,
1894 // but that seems less intuitive on the Lisp side.
1895 int word_index = unpacker->index / N_WORD_BYTES;
1896 int byte_index = unpacker->index % N_WORD_BYTES;
1897 int byte = (((unsigned int*)unpacker->data)[word_index]
1898 >> (byte_index * 8)) & 0xFF;
1899 #endif
1900 ++unpacker->index;
1901 accumulator |= (byte & 0x7F) << shift;
1902 if (!(byte & 0x80)) break;
1903 gc_assert(unpacker->index < unpacker->limit);
1904 shift += 7;
1906 *result = accumulator;
1907 return 1;
1910 /* Our own implementation of heapsort, because some C libraries have a qsort()
1911 * that calls malloc() apparently, which we MUST NOT do. */
1913 typedef uword_t* heap;
1915 #define swap(a,i,j) { uword_t temp=a[i];a[i]=a[j];a[j]=temp; }
1916 static void sift_down(heap array, int start, int end)
1918 int root = start;
1919 while (root * 2 + 1 <= end) {
1920 int child = root * 2 + 1;
1921 if (child + 1 <= end && array[child] < array[child+1])
1922 ++child;
1923 if (array[root] < array[child]) {
1924 swap(array, root, child);
1925 root = child;
1926 } else {
1927 return;
1932 static void heapify(heap array, int length)
1934 int start = (length - 2) / 2;
1935 while (start >= 0) {
1936 sift_down(array, start, length-1);
1937 --start;
1941 void gc_heapsort_uwords(heap array, int length)
1943 heapify(array, length);
1944 int end = length - 1;
1945 while (end > 0) {
1946 swap(array, end, 0);
1947 --end;
1948 sift_down(array, 0, end);
1952 //// Coalescing of constant vectors for SAVE-LISP-AND-DIE
1954 static boolean coalescible_number_p(lispobj* where)
1956 int widetag = widetag_of(*where);
1957 return widetag == BIGNUM_WIDETAG
1958 // Ratios and complex integers containing pointers to bignums don't work.
1959 || ((widetag == RATIO_WIDETAG || widetag == COMPLEX_WIDETAG)
1960 && fixnump(where[1]) && fixnump(where[2]))
1961 #ifndef LISP_FEATURE_64_BIT
1962 || widetag == SINGLE_FLOAT_WIDETAG
1963 #endif
1964 || widetag == DOUBLE_FLOAT_WIDETAG
1965 || widetag == COMPLEX_SINGLE_FLOAT_WIDETAG
1966 || widetag == COMPLEX_DOUBLE_FLOAT_WIDETAG;
1969 /// Return true of fixnums, bignums, strings, symbols.
1970 /// Strings are considered eql-comparable,
1971 /// because they're coalesced before comparing.
1972 static boolean eql_comparable_p(lispobj obj)
1974 if (fixnump(obj) || obj == NIL) return 1;
1975 if (lowtag_of(obj) != OTHER_POINTER_LOWTAG) return 0;
1976 int widetag = widetag_of(*native_pointer(obj));
1977 return widetag == BIGNUM_WIDETAG
1978 || widetag == SYMBOL_WIDETAG
1979 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
1980 || widetag == SIMPLE_CHARACTER_STRING_WIDETAG
1981 #endif
1982 || widetag == SIMPLE_BASE_STRING_WIDETAG;
1985 static boolean vector_isevery(boolean (*pred)(lispobj), struct vector* v)
1987 int i;
1988 for (i = fixnum_value(v->length)-1; i >= 0; --i)
1989 if (!pred(v->data[i])) return 0;
1990 return 1;
1993 static void coalesce_obj(lispobj* where, struct hopscotch_table* ht)
1995 lispobj ptr = *where;
1996 if (lowtag_of(ptr) != OTHER_POINTER_LOWTAG)
1997 return;
1999 extern char gc_coalesce_string_literals;
2000 // gc_coalesce_string_literals represents the "aggressiveness" level.
2001 // If 1, then we share vectors tagged as +VECTOR-SHAREABLE+,
2002 // but if >1, those and also +VECTOR-SHAREABLE-NONSTD+.
2003 int mask = gc_coalesce_string_literals > 1
2004 ? (VECTOR_SHAREABLE|VECTOR_SHAREABLE_NONSTD)<<N_WIDETAG_BITS
2005 : (VECTOR_SHAREABLE )<<N_WIDETAG_BITS;
2007 lispobj* obj = native_pointer(ptr);
2008 lispobj header = *obj;
2009 int widetag = widetag_of(header);
2011 if ((((header & mask) != 0) // optimistically assume it's a vector
2012 && ((widetag == SIMPLE_VECTOR_WIDETAG
2013 && vector_isevery(eql_comparable_p, (struct vector*)obj))
2014 || specialized_vector_widetag_p(widetag)))
2015 || coalescible_number_p(obj)) {
2016 if (widetag == SIMPLE_VECTOR_WIDETAG) {
2017 sword_t n_elts = fixnum_value(obj[1]), i;
2018 for (i = 2 ; i < n_elts+2 ; ++i)
2019 coalesce_obj(obj + i, ht);
2021 int index = hopscotch_get(ht, (uword_t)obj, 0);
2022 if (!index) // Not found
2023 hopscotch_insert(ht, (uword_t)obj, 1);
2024 else
2025 *where = make_lispobj((void*)ht->keys[index-1], OTHER_POINTER_LOWTAG);
2029 static uword_t coalesce_range(lispobj* where, lispobj* limit, uword_t arg)
2031 struct hopscotch_table* ht = (struct hopscotch_table*)arg;
2032 lispobj layout, bitmap, *next;
2033 sword_t nwords, i, j;
2035 for ( ; where < limit ; where = next ) {
2036 lispobj header = *where;
2037 if (is_cons_half(header)) {
2038 coalesce_obj(where+0, ht);
2039 coalesce_obj(where+1, ht);
2040 next = where + 2;
2041 } else {
2042 int widetag = widetag_of(header);
2043 nwords = sizetab[widetag](where);
2044 next = where + nwords;
2045 switch (widetag) {
2046 case INSTANCE_WIDETAG: // mixed boxed/unboxed objects
2047 #ifdef LISP_FEATURE_COMPACT_INSTANCE_HEADER
2048 case FUNCALLABLE_INSTANCE_WIDETAG:
2049 #endif
2050 layout = instance_layout(where);
2051 bitmap = LAYOUT(layout)->bitmap;
2052 for(i=1; i<nwords; ++i)
2053 if (layout_bitmap_logbitp(i-1, bitmap))
2054 coalesce_obj(where+i, ht);
2055 continue;
2056 case CODE_HEADER_WIDETAG:
2057 for_each_simple_fun(i, fun, (struct code*)where, 0, {
2058 lispobj* fun_slots = SIMPLE_FUN_SCAV_START(fun);
2059 for (j=0; j<SIMPLE_FUN_SCAV_NWORDS(fun); ++j)
2060 coalesce_obj(fun_slots+j, ht);
2062 nwords = code_header_words(header);
2063 break;
2064 default:
2065 if (unboxed_obj_widetag_p(widetag))
2066 continue; // Ignore this object.
2068 for(i=1; i<nwords; ++i)
2069 coalesce_obj(where+i, ht);
2072 return 0;
2075 void coalesce_similar_objects()
2077 struct hopscotch_table ht;
2078 hopscotch_create(&ht, HOPSCOTCH_VECTOR_HASH, 0, 1<<17, 0);
2079 #ifndef LISP_FEATURE_WIN32
2080 // Apparently this triggers the "Unable to recommit" lossage message
2081 // in handle_access_violation() in src/runtime/win32-os.c
2082 coalesce_range((lispobj*)STATIC_SPACE_START,
2083 (lispobj*)STATIC_SPACE_END,
2084 (uword_t)&ht);
2085 #endif
2086 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2087 coalesce_range((lispobj*)IMMOBILE_SPACE_START,
2088 immobile_fixedobj_free_pointer,
2089 (uword_t)&ht);
2090 coalesce_range((lispobj*)IMMOBILE_VARYOBJ_SUBSPACE_START,
2091 immobile_space_free_pointer,
2092 (uword_t)&ht);
2093 #endif
2094 #ifdef LISP_FEATURE_GENCGC
2095 walk_generation(coalesce_range, -1, (uword_t)&ht);
2096 #else
2097 // FIXME: implement
2098 #endif
2099 hopscotch_destroy(&ht);