addr_range_map.hh revision 9405
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    {
718918SN/A        const_iterator i;
728918SN/A
738918SN/A        i = tree.upper_bound(r);
748918SN/A
758918SN/A        if (i == tree.begin()) {
769405Sandreas.hansson@arm.com            if (i->first.intersects(r))
778918SN/A                return i;
788918SN/A            else
798918SN/A                // Nothing could match, so return end()
808918SN/A                return tree.end();
818918SN/A        }
828918SN/A
838918SN/A        --i;
848918SN/A
859405Sandreas.hansson@arm.com        if (i->first.intersects(r))
868918SN/A            return i;
878918SN/A
888918SN/A        return tree.end();
898918SN/A    }
908918SN/A
918918SN/A    iterator
929235Sandreas.hansson@arm.com    find(const AddrRange &r)
933804SN/A    {
943804SN/A        iterator i;
953804SN/A
963804SN/A        i = tree.upper_bound(r);
973804SN/A
983832SN/A        if (i == tree.begin()) {
999405Sandreas.hansson@arm.com            if (i->first.intersects(r))
1003832SN/A                return i;
1013832SN/A            else
1023832SN/A                // Nothing could match, so return end()
1033832SN/A                return tree.end();
1043832SN/A        }
1053804SN/A
1068918SN/A        --i;
1073804SN/A
1089405Sandreas.hansson@arm.com        if (i->first.intersects(r))
1093804SN/A            return i;
1103804SN/A
1113804SN/A        return tree.end();
1123804SN/A    }
1133804SN/A
1148918SN/A    const_iterator
1159235Sandreas.hansson@arm.com    find(const Addr &r) const
1168918SN/A    {
1178918SN/A        return find(RangeSize(r, 1));
1188918SN/A    }
1198918SN/A
1208918SN/A    iterator
1219235Sandreas.hansson@arm.com    find(const Addr &r)
1225609SN/A    {
1235609SN/A        return find(RangeSize(r, 1));
1245609SN/A    }
1255609SN/A
1265608SN/A    bool
1279235Sandreas.hansson@arm.com    intersect(const AddrRange &r)
1283804SN/A    {
1293804SN/A        iterator i;
1303804SN/A        i = find(r);
1313804SN/A        if (i != tree.end())
1323804SN/A            return true;
1333804SN/A        return false;
1343804SN/A    }
1353804SN/A
1365608SN/A    iterator
1379235Sandreas.hansson@arm.com    insert(const AddrRange &r, const V& d)
1383804SN/A    {
1393804SN/A        if (intersect(r))
1403804SN/A            return tree.end();
1413804SN/A
1428902SN/A        return tree.insert(std::make_pair(r, d)).first;
1433804SN/A    }
1443804SN/A
1459235Sandreas.hansson@arm.com    std::size_t
1469235Sandreas.hansson@arm.com    erase(Addr k)
1473804SN/A    {
1483804SN/A        return tree.erase(k);
1493804SN/A    }
1503804SN/A
1515608SN/A    void
1525608SN/A    erase(iterator p)
1533804SN/A    {
1543804SN/A        tree.erase(p);
1553804SN/A    }
1563804SN/A
1575608SN/A    void
1585608SN/A    erase(iterator p, iterator q)
1593804SN/A    {
1603804SN/A        tree.erase(p,q);
1613804SN/A    }
1623804SN/A
1635608SN/A    void
1645608SN/A    clear()
1653804SN/A    {
1663804SN/A        tree.erase(tree.begin(), tree.end());
1673804SN/A    }
1683804SN/A
1698918SN/A    const_iterator
1708918SN/A    begin() const
1718918SN/A    {
1728918SN/A        return tree.begin();
1738918SN/A    }
1748918SN/A
1755608SN/A    iterator
1765608SN/A    begin()
1773804SN/A    {
1783804SN/A        return tree.begin();
1793804SN/A    }
1803804SN/A
1818918SN/A    const_iterator
1828918SN/A    end() const
1838918SN/A    {
1848918SN/A        return tree.end();
1858918SN/A    }
1868918SN/A
1875608SN/A    iterator
1885608SN/A    end()
1893804SN/A    {
1903804SN/A        return tree.end();
1913804SN/A    }
1923804SN/A
1939235Sandreas.hansson@arm.com    std::size_t
1948918SN/A    size() const
1953804SN/A    {
1963804SN/A        return tree.size();
1973804SN/A    }
1983804SN/A
1995608SN/A    bool
2008918SN/A    empty() const
2013804SN/A    {
2023804SN/A        return tree.empty();
2033804SN/A    }
2043804SN/A};
2053804SN/A
2069235Sandreas.hansson@arm.com#endif //__BASE_ADDR_RANGE_MAP_HH__
207