From 76a7994eef0eeeebaac236b6cc951a1bcae5044b Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 18 Aug 2022 09:14:12 -0500 Subject: [PATCH] refactor: remove tr_free() (#3668) --- daemon/daemon.cc | 4 +--- gtk/Session.cc | 26 ++++++++++++-------------- libtransmission/cache.cc | 1 + libtransmission/error.cc | 1 + libtransmission/makemeta.cc | 1 + libtransmission/session.cc | 34 ++++++++++++++++------------------ libtransmission/tr-macros.h | 14 -------------- libtransmission/transmission.h | 20 ++++++++++++++++---- libtransmission/utils.cc | 22 ---------------------- libtransmission/utils.h | 20 -------------------- macosx/Controller.mm | 9 +++++---- qt/Session.cc | 4 ++-- 12 files changed, 55 insertions(+), 101 deletions(-) diff --git a/daemon/daemon.cc b/daemon/daemon.cc index 4cd4e0971..f9966faef 100644 --- a/daemon/daemon.cc +++ b/daemon/daemon.cc @@ -811,7 +811,6 @@ static int daemon_start(void* varg, [[maybe_unused]] bool foreground) /* load the torrents */ { - tr_torrent** torrents; tr_ctor* ctor = tr_ctorNew(mySession); if (arg->paused) @@ -819,8 +818,7 @@ static int daemon_start(void* varg, [[maybe_unused]] bool foreground) tr_ctorSetPaused(ctor, TR_FORCE, true); } - torrents = tr_sessionLoadTorrents(mySession, ctor, nullptr); - tr_free(torrents); + tr_sessionLoadTorrents(mySession, ctor); tr_ctorFree(ctor); } diff --git a/gtk/Session.cc b/gtk/Session.cc index 6f1641120..1d4ebe458 100644 --- a/gtk/Session.cc +++ b/gtk/Session.cc @@ -26,7 +26,7 @@ #include #include #include -#include // tr_free(), tr_time() +#include // tr_time() #include // tr_urlIsValid() #include @@ -1269,32 +1269,30 @@ void Session::remove_torrent(tr_torrent_id_t id, bool delete_local_data) } } -void Session::load(bool forcePaused) +void Session::load(bool force_paused) { - tr_ctor* ctor; - tr_torrent** torrents; - int count = 0; + auto* const ctor = tr_ctorNew(impl_->get_session()); - ctor = tr_ctorNew(impl_->get_session()); - - if (forcePaused) + if (force_paused) { tr_ctorSetPaused(ctor, TR_FORCE, true); } tr_ctorSetPeerLimit(ctor, TR_FALLBACK, gtr_pref_int_get(TR_KEY_peer_limit_per_torrent)); - torrents = tr_sessionLoadTorrents(impl_->get_session(), ctor, &count); + auto* session = impl_->get_session(); + auto const n_torrents = tr_sessionLoadTorrents(session, ctor); + tr_ctorFree(ctor); ScopedModelSortBlocker disable_sort(*gtr_get_ptr(impl_->get_model())); - for (int i = 0; i < count; ++i) + auto torrents = std::vector{}; + torrents.resize(n_torrents); + tr_sessionGetAllTorrents(session, std::data(torrents), std::size(torrents)); + for (auto* tor : torrents) { - impl_->add_torrent(torrents[i], false); + impl_->add_torrent(tor, false); } - - tr_free(torrents); - tr_ctorFree(ctor); } void Session::clear() diff --git a/libtransmission/cache.cc b/libtransmission/cache.cc index 4549e96cc..f25985eb4 100644 --- a/libtransmission/cache.cc +++ b/libtransmission/cache.cc @@ -3,6 +3,7 @@ // or any future license endorsed by Mnemosyne LLC. // License text can be found in the licenses/ folder. +#include #include // std::lldiv() #include // std::distance(), std::next(), std::prev() #include // std::numeric_limits::max() diff --git a/libtransmission/error.cc b/libtransmission/error.cc index ff822551f..c471c5093 100644 --- a/libtransmission/error.cc +++ b/libtransmission/error.cc @@ -3,6 +3,7 @@ // or any future license endorsed by Mnemosyne LLC. // License text can be found in the licenses/ folder. +#include #include #include diff --git a/libtransmission/makemeta.cc b/libtransmission/makemeta.cc index 3426831b9..1059961f5 100644 --- a/libtransmission/makemeta.cc +++ b/libtransmission/makemeta.cc @@ -3,6 +3,7 @@ // or any future license endorsed by Mnemosyne LLC. // License text can be found in the licenses/ folder. +#include #include // for ENOENT #include #include diff --git a/libtransmission/session.cc b/libtransmission/session.cc index b29468bc7..83d42c9ab 100644 --- a/libtransmission/session.cc +++ b/libtransmission/session.cc @@ -1891,8 +1891,6 @@ struct sessionLoadTorrentsData { tr_session* session; tr_ctor* ctor; - int* setmeCount; - tr_torrent** torrents; bool done; }; @@ -1935,41 +1933,41 @@ static void sessionLoadTorrents(struct sessionLoadTorrentsData* const data) tr_sys_dir_close(odir); } - int const n = std::size(torrents); - data->torrents = tr_new(tr_torrent*, n); // NOLINT(bugprone-sizeof-expression) - std::copy(std::begin(torrents), std::end(torrents), data->torrents); - - if (n != 0) + if (auto const n = std::size(torrents); n != 0U) { tr_logAddInfo(fmt::format(ngettext("Loaded {count} torrent", "Loaded {count} torrents", n), fmt::arg("count", n))); } - if (data->setmeCount != nullptr) - { - *data->setmeCount = n; - } - data->done = true; } -tr_torrent** tr_sessionLoadTorrents(tr_session* session, tr_ctor* ctor, int* setmeCount) +size_t tr_sessionLoadTorrents(tr_session* session, tr_ctor* ctor) { struct sessionLoadTorrentsData data; data.session = session; data.ctor = ctor; - data.setmeCount = setmeCount; - data.torrents = nullptr; data.done = false; - tr_runInEventThread(session, sessionLoadTorrents, &data); - while (!data.done) { tr_wait_msec(100); } - return data.torrents; + return std::size(session->torrents()); +} + +size_t tr_sessionGetAllTorrents(tr_session* session, tr_torrent** buf, size_t buflen) +{ + auto& torrents = session->torrents(); + auto const n = std::size(torrents); + + if (buflen >= n) + { + std::copy_n(std::begin(torrents), n, buf); + } + + return n; } /*** diff --git a/libtransmission/tr-macros.h b/libtransmission/tr-macros.h index 40e685a8f..31a43b27f 100644 --- a/libtransmission/tr-macros.h +++ b/libtransmission/tr-macros.h @@ -12,10 +12,6 @@ **** ***/ -#ifndef __has_attribute -#define __has_attribute(x) 0 -#endif - #ifndef __has_builtin #define __has_builtin(x) 0 #endif @@ -62,16 +58,6 @@ **** ***/ -#if __has_attribute(__malloc__) || TR_GNUC_CHECK_VERSION(2, 96) -#define TR_GNUC_MALLOC __attribute__((__malloc__)) -#else -#define TR_GNUC_MALLOC -#endif - -/*** -**** -***/ - #define TR_PATH_DELIMITER '/' #define TR_PATH_DELIMITER_STR "/" diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h index 641e71ca5..71ded48c9 100644 --- a/libtransmission/transmission.h +++ b/libtransmission/transmission.h @@ -710,11 +710,23 @@ void tr_sessionSetQueueStartCallback(tr_session*, void (*callback)(tr_session*, ***/ /** - * Load all the torrents in the session's torrent folder. - * This can be used at startup to kickstart all the torrents - * from the previous session. + * Load all the torrents in the session's torrent folder. + * This can be used at startup to kickstart all the torrents + * from the previous session. + * + * @return the number of torrents in the session */ -tr_torrent** tr_sessionLoadTorrents(tr_session* session, tr_ctor* ctor, int* setmeCount); +size_t tr_sessionLoadTorrents(tr_session* session, tr_ctor* ctor); + +/** + * Get pointers to all the torrents in a session. + * + * Iff `buflen` is large enough to hold the torrents pointers, + * then all of them are copied into `buf`. + * + * @return the number of torrents in the session + */ +size_t tr_sessionGetAllTorrents(tr_session* session, tr_torrent** buf, size_t buflen); /** *** diff --git a/libtransmission/utils.cc b/libtransmission/utils.cc index e56e84706..3e7e3487e 100644 --- a/libtransmission/utils.cc +++ b/libtransmission/utils.cc @@ -60,28 +60,6 @@ using namespace std::literals; time_t __tr_current_time = 0; -/*** -**** -***/ - -void* tr_malloc(size_t size) -{ - return size != 0 ? malloc(size) : nullptr; -} - -void* tr_malloc0(size_t size) -{ - return size != 0 ? calloc(1, size) : nullptr; -} - -void tr_free(void* p) -{ - if (p != nullptr) - { - free(p); - } -} - /** *** **/ diff --git a/libtransmission/utils.h b/libtransmission/utils.h index 7453f54fc..9f2bded14 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -122,17 +122,6 @@ int tr_main_win32(int argc, char** argv, int (*real_main)(int, char**)); **** ***/ -/** @brief Portability wrapper around malloc() in which `0' is a safe argument */ -void* tr_malloc(size_t size); - -/** @brief Portability wrapper around calloc() in which `0' is a safe argument */ -void* tr_malloc0(size_t size); - -/** @brief Portability wrapper around free() in which `nullptr' is a safe argument */ -void tr_free(void* p); - -#define tr_new(struct_type, n_structs) (static_cast(tr_malloc(sizeof(struct_type) * (size_t)(n_structs)))) - constexpr bool tr_str_is_empty(char const* value) { return value == nullptr || *value == '\0'; @@ -228,15 +217,6 @@ constexpr bool tr_strvSep(std::string_view* sv, std::string_view* token, char de */ size_t tr_strvToBuf(std::string_view src, char* buf, size_t buflen); -/** - * @brief copies `src` into `buf`. - * - * - Always returns std::size(src). - * - `src` will be copied into `buf` iff `buflen >= std::size(src)` - * - `buf` will also be zero terminated iff `buflen >= std::size(src) + 1`. - */ -size_t tr_strvToBuf(std::string_view src, char* buf, size_t buflen); - /*** **** ***/ diff --git a/macosx/Controller.mm b/macosx/Controller.mm index 51948258e..f2609144a 100644 --- a/macosx/Controller.mm +++ b/macosx/Controller.mm @@ -708,14 +708,15 @@ void onTorrentCompletenessChanged(tr_torrent* tor, tr_completeness status, bool //load previous transfers tr_ctor* ctor = tr_ctorNew(session); tr_ctorSetPaused(ctor, TR_FORCE, true); // paused by default; unpause below after checking state history - int n_torrents = 0; - tr_torrent** loaded_torrents = tr_sessionLoadTorrents(session, ctor, &n_torrents); + auto const n_torrents = tr_sessionLoadTorrents(session, ctor); tr_ctorFree(ctor); // process the loaded torrents - for (int i = 0; i < n_torrents; ++i) + auto torrents = std::vector{}; + torrents.resize(n_torrents); + tr_sessionGetAllTorrents(session, std::data(torrents), std::size(torrents)); + for (auto* tor : torrents) { - struct tr_torrent* tor = loaded_torrents[i]; NSString* location; if (tr_torrentGetDownloadDir(tor) != NULL) { diff --git a/qt/Session.cc b/qt/Session.cc index b8d7f3faf..7c0deba84 100644 --- a/qt/Session.cc +++ b/qt/Session.cc @@ -24,7 +24,7 @@ #include #include -#include // tr_free +#include #include #include "Session.h" @@ -357,7 +357,7 @@ void Session::start() rpc_.start(session_); auto* const ctor = tr_ctorNew(session_); - tr_free(tr_sessionLoadTorrents(session_, ctor, nullptr)); + tr_sessionLoadTorrents(session_, ctor); tr_ctorFree(ctor); }