-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathIntervalMap.h
277 lines (231 loc) · 7.67 KB
/
IntervalMap.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
This file is part of memview, a real-time memory trace visualization
application.
Copyright (C) 2013 Andrew Clinton
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA.
The GNU General Public License is contained in the file COPYING.
*/
#ifndef IntervalMap_H
#define IntervalMap_H
#include <QMutex>
#include "Math.h"
#include <unordered_set>
#include <map>
#include <string>
#include <assert.h>
#include <iostream>
template <typename T> class IntervalMapReader;
template <typename T> class IntervalMapWriter;
// This class stores a map of non-overlapping intervals [start, end). The
// manipulator methods ensure that the interval map is always
// non-overlapping. Access is through IntervalMapReader and
// IntervalMapWriter to guarantee thread-safety.
template <typename T>
class IntervalMap {
private:
friend class IntervalMapReader<T>;
friend class IntervalMapWriter<T>;
struct Entry {
uint64 start;
T obj;
};
typedef std::map<uint64, Entry> MapType;
MapType myMap;
mutable QMutex myLock;
};
// This class locks access to the interval map, so scope it appropriately.
// This allows multiple operations on the map within the same lock
template <typename T>
class IntervalMapReader {
typedef typename IntervalMap<T>::MapType MapType;
public:
IntervalMapReader(const IntervalMap<T> &imap)
: myMap(imap.myMap)
, myLock(&imap.myLock) {}
size_t size() const { return myMap.size(); }
class iterator {
public:
iterator(typename MapType::const_iterator it) : myIt(it) {}
uint64 start() const { return myIt->second.start; }
uint64 end() const { return myIt->first; }
const T &value() const { return myIt->second.obj; }
iterator& operator++() { ++myIt; return *this; }
iterator operator++(int)
{ iterator tmp(*this); operator++(); return tmp; }
bool operator==(const iterator& rhs) {return myIt == rhs.myIt;}
bool operator!=(const iterator& rhs) {return myIt != rhs.myIt;}
private:
friend class IntervalMap<T>;
typename MapType::const_iterator myIt;
};
iterator begin() const { return iterator(myMap.begin()); }
iterator end() const { return iterator(myMap.end()); }
// Finds the element above and below the query address, and returns the
// closer of the two.
iterator findClosest(uint64 addr) const
{
auto hi = myMap.upper_bound(addr);
if (hi != myMap.end())
{
auto lo = hi;
--lo;
if (lo != myMap.end() &&
dist2(hi, addr) > dist2(lo, addr))
hi = lo;
return iterator(hi);
}
else if (myMap.size())
{
auto lo = hi;
--lo;
return iterator(lo);
}
return iterator(hi);
}
// Returns the element whose interval contains addr if it exists -
// otherwise 0.
iterator find(uint64 addr) const
{
iterator rval = findAfter(addr);
return (rval == end() || rval.start() > addr) ? end() : rval;
}
// Returns the first interval that starts after addr or the interval
// that contains addr.
iterator findAfter(uint64 addr) const
{
return iterator(myMap.upper_bound(addr));
}
// Return the entire interval covered by the map
void getTotalInterval(uint64 &start, uint64 &end) const
{
if (myMap.size())
{
start = myMap.begin()->second.start;
end = myMap.rbegin()->first;
}
else
{
start = ~0ull;
end = 0ull;
}
}
void dump() const
{
for (auto it = myMap.begin(); it != myMap.end(); ++it)
{
std::cerr
<< "[" << it->second.start
<< ", " << it->first << "): "
<< it->second.obj << "\n";
}
}
private:
// Find the distance from an address to an interval
template <typename IT>
uint64 dist2(const IT &e, uint64 addr) const
{
if (addr < e->second.start)
return e->second.start - addr;
if (addr >= e->first)
return addr - e->first + 1;
return 0;
}
private:
const MapType &myMap;
QMutexLocker myLock;
};
// This class locks access to the interval map, so scope it appropriately.
// This allows multiple operations on the map within the same lock
template <typename T>
class IntervalMapWriter : public IntervalMapReader<T> {
typedef typename IntervalMap<T>::MapType MapType;
public:
IntervalMapWriter(IntervalMap<T> &imap)
: IntervalMapReader<T>(imap)
, myMap(imap.myMap) {}
void insert(uint64 start, uint64 end, const T &val)
{
clearOverlappingIntervals(start, end);
myMap[end].start = start;
myMap[end].obj = val;
}
void erase(uint64 start, uint64 end)
{
clearOverlappingIntervals(start, end);
}
// Apply the function to all intervals in [start, end). If any
// intervals overlap the boundary values, they are split and the
// function is applied only to the included values.
template <typename Func>
void apply(uint64 start, uint64 end, const Func func)
{
typename MapType::iterator first, last;
getOverlappingIntervals(start, end, first, last);
for (; first != last; ++first)
func(first->second.obj);
}
private:
// Find all intervals that overlap [start, end). If any intervals
// overlapped these boundary values, split the intervals to eliminate
// overlap.
void getOverlappingIntervals(
uint64 start, uint64 end,
typename MapType::iterator &first,
typename MapType::iterator &it)
{
first = myMap.upper_bound(start);
for (it = first; it != myMap.end() && it->second.start < end; )
{
if (it->second.start < start)
{
// We used upper_bound so this should always be true
assert(it->first > start);
myMap[start] = it->second;
it->second.start = start;
}
else if (it->first > end)
{
myMap[end] = it->second;
it->second.start = end;
}
else
++it;
}
}
void clearOverlappingIntervals(uint64 start, uint64 end)
{
typename MapType::iterator first, last;
getOverlappingIntervals(start, end, first, last);
myMap.erase(first, last);
}
private:
MapType &myMap;
};
#define MAP_TYPE(NAME, TYPE) \
typedef IntervalMap<TYPE> NAME; \
typedef IntervalMapReader<TYPE> NAME##Reader; \
typedef IntervalMapWriter<TYPE> NAME##Writer;
struct StackInfo {
std::string myStr;
uint32 myState;
};
struct MMapInfo {
std::string myStr;
int myIdx;
bool myMapped;
};
MAP_TYPE(StackTraceMap, StackInfo)
MAP_TYPE(MMapMap, MMapInfo)
#undef MAP_TYPE
#endif