[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 {
|
---|
[d318a18] | 43 | if( obj ) {
|
---|
[f229fc2] | 44 | if( obj->mark != this->mark ) {
|
---|
[68f9c43] | 45 | obj->mark = this->mark;
|
---|
| 46 | obj->trace( *this );
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 | return *this;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[d318a18] | 52 | bool GC::notrace_mark(const GC_Object* obj) const {
|
---|
| 53 | if ( obj && obj->mark != this->mark ) {
|
---|
| 54 | obj->mark = this->mark;
|
---|
| 55 | return true;
|
---|
| 56 | }
|
---|
| 57 | return false;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[fb97252f] | 60 | // build with flag GC_TRAP to compile in a breakpoint on register and sweep of a dynamically
|
---|
| 61 | // chosen object. Process to use:
|
---|
| 62 | // break GC::register_object
|
---|
| 63 | // run
|
---|
| 64 | // set variable GC_TRAP_OBJ = <target>
|
---|
| 65 | // disable <first breakpoint>
|
---|
[5af7306] | 66 | // continue
|
---|
[fb97252f] | 67 | #ifdef GC_TRAP
|
---|
| 68 | #include <csignal>
|
---|
| 69 |
|
---|
| 70 | /// Set to object to check in debugger
|
---|
| 71 | void* GC_TRAP_OBJ = nullptr;
|
---|
| 72 | #endif
|
---|
| 73 |
|
---|
[68f9c43] | 74 | void GC::register_object(GC_Object* obj) {
|
---|
[fb97252f] | 75 | #ifdef GC_TRAP
|
---|
| 76 | if ( obj == GC_TRAP_OBJ ) std::raise(SIGTRAP);
|
---|
| 77 | #endif
|
---|
[04570c7] | 78 | gens[g].push_back(obj);
|
---|
[f229fc2] | 79 | obj->mark = !this->mark; // initialize as un-marked
|
---|
[68f9c43] | 80 | }
|
---|
| 81 |
|
---|
[bd06384] | 82 | void GC::register_static_root(BaseSyntaxNode* root) {
|
---|
| 83 | static_roots.push_back(root);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[2efe4b8] | 86 | GC_Guard GC::new_generation() {
|
---|
[04570c7] | 87 | if ( ++g == gens.size() ) { gens.emplace_back(); } // ensure new generation available
|
---|
| 88 | mark = !mark; // switch mark so aged young objects will still be unmarked in old
|
---|
[2efe4b8] | 89 | return { *this, g };
|
---|
[68f9c43] | 90 | }
|
---|
| 91 |
|
---|
[bd06384] | 92 | void GC::trace_static_roots() {
|
---|
| 93 | PassVisitor<GcTracer> tracer{ *this };
|
---|
| 94 | for ( BaseSyntaxNode* root : static_roots ) {
|
---|
| 95 | root->accept( tracer );
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[68f9c43] | 99 | void GC::collect_young() {
|
---|
[04570c7] | 100 | // get generations and decrement generation
|
---|
| 101 | assert(g > 0 && "Cannot collect_young without young generation");
|
---|
| 102 | Generation& young = gens[g];
|
---|
| 103 | Generation& old = gens[--g];
|
---|
| 104 |
|
---|
[7a37f258] | 105 | // ensure static roots traced
|
---|
| 106 | trace_static_roots();
|
---|
| 107 |
|
---|
[68f9c43] | 108 | // collect young gen
|
---|
| 109 | for ( GC_Object*& obj : young ) {
|
---|
| 110 | if ( obj->mark != mark ) {
|
---|
[fb97252f] | 111 | #ifdef GC_TRAP
|
---|
| 112 | if ( obj == GC_TRAP_OBJ ) std::raise(SIGTRAP);
|
---|
| 113 | #endif
|
---|
[68f9c43] | 114 | delete obj;
|
---|
| 115 | obj = nullptr;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | // move uncollected elements into old gen
|
---|
| 120 | auto end_live = std::remove( young.begin(), young.end(), nullptr );
|
---|
| 121 | old.insert( old.end(), young.begin(), end_live );
|
---|
| 122 |
|
---|
[04570c7] | 123 | // clear young gen and reset mark to return to old generation mark
|
---|
[68f9c43] | 124 | young.clear();
|
---|
| 125 | mark = !mark;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | void GC::collect() {
|
---|
[f229fc2] | 129 | // ensure not called when young gen is active
|
---|
[04570c7] | 130 | assert(g == 0 && "Cannot do old collection when young generation is active");
|
---|
| 131 | Generation& old = gens[0];
|
---|
[f229fc2] | 132 |
|
---|
[7a37f258] | 133 | // ensure static roots traced
|
---|
| 134 | trace_static_roots();
|
---|
| 135 |
|
---|
[68f9c43] | 136 | // collect old gen
|
---|
| 137 | for ( GC_Object*& obj : old ) {
|
---|
| 138 | if ( obj->mark != mark ) {
|
---|
[fb97252f] | 139 | #ifdef GC_TRAP
|
---|
| 140 | if ( obj == GC_TRAP_OBJ ) std::raise(SIGTRAP);
|
---|
| 141 | #endif
|
---|
[68f9c43] | 142 | delete obj;
|
---|
| 143 | obj = nullptr;
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | // clear collected elements
|
---|
| 148 | old.erase( std::remove( old.begin(), old.end(), nullptr ), old.end() );
|
---|
| 149 |
|
---|
[f229fc2] | 150 | // reset mark
|
---|
| 151 | mark = !mark;
|
---|
[68f9c43] | 152 | }
|
---|
| 153 |
|
---|
| 154 | GC_Object::GC_Object() {
|
---|
| 155 | GC::get().register_object( this );
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[24de7b1] | 158 | GC_Object::GC_Object( const GC_Object& ) {
|
---|
| 159 | GC::get().register_object( this );
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | GC_Object::GC_Object( GC_Object&& ) {
|
---|
| 163 | GC::get().register_object( this );
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[68f9c43] | 166 | // Local Variables: //
|
---|
| 167 | // tab-width: 4 //
|
---|
| 168 | // mode: c++ //
|
---|
| 169 | // compile-command: "make install" //
|
---|
| 170 | // End: //
|
---|