changeset c0a0593510db in /z/repo/gem5
details: http://repo.gem5.org/gem5?cmd=changeset;node=c0a0593510db
description:
        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:

 src/arch/arm/linux/system.cc   |    2 +-
 src/base/addr_range.hh         |  116 ++++++++++++++++++++++------------------
 src/base/addr_range_map.hh     |    8 +-
 src/kern/tru64/tru64_events.cc |    2 +-
 src/mem/abstract_mem.cc        |   12 ++--
 src/mem/abstract_mem.hh        |    2 +-
 src/mem/addr_mapper.cc         |   15 ++--
 src/mem/bus.cc                 |   20 +++---
 src/mem/bus.hh                 |    6 +-
 src/mem/physical.cc            |   14 ++--
 10 files changed, 104 insertions(+), 93 deletions(-)

diffs (truncated from 438 to 300 lines):

diff -r c194718a592c -r c0a0593510db src/arch/arm/linux/system.cc
--- a/src/arch/arm/linux/system.cc      Mon Jan 07 13:05:38 2013 -0500
+++ b/src/arch/arm/linux/system.cc      Mon Jan 07 13:05:38 2013 -0500
@@ -171,7 +171,7 @@
         }
         AtagMem am;
         am.memSize(atagRanges.begin()->size());
-        am.memStart(atagRanges.begin()->start);
+        am.memStart(atagRanges.begin()->start());
 
         AtagCmdline ad;
         ad.cmdline(params()->boot_osflags);
diff -r c194718a592c -r c0a0593510db src/base/addr_range.hh
--- a/src/base/addr_range.hh    Mon Jan 07 13:05:38 2013 -0500
+++ b/src/base/addr_range.hh    Mon Jan 07 13:05:38 2013 -0500
@@ -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
 {
 
+  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;
+
   public:
 
-    Addr start;
-    Addr end;
-
     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; }
 
-    Addr size() const { return end - start + 1; }
-    bool valid() const { return start < end; }
+    /**
+     * Determine if the range is valid.
+     */
+    bool valid() const { return _start < _end; }
+
+    /**
+     * Get the start address of the range.
+     */
+    Addr start() const { return _start; }
+
+    /**
+     * 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 @@
      */
     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 @@
      */
     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;
-}
+    /**
+     * 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 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;
-}
-
-/**
- * @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 -r c194718a592c -r c0a0593510db src/base/addr_range_map.hh
--- a/src/base/addr_range_map.hh        Mon Jan 07 13:05:38 2013 -0500
+++ b/src/base/addr_range_map.hh        Mon Jan 07 13:05:38 2013 -0500
@@ -73,7 +73,7 @@
         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 @@
 
         --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 @@
         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 @@
 
         --i;
 
-        if (i->first.start <= r.end && i->first.end >= r.start)
+        if (i->first.intersects(r))
             return i;
 
         return tree.end();
diff -r c194718a592c -r c0a0593510db src/kern/tru64/tru64_events.cc
--- a/src/kern/tru64/tru64_events.cc    Mon Jan 07 13:05:38 2013 -0500
+++ b/src/kern/tru64/tru64_events.cc    Mon Jan 07 13:05:38 2013 -0500
@@ -65,7 +65,7 @@
     // get the address ranges of the connected slave port
     AddrRangeList resp = dataPort.getAddrRanges();
     for (iter = resp.begin(); iter != resp.end(); iter++) {
-        if (*iter == (K0Seg2Phys(a0) & PAddrImplMask))
+        if (iter->contains(K0Seg2Phys(a0) & PAddrImplMask))
             found = true;
     }
 
diff -r c194718a592c -r c0a0593510db src/mem/abstract_mem.cc
--- a/src/mem/abstract_mem.cc   Mon Jan 07 13:05:38 2013 -0500
+++ b/src/mem/abstract_mem.cc   Mon Jan 07 13:05:38 2013 -0500
@@ -303,8 +303,8 @@
 void
 AbstractMemory::access(PacketPtr pkt)
 {
-    assert(pkt->getAddr() >= range.start &&
-           (pkt->getAddr() + pkt->getSize() - 1) <= range.end);
+    assert(AddrRange(pkt->getAddr(),
+                     pkt->getAddr() + pkt->getSize() - 1).isSubset(range));
 
     if (pkt->memInhibitAsserted()) {
         DPRINTF(MemoryAccess, "mem inhibited on 0x%x: not responding\n",
@@ -312,7 +312,7 @@
         return;
     }
 
-    uint8_t *hostAddr = pmemAddr + pkt->getAddr() - range.start;
+    uint8_t *hostAddr = pmemAddr + pkt->getAddr() - range.start();
 
     if (pkt->cmd == MemCmd::SwapReq) {
         TheISA::IntReg overwrite_val;
@@ -384,10 +384,10 @@
 void
 AbstractMemory::functionalAccess(PacketPtr pkt)
 {
-    assert(pkt->getAddr() >= range.start &&
-           (pkt->getAddr() + pkt->getSize() - 1) <= range.end);
+    assert(AddrRange(pkt->getAddr(),
+                     pkt->getAddr() + pkt->getSize() - 1).isSubset(range));
 
-    uint8_t *hostAddr = pmemAddr + pkt->getAddr() - range.start;
+    uint8_t *hostAddr = pmemAddr + pkt->getAddr() - range.start();
 
     if (pkt->isRead()) {
         if (pmemAddr)
diff -r c194718a592c -r c0a0593510db src/mem/abstract_mem.hh
--- a/src/mem/abstract_mem.hh   Mon Jan 07 13:05:38 2013 -0500
+++ b/src/mem/abstract_mem.hh   Mon Jan 07 13:05:38 2013 -0500
@@ -266,7 +266,7 @@
      *
      * @return the start address of the memory
      */
-    Addr start() const { return range.start; }
+    Addr start() const { return range.start(); }
 
     /**
      *  Should this memory be passed to the kernel and part of the OS
diff -r c194718a592c -r c0a0593510db src/mem/addr_mapper.cc
--- a/src/mem/addr_mapper.cc    Mon Jan 07 13:05:38 2013 -0500
+++ b/src/mem/addr_mapper.cc    Mon Jan 07 13:05:38 2013 -0500
@@ -253,9 +253,9 @@
 RangeAddrMapper::remapAddr(Addr addr) const
 {
     for (int i = 0; i < originalRanges.size(); ++i) {
-        if (originalRanges[i] == addr) {
-            Addr offset = addr - originalRanges[i].start;
-            return offset + remappedRanges[i].start;
+        if (originalRanges[i].contains(addr)) {
+            Addr offset = addr - originalRanges[i].start();
+            return offset + remappedRanges[i].start();
         }
     }
 
@@ -277,11 +277,12 @@
                       " ranges but are not a subset.\n");
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to