- Timestamp:
- Apr 13, 2018, 4:39:28 PM (6 years ago)
- Branches:
- new-env, with_gc
- Children:
- 6f81db3
- Parents:
- 3205495
- git-author:
- Aaron Moss <a3moss@…> (04/13/18 16:27:11)
- git-committer:
- Aaron Moss <a3moss@…> (04/13/18 16:39:28)
- Location:
- src/Common
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Common/GC.cc
r3205495 r04570c7 28 28 } 29 29 30 GC::GC() : mark(false), using_young(false), old(), young(), static_roots() {31 old.reserve(70000);30 GC::GC() : gens(1), static_roots(), mark(false), g(0) { 31 gens[0].reserve(70000); 32 32 } 33 33 34 34 GC::~GC() { 35 for ( GC_Object* o : young ) { 36 delete o; 37 } 38 39 for ( GC_Object* o : old ) { 40 delete o; 35 for ( unsigned i = 0; i <= g; ++i ) { 36 for ( GC_Object* o : gens[i] ) { 37 delete o; 38 } 41 39 } 42 40 } … … 54 52 55 53 void GC::register_object(GC_Object* obj) { 56 (using_young ? young : old).push_back(obj);54 gens[g].push_back(obj); 57 55 obj->mark = !this->mark; // initialize as un-marked 58 56 } … … 63 61 64 62 void GC::new_generation() { 65 assert(!using_young && "Cannot start new generation when young generation already in use"); 66 67 using_young = true; // mark young generation as in-use 68 mark = !mark; // switch mark so aged young objects will still be unmarked in old 63 if ( ++g == gens.size() ) { gens.emplace_back(); } // ensure new generation available 64 mark = !mark; // switch mark so aged young objects will still be unmarked in old 69 65 } 70 66 … … 77 73 78 74 void GC::collect_young() { 75 // get generations and decrement generation 76 assert(g > 0 && "Cannot collect_young without young generation"); 77 Generation& young = gens[g]; 78 Generation& old = gens[--g]; 79 79 80 // collect young gen 80 81 for ( GC_Object*& obj : young ) { … … 89 90 old.insert( old.end(), young.begin(), end_live ); 90 91 91 // clear young gen 92 using_young = false; 92 // clear young gen and reset mark to return to old generation mark 93 93 young.clear(); 94 95 // reset mark to return to old generation mark96 94 mark = !mark; 97 95 } … … 99 97 void GC::collect() { 100 98 // ensure not called when young gen is active 101 assert(!using_young && "Cannot do old collection when young generation is active"); 99 assert(g == 0 && "Cannot do old collection when young generation is active"); 100 Generation& old = gens[0]; 102 101 103 102 // collect old gen -
src/Common/GC.h
r3205495 r04570c7 38 38 void register_static_root(BaseSyntaxNode*); 39 39 40 /// Use younggeneration for subsequent new objects40 /// Start new generation for subsequent new objects 41 41 void new_generation(); 42 42 … … 44 44 void trace_static_roots(); 45 45 46 /// Collects the young generation, placing survivors in old generation. 47 /// Old generation is used for subsequent new objects. 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. 48 49 void collect_young(); 49 50 50 /// Collects old generation; use oldgeneration afterward.51 /// Error if currently using young generation51 /// Collects oldest generation; use oldest generation afterward. 52 /// Error if currently using younger generation 52 53 void collect(); 53 54 … … 58 59 GC(); 59 60 60 bool mark; ///< The current collection's mark bit61 bool using_young; ///< Is the young generation in use?62 63 61 using Generation = std::vector<GC_Object*>; 64 Generation old; ///< Old generation 65 Generation young; ///< Young generation 62 std::vector<Generation> gens; ///< Set of generations; always at least one 66 63 67 64 using StaticRoots = std::vector<BaseSyntaxNode*>; 68 StaticRoots static_roots; ///< Set of static-lifetime roots 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 69 }; 70 70
Note: See TracChangeset
for help on using the changeset viewer.