[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 | |
---|
[34dcc474] | 25 | // #include <csignal> |
---|
| 26 | |
---|
[68f9c43] | 27 | GC& GC::get() { |
---|
| 28 | static GC gc; |
---|
| 29 | return gc; |
---|
| 30 | } |
---|
| 31 | |
---|
[bd06384] | 32 | GC::GC() : mark(false), using_young(false), old(), young(), static_roots() { |
---|
[68f9c43] | 33 | old.reserve(70000); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | GC::~GC() { |
---|
| 37 | for ( GC_Object* o : young ) { |
---|
| 38 | delete o; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | for ( GC_Object* o : old ) { |
---|
| 42 | delete o; |
---|
| 43 | } |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | const GC& GC::operator<< (const GC_Traceable* obj) const { |
---|
| 47 | if( obj ) |
---|
| 48 | { |
---|
| 49 | bool isMarked = obj->mark == this->mark; |
---|
| 50 | if( !isMarked ) { |
---|
| 51 | obj->mark = this->mark; |
---|
| 52 | obj->trace( *this ); |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | return *this; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | void GC::register_object(GC_Object* obj) { |
---|
[34dcc474] | 59 | // if ( obj == (GC_Object*)0x60f00000e410ul ) std::raise( SIGTRAP ); |
---|
[68f9c43] | 60 | (using_young ? young : old).push_back(obj); |
---|
[34dcc474] | 61 | obj->mark = ! this->mark; // initialize as un-marked |
---|
[68f9c43] | 62 | } |
---|
| 63 | |
---|
[bd06384] | 64 | void GC::register_static_root(BaseSyntaxNode* root) { |
---|
| 65 | static_roots.push_back(root); |
---|
| 66 | } |
---|
| 67 | |
---|
[68f9c43] | 68 | void GC::new_generation() { |
---|
| 69 | using_young = true; |
---|
| 70 | } |
---|
| 71 | |
---|
[bd06384] | 72 | void GC::trace_static_roots() { |
---|
| 73 | PassVisitor<GcTracer> tracer{ *this }; |
---|
| 74 | for ( BaseSyntaxNode* root : static_roots ) { |
---|
| 75 | root->accept( tracer ); |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
[68f9c43] | 79 | void GC::collect_young() { |
---|
| 80 | // check young generation, just reset mark if not using |
---|
| 81 | if ( ! using_young ) { |
---|
| 82 | mark = !mark; |
---|
| 83 | return; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | // collect young gen |
---|
| 87 | for ( GC_Object*& obj : young ) { |
---|
| 88 | if ( obj->mark != mark ) { |
---|
| 89 | delete obj; |
---|
| 90 | obj = nullptr; |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | // move uncollected elements into old gen |
---|
| 95 | auto end_live = std::remove( young.begin(), young.end(), nullptr ); |
---|
| 96 | old.insert( old.end(), young.begin(), end_live ); |
---|
| 97 | |
---|
| 98 | // clear young gen |
---|
| 99 | using_young = false; |
---|
| 100 | young.clear(); |
---|
| 101 | |
---|
| 102 | // reset mark for next collection |
---|
| 103 | mark = !mark; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | void GC::collect() { |
---|
| 107 | // collect old gen |
---|
| 108 | for ( GC_Object*& obj : old ) { |
---|
| 109 | if ( obj->mark != mark ) { |
---|
[34dcc474] | 110 | // if ( obj == (GC_Object*)0x60f00000e410ul ) std::raise( SIGTRAP ); |
---|
[68f9c43] | 111 | delete obj; |
---|
| 112 | obj = nullptr; |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | // clear collected elements |
---|
| 117 | old.erase( std::remove( old.begin(), old.end(), nullptr ), old.end() ); |
---|
| 118 | |
---|
| 119 | // collect young gen (also resets mark) |
---|
| 120 | collect_young(); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | GC_Object::GC_Object() { |
---|
| 124 | GC::get().register_object( this ); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | // Local Variables: // |
---|
| 128 | // tab-width: 4 // |
---|
| 129 | // mode: c++ // |
---|
| 130 | // compile-command: "make install" // |
---|
| 131 | // End: // |
---|