AddrRange: Transition from Range<T> to AddrRange

This patch takes the final plunge and transitions from the templated
Range class to the more specific AddrRange. In doing so it changes the
obvious Range<Addr> to AddrRange, and also bumps the range_map to be
AddrRangeMap.

In addition to the obvious changes, including the removal of redundant
includes, this patch also does some house keeping in preparing for the
introduction of address interleaving support in the ranges. The Range
class is also stripped of all the functionality that is never used.

--HG--
rename : src/base/range.hh => src/base/addr_range.hh
rename : src/base/range_map.hh => src/base/addr_range_map.hh
This commit is contained in:
Andreas Hansson
2012-09-19 06:15:44 -04:00
parent c34df76272
commit ffb6aec603
48 changed files with 213 additions and 454 deletions

126
src/base/addr_range.hh Normal file
View File

@@ -0,0 +1,126 @@
/*
* Copyright (c) 2012 ARM Limited
* All rights reserved
*
* The license below extends only to copyright in the software and shall
* not be construed as granting a license to any other intellectual
* property including but not limited to intellectual property relating
* to a hardware implementation of the functionality of the software
* licensed hereunder. You may use the software subject to the license
* terms below provided that you ensure that this notice is replicated
* unmodified and in its entirety in all distributions of the software,
* modified or unmodified, in source code or in binary form.
*
* Copyright (c) 2002-2005 The Regents of The University of Michigan
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer;
* redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution;
* neither the name of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Authors: Nathan Binkert
* Steve Reinhardt
* Andreas Hansson
*/
#ifndef __BASE_ADDR_RANGE_HH__
#define __BASE_ADDR_RANGE_HH__
#include "base/types.hh"
class AddrRange
{
public:
Addr start;
Addr end;
AddrRange()
: start(1), end(0)
{}
AddrRange(Addr _start, Addr _end)
: start(_start), end(_end)
{}
AddrRange(const std::pair<Addr, Addr> &r)
: start(r.first), end(r.second)
{}
Addr size() const { return end - start + 1; }
bool valid() const { return start < 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;
}
/**
* @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;
}
inline AddrRange
RangeEx(Addr start, Addr end)
{ return std::make_pair(start, end - 1); }
inline AddrRange
RangeIn(Addr start, Addr end)
{ return std::make_pair(start, end); }
inline AddrRange
RangeSize(Addr start, Addr size)
{ return std::make_pair(start, start + size - 1); }
#endif // SWIG
#endif // __BASE_ADDR_RANGE_HH__

View File

@@ -1,4 +1,16 @@
/*
* Copyright (c) 2012 ARM Limited
* All rights reserved
*
* The license below extends only to copyright in the software and shall
* not be construed as granting a license to any other intellectual
* property including but not limited to intellectual property relating
* to a hardware implementation of the functionality of the software
* licensed hereunder. You may use the software subject to the license
* terms below provided that you ensure that this notice is replicated
* unmodified and in its entirety in all distributions of the software,
* modified or unmodified, in source code or in binary form.
*
* Copyright (c) 2006 The Regents of The University of Michigan
* All rights reserved.
*
@@ -26,36 +38,35 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Authors: Ali Saidi
* Andreas Hansson
*/
#ifndef __BASE_RANGE_MAP_HH__
#define __BASE_RANGE_MAP_HH__
#ifndef __BASE_ADDR_RANGE_MAP_HH__
#define __BASE_ADDR_RANGE_MAP_HH__
#include <map>
#include <utility>
#include "base/range.hh"
#include "base/addr_range.hh"
/**
* The range_map uses an STL map to implement an interval tree. The
* type of both the key (range) and the value are template
* parameters. It can, for example, be used for address decoding,
* using a range of addresses to map to ports.
* The AddrRangeMap uses an STL map to implement an interval tree for
* address decoding. The value stored is a template type and can be
* e.g. a port identifier, or a pointer.
*/
template <class T,class V>
class range_map
template <typename V>
class AddrRangeMap
{
private:
typedef std::map<Range<T>,V> RangeMap;
typedef std::map<AddrRange, V> RangeMap;
RangeMap tree;
public:
typedef typename RangeMap::iterator iterator;
typedef typename RangeMap::const_iterator const_iterator;
template <class U>
const_iterator
find(const Range<U> &r) const
find(const AddrRange &r) const
{
const_iterator i;
@@ -77,9 +88,8 @@ class range_map
return tree.end();
}
template <class U>
iterator
find(const Range<U> &r)
find(const AddrRange &r)
{
iterator i;
@@ -101,23 +111,20 @@ class range_map
return tree.end();
}
template <class U>
const_iterator
find(const U &r) const
find(const Addr &r) const
{
return find(RangeSize(r, 1));
}
template <class U>
iterator
find(const U &r)
find(const Addr &r)
{
return find(RangeSize(r, 1));
}
template <class U>
bool
intersect(const Range<U> &r)
intersect(const AddrRange &r)
{
iterator i;
i = find(r);
@@ -126,9 +133,8 @@ class range_map
return false;
}
template <class U,class W>
iterator
insert(const Range<U> &r, const W d)
insert(const AddrRange &r, const V& d)
{
if (intersect(r))
return tree.end();
@@ -136,8 +142,8 @@ class range_map
return tree.insert(std::make_pair(r, d)).first;
}
size_t
erase(T k)
std::size_t
erase(Addr k)
{
return tree.erase(k);
}
@@ -184,7 +190,7 @@ class range_map
return tree.end();
}
size_t
std::size_t
size() const
{
return tree.size();
@@ -197,4 +203,4 @@ class range_map
}
};
#endif //__BASE_RANGE_MAP_HH__
#endif //__BASE_ADDR_RANGE_MAP_HH__

