refactor: remove unused macros (#2897)

* refactor: remove unused fu nction tr_lowerBound()

* refactor: tr_timerAdd() takes a reference, not a pointer

* refactor: tr_timerAddMsec() takes a reference, not a pointer

* refactor: tr_moveFile() takes a reference, not a pointer

* refactor: tr_wildmat() takes std::string_views

* refactor: remove unused macro TR_GNUC_NONNULL

* refactor: remove unused macro TR_GNUC_PRINTF

* refactor: remove unused macro TR_GNUC_NULL_TERMINATED

* refactor: remove unused macro TR_GNUC_HOT

* refactor: remove unused macro __has_feature

* refactor: remove unused macro __has_attribute

* refactor: remove unused macro TR_DEPRECATED

* refactor: remove unused macro TR_DISABLE_COPY_MOVE
This commit is contained in:
Charles Kerr 2022-04-07 19:20:29 -05:00 committed by GitHub
parent 31c65eec1f
commit b256da02e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 49 additions and 169 deletions

View File

@ -174,7 +174,7 @@ struct tr_announcer
void scheduleNextUpdate() const
{
tr_timerAddMsec(this->upkeep_timer, UpkeepIntervalMsec);
tr_timerAddMsec(*this->upkeep_timer, UpkeepIntervalMsec);
}
std::set<tr_announce_request*, StopsCompare> stops;

View File

@ -1201,7 +1201,7 @@ tr_handshake* tr_handshakeNew(
handshake->done_func_user_data = done_func_user_data;
handshake->session = session;
handshake->timeout_timer = evtimer_new(session->event_base, handshakeTimeout, handshake);
tr_timerAdd(handshake->timeout_timer, HANDSHAKE_TIMEOUT_SEC, 0);
tr_timerAdd(*handshake->timeout_timer, HANDSHAKE_TIMEOUT_SEC, 0);
tr_peerIoRef(io); /* balanced by the unref in tr_handshakeFree */
tr_peerIoSetIOFuncs(handshake->io, canRead, nullptr, gotError, handshake);

View File

@ -641,7 +641,7 @@ static void refillUpkeep(evutil_socket_t /*fd*/, short /*what*/, void* vmgr)
auto& torrents = mgr->session->torrents();
std::for_each(std::begin(torrents), std::end(torrents), [](auto* tor) { tr_swarmCancelOldRequests(tor->swarm); });
tr_timerAddMsec(mgr->refillUpkeepTimer, RefillUpkeepPeriodMsec);
tr_timerAddMsec(*mgr->refillUpkeepTimer, RefillUpkeepPeriodMsec);
}
static void addStrike(tr_swarm* s, tr_peer* peer)
@ -1362,7 +1362,7 @@ static void reconnectPulse(evutil_socket_t, short /*unused*/, void* /*vmgr*/);
static struct event* createTimer(tr_session* session, int msec, event_callback_fn callback, void* cbdata)
{
struct event* timer = evtimer_new(session->event_base, callback, cbdata);
tr_timerAddMsec(timer, msec);
tr_timerAddMsec(*timer, msec);
return timer;
}
@ -1402,7 +1402,7 @@ void tr_peerMgrStartTorrent(tr_torrent* tor)
s->maxPeers = tor->maxConnectedPeers;
// rechoke soon
tr_timerAddMsec(s->manager->rechokeTimer, 100);
tr_timerAddMsec(*s->manager->rechokeTimer, 100);
}
static void removeAllPeers(tr_swarm* /*swarm*/);
@ -2225,7 +2225,7 @@ static void rechokePulse(evutil_socket_t /*fd*/, short /*what*/, void* vmgr)
}
}
tr_timerAddMsec(mgr->rechokeTimer, RechokePeriodMsec);
tr_timerAddMsec(*mgr->rechokeTimer, RechokePeriodMsec);
}
/***
@ -2617,7 +2617,7 @@ static void bandwidthPulse(evutil_socket_t /*fd*/, short /*what*/, void* vmgr)
reconnectPulse(0, 0, mgr);
tr_timerAddMsec(mgr->bandwidthTimer, BandwidthPeriodMsec);
tr_timerAddMsec(*mgr->bandwidthTimer, BandwidthPeriodMsec);
}
/***
@ -2754,7 +2754,7 @@ static void atomPulse(evutil_socket_t /*fd*/, short /*what*/, void* vmgr)
}
}
tr_timerAddMsec(mgr->atomTimer, AtomPeriodMsec);
tr_timerAddMsec(*mgr->atomTimer, AtomPeriodMsec);
}
/***

View File

@ -243,7 +243,7 @@ public:
if (torrent->allowsPex())
{
pex_timer.reset(evtimer_new(torrent->session->event_base, pexPulse, this));
tr_timerAdd(pex_timer.get(), PexIntervalSecs, 0);
tr_timerAdd(*pex_timer, PexIntervalSecs, 0);
}
if (tr_peerIoSupportsUTP(io))
@ -2683,5 +2683,5 @@ static void pexPulse(evutil_socket_t /*fd*/, short /*what*/, void* vmsgs)
sendPex(msgs);
TR_ASSERT(msgs->pex_timer);
tr_timerAdd(msgs->pex_timer.get(), PexIntervalSecs, 0);
tr_timerAdd(*msgs->pex_timer, PexIntervalSecs, 0);
}

