mirror of
https://github.com/zeek/zeek.git
synced 2025-10-10 02:28:21 +00:00
Change BroValUnion to use IntrusivePtr for record field storage
This also changes the AsRecord() and AsNonConstRecord() accessors to return std::vector<IntrusivePtr<Val>>* instead of val_list*
This commit is contained in:
parent
9583873936
commit
377779bb2a
5 changed files with 38 additions and 36 deletions
3
NEWS
3
NEWS
|
@ -89,6 +89,9 @@ Changed Functionality
|
||||||
- ``TypeList::Types()`` and ``IndexType::IndexTypes()`` now return an
|
- ``TypeList::Types()`` and ``IndexType::IndexTypes()`` now return an
|
||||||
``std::vector`` instead of ``type_list*``
|
``std::vector`` instead of ``type_list*``
|
||||||
|
|
||||||
|
- ``AsRecord()`` and ``AsNonConstRecord()`` have changed to return
|
||||||
|
``std::vector<IntrusivePtr<Val>>*``.
|
||||||
|
|
||||||
Removed Functionality
|
Removed Functionality
|
||||||
---------------------
|
---------------------
|
||||||
|
|
||||||
|
|
|
@ -910,7 +910,7 @@ Connection* NetSessions::FindConnection(Val* v)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
RecordType* vr = vt->AsRecordType();
|
RecordType* vr = vt->AsRecordType();
|
||||||
const val_list* vl = v->AsRecord();
|
auto vl = v->AsRecord();
|
||||||
|
|
||||||
int orig_h, orig_p; // indices into record's value list
|
int orig_h, orig_p; // indices into record's value list
|
||||||
int resp_h, resp_p;
|
int resp_h, resp_p;
|
||||||
|
|
53
src/Val.cc
53
src/Val.cc
|
@ -2673,7 +2673,8 @@ RecordVal::RecordVal(IntrusivePtr<RecordType> t, bool init_fields) : Val(std::mo
|
||||||
origin = nullptr;
|
origin = nullptr;
|
||||||
auto rt = GetType()->AsRecordType();
|
auto rt = GetType()->AsRecordType();
|
||||||
int n = rt->NumFields();
|
int n = rt->NumFields();
|
||||||
val_list* vl = val.val_list_val = new val_list(n);
|
auto vl = val.record_val = new std::vector<IntrusivePtr<Val>>;
|
||||||
|
vl->reserve(n);
|
||||||
|
|
||||||
if ( is_parsing )
|
if ( is_parsing )
|
||||||
parse_time_records[rt].emplace_back(NewRef{}, this);
|
parse_time_records[rt].emplace_back(NewRef{}, this);
|
||||||
|
@ -2715,13 +2716,13 @@ RecordVal::RecordVal(IntrusivePtr<RecordType> t, bool init_fields) : Val(std::mo
|
||||||
def = make_intrusive<VectorVal>(cast_intrusive<VectorType>(type));
|
def = make_intrusive<VectorVal>(cast_intrusive<VectorType>(type));
|
||||||
}
|
}
|
||||||
|
|
||||||
vl->push_back(def.release());
|
vl->emplace_back(std::move(def));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
RecordVal::~RecordVal()
|
RecordVal::~RecordVal()
|
||||||
{
|
{
|
||||||
delete_vals(AsNonConstRecord());
|
delete AsNonConstRecord();
|
||||||
}
|
}
|
||||||
|
|
||||||
IntrusivePtr<Val> RecordVal::SizeVal() const
|
IntrusivePtr<Val> RecordVal::SizeVal() const
|
||||||
|
@ -2731,8 +2732,7 @@ IntrusivePtr<Val> RecordVal::SizeVal() const
|
||||||
|
|
||||||
void RecordVal::Assign(int field, IntrusivePtr<Val> new_val)
|
void RecordVal::Assign(int field, IntrusivePtr<Val> new_val)
|
||||||
{
|
{
|
||||||
Val* old_val = AsNonConstRecord()->replace(field, new_val.release());
|
(*AsNonConstRecord())[field] = std::move(new_val);
|
||||||
Unref(old_val);
|
|
||||||
Modified();
|
Modified();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2743,15 +2743,15 @@ void RecordVal::Assign(int field, Val* new_val)
|
||||||
|
|
||||||
Val* RecordVal::Lookup(int field) const
|
Val* RecordVal::Lookup(int field) const
|
||||||
{
|
{
|
||||||
return (*AsRecord())[field];
|
return (*AsRecord())[field].get();
|
||||||
}
|
}
|
||||||
|
|
||||||
IntrusivePtr<Val> RecordVal::LookupWithDefault(int field) const
|
IntrusivePtr<Val> RecordVal::LookupWithDefault(int field) const
|
||||||
{
|
{
|
||||||
Val* val = (*AsRecord())[field];
|
const auto& val = (*AsRecord())[field];
|
||||||
|
|
||||||
if ( val )
|
if ( val )
|
||||||
return {NewRef{}, val};
|
return val;
|
||||||
|
|
||||||
return GetType()->AsRecordType()->FieldDefault(field);
|
return GetType()->AsRecordType()->FieldDefault(field);
|
||||||
}
|
}
|
||||||
|
@ -2767,16 +2767,16 @@ void RecordVal::ResizeParseTimeRecords(RecordType* rt)
|
||||||
|
|
||||||
for ( auto& rv : rvs )
|
for ( auto& rv : rvs )
|
||||||
{
|
{
|
||||||
auto vs = rv->val.val_list_val;
|
auto vs = rv->val.record_val;
|
||||||
auto current_length = vs->length();
|
int current_length = vs->size();
|
||||||
auto required_length = rt->NumFields();
|
auto required_length = rt->NumFields();
|
||||||
|
|
||||||
if ( required_length > current_length )
|
if ( required_length > current_length )
|
||||||
{
|
{
|
||||||
vs->resize(required_length);
|
vs->reserve(required_length);
|
||||||
|
|
||||||
for ( auto i = current_length; i < required_length; ++i )
|
for ( auto i = current_length; i < required_length; ++i )
|
||||||
vs->replace(i, rt->FieldDefault(i).release());
|
vs->emplace_back(rt->FieldDefault(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2876,8 +2876,8 @@ IntrusivePtr<TableVal> RecordVal::GetRecordFieldsVal() const
|
||||||
|
|
||||||
void RecordVal::Describe(ODesc* d) const
|
void RecordVal::Describe(ODesc* d) const
|
||||||
{
|
{
|
||||||
const val_list* vl = AsRecord();
|
auto vl = AsRecord();
|
||||||
int n = vl->length();
|
auto n = vl->size();
|
||||||
auto record_type = GetType()->AsRecordType();
|
auto record_type = GetType()->AsRecordType();
|
||||||
|
|
||||||
if ( d->IsBinary() || d->IsPortable() )
|
if ( d->IsBinary() || d->IsPortable() )
|
||||||
|
@ -2890,7 +2890,7 @@ void RecordVal::Describe(ODesc* d) const
|
||||||
else
|
else
|
||||||
d->Add("[");
|
d->Add("[");
|
||||||
|
|
||||||
loop_over_list(*vl, i)
|
for ( size_t i = 0; i < n; ++i )
|
||||||
{
|
{
|
||||||
if ( ! d->IsBinary() && i > 0 )
|
if ( ! d->IsBinary() && i > 0 )
|
||||||
d->Add(", ");
|
d->Add(", ");
|
||||||
|
@ -2900,7 +2900,8 @@ void RecordVal::Describe(ODesc* d) const
|
||||||
if ( ! d->IsBinary() )
|
if ( ! d->IsBinary() )
|
||||||
d->Add("=");
|
d->Add("=");
|
||||||
|
|
||||||
Val* v = (*vl)[i];
|
const auto& v = (*vl)[i];
|
||||||
|
|
||||||
if ( v )
|
if ( v )
|
||||||
v->Describe(d);
|
v->Describe(d);
|
||||||
else
|
else
|
||||||
|
@ -2913,14 +2914,14 @@ void RecordVal::Describe(ODesc* d) const
|
||||||
|
|
||||||
void RecordVal::DescribeReST(ODesc* d) const
|
void RecordVal::DescribeReST(ODesc* d) const
|
||||||
{
|
{
|
||||||
const val_list* vl = AsRecord();
|
auto vl = AsRecord();
|
||||||
int n = vl->length();
|
auto n = vl->size();
|
||||||
auto record_type = GetType()->AsRecordType();
|
auto record_type = GetType()->AsRecordType();
|
||||||
|
|
||||||
d->Add("{");
|
d->Add("{");
|
||||||
d->PushIndent();
|
d->PushIndent();
|
||||||
|
|
||||||
loop_over_list(*vl, i)
|
for ( size_t i = 0; i < n; ++i )
|
||||||
{
|
{
|
||||||
if ( i > 0 )
|
if ( i > 0 )
|
||||||
d->NL();
|
d->NL();
|
||||||
|
@ -2928,7 +2929,7 @@ void RecordVal::DescribeReST(ODesc* d) const
|
||||||
d->Add(record_type->FieldName(i));
|
d->Add(record_type->FieldName(i));
|
||||||
d->Add("=");
|
d->Add("=");
|
||||||
|
|
||||||
Val* v = (*vl)[i];
|
const auto& v = (*vl)[i];
|
||||||
|
|
||||||
if ( v )
|
if ( v )
|
||||||
v->Describe(d);
|
v->Describe(d);
|
||||||
|
@ -2951,10 +2952,10 @@ IntrusivePtr<Val> RecordVal::DoClone(CloneState* state)
|
||||||
rv->origin = nullptr;
|
rv->origin = nullptr;
|
||||||
state->NewClone(this, rv);
|
state->NewClone(this, rv);
|
||||||
|
|
||||||
for ( const auto& vlv : *val.val_list_val )
|
for ( const auto& vlv : *val.record_val)
|
||||||
{
|
{
|
||||||
auto v = vlv ? vlv->Clone(state) : nullptr;
|
auto v = vlv ? vlv->Clone(state) : nullptr;
|
||||||
rv->val.val_list_val->push_back(v.release());
|
rv->val.record_val->emplace_back(std::move(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
return rv;
|
return rv;
|
||||||
|
@ -2963,15 +2964,17 @@ IntrusivePtr<Val> RecordVal::DoClone(CloneState* state)
|
||||||
unsigned int RecordVal::MemoryAllocation() const
|
unsigned int RecordVal::MemoryAllocation() const
|
||||||
{
|
{
|
||||||
unsigned int size = 0;
|
unsigned int size = 0;
|
||||||
const val_list* vl = AsRecord();
|
const auto& vl = *AsRecord();
|
||||||
|
|
||||||
for ( const auto& v : *vl )
|
for ( const auto& v : vl )
|
||||||
{
|
{
|
||||||
if ( v )
|
if ( v )
|
||||||
size += v->MemoryAllocation();
|
size += v->MemoryAllocation();
|
||||||
}
|
}
|
||||||
|
|
||||||
return size + padded_sizeof(*this) + val.val_list_val->MemoryAllocation();
|
size += pad_size(vl.capacity() * sizeof(IntrusivePtr<Val>));
|
||||||
|
size += padded_sizeof(vl);
|
||||||
|
return size + padded_sizeof(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
IntrusivePtr<Val> EnumVal::SizeVal() const
|
IntrusivePtr<Val> EnumVal::SizeVal() const
|
||||||
|
|
10
src/Val.h
10
src/Val.h
|
@ -80,8 +80,7 @@ union BroValUnion {
|
||||||
BroFile* file_val;
|
BroFile* file_val;
|
||||||
RE_Matcher* re_val;
|
RE_Matcher* re_val;
|
||||||
PDict<TableEntryVal>* table_val;
|
PDict<TableEntryVal>* table_val;
|
||||||
val_list* val_list_val;
|
std::vector<IntrusivePtr<Val>>* record_val;
|
||||||
|
|
||||||
std::vector<Val*>* vector_val;
|
std::vector<Val*>* vector_val;
|
||||||
|
|
||||||
BroValUnion() = default;
|
BroValUnion() = default;
|
||||||
|
@ -116,9 +115,6 @@ union BroValUnion {
|
||||||
constexpr BroValUnion(PDict<TableEntryVal>* value) noexcept
|
constexpr BroValUnion(PDict<TableEntryVal>* value) noexcept
|
||||||
: table_val(value) {}
|
: table_val(value) {}
|
||||||
|
|
||||||
constexpr BroValUnion(val_list* value) noexcept
|
|
||||||
: val_list_val(value) {}
|
|
||||||
|
|
||||||
constexpr BroValUnion(std::vector<Val*> *value) noexcept
|
constexpr BroValUnion(std::vector<Val*> *value) noexcept
|
||||||
: vector_val(value) {}
|
: vector_val(value) {}
|
||||||
};
|
};
|
||||||
|
@ -222,7 +218,7 @@ public:
|
||||||
CONST_ACCESSOR(TYPE_STRING, BroString*, string_val, AsString)
|
CONST_ACCESSOR(TYPE_STRING, BroString*, string_val, AsString)
|
||||||
CONST_ACCESSOR(TYPE_FUNC, Func*, func_val, AsFunc)
|
CONST_ACCESSOR(TYPE_FUNC, Func*, func_val, AsFunc)
|
||||||
CONST_ACCESSOR(TYPE_TABLE, PDict<TableEntryVal>*, table_val, AsTable)
|
CONST_ACCESSOR(TYPE_TABLE, PDict<TableEntryVal>*, table_val, AsTable)
|
||||||
CONST_ACCESSOR(TYPE_RECORD, val_list*, val_list_val, AsRecord)
|
CONST_ACCESSOR(TYPE_RECORD, std::vector<IntrusivePtr<Val>>*, record_val, AsRecord)
|
||||||
CONST_ACCESSOR(TYPE_FILE, BroFile*, file_val, AsFile)
|
CONST_ACCESSOR(TYPE_FILE, BroFile*, file_val, AsFile)
|
||||||
CONST_ACCESSOR(TYPE_PATTERN, RE_Matcher*, re_val, AsPattern)
|
CONST_ACCESSOR(TYPE_PATTERN, RE_Matcher*, re_val, AsPattern)
|
||||||
CONST_ACCESSOR(TYPE_VECTOR, std::vector<Val*>*, vector_val, AsVector)
|
CONST_ACCESSOR(TYPE_VECTOR, std::vector<Val*>*, vector_val, AsVector)
|
||||||
|
@ -368,7 +364,7 @@ protected:
|
||||||
{}
|
{}
|
||||||
|
|
||||||
ACCESSOR(TYPE_TABLE, PDict<TableEntryVal>*, table_val, AsNonConstTable)
|
ACCESSOR(TYPE_TABLE, PDict<TableEntryVal>*, table_val, AsNonConstTable)
|
||||||
ACCESSOR(TYPE_RECORD, val_list*, val_list_val, AsNonConstRecord)
|
ACCESSOR(TYPE_RECORD, std::vector<IntrusivePtr<Val>>*, record_val, AsNonConstRecord)
|
||||||
|
|
||||||
// For internal use by the Val::Clone() methods.
|
// For internal use by the Val::Clone() methods.
|
||||||
struct CloneState {
|
struct CloneState {
|
||||||
|
|
|
@ -3333,8 +3333,8 @@ function lookup_connection%(cid: conn_id%): connection
|
||||||
%%{
|
%%{
|
||||||
const char* conn_id_string(Val* c)
|
const char* conn_id_string(Val* c)
|
||||||
{
|
{
|
||||||
Val* id = (*(c->AsRecord()))[0];
|
const auto& id = (*(c->AsRecord()))[0];
|
||||||
const val_list* vl = id->AsRecord();
|
auto vl = id->AsRecord();
|
||||||
|
|
||||||
const IPAddr& orig_h = (*vl)[0]->AsAddr();
|
const IPAddr& orig_h = (*vl)[0]->AsAddr();
|
||||||
uint32_t orig_p = (*vl)[1]->AsPortVal()->Port();
|
uint32_t orig_p = (*vl)[1]->AsPortVal()->Port();
|
||||||
|
@ -3459,7 +3459,7 @@ function dump_packet%(pkt: pcap_packet, file_name: string%) : bool
|
||||||
uint32_t caplen, len, link_type;
|
uint32_t caplen, len, link_type;
|
||||||
u_char *data;
|
u_char *data;
|
||||||
|
|
||||||
const val_list* pkt_vl = pkt->AsRecord();
|
auto pkt_vl = pkt->AsRecord();
|
||||||
|
|
||||||
ts.tv_sec = (*pkt_vl)[0]->AsCount();
|
ts.tv_sec = (*pkt_vl)[0]->AsCount();
|
||||||
ts.tv_usec = (*pkt_vl)[1]->AsCount();
|
ts.tv_usec = (*pkt_vl)[1]->AsCount();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue