huangcm
2025-07-01 676035278781360996553c427a12bf358249ebf7
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
278
279
280
281
282
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#ifndef ART_LIBARTBASE_BASE_MEMORY_TYPE_TABLE_H_
#define ART_LIBARTBASE_BASE_MEMORY_TYPE_TABLE_H_
 
#include <iostream>
#include <map>
#include <vector>
 
#include <android-base/macros.h>   // For DISALLOW_COPY_AND_ASSIGN
#include <android-base/logging.h>  // For DCHECK macros
 
namespace art {
 
// Class representing a memory range together with type attribute.
template <typename T>
class MemoryTypeRange final {
 public:
  MemoryTypeRange(uintptr_t start, uintptr_t limit, const T& type)
      : start_(start), limit_(limit), type_(type) {}
  MemoryTypeRange() : start_(0), limit_(0), type_() {}
  MemoryTypeRange(MemoryTypeRange&& other) = default;
  MemoryTypeRange(const MemoryTypeRange& other) = default;
  MemoryTypeRange& operator=(const MemoryTypeRange& other) = default;
 
  uintptr_t Start() const {
    DCHECK(IsValid());
    return start_;
  }
 
  uintptr_t Limit() const {
    DCHECK(IsValid());
    return limit_;
  }
 
  uintptr_t Size() const { return Limit() - Start(); }
 
  const T& Type() const { return type_; }
 
  bool IsValid() const { return start_ <= limit_; }
 
  bool Contains(uintptr_t address) const {
    return address >= Start() && address < Limit();
  }
 
  bool Overlaps(const MemoryTypeRange& other) const {
    bool disjoint = Limit() <= other.Start() || Start() >= other.Limit();
    return !disjoint;
  }
 
  bool Adjoins(const MemoryTypeRange& other) const {
    return other.Start() == Limit() || other.Limit() == Start();
  }
 
  bool CombinableWith(const MemoryTypeRange& other) const {
    return Type() == other.Type() && Adjoins(other);
  }
 
 private:
  uintptr_t start_;
  uintptr_t limit_;
  T type_;
};
 
// Class representing a table of memory ranges.
template <typename T>
class MemoryTypeTable final {
 public:
  // Class used to construct and populate MemoryTypeTable instances.
  class Builder;
 
  MemoryTypeTable() {}
 
  MemoryTypeTable(MemoryTypeTable&& table) : ranges_(std::move(table.ranges_)) {}
 
  MemoryTypeTable& operator=(MemoryTypeTable&& table) {
    ranges_ = std::move(table.ranges_);
    return *this;
  }
 
  // Find the range containing |address|.
  // Returns a pointer to a range on success, nullptr otherwise.
  const MemoryTypeRange<T>* Lookup(uintptr_t address) const {
    int last = static_cast<int>(ranges_.size()) - 1;
    for (int l = 0, r = last; l <= r;) {
      int m = l + (r - l) / 2;
      if (address < ranges_[m].Start()) {
        r = m - 1;
      } else if (address >= ranges_[m].Limit()) {
        l = m + 1;
      } else {
        DCHECK(ranges_[m].Contains(address))
            << reinterpret_cast<void*>(address) << " " << ranges_[m];
        return &ranges_[m];
      }
    }
    return nullptr;
  }
 
  size_t Size() const { return ranges_.size(); }
 
  void Print(std::ostream& os) const {
    for (const auto& range : ranges_) {
      os << range << std::endl;
    }
  }
 
 private:
  std::vector<MemoryTypeRange<T>> ranges_;
 
  DISALLOW_COPY_AND_ASSIGN(MemoryTypeTable);
};
 
// Class for building MemoryTypeTable instances. Supports
// adding ranges and looking up ranges.
template <typename T>
class MemoryTypeTable<T>::Builder final {
 public:
  Builder() {}
 
  // Adds a range if it is valid and doesn't overlap with existing ranges.  If the range adjoins
  // with an existing range, then the ranges are merged.
  //
  // Overlapping ranges and ranges of zero size are not supported.
  //
  // Returns true on success, false otherwise.
  inline bool Add(const MemoryTypeRange<T>& region);
 
  // Find the range containing |address|.
  // Returns a pointer to a range on success, nullptr otherwise.
  inline const MemoryTypeRange<T>* Lookup(uintptr_t address) const;
 
  // Returns number of unique ranges.
  inline size_t Size() const { return ranges_.size(); }
 
  // Generates a MemoryTypeTable for added ranges.
  MemoryTypeTable Build() const {
    MemoryTypeTable table;
    for (const auto& it : ranges_) {
      table.ranges_.push_back(it.second);
    }
    return table;
  }
 
