From 6789a40fc6506e85237930dae9456da41f5ce6b7 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 6 Oct 2009 19:15:54 -0500 Subject: [PATCH] vm: refactor forwarding pointer logic --- vm/data_gc.cpp | 112 +++++++++++++++++++++---------------------------- vm/data_gc.hpp | 4 +- vm/vm.hpp | 2 + 3 files changed, 51 insertions(+), 67 deletions(-) diff --git a/vm/data_gc.cpp b/vm/data_gc.cpp index 89f3375c11..8680ffa99a 100755 --- a/vm/data_gc.cpp +++ b/vm/data_gc.cpp @@ -17,16 +17,48 @@ gc_state::gc_state(data_heap *data_, bool growing_data_heap_, cell collecting_ge gc_state::~gc_state() { } +template object *factor_vm::resolve_forwarding(object *untagged, Strategy &strategy) +{ + check_data_pointer(untagged); + + /* is there another forwarding pointer? */ + while(untagged->h.forwarding_pointer_p()) + untagged = untagged->h.forwarding_pointer(); + + /* we've found the destination */ + untagged->h.check_header(); + return untagged; +} + template void factor_vm::trace_handle(cell *handle, Strategy &strategy) { cell pointer = *handle; if(!immediate_p(pointer)) { - object *obj = untag(pointer); - check_data_pointer(obj); - if(strategy.should_copy_p(obj)) - *handle = strategy.copy_object(pointer); + object *untagged = untag(pointer); + if(strategy.should_copy_p(untagged)) + { + object *forwarding = resolve_forwarding(untagged,strategy); + if(forwarding == untagged) + *handle = strategy.copy_object(pointer); + else if(strategy.should_copy_p(forwarding)) + *handle = strategy.copy_object(RETAG(forwarding,TAG(pointer))); + else + *handle = RETAG(forwarding,TAG(pointer)); + } + } +} + +template void factor_vm::trace_slots(object *ptr, Strategy &strategy) +{ + cell *slot = (cell *)ptr; + cell *end = (cell *)((cell)ptr + binary_payload_start(ptr)); + + if(slot != end) + { + slot++; + for(; slot < end; slot++) trace_handle(slot,strategy); } } @@ -178,17 +210,13 @@ template void factor_vm::trace_registered_bignums(Strategy &s for(; iter < end; iter++) { - bignum **handle = (bignum **)(*iter); - bignum *pointer = *handle; + cell *handle = (cell *)(*iter); - if(pointer) + if(*handle) { - check_data_pointer(pointer); - if(strategy.should_copy_p(pointer)) - *handle = untag(strategy.copy_object(tag(pointer))); -#ifdef FACTOR_DEBUG - assert((*handle)->h.hi_tag() == BIGNUM_TYPE); -#endif + *handle |= BIGNUM_TYPE; + trace_handle(handle,strategy); + *handle &= ~BIGNUM_TYPE; } } } @@ -380,7 +408,7 @@ template Strategy ©ing_collector::strategy() } /* Given a pointer to oldspace, copy it to newspace */ -template object *copying_collector::copy_untagged_object_impl(object *pointer, cell size) +template object *copying_collector::copy_untagged_object(object *pointer, cell size) { if(newspace->here + size >= newspace->end) longjmp(current_gc->gc_unwind,1); @@ -395,47 +423,12 @@ template object *copying_collector::copy_untagged_o return newpointer; } -template object *copying_collector::copy_object_impl(object *untagged) -{ - object *newpointer = copy_untagged_object_impl(untagged,myvm->untagged_object_size(untagged)); - untagged->h.forward_to(newpointer); - return newpointer; -} - -/* Follow a chain of forwarding pointers */ -template object *copying_collector::resolve_forwarding(object *untagged) -{ - myvm->check_data_pointer(untagged); - - /* is there another forwarding pointer? */ - if(untagged->h.forwarding_pointer_p()) - return resolve_forwarding(untagged->h.forwarding_pointer()); - /* we've found the destination */ - else - { - untagged->h.check_header(); - if(should_copy_p(untagged)) - return copy_object_impl(untagged); - else - return untagged; - } -} - template cell copying_collector::copy_object(cell pointer) { object *untagged = myvm->untag(pointer); - - myvm->check_data_pointer(untagged); - - if(untagged->h.forwarding_pointer_p()) - untagged = resolve_forwarding(untagged->h.forwarding_pointer()); - else - { - untagged->h.check_header(); - untagged = copy_object_impl(untagged); - } - - return RETAG(untagged,TAG(pointer)); + object *newpointer = copy_untagged_object(untagged,myvm->untagged_object_size(untagged)); + untagged->h.forward_to(newpointer); + return RETAG(newpointer,TAG(pointer)); } template bool copying_collector::should_copy_p(object *pointer) @@ -445,18 +438,9 @@ template bool copying_collector::should_copy_p(obje template cell copying_collector::trace_next(cell scan) { - cell *obj = (cell *)scan; - cell *end = (cell *)(scan + myvm->binary_payload_start((object *)scan)); - - if(obj != end) - { - obj++; - - for(; obj < end; obj++) - myvm->trace_handle(obj,strategy()); - } - - return scan + myvm->untagged_object_size((object *)scan); + object *obj = (object *)scan; + myvm->trace_slots(obj,strategy()); + return scan + myvm->untagged_object_size(obj); } template void copying_collector::go() diff --git a/vm/data_gc.hpp b/vm/data_gc.hpp index a5622ffd1e..15332ad225 100755 --- a/vm/data_gc.hpp +++ b/vm/data_gc.hpp @@ -63,10 +63,8 @@ template struct copying_collector { explicit copying_collector(factor_vm *myvm_, zone *newspace); Strategy &strategy(); - object *copy_untagged_object_impl(object *pointer, cell size); + object *copy_untagged_object(object *pointer, cell size); cell trace_next(cell scan); - object *copy_object_impl(object *untagged); - object *resolve_forwarding(object *untagged); cell copy_object(cell pointer); bool should_copy_p(object *pointer); void go(); diff --git a/vm/vm.hpp b/vm/vm.hpp index d39163a209..06421f871d 100755 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -237,7 +237,9 @@ struct factor_vm cell code_heap_scans; void init_data_gc(); + template object *resolve_forwarding(object *untagged, Strategy &strategy); template void trace_handle(cell *handle, Strategy &strategy); + template void trace_slots(object *ptr, Strategy &strategy); template void trace_card(card *ptr, cell gen, cell here, Strategy &strategy); template void trace_card_deck(card_deck *deck, cell gen, card mask, card unmask, Strategy &strategy); template void trace_generation_cards(cell gen, Strategy &strategy);