source: src/Common/GC.h @ 68f9c43

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

First pass at delete removal

  • Property mode set to 100644
File size: 2.6 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;
22
23/// Manually traced and called garbage collector
24class GC {
25        friend class GcTracer;
26public:
27        /// Gets singleton GC instance
28        static GC& get();
29
30        /// Traces a traceable object
31        const GC& operator<< (const GC_Traceable*) const;
32
33        /// Adds a new object to garbage collection
34        void register_object(GC_Object*);
35
36        /// Use young generation for subsequent new objects
37        void new_generation();
38
39        /// Collects the young generation, placing survivors in old generation.
40        /// Old generation is used for subsequent new objects.
41        void collect_young();
42
43        /// Collects all memory; use old generation afterward.
44        void collect();
45
46        /// Collects all contained objects
47        ~GC();
48
49private:
50        GC();
51
52        /// The current collection's mark bit
53        bool mark;
54
55        typedef std::vector<class GC_Object*> Generation;
56        Generation old;
57        Generation young;
58        bool using_young;
59};
60
61/// Use young generation until next collection
62inline void new_generation() { GC::get().new_generation(); }
63
64/// no-op default trace
65template<typename T>
66inline const GC& operator<< (const GC& gc, const T& x) { return gc; }
67
68inline void traceAll(const GC& gc) {}
69
70/// Marks all arguments as live in current generation
71template<typename T, typename... Args>
72inline void traceAll(const GC& gc, T& x, Args&... xs) {
73        gc << x;
74        traceAll(gc, xs...);
75}
76
77/// Traces young-generation roots and does a young collection
78template<typename... Args>
79inline void collect_young(Args&... roots) {
80        GC& gc = GC::get();
81        traceAll(gc, roots...);
82        gc.collect_young();
83}
84
85/// Traces roots and collects other elements
86template<typename... Args>
87inline void collect(Args&... roots) {
88        GC& gc = GC::get();
89        traceAll(gc, roots...);
90        gc.collect();
91}
92
93/// Class that is traced by the GC, but not managed by it
94class GC_Traceable {
95        friend class GC;
96        friend class GcTracer;
97
98        mutable bool mark;
99protected:
100        /// override to trace any child objects
101        virtual void trace(const GC& gc) const {}
102};
103
104/// Class that is managed by the GC
105class GC_Object : public GC_Traceable {
106        friend class GC;
107protected:
108        virtual ~GC_Object() {}
109public:
110        GC_Object();
111};
112
113// Local Variables: //
114// tab-width: 4 //
115// mode: c++ //
116// compile-command: "make install" //
117// End: //
Note: See TracBrowser for help on using the repository browser.