-
Notifications
You must be signed in to change notification settings - Fork 142
/
DisasmCache.h
54 lines (45 loc) · 1.17 KB
/
DisasmCache.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once
#include <iostream>
#include <string>
#include <map>
#include "Crc.h"
//----
struct DisasmCache
{
static uint64_t calcChecks(const std::string& _disasm)
{
uint64_t init = 0;
return crc64(init, (unsigned char*)_disasm.c_str(), _disasm.length());
}
DisasmCache() {}
~DisasmCache() {
for (auto itr = disasmLines.begin(); itr != disasmLines.end(); ++itr) {
char* buf = itr->second;
if (buf) free(buf);
}
}
const char* get(uint32_t crc32)
{
auto itr = disasmLines.find(crc32);
if (itr == disasmLines.end()) {
return nullptr;
}
return disasmLines[crc32];
}
const char* put(const std::string& _disasm)
{
auto checks = calcChecks(_disasm);
auto infoPtr = get(checks);
if (infoPtr) {
return infoPtr;
}
const size_t len = _disasm.length();
char *buf = (char*)::malloc(len + 1);
::memcpy(buf, _disasm.c_str(), len);
buf[len] = 0;
disasmLines[checks] = buf;
return disasmLines[checks];
}
//---
std::map<uint64_t, char*> disasmLines;
};