diff --git a/src/DFA.cc b/src/DFA.cc index ec7f931dfd..23aecbad60 100644 --- a/src/DFA.cc +++ b/src/DFA.cc @@ -303,7 +303,7 @@ DFA_State_Cache::~DFA_State_Cache() states.clear(); } -DFA_State* DFA_State_Cache::Lookup(const NFA_state_list& nfas, DigestStr& digest) +DFA_State* DFA_State_Cache::Lookup(const NFA_state_list& nfas, DigestStr* digest) { // We assume that state ID's don't exceed 10 digits, plus // we allow one more character for the delimiter. @@ -332,9 +332,9 @@ DFA_State* DFA_State_Cache::Lookup(const NFA_state_list& nfas, DigestStr& digest // HashKey because the data is copied into the key. u_char digest_bytes[16]; internal_md5(id_tag, p - id_tag, digest_bytes); - digest = DigestStr(digest_bytes, 16); + *digest = DigestStr(digest_bytes, 16); - auto entry = states.find(digest); + auto entry = states.find(*digest); if ( entry == states.end() ) { ++misses; @@ -342,14 +342,14 @@ DFA_State* DFA_State_Cache::Lookup(const NFA_state_list& nfas, DigestStr& digest } ++hits; - digest.clear(); + digest->clear(); return entry->second; } -DFA_State* DFA_State_Cache::Insert(DFA_State* state, const DigestStr& digest) +DFA_State* DFA_State_Cache::Insert(DFA_State* state, DigestStr digest) { - states.emplace(digest, state); + states.emplace(std::move(digest), state); return state; } @@ -432,7 +432,7 @@ bool DFA_Machine::StateSetToDFA_State(NFA_state_list* state_set, DFA_State*& d, const EquivClass* ec) { DigestStr digest; - d = dfa_state_cache->Lookup(*state_set, digest); + d = dfa_state_cache->Lookup(*state_set, &digest); if ( d ) return false; @@ -454,7 +454,7 @@ bool DFA_Machine::StateSetToDFA_State(NFA_state_list* state_set, } DFA_State* ds = new DFA_State(state_count++, ec, state_set, accept); - d = dfa_state_cache->Insert(ds, digest); + d = dfa_state_cache->Insert(ds, std::move(digest)); return true; } diff --git a/src/DFA.h b/src/DFA.h index ce51310ef9..04d6e5a280 100644 --- a/src/DFA.h +++ b/src/DFA.h @@ -75,10 +75,10 @@ public: ~DFA_State_Cache(); // If the caller stores the handle, it has to call Ref() on it. - DFA_State* Lookup(const NFA_state_list& nfa_states, DigestStr& digest); + DFA_State* Lookup(const NFA_state_list& nfa_states, DigestStr* digest); - // Takes ownership of both; hash is the one returned by Lookup(). - DFA_State* Insert(DFA_State* state, const DigestStr& digest); + // Takes ownership of state; digest is the one returned by Lookup(). + DFA_State* Insert(DFA_State* state, DigestStr digest); int NumEntries() const { return states.size(); } diff --git a/src/DebugCmds.cc b/src/DebugCmds.cc index fe48f67f25..6ae6a56f40 100644 --- a/src/DebugCmds.cc +++ b/src/DebugCmds.cc @@ -51,7 +51,7 @@ void lookup_global_symbols_regex(const string& orig_regex, vector& matches, } Scope* global = global_scope(); - auto syms = global->Vars(); + const auto& syms = global->Vars(); ID* nextid; for ( const auto& sym : syms ) diff --git a/src/EventRegistry.cc b/src/EventRegistry.cc index e5f739248f..a7ad0636f4 100644 --- a/src/EventRegistry.cc +++ b/src/EventRegistry.cc @@ -92,7 +92,7 @@ void EventRegistry::SetErrorHandler(const string& name) return; } - reporter->InternalWarning( - "unknown event handler '%s' in SetErrorHandler()", name.c_str()); + reporter->InternalWarning("unknown event handler '%s' in SetErrorHandler()", + name.c_str()); } diff --git a/src/Expr.cc b/src/Expr.cc index 874ad5d09f..dfe81278cd 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -4347,7 +4347,7 @@ LambdaExpr::LambdaExpr(std::unique_ptr arg_ing, my_name = "lambda_<" + std::to_string(h[0]) + ">"; auto fullname = make_full_var_name(current_module.data(), my_name.data()); - auto id = global_scope()->Lookup(fullname.data()); + auto id = global_scope()->Lookup(fullname); if ( id ) // Just try again to make a unique lambda name. If two peer @@ -4413,7 +4413,7 @@ EventExpr::EventExpr(const char* arg_name, ListExpr* arg_args) name = arg_name; args = arg_args; - EventHandler* h = event_registry->Lookup(name.c_str()); + EventHandler* h = event_registry->Lookup(name); if ( ! h ) { h = new EventHandler(name.c_str()); diff --git a/src/OpaqueVal.cc b/src/OpaqueVal.cc index 595102e8a2..372e6b4d6b 100644 --- a/src/OpaqueVal.cc +++ b/src/OpaqueVal.cc @@ -122,7 +122,7 @@ BroType* OpaqueVal::UnserializeType(const broker::data& data) if ( ! name ) return nullptr; - ID* id = global_scope()->Lookup(name->c_str()); + ID* id = global_scope()->Lookup(*name); if ( ! id ) return nullptr; diff --git a/src/RE.cc b/src/RE.cc index b9706375e3..5b758dfb41 100644 --- a/src/RE.cc +++ b/src/RE.cc @@ -198,7 +198,7 @@ string Specific_RE_Matcher::LookupDef(const string& def) { const auto& iter = defs.find(def); if ( iter != defs.end() ) - return iter->first; + return iter->second; return string(); } @@ -418,7 +418,10 @@ unsigned int Specific_RE_Matcher::MemoryAllocation() const size += pad_size(sizeof(CCL*) * ccl_dict.size()); for ( const auto& entry : ccl_dict ) + { size += padded_sizeof(std::string) + pad_size(sizeof(std::string::value_type) * entry.first.size()); + size += entry.second->MemoryAllocation(); + } for ( const auto& entry : defs ) { diff --git a/src/RE.h b/src/RE.h index 8c8baf9fa8..3815527bf8 100644 --- a/src/RE.h +++ b/src/RE.h @@ -126,8 +126,8 @@ protected: int multiline; char* pattern_text; - std::map defs; - std::map ccl_dict; + std::map defs; + std::map ccl_dict; PList ccl_list; EquivClass equiv_class; int* ecs; diff --git a/src/Scope.cc b/src/Scope.cc index 4c41a3acea..b445ce0e53 100644 --- a/src/Scope.cc +++ b/src/Scope.cc @@ -128,7 +128,7 @@ ID* lookup_ID(const char* name, const char* curr_module, bool no_global, for ( int i = scopes.length() - 1; i >= 0; --i ) { - ID* id = scopes[i]->Lookup(fullname.c_str()); + ID* id = scopes[i]->Lookup(fullname); if ( id ) { if ( need_export && ! id->IsExport() && ! in_debug ) @@ -144,7 +144,7 @@ ID* lookup_ID(const char* name, const char* curr_module, bool no_global, ! same_module_only) ) { string globalname = make_full_var_name(GLOBAL_MODULE_NAME, name); - ID* id = global_scope()->Lookup(globalname.c_str()); + ID* id = global_scope()->Lookup(globalname); if ( id ) { Ref(id); diff --git a/src/Stats.cc b/src/Stats.cc index c4c43e01f1..339dd14371 100644 --- a/src/Stats.cc +++ b/src/Stats.cc @@ -239,7 +239,7 @@ void ProfileLogger::Log() // Script-level state. unsigned int size, mem = 0; - auto globals = global_scope()->Vars(); + const auto& globals = global_scope()->Vars(); if ( expensive ) { diff --git a/src/Type.cc b/src/Type.cc index 8de62e7266..ba21120bba 100644 --- a/src/Type.cc +++ b/src/Type.cc @@ -1799,7 +1799,7 @@ BroType* merge_types(const BroType* t1, const BroType* t2) // Doing a lookup here as a roundabout way of ref-ing t1, without // changing the function params which has t1 as const and also // (potentially) avoiding a pitfall mentioned earlier about clones. - auto id = global_scope()->Lookup(t1->GetName().data()); + auto id = global_scope()->Lookup(t1->GetName()); if ( id && id->AsType() && id->AsType()->Tag() == TYPE_ENUM ) // It should make most sense to return the real type here rather diff --git a/src/Type.h b/src/Type.h index 17a74476ab..a4a4bcc703 100644 --- a/src/Type.h +++ b/src/Type.h @@ -264,7 +264,7 @@ public: virtual unsigned MemoryAllocation() const; void SetName(const string& arg_name) { name = arg_name; } - string GetName() const { return name; } + const string& GetName() const { return name; } typedef std::map > TypeAliasMap; diff --git a/src/broker/Data.cc b/src/broker/Data.cc index c837e4ecb1..2efbe7f277 100644 --- a/src/broker/Data.cc +++ b/src/broker/Data.cc @@ -351,7 +351,7 @@ struct val_converter { if ( ! name ) return nullptr; - auto id = global_scope()->Lookup(name->c_str()); + auto id = global_scope()->Lookup(*name); if ( ! id ) return nullptr; @@ -703,7 +703,7 @@ struct type_checker { if ( ! name ) return false; - auto id = global_scope()->Lookup(name->c_str()); + auto id = global_scope()->Lookup(*name); if ( ! id ) return false; diff --git a/src/broker/Manager.cc b/src/broker/Manager.cc index 8225738b2e..1ffa82e984 100644 --- a/src/broker/Manager.cc +++ b/src/broker/Manager.cc @@ -407,7 +407,7 @@ bool Manager::PublishIdentifier(std::string topic, std::string id) if ( peer_count == 0 ) return true; - ID* i = global_scope()->Lookup(id.c_str()); + ID* i = global_scope()->Lookup(id); if ( ! i ) return false; @@ -998,7 +998,7 @@ void Manager::ProcessEvent(const broker::topic& topic, broker::zeek::Event ev) DBG_LOG(DBG_BROKER, "Process event: %s %s", name.data(), RenderMessage(args).data()); ++statistics.num_events_incoming; - auto handler = event_registry->Lookup(name.data()); + auto handler = event_registry->Lookup(name); if ( ! handler ) return; @@ -1240,7 +1240,7 @@ bool Manager::ProcessIdentifierUpdate(broker::zeek::IdentifierUpdate iu) ++statistics.num_ids_incoming; auto id_name = std::move(iu.id_name()); auto id_value = std::move(iu.id_value()); - auto id = global_scope()->Lookup(id_name.c_str()); + auto id = global_scope()->Lookup(id_name); if ( ! id ) { diff --git a/src/file_analysis/Manager.cc b/src/file_analysis/Manager.cc index d306af4003..ca6b9722b7 100644 --- a/src/file_analysis/Manager.cc +++ b/src/file_analysis/Manager.cc @@ -33,7 +33,7 @@ Manager::~Manager() for ( MIMEMap::iterator i = mime_types.begin(); i != mime_types.end(); i++ ) delete i->second; - // Have to assume that too much of Bro has been shutdown by this point + // Have to assume that too much of Zeek has been shutdown by this point // to do anything more than reclaim memory. for ( const auto& entry : id_map ) delete entry.second; @@ -58,6 +58,8 @@ void Manager::InitMagic() void Manager::Terminate() { vector keys; + keys.reserve(id_map.size()); + for ( const auto& entry : id_map ) keys.push_back(entry.first); @@ -406,8 +408,8 @@ bool Manager::RemoveFile(const string& file_id) f->EndOfFile(); delete f; - id_map.erase(file_id); + id_map.erase(file_id); ignored.erase(file_id); return true; } diff --git a/src/input/Manager.cc b/src/input/Manager.cc index 4b275c6b9b..e57a498645 100644 --- a/src/input/Manager.cc +++ b/src/input/Manager.cc @@ -1852,7 +1852,7 @@ bool Manager::SendEvent(ReaderFrontend* reader, const string& name, const int nu return false; } - EventHandler* handler = event_registry->Lookup(name.c_str()); + EventHandler* handler = event_registry->Lookup(name); if ( handler == 0 ) { Warning(i, "Event %s not found", name.c_str()); diff --git a/src/input/readers/config/Config.cc b/src/input/readers/config/Config.cc index 3cede2c5e3..d90020e1bf 100644 --- a/src/input/readers/config/Config.cc +++ b/src/input/readers/config/Config.cc @@ -28,7 +28,7 @@ Config::Config(ReaderFrontend *frontend) : ReaderBackend(frontend) fail_on_file_problem = false; // find all option names and their types. - auto globals = global_scope()->Vars(); + const auto& globals = global_scope()->Vars(); for ( const auto& entry : globals ) { diff --git a/src/zeek.bif b/src/zeek.bif index 604fbd2799..18b5c67a10 100644 --- a/src/zeek.bif +++ b/src/zeek.bif @@ -1895,7 +1895,7 @@ function reading_traces%(%): bool function global_sizes%(%): var_sizes %{ TableVal* sizes = new TableVal(var_sizes); - auto globals = global_scope()->Vars(); + const auto& globals = global_scope()->Vars(); for ( const auto& global : globals ) { @@ -1923,7 +1923,7 @@ function global_sizes%(%): var_sizes function global_ids%(%): id_table %{ TableVal* ids = new TableVal(id_table); - auto globals = global_scope()->Vars(); + const auto& globals = global_scope()->Vars(); for ( const auto& global : globals ) {