Frame: use class IntrusivePtr

This commit is contained in:
Max Kellermann 2020-03-05 19:33:51 +01:00
parent 79570fdfd6
commit 78e736621c
4 changed files with 17 additions and 37 deletions

View file

@ -25,7 +25,6 @@ Frame::Frame(int arg_size, const BroFunc* func, const val_list* fn_args)
break_before_next_stmt = false; break_before_next_stmt = false;
break_on_return = false; break_on_return = false;
trigger = nullptr;
call = nullptr; call = nullptr;
delayed = false; delayed = false;
@ -43,9 +42,6 @@ Frame::~Frame()
Unref(func); Unref(func);
} }
// Deleting a Frame that is a view is a no-op.
Unref(trigger);
if ( ! weak_closure_ref ) if ( ! weak_closure_ref )
Unref(closure); Unref(closure);
@ -183,8 +179,6 @@ Frame* Frame::Clone() const
other->call = call; other->call = call;
other->trigger = trigger; other->trigger = trigger;
if ( trigger )
Ref(trigger);
for (int i = 0; i < size; i++) for (int i = 0; i < size; i++)
other->frame[i] = frame[i] ? frame[i]->Clone().release() : nullptr; other->frame[i] = frame[i] ? frame[i]->Clone().release() : nullptr;
@ -359,14 +353,14 @@ broker::expected<broker::data> Frame::Serialize(const Frame* target, const id_li
return {std::move(rval)}; return {std::move(rval)};
} }
std::pair<bool, Frame*> Frame::Unserialize(const broker::vector& data) std::pair<bool, IntrusivePtr<Frame>> Frame::Unserialize(const broker::vector& data)
{ {
if ( data.size() == 0 ) if ( data.size() == 0 )
return std::make_pair(true, nullptr); return std::make_pair(true, nullptr);
id_list outer_ids; id_list outer_ids;
std::unordered_map<std::string, int> offset_map; std::unordered_map<std::string, int> offset_map;
Frame* closure = nullptr; IntrusivePtr<Frame> closure;
auto where = data.begin(); auto where = data.begin();
@ -410,7 +404,7 @@ std::pair<bool, Frame*> Frame::Unserialize(const broker::vector& data)
return std::make_pair(false, nullptr); return std::make_pair(false, nullptr);
} }
closure = closure_pair.second; closure = std::move(closure_pair.second);
} }
auto has_vec = broker::get_if<broker::vector>(*where); auto has_vec = broker::get_if<broker::vector>(*where);
@ -448,11 +442,11 @@ std::pair<bool, Frame*> Frame::Unserialize(const broker::vector& data)
int frame_size = body.size(); int frame_size = body.size();
// We'll associate this frame with a function later. // We'll associate this frame with a function later.
Frame* rf = new Frame(frame_size, nullptr, nullptr); auto rf = make_intrusive<Frame>(frame_size, nullptr, nullptr);
rf->offset_map = std::move(offset_map); rf->offset_map = std::move(offset_map);
// Frame takes ownership of unref'ing elements in outer_ids // Frame takes ownership of unref'ing elements in outer_ids
rf->outer_ids = std::move(outer_ids); rf->outer_ids = std::move(outer_ids);
rf->closure = closure; rf->closure = closure.release();
rf->weak_closure_ref = false; rf->weak_closure_ref = false;
for ( int i = 0; i < frame_size; ++i ) for ( int i = 0; i < frame_size; ++i )
@ -463,32 +457,23 @@ std::pair<bool, Frame*> Frame::Unserialize(const broker::vector& data)
broker::vector val_tuple = *has_vec; broker::vector val_tuple = *has_vec;
if ( val_tuple.size() != 2 ) if ( val_tuple.size() != 2 )
{
Unref(rf);
return std::make_pair(false, nullptr); return std::make_pair(false, nullptr);
}
auto has_type = broker::get_if<broker::integer>(val_tuple[1]); auto has_type = broker::get_if<broker::integer>(val_tuple[1]);
if ( ! has_type ) if ( ! has_type )
{
Unref(rf);
return std::make_pair(false, nullptr); return std::make_pair(false, nullptr);
}
broker::integer g = *has_type; broker::integer g = *has_type;
BroType t( static_cast<TypeTag>(g) ); BroType t( static_cast<TypeTag>(g) );
auto val = bro_broker::data_to_val(std::move(val_tuple[0]), &t); auto val = bro_broker::data_to_val(std::move(val_tuple[0]), &t);
if ( ! val ) if ( ! val )
{
Unref(rf);
return std::make_pair(false, nullptr); return std::make_pair(false, nullptr);
}
rf->frame[i] = val.release(); rf->frame[i] = val.release();
} }
return std::make_pair(true, rf); return std::make_pair(true, std::move(rf));
} }
void Frame::AddKnownOffsets(const id_list& ids) void Frame::AddKnownOffsets(const id_list& ids)
@ -521,19 +506,13 @@ void Frame::CaptureClosure(Frame* c, id_list arg_outer_ids)
// if (c) closure = c->SelectiveClone(outer_ids); // if (c) closure = c->SelectiveClone(outer_ids);
} }
void Frame::SetTrigger(trigger::Trigger* arg_trigger) void Frame::SetTrigger(IntrusivePtr<trigger::Trigger> arg_trigger)
{ {
ClearTrigger(); trigger = std::move(arg_trigger);
if ( arg_trigger )
Ref(arg_trigger);
trigger = arg_trigger;
} }
void Frame::ClearTrigger() void Frame::ClearTrigger()
{ {
Unref(trigger);
trigger = nullptr; trigger = nullptr;
} }

