Skip to content

Instantly share code, notes, and snippets.

@ivanstepanovftw
Last active July 29, 2018 08:42
Show Gist options
  • Save ivanstepanovftw/133378460328f88545ddf24e1684c3ce to your computer and use it in GitHub Desktop.
Save ivanstepanovftw/133378460328f88545ddf24e1684c3ce to your computer and use it in GitHub Desktop.
/*
* Rewritten from https://github.com/scanmem/scanmem/blob/d77b1713e548f6e26e62a3e146f852813bbee6d9/targetmem.h
*/
#include <iostream>
#include <vector>
#include <cstring>
#include <chrono>
using namespace std;
using namespace std::chrono;
/* Single match struct */
#pragma pack(push, 1)
struct byte_with_flags
{
uint8_t byte; // Remote process byte
uint16_t flags; // Flag
};
#pragma pack(pop)
class swath_t
{
public:
uintptr_t base_address; // Base remote address
vector<byte_with_flags> data; //
explicit swath_t(uintptr_t base_address)
: base_address(base_address) {
// data.reserve(2096000000uL);
}
uintptr_t
remote_get(size_t n) {
return base_address + n;
}
uintptr_t
remote_back() {
return remote_get(data.size() - 1);
}
};
vector<swath_t> swaths;
void add_element(const uintptr_t& remote_address) {
// timestamp = high_resolution_clock::now();
if (swaths.empty()) {
swaths.emplace_back(100);
cout<<"a"<<endl;
}
size_t remote_delta = remote_address - swaths.back().remote_back();
size_t local_delta = remote_delta * sizeof(byte_with_flags);
// seconds_total += duration_cast<duration<double>>(high_resolution_clock::now() - timestamp).count();
if (local_delta >= sizeof(swath_t) + swaths.back().data.capacity() + sizeof(byte_with_flags)) {
/// It is more memory-efficient to start a new swath
swaths.emplace_back(remote_address);
cout<<"b"<<endl;
}
else {
/// It is more memory-efficient to write over the intervening space with null values
while (remote_delta-- > 0) {
swaths.back().data.push_back(byte_with_flags{0, 0});
cout << "c" << endl;
}
}
swaths.back().data.push_back(byte_with_flags{3, 2});
}
/* Entry point */
int
main()
{
// Measure
high_resolution_clock::time_point timestamp;
size_t i;
size_t i_count = 121065475;
size_t remote_address = 0x100;
timestamp = high_resolution_clock::now();
for(i = 0; i < i_count; i++, remote_address++) {
add_element(remote_address);
}
clog<<"Scan 1 done in: "<<duration_cast<duration<double>>(high_resolution_clock::now() - timestamp).count()<<endl;
swaths.back().data.clear();
swaths.back().data.shrink_to_fit();
timestamp = high_resolution_clock::now();
for(i = 0; i < i_count; i++, remote_address++) {
swaths.back().data.push_back(byte_with_flags{3, 2});
}
clog<<"Scan 2 done in: "<<duration_cast<duration<double>>(high_resolution_clock::now() - timestamp).count()<<endl;
swaths.back().data.clear();
swaths.back().data.shrink_to_fit();
swath_t &swaths_back = swaths.back();
timestamp = high_resolution_clock::now();
for(i = 0; i < i_count; i++, remote_address++) {
swaths_back.data.push_back(byte_with_flags{3, 2});
}
clog<<"Scan 3 done in: "<<duration_cast<duration<double>>(high_resolution_clock::now() - timestamp).count()<<endl;
swaths.back().data.clear();
swaths.back().data.shrink_to_fit();
vector<byte_with_flags> &swaths_back_data = swaths.back().data;
timestamp = high_resolution_clock::now();
for(i = 0; i < i_count; i++, remote_address++) {
swaths_back_data.push_back(byte_with_flags{3, 2});
}
clog<<"Scan 4 done in: "<<duration_cast<duration<double>>(high_resolution_clock::now() - timestamp).count()<<endl;
vector<byte_with_flags> data;
timestamp = high_resolution_clock::now();
for(i = 0; i < i_count; i++, remote_address++) {
data.push_back(byte_with_flags{3, 2});
}
clog<<"Scan 5 done in: "<<duration_cast<duration<double>>(high_resolution_clock::now() - timestamp).count()<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment