source: src/Common/GC.cc@ 184557e

new-env
Last change on this file since 184557e was 7a37f258, checked in by Aaron Moss <a3moss@…>, 7 years ago

Fix bug with static root traces

  • Property mode set to 100644
File size: 3.5 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 // ensure static roots traced
99 trace_static_roots();
100
101 // collect young gen
102 for ( GC_Object*& obj : young ) {
103 if ( obj->mark != mark ) {
104 #ifdef GC_TRAP
105 if ( obj == GC_TRAP_OBJ ) std::raise(SIGTRAP);
106 #endif
107 delete obj;
108 obj = nullptr;
109 }
110 }
111
112 // move uncollected elements into old gen
113 auto end_live = std::remove( young.begin(), young.end(), nullptr );
114 old.insert( old.end(), young.begin(), end_live );
115
116 // clear young gen and reset mark to return to old generation mark
117 young.clear();
118 mark = !mark;
119}
120
121void GC::collect() {
122 // ensure not called when young gen is active
123 assert(g == 0 && "Cannot do old collection when young generation is active");
124 Generation& old = gens[0];
125
126 // ensure static roots traced
127 trace_static_roots();
128
129 // collect old gen
130 for ( GC_Object*& obj : old ) {
131 if ( obj->mark != mark ) {
132 #ifdef GC_TRAP
133 if ( obj == GC_TRAP_OBJ ) std::raise(SIGTRAP);
134 #endif
135 delete obj;
136 obj = nullptr;
137 }
138 }
139
140 // clear collected elements
141 old.erase( std::remove( old.begin(), old.end(), nullptr ), old.end() );
142
143 // reset mark
144 mark = !mark;
145}
146
147GC_Object::GC_Object() {
148 GC::get().register_object( this );
149}
150
151GC_Object::GC_Object( const GC_Object& ) {
152 GC::get().register_object( this );
153}
154
155GC_Object::GC_Object( GC_Object&& ) {
156 GC::get().register_object( this );
157}
158
159// Local Variables: //
160// tab-width: 4 //
161// mode: c++ //
162// compile-command: "make install" //
163// End: //
Note: See TracBrowser for help on using the repository browser.