From 03e1249177b2c6aa22a917c742d9b057f5640e27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aurimas=20Bla=C5=BEulionis?= <0x60@pm.me> Date: Fri, 5 Oct 2018 20:42:14 +0300 Subject: [PATCH] Remove constant heap allocations and deallocations when using WriteList. --- hlapi/hlapi.h | 8 +++++--- hlapi/winprocess.cpp | 7 +++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/hlapi/hlapi.h b/hlapi/hlapi.h index 77576d6..626a66e 100644 --- a/hlapi/hlapi.h +++ b/hlapi/hlapi.h @@ -9,6 +9,7 @@ #include #include #include +#include class VMException : public std::exception { @@ -146,13 +147,14 @@ class WriteList template void Write(uint64_t address, T& value) { - T* copy = (T*)malloc(sizeof(T)); - *copy = value; - writeList.push_back({(uint64_t)copy, address, sizeof(T)}); + writeList.push_back({(uint64_t)buffer.size(), address, sizeof(T)}); + buffer.reserve(sizeof(T)); + std::copy((char*)&value, (char*)&value + sizeof(T), std::back_inserter(buffer)); } private: std::vector writeList; + std::vector buffer; WinCtx* ctx; WinProc* proc; }; diff --git a/hlapi/winprocess.cpp b/hlapi/winprocess.cpp index acb9cc6..81da995 100644 --- a/hlapi/winprocess.cpp +++ b/hlapi/winprocess.cpp @@ -37,12 +37,15 @@ void WriteList::Commit() RWInfo* infos = writeList.data(); - VMemWriteMul(&ctx->process, proc->dirBase, infos, sz); + uint64_t bufmem = (uint64_t)buffer.data(); for (size_t i = 0; i < sz; i++) - free((void*)infos[i].local); + infos[i].local += bufmem; + + VMemWriteMul(&ctx->process, proc->dirBase, infos, sz); writeList.clear(); + buffer.clear(); } WinDll* WinProcess::GetModuleInfo(const char* moduleName)