/ src / processor / static_range_map-inl.h
static_range_map-inl.h
  1  // Copyright 2010 Google LLC
  2  //
  3  // Redistribution and use in source and binary forms, with or without
  4  // modification, are permitted provided that the following conditions are
  5  // met:
  6  //
  7  //     * Redistributions of source code must retain the above copyright
  8  // notice, this list of conditions and the following disclaimer.
  9  //     * Redistributions in binary form must reproduce the above
 10  // copyright notice, this list of conditions and the following disclaimer
 11  // in the documentation and/or other materials provided with the
 12  // distribution.
 13  //     * Neither the name of Google LLC nor the names of its
 14  // contributors may be used to endorse or promote products derived from
 15  // this software without specific prior written permission.
 16  //
 17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 21  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 22  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 23  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 24  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 25  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 27  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28  
 29  // static_range_map-inl.h: StaticRangeMap implementation.
 30  //
 31  // See static_range_map.h for documentation.
 32  //
 33  // Author: Siyang Xie (lambxsy@google.com)
 34  
 35  #ifndef PROCESSOR_STATIC_RANGE_MAP_INL_H__
 36  #define PROCESSOR_STATIC_RANGE_MAP_INL_H__
 37  
 38  #include "processor/static_range_map.h"
 39  #include "processor/logging.h"
 40  
 41  namespace google_breakpad {
 42  
 43  template<typename AddressType, typename EntryType>
 44  bool StaticRangeMap<AddressType, EntryType>::RetrieveRange(
 45      const AddressType& address, const EntryType*& entry,
 46      AddressType* entry_base, AddressType* entry_size) const {
 47    MapConstIterator iterator = map_.lower_bound(address);
 48    if (iterator == map_.end())
 49      return false;
 50  
 51    // The map is keyed by the high address of each range, so |address| is
 52    // guaranteed to be lower than the range's high address.  If |range| is
 53    // not directly preceded by another range, it's possible for address to
 54    // be below the range's low address, though.  When that happens, address
 55    // references something not within any range, so return false.
 56  
 57    const Range* range = iterator.GetValuePtr();
 58  
 59    // Make sure AddressType and EntryType are copyable basic types
 60    // e.g.: integer types, pointers etc
 61    if (address < range->base())
 62      return false;
 63  
 64    entry = range->entryptr();
 65    if (entry_base)
 66      *entry_base = range->base();
 67    if (entry_size)
 68      *entry_size = iterator.GetKey() - range->base() + 1;
 69  
 70    return true;
 71  }
 72  
 73  
 74  template<typename AddressType, typename EntryType>
 75  bool StaticRangeMap<AddressType, EntryType>::RetrieveNearestRange(
 76      const AddressType& address, const EntryType*& entry,
 77      AddressType* entry_base, AddressType* entry_size) const {
 78    // If address is within a range, RetrieveRange can handle it.
 79    if (RetrieveRange(address, entry, entry_base, entry_size))
 80      return true;
 81  
 82    // upper_bound gives the first element whose key is greater than address,
 83    // but we want the first element whose key is less than or equal to address.
 84    // Decrement the iterator to get there, but not if the upper_bound already
 85    // points to the beginning of the map - in that case, address is lower than
 86    // the lowest stored key, so return false.
 87  
 88    MapConstIterator iterator = map_.upper_bound(address);
 89    if (iterator == map_.begin())
 90      return false;
 91    --iterator;
 92  
 93    const Range* range = iterator.GetValuePtr();
 94    entry = range->entryptr();
 95    if (entry_base)
 96      *entry_base = range->base();
 97    if (entry_size)
 98      *entry_size = iterator.GetKey() - range->base() + 1;
 99  
100    return true;
101  }
102  
103  template<typename AddressType, typename EntryType>
104  bool StaticRangeMap<AddressType, EntryType>::RetrieveRangeAtIndex(
105      int index, const EntryType*& entry,
106      AddressType* entry_base, AddressType* entry_size) const {
107  
108    if (index >= GetCount()) {
109      BPLOG(ERROR) << "Index out of range: " << index << "/" << GetCount();
110      return false;
111    }
112  
113    MapConstIterator iterator = map_.IteratorAtIndex(index);
114  
115    const Range* range = iterator.GetValuePtr();
116  
117    entry = range->entryptr();
118    if (entry_base)
119      *entry_base = range->base();
120    if (entry_size)
121      *entry_size = iterator.GetKey() - range->base() + 1;
122  
123    return true;
124  }
125  
126  }  // namespace google_breakpad
127  
128  
129  #endif  // PROCESSOR_STATIC_RANGE_MAP_INL_H__