diff --git a/src/DNS_Mgr.cc b/src/DNS_Mgr.cc index 513bf53809..739e0d709d 100644 --- a/src/DNS_Mgr.cc +++ b/src/DNS_Mgr.cc @@ -32,6 +32,7 @@ #include #include "BroString.h" +#include "Expr.h" #include "Event.h" #include "Net.h" #include "Val.h" @@ -173,9 +174,9 @@ void DNS_Mgr_mapping_delete_func(void* v) static TableVal* empty_addr_set() { BroType* addr_t = base_type(TYPE_ADDR); - TypeList* set_index = new TypeList(addr_t); + auto set_index = make_intrusive(addr_t); set_index->Append(addr_t); - auto s = make_intrusive(set_index, nullptr); + auto s = make_intrusive(std::move(set_index), nullptr); return new TableVal(std::move(s)); } diff --git a/src/Expr.cc b/src/Expr.cc index 4a98be374e..4f9da75168 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -2300,7 +2300,7 @@ IntrusivePtr AssignExpr::InitType() const if ( tl->Tag() != TYPE_LIST ) Internal("inconsistent list expr in AssignExpr::InitType"); - return make_intrusive(tl->Ref()->AsTypeList(), op2->Type()->Ref()); + return make_intrusive(IntrusivePtr{NewRef{}, tl->AsTypeList()}, IntrusivePtr{NewRef{}, op2->Type()}); } void AssignExpr::EvalIntoAggregate(const BroType* t, Val* aggr, Frame* f) const @@ -3109,7 +3109,7 @@ TableConstructorExpr::TableConstructorExpr(IntrusivePtr constructor_li else { if ( op->AsListExpr()->Exprs().empty() ) - SetType(make_intrusive(new TypeList(base_type(TYPE_ANY)), nullptr)); + SetType(make_intrusive(make_intrusive(base_type(TYPE_ANY)), nullptr)); else { SetType({AdoptRef{}, init_type(op.get())}); @@ -3223,7 +3223,7 @@ SetConstructorExpr::SetConstructorExpr(IntrusivePtr constructor_list, else { if ( op->AsListExpr()->Exprs().empty() ) - SetType(make_intrusive<::SetType>(new TypeList(base_type(TYPE_ANY)), nullptr)); + SetType(make_intrusive<::SetType>(make_intrusive(base_type(TYPE_ANY)), nullptr)); else SetType({AdoptRef{}, init_type(op.get())}); } diff --git a/src/Type.cc b/src/Type.cc index 8f7da9b743..274aa9c468 100644 --- a/src/Type.cc +++ b/src/Type.cc @@ -220,11 +220,7 @@ unsigned int TypeList::MemoryAllocation() const + types.MemoryAllocation() - padded_sizeof(types); } -IndexType::~IndexType() - { - Unref(indices); - Unref(yield_type); - } +IndexType::~IndexType() = default; int IndexType::MatchesIndex(ListExpr* const index) const { @@ -242,12 +238,12 @@ int IndexType::MatchesIndex(ListExpr* const index) const BroType* IndexType::YieldType() { - return yield_type; + return yield_type.get(); } const BroType* IndexType::YieldType() const { - return yield_type; + return yield_type.get(); } void IndexType::Describe(ODesc* d) const @@ -326,8 +322,8 @@ bool IndexType::IsSubNetIndex() const return false; } -TableType::TableType(TypeList* ind, BroType* yield) -: IndexType(TYPE_TABLE, ind, yield) +TableType::TableType(IntrusivePtr ind, IntrusivePtr yield) +: IndexType(TYPE_TABLE, std::move(ind), std::move(yield)) { if ( ! indices ) return; @@ -355,11 +351,6 @@ TableType::TableType(TypeList* ind, BroType* yield) TableType* TableType::ShallowClone() { - if ( indices ) - indices->Ref(); - if ( yield_type ) - yield_type->Ref(); - return new TableType(indices, yield_type); } @@ -383,14 +374,13 @@ TypeList* TableType::ExpandRecordIndex(RecordType* rt) const return tl; } -SetType::SetType(TypeList* ind, ListExpr* arg_elements) : TableType(ind, 0) +SetType::SetType(IntrusivePtr ind, IntrusivePtr arg_elements) : TableType(std::move(ind), nullptr), elements(std::move(arg_elements)) { - elements = arg_elements; if ( elements ) { if ( indices ) { // We already have a type. - if ( ! check_and_promote_exprs(elements, indices) ) + if ( ! check_and_promote_exprs(elements.get(), indices.get()) ) SetError(); } else @@ -407,7 +397,7 @@ SetType::SetType(TypeList* ind, ListExpr* arg_elements) : TableType(ind, 0) else if ( tl->length() == 1 ) { BroType* t = flatten_type((*tl)[0]->Ref()); - indices = new TypeList(t); + indices = make_intrusive(t); indices->Append(t->Ref()); } @@ -428,7 +418,7 @@ SetType::SetType(TypeList* ind, ListExpr* arg_elements) : TableType(ind, 0) return; } - indices = new TypeList(t); + indices = make_intrusive(t); indices->Append(t); } } @@ -437,19 +427,10 @@ SetType::SetType(TypeList* ind, ListExpr* arg_elements) : TableType(ind, 0) SetType* SetType::ShallowClone() { - if ( elements ) - elements->Ref(); - - if ( indices ) - indices->Ref(); - return new SetType(indices, elements); } -SetType::~SetType() - { - Unref(elements); - } +SetType::~SetType() = default; FuncType::FuncType(RecordType* arg_args, BroType* arg_yield, function_flavor arg_flavor) : BroType(TYPE_FUNC) @@ -1837,7 +1818,7 @@ BroType* merge_types(const BroType* t1, const BroType* t2) const type_list* tl1 = it1->IndexTypes(); const type_list* tl2 = it2->IndexTypes(); - TypeList* tl3 = 0; + IntrusivePtr tl3; if ( tl1 || tl2 ) { @@ -1847,16 +1828,13 @@ BroType* merge_types(const BroType* t1, const BroType* t2) return 0; } - tl3 = new TypeList(); + tl3 = make_intrusive(); loop_over_list(*tl1, i) { BroType* tl3_i = merge_types((*tl1)[i], (*tl2)[i]); if ( ! tl3_i ) - { - Unref(tl3); return 0; - } tl3->Append(tl3_i); } @@ -1864,29 +1842,25 @@ BroType* merge_types(const BroType* t1, const BroType* t2) const BroType* y1 = t1->YieldType(); const BroType* y2 = t2->YieldType(); - BroType* y3 = 0; + IntrusivePtr y3; if ( y1 || y2 ) { if ( ! y1 || ! y2 ) { t1->Error("incompatible types", t2); - Unref(tl3); return 0; } - y3 = merge_types(y1, y2); + y3 = {AdoptRef{}, merge_types(y1, y2)}; if ( ! y3 ) - { - Unref(tl3); return 0; - } } if ( t1->IsSet() ) - return new SetType(tl3, 0); + return new SetType(std::move(tl3), nullptr); else - return new TableType(tl3, y3); + return new TableType(std::move(tl3), std::move(y3)); } case TYPE_FUNC: @@ -2136,7 +2110,7 @@ BroType* init_type(Expr* init) t = std::move(tl); } - return new SetType(t.release()->AsTypeList(), 0); + return new SetType({AdoptRef{}, t.release()->AsTypeList()}, nullptr); } bool is_atomic_type(const BroType* t) diff --git a/src/Type.h b/src/Type.h index 62dcadba78..0acc1da850 100644 --- a/src/Type.h +++ b/src/Type.h @@ -5,6 +5,7 @@ #include "Obj.h" #include "Attr.h" #include "BroList.h" +#include "IntrusivePtr.h" #include #include @@ -117,7 +118,6 @@ constexpr InternalTypeTag to_internal_type_tag(TypeTag tag) noexcept // Returns the name of the type. extern const char* type_name(TypeTag t); -template class IntrusivePtr; class Expr; class Attributes; class TypeList; @@ -386,7 +386,7 @@ class IndexType : public BroType { public: int MatchesIndex(ListExpr* index) const override; - TypeList* Indices() const { return indices; } + TypeList* Indices() const { return indices.get(); } const type_list* IndexTypes() const { return indices->Types(); } BroType* YieldType() override; const BroType* YieldType() const override; @@ -398,22 +398,19 @@ public: bool IsSubNetIndex() const; protected: - IndexType(){ indices = 0; yield_type = 0; } - IndexType(TypeTag t, TypeList* arg_indices, BroType* arg_yield_type) : - BroType(t) + IndexType(TypeTag t, IntrusivePtr arg_indices, IntrusivePtr arg_yield_type) : + BroType(t), indices(std::move(arg_indices)), yield_type(std::move(arg_yield_type)) { - indices = arg_indices; - yield_type = arg_yield_type; } ~IndexType() override; - TypeList* indices; - BroType* yield_type; + IntrusivePtr indices; + IntrusivePtr yield_type; }; class TableType : public IndexType { public: - TableType(TypeList* ind, BroType* yield); + TableType(IntrusivePtr ind, IntrusivePtr yield); TableType* ShallowClone() override; @@ -422,24 +419,20 @@ public: bool IsUnspecifiedTable() const; protected: - TableType() {} - TypeList* ExpandRecordIndex(RecordType* rt) const; }; class SetType : public TableType { public: - SetType(TypeList* ind, ListExpr* arg_elements); + SetType(IntrusivePtr ind, IntrusivePtr arg_elements); ~SetType() override; SetType* ShallowClone() override; - ListExpr* SetElements() const { return elements; } + ListExpr* SetElements() const { return elements.get(); } protected: - SetType() {} - - ListExpr* elements; + IntrusivePtr elements; }; class FuncType : public BroType { diff --git a/src/Val.cc b/src/Val.cc index caf1c0af55..7b82051d9c 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -1234,9 +1234,9 @@ TableVal* ListVal::ConvertToSet() const if ( tag == TYPE_ANY ) Internal("conversion of heterogeneous list to set"); - TypeList* set_index = new TypeList(type->AsTypeList()->PureType()); + auto set_index = make_intrusive(type->AsTypeList()->PureType()); set_index->Append(base_type(tag)); - auto s = make_intrusive(set_index, nullptr); + auto s = make_intrusive(std::move(set_index), nullptr); TableVal* t = new TableVal(std::move(s)); for ( const auto& val : vals ) diff --git a/src/file_analysis/File.cc b/src/file_analysis/File.cc index ec80d43120..f982080d5a 100644 --- a/src/file_analysis/File.cc +++ b/src/file_analysis/File.cc @@ -23,9 +23,9 @@ using namespace file_analysis; static Val* empty_connection_table() { - TypeList* tbl_index = new TypeList(conn_id); + auto tbl_index = make_intrusive(conn_id); tbl_index->Append(conn_id->Ref()); - auto tbl_type = make_intrusive(tbl_index, connection_type->Ref()); + auto tbl_type = make_intrusive(std::move(tbl_index), IntrusivePtr{NewRef{}, connection_type}); return new TableVal(std::move(tbl_type)); } diff --git a/src/input/Manager.cc b/src/input/Manager.cc index 1187b17873..18d13fff17 100644 --- a/src/input/Manager.cc +++ b/src/input/Manager.cc @@ -2377,9 +2377,9 @@ Val* Manager::ValueToVal(const Stream* i, const Value* val, BroType* request_typ { // all entries have to have the same type... BroType* type = request_type->AsTableType()->Indices()->PureType(); - TypeList* set_index = new TypeList(type->Ref()); + auto set_index = make_intrusive(type->Ref()); set_index->Append(type->Ref()); - auto s = make_intrusive(set_index, nullptr); + auto s = make_intrusive(std::move(set_index), nullptr); TableVal* t = new TableVal(std::move(s)); for ( int j = 0; j < val->val.set_val.size; j++ ) { @@ -2523,10 +2523,10 @@ Val* Manager::ValueToVal(const Stream* i, const Value* val, bool& have_error) co case TYPE_TABLE: { - TypeList* set_index; + IntrusivePtr set_index; if ( val->val.set_val.size == 0 && val->subtype == TYPE_VOID ) // don't know type - unspecified table. - set_index = new TypeList(); + set_index = make_intrusive(); else { // all entries have to have the same type... @@ -2557,11 +2557,11 @@ Val* Manager::ValueToVal(const Stream* i, const Value* val, bool& have_error) co else index_type = base_type_no_ref(stag); - set_index = new TypeList(index_type); + set_index = make_intrusive(index_type); set_index->Append(index_type->Ref()); } - auto s = make_intrusive(set_index, nullptr); + auto s = make_intrusive(std::move(set_index), nullptr); TableVal* t = new TableVal(std::move(s)); for ( int j = 0; j < val->val.set_val.size; j++ ) { diff --git a/src/parse.y b/src/parse.y index c9c9ed2672..621ec377af 100644 --- a/src/parse.y +++ b/src/parse.y @@ -915,13 +915,13 @@ type: | TOK_TABLE '[' type_list ']' TOK_OF type { set_location(@1, @6); - $$ = new TableType($3, $6); + $$ = new TableType({AdoptRef{}, $3}, {AdoptRef{}, $6}); } | TOK_SET '[' type_list ']' { set_location(@1, @4); - $$ = new SetType($3, 0); + $$ = new SetType({AdoptRef{}, $3}, nullptr); } | TOK_RECORD '{'