 private:
  std::map<uintptr_t, MemoryTypeRange<T>> ranges_;
 
  DISALLOW_COPY_AND_ASSIGN(Builder);
};
 
template <typename T>
bool operator==(const MemoryTypeRange<T>& lhs, const MemoryTypeRange<T>& rhs) {
  return (lhs.Start() == rhs.Start() && lhs.Limit() == rhs.Limit() && lhs.Type() == rhs.Type());
}
 
template <typename T>
bool operator!=(const MemoryTypeRange<T>& lhs, const MemoryTypeRange<T>& rhs) {
  return !(lhs == rhs);
}
 
template <typename T>
std::ostream& operator<<(std::ostream& os, const MemoryTypeRange<T>& range) {
  os << reinterpret_cast<void*>(range.Start())
     << '-'
     << reinterpret_cast<void*>(range.Limit())
     << ' '
     << range.Type();
  return os;
}
 
template <typename T>
std::ostream& operator<<(std::ostream& os, const MemoryTypeTable<T>& table) {
  table.Print(os);
  return os;
}
 
template <typename T>
bool MemoryTypeTable<T>::Builder::Add(const MemoryTypeRange<T>& range) {
  if (UNLIKELY(!range.IsValid() || range.Size() == 0u)) {
    return false;
  }
 
  typename std::map<uintptr_t, MemoryTypeRange<T>>::iterator pred, succ;
 
  // Find an iterator for the next element in the ranges.
  succ = ranges_.lower_bound(range.Limit());
 
  // Find an iterator for a predecessor element.
  if (succ == ranges_.begin()) {
    // No predecessor element if the successor is at the beginning of ranges.
    pred = ranges_.end();
  } else if (succ != ranges_.end()) {
    // Predecessor is element before successor.
    pred = std::prev(succ);
  } else {
    // Predecessor is the last element in a non-empty map when there is no successor.
    pred = ranges_.find(ranges_.rbegin()->first);
  }
 
  // Invalidate |succ| if it cannot be combined with |range|.
  if (succ != ranges_.end()) {
    DCHECK_GE(succ->second.Limit(), range.Start());
    if (range.Overlaps(succ->second)) {
      return false;
    }
    if (!range.CombinableWith(succ->second)) {
      succ = ranges_.end();
    }
  }
 
  // Invalidate |pred| if it cannot be combined with |range|.
  if (pred != ranges_.end()) {
    if (range.Overlaps(pred->second)) {
      return false;
    }
    if (!range.CombinableWith(pred->second)) {
      pred = ranges_.end();
    }
  }
 
  if (pred == ranges_.end()) {
    if (succ == ranges_.end()) {
      // |pred| is invalid, |succ| is invalid.
      // No compatible neighbors for merging.
      DCHECK(ranges_.find(range.Limit()) == ranges_.end());
      ranges_[range.Limit()] = range;
    } else {
      // |pred| is invalid, |succ| is valid. Merge into |succ|.
      const uintptr_t limit = succ->second.Limit();
      DCHECK_GT(limit, range.Limit());
      ranges_.erase(succ);
      ranges_[limit] = MemoryTypeRange<T>(range.Start(), limit, range.Type());
    }
  } else {
    if (succ == ranges_.end()) {
      // |pred| is valid, |succ| is invalid. Merge into |pred|.
      const uintptr_t start = pred->second.Start();
      const uintptr_t limit = range.Limit();
      DCHECK_LT(start, range.Start());
      DCHECK_GT(limit, pred->second.Limit());
      ranges_.erase(pred);
      ranges_[limit] = MemoryTypeRange<T>(start, limit, range.Type());
    } else {
      // |pred| is valid, |succ| is valid. Merge between |pred| and |succ|.
      DCHECK_LT(pred->second.Start(), range.Start());
      DCHECK_GT(succ->second.Limit(), range.Limit());
      const uintptr_t start = pred->second.Start();
      const uintptr_t limit = succ->second.Limit();
      ranges_.erase(pred, ++succ);
      ranges_[limit] = MemoryTypeRange<T>(start, limit, range.Type());
    }
  }
  return true;
}
 
template <typename T>
const MemoryTypeRange<T>* MemoryTypeTable<T>::Builder::Lookup(uintptr_t address) const {
  auto it = ranges_.upper_bound(address);
  if (it != ranges_.end() && it->second.Contains(address)) {
    return &it->second;
  } else {
    return nullptr;
  }
}
 
}  // namespace art
 
#endif  // ART_LIBARTBASE_BASE_MEMORY_TYPE_TABLE_H_