-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tracker.hpp
144 lines (116 loc) · 3.17 KB
/
Tracker.hpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// Tracker.hpp
// memtest
//
// Created by rick gessner on 1/22/22.
//
#ifndef Tracker_h
#define Tracker_h
#include <iostream>
#include <vector>
#include <filesystem>
namespace fs = std::filesystem;
#define _TRACKER_ON //comment this out to disable tracker...
#ifdef _TRACKER_ON
#define GPS(aPtr) Tracker::instance().watch(aPtr, __LINE__, __FILE__)
#else
#define GPS(aPtr) (aPtr)
#endif
struct Tracker {
static Tracker single;
static Tracker& instance() {
return single;
}
struct Memo {
void* ptr;
size_t line;
size_t filenum;
};
Tracker(bool aEnabled=false) {
enabled=aEnabled;
names.push_back("unknown");
}
bool isEnabled() const {return enabled;}
Tracker& enable(bool aState) {
enabled=aState;
return *this;
}
Tracker& reset() { //called to forget prior ptrs...
names.clear();
list.clear();
return *this;
}
void* track(void* aPtr) {
if(enabled) {
enabled=false;
list.push_back(Memo{aPtr});
enabled=true;
}
return aPtr;
}
template<typename T>
T* watch(T* aPtr, size_t aLine=0, const char* aFile=nullptr) {
if(aLine) {
bool wasEnabled=enabled;
enabled=false;
std::string theName=fs::path(aFile).filename().u8string();
auto theIt = find(names.begin(), names.end(), theName);
size_t theIndex=names.size();
if (theIt != names.end()) {
theIndex = theIt - names.begin();
}
else names.push_back(theName);
enabled=wasEnabled;
auto theEnd=list.rend();
for (auto it = list.rbegin(); it!=theEnd; it++) {
if(it->ptr==aPtr) {
it->line=aLine;
it->filenum=theIndex;
break;
}
}
}
return aPtr;
}
Tracker& untrack(void* aPtr) {
auto theIter = std::find_if(
list.begin(), list.end(), [aPtr](Memo const& aMemo){
return aMemo.ptr == aPtr;
});
if(theIter != list.end()) {list.erase(theIter);}
return *this;
}
Tracker& reportLeaks(std::ostream &aStream) {
for(auto &theMem: list) {
aStream << theMem.ptr << " : "
<< names[theMem.filenum] << "("
<< theMem.line << ")\n";
}
return *this;
}
protected:
Tracker(const Tracker &aTracker) {}
bool enabled;
std::vector<Memo> list;
std::vector<std::string> names;
};
Tracker Tracker::single(false);
//-------------------------------------------
#ifdef _TRACKER_ON
void * operator new(size_t aSize) {
auto thePtr=std::malloc(aSize);
Tracker::instance().track(thePtr);
return thePtr;
}
void * operator new[](size_t aSize) {
return operator new(aSize);
}
void operator delete(void* aPtr) noexcept {
Tracker::instance().untrack(aPtr);
std::free(aPtr);
}
void operator delete[](void* aPtr) noexcept {
operator delete(aPtr);
}
#endif
#endif /* Tracker_h */