From 878afe22bbf5c33e9b7d60e20f8a1a749eb2e7ee Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Wed, 24 Nov 2021 10:52:48 +0000 Subject: Avoid strdup. --- fnet/src/tests/info/info.cpp | 4 ++-- fnet/src/vespa/fnet/connection.h | 7 +++---- fnet/src/vespa/fnet/frt/reflection.cpp | 32 ++++++++++---------------------- fnet/src/vespa/fnet/frt/reflection.h | 31 +++++++++++++++---------------- fnet/src/vespa/fnet/iocomponent.cpp | 11 ++++------- fnet/src/vespa/fnet/iocomponent.h | 13 ++++++------- 6 files changed, 40 insertions(+), 58 deletions(-) (limited to 'fnet') diff --git a/fnet/src/tests/info/info.cpp b/fnet/src/tests/info/info.cpp index f2299df839e..4271546e647 100644 --- a/fnet/src/tests/info/info.cpp +++ b/fnet/src/tests/info/info.cpp @@ -77,10 +77,10 @@ TEST("size of important objects") #else constexpr size_t MUTEX_SIZE = 40u; #endif - EXPECT_EQUAL(MUTEX_SIZE + 128u, sizeof(FNET_IOComponent)); + EXPECT_EQUAL(MUTEX_SIZE + sizeof(std::string) + 112u, sizeof(FNET_IOComponent)); EXPECT_EQUAL(32u, sizeof(FNET_Channel)); EXPECT_EQUAL(40u, sizeof(FNET_PacketQueue_NoLock)); - EXPECT_EQUAL(MUTEX_SIZE + 432u, sizeof(FNET_Connection)); + EXPECT_EQUAL(MUTEX_SIZE + sizeof(std::string) + 416u, sizeof(FNET_Connection)); EXPECT_EQUAL(48u, sizeof(std::condition_variable)); EXPECT_EQUAL(56u, sizeof(FNET_DataBuffer)); EXPECT_EQUAL(8u, sizeof(FNET_Context)); diff --git a/fnet/src/vespa/fnet/connection.h b/fnet/src/vespa/fnet/connection.h index 6efb147d37f..e86b670b7e5 100644 --- a/fnet/src/vespa/fnet/connection.h +++ b/fnet/src/vespa/fnet/connection.h @@ -53,7 +53,7 @@ public: class FNET_Connection : public FNET_IOComponent { public: - enum State { + enum State : uint8_t { FNET_CONNECTING, FNET_CONNECTED, FNET_CLOSING, @@ -118,9 +118,6 @@ private: static std::atomic _num_connections; // total number of connections - FNET_Connection(const FNET_Connection &); - FNET_Connection &operator=(const FNET_Connection &); - /** * Get next ID that may be used for multiplexing on this connection. @@ -245,6 +242,8 @@ private: */ vespalib::string GetPeerSpec() const; public: + FNET_Connection(const FNET_Connection &) = delete; + FNET_Connection &operator=(const FNET_Connection &) = delete; /** * Construct a connection in server aspect. diff --git a/fnet/src/vespa/fnet/frt/reflection.cpp b/fnet/src/vespa/fnet/frt/reflection.cpp index 0719c8b4c71..211e681df94 100644 --- a/fnet/src/vespa/fnet/frt/reflection.cpp +++ b/fnet/src/vespa/fnet/frt/reflection.cpp @@ -9,42 +9,30 @@ FRT_Method::FRT_Method(const char * name, const char * paramSpec, const char * r FRT_METHOD_PT method, FRT_Invokable * handler) : _hashNext(nullptr), _listNext(nullptr), - _name(strdup(name)), - _paramSpec(strdup(paramSpec)), - _returnSpec(strdup(returnSpec)), + _name(name), + _paramSpec(paramSpec), + _returnSpec(returnSpec), _method(method), _handler(handler), - _docLen(0), - _doc(nullptr) + _doc() { - assert(_name != nullptr); - assert(_paramSpec != nullptr); - assert(_returnSpec != nullptr); } -FRT_Method::~FRT_Method() { - free(_name); - free(_paramSpec); - free(_returnSpec); - free(_doc); -} +FRT_Method::~FRT_Method() = default; void FRT_Method::SetDocumentation(FRT_Values *values) { - free(_doc); - _docLen = values->GetLength(); - _doc = (char *) malloc(_docLen); - assert(_doc != nullptr); + _doc.resize(values->GetLength()); - FNET_DataBuffer buf(_doc, _docLen); + FNET_DataBuffer buf(&_doc[0], _doc.size()); values->EncodeCopy(&buf); } void FRT_Method::GetDocumentation(FRT_Values *values) { - FNET_DataBuffer buf(_doc, _docLen); - buf.FreeToData(_docLen); - values->DecodeCopy(&buf, _docLen); + FNET_DataBuffer buf(&_doc[0], _doc.size()); + buf.FreeToData(_doc.size()); + values->DecodeCopy(&buf, _doc.size()); } FRT_ReflectionManager::FRT_ReflectionManager() diff --git a/fnet/src/vespa/fnet/frt/reflection.h b/fnet/src/vespa/fnet/frt/reflection.h index c867bbb45ec..6267cafeeb1 100644 --- a/fnet/src/vespa/fnet/frt/reflection.h +++ b/fnet/src/vespa/fnet/frt/reflection.h @@ -3,7 +3,8 @@ #pragma once #include "invokable.h" -#include +#include +#include class FRT_Values; class FRT_Supervisor; @@ -14,20 +15,18 @@ class FRT_Method friend class FRT_ReflectionManager; private: - FRT_Method *_hashNext; // list of methods in hash bucket - FRT_Method *_listNext; // list of all methods - char *_name; // method name - char *_paramSpec; // method parameter spec - char *_returnSpec; // method return spec - FRT_METHOD_PT _method; // method pointer - FRT_Invokable *_handler; // method handler - uint32_t _docLen; // method documentation length - char *_doc; // method documentation - - FRT_Method(const FRT_Method &); - FRT_Method &operator=(const FRT_Method &); + FRT_Method *_hashNext; // list of methods in hash bucket + FRT_Method *_listNext; // list of all methods + std::string _name; // method name + std::string _paramSpec; // method parameter spec + std::string _returnSpec; // method return spec + FRT_METHOD_PT _method; // method pointer + FRT_Invokable *_handler; // method handler + std::vector _doc; // method documentation public: + FRT_Method(const FRT_Method &) = delete; + FRT_Method &operator=(const FRT_Method &) = delete; FRT_Method(const char *name, const char *paramSpec, const char *returnSpec, @@ -37,9 +36,9 @@ public: ~FRT_Method(); FRT_Method *GetNext() { return _listNext; } - const char *GetName() { return _name; } - const char *GetParamSpec() { return _paramSpec; } - const char *GetReturnSpec() { return _returnSpec; } + const char *GetName() { return _name.c_str(); } + const char *GetParamSpec() { return _paramSpec.c_str(); } + const char *GetReturnSpec() { return _returnSpec.c_str(); } FRT_METHOD_PT GetMethod() { return _method; } FRT_Invokable *GetHandler() { return _handler; } void SetDocumentation(FRT_Values *values); diff --git a/fnet/src/vespa/fnet/iocomponent.cpp b/fnet/src/vespa/fnet/iocomponent.cpp index eeda3e12bea..f08718c0c5c 100644 --- a/fnet/src/vespa/fnet/iocomponent.cpp +++ b/fnet/src/vespa/fnet/iocomponent.cpp @@ -12,23 +12,20 @@ FNET_IOComponent::FNET_IOComponent(FNET_TransportThread *owner, : _ioc_next(nullptr), _ioc_prev(nullptr), _ioc_owner(owner), - _ioc_socket_fd(socket_fd), _ioc_selector(nullptr), - _ioc_spec(nullptr), + _ioc_spec(spec), _flags(shouldTimeOut), + _ioc_socket_fd(socket_fd), + _ioc_refcnt(1), _ioc_timestamp(vespalib::steady_clock::now()), _ioc_lock(), - _ioc_cond(), - _ioc_refcnt(1) + _ioc_cond() { - _ioc_spec = strdup(spec); - assert(_ioc_spec != nullptr); } FNET_IOComponent::~FNET_IOComponent() { - free(_ioc_spec); assert(_ioc_selector == nullptr); } diff --git a/fnet/src/vespa/fnet/iocomponent.h b/fnet/src/vespa/fnet/iocomponent.h index 9220b6dfe8f..b4f061e5bc0 100644 --- a/fnet/src/vespa/fnet/iocomponent.h +++ b/fnet/src/vespa/fnet/iocomponent.h @@ -21,9 +21,6 @@ class FNET_IOComponent { friend class FNET_TransportThread; - FNET_IOComponent(const FNET_IOComponent &); - FNET_IOComponent &operator=(const FNET_IOComponent &); - using Selector = vespalib::Selector; struct Flags { @@ -44,16 +41,18 @@ protected: FNET_IOComponent *_ioc_next; // next in list FNET_IOComponent *_ioc_prev; // prev in list FNET_TransportThread *_ioc_owner; // owner(TransportThread) ref. - int _ioc_socket_fd; // source of events. Selector *_ioc_selector; // attached event selector - char *_ioc_spec; // connect/listen spec + std::string _ioc_spec; // connect/listen spec Flags _flags; // Compressed representation of boolean flags; + int _ioc_socket_fd; // source of events. + uint32_t _ioc_refcnt; // reference counter vespalib::steady_time _ioc_timestamp; // last I/O activity std::mutex _ioc_lock; // synchronization std::condition_variable _ioc_cond; // synchronization - uint32_t _ioc_refcnt; // reference counter public: + FNET_IOComponent(const FNET_IOComponent &) = delete; + FNET_IOComponent &operator=(const FNET_IOComponent &) = delete; /** * Construct an IOComponent with the given owner. The socket that @@ -80,7 +79,7 @@ public: /** * @return connect/listen spec **/ - const char *GetSpec() const { return _ioc_spec; } + const char *GetSpec() const { return _ioc_spec.c_str(); } /* * Get a guard to gain exclusive access. -- cgit v1.2.3