View file

@ -4,6 +4,7 @@
#include "BroList.h" // for typedef val_list #include "BroList.h" // for typedef val_list
#include "Obj.h" #include "Obj.h"
#include "IntrusivePtr.h"
#include <unordered_map> #include <unordered_map>
#include <string> #include <string>
@ -189,7 +190,7 @@ public:
* and the second is the unserialized frame with reference count +1, or * and the second is the unserialized frame with reference count +1, or
* null if the serialization wasn't successful. * null if the serialization wasn't successful.
*/ */
static std::pair<bool, Frame*> Unserialize(const broker::vector& data); static std::pair<bool, IntrusivePtr<Frame>> Unserialize(const broker::vector& data);
/** /**
* Sets the IDs that the frame knows offsets for. These offsets will * Sets the IDs that the frame knows offsets for. These offsets will
@ -210,9 +211,9 @@ public:
// If the frame is run in the context of a trigger condition evaluation, // If the frame is run in the context of a trigger condition evaluation,
// the trigger needs to be registered. // the trigger needs to be registered.
void SetTrigger(trigger::Trigger* arg_trigger); void SetTrigger(IntrusivePtr<trigger::Trigger> arg_trigger);
void ClearTrigger(); void ClearTrigger();
trigger::Trigger* GetTrigger() const { return trigger; } trigger::Trigger* GetTrigger() const { return trigger.get(); }
void SetCall(const CallExpr* arg_call) { call = arg_call; } void SetCall(const CallExpr* arg_call) { call = arg_call; }
void ClearCall() { call = 0; } void ClearCall() { call = 0; }
@ -290,7 +291,7 @@ private:
bool break_before_next_stmt; bool break_before_next_stmt;
bool break_on_return; bool break_on_return;
trigger::Trigger* trigger; IntrusivePtr<trigger::Trigger> trigger;
const CallExpr* call; const CallExpr* call;
bool delayed; bool delayed;

View file

@ -337,7 +337,7 @@ IntrusivePtr<Val> BroFunc::Call(val_list* args, Frame* parent) const
// Hand down any trigger. // Hand down any trigger.
if ( parent ) if ( parent )
{ {
f->SetTrigger(parent->GetTrigger()); f->SetTrigger({NewRef{}, parent->GetTrigger()});
f->SetCall(parent->GetCall()); f->SetCall(parent->GetCall());
} }
@ -525,7 +525,7 @@ bool BroFunc::UpdateClosure(const broker::vector& data)
if ( ! result.first ) if ( ! result.first )
return false; return false;
Frame* new_closure = result.second; auto &new_closure = result.second;
if ( new_closure ) if ( new_closure )
new_closure->SetFunction(this); new_closure->SetFunction(this);
@ -533,7 +533,7 @@ bool BroFunc::UpdateClosure(const broker::vector& data)
Unref(closure); Unref(closure);
weak_closure_ref = false; weak_closure_ref = false;
closure = new_closure; closure = new_closure.release();
return true; return true;
} }

View file

@ -265,7 +265,7 @@ bool Trigger::Eval()
return false; return false;
} }
f->SetTrigger(this); f->SetTrigger({NewRef{}, this});
IntrusivePtr<Val> v; IntrusivePtr<Val> v;