1// Copyright (c) 2010, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8//     * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10//     * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14//     * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// static_range_map.h: StaticRangeMap.
31//
32// StaticRangeMap is similar as RangeMap.  However, StaticRangeMap wraps a
33// StaticMap instead of std::map, and does not support dynamic operations like
34// StoreRange(...).  StaticRangeMap provides same Retrieve*() interfaces as
35// RangeMap.  Please see range_map.h for more documentation.
36//
37// Author: Siyang Xie (lambxsy@google.com)
38
39#ifndef PROCESSOR_STATIC_RANGE_MAP_H__
40#define PROCESSOR_STATIC_RANGE_MAP_H__
41
42
43#include "processor/static_map-inl.h"
44
45namespace google_breakpad {
46
47// AddressType is basic type, e.g.: integer types, pointers etc
48// EntryType could be a complex type, so we retrieve its pointer instead.
49template<typename AddressType, typename EntryType>
50class StaticRangeMap {
51 public:
52  StaticRangeMap(): map_() { }
53  explicit StaticRangeMap(const char *memory): map_(memory) { }
54
55  // Locates the range encompassing the supplied address.  If there is
56  // no such range, returns false.  entry_base and entry_size, if non-NULL,
57  // are set to the base and size of the entry's range.
58  bool RetrieveRange(const AddressType &address, const EntryType *&entry,
59                     AddressType *entry_base, AddressType *entry_size) const;
60
61  // Locates the range encompassing the supplied address, if one exists.
62  // If no range encompasses the supplied address, locates the nearest range
63  // to the supplied address that is lower than the address.  Returns false
64  // if no range meets these criteria.  entry_base and entry_size, if
65  // non-NULL, are set to the base and size of the entry's range.
66  bool RetrieveNearestRange(const AddressType &address, const EntryType *&entry,
67                            AddressType *entry_base, AddressType *entry_size)
68                            const;
69
70  // Treating all ranges as a list ordered by the address spaces that they
71  // occupy, locates the range at the index specified by index.  Returns
72  // false if index is larger than the number of ranges stored.  entry_base
73  // and entry_size, if non-NULL, are set to the base and size of the entry's
74  // range.
75  //
76  // RetrieveRangeAtIndex is not optimized for speedy operation.
77  bool RetrieveRangeAtIndex(int index, const EntryType *&entry,
78                            AddressType *entry_base, AddressType *entry_size)
79                            const;
80
81  // Returns the number of ranges stored in the RangeMap.
82  inline int GetCount() const { return map_.size(); }
83
84 private:
85  friend class ModuleComparer;
86  class Range {
87   public:
88    AddressType base() const {
89      return *(reinterpret_cast<const AddressType*>(this));
90    }
91    const EntryType* entryptr() const {
92      return reinterpret_cast<const EntryType*>(this + sizeof(AddressType));
93    }
94  };
95
96  // Convenience types.
97  typedef StaticRangeMap* SelfPtr;
98  typedef StaticMap<AddressType, Range> AddressToRangeMap;
99  typedef typename AddressToRangeMap::const_iterator MapConstIterator;
100
101  AddressToRangeMap map_;
102};
103
104}  // namespace google_breakpad
105
106#endif  // PROCESSOR_STATIC_RANGE_MAP_H__
107