addr_range.hh (9334:0a12b040494a) | addr_range.hh (9405:c0a0593510db) |
---|---|
1/* 2 * Copyright (c) 2012 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 --- 31 unchanged lines hidden (view full) --- 40 * Authors: Nathan Binkert 41 * Steve Reinhardt 42 * Andreas Hansson 43 */ 44 45#ifndef __BASE_ADDR_RANGE_HH__ 46#define __BASE_ADDR_RANGE_HH__ 47 | 1/* 2 * Copyright (c) 2012 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 --- 31 unchanged lines hidden (view full) --- 40 * Authors: Nathan Binkert 41 * Steve Reinhardt 42 * Andreas Hansson 43 */ 44 45#ifndef __BASE_ADDR_RANGE_HH__ 46#define __BASE_ADDR_RANGE_HH__ 47 |
48#include <utility> // pair & make_pair 49 | 48#include "base/cprintf.hh" |
50#include "base/types.hh" 51 52class AddrRange 53{ 54 | 49#include "base/types.hh" 50 51class AddrRange 52{ 53 |
55 public: | 54 private: |
56 | 55 |
57 Addr start; 58 Addr end; | 56 /// Private fields for the start and end of the range. In the 57 /// future, these will be extended with interleaving functionality 58 /// and hence should never be manipulated directly. 59 Addr _start; 60 Addr _end; |
59 | 61 |
62 public: 63 |
|
60 AddrRange() | 64 AddrRange() |
61 : start(1), end(0) | 65 : _start(1), _end(0) |
62 {} 63 64 AddrRange(Addr _start, Addr _end) | 66 {} 67 68 AddrRange(Addr _start, Addr _end) |
65 : start(_start), end(_end) | 69 : _start(_start), _end(_end) |
66 {} 67 | 70 {} 71 |
68 AddrRange(const std::pair<Addr, Addr> &r) 69 : start(r.first), end(r.second) 70 {} | 72 /** 73 * Get the size of the address range. For a case where 74 * interleaving is used this should probably cause a panic. 75 */ 76 Addr size() const { return _end - _start + 1; } |
71 | 77 |
72 Addr size() const { return end - start + 1; } 73 bool valid() const { return start < end; } | 78 /** 79 * Determine if the range is valid. 80 */ 81 bool valid() const { return _start < _end; } |
74 75 /** | 82 83 /** |
84 * Get the start address of the range. 85 */ 86 Addr start() const { return _start; } 87 88 /** 89 * Get a string representation of the range. This could 90 * alternatively be implemented as a operator<<, but at the moment 91 * that seems like overkill. 92 */ 93 std::string to_string() const 94 { 95 return csprintf("[%#llx : %#llx]", _start, _end); 96 } 97 98 /** |
|
76 * Determine if another range intersects this one, i.e. if there 77 * is an address that is both in this range and the other 78 * range. No check is made to ensure either range is valid. 79 * 80 * @param r Range to intersect with 81 * @return true if the intersection of the two ranges is not empty 82 */ 83 bool intersects(const AddrRange& r) const 84 { | 99 * Determine if another range intersects this one, i.e. if there 100 * is an address that is both in this range and the other 101 * range. No check is made to ensure either range is valid. 102 * 103 * @param r Range to intersect with 104 * @return true if the intersection of the two ranges is not empty 105 */ 106 bool intersects(const AddrRange& r) const 107 { |
85 return (start <= r.start && end >= r.start) || 86 (start <= r.end && end >= r.end); | 108 return _start <= r._end && _end >= r._start; |
87 } 88 89 /** 90 * Determine if this range is a subset of another range, i.e. if 91 * every address in this range is also in the other range. No 92 * check is made to ensure either range is valid. 93 * 94 * @param r Range to compare with 95 * @return true if the this range is a subset of the other one 96 */ 97 bool isSubset(const AddrRange& r) const 98 { | 109 } 110 111 /** 112 * Determine if this range is a subset of another range, i.e. if 113 * every address in this range is also in the other range. No 114 * check is made to ensure either range is valid. 115 * 116 * @param r Range to compare with 117 * @return true if the this range is a subset of the other one 118 */ 119 bool isSubset(const AddrRange& r) const 120 { |
99 return start >= r.start && end <= r.end; | 121 return _start >= r._start && _end <= r._end; |
100 } | 122 } |
101}; | |
102 | 123 |
124 /** 125 * Determine if the range contains an address. 126 * 127 * @param a Address to compare with 128 * @return true if the address is in the range 129 */ 130 bool contains(const Addr& a) const 131 { 132 return a >= _start && a <= _end; 133 } 134 |
|
103/** 104 * Keep the operators away from SWIG. 105 */ 106#ifndef SWIG 107 | 135/** 136 * Keep the operators away from SWIG. 137 */ 138#ifndef SWIG 139 |
108/** 109 * @param range1 is a range. 110 * @param range2 is a range. 111 * @return if range1 is less than range2 and does not overlap range1. 112 */ 113inline bool 114operator<(const AddrRange& range1, const AddrRange& range2) 115{ 116 return range1.start < range2.start; 117} | 140 /** 141 * Less-than operator used to turn an STL map into a binary search 142 * tree of non-overlapping address ranges. 143 * 144 * @param r Range to compare with 145 * @return true if the start address is less than that of the other range 146 */ 147 bool operator<(const AddrRange& r) const 148 { 149 return _start < r._start; 150 } |
118 | 151 |
119/** 120 * @param addr address in the range 121 * @param range range compared against. 122 * @return indicates that the address is not within the range. 123 */ 124inline bool 125operator!=(const Addr& addr, const AddrRange& range) 126{ 127 return addr < range.start || addr > range.end; 128} | 152#endif // SWIG 153}; |
129 | 154 |
130/** 131 * @param range range compared against. 132 * @param pos position compared to the range. 133 * @return indicates that position pos is within the range. 134 */ 135inline bool 136operator==(const AddrRange& range, const Addr& addr) 137{ 138 return addr >= range.start && addr <= range.end; 139} 140 | |
141inline AddrRange 142RangeEx(Addr start, Addr end) | 155inline AddrRange 156RangeEx(Addr start, Addr end) |
143{ return std::make_pair(start, end - 1); } | 157{ return AddrRange(start, end - 1); } |
144 145inline AddrRange 146RangeIn(Addr start, Addr end) | 158 159inline AddrRange 160RangeIn(Addr start, Addr end) |
147{ return std::make_pair(start, end); } | 161{ return AddrRange(start, end); } |
148 149inline AddrRange 150RangeSize(Addr start, Addr size) | 162 163inline AddrRange 164RangeSize(Addr start, Addr size) |
151{ return std::make_pair(start, start + size - 1); } | 165{ return AddrRange(start, start + size - 1); } |
152 | 166 |
153#endif // SWIG 154 | |
155#endif // __BASE_ADDR_RANGE_HH__ | 167#endif // __BASE_ADDR_RANGE_HH__ |