From 14dfafde32e0c2c304c6fa51888f95d092fdbb20 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 28 Nov 2022 08:07:04 -0600 Subject: [PATCH] refactor: add std::unique_ptr handler to utils-ev (#4261) --- libtransmission/rpc-server.cc | 2 +- libtransmission/rpc-server.h | 4 ++-- libtransmission/session.h | 4 +++- libtransmission/tr-lpd.cc | 1 + libtransmission/utils-ev.h | 28 ++++++++++++++++++++++++++-- 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/libtransmission/rpc-server.cc b/libtransmission/rpc-server.cc index 8622c3306..5efd84496 100644 --- a/libtransmission/rpc-server.cc +++ b/libtransmission/rpc-server.cc @@ -716,7 +716,7 @@ static void startServer(tr_rpc_server* server) else { evhttp_set_gencb(httpd, handle_request, server); - server->httpd = std::unique_ptr{ httpd, &evhttp_free }; + server->httpd.reset(httpd); tr_logAddInfo(fmt::format(_("Listening for RPC and Web requests on '{address}'"), fmt::arg("address", addr_port_str))); } diff --git a/libtransmission/rpc-server.h b/libtransmission/rpc-server.h index 094f14cd0..4daa7596a 100644 --- a/libtransmission/rpc-server.h +++ b/libtransmission/rpc-server.h @@ -17,6 +17,7 @@ #include "transmission.h" #include "net.h" +#include "utils-ev.h" struct evhttp; struct tr_variant; @@ -155,8 +156,7 @@ public: std::unique_ptr bind_address_; std::unique_ptr start_retry_timer; - std::unique_ptr httpd{ nullptr, [](evhttp*) { - } }; + libtransmission::evhelpers::evhttp_unique_ptr httpd; tr_session* const session; size_t login_attempts_ = 0U; diff --git a/libtransmission/session.h b/libtransmission/session.h index adf25dd93..85eede865 100644 --- a/libtransmission/session.h +++ b/libtransmission/session.h @@ -21,9 +21,11 @@ #include #include #include -#include // std::pair +#include // for std::pair #include +#include // for evutil_socket_t + #include "transmission.h" #include "announce-list.h" diff --git a/libtransmission/tr-lpd.cc b/libtransmission/tr-lpd.cc index 36f0e38ee..ce9f1995f 100644 --- a/libtransmission/tr-lpd.cc +++ b/libtransmission/tr-lpd.cc @@ -18,6 +18,7 @@ #include /* sockaddr_in */ #endif +#include #include #include diff --git a/libtransmission/utils-ev.h b/libtransmission/utils-ev.h index 41c9cbb80..5d3396255 100644 --- a/libtransmission/utils-ev.h +++ b/libtransmission/utils-ev.h @@ -11,8 +11,19 @@ #include -#include -#include +extern "C" +{ + struct evbuffer; + struct event; + struct event_base; + struct evhttp; + + void evbuffer_free(struct evbuffer*); + void event_base_free(struct event_base*); + int event_del(struct event*); + void event_free(struct event*); + void evhttp_free(struct evhttp*); +} namespace libtransmission::evhelpers { @@ -57,4 +68,17 @@ struct EventDeleter using event_unique_ptr = std::unique_ptr; +struct EvhttpDeleter +{ + void operator()(struct evhttp* evh) const noexcept + { + if (evh != nullptr) + { + evhttp_free(evh); + } + } +}; + +using evhttp_unique_ptr = std::unique_ptr; + } // namespace libtransmission::evhelpers