/ src / processor / static_range_map.h
static_range_map.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.h: StaticRangeMap.
 30  //
 31  // StaticRangeMap is similar as RangeMap.  However, StaticRangeMap wraps a
 32  // StaticMap instead of std::map, and does not support dynamic operations like
 33  // StoreRange(...).  StaticRangeMap provides same Retrieve*() interfaces as
 34  // RangeMap.  Please see range_map.h for more documentation.
 35  //
 36  // Author: Siyang Xie (lambxsy@google.com)
 37  
 38  #ifndef PROCESSOR_STATIC_RANGE_MAP_H__
 39  #define PROCESSOR_STATIC_RANGE_MAP_H__
 40  
 41  
 42  #include "processor/static_map-inl.h"
 43  
 44  namespace google_breakpad {
 45  
 46  // AddressType is basic type, e.g.: integer types, pointers etc
 47  // EntryType could be a complex type, so we retrieve its pointer instead.
 48  template<typename AddressType, typename EntryType>
 49  class StaticRangeMap {
 50   public:
 51    StaticRangeMap(): map_() { }
 52    explicit StaticRangeMap(const char* memory): map_(memory) { }
 53  
 54    // Locates the range encompassing the supplied address.  If there is
 55    // no such range, returns false.  entry_base and entry_size, if non-NULL,
 56    // are set to the base and size of the entry's range.
 57    bool RetrieveRange(const AddressType& address, const EntryType*& entry,
 58                       AddressType* entry_base, AddressType* entry_size) const;
 59  
 60    // Locates the range encompassing the supplied address, if one exists.
 61    // If no range encompasses the supplied address, locates the nearest range
 62    // to the supplied address that is lower than the address.  Returns false
 63    // if no range meets these criteria.  entry_base and entry_size, if
 64    // non-NULL, are set to the base and size of the entry's range.
 65    bool RetrieveNearestRange(const AddressType& address, const EntryType*& entry,
 66                              AddressType* entry_base, AddressType* entry_size)
 67                              const;
 68  
 69    // Treating all ranges as a list ordered by the address spaces that they
 70    // occupy, locates the range at the index specified by index.  Returns
 71    // false if index is larger than the number of ranges stored.  entry_base
 72    // and entry_size, if non-NULL, are set to the base and size of the entry's
 73    // range.
 74    //
 75    // RetrieveRangeAtIndex is not optimized for speedy operation.
 76    bool RetrieveRangeAtIndex(int index, const EntryType*& entry,
 77                              AddressType* entry_base, AddressType* entry_size)
 78                              const;
 79  
 80    // Returns the number of ranges stored in the RangeMap.
 81    inline int GetCount() const { return map_.size(); }
 82  
 83   private:
 84    friend class ModuleComparer;
 85    class Range {
 86     public:
 87      AddressType base() const {
 88        return *(reinterpret_cast<const AddressType*>(this));
 89      }
 90      const EntryType* entryptr() const {
 91        return reinterpret_cast<const EntryType*>(this + sizeof(AddressType));
 92      }
 93    };
 94  
 95    // Convenience types.
 96    typedef StaticRangeMap* SelfPtr;
 97    typedef StaticMap<AddressType, Range> AddressToRangeMap;
 98    typedef typename AddressToRangeMap::const_iterator MapConstIterator;
 99  
100    AddressToRangeMap map_;
101  };
102  
103  }  // namespace google_breakpad
104  
105  #endif  // PROCESSOR_STATIC_RANGE_MAP_H__