From 202b3f877d438a24717d0852314ad6188fce6b4e Mon Sep 17 00:00:00 2001 From: Jon Siwek Date: Fri, 3 Apr 2020 16:36:01 -0700 Subject: [PATCH] Deprecate all ValManager "Get" methods Alternate methods that return IntrusivePtr are available in similarly named methods that omit the "Get" prefix. --- NEWS | 3 +++ src/Val.cc | 70 ++++++++++++++++++++++++++-------------------------- src/Val.h | 72 ++++++++++++++++++++++++++++++++++++------------------ 3 files changed, 86 insertions(+), 59 deletions(-) diff --git a/NEWS b/NEWS index 88fa60871b..c2d5289626 100644 --- a/NEWS +++ b/NEWS @@ -104,6 +104,9 @@ Deprecated Functionality ``analyzer::Analyzer::ConectionEventFast()`` methods are deprecated, use ``analyzer::Analyzer::EnqueueConnEvent()`` instead. +- All ``val_mgr`` methods starting with "Get" are deprecated, use the new + ``val_mgr`` methods that return ``IntrusivePtr``. + Zeek 3.1.0 ========== diff --git a/src/Val.cc b/src/Val.cc index 6c24a89471..b6502ddc59 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -3398,13 +3398,26 @@ bool can_cast_value_to_type(const BroType* s, BroType* t) return false; } +IntrusivePtr Val::MakeBool(bool b) + { + return IntrusivePtr{AdoptRef{}, new Val(bro_int_t(b), TYPE_BOOL)}; + } + +IntrusivePtr Val::MakeInt(bro_int_t i) + { + return IntrusivePtr{AdoptRef{}, new Val(i, TYPE_INT)}; + } + +IntrusivePtr Val::MakeCount(bro_uint_t u) + { + return IntrusivePtr{AdoptRef{}, new Val(u, TYPE_COUNT)}; + } + ValManager::ValManager() { - empty_string = new StringVal(""); + empty_string = make_intrusive(""); b_false = Val::MakeBool(false); b_true = Val::MakeBool(true); - counts = new Val*[PREALLOCATED_COUNTS]; - ints = new Val*[PREALLOCATED_INTS]; for ( auto i = 0u; i < PREALLOCATED_COUNTS; ++i ) counts[i] = Val::MakeCount(i); @@ -3418,37 +3431,16 @@ ValManager::ValManager() auto port_type = (TransportProto)i; for ( auto j = 0u; j < arr.size(); ++j ) - arr[j] = new PortVal(PortVal::Mask(j, port_type)); + arr[j] = IntrusivePtr{AdoptRef{}, new PortVal(PortVal::Mask(j, port_type))}; } } -ValManager::~ValManager() - { - Unref(empty_string); - Unref(b_true); - Unref(b_false); - - for ( auto i = 0u; i < PREALLOCATED_COUNTS; ++i ) - Unref(counts[i]); - - for ( auto i = 0u; i < PREALLOCATED_INTS; ++i ) - Unref(ints[i]); - - delete [] counts; - delete [] ints; - - for ( auto& arr : ports ) - for ( auto& pv : arr ) - Unref(pv); - } - StringVal* ValManager::GetEmptyString() const { - ::Ref(empty_string); - return empty_string; + return empty_string->Ref()->AsStringVal(); } -PortVal* ValManager::GetPort(uint32_t port_num, TransportProto port_type) const +const IntrusivePtr& ValManager::Port(uint32_t port_num, TransportProto port_type) const { if ( port_num >= 65536 ) { @@ -3456,22 +3448,30 @@ PortVal* ValManager::GetPort(uint32_t port_num, TransportProto port_type) const port_num = 0; } - auto rval = ports[port_type][port_num]; - ::Ref(rval); - return rval; + return ports[port_type][port_num]; } -PortVal* ValManager::GetPort(uint32_t port_num) const +PortVal* ValManager::GetPort(uint32_t port_num, TransportProto port_type) const + { + return Port(port_num, port_type)->Ref()->AsPortVal(); + } + +const IntrusivePtr& ValManager::Port(uint32_t port_num) const { auto mask = port_num & PORT_SPACE_MASK; port_num &= ~PORT_SPACE_MASK; if ( mask == TCP_PORT_MASK ) - return GetPort(port_num, TRANSPORT_TCP); + return Port(port_num, TRANSPORT_TCP); else if ( mask == UDP_PORT_MASK ) - return GetPort(port_num, TRANSPORT_UDP); + return Port(port_num, TRANSPORT_UDP); else if ( mask == ICMP_PORT_MASK ) - return GetPort(port_num, TRANSPORT_ICMP); + return Port(port_num, TRANSPORT_ICMP); else - return GetPort(port_num, TRANSPORT_UNKNOWN); + return Port(port_num, TRANSPORT_UNKNOWN); + } + +PortVal* ValManager::GetPort(uint32_t port_num) const + { + return Port(port_num)->Ref()->AsPortVal(); } diff --git a/src/Val.h b/src/Val.h index 6c2fb5ac1c..13dce4c18c 100644 --- a/src/Val.h +++ b/src/Val.h @@ -335,20 +335,9 @@ protected: virtual void ValDescribe(ODesc* d) const; virtual void ValDescribeReST(ODesc* d) const; - static Val* MakeBool(bool b) - { - return new Val(bro_int_t(b), TYPE_BOOL); - } - - static Val* MakeInt(bro_int_t i) - { - return new Val(i, TYPE_INT); - } - - static Val* MakeCount(bro_uint_t u) - { - return new Val(u, TYPE_COUNT); - } + static IntrusivePtr MakeBool(bool b); + static IntrusivePtr MakeInt(bro_int_t i); + static IntrusivePtr MakeCount(bro_uint_t u); template Val(V &&v, TypeTag t) noexcept @@ -406,44 +395,79 @@ public: ValManager(); - ~ValManager(); - + [[deprecated("Remove in v4.1. Use val_mgr->True() instead.")]] inline Val* GetTrue() const { return b_true->Ref(); } + inline const IntrusivePtr& True() const + { return b_true; } + + [[deprecated("Remove in v4.1. Use val_mgr->False() instead.")]] inline Val* GetFalse() const { return b_false->Ref(); } + inline const IntrusivePtr& False() const + { return b_false; } + + [[deprecated("Remove in v4.1. Use val_mgr->Bool() instead.")]] inline Val* GetBool(bool b) const { return b ? b_true->Ref() : b_false->Ref(); } + inline const IntrusivePtr& Bool(bool b) const + { return b ? b_true : b_false; } + + [[deprecated("Remove in v4.1. Use val_mgr->Int() instead.")]] inline Val* GetInt(int64_t i) const { return i < PREALLOCATED_INT_LOWEST || i > PREALLOCATED_INT_HIGHEST ? - Val::MakeInt(i) : ints[i - PREALLOCATED_INT_LOWEST]->Ref(); + Val::MakeInt(i).release() : ints[i - PREALLOCATED_INT_LOWEST]->Ref(); } + inline IntrusivePtr Int(int64_t i) const + { + return i < PREALLOCATED_INT_LOWEST || i > PREALLOCATED_INT_HIGHEST ? + Val::MakeInt(i) : ints[i - PREALLOCATED_INT_LOWEST]; + } + + [[deprecated("Remove in v4.1. Use val_mgr->Count() instead.")]] inline Val* GetCount(uint64_t i) const { - return i >= PREALLOCATED_COUNTS ? Val::MakeCount(i) : counts[i]->Ref(); + return i >= PREALLOCATED_COUNTS ? Val::MakeCount(i).release() : counts[i]->Ref(); } + inline IntrusivePtr Count(uint64_t i) const + { + return i >= PREALLOCATED_COUNTS ? Val::MakeCount(i) : counts[i]; + } + + [[deprecated("Remove in v4.1. Use val_mgr->EmptyString() instead.")]] StringVal* GetEmptyString() const; + inline const IntrusivePtr& EmptyString() const + { return empty_string; } + // Port number given in host order. + [[deprecated("Remove in v4.1. Use val_mgr->Port() instead.")]] PortVal* GetPort(uint32_t port_num, TransportProto port_type) const; + // Port number given in host order. + const IntrusivePtr& Port(uint32_t port_num, TransportProto port_type) const; + // Host-order port number already masked with port space protocol mask. + [[deprecated("Remove in v4.1. Use val_mgr->Port() instead.")]] PortVal* GetPort(uint32_t port_num) const; + // Host-order port number already masked with port space protocol mask. + const IntrusivePtr& Port(uint32_t port_num) const; + private: - std::array, NUM_PORT_SPACES> ports; - StringVal* empty_string; - Val* b_true; - Val* b_false; - Val** counts; - Val** ints; + std::array, 65536>, NUM_PORT_SPACES> ports; + std::array, PREALLOCATED_COUNTS> counts; + std::array, PREALLOCATED_INTS> ints; + IntrusivePtr empty_string; + IntrusivePtr b_true; + IntrusivePtr b_false; }; extern ValManager* val_mgr;