summaryrefslogtreecommitdiff
path: root/src/base
diff options
context:
space:
mode:
authorAndreas Hansson <andreas.hansson@arm.com>2013-01-07 13:05:38 -0500
committerAndreas Hansson <andreas.hansson@arm.com>2013-01-07 13:05:38 -0500
commit71da1d21578b6f9cf5b43bd4648f313326849533 (patch)
tree639972c7c05b28380952bc655b4dd170d888599d /src/base
parentcfdaf53104625a04d504972c76545bf869c6a476 (diff)
downloadgem5-71da1d21578b6f9cf5b43bd4648f313326849533.tar.xz
base: Encapsulate the underlying fields in AddrRange
This patch makes the start and end address private in a move to prevent direct manipulation and matching of ranges based on these fields. This is done so that a transition to ranges with interleaving support is possible. As a result of hiding the start and end, a number of member functions are needed to perform the comparisons and manipulations that previously took place directly on the members. An accessor function is provided for the start address, and a function is added to test if an address is within a range. As a result of the latter the != and == operator is also removed in favour of the member function. A member function that returns a string representation is also created to allow debug printing. In general, this patch does not add any functionality, but it does take us closer to a situation where interleaving (and more cleverness) can be added under the bonnet without exposing it to the user. More on that in a later patch.
Diffstat (limited to 'src/base')
-rw-r--r--src/base/addr_range.hh116
-rw-r--r--src/base/addr_range_map.hh8
2 files changed, 68 insertions, 56 deletions
diff --git a/src/base/addr_range.hh b/src/base/addr_range.hh
index dcd53b77a..1e86aa859 100644
--- a/src/base/addr_range.hh
+++ b/src/base/addr_range.hh
@@ -45,32 +45,55 @@
#ifndef __BASE_ADDR_RANGE_HH__
#define __BASE_ADDR_RANGE_HH__
-#include <utility> // pair & make_pair
-
+#include "base/cprintf.hh"
#include "base/types.hh"
class AddrRange
{
- public:
+ private:
+
+ /// Private fields for the start and end of the range. In the
+ /// future, these will be extended with interleaving functionality
+ /// and hence should never be manipulated directly.
+ Addr _start;
+ Addr _end;
- Addr start;
- Addr end;
+ public:
AddrRange()
- : start(1), end(0)
+ : _start(1), _end(0)
{}
AddrRange(Addr _start, Addr _end)
- : start(_start), end(_end)
+ : _start(_start), _end(_end)
{}
- AddrRange(const std::pair<Addr, Addr> &r)
- : start(r.first), end(r.second)
- {}
+ /**
+ * Get the size of the address range. For a case where
+ * interleaving is used this should probably cause a panic.
+ */
+ Addr size() const { return _end - _start + 1; }
+
+ /**
+ * Determine if the range is valid.
+ */
+ bool valid() const { return _start < _end; }
+
+ /**
+ * Get the start address of the range.
+ */
+ Addr start() const { return _start; }
- Addr size() const { return end - start + 1; }
- bool valid() const { return start < end; }
+ /**
+ * Get a string representation of the range. This could
+ * alternatively be implemented as a operator<<, but at the moment
+ * that seems like overkill.
+ */
+ std::string to_string() const
+ {
+ return csprintf("[%#llx : %#llx]", _start, _end);
+ }
/**
* Determine if another range intersects this one, i.e. if there
@@ -82,8 +105,7 @@ class AddrRange
*/
bool intersects(const AddrRange& r) const
{
- return (start <= r.start && end >= r.start) ||
- (start <= r.end && end >= r.end);
+ return _start <= r._end && _end >= r._start;
}
/**
@@ -96,60 +118,50 @@ class AddrRange
*/
bool isSubset(const AddrRange& r) const
{
- return start >= r.start && end <= r.end;
+ return _start >= r._start && _end <= r._end;
+ }
+
+ /**
+ * Determine if the range contains an address.
+ *
+ * @param a Address to compare with
+ * @return true if the address is in the range
+ */
+ bool contains(const Addr& a) const
+ {
+ return a >= _start && a <= _end;
}
-};
/**
* Keep the operators away from SWIG.
*/
#ifndef SWIG
-/**
- * @param range1 is a range.
- * @param range2 is a range.
- * @return if range1 is less than range2 and does not overlap range1.
- */
-inline bool
-operator<(const AddrRange& range1, const AddrRange& range2)
-{
- return range1.start < range2.start;
-}
-
-/**
- * @param addr address in the range
- * @param range range compared against.
- * @return indicates that the address is not within the range.
- */
-inline bool
-operator!=(const Addr& addr, const AddrRange& range)
-{
- return addr < range.start || addr > range.end;
-}
+ /**
+ * Less-than operator used to turn an STL map into a binary search
+ * tree of non-overlapping address ranges.
+ *
+ * @param r Range to compare with
+ * @return true if the start address is less than that of the other range
+ */
+ bool operator<(const AddrRange& r) const
+ {
+ return _start < r._start;
+ }
-/**
- * @param range range compared against.
- * @param pos position compared to the range.
- * @return indicates that position pos is within the range.
- */
-inline bool
-operator==(const AddrRange& range, const Addr& addr)
-{
- return addr >= range.start && addr <= range.end;
-}
+#endif // SWIG
+};
inline AddrRange
RangeEx(Addr start, Addr end)
-{ return std::make_pair(start, end - 1); }
+{ return AddrRange(start, end - 1); }
inline AddrRange
RangeIn(Addr start, Addr end)
-{ return std::make_pair(start, end); }
+{ return AddrRange(start, end); }
inline AddrRange
RangeSize(Addr start, Addr size)
-{ return std::make_pair(start, start + size - 1); }
-
-#endif // SWIG
+{ return AddrRange(start, start + size - 1); }
#endif // __BASE_ADDR_RANGE_HH__
diff --git a/src/base/addr_range_map.hh b/src/base/addr_range_map.hh
index c35befdce..1992c5e48 100644
--- a/src/base/addr_range_map.hh
+++ b/src/base/addr_range_map.hh
@@ -73,7 +73,7 @@ class AddrRangeMap
i = tree.upper_bound(r);
if (i == tree.begin()) {
- if (i->first.start <= r.end && i->first.end >= r.start)
+ if (i->first.intersects(r))
return i;
else
// Nothing could match, so return end()
@@ -82,7 +82,7 @@ class AddrRangeMap
--i;
- if (i->first.start <= r.end && i->first.end >= r.start)
+ if (i->first.intersects(r))
return i;
return tree.end();
@@ -96,7 +96,7 @@ class AddrRangeMap
i = tree.upper_bound(r);
if (i == tree.begin()) {
- if (i->first.start <= r.end && i->first.end >= r.start)
+ if (i->first.intersects(r))
return i;
else
// Nothing could match, so return end()
@@ -105,7 +105,7 @@ class AddrRangeMap
--i;
- if (i->first.start <= r.end && i->first.end >= r.start)
+ if (i->first.intersects(r))
return i;
return tree.end();