diff --git a/src/backend/common/Thread.h b/src/backend/common/Thread.h index fb6c618b0..2c02e5e83 100644 --- a/src/backend/common/Thread.h +++ b/src/backend/common/Thread.h @@ -65,22 +65,22 @@ public: } } # else - inline ~Thread() { m_thread.join(); delete m_worker; } + inline ~Thread() { m_thread.join(); } inline void start(void *(*callback)(void *)) { m_thread = std::thread(callback, this); } # endif inline const T &config() const { return m_config; } inline IBackend *backend() const { return m_backend; } - inline IWorker *worker() const { return m_worker; } + inline IWorker* worker() const { return m_worker.get(); } inline size_t id() const { return m_id; } - inline void setWorker(IWorker *worker) { m_worker = worker; } + inline void setWorker(std::shared_ptr worker) { m_worker = worker; } private: const size_t m_id = 0; const T m_config; IBackend *m_backend; - IWorker *m_worker = nullptr; + std::shared_ptr m_worker; #ifdef XMRIG_OS_APPLE pthread_t m_thread{}; diff --git a/src/backend/common/Workers.cpp b/src/backend/common/Workers.cpp index 9e2b230b8..19be3a2e8 100644 --- a/src/backend/common/Workers.cpp +++ b/src/backend/common/Workers.cpp @@ -62,19 +62,12 @@ public: template xmrig::Workers::Workers() : - d_ptr(new WorkersPrivate()) + d_ptr(std::make_shared()) { } -template -xmrig::Workers::~Workers() -{ - delete d_ptr; -} - - template bool xmrig::Workers::tick(uint64_t) { @@ -88,7 +81,7 @@ bool xmrig::Workers::tick(uint64_t) uint64_t hashCount = 0; uint64_t rawHashes = 0; - for (Thread *handle : m_workers) { + for (auto& handle : m_workers) { IWorker *worker = handle->worker(); if (worker) { worker->hashrateData(hashCount, ts, rawHashes); @@ -135,10 +128,6 @@ void xmrig::Workers::stop() Nonce::stop(T::backend()); # endif - for (Thread *worker : m_workers) { - delete worker; - } - m_workers.clear(); # ifdef XMRIG_MINER_PROJECT @@ -166,7 +155,7 @@ void xmrig::Workers::start(const std::vector &data, const std::shared_ptr< template -xmrig::IWorker *xmrig::Workers::create(Thread *) +std::shared_ptr xmrig::Workers::create(Thread *) { return nullptr; } @@ -177,22 +166,21 @@ void *xmrig::Workers::onReady(void *arg) { auto handle = static_cast* >(arg); - IWorker *worker = create(handle); - assert(worker != nullptr); + std::shared_ptr worker = create(handle); + assert(worker); if (!worker || !worker->selfTest()) { LOG_ERR("%s " RED("thread ") RED_BOLD("#%zu") RED(" self-test failed"), T::tag(), worker ? worker->id() : 0); - handle->backend()->start(worker, false); - delete worker; - + worker.reset(); + handle->backend()->start(worker.get(), false); return nullptr; } assert(handle->backend() != nullptr); handle->setWorker(worker); - handle->backend()->start(worker, true); + handle->backend()->start(worker.get(), true); return nullptr; } @@ -202,7 +190,7 @@ template void xmrig::Workers::start(const std::vector &data, bool /*sleep*/) { for (const auto &item : data) { - m_workers.push_back(new Thread(d_ptr->backend, m_workers.size(), item)); + m_workers.emplace_back(std::make_shared>(d_ptr->backend, m_workers.size(), item)); } d_ptr->hashrate = std::make_shared(m_workers.size()); @@ -211,7 +199,7 @@ void xmrig::Workers::start(const std::vector &data, bool /*sleep*/) Nonce::touch(T::backend()); # endif - for (auto worker : m_workers) { + for (auto& worker : m_workers) { worker->start(Workers::onReady); } } @@ -221,34 +209,34 @@ namespace xmrig { template<> -xmrig::IWorker *xmrig::Workers::create(Thread *handle) +std::shared_ptr Workers::create(Thread *handle) { # ifdef XMRIG_MINER_PROJECT switch (handle->config().intensity) { case 1: - return new CpuWorker<1>(handle->id(), handle->config()); + return std::make_shared>(handle->id(), handle->config()); case 2: - return new CpuWorker<2>(handle->id(), handle->config()); + return std::make_shared>(handle->id(), handle->config()); case 3: - return new CpuWorker<3>(handle->id(), handle->config()); + return std::make_shared>(handle->id(), handle->config()); case 4: - return new CpuWorker<4>(handle->id(), handle->config()); + return std::make_shared>(handle->id(), handle->config()); case 5: - return new CpuWorker<5>(handle->id(), handle->config()); + return std::make_shared>(handle->id(), handle->config()); case 8: - return new CpuWorker<8>(handle->id(), handle->config()); + return std::make_shared>(handle->id(), handle->config()); } return nullptr; # else assert(handle->config().intensity == 1); - return new CpuWorker<1>(handle->id(), handle->config()); + return std::make_shared>(handle->id(), handle->config()); # endif } @@ -258,9 +246,9 @@ template class Workers; #ifdef XMRIG_FEATURE_OPENCL template<> -xmrig::IWorker *xmrig::Workers::create(Thread *handle) +std::shared_ptr Workers::create(Thread *handle) { - return new OclWorker(handle->id(), handle->config()); + return std::make_shared(handle->id(), handle->config()); } @@ -270,9 +258,9 @@ template class Workers; #ifdef XMRIG_FEATURE_CUDA template<> -xmrig::IWorker *xmrig::Workers::create(Thread *handle) +std::shared_ptr Workers::create(Thread *handle) { - return new CudaWorker(handle->id(), handle->config()); + return std::make_shared(handle->id(), handle->config()); } diff --git a/src/backend/common/Workers.h b/src/backend/common/Workers.h index 76909dd59..d9ecead80 100644 --- a/src/backend/common/Workers.h +++ b/src/backend/common/Workers.h @@ -52,7 +52,6 @@ public: XMRIG_DISABLE_COPY_MOVE(Workers) Workers(); - ~Workers(); inline void start(const std::vector &data) { start(data, true); } @@ -67,20 +66,20 @@ public: # endif private: - static IWorker *create(Thread *handle); + static std::shared_ptr create(Thread *handle); static void *onReady(void *arg); void start(const std::vector &data, bool sleep); - std::vector *> m_workers; - WorkersPrivate *d_ptr; + std::vector>> m_workers; + std::shared_ptr d_ptr; }; template void xmrig::Workers::jobEarlyNotification(const Job &job) { - for (Thread* t : m_workers) { + for (auto& t : m_workers) { if (t->worker()) { t->worker()->jobEarlyNotification(job); } @@ -89,20 +88,20 @@ void xmrig::Workers::jobEarlyNotification(const Job &job) template<> -IWorker *Workers::create(Thread *handle); +std::shared_ptr Workers::create(Thread *handle); extern template class Workers; #ifdef XMRIG_FEATURE_OPENCL template<> -IWorker *Workers::create(Thread *handle); +std::shared_ptr Workers::create(Thread *handle); extern template class Workers; #endif #ifdef XMRIG_FEATURE_CUDA template<> -IWorker *Workers::create(Thread *handle); +std::shared_ptr Workers::create(Thread *handle); extern template class Workers; #endif diff --git a/src/backend/common/benchmark/BenchState.cpp b/src/backend/common/benchmark/BenchState.cpp index ec2074dc9..38700b3cc 100644 --- a/src/backend/common/benchmark/BenchState.cpp +++ b/src/backend/common/benchmark/BenchState.cpp @@ -51,7 +51,7 @@ public: }; -static BenchStatePrivate *d_ptr = nullptr; +static std::shared_ptr d_ptr; std::atomic BenchState::m_data{}; @@ -61,7 +61,7 @@ std::atomic BenchState::m_data{}; bool xmrig::BenchState::isDone() { - return d_ptr == nullptr; + return !d_ptr; } @@ -105,14 +105,13 @@ uint64_t xmrig::BenchState::start(size_t threads, const IBackend *backend) void xmrig::BenchState::destroy() { - delete d_ptr; - d_ptr = nullptr; + d_ptr.reset(); } void xmrig::BenchState::done() { - assert(d_ptr != nullptr && d_ptr->async && d_ptr->remaining > 0); + assert(d_ptr && d_ptr->async && d_ptr->remaining > 0); const uint64_t ts = Chrono::steadyMSecs(); @@ -129,15 +128,15 @@ void xmrig::BenchState::done() void xmrig::BenchState::init(IBenchListener *listener, uint32_t size) { - assert(d_ptr == nullptr); + assert(!d_ptr); - d_ptr = new BenchStatePrivate(listener, size); + d_ptr = std::make_shared(listener, size); } void xmrig::BenchState::setSize(uint32_t size) { - assert(d_ptr != nullptr); + assert(d_ptr); d_ptr->size = size; } diff --git a/src/backend/cpu/Cpu.cpp b/src/backend/cpu/Cpu.cpp index a49d65605..194e120b4 100644 --- a/src/backend/cpu/Cpu.cpp +++ b/src/backend/cpu/Cpu.cpp @@ -31,20 +31,20 @@ #endif -static xmrig::ICpuInfo *cpuInfo = nullptr; +static std::shared_ptr cpuInfo; xmrig::ICpuInfo *xmrig::Cpu::info() { - if (cpuInfo == nullptr) { + if (!cpuInfo) { # if defined(XMRIG_FEATURE_HWLOC) - cpuInfo = new HwlocCpuInfo(); + cpuInfo = std::make_shared(); # else - cpuInfo = new BasicCpuInfo(); + cpuInfo = std::make_shared(); # endif } - return cpuInfo; + return cpuInfo.get(); } @@ -56,6 +56,5 @@ rapidjson::Value xmrig::Cpu::toJSON(rapidjson::Document &doc) void xmrig::Cpu::release() { - delete cpuInfo; - cpuInfo = nullptr; + cpuInfo.reset(); } diff --git a/src/backend/cpu/CpuBackend.cpp b/src/backend/cpu/CpuBackend.cpp index 529b14e23..cc1ef7d73 100644 --- a/src/backend/cpu/CpuBackend.cpp +++ b/src/backend/cpu/CpuBackend.cpp @@ -242,7 +242,7 @@ const char *xmrig::cpu_tag() xmrig::CpuBackend::CpuBackend(Controller *controller) : - d_ptr(new CpuBackendPrivate(controller)) + d_ptr(std::make_shared(controller)) { d_ptr->workers.setBackend(this); } @@ -250,7 +250,6 @@ xmrig::CpuBackend::CpuBackend(Controller *controller) : xmrig::CpuBackend::~CpuBackend() { - delete d_ptr; } diff --git a/src/backend/cpu/CpuBackend.h b/src/backend/cpu/CpuBackend.h index 724e751ae..6f79ad941 100644 --- a/src/backend/cpu/CpuBackend.h +++ b/src/backend/cpu/CpuBackend.h @@ -70,7 +70,7 @@ protected: # endif private: - CpuBackendPrivate *d_ptr; + std::shared_ptr d_ptr; }; diff --git a/src/backend/cpu/CpuWorker.cpp b/src/backend/cpu/CpuWorker.cpp index bef2e898e..2b24eaea4 100644 --- a/src/backend/cpu/CpuWorker.cpp +++ b/src/backend/cpu/CpuWorker.cpp @@ -57,7 +57,7 @@ static constexpr uint32_t kReserveCount = 32768; #ifdef XMRIG_ALGO_CN_HEAVY static std::mutex cn_heavyZen3MemoryMutex; -VirtualMemory* cn_heavyZen3Memory = nullptr; +std::shared_ptr cn_heavyZen3Memory; #endif } // namespace xmrig @@ -87,14 +87,14 @@ xmrig::CpuWorker::CpuWorker(size_t id, const CpuLaunchData &data) : if (!cn_heavyZen3Memory) { // Round up number of threads to the multiple of 8 const size_t num_threads = ((m_threads + 7) / 8) * 8; - cn_heavyZen3Memory = new VirtualMemory(m_algorithm.l3() * num_threads, data.hugePages, false, false, node()); + cn_heavyZen3Memory = std::make_shared(m_algorithm.l3() * num_threads, data.hugePages, false, false, node()); } m_memory = cn_heavyZen3Memory; } else # endif { - m_memory = new VirtualMemory(m_algorithm.l3() * N, data.hugePages, false, true, node()); + m_memory = std::make_shared(m_algorithm.l3() * N, data.hugePages, false, true, node()); } # ifdef XMRIG_ALGO_GHOSTRIDER @@ -107,7 +107,7 @@ template xmrig::CpuWorker::~CpuWorker() { # ifdef XMRIG_ALGO_RANDOMX - RxVm::destroy(m_vm); + m_vm.reset(); # endif CnCtx::release(m_ctx, N); @@ -116,7 +116,7 @@ xmrig::CpuWorker::~CpuWorker() if (m_memory != cn_heavyZen3Memory) # endif { - delete m_memory; + m_memory.reset(); } # ifdef XMRIG_ALGO_GHOSTRIDER @@ -148,7 +148,7 @@ void xmrig::CpuWorker::allocateRandomX_VM() } else if (!dataset->get() && (m_job.currentJob().seed() != m_seed)) { // Update RandomX light VM with the new seed - randomx_vm_set_cache(m_vm, dataset->cache()->get()); + randomx_vm_set_cache(m_vm.get(), dataset->cache()->get()); } m_seed = m_job.currentJob().seed(); } @@ -296,7 +296,7 @@ void xmrig::CpuWorker::start() if (job.hasMinerSignature()) { job.generateMinerSignature(m_job.blob(), job.size(), miner_signature_ptr); } - randomx_calculate_hash_first(m_vm, tempHash, m_job.blob(), job.size()); + randomx_calculate_hash_first(m_vm.get(), tempHash, m_job.blob(), job.size()); } if (!nextRound()) { @@ -307,7 +307,7 @@ void xmrig::CpuWorker::start() memcpy(miner_signature_saved, miner_signature_ptr, sizeof(miner_signature_saved)); job.generateMinerSignature(m_job.blob(), job.size(), miner_signature_ptr); } - randomx_calculate_hash_next(m_vm, tempHash, m_job.blob(), job.size(), m_hash); + randomx_calculate_hash_next(m_vm.get(), tempHash, m_job.blob(), job.size(), m_hash); } else # endif diff --git a/src/backend/cpu/CpuWorker.h b/src/backend/cpu/CpuWorker.h index 18e4fed5c..4f50c6c0d 100644 --- a/src/backend/cpu/CpuWorker.h +++ b/src/backend/cpu/CpuWorker.h @@ -66,7 +66,7 @@ protected: void hashrateData(uint64_t &hashCount, uint64_t &timeStamp, uint64_t &rawHashes) const override; void start() override; - inline const VirtualMemory *memory() const override { return m_memory; } + inline const VirtualMemory* memory() const override { return m_memory.get(); } inline size_t intensity() const override { return N; } inline void jobEarlyNotification(const Job&) override {} @@ -92,11 +92,11 @@ private: const Miner *m_miner; const size_t m_threads; cryptonight_ctx *m_ctx[N]; - VirtualMemory *m_memory = nullptr; + std::shared_ptr m_memory; WorkerJob m_job; # ifdef XMRIG_ALGO_RANDOMX - randomx_vm *m_vm = nullptr; + std::shared_ptr m_vm; Buffer m_seed; # endif diff --git a/src/backend/opencl/OclBackend.cpp b/src/backend/opencl/OclBackend.cpp index 493ad4c8d..30791ef2a 100644 --- a/src/backend/opencl/OclBackend.cpp +++ b/src/backend/opencl/OclBackend.cpp @@ -283,7 +283,7 @@ const char *xmrig::ocl_tag() xmrig::OclBackend::OclBackend(Controller *controller) : - d_ptr(new OclBackendPrivate(controller)) + d_ptr(std::make_shared(controller)) { d_ptr->workers.setBackend(this); } @@ -291,7 +291,7 @@ xmrig::OclBackend::OclBackend(Controller *controller) : xmrig::OclBackend::~OclBackend() { - delete d_ptr; + d_ptr.reset(); OclLib::close(); diff --git a/src/backend/opencl/OclBackend.h b/src/backend/opencl/OclBackend.h index 64236ec99..9c0fac23b 100644 --- a/src/backend/opencl/OclBackend.h +++ b/src/backend/opencl/OclBackend.h @@ -70,7 +70,7 @@ protected: # endif private: - OclBackendPrivate *d_ptr; + std::shared_ptr d_ptr; }; diff --git a/src/base/api/Api.cpp b/src/base/api/Api.cpp index 77f8e4aa0..2e49d5bf7 100644 --- a/src/base/api/Api.cpp +++ b/src/base/api/Api.cpp @@ -95,8 +95,7 @@ xmrig::Api::~Api() # ifdef XMRIG_FEATURE_HTTP if (m_httpd) { m_httpd->stop(); - delete m_httpd; - m_httpd = nullptr; // Ensure the pointer is set to nullptr after deletion + m_httpd.reset(); } # endif } @@ -116,12 +115,11 @@ void xmrig::Api::start() # ifdef XMRIG_FEATURE_HTTP if (!m_httpd) { - m_httpd = new Httpd(m_base); + m_httpd = std::make_shared(m_base); if (!m_httpd->start()) { LOG_ERR("%s " RED_BOLD("HTTP API server failed to start."), Tags::network()); - delete m_httpd; // Properly handle failure to start - m_httpd = nullptr; + m_httpd.reset(); } } # endif diff --git a/src/base/api/Api.h b/src/base/api/Api.h index f3ad04c10..8f3a238ca 100644 --- a/src/base/api/Api.h +++ b/src/base/api/Api.h @@ -66,7 +66,7 @@ private: Base *m_base; char m_id[32]{}; const uint64_t m_timestamp; - Httpd *m_httpd = nullptr; + std::shared_ptr m_httpd; std::vector m_listeners; String m_workerId; uint8_t m_ticks = 0; diff --git a/src/base/api/Httpd.cpp b/src/base/api/Httpd.cpp index d2d3db4a7..163934fc0 100644 --- a/src/base/api/Httpd.cpp +++ b/src/base/api/Httpd.cpp @@ -69,13 +69,13 @@ bool xmrig::Httpd::start() bool tls = false; # ifdef XMRIG_FEATURE_TLS - m_http = new HttpsServer(m_httpListener); + m_http = std::make_shared(m_httpListener); tls = m_http->setTls(m_base->config()->tls()); # else - m_http = new HttpServer(m_httpListener); + m_http = std::make_shared(m_httpListener); # endif - m_server = new TcpServer(config.host(), config.port(), m_http); + m_server = std::make_shared(config.host(), config.port(), m_http.get()); const int rc = m_server->bind(); Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CSI "1;%dm%s:%d" " " RED_BOLD("%s"), @@ -112,9 +112,6 @@ bool xmrig::Httpd::start() void xmrig::Httpd::stop() { - delete m_server; - delete m_http; - m_server = nullptr; m_http = nullptr; m_port = 0; diff --git a/src/base/api/Httpd.h b/src/base/api/Httpd.h index e00c8eb5b..e03bae357 100644 --- a/src/base/api/Httpd.h +++ b/src/base/api/Httpd.h @@ -55,13 +55,13 @@ private: const Base *m_base; std::shared_ptr m_httpListener; - TcpServer *m_server = nullptr; + std::shared_ptr m_server; uint16_t m_port = 0; # ifdef XMRIG_FEATURE_TLS - HttpsServer *m_http = nullptr; + std::shared_ptr m_http; # else - HttpServer *m_http = nullptr; + std::shared_ptr m_http; # endif }; diff --git a/src/base/io/Async.cpp b/src/base/io/Async.cpp index 255be8c0f..0eeb4f5ea 100644 --- a/src/base/io/Async.cpp +++ b/src/base/io/Async.cpp @@ -128,7 +128,7 @@ public: } // namespace xmrig -xmrig::Async::Async(Callback callback) : d_ptr(new AsyncPrivate()) +xmrig::Async::Async(Callback callback) : d_ptr(std::make_shared()) { d_ptr->callback = std::move(callback); d_ptr->async = new uv_async_t; @@ -151,8 +151,6 @@ xmrig::Async::Async(IAsyncListener *listener) : d_ptr(new AsyncPrivate()) xmrig::Async::~Async() { Handle::close(d_ptr->async); - - delete d_ptr; } diff --git a/src/base/io/Async.h b/src/base/io/Async.h index 19491082b..2f5725bd4 100644 --- a/src/base/io/Async.h +++ b/src/base/io/Async.h @@ -49,7 +49,7 @@ public: void send(); private: - AsyncPrivate *d_ptr; + std::shared_ptr d_ptr; }; diff --git a/src/base/io/Watcher.cpp b/src/base/io/Watcher.cpp index f5ce9647a..4cdf39c7f 100644 --- a/src/base/io/Watcher.cpp +++ b/src/base/io/Watcher.cpp @@ -36,7 +36,7 @@ xmrig::Watcher::Watcher(const String &path, IWatcherListener *listener) : m_listener(listener), m_path(path) { - m_timer = new Timer(this); + m_timer = std::make_shared(this); m_fsEvent = new uv_fs_event_t; m_fsEvent->data = this; @@ -48,8 +48,6 @@ xmrig::Watcher::Watcher(const String &path, IWatcherListener *listener) : xmrig::Watcher::~Watcher() { - delete m_timer; - Handle::close(m_fsEvent); } diff --git a/src/base/io/Watcher.h b/src/base/io/Watcher.h index 6438cb7a2..e2682a049 100644 --- a/src/base/io/Watcher.h +++ b/src/base/io/Watcher.h @@ -60,7 +60,7 @@ private: IWatcherListener *m_listener; String m_path; - Timer *m_timer; + std::shared_ptr m_timer; uv_fs_event_t *m_fsEvent; }; diff --git a/src/base/io/log/Log.cpp b/src/base/io/log/Log.cpp index c20f85c7a..92a019635 100644 --- a/src/base/io/log/Log.cpp +++ b/src/base/io/log/Log.cpp @@ -66,17 +66,10 @@ public: LogPrivate() = default; + ~LogPrivate() = default; - inline ~LogPrivate() - { - for (auto backend : m_backends) { - delete backend; - } - } - - - inline void add(ILogBackend *backend) { m_backends.push_back(backend); } + inline void add(std::shared_ptr backend) { m_backends.emplace_back(backend); } void print(Log::Level level, const char *fmt, va_list args) @@ -108,7 +101,7 @@ public: } if (!m_backends.empty()) { - for (auto backend : m_backends) { + for (auto& backend : m_backends) { backend->print(ts, level, m_buf, offset, size, true); backend->print(ts, level, txt.c_str(), offset ? (offset - 11) : 0, txt.size(), false); } @@ -188,13 +181,13 @@ private: char m_buf[Log::kMaxBufferSize]{}; std::mutex m_mutex; - std::vector m_backends; + std::vector> m_backends; }; bool Log::m_background = false; bool Log::m_colors = true; -LogPrivate *Log::d = nullptr; +std::shared_ptr Log::d{}; uint32_t Log::m_verbose = 0; @@ -202,7 +195,7 @@ uint32_t Log::m_verbose = 0; -void xmrig::Log::add(ILogBackend *backend) +void xmrig::Log::add(std::shared_ptr backend) { assert(d != nullptr); @@ -214,14 +207,13 @@ void xmrig::Log::add(ILogBackend *backend) void xmrig::Log::destroy() { - delete d; - d = nullptr; + d.reset(); } void xmrig::Log::init() { - d = new LogPrivate(); + d = std::make_shared(); } diff --git a/src/base/io/log/Log.h b/src/base/io/log/Log.h index d4c0a3197..dfd49cb51 100644 --- a/src/base/io/log/Log.h +++ b/src/base/io/log/Log.h @@ -23,6 +23,7 @@ #include #include +#include namespace xmrig { @@ -49,7 +50,7 @@ public: constexpr static size_t kMaxBufferSize = 16384; - static void add(ILogBackend *backend); + static void add(std::shared_ptr backend); static void destroy(); static void init(); static void print(const char *fmt, ...); @@ -66,7 +67,7 @@ public: private: static bool m_background; static bool m_colors; - static LogPrivate *d; + static std::shared_ptr d; static uint32_t m_verbose; }; diff --git a/src/base/kernel/Base.cpp b/src/base/kernel/Base.cpp index 4aa2e466e..eb8941e4e 100644 --- a/src/base/kernel/Base.cpp +++ b/src/base/kernel/Base.cpp @@ -80,11 +80,10 @@ public: inline ~BasePrivate() { # ifdef XMRIG_FEATURE_API - delete api; + api.reset(); # endif - delete config; - delete watcher; + watcher.reset(); NetBuffer::destroy(); } @@ -98,27 +97,25 @@ public: } - inline void replace(Config *newConfig) + inline void replace(std::shared_ptr newConfig) { - Config *previousConfig = config; + auto previousConfig = config; config = newConfig; for (IBaseListener *listener : listeners) { - listener->onConfigChanged(config, previousConfig); + listener->onConfigChanged(config.get(), previousConfig.get()); } - - delete previousConfig; } - Api *api = nullptr; - Config *config = nullptr; + std::shared_ptr api; + std::shared_ptr config; std::vector listeners; - Watcher *watcher = nullptr; + std::shared_ptr watcher; private: - inline static Config *load(Process *process) + inline static std::shared_ptr load(Process *process) { JsonChain chain; ConfigTransform transform; @@ -127,29 +124,29 @@ private: ConfigTransform::load(chain, process, transform); if (read(chain, config)) { - return config.release(); + return config; } chain.addFile(Process::location(Process::DataLocation, "config.json")); if (read(chain, config)) { - return config.release(); + return config; } chain.addFile(Process::location(Process::HomeLocation, "." APP_ID ".json")); if (read(chain, config)) { - return config.release(); + return config; } chain.addFile(Process::location(Process::HomeLocation, ".config" XMRIG_DIR_SEPARATOR APP_ID ".json")); if (read(chain, config)) { - return config.release(); + return config; } # ifdef XMRIG_FEATURE_EMBEDDED_CONFIG chain.addRaw(default_config); if (read(chain, config)) { - return config.release(); + return config; } # endif @@ -162,7 +159,7 @@ private: xmrig::Base::Base(Process *process) - : d_ptr(new BasePrivate(process)) + : d_ptr(std::make_shared(process)) { } @@ -170,7 +167,6 @@ xmrig::Base::Base(Process *process) xmrig::Base::~Base() { - delete d_ptr; } @@ -183,7 +179,7 @@ bool xmrig::Base::isReady() const int xmrig::Base::init() { # ifdef XMRIG_FEATURE_API - d_ptr->api = new Api(this); + d_ptr->api = std::make_shared(this); d_ptr->api->addListener(this); # endif @@ -193,16 +189,16 @@ int xmrig::Base::init() Log::setBackground(true); } else { - Log::add(new ConsoleLog(config()->title())); + Log::add(std::make_shared(config()->title())); } if (config()->logFile()) { - Log::add(new FileLog(config()->logFile())); + Log::add(std::make_shared(config()->logFile())); } # ifdef HAVE_SYSLOG_H if (config()->isSyslog()) { - Log::add(new SysLog()); + Log::add(std::make_shared()); } # endif @@ -221,7 +217,7 @@ void xmrig::Base::start() } if (config()->isWatch()) { - d_ptr->watcher = new Watcher(config()->fileName(), this); + d_ptr->watcher = std::make_shared(config()->fileName(), this); } } @@ -232,8 +228,7 @@ void xmrig::Base::stop() api()->stop(); # endif - delete d_ptr->watcher; - d_ptr->watcher = nullptr; + d_ptr->watcher.reset(); } @@ -241,7 +236,7 @@ xmrig::Api *xmrig::Base::api() const { assert(d_ptr->api != nullptr); - return d_ptr->api; + return d_ptr->api.get(); } @@ -258,18 +253,14 @@ bool xmrig::Base::reload(const rapidjson::Value &json) return false; } - auto config = new Config(); + auto config = std::make_shared(); if (!config->read(reader, d_ptr->config->fileName())) { - delete config; - return false; } const bool saved = config->save(); if (config->isWatch() && d_ptr->watcher && saved) { - delete config; - return true; } @@ -279,11 +270,11 @@ bool xmrig::Base::reload(const rapidjson::Value &json) } -xmrig::Config *xmrig::Base::config() const +xmrig::Config* xmrig::Base::config() const { - assert(d_ptr->config != nullptr); + assert(d_ptr->config); - return d_ptr->config; + return d_ptr->config.get(); } @@ -300,12 +291,10 @@ void xmrig::Base::onFileChanged(const String &fileName) JsonChain chain; chain.addFile(fileName); - auto config = new Config(); + auto config = std::make_shared(); if (!config->read(chain, chain.fileName())) { LOG_ERR("%s " RED("reloading failed"), Tags::config()); - - delete config; return; } diff --git a/src/base/kernel/Base.h b/src/base/kernel/Base.h index 53eaaea3b..5253a9c62 100644 --- a/src/base/kernel/Base.h +++ b/src/base/kernel/Base.h @@ -64,7 +64,7 @@ protected: # endif private: - BasePrivate *d_ptr; + std::shared_ptr d_ptr; }; diff --git a/src/base/net/dns/DnsUvBackend.cpp b/src/base/net/dns/DnsUvBackend.cpp index 33f27f95e..ced51be9e 100644 --- a/src/base/net/dns/DnsUvBackend.cpp +++ b/src/base/net/dns/DnsUvBackend.cpp @@ -29,13 +29,13 @@ namespace xmrig { -static Storage *storage = nullptr; +static std::shared_ptr> storage = nullptr; Storage &DnsUvBackend::getStorage() { - if (storage == nullptr) { - storage = new Storage(); + if (!storage) { + storage = std::make_shared>(); } return *storage; @@ -67,8 +67,7 @@ xmrig::DnsUvBackend::~DnsUvBackend() storage->release(m_key); if (storage->isEmpty()) { - delete storage; - storage = nullptr; + storage.reset(); } } diff --git a/src/base/net/stratum/DaemonClient.cpp b/src/base/net/stratum/DaemonClient.cpp index 37f352bc1..c805a9a78 100644 --- a/src/base/net/stratum/DaemonClient.cpp +++ b/src/base/net/stratum/DaemonClient.cpp @@ -87,14 +87,13 @@ xmrig::DaemonClient::DaemonClient(int id, IClientListener *listener) : BaseClient(id, listener) { m_httpListener = std::make_shared(this); - m_timer = new Timer(this); + m_timer = std::make_shared(this); m_key = m_storage.add(this); } xmrig::DaemonClient::~DaemonClient() { - delete m_timer; delete m_ZMQSocket; } @@ -104,9 +103,6 @@ void xmrig::DaemonClient::deleteLater() if (m_pool.zmq_port() >= 0) { ZMQClose(true); } - else { - delete this; - } } diff --git a/src/base/net/stratum/DaemonClient.h b/src/base/net/stratum/DaemonClient.h index e852f428c..95121eacb 100644 --- a/src/base/net/stratum/DaemonClient.h +++ b/src/base/net/stratum/DaemonClient.h @@ -107,7 +107,7 @@ private: uint64_t m_jobSteadyMs = 0; String m_tlsFingerprint; String m_tlsVersion; - Timer *m_timer; + std::shared_ptr m_timer; uint64_t m_blocktemplateRequestHeight = 0; WalletAddress m_walletAddress; diff --git a/src/base/net/stratum/Pool.cpp b/src/base/net/stratum/Pool.cpp index 8b4a6e033..e2986497c 100644 --- a/src/base/net/stratum/Pool.cpp +++ b/src/base/net/stratum/Pool.cpp @@ -221,42 +221,42 @@ bool xmrig::Pool::isEqual(const Pool &other) const } -xmrig::IClient *xmrig::Pool::createClient(int id, IClientListener *listener) const +std::shared_ptr xmrig::Pool::createClient(int id, IClientListener* listener) const { - IClient *client = nullptr; + std::shared_ptr client; if (m_mode == MODE_POOL) { # if defined XMRIG_ALGO_KAWPOW || defined XMRIG_ALGO_GHOSTRIDER const uint32_t f = m_algorithm.family(); if ((f == Algorithm::KAWPOW) || (f == Algorithm::GHOSTRIDER) || (m_coin == Coin::RAVEN)) { - client = new EthStratumClient(id, Platform::userAgent(), listener); + client = std::make_shared(id, Platform::userAgent(), listener); } else # endif { - client = new Client(id, Platform::userAgent(), listener); + client = std::make_shared(id, Platform::userAgent(), listener); } } # ifdef XMRIG_FEATURE_HTTP else if (m_mode == MODE_DAEMON) { - client = new DaemonClient(id, listener); + client = std::make_shared(id, listener); } else if (m_mode == MODE_SELF_SELECT) { - client = new SelfSelectClient(id, Platform::userAgent(), listener, m_submitToOrigin); + client = std::make_shared(id, Platform::userAgent(), listener, m_submitToOrigin); } # endif # if defined XMRIG_ALGO_KAWPOW || defined XMRIG_ALGO_GHOSTRIDER else if (m_mode == MODE_AUTO_ETH) { - client = new AutoClient(id, Platform::userAgent(), listener); + client = std::make_shared(id, Platform::userAgent(), listener); } # endif # ifdef XMRIG_FEATURE_BENCHMARK else if (m_mode == MODE_BENCHMARK) { - client = new BenchClient(m_benchmark, listener); + client = std::make_shared(m_benchmark, listener); } # endif - assert(client != nullptr); + assert(client); if (client) { client->setPool(*this); diff --git a/src/base/net/stratum/Pool.h b/src/base/net/stratum/Pool.h index a78eb6bbe..629f6682d 100644 --- a/src/base/net/stratum/Pool.h +++ b/src/base/net/stratum/Pool.h @@ -127,7 +127,7 @@ public: bool isEnabled() const; bool isEqual(const Pool &other) const; - IClient *createClient(int id, IClientListener *listener) const; + std::shared_ptr createClient(int id, IClientListener *listener) const; rapidjson::Value toJSON(rapidjson::Document &doc) const; std::string printableName() const; diff --git a/src/base/net/stratum/Pools.cpp b/src/base/net/stratum/Pools.cpp index a2374b9dd..989888a7b 100644 --- a/src/base/net/stratum/Pools.cpp +++ b/src/base/net/stratum/Pools.cpp @@ -80,17 +80,17 @@ int xmrig::Pools::donateLevel() const } -xmrig::IStrategy *xmrig::Pools::createStrategy(IStrategyListener *listener) const +std::shared_ptr xmrig::Pools::createStrategy(IStrategyListener *listener) const { if (active() == 1) { for (const Pool &pool : m_data) { if (pool.isEnabled()) { - return new SinglePoolStrategy(pool, retryPause(), retries(), listener); + return std::make_shared(pool, retryPause(), retries(), listener); } } } - auto strategy = new FailoverStrategy(retryPause(), retries(), listener); + auto strategy = std::make_shared(retryPause(), retries(), listener); for (const Pool &pool : m_data) { if (pool.isEnabled()) { strategy->add(pool); @@ -154,7 +154,7 @@ void xmrig::Pools::load(const IJsonReader &reader) Pool pool(value); if (pool.isValid()) { - m_data.push_back(std::move(pool)); + m_data.emplace_back(std::move(pool)); } } diff --git a/src/base/net/stratum/Pools.h b/src/base/net/stratum/Pools.h index b3f6b0815..f085a4058 100644 --- a/src/base/net/stratum/Pools.h +++ b/src/base/net/stratum/Pools.h @@ -73,7 +73,7 @@ public: bool isEqual(const Pools &other) const; int donateLevel() const; - IStrategy *createStrategy(IStrategyListener *listener) const; + std::shared_ptr createStrategy(IStrategyListener *listener) const; rapidjson::Value toJSON(rapidjson::Document &doc) const; size_t active() const; uint32_t benchSize() const; diff --git a/src/base/net/stratum/SelfSelectClient.cpp b/src/base/net/stratum/SelfSelectClient.cpp index f411f3ec9..8ae06cb69 100644 --- a/src/base/net/stratum/SelfSelectClient.cpp +++ b/src/base/net/stratum/SelfSelectClient.cpp @@ -56,13 +56,12 @@ xmrig::SelfSelectClient::SelfSelectClient(int id, const char *agent, IClientList m_listener(listener) { m_httpListener = std::make_shared(this); - m_client = new Client(id, agent, this); + m_client = std::make_shared(id, agent, this); } xmrig::SelfSelectClient::~SelfSelectClient() { - delete m_client; } diff --git a/src/base/net/stratum/SelfSelectClient.h b/src/base/net/stratum/SelfSelectClient.h index c73198a5a..3b35438bc 100644 --- a/src/base/net/stratum/SelfSelectClient.h +++ b/src/base/net/stratum/SelfSelectClient.h @@ -105,7 +105,7 @@ private: bool m_active = false; bool m_quiet = false; const bool m_submitToOrigin; - IClient *m_client; + std::shared_ptr m_client; IClientListener *m_listener; int m_retries = 5; int64_t m_failures = 0; diff --git a/src/base/net/stratum/benchmark/BenchClient.h b/src/base/net/stratum/benchmark/BenchClient.h index c29e42e00..d3e48eb55 100644 --- a/src/base/net/stratum/benchmark/BenchClient.h +++ b/src/base/net/stratum/benchmark/BenchClient.h @@ -53,7 +53,7 @@ public: inline int64_t sequence() const override { return 0; } inline int64_t submit(const JobResult &) override { return 0; } inline void connect(const Pool &pool) override { setPool(pool); } - inline void deleteLater() override { delete this; } + inline void deleteLater() override {} inline void setAlgo(const Algorithm &algo) override {} inline void setEnabled(bool enabled) override {} inline void setProxy(const ProxyUrl &proxy) override {} diff --git a/src/base/net/stratum/strategies/FailoverStrategy.cpp b/src/base/net/stratum/strategies/FailoverStrategy.cpp index 604f55966..328582ae4 100644 --- a/src/base/net/stratum/strategies/FailoverStrategy.cpp +++ b/src/base/net/stratum/strategies/FailoverStrategy.cpp @@ -47,7 +47,7 @@ xmrig::FailoverStrategy::FailoverStrategy(int retryPause, int retries, IStrategy xmrig::FailoverStrategy::~FailoverStrategy() { - for (IClient *client : m_pools) { + for (auto& client : m_pools) { client->deleteLater(); } } @@ -55,7 +55,7 @@ xmrig::FailoverStrategy::~FailoverStrategy() void xmrig::FailoverStrategy::add(const Pool &pool) { - IClient *client = pool.createClient(static_cast(m_pools.size()), this); + std::shared_ptr client = pool.createClient(static_cast(m_pools.size()), this); client->setRetries(m_retries); client->setRetryPause(m_retryPause * 1000); @@ -93,7 +93,7 @@ void xmrig::FailoverStrategy::resume() void xmrig::FailoverStrategy::setAlgo(const Algorithm &algo) { - for (IClient *client : m_pools) { + for (auto& client : m_pools) { client->setAlgo(algo); } } @@ -101,7 +101,7 @@ void xmrig::FailoverStrategy::setAlgo(const Algorithm &algo) void xmrig::FailoverStrategy::setProxy(const ProxyUrl &proxy) { - for (IClient *client : m_pools) { + for (auto& client : m_pools) { client->setProxy(proxy); } } @@ -109,7 +109,7 @@ void xmrig::FailoverStrategy::setProxy(const ProxyUrl &proxy) void xmrig::FailoverStrategy::stop() { - for (auto &pool : m_pools) { + for (auto& pool : m_pools) { pool->disconnect(); } @@ -122,7 +122,7 @@ void xmrig::FailoverStrategy::stop() void xmrig::FailoverStrategy::tick(uint64_t now) { - for (IClient *client : m_pools) { + for (auto& client : m_pools) { client->tick(now); } } diff --git a/src/base/net/stratum/strategies/FailoverStrategy.h b/src/base/net/stratum/strategies/FailoverStrategy.h index a25c82b78..780545816 100644 --- a/src/base/net/stratum/strategies/FailoverStrategy.h +++ b/src/base/net/stratum/strategies/FailoverStrategy.h @@ -49,7 +49,7 @@ public: protected: inline bool isActive() const override { return m_active >= 0; } - inline IClient *client() const override { return isActive() ? active() : m_pools[m_index]; } + inline IClient* client() const override { return isActive() ? active() : m_pools[m_index].get(); } int64_t submit(const JobResult &result) override; void connect() override; @@ -67,7 +67,7 @@ protected: void onVerifyAlgorithm(const IClient *client, const Algorithm &algorithm, bool *ok) override; private: - inline IClient *active() const { return m_pools[static_cast(m_active)]; } + inline IClient* active() const { return m_pools[static_cast(m_active)].get(); } const bool m_quiet; const int m_retries; @@ -75,7 +75,7 @@ private: int m_active = -1; IStrategyListener *m_listener; size_t m_index = 0; - std::vector m_pools; + std::vector> m_pools; }; diff --git a/src/base/net/stratum/strategies/SinglePoolStrategy.cpp b/src/base/net/stratum/strategies/SinglePoolStrategy.cpp index fc0fc5624..0ef203154 100644 --- a/src/base/net/stratum/strategies/SinglePoolStrategy.cpp +++ b/src/base/net/stratum/strategies/SinglePoolStrategy.cpp @@ -66,7 +66,7 @@ void xmrig::SinglePoolStrategy::resume() return; } - m_listener->onJob(this, m_client, m_client->job(), rapidjson::Value(rapidjson::kNullType)); + m_listener->onJob(this, m_client.get(), m_client->job(), rapidjson::Value(rapidjson::kNullType)); } diff --git a/src/base/net/stratum/strategies/SinglePoolStrategy.h b/src/base/net/stratum/strategies/SinglePoolStrategy.h index 90b85c7d7..94998a3c1 100644 --- a/src/base/net/stratum/strategies/SinglePoolStrategy.h +++ b/src/base/net/stratum/strategies/SinglePoolStrategy.h @@ -49,7 +49,7 @@ public: protected: inline bool isActive() const override { return m_active; } - inline IClient *client() const override { return m_client; } + inline IClient* client() const override { return m_client.get(); } int64_t submit(const JobResult &result) override; void connect() override; @@ -68,7 +68,7 @@ protected: private: bool m_active; - IClient *m_client; + std::shared_ptr m_client; IStrategyListener *m_listener; }; diff --git a/src/base/net/tools/NetBuffer.cpp b/src/base/net/tools/NetBuffer.cpp index 8931f06e7..0c838b8e5 100644 --- a/src/base/net/tools/NetBuffer.cpp +++ b/src/base/net/tools/NetBuffer.cpp @@ -23,22 +23,23 @@ #include +#include #include namespace xmrig { -static MemPool *pool = nullptr; +static std::shared_ptr> pool; inline MemPool *getPool() { if (!pool) { - pool = new MemPool(); + pool = std::make_shared>(); } - return pool; + return pool.get(); } @@ -59,8 +60,7 @@ void xmrig::NetBuffer::destroy() assert(pool->freeSize() == pool->size()); - delete pool; - pool = nullptr; + pool.reset(); } diff --git a/src/core/Miner.cpp b/src/core/Miner.cpp index e2b1d596e..cc599519e 100644 --- a/src/core/Miner.cpp +++ b/src/core/Miner.cpp @@ -84,10 +84,10 @@ public: inline ~MinerPrivate() { - delete timer; + timer.reset(); - for (IBackend *backend : backends) { - delete backend; + for (auto& backend : backends) { + backend.reset(); } # ifdef XMRIG_ALGO_RANDOMX @@ -98,7 +98,7 @@ public: bool isEnabled(const Algorithm &algorithm) const { - for (IBackend *backend : backends) { + for (auto& backend : backends) { if (backend->isEnabled() && backend->isEnabled(algorithm)) { return true; } @@ -124,7 +124,7 @@ public: Nonce::reset(job.index()); } - for (IBackend *backend : backends) { + for (auto& backend : backends) { backend->setJob(job); } @@ -175,7 +175,7 @@ public: double t[3] = { 0.0 }; - for (IBackend *backend : backends) { + for (auto& backend : backends) { const Hashrate *hr = backend->hashrate(); if (!hr) { continue; @@ -221,7 +221,7 @@ public: reply.SetArray(); - for (IBackend *backend : backends) { + for (auto& backend : backends) { reply.PushBack(backend->toJSON(doc), allocator); } } @@ -364,9 +364,9 @@ public: Controller *controller; Job job; mutable std::map maxHashrate; - std::vector backends; + std::vector> backends; String userJobId; - Timer *timer = nullptr; + std::shared_ptr timer; uint64_t ticks = 0; Taskbar m_taskbar; @@ -378,7 +378,7 @@ public: xmrig::Miner::Miner(Controller *controller) - : d_ptr(new MinerPrivate(controller)) + : d_ptr(std::make_shared(controller)) { const int priority = controller->config()->cpu().priority(); if (priority >= 0) { @@ -400,29 +400,23 @@ xmrig::Miner::Miner(Controller *controller) controller->api()->addListener(this); # endif - d_ptr->timer = new Timer(this); + d_ptr->timer = std::make_shared(this); d_ptr->backends.reserve(3); - d_ptr->backends.push_back(new CpuBackend(controller)); + d_ptr->backends.emplace_back(std::make_shared(controller)); # ifdef XMRIG_FEATURE_OPENCL - d_ptr->backends.push_back(new OclBackend(controller)); + d_ptr->backends.emplace_back(std::make_shared(controller)); # endif # ifdef XMRIG_FEATURE_CUDA - d_ptr->backends.push_back(new CudaBackend(controller)); + d_ptr->backends.emplace_back(std::make_shared(controller)); # endif d_ptr->rebuild(); } -xmrig::Miner::~Miner() -{ - delete d_ptr; -} - - bool xmrig::Miner::isEnabled() const { return d_ptr->enabled; @@ -441,7 +435,7 @@ const xmrig::Algorithms &xmrig::Miner::algorithms() const } -const std::vector &xmrig::Miner::backends() const +const std::vector>& xmrig::Miner::backends() const { return d_ptr->backends; } @@ -538,7 +532,7 @@ void xmrig::Miner::setEnabled(bool enabled) void xmrig::Miner::setJob(const Job &job, bool donate) { - for (IBackend *backend : d_ptr->backends) { + for (auto& backend : d_ptr->backends) { backend->prepare(job); } @@ -606,7 +600,7 @@ void xmrig::Miner::stop() { Nonce::stop(); - for (IBackend *backend : d_ptr->backends) { + for (auto& backend : d_ptr->backends) { backend->stop(); } } @@ -622,7 +616,7 @@ void xmrig::Miner::onConfigChanged(Config *config, Config *previousConfig) const Job job = this->job(); - for (IBackend *backend : d_ptr->backends) { + for (auto& backend : d_ptr->backends) { backend->setJob(job); } } @@ -636,7 +630,7 @@ void xmrig::Miner::onTimer(const Timer *) bool stopMiner = false; - for (IBackend *backend : d_ptr->backends) { + for (auto& backend : d_ptr->backends) { if (!backend->tick(d_ptr->ticks)) { stopMiner = true; } @@ -718,7 +712,7 @@ void xmrig::Miner::onRequest(IApiRequest &request) } } - for (IBackend *backend : d_ptr->backends) { + for (auto& backend : d_ptr->backends) { backend->handleRequest(request); } } diff --git a/src/core/Miner.h b/src/core/Miner.h index bb4293d08..3eaa9eb6a 100644 --- a/src/core/Miner.h +++ b/src/core/Miner.h @@ -46,12 +46,12 @@ public: XMRIG_DISABLE_COPY_MOVE_DEFAULT(Miner) Miner(Controller *controller); - ~Miner() override; + ~Miner() override = default; bool isEnabled() const; bool isEnabled(const Algorithm &algorithm) const; const Algorithms &algorithms() const; - const std::vector &backends() const; + const std::vector> &backends() const; Job job() const; void execCommand(char command); void pause(); @@ -72,7 +72,7 @@ protected: # endif private: - MinerPrivate *d_ptr; + std::shared_ptr d_ptr; }; diff --git a/src/core/Taskbar.cpp b/src/core/Taskbar.cpp index c0af8bfac..3ca61f539 100644 --- a/src/core/Taskbar.cpp +++ b/src/core/Taskbar.cpp @@ -65,14 +65,13 @@ struct TaskbarPrivate }; -Taskbar::Taskbar() : d_ptr(new TaskbarPrivate()) +Taskbar::Taskbar() : d_ptr(std::make_shared()) { } Taskbar::~Taskbar() { - delete d_ptr; } diff --git a/src/core/Taskbar.h b/src/core/Taskbar.h index abb04fdea..6fe56c0ed 100644 --- a/src/core/Taskbar.h +++ b/src/core/Taskbar.h @@ -19,6 +19,7 @@ #ifndef XMRIG_TASKBAR_H #define XMRIG_TASKBAR_H +#include namespace xmrig { @@ -39,7 +40,7 @@ private: bool m_active = false; bool m_enabled = true; - TaskbarPrivate* d_ptr = nullptr; + std::shared_ptr d_ptr; void updateTaskbarColor(); }; diff --git a/src/core/config/Config.cpp b/src/core/config/Config.cpp index 6150607d0..411f95e1d 100644 --- a/src/core/config/Config.cpp +++ b/src/core/config/Config.cpp @@ -115,14 +115,13 @@ public: xmrig::Config::Config() : - d_ptr(new ConfigPrivate()) + d_ptr(std::make_shared()) { } xmrig::Config::~Config() { - delete d_ptr; } diff --git a/src/core/config/Config.h b/src/core/config/Config.h index c2da8523d..e1fe9fac5 100644 --- a/src/core/config/Config.h +++ b/src/core/config/Config.h @@ -101,7 +101,7 @@ public: void getJSON(rapidjson::Document &doc) const override; private: - ConfigPrivate *d_ptr; + std::shared_ptr d_ptr; }; diff --git a/src/crypto/common/MemoryPool.cpp b/src/crypto/common/MemoryPool.cpp index e99757ee9..e26ba4242 100644 --- a/src/crypto/common/MemoryPool.cpp +++ b/src/crypto/common/MemoryPool.cpp @@ -49,18 +49,12 @@ xmrig::MemoryPool::MemoryPool(size_t size, bool hugePages, uint32_t node) constexpr size_t alignment = 1 << 24; - m_memory = new VirtualMemory(size * pageSize + alignment, hugePages, false, false, node); + m_memory = std::make_shared(size * pageSize + alignment, hugePages, false, false, node); m_alignOffset = (alignment - (((size_t)m_memory->scratchpad()) % alignment)) % alignment; } -xmrig::MemoryPool::~MemoryPool() -{ - delete m_memory; -} - - bool xmrig::MemoryPool::isHugePages(uint32_t) const { return m_memory && m_memory->isHugePages(); diff --git a/src/crypto/common/MemoryPool.h b/src/crypto/common/MemoryPool.h index c6b4691b8..598a55184 100644 --- a/src/crypto/common/MemoryPool.h +++ b/src/crypto/common/MemoryPool.h @@ -44,7 +44,7 @@ public: XMRIG_DISABLE_COPY_MOVE_DEFAULT(MemoryPool) MemoryPool(size_t size, bool hugePages, uint32_t node = 0); - ~MemoryPool() override; + ~MemoryPool() override = default; protected: bool isHugePages(uint32_t node) const override; @@ -55,7 +55,7 @@ private: size_t m_refs = 0; size_t m_offset = 0; size_t m_alignOffset = 0; - VirtualMemory *m_memory = nullptr; + std::shared_ptr m_memory; }; diff --git a/src/crypto/common/NUMAMemoryPool.cpp b/src/crypto/common/NUMAMemoryPool.cpp index 726700244..3784eb928 100644 --- a/src/crypto/common/NUMAMemoryPool.cpp +++ b/src/crypto/common/NUMAMemoryPool.cpp @@ -42,14 +42,6 @@ xmrig::NUMAMemoryPool::NUMAMemoryPool(size_t size, bool hugePages) : } -xmrig::NUMAMemoryPool::~NUMAMemoryPool() -{ - for (auto kv : m_map) { - delete kv.second; - } -} - - bool xmrig::NUMAMemoryPool::isHugePages(uint32_t node) const { if (!m_size) { @@ -81,7 +73,7 @@ void xmrig::NUMAMemoryPool::release(uint32_t node) xmrig::IMemoryPool *xmrig::NUMAMemoryPool::get(uint32_t node) const { - return m_map.count(node) ? m_map.at(node) : nullptr; + return m_map.count(node) ? m_map.at(node).get() : nullptr; } @@ -89,8 +81,9 @@ xmrig::IMemoryPool *xmrig::NUMAMemoryPool::getOrCreate(uint32_t node) const { auto pool = get(node); if (!pool) { - pool = new MemoryPool(m_nodeSize, m_hugePages, node); - m_map.insert({ node, pool }); + auto new_pool = std::make_shared(m_nodeSize, m_hugePages, node); + m_map.emplace(node, new_pool); + pool = new_pool.get(); } return pool; diff --git a/src/crypto/common/NUMAMemoryPool.h b/src/crypto/common/NUMAMemoryPool.h index 4e030494f..66c7d4446 100644 --- a/src/crypto/common/NUMAMemoryPool.h +++ b/src/crypto/common/NUMAMemoryPool.h @@ -47,7 +47,7 @@ public: XMRIG_DISABLE_COPY_MOVE_DEFAULT(NUMAMemoryPool) NUMAMemoryPool(size_t size, bool hugePages); - ~NUMAMemoryPool() override; + ~NUMAMemoryPool() override = default; protected: bool isHugePages(uint32_t node) const override; @@ -61,7 +61,7 @@ private: bool m_hugePages = true; size_t m_nodeSize = 0; size_t m_size = 0; - mutable std::map m_map; + mutable std::map> m_map; }; diff --git a/src/crypto/common/VirtualMemory.cpp b/src/crypto/common/VirtualMemory.cpp index e425750dd..282501622 100644 --- a/src/crypto/common/VirtualMemory.cpp +++ b/src/crypto/common/VirtualMemory.cpp @@ -38,7 +38,7 @@ namespace xmrig { size_t VirtualMemory::m_hugePageSize = VirtualMemory::kDefaultHugePageSize; -static IMemoryPool *pool = nullptr; +static std::shared_ptr pool; static std::mutex mutex; @@ -113,7 +113,7 @@ uint32_t xmrig::VirtualMemory::bindToNUMANode(int64_t) void xmrig::VirtualMemory::destroy() { - delete pool; + pool.reset(); } @@ -125,10 +125,10 @@ void xmrig::VirtualMemory::init(size_t poolSize, size_t hugePageSize) # ifdef XMRIG_FEATURE_HWLOC if (Cpu::info()->nodes() > 1) { - pool = new NUMAMemoryPool(align(poolSize, Cpu::info()->nodes()), hugePageSize > 0); + pool = std::make_shared(align(poolSize, Cpu::info()->nodes()), hugePageSize > 0); } else # endif { - pool = new MemoryPool(poolSize, hugePageSize > 0); + pool = std::make_shared(poolSize, hugePageSize > 0); } } diff --git a/src/crypto/ghostrider/ghostrider.cpp b/src/crypto/ghostrider/ghostrider.cpp index 25bb44e74..8d45ea7ff 100644 --- a/src/crypto/ghostrider/ghostrider.cpp +++ b/src/crypto/ghostrider/ghostrider.cpp @@ -312,7 +312,7 @@ void benchmark() constexpr uint32_t N = 1U << 21; VirtualMemory::init(0, N); - VirtualMemory* memory = new VirtualMemory(N * 8, true, false, false); + std::shared_ptr memory = std::make_shared(N * 8, true, false, false); // 2 MB cache per core by default size_t max_scratchpad_size = 1U << 21; @@ -438,7 +438,6 @@ void benchmark() delete helper; CnCtx::release(ctx, 8); - delete memory; }); t.join(); diff --git a/src/crypto/kawpow/KPCache.cpp b/src/crypto/kawpow/KPCache.cpp index 2a60a7878..970f74306 100644 --- a/src/crypto/kawpow/KPCache.cpp +++ b/src/crypto/kawpow/KPCache.cpp @@ -38,17 +38,6 @@ std::mutex KPCache::s_cacheMutex; KPCache KPCache::s_cache; -KPCache::KPCache() -{ -} - - -KPCache::~KPCache() -{ - delete m_memory; -} - - bool KPCache::init(uint32_t epoch) { if (epoch >= sizeof(cache_sizes) / sizeof(cache_sizes[0])) { @@ -63,8 +52,7 @@ bool KPCache::init(uint32_t epoch) const size_t size = cache_sizes[epoch]; if (!m_memory || m_memory->size() < size) { - delete m_memory; - m_memory = new VirtualMemory(size, false, false, false); + m_memory = std::make_shared(size, false, false, false); } const ethash_h256_t seedhash = ethash_get_seedhash(epoch); diff --git a/src/crypto/kawpow/KPCache.h b/src/crypto/kawpow/KPCache.h index 6f998af3b..2d330786b 100644 --- a/src/crypto/kawpow/KPCache.h +++ b/src/crypto/kawpow/KPCache.h @@ -41,8 +41,8 @@ public: XMRIG_DISABLE_COPY_MOVE(KPCache) - KPCache(); - ~KPCache(); + KPCache() = default; + ~KPCache() = default; bool init(uint32_t epoch); @@ -61,7 +61,7 @@ public: static KPCache s_cache; private: - VirtualMemory* m_memory = nullptr; + std::shared_ptr m_memory; size_t m_size = 0; uint32_t m_epoch = 0xFFFFFFFFUL; std::vector m_DAGCache; diff --git a/src/crypto/rx/Rx.cpp b/src/crypto/rx/Rx.cpp index 1d9fa2939..658c90b83 100644 --- a/src/crypto/rx/Rx.cpp +++ b/src/crypto/rx/Rx.cpp @@ -40,7 +40,7 @@ class RxPrivate; static bool osInitialized = false; -static RxPrivate *d_ptr = nullptr; +static std::shared_ptr d_ptr; class RxPrivate @@ -73,15 +73,13 @@ void xmrig::Rx::destroy() RxMsr::destroy(); # endif - delete d_ptr; - - d_ptr = nullptr; + d_ptr.reset(); } void xmrig::Rx::init(IRxListener *listener) { - d_ptr = new RxPrivate(listener); + d_ptr = std::make_shared(listener); } diff --git a/src/crypto/rx/RxBasicStorage.cpp b/src/crypto/rx/RxBasicStorage.cpp index eacb6295d..ff3dd04e2 100644 --- a/src/crypto/rx/RxBasicStorage.cpp +++ b/src/crypto/rx/RxBasicStorage.cpp @@ -44,8 +44,8 @@ public: inline ~RxBasicStoragePrivate() { deleteDataset(); } inline bool isReady(const Job &job) const { return m_ready && m_seed == job; } - inline RxDataset *dataset() const { return m_dataset; } - inline void deleteDataset() { delete m_dataset; m_dataset = nullptr; } + inline RxDataset *dataset() const { return m_dataset.get(); } + inline void deleteDataset() { m_dataset.reset(); } inline void setSeed(const RxSeed &seed) @@ -64,7 +64,7 @@ public: { const uint64_t ts = Chrono::steadyMSecs(); - m_dataset = new RxDataset(hugePages, oneGbPages, true, mode, 0); + m_dataset = std::make_shared(hugePages, oneGbPages, true, mode, 0); if (!m_dataset->cache()->get()) { deleteDataset(); @@ -117,7 +117,7 @@ private: bool m_ready = false; - RxDataset *m_dataset = nullptr; + std::shared_ptr m_dataset; RxSeed m_seed; }; @@ -133,7 +133,6 @@ xmrig::RxBasicStorage::RxBasicStorage() : xmrig::RxBasicStorage::~RxBasicStorage() { - delete d_ptr; } diff --git a/src/crypto/rx/RxBasicStorage.h b/src/crypto/rx/RxBasicStorage.h index 463b479e5..8c42f2606 100644 --- a/src/crypto/rx/RxBasicStorage.h +++ b/src/crypto/rx/RxBasicStorage.h @@ -46,7 +46,7 @@ protected: void init(const RxSeed &seed, uint32_t threads, bool hugePages, bool oneGbPages, RxConfig::Mode mode, int priority) override; private: - RxBasicStoragePrivate *d_ptr; + std::shared_ptr d_ptr; }; diff --git a/src/crypto/rx/RxCache.cpp b/src/crypto/rx/RxCache.cpp index b262c599d..ebfae0ca1 100644 --- a/src/crypto/rx/RxCache.cpp +++ b/src/crypto/rx/RxCache.cpp @@ -35,7 +35,7 @@ static_assert(RANDOMX_FLAG_JIT == 8, "RANDOMX_FLAG_JIT flag mismatch"); xmrig::RxCache::RxCache(bool hugePages, uint32_t nodeId) { - m_memory = new VirtualMemory(maxSize(), hugePages, false, false, nodeId); + m_memory = std::make_shared(maxSize(), hugePages, false, false, nodeId); create(m_memory->raw()); } @@ -50,8 +50,6 @@ xmrig::RxCache::RxCache(uint8_t *memory) xmrig::RxCache::~RxCache() { randomx_release_cache(m_cache); - - delete m_memory; } diff --git a/src/crypto/rx/RxCache.h b/src/crypto/rx/RxCache.h index d1822b30a..02c23c275 100644 --- a/src/crypto/rx/RxCache.h +++ b/src/crypto/rx/RxCache.h @@ -69,7 +69,7 @@ private: bool m_jit = true; Buffer m_seed; randomx_cache *m_cache = nullptr; - VirtualMemory *m_memory = nullptr; + std::shared_ptr m_memory; }; diff --git a/src/crypto/rx/RxDataset.cpp b/src/crypto/rx/RxDataset.cpp index 86b3a3f6d..35a0da52c 100644 --- a/src/crypto/rx/RxDataset.cpp +++ b/src/crypto/rx/RxDataset.cpp @@ -79,10 +79,7 @@ xmrig::RxDataset::RxDataset(RxCache *cache) : xmrig::RxDataset::~RxDataset() { - randomx_release_dataset(m_dataset); - delete m_cache; - delete m_memory; } @@ -107,7 +104,7 @@ bool xmrig::RxDataset::init(const Buffer &seed, uint32_t numThreads, int priorit for (uint64_t i = 0; i < numThreads; ++i) { const uint32_t a = (datasetItemCount * i) / numThreads; const uint32_t b = (datasetItemCount * (i + 1)) / numThreads; - threads.emplace_back(init_dataset_wrapper, m_dataset, m_cache->get(), a, b - a, priority); + threads.emplace_back(init_dataset_wrapper, m_dataset.get(), m_cache->get(), a, b - a, priority); } for (uint32_t i = 0; i < numThreads; ++i) { @@ -115,7 +112,7 @@ bool xmrig::RxDataset::init(const Buffer &seed, uint32_t numThreads, int priorit } } else { - init_dataset_wrapper(m_dataset, m_cache->get(), 0, datasetItemCount, priority); + init_dataset_wrapper(m_dataset.get(), m_cache->get(), 0, datasetItemCount, priority); } return true; @@ -180,7 +177,7 @@ uint8_t *xmrig::RxDataset::tryAllocateScrathpad() void *xmrig::RxDataset::raw() const { - return m_dataset ? randomx_get_dataset_memory(m_dataset) : nullptr; + return m_dataset ? randomx_get_dataset_memory(m_dataset.get()) : nullptr; } @@ -191,7 +188,7 @@ void xmrig::RxDataset::setRaw(const void *raw) } volatile size_t N = maxSize(); - memcpy(randomx_get_dataset_memory(m_dataset), raw, N); + memcpy(randomx_get_dataset_memory(m_dataset.get()), raw, N); } @@ -199,24 +196,22 @@ void xmrig::RxDataset::allocate(bool hugePages, bool oneGbPages) { if (m_mode == RxConfig::LightMode) { LOG_ERR(CLEAR "%s" RED_BOLD_S "fast RandomX mode disabled by config", Tags::randomx()); - return; } if (m_mode == RxConfig::AutoMode && uv_get_total_memory() < (maxSize() + RxCache::maxSize())) { LOG_ERR(CLEAR "%s" RED_BOLD_S "not enough memory for RandomX dataset", Tags::randomx()); - return; } - m_memory = new VirtualMemory(maxSize(), hugePages, oneGbPages, false, m_node); + m_memory = std::make_shared(maxSize(), hugePages, oneGbPages, false, m_node); if (m_memory->isOneGbPages()) { m_scratchpadOffset = maxSize() + RANDOMX_CACHE_MAX_SIZE; m_scratchpadLimit = m_memory->capacity(); } - m_dataset = randomx_create_dataset(m_memory->raw()); + m_dataset = std::shared_ptr(randomx_create_dataset(m_memory->raw()), randomx_release_dataset); # ifdef XMRIG_OS_LINUX if (oneGbPages && !isOneGbPages()) { diff --git a/src/crypto/rx/RxDataset.h b/src/crypto/rx/RxDataset.h index 4f9caadf4..6b7bc267a 100644 --- a/src/crypto/rx/RxDataset.h +++ b/src/crypto/rx/RxDataset.h @@ -50,7 +50,7 @@ public: RxDataset(RxCache *cache); ~RxDataset(); - inline randomx_dataset *get() const { return m_dataset; } + inline randomx_dataset *get() const { return m_dataset.get(); } inline RxCache *cache() const { return m_cache; } inline void setCache(RxCache *cache) { m_cache = cache; } @@ -70,11 +70,11 @@ private: const RxConfig::Mode m_mode = RxConfig::FastMode; const uint32_t m_node; - randomx_dataset *m_dataset = nullptr; + std::shared_ptr m_dataset; RxCache *m_cache = nullptr; size_t m_scratchpadLimit = 0; std::atomic m_scratchpadOffset{}; - VirtualMemory *m_memory = nullptr; + std::shared_ptr m_memory; }; diff --git a/src/crypto/rx/RxQueue.cpp b/src/crypto/rx/RxQueue.cpp index 489a7bc7d..1aaab5435 100644 --- a/src/crypto/rx/RxQueue.cpp +++ b/src/crypto/rx/RxQueue.cpp @@ -49,8 +49,6 @@ xmrig::RxQueue::~RxQueue() m_cv.notify_one(); m_thread.join(); - - delete m_storage; } @@ -90,12 +88,12 @@ void xmrig::RxQueue::enqueue(const RxSeed &seed, const std::vector &no if (!m_storage) { # ifdef XMRIG_FEATURE_HWLOC if (!nodeset.empty()) { - m_storage = new RxNUMAStorage(nodeset); + m_storage = std::make_shared(nodeset); } else # endif { - m_storage = new RxBasicStorage(); + m_storage = std::make_shared(); } } diff --git a/src/crypto/rx/RxQueue.h b/src/crypto/rx/RxQueue.h index 7583ce7b9..7499d7ef7 100644 --- a/src/crypto/rx/RxQueue.h +++ b/src/crypto/rx/RxQueue.h @@ -94,7 +94,7 @@ private: void onReady(); IRxListener *m_listener = nullptr; - IRxStorage *m_storage = nullptr; + std::shared_ptr m_storage; RxSeed m_seed; State m_state = STATE_IDLE; std::condition_variable m_cv; diff --git a/src/crypto/rx/RxVm.cpp b/src/crypto/rx/RxVm.cpp index acaa25e05..f5d941ba5 100644 --- a/src/crypto/rx/RxVm.cpp +++ b/src/crypto/rx/RxVm.cpp @@ -25,7 +25,7 @@ #include "crypto/rx/RxVm.h" -randomx_vm *xmrig::RxVm::create(RxDataset *dataset, uint8_t *scratchpad, bool softAes, const Assembly &assembly, uint32_t node) +std::shared_ptr xmrig::RxVm::create(RxDataset *dataset, uint8_t *scratchpad, bool softAes, const Assembly &assembly, uint32_t node) { int flags = 0; @@ -46,13 +46,8 @@ randomx_vm *xmrig::RxVm::create(RxDataset *dataset, uint8_t *scratchpad, bool so flags |= RANDOMX_FLAG_AMD; } - return randomx_create_vm(static_cast(flags), !dataset->get() ? dataset->cache()->get() : nullptr, dataset->get(), scratchpad, node); + return std::shared_ptr(randomx_create_vm( + static_cast(flags), !dataset->get() ? dataset->cache()->get() : nullptr, dataset->get(), scratchpad, node), + randomx_destroy_vm); } - -void xmrig::RxVm::destroy(randomx_vm* vm) -{ - if (vm) { - randomx_destroy_vm(vm); - } -} diff --git a/src/crypto/rx/RxVm.h b/src/crypto/rx/RxVm.h index 424da209a..000244d76 100644 --- a/src/crypto/rx/RxVm.h +++ b/src/crypto/rx/RxVm.h @@ -38,8 +38,7 @@ class RxDataset; class RxVm { public: - static randomx_vm *create(RxDataset *dataset, uint8_t *scratchpad, bool softAes, const Assembly &assembly, uint32_t node); - static void destroy(randomx_vm *vm); + static std::shared_ptr create(RxDataset *dataset, uint8_t *scratchpad, bool softAes, const Assembly &assembly, uint32_t node); }; diff --git a/src/hw/msr/Msr.h b/src/hw/msr/Msr.h index 3bd88dd56..5d9a10199 100644 --- a/src/hw/msr/Msr.h +++ b/src/hw/msr/Msr.h @@ -59,7 +59,7 @@ private: bool rdmsr(uint32_t reg, int32_t cpu, uint64_t &value) const; bool wrmsr(uint32_t reg, uint64_t value, int32_t cpu); - MsrPrivate *d_ptr = nullptr; + std::shared_ptr d_ptr; }; diff --git a/src/hw/msr/Msr_linux.cpp b/src/hw/msr/Msr_linux.cpp index 29bb8ad1d..fb2f98c79 100644 --- a/src/hw/msr/Msr_linux.cpp +++ b/src/hw/msr/Msr_linux.cpp @@ -72,11 +72,9 @@ private: const bool m_available; }; - } // namespace xmrig - -xmrig::Msr::Msr() : d_ptr(new MsrPrivate()) +xmrig::Msr::Msr() : d_ptr(std::make_shared()) { if (!isAvailable()) { LOG_WARN("%s " YELLOW_BOLD("msr kernel module is not available"), tag()); @@ -86,7 +84,6 @@ xmrig::Msr::Msr() : d_ptr(new MsrPrivate()) xmrig::Msr::~Msr() { - delete d_ptr; } diff --git a/src/hw/msr/Msr_win.cpp b/src/hw/msr/Msr_win.cpp index ee5cc85af..876c14ee4 100644 --- a/src/hw/msr/Msr_win.cpp +++ b/src/hw/msr/Msr_win.cpp @@ -85,7 +85,7 @@ public: } // namespace xmrig -xmrig::Msr::Msr() : d_ptr(new MsrPrivate()) +xmrig::Msr::Msr() : d_ptr(std::make_shared()) { DWORD err = 0; @@ -195,8 +195,6 @@ xmrig::Msr::Msr() : d_ptr(new MsrPrivate()) xmrig::Msr::~Msr() { d_ptr->uninstall(); - - delete d_ptr; } diff --git a/src/net/JobResults.cpp b/src/net/JobResults.cpp index 19a1dc43c..928f9ff4a 100644 --- a/src/net/JobResults.cpp +++ b/src/net/JobResults.cpp @@ -133,12 +133,10 @@ static void getResults(JobBundle &bundle, std::vector &results, uint3 for (uint32_t nonce : bundle.nonces) { *bundle.job.nonce() = nonce; - randomx_calculate_hash(vm, bundle.job.blob(), bundle.job.size(), hash); + randomx_calculate_hash(vm.get(), bundle.job.blob(), bundle.job.size(), hash); checkHash(bundle, results, nonce, hash, errors); } - - RxVm::destroy(vm); # endif } else if (algorithm.family() == Algorithm::ARGON2) { @@ -303,7 +301,7 @@ private: }; -static JobResultsPrivate *handler = nullptr; +static std::shared_ptr handler; } // namespace xmrig @@ -317,19 +315,17 @@ void xmrig::JobResults::done(const Job &job) void xmrig::JobResults::setListener(IJobResultListener *listener, bool hwAES) { - assert(handler == nullptr); + assert(!handler); - handler = new JobResultsPrivate(listener, hwAES); + handler = std::make_shared(listener, hwAES); } void xmrig::JobResults::stop() { - assert(handler != nullptr); + assert(handler); - delete handler; - - handler = nullptr; + handler.reset(); } @@ -347,7 +343,7 @@ void xmrig::JobResults::submit(const Job& job, uint32_t nonce, const uint8_t* re void xmrig::JobResults::submit(const JobResult &result) { - assert(handler != nullptr); + assert(handler); if (handler) { handler->submit(result); diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 0ca1914a4..70d483b9e 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -67,27 +67,23 @@ xmrig::Network::Network(Controller *controller) : controller->api()->addListener(this); # endif - m_state = new NetworkState(this); + m_state = std::make_shared(this); const Pools &pools = controller->config()->pools(); - m_strategy = pools.createStrategy(m_state); + m_strategy = pools.createStrategy(m_state.get()); if (pools.donateLevel() > 0) { - m_donate = new DonateStrategy(controller, this); + m_donate = std::make_shared(controller, this); } - m_timer = new Timer(this, kTickInterval, kTickInterval); + static constexpr int kTickInterval = 1 * 1000; + m_timer = std::make_shared(this, kTickInterval, kTickInterval); } xmrig::Network::~Network() { JobResults::stop(); - - delete m_timer; - delete m_donate; - delete m_strategy; - delete m_state; } @@ -118,7 +114,7 @@ void xmrig::Network::execCommand(char command) void xmrig::Network::onActive(IStrategy *strategy, IClient *client) { - if (m_donate && m_donate == strategy) { + if (m_donate && m_donate.get() == strategy) { LOG_NOTICE("%s " WHITE_BOLD("dev donate started"), Tags::network()); return; } @@ -157,19 +153,18 @@ void xmrig::Network::onConfigChanged(Config *config, Config *previousConfig) config->pools().print(); - delete m_strategy; - m_strategy = config->pools().createStrategy(m_state); + m_strategy = config->pools().createStrategy(m_state.get()); connect(); } void xmrig::Network::onJob(IStrategy *strategy, IClient *client, const Job &job, const rapidjson::Value &) { - if (m_donate && m_donate->isActive() && m_donate != strategy) { + if (m_donate && m_donate->isActive() && m_donate.get() != strategy) { return; } - setJob(client, job, m_donate == strategy); + setJob(client, job, m_donate.get() == strategy); } @@ -210,7 +205,7 @@ void xmrig::Network::onLogin(IStrategy *, IClient *client, rapidjson::Document & void xmrig::Network::onPause(IStrategy *strategy) { - if (m_donate && m_donate == strategy) { + if (m_donate && m_donate.get() == strategy) { LOG_NOTICE("%s " WHITE_BOLD("dev donate finished"), Tags::network()); m_strategy->resume(); } @@ -292,7 +287,7 @@ void xmrig::Network::setJob(IClient *client, const Job &job, bool donate) } if (!donate && m_donate) { - static_cast(m_donate)->update(client, job); + static_cast(*m_donate).update(client, job); } m_controller->miner()->setJob(job, donate); diff --git a/src/net/Network.h b/src/net/Network.h index b936c0d55..48a7d5068 100644 --- a/src/net/Network.h +++ b/src/net/Network.h @@ -30,7 +30,7 @@ #include "interfaces/IJobResultListener.h" -#include +#include namespace xmrig { @@ -49,7 +49,7 @@ public: Network(Controller *controller); ~Network() override; - inline IStrategy *strategy() const { return m_strategy; } + inline IStrategy *strategy() const { return m_strategy.get(); } void connect(); void execCommand(char command); @@ -64,15 +64,13 @@ protected: void onLogin(IStrategy *strategy, IClient *client, rapidjson::Document &doc, rapidjson::Value ¶ms) override; void onPause(IStrategy *strategy) override; void onResultAccepted(IStrategy *strategy, IClient *client, const SubmitResult &result, const char *error) override; - void onVerifyAlgorithm(IStrategy *strategy, const IClient *client, const Algorithm &algorithm, bool *ok) override; + void onVerifyAlgorithm(IStrategy *strategy, const IClient *client, const Algorithm &algorithm, bool *ok) override; # ifdef XMRIG_FEATURE_API void onRequest(IApiRequest &request) override; # endif private: - constexpr static int kTickInterval = 1 * 1000; - void setJob(IClient *client, const Job &job, bool donate); void tick(); @@ -82,10 +80,10 @@ private: # endif Controller *m_controller; - IStrategy *m_donate = nullptr; - IStrategy *m_strategy = nullptr; - NetworkState *m_state = nullptr; - Timer *m_timer = nullptr; + std::shared_ptr m_donate; + std::shared_ptr m_strategy; + std::shared_ptr m_state; + std::shared_ptr m_timer; }; diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 1f647ae41..4eaa45bb9 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -75,13 +75,13 @@ xmrig::DonateStrategy::DonateStrategy(Controller *controller, IStrategyListener m_pools.emplace_back(kDonateHost, 3333, m_userId, nullptr, nullptr, 0, true, false, mode); if (m_pools.size() > 1) { - m_strategy = new FailoverStrategy(m_pools, 10, 2, this, true); + m_strategy = std::make_shared(m_pools, 10, 2, this, true); } else { - m_strategy = new SinglePoolStrategy(m_pools.front(), 10, 2, this, true); + m_strategy = std::make_shared(m_pools.front(), 10, 2, this, true); } - m_timer = new Timer(this); + m_timer = std::make_shared(this); setState(STATE_IDLE); } @@ -89,8 +89,8 @@ xmrig::DonateStrategy::DonateStrategy(Controller *controller, IStrategyListener xmrig::DonateStrategy::~DonateStrategy() { - delete m_timer; - delete m_strategy; + m_timer.reset(); + m_strategy.reset(); if (m_proxy) { m_proxy->deleteLater(); @@ -237,7 +237,7 @@ void xmrig::DonateStrategy::onVerifyAlgorithm(const IClient *client, const Algor } -void xmrig::DonateStrategy::onVerifyAlgorithm(IStrategy *, const IClient *client, const Algorithm &algorithm, bool *ok) +void xmrig::DonateStrategy::onVerifyAlgorithm(IStrategy *, const IClient *client, const Algorithm &algorithm, bool *ok) { m_listener->onVerifyAlgorithm(this, client, algorithm, ok); } @@ -249,7 +249,7 @@ void xmrig::DonateStrategy::onTimer(const Timer *) } -xmrig::IClient *xmrig::DonateStrategy::createProxy() +std::shared_ptr xmrig::DonateStrategy::createProxy() { if (m_controller->config()->pools().proxyDonate() == Pools::PROXY_DONATE_NONE) { return nullptr; @@ -267,7 +267,7 @@ xmrig::IClient *xmrig::DonateStrategy::createProxy() pool.setAlgo(client->pool().algorithm()); pool.setProxy(client->pool().proxy()); - IClient *proxy = new Client(-1, Platform::userAgent(), this); + std::shared_ptr proxy = std::make_shared(-1, Platform::userAgent(), this); proxy->setPool(pool); proxy->setQuiet(true); diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index 80ec45ca1..53c9e86fb 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -47,7 +47,7 @@ public: protected: inline bool isActive() const override { return state() == STATE_ACTIVE; } - inline IClient *client() const override { return m_proxy ? m_proxy : m_strategy->client(); } + inline IClient *client() const override { return m_proxy ? m_proxy.get() : m_strategy->client(); } inline void onJob(IStrategy *, IClient *client, const Job &job, const rapidjson::Value ¶ms) override { setJob(client, job, params); } inline void onJobReceived(IClient *client, const Job &job, const rapidjson::Value ¶ms) override { setJob(client, job, params); } inline void onResultAccepted(IClient *client, const SubmitResult &result, const char *error) override { setResult(client, result, error); } @@ -69,7 +69,7 @@ protected: void onLogin(IStrategy *strategy, IClient *client, rapidjson::Document &doc, rapidjson::Value ¶ms) override; void onLoginSuccess(IClient *client) override; void onVerifyAlgorithm(const IClient *client, const Algorithm &algorithm, bool *ok) override; - void onVerifyAlgorithm(IStrategy *strategy, const IClient *client, const Algorithm &algorithm, bool *ok) override; + void onVerifyAlgorithm(IStrategy *strategy, const IClient *client, const Algorithm &algorithm, bool *ok) override; void onTimer(const Timer *timer) override; @@ -84,7 +84,7 @@ private: inline State state() const { return m_state; } - IClient *createProxy(); + std::shared_ptr createProxy(); void idle(double min, double max); void setJob(IClient *client, const Job &job, const rapidjson::Value ¶ms); void setParams(rapidjson::Document &doc, rapidjson::Value ¶ms); @@ -98,12 +98,12 @@ private: const uint64_t m_donateTime; const uint64_t m_idleTime; Controller *m_controller; - IClient *m_proxy = nullptr; - IStrategy *m_strategy = nullptr; + std::shared_ptr m_proxy; + std::shared_ptr m_strategy; IStrategyListener *m_listener; State m_state = STATE_NEW; std::vector m_pools; - Timer *m_timer = nullptr; + std::shared_ptr m_timer; uint64_t m_diff = 0; uint64_t m_height = 0; uint64_t m_now = 0;