source: src/Common/GC.h @ bfc7811

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

Fix some GC bugs

  • Property mode set to 100644
File size: 3.2 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 <vector>
19
20class GC_Traceable;
21class GC_Object;
22class BaseSyntaxNode;
23
24/// Manually traced and called garbage collector
25class GC {
26        friend class GcTracer;
27public:
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        /// Use young generation for subsequent new objects
41        void new_generation();
42
43        /// Traces all static roots
44        void trace_static_roots();
45
46        /// Collects the young generation, placing survivors in old generation.
47        /// Old generation is used for subsequent new objects.
48        void collect_young();
49
50        /// Collects all memory; use old generation afterward.
51        void collect();
52
53        /// Collects all contained objects
54        ~GC();
55
56private:
57        GC();
58
59        bool mark;                 ///< The current collection's mark bit
60        bool using_young;          ///< Is the young generation in use?
61
62        using Generation = std::vector<GC_Object*>;
63        Generation old;            ///< Old generation
64        Generation young;          ///< Young generation
65
66        using StaticRoots = std::vector<BaseSyntaxNode*>;
67        StaticRoots static_roots;  ///< Set of static-lifetime roots
68};
69
70/// Use young generation until next collection
71inline void new_generation() { GC::get().new_generation(); }
72
73// /// no-op default trace
74// template<typename T>
75// inline const GC& operator<< (const GC& gc, const T& x) { return gc; }
76
77inline void traceAll(const GC&) {}
78
79/// Marks all arguments as live in current generation
80template<typename T, typename... Args>
81inline void traceAll(const GC& gc, T& x, Args&... xs) {
82        gc << x;
83        traceAll(gc, xs...);
84}
85
86/// Traces young-generation roots and does a young collection
87template<typename... Args>
88inline void collect_young(Args&... roots) {
89        GC& gc = GC::get();
90        traceAll(gc, roots...);
91        gc.trace_static_roots();
92        gc.collect_young();
93}
94
95/// Traces roots and collects other elements
96template<typename... Args>
97inline void collect(Args&... roots) {
98        GC& gc = GC::get();
99        traceAll(gc, roots...);
100        gc.trace_static_roots();
101        gc.collect();
102}
103
104/// Makes a new expression as a static root
105template<typename T, typename... Args>
106inline T* new_static_root( Args&&... args ) {
107        T* root = new T( std::forward<Args>(args)... );
108        GC::get().register_static_root( root );
109        return root;
110}
111
112/// Class that is traced by the GC, but not managed by it
113class GC_Traceable {
114        friend class GC;
115protected:
116        mutable bool mark;
117
118        /// override to trace any child objects
119        virtual void trace(const GC&) const {}
120};
121
122/// Class that is managed by the GC
123class GC_Object : public GC_Traceable {
124        friend class GC;
125protected:
126        virtual ~GC_Object() {}
127public:
128        GC_Object();
129};
130
131// Local Variables: //
132// tab-width: 4 //
133// mode: c++ //
134// compile-command: "make install" //
135// End: //
Note: See TracBrowser for help on using the repository browser.