addr_range.hh revision 9405:c0a0593510db
16145Snate@binkert.org/*
26145Snate@binkert.org * Copyright (c) 2012 ARM Limited
36145Snate@binkert.org * All rights reserved
46145Snate@binkert.org *
56145Snate@binkert.org * The license below extends only to copyright in the software and shall
66145Snate@binkert.org * not be construed as granting a license to any other intellectual
76145Snate@binkert.org * property including but not limited to intellectual property relating
86145Snate@binkert.org * to a hardware implementation of the functionality of the software
96145Snate@binkert.org * licensed hereunder.  You may use the software subject to the license
106145Snate@binkert.org * terms below provided that you ensure that this notice is replicated
116145Snate@binkert.org * unmodified and in its entirety in all distributions of the software,
126145Snate@binkert.org * modified or unmodified, in source code or in binary form.
136145Snate@binkert.org *
146145Snate@binkert.org * Copyright (c) 2002-2005 The Regents of The University of Michigan
156145Snate@binkert.org * All rights reserved.
166145Snate@binkert.org *
176145Snate@binkert.org * Redistribution and use in source and binary forms, with or without
186145Snate@binkert.org * modification, are permitted provided that the following conditions are
196145Snate@binkert.org * met: redistributions of source code must retain the above copyright
206145Snate@binkert.org * notice, this list of conditions and the following disclaimer;
216145Snate@binkert.org * redistributions in binary form must reproduce the above copyright
226145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
236145Snate@binkert.org * documentation and/or other materials provided with the distribution;
246145Snate@binkert.org * neither the name of the copyright holders nor the names of its
256145Snate@binkert.org * contributors may be used to endorse or promote products derived from
266145Snate@binkert.org * this software without specific prior written permission.
276145Snate@binkert.org *
286145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
296145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
306145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
316145Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
326145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
336145Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
346145Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
356145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
366145Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
376154Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
386154Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
396154Snate@binkert.org *
406154Snate@binkert.org * Authors: Nathan Binkert
416154Snate@binkert.org *          Steve Reinhardt
426154Snate@binkert.org *          Andreas Hansson
436154Snate@binkert.org */
446285Snate@binkert.org
456145Snate@binkert.org#ifndef __BASE_ADDR_RANGE_HH__
466145Snate@binkert.org#define __BASE_ADDR_RANGE_HH__
476145Snate@binkert.org
486145Snate@binkert.org#include "base/cprintf.hh"
496145Snate@binkert.org#include "base/types.hh"
506145Snate@binkert.org
516145Snate@binkert.orgclass AddrRange
526145Snate@binkert.org{
536145Snate@binkert.org
546145Snate@binkert.org  private:
556145Snate@binkert.org
566145Snate@binkert.org    /// Private fields for the start and end of the range. In the
576145Snate@binkert.org    /// future, these will be extended with interleaving functionality
586145Snate@binkert.org    /// and hence should never be manipulated directly.
596145Snate@binkert.org    Addr _start;
606145Snate@binkert.org    Addr _end;
616145Snate@binkert.org
626145Snate@binkert.org  public:
636145Snate@binkert.org
646145Snate@binkert.org    AddrRange()
656145Snate@binkert.org        : _start(1), _end(0)
666145Snate@binkert.org    {}
676145Snate@binkert.org
686145Snate@binkert.org    AddrRange(Addr _start, Addr _end)
696285Snate@binkert.org        : _start(_start), _end(_end)
706145Snate@binkert.org    {}
716145Snate@binkert.org
726145Snate@binkert.org    /**
736145Snate@binkert.org     * Get the size of the address range. For a case where
746145Snate@binkert.org     * interleaving is used this should probably cause a panic.
756145Snate@binkert.org     */
766145Snate@binkert.org    Addr size() const { return _end - _start + 1; }
776145Snate@binkert.org
786145Snate@binkert.org    /**
796145Snate@binkert.org     * Determine if the range is valid.
806145Snate@binkert.org     */
816145Snate@binkert.org    bool valid() const { return _start < _end; }
826285Snate@binkert.org
836145Snate@binkert.org    /**
846285Snate@binkert.org     * Get the start address of the range.
856285Snate@binkert.org     */
866145Snate@binkert.org    Addr start() const { return _start; }
876145Snate@binkert.org
886467Sdrh5@cs.wisc.edu    /**
896467Sdrh5@cs.wisc.edu     * Get a string representation of the range. This could
906467Sdrh5@cs.wisc.edu     * alternatively be implemented as a operator<<, but at the moment
916467Sdrh5@cs.wisc.edu     * that seems like overkill.
926467Sdrh5@cs.wisc.edu     */
936467Sdrh5@cs.wisc.edu    std::string to_string() const
946467Sdrh5@cs.wisc.edu    {
956467Sdrh5@cs.wisc.edu        return csprintf("[%#llx : %#llx]", _start, _end);
966145Snate@binkert.org    }
976145Snate@binkert.org
986145Snate@binkert.org    /**
996145Snate@binkert.org     * Determine if another range intersects this one, i.e. if there
1006145Snate@binkert.org     * is an address that is both in this range and the other
1016145Snate@binkert.org     * range. No check is made to ensure either range is valid.
1026145Snate@binkert.org     *
1036145Snate@binkert.org     * @param r Range to intersect with
1046145Snate@binkert.org     * @return true if the intersection of the two ranges is not empty
1056145Snate@binkert.org     */
1066145Snate@binkert.org    bool intersects(const AddrRange& r) const
1076145Snate@binkert.org    {
1086145Snate@binkert.org        return _start <= r._end && _end >= r._start;
1096145Snate@binkert.org    }
1106145Snate@binkert.org
1116372Sdrh5@cs.wisc.edu    /**
1126145Snate@binkert.org     * Determine if this range is a subset of another range, i.e. if
1136145Snate@binkert.org     * every address in this range is also in the other range. No
1146145Snate@binkert.org     * check is made to ensure either range is valid.
1156145Snate@binkert.org     *
1166145Snate@binkert.org     * @param r Range to compare with
1176145Snate@binkert.org     * @return true if the this range is a subset of the other one
1186145Snate@binkert.org     */
1196145Snate@binkert.org    bool isSubset(const AddrRange& r) const
1206145Snate@binkert.org    {
1216145Snate@binkert.org        return _start >= r._start && _end <= r._end;
1226145Snate@binkert.org    }
1236145Snate@binkert.org
1246145Snate@binkert.org    /**
1256145Snate@binkert.org     * Determine if the range contains an address.
1266145Snate@binkert.org     *
1276145Snate@binkert.org     * @param a Address to compare with
1286145Snate@binkert.org     * @return true if the address is in the range
1296145Snate@binkert.org     */
1306145Snate@binkert.org    bool contains(const Addr& a) const
1316145Snate@binkert.org    {
1326145Snate@binkert.org        return a >= _start && a <= _end;
1336145Snate@binkert.org    }
1346145Snate@binkert.org
135/**
136 * Keep the operators away from SWIG.
137 */
138#ifndef SWIG
139
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    }
151
152#endif // SWIG
153};
154
155inline AddrRange
156RangeEx(Addr start, Addr end)
157{ return AddrRange(start, end - 1); }
158
159inline AddrRange
160RangeIn(Addr start, Addr end)
161{ return AddrRange(start, end); }
162
163inline AddrRange
164RangeSize(Addr start, Addr size)
165{ return AddrRange(start, start + size - 1); }
166
167#endif // __BASE_ADDR_RANGE_HH__
168