/ src / processor / static_contained_range_map-inl.h
static_contained_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_contained_range_map-inl.h: Hierarchically-organized range map,
 30  // i.e., StaticContainedRangeMap implementation.
 31  //
 32  // See static_contained_range_map.h for documentation.
 33  //
 34  // Author: Siyang Xie (lambxsy@google.com)
 35  
 36  #ifndef PROCESSOR_STATIC_CONTAINED_RANGE_MAP_INL_H__
 37  #define PROCESSOR_STATIC_CONTAINED_RANGE_MAP_INL_H__
 38  
 39  #include "processor/static_contained_range_map.h"
 40  #include "processor/logging.h"
 41  
 42  namespace google_breakpad {
 43  
 44  template<typename AddressType, typename EntryType>
 45  StaticContainedRangeMap<AddressType, EntryType>::StaticContainedRangeMap(
 46      const char *base)
 47      : base_(*(reinterpret_cast<const AddressType*>(base))),
 48        entry_size_(*(reinterpret_cast<const uint32_t*>(base + sizeof(base_)))),
 49        entry_ptr_(reinterpret_cast<const EntryType*>(
 50            base + sizeof(base_) + sizeof(entry_size_))),
 51        map_(base + sizeof(base_) + sizeof(entry_size_) + entry_size_) {
 52    if (entry_size_ == 0)
 53      entry_ptr_ = NULL;
 54  }
 55  
 56  
 57  template<typename AddressType, typename EntryType>
 58  bool StaticContainedRangeMap<AddressType, EntryType>::RetrieveRange(
 59      const AddressType& address, const EntryType*& entry) const {
 60  
 61    // Get an iterator to the child range whose high address is equal to or
 62    // greater than the supplied address.  If the supplied address is higher
 63    // than all of the high addresses in the range, then this range does not
 64    // contain a child at address, so return false.  If the supplied address
 65    // is lower than the base address of the child range, then it is not within
 66    // the child range, so return false.
 67    MapConstIterator iterator = map_.lower_bound(address);
 68  
 69    if (iterator == map_.end())
 70      return false;
 71  
 72    const char *memory_child =
 73        reinterpret_cast<const char*>(iterator.GetValuePtr());
 74  
 75    StaticContainedRangeMap child_map(memory_child);
 76  
 77    if (address < child_map.base_)
 78      return false;
 79  
 80    // The child in iterator->second contains the specified address.  Find out
 81    // if it has a more-specific descendant that also contains it.  If it does,
 82    // it will set |entry| appropriately.  If not, set |entry| to the child.
 83    if (!child_map.RetrieveRange(address, entry))
 84      entry = child_map.entry_ptr_;
 85  
 86    return true;
 87  }
 88  
 89  template <typename AddressType, typename EntryType>
 90  bool StaticContainedRangeMap<AddressType, EntryType>::RetrieveRanges(
 91      const AddressType& address,
 92      std::vector<const EntryType*>& entries) const {
 93    MapConstIterator iterator = map_.lower_bound(address);
 94    if (iterator == map_.end())
 95      return false;
 96    const char* memory_child =
 97        reinterpret_cast<const char*>(iterator.GetValuePtr());
 98    StaticContainedRangeMap child_map(memory_child);
 99    if (address < child_map.base_)
100      return false;
101    child_map.RetrieveRanges(address, entries);
102    entries.push_back(child_map.entry_ptr_);
103    return true;
104  }
105  
106  }  // namespace google_breakpad
107  
108  #endif  // PROCESSOR_STATIC_CONTAINED_RANGE_MAP_INL_H__