| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "garbage_collector.h" | ||
| 2 | #include "sema/bindings.h" | ||
| 3 | |||
| 4 | namespace lython { | ||
| 5 | |||
| 6 | GargabeCollector::GargabeCollector(struct Bindings& bindings, struct Expression& root): | ||
| 7 | ✗ | bindings(bindings), root(root) // | |
| 8 | ✗ | {} | |
| 9 | |||
| 10 | void GargabeCollector::mark() { | ||
| 11 | // For generational collection we could start | ||
| 12 | // the check later into the binding array | ||
| 13 | ✗ | int i = bindings.global_index; | |
| 14 | |||
| 15 | ✗ | for (; i < bindings.bindings.size(); i++) { | |
| 16 | // | ||
| 17 | ✗ | BindingEntry& entry = bindings.bindings[i]; | |
| 18 | |||
| 19 | // Mark the object as viewed | ||
| 20 | ✗ | GCObject* value = entry.value; | |
| 21 | |||
| 22 | // TODO: fetch the GC metadata | ||
| 23 | ✗ | RTGCObject* mapped = nullptr; | |
| 24 | ✗ | if (mapped != nullptr) { | |
| 25 | ✗ | mapped->cycle = cycle; | |
| 26 | } | ||
| 27 | } | ||
| 28 | ✗ | } | |
| 29 | |||
| 30 | void GargabeCollector::free_object(RTGCObject* obj) { | ||
| 31 | ✗ | root.remove_child((GCObject*)obj->object, true); | |
| 32 | ✗ | } | |
| 33 | |||
| 34 | void GargabeCollector::sweep() { shrink(); } | ||
| 35 | |||
| 36 | void GargabeCollector::shrink() { | ||
| 37 | ✗ | int deleted = 0; | |
| 38 | ✗ | int i = 0; | |
| 39 | ✗ | int free_spot = 0; | |
| 40 | ✗ | Array<int> free_spots; | |
| 41 | |||
| 42 | ✗ | for (RTGCObject& obj: tracked) { | |
| 43 | ✗ | if (obj.cycle < cycle) { | |
| 44 | ✗ | deleted += 1; | |
| 45 | ✗ | free_object(&obj); | |
| 46 | ✗ | free_spots.push_back(i); | |
| 47 | ✗ | } else if (!free_spots.empty()) { | |
| 48 | ✗ | tracked[free_spot] = obj; | |
| 49 | ✗ | tracked[i] = RTGCObject{0, nullptr}; | |
| 50 | ✗ | free_spots.push_back(i); | |
| 51 | ✗ | free_spot += 1; | |
| 52 | } | ||
| 53 | ✗ | i += 1; | |
| 54 | } | ||
| 55 | |||
| 56 | ✗ | tracked.resize(int(tracked.size()) - deleted); | |
| 57 | ✗ | cycle += 1; | |
| 58 | ✗ | } | |
| 59 | } // namespace lython | ||
| 60 |