]> gitweb.factorcode.org Git - factor.git/blob - vm/collector.hpp
ce8b6445b8d5ab0ae92524ffd9d2067c67cd3688
[factor.git] / vm / collector.hpp
1 namespace factor {
2
3 struct must_start_gc_again {
4 };
5
6 template <typename TargetGeneration, typename Policy>
7 struct gc_workhorse : no_fixup {
8   static const bool translated_code_block_map = false;
9
10   factor_vm* parent;
11   TargetGeneration* target;
12   Policy policy;
13   code_heap* code;
14
15   gc_workhorse(factor_vm* parent, TargetGeneration* target, Policy policy)
16       : parent(parent), target(target), policy(policy), code(parent->code) {}
17
18   object* fixup_data(object* obj) {
19     FACTOR_ASSERT((parent->current_gc &&
20                    parent->current_gc->op == collect_growing_heap_op) ||
21                   parent->data->seg->in_segment_p((cell)obj));
22
23     if (!policy.should_copy_p(obj)) {
24       policy.visited_object(obj);
25       return obj;
26     }
27
28     /* is there another forwarding pointer? */
29     while (obj->forwarding_pointer_p()) {
30       object* dest = obj->forwarding_pointer();
31       obj = dest;
32     }
33
34     if (!policy.should_copy_p(obj)) {
35       policy.visited_object(obj);
36       return obj;
37     }
38
39     cell size = obj->size();
40     object* newpointer = target->allot(size);
41     if (!newpointer)
42       throw must_start_gc_again();
43
44     memcpy(newpointer, obj, size);
45     obj->forward_to(newpointer);
46
47     policy.promoted_object(newpointer);
48
49     return newpointer;
50   }
51
52   code_block* fixup_code(code_block* compiled) {
53     if (!code->allocator->state.marked_p((cell)compiled)) {
54       code->allocator->state.set_marked_p((cell)compiled, compiled->size());
55       parent->mark_stack.push_back((cell)compiled + 1);
56     }
57
58     return compiled;
59   }
60 };
61
62 template <typename TargetGeneration, typename Policy> struct collector {
63   factor_vm* parent;
64   data_heap* data;
65   code_heap* code;
66   TargetGeneration* target;
67   gc_workhorse<TargetGeneration, Policy> workhorse;
68   slot_visitor<gc_workhorse<TargetGeneration, Policy> > visitor;
69   cell cards_scanned;
70   cell decks_scanned;
71   cell code_blocks_scanned;
72
73   collector(factor_vm* parent, TargetGeneration* target, Policy policy)
74       : parent(parent),
75         data(parent->data),
76         code(parent->code),
77         target(target),
78         workhorse(parent, target, policy),
79         visitor(parent, workhorse),
80         cards_scanned(0),
81         decks_scanned(0),
82         code_blocks_scanned(0) {}
83
84   void trace_code_heap_roots(std::set<code_block*>* remembered_set) {
85     std::set<code_block*>::const_iterator iter = remembered_set->begin();
86     std::set<code_block*>::const_iterator end = remembered_set->end();
87
88     for (; iter != end; iter++) {
89       code_block* compiled = *iter;
90       visitor.visit_code_block_objects(compiled);
91       visitor.visit_embedded_literals(compiled);
92       compiled->flush_icache();
93       code_blocks_scanned++;
94     }
95   }
96
97   void trace_partial_objects(cell start, cell card_start, cell card_end) {
98     object* obj = (object*)start;
99     cell end = start + obj->binary_payload_start();
100     if (card_start < end) {
101       start += sizeof(cell);
102
103       start = std::max(start, card_start);
104       end = std::min(end, card_end);
105
106       cell* slot_ptr = (cell*)start;
107       cell* end_ptr = (cell*)end;
108
109       for (; slot_ptr < end_ptr; slot_ptr++)
110         visitor.visit_handle(slot_ptr);
111     }
112   }
113
114   template <typename SourceGeneration>
115   cell trace_card(SourceGeneration* gen, cell index, cell start) {
116
117     cell start_addr = data->start + index * card_size;
118     cell end_addr = start_addr + card_size;
119
120     if (!start || (start + ((object*)start)->size()) < start_addr) {
121       /* Optimization because finding the objects in a memory range is
122          expensive. It helps a lot when tracing consecutive cards. */
123       cell gen_start_card = (gen->start - data->start) / card_size;
124       start = gen->starts
125           .find_object_containing_card(index - gen_start_card);
126     }
127
128     while (start && start < end_addr) {
129       trace_partial_objects(start, start_addr, end_addr);
130       if ((start + ((object*)start)->size()) >= end_addr) {
131         /* The object can overlap the card boundary, then the
132            remainder of it will be handled in the next card
133            tracing if that card is marked. */
134         break;
135       }
136       start = gen->next_object_after(start);
137     }
138     return start;
139   }
140
141   template <typename SourceGeneration>
142   void trace_cards(SourceGeneration* gen, card mask, card unmask) {
143     card_deck* decks = data->decks;
144     card_deck* cards = data->cards;
145
146     cell first_deck = (gen->start - data->start) / deck_size;
147     cell last_deck = (gen->end - data->start) / deck_size;
148
149     /* Address of last traced object. */
150     cell start = 0;
151
152     for (cell deck_index = first_deck; deck_index < last_deck; deck_index++) {
153       if (decks[deck_index] & mask) {
154         decks[deck_index] &= ~unmask;
155         decks_scanned++;
156
157         cell first_card = cards_per_deck * deck_index;
158         cell last_card = first_card + cards_per_deck;
159
160         for (cell card_index = first_card; card_index < last_card;
161              card_index++) {
162           if (cards[card_index] & mask) {
163             cards[card_index] &= ~unmask;
164             cards_scanned++;
165
166             start = trace_card(gen, card_index, start);
167             if (!start) {
168               /* At end of generation, no need to scan more cards. */
169               return;
170             }
171           }
172         }
173       }
174     }
175   }
176 };
177
178 }