3 /* Scan all the objects in the card */
4 void copy_card(F_CARD *ptr, CELL gen, CELL here)
6 CELL card_scan = (CELL)CARD_TO_ADDR(ptr) + CARD_OFFSET(ptr);
7 CELL card_end = (CELL)CARD_TO_ADDR(ptr + 1);
12 copy_reachable_objects(card_scan,&card_end);
17 void copy_card_deck(F_DECK *deck, CELL gen, F_CARD mask, F_CARD unmask)
19 F_CARD *first_card = DECK_TO_CARD(deck);
20 F_CARD *last_card = DECK_TO_CARD(deck + 1);
22 CELL here = data_heap->generations[gen].here;
25 u32 quad_mask = mask | (mask << 8) | (mask << 16) | (mask << 24);
27 for(quad_ptr = (u32 *)first_card; quad_ptr < (u32 *)last_card; quad_ptr++)
29 if(*quad_ptr & quad_mask)
31 F_CARD *ptr = (F_CARD *)quad_ptr;
34 for(card = 0; card < 4; card++)
38 copy_card(&ptr[card],gen,here);
48 /* Copy all newspace objects referenced from marked cards to the destination */
49 void copy_gen_cards(CELL gen)
51 F_DECK *first_deck = ADDR_TO_DECK(data_heap->generations[gen].start);
52 F_DECK *last_deck = ADDR_TO_DECK(data_heap->generations[gen].end);
56 /* if we are collecting the nursery, we care about old->nursery pointers
57 but not old->aging pointers */
58 if(collecting_gen == NURSERY)
60 mask = CARD_POINTS_TO_NURSERY;
62 /* after the collection, no old->nursery pointers remain
63 anywhere, but old->aging pointers might remain in tenured
66 unmask = CARD_POINTS_TO_NURSERY;
67 /* after the collection, all cards in aging space can be
69 else if(HAVE_AGING_P && gen == AGING)
70 unmask = CARD_MARK_MASK;
73 critical_error("bug in copy_gen_cards",gen);
77 /* if we are collecting aging space into tenured space, we care about
78 all old->nursery and old->aging pointers. no old->aging pointers can
80 else if(HAVE_AGING_P && collecting_gen == AGING)
82 if(collecting_aging_again)
84 mask = CARD_POINTS_TO_AGING;
85 unmask = CARD_MARK_MASK;
87 /* after we collect aging space into the aging semispace, no
88 old->nursery pointers remain but tenured space might still have
89 pointers to aging space. */
92 mask = CARD_POINTS_TO_AGING;
93 unmask = CARD_POINTS_TO_NURSERY;
98 critical_error("bug in copy_gen_cards",gen);
104 for(ptr = first_deck; ptr < last_deck; ptr++)
108 copy_card_deck(ptr,gen,mask,unmask);
114 /* Scan cards in all generations older than the one being collected, copying
115 old->new references */
116 void copy_cards(void)
118 u64 start = current_micros();
121 for(i = collecting_gen + 1; i < data_heap->gen_count; i++)
124 card_scan_time += (current_micros() - start);
127 /* Copy all tagged pointers in a range of memory */
128 void copy_stack_elements(F_SEGMENT *region, CELL top)
130 CELL ptr = region->start;
132 for(; ptr <= top; ptr += CELLS)
133 copy_handle((CELL*)ptr);
136 void copy_registered_locals(void)
138 CELL ptr = gc_locals_region->start;
140 for(; ptr <= gc_locals; ptr += CELLS)
141 copy_handle(*(CELL **)ptr);
144 /* Copy roots over at the start of GC, namely various constants, stacks,
145 the user environment and extra roots registered with REGISTER_ROOT */
146 void copy_roots(void)
149 copy_handle(&bignum_zero);
150 copy_handle(&bignum_pos_one);
151 copy_handle(&bignum_neg_one);
153 copy_registered_locals();
154 copy_stack_elements(extra_roots_region,extra_roots);
156 if(!performing_compaction)
159 F_CONTEXT *stacks = stack_chain;
163 copy_stack_elements(stacks->datastack_region,stacks->datastack);
164 copy_stack_elements(stacks->retainstack_region,stacks->retainstack);
166 copy_handle(&stacks->catchstack_save);
167 copy_handle(&stacks->current_callback_save);
169 mark_active_blocks(stacks);
171 stacks = stacks->next;
176 for(i = 0; i < USER_ENV; i++)
177 copy_handle(&userenv[i]);
180 /* Given a pointer to oldspace, copy it to newspace */
181 INLINE void *copy_untagged_object(void *pointer, CELL size)
183 if(newspace->here + size >= newspace->end)
185 allot_barrier(newspace->here);
186 void *newpointer = allot_zone(newspace,size);
188 F_GC_STATS *s = &gc_stats[collecting_gen];
190 s->bytes_copied += size;
192 memcpy(newpointer,pointer,size);
196 INLINE void forward_object(CELL pointer, CELL newpointer)
198 if(pointer != newpointer)
199 put(UNTAG(pointer),RETAG(newpointer,GC_COLLECTED));
202 INLINE CELL copy_object_impl(CELL pointer)
204 CELL newpointer = (CELL)copy_untagged_object(
205 (void*)UNTAG(pointer),
206 object_size(pointer));
207 forward_object(pointer,newpointer);
211 /* Follow a chain of forwarding pointers */
212 CELL resolve_forwarding(CELL untagged, CELL tag)
214 CELL header = get(untagged);
215 /* another forwarding pointer */
216 if(TAG(header) == GC_COLLECTED)
217 return resolve_forwarding(UNTAG(header),tag);
218 /* we've found the destination */
221 CELL pointer = RETAG(untagged,tag);
222 if(should_copy(untagged))
223 pointer = RETAG(copy_object_impl(pointer),tag);
228 /* Given a pointer to a tagged pointer to oldspace, copy it to newspace.
229 If the object has already been copied, return the forwarding
230 pointer address without copying anything; otherwise, install
231 a new forwarding pointer. */
232 INLINE CELL copy_object(CELL pointer)
234 CELL tag = TAG(pointer);
235 CELL header = get(UNTAG(pointer));
237 if(TAG(header) == GC_COLLECTED)
238 return resolve_forwarding(UNTAG(header),tag);
240 return RETAG(copy_object_impl(pointer),tag);
243 void copy_handle(CELL *handle)
245 CELL pointer = *handle;
247 if(!immediate_p(pointer) && should_copy(pointer))
248 *handle = copy_object(pointer);
251 CELL copy_next_from_nursery(CELL scan)
253 CELL *obj = (CELL *)scan;
254 CELL *end = (CELL *)(scan + binary_payload_start(scan));
260 CELL nursery_start = nursery.start;
261 CELL nursery_end = nursery.end;
263 for(; obj < end; obj++)
267 if(!immediate_p(pointer)
268 && (pointer >= nursery_start && pointer < nursery_end))
269 *obj = copy_object(pointer);
273 return scan + untagged_object_size(scan);
276 CELL copy_next_from_aging(CELL scan)
278 CELL *obj = (CELL *)scan;
279 CELL *end = (CELL *)(scan + binary_payload_start(scan));
285 CELL tenured_start = data_heap->generations[TENURED].start;
286 CELL tenured_end = data_heap->generations[TENURED].end;
288 CELL newspace_start = newspace->start;
289 CELL newspace_end = newspace->end;
291 for(; obj < end; obj++)
295 if(!immediate_p(pointer)
296 && !(pointer >= newspace_start && pointer < newspace_end)
297 && !(pointer >= tenured_start && pointer < tenured_end))
298 *obj = copy_object(pointer);
302 return scan + untagged_object_size(scan);
305 CELL copy_next_from_tenured(CELL scan)
307 CELL *obj = (CELL *)scan;
308 CELL *end = (CELL *)(scan + binary_payload_start(scan));
314 CELL newspace_start = newspace->start;
315 CELL newspace_end = newspace->end;
317 for(; obj < end; obj++)
321 if(!immediate_p(pointer) && !(pointer >= newspace_start && pointer < newspace_end))
322 *obj = copy_object(pointer);
326 mark_object_code_block(scan);
328 return scan + untagged_object_size(scan);
331 void copy_reachable_objects(CELL scan, CELL *end)
333 if(collecting_gen == NURSERY)
336 scan = copy_next_from_nursery(scan);
338 else if(HAVE_AGING_P && collecting_gen == AGING)
341 scan = copy_next_from_aging(scan);
343 else if(collecting_gen == TENURED)
346 scan = copy_next_from_tenured(scan);
350 /* Prepare to start copying reachable objects into an unused zone */
351 void begin_gc(CELL requested_bytes)
353 if(growing_data_heap)
355 if(collecting_gen != TENURED)
356 critical_error("Invalid parameters to begin_gc",0);
358 old_data_heap = data_heap;
359 set_data_heap(grow_data_heap(old_data_heap,requested_bytes));
360 newspace = &data_heap->generations[TENURED];
362 else if(collecting_accumulation_gen_p())
364 /* when collecting one of these generations, rotate it
365 with the semispace */
366 F_ZONE z = data_heap->generations[collecting_gen];
367 data_heap->generations[collecting_gen] = data_heap->semispaces[collecting_gen];
368 data_heap->semispaces[collecting_gen] = z;
369 reset_generation(collecting_gen);
370 newspace = &data_heap->generations[collecting_gen];
371 clear_cards(collecting_gen,collecting_gen);
372 clear_decks(collecting_gen,collecting_gen);
373 clear_allot_markers(collecting_gen,collecting_gen);
377 /* when collecting a younger generation, we copy
378 reachable objects to the next oldest generation,
379 so we set the newspace so the next generation. */
380 newspace = &data_heap->generations[collecting_gen + 1];
384 void end_gc(CELL gc_elapsed)
386 F_GC_STATS *s = &gc_stats[collecting_gen];
389 s->gc_time += gc_elapsed;
390 if(s->max_gc_time < gc_elapsed)
391 s->max_gc_time = gc_elapsed;
393 if(growing_data_heap)
395 dealloc_data_heap(old_data_heap);
396 old_data_heap = NULL;
397 growing_data_heap = false;
400 if(collecting_accumulation_gen_p())
402 /* all younger generations except are now empty.
403 if collecting_gen == NURSERY here, we only have 1 generation;
404 old-school Cheney collector */
405 if(collecting_gen != NURSERY)
406 reset_generations(NURSERY,collecting_gen - 1);
408 else if(collecting_gen == NURSERY)
410 nursery.here = nursery.start;
414 /* all generations up to and including the one
415 collected are now empty */
416 reset_generations(NURSERY,collecting_gen);
419 collecting_aging_again = false;
422 /* Collect gen and all younger generations.
423 If growing_data_heap_ is true, we must grow the data heap to such a size that
424 an allocation of requested_bytes won't fail */
425 void garbage_collection(CELL gen,
426 bool growing_data_heap_,
427 CELL requested_bytes)
431 critical_error("GC disabled",gen);
435 u64 start = current_micros();
437 performing_gc = true;
438 growing_data_heap = growing_data_heap_;
439 collecting_gen = gen;
441 /* we come back here if a generation is full */
444 /* We have no older generations we can try collecting, so we
445 resort to growing the data heap */
446 if(collecting_gen == TENURED)
448 growing_data_heap = true;
450 /* see the comment in unmark_marked() */
451 unmark_marked(&code_heap);
453 /* we try collecting AGING space twice before going on to
456 && collecting_gen == AGING
457 && !collecting_aging_again)
459 collecting_aging_again = true;
461 /* Collect the next oldest generation */
468 begin_gc(requested_bytes);
470 /* initialize chase pointer */
471 CELL scan = newspace->here;
473 /* collect objects referenced from stacks and environment */
475 /* collect objects referenced from older generations */
477 /* do some tracing */
478 copy_reachable_objects(scan,&newspace->here);
480 /* don't scan code heap unless it has pointers to this
481 generation or younger */
482 if(collecting_gen >= last_code_heap_scan)
486 if(collecting_gen == TENURED)
487 free_unmarked(&code_heap,(HEAP_ITERATOR)update_literal_references);
489 copy_code_heap_roots();
491 if(collecting_accumulation_gen_p())
492 last_code_heap_scan = collecting_gen;
494 last_code_heap_scan = collecting_gen + 1;
497 CELL gc_elapsed = (current_micros() - start);
501 performing_gc = false;
506 garbage_collection(TENURED,false,0);
511 garbage_collection(NURSERY,false,0);
514 void primitive_gc(void)
519 void primitive_gc_stats(void)
521 GROWABLE_ARRAY(stats);
524 u64 total_gc_time = 0;
526 for(i = 0; i < MAX_GEN_COUNT; i++)
528 F_GC_STATS *s = &gc_stats[i];
529 GROWABLE_ARRAY_ADD(stats,allot_cell(s->collections));
530 GROWABLE_ARRAY_ADD(stats,tag_bignum(long_long_to_bignum(s->gc_time)));
531 GROWABLE_ARRAY_ADD(stats,tag_bignum(long_long_to_bignum(s->max_gc_time)));
532 GROWABLE_ARRAY_ADD(stats,allot_cell(s->collections == 0 ? 0 : s->gc_time / s->collections));
533 GROWABLE_ARRAY_ADD(stats,allot_cell(s->object_count));
534 GROWABLE_ARRAY_ADD(stats,tag_bignum(long_long_to_bignum(s->bytes_copied)));
536 total_gc_time += s->gc_time;
539 GROWABLE_ARRAY_ADD(stats,tag_bignum(ulong_long_to_bignum(total_gc_time)));
540 GROWABLE_ARRAY_ADD(stats,tag_bignum(ulong_long_to_bignum(cards_scanned)));
541 GROWABLE_ARRAY_ADD(stats,tag_bignum(ulong_long_to_bignum(decks_scanned)));
542 GROWABLE_ARRAY_ADD(stats,tag_bignum(ulong_long_to_bignum(card_scan_time)));
543 GROWABLE_ARRAY_ADD(stats,allot_cell(code_heap_scans));
545 GROWABLE_ARRAY_TRIM(stats);
546 GROWABLE_ARRAY_DONE(stats);
550 void clear_gc_stats(void)
553 for(i = 0; i < MAX_GEN_COUNT; i++)
554 memset(&gc_stats[i],0,sizeof(F_GC_STATS));
562 void primitive_clear_gc_stats(void)
567 /* classes.tuple uses this to reshape tuples; tools.deploy.shaker uses this
568 to coalesce equal but distinct quotations and wrappers. */
569 void primitive_become(void)
571 F_ARRAY *new_objects = untag_array(dpop());
572 F_ARRAY *old_objects = untag_array(dpop());
574 CELL capacity = array_capacity(new_objects);
575 if(capacity != array_capacity(old_objects))
576 critical_error("bad parameters to become",0);
580 for(i = 0; i < capacity; i++)
582 CELL old_obj = array_nth(old_objects,i);
583 CELL new_obj = array_nth(new_objects,i);
585 forward_object(old_obj,new_obj);
590 /* If a word's definition quotation was in old_objects and the
591 quotation in new_objects is not compiled, we might leak memory
592 by referencing the old quotation unless we recompile all
593 unoptimized words. */