-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.h
More file actions
83 lines (71 loc) · 1.9 KB
/
storage.h
File metadata and controls
83 lines (71 loc) · 1.9 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef STORAGE_H_
#define STORAGE_H_
// {{{ Includes
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <jvmti.h>
#include <fstream>
#include <map>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
// }}}
// {{{ Data
struct AllocationInfo {
long sizeBytes;
uintptr_t ref;
};
struct MethodInfo {
std::string name;
std::string klass;
std::string file;
int line;
};
inline std::ostream &operator<<(std::ostream &os, const MethodInfo &m) {
return (os << m.klass << m.name << "(" << m.file << ":" << m.line << ")");
}
class StackTrace {
public:
// TODO: expose necessary iterator instead of vector
std::vector<uintptr_t> GetFrames() const { return frames; }
void AddFrame(uintptr_t methodId) { frames.push_back(methodId); };
private:
std::vector<uintptr_t> frames;
};
inline std::ostream &operator<<(std::ostream &os, const StackTrace &st) {
std::stringstream sstream;
for (auto id : st.GetFrames()) {
sstream << std::hex << id << " ";
}
return (os << sstream.str());
}
class Storage {
public:
// TODO: hide fields and expose necessary iterators
std::multimap<long, AllocationInfo> allocations;
std::unordered_map<uintptr_t, MethodInfo> methods;
void AddMethod(uintptr_t id, MethodInfo methodInfo) {
methods.insert({id, methodInfo});
}
void AddAllocation(long id, StackTrace stackTrace,
AllocationInfo allocationInfo) {
if (stacks.count(id) == 0) {
stacks.insert({id, stackTrace});
}
allocations.insert({id, allocationInfo});
}
bool HasMethod(uintptr_t id) const { return methods.count(id) != 0; }
MethodInfo GetMethod(uintptr_t id) { return methods[id]; }
StackTrace GetStackTrace(long id) { return stacks[id]; }
void Clear() {
stacks.clear();
methods.clear();
allocations.clear();
}
private:
std::unordered_map<long, StackTrace> stacks;
};
// }}}
#endif // STORAGE_H_