Deprecate Val(Func*) ctor, replace with one using IntrusivePtr

This commit is contained in:
Jon Siwek 2020-05-15 16:24:53 -07:00
parent 4a2221b878
commit a031f5b727
7 changed files with 26 additions and 22 deletions

View file

@ -845,7 +845,7 @@ const char* CompositeHash::RecoverOneVal(const HashKey* k, const char* kp0,
const uint32_t* const kp = AlignType<uint32_t>(kp0); const uint32_t* const kp = AlignType<uint32_t>(kp0);
kp1 = reinterpret_cast<const char*>(kp+1); kp1 = reinterpret_cast<const char*>(kp+1);
Func* f = Func::GetFuncPtrByID(*kp); const auto& f = Func::GetFuncPtrByID(*kp);
if ( ! f ) if ( ! f )
reporter->InternalError("failed to look up unique function id %" PRIu32 " in CompositeHash::RecoverOneVal()", *kp); reporter->InternalError("failed to look up unique function id %" PRIu32 " in CompositeHash::RecoverOneVal()", *kp);

View file

@ -4207,7 +4207,7 @@ LambdaExpr::LambdaExpr(std::unique_ptr<function_ingredients> arg_ing,
// Install a dummy version of the function globally for use only // Install a dummy version of the function globally for use only
// when broker provides a closure. // when broker provides a closure.
BroFunc* dummy_func = new BroFunc( auto dummy_func = make_intrusive<BroFunc>(
ingredients->id.get(), ingredients->id.get(),
ingredients->body, ingredients->body,
shallow_copy_func_inits(ingredients->body, ingredients->inits).release(), shallow_copy_func_inits(ingredients->body, ingredients->inits).release(),
@ -4245,8 +4245,7 @@ LambdaExpr::LambdaExpr(std::unique_ptr<function_ingredients> arg_ing,
// Update lamb's name // Update lamb's name
dummy_func->SetName(my_name.c_str()); dummy_func->SetName(my_name.c_str());
auto v = make_intrusive<Val>(dummy_func); auto v = make_intrusive<Val>(std::move(dummy_func));
Unref(dummy_func);
id->SetVal(std::move(v)); id->SetVal(std::move(v));
id->SetType(ingredients->id->GetType()); id->SetType(ingredients->id->GetType());
id->SetConst(); id->SetConst();
@ -4272,7 +4271,7 @@ IntrusivePtr<Val> LambdaExpr::Eval(Frame* f) const
// Allows for lookups by the receiver. // Allows for lookups by the receiver.
lamb->SetName(my_name.c_str()); lamb->SetName(my_name.c_str());
return make_intrusive<Val>(lamb.get()); return make_intrusive<Val>(std::move(lamb));
} }
void LambdaExpr::ExprDescribe(ODesc* d) const void LambdaExpr::ExprDescribe(ODesc* d) const

View file

@ -59,7 +59,6 @@ extern RETSIGTYPE sig_handler(int signo);
std::vector<CallInfo> call_stack; std::vector<CallInfo> call_stack;
bool did_builtin_init = false; bool did_builtin_init = false;
std::vector<Func*> Func::unique_ids;
static const std::pair<bool, Val*> empty_hook_result(false, NULL); static const std::pair<bool, Val*> empty_hook_result(false, NULL);
std::string render_call_stack() std::string render_call_stack()
@ -111,13 +110,13 @@ std::string render_call_stack()
Func::Func() Func::Func()
{ {
unique_id = unique_ids.size(); unique_id = unique_ids.size();
unique_ids.push_back(this); unique_ids.push_back({NewRef{}, this});
} }
Func::Func(Kind arg_kind) : kind(arg_kind) Func::Func(Kind arg_kind) : kind(arg_kind)
{ {
unique_id = unique_ids.size(); unique_id = unique_ids.size();
unique_ids.push_back(this); unique_ids.push_back({NewRef{}, this});
} }
Func::~Func() = default; Func::~Func() = default;
@ -594,7 +593,7 @@ BuiltinFunc::BuiltinFunc(built_in_func arg_func, const char* arg_name,
reporter->InternalError("built-in function %s multiply defined", Name()); reporter->InternalError("built-in function %s multiply defined", Name());
type = id->GetType(); type = id->GetType();
id->SetVal(make_intrusive<Val>(this)); id->SetVal(make_intrusive<Val>(IntrusivePtr{NewRef{}, this}));
} }
BuiltinFunc::~BuiltinFunc() BuiltinFunc::~BuiltinFunc()

View file

@ -93,8 +93,11 @@ public:
virtual TraversalCode Traverse(TraversalCallback* cb) const; virtual TraversalCode Traverse(TraversalCallback* cb) const;
uint32_t GetUniqueFuncID() const { return unique_id; } uint32_t GetUniqueFuncID() const { return unique_id; }
static Func* GetFuncPtrByID(uint32_t id) static const IntrusivePtr<Func>& GetFuncPtrByID(uint32_t id)
{ return id >= unique_ids.size() ? nullptr : unique_ids[id]; } {
static IntrusivePtr<Func> nil;
return id >= unique_ids.size() ? nil : unique_ids[id];
}
protected: protected:
Func(); Func();
@ -111,7 +114,7 @@ protected:
uint32_t unique_id; uint32_t unique_id;
IntrusivePtr<BroType> type; IntrusivePtr<BroType> type;
std::string name; std::string name;
static std::vector<Func*> unique_ids; static inline std::vector<IntrusivePtr<Func>> unique_ids;
}; };