View File

@ -143,7 +143,7 @@ static void set_evtimer_from_status(tr_shared* s)
if (s->timer != nullptr)
{
tr_timerAdd(s->timer, sec, msec);
tr_timerAdd(*s->timer, sec, msec);
}
}

View File

@ -13,44 +13,6 @@
static auto constexpr Floor = int{ 32 };
int tr_lowerBound(
void const* key,
void const* base,
size_t nmemb,
size_t size,
tr_voidptr_compare_func compar,
bool* exact_match)
{
size_t first = 0;
auto const* cbase = static_cast<char const*>(base);
bool exact = false;
while (nmemb != 0)
{
size_t const half = nmemb / 2;
size_t const middle = first + half;
int const c = (*compar)(key, cbase + size * middle);
if (c <= 0)
{
if (c == 0)
{
exact = true;
}
nmemb = half;
}
else
{
first = middle + 1;
nmemb = nmemb - half - 1;
}
}
*exact_match = exact;
return first;
}
void tr_ptrArrayDestruct(tr_ptrArray* p, PtrArrayForeachFunc func)
{
TR_ASSERT(p != nullptr);

View File

@ -98,13 +98,4 @@ void tr_ptrArrayRemoveSortedPointer(tr_ptrArray* t, void const* ptr, tr_voidptr_
@return the matching pointer, or nullptr if no match was found */
void* tr_ptrArrayFindSorted(tr_ptrArray* array, void const* key, tr_voidptr_compare_func compare);
/** @brief similar to bsearch() but returns the index of the lower bound */
int tr_lowerBound(
void const* key,
void const* base,
size_t nmemb,
size_t size,
tr_voidptr_compare_func compar,
bool* exact_match) TR_GNUC_HOT TR_GNUC_NONNULL(1, 5, 6);
/* @} */

View File

@ -369,7 +369,7 @@ static bool isAddressAllowed(tr_rpc_server const* server, char const* address)
auto const& src = server->whitelist;
return !server->isWhitelistEnabled ||
std::any_of(std::begin(src), std::end(src), [&address](auto const& s) { return tr_wildmat(address, s.c_str()); });
std::any_of(std::begin(src), std::end(src), [&address](auto const& s) { return tr_wildmat(address, s); });
}
static bool isIPAddressWithOptionalPort(char const* host)
@ -709,7 +709,7 @@ static int rpc_server_start_retry(tr_rpc_server* server)
server->start_retry_timer = evtimer_new(server->session->event_base, rpc_server_on_start_retry, server);
}
tr_timerAdd(server->start_retry_timer, retry_delay, 0);
tr_timerAdd(*server->start_retry_timer, retry_delay, 0);
++server->start_retry_counter;
return retry_delay;

View File

