addr_range_map.hh revision 9409
13804SN/A/*
29235Sandreas.hansson@arm.com * Copyright (c) 2012 ARM Limited
39235Sandreas.hansson@arm.com * All rights reserved
49235Sandreas.hansson@arm.com *
59235Sandreas.hansson@arm.com * The license below extends only to copyright in the software and shall
69235Sandreas.hansson@arm.com * not be construed as granting a license to any other intellectual
79235Sandreas.hansson@arm.com * property including but not limited to intellectual property relating
89235Sandreas.hansson@arm.com * to a hardware implementation of the functionality of the software
99235Sandreas.hansson@arm.com * licensed hereunder.  You may use the software subject to the license
109235Sandreas.hansson@arm.com * terms below provided that you ensure that this notice is replicated
119235Sandreas.hansson@arm.com * unmodified and in its entirety in all distributions of the software,
129235Sandreas.hansson@arm.com * modified or unmodified, in source code or in binary form.
139235Sandreas.hansson@arm.com *
143804SN/A * Copyright (c) 2006 The Regents of The University of Michigan
153804SN/A * All rights reserved.
163804SN/A *
173804SN/A * Redistribution and use in source and binary forms, with or without
183804SN/A * modification, are permitted provided that the following conditions are
193804SN/A * met: redistributions of source code must retain the above copyright
203804SN/A * notice, this list of conditions and the following disclaimer;
213804SN/A * redistributions in binary form must reproduce the above copyright
223804SN/A * notice, this list of conditions and the following disclaimer in the
233804SN/A * documentation and/or other materials provided with the distribution;
243804SN/A * neither the name of the copyright holders nor the names of its
253804SN/A * contributors may be used to endorse or promote products derived from
263804SN/A * this software without specific prior written permission.
273804SN/A *
283804SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
293804SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
303804SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
313804SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
323804SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
333804SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
343804SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
353804SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
363804SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
373804SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
383804SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
393804SN/A *
403804SN/A * Authors: Ali Saidi
419235Sandreas.hansson@arm.com *          Andreas Hansson
423804SN/A */
433804SN/A
449235Sandreas.hansson@arm.com#ifndef __BASE_ADDR_RANGE_MAP_HH__
459235Sandreas.hansson@arm.com#define __BASE_ADDR_RANGE_MAP_HH__
463804SN/A
478229SN/A#include <map>
488902SN/A#include <utility>
498229SN/A
509235Sandreas.hansson@arm.com#include "base/addr_range.hh"
513804SN/A
528918SN/A/**
539235Sandreas.hansson@arm.com * The AddrRangeMap uses an STL map to implement an interval tree for
549235Sandreas.hansson@arm.com * address decoding. The value stored is a template type and can be
559235Sandreas.hansson@arm.com * e.g. a port identifier, or a pointer.
568918SN/A */
579235Sandreas.hansson@arm.comtemplate <typename V>
589235Sandreas.hansson@arm.comclass AddrRangeMap
593804SN/A{
603804SN/A  private:
619235Sandreas.hansson@arm.com    typedef std::map<AddrRange, V> RangeMap;
623804SN/A    RangeMap tree;
633804SN/A
643804SN/A  public:
653804SN/A    typedef typename RangeMap::iterator iterator;
668918SN/A    typedef typename RangeMap::const_iterator const_iterator;
673804SN/A
688918SN/A    const_iterator
699235Sandreas.hansson@arm.com    find(const AddrRange &r) const
708918SN/A    {
719409Sandreas.hansson@arm.com        if (tree.empty())
729409Sandreas.hansson@arm.com            return tree.end();
738918SN/A
749409Sandreas.hansson@arm.com        const_iterator i = tree.upper_bound(r);
753804SN/A
763832SN/A        if (i == tree.begin()) {
779405Sandreas.hansson@arm.com            if (i->first.intersects(r))
783832SN/A                return i;
793832SN/A            else
803832SN/A                // Nothing could match, so return end()
813832SN/A                return tree.end();
823832SN/A        }
833804SN/A
848918SN/A        --i;
853804SN/A
869405Sandreas.hansson@arm.com        if (i->first.intersects(r))
873804SN/A            return i;
883804SN/A
893804SN/A        return tree.end();
903804SN/A    }
913804SN/A
928918SN/A    const_iterator
939235Sandreas.hansson@arm.com    find(const Addr &r) const
948918SN/A    {
958918SN/A        return find(RangeSize(r, 1));
968918SN/A    }
978918SN/A
989409Sandreas.hansson@arm.com    bool
999409Sandreas.hansson@arm.com    intersect(const AddrRange &r) const
1005609SN/A    {
1019409Sandreas.hansson@arm.com        return find(r) != tree.end();
1025609SN/A    }
1035609SN/A
1049409Sandreas.hansson@arm.com    const_iterator
1059235Sandreas.hansson@arm.com    insert(const AddrRange &r, const V& d)
1063804SN/A    {
1073804SN/A        if (intersect(r))
1083804SN/A            return tree.end();
1093804SN/A
1108902SN/A        return tree.insert(std::make_pair(r, d)).first;
1113804SN/A    }
1123804SN/A
1135608SN/A    void
1145608SN/A    erase(iterator p)
1153804SN/A    {
1163804SN/A        tree.erase(p);
1173804SN/A    }
1183804SN/A
1195608SN/A    void
1205608SN/A    erase(iterator p, iterator q)
1213804SN/A    {
1223804SN/A        tree.erase(p,q);
1233804SN/A    }
1243804SN/A
1255608SN/A    void
1265608SN/A    clear()
1273804SN/A    {
1283804SN/A        tree.erase(tree.begin(), tree.end());
1293804SN/A    }
1303804SN/A
1318918SN/A    const_iterator
1328918SN/A    begin() const
1338918SN/A    {
1348918SN/A        return tree.begin();
1358918SN/A    }
1368918SN/A
1375608SN/A    iterator
1385608SN/A    begin()
1393804SN/A    {
1403804SN/A        return tree.begin();
1413804SN/A    }
1423804SN/A
1438918SN/A    const_iterator
1448918SN/A    end() const
1458918SN/A    {
1468918SN/A        return tree.end();
1478918SN/A    }
1488918SN/A
1495608SN/A    iterator
1505608SN/A    end()
1513804SN/A    {
1523804SN/A        return tree.end();
1533804SN/A    }
1543804SN/A
1559235Sandreas.hansson@arm.com    std::size_t
1568918SN/A    size() const
1573804SN/A    {
1583804SN/A        return tree.size();
1593804SN/A    }
1603804SN/A
1615608SN/A    bool
1628918SN/A    empty() const
1633804SN/A    {
1643804SN/A        return tree.empty();
1653804SN/A    }
1663804SN/A};
1673804SN/A
1689235Sandreas.hansson@arm.com#endif //__BASE_ADDR_RANGE_MAP_HH__
169