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
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 "Common/PassVisitor.h"
19
20#include "SynTree/GcTracer.h"
21
22#include <algorithm>
23#include <cassert>
24
25GC& GC::get() {
26        static GC gc;
27        return gc;
28}
29
30GC::GC() : gens(1), static_roots(), mark(false), g(0) {
31        gens[0].reserve(70000);
32}
33
34GC::~GC() {
35        for ( unsigned i = 0; i <= g; ++i ) {
36                for ( GC_Object* o : gens[i] ) {
37                        delete o;
38                }
39        }
40}
41
42const GC& GC::operator<< (const GC_Object* obj) const {
43        if( obj )
44        {
45                if( obj->mark != this->mark ) {
46                        obj->mark = this->mark;
47                        obj->trace( *this );
48                }
49        }
50        return *this;
51}
52
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>
59//     continue
60#ifdef GC_TRAP
61#include <csignal>
62
63/// Set to object to check in debugger
64void* GC_TRAP_OBJ = nullptr;
65#endif
66
67void GC::register_object(GC_Object* obj) {
68        #ifdef GC_TRAP
69                if ( obj == GC_TRAP_OBJ ) std::raise(SIGTRAP);
70        #endif
71        gens[g].push_back(obj);
72        obj->mark = !this->mark;  // initialize as un-marked
73}
74
75void GC::register_static_root(BaseSyntaxNode* root) {
76        static_roots.push_back(root);
77}
78
79GC_Guard GC::new_generation() {
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
82        return { *this, g };
83}
84
85void GC::trace_static_roots() {
86        PassVisitor<GcTracer> tracer{ *this };
87        for ( BaseSyntaxNode* root : static_roots ) {
88                root->accept( tracer );
89        }
90}
91
92void GC::collect_young() {
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
98        // collect young gen
99        for ( GC_Object*& obj : young ) {
100                if ( obj->mark != mark ) {
101                        #ifdef GC_TRAP
102                                if ( obj == GC_TRAP_OBJ ) std::raise(SIGTRAP);
103                        #endif
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       
113        // clear young gen and reset mark to return to old generation mark
114        young.clear();
115        mark = !mark;
116}
117
118void GC::collect() {
119        // ensure not called when young gen is active
120        assert(g == 0 && "Cannot do old collection when young generation is active");
121        Generation& old = gens[0];
122
123        // collect old gen
124        for ( GC_Object*& obj : old ) {
125                if ( obj->mark != mark ) {
126                        #ifdef GC_TRAP
127                                if ( obj == GC_TRAP_OBJ ) std::raise(SIGTRAP);
128                        #endif
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
137        // reset mark
138        mark = !mark;
139}
140
141GC_Object::GC_Object() {
142        GC::get().register_object( this );
143}
144
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
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.