diff --git a/src/Expr.cc b/src/Expr.cc index 7b33a141d8..4912c1c1c0 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -272,7 +272,7 @@ IntrusivePtr NameExpr::MakeLvalue() void NameExpr::Assign(Frame* f, IntrusivePtr v) { if ( id->IsGlobal() ) - id->SetVal(v.release()); + id->SetVal(std::move(v)); else f->SetElement(id.get(), v.release()); } @@ -4241,10 +4241,10 @@ LambdaExpr::LambdaExpr(std::unique_ptr arg_ing, // Update lamb's name dummy_func->SetName(my_name.c_str()); - Val* v = new Val(dummy_func); + auto v = make_intrusive(dummy_func); Unref(dummy_func); - id->SetVal(v); // id will unref v when its done. - id->SetType(ingredients->id->Type()->Ref()); + id->SetVal(std::move(v)); + id->SetType({NewRef{}, ingredients->id->Type()}); id->SetConst(); } diff --git a/src/Func.cc b/src/Func.cc index 5583032f79..58652c8217 100644 --- a/src/Func.cc +++ b/src/Func.cc @@ -613,7 +613,7 @@ BuiltinFunc::BuiltinFunc(built_in_func arg_func, const char* arg_name, reporter->InternalError("built-in function %s multiply defined", Name()); type = id->Type()->Ref(); - id->SetVal(new Val(this)); + id->SetVal(make_intrusive(this)); } BuiltinFunc::~BuiltinFunc() diff --git a/src/ID.cc b/src/ID.cc index 2eea08acfa..2355962fb6 100644 --- a/src/ID.cc +++ b/src/ID.cc @@ -26,9 +26,7 @@ ID::ID(const char* arg_name, IDScope arg_scope, bool arg_is_export) scope = arg_scope; is_export = arg_is_export; is_option = false; - type = 0; val = 0; - attrs = 0; is_const = false; is_enum_const = false; is_type = false; @@ -43,11 +41,6 @@ ID::ID(const char* arg_name, IDScope arg_scope, bool arg_is_export) ID::~ID() { delete [] name; - Unref(type); - Unref(attrs); - - for ( auto element : option_handlers ) - Unref(element.second); if ( ! weak_ref ) Unref(val); @@ -58,9 +51,9 @@ string ID::ModuleName() const return extract_module_name(name); } -void ID::SetType(BroType* t) +void ID::SetType(IntrusivePtr t) { - Unref(type); type = t; + type = std::move(t); } void ID::ClearVal() @@ -71,12 +64,12 @@ void ID::ClearVal() val = 0; } -void ID::SetVal(Val* v, bool arg_weak_ref) +void ID::SetVal(IntrusivePtr v, bool arg_weak_ref) { if ( ! weak_ref ) Unref(val); - val = v; + val = v.release(); weak_ref = arg_weak_ref; Modified(); @@ -104,11 +97,11 @@ void ID::SetVal(Val* v, bool arg_weak_ref) } } -void ID::SetVal(Val* v, init_class c) +void ID::SetVal(IntrusivePtr v, init_class c) { if ( c == INIT_NONE || c == INIT_FULL ) { - SetVal(v); + SetVal(std::move(v)); return; } @@ -117,9 +110,9 @@ void ID::SetVal(Val* v, init_class c) (type->Tag() != TYPE_VECTOR || c == INIT_REMOVE) ) { if ( c == INIT_EXTRA ) - Error("+= initializer only applies to tables, sets, vectors and patterns", v); + Error("+= initializer only applies to tables, sets, vectors and patterns", v.get()); else - Error("-= initializer only applies to tables and sets", v); + Error("-= initializer only applies to tables and sets", v.get()); } else @@ -128,7 +121,7 @@ void ID::SetVal(Val* v, init_class c) { if ( ! val ) { - SetVal(v); + SetVal(std::move(v)); return; } else @@ -140,11 +133,9 @@ void ID::SetVal(Val* v, init_class c) v->RemoveFrom(val); } } - - Unref(v); } -void ID::SetVal(Expr* ev, init_class c) +void ID::SetVal(IntrusivePtr ev, init_class c) { Attr* a = attrs->FindAttr(c == INIT_EXTRA ? ATTR_ADD_FUNC : ATTR_DEL_FUNC); @@ -152,7 +143,7 @@ void ID::SetVal(Expr* ev, init_class c) if ( ! a ) Internal("no add/delete function in ID::SetVal"); - EvalFunc(a->AttrExpr(), ev); + EvalFunc({NewRef{}, a->AttrExpr()}, std::move(ev)); } bool ID::IsRedefinable() const @@ -160,11 +151,10 @@ bool ID::IsRedefinable() const return FindAttr(ATTR_REDEF) != 0; } -void ID::SetAttrs(Attributes* a) +void ID::SetAttrs(IntrusivePtr a) { - Unref(attrs); attrs = 0; - AddAttrs(a); + AddAttrs(std::move(a)); } void ID::UpdateValAttrs() @@ -173,10 +163,10 @@ void ID::UpdateValAttrs() return; if ( val && val->Type()->Tag() == TYPE_TABLE ) - val->AsTableVal()->SetAttrs(attrs); + val->AsTableVal()->SetAttrs(attrs.get()); if ( val && val->Type()->Tag() == TYPE_FILE ) - val->AsFile()->SetAttrs(attrs); + val->AsFile()->SetAttrs(attrs.get()); if ( Type()->Tag() == TYPE_FUNC ) { @@ -222,7 +212,7 @@ void ID::MakeDeprecated(Expr* deprecation) return; attr_list* attr = new attr_list{new Attr(ATTR_DEPRECATED, deprecation)}; - AddAttrs(new Attributes(attr, Type(), false, IsGlobal())); + AddAttrs(make_intrusive(attr, Type(), false, IsGlobal())); } string ID::GetDeprecationWarning() const @@ -245,12 +235,12 @@ string ID::GetDeprecationWarning() const return fmt("deprecated (%s): %s", Name(), result.c_str()); } -void ID::AddAttrs(Attributes* a) +void ID::AddAttrs(IntrusivePtr a) { if ( attrs ) - attrs->AddAttrs(a); + attrs->AddAttrs(a.release()); else - attrs = a; + attrs = std::move(a); UpdateValAttrs(); } @@ -272,20 +262,20 @@ void ID::SetOption() if ( ! IsRedefinable() ) { attr_list* attr = new attr_list{new Attr(ATTR_REDEF)}; - AddAttrs(new Attributes(attr, Type(), false, IsGlobal())); + AddAttrs(make_intrusive(attr, Type(), false, IsGlobal())); } } -void ID::EvalFunc(Expr* ef, Expr* ev) +void ID::EvalFunc(IntrusivePtr ef, IntrusivePtr ev) { auto arg1 = make_intrusive(IntrusivePtr{NewRef{}, val}); auto args = make_intrusive(); args->Append(std::move(arg1)); - args->Append({NewRef{}, ev}); + args->Append(std::move(ev)); - auto ce = make_intrusive(IntrusivePtr{NewRef{}, ef}, std::move(args)); + auto ce = make_intrusive(std::move(ef), std::move(args)); - SetVal(ce->Eval(0).release()); + SetVal(ce->Eval(0)); } #if 0 @@ -358,7 +348,7 @@ TraversalCode ID::Traverse(TraversalCallback* cb) const void ID::Error(const char* msg, const BroObj* o2) { BroObj::Error(msg, o2, 1); - SetType(error_type()); + SetType({AdoptRef{}, error_type()}); } void ID::Describe(ODesc* d) const @@ -581,9 +571,9 @@ void ID::UpdateValID() } #endif -void ID::AddOptionHandler(Func* callback, int priority) +void ID::AddOptionHandler(IntrusivePtr callback, int priority) { - option_handlers.insert({priority, callback}); + option_handlers.emplace(priority, std::move(callback)); } vector ID::GetOptionHandlers() const @@ -593,6 +583,6 @@ vector ID::GetOptionHandlers() const // a lot... vector v; for ( auto& element : option_handlers ) - v.push_back(element.second); + v.push_back(element.second.get()); return v; } diff --git a/src/ID.h b/src/ID.h index 0eaf1d42b8..7394bcd450 100644 --- a/src/ID.h +++ b/src/ID.h @@ -2,6 +2,7 @@ #pragma once +#include "IntrusivePtr.h" #include "Obj.h" #include "Attr.h" #include "Notifier.h" @@ -35,9 +36,9 @@ public: std::string ModuleName() const; - void SetType(BroType* t); - BroType* Type() { return type; } - const BroType* Type() const { return type; } + void SetType(IntrusivePtr t); + BroType* Type() { return type.get(); } + const BroType* Type() const { return type.get(); } void MakeType() { is_type = true; } BroType* AsType() { return is_type ? Type() : 0; } @@ -51,10 +52,10 @@ public: // reference to the Val, the Val will be destroyed (naturally, // you have to take care that it will not be accessed via // the ID afterwards). - void SetVal(Val* v, bool weak_ref = false); + void SetVal(IntrusivePtr v, bool weak_ref = false); - void SetVal(Val* v, init_class c); - void SetVal(Expr* ev, init_class c); + void SetVal(IntrusivePtr v, init_class c); + void SetVal(IntrusivePtr ev, init_class c); bool HasVal() const { return val != 0; } Val* ID_Val() { return val; } @@ -75,11 +76,11 @@ public: bool IsRedefinable() const; - void SetAttrs(Attributes* attr); - void AddAttrs(Attributes* attr); + void SetAttrs(IntrusivePtr attr); + void AddAttrs(IntrusivePtr attr); void RemoveAttr(attr_tag a); void UpdateValAttrs(); - Attributes* Attrs() const { return attrs; } + Attributes* Attrs() const { return attrs.get(); } Attr* FindAttr(attr_tag t) const; @@ -109,11 +110,11 @@ public: { return !option_handlers.empty(); } // Takes ownership of callback. - void AddOptionHandler(Func* callback, int priority); + void AddOptionHandler(IntrusivePtr callback, int priority); std::vector GetOptionHandlers() const; protected: - void EvalFunc(Expr* ef, Expr* ev); + void EvalFunc(IntrusivePtr ef, IntrusivePtr ev); #ifdef DEBUG void UpdateValID(); @@ -122,13 +123,13 @@ protected: const char* name; IDScope scope; bool is_export; - BroType* type; + IntrusivePtr type; bool is_const, is_enum_const, is_type, is_option; int offset; Val* val; - Attributes* attrs; + IntrusivePtr attrs; // contains list of functions that are called when an option changes - std::multimap option_handlers; + std::multimap> option_handlers; bool infer_return_type; bool weak_ref; diff --git a/src/Net.cc b/src/Net.cc index 744bd8a811..6aa3177639 100644 --- a/src/Net.cc +++ b/src/Net.cc @@ -194,7 +194,7 @@ void net_init(const std::optional& interface, writefile, pkt_dumper->ErrorMsg()); if ( ID* id = global_scope()->Lookup("trace_output_file") ) - id->SetVal(new StringVal(writefile)); + id->SetVal(make_intrusive(writefile)); else reporter->Error("trace_output_file not defined in bro.init"); } diff --git a/src/Type.cc b/src/Type.cc index 8ab11ce312..cb9bd60bb0 100644 --- a/src/Type.cc +++ b/src/Type.cc @@ -1177,7 +1177,7 @@ void EnumType::CheckAndAddName(const string& module_name, const char* name, if ( ! id ) { id = install_ID(name, module_name.c_str(), true, is_export); - id->SetType(this->Ref()); + id->SetType({NewRef{}, this}); id->SetEnumConst(); if ( deprecation ) diff --git a/src/Var.cc b/src/Var.cc index 0399af05cb..80af567595 100644 --- a/src/Var.cc +++ b/src/Var.cc @@ -104,15 +104,15 @@ static void make_var(ID* id, IntrusivePtr t, init_class c, IntrusivePtr t = {AdoptRef{}, init_type(init.get())}; if ( ! t ) { - id->SetType(error_type()); + id->SetType({AdoptRef{}, error_type()}); return; } } - id->SetType(t->Ref()); + id->SetType(t); if ( attr ) - id->AddAttrs(new Attributes(attr, t.get(), false, id->IsGlobal())); + id->AddAttrs(make_intrusive(attr, t.get(), false, id->IsGlobal())); if ( init ) { @@ -121,10 +121,7 @@ static void make_var(ID* id, IntrusivePtr t, init_class c, IntrusivePtr { TableConstructorExpr* ctor = (TableConstructorExpr*) init.get(); if ( ctor->Attrs() ) - { - ::Ref(ctor->Attrs()); - id->AddAttrs(ctor->Attrs()); - } + id->AddAttrs({NewRef{}, ctor->Attrs()}); } break; @@ -132,10 +129,7 @@ static void make_var(ID* id, IntrusivePtr t, init_class c, IntrusivePtr { SetConstructorExpr* ctor = (SetConstructorExpr*) init.get(); if ( ctor->Attrs() ) - { - ::Ref(ctor->Attrs()); - id->AddAttrs(ctor->Attrs()); - } + id->AddAttrs({NewRef{}, ctor->Attrs()}); } break; @@ -155,7 +149,7 @@ static void make_var(ID* id, IntrusivePtr t, init_class c, IntrusivePtr if ( init && ((c == INIT_EXTRA && id->FindAttr(ATTR_ADD_FUNC)) || (c == INIT_REMOVE && id->FindAttr(ATTR_DEL_FUNC)) )) // Just apply the function. - id->SetVal(init->Ref(), c); + id->SetVal(init, c); else if ( dt != VAR_REDEF || init || ! attr ) { @@ -184,9 +178,9 @@ static void make_var(ID* id, IntrusivePtr t, init_class c, IntrusivePtr } if ( aggr ) - id->SetVal(aggr.release(), c); + id->SetVal(std::move(aggr), c); else if ( v ) - id->SetVal(v.release(), c); + id->SetVal(std::move(v), c); } } @@ -216,7 +210,7 @@ static void make_var(ID* id, IntrusivePtr t, init_class c, IntrusivePtr // we can later access the ID even if no implementations have been // defined. Func* f = new BroFunc(id, 0, 0, 0, 0); - id->SetVal(new Val(f)); + id->SetVal(make_intrusive(f)); } } @@ -284,11 +278,11 @@ void add_type(ID* id, IntrusivePtr t, attr_list* attr) tnew->SetName(id->Name()); - id->SetType(IntrusivePtr{tnew}.release()); + id->SetType(tnew); id->MakeType(); if ( attr ) - id->SetAttrs(new Attributes(attr, tnew.get(), false, false)); + id->SetAttrs(make_intrusive(attr, tnew.get(), false, false)); } static void transfer_arg_defaults(RecordType* args, RecordType* recv) @@ -386,7 +380,7 @@ void begin_func(ID* id, const char* module_name, function_flavor flavor, } } else - id->SetType(IntrusivePtr{t}.release()); + id->SetType(t); push_scope(id, attrs); @@ -402,7 +396,7 @@ void begin_func(ID* id, const char* module_name, function_flavor flavor, arg_id->Error("argument name used twice"); arg_id = install_ID(arg_i->id, module_name, false, false); - arg_id->SetType(arg_i->type->Ref()); + arg_id->SetType({NewRef{}, arg_i->type}); } if ( Attr* depr_attr = find_attr(attrs, ATTR_DEPRECATED) ) @@ -494,7 +488,7 @@ void end_func(IntrusivePtr body) ingredients->frame_size, ingredients->priority); - ingredients->id->SetVal(new Val(f)); + ingredients->id->SetVal(make_intrusive(f)); ingredients->id->SetConst(); } diff --git a/src/broker/Manager.cc b/src/broker/Manager.cc index e71612597d..0a24a5ebc4 100644 --- a/src/broker/Manager.cc +++ b/src/broker/Manager.cc @@ -1210,7 +1210,7 @@ bool Manager::ProcessIdentifierUpdate(broker::zeek::IdentifierUpdate iu) return false; } - id->SetVal(val.release()); + id->SetVal(std::move(val)); return true; } diff --git a/src/main.cc b/src/main.cc index b47d2fcf59..30334c5b3e 100644 --- a/src/main.cc +++ b/src/main.cc @@ -717,7 +717,7 @@ int main(int argc, char** argv) if ( ! id ) reporter->InternalError("global cmd_line_bpf_filter not defined"); - id->SetVal(new StringVal(*options.pcap_filter)); + id->SetVal(make_intrusive(*options.pcap_filter)); } auto all_signature_files = options.signature_files; diff --git a/src/option.bif b/src/option.bif index 8c56fe4c38..5355216ee7 100644 --- a/src/option.bif +++ b/src/option.bif @@ -33,7 +33,7 @@ static bool call_option_handlers_and_set_value(StringVal* name, ID* i, Val* val, } // clone to prevent changes - i->SetVal(val->Clone()); + i->SetVal({AdoptRef{}, val->Clone()}); Unref(val); // Either ref'd once or function call result. return true; } @@ -206,7 +206,6 @@ function Option::set_change_handler%(ID: string, on_change: any, priority: int & } auto* func = on_change->AsFunc(); - Ref(func); - i->AddOptionHandler(func, -priority); + i->AddOptionHandler({NewRef{}, func}, -priority); return val_mgr->GetBool(1); %} diff --git a/src/parse.y b/src/parse.y index 3f9c51e973..c11242e7ec 100644 --- a/src/parse.y +++ b/src/parse.y @@ -693,7 +693,7 @@ expr: if ( ! id->Type() ) { id->Error("undeclared variable"); - id->SetType(error_type()); + id->SetType({AdoptRef{}, error_type()}); $$ = new NameExpr(std::move(id)); } @@ -1623,7 +1623,7 @@ case_type: TOK_TYPE type { $$ = new ID(0, SCOPE_FUNCTION, 0); - $$->SetType($2); + $$->SetType({AdoptRef{}, $2}); } | TOK_TYPE type TOK_AS TOK_ID diff --git a/src/supervisor/Supervisor.cc b/src/supervisor/Supervisor.cc index 097724665b..3d91725b26 100644 --- a/src/supervisor/Supervisor.cc +++ b/src/supervisor/Supervisor.cc @@ -1233,7 +1233,7 @@ bool Supervisor::SupervisedNode::InitCluster() const cluster_nodes->Assign(key.get(), val.release()); } - cluster_manager_is_logger_id->SetVal(val_mgr->GetBool(! has_logger)); + cluster_manager_is_logger_id->SetVal({AdoptRef{}, val_mgr->GetBool(! has_logger)}); return true; }