From e03bc8e5bca9a27b6d16607f1eff4e6f1306b707 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 29 Sep 2021 12:25:52 -0500 Subject: [PATCH] refactor: replace tr_ptrArray with std::unordered_set in tr_watchdir_scan() (#1854) * replace tr_ptrArray with std:: in tr_watchdir_scan --- libtransmission/watchdir-common.h | 7 ++++--- libtransmission/watchdir-generic.cc | 15 ++++++--------- libtransmission/watchdir-kqueue.cc | 14 ++++++-------- libtransmission/watchdir.cc | 11 +++++------ tests/libtransmission/watchdir-test.cc | 2 ++ 5 files changed, 23 insertions(+), 26 deletions(-) diff --git a/libtransmission/watchdir-common.h b/libtransmission/watchdir-common.h index 34c6dab81..f3d807848 100644 --- a/libtransmission/watchdir-common.h +++ b/libtransmission/watchdir-common.h @@ -12,14 +12,15 @@ #error only the libtransmission watchdir module should #include this header. #endif -struct tr_ptrArray; +#include +#include typedef struct tr_watchdir_backend { void (*free_func)(struct tr_watchdir_backend*); } tr_watchdir_backend; -#define BACKEND_DOWNCAST(b) ((tr_watchdir_backend*)(b)) +#define BACKEND_DOWNCAST(b) (reinterpret_cast(b)) /* ... */ @@ -31,7 +32,7 @@ struct event_base* tr_watchdir_get_event_base(tr_watchdir_t handle); void tr_watchdir_process(tr_watchdir_t handle, char const* name); -void tr_watchdir_scan(tr_watchdir_t handle, struct tr_ptrArray* dir_entries); +void tr_watchdir_scan(tr_watchdir_t handle, std::unordered_set* dir_entries); /* ... */ diff --git a/libtransmission/watchdir-generic.cc b/libtransmission/watchdir-generic.cc index 1a5b2d606..b51fca0e6 100644 --- a/libtransmission/watchdir-generic.cc +++ b/libtransmission/watchdir-generic.cc @@ -7,6 +7,8 @@ */ #include +#include +#include #include @@ -14,7 +16,6 @@ #include "transmission.h" #include "log.h" -#include "ptrarray.h" #include "tr-assert.h" #include "utils.h" #include "watchdir.h" @@ -37,10 +38,10 @@ typedef struct tr_watchdir_generic tr_watchdir_backend base; struct event* event; - tr_ptrArray dir_entries; + std::unordered_set dir_entries; } tr_watchdir_generic; -#define BACKEND_UPCAST(b) ((tr_watchdir_generic*)(b)) +#define BACKEND_UPCAST(b) (reinterpret_cast(b)) /* Non-static and mutable for unit tests. default to 10 sec. */ auto tr_watchdir_generic_interval = timeval{ 10, 0 }; @@ -77,16 +78,12 @@ static void tr_watchdir_generic_free(tr_watchdir_backend* backend_base) event_free(backend->event); } - tr_ptrArrayDestruct(&backend->dir_entries, &tr_free); - - tr_free(backend); + delete backend; } tr_watchdir_backend* tr_watchdir_generic_new(tr_watchdir_t handle) { - tr_watchdir_generic* backend; - - backend = tr_new0(tr_watchdir_generic, 1); + auto* backend = new tr_watchdir_generic{}; backend->base.free_func = &tr_watchdir_generic_free; if ((backend diff --git a/libtransmission/watchdir-kqueue.cc b/libtransmission/watchdir-kqueue.cc index 4806fe91a..0ecc4dce3 100644 --- a/libtransmission/watchdir-kqueue.cc +++ b/libtransmission/watchdir-kqueue.cc @@ -8,6 +8,8 @@ #include #include /* strcmp() */ +#include +#include #include /* open() */ #include /* close() */ @@ -25,7 +27,6 @@ #include "transmission.h" #include "log.h" -#include "ptrarray.h" #include "tr-assert.h" #include "utils.h" #include "watchdir.h" @@ -50,10 +51,10 @@ typedef struct tr_watchdir_kqueue int kq; int dirfd; struct event* event; - tr_ptrArray dir_entries; + std::unordered_set dir_entries; } tr_watchdir_kqueue; -#define BACKEND_UPCAST(b) ((tr_watchdir_kqueue*)(b)) +#define BACKEND_UPCAST(b) (reinterpret_cast(b)) #define KQUEUE_WATCH_MASK (NOTE_WRITE | NOTE_EXTEND) @@ -108,18 +109,15 @@ static void tr_watchdir_kqueue_free(tr_watchdir_backend* backend_base) close(backend->dirfd); } - tr_ptrArrayDestruct(&backend->dir_entries, &tr_free); - - tr_free(backend); + delete backend; } tr_watchdir_backend* tr_watchdir_kqueue_new(tr_watchdir_t handle) { char const* const path = tr_watchdir_get_path(handle); struct kevent ke; - tr_watchdir_kqueue* backend; - backend = tr_new0(tr_watchdir_kqueue, 1); + auto* backend = new tr_watchdir_kqueue{}; backend->base.free_func = &tr_watchdir_kqueue_free; backend->kq = -1; backend->dirfd = -1; diff --git a/libtransmission/watchdir.cc b/libtransmission/watchdir.cc index 000108000..8eb0411ad 100644 --- a/libtransmission/watchdir.cc +++ b/libtransmission/watchdir.cc @@ -328,12 +328,11 @@ void tr_watchdir_process(tr_watchdir_t handle, char const* name) } } -void tr_watchdir_scan(tr_watchdir_t handle, tr_ptrArray* dir_entries) +void tr_watchdir_scan(tr_watchdir_t handle, std::unordered_set* dir_entries) { tr_sys_dir_t dir; char const* name; - auto new_dir_entries = tr_ptrArray{}; - auto const name_compare_func = (PtrArrayCompareFunc)&strcmp; + auto new_dir_entries = std::unordered_set{}; tr_error* error = nullptr; if ((dir = tr_sys_dir_open(handle->path, &error)) == TR_BAD_SYS_DIR) @@ -352,9 +351,10 @@ void tr_watchdir_scan(tr_watchdir_t handle, tr_ptrArray* dir_entries) if (dir_entries != nullptr) { - tr_ptrArrayInsertSorted(&new_dir_entries, tr_strdup(name), name_compare_func); + auto const namestr = std::string(name); + new_dir_entries.insert(namestr); - if (tr_ptrArrayFindSorted(dir_entries, name, name_compare_func) != nullptr) + if (dir_entries->count(namestr) != 0) { continue; } @@ -373,7 +373,6 @@ void tr_watchdir_scan(tr_watchdir_t handle, tr_ptrArray* dir_entries) if (dir_entries != nullptr) { - tr_ptrArrayDestruct(dir_entries, &tr_free); *dir_entries = new_dir_entries; } } diff --git a/tests/libtransmission/watchdir-test.cc b/tests/libtransmission/watchdir-test.cc index f294099a3..8a0194266 100644 --- a/tests/libtransmission/watchdir-test.cc +++ b/tests/libtransmission/watchdir-test.cc @@ -360,6 +360,8 @@ TEST_P(WatchDirTest, retry) processEvents(); EXPECT_EQ(wd, wd_data.wd); EXPECT_EQ(test_file, wd_data.name); + + tr_watchdir_free(wd); } INSTANTIATE_TEST_SUITE_P( //