[68f9c43] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // GC.cc -- |
---|
| 8 | // |
---|
| 9 | // Author : Aaron B. Moss |
---|
| 10 | // Created On : Thu Mar 15 14:47:00 2018 |
---|
| 11 | // Last Modified By : Aaron B. Moss |
---|
| 12 | // Last Modified On : Thu Mar 15 14:47:00 2018 |
---|
| 13 | // Update Count : 1 |
---|
| 14 | // |
---|
| 15 | |
---|
| 16 | #include "GC.h" |
---|
| 17 | |
---|
[bd06384] | 18 | #include "Common/PassVisitor.h" |
---|
| 19 | |
---|
| 20 | #include "SynTree/GcTracer.h" |
---|
| 21 | |
---|
[68f9c43] | 22 | #include <algorithm> |
---|
[7e4b44db] | 23 | #include <cassert> |
---|
[68f9c43] | 24 | |
---|
| 25 | GC& GC::get() { |
---|
| 26 | static GC gc; |
---|
| 27 | return gc; |
---|
| 28 | } |
---|
| 29 | |
---|
[04570c7] | 30 | GC::GC() : gens(1), static_roots(), mark(false), g(0) { |
---|
| 31 | gens[0].reserve(70000); |
---|
[68f9c43] | 32 | } |
---|
| 33 | |
---|
| 34 | GC::~GC() { |
---|
[04570c7] | 35 | for ( unsigned i = 0; i <= g; ++i ) { |
---|
| 36 | for ( GC_Object* o : gens[i] ) { |
---|
| 37 | delete o; |
---|
| 38 | } |
---|
[68f9c43] | 39 | } |
---|
| 40 | } |
---|
| 41 | |
---|
[24de7b1] | 42 | const GC& GC::operator<< (const GC_Object* obj) const { |
---|
[68f9c43] | 43 | if( obj ) |
---|
| 44 | { |
---|
[f229fc2] | 45 | if( obj->mark != this->mark ) { |
---|
[68f9c43] | 46 | obj->mark = this->mark; |
---|
| 47 | obj->trace( *this ); |
---|
| 48 | } |
---|
| 49 | } |
---|
| 50 | return *this; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | void GC::register_object(GC_Object* obj) { |
---|
[04570c7] | 54 | gens[g].push_back(obj); |
---|
[f229fc2] | 55 | obj->mark = !this->mark; // initialize as un-marked |
---|
[68f9c43] | 56 | } |
---|
| 57 | |
---|
[bd06384] | 58 | void GC::register_static_root(BaseSyntaxNode* root) { |
---|
| 59 | static_roots.push_back(root); |
---|
| 60 | } |
---|
| 61 | |
---|
[68f9c43] | 62 | void GC::new_generation() { |
---|
[04570c7] | 63 | if ( ++g == gens.size() ) { gens.emplace_back(); } // ensure new generation available |
---|
| 64 | mark = !mark; // switch mark so aged young objects will still be unmarked in old |
---|
[68f9c43] | 65 | } |
---|
| 66 | |
---|
[bd06384] | 67 | void GC::trace_static_roots() { |
---|
| 68 | PassVisitor<GcTracer> tracer{ *this }; |
---|
| 69 | for ( BaseSyntaxNode* root : static_roots ) { |
---|
| 70 | root->accept( tracer ); |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | |
---|
[68f9c43] | 74 | void GC::collect_young() { |
---|
[04570c7] | 75 | // get generations and decrement generation |
---|
| 76 | assert(g > 0 && "Cannot collect_young without young generation"); |
---|
| 77 | Generation& young = gens[g]; |
---|
| 78 | Generation& old = gens[--g]; |
---|
| 79 | |
---|
[68f9c43] | 80 | // collect young gen |
---|
| 81 | for ( GC_Object*& obj : young ) { |
---|
| 82 | if ( obj->mark != mark ) { |
---|
| 83 | delete obj; |
---|
| 84 | obj = nullptr; |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | // move uncollected elements into old gen |
---|
| 89 | auto end_live = std::remove( young.begin(), young.end(), nullptr ); |
---|
| 90 | old.insert( old.end(), young.begin(), end_live ); |
---|
| 91 | |
---|
[04570c7] | 92 | // clear young gen and reset mark to return to old generation mark |
---|
[68f9c43] | 93 | young.clear(); |
---|
| 94 | mark = !mark; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void GC::collect() { |
---|
[f229fc2] | 98 | // ensure not called when young gen is active |
---|
[04570c7] | 99 | assert(g == 0 && "Cannot do old collection when young generation is active"); |
---|
| 100 | Generation& old = gens[0]; |
---|
[f229fc2] | 101 | |
---|
[68f9c43] | 102 | // collect old gen |
---|
| 103 | for ( GC_Object*& obj : old ) { |
---|
| 104 | if ( obj->mark != mark ) { |
---|
| 105 | delete obj; |
---|
| 106 | obj = nullptr; |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | // clear collected elements |
---|
| 111 | old.erase( std::remove( old.begin(), old.end(), nullptr ), old.end() ); |
---|
| 112 | |
---|
[f229fc2] | 113 | // reset mark |
---|
| 114 | mark = !mark; |
---|
[68f9c43] | 115 | } |
---|
| 116 | |
---|
| 117 | GC_Object::GC_Object() { |
---|
| 118 | GC::get().register_object( this ); |
---|
| 119 | } |
---|
| 120 | |
---|
[24de7b1] | 121 | GC_Object::GC_Object( const GC_Object& ) { |
---|
| 122 | GC::get().register_object( this ); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | GC_Object::GC_Object( GC_Object&& ) { |
---|
| 126 | GC::get().register_object( this ); |
---|
| 127 | } |
---|
| 128 | |
---|
[68f9c43] | 129 | // Local Variables: // |
---|
| 130 | // tab-width: 4 // |
---|
| 131 | // mode: c++ // |
---|
| 132 | // compile-command: "make install" // |
---|
| 133 | // End: // |
---|