View File

@@ -39,7 +39,6 @@
#include <utility>
#include <vector>
#include "base/range.hh"
#include "base/types.hh"
#include "dev/etherpkt.hh"
#include "dnet/os.h"

View File

@@ -42,7 +42,6 @@
#include <ios>
#include <string>
#include "base/range.hh"
#include "base/types.hh"
class Checkpoint;
@@ -210,13 +209,6 @@ class Random
return _random(min, max);
}
template <typename T>
T
random(const Range<T> &range)
{
return _random(range.start, range.end);
}
// [0,1]
double
gen_real1()

View File

@@ -1,328 +0,0 @@
/*
* Copyright (c) 2002-2005 The Regents of The University of Michigan
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer;
* redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution;
* neither the name of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Authors: Nathan Binkert
* Steve Reinhardt
*/
#ifndef __BASE_RANGE_HH__
#define __BASE_RANGE_HH__
template <class T>
struct Range
{
T start;
T end;
Range() { invalidate(); }
template <class U>
Range(const std::pair<U, U> &r)
: start(r.first), end(r.second)
{}
template <class U>
Range(const Range<U> &r)
: start(r.start), end(r.end)
{}
template <class U>
const Range<T> &operator=(const Range<U> &r)
{
start = r.start;
end = r.end;
return *this;
}
template <class U>
const Range<T> &operator=(const std::pair<U, U> &r)
{
start = r.first;
end = r.second;
return *this;
}
void invalidate() { start = 1; end = 0; }
T size() const { return end - start + 1; }
bool valid() const { return start < end; }
};
template <class T>
inline Range<T>
RangeEx(T start, T end)
{ return std::make_pair(start, end - 1); }
template <class T>
inline Range<T>
RangeIn(T start, T end)
{ return std::make_pair(start, end); }
template <class T, class U>
inline Range<T>
RangeSize(T start, U size)
{ return std::make_pair(start, start + size - 1); }
////////////////////////////////////////////////////////////////////////
//
// Range to Range Comparisons
//
/**
* @param range1 is a range.
* @param range2 is a range.
* @return if range1 and range2 are identical.
*/
template <class T, class U>
inline bool
operator==(const Range<T> &range1, const Range<U> &range2)
{
return range1.start == range2.start && range1.end == range2.end;
}
/**
* @param range1 is a range.
* @param range2 is a range.
* @return if range1 and range2 are not identical.
*/
template <class T, class U>
inline bool
operator!=(const Range<T> &range1, const Range<U> &range2)
{
return range1.start != range2.start || range1.end != range2.end;
}
/**
* @param range1 is a range.
* @param range2 is a range.
* @return if range1 is less than range2 and does not overlap range1.
*/
template <class T, class U>
inline bool
operator<(const Range<T> &range1, const Range<U> &range2)
{
return range1.start < range2.start;
}
/**
* @param range1 is a range.
* @param range2 is a range.
* @return if range1 is less than range2. range1 may overlap range2,
* but not extend beyond the end of range2.
*/
template <class T, class U>
inline bool
operator<=(const Range<T> &range1, const Range<U> &range2)
{
return range1.start <= range2.start;
}
/**
* @param range1 is a range.
* @param range2 is a range.
* @return if range1 is greater than range2 and does not overlap range2.
*/
template <class T, class U>
inline bool
operator>(const Range<T> &range1, const Range<U> &range2)
{
return range1.start > range2.start;
}
/**
* @param range1 is a range.
* @param range2 is a range.
* @return if range1 is greater than range2. range1 may overlap range2,
* but not extend beyond the beginning of range2.
*/
template <class T, class U>
inline bool
operator>=(const Range<T> &range1, const Range<U> &range2)
{
return range1.start >= range2.start;
}
////////////////////////////////////////////////////////////////////////
//
// Position to Range Comparisons
//
/**
* @param pos position compared to the range.
* @param range range compared against.
* @return indicates that position pos is within the range.
*/
template <class T, class U>
inline bool
operator==(const T &pos, const Range<U> &range)
{
return pos >= range.start && pos <= range.end;
}
/**
* @param pos position compared to the range.
* @param range range compared against.
* @return indicates that position pos is not within the range.
*/
template <class T, class U>
inline bool
operator!=(const T &pos, const Range<U> &range)
{
return pos < range.start || pos > range.end;
}
/**
* @param pos position compared to the range.
* @param range range compared against.
* @return indicates that position pos is below the range.
*/
template <class T, class U>
inline bool
operator<(const T &pos, const Range<U> &range)
{
return pos < range.start;
}
/**
* @param pos position compared to the range.
* @param range range compared against.
* @return indicates that position pos is below or in the range.
*/
template <class T, class U>
inline bool
operator<=(const T &pos, const Range<U> &range)
{
return pos <= range.end;
}
/**
* @param pos position compared to the range.
* @param range range compared against.
* @return indicates that position pos is above the range.
*/
template <class T, class U>
inline bool
operator>(const T &pos, const Range<U> &range)
{
return pos > range.end;
}
/**
* @param pos position compared to the range.
* @param range range compared against.
* @return indicates that position pos is above or in the range.
*/
template <class T, class U>
inline bool
operator>=(const T &pos, const Range<U> &range)
{
return pos >= range.start;
}
////////////////////////////////////////////////////////////////////////
//
// Range to Position Comparisons (for symmetry)
//
/**
* @param range range compared against.
* @param pos position compared to the range.
* @return indicates that position pos is within the range.
*/
template <class T, class U>
inline bool
operator==(const Range<T> &range, const U &pos)
{
return pos >= range.start && pos <= range.end;
}
/**
* @param range range compared against.
* @param pos position compared to the range.
* @return indicates that position pos is not within the range.
*/
template <class T, class U>
inline bool
operator!=(const Range<T> &range, const U &pos)
{
return pos < range.start || pos > range.end;
}
/**
* @param range range compared against.
* @param pos position compared to the range.
* @return indicates that position pos is above the range.
*/
template <class T, class U>
inline bool
operator<(const Range<T> &range, const U &pos)
{
// with -std=gnu++0x, gcc and clang get confused when range.end is
// compared to pos using the operator "<", and the parser expects it
// to be the opening bracket for a template parameter,
// i.e. range.end<pos>(...);, the reason seems to be the range-type
// iteration introduced in c++11 where begin and end are members
// that return iterators
return operator<(range.end, pos);
}
/**
* @param range range compared against.
* @param pos position compared to the range.
* @return indicates that position pos is above or in the range.
*/
template <class T, class U>
inline bool
operator<=(const Range<T> &range, const U &pos)
{
return range.start <= pos;
}
/**
* @param range range compared against.
* @param pos position compared to the range.
* 'range > pos' indicates that position pos is below the range.
*/
template <class T, class U>
inline bool
operator>(const Range<T> &range, const U &pos)
{
return range.start > pos;
}
/**
* @param range range compared against.
* @param pos position compared to the range.
* 'range >= pos' indicates that position pos is below or in the range.
*/
template <class T, class U>
inline bool
operator>=(const Range<T> &range, const U &pos)
{
return range.end >= pos;
}
#endif // __BASE_RANGE_HH__