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

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

Assorted GC bugfixes

  • Property mode set to 100644
File size: 4.0 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.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
21class GC_Object;
22class BaseSyntaxNode;
23class GC_Guard;
24
25/// Manually traced and called garbage collector
26class 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();
34public:
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
60private:
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
74class 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
82public:
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
90inline 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
96inline void traceAll(const GC&) {}
97
98/// Marks all arguments as live in current generation
99template<typename T, typename... Args>
100inline void traceAll(const GC& gc, T& x, Args&... xs) {
101        gc << x;
102        traceAll(gc, xs...);
103}
104
105/// Traces roots without collecting
106template<typename... Args>
107inline void trace(Args&... roots) {
108        GC& gc = GC::get();
109        traceAll(gc, roots...);
110        gc.trace_static_roots();
111}
112
113/// Traces roots and collects other elements; should not be any young generations live
114template<typename... Args>
115inline void collect(Args&... roots) {
116        GC& gc = GC::get();
117        traceAll(gc, roots...);
118        gc.trace_static_roots();
119        gc.collect();
120}
121
122/// Makes a new expression as a static root
123template<typename T, typename... Args>
124inline T* new_static_root( Args&&... args ) {
125        T* root = new T( std::forward<Args>(args)... );
126        GC::get().register_static_root( root );
127        return root;
128}
129
130/// Class that is managed by the GC
131class GC_Object {
132        friend class GC;
133protected:
134        mutable bool mark;
135
136        // Override default constructors to ensure clones are registered and properly marked
137        GC_Object();
138
139        GC_Object(const GC_Object&);
140
141        GC_Object(GC_Object&&);
142
143        GC_Object& operator= (const GC_Object&) { /* do not assign mark */ return *this; }
144
145        GC_Object& operator= (GC_Object&&) { /* do not assign mark */ return *this; }
146
147        // Ensure subclasses can be deleted by garbage collector
148        virtual ~GC_Object() {}
149
150        /// override to trace any child objects
151        virtual void trace(const GC&) const {}
152};
153
154// Local Variables: //
155// tab-width: 4 //
156// mode: c++ //
157// compile-command: "make install" //
158// End: //
Note: See TracBrowser for help on using the repository browser.