Changeset 2efe4b8


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

Assorted GC bugfixes

Location:
src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/Common/GC.cc

    r5af7306 r2efe4b8  
    7777}
    7878
    79 void GC::new_generation() {
     79GC_Guard GC::new_generation() {
    8080        if ( ++g == gens.size() ) { gens.emplace_back(); }  // ensure new generation available
    8181        mark = !mark;  // switch mark so aged young objects will still be unmarked in old
     82        return { *this, g };
    8283}
    8384
  • 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) {
  • src/GenPoly/Box.cc

    r5af7306 r2efe4b8  
    283283                for ( std::list< TypeDecl* >::const_iterator param = otypeParams.begin();
    284284                                param != otypeParams.end(); ++param ) {
    285                         TypeInstType paramType( Type::Qualifiers(), (*param)->get_name(), *param );
    286                         std::string paramName = mangleType( &paramType );
     285                        auto paramType = new TypeInstType( Type::Qualifiers(), (*param)->get_name(), *param );
     286                        std::string paramName = mangleType( paramType );
    287287                        layoutFnType->get_parameters().push_back( new ObjectDecl( sizeofName( paramName ), Type::StorageClasses(), LinkageSpec::Cforall, 0, sizeAlignType->clone(), 0 ) );
    288288                        layoutFnType->get_parameters().push_back( new ObjectDecl( alignofName( paramName ), Type::StorageClasses(), LinkageSpec::Cforall, 0, sizeAlignType->clone(), 0 ) );
     
    14361436                        if ( Type * base = typeDecl->base ) {
    14371437                                // add size/align variables for opaque type declarations
    1438                                 TypeInstType inst( Type::Qualifiers(), typeDecl->name, typeDecl );
    1439                                 std::string typeName = mangleType( &inst );
     1438                                auto inst = new TypeInstType( Type::Qualifiers(), typeDecl->name, typeDecl );
     1439                                std::string typeName = mangleType( inst );
    14401440                                Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
    14411441
  • src/ResolvExpr/Resolver.cc

    r5af7306 r2efe4b8  
    148148                        assertf( untyped, "expected a non-null expression." );
    149149
    150                         new_generation();  // set up GC young generation for this top-level expression
     150                        auto guard = new_generation();  // set up GC generation for this top-level expression
    151151
    152152                        TypeEnvironment env;
     
    177177                        findMinCost( candidates.begin(), candidates.end(), back_inserter( winners ) );
    178178                        if ( winners.size() == 0 ) {
    179                                 collect_young();
    180179                                SemanticError( untyped, toString(
    181180                                        "No reasonable alternatives for ", kindStr, (kindStr != "" ? " " : ""),
     
    189188                                printAlts( winners, stream, 1 );
    190189                               
    191                                 collect_young();
    192190                                SemanticError( untyped->location, stream.str() );
    193191                        }
     
    196194                        Alternative & choice = winners.front();
    197195                        if ( findDeletedExpr( choice.expr ) ) {
    198                                 collect_young( choice.expr );
     196                                trace( choice.expr );
    199197                                SemanticError( choice.expr,
    200198                                        "Unique best alternative includes deleted identifier in " );
    201199                        }
    202200                        alt = std::move( choice );
    203                         collect_young( alt );
     201                        trace( alt );
    204202                }
    205203
  • src/SynTree/GcTracer.h

    r5af7306 r2efe4b8  
    2020#include "BaseSyntaxNode.h"
    2121#include "Expression.h"
     22#include "Label.h"
    2223#include "Type.h"
    2324
     
    5354        }
    5455
     56        void postvisit( AggregateDecl* decl ) {
     57                acceptAll( decl->attributes, *visitor );
     58        }
     59
    5560        void postvisit( DeclarationWithType* decl ) {
    5661                maybeAccept( decl->asmName, *visitor );
     
    7378        }
    7479
     80        void postvisit( UniqueExpr* expr ) {
     81                postvisit( static_cast<Expression*>(expr) );
     82                maybeAccept( expr->object, *visitor );
     83                maybeAccept( expr->var, *visitor );
     84        }
     85
    7586        void postvisit( UntypedExpr* expr ) {
    7687                postvisit( static_cast<Expression*>(expr) );
     
    8192                postvisit( static_cast<Expression*>(expr) );
    8293                maybeAccept( expr->var, *visitor );  // not in PassVisitor because it causes cycle
     94        }
     95
     96private:
     97        void visit( Label& lbl ) {
     98                acceptAll( lbl.get_attributes(), *visitor );
     99                maybeAccept( lbl.get_statement(), *visitor );  // xxx - not sure this is needed...
     100        }
     101
     102public:
     103        void postvisit( Statement* stmt ) {
     104                for ( Label& l : stmt->labels ) {
     105                        visit( l );
     106                }
    83107        }
    84108
Note: See TracChangeset for help on using the changeset viewer.