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_contained_range_map.h: StaticContainedRangeMap.
31//
32// StaticContainedRangeMap is similar to ContainedRangeMap. However,
33// StaticContainedRangeMap wraps a StaticMap instead of std::map, and does not
34// support dynamic operations like StoreRange(...).
35// StaticContainedRangeMap provides same RetrieveRange(...) interfaces as
36// ContainedRangeMap.
37//
38// Please see contained_range_map.h for more documentation.
39//
40// Author: Siyang Xie (lambxsy@google.com)
41
42#ifndef PROCESSOR_STATIC_CONTAINED_RANGE_MAP_H__
43#define PROCESSOR_STATIC_CONTAINED_RANGE_MAP_H__
44
45#include <vector>
46#include "processor/static_map-inl.h"
47
48namespace google_breakpad {
49
50template<typename AddressType, typename EntryType>
51class StaticContainedRangeMap {
52 public:
53 StaticContainedRangeMap(): base_(), entry_size_(), entry_ptr_(), map_() { }
54 explicit StaticContainedRangeMap(const char *base);
55
56 // Retrieves the most specific (smallest) descendant range encompassing
57 // the specified address. This method will only return entries held by
58 // child ranges, and not the entry contained by |this|. This is necessary
59 // to support a sparsely-populated root range. If no descendant range
60 // encompasses the address, returns false.
61 bool RetrieveRange(const AddressType& address, const EntryType*& entry) const;
62
63 // Retrieves the vector of entries encompassing the specified address from the
64 // innermost entry to the outermost entry.
65 bool RetrieveRanges(const AddressType& address,
66 std::vector<const EntryType*>& entry) const;
67
68 private:
69 friend class ModuleComparer;
70 // AddressToRangeMap stores pointers. This makes reparenting simpler in
71 // StoreRange, because it doesn't need to copy entire objects.
72 typedef StaticContainedRangeMap* SelfPtr;
73 typedef
74 StaticMap<AddressType, StaticContainedRangeMap> AddressToRangeMap;
75 typedef typename AddressToRangeMap::const_iterator MapConstIterator;
76
77 // The base address of this range. The high address does not need to
78 // be stored, because it is used as the key to an object in its parent's
79 // map, and all ContainedRangeMaps except for the root range are contained
80 // within maps. The root range does not actually contain an entry, so its
81 // base_ field is meaningless, and the fact that it has no parent and thus
82 // no key is unimportant. For this reason, the base_ field should only be
83 // is accessed on child ContainedRangeMap objects, and never on |this|.
84 AddressType base_;
85
86 // The entry corresponding to this range. The root range does not
87 // actually contain an entry, so its entry_ field is meaningless. For
88 // this reason, the entry_ field should only be accessed on child
89 // ContainedRangeMap objects, and never on |this|.
90 uint32_t entry_size_;
91 const EntryType *entry_ptr_;
92
93 // The map containing child ranges, keyed by each child range's high
94 // address. This is a pointer to avoid allocating map structures for
95 // leaf nodes, where they are not needed.
96 AddressToRangeMap map_;
97};
98
99} // namespace google_breakpad
100
101
102#endif // PROCESSOR_STATIC_CONTAINED_RANGE_MAP_H__
103