ID: use class IntrusivePtr

This commit is contained in:
Max Kellermann 2020-02-26 19:21:22 +01:00
parent c3ea246237
commit edde591748
12 changed files with 71 additions and 87 deletions

View file

@ -272,7 +272,7 @@ IntrusivePtr<Expr> NameExpr::MakeLvalue()
void NameExpr::Assign(Frame* f, IntrusivePtr<Val> 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<function_ingredients> arg_ing,
// Update lamb's name
dummy_func->SetName(my_name.c_str());
Val* v = new Val(dummy_func);
auto v = make_intrusive<Val>(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();
}

View file

@ -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<Val>(this));
}
BuiltinFunc::~BuiltinFunc()

View file

@ -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<BroType> 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<Val> 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<Val> 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<Expr> 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<Attributes> 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<Attributes>(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<Attributes> 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<Attributes>(attr, Type(), false, IsGlobal()));
}
}
void ID::EvalFunc(Expr* ef, Expr* ev)
void ID::EvalFunc(IntrusivePtr<Expr> ef, IntrusivePtr<Expr> ev)
{
auto arg1 = make_intrusive<ConstExpr>(IntrusivePtr{NewRef{}, val});
auto args = make_intrusive<ListExpr>();
args->Append(std::move(arg1));
args->Append({NewRef{}, ev});
args->Append(std::move(ev));
auto ce = make_intrusive<CallExpr>(IntrusivePtr{NewRef{}, ef}, std::move(args));
auto ce = make_intrusive<CallExpr>(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<Func> callback, int priority)
{
option_handlers.insert({priority, callback});
option_handlers.emplace(priority, std::move(callback));
}
vector<Func*> ID::GetOptionHandlers() const
@ -593,6 +583,6 @@ vector<Func*> ID::GetOptionHandlers() const
// a lot...
vector<Func*> v;
for ( auto& element : option_handlers )
v.push_back(element.second);
v.push_back(element.second.get());
return v;
}

View file

@ -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<BroType> 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<Val> v, bool weak_ref = false);
void SetVal(Val* v, init_class c);
void SetVal(Expr* ev, init_class c);
void SetVal(IntrusivePtr<Val> v, init_class c);
void SetVal(IntrusivePtr<Expr> 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<Attributes> attr);
void AddAttrs(IntrusivePtr<Attributes> 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<Func> callback, int priority);
std::vector<Func*> GetOptionHandlers() const;
protected:
void EvalFunc(Expr* ef, Expr* ev);
void EvalFunc(IntrusivePtr<Expr> ef, IntrusivePtr<Expr> ev);
#ifdef DEBUG
void UpdateValID();
@ -122,13 +123,13 @@ protected:
const char* name;
IDScope scope;
bool is_export;
BroType* type;
IntrusivePtr<BroType> type;
bool is_const, is_enum_const, is_type, is_option;
int offset;
Val* val;
Attributes* attrs;
IntrusivePtr<Attributes> attrs;
// contains list of functions that are called when an option changes
std::multimap<int, Func*> option_handlers;
std::multimap<int, IntrusivePtr<Func>> option_handlers;
bool infer_return_type;
bool weak_ref;

View file

@ -194,7 +194,7 @@ void net_init(const std::optional<std::string>& interface,
writefile, pkt_dumper->ErrorMsg());
if ( ID* id = global_scope()->Lookup("trace_output_file") )
id->SetVal(new StringVal(writefile));
id->SetVal(make_intrusive<StringVal>(writefile));
else
reporter->Error("trace_output_file not defined in bro.init");
}

View file

@ -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 )

View file

@ -104,15 +104,15 @@ static void make_var(ID* id, IntrusivePtr<BroType> 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<Attributes>(attr, t.get(), false, id->IsGlobal()));
if ( init )
{
@ -121,10 +121,7 @@ static void make_var(ID* id, IntrusivePtr<BroType> 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<BroType> 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<BroType> 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<BroType> 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<BroType> 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<Val>(f));
}
}
@ -284,11 +278,11 @@ void add_type(ID* id, IntrusivePtr<BroType> 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<Attributes>(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<Stmt> body)
ingredients->frame_size,
ingredients->priority);
ingredients->id->SetVal(new Val(f));
ingredients->id->SetVal(make_intrusive<Val>(f));
ingredients->id->SetConst();
}

View file

@ -1210,7 +1210,7 @@ bool Manager::ProcessIdentifierUpdate(broker::zeek::IdentifierUpdate iu)
return false;
}
id->SetVal(val.release());
id->SetVal(std::move(val));
return true;
}

View file

@ -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<StringVal>(*options.pcap_filter));
}
auto all_signature_files = options.signature_files;

View file

@ -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);
%}

View file

@ -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

View file

@ -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;
}