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.h --
|
---|
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 | #pragma once
|
---|
17 |
|
---|
18 | #include <cassert>
|
---|
19 | #include <vector>
|
---|
20 |
|
---|
21 | class GC_Object;
|
---|
22 | class BaseSyntaxNode;
|
---|
23 | class GC_Guard;
|
---|
24 |
|
---|
25 | /// Manually traced and called garbage collector
|
---|
26 | class GC {
|
---|
27 | friend class GcTracer;
|
---|
28 | friend class GC_Guard;
|
---|
29 |
|
---|
30 | /// Collects the youngest generation, placing survivors in previous generation.
|
---|
31 | /// Young generation objects cannot be kept alive by pointers from older generation.
|
---|
32 | /// Older generation is used for subsequent new objects.
|
---|
33 | void collect_young();
|
---|
34 | public:
|
---|
35 | /// Gets singleton GC instance
|
---|
36 | static GC& get();
|
---|
37 |
|
---|
38 | /// Traces a traceable object
|
---|
39 | const GC& operator<< (const GC_Object*) const;
|
---|
40 |
|
---|
41 | /// Adds a new object to garbage collection
|
---|
42 | void register_object(GC_Object*);
|
---|
43 |
|
---|
44 | /// Adds an object to the set of static roots
|
---|
45 | void register_static_root(BaseSyntaxNode*);
|
---|
46 |
|
---|
47 | /// Start new generation for subsequent new objects
|
---|
48 | GC_Guard new_generation();
|
---|
49 |
|
---|
50 | /// Traces all static roots
|
---|
51 | void trace_static_roots();
|
---|
52 |
|
---|
53 | /// Collects oldest generation; use oldest generation afterward.
|
---|
54 | /// Error if currently using younger generation
|
---|
55 | void collect();
|
---|
56 |
|
---|
57 | /// Collects all contained objects
|
---|
58 | ~GC();
|
---|
59 |
|
---|
60 | private:
|
---|
61 | GC();
|
---|
62 |
|
---|
63 | using Generation = std::vector<GC_Object*>;
|
---|
64 | std::vector<Generation> gens; ///< Set of generations; always at least one
|
---|
65 |
|
---|
66 | using StaticRoots = std::vector<BaseSyntaxNode*>;
|
---|
67 | StaticRoots static_roots; ///< Set of static-lifetime roots
|
---|
68 |
|
---|
69 | bool mark; ///< The current collection's mark bit
|
---|
70 | unsigned g; ///< The current number generation in use
|
---|
71 | };
|
---|
72 |
|
---|
73 | /// Cleanup object for young generation
|
---|
74 | class GC_Guard {
|
---|
75 | friend class GC;
|
---|
76 |
|
---|
77 | GC& gc; ///< GC associated with
|
---|
78 | unsigned g; ///< Generation constructed for
|
---|
79 |
|
---|
80 | GC_Guard( GC& gc, unsigned g ) : gc(gc), g(g) {}
|
---|
81 |
|
---|
82 | public:
|
---|
83 | ~GC_Guard() {
|
---|
84 | assert( gc.g == g && "collecting current generation" );
|
---|
85 | gc.collect_young();
|
---|
86 | }
|
---|
87 | };
|
---|
88 |
|
---|
89 | /// Use young generation until next collection
|
---|
90 | inline GC_Guard new_generation() { return GC::get().new_generation(); }
|
---|
91 |
|
---|
92 | // /// no-op default trace
|
---|
93 | // template<typename T>
|
---|
94 | // inline const GC& operator<< (const GC& gc, const T& x) { return gc; }
|
---|
95 |
|
---|
96 | inline void traceAll(const GC&) {}
|
---|
97 |
|
---|
98 | /// Marks all arguments as live in current generation
|
---|
99 | template<typename T, typename... Args>
|
---|
100 | inline void traceAll(const GC& gc, T& x, Args&... xs) {
|
---|
101 | gc << x;
|
---|
102 | traceAll(gc, xs...);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /// Traces roots without collecting
|
---|
106 | template<typename... Args>
|
---|
107 | inline void trace(Args&... roots) {
|
---|
108 | GC& gc = GC::get();
|
---|
109 | traceAll(gc, roots...);
|
---|
110 | }
|
---|
111 |
|
---|
112 | /// Traces roots and collects other elements; should not be any young generations live
|
---|
113 | template<typename... Args>
|
---|
114 | inline void collect(Args&... roots) {
|
---|
115 | GC& gc = GC::get();
|
---|
116 | traceAll(gc, roots...);
|
---|
117 | gc.collect();
|
---|
118 | }
|
---|
119 |
|
---|
120 | /// Makes a new expression as a static root
|
---|
121 | template<typename T, typename... Args>
|
---|
122 | inline T* new_static_root( Args&&... args ) {
|
---|
123 | T* root = new T( std::forward<Args>(args)... );
|
---|
124 | GC::get().register_static_root( root );
|
---|
125 | return root;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /// Class that is managed by the GC
|
---|
129 | class GC_Object {
|
---|
130 | friend class GC;
|
---|
131 | protected:
|
---|
132 | mutable bool mark;
|
---|
133 |
|
---|
134 | // Override default constructors to ensure clones are registered and properly marked
|
---|
135 | GC_Object();
|
---|
136 |
|
---|
137 | GC_Object(const GC_Object&);
|
---|
138 |
|
---|
139 | GC_Object(GC_Object&&);
|
---|
140 |
|
---|
141 | GC_Object& operator= (const GC_Object&) { /* do not assign mark */ return *this; }
|
---|
142 |
|
---|
143 | GC_Object& operator= (GC_Object&&) { /* do not assign mark */ return *this; }
|
---|
144 |
|
---|
145 | // Ensure subclasses can be deleted by garbage collector
|
---|
146 | virtual ~GC_Object() {}
|
---|
147 |
|
---|
148 | /// override to trace any child objects
|
---|
149 | virtual void trace(const GC&) const {}
|
---|
150 | };
|
---|
151 |
|
---|
152 | // Local Variables: //
|
---|
153 | // tab-width: 4 //
|
---|
154 | // mode: c++ //
|
---|
155 | // compile-command: "make install" //
|
---|
156 | // End: //
|
---|