View file

@ -42,11 +42,12 @@
using namespace std; using namespace std;
Val::Val(Func* f) Val::Val(Func* f) : Val({NewRef{}, f})
: val(f), type({NewRef{}, f->FType()}) {}
{
::Ref(val.func_val); Val::Val(IntrusivePtr<Func> f)
} : val(f.release()), type({NewRef{}, val.func_val->FType()})
{}
static const IntrusivePtr<FileType>& GetStringFileType() noexcept static const IntrusivePtr<FileType>& GetStringFileType() noexcept
{ {
@ -118,7 +119,7 @@ IntrusivePtr<Val> Val::DoClone(CloneState* state)
// Derived classes are responsible for this. Exception: // Derived classes are responsible for this. Exception:
// Functions and files. There aren't any derived classes. // Functions and files. There aren't any derived classes.
if ( type->Tag() == TYPE_FUNC ) if ( type->Tag() == TYPE_FUNC )
return make_intrusive<Val>(AsFunc()->DoClone().get()); return make_intrusive<Val>(AsFunc()->DoClone());
if ( type->Tag() == TYPE_FILE ) if ( type->Tag() == TYPE_FILE )
{ {

View file

@ -129,7 +129,9 @@ public:
: val(d), type(base_type(t)) : val(d), type(base_type(t))
{} {}
[[deprecated("Remove in v4.1. Construct from IntrusivePtr instead.")]]
explicit Val(Func* f); explicit Val(Func* f);
explicit Val(IntrusivePtr<Func> f);
// Note, will unref 'f' when it's done, closing it unless // Note, will unref 'f' when it's done, closing it unless
// class has ref'd it. // class has ref'd it.

View file

@ -298,8 +298,8 @@ static void make_var(ID* id, IntrusivePtr<BroType> t, init_class c,
// For events, add a function value (without any body) here so that // For events, add a function value (without any body) here so that
// we can later access the ID even if no implementations have been // we can later access the ID even if no implementations have been
// defined. // defined.
Func* f = new BroFunc(id, nullptr, nullptr, 0, 0); auto f = make_intrusive<BroFunc>(id, nullptr, nullptr, 0, 0);
id->SetVal(make_intrusive<Val>(f)); id->SetVal(make_intrusive<Val>(std::move(f)));
} }
} }
@ -638,14 +638,14 @@ void end_func(IntrusivePtr<Stmt> body)
ingredients->priority); ingredients->priority);
else else
{ {
Func* f = new BroFunc( auto f = make_intrusive<BroFunc>(
ingredients->id.get(), ingredients->id.get(),
ingredients->body, ingredients->body,
ingredients->inits, ingredients->inits,
ingredients->frame_size, ingredients->frame_size,
ingredients->priority); ingredients->priority);
ingredients->id->SetVal(make_intrusive<Val>(f)); ingredients->id->SetVal(make_intrusive<Val>(std::move(f)));
ingredients->id->SetConst(); ingredients->id->SetConst();
} }