From bdaf28adf814cda247db0592c403b8ee4c92a321 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 2 Aug 2019 14:44:38 +0700 Subject: [PATCH] Unified memory allocation functions. --- src/crypto/randomx/virtual_memory.cpp | 81 ++++++--------------------- 1 file changed, 17 insertions(+), 64 deletions(-) diff --git a/src/crypto/randomx/virtual_memory.cpp b/src/crypto/randomx/virtual_memory.cpp index 925e8e86..661740fc 100644 --- a/src/crypto/randomx/virtual_memory.cpp +++ b/src/crypto/randomx/virtual_memory.cpp @@ -26,80 +26,33 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "virtual_memory.hpp" - #include -#if defined(_WIN32) || defined(__CYGWIN__) -#include -#else -#ifdef __APPLE__ -#include -#endif -#include -#include -#ifndef MAP_ANONYMOUS -#define MAP_ANONYMOUS MAP_ANON -#endif -#endif -#if defined(_WIN32) || defined(__CYGWIN__) -std::string getErrorMessage(const char* function) { - LPSTR messageBuffer = nullptr; - size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL); - std::string message(messageBuffer, size); - LocalFree(messageBuffer); - return std::string(function) + std::string(": ") + message; -} -#endif +#include "crypto/common/VirtualMemory.h" +#include "virtual_memory.hpp" + void* allocExecutableMemory(std::size_t bytes) { - void* mem; -#if defined(_WIN32) || defined(__CYGWIN__) - mem = VirtualAlloc(nullptr, bytes, MEM_COMMIT, PAGE_EXECUTE_READWRITE); - if (mem == nullptr) - throw std::runtime_error(getErrorMessage("allocExecutableMemory - VirtualAlloc")); -#else - mem = mmap(nullptr, bytes, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); - if (mem == MAP_FAILED) - throw std::runtime_error("allocExecutableMemory - mmap failed"); -#endif - return mem; + void *mem = xmrig::VirtualMemory::allocateExecutableMemory(bytes); + if (mem == nullptr) { + throw std::runtime_error("Failed to allocate executable memory"); + } + + return mem; } -constexpr std::size_t align(std::size_t pos, std::size_t align) { - return ((pos - 1) / align + 1) * align; -} void* allocLargePagesMemory(std::size_t bytes) { - void* mem; -#if defined(_WIN32) || defined(__CYGWIN__) - auto pageMinimum = GetLargePageMinimum(); - if (pageMinimum > 0) - mem = VirtualAlloc(NULL, align(bytes, pageMinimum), MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE); - else - throw std::runtime_error("allocLargePagesMemory - Large pages are not supported"); - if (mem == nullptr) - throw std::runtime_error(getErrorMessage("allocLargePagesMemory - VirtualAlloc")); -#else -#ifdef __APPLE__ - mem = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0); -#elif defined(__FreeBSD__) - mem = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER, -1, 0); -#else - mem = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, -1, 0); -#endif - if (mem == MAP_FAILED) - throw std::runtime_error("allocLargePagesMemory - mmap failed"); -#endif - return mem; + void *mem = xmrig::VirtualMemory::allocateLargePagesMemory(bytes); + if (mem == nullptr) { + throw std::runtime_error("Failed to allocate large pages memory"); + } + + return mem; } + void freePagedMemory(void* ptr, std::size_t bytes) { -#if defined(_WIN32) || defined(__CYGWIN__) - VirtualFree(ptr, 0, MEM_RELEASE); -#else - munmap(ptr, bytes); -#endif + xmrig::VirtualMemory::freeLargePagesMemory(ptr, bytes); }