@ -574,7 +574,7 @@ static void onSaveTimer(evutil_socket_t /*fd*/, short /*what*/, void* vsession)
tr_statsSaveDirty(session);
tr_timerAdd(session->saveTimer, SaveIntervalSecs, 0);
tr_timerAdd(*session->saveTimer, SaveIntervalSecs, 0);
}
/***
@ -692,7 +692,7 @@ static void onNowTimer(evutil_socket_t /*fd*/, short /*what*/, void* vsession)
int constexpr Max = 999999;
int const usec = std::clamp(int(1000000 - tv.tv_usec), Min, Max);
tr_timerAdd(session->nowTimer, 0, usec);
tr_timerAdd(*session->nowTimer, 0, usec);
}
static void loadBlocklists(tr_session* session);
@ -745,7 +745,7 @@ static void tr_sessionInitImpl(init_data* data)
TR_ASSERT(tr_isSession(session));
session->saveTimer = evtimer_new(session->event_base, onSaveTimer, session);
tr_timerAdd(session->saveTimer, SaveIntervalSecs, 0);
tr_timerAdd(*session->saveTimer, SaveIntervalSecs, 0);
tr_announcerInit(session);
@ -1876,7 +1876,7 @@ static void sessionCloseImplStart(tr_session* session)
/* saveTimer is not used at this point, reusing for UDP shutdown wait */
TR_ASSERT(session->saveTimer == nullptr);
session->saveTimer = evtimer_new(session->event_base, sessionCloseImplWaitForIdleUdp, session);
tr_timerAdd(session->saveTimer, 0, 0);
tr_timerAdd(*session->saveTimer, 0, 0);
}
static void sessionCloseImplFinish(tr_session* session);
@ -1892,7 +1892,7 @@ static void sessionCloseImplWaitForIdleUdp(evutil_socket_t /*fd*/, short /*what*
if (!tr_tracker_udp_is_idle(session))
{
tr_tracker_udp_upkeep(session);
tr_timerAdd(session->saveTimer, 0, 100000);
tr_timerAdd(*session->saveTimer, 0, 100000);
return;
}

View File

@ -2265,7 +2265,7 @@ static void deleteLocalData(tr_torrent const* tor, tr_fileFunc func)
if (!std::empty(filename))
{
auto target = tr_strvPath(tmpdir, tor->fileSubpath(f));
tr_moveFile(filename.c_str(), target.c_str(), nullptr);
tr_moveFile(filename, target);
files.emplace_back(target);
}
}

View File

@ -373,7 +373,7 @@ int tr_dhtInit(tr_session* ss)
std::thread(dht_bootstrap, cl).detach();
dht_timer = evtimer_new(session_->event_base, timer_callback, session_);
tr_timerAdd(dht_timer, 0, tr_rand_int_weak(1000000));
tr_timerAdd(*dht_timer, 0, tr_rand_int_weak(1000000));
tr_logAddDebug("DHT initialized");
@ -772,7 +772,7 @@ void tr_dhtCallback(unsigned char* buf, int buflen, struct sockaddr* from, sockl
/* Being slightly late is fine,
and has the added benefit of adding some jitter. */
tr_timerAdd(dht_timer, (int)tosleep, tr_rand_int_weak(1000000));
tr_timerAdd(*dht_timer, (int)tosleep, tr_rand_int_weak(1000000));
}
static void timer_callback(evutil_socket_t /*s*/, short /*type*/, void* session)

View File

