From 785ff57d11a9a835843faa90041c93cc7ea7cca2 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 6 Mar 2020 13:03:41 +0100 Subject: [PATCH] Scope: store IntrusivePtr in `local` --- src/DebugCmds.cc | 2 +- src/Scope.cc | 11 ++++------- src/Scope.h | 23 +++++++---------------- src/Stats.cc | 2 +- src/input/readers/config/Config.cc | 2 +- src/zeek.bif | 4 ++-- 6 files changed, 16 insertions(+), 28 deletions(-) diff --git a/src/DebugCmds.cc b/src/DebugCmds.cc index e9a6720dcd..f59651d65e 100644 --- a/src/DebugCmds.cc +++ b/src/DebugCmds.cc @@ -62,7 +62,7 @@ void lookup_global_symbols_regex(const string& orig_regex, vector& matches, ID* nextid; for ( const auto& sym : syms ) { - ID* nextid = sym.second; + ID* nextid = sym.second.get(); if ( ! func_only || nextid->Type()->Tag() == TYPE_FUNC ) if ( ! regexec (&re, nextid->Name(), 0, 0, 0) ) matches.push_back(nextid); diff --git a/src/Scope.cc b/src/Scope.cc index fe834a3fd4..8c0e3e951f 100644 --- a/src/Scope.cc +++ b/src/Scope.cc @@ -40,9 +40,6 @@ Scope::Scope(IntrusivePtr id, attr_list* al) Scope::~Scope() { - for ( const auto& entry : local ) - Unref(entry.second); - if ( attrs ) { for ( const auto& attr : *attrs ) @@ -101,7 +98,7 @@ void Scope::Describe(ODesc* d) const for ( const auto& entry : local ) { - ID* id = entry.second; + ID* id = entry.second.get(); id->Describe(d); d->NL(); } @@ -111,7 +108,7 @@ TraversalCode Scope::Traverse(TraversalCallback* cb) const { for ( const auto& entry : local ) { - ID* id = entry.second; + ID* id = entry.second.get(); TraversalCode tc = id->Traverse(cb); HANDLE_TC_STMT_PRE(tc); } @@ -176,11 +173,11 @@ IntrusivePtr install_ID(const char* name, const char* module_name, auto id = make_intrusive(full_name.data(), scope, is_export); if ( SCOPE_FUNCTION != scope ) - global_scope()->Insert(std::move(full_name), IntrusivePtr{id}.release()); + global_scope()->Insert(std::move(full_name), id); else { id->SetOffset(top_scope->Length()); - top_scope->Insert(std::move(full_name), IntrusivePtr{id}.release()); + top_scope->Insert(std::move(full_name), id); } return id; diff --git a/src/Scope.h b/src/Scope.h index 5b8c96066c..ee496ebfb1 100644 --- a/src/Scope.h +++ b/src/Scope.h @@ -27,31 +27,22 @@ public: const auto& entry = local.find(std::forward(name)); if ( entry != local.end() ) - return entry->second; + return entry->second.get(); return nullptr; } - template - void Insert(N&& name, ID* id) - { - auto [it, inserted] = local.emplace(std::forward(name), id); - - if ( ! inserted ) - { - Unref(it->second); - it->second = id; - } - } + template + void Insert(N&& name, I&& id) { local[std::forward(name)] = std::forward(id); } template - ID* Remove(N&& name) + IntrusivePtr Remove(N&& name) { const auto& entry = local.find(std::forward(name)); if ( entry != local.end() ) { - ID* id = entry->second; + auto id = std::move(entry->second); local.erase(entry); return id; } @@ -64,7 +55,7 @@ public: BroType* ReturnType() const { return return_type.get(); } size_t Length() const { return local.size(); } - const std::map& Vars() { return local; } + const auto& Vars() { return local; } ID* GenerateTemporary(const char* name); @@ -83,7 +74,7 @@ protected: IntrusivePtr scope_id; attr_list* attrs; IntrusivePtr return_type; - std::map local; + std::map> local; id_list* inits; }; diff --git a/src/Stats.cc b/src/Stats.cc index 7459f24a6f..4ab911507e 100644 --- a/src/Stats.cc +++ b/src/Stats.cc @@ -252,7 +252,7 @@ void ProfileLogger::Log() for ( const auto& global : globals ) { - ID* id = global.second; + ID* id = global.second.get(); // We don't show/count internal globals as they are always // contained in some other global user-visible container. diff --git a/src/input/readers/config/Config.cc b/src/input/readers/config/Config.cc index ab11f80487..2908484f37 100644 --- a/src/input/readers/config/Config.cc +++ b/src/input/readers/config/Config.cc @@ -31,7 +31,7 @@ Config::Config(ReaderFrontend *frontend) : ReaderBackend(frontend) for ( const auto& entry : globals ) { - ID* id = entry.second; + ID* id = entry.second.get(); if ( ! id->IsOption() ) continue; diff --git a/src/zeek.bif b/src/zeek.bif index ee1ed259c7..cbd7bc0b54 100644 --- a/src/zeek.bif +++ b/src/zeek.bif @@ -1924,7 +1924,7 @@ function global_sizes%(%): var_sizes for ( const auto& global : globals ) { - ID* id = global.second; + ID* id = global.second.get(); if ( id->HasVal() ) { Val* id_name = new StringVal(id->Name()); @@ -1952,7 +1952,7 @@ function global_ids%(%): id_table for ( const auto& global : globals ) { - ID* id = global.second; + ID* id = global.second.get(); auto rec = make_intrusive(script_id); rec->Assign(0, make_intrusive(type_name(id->Type()->Tag()))); rec->Assign(1, val_mgr->GetBool(id->IsExport()));