Changeset 2efe4b8 for src/Common/GC.h


Ignore:
Timestamp:
Apr 25, 2018, 3:42:34 PM (4 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
new-env, with_gc
Children:
1cdfa82
Parents:
5af7306
Message:

Assorted GC bugfixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/GC.h

    r5af7306 r2efe4b8  
    1616#pragma once
    1717
     18#include <cassert>
    1819#include <vector>
    1920
    2021class GC_Object;
    2122class BaseSyntaxNode;
     23class GC_Guard;
    2224
    2325/// Manually traced and called garbage collector
    2426class GC {
    2527        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();
    2634public:
    2735        /// Gets singleton GC instance
     
    3846
    3947        /// Start new generation for subsequent new objects
    40         void new_generation();
     48        GC_Guard new_generation();
    4149
    4250        /// Traces all static roots
    4351        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();
    4952
    5053        /// Collects oldest generation; use oldest generation afterward.
     
    6871};
    6972
     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
    7089/// Use young generation until next collection
    71 inline void new_generation() { GC::get().new_generation(); }
     90inline GC_Guard new_generation() { return GC::get().new_generation(); }
    7291
    7392// /// no-op default trace
     
    84103}
    85104
    86 /// Traces young-generation roots and does a young collection
     105/// Traces roots without collecting
    87106template<typename... Args>
    88 inline void collect_young(Args&... roots) {
     107inline void trace(Args&... roots) {
    89108        GC& gc = GC::get();
    90109        traceAll(gc, roots...);
    91110        gc.trace_static_roots();
    92         gc.collect_young();
    93111}
    94112
    95 /// Traces roots and collects other elements
     113/// Traces roots and collects other elements; should not be any young generations live
    96114template<typename... Args>
    97115inline void collect(Args&... roots) {
Note: See TracChangeset for help on using the changeset viewer.