@ -362,7 +362,7 @@ int tr_lpdInit(tr_session* ss, tr_address* /*tr_addr*/)
event_add(lpd_event, nullptr);
upkeep_timer = evtimer_new(ss->event_base, on_upkeep_timer, ss);
tr_timerAdd(upkeep_timer, UpkeepIntervalSecs, 0);
tr_timerAdd(*upkeep_timer, UpkeepIntervalSecs, 0);
tr_logAddDebug("Local Peer Discovery initialised");
@ -622,7 +622,7 @@ static void on_upkeep_timer(evutil_socket_t /*s*/, short /*type*/, void* /*user_
{
time_t const now = tr_time();
tr_lpdAnnounceMore(now, UpkeepIntervalSecs);
tr_timerAdd(upkeep_timer, UpkeepIntervalSecs, 0);
tr_timerAdd(*upkeep_timer, UpkeepIntervalSecs, 0);
}
/**

View File

@ -13,14 +13,6 @@
****
***/
#ifndef __has_feature
#define __has_feature(x) 0
#endif
#ifndef __has_extension
#define __has_extension __has_feature
#endif
#ifndef __has_attribute
#define __has_attribute(x) 0
#endif
@ -61,54 +53,16 @@
#define TR_UNLIKELY(x) (x)
#endif
#define TR_DISABLE_COPY(Class) \
Class(Class const&) = delete; \
Class& operator=(Class const&) = delete;
#define TR_DISABLE_MOVE(Class) \
Class(Class&&) = delete; \
Class& operator=(Class&&) = delete;
#define TR_DISABLE_COPY_MOVE(Class) \
TR_DISABLE_COPY(Class) \
TR_DISABLE_MOVE(Class)
Class& operator=(Class const&) = delete; \
Class& operator=(Class&&) = delete; \
Class(Class const&) = delete; \
Class(Class&&) = delete;
/***
****
***/
#if __has_attribute(__deprecated__) || TR_GNUC_CHECK_VERSION(3, 1)
#define TR_DEPRECATED __attribute__((__deprecated__))
#elif defined(_MSC_VER)
#define TR_DEPRECATED __declspec(deprecated)
#else
#define TR_DEPRECATED
#endif
#if __has_attribute(__format__) || TR_GNUC_CHECK_VERSION(2, 3)
#define TR_GNUC_PRINTF(fmt, args) __attribute__((__format__(printf, fmt, args)))
#else
#define TR_GNUC_PRINTF(fmt, args)
#endif
#if __has_attribute(__nonnull__) || TR_GNUC_CHECK_VERSION(3, 3)
#define TR_GNUC_NONNULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
#else
#define TR_GNUC_NONNULL(...)
#endif
#if __has_attribute(__sentinel__) || TR_GNUC_CHECK_VERSION(4, 3)
#define TR_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
#else
#define TR_GNUC_NULL_TERMINATED
#endif
#if __has_attribute(__hot__) || TR_GNUC_CHECK_VERSION(4, 3)
#define TR_GNUC_HOT __attribute__((__hot__))
#else
#define TR_GNUC_HOT
#endif
#if __has_attribute(__malloc__) || TR_GNUC_CHECK_VERSION(2, 96)
#define TR_GNUC_MALLOC __attribute__((__malloc__))
#else

View File

@ -141,7 +141,7 @@ static void reset_timer(tr_session* ss)
usec = tr_rand_int_weak(1000000);
}
tr_timerAdd(ss->utp_timer, sec, usec);
tr_timerAdd(*ss->utp_timer, sec, usec);
}
static void timer_callback(evutil_socket_t /*s*/, short /*type*/, void* vsession)

View File

