diff options
author | jbzdarkid <jbzdarkid@gmail.com> | 2018-10-24 18:44:37 -0700 |
---|---|---|
committer | jbzdarkid <jbzdarkid@gmail.com> | 2018-10-24 18:44:37 -0700 |
commit | 2473736979b829e09130a7261b38eb8c1367a06d (patch) | |
tree | a8fd86b1a464a96420bc7f4cf1028d0fb2d01b41 /WitnessRandomizer/Memory.cpp | |
parent | 922dbb03a50a54f3e5ae9efaec3b4759cd701c3e (diff) | |
download | witness-tutorializer-2473736979b829e09130a7261b38eb8c1367a06d.tar.gz witness-tutorializer-2473736979b829e09130a7261b38eb8c1367a06d.tar.bz2 witness-tutorializer-2473736979b829e09130a7261b38eb8c1367a06d.zip |
Perf: Cache memory offsets
Diffstat (limited to 'WitnessRandomizer/Memory.cpp')
-rw-r--r-- | WitnessRandomizer/Memory.cpp | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/WitnessRandomizer/Memory.cpp b/WitnessRandomizer/Memory.cpp index c5852ac..f2bd8f1 100644 --- a/WitnessRandomizer/Memory.cpp +++ b/WitnessRandomizer/Memory.cpp | |||
@@ -58,16 +58,22 @@ void Memory::ThrowError() { | |||
58 | 58 | ||
59 | uintptr_t Memory::ComputeOffset(std::vector<int> offsets) | 59 | uintptr_t Memory::ComputeOffset(std::vector<int> offsets) |
60 | { | 60 | { |
61 | uintptr_t cumulativeAddress = _baseAddress; | ||
62 | |||
63 | // Leave off the last offset, since it will be either read/write, and may not be of type unitptr_t. | 61 | // Leave off the last offset, since it will be either read/write, and may not be of type unitptr_t. |
64 | int final_offset = offsets.back(); | 62 | int final_offset = offsets.back(); |
65 | offsets.pop_back(); | 63 | offsets.pop_back(); |
64 | |||
65 | auto search = _computedOffsets.find(offsets); | ||
66 | if (search != std::end(_computedOffsets)) { | ||
67 | return search->second + final_offset; | ||
68 | } | ||
69 | |||
70 | uintptr_t cumulativeAddress = _baseAddress; | ||
66 | for (int offset : offsets) { | 71 | for (int offset : offsets) { |
67 | cumulativeAddress += offset; | 72 | cumulativeAddress += offset; |
68 | if (!ReadProcessMemory(_handle, (LPVOID)cumulativeAddress, &cumulativeAddress, sizeof(uintptr_t), NULL)) { | 73 | if (!ReadProcessMemory(_handle, (LPVOID)cumulativeAddress, &cumulativeAddress, sizeof(uintptr_t), NULL)) { |
69 | ThrowError(); | 74 | ThrowError(); |
70 | } | 75 | } |
71 | } | 76 | } |
77 | _computedOffsets[offsets] = cumulativeAddress; | ||
72 | return cumulativeAddress + final_offset; | 78 | return cumulativeAddress + final_offset; |
73 | } | 79 | } |