source: src/Common/GC.cc @ 2efe4b8

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

Assorted GC bugfixes

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