diff --git a/src/script_opt/ReachingDefs.cc b/src/script_opt/ReachingDefs.cc index 197f0327a2..fc243b8486 100644 --- a/src/script_opt/ReachingDefs.cc +++ b/src/script_opt/ReachingDefs.cc @@ -9,13 +9,13 @@ namespace zeek::detail { ReachingDefs::ReachingDefs() { - my_rd_map = std::make_unique(); + my_rd_map = std::make_shared(); const_rd_map = nullptr; } ReachingDefs::ReachingDefs(RDPtr rd) { - const_rd_map = std::move(rd); + const_rd_map = rd->RDMap(); my_rd_map = nullptr; } @@ -65,7 +65,7 @@ RDPtr ReachingDefs::Intersect(const RDPtr& r) const auto res = make_intrusive(); - for ( const auto& i : *RDMap() ) + for ( const auto& i : *RDMapRef() ) for ( const auto& dp : *i.second ) { if ( r->HasPair(i.first, dp) ) @@ -87,7 +87,7 @@ RDPtr ReachingDefs::Union(const RDPtr& r) const res->AddRDs(r); - for ( const auto& i : *RDMap() ) + for ( const auto& i : *RDMapRef() ) for ( const auto& dp : *i.second ) res->AddRD(i.first, dp); @@ -102,7 +102,7 @@ RDPtr ReachingDefs::IntersectWithConsolidation(const RDPtr& r, auto res = make_intrusive(); - for ( const auto& i : *RDMap() ) + for ( const auto& i : *RDMapRef() ) for ( const auto& dp : *i.second ) { if ( r->HasPair(i.first, dp) ) @@ -122,7 +122,7 @@ RDPtr ReachingDefs::IntersectWithConsolidation(const RDPtr& r, bool ReachingDefs::HasPair(const DefinitionItem* di, const DefinitionPoint& dp) const { - auto map = RDMap(); + const auto& map = RDMapRef(); auto l = map->find(di); if ( l == map->end() ) @@ -135,7 +135,7 @@ const return false; } -void ReachingDefs::AddRDs(const ReachingDefsMap* rd_m) +void ReachingDefs::AddRDs(const std::shared_ptr& rd_m) { for ( const auto& one_rd : *rd_m ) for ( const auto& dp : *one_rd.second ) @@ -147,18 +147,18 @@ void ReachingDefs::CopyMapIfNeeded() if ( my_rd_map ) return; - my_rd_map = std::make_unique(); - auto old_const_rd_map = const_rd_map; + my_rd_map = std::make_shared(); + auto old_const_rd_map = std::move(const_rd_map); const_rd_map = nullptr; AddRDs(old_const_rd_map); } void ReachingDefs::Dump() const { - DumpMap(RDMap()); + DumpMap(RDMapRef()); } -void ReachingDefs::DumpMap(const ReachingDefsMap* map) const +void ReachingDefs::DumpMap(const std::shared_ptr& map) const { printf("%d RD element%s: ", Size(), Size() == 1 ? "" : "s"); diff --git a/src/script_opt/ReachingDefs.h b/src/script_opt/ReachingDefs.h index fed6657ec7..11605b3bb0 100644 --- a/src/script_opt/ReachingDefs.h +++ b/src/script_opt/ReachingDefs.h @@ -45,7 +45,7 @@ public: // Add in all the definition points from rd into our set, if // we don't already have them. - void AddRDs(const RDPtr& rd) { AddRDs(rd->RDMap()); } + void AddRDs(const RDPtr& rd) { AddRDs(rd->RDMapRef()); } // Add in a single definition pair, creating the entry for // the item if necessary. @@ -59,7 +59,7 @@ public: // True if the given definition item is present in our RDs. bool HasDI(const DefinitionItem* di) const { - auto map = RDMap(); + const auto& map = RDMapRef(); return map->find(di) != map->end(); } @@ -67,7 +67,7 @@ public: // points at our location in the AST. DefPoints* GetDefPoints(const DefinitionItem* di) { - auto map = RDMap(); + const auto& map = RDMapRef(); auto dps = map->find(di); return dps == map->end() ? nullptr : dps->second.get(); } @@ -115,11 +115,11 @@ public: RDPtr IntersectWithConsolidation(const RDPtr& r, const DefinitionPoint& di) const; - int Size() const { return RDMap()->size(); } + int Size() const { return RDMapRef()->size(); } // Print out the RDs, for debugging purposes. void Dump() const; - void DumpMap(const ReachingDefsMap* map) const; + void DumpMap(const std::shared_ptr& map) const; protected: // True if our RDs include the given definition item, defined at @@ -127,10 +127,13 @@ protected: bool HasPair(const DefinitionItem* di, const DefinitionPoint& dp) const; // Adds in the given RDs if we don't already have them. - void AddRDs(const ReachingDefsMap* rd_m); + void AddRDs(const std::shared_ptr& rd_m); - const ReachingDefsMap* RDMap() const - { return my_rd_map ? my_rd_map.get() : const_rd_map->RDMap(); } + std::shared_ptr RDMap() const + { return my_rd_map ? my_rd_map : const_rd_map; } + + const std::shared_ptr& RDMapRef() const + { return my_rd_map ? my_rd_map : const_rd_map; } // If we don't already have our own map, copy the one we're using // so that we then do. @@ -140,9 +143,11 @@ protected: void PrintRD(const DefinitionItem* di, const DefinitionPoint& dp) const; // If my_rd_map is non-nil, then we use that map. Otherwise, - // we use the map that const_rd_map points to. - RDPtr const_rd_map; - std::unique_ptr my_rd_map; + // we use the map that const_rd_map points to. The "const" in + // the latter's name is a reminder to not make any changes to + // that map. + std::shared_ptr my_rd_map; + std::shared_ptr const_rd_map; };