VM: Refactor collector.hpp to Factor style

db4
Erik Charlebois 2013-05-11 21:52:27 -04:00
parent ef7c009d2a
commit 66976a12bf
1 changed files with 182 additions and 213 deletions

View File

@ -1,9 +1,10 @@
namespace factor
{
namespace factor {
struct must_start_gc_again {};
struct must_start_gc_again {
};
template<typename TargetGeneration, typename Policy> struct gc_workhorse : no_fixup {
template <typename TargetGeneration, typename Policy>
struct gc_workhorse : no_fixup {
static const bool translated_code_block_map = false;
factor_vm* parent;
@ -11,14 +12,11 @@ template<typename TargetGeneration, typename Policy> struct gc_workhorse : no_fi
Policy policy;
code_heap* code;
explicit gc_workhorse(factor_vm *parent_, TargetGeneration *target_, Policy policy_) :
parent(parent_),
target(target_),
policy(policy_),
code(parent->code) {}
explicit gc_workhorse(factor_vm* parent_, TargetGeneration* target_,
Policy policy_)
: parent(parent_), target(target_), policy(policy_), code(parent->code) {}
object *resolve_forwarding(object *untagged)
{
object* resolve_forwarding(object* untagged) {
parent->check_data_pointer(untagged);
/* is there another forwarding pointer? */
@ -29,11 +27,11 @@ template<typename TargetGeneration, typename Policy> struct gc_workhorse : no_fi
return untagged;
}
object *promote_object(object *untagged)
{
object* promote_object(object* untagged) {
cell size = untagged->size();
object* newpointer = target->allot(size);
if(!newpointer) throw must_start_gc_again();
if (!newpointer)
throw must_start_gc_again();
memcpy(newpointer, untagged, size);
untagged->forward_to(newpointer);
@ -43,12 +41,10 @@ template<typename TargetGeneration, typename Policy> struct gc_workhorse : no_fi
return newpointer;
}
object *fixup_data(object *obj)
{
object* fixup_data(object* obj) {
parent->check_data_pointer(obj);
if(!policy.should_copy_p(obj))
{
if (!policy.should_copy_p(obj)) {
policy.visited_object(obj);
return obj;
}
@ -59,17 +55,14 @@ template<typename TargetGeneration, typename Policy> struct gc_workhorse : no_fi
return promote_object(obj);
else if (policy.should_copy_p(forwarding))
return promote_object(forwarding);
else
{
else {
policy.visited_object(forwarding);
return forwarding;
}
}
code_block *fixup_code(code_block *compiled)
{
if(!code->marked_p(compiled))
{
code_block* fixup_code(code_block* compiled) {
if (!code->marked_p(compiled)) {
code->set_marked_p(compiled);
parent->mark_stack.push_back((cell) compiled + 1);
}
@ -93,8 +86,7 @@ struct full_unmarker {
void operator()(card* ptr) { *ptr = 0; }
};
template<typename TargetGeneration, typename Policy>
struct collector {
template <typename TargetGeneration, typename Policy> struct collector {
factor_vm* parent;
data_heap* data;
code_heap* code;
@ -105,8 +97,9 @@ struct collector {
cell decks_scanned;
cell code_blocks_scanned;
explicit collector(factor_vm *parent_, TargetGeneration *target_, Policy policy_) :
parent(parent_),
explicit collector(factor_vm* parent_, TargetGeneration* target_,
Policy policy_)
: parent(parent_),
data(parent_->data),
code(parent_->code),
target(target_),
@ -116,45 +109,31 @@ struct collector {
decks_scanned(0),
code_blocks_scanned(0) {}
void trace_handle(cell *handle)
{
data_visitor.visit_handle(handle);
}
void trace_handle(cell* handle) { data_visitor.visit_handle(handle); }
void trace_object(object *ptr)
{
void trace_object(object* ptr) {
data_visitor.visit_slots(ptr);
if (ptr->type() == ALIEN_TYPE)
((alien*)ptr)->update_address();
}
void trace_roots()
{
data_visitor.visit_roots();
}
void trace_roots() { data_visitor.visit_roots(); }
void trace_contexts()
{
data_visitor.visit_contexts();
}
void trace_contexts() { data_visitor.visit_contexts(); }
void trace_code_block_objects(code_block *compiled)
{
void trace_code_block_objects(code_block* compiled) {
data_visitor.visit_code_block_objects(compiled);
}
void trace_embedded_literals(code_block *compiled)
{
void trace_embedded_literals(code_block* compiled) {
data_visitor.visit_embedded_literals(compiled);
}
void trace_code_heap_roots(std::set<code_block *> *remembered_set)
{
void trace_code_heap_roots(std::set<code_block*>* remembered_set) {
std::set<code_block*>::const_iterator iter = remembered_set->begin();
std::set<code_block*>::const_iterator end = remembered_set->end();
for(; iter != end; iter++)
{
for (; iter != end; iter++) {
code_block* compiled = *iter;
trace_code_block_objects(compiled);
trace_embedded_literals(compiled);
@ -163,39 +142,35 @@ struct collector {
}
}
inline cell first_card_in_deck(cell deck)
{
inline cell first_card_in_deck(cell deck) {
return deck << (deck_bits - card_bits);
}
inline cell last_card_in_deck(cell deck)
{
inline cell last_card_in_deck(cell deck) {
return first_card_in_deck(deck + 1);
}
inline cell card_deck_for_address(cell a)
{
inline cell card_deck_for_address(cell a) {
return addr_to_deck(a - data->start);
}
inline cell card_start_address(cell card)
{
inline cell card_start_address(cell card) {
return (card << card_bits) + data->start;
}
inline cell card_end_address(cell card)
{
inline cell card_end_address(cell card) {
return ((card + 1) << card_bits) + data->start;
}
void trace_partial_objects(cell start, cell end, cell card_start, cell card_end)
{
if(card_start < end)
{
void trace_partial_objects(cell start, cell end, cell card_start,
cell card_end) {
if (card_start < end) {
start += sizeof(cell);
if(start < card_start) start = card_start;
if(end > card_end) end = card_end;
if (start < card_start)
start = card_start;
if (end > card_end)
end = card_end;
cell* slot_ptr = (cell*)start;
cell* end_ptr = (cell*)end;
@ -206,8 +181,7 @@ struct collector {
}
template <typename SourceGeneration, typename Unmarker>
void trace_cards(SourceGeneration *gen, card mask, Unmarker unmarker)
{
void trace_cards(SourceGeneration* gen, card mask, Unmarker unmarker) {
card_deck* decks = data->decks;
card_deck* cards = data->cards;
@ -218,41 +192,35 @@ struct collector {
cell start = 0, binary_start = 0, end = 0;
for(cell deck_index = first_deck; deck_index < last_deck; deck_index++)
{
if(decks[deck_index] & mask)
{
for (cell deck_index = first_deck; deck_index < last_deck; deck_index++) {
if (decks[deck_index] & mask) {
decks_scanned++;
cell first_card = first_card_in_deck(deck_index);
cell last_card = last_card_in_deck(deck_index);
for(cell card_index = first_card; card_index < last_card; card_index++)
{
if(cards[card_index] & mask)
{
for (cell card_index = first_card; card_index < last_card;
card_index++) {
if (cards[card_index] & mask) {
cards_scanned++;
if(end < card_start_address(card_index))
{
start = gen->starts.find_object_containing_card(card_index - gen_start_card);
if (end < card_start_address(card_index)) {
start = gen->starts
.find_object_containing_card(card_index - gen_start_card);
binary_start = start + ((object*)start)->binary_payload_start();
end = start + ((object*)start)->size();
}
scan_next_object: if(start < card_end_address(card_index))
{
trace_partial_objects(
start,
binary_start,
scan_next_object:
if (start < card_end_address(card_index)) {
trace_partial_objects(start, binary_start,
card_start_address(card_index),
card_end_address(card_index));
if(end < card_end_address(card_index))
{
if (end < card_end_address(card_index)) {
start = gen->next_object_after(start);
if(start)
{
binary_start = start + ((object *)start)->binary_payload_start();
if (start) {
binary_start =
start + ((object*)start)->binary_payload_start();
end = start + ((object*)start)->size();
goto scan_next_object;
}
@ -261,7 +229,8 @@ scan_next_object: if(start < card_end_address(card_index))
unmarker(&cards[card_index]);
if(!start) return;
if (!start)
return;
}
}