2 * Copyright (c) 2012, 2018 ARM Limited
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.
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
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.
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.
44 #ifndef __BASE_ADDR_RANGE_MAP_HH__
45 #define __BASE_ADDR_RANGE_MAP_HH__
53 #include "base/addr_range.hh"
54 #include "base/types.hh"
57 * The AddrRangeMap uses an STL map to implement an interval tree for
58 * address decoding. The value stored is a template type and can be
59 * e.g. a port identifier, or a pointer.
61 template <typename V, int max_cache_size=0>
65 typedef std::map<AddrRange, V> RangeMap;
68 typedef typename RangeMap::iterator iterator;
69 typedef typename RangeMap::const_iterator const_iterator;
72 * Find entry that contains the given address range
74 * Searches through the ranges in the address map and returns an
75 * iterator to the entry which range is a superset of the input
76 * address range. Returns end() if none found.
78 * @param r An input address range
79 * @return An iterator that contains the input address range
82 contains(const AddrRange &r) const
84 return find(r, [r](const AddrRange r1) { return r.isSubset(r1); });
88 * Find entry that contains the given address
90 * Searches through the ranges in the address map and returns an
91 * iterator to the entry which range is a superset of the input
92 * address. Returns end() if none found.
94 * @param r An input address
95 * @return An iterator that contains the input address
98 contains(Addr r) const
100 return contains(RangeSize(r, 1));
104 * Find entry that intersects with the given address range
106 * Searches through the ranges in the address map and returns an
107 * iterator to the first entry which range intersects with the
110 * @param r An input address
111 * @return An iterator that intersects with the input address range
114 intersects(const AddrRange &r) const
116 return find(r, [r](const AddrRange r1) { return r.intersects(r1); });
120 insert(const AddrRange &r, const V& d)
122 if (intersects(r) != end())
125 return tree.insert(std::make_pair(r, d)).first;
136 erase(iterator p, iterator q)
138 for (auto it = p; it != q; it++) {
147 cache.erase(cache.begin(), cache.end());
148 tree.erase(tree.begin(), tree.end());
189 * Add an address range map entry to the cache.
191 * @param it Iterator to the entry in the address range map
194 addNewEntryToCache(const_iterator it) const
196 if (max_cache_size != 0) {
197 // If there's a cache, add this element to it.
198 if (cache.size() >= max_cache_size) {
199 // If the cache is full, move the last element to the
200 // front and overwrite it with the new value. This
201 // avoids creating or destroying elements of the list.
202 auto last = cache.end();
205 if (max_cache_size > 1)
206 cache.splice(cache.begin(), cache, last);
208 cache.push_front(it);
214 * Find entry that satisfies a condition on an address range
216 * Searches through the ranges in the address map and returns an
217 * iterator to the entry that satisfies the input conidition on
218 * the input address range. Returns end() if none found.
220 * @param r An input address range
221 * @param f A condition on an address range
222 * @return An iterator that contains the input address range
225 find(const AddrRange &r, std::function<bool(const AddrRange)> cond) const
227 // Check the cache first
228 for (auto c = cache.begin(); c != cache.end(); c++) {
230 if (cond(it->first)) {
231 // If this entry matches, promote it to the front
232 // of the cache and return it.
233 cache.splice(cache.begin(), cache, c);
238 const_iterator next = tree.upper_bound(r);
239 if (next != end() && cond(next->first)) {
240 addNewEntryToCache(next);
250 if (cond(i->first)) {
251 addNewEntryToCache(i);
254 // Keep looking if the next range merges with the current one.
255 } while (next != begin() &&
256 (--next)->first.mergesWith(i->first));
264 * A list of iterator that correspond to the max_cache_size most
265 * recently used entries in the address range map. This mainly
266 * used to optimize lookups. The elements in the list should
267 * always be valid iterators of the tree.
269 mutable std::list<const_iterator> cache;
272 #endif //__BASE_ADDR_RANGE_MAP_HH__