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