| 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_Object;
|
|---|
| 21 | class BaseSyntaxNode;
|
|---|
| 22 |
|
|---|
| 23 | /// Manually traced and called garbage collector
|
|---|
| 24 | class GC {
|
|---|
| 25 | friend class GcTracer;
|
|---|
| 26 | public:
|
|---|
| 27 | /// Gets singleton GC instance
|
|---|
| 28 | static GC& get();
|
|---|
| 29 |
|
|---|
| 30 | /// Traces a traceable object
|
|---|
| 31 | const GC& operator<< (const GC_Object*) const;
|
|---|
| 32 |
|
|---|
| 33 | /// Adds a new object to garbage collection
|
|---|
| 34 | void register_object(GC_Object*);
|
|---|
| 35 |
|
|---|
| 36 | /// Adds an object to the set of static roots
|
|---|
| 37 | void register_static_root(BaseSyntaxNode*);
|
|---|
| 38 |
|
|---|
| 39 | /// Start new generation for subsequent new objects
|
|---|
| 40 | void new_generation();
|
|---|
| 41 |
|
|---|
| 42 | /// Traces all static roots
|
|---|
| 43 | void trace_static_roots();
|
|---|
| 44 |
|
|---|
| 45 | /// Collects the youngest generation, placing survivors in previous generation.
|
|---|
| 46 | /// Young generation objects cannot be kept alive by pointers from older generation.
|
|---|
| 47 | /// Older generation is used for subsequent new objects.
|
|---|
| 48 | void collect_young();
|
|---|
| 49 |
|
|---|
| 50 | /// Collects oldest generation; use oldest generation afterward.
|
|---|
| 51 | /// Error if currently using younger generation
|
|---|
| 52 | void collect();
|
|---|
| 53 |
|
|---|
| 54 | /// Collects all contained objects
|
|---|
| 55 | ~GC();
|
|---|
| 56 |
|
|---|
| 57 | private:
|
|---|
| 58 | GC();
|
|---|
| 59 |
|
|---|
| 60 | using Generation = std::vector<GC_Object*>;
|
|---|
| 61 | std::vector<Generation> gens; ///< Set of generations; always at least one
|
|---|
| 62 |
|
|---|
| 63 | using StaticRoots = std::vector<BaseSyntaxNode*>;
|
|---|
| 64 | StaticRoots static_roots; ///< Set of static-lifetime roots
|
|---|
| 65 |
|
|---|
| 66 | bool mark; ///< The current collection's mark bit
|
|---|
| 67 | unsigned g; ///< The current number generation in use
|
|---|
| 68 | };
|
|---|
| 69 |
|
|---|
| 70 | /// Use young generation until next collection
|
|---|
| 71 | inline 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 |
|
|---|
| 77 | inline void traceAll(const GC&) {}
|
|---|
| 78 |
|
|---|
| 79 | /// Marks all arguments as live in current generation
|
|---|
| 80 | template<typename T, typename... Args>
|
|---|
| 81 | inline 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
|
|---|
| 87 | template<typename... Args>
|
|---|
| 88 | inline 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
|
|---|
| 96 | template<typename... Args>
|
|---|
| 97 | inline 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
|
|---|
| 105 | template<typename T, typename... Args>
|
|---|
| 106 | inline 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 managed by the GC
|
|---|
| 113 | class GC_Object {
|
|---|
| 114 | friend class GC;
|
|---|
| 115 | protected:
|
|---|
| 116 | mutable bool mark;
|
|---|
| 117 |
|
|---|
| 118 | // Override default constructors to ensure clones are registered and properly marked
|
|---|
| 119 | GC_Object();
|
|---|
| 120 |
|
|---|
| 121 | GC_Object(const GC_Object&);
|
|---|
| 122 |
|
|---|
| 123 | GC_Object(GC_Object&&);
|
|---|
| 124 |
|
|---|
| 125 | GC_Object& operator= (const GC_Object&) { /* do not assign mark */ return *this; }
|
|---|
| 126 |
|
|---|
| 127 | GC_Object& operator= (GC_Object&&) { /* do not assign mark */ return *this; }
|
|---|
| 128 |
|
|---|
| 129 | // Ensure subclasses can be deleted by garbage collector
|
|---|
| 130 | virtual ~GC_Object() {}
|
|---|
| 131 |
|
|---|
| 132 | /// override to trace any child objects
|
|---|
| 133 | virtual void trace(const GC&) const {}
|
|---|
| 134 | };
|
|---|
| 135 |
|
|---|
| 136 | // Local Variables: //
|
|---|
| 137 | // tab-width: 4 //
|
|---|
| 138 | // mode: c++ //
|
|---|
| 139 | // compile-command: "make install" //
|
|---|
| 140 | // End: //
|
|---|