refactor: remove tr_free() (#3668)

This commit is contained in:
Charles Kerr 2022-08-18 09:14:12 -05:00 committed by GitHub
parent 963a8112a1
commit 76a7994eef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 55 additions and 101 deletions

View File

@ -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);
}

View File

@ -26,7 +26,7 @@
#include <libtransmission/rpcimpl.h>
#include <libtransmission/torrent-metainfo.h>
#include <libtransmission/tr-assert.h>
#include <libtransmission/utils.h> // tr_free(), tr_time()
#include <libtransmission/utils.h> // tr_time()
#include <libtransmission/web-utils.h> // tr_urlIsValid()
#include <libtransmission/variant.h>
@ -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<tr_torrent*>{};
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()

View File

@ -3,6 +3,7 @@
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#include <algorithm>
#include <cstdlib> // std::lldiv()
#include <iterator> // std::distance(), std::next(), std::prev()
#include <limits> // std::numeric_limits<size_t>::max()

View File

@ -3,6 +3,7 @@
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#include <algorithm>
#include <string_view>
#include <fmt/format.h>

View File

@ -3,6 +3,7 @@
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#include <algorithm>
#include <cerrno> // for ENOENT
#include <optional>
#include <set>

View File

@ -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;
}
/***

View File

@ -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 "/"

View File

@ -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);
/**
***

View File

@ -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);
}
}
/**
***
**/

View File

@ -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<struct_type*>(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);
/***
****
***/

View File

@ -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<tr_torrent*>{};
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)
{

View File

@ -24,7 +24,7 @@
#include <libtransmission/transmission.h>
#include <libtransmission/session-id.h>
#include <libtransmission/utils.h> // tr_free
#include <libtransmission/utils.h>
#include <libtransmission/variant.h>
#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);
}