Skip to content
This repository has been archived by the owner on Sep 29, 2020. It is now read-only.

Commit

Permalink
Remove constant heap allocations and deallocations when using WriteList.
Browse files Browse the repository at this point in the history
  • Loading branch information
h33p committed Oct 5, 2018
1 parent 2a22be6 commit 03e1249
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
8 changes: 5 additions & 3 deletions hlapi/hlapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <stdexcept>
#include <string.h>
#include <vector>
#include <algorithm>

class VMException : public std::exception
{
Expand Down Expand Up @@ -146,13 +147,14 @@ class WriteList
template<typename T>
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<RWInfo> writeList;
std::vector<char> buffer;
WinCtx* ctx;
WinProc* proc;
};
Expand Down
7 changes: 5 additions & 2 deletions hlapi/winprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 03e1249

Please sign in to comment.