From 41abe17286d66c812f12f48907d9250319beea5d Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 13 Mar 2018 20:59:35 +0700 Subject: [PATCH] #438 Fixed memory release. --- src/Mem.cpp | 1 + src/Mem.h | 1 + src/Mem_unix.cpp | 22 ++++++++++------------ src/Mem_win.cpp | 8 ++++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Mem.cpp b/src/Mem.cpp index 73576d204..9c608f73c 100644 --- a/src/Mem.cpp +++ b/src/Mem.cpp @@ -37,6 +37,7 @@ int Mem::m_algo = 0; int Mem::m_flags = 0; int Mem::m_threads = 0; size_t Mem::m_offset = 0; +size_t Mem::m_size = 0; uint8_t *Mem::m_memory = nullptr; diff --git a/src/Mem.h b/src/Mem.h index a9635a177..eaa4f49a8 100644 --- a/src/Mem.h +++ b/src/Mem.h @@ -62,6 +62,7 @@ private: static int m_flags; static int m_threads; static size_t m_offset; + static size_t m_size; VAR_ALIGN(16, static uint8_t *m_memory); # ifndef XMRIG_NO_AEON diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index 93cdbec04..f09b5096e 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -47,8 +47,8 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_threads = threads; m_doubleHash = doubleHash; - const int ratio = (doubleHash && algo != xmrig::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; - const size_t size = MONERO_MEMORY * (threads * ratio + 1); + const int ratio = (doubleHash && algo != xmrig::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; + m_size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { m_memory = static_cast(_mm_malloc(size, 16)); @@ -58,24 +58,24 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_flags |= HugepagesAvailable; # if defined(__APPLE__) - m_memory = static_cast(mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0)); + m_memory = static_cast(mmap(0, m_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0)); # elif defined(__FreeBSD__) - m_memory = static_cast(mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER | MAP_PREFAULT_READ, -1, 0)); + m_memory = static_cast(mmap(0, m_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER | MAP_PREFAULT_READ, -1, 0)); # else - m_memory = static_cast(mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, 0, 0)); + m_memory = static_cast(mmap(0, m_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, 0, 0)); # endif if (m_memory == MAP_FAILED) { - m_memory = static_cast(_mm_malloc(size, 16)); + m_memory = static_cast(_mm_malloc(m_size, 16)); return true; } m_flags |= HugepagesEnabled; - if (madvise(m_memory, size, MADV_RANDOM | MADV_WILLNEED) != 0) { + if (madvise(m_memory, m_size, MADV_RANDOM | MADV_WILLNEED) != 0) { LOG_ERR("madvise failed"); } - if (mlock(m_memory, size) == 0) { + if (mlock(m_memory, m_size) == 0) { m_flags |= Lock; } @@ -85,14 +85,12 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) void Mem::release() { - const int size = MONERO_MEMORY * (m_threads + 1); - if (m_flags & HugepagesEnabled) { if (m_flags & Lock) { - munlock(m_memory, size); + munlock(m_memory, m_size); } - munmap(m_memory, size); + munmap(m_memory, m_size); } else { _mm_free(m_memory); diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index 28eaff496..239bda7d8 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -153,10 +153,10 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_doubleHash = doubleHash; const int ratio = (doubleHash && algo != xmrig::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; - const size_t size = MONERO_MEMORY * (threads * ratio + 1); + m_size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { - m_memory = static_cast(_mm_malloc(size, 16)); + m_memory = static_cast(_mm_malloc(m_size, 16)); return true; } @@ -164,9 +164,9 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_flags |= HugepagesAvailable; } - m_memory = static_cast(VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE)); + m_memory = static_cast(VirtualAlloc(NULL, m_size, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE)); if (!m_memory) { - m_memory = static_cast(_mm_malloc(size, 16)); + m_memory = static_cast(_mm_malloc(m_size, 16)); } else { m_flags |= HugepagesEnabled;