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