gem5  v20.0.0.2
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
addr_range_map.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, 2018 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Copyright (c) 2006 The Regents of The University of Michigan
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are
19  * met: redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer;
21  * redistributions in binary form must reproduce the above copyright
22  * notice, this list of conditions and the following disclaimer in the
23  * documentation and/or other materials provided with the distribution;
24  * neither the name of the copyright holders nor the names of its
25  * contributors may be used to endorse or promote products derived from
26  * this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 #ifndef __BASE_ADDR_RANGE_MAP_HH__
42 #define __BASE_ADDR_RANGE_MAP_HH__
43 
44 #include <cstddef>
45 #include <functional>
46 #include <list>
47 #include <map>
48 #include <utility>
49 
50 #include "base/addr_range.hh"
51 #include "base/types.hh"
52 
58 template <typename V, int max_cache_size=0>
60 {
61  private:
62  typedef std::map<AddrRange, V> RangeMap;
63 
64  public:
65  typedef typename RangeMap::iterator iterator;
66  typedef typename RangeMap::const_iterator const_iterator;
67 
78  const_iterator
79  contains(const AddrRange &r) const
80  {
81  return find(r, [r](const AddrRange r1) { return r.isSubset(r1); });
82  }
83  iterator
85  {
86  return find(r, [r](const AddrRange r1) { return r.isSubset(r1); });
87  }
88 
99  const_iterator
100  contains(Addr r) const
101  {
102  return contains(RangeSize(r, 1));
103  }
104  iterator
106  {
107  return contains(RangeSize(r, 1));
108  }
109 
120  const_iterator
121  intersects(const AddrRange &r) const
122  {
123  return find(r, [r](const AddrRange r1) { return r.intersects(r1); });
124  }
125  iterator
127  {
128  return find(r, [r](const AddrRange r1) { return r.intersects(r1); });
129  }
130 
131  iterator
132  insert(const AddrRange &r, const V& d)
133  {
134  if (intersects(r) != end())
135  return tree.end();
136 
137  return tree.insert(std::make_pair(r, d)).first;
138  }
139 
140  void
141  erase(iterator p)
142  {
143  cache.remove(p);
144  tree.erase(p);
145  }
146 
147  void
148  erase(iterator p, iterator q)
149  {
150  for (auto it = p; it != q; it++) {
151  cache.remove(p);
152  }
153  tree.erase(p,q);
154  }
155 
156  void
158  {
159  cache.erase(cache.begin(), cache.end());
160  tree.erase(tree.begin(), tree.end());
161  }
162 
163  const_iterator
164  begin() const
165  {
166  return tree.begin();
167  }
168 
169  iterator
171  {
172  return tree.begin();
173  }
174 
175  const_iterator
176  end() const
177  {
178  return tree.end();
179  }
180 
181  iterator
182  end()
183  {
184  return tree.end();
185  }
186 
187  std::size_t
188  size() const
189  {
190  return tree.size();
191  }
192 
193  bool
194  empty() const
195  {
196  return tree.empty();
197  }
198 
199  private:
205  void
206  addNewEntryToCache(iterator it) const
207  {
208  if (max_cache_size != 0) {
209  // If there's a cache, add this element to it.
210  if (cache.size() >= max_cache_size) {
211  // If the cache is full, move the last element to the
212  // front and overwrite it with the new value. This
213  // avoids creating or destroying elements of the list.
214  auto last = cache.end();
215  last--;
216  *last = it;
217  if (max_cache_size > 1)
218  cache.splice(cache.begin(), cache, last);
219  } else {
220  cache.push_front(it);
221  }
222  }
223  }
224 
236  iterator
237  find(const AddrRange &r, std::function<bool(const AddrRange)> cond)
238  {
239  // Check the cache first
240  for (auto c = cache.begin(); c != cache.end(); c++) {
241  auto it = *c;
242  if (cond(it->first)) {
243  // If this entry matches, promote it to the front
244  // of the cache and return it.
245  cache.splice(cache.begin(), cache, c);
246  return it;
247  }
248  }
249 
250  iterator next = tree.upper_bound(r);
251  if (next != end() && cond(next->first)) {
252  addNewEntryToCache(next);
253  return next;
254  }
255  if (next == begin())
256  return end();
257  next--;
258 
259  iterator i;
260  do {
261  i = next;
262  if (cond(i->first)) {
264  return i;
265  }
266  // Keep looking if the next range merges with the current one.
267  } while (next != begin() &&
268  (--next)->first.mergesWith(i->first));
269 
270  return end();
271  }
272 
273  const_iterator
274  find(const AddrRange &r, std::function<bool(const AddrRange)> cond) const
275  {
276  return const_cast<AddrRangeMap *>(this)->find(r, cond);
277  }
278 
279  RangeMap tree;
280 
288 };
289 
290 #endif //__BASE_ADDR_RANGE_MAP_HH__
iterator begin()
AddrRange RangeSize(Addr start, Addr size)
Definition: addr_range.hh:580
std::map< AddrRange, V > RangeMap
Bitfield< 7 > i
void erase(iterator p, iterator q)
iterator end()
void erase(iterator p)
bool isSubset(const AddrRange &r) const
Determine if this range is a subset of another range, i.e.
Definition: addr_range.hh:379
bool intersects(const AddrRange &r) const
Determine if another range intersects this one, i.e.
Definition: addr_range.hh:347
const_iterator begin() const
bool empty() const
iterator insert(const AddrRange &r, const V &d)
const_iterator contains(const AddrRange &r) const
Find entry that contains the given address range.
The AddrRange class encapsulates an address range, and supports a number of tests to check if two ran...
Definition: addr_range.hh:68
RangeMap::const_iterator const_iterator
Bitfield< 27 > q
Bitfield< 9 > d
iterator intersects(const AddrRange &r)
const_iterator end() const
The AddrRangeMap uses an STL map to implement an interval tree for address decoding.
iterator contains(Addr r)
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,16,32,64}_t.
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:140
Bitfield< 29 > c
void addNewEntryToCache(iterator it) const
Add an address range map entry to the cache.
iterator find(const AddrRange &r, std::function< bool(const AddrRange)> cond)
Find entry that satisfies a condition on an address range.
std::list< iterator > cache
A list of iterator that correspond to the max_cache_size most recently used entries in the address ra...
iterator contains(const AddrRange &r)
const_iterator contains(Addr r) const
Find entry that contains the given address.
std::size_t size() const
const_iterator intersects(const AddrRange &r) const
Find entry that intersects with the given address range.
Bitfield< 0 > p
RangeMap::iterator iterator
cond
Definition: types.hh:61
const_iterator find(const AddrRange &r, std::function< bool(const AddrRange)> cond) const

Generated on Mon Jun 8 2020 15:45:07 for gem5 by doxygen 1.8.13