gem5  v20.1.0.0
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:
69  typedef typename RangeMap::iterator iterator;
70  typedef typename RangeMap::const_iterator const_iterator; // end of api_addr_range
72 
87  contains(const AddrRange &r) const
88  {
89  return find(r, [r](const AddrRange r1) { return r.isSubset(r1); });
90  }
91  iterator
93  {
94  return find(r, [r](const AddrRange r1) { return r.isSubset(r1); });
95  } // end of api_addr_range
97 
112  contains(Addr r) const
113  {
114  return contains(RangeSize(r, 1));
115  }
116  iterator
118  {
119  return contains(RangeSize(r, 1));
120  } // end of api_addr_range
122 
137  intersects(const AddrRange &r) const
138  {
139  return find(r, [r](const AddrRange r1) { return r.intersects(r1); });
140  }
141  iterator
143  {
144  return find(r, [r](const AddrRange r1) { return r.intersects(r1); });
145  } // end of api_addr_range
147 
151  iterator
152  insert(const AddrRange &r, const V& d)
153  {
154  if (intersects(r) != end())
155  return tree.end();
156 
157  return tree.insert(std::make_pair(r, d)).first;
158  }
159 
163  void
165  {
166  cache.remove(p);
167  tree.erase(p);
168  }
169 
173  void
175  {
176  for (auto it = p; it != q; it++) {
177  cache.remove(p);
178  }
179  tree.erase(p,q);
180  }
181 
185  void
187  {
188  cache.erase(cache.begin(), cache.end());
189  tree.erase(tree.begin(), tree.end());
190  }
191 
196  begin() const
197  {
198  return tree.begin();
199  }
200 
204  iterator
206  {
207  return tree.begin();
208  }
209 
214  end() const
215  {
216  return tree.end();
217  }
218 
222  iterator
223  end()
224  {
225  return tree.end();
226  }
227 
231  std::size_t
232  size() const
233  {
234  return tree.size();
235  }
236 
240  bool
241  empty() const
242  {
243  return tree.empty();
244  }
245 
246  private:
252  void
254  {
255  if (max_cache_size != 0) {
256  // If there's a cache, add this element to it.
257  if (cache.size() >= max_cache_size) {
258  // If the cache is full, move the last element to the
259  // front and overwrite it with the new value. This
260  // avoids creating or destroying elements of the list.
261  auto last = cache.end();
262  last--;
263  *last = it;
264  if (max_cache_size > 1)
265  cache.splice(cache.begin(), cache, last);
266  } else {
267  cache.push_front(it);
268  }
269  }
270  }
271 
283  iterator
284  find(const AddrRange &r, std::function<bool(const AddrRange)> cond)
285  {
286  // Check the cache first
287  for (auto c = cache.begin(); c != cache.end(); c++) {
288  auto it = *c;
289  if (cond(it->first)) {
290  // If this entry matches, promote it to the front
291  // of the cache and return it.
292  cache.splice(cache.begin(), cache, c);
293  return it;
294  }
295  }
296 
297  iterator next = tree.upper_bound(r);
298  if (next != end() && cond(next->first)) {
299  addNewEntryToCache(next);
300  return next;
301  }
302  if (next == begin())
303  return end();
304  next--;
305 
306  iterator i;
307  do {
308  i = next;
309  if (cond(i->first)) {
311  return i;
312  }
313  // Keep looking if the next range merges with the current one.
314  } while (next != begin() &&
315  (--next)->first.mergesWith(i->first));
316 
317  return end();
318  }
319 
321  find(const AddrRange &r, std::function<bool(const AddrRange)> cond) const
322  {
323  return const_cast<AddrRangeMap *>(this)->find(r, cond);
324  }
325 
327 
335 };
336 
337 #endif //__BASE_ADDR_RANGE_MAP_HH__
AddrRangeMap::erase
void erase(iterator p, iterator q)
Definition: addr_range_map.hh:174
AddrRangeMap::insert
iterator insert(const AddrRange &r, const V &d)
Definition: addr_range_map.hh:152
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
AddrRangeMap::end
const_iterator end() const
Definition: addr_range_map.hh:214
AddrRangeMap::const_iterator
RangeMap::const_iterator const_iterator
Definition: addr_range_map.hh:70
AddrRangeMap::erase
void erase(iterator p)
Definition: addr_range_map.hh:164
ArmISA::q
Bitfield< 27 > q
Definition: miscregs_types.hh:52
ArmISA::cond
cond
Definition: types.hh:61
AddrRangeMap::end
iterator end()
Definition: addr_range_map.hh:223
AddrRangeMap::cache
std::list< iterator > cache
A list of iterator that correspond to the max_cache_size most recently used entries in the address ra...
Definition: addr_range_map.hh:334
AddrRangeMap::contains
const_iterator contains(Addr r) const
Find entry that contains the given address.
Definition: addr_range_map.hh:112
AddrRangeMap::size
std::size_t size() const
Definition: addr_range_map.hh:232
AddrRangeMap::clear
void clear()
Definition: addr_range_map.hh:186
AddrRangeMap::empty
bool empty() const
Definition: addr_range_map.hh:241
AddrRangeMap::begin
const_iterator begin() const
Definition: addr_range_map.hh:196
AddrRangeMap::contains
iterator contains(Addr r)
Definition: addr_range_map.hh:117
AddrRangeMap::iterator
RangeMap::iterator iterator
Definition: addr_range_map.hh:69
AddrRange
The AddrRange class encapsulates an address range, and supports a number of tests to check if two ran...
Definition: addr_range.hh:68
ArmISA::d
Bitfield< 9 > d
Definition: miscregs_types.hh:60
AddrRangeMap::intersects
iterator intersects(const AddrRange &r)
Definition: addr_range_map.hh:142
MipsISA::r
r
Definition: pra_constants.hh:95
RangeSize
AddrRange RangeSize(Addr start, Addr size)
Definition: addr_range.hh:638
AddrRangeMap
The AddrRangeMap uses an STL map to implement an interval tree for address decoding.
Definition: addr_range_map.hh:59
AddrRangeMap::contains
iterator contains(const AddrRange &r)
Definition: addr_range_map.hh:92
AddrRangeMap::find
const_iterator find(const AddrRange &r, std::function< bool(const AddrRange)> cond) const
Definition: addr_range_map.hh:321
AddrRangeMap::RangeMap
std::map< AddrRange, V > RangeMap
Definition: addr_range_map.hh:62
AddrRangeMap::addNewEntryToCache
void addNewEntryToCache(iterator it) const
Add an address range map entry to the cache.
Definition: addr_range_map.hh:253
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
addr_range.hh
types.hh
AddrRangeMap::contains
const_iterator contains(const AddrRange &r) const
Find entry that contains the given address range.
Definition: addr_range_map.hh:87
AddrRangeMap::intersects
const_iterator intersects(const AddrRange &r) const
Find entry that intersects with the given address range.
Definition: addr_range_map.hh:137
ArmISA::c
Bitfield< 29 > c
Definition: miscregs_types.hh:50
MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:323
std::list< iterator >
AddrRangeMap::begin
iterator begin()
Definition: addr_range_map.hh:205
AddrRangeMap::find
iterator find(const AddrRange &r, std::function< bool(const AddrRange)> cond)
Find entry that satisfies a condition on an address range.
Definition: addr_range_map.hh:284
AddrRangeMap::tree
RangeMap tree
Definition: addr_range_map.hh:326

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17