From a3deb0446c899ec1daafa451f69111956d826b11 Mon Sep 17 00:00:00 2001 From: Tim Wojtulewicz Date: Fri, 26 Jul 2019 13:04:29 -0700 Subject: [PATCH] Scope: remove uses of PDict --- src/DebugCmds.cc | 8 +++++--- src/Scope.cc | 20 +++++++----------- src/Scope.h | 33 +++++++++++++++++++++--------- src/Stats.cc | 9 ++++---- src/Var.cc | 2 +- src/input/readers/config/Config.cc | 4 ++-- src/zeek.bif | 16 +++++++-------- 7 files changed, 51 insertions(+), 41 deletions(-) diff --git a/src/DebugCmds.cc b/src/DebugCmds.cc index dcba46d195..fe48f67f25 100644 --- a/src/DebugCmds.cc +++ b/src/DebugCmds.cc @@ -51,14 +51,16 @@ void lookup_global_symbols_regex(const string& orig_regex, vector& matches, } Scope* global = global_scope(); - PDict* syms = global->Vars(); + auto syms = global->Vars(); ID* nextid; - IterCookie* cookie = syms->InitForIteration(); - while ( (nextid = syms->NextEntry( cookie )) ) + for ( const auto& sym : syms ) + { + ID* nextid = sym.second; if ( ! func_only || nextid->Type()->Tag() == TYPE_FUNC ) if ( ! regexec (&re, nextid->Name(), 0, 0, 0) ) matches.push_back(nextid); + } } void choose_global_symbols_regex(const string& regex, vector& choices, diff --git a/src/Scope.cc b/src/Scope.cc index 20492a3f15..4c41a3acea 100644 --- a/src/Scope.cc +++ b/src/Scope.cc @@ -19,7 +19,6 @@ Scope::Scope(ID* id, attr_list* al) attrs = al; return_type = 0; - local = new PDict(ORDERED); inits = new id_list; if ( id ) @@ -42,8 +41,8 @@ Scope::Scope(ID* id, attr_list* al) Scope::~Scope() { - for ( int i = 0; i < local->Length(); ++i ) - Unref(local->NthEntry(i)); + for ( const auto& entry : local ) + Unref(entry.second); if ( attrs ) { @@ -55,7 +54,6 @@ Scope::~Scope() Unref(scope_id); Unref(return_type); - delete local; delete inits; } @@ -82,7 +80,7 @@ void Scope::Describe(ODesc* d) const d->SP(); d->Add(return_type != 0); d->SP(); - d->Add(local->Length()); + d->Add(static_cast(local.size())); d->SP(); } @@ -98,9 +96,9 @@ void Scope::Describe(ODesc* d) const d->NL(); } - for ( int i = 0; i < local->Length(); ++i ) + for ( const auto& entry : local ) { - ID* id = local->NthEntry(i); + ID* id = entry.second; id->Describe(d); d->NL(); } @@ -108,13 +106,9 @@ void Scope::Describe(ODesc* d) const TraversalCode Scope::Traverse(TraversalCallback* cb) const { - PDict* ids = GetIDs(); - IterCookie* iter = ids->InitForIteration(); - - HashKey* key; - ID* id; - while ( (id = ids->NextEntry(key, iter)) ) + for ( const auto& entry : local ) { + ID* id = entry.second; TraversalCode tc = id->Traverse(cb); HANDLE_TC_STMT_PRE(tc); } diff --git a/src/Scope.h b/src/Scope.h index 954e0a57cc..4eca1d46f5 100644 --- a/src/Scope.h +++ b/src/Scope.h @@ -4,6 +4,7 @@ #define scope_h #include +#include #include "Dict.h" #include "Obj.h" @@ -20,25 +21,37 @@ public: explicit Scope(ID* id, attr_list* al); ~Scope() override; - ID* Lookup(const char* name) const { return local->Lookup(name); } - void Insert(const char* name, ID* id) { local->Insert(name, id); } - ID* Remove(const char* name) + ID* Lookup(const std::string& name) const { - HashKey key(name); - return (ID*) local->Remove(&key); + const auto& entry = local.find(name); + if ( entry != local.end() ) + return entry->second; + + return nullptr; + } + void Insert(const std::string& name, ID* id) { local[name] = id; } + ID* Remove(const std::string& name) + { + const auto& entry = local.find(name); + if ( entry != local.end() ) + { + ID* id = entry->second; + local.erase(entry); + return id; + } + + return nullptr; } ID* ScopeID() const { return scope_id; } attr_list* Attrs() const { return attrs; } BroType* ReturnType() const { return return_type; } - int Length() const { return local->Length(); } - PDict* Vars() const { return local; } + size_t Length() const { return local.size(); } + std::map& Vars() { return local; } ID* GenerateTemporary(const char* name); - PDict* GetIDs() const { return local; } - // Returns the list of variables needing initialization, and // removes it from this Scope. id_list* GetInits(); @@ -54,7 +67,7 @@ protected: ID* scope_id; attr_list* attrs; BroType* return_type; - PDict* local; + std::map local; id_list* inits; }; diff --git a/src/Stats.cc b/src/Stats.cc index 674c2fb50c..c4c43e01f1 100644 --- a/src/Stats.cc +++ b/src/Stats.cc @@ -239,7 +239,7 @@ void ProfileLogger::Log() // Script-level state. unsigned int size, mem = 0; - PDict* globals = global_scope()->Vars(); + auto globals = global_scope()->Vars(); if ( expensive ) { @@ -249,10 +249,10 @@ void ProfileLogger::Log() file->Write(fmt("%.06f Global_sizes > 100k: %dK\n", network_time, mem / 1024)); - ID* id; - IterCookie* c = globals->InitForIteration(); + for ( const auto& global : globals ) + { + ID* id = global.second; - while ( (id = globals->NextEntry(c)) ) // We don't show/count internal globals as they are always // contained in some other global user-visible container. if ( id->HasVal() ) @@ -298,6 +298,7 @@ void ProfileLogger::Log() file->Write("\n"); } } + } file->Write(fmt("%.06f Global_sizes total: %dK\n", network_time, mem / 1024)); diff --git a/src/Var.cc b/src/Var.cc index 73baab9683..119ded1a50 100644 --- a/src/Var.cc +++ b/src/Var.cc @@ -429,7 +429,7 @@ TraversalCode OuterIDBindingFinder::PreExpr(const Expr* expr) if ( e->Id()->IsGlobal() ) return TC_CONTINUE; - if ( scope->GetIDs()->Lookup(e->Id()->Name()) ) + if ( scope->Lookup(e->Id()->Name()) ) return TC_CONTINUE; outer_id_references.push_back(e); diff --git a/src/input/readers/config/Config.cc b/src/input/readers/config/Config.cc index 8f0447cf66..3cede2c5e3 100644 --- a/src/input/readers/config/Config.cc +++ b/src/input/readers/config/Config.cc @@ -29,10 +29,10 @@ Config::Config(ReaderFrontend *frontend) : ReaderBackend(frontend) // find all option names and their types. auto globals = global_scope()->Vars(); - auto c = globals->InitForIteration(); - while ( auto id = globals->NextEntry(c) ) + for ( const auto& entry : globals ) { + ID* id = entry.second; if ( ! id->IsOption() ) continue; diff --git a/src/zeek.bif b/src/zeek.bif index 71c49f1c9e..604fbd2799 100644 --- a/src/zeek.bif +++ b/src/zeek.bif @@ -1895,11 +1895,11 @@ function reading_traces%(%): bool function global_sizes%(%): var_sizes %{ TableVal* sizes = new TableVal(var_sizes); - PDict* globals = global_scope()->Vars(); - IterCookie* c = globals->InitForIteration(); + auto globals = global_scope()->Vars(); - ID* id; - while ( (id = globals->NextEntry(c)) ) + for ( const auto& global : globals ) + { + ID* id = global.second; if ( id->HasVal() ) { Val* id_name = new StringVal(id->Name()); @@ -1907,6 +1907,7 @@ function global_sizes%(%): var_sizes sizes->Assign(id_name, id_size); Unref(id_name); } + } return sizes; %} @@ -1922,12 +1923,11 @@ function global_sizes%(%): var_sizes function global_ids%(%): id_table %{ TableVal* ids = new TableVal(id_table); - PDict* globals = global_scope()->Vars(); - IterCookie* c = globals->InitForIteration(); + auto globals = global_scope()->Vars(); - ID* id; - while ( (id = globals->NextEntry(c)) ) + for ( const auto& global : globals ) { + ID* id = global.second; RecordVal* rec = new RecordVal(script_id); rec->Assign(0, new StringVal(type_name(id->Type()->Tag()))); rec->Assign(1, val_mgr->GetBool(id->IsExport()));