addr_range.hh revision 9279
12SN/A/* 21762SN/A * Copyright (c) 2012 ARM Limited 35502Snate@binkert.org * All rights reserved 42SN/A * 52SN/A * The license below extends only to copyright in the software and shall 62SN/A * not be construed as granting a license to any other intellectual 72SN/A * property including but not limited to intellectual property relating 82SN/A * to a hardware implementation of the functionality of the software 92SN/A * licensed hereunder. You may use the software subject to the license 102SN/A * terms below provided that you ensure that this notice is replicated 112SN/A * unmodified and in its entirety in all distributions of the software, 122SN/A * modified or unmodified, in source code or in binary form. 132SN/A * 142SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan 152SN/A * All rights reserved. 162SN/A * 172SN/A * Redistribution and use in source and binary forms, with or without 182SN/A * modification, are permitted provided that the following conditions are 192SN/A * met: redistributions of source code must retain the above copyright 202SN/A * notice, this list of conditions and the following disclaimer; 212SN/A * redistributions in binary form must reproduce the above copyright 222SN/A * notice, this list of conditions and the following disclaimer in the 232SN/A * documentation and/or other materials provided with the distribution; 242SN/A * neither the name of the copyright holders nor the names of its 252SN/A * contributors may be used to endorse or promote products derived from 262SN/A * this software without specific prior written permission. 272SN/A * 282665Ssaidi@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 292665Ssaidi@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 302665Ssaidi@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 312665Ssaidi@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 322SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 332SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 345501Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 352SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 362SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 372SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 382SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 395502Snate@binkert.org * 405501Snate@binkert.org * Authors: Nathan Binkert 415501Snate@binkert.org * Steve Reinhardt 421717SN/A * Andreas Hansson 435501Snate@binkert.org */ 4456SN/A 452SN/A#ifndef __BASE_ADDR_RANGE_HH__ 462SN/A#define __BASE_ADDR_RANGE_HH__ 472SN/A 482SN/A#include "base/types.hh" 492SN/A 502SN/Aclass AddrRange 512SN/A{ 522SN/A 532SN/A public: 545605Snate@binkert.org 552SN/A Addr start; 564017Sstever@eecs.umich.edu Addr end; 574016Sstever@eecs.umich.edu 584017Sstever@eecs.umich.edu AddrRange() 594016Sstever@eecs.umich.edu : start(1), end(0) 605768Snate@binkert.org {} 615768Snate@binkert.org 625774Snate@binkert.org AddrRange(Addr _start, Addr _end) 635768Snate@binkert.org : start(_start), end(_end) 645768Snate@binkert.org {} 655768Snate@binkert.org 665768Snate@binkert.org AddrRange(const std::pair<Addr, Addr> &r) 675768Snate@binkert.org : start(r.first), end(r.second) 685768Snate@binkert.org {} 695768Snate@binkert.org 705768Snate@binkert.org Addr size() const { return end - start + 1; } 715768Snate@binkert.org bool valid() const { return start < end; } 725768Snate@binkert.org 735768Snate@binkert.org /** 745768Snate@binkert.org * Determine if another range intersects this one, i.e. if there 755768Snate@binkert.org * is an address that is both in this range and the other 765602Snate@binkert.org * range. No check is made to ensure either range is valid. 775602Snate@binkert.org * 785502Snate@binkert.org * @param r Range to intersect with 795503Snate@binkert.org * @return true if the intersection of the two ranges is not empty 805502Snate@binkert.org */ 815502Snate@binkert.org bool intersects(const AddrRange& r) const 825502Snate@binkert.org { 835502Snate@binkert.org return (start <= r.start && end >= r.start) || 845502Snate@binkert.org (start <= r.end && end >= r.end); 855503Snate@binkert.org } 865502Snate@binkert.org 875502Snate@binkert.org /** 885502Snate@binkert.org * Determine if this range is a subset of another range, i.e. if 895502Snate@binkert.org * every address in this range is also in the other range. No 905503Snate@binkert.org * check is made to ensure either range is valid. 915503Snate@binkert.org * 925503Snate@binkert.org * @param r Range to compare with 935502Snate@binkert.org * @return true if the this range is a subset of the other one 945503Snate@binkert.org */ 955502Snate@binkert.org bool isSubset(const AddrRange& r) const 965502Snate@binkert.org { 972SN/A return start >= r.start && end <= r.end; 982SN/A } 992SN/A}; 1005502Snate@binkert.org 1015502Snate@binkert.org/** 1025602Snate@binkert.org * Keep the operators away from SWIG. 1035502Snate@binkert.org */ 1045502Snate@binkert.org#ifndef SWIG 1052SN/A 1065502Snate@binkert.org/** 1075502Snate@binkert.org * @param range1 is a range. 1085503Snate@binkert.org * @param range2 is a range. 1095503Snate@binkert.org * @return if range1 is less than range2 and does not overlap range1. 1105503Snate@binkert.org */ 1115503Snate@binkert.orginline bool 1125503Snate@binkert.orgoperator<(const AddrRange& range1, const AddrRange& range2) 1135502Snate@binkert.org{ 1142SN/A return range1.start < range2.start; 1155503Snate@binkert.org} 1165503Snate@binkert.org 1175602Snate@binkert.org/** 1185502Snate@binkert.org * @param addr address in the range 1192SN/A * @param range range compared against. 1205602Snate@binkert.org * @return indicates that the address is not within the range. 1215602Snate@binkert.org */ 1225502Snate@binkert.orginline bool 1235503Snate@binkert.orgoperator!=(const Addr& addr, const AddrRange& range) 1245503Snate@binkert.org{ 1255502Snate@binkert.org return addr < range.start || addr > range.end; 1265503Snate@binkert.org} 1275503Snate@binkert.org 1285503Snate@binkert.org/** 1295503Snate@binkert.org * @param range range compared against. 1305503Snate@binkert.org * @param pos position compared to the range. 1315503Snate@binkert.org * @return indicates that position pos is within the range. 1325503Snate@binkert.org */ 1335503Snate@binkert.orginline bool 1345503Snate@binkert.orgoperator==(const AddrRange& range, const Addr& addr) 1355503Snate@binkert.org{ 1365503Snate@binkert.org return addr >= range.start && addr <= range.end; 1375503Snate@binkert.org} 1385503Snate@binkert.org 1395503Snate@binkert.orginline AddrRange 1405502Snate@binkert.orgRangeEx(Addr start, Addr end) 1415502Snate@binkert.org{ return std::make_pair(start, end - 1); } 1425503Snate@binkert.org 1435503Snate@binkert.orginline AddrRange 1442SN/ARangeIn(Addr start, Addr end) 1455502Snate@binkert.org{ return std::make_pair(start, end); } 1465503Snate@binkert.org 1475503Snate@binkert.orginline AddrRange 1485503Snate@binkert.orgRangeSize(Addr start, Addr size) 1492SN/A{ return std::make_pair(start, start + size - 1); } 1502SN/A 1512SN/A#endif // SWIG 1522SN/A 1532SN/A#endif // __BASE_ADDR_RANGE_HH__ 1542SN/A