From 497863441a3fb04a8b116fc501889f634c51ee9a Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 12 Dec 2020 12:39:11 +0700 Subject: [PATCH] Remove duplicated code. --- src/crypto/common/VirtualMemory.h | 14 ++++++++------ src/crypto/common/VirtualMemory_unix.cpp | 9 ++++++--- src/crypto/randomx/jit_compiler_a64.cpp | 21 +++------------------ 3 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/crypto/common/VirtualMemory.h b/src/crypto/common/VirtualMemory.h index a7f03e0f..e0065e3e 100644 --- a/src/crypto/common/VirtualMemory.h +++ b/src/crypto/common/VirtualMemory.h @@ -42,12 +42,14 @@ public: VirtualMemory(size_t size, bool hugePages, bool oneGbPages, bool usePool, uint32_t node = 0, size_t alignSize = 64); ~VirtualMemory(); - inline bool isHugePages() const { return m_flags.test(FLAG_HUGEPAGES); } - inline bool isOneGbPages() const { return m_flags.test(FLAG_1GB_PAGES); } - inline size_t size() const { return m_size; } - inline size_t capacity() const { return m_capacity; } - inline uint8_t *raw() const { return m_scratchpad; } - inline uint8_t *scratchpad() const { return m_scratchpad; } + inline bool isHugePages() const { return m_flags.test(FLAG_HUGEPAGES); } + inline bool isOneGbPages() const { return m_flags.test(FLAG_1GB_PAGES); } + inline size_t size() const { return m_size; } + inline size_t capacity() const { return m_capacity; } + inline uint8_t *raw() const { return m_scratchpad; } + inline uint8_t *scratchpad() const { return m_scratchpad; } + + inline static void flushInstructionCache(void *p1, void *p2) { flushInstructionCache(p1, static_cast(p2) - static_cast(p1)); } HugePagesInfo hugePages() const; diff --git a/src/crypto/common/VirtualMemory_unix.cpp b/src/crypto/common/VirtualMemory_unix.cpp index a523ea76..ba2da31b 100644 --- a/src/crypto/common/VirtualMemory_unix.cpp +++ b/src/crypto/common/VirtualMemory_unix.cpp @@ -27,9 +27,10 @@ #include "crypto/common/VirtualMemory.h" -#ifdef __APPLE__ -# include +#ifdef XMRIG_OS_APPLE +# include # include +# include # ifdef XMRIG_OS_MACOS # define MEXTRA MAP_JIT # else @@ -178,7 +179,9 @@ void *xmrig::VirtualMemory::allocateOneGbPagesMemory(size_t size) void xmrig::VirtualMemory::flushInstructionCache(void *p, size_t size) { -# ifdef HAVE_BUILTIN_CLEAR_CACHE +# if defined(XMRIG_OS_APPLE) + sys_icache_invalidate(p, size); +# elif defined (HAVE_BUILTIN_CLEAR_CACHE) || defined (__GNUC__) __builtin___clear_cache(reinterpret_cast(p), reinterpret_cast(p) + size); # endif } diff --git a/src/crypto/randomx/jit_compiler_a64.cpp b/src/crypto/randomx/jit_compiler_a64.cpp index 4d9a34ca..97f78109 100644 --- a/src/crypto/randomx/jit_compiler_a64.cpp +++ b/src/crypto/randomx/jit_compiler_a64.cpp @@ -112,21 +112,6 @@ JitCompilerA64::~JitCompilerA64() freePagedMemory(code, CodeSize + CalcDatasetItemSize()); } -#if defined(ios_HOST_OS) || defined (darwin_HOST_OS) -void sys_icache_invalidate(void *start, size_t len); -#endif - -static void clear_code_cache(char* p1, char* p2) -{ -# if defined(ios_HOST_OS) || defined (darwin_HOST_OS) - sys_icache_invalidate(p1, static_cast(p2 - p1)); -# elif defined (HAVE_BUILTIN_CLEAR_CACHE) || defined (__GNUC__) - __builtin___clear_cache(p1, p2); -# else -# error "No clear code cache function found" -# endif -} - void JitCompilerA64::generateProgram(Program& program, ProgramConfiguration& config, uint32_t) { uint32_t codePos = MainLoopBegin + 4; @@ -173,7 +158,7 @@ void JitCompilerA64::generateProgram(Program& program, ProgramConfiguration& con codePos = ((uint8_t*)randomx_program_aarch64_update_spMix1) - ((uint8_t*)randomx_program_aarch64); emit32(ARMV8A::EOR | 10 | (IntRegMap[config.readReg0] << 5) | (IntRegMap[config.readReg1] << 16), code, codePos); - clear_code_cache(reinterpret_cast(code + MainLoopBegin), reinterpret_cast(code + codePos)); + xmrig::VirtualMemory::flushInstructionCache(reinterpret_cast(code + MainLoopBegin), reinterpret_cast(code + codePos)); } void JitCompilerA64::generateProgramLight(Program& program, ProgramConfiguration& config, uint32_t datasetOffset) @@ -228,7 +213,7 @@ void JitCompilerA64::generateProgramLight(Program& program, ProgramConfiguration emit32(ARMV8A::ADD_IMM_LO | 2 | (2 << 5) | (imm_lo << 10), code, codePos); emit32(ARMV8A::ADD_IMM_HI | 2 | (2 << 5) | (imm_hi << 10), code, codePos); - clear_code_cache(reinterpret_cast(code + MainLoopBegin), reinterpret_cast(code + codePos)); + xmrig::VirtualMemory::flushInstructionCache(reinterpret_cast(code + MainLoopBegin), reinterpret_cast(code + codePos)); } template @@ -344,7 +329,7 @@ void JitCompilerA64::generateSuperscalarHash(SuperscalarProgram(&programs)[N]) memcpy(code + codePos, p1, p2 - p1); codePos += p2 - p1; - clear_code_cache(reinterpret_cast(code + CodeSize), reinterpret_cast(code + codePos)); + xmrig::VirtualMemory::flushInstructionCache(reinterpret_cast(code + CodeSize), reinterpret_cast(code + codePos)); } template void JitCompilerA64::generateSuperscalarHash(SuperscalarProgram(&programs)[RANDOMX_CACHE_MAX_ACCESSES]);