source: src/Common/GC.cc @ 8d7bef2

new-envwith_gc
Last change on this file since 8d7bef2 was 68f9c43, checked in by Aaron Moss <a3moss@…>, 6 years ago

First pass at delete removal

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