diff --git a/src/CompHash.cc b/src/CompHash.cc index 7406410b7d..935d0ade93 100644 --- a/src/CompHash.cc +++ b/src/CompHash.cc @@ -207,7 +207,7 @@ char* CompositeHash::SingleValHash(int type_check, char* kp0, auto tbl = tv->AsTable(); auto it = tbl->InitForIteration(); - ListVal* lv = new ListVal(TYPE_ANY); + auto lv = make_intrusive(TYPE_ANY); struct HashKeyComparer { bool operator()(const HashKey* a, const HashKey* b) const @@ -229,7 +229,7 @@ char* CompositeHash::SingleValHash(int type_check, char* kp0, while ( tbl->NextEntry(k, it) ) { hashkeys[k] = idx++; - lv->Append(tv->RecoverIndex(k)); + lv->Append(tv->RecoverIndex(k).release()); } for ( auto& kv : hashkeys ) @@ -242,10 +242,7 @@ char* CompositeHash::SingleValHash(int type_check, char* kp0, if ( ! (kp1 = SingleValHash(type_check, kp1, key->Type(), key, false)) ) - { - Unref(lv); return 0; - } if ( ! v->Type()->IsSet() ) { @@ -253,14 +250,10 @@ char* CompositeHash::SingleValHash(int type_check, char* kp0, if ( ! (kp1 = SingleValHash(type_check, kp1, val->Type(), val.get(), false)) ) - { - Unref(lv); return 0; - } } } - Unref(lv); } break; diff --git a/src/Reporter.cc b/src/Reporter.cc index 978a0a8a39..ddd141ebd4 100644 --- a/src/Reporter.cc +++ b/src/Reporter.cc @@ -80,7 +80,6 @@ void Reporter::InitOptions() auto index = wl_val->RecoverIndex(k); string key = index->Index(0)->AsString()->CheckString(); weird_sampling_whitelist.emplace(move(key)); - Unref(index); delete k; } } diff --git a/src/Stmt.cc b/src/Stmt.cc index 7f884fe3e4..172f54014b 100644 --- a/src/Stmt.cc +++ b/src/Stmt.cc @@ -1198,7 +1198,7 @@ IntrusivePtr ForStmt::DoExec(Frame* f, Val* v, stmt_flow_type& flow) const IterCookie* c = loop_vals->InitForIteration(); while ( (current_tev = loop_vals->NextEntry(k, c)) ) { - IntrusivePtr ind_lv{AdoptRef{}, tv->RecoverIndex(k)}; + auto ind_lv = tv->RecoverIndex(k); delete k; if ( value_var ) diff --git a/src/Val.cc b/src/Val.cc index f6c0e2ec18..b72d9d7e95 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -532,12 +532,7 @@ static void BuildJSON(threading::formatter::JSON::NullDoubleWriter& writer, Val* { auto lv = tval->RecoverIndex(k); delete k; - - Val* entry_key; - if ( lv->Length() == 1 ) - entry_key = lv->Index(0)->Ref(); - else - entry_key = lv->Ref(); + Val* entry_key = lv->Length() == 1 ? lv->Index(0) : lv.get(); if ( tval->Type()->IsSet() ) BuildJSON(writer, entry_key, only_loggable, re); @@ -556,9 +551,6 @@ static void BuildJSON(threading::formatter::JSON::NullDoubleWriter& writer, Val* BuildJSON(writer, entry->Value(), only_loggable, re, key_str); } - - Unref(entry_key); - Unref(lv); } if ( tval->Type()->IsSet() ) @@ -1537,9 +1529,8 @@ int TableVal::Assign(Val* index, HashKey* k, IntrusivePtr new_val) { if ( ! index ) { - Val* v = RecoverIndex(&k_copy); - subnets->Insert(v, new_entry_val); - Unref(v); + auto v = RecoverIndex(&k_copy); + subnets->Insert(v.get(), new_entry_val); } else subnets->Insert(index, new_entry_val); @@ -1553,10 +1544,10 @@ int TableVal::Assign(Val* index, HashKey* k, IntrusivePtr new_val) if ( change_func ) { - Val* change_index = index ? index->Ref() : RecoverIndex(&k_copy); + auto change_index = index ? IntrusivePtr{NewRef{}, index} + : RecoverIndex(&k_copy); Val* v = old_entry_val ? old_entry_val->Value() : new_val.get(); - CallChangeFunc(change_index, v, old_entry_val ? ELEMENT_CHANGED : ELEMENT_NEW); - Unref(change_index); + CallChangeFunc(change_index.get(), v, old_entry_val ? ELEMENT_CHANGED : ELEMENT_NEW); } delete old_entry_val; @@ -1991,9 +1982,9 @@ bool TableVal::UpdateTimestamp(Val* index) return true; } -ListVal* TableVal::RecoverIndex(const HashKey* k) const +IntrusivePtr TableVal::RecoverIndex(const HashKey* k) const { - return table_hash->RecoverVals(k).release(); + return table_hash->RecoverVals(k); } void TableVal::CallChangeFunc(const Val* index, Val* old_value, OnChangeType tpe) @@ -2344,11 +2335,12 @@ void TableVal::DoExpire(double t) else if ( v->ExpireAccessTime() + timeout < t ) { - Val* idx = nullptr; + IntrusivePtr idx = nullptr; + if ( expire_func ) { idx = RecoverIndex(k); - double secs = CallExpireFunc({NewRef{}, idx}); + double secs = CallExpireFunc(idx); // It's possible that the user-provided // function modified or deleted the table @@ -2359,7 +2351,6 @@ void TableVal::DoExpire(double t) if ( ! v ) { // user-provided function deleted it v = v_saved; - Unref(idx); delete k; continue; } @@ -2369,7 +2360,6 @@ void TableVal::DoExpire(double t) // User doesn't want us to expire // this now. v->SetExpireAccess(network_time - timeout + secs); - Unref(idx); delete k; continue; } @@ -2380,7 +2370,7 @@ void TableVal::DoExpire(double t) { if ( ! idx ) idx = RecoverIndex(k); - if ( ! subnets->Remove(idx) ) + if ( ! subnets->Remove(idx.get()) ) reporter->InternalWarning("index not in prefix table"); } @@ -2389,9 +2379,9 @@ void TableVal::DoExpire(double t) { if ( ! idx ) idx = RecoverIndex(k); - CallChangeFunc(idx, v->Value(), ELEMENT_EXPIRED); + CallChangeFunc(idx.get(), v->Value(), ELEMENT_EXPIRED); } - Unref(idx); + delete v; modified = true; } @@ -2439,7 +2429,7 @@ double TableVal::GetExpireTime() return -1; } -double TableVal::CallExpireFunc(IntrusivePtr idx) +double TableVal::CallExpireFunc(IntrusivePtr idx) { if ( ! expire_func ) return 0; @@ -2468,25 +2458,20 @@ double TableVal::CallExpireFunc(IntrusivePtr idx) // backwards compatibility with idx: any idiom bool any_idiom = func_args->length() == 2 && func_args->back()->Tag() == TYPE_ANY; - if ( idx->Type()->Tag() == TYPE_LIST ) + if ( ! any_idiom ) { - if ( ! any_idiom ) - { - for ( const auto& v : *idx->AsListVal()->Vals() ) - vl.append(v->Ref()); - } - else - { - ListVal* idx_list = idx->AsListVal(); - // Flatten if only one element - if ( idx_list->Length() == 1 ) - idx = {NewRef{}, idx_list->Index(0)}; - - vl.append(idx.release()); - } + for ( const auto& v : *idx->AsListVal()->Vals() ) + vl.append(v->Ref()); } else - vl.append(idx.release()); + { + ListVal* idx_list = idx->AsListVal(); + // Flatten if only one element + if ( idx_list->Length() == 1 ) + vl.append(idx_list->Index(0)->Ref()); + else + vl.append(idx.release()); + } auto result = f->Call(&vl); @@ -2518,9 +2503,8 @@ IntrusivePtr TableVal::DoClone(CloneState* state) if ( subnets ) { - Val* idx = RecoverIndex(key); - tv->subnets->Insert(idx, nval); - Unref(idx); + auto idx = RecoverIndex(key); + tv->subnets->Insert(idx.get(), nval); } delete key; @@ -2609,7 +2593,7 @@ TableVal::ParseTimeTableState TableVal::DumpTableState() while ( (val = tbl->NextEntry(key, cookie)) ) { - rval.emplace_back(IntrusivePtr{AdoptRef{}, RecoverIndex(key)}, + rval.emplace_back(RecoverIndex(key), IntrusivePtr{NewRef{}, val->Value()}); delete key; diff --git a/src/Val.h b/src/Val.h index 7b8aad5e26..7d944e9760 100644 --- a/src/Val.h +++ b/src/Val.h @@ -766,7 +766,7 @@ public: bool UpdateTimestamp(Val* index); // Returns the index corresponding to the given HashKey. - ListVal* RecoverIndex(const HashKey* k) const; + IntrusivePtr RecoverIndex(const HashKey* k) const; // Returns the element if it was in the table, false otherwise. IntrusivePtr Delete(const Val* index); @@ -851,7 +851,7 @@ protected: double GetExpireTime(); // Calls &expire_func and returns its return interval; - double CallExpireFunc(IntrusivePtr idx); + double CallExpireFunc(IntrusivePtr idx); // Enum for the different kinds of changes an &on_change handler can see enum OnChangeType { ELEMENT_NEW, ELEMENT_CHANGED, ELEMENT_REMOVED, ELEMENT_EXPIRED }; diff --git a/src/broker/Data.cc b/src/broker/Data.cc index f8de5c4415..7459c8f3ac 100644 --- a/src/broker/Data.cc +++ b/src/broker/Data.cc @@ -898,29 +898,14 @@ broker::expected bro_broker::val_to_data(const Val* v) else rval = broker::table(); - struct iter_guard { - iter_guard(HashKey* arg_k, ListVal* arg_lv) - : k(arg_k), lv(arg_lv) - {} - - ~iter_guard() - { - delete k; - Unref(lv); - } - - HashKey* k; - ListVal* lv; - }; - - HashKey* k; + HashKey* hk; TableEntryVal* entry; auto c = table->InitForIteration(); - while ( (entry = table->NextEntry(k, c)) ) + while ( (entry = table->NextEntry(hk, c)) ) { - auto vl = table_val->RecoverIndex(k); - iter_guard ig(k, vl); + auto vl = table_val->RecoverIndex(hk); + delete hk; broker::vector composite_key; composite_key.reserve(vl->Length()); diff --git a/src/broker/messaging.bif b/src/broker/messaging.bif index 1e1ef1eabd..95fe80c0a1 100644 --- a/src/broker/messaging.bif +++ b/src/broker/messaging.bif @@ -40,7 +40,6 @@ std::set val_to_topic_set(Val* val) { auto index = val->AsTableVal()->RecoverIndex(k); rval.emplace(index->Index(0)->AsString()->CheckString()); - Unref(index); delete k; } } diff --git a/src/input/Manager.cc b/src/input/Manager.cc index 2f21725956..5408a51d85 100644 --- a/src/input/Manager.cc +++ b/src/input/Manager.cc @@ -283,11 +283,10 @@ bool Manager::CreateStream(Stream* info, RecordVal* description) TableEntryVal* v; while ( (v = info->config->AsTable()->NextEntry(k, c)) ) { - ListVal* index = info->config->RecoverIndex(k); + auto index = info->config->RecoverIndex(k); string key = index->Index(0)->AsString()->CheckString(); string value = v->Value()->AsString()->CheckString(); rinfo.config.insert(std::make_pair(copy_string(key.c_str()), copy_string(value.c_str()))); - Unref(index); delete k; } } @@ -1335,7 +1334,6 @@ void Manager::EndCurrentSend(ReaderFrontend* reader) while ( ( ih = stream->lastDict->NextEntry(lastDictIdxKey, c) ) ) { - ListVal * idx = 0; IntrusivePtr val; Val* predidx = 0; @@ -1344,12 +1342,11 @@ void Manager::EndCurrentSend(ReaderFrontend* reader) if ( stream->pred || stream->event ) { - idx = stream->tab->RecoverIndex(ih->idxkey); - assert(idx != 0); - val = stream->tab->Lookup(idx); + auto idx = stream->tab->RecoverIndex(ih->idxkey); + assert(idx != nullptr); + val = stream->tab->Lookup(idx.get()); assert(val != 0); - predidx = ListValToRecordVal(idx, stream->itype, &startpos); - Unref(idx); + predidx = ListValToRecordVal(idx.get(), stream->itype, &startpos); ev = BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_REMOVED).release(); } diff --git a/src/logging/Manager.cc b/src/logging/Manager.cc index cdcfda73f9..87eac0e066 100644 --- a/src/logging/Manager.cc +++ b/src/logging/Manager.cc @@ -868,11 +868,10 @@ bool Manager::Write(EnumVal* id, RecordVal* columns_arg) TableEntryVal* v; while ( (v = filter->config->AsTable()->NextEntry(k, c)) ) { - ListVal* index = filter->config->RecoverIndex(k); + auto index = filter->config->RecoverIndex(k); string key = index->Index(0)->AsString()->CheckString(); string value = v->Value()->AsString()->CheckString(); info->config.insert(std::make_pair(copy_string(key.c_str()), copy_string(value.c_str()))); - Unref(index); delete k; } diff --git a/src/reporter.bif b/src/reporter.bif index 00635947fb..426ba99f93 100644 --- a/src/reporter.bif +++ b/src/reporter.bif @@ -182,7 +182,6 @@ function Reporter::set_weird_sampling_whitelist%(weird_sampling_whitelist: strin auto index = wl_val->RecoverIndex(k); string key = index->Index(0)->AsString()->CheckString(); whitelist_set.emplace(move(key)); - Unref(index); delete k; } reporter->SetWeirdSamplingWhitelist(whitelist_set); diff --git a/src/supervisor/Supervisor.cc b/src/supervisor/Supervisor.cc index 3f511816c4..a8af880138 100644 --- a/src/supervisor/Supervisor.cc +++ b/src/supervisor/Supervisor.cc @@ -1024,7 +1024,7 @@ Supervisor::NodeConfig Supervisor::NodeConfig::FromRecord(const RecordVal* node) while ( (v = cluster_table->NextEntry(k, c)) ) { - IntrusivePtr key{AdoptRef{}, cluster_table_val->RecoverIndex(k)}; + auto key = cluster_table_val->RecoverIndex(k); delete k; auto name = key->Index(0)->AsStringVal()->ToStdString(); auto rv = v->Value()->AsRecordVal();