@ -160,7 +160,7 @@ void* tr_memdup(void const* src, size_t byteCount)
****
***/
void tr_timerAdd(struct event* timer, int seconds, int microseconds)
void tr_timerAdd(struct event& timer, int seconds, int microseconds)
{
auto tv = timeval{};
tv.tv_sec = seconds;
@ -170,10 +170,10 @@ void tr_timerAdd(struct event* timer, int seconds, int microseconds)
TR_ASSERT(tv.tv_usec >= 0);
TR_ASSERT(tv.tv_usec < 1000000);
evtimer_add(timer, &tv);
evtimer_add(&timer, &tv);
}
void tr_timerAddMsec(struct event* timer, int msec)
void tr_timerAddMsec(struct event& timer, int msec)
{
int const seconds = msec / 1000;
int const usec = (msec % 1000) * 1000;
@ -397,9 +397,11 @@ extern "C"
}
/* User-level routine. returns whether or not 'text' and 'p' matched */
bool tr_wildmat(char const* text, char const* p)
bool tr_wildmat(std::string_view text, std::string_view pattern)
{
return (p[0] == '*' && p[1] == '\0') || (DoMatch(text, p) != 0);
// TODO(ckerr): replace wildmat with base/strings/pattern.cc
// wildmat wants these to be zero-terminated.
return pattern == "*"sv || DoMatch(std::string{ text }.c_str(), std::string{ pattern }.c_str()) != 0;
}
char const* tr_strerror(int i)
@ -999,17 +1001,18 @@ std::string tr_strratio(double ratio, char const* infinity)
****
***/
bool tr_moveFile(char const* oldpath, char const* newpath, tr_error** error)
bool tr_moveFile(std::string_view oldpath_in, std::string_view newpath_in, tr_error** error)
{
tr_sys_path_info info;
auto const oldpath = tr_pathbuf{ oldpath_in };
auto const newpath = tr_pathbuf{ newpath_in };
/* make sure the old file exists */
// make sure the old file exists
auto info = tr_sys_path_info{};
if (!tr_sys_path_get_info(oldpath, 0, &info, error))
{
tr_error_prefix(error, "Unable to get information on old file: ");
return false;
}
if (info.type != TR_SYS_PATH_IS_FILE)
{
tr_error_set(error, TR_ERROR_EINVAL, "Old path does not point to a file."sv);
@ -1017,14 +1020,11 @@ bool tr_moveFile(char const* oldpath, char const* newpath, tr_error** error)
}
// ensure the target directory exists
if (auto const newdir = tr_sys_path_dirname(newpath, error);
std::empty(newdir) || !tr_sys_dir_create(newdir.c_str(), TR_SYS_DIR_CREATE_PARENTS, 0777, error))
{
auto const newdir = tr_sys_path_dirname(newpath, error);
bool const i = !std::empty(newdir) && tr_sys_dir_create(newdir.c_str(), TR_SYS_DIR_CREATE_PARENTS, 0777, error);
if (!i)
{
tr_error_prefix(error, "Unable to create directory for new file: ");
return false;
}
tr_error_prefix(error, "Unable to create directory for new file: ");
return false;
}
/* they might be on the same filesystem... */

View File

@ -71,7 +71,7 @@ struct tr_error;
* @brief Rich Salz's classic implementation of shell-style pattern matching for ?, \, [], and * characters.
* @return 1 if the pattern matches, 0 if it doesn't, or -1 if an error occured
*/
[[nodiscard]] bool tr_wildmat(char const* text, char const* pattern) TR_GNUC_NONNULL(1, 2);
[[nodiscard]] bool tr_wildmat(std::string_view text, std::string_view pattern);
/**
* @brief Loads a file and returns its contents.
@ -114,14 +114,14 @@ tr_disk_space tr_dirSpace(std::string_view path);
* @param seconds seconds to wait
* @param microseconds microseconds to wait
*/
void tr_timerAdd(struct event* timer, int seconds, int microseconds) TR_GNUC_NONNULL(1);
void tr_timerAdd(struct event& timer, int seconds, int microseconds);
/**
* @brief Convenience wrapper around timer_add() to have a timer wake up in a number of milliseconds
* @param timer the timer to set
* @param milliseconds milliseconds to wait
*/
void tr_timerAddMsec(struct event* timer, int milliseconds) TR_GNUC_NONNULL(1);
void tr_timerAddMsec(struct event& timer, int milliseconds);
/** @brief return the current date in milliseconds */
uint64_t tr_time_msec();
@ -412,7 +412,7 @@ struct timeval tr_gettimeofday();
* @brief move a file
* @return `True` on success, `false` otherwise (with `error` set accordingly).
*/
bool tr_moveFile(char const* oldpath, char const* newpath, struct tr_error** error) TR_GNUC_NONNULL(1, 2);
bool tr_moveFile(std::string_view oldpath, std::string_view newpath, struct tr_error** error = nullptr);
/** @brief convenience function to remove an item from an array */
void tr_removeElementFromArray(void* array, size_t index_to_remove, size_t sizeof_element, size_t nmemb);

View File

@ -260,7 +260,7 @@ private:
void startTimer()
{
tr_timerAddMsec(pulse_timer.get(), IdleTimerMsec);
tr_timerAddMsec(*pulse_timer, IdleTimerMsec);
}
static void onTimer(evutil_socket_t /*fd*/, short /*what*/, void* vwebseed)

View File

@ -206,33 +206,6 @@ TEST_F(UtilsTest, trParseNumberRange)
EXPECT_EQ(empty_string, tostring(numbers));
}
namespace
{
int compareInts(void const* va, void const* vb) noexcept
{
auto const a = *static_cast<int const*>(va);
auto const b = *static_cast<int const*>(vb);
return a - b;
}
} // namespace
TEST_F(UtilsTest, lowerbound)
{
auto const a = std::array<int, 7>{ 1, 2, 3, 3, 3, 5, 8 };
auto const expected_pos = std::array<int, 10>{ 0, 1, 2, 5, 5, 6, 6, 6, 7, 7 };
auto const expected_exact = std::array<bool, 10>{ true, true, true, false, true, false, false, true, false, false };
for (int i = 1; i <= 10; i++)
{
bool exact;
auto const pos = tr_lowerBound(&i, a.data(), a.size(), sizeof(int), compareInts, &exact);
EXPECT_EQ(expected_pos[i - 1], pos);
EXPECT_EQ(expected_exact[i - 1], exact);
}
}
TEST_F(UtilsTest, trStrlower)
{
EXPECT_EQ(""sv, tr_strlower(""sv));