addr_range.hh revision 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 9 * licensed hereunder. You may use the software subject to the license 10 * terms below provided that you ensure that this notice is replicated 11 * unmodified and in its entirety in all distributions of the software, 12 * modified or unmodified, in source code or in binary form. 13 * 14 * Copyright (c) 2002-2005 The Regents of The University of Michigan 15 * All rights reserved. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions are 19 * met: redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer; 21 * redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution; 24 * neither the name of the copyright holders nor the names of its 25 * contributors may be used to endorse or promote products derived from 26 * this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 39 * 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 "base/cprintf.hh" 49#include "base/types.hh" 50 51class AddrRange 52{ 53 54 private: 55 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; 61 62 public: 63 64 AddrRange() 65 : _start(1), _end(0) 66 {} 67 68 AddrRange(Addr _start, Addr _end) 69 : _start(_start), _end(_end) 70 {} 71 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; } 77 78 /** 79 * Determine if the range is valid. 80 */ 81 bool valid() const { return _start < _end; } 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 /** 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 { 108 return _start <= r._end && _end >= r._start; 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 { 121 return _start >= r._start && _end <= r._end; 122 } 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 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