-
Notifications
You must be signed in to change notification settings - Fork 4
/
AddressRangeFilter.cc
189 lines (154 loc) · 3.68 KB
/
AddressRangeFilter.cc
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
/*
* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2019 Intel Corporation
*
* Authors: Huang Ying <[email protected]>
* Jin Yao <[email protected]>
*/
#include "AddressRangeFilter.h"
AddressRangeFilter::Iterator AddressRangeFilter::search_range(
int pid, unsigned long addr,
AddressRangeFilter::Iterator *lower_p,
AddressRangeFilter::Iterator *upper_p)
{
Key k(pid, addr);
Range r;
auto lower = rmap.end(), upper = rmap.end(), it = rmap.end();
if (rmap.empty())
goto exit;
upper = rmap.upper_bound(k);
it = upper;
it--;
if (it == upper) {
it = rmap.end();
goto exit;
}
k = it->first;
r = it->second;
if (k.pid != (unsigned long)pid) {
it = rmap.end();
goto exit;
}
if (addr < k.start + r.size) {
/* Range is found, try to get the lower range */
lower = it;
lower--;
if (lower == it) {
/* It's possible when it and lower are all rmap.begin() */
lower = rmap.end();
}
goto exit;
}
lower = it;
it = rmap.end();
exit:
if (lower_p)
*lower_p = lower;
if (upper_p)
*upper_p = upper;
return it;
}
bool AddressRangeFilter::search_address(int pid, unsigned long addr)
{
Iterator it;
it = search_range(pid, addr, NULL, NULL);
return it != rmap.end();
}
void AddressRangeFilter::insert_new_range(int pid, unsigned long start,
unsigned long size)
{
Key k(pid, start);
Range r;
r.size = size;
rmap.insert(std::pair<Key, Range>(k, r));
}
void AddressRangeFilter::remove_ranges(
AddressRangeFilter::Iterator rm_start, AddressRangeFilter::Iterator rm_end,
int pid, unsigned long new_start, unsigned long new_end)
{
Iterator begin = rm_start;
Iterator it;
if (begin == rmap.end())
begin = rmap.begin();
it = begin;
while (it != rmap.end() && it != rm_end) {
Key k;
Range r;
k = it->first;
r = it->second;
if ((unsigned long)pid != k.pid) {
it++;
continue;
}
if (new_start <= k.start && new_end >= k.start + r.size) {
it = rmap.erase(it);
continue;
}
it++;
}
if (it != rmap.end())
rmap.erase(it);
}
void AddressRangeFilter::insert_range(int pid, unsigned long start,
unsigned long size)
{
Iterator rm_start, rm_end, lower, upper;
unsigned long new_start = start, new_end = start + size;
Key k;
Range r;
/*
* $lower$ |new|
* $---------$ |-----|
* $lower$ |new|
* $---------|-$-----|
*/
rm_start = search_range(pid, start, &lower, NULL);
if (rm_start != rmap.end()) {
k = rm_start->first;
new_start = k.start;
} else if (lower != rmap.end()) {
rm_start = lower;
k = rm_start->first;
r = rm_start->second;
if ((unsigned long)pid == k.pid && k.start + r.size == start)
new_start = k.start;
else
rm_start++;
}
/*
* |new| $upper$
* |---------| $-------$
* |new| $upper$
* |---------$-|-------$
*/
rm_end = search_range(pid, start + size - 1, NULL, &upper);
if (rm_end != rmap.end()) {
k = rm_end->first;
r = rm_end->second;
new_end = k.start + r.size;
} else if (upper != rmap.end()) {
k = upper->first;
r = upper->second;
if ((unsigned long)pid == k.pid && new_end == k.start) {
rm_end = upper;
new_end = k.start + r.size;
}
}
remove_ranges(rm_start, rm_end, pid, new_start, new_end);
insert_new_range(pid, new_start, new_end - new_start);
}
void AddressRangeFilter::clear(void)
{
rmap.clear();
}
void AddressRangeFilter::show(void)
{
auto it = rmap.begin();
while (it != rmap.end()) {
Key k = it->first;
Range *r = &it->second;
printf("pid: %d, start: %ld, size: %ld\n", (int)k.pid, k.start, r->size);
it++;
}
}