]> gitweb.factorcode.org Git - factor.git/blob - vm/aging_collector.cpp
scryfall: better moxfield words
[factor.git] / vm / aging_collector.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 struct to_aging_copier : no_fixup {
6   aging_space* aging;
7   tenured_space* tenured;
8
9   to_aging_copier(aging_space* aging, tenured_space* tenured)
10       : aging(aging), tenured(tenured) { }
11
12   object* fixup_data(object* obj) {
13     if (aging->contains_p(obj) || tenured->contains_p(obj)) {
14       return obj;
15     }
16
17     // Is there another forwarding pointer?
18     while (obj->forwarding_pointer_p()) {
19       object* dest = obj->forwarding_pointer();
20       obj = dest;
21     }
22
23     if (aging->contains_p(obj) || tenured->contains_p(obj)) {
24       return obj;
25     }
26
27     cell size = obj->size();
28     object* newpointer = aging->allot(size);
29     if (!newpointer)
30       throw must_start_gc_again();
31
32     memcpy(newpointer, obj, size);
33     obj->forward_to(newpointer);
34
35     return newpointer;
36   }
37 };
38
39 void factor_vm::collect_aging() {
40   // Promote objects referenced from tenured space to tenured space, copy
41   // everything else to the aging semi-space, and reset the nursery pointer.
42   {
43     // Change the op so that if we fail here, an assertion will be raised.
44     current_gc->op = COLLECT_TO_TENURED_OP;
45
46     slot_visitor<from_tenured_refs_copier>
47         visitor(this, from_tenured_refs_copier(data->tenured, &mark_stack));
48
49     gc_event* event = current_gc->event;
50
51     if (event)
52       event->reset_timer();
53     visitor.visit_cards(data->tenured, card_points_to_aging, 0xff);
54     if (event) {
55       event->ended_phase(PHASE_CARD_SCAN);
56       event->cards_scanned += visitor.cards_scanned;
57       event->decks_scanned += visitor.decks_scanned;
58     }
59
60     if (event)
61       event->reset_timer();
62     visitor.visit_code_heap_roots(&code->points_to_aging);
63     if (event) {
64       event->ended_phase(PHASE_CODE_SCAN);
65       event->code_blocks_scanned += code->points_to_aging.size();
66     }
67     visitor.visit_mark_stack(&mark_stack);
68   }
69   {
70     // If collection fails here, do a to_tenured collection.
71     current_gc->op = COLLECT_AGING_OP;
72
73     std::swap(data->aging, data->aging_semispace);
74     data->reset_aging();
75
76     aging_space *aging = data->aging;
77     slot_visitor<to_aging_copier>
78         visitor(this, to_aging_copier(aging, data->tenured));
79
80     cell scan = aging->start + aging->occupied_space();
81
82     visitor.visit_all_roots();
83     visitor.cheneys_algorithm(aging, scan);
84
85     data->reset_nursery();
86     code->clear_remembered_set();
87   }
88 }
89
90 }